forked from mtusi2026/mtusi2026lab2
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
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")
|
|
}
|
|
}
|