This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
codec_test.go
621 lines (586 loc) · 12.4 KB
/
codec_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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package lcs
import (
"encoding/hex"
"errors"
"reflect"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type testCase struct {
v interface{}
b []byte
skipMarshal bool
skipUnmarshal bool
errMarshal error
errUnmarshal error
name string
}
func runTest(t *testing.T, cases []*testCase) {
var b []byte
var err error
for idx, c := range cases {
var name string
if c.name == "" {
name = strconv.Itoa(idx)
} else {
name = c.name
}
if !c.skipMarshal {
t.Run(name+"_marshal", func(t *testing.T) {
b, err = Marshal(c.v)
if c.errMarshal != nil {
assert.EqualError(t, err, c.errMarshal.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, c.b, b)
}
// t.Logf("Case #%d(%s) marshal: Done", idx, c.name)
})
}
if !c.skipUnmarshal {
t.Run(name+"_unmarshal", func(t *testing.T) {
v := reflect.New(reflect.TypeOf(c.v))
err = Unmarshal(c.b, v.Interface())
if c.errUnmarshal != nil {
assert.EqualError(t, err, c.errUnmarshal.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, c.v, v.Elem().Interface())
}
// t.Logf("Case #%d(%s) unmarshal: Done", idx, c.name)
})
}
}
}
func hexMustDecode(s string) []byte {
b, err := hex.DecodeString(strings.ReplaceAll(s, " ", ""))
if err != nil {
panic(err)
}
return b
}
func TestBool(t *testing.T) {
vTrue := true
type Bool bool
vTrue2 := Bool(true)
runTest(t, []*testCase{
{
v: bool(true),
b: []byte{1},
name: "bool true",
},
{
v: bool(false),
b: []byte{0},
name: "bool false",
},
{
v: &vTrue,
b: []byte{1},
name: "ptr to bool",
},
{
v: &vTrue2,
b: []byte{1},
name: "ptr to alias of bool",
},
})
}
func TestInts(t *testing.T) {
runTest(t, []*testCase{
{
v: int8(-1),
b: []byte{0xFF},
name: "int8 neg",
},
{
v: uint8(1),
b: []byte{1},
name: "uint8 pos",
},
{
v: int16(-4660),
b: hexMustDecode("CCED"),
name: "int16 neg",
},
{
v: uint16(4660),
b: hexMustDecode("3412"),
name: "uint16 pos",
},
{
v: int32(-305419896),
b: hexMustDecode("88A9CBED"),
name: "int32 neg",
},
{
v: uint32(305419896),
b: hexMustDecode("78563412"),
name: "uint32 pos",
},
{
v: int64(-1311768467750121216),
b: hexMustDecode("0011325487A9CBED"),
name: "int64 neg",
},
{
v: uint64(1311768467750121216),
b: hexMustDecode("00EFCDAB78563412"),
name: "uint64 pos",
},
})
}
func TestBasicSlice(t *testing.T) {
runTest(t, []*testCase{
{
v: []byte{0x11, 0x22, 0x33, 0x44, 0x55},
b: hexMustDecode("05 11 22 33 44 55"),
name: "byte slice",
},
{
v: []uint16{0x11, 0x22},
b: hexMustDecode("02 1100 2200"),
name: "uint16 slice",
},
{
v: make([]uint8, 130),
b: hexMustDecode("8201 " + strings.Repeat("00", 130)),
name: "uint8 long slice",
},
{
v: "ሰማይ አይታረስ ንጉሥ አይከሰስ።",
b: hexMustDecode("36E188B0E1889BE18BAD20E18AA0E18BADE189B3E188A8E188B520E18A95E18C89E188A520E18AA0E18BADE18AA8E188B0E188B5E18DA2"),
name: "utf8 string",
},
})
}
func Test2DSlice(t *testing.T) {
runTest(t, []*testCase{
{
v: [][]byte{{0x01, 0x02}, {0x11, 0x12, 0x13}, {0x21}},
b: hexMustDecode("03 02 0102 03 111213 01 21"),
name: "2d byte slice",
},
{
v: []string{"hello", "world"},
b: hexMustDecode("02 05 68656c6c6f 05 776f726c64"),
name: "string slice",
},
})
}
func TestBasicStruct(t *testing.T) {
type MyStruct struct {
Boolean bool
Bytes []byte
Label string
unexported uint32
}
type Wrapper struct {
Inner *MyStruct
Name string
}
runTest(t, []*testCase{
{
v: struct{}{},
b: nil,
name: "empty struct",
},
{
v: MyStruct{
Boolean: true,
Bytes: []byte{0x11, 0x22},
Label: "hello",
},
b: hexMustDecode("01 02 11 22 05 68656c6c6f"),
name: "struct with unexported fields",
},
{
v: &MyStruct{
Boolean: true,
Bytes: []byte{0x11, 0x22},
Label: "hello",
},
b: hexMustDecode("01 02 11 22 05 68656c6c6f"),
name: "pointer to struct",
},
{
v: Wrapper{
Inner: &MyStruct{
Boolean: true,
Bytes: []byte{0x11, 0x22},
Label: "hello",
},
Name: "world",
},
b: hexMustDecode("01 02 11 22 05 68656c6c6f 05 776f726c64"),
name: "nested struct",
},
{
v: &Wrapper{
Inner: &MyStruct{
Boolean: true,
Bytes: []byte{0x11, 0x22},
Label: "hello",
},
Name: "world",
},
b: hexMustDecode("01 02 1122 05 68656c6c6f 05 776f726c64"),
name: "pointer to nested struct",
},
})
}
func TestStructWithFixedLenMember(t *testing.T) {
type MyStruct struct {
Str string `lcs:"len=2"`
Bytes []byte `lcs:"len=4"`
OptionalBytes []byte `lcs:"len=4,optional"`
}
runTest(t, []*testCase{
{
v: MyStruct{
Str: "12",
Bytes: []byte{0x11, 0x22},
},
errMarshal: errors.New("actual len not equal to fixed len"),
name: "struct with wrong fixed len (bytes)",
skipUnmarshal: true,
},
{
v: MyStruct{
Str: "",
Bytes: []byte{0x11, 0x22, 0x33, 0x44},
},
errMarshal: errors.New("actual len not equal to fixed len"),
name: "struct with wrong fixed len (string)",
skipUnmarshal: true,
},
{
v: MyStruct{
Str: "12",
Bytes: []byte{0x11, 0x22, 0x33, 0x44},
},
b: hexMustDecode("31 32 11223344 00"),
name: "struct with fixed len",
},
{
v: MyStruct{
Str: "12",
Bytes: []byte{0x11, 0x22, 0x33, 0x44},
OptionalBytes: []byte{0x55, 0x66, 0x77, 0x88},
},
b: hexMustDecode("3132 11223344 01 55667788"),
name: "struct with optional fixed len",
},
})
}
func TestArray(t *testing.T) {
runTest(t, []*testCase{
{
v: [4]byte{0x11, 0x22, 0x33, 0x44},
b: hexMustDecode("11223344"),
name: "byte array",
},
{
v: [2]uint32{0x11, 0x22},
b: hexMustDecode("11000000 22000000"),
name: "uint32 array",
},
})
}
func TestRecursiveStruct(t *testing.T) {
type StructTag struct {
Address []byte
Module string
Name string
TypeParams []*StructTag
}
runTest(t, []*testCase{
{
v: &StructTag{
Address: []byte{0x11, 0x22},
Module: "hello",
Name: "world",
TypeParams: []*StructTag{},
},
b: hexMustDecode("02 1122 05 68656c6c6f 05 776f726c64 00"),
name: "recursive struct",
},
})
}
func TestOptional(t *testing.T) {
type Wrapper struct {
Ignored int `lcs:"-"`
Name *string `lcs:"optional"`
}
hello := "hello"
type Wrapper2 struct {
Slice []byte `lcs:"optional"`
}
type Wrapper3 struct {
Map map[uint8]uint8 `lcs:"optional"`
}
runTest(t, []*testCase{
{
v: Wrapper{
Name: &hello,
},
b: hexMustDecode("01 05 68656c6c6f"),
name: "struct with set optional fields",
},
{
v: Wrapper{},
b: hexMustDecode("00"),
name: "struct with unset optional fields",
},
{
v: Wrapper2{
Slice: []byte(hello),
},
b: hexMustDecode("01 05 68656c6c6f"),
name: "struct with set optional slice",
},
{
v: Wrapper2{},
b: hexMustDecode("00"),
name: "struct with unset optional slice",
},
{
v: Wrapper3{
Map: map[uint8]uint8{1: 2},
},
b: hexMustDecode("01 01 01 02"),
name: "struct with set optional map",
},
{
v: Wrapper3{},
b: hexMustDecode("00"),
name: "struct with unset optional map",
},
})
}
func TestMap(t *testing.T) {
runTest(t, []*testCase{
{
v: map[uint8]string{1: "hello", 2: "world"},
b: hexMustDecode("02 01 05 68656c6c6f 02 05 776f726c64"),
name: "map[uint8]string",
},
{
v: map[string]uint8{"hello": 1, "world": 2},
b: hexMustDecode("02 05 68656c6c6f 01 05 776f726c64 02"),
name: "map[string]uint8",
},
})
}
type Option0 struct {
Data uint32
}
type Option1 struct{}
type Option2 bool
type Option3 []byte
type isOption interface {
isOption()
}
type Option struct {
Option isOption `lcs:"enum=option"`
}
type OptionalOption struct {
Option isOption `lcs:"optional,enum=option"`
}
func (*Option0) isOption() {}
func (Option1) isOption() {}
func (Option2) isOption() {}
func (Option3) isOption() {}
var optionEnumDef = []EnumVariant{
{
Name: "option",
Value: 0,
Template: (*Option0)(nil),
},
{
Name: "option",
Value: 1,
Template: Option1{},
},
{
Name: "option",
Value: 2,
Template: Option2(false),
},
{
Name: "option",
Value: 3,
Template: Option3(nil),
},
}
func (*Option) EnumTypes() []EnumVariant { return optionEnumDef }
func (*OptionalOption) EnumTypes() []EnumVariant { return optionEnumDef }
func TestEnum(t *testing.T) {
runTest(t, []*testCase{
{
v: &Option{
Option: &Option0{5},
},
b: hexMustDecode("00 0500 0000"),
name: "ptr to struct with ptr enum variant",
},
{
v: &Option{
Option: Option1{},
},
b: hexMustDecode("01"),
name: "ptr to struct with non-ptr empty variant",
},
{
v: &Option{
Option: Option2(true),
},
b: hexMustDecode("02 01"),
name: "ptr to struct with real value as enum variant",
},
{
v: &Option{
Option: Option3([]byte{0x11, 0x22}),
},
b: hexMustDecode("03 02 11 22"),
name: "ptr to struct with slice as enum variant",
},
{
v: &Option{
Option: Option3([]byte{}),
},
b: hexMustDecode("03 00"),
name: "ptr to struct with nil slice as enum variant",
},
{
v: Option{
Option: Option1{},
},
b: hexMustDecode("01"),
name: "non-ptr struct with variant",
},
{
v: OptionalOption{},
name: "nil enum on optional field",
b: hexMustDecode("00"),
},
{
v: Option{},
name: "nil variant on non-optional field",
skipUnmarshal: true,
errMarshal: errors.New("non-optional enum value is nil"),
},
})
}
type OptionWrap struct {
OptionStruct Option
OptionStructPtr *Option
}
func TestEnumInStruct(t *testing.T) {
runTest(t, []*testCase{
{
v: &OptionWrap{
OptionStruct: Option{
Option: Option3([]byte{0x11, 0x22}),
},
OptionStructPtr: &Option{
Option: Option1{},
},
},
b: hexMustDecode("03 02 1122 01"),
name: "enum struct in struct",
},
{
v: &OptionWrap{
OptionStructPtr: &Option{
Option: Option3([]byte{0x11, 0x22}),
},
OptionStruct: Option{
Option: Option1{},
},
},
b: hexMustDecode("01 03 02 1122"),
name: "enum struct in struct 2",
},
})
}
type OptionSlice struct {
Option []isOption `lcs:"enum=option"`
}
func (*OptionSlice) EnumTypes() []EnumVariant {
return []EnumVariant{
{
Name: "option",
Value: 0,
Template: (*Option0)(nil),
},
{
Name: "option",
Value: 1,
Template: Option1{},
},
{
Name: "option",
Value: 2,
Template: Option2(false),
},
}
}
func TestEnumSlice(t *testing.T) {
runTest(t, []*testCase{
{
v: &OptionSlice{
Option: []isOption{
&Option0{5},
Option1{},
Option2(true),
},
},
b: hexMustDecode("03 00 05000000 01 02 01"),
name: "enum slice",
},
})
}
type OptionSlice2D struct {
Option [][]isOption `lcs:"enum=option"`
}
func (*OptionSlice2D) EnumTypes() []EnumVariant {
return []EnumVariant{
{
Name: "option",
Value: 0,
Template: (*Option0)(nil),
},
{
Name: "option",
Value: 1,
Template: (*Option1)(nil),
},
{
Name: "option",
Value: 2,
Template: Option2(false),
},
}
}
func TestEnum2DSlice(t *testing.T) {
runTest(t, []*testCase{
{
v: &OptionSlice2D{
Option: [][]isOption{
{
&Option0{5},
Option2(true),
},
{
Option2(false),
},
},
},
b: hexMustDecode("02 02 00 05000000 02 01 01 02 00"),
name: "2D enum slice",
},
})
}