Files
lincodex/backend/internal/usecase/identity/credentials_test.go
T

134 lines
4.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package identity
import (
"bytes"
"context"
"errors"
"log/slog"
"strings"
"testing"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
const testPasswordPHC = "$argon2id$v=19$m=65536,t=3,p=4$c2FsdHNhbHRzYWx0c2FsdA$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaGhhc2g"
// countingHasher records calls so a test can prove that validation stops a
// password before any hashing.
type countingHasher struct {
hashCalls int
hashed domain.Password
verifyCalls int
dummyCalls int
verified domain.Password
stored domain.PasswordHash
verifyErr error
}
func (hasher *countingHasher) Hash(_ context.Context, password domain.Password) (domain.PasswordHash, error) {
hasher.hashCalls++
hasher.hashed = password
return domain.NewPasswordHash(testPasswordPHC)
}
func (hasher *countingHasher) Verify(_ context.Context, password domain.Password, stored domain.PasswordHash) (bool, error) {
hasher.verifyCalls++
hasher.verified = password
hasher.stored = stored
return false, hasher.verifyErr
}
func (hasher *countingHasher) VerifyDummy(context.Context, domain.Password) error {
hasher.dummyCalls++
return outbound.ErrPasswordMismatch
}
var _ outbound.PasswordHasher = (*countingHasher)(nil)
func TestHashNewPasswordRefusesAnOversizedPasswordBeforeHashing(t *testing.T) {
const marker = "do-not-log-this-password-"
tests := map[string]string{
"129 code points": marker + strings.Repeat("x", 129-len(marker)),
"over the raw byte cap": marker + strings.Repeat("\U0001F600", 200),
"one megabyte": marker + strings.Repeat("y", 1<<20),
"too long after normalization": marker + strings.Repeat("fi", 60),
}
for name, raw := range tests {
hasher := &countingHasher{}
var logs bytes.Buffer
// The handling boundary logs the error as an HTTP handler would.
logger := slog.New(slog.NewJSONHandler(&logs, nil))
_, err := NewCredentials(hasher).HashNewPassword(context.Background(), raw)
logger.Error("registration refused", "error", err)
if !errors.Is(err, domain.ErrInvalidPassword) {
t.Fatalf("HashNewPassword(%s) error = %v, want ErrInvalidPassword", name, err)
}
if hasher.hashCalls != 0 {
t.Fatalf("HashNewPassword(%s) called the hasher %d times, want none", name, hasher.hashCalls)
}
if strings.Contains(logs.String(), marker) || strings.Contains(err.Error(), marker) {
t.Fatalf("HashNewPassword(%s) exposed the input in the error or the log: %s", name, logs.String())
}
}
}
func TestHashNewPasswordRefusesAShortPasswordBeforeHashing(t *testing.T) {
hasher := &countingHasher{}
_, err := NewCredentials(hasher).HashNewPassword(context.Background(), "fourteen chars")
if !errors.Is(err, domain.ErrInvalidPassword) || hasher.hashCalls != 0 {
t.Fatalf("HashNewPassword(14 code points) = %v after %d hash calls, want ErrInvalidPassword and no call", err, hasher.hashCalls)
}
}
func TestHashNewPasswordHashesTheNormalizedPassword(t *testing.T) {
hasher := &countingHasher{}
if _, err := NewCredentials(hasher).HashNewPassword(context.Background(), "correct horse battery"); err != nil {
t.Fatalf("HashNewPassword() error = %v", err)
}
if hasher.hashCalls != 1 || hasher.hashed.Reveal() != "correct horse battery" {
t.Fatalf("hasher received %d calls, want one with the NFKC form", hasher.hashCalls)
}
}
func TestVerifyLoginPasswordUsesLoginBoundsAndNFKC(t *testing.T) {
hasher := &countingHasher{}
stored, err := domain.NewPasswordHash(testPasswordPHC)
if err != nil {
t.Fatal(err)
}
if err := NewCredentials(hasher).VerifyLoginPassword(context.Background(), "x", stored); err != nil {
t.Fatalf("VerifyLoginPassword() error = %v", err)
}
if hasher.verifyCalls != 1 || hasher.verified.Reveal() != "x" || hasher.stored.PHC() != stored.PHC() {
t.Fatalf("Verify calls = %d, password = %q, stored hash matched = %v", hasher.verifyCalls, hasher.verified.Reveal(), hasher.stored.PHC() == stored.PHC())
}
}
func TestVerifyLoginPasswordRejectsEmptyInputBeforeKDF(t *testing.T) {
hasher := &countingHasher{}
stored, err := domain.NewPasswordHash(testPasswordPHC)
if err != nil {
t.Fatal(err)
}
err = NewCredentials(hasher).VerifyLoginPassword(context.Background(), "", stored)
if !errors.Is(err, domain.ErrInvalidPassword) || hasher.verifyCalls != 0 {
t.Fatalf("VerifyLoginPassword(empty) = %v after %d verify calls, want ErrInvalidPassword before KDF", err, hasher.verifyCalls)
}
}
func TestVerifyDummyLoginPasswordAlwaysRunsDummyHashWork(t *testing.T) {
hasher := &countingHasher{}
credentials := NewCredentials(hasher)
for _, password := range []string{"correct horse battery staple", ""} {
if err := credentials.VerifyDummyLoginPassword(context.Background(), password); !errors.Is(err, outbound.ErrPasswordMismatch) {
t.Fatalf("VerifyDummyLoginPassword() error = %v, want ErrPasswordMismatch", err)
}
}
if hasher.dummyCalls != 2 || hasher.verifyCalls != 0 {
t.Fatalf("dummy calls=%d normal calls=%d, want two dummy calls", hasher.dummyCalls, hasher.verifyCalls)
}
}