| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package siv |
| 17 |
|
| 18 |
import ( |
| 19 |
"strconv" |
| 20 |
"testing" |
| 21 |
|
| 22 |
. "github.com/jacobsa/oglematchers" |
| 23 |
. "github.com/jacobsa/ogletest" |
| 24 |
) |
| 25 |
|
| 26 |
func TestXorend(t *testing.T) { RunTests(t) } |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
func easyXorend(a, b []byte) []byte { |
| 34 |
dst := make([]byte, len(a)) |
| 35 |
xorend(dst, a, b) |
| 36 |
return dst |
| 37 |
} |
| 38 |
|
| 39 |
func fromBinary(s string) byte { |
| 40 |
AssertEq(8, len(s), "%s", s) |
| 41 |
|
| 42 |
u, err := strconv.ParseUint(s, 2, 8) |
| 43 |
AssertEq(nil, err, "%s", s) |
| 44 |
|
| 45 |
return byte(u) |
| 46 |
} |
| 47 |
|
| 48 |
type XorendTest struct{} |
| 49 |
|
| 50 |
func init() { RegisterTestSuite(&XorendTest{}) } |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
func (t *XorendTest) AIsShorterThanB() { |
| 57 |
a := []byte{0xde} |
| 58 |
b := []byte{0xde, 0xad} |
| 59 |
|
| 60 |
f := func() { easyXorend(a, b) } |
| 61 |
ExpectThat(f, Panics(HasSubstr("length"))) |
| 62 |
} |
| 63 |
|
| 64 |
func (t *XorendTest) BothAreNil() { |
| 65 |
a := []byte(nil) |
| 66 |
b := []byte(nil) |
| 67 |
|
| 68 |
expected := []byte{} |
| 69 |
ExpectThat(easyXorend(a, b), DeepEquals(expected)) |
| 70 |
} |
| 71 |
|
| 72 |
func (t *XorendTest) BIsNil() { |
| 73 |
a := []byte{fromBinary("10101010"), fromBinary("00000000")} |
| 74 |
b := []byte(nil) |
| 75 |
|
| 76 |
expected := a |
| 77 |
ExpectThat(easyXorend(a, b), DeepEquals(expected)) |
| 78 |
} |
| 79 |
|
| 80 |
func (t *XorendTest) BothAreEmpty() { |
| 81 |
a := []byte{} |
| 82 |
b := []byte{} |
| 83 |
|
| 84 |
expected := []byte{} |
| 85 |
ExpectThat(easyXorend(a, b), DeepEquals(expected)) |
| 86 |
} |
| 87 |
|
| 88 |
func (t *XorendTest) BIsEmpty() { |
| 89 |
a := []byte{fromBinary("10101010"), fromBinary("00000000")} |
| 90 |
b := []byte{} |
| 91 |
|
| 92 |
expected := a |
| 93 |
ExpectThat(easyXorend(a, b), DeepEquals(expected)) |
| 94 |
} |
| 95 |
|
| 96 |
func (t *XorendTest) BIsNonEmpty() { |
| 97 |
a := []byte{ |
| 98 |
fromBinary("11110000"), |
| 99 |
fromBinary("10101010"), |
| 100 |
fromBinary("00000000"), |
| 101 |
} |
| 102 |
|
| 103 |
b := []byte{ |
| 104 |
fromBinary("11110000"), |
| 105 |
fromBinary("00001111"), |
| 106 |
} |
| 107 |
|
| 108 |
expected := []byte{ |
| 109 |
fromBinary("11110000"), |
| 110 |
fromBinary("01011010"), |
| 111 |
fromBinary("00001111"), |
| 112 |
} |
| 113 |
|
| 114 |
ExpectThat(easyXorend(a, b), DeepEquals(expected)) |
| 115 |
} |
| 116 |
|
| 117 |
func (t *XorendTest) DoesntClobberInputData() { |
| 118 |
a := []byte{ |
| 119 |
fromBinary("11110000"), |
| 120 |
fromBinary("10101010"), |
| 121 |
fromBinary("00000000"), |
| 122 |
} |
| 123 |
aCopy := dup(a) |
| 124 |
|
| 125 |
b := []byte{ |
| 126 |
fromBinary("11110000"), |
| 127 |
fromBinary("00001111"), |
| 128 |
} |
| 129 |
bCopy := dup(b) |
| 130 |
|
| 131 |
easyXorend(a, b) |
| 132 |
ExpectThat(a, DeepEquals(aCopy)) |
| 133 |
ExpectThat(b, DeepEquals(bCopy)) |
| 134 |
} |