1+ package crypto
2+
3+ import (
4+ "testing"
5+ )
6+
7+ // --- DeriveKey ---
8+
9+ func TestDeriveKeyProducesDeterministicOutput (t * testing.T ) {
10+ salt := []byte ("1234567890123456" )
11+ key1 := DeriveKey ("mypassword" , salt )
12+ key2 := DeriveKey ("mypassword" , salt )
13+
14+ if string (key1 ) != string (key2 ) {
15+ t .Fatal ("DeriveKey should produce the same key for the same password and salt" )
16+ }
17+ }
18+
19+ func TestDeriveKeyDiffersWithDifferentPassword (t * testing.T ) {
20+ salt := []byte ("1234567890123456" )
21+ key1 := DeriveKey ("password1" , salt )
22+ key2 := DeriveKey ("password2" , salt )
23+
24+ if string (key1 ) == string (key2 ) {
25+ t .Fatal ("DeriveKey should produce different keys for different passwords" )
26+ }
27+ }
28+
29+ func TestDeriveKeyDiffersWithDifferentSalt (t * testing.T ) {
30+ key1 := DeriveKey ("mypassword" , []byte ("salt1salt1salt1s" ))
31+ key2 := DeriveKey ("mypassword" , []byte ("salt2salt2salt2s" ))
32+
33+ if string (key1 ) == string (key2 ) {
34+ t .Fatal ("DeriveKey should produce different keys for different salts" )
35+ }
36+ }
37+
38+ func TestDeriveKeyLength (t * testing.T ) {
39+ salt := []byte ("1234567890123456" )
40+ key := DeriveKey ("mypassword" , salt )
41+
42+ if len (key ) != 32 {
43+ t .Fatalf ("expected key length 32, got %d" , len (key ))
44+ }
45+ }
46+
47+ // --- GenerateSalt ---
48+
49+ func TestGenerateSaltLength (t * testing.T ) {
50+ salt , err := GenerateSalt ()
51+ if err != nil {
52+ t .Fatalf ("unexpected error: %v" , err )
53+ }
54+ if len (salt ) != 16 {
55+ t .Fatalf ("expected salt length 16, got %d" , len (salt ))
56+ }
57+ }
58+
59+ func TestGenerateSaltIsRandom (t * testing.T ) {
60+ salt1 , _ := GenerateSalt ()
61+ salt2 , _ := GenerateSalt ()
62+
63+ if string (salt1 ) == string (salt2 ) {
64+ t .Fatal ("GenerateSalt should produce different values each time" )
65+ }
66+ }
67+
68+ // --- Encrypt / Decrypt ---
69+
70+ func TestEncryptDecryptRoundtrip (t * testing.T ) {
71+ key := make ([]byte , 32 )
72+ plaintext := "super-secret-value"
73+
74+ encrypted , err := Encrypt (plaintext , key )
75+ if err != nil {
76+ t .Fatalf ("Encrypt failed: %v" , err )
77+ }
78+
79+ decrypted , err := Decrypt (encrypted , key )
80+ if err != nil {
81+ t .Fatalf ("Decrypt failed: %v" , err )
82+ }
83+
84+ if decrypted != plaintext {
85+ t .Fatalf ("expected %q, got %q" , plaintext , decrypted )
86+ }
87+ }
88+
89+ func TestEncryptProducesDifferentCiphertextEachTime (t * testing.T ) {
90+ key := make ([]byte , 32 )
91+ plaintext := "same-value"
92+
93+ c1 , _ := Encrypt (plaintext , key )
94+ c2 , _ := Encrypt (plaintext , key )
95+
96+ if c1 == c2 {
97+ t .Fatal ("Encrypt should produce different ciphertext each time due to random nonce" )
98+ }
99+ }
100+
101+ func TestDecryptFailsWithWrongKey (t * testing.T ) {
102+ key1 := make ([]byte , 32 )
103+ key2 := make ([]byte , 32 )
104+ key2 [0 ] = 0xFF
105+
106+ encrypted , err := Encrypt ("secret" , key1 )
107+ if err != nil {
108+ t .Fatalf ("Encrypt failed: %v" , err )
109+ }
110+
111+ _ , err = Decrypt (encrypted , key2 )
112+ if err == nil {
113+ t .Fatal ("Decrypt should fail with a wrong key" )
114+ }
115+ }
116+
117+ func TestDecryptFailsWithTamperedCiphertext (t * testing.T ) {
118+ key := make ([]byte , 32 )
119+
120+ encrypted , err := Encrypt ("secret" , key )
121+ if err != nil {
122+ t .Fatalf ("Encrypt failed: %v" , err )
123+ }
124+
125+ // Flip the last character of the hex string
126+ tampered := encrypted [:len (encrypted )- 1 ] + "x"
127+ _ , err = Decrypt (tampered , key )
128+ if err == nil {
129+ t .Fatal ("Decrypt should fail with tampered ciphertext" )
130+ }
131+ }
132+
133+ func TestDecryptFailsWithEmptyInput (t * testing.T ) {
134+ key := make ([]byte , 32 )
135+ _ , err := Decrypt ("" , key )
136+ if err == nil {
137+ t .Fatal ("Decrypt should fail with empty input" )
138+ }
139+ }
140+
141+ // --- DeriveTransportKey ---
142+
143+ func TestDeriveTransportKeyLength (t * testing.T ) {
144+ key := DeriveTransportKey ("my-api-key" )
145+ if len (key ) != 32 {
146+ t .Fatalf ("expected transport key length 32, got %d" , len (key ))
147+ }
148+ }
149+
150+ func TestDeriveTransportKeyIsDeterministic (t * testing.T ) {
151+ k1 := DeriveTransportKey ("my-api-key" )
152+ k2 := DeriveTransportKey ("my-api-key" )
153+
154+ if string (k1 ) != string (k2 ) {
155+ t .Fatal ("DeriveTransportKey should be deterministic" )
156+ }
157+ }
158+
159+ func TestDeriveTransportKeyDiffersForDifferentKeys (t * testing.T ) {
160+ k1 := DeriveTransportKey ("api-key-1" )
161+ k2 := DeriveTransportKey ("api-key-2" )
162+
163+ if string (k1 ) == string (k2 ) {
164+ t .Fatal ("DeriveTransportKey should produce different keys for different API keys" )
165+ }
166+ }
0 commit comments