Refs #3 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPasswordHash(t *testing.T) {
|
|
h, err := HashPassword("correct horse battery")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, rehash, err := VerifyPassword(h, "correct horse battery"); !ok || rehash || err != nil {
|
|
t.Fatalf("ok=%v rehash=%v err=%v", ok, rehash, err)
|
|
}
|
|
if ok, _, _ := VerifyPassword(h, "correct horse batterz"); ok {
|
|
t.Fatal("wrong password accepted")
|
|
}
|
|
if _, err := HashPassword("short"); err != ErrPasswordTooShort {
|
|
t.Fatalf("got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPasswordRehashOnWeakerParams(t *testing.T) {
|
|
saved := current
|
|
current = argonParams{memory: 8 * 1024, time: 1, threads: 1, keyLen: 32}
|
|
h, _ := HashPassword("correct horse battery")
|
|
current = saved
|
|
ok, rehash, err := VerifyPassword(h, "correct horse battery")
|
|
if !ok || !rehash || err != nil {
|
|
t.Fatalf("ok=%v rehash=%v err=%v", ok, rehash, err)
|
|
}
|
|
}
|
|
|
|
func TestPasswordHashRejectsForeignFormats(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("accepted %q", h)
|
|
}
|
|
}
|
|
}
|