package identity import ( "strings" "testing" ) func TestPasswordVerification(t *testing.T) { hash, err := HashPassword("a sufficiently long password") if err != nil { t.Fatal(err) } for _, tc := range []struct { name, password, hash string want bool }{ {"correct", "a sufficiently long password", hash, true}, {"wrong", "a different long password", hash, false}, {"malformed", "a sufficiently long password", "bad", false}, {"unbounded parameters", "password", "$argon2id$v=19$m=999999999,t=2,p=1$c2FsdA$aGFzaA", false}, } { t.Run(tc.name, func(t *testing.T) { if got := VerifyPassword(tc.password, tc.hash); got != tc.want { t.Fatalf("verification = %v, want %v", got, tc.want) } }) } for _, password := range []string{"short", strings.Repeat("a", 1025), "123456789012345", "passwordpassword"} { if _, err := HashPassword(password); err == nil { t.Fatal("accepted invalid password length") } } second, err := HashPassword("a sufficiently long password") if err != nil { t.Fatal(err) } if hash == second { t.Fatal("password hashes reuse salts") } }