|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "encoding/base64" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func validKey(t *testing.T) string { |
| 11 | + t.Helper() |
| 12 | + raw := make([]byte, 32) |
| 13 | + if _, err := rand.Read(raw); err != nil { |
| 14 | + t.Fatal(err) |
| 15 | + } |
| 16 | + return base64.StdEncoding.EncodeToString(raw) |
| 17 | +} |
| 18 | + |
| 19 | +func TestValidateKey_Valid(t *testing.T) { |
| 20 | + encoded := validKey(t) |
| 21 | + key, err := ValidateKey("TEST_KEY", encoded) |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("expected no error, got: %v", err) |
| 24 | + } |
| 25 | + if len(key) != 32 { |
| 26 | + t.Fatalf("expected 32 bytes, got %d", len(key)) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestValidateKey_Empty(t *testing.T) { |
| 31 | + _, err := ValidateKey("ENCRYPTION_KEY", "") |
| 32 | + if err == nil { |
| 33 | + t.Fatal("expected error for empty key") |
| 34 | + } |
| 35 | + if !strings.Contains(err.Error(), "ENCRYPTION_KEY is not set") { |
| 36 | + t.Fatalf("error should mention env var name, got: %v", err) |
| 37 | + } |
| 38 | + if !strings.Contains(err.Error(), "openssl rand -base64 32") { |
| 39 | + t.Fatalf("error should include generation hint, got: %v", err) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestValidateKey_InvalidBase64(t *testing.T) { |
| 44 | + _, err := ValidateKey("STATE_KEY", "not!!valid!!base64$$") |
| 45 | + if err == nil { |
| 46 | + t.Fatal("expected error for invalid base64") |
| 47 | + } |
| 48 | + if !strings.Contains(err.Error(), "STATE_KEY is not valid base64") { |
| 49 | + t.Fatalf("error should mention invalid base64, got: %v", err) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestValidateKey_WrongLength_Short(t *testing.T) { |
| 54 | + short := base64.StdEncoding.EncodeToString(make([]byte, 16)) |
| 55 | + _, err := ValidateKey("ENCRYPTION_KEY", short) |
| 56 | + if err == nil { |
| 57 | + t.Fatal("expected error for 16-byte key") |
| 58 | + } |
| 59 | + if !strings.Contains(err.Error(), "16 bytes") { |
| 60 | + t.Fatalf("error should report actual length, got: %v", err) |
| 61 | + } |
| 62 | + if !strings.Contains(err.Error(), "expected exactly 32") { |
| 63 | + t.Fatalf("error should state expected length, got: %v", err) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestValidateKey_WrongLength_Long(t *testing.T) { |
| 68 | + long := base64.StdEncoding.EncodeToString(make([]byte, 64)) |
| 69 | + _, err := ValidateKey("ENCRYPTION_KEY", long) |
| 70 | + if err == nil { |
| 71 | + t.Fatal("expected error for 64-byte key") |
| 72 | + } |
| 73 | + if !strings.Contains(err.Error(), "64 bytes") { |
| 74 | + t.Fatalf("error should report actual length, got: %v", err) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestKeyFingerprint(t *testing.T) { |
| 79 | + raw := make([]byte, 32) |
| 80 | + for i := range raw { |
| 81 | + raw[i] = byte(i) |
| 82 | + } |
| 83 | + fp := KeyFingerprint(raw) |
| 84 | + encoded := base64.StdEncoding.EncodeToString(raw) |
| 85 | + |
| 86 | + if !strings.HasPrefix(encoded, strings.TrimSuffix(fp, "...")) { |
| 87 | + t.Fatalf("fingerprint %q should be prefix of full encoding %q", fp, encoded) |
| 88 | + } |
| 89 | + if !strings.HasSuffix(fp, "...") { |
| 90 | + t.Fatalf("fingerprint should end with ellipsis, got: %q", fp) |
| 91 | + } |
| 92 | + if len(fp) != 11 { // 8 chars + "..." |
| 93 | + t.Fatalf("fingerprint should be 11 chars (8 + ...), got %d: %q", len(fp), fp) |
| 94 | + } |
| 95 | +} |
0 commit comments