Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package auth
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestHashPassword(t *testing.T) {
|
|
const pw = "correct horse battery"
|
|
h, err := HashPassword(pw)
|
|
if err != nil {
|
|
t.Fatalf("HashPassword(%q) failed: %v", pw, err)
|
|
}
|
|
ok, rehash, err := VerifyPassword(h, pw)
|
|
if err != nil || !ok || rehash {
|
|
t.Errorf("VerifyPassword(h, %q) = %v, %v, %v; want true, false, nil", pw, ok, rehash, err)
|
|
}
|
|
if ok, _, _ := VerifyPassword(h, pw+"x"); ok {
|
|
t.Errorf("VerifyPassword(h, %q) = true, want false", pw+"x")
|
|
}
|
|
if _, err := HashPassword("short"); !errors.Is(err, ErrPasswordTooShort) {
|
|
t.Errorf("HashPassword(%q) error = %v, want %v", "short", err, ErrPasswordTooShort)
|
|
}
|
|
}
|
|
|
|
func TestVerifyPasswordRequestsRehashForOldParams(t *testing.T) {
|
|
const pw = "correct horse battery"
|
|
weak := argonParams{memory: 8 * 1024, time: 1, threads: 1, keyLen: 32}
|
|
ok, rehash, err := VerifyPassword(hashPassword(pw, weak), pw)
|
|
if err != nil || !ok || !rehash {
|
|
t.Errorf("VerifyPassword(weak hash) = %v, %v, %v; want true, true, nil", ok, rehash, err)
|
|
}
|
|
}
|
|
|
|
func TestVerifyPasswordRejectsForeignFormats(t *testing.T) {
|
|
for _, h := range []string{"", "$2a$10$abc", strings.Replace(dummyHash(), "argon2id", "argon2i", 1)} {
|
|
if _, _, err := VerifyPassword(h, "x"); err == nil {
|
|
t.Errorf("VerifyPassword(%q, _) error = nil, want error", h)
|
|
}
|
|
}
|
|
}
|