forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xor_test.go
117 lines (107 loc) · 2.52 KB
/
xor_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
package xor
import (
"bytes"
"fmt"
"reflect"
"testing"
)
func Example() {
const (
seed = "Hello World"
key = 97
)
encrypted := Encrypt(byte(key), []byte(seed))
fmt.Printf("Encrypt=> key: %d, seed: %s, encryptedText: %v\n", key, seed, encrypted)
decrypted := Decrypt(byte(key), encrypted)
fmt.Printf("Decrypt=> key: %d, encryptedText: %v, DecryptedText: %s\n", key, encrypted, string(decrypted))
// Output:
// Encrypt=> key: 97, seed: Hello World, encryptedText: [41 4 13 13 14 65 54 14 19 13 5]
// Decrypt=> key: 97, encryptedText: [41 4 13 13 14 65 54 14 19 13 5], DecryptedText: Hello World
}
var xorTestData = []struct {
description string
input string
key int
encrypted string
}{
{
"Encrypt letter 'a' with key 0 makes no changes",
"a",
0,
"a",
},
{
"Encrypt letter 'a' with key 1",
"a",
1,
"`",
},
{
"Encrypt letter 'a' with key 10",
"a",
10,
"k",
},
{
"Encrypt 'hello world' with key 0 makes no changes",
"hello world",
0,
"hello world",
},
{
"Encrypt 'hello world' with key 1",
"hello world",
1,
"idmmn!vnsme",
},
{
"Encrypt 'hello world' with key 10",
"hello world",
10,
"boffe*}exfn",
},
{
"Encrypt full sentence with key 64",
"the quick brown fox jumps over the lazy dog.",
64,
"4(%`15)#+`\"2/7.`&/8`*5-03`/6%2`4(%`,!:9`$/'n",
},
{
"Encrypt a word with key 32 make the case swap",
"abcdefghijklmNOPQRSTUVWXYZ",
32,
"ABCDEFGHIJKLMnopqrstuvwxyz",
},
}
func TestXorCipherEncrypt(t *testing.T) {
for _, test := range xorTestData {
t.Run(test.description, func(t *testing.T) {
encrypted := Encrypt(byte(test.key), []byte(test.input))
if !reflect.DeepEqual(string(encrypted), test.encrypted) {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expecting %s, actual %s", test.encrypted, string(encrypted))
}
})
}
}
func TestXorCipherDecrypt(t *testing.T) {
for _, test := range xorTestData {
t.Run(test.description, func(t *testing.T) {
decrypted := Decrypt(byte(test.key), []byte(test.encrypted))
if !reflect.DeepEqual(string(decrypted), test.input) {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expecting %s, actual %s", test.input, string(decrypted))
}
})
}
}
func FuzzXOR(f *testing.F) {
f.Add([]byte("The Quick Brown Fox Jumps over the Lazy Dog."), byte('X'))
f.Fuzz(func(t *testing.T, input []byte, key byte) {
cipherText := Encrypt(key, input)
result := Decrypt(key, cipherText)
if !bytes.Equal(input, result) {
t.Errorf("Before: %s, after: %s, key: %d", input, result, key)
}
})
}