This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func BenchmarkHashPasswordGrade3(b *testing.B) {
|
||||
const password = "benchmark-password-123"
|
||||
for index := 0; index < b.N; index++ {
|
||||
if _, err := HashPassword(password); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkVerifyPasswordGrade4(b *testing.B) {
|
||||
const password = "benchmark-password-123"
|
||||
encodedHash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.ResetTimer()
|
||||
for index := 0; index < b.N; index++ {
|
||||
matches, verifyErr := VerifyPassword(password, encodedHash)
|
||||
if verifyErr != nil || !matches {
|
||||
b.Fatalf("verify failed: matches=%v err=%v", matches, verifyErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashPasswordRejectsWeakConfiguredCost(t *testing.T) {
|
||||
t.Setenv("ARGON2_MEMORY_KIB", "8192")
|
||||
if _, err := HashPassword("valid-password-123"); err == nil {
|
||||
t.Fatal("HashPassword accepted memory below the lab7 minimum")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPasswordHashAndVerify(t *testing.T) {
|
||||
password := "Correct-password-123"
|
||||
|
||||
encodedHash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("HashPassword returned error: %v", err)
|
||||
}
|
||||
|
||||
if strings.Contains(encodedHash, password) {
|
||||
t.Fatal("encoded hash contains plaintext password")
|
||||
}
|
||||
|
||||
matches, err := VerifyPassword(password, encodedHash)
|
||||
if err != nil {
|
||||
t.Fatalf("VerifyPassword returned error: %v", err)
|
||||
}
|
||||
if !matches {
|
||||
t.Fatal("correct password did not match")
|
||||
}
|
||||
|
||||
matches, err = VerifyPassword("Wrong-password-123", encodedHash)
|
||||
if err != nil {
|
||||
t.Fatalf("VerifyPassword returned error: %v", err)
|
||||
}
|
||||
if matches {
|
||||
t.Fatal("wrong password matched")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPasswordHashesUseDifferentSalts(t *testing.T) {
|
||||
password := "Correct-password-123"
|
||||
|
||||
firstHash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("first HashPassword returned error: %v", err)
|
||||
}
|
||||
|
||||
secondHash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("second HashPassword returned error: %v", err)
|
||||
}
|
||||
|
||||
if firstHash == secondHash {
|
||||
t.Fatal("equal passwords produced equal encoded hashes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPasswordValidation(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
password string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "minimum length",
|
||||
password: "123456789012",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "unicode",
|
||||
password: "пароль-проверка-123",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "colon is allowed",
|
||||
password: "password:with:colon",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "too short",
|
||||
password: "short",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
err := ValidatePassword(testCase.password)
|
||||
|
||||
if testCase.wantErr && err == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
if !testCase.wantErr && err != nil {
|
||||
t.Fatalf("unexpected validation error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMalformedPasswordHash(t *testing.T) {
|
||||
_, err := VerifyPassword(
|
||||
"Correct-password-123",
|
||||
"not-an-argon2-hash",
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatal("expected malformed hash error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNeedsPasswordRehash(t *testing.T) {
|
||||
currentHash, err := HashPassword("Correct-password-123")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
needsRehash, err := NeedsPasswordRehash(currentHash)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if needsRehash {
|
||||
t.Fatal("current policy hash unexpectedly requires rehash")
|
||||
}
|
||||
|
||||
legacyHash := strings.Replace(currentHash, "m=19456,t=2,p=1", "m=8192,t=2,p=1", 1)
|
||||
needsRehash, err = NeedsPasswordRehash(legacyHash)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !needsRehash {
|
||||
t.Fatal("legacy policy hash was not marked for rehash")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user