|
| 1 | +package encryption |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/ed25519" |
| 5 | + "crypto/rand" |
| 6 | + "encoding/hex" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + // Test data shared across tests |
| 13 | + testValidKey ed25519.PublicKey |
| 14 | + testValidHex string |
| 15 | + testAllZeros = make([]byte, 32) |
| 16 | + testAllMax = func() []byte { |
| 17 | + b := make([]byte, 32) |
| 18 | + for i := range b { |
| 19 | + b[i] = 0xFF |
| 20 | + } |
| 21 | + return b |
| 22 | + }() |
| 23 | +) |
| 24 | + |
| 25 | +func init() { |
| 26 | + // Generate a single valid key for all tests |
| 27 | + testValidKey, _, _ = ed25519.GenerateKey(rand.Reader) |
| 28 | + testValidHex = hex.EncodeToString(testValidKey) |
| 29 | +} |
| 30 | + |
| 31 | +// Helper function to check error expectations |
| 32 | +func checkError(t *testing.T, err error, wantError bool, errorMsg string) { |
| 33 | + t.Helper() |
| 34 | + if wantError { |
| 35 | + if err == nil { |
| 36 | + t.Errorf("expected error but got none") |
| 37 | + } else if errorMsg != "" && !strings.Contains(err.Error(), errorMsg) { |
| 38 | + t.Errorf("error = %v, want error containing %v", err, errorMsg) |
| 39 | + } |
| 40 | + } else if err != nil { |
| 41 | + t.Errorf("unexpected error = %v", err) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestParseEd25519PublicKeyFromHex(t *testing.T) { |
| 46 | + tests := []struct { |
| 47 | + name string |
| 48 | + hexKey string |
| 49 | + wantError bool |
| 50 | + errorMsg string |
| 51 | + }{ |
| 52 | + {"valid hex key", testValidHex, false, ""}, |
| 53 | + {"empty hex string", "", true, "public key hex string is empty"}, |
| 54 | + {"invalid hex characters", strings.Repeat("g", 64), true, "invalid hex format"}, |
| 55 | + {"too short hex string", "abcdef1234567890", true, "invalid Ed25519 public key length: expected 32 bytes, got 8"}, |
| 56 | + {"too long hex string", strings.Repeat("ab", 40), true, "invalid Ed25519 public key length: expected 32 bytes, got 40"}, |
| 57 | + {"odd length hex string", "abc", true, "invalid hex format"}, |
| 58 | + {"all zeros", hex.EncodeToString(testAllZeros), false, ""}, |
| 59 | + {"all max bytes", hex.EncodeToString(testAllMax), false, ""}, |
| 60 | + } |
| 61 | + |
| 62 | + for _, tt := range tests { |
| 63 | + t.Run(tt.name, func(t *testing.T) { |
| 64 | + result, err := ParseEd25519PublicKeyFromHex(tt.hexKey) |
| 65 | + |
| 66 | + checkError(t, err, tt.wantError, tt.errorMsg) |
| 67 | + |
| 68 | + if !tt.wantError { |
| 69 | + if result == nil { |
| 70 | + t.Errorf("expected non-nil result") |
| 71 | + } else if len(result) != ed25519.PublicKeySize { |
| 72 | + t.Errorf("result length = %d, want %d", len(result), ed25519.PublicKeySize) |
| 73 | + } |
| 74 | + } else if result != nil { |
| 75 | + t.Errorf("expected nil result on error, got %v", result) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestValidateEd25519PublicKey(t *testing.T) { |
| 82 | + tests := []struct { |
| 83 | + name string |
| 84 | + keyBytes []byte |
| 85 | + wantError bool |
| 86 | + errorMsg string |
| 87 | + }{ |
| 88 | + {"valid public key", testValidKey, false, ""}, |
| 89 | + {"nil key bytes", nil, true, "invalid Ed25519 public key length: expected 32 bytes, got 0"}, |
| 90 | + {"empty key bytes", []byte{}, true, "invalid Ed25519 public key length: expected 32 bytes, got 0"}, |
| 91 | + {"too short key", make([]byte, 16), true, "invalid Ed25519 public key length: expected 32 bytes, got 16"}, |
| 92 | + {"too long key", make([]byte, 64), true, "invalid Ed25519 public key length: expected 32 bytes, got 64"}, |
| 93 | + {"all zeros", testAllZeros, false, ""}, |
| 94 | + {"all max bytes", testAllMax, false, ""}, |
| 95 | + } |
| 96 | + |
| 97 | + for _, tt := range tests { |
| 98 | + t.Run(tt.name, func(t *testing.T) { |
| 99 | + err := ValidateEd25519PublicKey(tt.keyBytes) |
| 100 | + checkError(t, err, tt.wantError, tt.errorMsg) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestParseAndValidateIntegration(t *testing.T) { |
| 106 | + testKeys := []ed25519.PublicKey{testValidKey} |
| 107 | + |
| 108 | + // Generate a few more keys for testing |
| 109 | + for i := 0; i < 3; i++ { |
| 110 | + pubKey, _, err := ed25519.GenerateKey(rand.Reader) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("Failed to generate test key %d: %v", i, err) |
| 113 | + } |
| 114 | + testKeys = append(testKeys, pubKey) |
| 115 | + } |
| 116 | + |
| 117 | + for i, validPubKey := range testKeys { |
| 118 | + validHex := hex.EncodeToString(validPubKey) |
| 119 | + |
| 120 | + parsedKey, err := ParseEd25519PublicKeyFromHex(validHex) |
| 121 | + if err != nil { |
| 122 | + t.Errorf("ParseEd25519PublicKeyFromHex() failed for key %d: %v", i, err) |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + if err := ValidateEd25519PublicKey(parsedKey); err != nil { |
| 127 | + t.Errorf("ValidateEd25519PublicKey() failed for key %d: %v", i, err) |
| 128 | + } |
| 129 | + |
| 130 | + if !compareBytes(validPubKey, parsedKey) { |
| 131 | + t.Errorf("Key %d: parsed key differs from original", i) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// Helper function to compare byte slices |
| 137 | +func compareBytes(a, b []byte) bool { |
| 138 | + if len(a) != len(b) { |
| 139 | + return false |
| 140 | + } |
| 141 | + for i, v := range a { |
| 142 | + if v != b[i] { |
| 143 | + return false |
| 144 | + } |
| 145 | + } |
| 146 | + return true |
| 147 | +} |
| 148 | + |
| 149 | +func BenchmarkParseEd25519PublicKeyFromHex(b *testing.B) { |
| 150 | + for i := 0; i < b.N; i++ { |
| 151 | + _, _ = ParseEd25519PublicKeyFromHex(testValidHex) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func BenchmarkValidateEd25519PublicKey(b *testing.B) { |
| 156 | + for i := 0; i < b.N; i++ { |
| 157 | + _ = ValidateEd25519PublicKey(testValidKey) |
| 158 | + } |
| 159 | +} |
0 commit comments