38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
package password
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestPasswordRoundTrip verifies long Unicode input without bcrypt-style truncation.
|
|
func TestPasswordRoundTrip(t *testing.T) {
|
|
password := strings.Repeat("🔐", 128)
|
|
hasher := Hasher{}
|
|
encoded, err := hasher.Hash(password)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !hasher.Verify(encoded, password) || hasher.Verify(encoded, password+"x") {
|
|
t.Fatal("password verification mismatch")
|
|
}
|
|
second, err := hasher.Hash(password)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if encoded == second {
|
|
t.Fatal("salt reused")
|
|
}
|
|
}
|
|
|
|
// TestPasswordRejectsInvalidEncoding checks corrupt or unbounded KDF descriptions.
|
|
func TestPasswordRejectsInvalidEncoding(t *testing.T) {
|
|
for _, encoded := range []string{"", "$argon2id$v=19$m=999999999,t=2,p=1$a$b", prefix + "x", prefix + "%%%$abc", prefix + "YWJj$abc", prefix + "AAAAAAAAAAAAAAAAAAAAAA$%%%", prefix + "AAAAAAAAAAAAAAAAAAAAAA$YWJj"} {
|
|
t.Run(encoded, func(t *testing.T) {
|
|
if (Hasher{}).Verify(encoded, "password") {
|
|
t.Fatal("invalid encoding accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|