Debian Codegraph @

siv/xorend_test.go

raw symbols SCIP
1 // Copyright 2012 Aaron Jacobs. All Rights Reserved.
2 // Author: [email protected] (Aaron Jacobs)
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
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 // Helpers
30 ////////////////////////////////////////////////////////////////////////
31
32 // Like xorend, but doesn't require an existing buffer.
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 // Tests
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 }