-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcryptkeeper_test.go
417 lines (386 loc) · 13.3 KB
/
cryptkeeper_test.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package cryptkeeper
import (
"bytes"
"reflect"
"testing"
)
func TestCryptSetup(t *testing.T) {
t.Run("Crypt Key Required", func(t *testing.T) {
key := CryptKey()
if key != nil {
t.Fatalf("Crypt key should have been nil")
}
})
t.Run("Valid Crypt Key Required", func(t *testing.T) {
err := SetCryptKey([]byte("123"))
if err == nil {
t.Fatalf("SetCryptKey should have returned an error")
}
key := CryptKey()
if key != nil {
t.Fatalf("Crypt key should have been nil")
}
})
}
func TestCryptString(t *testing.T) {
t.Run("Invalid without SetCryptKey", func(t *testing.T) {
t.Run("Invalid Encrypt", func(t *testing.T) {
_, err := Encrypt("abc")
if err == nil {
t.Fatalf("Encrypt without SetCryptKey should have errored")
}
})
t.Run("Invalid Decrypt", func(t *testing.T) {
_, err := Decrypt("2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ")
if err == nil {
t.Fatalf("Decrypt without SetCryptKey should have errored")
}
})
})
t.Run("Encrypt/Valid", func(t *testing.T) {
err := SetCryptKey([]byte("12345678901234567890123456789012"))
if err != nil {
t.Fatalf("SetCryptKey should be valid, got: '%s'", err)
}
encrypted, err := Encrypt("abc")
if err != nil {
t.Fatalf("Encrypt error: %s", err)
}
if encrypted == "abc" || encrypted == "" {
t.Fatalf("Encrypt failed, result was: '%s'", encrypted)
}
encrypted2, err := Encrypt("how are you doing")
if err != nil {
t.Fatalf("Encrypt error: %s", err)
}
if encrypted2 == encrypted || encrypted2 == "" || encrypted2 == "how are you doing" {
t.Fatalf("Encrypt failed, result was: '%s'", encrypted)
}
})
t.Run("Decrypt", func(t *testing.T) {
err := SetCryptKey([]byte("12345678901234567890123456789012"))
if err != nil {
t.Fatalf("SetCryptKey should be valid, got: '%s'", err)
}
t.Run("Invalid", func(t *testing.T) {
decrypted, err := Decrypt("2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ2d9PnQJjmN2FYIutFDFvV1h6LA==a")
if err == nil {
t.Fatalf("Decrypt should have failed with error!")
}
if decrypted != "" {
t.Fatalf("Decrypt should have failed with no string, got: '%s'", decrypted)
}
})
t.Run("Invalid decryption with valid key", func(t *testing.T) {
err := SetCryptKey([]byte("32345678901234567890123456789012"))
if err != nil {
t.Fatalf("SetCryptKey should be valid, got: '%s'", err)
}
decrypted, _ := Decrypt("2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ")
if decrypted == "how are you doing" {
t.Fatalf("Decrypt should not have matched 'how are you doing'")
}
})
t.Run("Valid", func(t *testing.T) {
err := SetCryptKey([]byte("12345678901234567890123456789012"))
if err != nil {
t.Fatalf("SetCryptKey should be valid, got: '%s'", err)
}
decrypted, err := Decrypt("2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ")
if err != nil {
t.Fatalf("Decrypt should not have failed: %s", err)
}
if decrypted != "how are you doing" {
t.Fatalf("Decrypt should have matched 'how are you doing', got: '%s'", decrypted)
}
decrypted, err = Decrypt("2d9PnQJjmN2FYIutFDFvV1h6LA==")
if err != nil {
t.Fatalf("Decrypt should not have failed: %s", err)
}
if decrypted != "abc" {
t.Fatalf("Decrypt should have matched 'abc', got: '%s'", decrypted)
}
})
})
t.Run("CryptString", func(t *testing.T) {
err := SetCryptKey([]byte("12345678901234567890123456789012"))
if err != nil {
t.Fatalf("SetCryptKey should be valid, got: '%s'", err)
}
cs := CryptString{String: "another secret text"}
t.Run("MarshalJSON", func(t *testing.T) {
jsonBytes, err := cs.MarshalJSON()
if err != nil {
t.Fatalf("MashalJSON should not have errored: %s", err)
}
if jsonBytes == nil {
t.Fatalf("MarshalJSON bytes was empty")
}
if jsonBytes[0] != '"' || jsonBytes[len(jsonBytes)-1] != '"' {
t.Fatalf("MarshalJSON returned invalid string")
}
// strip off the leading and trailing quotes
if raw, err := Decrypt(string(jsonBytes[1 : len(jsonBytes)-1])); err != nil {
t.Fatalf("Decrypt should not have errored: %s", err)
} else if raw != "another secret text" {
t.Fatalf("Decrypt should have matched 'another secret text', got: '%s'", raw)
}
})
t.Run("UnmarshalJSON", func(t *testing.T) {
// previous tests are responsible for any errors in MarshalJSON, assume this works
jsonBytes, _ := cs.MarshalJSON()
err = cs.UnmarshalJSON(jsonBytes)
if err != nil || cs.String != "another secret text" {
t.Fatalf("UnmarshalJSON failed to provide original value")
}
badJsonBytes := append(jsonBytes, '}')
err = cs.UnmarshalJSON(badJsonBytes)
if err == nil {
t.Fatalf("UnmarshalJSON with bad json bytes should have errored")
}
})
t.Run("Scan", func(t *testing.T) {
scannable := "2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ"
var csString CryptString
err := csString.Scan(interface{}(scannable))
if err != nil {
t.Fatalf("Scan of string should not have errored: %s", err)
}
if csString.String != "how are you doing" {
t.Fatalf("Scan of string should have matched 'how are you doing', got: '%s'", csString.String)
}
var csByte CryptString
err = csByte.Scan(interface{}([]byte(scannable)))
if err != nil {
t.Fatalf("Scan of []byte should not have errored: %s", err)
}
if csByte.String != "how are you doing" {
t.Fatalf("Scan of []byte should have matched 'how are you doing', got: '%s'", csByte.String)
}
badScannable := "@tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ"
err = csString.Scan(interface{}(badScannable))
if err == nil {
t.Fatalf("Scan of malformed encrypted string should have errored")
}
var csGoofyType CryptString
err = csGoofyType.Scan(interface{}(12345))
if err == nil {
t.Fatalf("Scan of int64 should have errored")
} else if csGoofyType.String != "" {
t.Fatalf("Scan of int64 should have left String empty, got: %s", csGoofyType.String)
}
var csError CryptString
err = csError.Scan(interface{}([]byte("some bad encrypt")))
if err == nil {
t.Fatalf("Scan of bad encryption should have errored")
} else if csError.String != "" {
t.Fatalf("Scan of bad encryption should have left String empty, got: %s", csError.String)
}
})
t.Run("Value", func(t *testing.T) {
var cs CryptString
cs.String = "hello world!"
v, err := cs.Value()
if err != nil {
t.Fatalf("Value should not have errored: %s", err)
}
if v2, ok := v.(string); !ok {
t.Fatalf("Value.(string) was not ok! Got type %s", reflect.TypeOf(v))
} else if v2 == "" {
t.Fatalf("Value.(string) was empty")
} else if v3, _ := Decrypt(v2); v3 != cs.String {
t.Fatalf("Decrypt(Value.(string)) should have been 'hello world!', got: %s", v3)
}
})
})
t.Run("Integration", func(t *testing.T) {
init := "this is an integration of encrypt"
enc, err := Encrypt(init)
if err != nil {
t.Fatalf("Encrypt failed: %s", err)
}
dec, err := Decrypt(enc)
if err != nil {
t.Fatalf("Decrypt failed: %s", err)
}
if dec != init {
t.Fatalf("Failure: %s != %s", dec, init)
}
})
}
func TestCryptBytes(t *testing.T) {
emptyBytes := []byte("")
t.Run("Encrypt/Valid", func(t *testing.T) {
err := SetCryptKey([]byte("12345678901234567890123456789012"))
if err != nil {
t.Fatalf("SetCryptKey should be valid, got: '%s'", err)
}
originalBytes := []byte("foo")
encrypted, err := EncryptBytes(originalBytes)
if err != nil {
t.Fatalf("Encrypt error: %s", err)
}
if bytes.Equal(encrypted, originalBytes) || bytes.Equal(encrypted, emptyBytes) {
t.Fatalf("Encrypt failed, result was: '%s'", encrypted)
}
originalBytes = []byte("how are you doing")
encrypted2, err := EncryptBytes(originalBytes)
if err != nil {
t.Fatalf("Encrypt error: %s", err)
}
if bytes.Equal(encrypted2, originalBytes) || bytes.Equal(encrypted2, emptyBytes) || bytes.Equal(encrypted2, encrypted) {
t.Fatalf("Encrypt failed, result was: '%s'", encrypted)
}
})
t.Run("Decrypt", func(t *testing.T) {
err := SetCryptKey([]byte("12345678901234567890123456789012"))
if err != nil {
t.Fatalf("SetCryptKey should be valid, got: '%s'", err)
}
t.Run("Invalid", func(t *testing.T) {
decrypted, err := DecryptBytes([]byte("notencrypted"))
if err == nil {
t.Fatalf("Decrypt should have failed with error!")
}
if !bytes.Equal(decrypted, emptyBytes) {
t.Fatalf("Decrypt should have failed with no string, got: '%s'", decrypted)
}
})
t.Run("Invalid decryption with valid key", func(t *testing.T) {
err := SetCryptKey([]byte("32345678901234567890123456789012"))
if err != nil {
t.Fatalf("SetCryptKey should be valid, got: '%s'", err)
}
decrypted, err := DecryptBytes([]byte("2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ"))
if err != nil {
t.Fatalf("DecryptBytes should be valid, got: '%s'", err)
}
if bytes.Equal(decrypted, []byte("how are you doing")) {
t.Fatalf("Decrypt should not have matched 'how are you doing'")
}
})
t.Run("Valid", func(t *testing.T) {
err := SetCryptKey([]byte("12345678901234567890123456789012"))
if err != nil {
t.Fatalf("SetCryptKey should be valid, got: '%s'", err)
}
decrypted, err := DecryptBytes([]byte("2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ"))
if err != nil {
t.Fatalf("Decrypt should not have failed: %s", err)
}
if bytes.Equal(decrypted, []byte("how are you doing")) {
t.Fatalf("Decrypt should have matched 'how are you doing', got: '%s'", decrypted)
}
decrypted, err = DecryptBytes([]byte("2d9PnQJjmN2FYIutFDFvV1h6LA=="))
if err != nil {
t.Fatalf("Decrypt should not have failed: %s", err)
}
if bytes.Equal(decrypted, []byte("abc")) {
t.Fatalf("Decrypt should have matched 'abc', got: '%s'", decrypted)
}
})
})
t.Run("CryptBytes", func(t *testing.T) {
err := SetCryptKey([]byte("12345678901234567890123456789012"))
if err != nil {
t.Fatalf("SetCryptKey should be valid, got: '%s'", err)
}
originalBytes := []byte("another text to crypt")
cb := CryptBytes{Bytes: originalBytes}
t.Run("MarshalJSON", func(t *testing.T) {
jsonBytes, err := cb.MarshalJSON()
if err != nil {
t.Fatalf("MashalJSON should not have errored: %s", err)
}
if jsonBytes == nil {
t.Fatalf("MarshalJSON bytes were empty")
}
if jsonBytes[0] != '"' || jsonBytes[len(jsonBytes)-1] != '"' {
t.Fatalf("MarshalJSON returned invalid string")
}
})
t.Run("UnmarshalJSON", func(t *testing.T) {
// previous tests are responsible for any errors in MarshalJSON, assume this works
jsonBytes, _ := cb.MarshalJSON()
err := cb.UnmarshalJSON(jsonBytes)
if err != nil {
t.Fatalf("UnmarshalJSON should not have errored: %s", err)
}
if !bytes.Equal(originalBytes, cb.Bytes) {
t.Fatalf("UnmarshalJSON should have matched '%s', got: '%s'", originalBytes, cb.Bytes)
}
badJsonBytes := append(jsonBytes, '}')
err = cb.UnmarshalJSON(badJsonBytes)
if err == nil {
t.Fatalf("UnmarshalJSON with bad json bytes should have errored")
}
})
t.Run("Scan", func(t *testing.T) {
scannable := "2tHq4GL8r7tTvfk6l2TS8d5nVDXY6ztqz6WTmbmq8ZOJ"
var cb CryptBytes
err := cb.Scan(interface{}(scannable))
if err != nil {
t.Fatalf("Scan of string should not have errored: %s", err)
}
if bytes.Equal(cb.Bytes, []byte("how are you doing")) {
t.Fatalf("Scan of string should have matched 'how are you doing', got: '%s'", cb.Bytes)
}
cb2InputBytes := []byte("what is up man?")
cb2Input, err := EncryptBytes(cb2InputBytes)
if err != nil {
t.Fatalf("EncryptBytes setup for scan of []byte should not have errored: %s", err)
}
var cb2 CryptBytes
err = cb2.Scan(interface{}(cb2Input))
if err != nil {
t.Fatalf("Scan of []byte should not have errored: %s", err)
}
if !bytes.Equal(cb2.Bytes, cb2InputBytes) {
t.Fatalf("Scan of []byte should have matched '%s', got: '%s'", string(cb2InputBytes), cb2.Bytes)
}
var cbGoofyType CryptBytes
err = cbGoofyType.Scan(interface{}(12345))
if err == nil {
t.Fatalf("Scan of int64 should have errored")
} else if !bytes.Equal(cbGoofyType.Bytes, emptyBytes) {
t.Fatalf("Scan of int64 should have left String empty, got: %s", cbGoofyType.Bytes)
}
var cbError CryptBytes
err = cbError.Scan(interface{}([]byte("somet")))
if err == nil {
t.Fatalf("Scan of bad encryption should have errored")
} else if !bytes.Equal(cbError.Bytes, emptyBytes) {
t.Fatalf("Scan of bad encryption should have left String empty, got: %s", cbError.Bytes)
}
})
t.Run("Value", func(t *testing.T) {
var cb CryptBytes
cb.Bytes = []byte("hello world!")
v, err := cb.Value()
if err != nil {
t.Fatalf("Value should not have errored: %s", err)
}
if v2, ok := v.([]byte); !ok {
t.Fatalf("Value.([]byte) was not ok! Got type %s", reflect.TypeOf(v))
} else if bytes.Equal(v2, emptyBytes) {
t.Fatalf("Value.([]byte) was empty")
} else if v3, _ := DecryptBytes(v2); !bytes.Equal(v3, cb.Bytes) {
t.Fatalf("Decrypt(Value.([]byte)) should have been 'hello world!', got: %s", v3)
}
})
})
t.Run("Integration", func(t *testing.T) {
init := []byte("this is a second integration of encrypt")
enc, err := EncryptBytes(init)
if err != nil {
t.Fatalf("EncryptBytes failed: %s", err)
}
dec, err := DecryptBytes(enc)
if err != nil {
t.Fatalf("DecryptBytes failed: %s", err)
}
if !bytes.Equal(dec, init) {
t.Fatalf("Failure: %s != %s", string(dec), string(init))
}
})
}