Spaces:
Running
Running
File size: 1,714 Bytes
84ffb7a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | package auth
import "testing"
func TestHashAndCheckPassword(t *testing.T) {
hash, err := HashPassword("s3cret-pass")
if err != nil {
t.Fatalf("HashPassword: %v", err)
}
if hash == "s3cret-pass" {
t.Fatal("hash must not equal plaintext")
}
if !CheckPassword(hash, "s3cret-pass") {
t.Fatal("expected correct password to verify")
}
if CheckPassword(hash, "wrong-pass") {
t.Fatal("expected wrong password to fail")
}
}
func TestHashPasswordCost(t *testing.T) {
// bcrypt output must be 60 chars.
hash, err := HashPassword("x")
if err != nil {
t.Fatalf("HashPassword: %v", err)
}
if len(hash) != 60 {
t.Fatalf("expected 60-char bcrypt hash, got %d", len(hash))
}
}
func TestNewTokenUniqueness(t *testing.T) {
seen := map[string]bool{}
for i := 0; i < 100; i++ {
tok, err := NewToken()
if err != nil {
t.Fatalf("NewToken: %v", err)
}
if len(tok) < 32 {
t.Fatalf("token too short: %d", len(tok))
}
if seen[tok] {
t.Fatal("duplicate token generated")
}
seen[tok] = true
}
}
func TestHashTokenIsDeterministicDigest(t *testing.T) {
a := HashToken("abc123")
b := HashToken("abc123")
if a != b {
t.Fatal("same input must hash identically")
}
if HashToken("abc123") == HashToken("abc124") {
t.Fatal("different inputs must differ")
}
if a == "abc123" {
t.Fatal("token must be hashed, not stored raw")
}
}
func TestConstantTimeEqual(t *testing.T) {
if !ConstantTimeEqual("token", "token") {
t.Fatal("equal strings must compare true")
}
if ConstantTimeEqual("token", "tokem") {
t.Fatal("different strings must compare false")
}
if ConstantTimeEqual("", "x") || ConstantTimeEqual("x", "") {
t.Fatal("empty vs non-empty must compare false")
}
}
|