1
2
3
4 package codec
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import (
26 "bufio"
27 "bytes"
28 "encoding/gob"
29 "errors"
30 "fmt"
31 "io"
32 "io/ioutil"
33 "math"
34 "math/rand"
35 "net"
36 "net/rpc"
37 "os"
38 "os/exec"
39 "path/filepath"
40 "reflect"
41 "strconv"
42 "strings"
43 "sync/atomic"
44 "testing"
45 "time"
46 )
47
48 func init() {
49 testPreInitFns = append(testPreInitFns, testInit)
50 }
51
52 const testRecoverPanicToErr = !debugging
53
54
55
56 var testSkipIfNotRecoverPanicToErrMsg = "tests checks for errors, and testRecoverPanicToErr=false"
57
58 func testPanicToErr(h errDecorator, err *error) {
59
60
61 if x := recover(); x != nil {
62 panicValToErr(h, x, err)
63 }
64 }
65
66 var testBytesFreeList bytesFreelist
67
68 type testCustomStringT string
69
70
71 type testMbsT []interface{}
72 type testMbsArr0T [0]interface{}
73 type testMbsArr4T [4]interface{}
74 type testMbsArr5T [5]interface{}
75 type testMbsCustStrT []testCustomStringT
76
77 func (testMbsT) MapBySlice() {}
78 func (*testMbsArr0T) MapBySlice() {}
79 func (*testMbsArr4T) MapBySlice() {}
80 func (testMbsArr5T) MapBySlice() {}
81 func (testMbsCustStrT) MapBySlice() {}
82
83
84
85
86
87
88
89
90
91
92 type testIntfMapI interface {
93 GetIntfMapV() string
94 }
95
96 type testIntfMapT1 struct {
97 IntfMapV string
98 }
99
100 func (x *testIntfMapT1) GetIntfMapV() string { return x.IntfMapV }
101
102 type testIntfMapT2 struct {
103 IntfMapV string
104 }
105
106 func (x testIntfMapT2) GetIntfMapV() string { return x.IntfMapV }
107
108 type testMissingFieldsMap struct {
109 m map[string]interface{}
110 }
111
112 func (mf *testMissingFieldsMap) CodecMissingField(field []byte, value interface{}) bool {
113 if mf.m == nil {
114 mf.m = map[string]interface{}{}
115 }
116
117 (mf.m)[string(field)] = value
118
119 return true
120 }
121
122 func (mf *testMissingFieldsMap) CodecMissingFields() map[string]interface{} {
123 return mf.m
124 }
125
126 var _ MissingFielder = (*testMissingFieldsMap)(nil)
127
128 var testErrWriterErr = errors.New("testErrWriterErr")
129
130 type testErrWriter struct{}
131
132 func (x *testErrWriter) Write(p []byte) (int, error) {
133 return 0, testErrWriterErr
134 }
135
136
137
138 type testVerifyFlag uint8
139
140 const (
141 _ testVerifyFlag = 1 << iota
142 testVerifyMapTypeSame
143 testVerifyMapTypeStrIntf
144 testVerifyMapTypeIntfIntf
145
146 testVerifyForPython
147 testVerifyDoNil
148 testVerifyTimeAsInteger
149 )
150
151 func (f testVerifyFlag) isset(v testVerifyFlag) bool {
152 return f&v == v
153 }
154
155
156
157 var (
158 testTableNumPrimitives int
159 testTableIdxTime int
160 testTableNumMaps int
161
162
163 testSkipRPCTests = false
164
165 testSkipRPCTestsMsg = "testSkipRPCTests=true"
166 )
167
168 var (
169 skipVerifyVal interface{} = &(struct{}{})
170
171 testMapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
172
173
174
175
176 timeLoc = time.FixedZone("", -8*60*60)
177 timeToCompare1 = time.Date(2012, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
178 timeToCompare2 = time.Date(1900, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
179 timeToCompare3 = time.Unix(0, 270).UTC()
180
181 timeToCompare4 = time.Unix(-2013855848, 4223).UTC()
182
183 table []interface{}
184
185
186 testRpcServer = rpc.NewServer()
187 testRpcInt = new(TestRpcInt)
188 )
189
190 func init() {
191 testRpcServer.Register(testRpcInt)
192 }
193
194 var wrapInt64Typ = reflect.TypeOf(wrapInt64(0))
195 var wrapBytesTyp = reflect.TypeOf(wrapBytes(nil))
196
197 var testUintToBytesTyp = reflect.TypeOf(testUintToBytes(0))
198 var testSelfExtTyp = reflect.TypeOf((*TestSelfExtImpl)(nil)).Elem()
199 var testSelfExt2Typ = reflect.TypeOf((*TestSelfExtImpl2)(nil)).Elem()
200
201 func testByteBuf(in []byte) *bytes.Buffer {
202 return bytes.NewBuffer(in)
203 }
204
205 type TestABC struct {
206 A, B, C string
207 }
208
209 func (x *TestABC) MarshalBinary() ([]byte, error) {
210 return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
211 }
212 func (x *TestABC) MarshalText() ([]byte, error) {
213 return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
214 }
215 func (x *TestABC) MarshalJSON() ([]byte, error) {
216 return []byte(fmt.Sprintf(`"%s %s %s"`, x.A, x.B, x.C)), nil
217 }
218
219 func (x *TestABC) UnmarshalBinary(data []byte) (err error) {
220 ss := strings.Split(string(data), " ")
221 x.A, x.B, x.C = ss[0], ss[1], ss[2]
222 return
223 }
224 func (x *TestABC) UnmarshalText(data []byte) (err error) {
225 return x.UnmarshalBinary(data)
226 }
227 func (x *TestABC) UnmarshalJSON(data []byte) (err error) {
228 return x.UnmarshalBinary(data[1 : len(data)-1])
229 }
230
231 type TestABC2 struct {
232 A, B, C string
233 }
234
235 func (x TestABC2) MarshalText() ([]byte, error) {
236 return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
237 }
238 func (x *TestABC2) UnmarshalText(data []byte) (err error) {
239 ss := strings.Split(string(data), " ")
240 x.A, x.B, x.C = ss[0], ss[1], ss[2]
241 return
242
243 }
244
245 type TestSimplish struct {
246 Ii int
247 Ss string
248 Ar [2]*TestSimplish
249 Sl []*TestSimplish
250 Mm map[string]*TestSimplish
251 }
252
253 type TestRpcABC struct {
254 A, B, C string
255 }
256
257 type TestRpcInt struct {
258 i int64
259 }
260
261 func (r *TestRpcInt) Update(n int, res *int) error {
262 atomic.StoreInt64(&r.i, int64(n))
263 *res = n
264 return nil
265 }
266 func (r *TestRpcInt) Square(ignore int, res *int) error {
267 i := int(atomic.LoadInt64(&r.i))
268 *res = i * i
269 return nil
270 }
271 func (r *TestRpcInt) Mult(n int, res *int) error {
272 *res = int(atomic.LoadInt64(&r.i)) * n
273 return nil
274 }
275 func (r *TestRpcInt) EchoStruct(arg TestRpcABC, res *string) error {
276 *res = fmt.Sprintf("%#v", arg)
277 return nil
278 }
279 func (r *TestRpcInt) Echo123(args []string, res *string) error {
280 *res = fmt.Sprintf("%#v", args)
281 return nil
282 }
283
284 type TestRawValue struct {
285 R Raw
286 I int
287 }
288
289
290
291 type testUnixNanoTimeExt struct {
292
293
294 }
295
296 func (x *testUnixNanoTimeExt) WriteExt(v interface{}) []byte {
297 v2 := v.(*time.Time)
298 bs := make([]byte, 8)
299 bigenstd.PutUint64(bs, uint64(v2.UnixNano()))
300 return bs
301 }
302 func (x *testUnixNanoTimeExt) ReadExt(v interface{}, bs []byte) {
303 v2 := v.(*time.Time)
304 ui := bigenstd.Uint64(bs)
305 *v2 = time.Unix(0, int64(ui)).UTC()
306 }
307
308 type testUnixNanoTimeInterfaceExt struct{}
309
310 func (x testUnixNanoTimeInterfaceExt) ConvertExt(v interface{}) interface{} {
311 v2 := v.(*time.Time)
312 return v2.UTC().UnixNano()
313 }
314
315 func (x testUnixNanoTimeInterfaceExt) UpdateExt(dest interface{}, v interface{}) {
316 tt := dest.(*time.Time)
317 *tt = time.Unix(0, v.(int64)).UTC()
318
319
320
321
322
323
324
325
326
327
328 }
329
330
331
332 type wrapInt64Ext int64
333
334 func (x *wrapInt64Ext) WriteExt(v interface{}) []byte {
335 v2 := uint64(int64(v.(wrapInt64)))
336 bs := make([]byte, 8)
337 bigenstd.PutUint64(bs, v2)
338 return bs
339 }
340 func (x *wrapInt64Ext) ReadExt(v interface{}, bs []byte) {
341 v2 := v.(*wrapInt64)
342 ui := bigenstd.Uint64(bs)
343 *v2 = wrapInt64(int64(ui))
344 }
345 func (x *wrapInt64Ext) ConvertExt(v interface{}) interface{} {
346 return int64(v.(wrapInt64))
347 }
348 func (x *wrapInt64Ext) UpdateExt(dest interface{}, v interface{}) {
349 v2 := dest.(*wrapInt64)
350 *v2 = wrapInt64(v.(int64))
351 }
352
353
354
355 type wrapBytesExt struct{}
356
357 func (x *wrapBytesExt) WriteExt(v interface{}) []byte {
358 return ([]byte)(v.(wrapBytes))
359 }
360 func (x *wrapBytesExt) ReadExt(v interface{}, bs []byte) {
361 v2 := v.(*wrapBytes)
362 *v2 = wrapBytes(bs)
363 }
364 func (x *wrapBytesExt) ConvertExt(v interface{}) interface{} {
365 return ([]byte)(v.(wrapBytes))
366 }
367 func (x *wrapBytesExt) UpdateExt(dest interface{}, v interface{}) {
368 v2 := dest.(*wrapBytes)
369
370 switch v3 := v.(type) {
371 case []byte:
372 *v2 = wrapBytes(v3)
373 case string:
374 *v2 = wrapBytes([]byte(v3))
375 default:
376 panic(errors.New("UpdateExt for wrapBytesExt expects string or []byte"))
377 }
378
379 }
380
381
382
383
384
385 type timeBytesExt struct{}
386
387 func (x timeBytesExt) WriteExt(v interface{}) (bs []byte) {
388 return bincEncodeTime(*(v.(*time.Time)))
389
390
391
392
393
394
395
396
397
398
399 }
400 func (x timeBytesExt) ReadExt(v interface{}, bs []byte) {
401 tt, err := bincDecodeTime(bs)
402 if err != nil {
403 panic(err)
404 }
405 *(v.(*time.Time)) = tt
406 }
407
408 type timeInterfaceExt struct{}
409
410 func (x timeInterfaceExt) ConvertExt(v interface{}) interface{} {
411 return timeBytesExt{}.WriteExt(v)
412 }
413 func (x timeInterfaceExt) UpdateExt(v interface{}, src interface{}) {
414 timeBytesExt{}.ReadExt(v, src.([]byte))
415 }
416
417
418 type testUintToBytesExt struct{}
419
420 func (x testUintToBytesExt) WriteExt(v interface{}) (bs []byte) {
421 z := uint32(v.(testUintToBytes))
422 if z == 0 {
423 return nil
424 }
425 return make([]byte, z)
426 }
427 func (x testUintToBytesExt) ReadExt(v interface{}, bs []byte) {
428 *(v.(*testUintToBytes)) = testUintToBytes(len(bs))
429 }
430 func (x testUintToBytesExt) ConvertExt(v interface{}) interface{} {
431 return x.WriteExt(v)
432 }
433 func (x testUintToBytesExt) UpdateExt(v interface{}, src interface{}) {
434 x.ReadExt(v, src.([]byte))
435 }
436
437
438
439 func testSetupNoop() {}
440
441 func testSetup(t *testing.T, h *Handle) (fn func()) {
442 return testSetupWithChecks(t, h, true)
443 }
444
445
446
447
448
449
450
451 func testSetupWithChecks(t *testing.T, h *Handle, allowParallel bool) (fn func()) {
452 testOnce.Do(testInitAll)
453 if allowParallel && testUseParallel {
454 t.Parallel()
455 if h != nil {
456 *h = testHandleCopy(*h)
457 }
458 }
459
460 if testRecoverPanicToErr {
461 fnRecoverPanic := func() {
462 if x := recover(); x != nil {
463 var err error
464 panicValToErr(errDecoratorDef{}, x, &err)
465 t.Logf("recovered error: %v", err)
466 t.FailNow()
467 }
468 }
469 fn = fnRecoverPanic
470 }
471 if fn == nil {
472 fn = testSetupNoop
473 }
474 return
475 }
476
477 func testBasicHandle(h Handle) *BasicHandle { return h.getBasicHandle() }
478
479 func testCodecEncode(ts interface{}, bsIn []byte, fn func([]byte) *bytes.Buffer, h Handle, useMust bool) (bs []byte, err error) {
480 return testSharedCodecEncode(ts, bsIn, fn, h, testBasicHandle(h), useMust)
481 }
482
483 func testCodecDecode(bs []byte, ts interface{}, h Handle, useMust bool) (err error) {
484 return testSharedCodecDecode(bs, ts, h, testBasicHandle(h), useMust)
485 }
486
487 func testCheckErr(t *testing.T, err error) {
488 if err != nil {
489 t.Logf("err: %v", err)
490 t.FailNow()
491 }
492 }
493
494 func testCheckEqual(t *testing.T, v1 interface{}, v2 interface{}, desc string) {
495 t.Helper()
496 if err := deepEqual(v1, v2); err != nil {
497 t.Logf("Not Equal: %s: %v", desc, err)
498 if testVerbose {
499 t.Logf("\tv1: %v, v2: %v", v1, v2)
500 }
501 t.FailNow()
502 }
503 }
504
505 func testInit() {
506 gob.Register(new(TestStrucFlex))
507
508 for _, v := range testHandles {
509 bh := testBasicHandle(v)
510 bh.clearInited()
511
512 bh.EncodeOptions = testEncodeOptions
513 bh.DecodeOptions = testDecodeOptions
514 bh.RPCOptions = testRPCOptions
515
516
517
518 bh.MaxInitLen = testMaxInitLen
519 }
520
521 var tTimeExt timeBytesExt
522 var tBytesExt wrapBytesExt
523 var tI64Ext wrapInt64Ext
524 var tUintToBytesExt testUintToBytesExt
525
526
527
528 var (
529 myExtEncFn = func(x BytesExt, rv reflect.Value) (bs []byte, err error) {
530 defer testPanicToErr(errDecoratorDef{}, &err)
531 bs = x.WriteExt(rv.Interface())
532 return
533 }
534 myExtDecFn = func(x BytesExt, rv reflect.Value, bs []byte) (err error) {
535 defer testPanicToErr(errDecoratorDef{}, &err)
536 x.ReadExt(rv.Interface(), bs)
537 return
538 }
539 timeExtEncFn = func(rv reflect.Value) (bs []byte, err error) { return myExtEncFn(tTimeExt, rv) }
540 timeExtDecFn = func(rv reflect.Value, bs []byte) (err error) { return myExtDecFn(tTimeExt, rv, bs) }
541 wrapInt64ExtEncFn = func(rv reflect.Value) (bs []byte, err error) { return myExtEncFn(&tI64Ext, rv) }
542 wrapInt64ExtDecFn = func(rv reflect.Value, bs []byte) (err error) { return myExtDecFn(&tI64Ext, rv, bs) }
543 )
544
545 chkErr := func(err error) {
546 if err != nil {
547 panic(err)
548 }
549 }
550
551
552
553 chkErr(testSimpleH.AddExt(timeTyp, 1, timeExtEncFn, timeExtDecFn))
554
555 chkErr(testMsgpackH.SetBytesExt(timeTyp, 1, timeBytesExt{}))
556 chkErr(testCborH.SetInterfaceExt(timeTyp, 1, testUnixNanoTimeInterfaceExt{}))
557
558
559
560 chkErr(testSimpleH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
561 chkErr(testMsgpackH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
562 chkErr(testBincH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
563 chkErr(testJsonH.SetInterfaceExt(testSelfExtTyp, 78, SelfExt))
564 chkErr(testCborH.SetInterfaceExt(testSelfExtTyp, 78, SelfExt))
565
566 chkErr(testSimpleH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
567 chkErr(testMsgpackH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
568 chkErr(testBincH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
569 chkErr(testJsonH.SetInterfaceExt(testSelfExt2Typ, 79, SelfExt))
570 chkErr(testCborH.SetInterfaceExt(testSelfExt2Typ, 79, SelfExt))
571
572
573
574
575 chkErr(testSimpleH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
576 chkErr(testMsgpackH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
577 chkErr(testBincH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
578 chkErr(testJsonH.SetInterfaceExt(wrapBytesTyp, 32, &tBytesExt))
579 chkErr(testCborH.SetInterfaceExt(wrapBytesTyp, 32, &tBytesExt))
580
581 chkErr(testSimpleH.SetBytesExt(testUintToBytesTyp, 33, &tUintToBytesExt))
582 chkErr(testMsgpackH.SetBytesExt(testUintToBytesTyp, 33, &tUintToBytesExt))
583 chkErr(testBincH.SetBytesExt(testUintToBytesTyp, 33, &tUintToBytesExt))
584 chkErr(testJsonH.SetInterfaceExt(testUintToBytesTyp, 33, &tUintToBytesExt))
585 chkErr(testCborH.SetInterfaceExt(testUintToBytesTyp, 33, &tUintToBytesExt))
586
587 chkErr(testSimpleH.AddExt(wrapInt64Typ, 16, wrapInt64ExtEncFn, wrapInt64ExtDecFn))
588
589 chkErr(testMsgpackH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
590 chkErr(testBincH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
591 chkErr(testJsonH.SetInterfaceExt(wrapInt64Typ, 16, &tI64Ext))
592 chkErr(testCborH.SetInterfaceExt(wrapInt64Typ, 16, &tI64Ext))
593
594
595 primitives := []interface{}{
596 int8(-8),
597 int16(-1616),
598 int32(-32323232),
599 int64(-6464646464646464),
600 uint8(192),
601 uint16(1616),
602 uint32(32323232),
603 uint64(6464646464646464),
604 byte(192),
605 float32(-3232.0),
606 float64(-6464646464.0),
607 float32(3232.0),
608 float64(6464.0),
609 float64(6464646464.0),
610 complex64(complex(160.0, 0)),
611 complex128(complex(1616, 0)),
612 false,
613 true,
614 "null",
615 nil,
616 "some&day>some<day",
617 timeToCompare1,
618 "",
619 timeToCompare2,
620 "bytestring",
621 timeToCompare3,
622 "none",
623 timeToCompare4,
624 }
625
626 maps := []interface{}{
627 map[string]bool{
628 "true": true,
629 "false": false,
630 },
631 map[string]interface{}{
632 "true": "True",
633 "false": false,
634 "uint16(1616)": uint16(1616),
635 },
636
637
638 map[string]interface{}{
639 "list": []interface{}{
640 int16(1616),
641 int32(32323232),
642 true,
643 float32(-3232.0),
644 map[string]interface{}{
645 "TRUE": true,
646 "FALSE": false,
647 },
648 []interface{}{true, false},
649 },
650 "int32": int32(32323232),
651 "bool": true,
652 "LONG STRING": `
653 1234567890 1234567890
654 1234567890 1234567890
655 1234567890 1234567890
656 ABCDEDFGHIJKLMNOPQRSTUVWXYZ
657 abcdedfghijklmnopqrstuvwxyz
658 ABCDEDFGHIJKLMNOPQRSTUVWXYZ
659 abcdedfghijklmnopqrstuvwxyz
660 "ABCDEDFGHIJKLMNOPQRSTUVWXYZ"
661 ' a tab '
662 \a\b\c\d\e
663 \b\f\n\r\t all literally
664 ugorji
665 `,
666 "SHORT STRING": "1234567890",
667 },
668 map[interface{}]interface{}{
669 true: "true",
670 uint8(138): false,
671 false: uint8(200),
672 },
673 }
674
675 testTableNumPrimitives = len(primitives)
676 testTableIdxTime = testTableNumPrimitives - 8
677 testTableNumMaps = len(maps)
678
679 table = []interface{}{}
680 table = append(table, primitives...)
681 table = append(table, primitives)
682 table = append(table, testMbsT(primitives))
683 table = append(table, maps...)
684 table = append(table, newTestStrucFlex(0, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly))
685
686 }
687
688 func testTableVerify(f testVerifyFlag, h Handle) (av []interface{}) {
689 av = make([]interface{}, len(table))
690 lp := testTableNumPrimitives + 4
691
692
693 switch {
694 case f.isset(testVerifyForPython):
695 for i, v := range table {
696 if i == testTableNumPrimitives+1 || i > lp {
697 av[i] = skipVerifyVal
698 continue
699 }
700 av[i] = testVerifyVal(v, f, h)
701 }
702
703 av = av[:testTableNumPrimitives+2+testTableNumMaps-2]
704 case f.isset(testVerifyDoNil):
705 for i, v := range table {
706 if i > lp {
707 av[i] = skipVerifyVal
708 continue
709 }
710 av[i] = testVerifyVal(v, f, h)
711 }
712 default:
713 for i, v := range table {
714 if i == lp {
715 av[i] = skipVerifyVal
716 continue
717 }
718
719 switch v.(type) {
720 case []interface{}:
721 av[i] = testVerifyVal(v, f, h)
722 case testMbsT:
723 av[i] = testVerifyVal(v, f, h)
724 case map[string]interface{}:
725 av[i] = testVerifyVal(v, f, h)
726 case map[interface{}]interface{}:
727 av[i] = testVerifyVal(v, f, h)
728 case time.Time:
729 av[i] = testVerifyVal(v, f, h)
730 default:
731 av[i] = v
732 }
733 }
734 }
735 return
736 }
737
738 func testVerifyValInt(v int64, isMsgp bool) (v2 interface{}) {
739 if isMsgp {
740 if v >= 0 && v <= 127 {
741 v2 = uint64(v)
742 } else {
743 v2 = int64(v)
744 }
745 } else if v >= 0 {
746 v2 = uint64(v)
747 } else {
748 v2 = int64(v)
749 }
750 return
751 }
752
753 func testVerifyVal(v interface{}, f testVerifyFlag, h Handle) (v2 interface{}) {
754
755
756
757 _, isMsgp := h.(*MsgpackHandle)
758 _, isCbor := h.(*CborHandle)
759 switch iv := v.(type) {
760 case int8:
761 v2 = testVerifyValInt(int64(iv), isMsgp)
762
763 case int16:
764 v2 = testVerifyValInt(int64(iv), isMsgp)
765 case int32:
766 v2 = testVerifyValInt(int64(iv), isMsgp)
767 case int64:
768 v2 = testVerifyValInt(int64(iv), isMsgp)
769 case uint8:
770 v2 = uint64(iv)
771 case uint16:
772 v2 = uint64(iv)
773 case uint32:
774 v2 = uint64(iv)
775 case uint64:
776 v2 = uint64(iv)
777 case float32:
778 v2 = float64(iv)
779 case float64:
780 v2 = float64(iv)
781 case complex64:
782 v2 = float64(float32(real(iv)))
783 case complex128:
784 v2 = float64(real(iv))
785 case []interface{}:
786 m2 := make([]interface{}, len(iv))
787 for j, vj := range iv {
788 m2[j] = testVerifyVal(vj, f, h)
789 }
790 v2 = m2
791 case testMbsT:
792 m2 := make([]interface{}, len(iv))
793 for j, vj := range iv {
794 m2[j] = testVerifyVal(vj, f, h)
795 }
796 v2 = testMbsT(m2)
797 case map[string]bool:
798 switch {
799 case f.isset(testVerifyMapTypeSame):
800 m2 := make(map[string]bool)
801 for kj, kv := range iv {
802 m2[kj] = kv
803 }
804 v2 = m2
805 case f.isset(testVerifyMapTypeStrIntf):
806 m2 := make(map[string]interface{})
807 for kj, kv := range iv {
808 m2[kj] = kv
809 }
810 v2 = m2
811 case f.isset(testVerifyMapTypeIntfIntf):
812 m2 := make(map[interface{}]interface{})
813 for kj, kv := range iv {
814 m2[kj] = kv
815 }
816 v2 = m2
817 }
818 case map[string]interface{}:
819 switch {
820 case f.isset(testVerifyMapTypeSame):
821 m2 := make(map[string]interface{})
822 for kj, kv := range iv {
823 m2[kj] = testVerifyVal(kv, f, h)
824 }
825 v2 = m2
826 case f.isset(testVerifyMapTypeStrIntf):
827 m2 := make(map[string]interface{})
828 for kj, kv := range iv {
829 m2[kj] = testVerifyVal(kv, f, h)
830 }
831 v2 = m2
832 case f.isset(testVerifyMapTypeIntfIntf):
833 m2 := make(map[interface{}]interface{})
834 for kj, kv := range iv {
835 m2[kj] = testVerifyVal(kv, f, h)
836 }
837 v2 = m2
838 }
839 case map[interface{}]interface{}:
840 m2 := make(map[interface{}]interface{})
841 for kj, kv := range iv {
842 m2[testVerifyVal(kj, f, h)] = testVerifyVal(kv, f, h)
843 }
844 v2 = m2
845 case time.Time:
846 switch {
847 case f.isset(testVerifyTimeAsInteger):
848 if iv2 := iv.UnixNano(); iv2 >= 0 {
849 v2 = uint64(iv2)
850 } else {
851 v2 = int64(iv2)
852 }
853 case isMsgp:
854 v2 = iv.UTC()
855 case isCbor:
856
857 v2 = iv.UTC().Round(time.Microsecond)
858 default:
859 v2 = v
860 }
861 default:
862 v2 = v
863 }
864 return
865 }
866
867 func testReleaseBytes(bs []byte) {
868 if !testUseParallel {
869 testBytesFreeList.put(bs)
870 }
871 }
872
873 func testGetBytes() (bs []byte) {
874 if !testUseParallel {
875 bs = testBytesFreeList.get(64)
876 }
877 return
878 }
879
880 func testHandleCopy(h Handle) (h2 Handle) {
881 switch v := h.(type) {
882 case *JsonHandle:
883 v2 := *v
884 h2 = &v2
885 case *CborHandle:
886 v2 := *v
887 h2 = &v2
888 case *MsgpackHandle:
889 v2 := *v
890 h2 = &v2
891 case *SimpleHandle:
892 v2 := *v
893 h2 = &v2
894 case *BincHandle:
895 v2 := *v
896 h2 = &v2
897 }
898 return
899 }
900
901 func testMarshal(v interface{}, h Handle) (bs []byte, err error) {
902
903 return testCodecEncode(v, testGetBytes(), testByteBuf, h, false)
904 }
905
906 func testUnmarshal(v interface{}, data []byte, h Handle) (err error) {
907 return testCodecDecode(data, v, h, false)
908 }
909
910 func testMarshalErr(v interface{}, h Handle, t *testing.T, name string) (bs []byte) {
911 t.Helper()
912 bs, err := testCodecEncode(v, testGetBytes(), testByteBuf, h, true)
913 if err != nil {
914 t.Logf("%s: marshal failed: %v", name, err)
915 if testVerbose {
916 t.Logf("Error encoding %s: %v, Err: %v", name, v, err)
917 }
918 t.FailNow()
919 }
920 return
921 }
922
923 func testUnmarshalErr(v interface{}, data []byte, h Handle, t *testing.T, name string) {
924 t.Helper()
925 err := testCodecDecode(data, v, h, true)
926 if err != nil {
927 t.Logf("%s: unmarshal failed: %v", name, err)
928 if testVerbose {
929 t.Logf("Error Decoding into %s: %v, Err: %v", name, v, err)
930 }
931 t.FailNow()
932 }
933 }
934
935 func testDeepEqualErr(v1, v2 interface{}, t *testing.T, name string) {
936 t.Helper()
937 if err := deepEqual(v1, v2); err == nil {
938 if testVerbose {
939 t.Logf("%s: values equal", name)
940 }
941 } else {
942 t.Logf("%s: values not equal: %v", name, err)
943 if testVerbose {
944 t.Logf("%s: values not equal: %v. 1: %#v, 2: %#v", name, err, v1, v2)
945 }
946 t.FailNow()
947 }
948 }
949
950 func testReadWriteCloser(c io.ReadWriteCloser) io.ReadWriteCloser {
951 if testRpcBufsize <= 0 && rand.Int63()%2 == 0 {
952 return c
953 }
954 return struct {
955 io.Closer
956 *bufio.Reader
957 *bufio.Writer
958 }{c, bufio.NewReaderSize(c, testRpcBufsize), bufio.NewWriterSize(c, testRpcBufsize)}
959 }
960
961
962 func testCodecTableOne(t *testing.T, testNil bool, h Handle,
963 vs []interface{}, vsVerify []interface{}) {
964
965
966 if testVerbose {
967 t.Logf("================ TestNil: %v: %v entries ================\n", testNil, len(vs))
968 }
969
970 if mh, ok := h.(*MsgpackHandle); ok {
971 defer func(a, b bool) {
972 mh.RawToString = a
973 mh.PositiveIntUnsigned = b
974 }(mh.RawToString, mh.PositiveIntUnsigned)
975 mh.RawToString = true
976 mh.PositiveIntUnsigned = false
977 }
978
979 bh := testBasicHandle(h)
980 for i, v0 := range vs {
981 if testVerbose {
982 t.Logf("..............................................")
983 t.Logf(" Testing: #%d:, %T, %#v\n", i, v0, v0)
984 }
985
986
987
988 var mapMstrUi64TSelf map[stringUint64T]*stringUint64T
989 var mapMsu2wss map[stringUint64T]wrapStringSlice
990
991
992
993
994
995
996 var mapMsp2ss map[*string][]string
997 var mapMip2ss map[*uint64][]string
998
999 tsflex, _ := v0.(*TestStrucFlex)
1000 if tsflex != nil {
1001 mapMstrUi64TSelf = tsflex.MstrUi64TSelf
1002 mapMsu2wss = tsflex.Msu2wss
1003 mapMsp2ss = tsflex.Msp2ss
1004 mapMip2ss = tsflex.Mip2ss
1005 if testNil {
1006 tsflex.MstrUi64TSelf = nil
1007 tsflex.Msu2wss = nil
1008 }
1009 }
1010 b0 := testMarshalErr(v0, h, t, "v0")
1011 var b1 = b0
1012 if len(b0) > 1024 {
1013 b1 = b0[:1024]
1014 }
1015 bytesorstr := "string"
1016 if h.isBinary() {
1017 bytesorstr = "bytes"
1018 if len(b0) > 256 {
1019 b1 = b0[:256]
1020 }
1021 }
1022 if testVerbose {
1023 t.Logf(" Encoded %s: type: %T, len/cap: %v/%v, %v, %s\n", bytesorstr, v0, len(b0), cap(b0), b1, "...")
1024 }
1025
1026 if _, ok := v0.(*TestStrucFlex); ok && bh.SignedInteger {
1027 continue
1028 }
1029
1030 var v1 interface{}
1031 var err error
1032
1033 if tsflex != nil {
1034 if testNil {
1035 tsflex.MstrUi64TSelf = mapMstrUi64TSelf
1036 tsflex.Msu2wss = mapMsu2wss
1037 }
1038 tsflex.Msp2ss = nil
1039 tsflex.Mip2ss = nil
1040 }
1041
1042 if testNil {
1043 err = testUnmarshal(&v1, b0, h)
1044 } else if v0 != nil {
1045 v0rt := reflect.TypeOf(v0)
1046 if v0rt.Kind() == reflect.Ptr {
1047 err = testUnmarshal(v0, b0, h)
1048 v1 = v0
1049 } else {
1050 rv1 := reflect.New(v0rt)
1051 err = testUnmarshal(rv1.Interface(), b0, h)
1052 v1 = rv1.Elem().Interface()
1053
1054 }
1055 }
1056
1057 if tsflex != nil {
1058
1059 _, _ = mapMsp2ss, mapMip2ss
1060 }
1061
1062 if testVerbose {
1063 t.Logf(" v1 returned: %T, %v %#v", v1, v1, v1)
1064 }
1065
1066
1067
1068
1069
1070 if err != nil {
1071 t.Logf("-------- Error: %v", err)
1072 if testVerbose {
1073 t.Logf("-------- Partial return: %v", v1)
1074 }
1075 t.FailNow()
1076 }
1077 v0check := vsVerify[i]
1078 if v0check == skipVerifyVal || bh.SignedInteger {
1079 if testVerbose {
1080 t.Logf(" Nil Check skipped: Decoded: %T, %#v\n", v1, v1)
1081 }
1082 continue
1083 }
1084 if err = deepEqual(v0check, v1); err == nil {
1085 if testVerbose {
1086 t.Logf("++++++++ Before and After marshal matched\n")
1087 }
1088 } else {
1089
1090
1091 t.Logf("-------- FAIL: Before and After marshal do not match: Error: %v", err)
1092 if testVerbose {
1093 t.Logf(" ....... GOLDEN: (%T) %v %#v", v0check, v0check, v0check)
1094 t.Logf(" ....... DECODED: (%T) %v %#v", v1, v1, v1)
1095 }
1096 t.FailNow()
1097 }
1098 testReleaseBytes(b0)
1099 }
1100 }
1101
1102 func doTestCodecTableOne(t *testing.T, h Handle) {
1103
1104
1105
1106 defer testSetupWithChecks(t, &h, false)()
1107
1108 numPrim, numMap, idxTime, idxMap := testTableNumPrimitives, testTableNumMaps, testTableIdxTime, testTableNumPrimitives+2
1109
1110 tableVerify := testTableVerify(testVerifyMapTypeSame, h)
1111 tableTestNilVerify := testTableVerify(testVerifyDoNil|testVerifyMapTypeStrIntf, h)
1112 switch v := h.(type) {
1113 case *MsgpackHandle:
1114 oldWriteExt := v.WriteExt
1115 v.WriteExt = true
1116 testCodecTableOne(t, false, h, table, tableVerify)
1117 v.WriteExt = oldWriteExt
1118 case *JsonHandle:
1119
1120
1121 testCodecTableOne(t, false, h, table[:numPrim], tableVerify[:numPrim])
1122 testCodecTableOne(t, false, h, table[idxMap:], tableVerify[idxMap:])
1123 default:
1124 testCodecTableOne(t, false, h, table, tableVerify)
1125 }
1126
1127
1128
1129
1130
1131
1132 var oldMapType reflect.Type
1133 v := testBasicHandle(h)
1134
1135 oldMapType, v.MapType = v.MapType, testMapStrIntfTyp
1136
1137
1138 testCodecTableOne(t, true, h, table[:idxTime], tableTestNilVerify[:idxTime])
1139 testCodecTableOne(t, true, h, table[idxMap:idxMap+numMap-1], tableTestNilVerify[idxMap:idxMap+numMap-1])
1140 v.MapType = oldMapType
1141
1142
1143
1144 idx2 := idxMap + numMap - 1
1145 testCodecTableOne(t, true, h, table[idx2:], tableTestNilVerify[idx2:])
1146
1147 }
1148
1149 func doTestCodecMiscOne(t *testing.T, h Handle) {
1150 defer testSetup(t, &h)()
1151 var err error
1152 bh := testBasicHandle(h)
1153 b := testMarshalErr(32, h, t, "32")
1154
1155
1156
1157
1158
1159
1160 var i2 int32
1161 testUnmarshalErr(&i2, b, h, t, "int32-ptr")
1162 if i2 != int32(32) {
1163 t.Logf("------- didn't unmarshal to 32: Received: %d", i2)
1164 t.FailNow()
1165 }
1166
1167
1168 ts := newTestStrucFlex(testDepth, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
1169 testReleaseBytes(b)
1170 b = testMarshalErr(ts, h, t, "pointer-to-struct")
1171 if len(b) < 40 {
1172 t.Logf("------- Size must be > 40. Size: %d", len(b))
1173 t.FailNow()
1174 }
1175 var b1 = b
1176 if len(b1) > 256 {
1177 b1 = b1[:256]
1178 }
1179 if testVerbose {
1180 if h.isBinary() {
1181 t.Logf("------- b: size: %v, value: %v", len(b), b1)
1182 } else {
1183 t.Logf("------- b: size: %v, value: %s", len(b), b1)
1184 }
1185 }
1186
1187
1188 var ts2 = new(TestStrucFlex)
1189
1190 ts2.Chstr = make(chan string, teststrucflexChanCap)
1191 go func() {
1192 for range ts2.Chstr {
1193 }
1194 }()
1195
1196 testUnmarshalErr(ts2, b, h, t, "pointer-to-struct")
1197 if ts2.I64 != math.MaxInt64*2/3 {
1198 t.Logf("------- Unmarshal wrong. Expect I64 = 64. Got: %v", ts2.I64)
1199 t.FailNow()
1200 }
1201 close(ts2.Chstr)
1202 testReleaseBytes(b)
1203
1204
1205 defer func(a, b bool) {
1206 bh.SliceElementReset = a
1207 bh.InterfaceReset = b
1208 }(bh.SliceElementReset, bh.InterfaceReset)
1209
1210 bh.SliceElementReset = false
1211 bh.InterfaceReset = false
1212
1213 m := map[string]int{"A": 2, "B": 3}
1214 p := []interface{}{m}
1215 bs := testMarshalErr(p, h, t, "p")
1216
1217 m2 := map[string]int{}
1218 p2 := []interface{}{m2}
1219 testUnmarshalErr(&p2, bs, h, t, "&p2")
1220
1221 if m2["A"] != 2 || m2["B"] != 3 {
1222 t.Logf("FAIL: m2 not as expected: expecting: %v, got: %v", m, m2)
1223 t.FailNow()
1224 }
1225
1226 testCheckEqual(t, p, p2, "p=p2")
1227 testCheckEqual(t, m, m2, "m=m2")
1228 if err = deepEqual(p, p2); err == nil {
1229 if testVerbose {
1230 t.Logf("p and p2 match")
1231 }
1232 } else {
1233 t.Logf("Not Equal: %v. p: %v, p2: %v", err, p, p2)
1234 t.FailNow()
1235 }
1236 if err = deepEqual(m, m2); err == nil {
1237 if testVerbose {
1238 t.Logf("m and m2 match")
1239 }
1240 } else {
1241 t.Logf("Not Equal: %v. m: %v, m2: %v", err, m, m2)
1242 t.FailNow()
1243 }
1244 testReleaseBytes(bs)
1245
1246
1247
1248 mm := map[string]interface{}{"A": 5, "B": 99, "C": 333}
1249 bs = testMarshalErr(mm, h, t, "mm")
1250 type ttt struct {
1251 A uint8
1252 C int32
1253 }
1254 var t2 ttt
1255 testUnmarshalErr(&t2, bs, h, t, "t2")
1256 t3 := ttt{5, 333}
1257 testCheckEqual(t, t2, t3, "t2=t3")
1258 testReleaseBytes(bs)
1259
1260
1261
1262 type tarr struct {
1263 A int64
1264 B [3]int64
1265 C []byte
1266 D [3]byte
1267 }
1268 var tarr0 = tarr{1, [3]int64{2, 3, 4}, []byte{4, 5, 6}, [3]byte{7, 8, 9}}
1269
1270 for _, tarr1 := range []interface{}{tarr0, &tarr0} {
1271 bs = testMarshalErr(tarr1, h, t, "tarr1")
1272 if _, ok := h.(*JsonHandle); ok {
1273 if testVerbose {
1274 t.Logf("Marshal as: %s", bs)
1275 }
1276 }
1277 var tarr2 tarr
1278 testUnmarshalErr(&tarr2, bs, h, t, "tarr2")
1279 testCheckEqual(t, tarr0, tarr2, "tarr0=tarr2")
1280 testReleaseBytes(bs)
1281 }
1282
1283
1284 if _, ok := h.(*MsgpackHandle); ok {
1285 type ystruct struct {
1286 Anarray []byte
1287 }
1288 var ya = ystruct{}
1289 testUnmarshalErr(&ya, []byte{0x91, 0x90}, h, t, "ya")
1290 }
1291
1292 var tt1, tt2 time.Time
1293 tt2 = time.Now()
1294 bs = testMarshalErr(tt1, h, t, "zero-time-enc")
1295 testUnmarshalErr(&tt2, bs, h, t, "zero-time-dec")
1296 testDeepEqualErr(tt1, tt2, t, "zero-time-eq")
1297 testReleaseBytes(bs)
1298
1299
1300 var sw = []wrapUint8{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'}
1301 var bw []byte
1302 bs = testMarshalErr(sw, h, t, "wrap-bytes-enc")
1303 testUnmarshalErr(&bw, bs, h, t, "wrap-bytes-dec")
1304 testDeepEqualErr(bw, []byte("ABCDEFGHIJ"), t, "wrap-bytes-eq")
1305 testReleaseBytes(bs)
1306 }
1307
1308 func doTestCodecEmbeddedPointer(t *testing.T, h Handle) {
1309 defer testSetup(t, &h)()
1310 type Z int
1311 type A struct {
1312 AnInt int
1313 }
1314 type B struct {
1315 *Z
1316 *A
1317 MoreInt int
1318 }
1319 var z Z = 4
1320 x1 := &B{&z, &A{5}, 6}
1321 bs := testMarshalErr(x1, h, t, "x1")
1322 var x2 = new(B)
1323 testUnmarshalErr(x2, bs, h, t, "x2")
1324 testCheckEqual(t, x1, x2, "x1=x2")
1325 testReleaseBytes(bs)
1326 }
1327
1328 func testCodecUnderlyingType(t *testing.T, h Handle) {
1329 defer testSetup(t, &h)()
1330
1331
1332
1333 type T1 map[string]string
1334 v := T1{"1": "1s", "2": "2s"}
1335 var bs []byte
1336 var err error
1337 NewEncoderBytes(&bs, h).MustEncode(v)
1338 if err != nil {
1339 t.Logf("Error during encode: %v", err)
1340 t.FailNow()
1341 }
1342 var v2 T1
1343 NewDecoderBytes(bs, h).MustDecode(&v2)
1344 if err != nil {
1345 t.Logf("Error during decode: %v", err)
1346 t.FailNow()
1347 }
1348 }
1349
1350 func doTestCodecChan(t *testing.T, h Handle) {
1351 defer testSetup(t, &h)()
1352
1353
1354
1355
1356
1357
1358 {
1359 if testVerbose {
1360 t.Logf("*int64")
1361 }
1362 sl1 := make([]*int64, 4)
1363 for i := range sl1 {
1364 var j int64 = int64(i)
1365 sl1[i] = &j
1366 }
1367 ch1 := make(chan *int64, 4)
1368 for _, j := range sl1 {
1369 ch1 <- j
1370 }
1371 var bs []byte
1372 NewEncoderBytes(&bs, h).MustEncode(ch1)
1373 ch2 := make(chan *int64, 8)
1374 NewDecoderBytes(bs, h).MustDecode(&ch2)
1375 close(ch2)
1376 var sl2 []*int64
1377 for j := range ch2 {
1378 sl2 = append(sl2, j)
1379 }
1380 if err := deepEqual(sl1, sl2); err != nil {
1381 t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
1382 if testVerbose {
1383 t.Logf("sl1: %#v, sl2: %#v", sl1, sl2)
1384 }
1385 t.FailNow()
1386 }
1387 }
1388
1389 {
1390 if testVerbose {
1391 t.Logf("testBytesT []byte - input []byte")
1392 }
1393 type testBytesT []byte
1394 sl1 := make([]testBytesT, 4)
1395 for i := range sl1 {
1396 var j = []byte(strings.Repeat(strconv.FormatInt(int64(i), 10), i))
1397 sl1[i] = j
1398 }
1399 ch1 := make(chan testBytesT, 4)
1400 for _, j := range sl1 {
1401 ch1 <- j
1402 }
1403 var bs []byte
1404 NewEncoderBytes(&bs, h).MustEncode(ch1)
1405 ch2 := make(chan testBytesT, 8)
1406 NewDecoderBytes(bs, h).MustDecode(&ch2)
1407 close(ch2)
1408 var sl2 []testBytesT
1409 for j := range ch2 {
1410
1411 sl2 = append(sl2, j)
1412 }
1413 if err := deepEqual(sl1, sl2); err != nil {
1414 t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
1415 if testVerbose {
1416 t.Logf("sl1: %#v, sl2: %#v", sl1, sl2)
1417 }
1418 t.FailNow()
1419 }
1420 }
1421 {
1422 if testVerbose {
1423 t.Logf("testBytesT byte - input string/testBytesT")
1424 }
1425 type testBytesT byte
1426 sl1 := make([]testBytesT, 4)
1427 for i := range sl1 {
1428 var j = strconv.FormatInt(int64(i), 10)[0]
1429 sl1[i] = testBytesT(j)
1430 }
1431 ch1 := make(chan testBytesT, 4)
1432 for _, j := range sl1 {
1433 ch1 <- j
1434 }
1435 var bs []byte
1436 NewEncoderBytes(&bs, h).MustEncode(ch1)
1437 ch2 := make(chan testBytesT, 8)
1438 NewDecoderBytes(bs, h).MustDecode(&ch2)
1439 close(ch2)
1440 var sl2 []testBytesT
1441 for j := range ch2 {
1442 sl2 = append(sl2, j)
1443 }
1444 if err := deepEqual(sl1, sl2); err != nil {
1445 t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
1446 t.FailNow()
1447 }
1448 }
1449
1450 {
1451 if testVerbose {
1452 t.Logf("*[]byte")
1453 }
1454 sl1 := make([]byte, 4)
1455 for i := range sl1 {
1456 var j = strconv.FormatInt(int64(i), 10)[0]
1457 sl1[i] = byte(j)
1458 }
1459 ch1 := make(chan byte, 4)
1460 for _, j := range sl1 {
1461 ch1 <- j
1462 }
1463 var bs []byte
1464 NewEncoderBytes(&bs, h).MustEncode(ch1)
1465 ch2 := make(chan byte, 8)
1466 NewDecoderBytes(bs, h).MustDecode(&ch2)
1467 close(ch2)
1468 var sl2 []byte
1469 for j := range ch2 {
1470 sl2 = append(sl2, j)
1471 }
1472 if err := deepEqual(sl1, sl2); err != nil {
1473 t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
1474 t.FailNow()
1475 }
1476 }
1477 }
1478
1479 func doTestCodecRpcOne(t *testing.T, rr Rpc, h Handle, doRequest bool, exitSleepMs time.Duration) (port int) {
1480 defer testSetup(t, &h)()
1481 if testSkipRPCTests {
1482 t.Skip(testSkipRPCTestsMsg)
1483 }
1484 if !testRecoverPanicToErr {
1485 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
1486 }
1487
1488 if mh, ok := h.(*MsgpackHandle); ok && mh.SliceElementReset {
1489 t.Skipf("skipping ... MsgpackRpcSpec does not handle SliceElementReset - needs investigation")
1490 }
1491
1492 if jsonH, ok := h.(*JsonHandle); ok && !jsonH.TermWhitespace {
1493 jsonH.TermWhitespace = true
1494 defer func() { jsonH.TermWhitespace = false }()
1495 }
1496
1497
1498
1499 srv := testRpcServer
1500
1501 ln, err := net.Listen("tcp", "127.0.0.1:0")
1502 testCheckErr(t, err)
1503 port = (ln.Addr().(*net.TCPAddr)).Port
1504 if testVerbose {
1505 t.Logf("connFn: addr: %v, network: %v, port: %v", ln.Addr(), ln.Addr().Network(), port)
1506 }
1507
1508
1509
1510 serverExitChan := make(chan bool, 1)
1511 var serverExitFlag uint64
1512 serverFn := func() {
1513 var conns []net.Conn
1514 var svrcodecs []rpc.ServerCodec
1515 defer func() {
1516 for i := range conns {
1517 svrcodecs[i].Close()
1518 conns[i].Close()
1519 }
1520 ln.Close()
1521 serverExitChan <- true
1522 }()
1523 for {
1524 conn1, err1 := ln.Accept()
1525 if atomic.LoadUint64(&serverExitFlag) == 1 {
1526 if conn1 != nil {
1527 conn1.Close()
1528 }
1529 break
1530 }
1531 if err1 != nil {
1532
1533 if testVerbose {
1534 t.Logf("rpc error accepting connection: %v", err1)
1535 }
1536 continue
1537 }
1538 if conn1 != nil {
1539 sc := rr.ServerCodec(testReadWriteCloser(conn1), h)
1540 conns = append(conns, conn1)
1541 svrcodecs = append(svrcodecs, sc)
1542 go srv.ServeCodec(sc)
1543 }
1544 }
1545 }
1546
1547 connFn := func() (bs net.Conn) {
1548 bs, err2 := net.Dial(ln.Addr().Network(), ln.Addr().String())
1549 testCheckErr(t, err2)
1550 return
1551 }
1552
1553 exitFn := func() {
1554 if atomic.LoadUint64(&serverExitFlag) == 1 {
1555 return
1556 }
1557 atomic.StoreUint64(&serverExitFlag, 1)
1558 bs := connFn()
1559 defer bs.Close()
1560 <-serverExitChan
1561
1562
1563 }
1564
1565 go serverFn()
1566
1567
1568
1569 if exitSleepMs == 0 {
1570 defer exitFn()
1571 } else {
1572 go func() {
1573 time.Sleep(exitSleepMs)
1574 exitFn()
1575 }()
1576 }
1577
1578 if doRequest {
1579 func() {
1580 bs := connFn()
1581 defer bs.Close()
1582 cc := rr.ClientCodec(testReadWriteCloser(bs), h)
1583 defer cc.Close()
1584 cl := rpc.NewClientWithCodec(cc)
1585 defer cl.Close()
1586 var up, sq, mult int
1587 var rstr string
1588 testCheckErr(t, cl.Call("TestRpcInt.Update", 5, &up))
1589 testCheckEqual(t, atomic.LoadInt64(&testRpcInt.i), int64(5), "testRpcInt.i=5")
1590 testCheckEqual(t, up, 5, "up=5")
1591 testCheckErr(t, cl.Call("TestRpcInt.Square", 1, &sq))
1592 testCheckEqual(t, sq, 25, "sq=25")
1593 testCheckErr(t, cl.Call("TestRpcInt.Mult", 20, &mult))
1594 testCheckEqual(t, mult, 100, "mult=100")
1595 testCheckErr(t, cl.Call("TestRpcInt.EchoStruct", TestRpcABC{"Aa", "Bb", "Cc"}, &rstr))
1596 testCheckEqual(t, rstr, fmt.Sprintf("%#v", TestRpcABC{"Aa", "Bb", "Cc"}), "rstr=")
1597 testCheckErr(t, cl.Call("TestRpcInt.Echo123", []string{"A1", "B2", "C3"}, &rstr))
1598 testCheckEqual(t, rstr, fmt.Sprintf("%#v", []string{"A1", "B2", "C3"}), "rstr=")
1599 }()
1600 }
1601 return
1602 }
1603
1604 func doTestMapEncodeForCanonical(t *testing.T, h Handle) {
1605 defer testSetup(t, &h)()
1606
1607 v1 := map[stringUint64T]interface{}{
1608 stringUint64T{"a", 1}: 1,
1609 stringUint64T{"b", 2}: "hello",
1610 stringUint64T{"c", 3}: map[string]interface{}{
1611 "c/a": 1,
1612 "c/b": "world",
1613 "c/c": []int{1, 2, 3, 4},
1614 "c/d": map[string]interface{}{
1615 "c/d/a": "fdisajfoidsajfopdjsaopfjdsapofda",
1616 "c/d/b": "fdsafjdposakfodpsakfopdsakfpodsakfpodksaopfkdsopafkdopsa",
1617 "c/d/c": "poir02 ir30qif4p03qir0pogjfpoaerfgjp ofke[padfk[ewapf kdp[afep[aw",
1618 "c/d/d": "fdsopafkd[sa f-32qor-=4qeof -afo-erfo r-eafo 4e- o r4-qwo ag",
1619 "c/d/e": "kfep[a sfkr0[paf[a foe-[wq ewpfao-q ro3-q ro-4qof4-qor 3-e orfkropzjbvoisdb",
1620 "c/d/f": "",
1621 },
1622 "c/e": map[int]string{
1623 1: "1",
1624 22: "22",
1625 333: "333",
1626 4444: "4444",
1627 55555: "55555",
1628 },
1629 "c/f": map[string]int{
1630 "1": 1,
1631 "22": 22,
1632 "333": 333,
1633 "4444": 4444,
1634 "55555": 55555,
1635 },
1636 "c/g": map[bool]int{
1637 false: 0,
1638 true: 1,
1639 },
1640 "c/t": map[time.Time]int64{
1641 time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC): time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
1642 time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC): time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
1643 time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC): time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
1644 },
1645 },
1646 }
1647 var v2 map[stringUint64T]interface{}
1648 var b1, b2, b3 []byte
1649
1650
1651
1652
1653
1654
1655
1656
1657 var cborIndef bool
1658 if ch, ok := h.(*CborHandle); ok {
1659 cborIndef = ch.IndefiniteLength
1660 }
1661
1662
1663
1664
1665
1666
1667 bh := testBasicHandle(h)
1668
1669 defer func(c, si bool) {
1670 bh.Canonical = c
1671 bh.SignedInteger = si
1672 }(bh.Canonical, bh.SignedInteger)
1673 bh.Canonical = true
1674
1675
1676 e1 := NewEncoderBytes(&b1, h)
1677 e1.MustEncode(v1)
1678 d1 := NewDecoderBytes(b1, h)
1679 d1.MustDecode(&v2)
1680
1681 e2 := NewEncoderBytes(&b2, h)
1682 e2.MustEncode(v2)
1683 var b1t, b2t = b1, b2
1684 if cborIndef {
1685 e2 = NewEncoderBytes(&b3, h)
1686 e2.MustEncode(v2)
1687 b1t, b2t = b2, b3
1688 }
1689
1690 if !bytes.Equal(b1t, b2t) {
1691 t.Logf("Unequal bytes of length: %v vs %v", len(b1t), len(b2t))
1692 if testVerbose {
1693 t.Logf("Unequal bytes: \n\t%v \n\tVS \n\t%v", b1t, b2t)
1694 }
1695 t.FailNow()
1696 }
1697 }
1698
1699 func doTestStdEncIntf(t *testing.T, h Handle) {
1700 defer testSetup(t, &h)()
1701 args := [][2]interface{}{
1702 {&TestABC{"A", "BB", "CCC"}, new(TestABC)},
1703 {&TestABC2{"AAA", "BB", "C"}, new(TestABC2)},
1704 }
1705 for _, a := range args {
1706 var b []byte
1707 e := NewEncoderBytes(&b, h)
1708 e.MustEncode(a[0])
1709 d := NewDecoderBytes(b, h)
1710 d.MustDecode(a[1])
1711 if err := deepEqual(a[0], a[1]); err == nil {
1712 if testVerbose {
1713 t.Logf("++++ Objects match")
1714 }
1715 } else {
1716 t.Logf("---- FAIL: Objects do not match: y0: %v, y1: %v, err: %v", a[0], a[1], err)
1717 t.FailNow()
1718 }
1719 }
1720 }
1721
1722 func doTestEncCircularRef(t *testing.T, h Handle) {
1723 if !testRecoverPanicToErr {
1724 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
1725 }
1726 defer testSetup(t, &h)()
1727 type T1 struct {
1728 S string
1729 B bool
1730 T interface{}
1731 }
1732 type T2 struct {
1733 S string
1734 T *T1
1735 }
1736 type T3 struct {
1737 S string
1738 T *T2
1739 }
1740 t1 := T1{"t1", true, nil}
1741 t2 := T2{"t2", &t1}
1742 t3 := T3{"t3", &t2}
1743 t1.T = &t3
1744
1745 var bs []byte
1746 var err error
1747
1748 bh := testBasicHandle(h)
1749 if !bh.CheckCircularRef {
1750 bh.CheckCircularRef = true
1751 defer func() { bh.CheckCircularRef = false }()
1752 }
1753 err = NewEncoderBytes(&bs, h).Encode(&t3)
1754 if err == nil || !strings.Contains(err.Error(), "circular reference found") {
1755 t.Logf("expect circular reference error, got: %v", err)
1756 t.FailNow()
1757 }
1758 if x := err.Error(); strings.Contains(x, "circular") || strings.Contains(x, "cyclic") {
1759 if testVerbose {
1760 t.Logf("error detected as expected: %v", x)
1761 }
1762 } else {
1763 t.Logf("FAIL: error detected was not as expected: %v", x)
1764 t.FailNow()
1765 }
1766 }
1767
1768
1769
1770 type (
1771 TestAnonCycleT1 struct {
1772 S string
1773 TestAnonCycleT2
1774 }
1775 TestAnonCycleT2 struct {
1776 S2 string
1777 TestAnonCycleT3
1778 }
1779 TestAnonCycleT3 struct {
1780 *TestAnonCycleT1
1781 }
1782 )
1783
1784 func doTestAnonCycle(t *testing.T, h Handle) {
1785 defer testSetup(t, &h)()
1786 var x TestAnonCycleT1
1787 x.S = "hello"
1788 x.TestAnonCycleT2.S2 = "hello.2"
1789 x.TestAnonCycleT2.TestAnonCycleT3.TestAnonCycleT1 = &x
1790
1791
1792 rt := reflect.TypeOf((*TestAnonCycleT1)(nil)).Elem()
1793 rtid := rt2id(rt)
1794 pti := testBasicHandle(h).getTypeInfo(rtid, rt)
1795 if testVerbose {
1796 t.Logf("[%s] pti: %v", h.Name(), pti)
1797 }
1798 }
1799
1800 func doTestAllErrWriter(t *testing.T, hh ...Handle) {
1801 if !testRecoverPanicToErr {
1802 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
1803 }
1804 defer testSetup(t, nil)()
1805 for _, h := range hh {
1806 if testUseParallel {
1807 h = testHandleCopy(h)
1808 }
1809 __doTestErrWriter(t, h)
1810 }
1811 }
1812
1813 func __doTestErrWriter(t *testing.T, h Handle) {
1814 name := h.Name()
1815 var ew testErrWriter
1816 w := bufio.NewWriterSize(&ew, 4)
1817 enc := NewEncoder(w, h)
1818 for i := 0; i < 4; i++ {
1819 err := enc.Encode("ugorji")
1820 if ev, ok := err.(*codecError); ok {
1821 err = ev.Cause()
1822 }
1823 if err != testErrWriterErr {
1824 t.Logf("%s: expecting err: %v, received: %v", name, testErrWriterErr, err)
1825 t.FailNow()
1826 }
1827 }
1828 }
1829
1830 func __doTestJsonLargeInteger(t *testing.T, v interface{}, ias uint8, jh *JsonHandle) {
1831 if testVerbose {
1832 t.Logf("Running TestJsonLargeInteger: v: %#v, ias: %c", v, ias)
1833 }
1834 oldIAS := jh.IntegerAsString
1835 defer func() { jh.IntegerAsString = oldIAS }()
1836 jh.IntegerAsString = ias
1837
1838 var vu uint
1839 var vi int
1840 var vb bool
1841 var b []byte
1842 e := NewEncoderBytes(&b, jh)
1843 e.MustEncode(v)
1844 e.MustEncode(true)
1845 d := NewDecoderBytes(b, jh)
1846
1847
1848 fnStrChk := func() {
1849
1850
1851 if !(len(b) >= 5 && b[0] == '"' && string(b[len(b)-5:]) == `"true`) {
1852 t.Logf("Expecting a JSON string, got: '%s'", b)
1853 t.FailNow()
1854 }
1855 }
1856
1857 switch ias {
1858 case 'L':
1859 switch v2 := v.(type) {
1860 case int:
1861 v2n := int64(v2)
1862 if v2n > 1<<53 || (v2n < 0 && -v2n > 1<<53) {
1863 fnStrChk()
1864 }
1865 case uint:
1866 v2n := uint64(v2)
1867 if v2n > 1<<53 {
1868 fnStrChk()
1869 }
1870 }
1871 case 'A':
1872 fnStrChk()
1873 default:
1874
1875 for _, i := range b {
1876 if i == '"' {
1877 t.Logf("Expecting a JSON Number without quotation: got: %s", b)
1878 t.FailNow()
1879 }
1880 }
1881 }
1882 switch v2 := v.(type) {
1883 case int:
1884 d.MustDecode(&vi)
1885 d.MustDecode(&vb)
1886
1887 if !(vb && vi == v2) {
1888 t.Logf("Expecting equal values from %s: got golden: %v, decoded: %v", b, v2, vi)
1889 t.FailNow()
1890 }
1891 case uint:
1892 d.MustDecode(&vu)
1893 d.MustDecode(&vb)
1894
1895 if !(vb && vu == v2) {
1896 t.Logf("Expecting equal values from %s: got golden: %v, decoded: %v", b, v2, vu)
1897 t.FailNow()
1898 }
1899 }
1900 }
1901
1902 func doTestRawValue(t *testing.T, h Handle) {
1903 defer testSetup(t, &h)()
1904 bh := testBasicHandle(h)
1905 if !bh.Raw {
1906 bh.Raw = true
1907 defer func() { bh.Raw = false }()
1908 }
1909
1910 var i, i2 int
1911 var v, v2 TestRawValue
1912 var bs, bs2 []byte
1913
1914 i = 1234
1915 v = TestRawValue{I: i}
1916 e := NewEncoderBytes(&bs, h)
1917 e.MustEncode(v.I)
1918 if testVerbose {
1919 t.Logf(">>> raw: %v, %s\n", bs, bs)
1920 }
1921
1922 v.R = Raw(bs)
1923 e.ResetBytes(&bs2)
1924 e.MustEncode(v)
1925
1926 if testVerbose {
1927 t.Logf(">>> bs2: %v, %s\n", bs2, bs2)
1928 }
1929 d := NewDecoderBytes(bs2, h)
1930 d.MustDecode(&v2)
1931 d.ResetBytes(v2.R)
1932 if testVerbose {
1933 t.Logf(">>> v2.R: %v, %s\n", ([]byte)(v2.R), ([]byte)(v2.R))
1934 }
1935 d.MustDecode(&i2)
1936
1937 if testVerbose {
1938 t.Logf(">>> Encoded %v, decoded %v\n", i, i2)
1939 }
1940
1941 if i != i2 {
1942 t.Logf("Error: encoded %v, decoded %v", i, i2)
1943 t.FailNow()
1944 }
1945 }
1946
1947
1948
1949
1950
1951 func doTestPythonGenStreams(t *testing.T, h Handle) {
1952 defer testSetup(t, &h)()
1953
1954
1955
1956
1957 name := h.Name()
1958 if testVerbose {
1959 t.Logf("TestPythonGenStreams-%v", name)
1960 }
1961 tmpdir, err := ioutil.TempDir("", "golang-"+name+"-test")
1962 if err != nil {
1963 t.Logf("-------- Unable to create temp directory\n")
1964 t.FailNow()
1965 }
1966 defer os.RemoveAll(tmpdir)
1967 if testVerbose {
1968 t.Logf("tmpdir: %v", tmpdir)
1969 }
1970 cmd := exec.Command("python", "test.py", "testdata", tmpdir)
1971
1972
1973 var cmdout []byte
1974 if cmdout, err = cmd.CombinedOutput(); err != nil {
1975 t.Logf("-------- Error running test.py testdata. Err: %v", err)
1976 t.Logf(" %v", string(cmdout))
1977 t.FailNow()
1978 }
1979
1980 bh := testBasicHandle(h)
1981
1982 defer func(tt reflect.Type) { bh.MapType = tt }(bh.MapType)
1983 defer func(b bool) { bh.RawToString = b }(bh.RawToString)
1984
1985
1986 bh.RawToString = true
1987
1988 oldMapType := bh.MapType
1989 tablePythonVerify := testTableVerify(testVerifyForPython|testVerifyTimeAsInteger|testVerifyMapTypeStrIntf, h)
1990 for i, v := range tablePythonVerify {
1991
1992 bh.MapType = oldMapType
1993
1994
1995
1996
1997
1998 if testVerbose {
1999 t.Logf("..............................................")
2000 t.Logf(" Testing: #%d: %T, %#v\n", i, v, v)
2001 }
2002 var bss []byte
2003 bss, err = ioutil.ReadFile(filepath.Join(tmpdir, strconv.Itoa(i)+"."+name+".golden"))
2004 if err != nil {
2005 t.Logf("-------- Error reading golden file: %d. Err: %v", i, err)
2006 t.FailNow()
2007 continue
2008 }
2009 bh.MapType = testMapStrIntfTyp
2010
2011 var v1 interface{}
2012 if err = testUnmarshal(&v1, bss, h); err != nil {
2013 t.Logf("-------- Error decoding stream: %d: Err: %v", i, err)
2014 t.FailNow()
2015 continue
2016 }
2017 if v == skipVerifyVal {
2018 continue
2019 }
2020
2021
2022 if err = deepEqual(v, v1); err == nil {
2023 if testVerbose {
2024 t.Logf("++++++++ Objects match: %T, %v", v, v)
2025 }
2026 } else {
2027 t.Logf("-------- FAIL: Objects do not match: %v. Source: %T. Decoded: %T", err, v, v1)
2028 if testVerbose {
2029 t.Logf("-------- GOLDEN: %#v", v)
2030
2031 t.Logf("-------- DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
2032 }
2033 t.FailNow()
2034 }
2035 bsb, err := testMarshal(v1, h)
2036 if err != nil {
2037 t.Logf("Error encoding to stream: %d: Err: %v", i, err)
2038 t.FailNow()
2039 continue
2040 }
2041 if err = deepEqual(bsb, bss); err == nil {
2042 if testVerbose {
2043 t.Logf("++++++++ Bytes match")
2044 }
2045 } else {
2046 xs := "--------"
2047 if reflect.ValueOf(v).Kind() == reflect.Map {
2048 xs = " "
2049 if testVerbose {
2050 t.Logf("%s FAIL - bytes do not match, but it's a map (ok - dependent on ordering): %v", xs, err)
2051 }
2052 } else {
2053 t.Logf("%s FAIL - bytes do not match and is not a map (bad): %v", xs, err)
2054 t.FailNow()
2055 }
2056 if testVerbose {
2057 t.Logf("%s FROM_FILE: %4d] %v", xs, len(bss), bss)
2058 t.Logf("%s ENCODED: %4d] %v", xs, len(bsb), bsb)
2059 }
2060 }
2061 testReleaseBytes(bsb)
2062 }
2063 bh.MapType = oldMapType
2064 }
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075 func doTestMsgpackRpcSpecGoClientToPythonSvc(t *testing.T, h Handle) {
2076 if testSkipRPCTests {
2077 t.Skip(testSkipRPCTestsMsg)
2078 }
2079 defer testSetup(t, &h)()
2080
2081
2082 r := rand.New(rand.NewSource(time.Now().UnixNano()))
2083 openPort := strconv.FormatInt(6700+r.Int63n(99), 10)
2084
2085 cmd := exec.Command("python", "test.py", "rpc-server", openPort, "4")
2086 testCheckErr(t, cmd.Start())
2087 bs, err2 := net.Dial("tcp", ":"+openPort)
2088 maxSleepTime := 500 * time.Millisecond
2089 iterSleepTime := 5 * time.Millisecond
2090 for i := 0; i < int(maxSleepTime/iterSleepTime) && err2 != nil; i++ {
2091 time.Sleep(iterSleepTime)
2092 bs, err2 = net.Dial("tcp", ":"+openPort)
2093 }
2094 testCheckErr(t, err2)
2095 cc := MsgpackSpecRpc.ClientCodec(testReadWriteCloser(bs), h)
2096 cl := rpc.NewClientWithCodec(cc)
2097 defer cl.Close()
2098 var rstr string
2099 testCheckErr(t, cl.Call("EchoStruct", TestRpcABC{"Aa", "Bb", "Cc"}, &rstr))
2100
2101 var mArgs MsgpackSpecRpcMultiArgs = []interface{}{"A1", "B2", "C3"}
2102 testCheckErr(t, cl.Call("Echo123", mArgs, &rstr))
2103 testCheckEqual(t, rstr, "1:A1 2:B2 3:C3", "rstr=")
2104 cmd.Process.Kill()
2105 }
2106
2107 func doTestMsgpackRpcSpecPythonClientToGoSvc(t *testing.T, h Handle) {
2108 if testSkipRPCTests {
2109 t.Skip(testSkipRPCTestsMsg)
2110 }
2111 defer testSetup(t, &h)()
2112
2113 exitSleepDur := 1000 * time.Millisecond
2114 port := doTestCodecRpcOne(t, MsgpackSpecRpc, h, false, exitSleepDur)
2115 cmd := exec.Command("python", "test.py", "rpc-client-go-service", strconv.Itoa(port))
2116 var cmdout []byte
2117 var err error
2118 if cmdout, err = cmd.CombinedOutput(); err != nil {
2119 t.Logf("-------- Error running test.py rpc-client-go-service. Err: %v", err)
2120 t.Logf(" %v", string(cmdout))
2121 t.FailNow()
2122 }
2123 testCheckEqual(t, string(cmdout),
2124 fmt.Sprintf("%#v\n%#v\n", []string{"A1", "B2", "C3"}, TestRpcABC{"Aa", "Bb", "Cc"}), "cmdout=")
2125 }
2126
2127 func doTestSwallowAndZero(t *testing.T, h Handle) {
2128 defer testSetup(t, &h)()
2129 v1 := newTestStrucFlex(testDepth, testNumRepeatString, false, false, testMapStringKeyOnly)
2130 var b1 []byte
2131
2132 e1 := NewEncoderBytes(&b1, h)
2133 e1.MustEncode(v1)
2134 d1 := NewDecoderBytes(b1, h)
2135 d1.swallow()
2136 if d1.r().numread() != uint(len(b1)) {
2137 t.Logf("swallow didn't consume all encoded bytes: %v out of %v", d1.r().numread(), len(b1))
2138 t.FailNow()
2139 }
2140 setZero(v1)
2141 testDeepEqualErr(v1, &TestStrucFlex{}, t, "filled-and-zeroed")
2142 }
2143
2144 func doTestRawExt(t *testing.T, h Handle) {
2145 defer testSetup(t, &h)()
2146 var b []byte
2147 var v RawExt
2148 _, isJson := h.(*JsonHandle)
2149 _, isCbor := h.(*CborHandle)
2150 bh := testBasicHandle(h)
2151
2152
2153 for _, r := range []RawExt{
2154 {Tag: 99, Value: "9999", Data: []byte("9999")},
2155 } {
2156 e := NewEncoderBytes(&b, h)
2157 e.MustEncode(&r)
2158
2159 d := NewDecoderBytes(b, h)
2160 d.MustDecode(&v)
2161 var r2 = r
2162 switch {
2163 case isJson:
2164 r2.Tag = 0
2165 r2.Data = nil
2166 case isCbor:
2167 r2.Data = nil
2168 default:
2169 r2.Value = nil
2170 }
2171 testDeepEqualErr(v, r2, t, "rawext-default")
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184 }
2185
2186
2187 if b != nil {
2188 b = b[:0]
2189 }
2190 oldRawMode := bh.Raw
2191 defer func() { bh.Raw = oldRawMode }()
2192 bh.Raw = true
2193
2194 var v2 Raw
2195 for _, s := range []string{
2196 "goodbye",
2197 "hello",
2198 } {
2199 e := NewEncoderBytes(&b, h)
2200 e.MustEncode(&s)
2201
2202 var r Raw = make([]byte, len(b))
2203 copy(r, b)
2204 d := NewDecoderBytes(b, h)
2205 d.MustDecode(&v2)
2206 testDeepEqualErr(v2, r, t, "raw-default")
2207 }
2208
2209 }
2210
2211
2212
2213
2214
2215
2216 func doTestMapStructKey(t *testing.T, h Handle) {
2217 defer testSetup(t, &h)()
2218 var b []byte
2219 var v interface{}
2220 bh := testBasicHandle(h)
2221 m := map[stringUint64T]wrapUint64Slice{
2222 stringUint64T{"55555", 55555}: []wrapUint64{12345},
2223 stringUint64T{"333", 333}: []wrapUint64{123},
2224 }
2225 oldCanonical := bh.Canonical
2226 oldMapType := bh.MapType
2227 defer func() {
2228 bh.Canonical = oldCanonical
2229 bh.MapType = oldMapType
2230 }()
2231
2232 bh.MapType = reflect.TypeOf((*map[stringUint64T]wrapUint64Slice)(nil)).Elem()
2233 for _, bv := range [2]bool{true, false} {
2234 b, v = nil, nil
2235 bh.Canonical = bv
2236 e := NewEncoderBytes(&b, h)
2237 e.MustEncode(m)
2238 d := NewDecoderBytes(b, h)
2239 d.MustDecode(&v)
2240 testDeepEqualErr(v, m, t, "map-structkey")
2241 }
2242 }
2243
2244 func doTestDecodeNilMapValue(t *testing.T, h Handle) {
2245 defer testSetup(t, &h)()
2246 type Struct struct {
2247 Field map[uint16]map[uint32]struct{}
2248 }
2249
2250 bh := testBasicHandle(h)
2251 defer func(t reflect.Type) {
2252 bh.MapType = t
2253 }(bh.MapType)
2254
2255
2256
2257 _, isJsonHandle := h.(*JsonHandle)
2258
2259 toEncode := Struct{Field: map[uint16]map[uint32]struct{}{
2260 1: nil,
2261 }}
2262
2263 bs := testMarshalErr(toEncode, h, t, "-")
2264 if isJsonHandle && testVerbose {
2265 t.Logf("json encoded: %s\n", bs)
2266 }
2267
2268 var decoded Struct
2269 testUnmarshalErr(&decoded, bs, h, t, "-")
2270 if !reflect.DeepEqual(decoded, toEncode) {
2271 t.Logf("Decoded value %#v != %#v", decoded, toEncode)
2272 t.FailNow()
2273 }
2274 testReleaseBytes(bs)
2275
2276 __doTestDecodeNilMapEntryValue(t, h)
2277 }
2278
2279 func __doTestDecodeNilMapEntryValue(t *testing.T, h Handle) {
2280 type Entry struct{}
2281 type Entries struct {
2282 Map map[string]*Entry
2283 }
2284
2285 c := Entries{
2286 Map: map[string]*Entry{
2287 "nil": nil,
2288 "empty": &Entry{},
2289 },
2290 }
2291
2292 bs, err := testMarshal(&c, h)
2293 if err != nil {
2294 t.Logf("failed to encode: %v", err)
2295 t.FailNow()
2296 }
2297
2298 var f Entries
2299 err = testUnmarshal(&f, bs, h)
2300 if err != nil {
2301 t.Logf("failed to decode: %v", err)
2302 t.FailNow()
2303 }
2304
2305 if !reflect.DeepEqual(c, f) {
2306 t.Logf("roundtrip encoding doesn't match\nexpected: %v\nfound: %v\n\n"+
2307 "empty value: %#+v\nnil value: %#+v",
2308 c, f, f.Map["empty"], f.Map["nil"])
2309 t.FailNow()
2310 }
2311 testReleaseBytes(bs)
2312 }
2313
2314 func doTestEmbeddedFieldPrecedence(t *testing.T, h Handle) {
2315 defer testSetup(t, &h)()
2316 type Embedded struct {
2317 Field byte
2318 }
2319 type Struct struct {
2320 Field byte
2321 Embedded
2322 }
2323 toEncode := Struct{
2324 Field: 1,
2325 Embedded: Embedded{Field: 2},
2326 }
2327 _, isJsonHandle := h.(*JsonHandle)
2328 handle := testBasicHandle(h)
2329 oldMapType := handle.MapType
2330 defer func() { handle.MapType = oldMapType }()
2331
2332 handle.MapType = reflect.TypeOf(map[interface{}]interface{}(nil))
2333
2334 bs, err := testMarshal(toEncode, h)
2335 if err != nil {
2336 t.Logf("Error encoding: %v, Err: %v", toEncode, err)
2337 t.FailNow()
2338 }
2339
2340 var decoded Struct
2341 err = testUnmarshal(&decoded, bs, h)
2342 if err != nil {
2343 t.Logf("Error decoding: %v", err)
2344 t.FailNow()
2345 }
2346
2347 if decoded.Field != toEncode.Field {
2348 t.Logf("Decoded result %v != %v", decoded.Field, toEncode.Field)
2349 if isJsonHandle {
2350 t.Logf("JSON encoded as: %s", bs)
2351 }
2352 t.FailNow()
2353 }
2354 testReleaseBytes(bs)
2355 }
2356
2357 func doTestLargeContainerLen(t *testing.T, h Handle) {
2358 defer testSetup(t, &h)()
2359
2360
2361
2362
2363
2364
2365 if _, ok := h.(*JsonHandle); ok {
2366 t.Skipf("skipping as json doesn't support prefixed lengths")
2367 }
2368 if c, ok := h.(*CborHandle); ok && c.IndefiniteLength {
2369 t.Skipf("skipping as cbor Indefinite Length doesn't use prefixed lengths")
2370 }
2371
2372 bh := testBasicHandle(h)
2373
2374 var sizes []int
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387 sizes = []int{
2388
2389 0,
2390 1,
2391 math.MaxInt8 + 4,
2392 math.MaxUint8 + 4,
2393 }
2394 if !testing.Short() {
2395 sizes = append(sizes, math.MaxUint16+4)
2396 }
2397
2398 m := make(map[int][]struct{})
2399 for _, i := range sizes {
2400 m[i] = make([]struct{}, i)
2401 }
2402
2403 bs := testMarshalErr(m, h, t, "-slices")
2404 var m2 = make(map[int][]struct{})
2405 testUnmarshalErr(m2, bs, h, t, "-slices")
2406 testDeepEqualErr(m, m2, t, "-slices")
2407
2408 d, x := testSharedCodecDecoder(bs, h, bh)
2409 bs2 := d.d.nextValueBytes([]byte{})
2410 testSharedCodecDecoderAfter(d, x, bh)
2411 testDeepEqualErr(bs, bs2, t, "nextvaluebytes-slices")
2412
2413 testReleaseBytes(bs)
2414
2415
2416 mm := make(map[int]struct{})
2417 for _, i := range sizes {
2418 for j := len(mm); j < i; j++ {
2419 mm[j] = struct{}{}
2420 }
2421 bs = testMarshalErr(mm, h, t, "-map")
2422 var mm2 = make(map[int]struct{})
2423 testUnmarshalErr(mm2, bs, h, t, "-map")
2424 testDeepEqualErr(mm, mm2, t, "-map")
2425
2426 d, x = testSharedCodecDecoder(bs, h, bh)
2427 bs2 = d.d.nextValueBytes([]byte{})
2428 testSharedCodecDecoderAfter(d, x, bh)
2429 testDeepEqualErr(bs, bs2, t, "nextvaluebytes-map")
2430 testReleaseBytes(bs)
2431 }
2432
2433
2434
2435 if safeMode || (32<<(^uint(0)>>63)) < 64 {
2436 return
2437 }
2438
2439
2440
2441
2442
2443
2444 hbinc, okbinc := h.(*BincHandle)
2445 if okbinc {
2446 oldAsSymbols := hbinc.AsSymbols
2447 defer func() { hbinc.AsSymbols = oldAsSymbols }()
2448 }
2449 inOutLen := math.MaxUint16 * 3 / 2
2450 if testing.Short() {
2451 inOutLen = math.MaxUint8 * 2
2452 }
2453 var out = make([]byte, 0, inOutLen)
2454 var in []byte = make([]byte, inOutLen)
2455 for i := range in {
2456 in[i] = 'A'
2457 }
2458
2459 e := NewEncoder(nil, h)
2460
2461 sizes = []int{
2462 0, 1, 4, 8, 12, 16, 28, 32, 36,
2463 math.MaxInt8 - 4, math.MaxInt8, math.MaxInt8 + 4,
2464 math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
2465 }
2466 if !testing.Short() {
2467 sizes = append(sizes,
2468 math.MaxInt16-4, math.MaxInt16, math.MaxInt16+4,
2469 math.MaxUint16, math.MaxUint16+4, math.MaxUint16-4)
2470 }
2471
2472 for _, i := range sizes {
2473 var m1, m2 map[string]bool
2474 m1 = make(map[string]bool, 1)
2475
2476 var s1 = string(in[:i])
2477
2478 m1[s1] = true
2479
2480 if okbinc {
2481 hbinc.AsSymbols = 2
2482 }
2483 out = out[:0]
2484 e.ResetBytes(&out)
2485 e.MustEncode(m1)
2486
2487 m2 = make(map[string]bool, 1)
2488 testUnmarshalErr(m2, out, h, t, "no-symbols-string")
2489 testDeepEqualErr(m1, m2, t, "no-symbols-string")
2490
2491 d, x = testSharedCodecDecoder(out, h, bh)
2492 bs2 = d.d.nextValueBytes([]byte{})
2493 testSharedCodecDecoderAfter(d, x, bh)
2494 testDeepEqualErr(out, bs2, t, "nextvaluebytes-no-symbols-string")
2495
2496 if okbinc {
2497
2498 hbinc.AsSymbols = 1
2499 out = out[:0]
2500 e.ResetBytes(&out)
2501 e.MustEncode(m1)
2502
2503 m2 = make(map[string]bool, 1)
2504 testUnmarshalErr(m2, out, h, t, "symbols-string")
2505 testDeepEqualErr(m1, m2, t, "symbols-string")
2506
2507 d, x = testSharedCodecDecoder(out, h, bh)
2508 bs2 = d.d.nextValueBytes([]byte{})
2509 testSharedCodecDecoderAfter(d, x, bh)
2510 testDeepEqualErr(out, bs2, t, "nextvaluebytes-symbols-string")
2511 hbinc.AsSymbols = 2
2512 }
2513 }
2514
2515
2516 var xl, xl2 testUintToBytes
2517 xl = testUintToBytes(inOutLen)
2518 bs = testMarshalErr(xl, h, t, "-large-extension-bytes")
2519 testUnmarshalErr(&xl2, bs, h, t, "-large-extension-bytes")
2520 testDeepEqualErr(xl, xl2, t, "-large-extension-bytes")
2521
2522 d, x = testSharedCodecDecoder(bs, h, bh)
2523 bs2 = d.d.nextValueBytes([]byte{})
2524 testSharedCodecDecoderAfter(d, x, bh)
2525 testDeepEqualErr(bs, bs2, t, "nextvaluebytes-large-extension-bytes")
2526
2527 xl = testUintToBytes(0)
2528 bs = testMarshalErr(xl, h, t, "-large-extension-bytes")
2529 testUnmarshalErr(&xl2, bs, h, t, "-large-extension-bytes")
2530 testDeepEqualErr(xl, xl2, t, "-large-extension-bytes")
2531 }
2532
2533 func testRandomFillRV(v reflect.Value) {
2534 fneg := func() int64 {
2535 i := rand.Intn(2)
2536 if i == 1 {
2537 return 1
2538 }
2539 return -1
2540 }
2541
2542 if v.Type() == timeTyp {
2543 v.Set(reflect.ValueOf(timeToCompare1))
2544 return
2545 }
2546
2547 switch v.Kind() {
2548 case reflect.Invalid:
2549 case reflect.Ptr:
2550 if v.IsNil() {
2551 v.Set(reflect.New(v.Type().Elem()))
2552 }
2553 testRandomFillRV(v.Elem())
2554 case reflect.Interface:
2555 if v.IsNil() {
2556 v.Set(reflect.ValueOf("nothing"))
2557 } else {
2558 testRandomFillRV(v.Elem())
2559 }
2560 case reflect.Struct:
2561 for i, n := 0, v.NumField(); i < n; i++ {
2562 testRandomFillRV(v.Field(i))
2563 }
2564 case reflect.Slice:
2565 if v.IsNil() {
2566 v.Set(reflect.MakeSlice(v.Type(), 4, 4))
2567 }
2568 fallthrough
2569 case reflect.Array:
2570 for i, n := 0, v.Len(); i < n; i++ {
2571 testRandomFillRV(v.Index(i))
2572 }
2573 case reflect.Map:
2574 if v.IsNil() {
2575 v.Set(reflect.MakeMap(v.Type()))
2576 }
2577 if v.Len() == 0 {
2578 kt, vt := v.Type().Key(), v.Type().Elem()
2579 for i := 0; i < 4; i++ {
2580 k0 := reflect.New(kt).Elem()
2581 v0 := reflect.New(vt).Elem()
2582 testRandomFillRV(k0)
2583 testRandomFillRV(v0)
2584 v.SetMapIndex(k0, v0)
2585 }
2586 } else {
2587 for _, k := range v.MapKeys() {
2588 testRandomFillRV(v.MapIndex(k))
2589 }
2590 }
2591 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2592 v.SetInt(fneg() * rand.Int63n(127))
2593 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
2594 v.SetUint(uint64(rand.Int63n(255)))
2595 case reflect.Bool:
2596 v.SetBool(fneg() == 1)
2597 case reflect.Float32, reflect.Float64:
2598 v.SetFloat(float64(fneg()) * float64(rand.Float32()))
2599 case reflect.Complex64, reflect.Complex128:
2600 v.SetComplex(complex(float64(fneg())*float64(rand.Float32()), 0))
2601 case reflect.String:
2602
2603 v.SetString(strings.Repeat(strconv.FormatInt(rand.Int63n(99), 10), rand.Intn(8)) +
2604 "- ABC \x41=\x42 \u2318 - \r \b \f - \u2028 and \u2029 .")
2605 default:
2606 panic(fmt.Errorf("testRandomFillRV: unsupported type: %v", v.Kind()))
2607 }
2608 }
2609
2610 func doTestTime(t *testing.T, h Handle) {
2611 defer testSetup(t, &h)()
2612 name := h.Name()
2613
2614 var tt, tt2 time.Time
2615
2616 tt = time.Unix(20*366*24*60*60, 1000*900).In(time.FixedZone("UGO", -5*60*60))
2617
2618 b := testMarshalErr(tt, h, t, "time-"+name)
2619 testUnmarshalErr(&tt2, b, h, t, "time-"+name)
2620
2621 if !tt2.Equal(tt) {
2622 t.Logf("%s: values not equal: 1: %v, 2: %v", name, tt2, tt)
2623 t.FailNow()
2624 }
2625
2626 testReleaseBytes(b)
2627 }
2628
2629 func doTestUintToInt(t *testing.T, h Handle) {
2630 defer testSetup(t, &h)()
2631 name := h.Name()
2632 var golden = [...]int64{
2633 0, 1, 22, 333, 4444, 55555, 666666,
2634
2635 24, 128,
2636
2637 math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
2638 math.MaxUint16, math.MaxUint16 + 4, math.MaxUint16 - 4,
2639 math.MaxUint32, math.MaxUint32 + 4, math.MaxUint32 - 4,
2640 math.MaxInt8, math.MaxInt8 + 4, math.MaxInt8 - 4,
2641 math.MaxInt16, math.MaxInt16 + 4, math.MaxInt16 - 4,
2642 math.MaxInt32, math.MaxInt32 + 4, math.MaxInt32 - 4,
2643 math.MaxInt64, math.MaxInt64 - 4,
2644 }
2645 var i int64
2646 var ui, ui2 uint64
2647 var fi float64
2648 var b []byte
2649 for _, v := range golden {
2650 i = v
2651 ui = 0
2652 b = testMarshalErr(i, h, t, "int2uint-"+name)
2653 testUnmarshalErr(&ui, b, h, t, "int2uint-"+name)
2654 if ui != uint64(i) {
2655 t.Logf("%s: values not equal: %v, %v", name, ui, uint64(i))
2656 t.FailNow()
2657 }
2658 testReleaseBytes(b)
2659
2660 ui = uint64(i)
2661 i = 0
2662 b = testMarshalErr(ui, h, t, "uint2int-"+name)
2663 testUnmarshalErr(&i, b, h, t, "uint2int-"+name)
2664 if i != int64(ui) {
2665 t.Logf("%s: values not equal: %v, %v", name, i, int64(ui))
2666 t.FailNow()
2667 }
2668 testReleaseBytes(b)
2669
2670 if v == math.MaxInt64 {
2671 ui = uint64(-(v - 1))
2672 } else {
2673 ui = uint64(-v)
2674 }
2675
2676 b = testMarshalErr(ui, h, t, "negint2uint-"+name)
2677 testUnmarshalErr(&ui2, b, h, t, "negint2uint-"+name)
2678 if ui2 != ui {
2679 t.Logf("%s: values not equal: %v, %v", name, ui2, ui)
2680 t.FailNow()
2681 }
2682 testReleaseBytes(b)
2683
2684 fi = 0
2685 b = testMarshalErr(i, h, t, "int2float-"+name)
2686 testUnmarshalErr(&fi, b, h, t, "int2float-"+name)
2687 testReleaseBytes(b)
2688 if fi != float64(i) {
2689 t.Logf("%s: values not equal: %v, %v", name, fi, float64(i))
2690 t.FailNow()
2691 }
2692 }
2693 }
2694
2695 func doTestDifferentMapOrSliceType(t *testing.T, h Handle) {
2696 if !testRecoverPanicToErr {
2697 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
2698 }
2699 defer testSetup(t, &h)()
2700
2701 if mh, ok := h.(*MsgpackHandle); ok {
2702 defer func(b bool) { mh.RawToString = b }(mh.RawToString)
2703 mh.RawToString = true
2704 }
2705
2706 name := h.Name()
2707
2708
2709
2710
2711
2712
2713 bh := testBasicHandle(h)
2714 defer func(oldM, oldS reflect.Type) { bh.MapType, bh.SliceType = oldM, oldS }(bh.MapType, bh.SliceType)
2715
2716
2717 fnArr := func() {
2718 defer func(b1, b2 bool) { bh.StringToRaw, _ = b1, b2 }(bh.StringToRaw, false)
2719 bh.StringToRaw = false
2720 vi := []interface{}{
2721 "hello 1",
2722 float64(111.0),
2723 "hello 3",
2724 float64(333.0),
2725 "hello 5",
2726 }
2727
2728 var mi, mi2 map[string]float64
2729 mi = make(map[string]float64)
2730
2731 mi[vi[0].(string)] = vi[1].(float64)
2732 mi[vi[2].(string)] = vi[3].(float64)
2733
2734 var v4a, v4a2 testMbsArr4T
2735 copy(v4a[:], vi)
2736
2737 b := testMarshalErr(v4a, h, t, "-")
2738 testUnmarshalErr(&mi2, b, h, t, "-")
2739 testDeepEqualErr(mi2, mi, t, "-")
2740 testUnmarshalErr(&v4a2, b, h, t, "-")
2741 testDeepEqualErr(v4a2, v4a, t, "-")
2742 testReleaseBytes(b)
2743
2744 var v0a, v0a2 testMbsArr0T
2745 copy(v0a[:], vi)
2746
2747 mi2 = nil
2748 b = testMarshalErr(v0a, h, t, "-")
2749 testUnmarshalErr(&mi2, b, h, t, "-")
2750 testDeepEqualErr(mi2, map[string]float64{}, t, "-")
2751 testUnmarshalErr(&v0a2, b, h, t, "-")
2752 testDeepEqualErr(v0a2, v0a, t, "-")
2753 testReleaseBytes(b)
2754
2755 {
2756 var v5a testMbsArr5T
2757 copy(v5a[:], vi)
2758 b, err := testMarshal(v5a, h)
2759 testReleaseBytes(b)
2760 if err == nil || !strings.Contains(err.Error(), "mapBySlice requires even slice length") {
2761 t.Logf("mapBySlice for odd length array fail: expected mapBySlice error, got: %v", err)
2762 t.FailNow()
2763 }
2764 }
2765 }
2766 fnArr()
2767
2768 var b []byte
2769
2770 var vi = []interface{}{
2771 "hello 1",
2772 []byte("hello 2"),
2773 "hello 3",
2774 []byte("hello 4"),
2775 "hello 5",
2776 }
2777
2778 var vs []string
2779 var v2i, v2s testMbsT
2780 var v2ss testMbsCustStrT
2781
2782
2783 for i, v := range vi {
2784 vv, ok := v.(string)
2785 if !ok {
2786 vv = string(v.([]byte))
2787 }
2788 vs = append(vs, vv)
2789 v2i = append(v2i, v, strconv.FormatInt(int64(i+1), 10))
2790 v2s = append(v2s, vv, strconv.FormatInt(int64(i+1), 10))
2791 v2ss = append(v2ss, testCustomStringT(vv), testCustomStringT(strconv.FormatInt(int64(i+1), 10)))
2792 }
2793
2794 var v2d interface{}
2795
2796
2797 var goldSliceS = []string{"hello 1", "hello 2", "hello 3", "hello 4", "hello 5"}
2798 var goldSliceI = []interface{}{"hello 1", "hello 2", "hello 3", "hello 4", "hello 5"}
2799 var goldSlice = []interface{}{goldSliceS, goldSliceI}
2800 for j, g := range goldSlice {
2801 bh.SliceType = reflect.TypeOf(g)
2802 name := fmt.Sprintf("slice-%s-%v", name, j+1)
2803 b = testMarshalErr(vs, h, t, name)
2804 v2d = nil
2805
2806 testUnmarshalErr(&v2d, b, h, t, name)
2807 testDeepEqualErr(v2d, goldSlice[j], t, name)
2808 testReleaseBytes(b)
2809 }
2810
2811
2812
2813
2814
2815
2816 var goldMapSS = map[string]string{"hello 1": "1", "hello 2": "2", "hello 3": "3", "hello 4": "4", "hello 5": "5"}
2817 var goldMapSI = map[string]interface{}{"hello 1": "1", "hello 2": "2", "hello 3": "3", "hello 4": "4", "hello 5": "5"}
2818 var goldMapIS = map[interface{}]testCustomStringT{"hello 1": "1", "hello 2": "2", "hello 3": "3", "hello 4": "4", "hello 5": "5"}
2819 var goldMap = []interface{}{goldMapSS, goldMapSI, goldMapIS}
2820 for j, g := range goldMap {
2821 bh.MapType = reflect.TypeOf(g)
2822 name := fmt.Sprintf("map-%s-%v", name, j+1)
2823
2824
2825 v2d = nil
2826
2827 switch h.(type) {
2828 case *MsgpackHandle, *BincHandle, *CborHandle:
2829 b = testMarshalErr(v2i, h, t, name)
2830 testUnmarshalErr(&v2d, b, h, t, name)
2831 testDeepEqualErr(v2d, goldMap[j], t, name)
2832 testReleaseBytes(b)
2833 default:
2834 b = testMarshalErr(v2s, h, t, name)
2835 testUnmarshalErr(&v2d, b, h, t, name)
2836 testDeepEqualErr(v2d, goldMap[j], t, name)
2837 testReleaseBytes(b)
2838 b = testMarshalErr(v2ss, h, t, name)
2839 v2d = nil
2840 testUnmarshalErr(&v2d, b, h, t, name)
2841 testDeepEqualErr(v2d, goldMap[j], t, name)
2842 testReleaseBytes(b)
2843 }
2844 }
2845
2846
2847
2848 {
2849 var bs1 = []byte("abcdefghijklmnopqrstuvwxyz")
2850 b := testMarshalErr(bs1, h, t, "enc-bytes")
2851
2852 var bs2 = make([]byte, 32, 32)
2853 testUnmarshalErr(bs2, b, h, t, "dec-bytes")
2854 testDeepEqualErr(bs1, bs2[:len(bs1)], t, "cmp-enc-dec-bytes")
2855
2856 testUnmarshalErr(&bs2, b, h, t, "dec-bytes-2")
2857 testDeepEqualErr(bs1, bs2, t, "cmp-enc-dec-bytes-2")
2858
2859 bs2 = bs2[:2:4]
2860 testUnmarshalErr(&bs2, b, h, t, "dec-bytes-2")
2861 testDeepEqualErr(bs1, bs2, t, "cmp-enc-dec-bytes-2")
2862
2863 bs2 = nil
2864 testUnmarshalErr(&bs2, b, h, t, "dec-bytes-2")
2865 testDeepEqualErr(bs1, bs2, t, "cmp-enc-dec-bytes-2")
2866
2867 bs1 = []byte{}
2868 b = testMarshalErr(bs1, h, t, "enc-bytes")
2869
2870 bs2 = nil
2871 testUnmarshalErr(&bs2, b, h, t, "dec-bytes-2")
2872 testDeepEqualErr(bs1, bs2, t, "cmp-enc-dec-bytes-2")
2873
2874 type Ti32 int32
2875 v1 := []Ti32{
2876 9, 99, 999, 9999, 99999, 999999,
2877 9, 99, 999, 9999, 99999, 999999,
2878 9, 99, 999, 9999, 99999, 999999,
2879 }
2880 b = testMarshalErr(v1, h, t, "enc-Ti32")
2881
2882 v2 := make([]Ti32, 20, 20)
2883 testUnmarshalErr(v2, b, h, t, "dec-Ti32")
2884 testDeepEqualErr(v1, v2[:len(v1)], t, "cmp-enc-dec-Ti32")
2885
2886 testUnmarshalErr(&v2, b, h, t, "dec-Ti32-2")
2887 testDeepEqualErr(v1, v2, t, "cmp-enc-dec-Ti32-2")
2888
2889 v2 = v2[:1:3]
2890 testUnmarshalErr(&v2, b, h, t, "dec-Ti32-2")
2891 testDeepEqualErr(v1, v2, t, "cmp-enc-dec-Ti32-2")
2892
2893 v2 = nil
2894 testUnmarshalErr(&v2, b, h, t, "dec-Ti32-2")
2895 testDeepEqualErr(v1, v2, t, "cmp-enc-dec-Ti32-2")
2896
2897 v1 = []Ti32{}
2898 b = testMarshalErr(v1, h, t, "enc-Ti32")
2899
2900 v2 = nil
2901 testUnmarshalErr(&v2, b, h, t, "dec-Ti32-2")
2902 testDeepEqualErr(v1, v2, t, "cmp-enc-dec-Ti32-2")
2903 }
2904 }
2905
2906 func doTestScalars(t *testing.T, h Handle) {
2907 defer testSetup(t, &h)()
2908
2909 if mh, ok := h.(*MsgpackHandle); ok {
2910 defer func(b bool) { mh.RawToString = b }(mh.RawToString)
2911 mh.RawToString = true
2912 }
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925 bh := testBasicHandle(h)
2926 if !bh.Canonical {
2927 bh.Canonical = true
2928 defer func() { bh.Canonical = false }()
2929 }
2930
2931 var bzero = testMarshalErr(nil, h, t, "nil-enc")
2932
2933 vi := []interface{}{
2934 int(0),
2935 int8(0),
2936 int16(0),
2937 int32(0),
2938 int64(0),
2939 uint(0),
2940 uint8(0),
2941 uint16(0),
2942 uint32(0),
2943 uint64(0),
2944 uintptr(0),
2945 float32(0),
2946 float64(0),
2947 bool(false),
2948 time.Time{},
2949 string(""),
2950 []byte(nil),
2951 }
2952 for _, v := range fastpathAv {
2953 vi = append(vi, reflect.Zero(v.rt).Interface())
2954 }
2955 for _, v := range vi {
2956 rv := reflect.New(reflect.TypeOf(v)).Elem()
2957 testRandomFillRV(rv)
2958 v = rv.Interface()
2959
2960 rv2 := reflect.New(rv.Type())
2961 rv2.Elem().Set(rv)
2962 vp := rv2.Interface()
2963
2964 var tname string
2965 switch rv.Kind() {
2966 case reflect.Map:
2967 tname = "map[" + rv.Type().Key().Name() + "]" + rv.Type().Elem().Name()
2968 case reflect.Slice:
2969 tname = "[]" + rv.Type().Elem().Name()
2970 default:
2971 tname = rv.Type().Name()
2972 }
2973
2974 var b, b1, b2 []byte
2975 b1 = testMarshalErr(v, h, t, tname+"-enc")
2976
2977 b = make([]byte, len(b1))
2978 copy(b, b1)
2979 b2 = testMarshalErr(vp, h, t, tname+"-enc-ptr")
2980 testDeepEqualErr(b1, b2, t, tname+"-enc-eq")
2981
2982
2983 setZero(vp)
2984 testDeepEqualErr(rv2.Elem().Interface(), reflect.Zero(rv.Type()).Interface(), t, tname+"-enc-eq-zero-ref")
2985
2986 testUnmarshalErr(vp, b, h, t, tname+"-dec")
2987 testDeepEqualErr(rv2.Elem().Interface(), v, t, tname+"-dec-eq")
2988
2989
2990 testUnmarshalErr(vp, bzero, h, t, tname+"-dec-from-enc-nil")
2991 testDeepEqualErr(rv2.Elem().Interface(), reflect.Zero(rv.Type()).Interface(), t, tname+"-dec-from-enc-nil")
2992 testReleaseBytes(b1)
2993 testReleaseBytes(b2)
2994 }
2995
2996
2997 var r0 Raw
2998 var r = Raw([]byte("hello"))
2999 setZero(&r)
3000 testDeepEqualErr(r, r0, t, "raw-zeroed")
3001
3002 r = Raw([]byte("hello"))
3003 var rv = reflect.ValueOf(&r)
3004 setZero(rv)
3005
3006
3007
3008 testDeepEqualErr(rv.Interface(), &r0, t, "raw-reflect-zeroed")
3009 testReleaseBytes(bzero)
3010 }
3011
3012 func doTestIntfMapping(t *testing.T, h Handle) {
3013 defer testSetup(t, &h)()
3014 name := h.Name()
3015 rti := reflect.TypeOf((*testIntfMapI)(nil)).Elem()
3016 defer func() { testBasicHandle(h).Intf2Impl(rti, nil) }()
3017
3018 type T9 struct {
3019 I testIntfMapI
3020 }
3021
3022 for i, v := range []testIntfMapI{
3023
3024 &testIntfMapT1{"ABC \x41=\x42 \u2318 - \r \b \f - \u2028 and \u2029 ."},
3025 testIntfMapT2{"DEF"},
3026 } {
3027 if err := testBasicHandle(h).Intf2Impl(rti, reflect.TypeOf(v)); err != nil {
3028 t.Logf("Error mapping %v to %T", rti, v)
3029 t.FailNow()
3030 }
3031 var v1, v2 T9
3032 v1 = T9{v}
3033 b := testMarshalErr(v1, h, t, name+"-enc-"+strconv.Itoa(i))
3034 testUnmarshalErr(&v2, b, h, t, name+"-dec-"+strconv.Itoa(i))
3035 testDeepEqualErr(v1, v2, t, name+"-dec-eq-"+strconv.Itoa(i))
3036 testReleaseBytes(b)
3037 }
3038 }
3039
3040 func doTestOmitempty(t *testing.T, h Handle) {
3041 defer testSetup(t, &h)()
3042 name := h.Name()
3043 if testBasicHandle(h).StructToArray {
3044 t.Skipf("skipping OmitEmpty test when StructToArray=true")
3045 }
3046 type T1 struct {
3047 A int `codec:"a"`
3048 B *int `codec:"b,omitempty"`
3049 C int `codec:"c,omitempty"`
3050 }
3051 type T2 struct {
3052 A int `codec:"a"`
3053 }
3054 var v1 T1
3055 var v2 T2
3056 b1 := testMarshalErr(v1, h, t, name+"-omitempty")
3057 b2 := testMarshalErr(v2, h, t, name+"-no-omitempty-trunc")
3058 testDeepEqualErr(b1, b2, t, name+"-omitempty-cmp")
3059 testReleaseBytes(b1)
3060 testReleaseBytes(b2)
3061 }
3062
3063 func doTestMissingFields(t *testing.T, h Handle) {
3064 defer testSetup(t, &h)()
3065 name := h.Name()
3066 if codecgen {
3067 t.Skipf("skipping Missing Fields tests as it is not honored by codecgen")
3068 }
3069 if testBasicHandle(h).StructToArray {
3070 t.Skipf("skipping Missing Fields test when StructToArray=true")
3071 }
3072
3073 v1 := missingFielderT2{S: "true seven eight", B: true, F: 777.0, I: -888}
3074 b1 := testMarshalErr(v1, h, t, name+"-missing-enc-2")
3075
3076 var v2 missingFielderT1
3077 testUnmarshalErr(&v2, b1, h, t, name+"-missing-dec-1")
3078
3079 b2 := testMarshalErr(&v2, h, t, name+"-missing-enc-1")
3080
3081 var v3 missingFielderT2
3082 testUnmarshalErr(&v3, b2, h, t, name+"-missing-dec-2")
3083
3084 testDeepEqualErr(v1, v3, t, name+"-missing-cmp-2")
3085
3086 testReleaseBytes(b1)
3087 testReleaseBytes(b2)
3088
3089 v4 := missingFielderT11{s1: "s111", S2: "S222"}
3090 b1 = testMarshalErr(v4, h, t, name+"-missing-enc-11")
3091 var m4 map[string]string
3092 testUnmarshalErr(&m4, b1, h, t, name+"-missing-dec-11")
3093 testDeepEqualErr(m4, map[string]string{"s1": "s111", "S2": "S222"}, t, name+"-missing-cmp-11")
3094
3095 testReleaseBytes(b1)
3096
3097
3098 bh := testBasicHandle(h)
3099
3100 defer func(c bool) {
3101 bh.Canonical = c
3102 }(bh.Canonical)
3103 bh.Canonical = true
3104
3105 b1 = nil
3106
3107 var s1 = struct {
3108 A int
3109 B int
3110 C int
3111 }{1, 2, 3}
3112
3113 NewEncoderBytes(&b1, h).MustEncode(&s1)
3114
3115 var s2 = struct {
3116 C int
3117 testMissingFieldsMap
3118 }{C: 3}
3119 s2.testMissingFieldsMap = testMissingFieldsMap{
3120 m: map[string]interface{}{
3121 "A": 1,
3122 "B": 2,
3123 },
3124 }
3125
3126 for i := 0; i < 16; i++ {
3127 b2 = nil
3128 NewEncoderBytes(&b2, h).MustEncode(&s2)
3129
3130 if !bytes.Equal(b1, b2) {
3131 t.Fatalf("bytes differed:'%s' vs '%s'", b1, b2)
3132 }
3133 }
3134 }
3135
3136 func doTestMaxDepth(t *testing.T, h Handle) {
3137 if !testRecoverPanicToErr {
3138 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
3139 }
3140 defer testSetup(t, &h)()
3141 name := h.Name()
3142 type T struct {
3143 I interface{}
3144 M int16
3145 S bool
3146 E interface{}
3147 }
3148 type T1 struct {
3149 A1 *T1
3150 }
3151 var table []T
3152 var sfunc = func(n int) (s [1]interface{}, s1 *[1]interface{}) {
3153 s1 = &s
3154 for i := 0; i < n; i++ {
3155 var s0 [1]interface{}
3156 s1[0] = &s0
3157 s1 = &s0
3158 }
3159
3160 return
3161
3162
3163
3164
3165
3166
3167
3168 }
3169 var mfunc = func(n int) (m map[string]interface{}, mlast map[string]interface{}) {
3170 m = make(map[string]interface{})
3171 mlast = make(map[string]interface{})
3172 m["A0"] = mlast
3173 for i := 1; i < n; i++ {
3174 m0 := make(map[string]interface{})
3175 mlast["A"+strconv.FormatInt(int64(i), 10)] = m0
3176 mlast = m0
3177 }
3178
3179 return
3180 }
3181 s, s1 := sfunc(5)
3182 m, _ := mfunc(5)
3183 m99, _ := mfunc(99)
3184
3185 s1[0] = m
3186
3187 table = append(table, T{s, 0, false, nil})
3188 table = append(table, T{s, 256, false, nil})
3189 table = append(table, T{s, 7, false, errMaxDepthExceeded})
3190 table = append(table, T{s, 15, false, nil})
3191 table = append(table, T{m99, 15, true, errMaxDepthExceeded})
3192 table = append(table, T{m99, 215, true, nil})
3193
3194 defer func(n int16) {
3195 testBasicHandle(h).MaxDepth = n
3196 }(testBasicHandle(h).MaxDepth)
3197
3198 for i, v := range table {
3199 testBasicHandle(h).MaxDepth = v.M
3200 b1 := testMarshalErr(v.I, h, t, name+"-maxdepth-enc"+strconv.FormatInt(int64(i), 10))
3201
3202 var err error
3203 v.S = false
3204 if v.S {
3205 var v2 T1
3206 err = testUnmarshal(&v2, b1, h)
3207 } else {
3208 var v2 interface{}
3209 err = testUnmarshal(&v2, b1, h)
3210 }
3211 var err0 interface{} = err
3212 if err1, ok := err.(*codecError); ok {
3213 err0 = err1.err
3214 }
3215 if err0 != v.E {
3216 t.Logf("Unexpected error testing max depth for depth %d: expected %v, received %v", v.M, v.E, err)
3217 t.FailNow()
3218 }
3219
3220
3221 testReleaseBytes(b1)
3222 }
3223 }
3224
3225 func doTestMultipleEncDec(t *testing.T, h Handle) {
3226 defer testSetup(t, &h)()
3227 name := h.Name()
3228
3229
3230
3231 var s1 = "ugorji"
3232 var s2 = "nwoke"
3233 var s11, s21 string
3234 var buf bytes.Buffer
3235 e := NewEncoder(&buf, h)
3236 e.MustEncode(s1)
3237 e.MustEncode(s2)
3238 d := NewDecoder(&buf, h)
3239 d.MustDecode(&s11)
3240 d.MustDecode(&s21)
3241 testDeepEqualErr(s1, s11, t, name+"-multiple-encode")
3242 testDeepEqualErr(s2, s21, t, name+"-multiple-encode")
3243 }
3244
3245 func doTestSelfExt(t *testing.T, h Handle) {
3246 defer testSetup(t, &h)()
3247 name := h.Name()
3248 var ts TestSelfExtImpl
3249 ts.S = "ugorji"
3250 ts.I = 5678
3251 ts.B = true
3252 var ts2 TestSelfExtImpl
3253
3254 bs := testMarshalErr(&ts, h, t, name)
3255 testUnmarshalErr(&ts2, bs, h, t, name)
3256 testDeepEqualErr(&ts, &ts2, t, name)
3257 testReleaseBytes(bs)
3258 }
3259
3260 func doTestBytesEncodedAsArray(t *testing.T, h Handle) {
3261 defer testSetup(t, &h)()
3262 name := h.Name()
3263
3264
3265
3266
3267
3268 var in = make([]int32, 128)
3269 var un = make([]uint8, 128)
3270 for i := range in {
3271 in[i] = int32(i)
3272 un[i] = uint8(i)
3273 }
3274 var out []byte
3275 bs := testMarshalErr(&in, h, t, name)
3276 testUnmarshalErr(&out, bs, h, t, name)
3277
3278 testDeepEqualErr(un, out, t, name)
3279 testReleaseBytes(bs)
3280 }
3281
3282 func doTestStrucEncDec(t *testing.T, h Handle) {
3283 defer testSetup(t, &h)()
3284 name := h.Name()
3285
3286 {
3287 var ts1 = newTestStruc(2, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
3288 var ts2 TestStruc
3289 bs := testMarshalErr(ts1, h, t, name)
3290 testUnmarshalErr(&ts2, bs, h, t, name)
3291 testDeepEqualErr(ts1, &ts2, t, name)
3292 testReleaseBytes(bs)
3293 }
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305 }
3306
3307 func doTestStructKeyType(t *testing.T, h Handle) {
3308 defer testSetup(t, &h)()
3309 name := h.Name()
3310
3311 mh, mok := h.(*MsgpackHandle)
3312
3313 bch, bcok := h.(*BincHandle)
3314
3315 bh := testBasicHandle(h)
3316 s2a := bh.StructToArray
3317 bh.StructToArray = false
3318 ir := bh.InterfaceReset
3319 bh.InterfaceReset = false
3320 var mfx bool
3321 if mok {
3322 mfx = mh.NoFixedNum
3323 mh.NoFixedNum = false
3324 }
3325 var bcsym uint8
3326 if bcok {
3327 bcsym = bch.AsSymbols
3328 bch.AsSymbols = 2
3329 }
3330 defer func() {
3331 bh.StructToArray = s2a
3332 bh.InterfaceReset = ir
3333 if mok {
3334 mh.NoFixedNum = mfx
3335 }
3336 if bcok {
3337 bch.AsSymbols = bcsym
3338 }
3339 }()
3340
3341 var bs1, bs2 []byte
3342
3343 var m = make(map[interface{}]interface{})
3344
3345 fn := func(v interface{}) {
3346 v1 := reflect.New(reflect.TypeOf(v)).Elem().Interface()
3347 bs1 = testMarshalErr(v, h, t, "")
3348 testUnmarshalErr(&v1, bs1, h, t, "")
3349 testDeepEqualErr(v, v1, t, name+"")
3350 bs2 = testMarshalErr(m, h, t, "")
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364 testDeepEqualErr(bs1, bs2, t, name+"")
3365 testReleaseBytes(bs1)
3366 testReleaseBytes(bs2)
3367 }
3368
3369 fnclr := func() {
3370 for k := range m {
3371 delete(m, k)
3372 }
3373 }
3374
3375 m["F"] = 90
3376 fn(&testStrucKeyTypeT0{F: 90})
3377 fnclr()
3378
3379 m["FFFF"] = 100
3380 fn(&testStrucKeyTypeT1{F: 100})
3381 fnclr()
3382
3383 m[int64(-1)] = 200
3384 fn(&testStrucKeyTypeT2{F: 200})
3385 fnclr()
3386
3387 m[int64(1)] = 300
3388 fn(&testStrucKeyTypeT3{F: 300})
3389 fnclr()
3390
3391 m[float64(2.5)] = 400
3392 fn(&testStrucKeyTypeT4{F: 400})
3393 fnclr()
3394 }
3395
3396 func doTestRawToStringToRawEtc(t *testing.T, h Handle) {
3397 defer testSetup(t, &h)()
3398
3399
3400
3401
3402
3403
3404
3405
3406 bh := testBasicHandle(h)
3407
3408 r2s := bh.RawToString
3409 s2r := bh.StringToRaw
3410 can := bh.Canonical
3411 mvr := bh.MapValueReset
3412
3413 mh, mok := h.(*MsgpackHandle)
3414
3415 _, jok := h.(*JsonHandle)
3416
3417 defer func() {
3418 bh.RawToString = r2s
3419 bh.StringToRaw = s2r
3420 bh.Canonical = can
3421 bh.MapValueReset = mvr
3422 }()
3423
3424 bh.Canonical = false
3425
3426 var bs1, bs2 []byte
3427
3428
3429
3430
3431
3432 fne := func(v1, v2 interface{}, b bool) {
3433 bh.StringToRaw = b
3434 bs1 = testMarshalErr(v1, h, t, "")
3435
3436 bs2 = testMarshalErr(v2, h, t, "")
3437 testDeepEqualErr(bs1, bs2, t, "")
3438 testReleaseBytes(bs1)
3439 testReleaseBytes(bs2)
3440 }
3441
3442
3443 fnd := func(v1, v2 interface{}, bs2r, br2s, bwext bool) {
3444 bh.RawToString = br2s
3445 bh.StringToRaw = bs2r
3446 if mok {
3447 mh.RawToString = bwext
3448 }
3449 bs1 = testMarshalErr(v1, h, t, "")
3450 var vn interface{}
3451 testUnmarshalErr(&vn, bs1, h, t, "")
3452 testDeepEqualErr(vn, v2, t, "")
3453 testReleaseBytes(bs1)
3454 }
3455
3456 sv0 := "value"
3457 bv0 := []byte(sv0)
3458
3459 sv1 := sv0
3460 bv1 := []byte(sv1)
3461
3462 m1 := map[string]*string{"key": &sv1}
3463 m2 := map[string]*[]byte{"key": &bv1}
3464
3465
3466 m4 := map[[3]byte][]byte{[3]byte{'k', 'e', 'y'}: bv0}
3467 m5 := map[string][]byte{"key": bv0}
3468
3469
3470 m7 := map[interface{}]interface{}{"key": sv0}
3471 m8 := map[interface{}]interface{}{"key": bv0}
3472
3473
3474 fne(m1, m4, true)
3475
3476
3477
3478 fne(m2, m5, false)
3479
3480
3481
3482
3483 if jok {
3484 goto MAP_VALUE_RESET
3485 }
3486
3487
3488
3489
3490
3491 fnd(m2, m7, true, true, true)
3492
3493
3494
3495 fnd(m1, m8, true, false, false)
3496
3497
3498
3499 fnd(m2, m7, false, true, true)
3500
3501 MAP_VALUE_RESET:
3502
3503 sv2 := "value-new"
3504 m9 := map[string]*string{"key": &sv2}
3505
3506 bs1 = testMarshalErr(m1, h, t, "")
3507
3508 bh.MapValueReset = false
3509 testUnmarshalErr(&m9, bs1, h, t, "")
3510
3511 testDeepEqualErr(sv2, "value", t, "")
3512 testDeepEqualErr(&sv2, m9["key"], t, "")
3513
3514 sv2 = "value-new"
3515 m9["key"] = &sv2
3516 bh.MapValueReset = true
3517 testUnmarshalErr(&m9, bs1, h, t, "")
3518 testDeepEqualErr(sv2, "value-new", t, "")
3519 testDeepEqualErr("value", *(m9["key"]), t, "")
3520
3521
3522
3523
3524
3525
3526
3527 testReleaseBytes(bs1)
3528
3529 }
3530
3531
3532
3533 func doTestJsonDecodeNonStringScalarInStringContext(t *testing.T, h Handle) {
3534 defer testSetup(t, &h)()
3535 var b = `{"s.true": "true", "b.true": true, "s.false": "false", "b.false": false, "s.10": "10", "i.10": 10, "i.-10": -10}`
3536 var golden = map[string]string{"s.true": "true", "b.true": "true", "s.false": "false", "b.false": "false", "s.10": "10", "i.10": "10", "i.-10": "-10"}
3537
3538 var m map[string]string
3539 d := NewDecoderBytes([]byte(b), h)
3540 d.MustDecode(&m)
3541 if err := deepEqual(golden, m); err == nil {
3542 if testVerbose {
3543 t.Logf("++++ match: decoded: %#v", m)
3544 }
3545 } else {
3546 t.Logf("---- mismatch: %v ==> golden: %#v, decoded: %#v", err, golden, m)
3547 t.FailNow()
3548 }
3549
3550 jh := h.(*JsonHandle)
3551
3552 oldMapKeyAsString := jh.MapKeyAsString
3553 oldPreferFloat := jh.PreferFloat
3554 oldSignedInteger := jh.SignedInteger
3555 oldHTMLCharAsIs := jh.HTMLCharsAsIs
3556 oldMapType := jh.MapType
3557
3558 defer func() {
3559 jh.MapKeyAsString = oldMapKeyAsString
3560 jh.PreferFloat = oldPreferFloat
3561 jh.SignedInteger = oldSignedInteger
3562 jh.HTMLCharsAsIs = oldHTMLCharAsIs
3563 jh.MapType = oldMapType
3564 }()
3565
3566 jh.MapType = mapIntfIntfTyp
3567 jh.HTMLCharsAsIs = false
3568 jh.MapKeyAsString = true
3569 jh.PreferFloat = false
3570 jh.SignedInteger = false
3571
3572
3573 b = `{"true": true, "false": false, null: null, "7700000000000000000": 7700000000000000000}`
3574 const num = 7700000000000000000
3575 var golden2 = map[interface{}]interface{}{
3576 true: true,
3577 false: false,
3578 nil: nil,
3579
3580 }
3581
3582 fn := func() {
3583 d.ResetBytes([]byte(b))
3584 var mf interface{}
3585 d.MustDecode(&mf)
3586 if err := deepEqual(golden2, mf); err != nil {
3587 t.Logf("---- mismatch: %v ==> golden: %#v, decoded: %#v", err, golden2, mf)
3588 t.FailNow()
3589 }
3590 }
3591
3592 golden2[uint64(num)] = uint64(num)
3593 fn()
3594 delete(golden2, uint64(num))
3595
3596 jh.SignedInteger = true
3597 golden2[int64(num)] = int64(num)
3598 fn()
3599 delete(golden2, int64(num))
3600 jh.SignedInteger = false
3601
3602 jh.PreferFloat = true
3603 golden2[float64(num)] = float64(num)
3604 fn()
3605 delete(golden2, float64(num))
3606 jh.PreferFloat = false
3607 }
3608
3609 func doTestJsonEncodeIndent(t *testing.T, h Handle) {
3610 defer testSetup(t, &h)()
3611 v := TestSimplish{
3612 Ii: -794,
3613 Ss: `A Man is
3614 after the new line
3615 after new line and tab
3616 `,
3617 }
3618 v2 := v
3619 v.Mm = make(map[string]*TestSimplish)
3620 for i := 0; i < len(v.Ar); i++ {
3621 v3 := v2
3622 v3.Ii += (i * 4)
3623 v3.Ss = fmt.Sprintf("%d - %s", v3.Ii, v3.Ss)
3624 if i%2 == 0 {
3625 v.Ar[i] = &v3
3626 }
3627
3628 v.Sl = append(v.Sl, &v3)
3629 v.Mm[strconv.FormatInt(int64(i), 10)] = &v3
3630 }
3631 jh := h.(*JsonHandle)
3632
3633 oldcan := jh.Canonical
3634 oldIndent := jh.Indent
3635 oldS2A := jh.StructToArray
3636 defer func() {
3637 jh.Canonical = oldcan
3638 jh.Indent = oldIndent
3639 jh.StructToArray = oldS2A
3640 }()
3641 jh.Canonical = true
3642 jh.Indent = -1
3643 jh.StructToArray = false
3644 var bs []byte
3645 NewEncoderBytes(&bs, jh).MustEncode(&v)
3646 txt1Tab := string(bs)
3647 bs = nil
3648 jh.Indent = 120
3649 NewEncoderBytes(&bs, jh).MustEncode(&v)
3650 txtSpaces := string(bs)
3651
3652
3653 goldenResultTab := `{
3654 "Ar": [
3655 {
3656 "Ar": [
3657 null,
3658 null
3659 ],
3660 "Ii": -794,
3661 "Mm": null,
3662 "Sl": null,
3663 "Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
3664 },
3665 null
3666 ],
3667 "Ii": -794,
3668 "Mm": {
3669 "0": {
3670 "Ar": [
3671 null,
3672 null
3673 ],
3674 "Ii": -794,
3675 "Mm": null,
3676 "Sl": null,
3677 "Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
3678 },
3679 "1": {
3680 "Ar": [
3681 null,
3682 null
3683 ],
3684 "Ii": -790,
3685 "Mm": null,
3686 "Sl": null,
3687 "Ss": "-790 - A Man is\nafter the new line\n\tafter new line and tab\n"
3688 }
3689 },
3690 "Sl": [
3691 {
3692 "Ar": [
3693 null,
3694 null
3695 ],
3696 "Ii": -794,
3697 "Mm": null,
3698 "Sl": null,
3699 "Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
3700 },
3701 {
3702 "Ar": [
3703 null,
3704 null
3705 ],
3706 "Ii": -790,
3707 "Mm": null,
3708 "Sl": null,
3709 "Ss": "-790 - A Man is\nafter the new line\n\tafter new line and tab\n"
3710 }
3711 ],
3712 "Ss": "A Man is\nafter the new line\n\tafter new line and tab\n"
3713 }`
3714
3715 if txt1Tab != goldenResultTab {
3716 t.Logf("decoded indented with tabs != expected: \nexpected: %s\nencoded: %s", goldenResultTab, txt1Tab)
3717 t.FailNow()
3718 }
3719 if txtSpaces != strings.Replace(goldenResultTab, "\t", strings.Repeat(" ", 120), -1) {
3720 t.Logf("decoded indented with spaces != expected: \nexpected: %s\nencoded: %s", goldenResultTab, txtSpaces)
3721 t.FailNow()
3722 }
3723 }
3724
3725 func doTestPreferArrayOverSlice(t *testing.T, h Handle) {
3726 defer testSetup(t, &h)()
3727
3728
3729 if codecgen {
3730 t.Skip("skipping ... prefer array over slice is not supported in codecgen mode")
3731 }
3732 bh := testBasicHandle(h)
3733 paos := bh.PreferArrayOverSlice
3734 styp := bh.SliceType
3735 defer func() {
3736 bh.PreferArrayOverSlice = paos
3737 bh.SliceType = styp
3738 }()
3739 bh.PreferArrayOverSlice = true
3740 bh.SliceType = reflect.TypeOf(([]bool)(nil))
3741
3742 s2 := [4]bool{true, false, true, false}
3743 s := s2[:]
3744 var v interface{}
3745 bs := testMarshalErr(s, h, t, t.Name())
3746 testUnmarshalErr(&v, bs, h, t, t.Name())
3747 testDeepEqualErr(s2, v, t, t.Name())
3748 testReleaseBytes(bs)
3749 }
3750
3751 func doTestZeroCopyBytes(t *testing.T, h Handle) {
3752 defer testSetup(t, &h)()
3753
3754 if _, ok := h.(*JsonHandle); ok {
3755 t.Skipf("skipping ... zero copy bytes not supported by json handle")
3756 }
3757 if ch, ok := h.(*CborHandle); ok && ch.IndefiniteLength {
3758 t.Skipf("skipping ... zero copy bytes not supported by cbor handle with IndefiniteLength=true")
3759 }
3760
3761 bh := testBasicHandle(h)
3762 zc := bh.ZeroCopy
3763 defer func() {
3764 bh.ZeroCopy = zc
3765 }()
3766 bh.ZeroCopy = true
3767
3768 s := []byte("hello")
3769 var v []byte
3770 bs := testMarshalErr(s, h, t, t.Name())
3771
3772
3773 NewDecoderBytes(bs, h).MustDecode(&v)
3774
3775
3776
3777 for i := range bs {
3778 if &bs[i] == &v[0] {
3779 return
3780 }
3781 }
3782
3783
3784 if len(bs) > 0 && len(v) > 0 {
3785 t.Logf("%s: ZeroCopy=true, but decoded (%p) is not slice of input: (%p)", h.Name(), &v[0], &bs[0])
3786 } else {
3787 t.Logf("%s: ZeroCopy=true, but decoded OR input slice is empty: %v, %v", h.Name(), v, bs)
3788 }
3789 testReleaseBytes(bs)
3790 t.FailNow()
3791 }
3792
3793 func doTestNextValueBytes(t *testing.T, h Handle) {
3794 defer testSetup(t, &h)()
3795
3796 bh := testBasicHandle(h)
3797
3798
3799
3800 var inputs = []interface{}{
3801 uint64(7777),
3802 int64(9999),
3803 float64(12.25),
3804 true,
3805 false,
3806 map[string]uint64{"1": 1, "22": 22, "333": 333, "4444": 4444},
3807 []string{"1", "22", "333", "4444"},
3808
3809
3810 newTestStruc(testDepth, testNumRepeatString, false, false, true),
3811 "1223334444",
3812 }
3813 var out []byte
3814
3815 for i, v := range inputs {
3816 _ = i
3817 bs := testMarshalErr(v, h, t, "nextvaluebytes")
3818 out = append(out, bs...)
3819 bs2 := testMarshalErr(nil, h, t, "nextvaluebytes")
3820 out = append(out, bs2...)
3821 testReleaseBytes(bs)
3822 testReleaseBytes(bs2)
3823 }
3824
3825
3826 var valueBytes = make([][]byte, len(inputs)*2)
3827
3828 d, oldReadBufferSize := testSharedCodecDecoder(out, h, testBasicHandle(h))
3829 for i := 0; i < len(inputs)*2; i++ {
3830 valueBytes[i] = d.d.nextValueBytes([]byte{})
3831
3832
3833
3834 }
3835 if testUseIoEncDec >= 0 {
3836 bh.ReaderBufferSize = oldReadBufferSize
3837 }
3838
3839 defer func(b bool) { bh.InterfaceReset = b }(bh.InterfaceReset)
3840 bh.InterfaceReset = false
3841
3842 var result interface{}
3843 for i := 0; i < len(inputs); i++ {
3844
3845 result = reflect.Zero(reflect.TypeOf(inputs[i])).Interface()
3846 testUnmarshalErr(&result, valueBytes[i*2], h, t, "nextvaluebytes")
3847 testDeepEqualErr(inputs[i], result, t, "nextvaluebytes-1")
3848 result = nil
3849 testUnmarshalErr(&result, valueBytes[(i*2)+1], h, t, "nextvaluebytes")
3850 testDeepEqualErr(nil, result, t, "nextvaluebytes-2")
3851 }
3852 }
3853
3854 func doTestNumbers(t *testing.T, h Handle) {
3855 defer testSetup(t, &h)()
3856 __doTestIntegers(t, h)
3857 __doTestFloats(t, h)
3858 __doTestIntegerFloatConversions(t, h)
3859 }
3860
3861 func __doTestIntegers(t *testing.T, h Handle) {
3862
3863
3864
3865 bh := testBasicHandle(h)
3866
3867 oldSignedInteger := bh.SignedInteger
3868 var oldPreferFloat bool
3869 var oldNoFixedNum bool
3870 jh, jok := h.(*JsonHandle)
3871 mh, mok := h.(*MsgpackHandle)
3872 if jok {
3873 oldPreferFloat = jh.PreferFloat
3874 }
3875 if mok {
3876 oldNoFixedNum = mh.NoFixedNum
3877 mh.NoFixedNum = true
3878 }
3879
3880 defer func() {
3881 bh.SignedInteger = oldSignedInteger
3882 if jok {
3883 jh.PreferFloat = oldPreferFloat
3884 }
3885 if mok {
3886 mh.NoFixedNum = oldNoFixedNum
3887 }
3888 }()
3889
3890
3891
3892 var ii interface{}
3893
3894 for _, v := range testUintsToParse {
3895 if jok {
3896 jh.PreferFloat = false
3897 }
3898 b := testMarshalErr(v, h, t, "test-integers")
3899 ii = nil
3900 bh.SignedInteger = true
3901 testUnmarshalErr(&ii, b, h, t, "test-integers")
3902 testDeepEqualErr(ii, int64(v), t, "test-integers-signed")
3903 ii = nil
3904 bh.SignedInteger = false
3905 testUnmarshalErr(&ii, b, h, t, "test-integers")
3906 testDeepEqualErr(ii, uint64(v), t, "test-integers-unsigned")
3907 ii = nil
3908 if jok {
3909 jh.PreferFloat = true
3910 testUnmarshalErr(&ii, b, h, t, "test-integers")
3911 testDeepEqualErr(ii, float64(v), t, "test-integers-float")
3912 }
3913 testReleaseBytes(b)
3914 }
3915 }
3916
3917 var testUintsToParse = []uint64{
3918
3919
3920 2,
3921 2048,
3922 1<<63 - 4,
3923 1800000000e-2,
3924 18000000e+2,
3925 4.56e+4,
3926 4.56e+16,
3927 }
3928
3929 var testFloatsToParse = []float64{
3930 3,
3931 math.NaN(),
3932 math.Inf(1),
3933 math.Inf(-1),
3934 4.56e+4,
3935 4.56e+18,
3936 4.56e+10,
3937 4.56e+30,
3938 1.01234567890123456789e+30,
3939 1.32e+5,
3940 1.32e-5,
3941 0e+01234567890123456789,
3942 1.7976931348623157e308,
3943 -1.7976931348623157e+308,
3944 1e308,
3945 1e-308,
3946 1.694649e-317,
3947
3948 2.2250738585072012e-308,
3949 4.630813248087435e+307,
3950 1.00000000000000011102230246251565404236316680908203125,
3951 1.00000000000000033306690738754696212708950042724609375,
3952 }
3953
3954 func __doTestFloats(t *testing.T, h Handle) {
3955 _, jok := h.(*JsonHandle)
3956
3957 f64s := testFloatsToParse
3958 const unusedVal = 9999
3959
3960
3961
3962 for _, f64 := range f64s {
3963 {
3964 f := f64
3965 var w float64 = unusedVal
3966 b := testMarshalErr(f, h, t, "test-floats-enc")
3967 testUnmarshalErr(&w, b, h, t, "test-floats-dec")
3968
3969 if (jok && (math.IsNaN(f64) || math.IsInf(f64, 0)) && w != 0) ||
3970 (!jok && w != f && !math.IsNaN(float64(f))) {
3971 t.Logf("error testing float64: %v, decoded as: %v", f, w)
3972 t.FailNow()
3973 }
3974 var wi interface{}
3975 testUnmarshalErr(&wi, b, h, t, "test-floats-dec")
3976 if (jok && (math.IsNaN(f64) || math.IsInf(f64, 0)) && wi != nil) ||
3977 (!jok && wi.(float64) != f && !math.IsNaN(float64(f))) {
3978 t.Logf("error testing float64: %v, decoded as: %v", f, wi)
3979 t.FailNow()
3980 }
3981 testReleaseBytes(b)
3982 }
3983 {
3984 f := float32(f64)
3985 var w float32 = unusedVal
3986 b := testMarshalErr(f, h, t, "test-floats-enc")
3987
3988 testUnmarshalErr(&w, b, h, t, "test-floats-dec")
3989 if (jok && (math.IsNaN(f64) || math.IsInf(f64, 0)) && w != 0) ||
3990 (!jok && w != f && !math.IsNaN(float64(f))) {
3991 t.Logf("error testing float32: %v, decoded as: %v", f, w)
3992 t.FailNow()
3993 }
3994 testReleaseBytes(b)
3995 }
3996 }
3997 }
3998
3999 func __doTestIntegerFloatConversions(t *testing.T, h Handle) {
4000 if !testRecoverPanicToErr {
4001 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
4002 }
4003 type tI struct{ N int }
4004 type tU struct{ N uint }
4005 type tF struct{ N float64 }
4006
4007 type elem struct {
4008 in interface{}
4009 out interface{}
4010 err string
4011 }
4012 tests := []elem{
4013
4014 {tI{5}, tF{5.0}, ""},
4015 {tU{5}, tF{5.0}, ""},
4016 {tF{5.0}, tU{5}, ""},
4017 {tF{5.0}, tI{5}, ""},
4018 {tF{-5.0}, tI{-5}, ""},
4019
4020 {tI{-5}, tU{5}, "error"},
4021 {tF{-5.0}, tU{5}, "error"},
4022
4023 {tF{-5.7}, tU{5}, "error"},
4024 {tF{-5.7}, tI{5}, "error"},
4025 }
4026 for _, v := range tests {
4027 r := reflect.New(reflect.TypeOf(v.out))
4028 b := testMarshalErr(v.in, h, t, "")
4029 err := testUnmarshal(r.Interface(), b, h)
4030 if v.err == "" {
4031 testDeepEqualErr(err, nil, t, "")
4032 testDeepEqualErr(r.Elem().Interface(), v.out, t, "")
4033 } else if err == nil {
4034 t.Logf("expecting an error but didn't receive any, with in: %v, out: %v, expecting err matching: %v", v.in, v.out, v.err)
4035 t.FailNow()
4036 }
4037 }
4038 }
4039
4040 func doTestStructFieldInfoToArray(t *testing.T, h Handle) {
4041 if !testRecoverPanicToErr {
4042 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
4043 }
4044 defer testSetup(t, &h)()
4045 bh := testBasicHandle(h)
4046
4047 defer func(b bool) { bh.CheckCircularRef = b }(bh.CheckCircularRef)
4048 bh.CheckCircularRef = true
4049
4050 var vs = Sstructsmall{A: 99}
4051 var vb = Sstructbig{
4052 A: 77,
4053 B: true,
4054 c: "ccc 3 ccc",
4055 Ssmallptr: &vs,
4056 Ssmall: vs,
4057 }
4058 vb.Sptr = &vb
4059
4060 vba := SstructbigToArray{
4061 A: vb.A,
4062 B: vb.B,
4063 c: vb.c,
4064 Ssmallptr: vb.Ssmallptr,
4065 Ssmall: vb.Ssmall,
4066 Sptr: vb.Sptr,
4067 }
4068
4069 var b []byte
4070 var err error
4071 if !codecgen {
4072
4073 b, err = testMarshal(&vba, h)
4074 testReleaseBytes(b)
4075 if err == nil || !strings.Contains(err.Error(), "circular reference found") {
4076 t.Logf("expect circular reference error, got: %v", err)
4077 t.FailNow()
4078 }
4079 }
4080
4081 vb2 := vb
4082 vb.Sptr = nil
4083 vba.Sptr = &vb2
4084
4085 var ss []interface{}
4086
4087
4088 b = testMarshalErr(&vba, h, t, "-")
4089 testUnmarshalErr(&ss, b, h, t, "-")
4090 testDeepEqualErr(ss[1], true, t, "-")
4091 testReleaseBytes(b)
4092 }
4093
4094 func doTestDesc(t *testing.T, h Handle, m map[byte]string) {
4095 defer testSetup(t, &h)()
4096 for k, v := range m {
4097 if s := h.desc(k); s != v {
4098 t.Logf("error describing descriptor: '%q' i.e. 0x%x, expected '%s', got '%s'", k, k, v, s)
4099 t.FailNow()
4100 }
4101 }
4102 }
4103
4104 func TestAtomic(t *testing.T) {
4105 defer testSetup(t, nil)()
4106
4107 if true {
4108 var a atomicTypeInfoSlice
4109 l := a.load()
4110 if l != nil {
4111 t.Logf("atomic fail: %T, expected load return nil, received: %v", a, l)
4112 t.FailNow()
4113 }
4114 l = append(l, rtid2ti{})
4115 a.store(l)
4116 l = a.load()
4117 if len(l) != 1 {
4118 t.Logf("atomic fail: %T, expected load to have length 1, received: %d", a, len(l))
4119 t.FailNow()
4120 }
4121 }
4122 if true {
4123 var a atomicRtidFnSlice
4124 l := a.load()
4125 if l != nil {
4126 t.Logf("atomic fail: %T, expected load return nil, received: %v", a, l)
4127 t.FailNow()
4128 }
4129 l = append(l, codecRtidFn{})
4130 a.store(l)
4131 l = a.load()
4132 if len(l) != 1 {
4133 t.Logf("atomic fail: %T, expected load to have length 1, received: %d", a, len(l))
4134 t.FailNow()
4135 }
4136 }
4137 if true {
4138 var a atomicClsErr
4139 l := a.load()
4140 if l.err != nil {
4141 t.Logf("atomic fail: %T, expected load return clsErr = nil, received: %v", a, l.err)
4142 t.FailNow()
4143 }
4144 l.err = io.EOF
4145 a.store(l)
4146 l = a.load()
4147 if l.err != io.EOF {
4148 t.Logf("atomic fail: %T, expected clsErr = io.EOF, received: %v", a, l.err)
4149 t.FailNow()
4150 }
4151 }
4152 }
4153
4154
4155
4156 func doTestJsonLargeInteger(t *testing.T, h Handle) {
4157 if !testRecoverPanicToErr {
4158 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
4159 }
4160 defer testSetup(t, &h)()
4161 jh := h.(*JsonHandle)
4162 for _, i := range []uint8{'L', 'A', 0} {
4163 for _, j := range []interface{}{
4164 int64(1 << 60),
4165 -int64(1 << 60),
4166 0,
4167 1 << 20,
4168 -(1 << 20),
4169 uint64(1 << 60),
4170 uint(0),
4171 uint(1 << 20),
4172 int64(1840000e-2),
4173 uint64(1840000e+2),
4174 } {
4175 __doTestJsonLargeInteger(t, j, i, jh)
4176 }
4177 }
4178
4179 oldIAS := jh.IntegerAsString
4180 defer func() { jh.IntegerAsString = oldIAS }()
4181 jh.IntegerAsString = 0
4182
4183 type tt struct {
4184 s string
4185 canI, canUi bool
4186 i int64
4187 ui uint64
4188 }
4189
4190 var i int64
4191 var ui uint64
4192 var err error
4193 var d *Decoder = NewDecoderBytes(nil, jh)
4194 for _, v := range []tt{
4195 {"0", true, true, 0, 0},
4196 {"0000", true, true, 0, 0},
4197 {"0.00e+2", true, true, 0, 0},
4198 {"000e-2", true, true, 0, 0},
4199 {"0.00e-2", true, true, 0, 0},
4200
4201 {"9223372036854775807", true, true, math.MaxInt64, math.MaxInt64},
4202 {"92233720368547758.07e+2", true, true, math.MaxInt64, math.MaxInt64},
4203 {"922337203685477580700e-2", true, true, math.MaxInt64, math.MaxInt64},
4204 {"9223372.036854775807E+12", true, true, math.MaxInt64, math.MaxInt64},
4205 {"9223372036854775807000000000000E-12", true, true, math.MaxInt64, math.MaxInt64},
4206 {"0.9223372036854775807E+19", true, true, math.MaxInt64, math.MaxInt64},
4207 {"92233720368547758070000000000000000000E-19", true, true, math.MaxInt64, math.MaxInt64},
4208 {"0.000009223372036854775807E+24", true, true, math.MaxInt64, math.MaxInt64},
4209 {"9223372036854775807000000000000000000000000E-24", true, true, math.MaxInt64, math.MaxInt64},
4210
4211 {"-9223372036854775808", true, false, math.MinInt64, 0},
4212 {"-92233720368547758.08e+2", true, false, math.MinInt64, 0},
4213 {"-922337203685477580800E-2", true, false, math.MinInt64, 0},
4214 {"-9223372.036854775808e+12", true, false, math.MinInt64, 0},
4215 {"-9223372036854775808000000000000E-12", true, false, math.MinInt64, 0},
4216 {"-0.9223372036854775808e+19", true, false, math.MinInt64, 0},
4217 {"-92233720368547758080000000000000000000E-19", true, false, math.MinInt64, 0},
4218 {"-0.000009223372036854775808e+24", true, false, math.MinInt64, 0},
4219 {"-9223372036854775808000000000000000000000000E-24", true, false, math.MinInt64, 0},
4220
4221 {"18446744073709551615", false, true, 0, math.MaxUint64},
4222 {"18446744.073709551615E+12", false, true, 0, math.MaxUint64},
4223 {"18446744073709551615000000000000E-12", false, true, 0, math.MaxUint64},
4224 {"0.000018446744073709551615E+24", false, true, 0, math.MaxUint64},
4225 {"18446744073709551615000000000000000000000000E-24", false, true, 0, math.MaxUint64},
4226
4227
4228 {"18446744073709551610", false, true, 0, math.MaxUint64 - 5},
4229 {"18446744.073709551610E+12", false, true, 0, math.MaxUint64 - 5},
4230 {"18446744073709551610000000000000E-12", false, true, 0, math.MaxUint64 - 5},
4231 {"0.000018446744073709551610E+24", false, true, 0, math.MaxUint64 - 5},
4232 {"18446744073709551610000000000000000000000000E-24", false, true, 0, math.MaxUint64 - 5},
4233
4234 } {
4235 if v.s == "" {
4236 continue
4237 }
4238 d.ResetBytes([]byte(v.s))
4239 err = d.Decode(&ui)
4240 if (v.canUi && err != nil) || (!v.canUi && err == nil) || (v.canUi && err == nil && v.ui != ui) {
4241 t.Logf("Failing to decode %s (as unsigned): %v", v.s, err)
4242 t.FailNow()
4243 }
4244
4245 d.ResetBytes([]byte(v.s))
4246 err = d.Decode(&i)
4247 if (v.canI && err != nil) || (!v.canI && err == nil) || (v.canI && err == nil && v.i != i) {
4248 t.Logf("Failing to decode %s (as signed): %v", v.s, err)
4249 t.FailNow()
4250 }
4251 }
4252 }
4253
4254 func doTestJsonInvalidUnicode(t *testing.T, h Handle) {
4255 if !testRecoverPanicToErr {
4256 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
4257 }
4258 defer testSetup(t, &h)()
4259
4260 jh := h.(*JsonHandle)
4261
4262 var err error
4263
4264
4265 var m = map[string]string{
4266 `"\udc49\u0430abc"`: "\uFFFDabc",
4267 `"\udc49\u0430"`: "\uFFFD",
4268 `"\udc49abc"`: "\uFFFDabc",
4269 `"\udc49"`: "\uFFFD",
4270 `"\udZ49\u0430abc"`: "\uFFFD\u0430abc",
4271 `"\udcG9\u0430"`: "\uFFFD\u0430",
4272 `"\uHc49abc"`: "\uFFFDabc",
4273 `"\uKc49"`: "\uFFFD",
4274 }
4275
4276 for k, v := range m {
4277
4278
4279
4280 var s string
4281 err = testUnmarshal(&s, []byte(k), jh)
4282 if err != nil {
4283 if err == io.EOF || err == io.ErrUnexpectedEOF {
4284 continue
4285 }
4286 t.Logf("%s: unmarshal failed: %v", "-", err)
4287 t.FailNow()
4288 }
4289
4290 if s != v {
4291 t.Logf("unmarshal: not equal: %q, %q", v, s)
4292 t.FailNow()
4293 }
4294 }
4295
4296
4297 m = map[string]string{
4298 `"az\uD834\udD1E"`: "az𝄞",
4299 `"n\ud834\uDD1en"`: "n\U0001D11En",
4300
4301 `"a\\\"\/\"\b\f\n\r\"\tz"`: "a\\\"/\"\b\f\n\r\"\tz",
4302 }
4303 for k, v := range m {
4304 var s string
4305 err = testUnmarshal(&s, []byte(k), jh)
4306 if err != nil {
4307 t.Logf("%s: unmarshal failed: %v", "-", err)
4308 t.FailNow()
4309 }
4310
4311 if s != v {
4312 t.Logf("unmarshal: not equal: %q, %q", v, s)
4313 t.FailNow()
4314 }
4315 }
4316
4317
4318 var b = []byte{'"', 0xef, 0xbf, 0xbd}
4319 var m2 = map[string][]byte{
4320 string([]byte{0xef, 0xbf, 0xbd}): append(b, '"'),
4321 string([]byte{0xef, 0xbf, 0xbd, 0x0, 0x0}): append(b, `\u0000\u0000"`...),
4322
4323 "a\\\"/\"\b\f\n\r\"\tz": []byte(`"a\\\"/\"\b\f\n\r\"\tz"`),
4324
4325
4326
4327
4328 "n\U0001D11En": []byte(`"n𝄞n"`),
4329 "az𝄞": []byte(`"az𝄞"`),
4330
4331 string([]byte{129, 129}): []byte(`"\uFFFD\uFFFD"`),
4332 }
4333
4334 for k, v := range m2 {
4335 b, err = testMarshal(k, jh)
4336 if err != nil {
4337 t.Logf("%s: marshal failed: %v", "-", err)
4338 t.FailNow()
4339 }
4340
4341 if !bytes.Equal(b, v) {
4342 t.Logf("marshal: not equal: %q, %q", v, b)
4343 t.FailNow()
4344 }
4345 testReleaseBytes(b)
4346 }
4347 }
4348
4349 func doTestJsonNumberParsing(t *testing.T, h Handle) {
4350 defer testSetup(t, &h)()
4351 parseInt64_reader := func(r readFloatResult) (v int64, fail bool) {
4352 u, fail := parseUint64_reader(r)
4353 if fail {
4354 return
4355 }
4356 if r.neg {
4357 v = -int64(u)
4358 } else {
4359 v = int64(u)
4360 }
4361 return
4362 }
4363 for _, f64 := range testFloatsToParse {
4364
4365
4366
4367 var precs = [...]int{32, -1}
4368 var r readFloatResult
4369 var fail bool
4370 var fs uint64
4371 var fsi int64
4372 var fint, ffrac float64
4373 _ = ffrac
4374 var bv []byte
4375 for _, prec := range precs {
4376 f := f64
4377 if math.IsNaN(f64) || math.IsInf(f64, 0) {
4378 goto F32
4379 }
4380 bv = strconv.AppendFloat(nil, f, 'E', prec, 64)
4381 if fs, err := parseFloat64_custom(bv); err != nil || fs != f {
4382 t.Logf("float64 -> float64 error (prec: %v) parsing '%s', got %v, expected %v: %v", prec, bv, fs, f, err)
4383 t.FailNow()
4384 }
4385
4386 fint, ffrac = math.Modf(f)
4387
4388 bv = strconv.AppendFloat(bv[:0], fint, 'E', prec, 64)
4389 if f < 0 || fint != float64(uint64(fint)) {
4390 goto F64i
4391 }
4392 r = readFloat(bv, fi64u)
4393 fail = !r.ok
4394 if r.ok {
4395 fs, fail = parseUint64_reader(r)
4396 }
4397 if fail || fs != uint64(fint) {
4398 t.Logf("float64 -> uint64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fs, uint64(fint))
4399 t.FailNow()
4400 }
4401 F64i:
4402 if fint != float64(int64(fint)) {
4403 goto F32
4404 }
4405 r = readFloat(bv, fi64u)
4406 fail = !r.ok
4407 if r.ok {
4408 fsi, fail = parseInt64_reader(r)
4409 }
4410 if fail || fsi != int64(fint) {
4411 t.Logf("float64 -> int64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fsi, int64(fint))
4412 t.FailNow()
4413 }
4414 F32:
4415 f32 := float32(f64)
4416 f64n := float64(f32)
4417 if !(math.IsNaN(f64n) || math.IsInf(f64n, 0)) {
4418 bv := strconv.AppendFloat(nil, f64n, 'E', prec, 32)
4419 if fs, err := parseFloat32_custom(bv); err != nil || fs != f32 {
4420 t.Logf("float32 -> float32 error (prec: %v) parsing '%s', got %v, expected %v: %v", prec, bv, fs, f32, err)
4421 t.FailNow()
4422 }
4423 }
4424 }
4425 }
4426
4427 for _, v := range testUintsToParse {
4428 const prec int = 32
4429 v = uint64(float64(v))
4430 bv := strconv.AppendFloat(nil, float64(v), 'E', prec, 64)
4431 r := readFloat(bv, fi64u)
4432 if r.ok {
4433 if fs, fail := parseUint64_reader(r); fail || fs != v {
4434 t.Logf("uint64 -> uint64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fs, v)
4435 t.FailNow()
4436 }
4437 } else {
4438 t.Logf("uint64 -> uint64 not ok (prec: %v) parsing '%s', expected %v", prec, bv, v)
4439 t.FailNow()
4440 }
4441 vi := int64(v)
4442 bv = strconv.AppendFloat(nil, float64(vi), 'E', prec, 64)
4443 r = readFloat(bv, fi64u)
4444 if r.ok {
4445 if fs, fail := parseInt64_reader(r); fail || fs != vi {
4446 t.Logf("int64 -> int64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fs, vi)
4447 t.FailNow()
4448 }
4449 } else {
4450 t.Logf("int64 -> int64 not ok (prec: %v) parsing '%s', expected %v", prec, bv, vi)
4451 t.FailNow()
4452 }
4453 }
4454 {
4455 var f64 float64 = 64.0
4456 var f32 float32 = 32.0
4457 var i int = 11
4458 var u64 uint64 = 128
4459
4460 for _, s := range []string{`""`, `null`} {
4461 b := []byte(s)
4462 testUnmarshalErr(&f64, b, h, t, "")
4463 testDeepEqualErr(f64, float64(0), t, "")
4464 testUnmarshalErr(&f32, b, h, t, "")
4465 testDeepEqualErr(f32, float32(0), t, "")
4466 testUnmarshalErr(&i, b, h, t, "")
4467 testDeepEqualErr(i, int(0), t, "")
4468 testUnmarshalErr(&u64, b, h, t, "")
4469 testDeepEqualErr(u64, uint64(0), t, "")
4470 testUnmarshalErr(&u64, b, h, t, "")
4471 testDeepEqualErr(u64, uint64(0), t, "")
4472 }
4473 }
4474 }
4475
4476 func doTestMsgpackDecodeMapAndExtSizeMismatch(t *testing.T, h Handle) {
4477 if !testRecoverPanicToErr {
4478 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
4479 }
4480 defer testSetup(t, &h)()
4481 fn := func(t *testing.T, b []byte, v interface{}) {
4482 if err := NewDecoderBytes(b, h).Decode(v); err != io.EOF && err != io.ErrUnexpectedEOF {
4483 t.Fatalf("expected EOF or ErrUnexpectedEOF, got %v", err)
4484 }
4485 }
4486
4487
4488 var b = []byte{0xdf, 0x10, 0xee, 0xee, 0xee, 0x1, 0xa1, 0x1}
4489 var m1 map[int]string
4490 var m2 map[int][]byte
4491 fn(t, b, &m1)
4492 fn(t, b, &m2)
4493
4494
4495 b = []byte{0xc9, 0x7f, 0xff, 0xff, 0xff, 0xda, 0x1}
4496 var a interface{}
4497 fn(t, b, &a)
4498
4499
4500
4501
4502 }
4503
4504 func TestMapRangeIndex(t *testing.T) {
4505 defer testSetup(t, nil)()
4506
4507 type T struct {
4508 I int
4509 S string
4510 B bool
4511 M map[int]T
4512 }
4513
4514 t1 := T{I: 1, B: true, S: "11", M: map[int]T{11: T{I: 11}}}
4515 t2 := T{I: 1, B: true, S: "12", M: map[int]T{12: T{I: 12}}}
4516
4517
4518
4519 var m1 = map[string]*T{
4520 "11": &t1,
4521 "12": &t2,
4522 }
4523 var m1c = make(map[string]T)
4524 for k, v := range m1 {
4525 m1c[k] = *v
4526 }
4527
4528 fnrv := func(r1, r2 reflect.Value) reflect.Value {
4529 if r1.IsValid() {
4530 return r1
4531 }
4532 return r2
4533 }
4534
4535
4536
4537 mt := reflect.TypeOf(m1)
4538 rvk := mapAddrLoopvarRV(mt.Key(), mt.Key().Kind())
4539 rvv := mapAddrLoopvarRV(mt.Elem(), mt.Elem().Kind())
4540 var it mapIter
4541 mapRange(&it, reflect.ValueOf(m1), rvk, rvv, true)
4542 for it.Next() {
4543 k := fnrv(it.Key(), rvk).Interface().(string)
4544 v := fnrv(it.Value(), rvv).Interface().(*T)
4545 testDeepEqualErr(m1[k], v, t, "map-key-eq-it-key")
4546 if _, ok := m1c[k]; ok {
4547 delete(m1c, k)
4548 } else {
4549 t.Logf("unexpected key in map: %v", k)
4550 t.FailNow()
4551 }
4552 }
4553 it.Done()
4554 testDeepEqualErr(len(m1c), 0, t, "all-keys-not-consumed")
4555
4556
4557
4558 var m2 = map[*T]T{
4559 &t1: t1,
4560 &t2: t2,
4561 }
4562 var m2c = make(map[*T]*T)
4563 for k := range m2 {
4564 m2c[k] = k
4565 }
4566
4567 mt = reflect.TypeOf(m2)
4568 rvk = mapAddrLoopvarRV(mt.Key(), mt.Key().Kind())
4569 rvv = mapAddrLoopvarRV(mt.Elem(), mt.Elem().Kind())
4570 it = mapIter{}
4571 mapRange(&it, reflect.ValueOf(m2), rvk, rvv, true)
4572 for it.Next() {
4573 k := fnrv(it.Key(), rvk).Interface().(*T)
4574 v := fnrv(it.Value(), rvv).Interface().(T)
4575 testDeepEqualErr(m2[k], v, t, "map-key-eq-it-key")
4576 if _, ok := m2c[k]; ok {
4577 delete(m2c, k)
4578 } else {
4579 t.Logf("unexpected key in map: %v", k)
4580 t.FailNow()
4581 }
4582 }
4583 it.Done()
4584 testDeepEqualErr(len(m2c), 0, t, "all-keys-not-consumed")
4585
4586
4587
4588 fnTestMapIndex := func(mi ...interface{}) {
4589 for _, m0 := range mi {
4590 m := reflect.ValueOf(m0)
4591 mkt := m.Type().Key()
4592 mvt := m.Type().Elem()
4593 kfast := mapKeyFastKindFor(mkt.Kind())
4594 rvv := mapAddrLoopvarRV(mvt, mvt.Kind())
4595 visindirect := mapStoresElemIndirect(mvt.Size())
4596 visref := refBitset.isset(byte(mvt.Kind()))
4597
4598 for _, k := range m.MapKeys() {
4599 mg := mapGet(m, k, rvv, kfast, visindirect, visref).Interface()
4600 testDeepEqualErr(m.MapIndex(k).Interface(), mg, t, "map-index-eq")
4601 }
4602 }
4603 }
4604
4605 fnTestMapIndex(m1, m1c, m2, m2c)
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622 }
4623
4624
4625
4626 func TestBincCodecsTable(t *testing.T) {
4627 doTestCodecTableOne(t, testBincH)
4628 }
4629
4630 func TestBincCodecsMisc(t *testing.T) {
4631 doTestCodecMiscOne(t, testBincH)
4632 }
4633
4634 func TestBincCodecsEmbeddedPointer(t *testing.T) {
4635 doTestCodecEmbeddedPointer(t, testBincH)
4636 }
4637
4638 func TestBincStdEncIntf(t *testing.T) {
4639 doTestStdEncIntf(t, testBincH)
4640 }
4641
4642 func TestSimpleCodecsTable(t *testing.T) {
4643 doTestCodecTableOne(t, testSimpleH)
4644 }
4645
4646 func TestSimpleCodecsMisc(t *testing.T) {
4647 doTestCodecMiscOne(t, testSimpleH)
4648 }
4649
4650 func TestSimpleCodecsEmbeddedPointer(t *testing.T) {
4651 doTestCodecEmbeddedPointer(t, testSimpleH)
4652 }
4653
4654 func TestSimpleStdEncIntf(t *testing.T) {
4655 doTestStdEncIntf(t, testSimpleH)
4656 }
4657
4658 func TestMsgpackCodecsTable(t *testing.T) {
4659 doTestCodecTableOne(t, testMsgpackH)
4660 }
4661
4662 func TestMsgpackCodecsMisc(t *testing.T) {
4663 doTestCodecMiscOne(t, testMsgpackH)
4664 }
4665
4666 func TestMsgpackCodecsEmbeddedPointer(t *testing.T) {
4667 doTestCodecEmbeddedPointer(t, testMsgpackH)
4668 }
4669
4670 func TestMsgpackStdEncIntf(t *testing.T) {
4671 doTestStdEncIntf(t, testMsgpackH)
4672 }
4673
4674 func TestCborCodecsTable(t *testing.T) {
4675 doTestCodecTableOne(t, testCborH)
4676 }
4677
4678 func TestCborCodecsMisc(t *testing.T) {
4679 doTestCodecMiscOne(t, testCborH)
4680 }
4681
4682 func TestCborCodecsEmbeddedPointer(t *testing.T) {
4683 doTestCodecEmbeddedPointer(t, testCborH)
4684 }
4685
4686 func TestCborCodecChan(t *testing.T) {
4687 doTestCodecChan(t, testCborH)
4688 }
4689
4690 func TestCborStdEncIntf(t *testing.T) {
4691 doTestStdEncIntf(t, testCborH)
4692 }
4693
4694 func TestJsonCodecsTable(t *testing.T) {
4695 doTestCodecTableOne(t, testJsonH)
4696 }
4697
4698 func TestJsonCodecsMisc(t *testing.T) {
4699 doTestCodecMiscOne(t, testJsonH)
4700 }
4701
4702 func TestJsonCodecsEmbeddedPointer(t *testing.T) {
4703 doTestCodecEmbeddedPointer(t, testJsonH)
4704 }
4705
4706 func TestJsonCodecChan(t *testing.T) {
4707 doTestCodecChan(t, testJsonH)
4708 }
4709
4710 func TestJsonStdEncIntf(t *testing.T) {
4711 doTestStdEncIntf(t, testJsonH)
4712 }
4713
4714
4715 func TestJsonRaw(t *testing.T) {
4716 doTestRawValue(t, testJsonH)
4717 }
4718 func TestBincRaw(t *testing.T) {
4719 doTestRawValue(t, testBincH)
4720 }
4721 func TestMsgpackRaw(t *testing.T) {
4722 doTestRawValue(t, testMsgpackH)
4723 }
4724 func TestSimpleRaw(t *testing.T) {
4725 doTestRawValue(t, testSimpleH)
4726 }
4727 func TestCborRaw(t *testing.T) {
4728 doTestRawValue(t, testCborH)
4729 }
4730
4731
4732
4733 func TestAllEncCircularRef(t *testing.T) {
4734 doTestEncCircularRef(t, testCborH)
4735 }
4736
4737 func TestAllAnonCycle(t *testing.T) {
4738 doTestAnonCycle(t, testCborH)
4739 }
4740
4741 func TestAllErrWriter(t *testing.T) {
4742 doTestAllErrWriter(t, testCborH, testJsonH)
4743 }
4744
4745
4746
4747 func TestMsgpackRpcSpec(t *testing.T) {
4748 doTestCodecRpcOne(t, MsgpackSpecRpc, testMsgpackH, true, 0)
4749 }
4750
4751
4752
4753 func TestBincRpcGo(t *testing.T) {
4754 doTestCodecRpcOne(t, GoRpc, testBincH, true, 0)
4755 }
4756
4757 func TestSimpleRpcGo(t *testing.T) {
4758 doTestCodecRpcOne(t, GoRpc, testSimpleH, true, 0)
4759 }
4760
4761 func TestMsgpackRpcGo(t *testing.T) {
4762 doTestCodecRpcOne(t, GoRpc, testMsgpackH, true, 0)
4763 }
4764
4765 func TestCborRpcGo(t *testing.T) {
4766 doTestCodecRpcOne(t, GoRpc, testCborH, true, 0)
4767 }
4768
4769 func TestJsonRpcGo(t *testing.T) {
4770 doTestCodecRpcOne(t, GoRpc, testJsonH, true, 0)
4771 }
4772
4773
4774
4775 func TestBincMapEncodeForCanonical(t *testing.T) {
4776 t.Skipf("skipping ... needs investigation")
4777 doTestMapEncodeForCanonical(t, testBincH)
4778 }
4779
4780 func TestSimpleMapEncodeForCanonical(t *testing.T) {
4781 doTestMapEncodeForCanonical(t, testSimpleH)
4782 }
4783
4784 func TestMsgpackMapEncodeForCanonical(t *testing.T) {
4785 doTestMapEncodeForCanonical(t, testMsgpackH)
4786 }
4787
4788 func TestCborMapEncodeForCanonical(t *testing.T) {
4789 doTestMapEncodeForCanonical(t, testCborH)
4790 }
4791
4792 func TestJsonMapEncodeForCanonical(t *testing.T) {
4793 doTestMapEncodeForCanonical(t, testJsonH)
4794 }
4795
4796 func TestBincUnderlyingType(t *testing.T) {
4797 testCodecUnderlyingType(t, testBincH)
4798 }
4799
4800 func TestJsonSwallowAndZero(t *testing.T) {
4801 doTestSwallowAndZero(t, testJsonH)
4802 }
4803
4804 func TestCborSwallowAndZero(t *testing.T) {
4805 doTestSwallowAndZero(t, testCborH)
4806 }
4807
4808 func TestMsgpackSwallowAndZero(t *testing.T) {
4809 doTestSwallowAndZero(t, testMsgpackH)
4810 }
4811
4812 func TestBincSwallowAndZero(t *testing.T) {
4813 doTestSwallowAndZero(t, testBincH)
4814 }
4815
4816 func TestSimpleSwallowAndZero(t *testing.T) {
4817 doTestSwallowAndZero(t, testSimpleH)
4818 }
4819
4820 func TestJsonRawExt(t *testing.T) {
4821 doTestRawExt(t, testJsonH)
4822 }
4823
4824 func TestCborRawExt(t *testing.T) {
4825 doTestRawExt(t, testCborH)
4826 }
4827
4828 func TestMsgpackRawExt(t *testing.T) {
4829 doTestRawExt(t, testMsgpackH)
4830 }
4831
4832 func TestBincRawExt(t *testing.T) {
4833 doTestRawExt(t, testBincH)
4834 }
4835
4836 func TestSimpleRawExt(t *testing.T) {
4837 doTestRawExt(t, testSimpleH)
4838 }
4839
4840 func TestJsonMapStructKey(t *testing.T) {
4841 doTestMapStructKey(t, testJsonH)
4842 }
4843
4844 func TestCborMapStructKey(t *testing.T) {
4845 doTestMapStructKey(t, testCborH)
4846 }
4847
4848 func TestMsgpackMapStructKey(t *testing.T) {
4849 doTestMapStructKey(t, testMsgpackH)
4850 }
4851
4852 func TestBincMapStructKey(t *testing.T) {
4853 doTestMapStructKey(t, testBincH)
4854 }
4855
4856 func TestSimpleMapStructKey(t *testing.T) {
4857 doTestMapStructKey(t, testSimpleH)
4858 }
4859
4860 func TestJsonDecodeNilMapValue(t *testing.T) {
4861 doTestDecodeNilMapValue(t, testJsonH)
4862 }
4863
4864 func TestCborDecodeNilMapValue(t *testing.T) {
4865 doTestDecodeNilMapValue(t, testCborH)
4866 }
4867
4868 func TestMsgpackDecodeNilMapValue(t *testing.T) {
4869 doTestDecodeNilMapValue(t, testMsgpackH)
4870 }
4871
4872 func TestBincDecodeNilMapValue(t *testing.T) {
4873 doTestDecodeNilMapValue(t, testBincH)
4874 }
4875
4876 func TestSimpleDecodeNilMapValue(t *testing.T) {
4877 doTestDecodeNilMapValue(t, testSimpleH)
4878 }
4879
4880 func TestJsonEmbeddedFieldPrecedence(t *testing.T) {
4881 doTestEmbeddedFieldPrecedence(t, testJsonH)
4882 }
4883
4884 func TestCborEmbeddedFieldPrecedence(t *testing.T) {
4885 doTestEmbeddedFieldPrecedence(t, testCborH)
4886 }
4887
4888 func TestMsgpackEmbeddedFieldPrecedence(t *testing.T) {
4889 doTestEmbeddedFieldPrecedence(t, testMsgpackH)
4890 }
4891
4892 func TestBincEmbeddedFieldPrecedence(t *testing.T) {
4893 doTestEmbeddedFieldPrecedence(t, testBincH)
4894 }
4895
4896 func TestSimpleEmbeddedFieldPrecedence(t *testing.T) {
4897 doTestEmbeddedFieldPrecedence(t, testSimpleH)
4898 }
4899
4900 func TestJsonLargeContainerLen(t *testing.T) {
4901 doTestLargeContainerLen(t, testJsonH)
4902 }
4903
4904 func TestCborLargeContainerLen(t *testing.T) {
4905 doTestLargeContainerLen(t, testCborH)
4906 }
4907
4908 func TestMsgpackLargeContainerLen(t *testing.T) {
4909 doTestLargeContainerLen(t, testMsgpackH)
4910 }
4911
4912 func TestBincLargeContainerLen(t *testing.T) {
4913 doTestLargeContainerLen(t, testBincH)
4914 }
4915
4916 func TestSimpleLargeContainerLen(t *testing.T) {
4917 doTestLargeContainerLen(t, testSimpleH)
4918 }
4919
4920 func TestJsonTime(t *testing.T) {
4921 doTestTime(t, testJsonH)
4922 }
4923
4924 func TestCborTime(t *testing.T) {
4925 doTestTime(t, testCborH)
4926 }
4927
4928 func TestMsgpackTime(t *testing.T) {
4929 doTestTime(t, testMsgpackH)
4930 }
4931
4932 func TestBincTime(t *testing.T) {
4933 doTestTime(t, testBincH)
4934 }
4935
4936 func TestSimpleTime(t *testing.T) {
4937 doTestTime(t, testSimpleH)
4938 }
4939
4940 func TestJsonUintToInt(t *testing.T) {
4941 doTestUintToInt(t, testJsonH)
4942 }
4943
4944 func TestCborUintToInt(t *testing.T) {
4945 doTestUintToInt(t, testCborH)
4946 }
4947
4948 func TestMsgpackUintToInt(t *testing.T) {
4949 doTestUintToInt(t, testMsgpackH)
4950 }
4951
4952 func TestBincUintToInt(t *testing.T) {
4953 doTestUintToInt(t, testBincH)
4954 }
4955
4956 func TestSimpleUintToInt(t *testing.T) {
4957 doTestUintToInt(t, testSimpleH)
4958 }
4959
4960 func TestJsonDifferentMapOrSliceType(t *testing.T) {
4961 doTestDifferentMapOrSliceType(t, testJsonH)
4962 }
4963
4964 func TestCborDifferentMapOrSliceType(t *testing.T) {
4965 doTestDifferentMapOrSliceType(t, testCborH)
4966 }
4967
4968 func TestMsgpackDifferentMapOrSliceType(t *testing.T) {
4969 doTestDifferentMapOrSliceType(t, testMsgpackH)
4970 }
4971
4972 func TestBincDifferentMapOrSliceType(t *testing.T) {
4973 doTestDifferentMapOrSliceType(t, testBincH)
4974 }
4975
4976 func TestSimpleDifferentMapOrSliceType(t *testing.T) {
4977 doTestDifferentMapOrSliceType(t, testSimpleH)
4978 }
4979
4980 func TestJsonScalars(t *testing.T) {
4981 doTestScalars(t, testJsonH)
4982 }
4983
4984 func TestCborScalars(t *testing.T) {
4985 doTestScalars(t, testCborH)
4986 }
4987
4988 func TestMsgpackScalars(t *testing.T) {
4989 doTestScalars(t, testMsgpackH)
4990 }
4991
4992 func TestBincScalars(t *testing.T) {
4993 doTestScalars(t, testBincH)
4994 }
4995
4996 func TestSimpleScalars(t *testing.T) {
4997 doTestScalars(t, testSimpleH)
4998 }
4999
5000 func TestJsonOmitempty(t *testing.T) {
5001 doTestOmitempty(t, testJsonH)
5002 }
5003
5004 func TestCborOmitempty(t *testing.T) {
5005 doTestOmitempty(t, testCborH)
5006 }
5007
5008 func TestMsgpackOmitempty(t *testing.T) {
5009 doTestOmitempty(t, testMsgpackH)
5010 }
5011
5012 func TestBincOmitempty(t *testing.T) {
5013 doTestOmitempty(t, testBincH)
5014 }
5015
5016 func TestSimpleOmitempty(t *testing.T) {
5017 doTestOmitempty(t, testSimpleH)
5018 }
5019
5020 func TestJsonIntfMapping(t *testing.T) {
5021 doTestIntfMapping(t, testJsonH)
5022 }
5023
5024 func TestCborIntfMapping(t *testing.T) {
5025 doTestIntfMapping(t, testCborH)
5026 }
5027
5028 func TestMsgpackIntfMapping(t *testing.T) {
5029 doTestIntfMapping(t, testMsgpackH)
5030 }
5031
5032 func TestBincIntfMapping(t *testing.T) {
5033 doTestIntfMapping(t, testBincH)
5034 }
5035
5036 func TestSimpleIntfMapping(t *testing.T) {
5037 doTestIntfMapping(t, testSimpleH)
5038 }
5039
5040 func TestJsonMissingFields(t *testing.T) {
5041 doTestMissingFields(t, testJsonH)
5042 }
5043
5044 func TestCborMissingFields(t *testing.T) {
5045 doTestMissingFields(t, testCborH)
5046 }
5047
5048 func TestMsgpackMissingFields(t *testing.T) {
5049 doTestMissingFields(t, testMsgpackH)
5050 }
5051
5052 func TestBincMissingFields(t *testing.T) {
5053 doTestMissingFields(t, testBincH)
5054 }
5055
5056 func TestSimpleMissingFields(t *testing.T) {
5057 doTestMissingFields(t, testSimpleH)
5058 }
5059
5060 func TestJsonMaxDepth(t *testing.T) {
5061 doTestMaxDepth(t, testJsonH)
5062 }
5063
5064 func TestCborMaxDepth(t *testing.T) {
5065 doTestMaxDepth(t, testCborH)
5066 }
5067
5068 func TestMsgpackMaxDepth(t *testing.T) {
5069 doTestMaxDepth(t, testMsgpackH)
5070 }
5071
5072 func TestBincMaxDepth(t *testing.T) {
5073 doTestMaxDepth(t, testBincH)
5074 }
5075
5076 func TestSimpleMaxDepth(t *testing.T) {
5077 doTestMaxDepth(t, testSimpleH)
5078 }
5079
5080 func TestJsonSelfExt(t *testing.T) {
5081 doTestSelfExt(t, testJsonH)
5082 }
5083
5084 func TestCborSelfExt(t *testing.T) {
5085 doTestSelfExt(t, testCborH)
5086 }
5087
5088 func TestMsgpackSelfExt(t *testing.T) {
5089 doTestSelfExt(t, testMsgpackH)
5090 }
5091
5092 func TestBincSelfExt(t *testing.T) {
5093 doTestSelfExt(t, testBincH)
5094 }
5095
5096 func TestSimpleSelfExt(t *testing.T) {
5097 doTestSelfExt(t, testSimpleH)
5098 }
5099
5100 func TestJsonBytesEncodedAsArray(t *testing.T) {
5101 doTestBytesEncodedAsArray(t, testJsonH)
5102 }
5103
5104 func TestCborBytesEncodedAsArray(t *testing.T) {
5105 doTestBytesEncodedAsArray(t, testCborH)
5106 }
5107
5108 func TestMsgpackBytesEncodedAsArray(t *testing.T) {
5109 doTestBytesEncodedAsArray(t, testMsgpackH)
5110 }
5111
5112 func TestBincBytesEncodedAsArray(t *testing.T) {
5113 doTestBytesEncodedAsArray(t, testBincH)
5114 }
5115
5116 func TestSimpleBytesEncodedAsArray(t *testing.T) {
5117 doTestBytesEncodedAsArray(t, testSimpleH)
5118 }
5119
5120 func TestJsonStrucEncDec(t *testing.T) {
5121 doTestStrucEncDec(t, testJsonH)
5122 }
5123
5124 func TestCborStrucEncDec(t *testing.T) {
5125 doTestStrucEncDec(t, testCborH)
5126 }
5127
5128 func TestMsgpackStrucEncDec(t *testing.T) {
5129 doTestStrucEncDec(t, testMsgpackH)
5130 }
5131
5132 func TestBincStrucEncDec(t *testing.T) {
5133 doTestStrucEncDec(t, testBincH)
5134 }
5135
5136 func TestSimpleStrucEncDec(t *testing.T) {
5137 doTestStrucEncDec(t, testSimpleH)
5138 }
5139
5140 func TestJsonRawToStringToRawEtc(t *testing.T) {
5141 doTestRawToStringToRawEtc(t, testJsonH)
5142 }
5143
5144 func TestCborRawToStringToRawEtc(t *testing.T) {
5145 doTestRawToStringToRawEtc(t, testCborH)
5146 }
5147
5148 func TestMsgpackRawToStringToRawEtc(t *testing.T) {
5149 doTestRawToStringToRawEtc(t, testMsgpackH)
5150 }
5151
5152 func TestBincRawToStringToRawEtc(t *testing.T) {
5153 doTestRawToStringToRawEtc(t, testBincH)
5154 }
5155
5156 func TestSimpleRawToStringToRawEtc(t *testing.T) {
5157 doTestRawToStringToRawEtc(t, testSimpleH)
5158 }
5159
5160 func TestJsonStructKeyType(t *testing.T) {
5161 doTestStructKeyType(t, testJsonH)
5162 }
5163
5164 func TestCborStructKeyType(t *testing.T) {
5165 doTestStructKeyType(t, testCborH)
5166 }
5167
5168 func TestMsgpackStructKeyType(t *testing.T) {
5169 doTestStructKeyType(t, testMsgpackH)
5170 }
5171
5172 func TestBincStructKeyType(t *testing.T) {
5173 doTestStructKeyType(t, testBincH)
5174 }
5175
5176 func TestSimpleStructKeyType(t *testing.T) {
5177 doTestStructKeyType(t, testSimpleH)
5178 }
5179
5180 func TestJsonPreferArrayOverSlice(t *testing.T) {
5181 doTestPreferArrayOverSlice(t, testJsonH)
5182 }
5183
5184 func TestCborPreferArrayOverSlice(t *testing.T) {
5185 doTestPreferArrayOverSlice(t, testCborH)
5186 }
5187
5188 func TestMsgpackPreferArrayOverSlice(t *testing.T) {
5189 doTestPreferArrayOverSlice(t, testMsgpackH)
5190 }
5191
5192 func TestBincPreferArrayOverSlice(t *testing.T) {
5193 doTestPreferArrayOverSlice(t, testBincH)
5194 }
5195
5196 func TestSimplePreferArrayOverSlice(t *testing.T) {
5197 doTestPreferArrayOverSlice(t, testSimpleH)
5198 }
5199
5200 func TestJsonZeroCopyBytes(t *testing.T) {
5201 doTestZeroCopyBytes(t, testJsonH)
5202 }
5203
5204 func TestCborZeroCopyBytes(t *testing.T) {
5205 doTestZeroCopyBytes(t, testCborH)
5206 }
5207
5208 func TestMsgpackZeroCopyBytes(t *testing.T) {
5209 doTestZeroCopyBytes(t, testMsgpackH)
5210 }
5211
5212 func TestBincZeroCopyBytes(t *testing.T) {
5213 doTestZeroCopyBytes(t, testBincH)
5214 }
5215
5216 func TestSimpleZeroCopyBytes(t *testing.T) {
5217 doTestZeroCopyBytes(t, testSimpleH)
5218 }
5219
5220 func TestJsonNextValueBytes(t *testing.T) {
5221 doTestNextValueBytes(t, testJsonH)
5222 }
5223
5224 func TestCborNextValueBytes(t *testing.T) {
5225
5226
5227
5228
5229
5230
5231
5232
5233 doTestNextValueBytes(t, testCborH)
5234 }
5235
5236 func TestMsgpackNextValueBytes(t *testing.T) {
5237 doTestNextValueBytes(t, testMsgpackH)
5238 }
5239
5240 func TestBincNextValueBytes(t *testing.T) {
5241 doTestNextValueBytes(t, testBincH)
5242 }
5243
5244 func TestSimpleNextValueBytes(t *testing.T) {
5245 doTestNextValueBytes(t, testSimpleH)
5246 }
5247
5248 func TestJsonNumbers(t *testing.T) {
5249 doTestNumbers(t, testJsonH)
5250 }
5251
5252 func TestCborNumbers(t *testing.T) {
5253 doTestNumbers(t, testCborH)
5254 }
5255
5256 func TestMsgpackNumbers(t *testing.T) {
5257 doTestNumbers(t, testMsgpackH)
5258 }
5259
5260 func TestBincNumbers(t *testing.T) {
5261 doTestNumbers(t, testBincH)
5262 }
5263
5264 func TestSimpleNumbers(t *testing.T) {
5265 doTestNumbers(t, testSimpleH)
5266 }
5267
5268 func TestJsonDesc(t *testing.T) {
5269 doTestDesc(t, testJsonH, map[byte]string{'"': `"`, '{': `{`, '}': `}`, '[': `[`, ']': `]`})
5270 }
5271
5272 func TestCborDesc(t *testing.T) {
5273 m := make(map[byte]string)
5274 for k, v := range cbordescMajorNames {
5275
5276 m[k<<5] = v
5277 }
5278 for k, v := range cbordescSimpleNames {
5279 m[k] = v
5280 }
5281 delete(m, cborMajorSimpleOrFloat<<5)
5282 doTestDesc(t, testCborH, m)
5283 }
5284
5285 func TestMsgpackDesc(t *testing.T) {
5286 m := make(map[byte]string)
5287 for k, v := range mpdescNames {
5288 m[k] = v
5289 }
5290 m[mpPosFixNumMin] = "int"
5291 m[mpFixStrMin] = "string|bytes"
5292 m[mpFixArrayMin] = "array"
5293 m[mpFixMapMin] = "map"
5294 m[mpFixExt1] = "ext"
5295
5296 doTestDesc(t, testMsgpackH, m)
5297 }
5298
5299 func TestBincDesc(t *testing.T) {
5300 m := make(map[byte]string)
5301 for k, v := range bincdescVdNames {
5302 m[k<<4] = v
5303 }
5304 for k, v := range bincdescSpecialVsNames {
5305 m[k] = v
5306 }
5307 delete(m, bincVdSpecial<<4)
5308 doTestDesc(t, testBincH, m)
5309 }
5310
5311 func TestSimpleDesc(t *testing.T) {
5312 doTestDesc(t, testSimpleH, simpledescNames)
5313 }
5314
5315 func TestJsonStructFieldInfoToArray(t *testing.T) {
5316 doTestStructFieldInfoToArray(t, testJsonH)
5317 }
5318
5319 func TestCborStructFieldInfoToArray(t *testing.T) {
5320 doTestStructFieldInfoToArray(t, testCborH)
5321 }
5322
5323 func TestMsgpackStructFieldInfoToArray(t *testing.T) {
5324 doTestStructFieldInfoToArray(t, testMsgpackH)
5325 }
5326
5327 func TestBincStructFieldInfoToArray(t *testing.T) {
5328 doTestStructFieldInfoToArray(t, testBincH)
5329 }
5330
5331 func TestSimpleStructFieldInfoToArray(t *testing.T) {
5332 doTestStructFieldInfoToArray(t, testSimpleH)
5333 }
5334
5335
5336
5337 func TestMultipleEncDec(t *testing.T) {
5338 doTestMultipleEncDec(t, testJsonH)
5339 }
5340
5341 func TestJsonEncodeIndent(t *testing.T) {
5342 doTestJsonEncodeIndent(t, testJsonH)
5343 }
5344
5345 func TestJsonDecodeNonStringScalarInStringContext(t *testing.T) {
5346 doTestJsonDecodeNonStringScalarInStringContext(t, testJsonH)
5347 }
5348
5349 func TestJsonLargeInteger(t *testing.T) {
5350 doTestJsonLargeInteger(t, testJsonH)
5351 }
5352
5353 func TestJsonInvalidUnicode(t *testing.T) {
5354 doTestJsonInvalidUnicode(t, testJsonH)
5355 }
5356
5357 func TestJsonNumberParsing(t *testing.T) {
5358 doTestJsonNumberParsing(t, testJsonH)
5359 }
5360
5361 func TestMsgpackDecodeMapAndExtSizeMismatch(t *testing.T) {
5362 doTestMsgpackDecodeMapAndExtSizeMismatch(t, testMsgpackH)
5363 }
5364
View as plain text