-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor_test.go
78 lines (67 loc) · 1.55 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
package cryptanalysis
import (
"bytes"
"testing"
)
type xorbyte struct {
b1 byte
b2 byte
result byte
}
type xorarray struct {
a1 []byte
a2 []byte
result []byte
}
type xorarraybyte struct {
a1 []byte
b1 byte
result []byte
}
func TestXorBytes(t *testing.T) {
var tests = []xorbyte{
{0, 0, 0},
{0, 255, 255},
{128, 128, 0},
}
for _, test := range tests {
xor := XorBytes(test.b1, test.b2)
if xor != test.result {
t.Error("Expected", test.result, "got", xor)
}
}
}
func TestXorArrayByte(t *testing.T) {
var tests = []xorarraybyte{
{[]byte{0, 0, 0}, 0, []byte{0, 0, 0}},
{[]byte{0, 0, 0}, 255, []byte{255, 255, 255}},
{[]byte{128, 128, 128}, 128, []byte{0, 0, 0}},
}
for _, test := range tests {
xor := XorArrayByte(test.a1, test.b1)
if bytes.Compare(xor, test.result) != 0 {
t.Error("Expected", test.result, "got", xor)
}
}
}
func TestXorArrays(t *testing.T) {
var tests = []xorarray{
{[]byte{0, 0, 0}, []byte{0, 0, 0}, []byte{0, 0, 0}},
{[]byte{0, 0, 0}, []byte{255, 255, 255}, []byte{255, 255, 255}},
{[]byte{128, 128, 128}, []byte{128, 128, 128}, []byte{0, 0, 0}},
{[]byte{128, 128}, []byte{128}, []byte("Byte arrays have different lengths: 2, 1")},
}
for _, test := range tests {
xor, err := XorArrays(test.a1, test.a2)
if err != nil {
result := []byte(err.Error())
if bytes.Compare(result, test.result) != 0 {
t.Error("Expected", test.result, "got", result)
}
} else {
if bytes.Compare(xor, test.result) != 0 {
t.Error("Expected", test.result, "got", xor)
}
}
}
}