-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathencode_test.go
More file actions
executable file
·969 lines (894 loc) · 21.7 KB
/
encode_test.go
File metadata and controls
executable file
·969 lines (894 loc) · 21.7 KB
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
package bertlv
import (
"bytes"
"encoding/hex"
"errors"
"reflect"
"testing"
)
// Test basic tag and length encoding with proper omitempty handling
func TestTagLengthEncoding(t *testing.T) {
type testStruct struct {
ShortTag1 []byte `bertlv:"1,universal,omitempty"` // Tag 1 (short form)
ShortTag30 []byte `bertlv:"30,universal,omitempty"` // Tag 30 (boundary)
LongTag31 []byte `bertlv:"31,universal,omitempty"` // Tag 31 (long form)
LongTag127 []byte `bertlv:"127,universal,omitempty"` // Tag 127 (long form)
VeryLong []byte `bertlv:"16383,universal,omitempty"` // Tag 16383 (max 2-byte)
}
tests := []struct {
name string
input testStruct
expected string
}{
{
name: "short form tags",
input: testStruct{
ShortTag1: []byte{0x01},
ShortTag30: []byte{0x02},
},
expected: "0101011e0102", // Tag 1 + len 1 + 0x01, Tag 30 + len 1 + 0x02
},
{
name: "long form tags",
input: testStruct{
LongTag31: []byte{0x03},
LongTag127: []byte{0x04},
},
expected: "1f1f0103" + // Tag 31 (0x1F 0x1F) + len 1 + 0x03
"1f7f0104", // Tag 127 (0x1F 0x7F) + len 1 + 0x04
},
{
name: "very long tag",
input: testStruct{
VeryLong: []byte{0x05},
},
expected: "1fff7f0105", // Tag 16383 (0x1F 0xFF 0x7F) + len 1 + 0x05
},
{
name: "empty values with omitempty - should be omitted",
input: testStruct{
ShortTag1: []byte{}, // Empty with omitempty - should not appear
},
expected: "", // Nothing should be encoded
},
{
name: "nil values with omitempty - should be omitted",
input: testStruct{
ShortTag1: nil, // Nil with omitempty - should not appear
},
expected: "", // Nothing should be encoded
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
got := hex.EncodeToString(encoded)
if got != tt.expected {
t.Errorf("Marshal() = %v, want %v", got, tt.expected)
}
})
}
}
// Test omitempty behavior comprehensively
func TestOmitEmpty(t *testing.T) {
type testStruct struct {
StringEmpty string `bertlv:"1,universal,omitempty"`
StringFilled string `bertlv:"2,universal,omitempty"`
IntZero int `bertlv:"3,universal,omitempty"`
IntNonZero int `bertlv:"4,universal,omitempty"`
BoolFalse bool `bertlv:"5,universal,omitempty"`
BoolTrue bool `bertlv:"6,universal,omitempty"`
BytesNil []byte `bertlv:"7,universal,omitempty"`
BytesEmpty []byte `bertlv:"8,universal,omitempty"`
BytesFilled []byte `bertlv:"9,universal,omitempty"`
NoOmitEmpty string `bertlv:"10"` // Should always be included
}
tests := []struct {
name string
input testStruct
expected string
}{
{
name: "all empty values with omitempty",
input: testStruct{
StringEmpty: "",
IntZero: 0,
BoolFalse: false,
BytesNil: nil,
BytesEmpty: []byte{},
NoOmitEmpty: "", // Should still be included
},
expected: "0a00", // Only NoOmitEmpty field (tag 10, len 0)
},
{
name: "mixed empty and non-empty values",
input: testStruct{
StringEmpty: "", // Omitted
StringFilled: "test",
IntZero: 0, // Omitted
IntNonZero: 42,
BoolFalse: false, // Omitted
BoolTrue: true,
BytesNil: nil, // Omitted
BytesEmpty: []byte{}, // Omitted
BytesFilled: []byte{0xAB},
NoOmitEmpty: "always",
},
expected: "020474657374" + // StringFilled: "test"
"04012a" + // IntNonZero: 42
"060101" + // BoolTrue: true
"0901ab" + // BytesFilled: 0xAB
"0a06616c77617973", // NoOmitEmpty: "always"
},
{
name: "only non-empty values",
input: testStruct{
StringFilled: "abc",
IntNonZero: 123,
BoolTrue: true,
BytesFilled: []byte{0x01, 0x02},
NoOmitEmpty: "xyz",
},
expected: "0203616263" + // StringFilled: "abc"
"04017b" + // IntNonZero: 123
"060101" + // BoolTrue: true
"09020102" + // BytesFilled: [0x01, 0x02]
"0a0378797a", // NoOmitEmpty: "xyz"
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
got := hex.EncodeToString(encoded)
if got != tt.expected {
t.Errorf("Marshal() = %v, want %v", got, tt.expected)
}
})
}
}
// Test length encoding variations
func TestLengthEncoding(t *testing.T) {
type testStruct struct {
Field []byte `bertlv:"1"`
}
tests := []struct {
name string
dataSize int
expectedLen string // Just the length encoding part
}{
{
name: "zero length",
dataSize: 0,
expectedLen: "00",
},
{
name: "short form max (127)",
dataSize: 127,
expectedLen: "7f",
},
{
name: "long form min (128)",
dataSize: 128,
expectedLen: "8180", // 0x81 (1 byte follows) 0x80 (128)
},
{
name: "long form 255",
dataSize: 255,
expectedLen: "81ff",
},
{
name: "long form 256",
dataSize: 256,
expectedLen: "820100", // 0x82 (2 bytes follow) 0x01 0x00
},
{
name: "long form 16383",
dataSize: 16383,
expectedLen: "823fff",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input := testStruct{
Field: bytes.Repeat([]byte{0x01}, tt.dataSize),
}
encoded, err := Marshal(input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
// Extract length encoding (skip tag byte)
got := hex.EncodeToString(encoded)
// Tag is "01", then comes length
gotLen := got[2 : 2+len(tt.expectedLen)]
if gotLen != tt.expectedLen {
t.Errorf("Length encoding = %v, want %v", gotLen, tt.expectedLen)
}
})
}
}
// Test primitive type encoding
func TestPrimitiveTypes(t *testing.T) {
type testStruct struct {
String string `bertlv:"1,universal,omitempty"`
Int int `bertlv:"2,universal,omitempty"`
Int64 int64 `bertlv:"3,universal,omitempty"`
Uint uint `bertlv:"4,universal,omitempty"`
Uint64 uint64 `bertlv:"5,universal,omitempty"`
Bool bool `bertlv:"6,universal,omitempty"`
ByteSlice []byte `bertlv:"7,universal,omitempty"`
}
tests := []struct {
name string
input testStruct
expected string
}{
{
name: "string encoding",
input: testStruct{
String: "test",
},
expected: "010474657374", // Tag 1 + len 4 + "test"
},
{
name: "integer encoding",
input: testStruct{
Int: 255,
},
expected: "020200ff", // Tag 2 + len 2 + 0x00FF
},
{
name: "large integer",
input: testStruct{
Int64: 65535,
},
expected: "030300ffff", // Tag 3 + len 3 + 0x00FFFF
},
{
name: "unsigned integer",
input: testStruct{
Uint: 256,
},
expected: "04020100", // Tag 4 + len 2 + 0x0100
},
{
name: "boolean true",
input: testStruct{
Bool: true,
},
expected: "060101", // Tag 6 + len 1 + 0x01
},
{
name: "boolean false - omitted with omitempty",
input: testStruct{
Bool: false,
},
expected: "", // Boolean false with omitempty should be omitted
},
{
name: "byte slice",
input: testStruct{
ByteSlice: []byte{0xDE, 0xAD, 0xBE, 0xEF},
},
expected: "0704deadbeef", // Tag 7 + len 4 + bytes
},
{
name: "empty string - omitted with omitempty",
input: testStruct{
String: "",
},
expected: "", // Empty string with omitempty should be omitted
},
{
name: "zero values - omitted with omitempty",
input: testStruct{
Int: 0,
Uint64: 0,
},
expected: "", // Zero values with omitempty should be omitted
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
got := hex.EncodeToString(encoded)
if got != tt.expected {
t.Errorf("Marshal() = %v, want %v", got, tt.expected)
}
})
}
}
// Test tag classes
func TestTagClasses(t *testing.T) {
type testStruct struct {
Universal string `bertlv:"1,universal"`
Application string `bertlv:"2,application"`
Context string `bertlv:"3,context"`
Private string `bertlv:"4,private"`
}
tests := []struct {
name string
input testStruct
expected string
}{
{
name: "all classes",
input: testStruct{
Universal: "U",
Application: "A",
Context: "C",
Private: "P",
},
expected: "010155" + // Universal: 0x01
"420141" + // Application: 0x42 (0x40 | 0x02)
"830143" + // Context: 0x83 (0x80 | 0x03)
"c40150", // Private: 0xC4 (0xC0 | 0x04)
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
got := hex.EncodeToString(encoded)
if got != tt.expected {
t.Errorf("Marshal() = %v, want %v", got, tt.expected)
}
})
}
}
// Test constructed types (nested structs)
func TestConstructedTypes(t *testing.T) {
type innerStruct struct {
Field1 string `bertlv:"1,universal,omitempty"`
Field2 int `bertlv:"2,universal,omitempty"`
}
type outerStruct struct {
Nested innerStruct `bertlv:"10"`
Simple string `bertlv:"11"`
}
tests := []struct {
name string
input outerStruct
expected string
}{
{
name: "nested struct with values",
input: outerStruct{
Nested: innerStruct{
Field1: "abc",
Field2: 123,
},
Simple: "xyz",
},
expected: "2a08" + // Tag 10 (0x0A) constructed (0x20) = 0x2A, len 8
"0103616263" + // Field1: tag 1, len 3, "abc"
"02017b" + // Field2: tag 2, len 1, 123
"0b0378797a", // Simple: tag 11, len 3, "xyz"
},
{
name: "nested struct with omitempty - empty fields omitted",
input: outerStruct{
Nested: innerStruct{
Field1: "", // Will be omitted due to omitempty
Field2: 0, // Will be omitted due to omitempty
},
Simple: "test",
},
expected: "2a00" + // Tag 10 constructed, len 0 (no fields inside)
"0b0474657374", // Simple: "test"
},
{
name: "nested struct with partial values",
input: outerStruct{
Nested: innerStruct{
Field1: "value", // Has value
Field2: 0, // Will be omitted
},
Simple: "",
},
expected: "2a07010576616c7565" + // Tag 10 constructed, Field1 only
"0b00", // Simple: empty string (no omitempty)
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
got := hex.EncodeToString(encoded)
if got != tt.expected {
t.Errorf("Marshal() = %v, want %v", got, tt.expected)
}
})
}
}
// Test field tags combinations
func TestFieldTags(t *testing.T) {
type testStruct struct {
Required string `bertlv:"1,universal,required"`
Optional string `bertlv:"2,universal,omitempty"`
Normal string `bertlv:"3"`
RequiredEmpty string `bertlv:"4,universal,required,omitempty"`
}
tests := []struct {
name string
input testStruct
expected string
}{
{
name: "all fields present",
input: testStruct{
Required: "req",
Optional: "opt",
Normal: "norm",
RequiredEmpty: "filled",
},
expected: "0103726571" + // Required
"02036f7074" + // Optional
"03046e6f726d" + // Normal
"040666696c6c6564", // RequiredEmpty
},
{
name: "omitempty field empty",
input: testStruct{
Required: "req",
Optional: "", // Should be omitted
Normal: "", // Should be included even if empty
RequiredEmpty: "x",
},
expected: "0103726571" + // Required
"0300" + // Normal (empty)
"040178", // RequiredEmpty
},
{
name: "required but omitempty when empty",
input: testStruct{
Required: "req",
Normal: "norm",
RequiredEmpty: "", // Required but has omitempty, should be omitted when empty
},
expected: "0103726571" + // Required
"03046e6f726d", // Normal
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
got := hex.EncodeToString(encoded)
if got != tt.expected {
t.Errorf("Marshal() = %v, want %v", got, tt.expected)
}
})
}
}
// Test slice encoding
func TestSliceEncoding(t *testing.T) {
type innerStruct struct {
Value int `bertlv:"1,universal,omitempty"`
}
type testStruct struct {
ByteSlice []byte `bertlv:"1,universal,omitempty"`
StructSlice []innerStruct `bertlv:"2,universal,omitempty"`
EmptySlice []innerStruct `bertlv:"3,universal,omitempty"`
}
tests := []struct {
name string
input testStruct
expected string
}{
{
name: "byte slice as primitive",
input: testStruct{
ByteSlice: []byte{0x01, 0x02, 0x03},
},
expected: "0103010203", // Primitive encoding
},
{
name: "struct slice as constructed",
input: testStruct{
StructSlice: []innerStruct{
{Value: 10},
{Value: 20},
},
},
expected: "220301010a" + // Tag 2 constructed, len 3, Value=10
"2203010114", // Tag 2 constructed, len 3, Value=20
},
{
name: "empty slices with omitempty",
input: testStruct{
ByteSlice: []byte{}, // Empty, should be omitted
StructSlice: nil, // Nil, should be omitted
EmptySlice: []innerStruct{}, // Empty, should be omitted
},
expected: "", // All fields omitted
},
{
name: "struct slice with empty elements",
input: testStruct{
StructSlice: []innerStruct{
{Value: 0}, // Value will be omitted due to omitempty
{Value: 5},
},
},
expected: "2200" + // Tag 2 constructed, len 0 (no inner fields due to omitempty)
"2203010105", // Tag 2 constructed, len 3, Value=5
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
got := hex.EncodeToString(encoded)
if got != tt.expected {
t.Errorf("Marshal() = %v, want %v", got, tt.expected)
}
})
}
}
// Test custom marshaler implementation
type customBool bool
func (c customBool) MarshalBERTLV() ([]byte, error) {
if c {
return []byte{0xFF}, nil
}
return []byte{0x00}, nil
}
type customError struct{}
func (c customError) MarshalBERTLV() ([]byte, error) {
return nil, errors.New("custom marshal error")
}
func TestCustomMarshaler(t *testing.T) {
type testStruct struct {
Custom customBool `bertlv:"1"`
}
type errorStruct struct {
Error customError `bertlv:"1"`
}
tests := []struct {
name string
input interface{}
want string
wantErr bool
}{
{
name: "custom marshaler true",
input: testStruct{
Custom: true,
},
want: "0101ff",
wantErr: false,
},
{
name: "custom marshaler false",
input: testStruct{
Custom: false,
},
want: "010100",
wantErr: false,
},
{
name: "custom marshaler error",
input: errorStruct{
Error: customError{},
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Marshal(tt.input)
if tt.wantErr {
if err == nil {
t.Errorf("Marshal() expected error, got none")
}
return
}
if err != nil {
t.Fatalf("Marshal() unexpected error: %v", err)
}
got := hex.EncodeToString(encoded)
if got != tt.want {
t.Errorf("Marshal() = %v, want %v", got, tt.want)
}
})
}
}
// Test pointer types with omitempty
func TestPointerTypes(t *testing.T) {
type testStruct struct {
StringPtr *string `bertlv:"1,universal,omitempty"`
IntPtr *int `bertlv:"2,universal,omitempty"`
BoolPtr *bool `bertlv:"3,universal,omitempty"`
}
strVal := "test"
intVal := 42
boolVal := true
tests := []struct {
name string
input testStruct
expected string
}{
{
name: "all pointers nil with omitempty",
input: testStruct{
StringPtr: nil,
IntPtr: nil,
BoolPtr: nil,
},
expected: "", // All omitted
},
{
name: "all pointers set",
input: testStruct{
StringPtr: &strVal,
IntPtr: &intVal,
BoolPtr: &boolVal,
},
expected: "010474657374" + // StringPtr: "test"
"02012a" + // IntPtr: 42
"030101", // BoolPtr: true
},
{
name: "mixed nil and non-nil pointers",
input: testStruct{
StringPtr: &strVal,
IntPtr: nil, // Omitted
BoolPtr: &boolVal,
},
expected: "010474657374" + // StringPtr: "test"
"030101", // BoolPtr: true
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
got := hex.EncodeToString(encoded)
if got != tt.expected {
t.Errorf("Marshal() = %v, want %v", got, tt.expected)
}
})
}
}
// Test edge cases and error conditions
func TestEdgeCases(t *testing.T) {
tests := []struct {
name string
input interface{}
wantErr bool
}{
{
name: "nil input",
input: nil,
wantErr: true,
},
{
name: "empty struct",
input: struct{}{},
wantErr: false,
},
{
name: "unexported fields",
input: struct {
exported string `bertlv:"1"`
unexported string
}{
exported: "test",
unexported: "ignored",
},
wantErr: false,
},
{
name: "ignored field",
input: struct {
Field1 string `bertlv:"1"`
Field2 string `bertlv:"-"`
}{
Field1: "included",
Field2: "ignored",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := Marshal(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
// Test roundtrip encoding/decoding
func TestRoundtrip(t *testing.T) {
type complexStruct struct {
String string `bertlv:"1"`
Integer int `bertlv:"2"`
Bytes []byte `bertlv:"3"`
Nested struct {
Field1 string `bertlv:"10"`
Field2 bool `bertlv:"11"`
} `bertlv:"4"`
Optional string `bertlv:"5,universal,omitempty"`
}
tests := []struct {
name string
data complexStruct
}{
{
name: "full data",
data: complexStruct{
String: "hello world",
Integer: 42,
Bytes: []byte{0xCA, 0xFE, 0xBA, 0xBE},
Nested: struct {
Field1 string `bertlv:"10"`
Field2 bool `bertlv:"11"`
}{
Field1: "nested",
Field2: true,
},
Optional: "present",
},
},
{
name: "with omitted optional",
data: complexStruct{
String: "test",
Integer: 0,
Bytes: []byte{},
Nested: struct {
Field1 string `bertlv:"10"`
Field2 bool `bertlv:"11"`
}{
Field1: "",
Field2: false,
},
Optional: "", // Should be omitted
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := Marshal(tt.data)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
var decoded complexStruct
err = Unmarshal(encoded, &decoded)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
// For omitempty fields, empty values won't round-trip
// So we need to normalize before comparison
if tt.data.Optional == "" {
decoded.Optional = ""
}
if !reflect.DeepEqual(tt.data, decoded) {
t.Errorf("Roundtrip failed:\noriginal: %+v\ndecoded: %+v", tt.data, decoded)
}
})
}
}
// Benchmark marshaling performance
func BenchmarkMarshalSimple(b *testing.B) {
type simple struct {
Field1 string `bertlv:"1"`
Field2 int `bertlv:"2"`
Field3 []byte `bertlv:"3"`
}
input := simple{
Field1: "benchmark test string",
Field2: 123456,
Field3: bytes.Repeat([]byte{0x42}, 100),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Marshal(input)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkMarshalComplex(b *testing.B) {
type nested struct {
A string `bertlv:"1"`
B int `bertlv:"2"`
}
type complex struct {
Field1 string `bertlv:"1"`
Field2 []nested `bertlv:"2"`
Field3 []byte `bertlv:"3"`
}
input := complex{
Field1: "complex benchmark",
Field2: []nested{
{A: "first", B: 1},
{A: "second", B: 2},
{A: "third", B: 3},
},
Field3: bytes.Repeat([]byte{0xFF}, 256),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Marshal(input)
if err != nil {
b.Fatal(err)
}
}
}
// Regression test: []NamedByteSlice should emit one primitive TLV per element,
// not a constructed wrapper. Previously `shouldBeConstructed` inspected the
// slice type rather than the element type, which flipped the constructed bit
// whenever the Go type was a slice-of-named-byteslice.
func TestSliceOfNamedByteSlice(t *testing.T) {
type AID []byte
type container struct {
AIDs []AID `bertlv:"0x4F"`
}
input := container{
AIDs: []AID{
{0xA0, 0x00, 0x00, 0x00, 0x03},
{0xA0, 0x00, 0x00, 0x00, 0x04},
},
}
encoded, err := Marshal(input)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
got := hex.EncodeToString(encoded)
want := "4f05a0000000034f05a000000004"
if got != want {
t.Errorf("Marshal() = %s, want %s", got, want)
}
var decoded container
if err := Unmarshal(encoded, &decoded); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if !reflect.DeepEqual(input, decoded) {
t.Errorf("roundtrip mismatch:\n got: %+v\nwant: %+v", decoded, input)
}
}
func BenchmarkMarshalWithOmitEmpty(b *testing.B) {
type data struct {
Field1 string `bertlv:"1,universal,omitempty"`
Field2 int `bertlv:"2,universal,omitempty"`
Field3 []byte `bertlv:"3,universal,omitempty"`
Field4 string `bertlv:"4,universal,omitempty"`
Field5 int `bertlv:"5,universal,omitempty"`
}
input := data{
Field1: "value", // Others will be omitted
Field2: 0, // Omitted
Field3: nil, // Omitted
Field4: "", // Omitted
Field5: 0, // Omitted
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Marshal(input)
if err != nil {
b.Fatal(err)
}
}
}