-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_test.go
1529 lines (1409 loc) · 35 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
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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gob
import (
"bytes"
"errors"
"flag"
"math"
"math/rand"
"reflect"
"strings"
"testing"
"time"
)
var doFuzzTests = flag.Bool("gob.fuzz", false, "run the fuzz tests, which are large and very slow")
// Guarantee encoding format by comparing some encodings to hand-written values
type EncodeT struct {
x uint64
b []byte
}
var encodeT = []EncodeT{
{0x00, []byte{0x00}},
{0x0F, []byte{0x0F}},
{0xFF, []byte{0xFF, 0xFF}},
{0xFFFF, []byte{0xFE, 0xFF, 0xFF}},
{0xFFFFFF, []byte{0xFD, 0xFF, 0xFF, 0xFF}},
{0xFFFFFFFF, []byte{0xFC, 0xFF, 0xFF, 0xFF, 0xFF}},
{0xFFFFFFFFFF, []byte{0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
{0xFFFFFFFFFFFF, []byte{0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
{0xFFFFFFFFFFFFFF, []byte{0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
{0xFFFFFFFFFFFFFFFF, []byte{0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
{0x1111, []byte{0xFE, 0x11, 0x11}},
{0x1111111111111111, []byte{0xF8, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}},
{0x8888888888888888, []byte{0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88}},
{1 << 63, []byte{0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
}
// testError is meant to be used as a deferred function to turn a panic(gobError) into a
// plain test.Error call.
func testError(t *testing.T) {
if e := recover(); e != nil {
t.Error(e.(gobError).err) // Will re-panic if not one of our errors, such as a runtime error.
}
}
func newDecBuffer(data []byte) *decBuffer {
return &decBuffer{
data: data,
}
}
// Test basic encode/decode routines for unsigned integers
func TestUintCodec(t *testing.T) {
defer testError(t)
b := new(encBuffer)
encState := newEncoderState(b)
for _, tt := range encodeT {
b.Reset()
encState.encodeUint(tt.x)
if !bytes.Equal(tt.b, b.Bytes()) {
t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes())
}
}
for u := uint64(0); ; u = (u + 1) * 7 {
b.Reset()
encState.encodeUint(u)
decState := newDecodeState(newDecBuffer(b.Bytes()))
v := decState.decodeUint()
if u != v {
t.Errorf("Encode/Decode: sent %#x received %#x", u, v)
}
if u&(1<<63) != 0 {
break
}
}
}
func verifyInt(i int64, t *testing.T) {
defer testError(t)
var b = new(encBuffer)
encState := newEncoderState(b)
encState.encodeInt(i)
decState := newDecodeState(newDecBuffer(b.Bytes()))
j := decState.decodeInt()
if i != j {
t.Errorf("Encode/Decode: sent %#x received %#x", uint64(i), uint64(j))
}
}
// Test basic encode/decode routines for signed integers
func TestIntCodec(t *testing.T) {
for u := uint64(0); ; u = (u + 1) * 7 {
// Do positive and negative values
i := int64(u)
verifyInt(i, t)
verifyInt(-i, t)
verifyInt(^i, t)
if u&(1<<63) != 0 {
break
}
}
verifyInt(-1<<63, t) // a tricky case
}
// The result of encoding a true boolean with field number 7
var boolResult = []byte{0x07, 0x01}
// The result of encoding a number 17 with field number 7
var signedResult = []byte{0x07, 2 * 17}
var unsignedResult = []byte{0x07, 17}
var floatResult = []byte{0x07, 0xFE, 0x31, 0x40}
// The result of encoding a number 17+19i with field number 7
var complexResult = []byte{0x07, 0xFE, 0x31, 0x40, 0xFE, 0x33, 0x40}
// The result of encoding "hello" with field number 7
var bytesResult = []byte{0x07, 0x05, 'h', 'e', 'l', 'l', 'o'}
func newDecodeState(buf *decBuffer) *decoderState {
d := new(decoderState)
d.b = buf
return d
}
func newEncoderState(b *encBuffer) *encoderState {
b.Reset()
state := &encoderState{enc: nil, b: b}
state.fieldnum = -1
return state
}
// Test instruction execution for encoding.
// Do not run the machine yet; instead do individual instructions crafted by hand.
func TestScalarEncInstructions(t *testing.T) {
var b = new(encBuffer)
// bool
{
var data bool = true
instr := &encInstr{encBool, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(boolResult, b.Bytes()) {
t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Bytes())
}
}
// int
{
b.Reset()
var data int = 17
instr := &encInstr{encInt, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(signedResult, b.Bytes()) {
t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Bytes())
}
}
// uint
{
b.Reset()
var data uint = 17
instr := &encInstr{encUint, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(unsignedResult, b.Bytes()) {
t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Bytes())
}
}
// int8
{
b.Reset()
var data int8 = 17
instr := &encInstr{encInt, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(signedResult, b.Bytes()) {
t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Bytes())
}
}
// uint8
{
b.Reset()
var data uint8 = 17
instr := &encInstr{encUint, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(unsignedResult, b.Bytes()) {
t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
}
}
// int16
{
b.Reset()
var data int16 = 17
instr := &encInstr{encInt, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(signedResult, b.Bytes()) {
t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Bytes())
}
}
// uint16
{
b.Reset()
var data uint16 = 17
instr := &encInstr{encUint, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(unsignedResult, b.Bytes()) {
t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
}
}
// int32
{
b.Reset()
var data int32 = 17
instr := &encInstr{encInt, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(signedResult, b.Bytes()) {
t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Bytes())
}
}
// uint32
{
b.Reset()
var data uint32 = 17
instr := &encInstr{encUint, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(unsignedResult, b.Bytes()) {
t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
}
}
// int64
{
b.Reset()
var data int64 = 17
instr := &encInstr{encInt, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(signedResult, b.Bytes()) {
t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Bytes())
}
}
// uint64
{
b.Reset()
var data uint64 = 17
instr := &encInstr{encUint, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(unsignedResult, b.Bytes()) {
t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
}
}
// float32
{
b.Reset()
var data float32 = 17
instr := &encInstr{encFloat, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(floatResult, b.Bytes()) {
t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Bytes())
}
}
// float64
{
b.Reset()
var data float64 = 17
instr := &encInstr{encFloat, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(floatResult, b.Bytes()) {
t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Bytes())
}
}
// bytes == []uint8
{
b.Reset()
data := []byte("hello")
instr := &encInstr{encUint8Array, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(bytesResult, b.Bytes()) {
t.Errorf("bytes enc instructions: expected % x got % x", bytesResult, b.Bytes())
}
}
// string
{
b.Reset()
var data string = "hello"
instr := &encInstr{encString, 6, nil, 0}
state := newEncoderState(b)
instr.op(instr, state, reflect.ValueOf(data))
if !bytes.Equal(bytesResult, b.Bytes()) {
t.Errorf("string enc instructions: expected % x got % x", bytesResult, b.Bytes())
}
}
}
func execDec(instr *decInstr, state *decoderState, t *testing.T, value reflect.Value) {
defer testError(t)
v := int(state.decodeUint())
if v+state.fieldnum != 6 {
t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum)
}
instr.op(instr, state, value.Elem())
state.fieldnum = 6
}
func newDecodeStateFromData(data []byte) *decoderState {
b := newDecBuffer(data)
state := newDecodeState(b)
state.fieldnum = -1
return state
}
// Test instruction execution for decoding.
// Do not run the machine yet; instead do individual instructions crafted by hand.
func TestScalarDecInstructions(t *testing.T) {
ovfl := errors.New("overflow")
// bool
{
var data bool
instr := &decInstr{decBool, 6, nil, ovfl}
state := newDecodeStateFromData(boolResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != true {
t.Errorf("bool a = %v not true", data)
}
}
// int
{
var data int
instr := &decInstr{decOpTable[reflect.Int], 6, nil, ovfl}
state := newDecodeStateFromData(signedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("int a = %v not 17", data)
}
}
// uint
{
var data uint
instr := &decInstr{decOpTable[reflect.Uint], 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("uint a = %v not 17", data)
}
}
// int8
{
var data int8
instr := &decInstr{decInt8, 6, nil, ovfl}
state := newDecodeStateFromData(signedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("int8 a = %v not 17", data)
}
}
// uint8
{
var data uint8
instr := &decInstr{decUint8, 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("uint8 a = %v not 17", data)
}
}
// int16
{
var data int16
instr := &decInstr{decInt16, 6, nil, ovfl}
state := newDecodeStateFromData(signedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("int16 a = %v not 17", data)
}
}
// uint16
{
var data uint16
instr := &decInstr{decUint16, 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("uint16 a = %v not 17", data)
}
}
// int32
{
var data int32
instr := &decInstr{decInt32, 6, nil, ovfl}
state := newDecodeStateFromData(signedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("int32 a = %v not 17", data)
}
}
// uint32
{
var data uint32
instr := &decInstr{decUint32, 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("uint32 a = %v not 17", data)
}
}
// uintptr
{
var data uintptr
instr := &decInstr{decOpTable[reflect.Uintptr], 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("uintptr a = %v not 17", data)
}
}
// int64
{
var data int64
instr := &decInstr{decInt64, 6, nil, ovfl}
state := newDecodeStateFromData(signedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("int64 a = %v not 17", data)
}
}
// uint64
{
var data uint64
instr := &decInstr{decUint64, 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("uint64 a = %v not 17", data)
}
}
// float32
{
var data float32
instr := &decInstr{decFloat32, 6, nil, ovfl}
state := newDecodeStateFromData(floatResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("float32 a = %v not 17", data)
}
}
// float64
{
var data float64
instr := &decInstr{decFloat64, 6, nil, ovfl}
state := newDecodeStateFromData(floatResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17 {
t.Errorf("float64 a = %v not 17", data)
}
}
// complex64
{
var data complex64
instr := &decInstr{decOpTable[reflect.Complex64], 6, nil, ovfl}
state := newDecodeStateFromData(complexResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17+19i {
t.Errorf("complex a = %v not 17+19i", data)
}
}
// complex128
{
var data complex128
instr := &decInstr{decOpTable[reflect.Complex128], 6, nil, ovfl}
state := newDecodeStateFromData(complexResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != 17+19i {
t.Errorf("complex a = %v not 17+19i", data)
}
}
// bytes == []uint8
{
var data []byte
instr := &decInstr{decUint8Slice, 6, nil, ovfl}
state := newDecodeStateFromData(bytesResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if string(data) != "hello" {
t.Errorf(`bytes a = %q not "hello"`, string(data))
}
}
// string
{
var data string
instr := &decInstr{decString, 6, nil, ovfl}
state := newDecodeStateFromData(bytesResult)
execDec(instr, state, t, reflect.ValueOf(&data))
if data != "hello" {
t.Errorf(`bytes a = %q not "hello"`, data)
}
}
}
func TestEndToEnd(t *testing.T) {
type T2 struct {
T string
}
type T3 struct {
X float64
Z *int
}
type T1 struct {
A, B, C int
M map[string]*float64
M2 map[int]T3
Mstring map[string]string
Mintptr map[int]*int
Mcomp map[complex128]complex128
Marr map[[2]string][2]*float64
EmptyMap map[string]int // to check that we receive a non-nil map.
N *[3]float64
Strs *[2]string
Int64s *[]int64
RI complex64
S string
Y []byte
T *T2
}
pi := 3.14159
e := 2.71828
two := 2.0
meaning := 42
fingers := 5
s1 := "string1"
s2 := "string2"
var comp1 complex128 = complex(1.0, 1.0)
var comp2 complex128 = complex(1.0, 1.0)
var arr1 [2]string
arr1[0] = s1
arr1[1] = s2
var arr2 [2]string
arr2[0] = s2
arr2[1] = s1
var floatArr1 [2]*float64
floatArr1[0] = &pi
floatArr1[1] = &e
var floatArr2 [2]*float64
floatArr2[0] = &e
floatArr2[1] = &two
t1 := &T1{
A: 17,
B: 18,
C: -5,
M: map[string]*float64{"pi": &pi, "e": &e},
M2: map[int]T3{4: {X: pi, Z: &meaning}, 10: {X: e, Z: &fingers}},
Mstring: map[string]string{"pi": "3.14", "e": "2.71"},
Mintptr: map[int]*int{meaning: &fingers, fingers: &meaning},
Mcomp: map[complex128]complex128{comp1: comp2, comp2: comp1},
Marr: map[[2]string][2]*float64{arr1: floatArr1, arr2: floatArr2},
EmptyMap: make(map[string]int),
N: &[3]float64{1.5, 2.5, 3.5},
Strs: &[2]string{s1, s2},
Int64s: &[]int64{77, 89, 123412342134},
RI: 17 - 23i,
S: "Now is the time",
Y: []byte("hello, sailor"),
T: &T2{"this is T2"},
}
b := new(bytes.Buffer)
err := NewEncoder(b).Encode(t1)
if err != nil {
t.Error("encode:", err)
}
var _t1 T1
err = NewDecoder(b).Decode(&_t1)
if err != nil {
t.Fatal("decode:", err)
}
if !reflect.DeepEqual(t1, &_t1) {
t.Errorf("encode expected %v got %v", *t1, _t1)
}
// Be absolutely sure the received map is non-nil.
if t1.EmptyMap == nil {
t.Errorf("nil map sent")
}
if _t1.EmptyMap == nil {
t.Errorf("nil map received")
}
}
func TestOverflow(t *testing.T) {
type inputT struct {
Maxi int64
Mini int64
Maxu uint64
Maxf float64
Minf float64
Maxc complex128
Minc complex128
}
var it inputT
var err error
b := new(bytes.Buffer)
enc := NewEncoder(b)
dec := NewDecoder(b)
// int8
b.Reset()
it = inputT{
Maxi: math.MaxInt8 + 1,
}
type outi8 struct {
Maxi int8
Mini int8
}
var o1 outi8
enc.Encode(it)
err = dec.Decode(&o1)
if err == nil || err.Error() != `value for "Maxi" out of range` {
t.Error("wrong overflow error for int8:", err)
}
it = inputT{
Mini: math.MinInt8 - 1,
}
b.Reset()
enc.Encode(it)
err = dec.Decode(&o1)
if err == nil || err.Error() != `value for "Mini" out of range` {
t.Error("wrong underflow error for int8:", err)
}
// int16
b.Reset()
it = inputT{
Maxi: math.MaxInt16 + 1,
}
type outi16 struct {
Maxi int16
Mini int16
}
var o2 outi16
enc.Encode(it)
err = dec.Decode(&o2)
if err == nil || err.Error() != `value for "Maxi" out of range` {
t.Error("wrong overflow error for int16:", err)
}
it = inputT{
Mini: math.MinInt16 - 1,
}
b.Reset()
enc.Encode(it)
err = dec.Decode(&o2)
if err == nil || err.Error() != `value for "Mini" out of range` {
t.Error("wrong underflow error for int16:", err)
}
// int32
b.Reset()
it = inputT{
Maxi: math.MaxInt32 + 1,
}
type outi32 struct {
Maxi int32
Mini int32
}
var o3 outi32
enc.Encode(it)
err = dec.Decode(&o3)
if err == nil || err.Error() != `value for "Maxi" out of range` {
t.Error("wrong overflow error for int32:", err)
}
it = inputT{
Mini: math.MinInt32 - 1,
}
b.Reset()
enc.Encode(it)
err = dec.Decode(&o3)
if err == nil || err.Error() != `value for "Mini" out of range` {
t.Error("wrong underflow error for int32:", err)
}
// uint8
b.Reset()
it = inputT{
Maxu: math.MaxUint8 + 1,
}
type outu8 struct {
Maxu uint8
}
var o4 outu8
enc.Encode(it)
err = dec.Decode(&o4)
if err == nil || err.Error() != `value for "Maxu" out of range` {
t.Error("wrong overflow error for uint8:", err)
}
// uint16
b.Reset()
it = inputT{
Maxu: math.MaxUint16 + 1,
}
type outu16 struct {
Maxu uint16
}
var o5 outu16
enc.Encode(it)
err = dec.Decode(&o5)
if err == nil || err.Error() != `value for "Maxu" out of range` {
t.Error("wrong overflow error for uint16:", err)
}
// uint32
b.Reset()
it = inputT{
Maxu: math.MaxUint32 + 1,
}
type outu32 struct {
Maxu uint32
}
var o6 outu32
enc.Encode(it)
err = dec.Decode(&o6)
if err == nil || err.Error() != `value for "Maxu" out of range` {
t.Error("wrong overflow error for uint32:", err)
}
// float32
b.Reset()
it = inputT{
Maxf: math.MaxFloat32 * 2,
}
type outf32 struct {
Maxf float32
Minf float32
}
var o7 outf32
enc.Encode(it)
err = dec.Decode(&o7)
if err == nil || err.Error() != `value for "Maxf" out of range` {
t.Error("wrong overflow error for float32:", err)
}
// complex64
b.Reset()
it = inputT{
Maxc: complex(math.MaxFloat32*2, math.MaxFloat32*2),
}
type outc64 struct {
Maxc complex64
Minc complex64
}
var o8 outc64
enc.Encode(it)
err = dec.Decode(&o8)
if err == nil || err.Error() != `value for "Maxc" out of range` {
t.Error("wrong overflow error for complex64:", err)
}
}
func TestNesting(t *testing.T) {
type RT struct {
A string
Next *RT
}
rt := new(RT)
rt.A = "level1"
rt.Next = new(RT)
rt.Next.A = "level2"
b := new(bytes.Buffer)
NewEncoder(b).Encode(rt)
var drt RT
dec := NewDecoder(b)
err := dec.Decode(&drt)
if err != nil {
t.Fatal("decoder error:", err)
}
if drt.A != rt.A {
t.Errorf("nesting: encode expected %v got %v", *rt, drt)
}
if drt.Next == nil {
t.Errorf("nesting: recursion failed")
}
if drt.Next.A != rt.Next.A {
t.Errorf("nesting: encode expected %v got %v", *rt.Next, *drt.Next)
}
}
// These three structures have the same data with different indirections
type T0 struct {
A int
B int
C int
D int
}
type T1 struct {
A int
B *int
C **int
D ***int
}
type T2 struct {
A ***int
B **int
C *int
D int
}
func TestAutoIndirection(t *testing.T) {
// First transfer t1 into t0
var t1 T1
t1.A = 17
t1.B = new(int)
*t1.B = 177
t1.C = new(*int)
*t1.C = new(int)
**t1.C = 1777
t1.D = new(**int)
*t1.D = new(*int)
**t1.D = new(int)
***t1.D = 17777
b := new(bytes.Buffer)
enc := NewEncoder(b)
enc.Encode(t1)
dec := NewDecoder(b)
var t0 T0
dec.Decode(&t0)
if t0.A != 17 || t0.B != 177 || t0.C != 1777 || t0.D != 17777 {
t.Errorf("t1->t0: expected {17 177 1777 17777}; got %v", t0)
}
// Now transfer t2 into t0
var t2 T2
t2.D = 17777
t2.C = new(int)
*t2.C = 1777
t2.B = new(*int)
*t2.B = new(int)
**t2.B = 177
t2.A = new(**int)
*t2.A = new(*int)
**t2.A = new(int)
***t2.A = 17
b.Reset()
enc.Encode(t2)
t0 = T0{}
dec.Decode(&t0)
if t0.A != 17 || t0.B != 177 || t0.C != 1777 || t0.D != 17777 {
t.Errorf("t2->t0 expected {17 177 1777 17777}; got %v", t0)
}
// Now transfer t0 into t1
t0 = T0{17, 177, 1777, 17777}
b.Reset()
enc.Encode(t0)
t1 = T1{}
dec.Decode(&t1)
if t1.A != 17 || *t1.B != 177 || **t1.C != 1777 || ***t1.D != 17777 {
t.Errorf("t0->t1 expected {17 177 1777 17777}; got {%d %d %d %d}", t1.A, *t1.B, **t1.C, ***t1.D)
}
// Now transfer t0 into t2
b.Reset()
enc.Encode(t0)
t2 = T2{}
dec.Decode(&t2)
if ***t2.A != 17 || **t2.B != 177 || *t2.C != 1777 || t2.D != 17777 {
t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.A, **t2.B, *t2.C, t2.D)
}
// Now do t2 again but without pre-allocated pointers.
b.Reset()
enc.Encode(t0)
***t2.A = 0
**t2.B = 0
*t2.C = 0
t2.D = 0
dec.Decode(&t2)
if ***t2.A != 17 || **t2.B != 177 || *t2.C != 1777 || t2.D != 17777 {
t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.A, **t2.B, *t2.C, t2.D)
}
}
type RT0 struct {
A int
B string
C float64
}
type RT1 struct {
C float64
B string
A int
NotSet string
}
func TestReorderedFields(t *testing.T) {
var rt0 RT0
rt0.A = 17
rt0.B = "hello"
rt0.C = 3.14159
b := new(bytes.Buffer)
NewEncoder(b).Encode(rt0)
dec := NewDecoder(b)
var rt1 RT1
// Wire type is RT0, local type is RT1.
err := dec.Decode(&rt1)
if err != nil {
t.Fatal("decode error:", err)
}
if rt0.A != rt1.A || rt0.B != rt1.B || rt0.C != rt1.C {
t.Errorf("rt1->rt0: expected %v; got %v", rt0, rt1)
}
}
// Like an RT0 but with fields we'll ignore on the decode side.
type IT0 struct {
A int64
B string
Ignore_d []int
Ignore_e [3]float64
Ignore_f bool
Ignore_g string
Ignore_h []byte
Ignore_i *RT1
Ignore_m map[string]int
C float64
}
func TestIgnoredFields(t *testing.T) {
var it0 IT0
it0.A = 17
it0.B = "hello"
it0.C = 3.14159
it0.Ignore_d = []int{1, 2, 3}
it0.Ignore_e[0] = 1.0
it0.Ignore_e[1] = 2.0
it0.Ignore_e[2] = 3.0
it0.Ignore_f = true
it0.Ignore_g = "pay no attention"
it0.Ignore_h = []byte("to the curtain")
it0.Ignore_i = &RT1{3.1, "hi", 7, "hello"}
it0.Ignore_m = map[string]int{"one": 1, "two": 2}
b := new(bytes.Buffer)
NewEncoder(b).Encode(it0)
dec := NewDecoder(b)
var rt1 RT1
// Wire type is IT0, local type is RT1.
err := dec.Decode(&rt1)
if err != nil {
t.Error("error: ", err)
}
if int(it0.A) != rt1.A || it0.B != rt1.B || it0.C != rt1.C {
t.Errorf("rt0->rt1: expected %v; got %v", it0, rt1)
}
}
func TestBadRecursiveType(t *testing.T) {
type Rec ***Rec
var rec Rec
b := new(bytes.Buffer)
err := NewEncoder(b).Encode(&rec)