forked from mtusi2026/mtusi2026lab5
chore(migrate) moved codebase from lab4
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPasswordRoundTripPreservesFullRange(t *testing.T) {
|
||||
passwords := []string{
|
||||
"simple-password-123",
|
||||
"colon:inside:password",
|
||||
"пароль-с-кириллицей-12",
|
||||
strings.Repeat("😀", 128),
|
||||
}
|
||||
for _, password := range passwords {
|
||||
hash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("HashPassword: %v", err)
|
||||
}
|
||||
ok, err := VerifyPassword(hash, password)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("VerifyPassword rejected the correct password (ok=%v err=%v)", ok, err)
|
||||
}
|
||||
ok, err = VerifyPassword(hash, password+"x")
|
||||
if err != nil || ok {
|
||||
t.Fatalf("VerifyPassword accepted a different password (ok=%v err=%v)", ok, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPasswordHashIsSalted(t *testing.T) {
|
||||
first, err := HashPassword("same-password-123")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := HashPassword("same-password-123")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first == second {
|
||||
t.Fatal("two hashes of the same password are identical")
|
||||
}
|
||||
if _, err := VerifyPassword("not-a-hash", "same-password-123"); err == nil {
|
||||
t.Fatal("invalid encoded hash was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyPasswordRejectsMalformedCostAndEmptyDigest(t *testing.T) {
|
||||
malformed := []string{
|
||||
// Empty digest would make argon2 derive a zero-length key.
|
||||
"$argon2id$v=19$m=65536,t=2,p=4$c2FsdA$",
|
||||
// Zero cost parameters are outside argon2's supported range and would
|
||||
// otherwise panic inside the KDF.
|
||||
"$argon2id$v=19$m=65536,t=0,p=4$c2FsdA$YWJjZA",
|
||||
"$argon2id$v=19$m=65536,t=2,p=0$c2FsdA$YWJjZA",
|
||||
"$argon2id$v=19$m=1,t=2,p=4$c2FsdA$YWJjZA",
|
||||
}
|
||||
for _, encoded := range malformed {
|
||||
ok, err := VerifyPassword(encoded, "same-password-123")
|
||||
if err == nil || ok {
|
||||
t.Fatalf("VerifyPassword(%q) = (%v, %v), want ErrInvalidHash", encoded, ok, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user