-
Notifications
You must be signed in to change notification settings - Fork 9
/
decoder_test.go
519 lines (430 loc) · 12.9 KB
/
decoder_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
Copyright 2021 Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package structex
import (
"bytes"
"fmt"
"math"
"math/bits"
"testing"
)
const (
bufferSize = 512
)
type testReader struct {
bytes [bufferSize]byte
offset int
nbytes int
}
func newReader(b []byte) *testReader {
var tr = new(testReader)
copy(tr.bytes[:], b)
tr.offset = 0
tr.nbytes = len(b)
return tr
}
func (tr *testReader) ReadByte() (byte, error) {
if tr.offset == tr.nbytes {
return 0, fmt.Errorf("Buffer underrun")
}
b := tr.bytes[tr.offset]
tr.offset++
return b, nil
}
func unpackAndTest(t *testing.T, s interface{}, tr *testReader, testFunc func(t *testing.T, s interface{})) {
if err := Decode(tr, s); err != nil {
t.Error(err)
}
testFunc(t, s)
}
func TestBasicDecoder(t *testing.T) {
type ts struct {
A uint8
B uint16
C uint32
D uint64
}
var s = new(ts)
var tr = newReader([]byte{
0x00,
0x01, 0x00,
0x02, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.A != 0 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x00, s.A)
}
if s.B != 0x0001 {
t.Errorf("Test Value Incorrect: Expected: %#04x Actual: %#04x", 0x0001, s.B)
}
})
}
func TestEndianDecoder(t *testing.T) {
type ts struct {
Big16 uint16 `structex:"big"`
Little16 uint16
Big32 uint32 `structex:"big"`
Little32 uint32
Big64 uint64 `structex:"big"`
Little64 uint64
Big32Forced uint32 `structex:"big"`
Little32Forced uint32 `structex:"little"`
}
var s = new(ts)
var tr = newReader([]byte{
0x01, 0x23,
0x01, 0x23,
0x01, 0x23, 0x45, 0x67,
0x01, 0x23, 0x45, 0x67,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0x89, 0xAB, 0xCD, 0xEF,
0x89, 0xAB, 0xCD, 0xEF})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.Big16 != 0x0123 {
t.Errorf("Invalid big-endian 16-bit value: Expected: %#04x Actual: %#04x", 0x0123, s.Big16)
}
if s.Little16 != bits.ReverseBytes16(0x0123) {
t.Errorf("Invalid little-endian 16-bit value: Expected: %#04x Actual: %#04x", bits.ReverseBytes16(0x0123), s.Little16)
}
if s.Big32 != 0x01234567 {
t.Errorf("Invalid big-endian 32-bit value: Expected: %#08x Actual: %#08x", 0x01234567, s.Big32)
}
if s.Little32 != bits.ReverseBytes32(0x01234567) {
t.Errorf("Invalid little-endian 32-bit value: Expected: %#08x Actual: %#08x", bits.ReverseBytes32(0x01234567), s.Little32)
}
if s.Big64 != 0x0123456789ABCDEF {
t.Errorf("Invalid big-endian 64-bit value: Expected: %#016x Actual: %#016x", 0x0123456789ABCDEF, s.Big64)
}
if s.Little64 != bits.ReverseBytes64(0x0123456789ABCDEF) {
t.Errorf("Invalid little-endian 64-bit value: Expected: %#016x Actual: %#016x", bits.ReverseBytes64(0x0123456789ABCDEF), s.Little64)
}
if s.Big32Forced != 0x89ABCDEF {
t.Errorf("Invalid FORCED big-endian 32-bit value: Expected %#08x Actual: %08x", 0x89ABCDEF, s.Big32Forced)
}
if s.Little32Forced != bits.ReverseBytes32(0x89ABCDEF) {
t.Errorf("Invalid FORCED little-endian 32-bit value: Expected %#08x Actual: %08x", bits.ReverseBytes32(0x89ABCDEF), s.Little32Forced)
}
})
}
func TestBitfieldDecoder(t *testing.T) {
type ts struct {
A uint `bitfield:"3"`
B uint `bitfield:"4"`
C uint `bitfield:"1"`
D uint `bitfield:"12"`
E uint `bitfield:"4"`
}
var s = new(ts)
var tr = newReader([]byte{0xC7, 0xFF, 0x1F})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.A != 0x07 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x07, s.A)
}
if s.B != 0x08 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x08, s.B)
}
if s.C != 0x01 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x08, s.C)
}
if s.D != 0x0FFF {
t.Errorf("Test Value Incorrect: Expected %#03x Actual: %#03x", 0xFFF, s.D)
}
if s.E != 0x1 {
t.Errorf("Test Value Incorrect: Expected: %#x Actual: %#x", 0x1, s.E)
}
})
}
func TestNestedDecoder(t *testing.T) {
type ns struct {
M uint `bitfield:"3"`
N uint `bitfield:"4"`
O uint `bitfield:"1"`
}
type ts struct {
A uint8
B uint8
C ns
}
var s = new(ts)
var tr = newReader([]byte{0x01, 0x02, 0xC7})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.A != 0x01 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x01, s.A)
}
if s.B != 0x02 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x02, s.B)
}
if s.C.M != 0x07 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x07, s.C.M)
}
if s.C.N != 0x08 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x08, s.C.N)
}
if s.C.O != 0x01 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x08, s.C.O)
}
})
}
func TestArrayDecoder(t *testing.T) {
type as struct {
A uint8 `bitfield:"4"`
B uint8 `bitfield:"4"`
}
type ts struct {
Count uint8 `countOf:"Cs"`
Size uint8 `sizeOf:"Ss"`
Cs []as
Ss []as
A [1]byte
}
var s = new(ts)
var tr = newReader([]byte{2, 2, 0x11, 0x22, 0x33, 0x44, 0x55})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.Count != 2 {
t.Errorf("Count Value Incorrect: Expected: %#02x Actual: %#02x", 2, s.Count)
}
if s.Size != 2 {
t.Errorf("Size Value Incorrect: Expected: %#02x Actual: %#02x", 2, s.Size)
}
if s.Cs[0].A != 1 || s.Cs[0].B != 1 {
val := s.Cs[0].A | (s.Cs[0].B << 4)
t.Errorf("Array Value Incorrect: Expected: %#02x Actual: %#02x", 0x11, val)
}
if s.Cs[1].A != 2 || s.Cs[1].B != 2 {
val := s.Cs[1].A | (s.Cs[1].B << 4)
t.Errorf("Array Value Incorrect: Expected: %#02x Actual: %#02x", 0x22, val)
}
if s.Ss[0].A != 3 || s.Ss[0].B != 3 {
val := s.Ss[0].A | (s.Ss[0].B << 4)
t.Errorf("Array Value Incorrect: Expected: %#02x Actual: %#02x", 0x33, val)
}
if s.Ss[1].A != 4 || s.Ss[1].B != 4 {
val := s.Ss[1].A | (s.Ss[1].B << 4)
t.Errorf("Array Value Incorrect: Expected: %#02x Actual: %#02x", 0x44, val)
}
if s.A[0] != 0x55 {
t.Errorf("Array Value Incorrect: Expected %#02x Actual: %#02x", 0x55, s.A[0])
}
})
}
func TestArrayLittleEndian16Decoder(t *testing.T) {
type ts struct {
Count uint8 `countOf:"Ts"`
Size uint8 `sizeOf:"Ts"`
Ts [2]uint16 `little:""`
}
var s = new(ts)
var tr = newReader([]byte{2, 4, 0x34, 0x12, 0x78, 0x56})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.Count != 2 {
t.Errorf("Count Value Incorrect: Expected: %#02x Actual: %#02x", 2, s.Count)
}
if s.Size != 4 {
t.Errorf("Size Value Incorrect: Expected: %#02x Actual: %#02x", 4, s.Size)
}
if s.Ts[0] != 0x1234 {
t.Errorf("Array Value Incorrect: Expected %#02x Actual: %#02x", 0x1234, s.Ts[0])
}
if s.Ts[1] != 0x5678 {
t.Errorf("Array Value Incorrect: Expected %#02x Actual: %#02x", 0x5678, s.Ts[1])
}
})
}
func TestArrayBigEndian16Decoder(t *testing.T) {
type ts struct {
Count uint8 `countOf:"Ts"`
Size uint8 `sizeOf:"Ts"`
Ts [2]uint16 `big:""`
}
var s = new(ts)
var tr = newReader([]byte{2, 4, 0x12, 0x34, 0x56, 0x78})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.Count != 2 {
t.Errorf("Count Value Incorrect: Expected: %#02x Actual: %#02x", 2, s.Count)
}
if s.Size != 4 {
t.Errorf("Size Value Incorrect: Expected: %#02x Actual: %#02x", 4, s.Size)
}
if s.Ts[0] != 0x1234 {
t.Errorf("Array Value Incorrect: Expected %#02x Actual: %#02x", 0x1234, s.Ts[0])
}
if s.Ts[1] != 0x5678 {
t.Errorf("Array Value Incorrect: Expected %#02x Actual: %#02x", 0x5678, s.Ts[1])
}
})
}
func TestHeaderStyleDecoder(t *testing.T) {
type hdr struct {
Count uint8 `countOf:"Details"`
}
type detail struct {
Val uint8
}
type ts struct {
Hdr hdr
Details []detail
}
s := new(ts)
tr := newReader([]byte{4, 0, 1, 2, 3})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
s := i.(*ts)
if s.Hdr.Count != 4 {
t.Errorf("Count Value Incorrect: Expected: %d Actual: %d", 4, s.Hdr.Count)
}
if len(s.Details) != 4 {
t.Errorf("Details Array Len Incorrect: Expected: %d Actual: %d", 4, len(s.Details))
}
for i := uint8(0); i < s.Hdr.Count; i++ {
if s.Details[i].Val != i {
t.Errorf("Detail Value Incorrect: Expected: %d Actual: %d", i, s.Details[i].Val)
}
}
})
}
func TestBytesBuffer(t *testing.T) {
type ts struct {
A byte
B byte
}
var s = new(ts)
b := bytes.NewBuffer([]byte{0x01, 0x02})
if err := DecodeByteBuffer(b, s); err != nil {
t.Errorf("Unpack Buffer failed: Err: %v", err)
}
if s.A != 0x01 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x01, s.A)
}
if s.B != 0x02 {
t.Errorf("Test Value Incorrect: Expected: %#02x Actual: %#02x", 0x02, s.B)
}
}
func TestTruncate(t *testing.T) {
type ts struct {
A [1024]byte `truncate:""`
}
var s = new(ts)
if err := DecodeByteBuffer(bytes.NewBuffer([]byte{0}), s); err != nil {
t.Errorf("Truncate test failed: %s", err)
}
}
func TestAlignmentDecoder(t *testing.T) {
type ts struct {
Pad uint8
Aligned uint32 `align:"4"`
}
var s = new(ts)
tr := newReader([]byte{0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
s := i.(*ts)
if s.Aligned != 0xFFFFFFFF {
t.Errorf("Unexpected aligned parameter: Expected: %#08x Actual: %#08x", 0xFFFFFFFF, s.Aligned)
}
})
}
func TestBoolDecoder(t *testing.T) {
type ts struct {
IsD bool `bitfield:"1"`
ValC uint8 `bitfield:"2"`
IsC bool `bitfield:"1"`
IsB bool `bitfield:"1"`
ValB uint8 `bitfield:"2"`
IsA bool `bitfield:"1"`
ValD uint8
ValF uint16 `bitfield:"2"`
IsH bool `bitfield:"1"`
IsG bool `bitfield:"1"`
ValE uint16 `bitfield:"10"`
IsF bool `bitfield:"1"`
IsE bool `bitfield:"1"`
}
var s = new(ts)
var tr = newReader([]byte{0b10101101, 0x12, 0b11110101, 0b10001111})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if !s.IsA {
t.Errorf("IsA Value Incorrect: Expected: %v Actual: %v", true, s.IsA)
}
if s.ValB != 0b01 {
t.Errorf("ValB Value Incorrect: Expected: %#02x Actual: %#02x", 0b01, s.ValB)
}
if s.IsB {
t.Errorf("IsB Value Incorrect: Expected: %v Actual: %v", false, s.IsB)
}
if !s.IsC {
t.Errorf("IsC Value Incorrect: Expected: %v Actual: %v", true, s.IsC)
}
if s.ValC != 0b10 {
t.Errorf("ValC Value Incorrect: Expected: %#02x Actual: %#02x", 0b10, s.ValC)
}
if !s.IsD {
t.Errorf("IsB Value Incorrect: Expected: %v Actual: %v", true, s.IsD)
}
if s.ValD != 0x12 {
t.Errorf("ValC Value Incorrect: Expected: %#02x Actual: %#02x", 0x12, s.ValD)
}
if !s.IsE {
t.Errorf("IsE Value Incorrect: Expected: %v Actual: %v", true, s.IsE)
}
if s.IsF {
t.Errorf("IsF Value Incorrect: Expected: %v Actual: %v", false, s.IsF)
}
if s.ValE != 0b0011111111 {
t.Errorf("ValE Value Incorrect: Expected: %#02x Actual: %#02x", 0x0011111111, s.ValE)
}
if s.IsG {
t.Errorf("IsG Value Incorrect: Expected: %v Actual: %v", false, s.IsG)
}
if !s.IsH {
t.Errorf("IsH Value Incorrect: Expected: %v Actual: %v", true, s.IsH)
}
})
}
func TestSignedDecoder(t *testing.T) {
type ts struct {
A int8
B int8 `bitfield:"8"`
C int8 `bitfield:"3"`
Dummy uint8 `bitfield:"5"`
}
var s = new(ts)
var tr = newReader([]byte{-math.MinInt8, -math.MinInt8, 0b11111100})
unpackAndTest(t, s, tr, func(t *testing.T, i interface{}) {
var s = i.(*ts)
if s.A != math.MinInt8 {
t.Errorf("A Value Incorrect: Expected: %d Actual: %d", math.MinInt8, s.A)
}
if s.B != math.MinInt8 {
t.Errorf("B Value Incorrect: Expected: %d Actual: %d", math.MinInt8, s.B)
}
if s.C != -4 {
t.Errorf("C Value Incorrect: Expected: %d Actual: %d", -4, s.C)
}
if s.Dummy != 0b11111 {
t.Errorf("Dummy Value incorrect: Expected: %#b Actual: %#b", 0b111111, s.Dummy)
}
})
}