-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcipher_test.go
More file actions
651 lines (586 loc) · 15.9 KB
/
cipher_test.go
File metadata and controls
651 lines (586 loc) · 15.9 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package sentencecipher
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
)
func TestEncodeDecode(t *testing.T) {
tests := []struct {
name string
input []byte
}{
{"empty", []byte{}},
{"single byte", []byte{0x00}},
{"single byte max", []byte{0xFF}},
{"two bytes", []byte{0x48, 0x69}}, // "Hi"
{"hello", []byte("Hello")},
{"hello world", []byte("Hello, World!")},
{"numbers", []byte("12345")},
{"special chars", []byte("!@#$%")},
{"thai text", []byte("สวัสดี")},
{"mixed", []byte("Hello สวัสดี 123")},
{"all byte values", func() []byte {
b := make([]byte, 256)
for i := 0; i < 256; i++ {
b[i] = byte(i)
}
return b
}()},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Encode(tt.input)
if err != nil {
t.Fatalf("Encode error: %v", err)
}
decoded, err := Decode(encoded)
if err != nil {
t.Fatalf("Decode error: %v", err)
}
if !bytes.Equal(decoded, tt.input) {
t.Errorf("Encode/Decode mismatch\noriginal: %v\ndecoded: %v", tt.input, decoded)
}
})
}
}
func TestEncodeDecodeString(t *testing.T) {
tests := []struct {
name string
input string
}{
{"empty", ""},
{"simple", "Hello"},
{"with spaces", "Hello World"},
{"with punctuation", "Hello, World!"},
{"thai", "สวัสดีครับ"},
{"emoji", "Hello 👋"},
{"secret message", "Meet at 9pm"},
{"long text", "The quick brown fox jumps over the lazy dog"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := EncodeString(tt.input)
if err != nil {
t.Fatalf("EncodeString error: %v", err)
}
decoded, err := DecodeString(encoded)
if err != nil {
t.Fatalf("DecodeString error: %v", err)
}
if decoded != tt.input {
t.Errorf("EncodeString/DecodeString mismatch\noriginal: %q\ndecoded: %q", tt.input, decoded)
}
})
}
}
func TestEncodeNaturalDecodeNatural(t *testing.T) {
tests := []struct {
name string
input []byte
}{
{"empty", []byte{}},
{"hello", []byte("Hello")},
{"secret", []byte("Secret message")},
{"thai", []byte("สวัสดี")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := EncodeNatural(tt.input)
if err != nil {
t.Fatalf("EncodeNatural error: %v", err)
}
decoded, err := DecodeNatural(encoded)
if err != nil {
t.Fatalf("DecodeNatural error: %v", err)
}
t.Logf("Encoded: %q", encoded)
t.Logf("Decoded: %q", decoded)
if !bytes.Equal(decoded, tt.input) {
t.Errorf("EncodeNatural/DecodeNatural mismatch\noriginal: %v\ndecoded: %v", tt.input, decoded)
}
})
}
}
func TestEncodedLooksLikeEnglish(t *testing.T) {
input := []byte("Secret")
encoded, err := Encode(input)
if err != nil {
t.Fatalf("Encode error: %v", err)
}
// Check that encoded text contains expected sentence structure
if len(encoded) == 0 {
t.Error("Encoded text should not be empty")
}
// Should end with a period
if encoded[len(encoded)-1] != '.' {
t.Error("Encoded text should end with a period")
}
// Should contain spaces (word separation)
if !bytes.Contains([]byte(encoded), []byte(" ")) {
t.Error("Encoded text should contain spaces")
}
t.Logf("Input: %q", input)
t.Logf("Encoded: %q", encoded)
}
func TestNaturalEncodingVariety(t *testing.T) {
input := []byte("Hello World!")
encoded, err := EncodeNatural(input)
if err != nil {
t.Fatalf("EncodeNatural error: %v", err)
}
// Natural encoding should use varied templates
t.Logf("Natural encoded: %q", encoded)
// Should contain some variety markers (Email structure)
hasVariety := bytes.Contains([]byte(encoded), []byte("Subject:")) ||
bytes.Contains([]byte(encoded), []byte("Hi")) ||
bytes.Contains([]byte(encoded), []byte("Dear")) ||
bytes.Contains([]byte(encoded), []byte("Regards,"))
if !hasVariety {
t.Log("Warning: Natural encoding might not have enough variety")
}
}
func TestDecodeInvalidInput(t *testing.T) {
tests := []struct {
name string
input string
}{
{"gibberish", "asdfghjkl"},
{"unknown subject", "Xyz loves something."},
{"unknown verb", "Tom xyzs something."},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := Decode(tt.input)
if err == nil {
t.Error("Expected error for invalid input, got nil")
}
})
}
}
func TestAllByteValues(t *testing.T) {
// Test that all possible byte values can be encoded and decoded
for i := 0; i < 256; i++ {
input := []byte{byte(i)}
encoded, err := Encode(input)
if err != nil {
t.Errorf("Failed to encode byte %d: %v", i, err)
continue
}
decoded, err := Decode(encoded)
if err != nil {
t.Errorf("Failed to decode byte %d: %v", i, err)
continue
}
if len(decoded) != 1 || decoded[0] != byte(i) {
t.Errorf("Byte %d: expected %v, got %v", i, input, decoded)
}
}
}
func TestAllTwoByteCombinatons(t *testing.T) {
// Test a sample of two-byte combinations
samples := [][2]byte{
{0x00, 0x00},
{0xFF, 0xFF},
{0x00, 0xFF},
{0xFF, 0x00},
{0x48, 0x69}, // "Hi"
{0xAB, 0xCD},
{0x12, 0x34},
}
for _, sample := range samples {
input := []byte{sample[0], sample[1]}
encoded, err := Encode(input)
if err != nil {
t.Errorf("Failed to encode %v: %v", input, err)
continue
}
decoded, err := Decode(encoded)
if err != nil {
t.Errorf("Failed to decode %v: %v", input, err)
continue
}
if !bytes.Equal(decoded, input) {
t.Errorf("Two bytes %v: expected %v, got %v", sample, input, decoded)
}
}
}
func TestBinaryData(t *testing.T) {
// PNG header signature
pngHeader := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}
tests := []struct {
name string
input []byte
}{
{"png header", pngHeader},
{"null bytes", []byte{0x00, 0x00, 0x00, 0x00}},
{"high bytes", []byte{0xFF, 0xFE, 0xFD, 0xFC}},
{"mixed binary", []byte{0x00, 0xFF, 0x7F, 0x80, 0x01, 0xFE}},
{"random binary", []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Encode(tt.input)
if err != nil {
t.Fatalf("Encode error: %v", err)
}
decoded, err := Decode(encoded)
if err != nil {
t.Fatalf("Decode error: %v", err)
}
if !bytes.Equal(decoded, tt.input) {
t.Errorf("Binary encode/decode mismatch\noriginal: %v\ndecoded: %v", tt.input, decoded)
}
})
}
}
func TestCipherWithKey(t *testing.T) {
tests := []struct {
name string
key string
input []byte
}{
{"simple key", "secret", []byte("Hello")},
{"complex key", "my-super-secret-key-123!", []byte("Secret message")},
{"binary with key", "test-key", []byte{0x89, 0x50, 0x4E, 0x47}},
{"all bytes with key", "key123", func() []byte {
b := make([]byte, 256)
for i := 0; i < 256; i++ {
b[i] = byte(i)
}
return b
}()},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cipher, err := NewCipher(tt.key)
if err != nil {
t.Fatalf("NewCipher error: %v", err)
}
encoded, err := cipher.Encode(tt.input)
if err != nil {
t.Fatalf("Encode error: %v", err)
}
decoded, err := cipher.Decode(encoded)
if err != nil {
t.Fatalf("Decode error: %v", err)
}
if !bytes.Equal(decoded, tt.input) {
t.Errorf("Cipher encode/decode mismatch\noriginal: %v\ndecoded: %v", tt.input, decoded)
}
})
}
}
func TestCipherNaturalWithKey(t *testing.T) {
tests := []struct {
name string
key string
input []byte
}{
{"natural with key", "my-key", []byte("Hello World")},
{"binary natural", "binary-key", []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cipher, err := NewCipher(tt.key)
if err != nil {
t.Fatalf("NewCipher error: %v", err)
}
encoded, err := cipher.EncodeNatural(tt.input)
if err != nil {
t.Fatalf("EncodeNatural error: %v", err)
}
decoded, err := cipher.DecodeNatural(encoded)
if err != nil {
t.Fatalf("DecodeNatural error: %v", err)
}
if !bytes.Equal(decoded, tt.input) {
t.Errorf("Cipher natural encode/decode mismatch\noriginal: %v\ndecoded: %v", tt.input, decoded)
}
})
}
}
func TestDifferentKeysProduceDifferentOutput(t *testing.T) {
input := []byte("Secret message")
cipher1, _ := NewCipher("key1")
cipher2, _ := NewCipher("key2")
defaultCipher := NewDefaultCipher()
encoded1, _ := cipher1.Encode(input)
encoded2, _ := cipher2.Encode(input)
encodedDefault, _ := defaultCipher.Encode(input)
if encoded1 == encoded2 {
t.Error("Different keys should produce different encoded output")
}
if encoded1 == encodedDefault {
t.Error("Keyed cipher should produce different output than default")
}
if encoded2 == encodedDefault {
t.Error("Keyed cipher should produce different output than default")
}
}
func TestWrongKeyCannotDecode(t *testing.T) {
input := []byte("Secret message")
cipher1, _ := NewCipher("correct-key")
cipher2, _ := NewCipher("wrong-key")
encoded, err := cipher1.Encode(input)
if err != nil {
t.Fatalf("Encode error: %v", err)
}
decoded, err := cipher2.Decode(encoded)
// With wrong key, either error or different data
if err == nil && bytes.Equal(decoded, input) {
t.Error("Wrong key should not decode correctly")
}
}
func TestAllThreeByteGroups(t *testing.T) {
// Test all possible 3-byte combinations for first 16 values
// (full 256^3 would be too slow)
for b1 := 0; b1 < 16; b1++ {
for b2 := 0; b2 < 16; b2++ {
for b3 := 0; b3 < 16; b3++ {
input := []byte{byte(b1 * 16), byte(b2 * 16), byte(b3 * 16)}
encoded, err := Encode(input)
if err != nil {
t.Errorf("Failed to encode %v: %v", input, err)
continue
}
decoded, err := Decode(encoded)
if err != nil {
t.Errorf("Failed to decode %v: %v", input, err)
continue
}
if !bytes.Equal(decoded, input) {
t.Errorf("Three bytes %v: got %v", input, decoded)
}
}
}
}
}
func TestLargeBinaryData(t *testing.T) {
// Test with larger binary data (simulating a small file)
sizes := []int{100, 500, 1000, 2000}
for _, size := range sizes {
t.Run(fmt.Sprintf("size_%d", size), func(t *testing.T) {
input := make([]byte, size)
for i := 0; i < size; i++ {
input[i] = byte(i % 256)
}
encoded, err := Encode(input)
if err != nil {
t.Fatalf("Encode error for size %d: %v", size, err)
}
decoded, err := Decode(encoded)
if err != nil {
t.Fatalf("Decode error for size %d: %v", size, err)
}
if !bytes.Equal(decoded, input) {
t.Errorf("Large binary mismatch for size %d", size)
}
})
}
}
func TestLargeBinaryWithKey(t *testing.T) {
cipher, _ := NewCipher("test-key-for-large-data")
input := make([]byte, 1000)
for i := 0; i < 1000; i++ {
input[i] = byte(i % 256)
}
encoded, err := cipher.Encode(input)
if err != nil {
t.Fatalf("Encode error: %v", err)
}
decoded, err := cipher.Decode(encoded)
if err != nil {
t.Fatalf("Decode error: %v", err)
}
if !bytes.Equal(decoded, input) {
t.Error("Large binary with key mismatch")
}
}
func TestImageFile(t *testing.T) {
// Read actual PNG file
imgData, err := os.ReadFile("icon16.png")
if err != nil {
t.Skipf("Skipping image test: %v", err)
}
t.Run("normal_mode", func(t *testing.T) {
encoded, err := Encode(imgData)
if err != nil {
t.Fatalf("Encode error: %v", err)
}
decoded, err := Decode(encoded)
if err != nil {
t.Fatalf("Decode error: %v", err)
}
if !bytes.Equal(decoded, imgData) {
t.Errorf("Image encode/decode mismatch: original %d bytes, decoded %d bytes", len(imgData), len(decoded))
}
})
t.Run("natural_mode", func(t *testing.T) {
encoded, err := EncodeNatural(imgData)
if err != nil {
t.Fatalf("EncodeNatural error: %v", err)
}
decoded, err := DecodeNatural(encoded)
if err != nil {
t.Fatalf("DecodeNatural error: %v", err)
}
if !bytes.Equal(decoded, imgData) {
t.Errorf("Image natural encode/decode mismatch: original %d bytes, decoded %d bytes", len(imgData), len(decoded))
}
})
t.Run("with_key", func(t *testing.T) {
cipher, _ := NewCipher("image-test-key")
encoded, err := cipher.Encode(imgData)
if err != nil {
t.Fatalf("Encode error: %v", err)
}
decoded, err := cipher.Decode(encoded)
if err != nil {
t.Fatalf("Decode error: %v", err)
}
if !bytes.Equal(decoded, imgData) {
t.Errorf("Image with key encode/decode mismatch: original %d bytes, decoded %d bytes", len(imgData), len(decoded))
}
})
t.Run("natural_with_key", func(t *testing.T) {
cipher, _ := NewCipher("image-natural-key")
encoded, err := cipher.EncodeNatural(imgData)
if err != nil {
t.Fatalf("EncodeNatural error: %v", err)
}
decoded, err := cipher.DecodeNatural(encoded)
if err != nil {
t.Fatalf("DecodeNatural error: %v", err)
}
if !bytes.Equal(decoded, imgData) {
t.Errorf("Image natural with key encode/decode mismatch: original %d bytes, decoded %d bytes", len(imgData), len(decoded))
}
})
}
func BenchmarkEncode(b *testing.B) {
input := []byte("The quick brown fox jumps over the lazy dog")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Encode(input)
}
}
func BenchmarkDecode(b *testing.B) {
input := []byte("The quick brown fox jumps over the lazy dog")
encoded, _ := Encode(input)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Decode(encoded)
}
}
func BenchmarkEncodeNatural(b *testing.B) {
input := []byte("The quick brown fox jumps over the lazy dog")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = EncodeNatural(input)
}
}
func BenchmarkDecodeNatural(b *testing.B) {
input := []byte("The quick brown fox jumps over the lazy dog")
encoded, _ := EncodeNatural(input)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = DecodeNatural(encoded)
}
}
// Example tests
func ExampleEncodeString() {
encoded, _ := EncodeString("Hi")
_ = encoded // Encoded looks like English sentences
}
func ExampleDecodeString() {
encoded, _ := EncodeString("Hi")
decoded, _ := DecodeString(encoded)
_ = decoded // "Hi"
}
func TestWordListIntegrity(t *testing.T) {
lists := []struct {
name string
list []string
}{
{"defaultNames", defaultNames},
{"defaultVerbs", defaultVerbs},
{"defaultObjects", defaultObjects},
{"techVerbs", techVerbs},
{"techObjects", techObjects},
}
for _, l := range lists {
t.Run(l.name, func(t *testing.T) {
if len(l.list) != 256 {
t.Errorf("List %s has length %d, want 256", l.name, len(l.list))
}
checkDuplicates(t, l.name, l.list)
})
}
}
func checkDuplicates(t *testing.T, name string, list []string) {
seen := make(map[string]int)
for i, w := range list {
w = strings.ToLower(w)
if idx, exists := seen[w]; exists {
t.Errorf("Duplicate in %s: '%s' at index %d and %d", name, w, idx, i)
}
seen[w] = i
}
}
func TestSubjectOverlap(t *testing.T) {
// Check if any subject exists in both business and tech subjects
// This could confuse theme detection
business := businessSubjects
tech := techSubjects
for _, b := range business {
for _, te := range tech {
if strings.EqualFold(b, te) {
t.Errorf("Subject overlap found: '%s' is in both Business and Tech lists", b)
}
}
}
}
func TestNoSpacesInWords(t *testing.T) {
lists := []struct {
name string
list []string
}{
{"defaultNames", defaultNames},
{"defaultVerbs", defaultVerbs},
{"defaultObjects", defaultObjects},
{"techVerbs", techVerbs},
{"techObjects", techObjects},
}
for _, l := range lists {
t.Run(l.name, func(t *testing.T) {
for i, w := range l.list {
if strings.Contains(w, " ") {
t.Errorf("Space found in %s[%d]: '%s'", l.name, i, w)
}
}
})
}
}
func TestNoPunctuationInWords(t *testing.T) {
lists := []struct {
name string
list []string
}{
{"defaultNames", defaultNames},
{"defaultVerbs", defaultVerbs},
{"defaultObjects", defaultObjects},
{"techVerbs", techVerbs},
{"techObjects", techObjects},
}
for _, l := range lists {
t.Run(l.name, func(t *testing.T) {
for i, w := range l.list {
if strings.ContainsAny(w, ".,") {
t.Errorf("Punctuation found in %s[%d]: '%s'", l.name, i, w)
}
}
})
}
}