Debian Codegraph @

syntax/success_test.go

raw symbols SCIP
1 package syntax
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "fmt"
7 "reflect"
8 "strings"
9 "testing"
10 )
11
12 func unhex(encoded string) []byte {
13 decoded, err := hex.DecodeString(encoded)
14 if err != nil {
15 panic(err)
16 }
17 return decoded
18 }
19
20 func buffer(size int) []byte {
21 return bytes.Repeat([]byte{0xA0}, size)
22 }
23
24 func hexBuffer(size int) string {
25 return strings.Repeat("A0", size)
26 }
27
28 type CrypticString string
29
30 // A CrypticString marshalls as one length octet followed by the
31 // UTF-8 bytes of the string, XOR'ed with an increasing sequence
32 // starting with the length plus one (L+1, L+2, ...).
33 func (cs CrypticString) MarshalTLS() ([]byte, error) {
34 l := byte(len(cs))
35 b := []byte(cs)
36 for i := range b {
37 b[i] ^= l + byte(i) + 1
38 }
39 return append([]byte{l}, b...), nil
40 }
41
42 func (cs *CrypticString) UnmarshalTLS(data []byte) (int, error) {
43 if len(data) == 0 {
44 return 0, fmt.Errorf("Length of CrypticString must be at least 1")
45 }
46
47 l := data[0]
48 if len(data) < int(l)+1 {
49 return 0, fmt.Errorf("TLS data not long enough for CrypticString")
50 }
51
52 b := data[1 : l+1]
53 for i := range b {
54 b[i] ^= l + byte(i) + 1
55 }
56
57 *cs = CrypticString(string(b))
58
59 return int(l + 1), nil
60 }
61
62 func (cs CrypticString) ValidForTLS() error {
63 if len(cs) > 256 {
64 return fmt.Errorf("CrypticString length to large: %d", len(cs))
65 }
66
67 if string(cs) == "fnord" {
68 return fmt.Errorf("Forbidden value")
69 }
70
71 return nil
72 }
73
74 func TestSuccessCases(t *testing.T) {
75 dummyUint16 := uint16(0xFFFF)
76 crypticHello := CrypticString("hello")
77 testCases := map[string]struct {
78 value interface{}
79 encoding []byte
80 }{
81 // Uints
82 "uint8": {
83 value: uint8(0xA0),
84 encoding: unhex("A0"),
85 },
86 "uint16": {
87 value: uint16(0xB0A0),
88 encoding: unhex("B0A0"),
89 },
90 "uint32": {
91 value: uint32(0xD0C0B0A0),
92 encoding: unhex("D0C0B0A0"),
93 },
94 "uint64": {
95 value: uint64(0xD0C0B0A090807060),
96 encoding: unhex("D0C0B0A090807060"),
97 },
98
99 // Varints
100 "varint8": {
101 value: struct {
102 V uint8 `tls:"varint"`
103 }{V: 0x3F},
104 encoding: unhex("3F"),
105 },
106 "varint16": {
107 value: struct {
108 V uint16 `tls:"varint"`
109 }{V: 0x3FFF},
110 encoding: unhex("7FFF"),
111 },
112 "varint32": {
113 value: struct {
114 V uint32 `tls:"varint"`
115 }{V: 0x3FFFFFFF},
116 encoding: unhex("BFFFFFFF"),
117 },
118 "varint64": {
119 value: struct {
120 V uint64 `tls:"varint"`
121 }{V: 0x3FFFFFFFFFFFFFFF},
122 encoding: unhex("FFFFFFFFFFFFFFFF"),
123 },
124
125 // Arrays
126 "array": {
127 value: [5]uint16{0x0102, 0x0304, 0x0506, 0x0708, 0x090a},
128 encoding: unhex("0102030405060708090a"),
129 },
130
131 // Slices
132 "slice-0x20": {
133 value: struct {
134 V []byte `tls:"head=1"`
135 }{
136 V: buffer(0x20),
137 },
138 encoding: unhex("20" + hexBuffer(0x20)),
139 },
140 "slice-0x200": {
141 value: struct {
142 V []byte `tls:"head=2"`
143 }{
144 V: buffer(0x200),
145 },
146 encoding: unhex("0200" + hexBuffer(0x200)),
147 },
148 "slice-0x20000": {
149 value: struct {
150 V []byte `tls:"head=3"`
151 }{
152 V: buffer(0x20000),
153 },
154 encoding: unhex("020000" + hexBuffer(0x20000)),
155 },
156 "slice-none": {
157 value: struct {
158 V []byte `tls:"head=none"`
159 }{
160 V: buffer(0x3FFF),
161 },
162 encoding: unhex(hexBuffer(0x3FFF)),
163 },
164 "slice-varint": {
165 value: struct {
166 V []byte `tls:"head=varint"`
167 }{
168 V: buffer(0x3FFF),
169 },
170 encoding: unhex("7FFF" + hexBuffer(0x3FFF)),
171 },
172
173 // Maps
174 "map": {
175 value: struct {
176 V map[uint16]uint8 `tls:"head=1"`
177 }{
178 V: map[uint16]uint8{2: 1, 1: 2},
179 },
180 encoding: unhex("06000102000201"),
181 },
182
183 // Struct
184 "struct": {
185 value: struct {
186 A uint16
187 B []uint8 `tls:"head=2"`
188 C [4]uint32
189 }{
190 A: 0xB0A0,
191 B: []uint8{0xA0, 0xA1, 0xA2, 0xA3, 0xA4},
192 C: [4]uint32{0x10111213, 0x20212223, 0x30313233, 0x40414243},
193 },
194 encoding: unhex("B0A0" + "0005A0A1A2A3A4" + "10111213202122233031323340414243"),
195 },
196 "struct-omit": {
197 value: struct {
198 A uint16
199 B []uint8 `tls:"omit"`
200 C [4]uint32
201 }{
202 A: 0xB0A0,
203 B: nil,
204 C: [4]uint32{0x10111213, 0x20212223, 0x30313233, 0x40414243},
205 },
206 encoding: unhex("B0A0" + "10111213202122233031323340414243"),
207 },
208 "struct-pointer": {
209 value: struct{ V *uint16 }{V: &dummyUint16},
210 encoding: unhex("FFFF"),
211 },
212
213 // Optional
214 "optional-absent": {
215 value: struct {
216 A *uint16 `tls:"optional"`
217 }{
218 A: nil,
219 },
220 encoding: unhex("00"),
221 },
222 "optional-present": {
223 value: struct {
224 A *uint16 `tls:"optional"`
225 }{
226 A: &dummyUint16,
227 },
228 encoding: unhex("01FFFF"),
229 },
230 "optional-marshaler-absent": {
231 value: struct {
232 A *CrypticString `tls:"optional"`
233 }{
234 A: nil,
235 },
236 encoding: unhex("00"),
237 },
238 "optional-marshaler-present": {
239 value: struct {
240 A *CrypticString `tls:"optional"`
241 }{
242 A: &crypticHello,
243 },
244 encoding: unhex("01056e62646565"),
245 },
246
247 // Marshaler
248 "marshaler": {
249 value: crypticHello,
250 encoding: unhex("056e62646565"),
251 },
252 "struct-marshaler": {
253 value: struct {
254 A CrypticString
255 B uint16
256 C CrypticString
257 }{
258 A: CrypticString("hello"),
259 B: 0xB0A0,
260 C: CrypticString("... world!"),
261 },
262 encoding: unhex("056e62646565" + "B0A0" + "0a2522232e787f637e7735"),
263 },
264 }
265 for label, testCase := range testCases {
266 // Test that encode succeeds
267 encoding, err := Marshal(testCase.value)
268 if err != nil {
269 t.Fatalf("Encode error [%s]: %v", label, err)
270 }
271
272 if !bytes.Equal(encoding, testCase.encoding) {
273 t.Fatalf("Invalid encoding [%s]: %x != %x", label, encoding, testCase.encoding)
274 }
275
276 // Test that decode succeeds
277 decodedPointer := reflect.New(reflect.TypeOf(testCase.value))
278 read, err := Unmarshal(testCase.encoding, decodedPointer.Interface())
279 if err != nil {
280 t.Fatalf("Decode error [%s]: %v", label, err)
281 }
282
283 if read != len(testCase.encoding) {
284 t.Fatalf("Decode failed to consume buffer [%s]: %v != %v", label, read, len(testCase.encoding))
285 }
286
287 decodedValue := decodedPointer.Elem().Interface()
288 if !reflect.DeepEqual(decodedValue, testCase.value) {
289 t.Fatalf("Invalid decoded value [%s]: %v != %v", label, decodedValue, testCase.value)
290 }
291
292 t.Logf("PASS [%s]", label)
293 }
294 }