test: report failures as got/want and use subtests for tables
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
d20225982a
commit
b6843330a8
8 files changed
+184
-175
No files matched your search
@@ -1,41 +1,42 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPasswordHash(t *testing.T) {
|
||||
h, err := HashPassword("correct horse battery")
|
||||
func TestHashPassword(t *testing.T) {
|
||||
const pw = "correct horse battery"
|
||||
h, err := HashPassword(pw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Fatalf("HashPassword(%q) failed: %v", pw, 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)
|
||||
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, "correct horse batterz"); ok {
|
||||
t.Fatal("wrong password accepted")
|
||||
if ok, _, _ := VerifyPassword(h, pw+"x"); ok {
|
||||
t.Errorf("VerifyPassword(h, %q) = true, want false", pw+"x")
|
||||
}
|
||||
if _, err := HashPassword("short"); err != ErrPasswordTooShort {
|
||||
t.Fatalf("got %v", err)
|
||||
if _, err := HashPassword("short"); !errors.Is(err, ErrPasswordTooShort) {
|
||||
t.Errorf("HashPassword(%q) error = %v, want %v", "short", err, ErrPasswordTooShort)
|
||||
}
|
||||
}
|
||||
|
||||
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 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 TestPasswordHashRejectsForeignFormats(t *testing.T) {
|
||||
for _, h := range []string{"", "$2a$10$abc", strings.Replace(dummyHash, "argon2id", "argon2i", 1)} {
|
||||
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("accepted %q", h)
|
||||
t.Errorf("VerifyPassword(%q, _) error = nil, want error", h)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user