package auth import "testing" func TestPasswordHashRoundTrip(t *testing.T) { hash, err := HashPassword("correct horse battery staple") if err != nil { t.Fatal(err) } if !VerifyPassword(hash, "correct horse battery staple") { t.Fatal("valid password was rejected") } if VerifyPassword(hash, "wrong password") { t.Fatal("invalid password was accepted") } if VerifyPassword("not-a-hash", "anything") { t.Fatal("malformed hash was accepted") } } func TestPasswordPolicy(t *testing.T) { if err := ValidatePassword("short"); err == nil { t.Fatal("short password accepted") } if err := ValidatePassword("this password is long enough"); err != nil { t.Fatal(err) } if err := ValidatePassword(string(make([]byte, 257))); err == nil { t.Fatal("overlong password accepted") } } func TestPasswordSpecialCharacters(t *testing.T) { password := "p@ssw0rd!with spaces, punctuation: #$%^&*()_+-=[]{};'\"<>/?\\|`~" if err := ValidatePassword(password); err != nil { t.Fatalf("special-character password rejected: %v", err) } hash, err := HashPassword(password) if err != nil { t.Fatal(err) } if !VerifyPassword(hash, password) { t.Fatal("special-character password did not verify") } if VerifyPassword(hash, "p@ssw0rd") { t.Fatal("different password accepted") } }