Debian Codegraph @

codec/codec_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
2 // Use of this source code is governed by a MIT license found in the LICENSE file.
3
4 package codec
5
6 // NAMING CONVENTION FOR TESTS
7 //
8 // function and variable/const names here fit a simple naming convention
9 //
10 // - each test is a doTestXXX(...). TestXXX calls doTestXXX.
11 // - testXXX are helper functions.
12 // - doTestXXX are test functions that take an extra arg of a handle.
13 // - testXXX variables and constants are only used in tests.
14 // - shared functions/vars/consts are testShared...
15 // - fnBenchmarkXX and fnTestXXX can be used as needed.
16 //
17 // - each TestXXX must only call testSetup once.
18 // - each test with a prefix __doTest is a dependent helper function,
19 // which MUST not call testSetup itself.
20 // doTestXXX or TestXXX may call it.
21
22 // if we are testing in parallel,
23 // then we don't want to share much state: testBytesFreeList, etc.
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 // tests which check for errors will fail if testRecoverPanicToErr=false (debugging=true).
55 // Consequently, skip them.
56 var testSkipIfNotRecoverPanicToErrMsg = "tests checks for errors, and testRecoverPanicToErr=false"
57
58 func testPanicToErr(h errDecorator, err *error) {
59 // Note: This method MUST be called directly from defer i.e. defer testPanicToErr ...
60 // else it seems the recover is not fully handled
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 // make these mapbyslice
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 // type testSelferRecur struct{}
84
85 // func (s *testSelferRecur) CodecEncodeSelf(e *Encoder) {
86 // e.MustEncode(s)
87 // }
88 // func (s *testSelferRecur) CodecDecodeSelf(d *Decoder) {
89 // d.MustDecode(s)
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 // testVerifySliceIntf
146 testVerifyForPython
147 testVerifyDoNil
148 testVerifyTimeAsInteger
149 )
150
151 func (f testVerifyFlag) isset(v testVerifyFlag) bool {
152 return f&v == v
153 }
154
155 // const testSkipRPCTests = false
156
157 var (
158 testTableNumPrimitives int
159 testTableIdxTime int
160 testTableNumMaps int
161
162 // set this when running using bufio, etc
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 // For Go Time, do not use a descriptive timezone.
174 // It's unnecessary, and makes it harder to do a reflect.DeepEqual.
175 // The Offset already tells what the offset should be, if not on UTC and unknown zone name.
176 timeLoc = time.FixedZone("", -8*60*60) // UTC-08:00 //time.UTC-8
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() // use value that must be encoded as uint64 for nanoseconds (for cbor/msgpack comparison)
180 //timeToCompare4 = time.Time{}.UTC() // does not work well with simple cbor time encoding (overflow)
181 timeToCompare4 = time.Unix(-2013855848, 4223).UTC()
182
183 table []interface{} // main items we encode
184 // will encode a float32 as float64, or large int as uint
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 // _, err = fmt.Sscanf(string(data), "%s %s %s", &x.A, &x.B, &x.C)
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 // keep timestamp here, so that do not incur interface-conversion costs
293 // ts int64
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) // structs are encoded by passing the ptr
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 // switch v2 := v.(type) {
319 // case int64:
320 // *tt = time.Unix(0, v2).UTC()
321 // case uint64:
322 // *tt = time.Unix(0, int64(v2)).UTC()
323 // //case float64:
324 // //case string:
325 // default:
326 // panic(fmt.Errorf("unsupported format for time conversion: expecting int64/uint64; got %T", v))
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 // some formats (e.g. json) cannot nakedly determine []byte from string, so expect both
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 // *v2 = wrapBytes(v.([]byte))
379 }
380
381 // ----
382
383 // timeExt is an extension handler for time.Time, that uses binc model for encoding/decoding time.
384 // we used binc model, as that is the only custom time representation that we designed ourselves.
385 type timeBytesExt struct{}
386
387 func (x timeBytesExt) WriteExt(v interface{}) (bs []byte) {
388 return bincEncodeTime(*(v.(*time.Time)))
389 // return bincEncodeTime(v.(time.Time))
390 // switch v2 := v.(type) {
391 // case time.Time:
392 // bs = bincEncodeTime(v2)
393 // case *time.Time:
394 // bs = bincEncodeTime(*v2)
395 // default:
396 // panic(fmt.Errorf("unsupported format for time conversion: expecting time.Time; got %T", v2))
397 // }
398 // return
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 // testSetup will ensure testInitAll is run, and then
446 // return a function that should be deferred to run at the end
447 // of the test.
448 //
449 // This function can track how much time a test took,
450 // or recover from panic's and fail the test appropriately.
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 // in case an error is seen, recover it here.
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() // so it is reinitialized next time around
511 // pre-fill them first
512 bh.EncodeOptions = testEncodeOptions
513 bh.DecodeOptions = testDecodeOptions
514 bh.RPCOptions = testRPCOptions
515 // bh.InterfaceReset = true
516 // bh.PreferArrayOverSlice = true
517 // modify from flag'ish things
518 bh.MaxInitLen = testMaxInitLen
519 }
520
521 var tTimeExt timeBytesExt
522 var tBytesExt wrapBytesExt
523 var tI64Ext wrapInt64Ext
524 var tUintToBytesExt testUintToBytesExt
525
526 // create legacy functions suitable for deprecated AddExt functionality,
527 // and use on some places for testSimpleH e.g. for time.Time and wrapInt64
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 // time.Time is a native type, so extensions will have no effect.
552 // However, we add these here to ensure nothing happens.
553 chkErr(testSimpleH.AddExt(timeTyp, 1, timeExtEncFn, timeExtDecFn))
554 // testBincH.SetBytesExt(timeTyp, 1, timeExt{}) // time is builtin for binc
555 chkErr(testMsgpackH.SetBytesExt(timeTyp, 1, timeBytesExt{}))
556 chkErr(testCborH.SetInterfaceExt(timeTyp, 1, testUnixNanoTimeInterfaceExt{}))
557 // testJsonH.SetInterfaceExt(timeTyp, 1, &testUnixNanoTimeExt{})
558
559 // Add extensions for the testSelfExt
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 // Now, add extensions for the type wrapInt64 and wrapBytes,
573 // so we can execute the Encode/Decode Ext paths.
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 // chkErr(testSimpleH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
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 // primitives MUST be an even number, so it can be used as a mapBySlice also.
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 //add a complex combo map in here. (map has list which has map)
637 //note that after the first thing, everything else should be generic.
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 // table = append(table, newTestStrucFlex(0, testNumRepeatString, true, !testSkipIntf, testMapStringKeyOnly))
686 }
687
688 func testTableVerify(f testVerifyFlag, h Handle) (av []interface{}) {
689 av = make([]interface{}, len(table))
690 lp := testTableNumPrimitives + 4
691 // doNil := f & testVerifyDoNil == testVerifyDoNil
692 // doPython := f & testVerifyForPython == testVerifyForPython
693 switch {
694 case f.isset(testVerifyForPython):
695 for i, v := range table {
696 if i == testTableNumPrimitives+1 || i > lp { // testTableNumPrimitives+1 is the mapBySlice
697 av[i] = skipVerifyVal
698 continue
699 }
700 av[i] = testVerifyVal(v, f, h)
701 }
702 // only do the python verify up to the maps, skipping the last 2 maps.
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 //av[i] = testVerifyVal(v, testVerifyMapTypeSame)
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 //for python msgpack,
755 // - all positive integers are unsigned 64-bit ints
756 // - all floats are float64
757 _, isMsgp := h.(*MsgpackHandle)
758 _, isCbor := h.(*CborHandle)
759 switch iv := v.(type) {
760 case int8:
761 v2 = testVerifyValInt(int64(iv), isMsgp)
762 // fmt.Printf(">>>> is msgp: %v, v: %T, %v ==> v2: %T, %v\n", isMsgp, v, v, v2, v2)
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 // fmt.Printf("%%%% cbor verifier\n")
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 // return testCodecEncode(v, nil, testByteBuf, h)
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 // testCodecTableOne allows us test for different variations based on arguments passed.
962 func testCodecTableOne(t *testing.T, testNil bool, h Handle,
963 vs []interface{}, vsVerify []interface{}) {
964 //if testNil, then just test for when a pointer to a nil interface{} is passed. It should work.
965 //Current setup allows us test (at least manually) the nil interface or typed interface.
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 // if a TestStrucFlex and we are doing a testNil,
986 // ensure the fields which are not encodeable are set to nil appropriately
987 // i.e. TestStrucFlex.{MstrUi64TSelf, mapMsu2wss}
988 var mapMstrUi64TSelf map[stringUint64T]*stringUint64T
989 var mapMsu2wss map[stringUint64T]wrapStringSlice
990 // TestStrucFlex.{Msp2ss, Mip2ss} have pointer keys.
991 // When we encode and the decode back into the same value,
992 // the length of this map will effectively double, because
993 // each pointer has equal underlying value, but are separate entries in the map.
994 //
995 // Best way to compare is to store them, and then compare them later if needed.
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 // TestStrucFlex has many fields which will encode differently if SignedInteger - so skip
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) // ptr
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 // v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
1054 }
1055 }
1056
1057 if tsflex != nil {
1058 // MARKER: consider compare tsflex.{Msp2ss, Mip2ss} to map{Msp2ss, Mip2ss}
1059 _, _ = mapMsp2ss, mapMip2ss
1060 }
1061
1062 if testVerbose {
1063 t.Logf(" v1 returned: %T, %v %#v", v1, v1, v1)
1064 }
1065 // if v1 != nil {
1066 // t.Logf(" v1 returned: %T, %#v", v1, v1)
1067 // //we always indirect, because ptr to typed value may be passed (if not testNil)
1068 // v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
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 // t.Logf("-------- Before and After marshal do not match: Error: %v"+
1090 // " ====> GOLDEN: (%T) %#v, DECODED: (%T) %#v\n", err, v0check, v0check, v1, v1)
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 // since this test modifies maps (and slices?), it should not be run in parallel,
1104 // else we may get "concurrent modification/range/set" errors.
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 //skip []interface{} containing time.Time, as it encodes as a number, but cannot decode back to time.Time.
1120 //As there is no real support for extension tags in json, this must be skipped.
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 // func TestMsgpackAll(t *testing.T) {
1127
1128 // //skip []interface{} containing time.Time
1129 // testCodecTableOne(t, false, h, table[:numPrim], tableVerify[:numPrim])
1130 // testCodecTableOne(t, false, h, table[numPrim+1:], tableVerify[numPrim+1:])
1131 // func TestMsgpackNilStringMap(t *testing.T) {
1132 var oldMapType reflect.Type
1133 v := testBasicHandle(h)
1134
1135 oldMapType, v.MapType = v.MapType, testMapStrIntfTyp
1136 // defer func() { v.MapType = oldMapType }()
1137 //skip time.Time, []interface{} containing time.Time, last map, and newStruc
1138 testCodecTableOne(t, true, h, table[:idxTime], tableTestNilVerify[:idxTime])
1139 testCodecTableOne(t, true, h, table[idxMap:idxMap+numMap-1], tableTestNilVerify[idxMap:idxMap+numMap-1]) // failing one for msgpack
1140 v.MapType = oldMapType
1141 // func TestMsgpackNilIntf(t *testing.T) {
1142
1143 //do last map and newStruc
1144 idx2 := idxMap + numMap - 1
1145 testCodecTableOne(t, true, h, table[idx2:], tableTestNilVerify[idx2:])
1146 //testCodecTableOne(t, true, h, table[17:18], tableTestNilVerify[17:18]) // do we need this?
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 // Cannot do this nil one, because faster type assertion decoding will panic
1155 // var i *int32
1156 // if err = testUnmarshal(b, i, nil); err == nil {
1157 // t.Logf("------- Expecting error because we cannot unmarshal to int32 nil ptr")
1158 // t.FailNow()
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 // func TestMsgpackDecodePtr(t *testing.T) {
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 // ts2 := testEmptyTestStrucFlex()
1188 var ts2 = new(TestStrucFlex)
1189 // we initialize and start draining the chan, so that we can decode into it without it blocking due to no consumer
1190 ts2.Chstr = make(chan string, teststrucflexChanCap)
1191 go func() {
1192 for range ts2.Chstr {
1193 }
1194 }() // drain it
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 // Note: These will not work with SliceElementReset or InterfaceReset=true, so handle that.
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 // func TestMsgpackDecodeStructSubset(t *testing.T) {
1247 // test that we can decode a subset of the stream
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 // println(">>>>>")
1261 // test simple arrays, non-addressable arrays, slices
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 // test both pointer and non-pointer (value)
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 // test byte array, even if empty (msgpack only)
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 // test encoding a slice of byte (but not []byte) and decoding into a []byte
1300 var sw = []wrapUint8{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'}
1301 var bw []byte // ("ABCDEFGHIJ")
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 // Manual Test.
1331 // Run by hand, with accompanying printf.statements in fast-path.go
1332 // to ensure that the fast functions are called.
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 // - send a slice []*int64 (sl1) into an chan (ch1) with cap > len(s1)
1353 // - encode ch1 as a stream array
1354 // - decode a chan (ch2), with cap > len(s1) from the stream array
1355 // - receive from ch2 into slice sl2
1356 // - compare sl1 and sl2
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 // t.Logf(">>>> from chan: is nil? %v, %v", j == nil, j)
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 // srv := rpc.NewServer()
1498 // srv.Register(testRpcInt)
1499 srv := testRpcServer
1500
1501 ln, err := net.Listen("tcp", "127.0.0.1:0") // listen on ipv4 localhost
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 // var opts *DecoderOptions
1508 // opts := testDecOpts
1509 // opts.MapType = mapStrIntfTyp
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 // fmt.Printf("accept err1: %v\n", err1)
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 // atomic.StoreUint64(&serverExitFlag, 0)
1562 // serverExitChan <- true
1563 }
1564
1565 go serverFn()
1566
1567 // runtime.Gosched()
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 // println("doTestMapEncodeForCanonical")
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 // encode v1 into b1, decode b1 into v2, encode v2 into b2, and compare b1 and b2.
1651 // OR
1652 // encode v1 into b1, decode b1 into v2, encode v2 into b2 and b3, and compare b2 and b3.
1653 // e.g. when doing cbor indefinite, we may haveto use out-of-band encoding
1654 // where each key is encoded as an indefinite length string, which makes it not the same
1655 // order as the strings were lexicographically ordered before.
1656
1657 var cborIndef bool
1658 if ch, ok := h.(*CborHandle); ok {
1659 cborIndef = ch.IndefiniteLength
1660 }
1661
1662 // if ch, ok := h.(*BincHandle); ok && ch.AsSymbols != 2 {
1663 // defer func(u uint8) { ch.AsSymbols = u }(ch.AsSymbols)
1664 // ch.AsSymbols = 2
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 // bh.SignedInteger = true
1675
1676 e1 := NewEncoderBytes(&b1, h)
1677 e1.MustEncode(v1)
1678 d1 := NewDecoderBytes(b1, h)
1679 d1.MustDecode(&v2)
1680 // testDeepEqualErr(v1, v2, t, "huh?")
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 // TestAnonCycleT{1,2,3} types are used to test anonymous cycles.
1769 // They are top-level, so that they can have circular references.
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 // just check that you can get typeInfo for T1
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 // below, we validate that the json string or number was encoded,
1847 // then decode, and validate that the correct value was decoded.
1848 fnStrChk := func() {
1849 // check that output started with '"', and ended with '"true'
1850 // if !(len(b) >= 7 && b[0] == '"' && string(b[len(b)-7:]) == `" true `) {
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) // done to work with 32-bit OS
1862 if v2n > 1<<53 || (v2n < 0 && -v2n > 1<<53) {
1863 fnStrChk()
1864 }
1865 case uint:
1866 v2n := uint64(v2) // done to work with 32-bit OS
1867 if v2n > 1<<53 {
1868 fnStrChk()
1869 }
1870 }
1871 case 'A':
1872 fnStrChk()
1873 default:
1874 // check that output doesn't contain " at all
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 // check that vb = true, and vi == v2
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 // check that vb = true, and vi == v2
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 //1234567890
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 // t.Logf("Encoded %v, decoded %v", i, i2)
1941 if i != i2 {
1942 t.Logf("Error: encoded %v, decoded %v", i, i2)
1943 t.FailNow()
1944 }
1945 }
1946
1947 // Comprehensive testing that generates data encoded from python handle (cbor, msgpack),
1948 // and validates that our code can read and write it out accordingly.
1949 // We keep this unexported here, and put actual test in ext_dep_test.go.
1950 // This way, it can be excluded by excluding file completely.
1951 func doTestPythonGenStreams(t *testing.T, h Handle) {
1952 defer testSetup(t, &h)()
1953
1954 // time0 := time.Now()
1955 // defer func() { xdebugf("python-gen-streams: %s: took: %v", h.Name(), time.Since(time0)) }()
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 //cmd.Stdin = strings.NewReader("some input")
1972 //cmd.Stdout = &out
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 // msgpack python needs Raw converted to string
1986 bh.RawToString = true
1987
1988 oldMapType := bh.MapType
1989 tablePythonVerify := testTableVerify(testVerifyForPython|testVerifyTimeAsInteger|testVerifyMapTypeStrIntf, h)
1990 for i, v := range tablePythonVerify {
1991 // if v == uint64(0) && h is a *MsgpackHandle: v = int64(0)
1992 bh.MapType = oldMapType
1993 //load up the golden file based on number
1994 //decode it
1995 //compare to in-mem object
1996 //encode it again
1997 //compare to output stream
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 //no need to indirect, because we pass a nil ptr, so we already have the value
2021 //if v1 != nil { v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface() }
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 // t.Logf("-------- DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
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 // To test MsgpackSpecRpc, we test 3 scenarios:
2067 // - Go Client to Go RPC Service (contained within TestMsgpackRpcSpec)
2068 // - Go client to Python RPC Service (contained within doTestMsgpackRpcSpecGoClientToPythonSvc)
2069 // - Python Client to Go RPC Service (contained within doTestMsgpackRpcSpecPythonClientToGoSvc)
2070 //
2071 // This allows us test the different calling conventions
2072 // - Go Service requires only one argument
2073 // - Python Service allows multiple arguments
2074
2075 func doTestMsgpackRpcSpecGoClientToPythonSvc(t *testing.T, h Handle) {
2076 if testSkipRPCTests {
2077 t.Skip(testSkipRPCTestsMsg)
2078 }
2079 defer testSetup(t, &h)()
2080
2081 // openPorts are between 6700 and 6800
2082 r := rand.New(rand.NewSource(time.Now().UnixNano()))
2083 openPort := strconv.FormatInt(6700+r.Int63n(99), 10)
2084 // openPort := "6792"
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) // time for python rpc server to start
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 //testCheckEqual(t, rstr, "{'A': 'Aa', 'B': 'Bb', 'C': 'Cc'}")
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 // seems 10ms is not enough for test run; set high to 1 second, and client will close when done
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 // interface{}
2148 _, isJson := h.(*JsonHandle)
2149 _, isCbor := h.(*CborHandle)
2150 bh := testBasicHandle(h)
2151 // isValuer := isJson || isCbor
2152 // _ = isValuer
2153 for _, r := range []RawExt{
2154 {Tag: 99, Value: "9999", Data: []byte("9999")},
2155 } {
2156 e := NewEncoderBytes(&b, h)
2157 e.MustEncode(&r)
2158 // fmt.Printf(">>>> rawext: isnil? %v, %d - %v\n", b == nil, len(b), b)
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 // switch h.(type) {
2173 // case *JsonHandle:
2174 // testDeepEqualErr(r.Value, v, t, "rawext-json")
2175 // default:
2176 // var r2 = r
2177 // if isValuer {
2178 // r2.Data = nil
2179 // } else {
2180 // r2.Value = nil
2181 // }
2182 // testDeepEqualErr(v, r2, t, "rawext-default")
2183 // }
2184 }
2185
2186 // Add testing for Raw also
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 // fmt.Printf(">>>> rawext: isnil? %v, %d - %v\n", b == nil, len(b), b)
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 // func doTestTimeExt(t *testing.T, h Handle) {
2212 // var t = time.Now()
2213 // // add time ext to the handle
2214 // }
2215
2216 func doTestMapStructKey(t *testing.T, h Handle) {
2217 defer testSetup(t, &h)()
2218 var b []byte
2219 var v interface{} // map[stringUint64T]wrapUint64Slice // 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 // this test expects that nil doesn't result in deleting entries
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) // hex to look at what was encoded
2349 if isJsonHandle {
2350 t.Logf("JSON encoded as: %s", bs) // hex to look at what was encoded
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 // This test can take a while if run multiple times in a loop, as it creates
2361 // large maps/slices. Use t.Short() appropriately to limit its execution time.
2362
2363 // Note that this test does not make sense for formats which do not pre-record
2364 // the length, like json, or cbor with indefinite length.
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 // sizes = []int{
2376 // 0, 1,
2377 // math.MaxInt8, math.MaxInt8 + 4, math.MaxInt8 - 4,
2378 // math.MaxInt16, math.MaxInt16 + 4, math.MaxInt16 - 4,
2379 // math.MaxInt32, math.MaxInt32 - 4,
2380 // // math.MaxInt32 + 4, // bombs on 32-bit
2381 // // math.MaxInt64, math.MaxInt64 - 4, // bombs on 32-bit
2382
2383 // math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
2384 // math.MaxUint16, math.MaxUint16 + 4, math.MaxUint16 - 4,
2385 // // math.MaxUint32, math.MaxUint32 + 4, math.MaxUint32 - 4, // bombs on 32-bit
2386 // }
2387 sizes = []int{
2388 // ensure in ascending order (as creating mm below requires it)
2389 0,
2390 1,
2391 math.MaxInt8 + 4,
2392 math.MaxUint8 + 4,
2393 }
2394 if !testing.Short() {
2395 sizes = append(sizes, math.MaxUint16+4) // math.MaxInt16+4, 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 // if len(bs2) != 0 || len(bs2) != len(bs) { }
2413 testReleaseBytes(bs)
2414
2415 // requires sizes to be in ascending order
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 // do same tests for large strings (encoded as symbols or not)
2434 // skip if 32-bit or not using unsafe mode
2435 if safeMode || (32<<(^uint(0)>>63)) < 64 {
2436 return
2437 }
2438
2439 // now, want to do tests for large strings, which
2440 // could be encoded as symbols.
2441 // to do this, we create a simple one-field struct,
2442 // use use flags to switch from symbols to non-symbols
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 // math.MaxUint16 / 16
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 // var s1 = stringView(in[:i])
2476 var s1 = string(in[:i])
2477 // fmt.Printf("testcontainerlen: large string: i: %v, |%s|\n", i, s1)
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 // bs, _ = testMarshalErr(m1, h, t, "-")
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 // now, do as symbols
2498 hbinc.AsSymbols = 1
2499 out = out[:0]
2500 e.ResetBytes(&out)
2501 e.MustEncode(m1)
2502 // bs, _ = testMarshalErr(m1, h, t, "-")
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 // test out extensions with large output
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) // so it's WriteExt returns nil
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(1)
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 // ensure this string can test the extent of json string decoding
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 // test time which uses the time.go implementation (ie Binc)
2614 var tt, tt2 time.Time
2615 // time in 1990
2616 tt = time.Unix(20*366*24*60*60, 1000*900).In(time.FixedZone("UGO", -5*60*60))
2617 // fmt.Printf("time tt: %v\n", tt)
2618 b := testMarshalErr(tt, h, t, "time-"+name)
2619 testUnmarshalErr(&tt2, b, h, t, "time-"+name)
2620 // per go documentation, test time with .Equal not ==
2621 if !tt2.Equal(tt) {
2622 t.Logf("%s: values not equal: 1: %v, 2: %v", name, tt2, tt)
2623 t.FailNow()
2624 }
2625 // testDeepEqualErr(tt.UTC(), tt2, t, "time-"+name)
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 // msgpack ones
2635 24, 128,
2636 // standard ones
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 // - maptype, slicetype: diff from map[string]intf, map[intf]intf or []intf, etc
2709 // include map[interface{}]string where some keys are []byte.
2710 // To test, take a sequence of []byte and string, and decode into []string and []interface.
2711 // Also, decode into map[string]string, map[string]interface{}, map[interface{}]string
2712
2713 bh := testBasicHandle(h)
2714 defer func(oldM, oldS reflect.Type) { bh.MapType, bh.SliceType = oldM, oldS }(bh.MapType, bh.SliceType)
2715
2716 // test for arrays first
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 // encode it as a map or as a slice
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 // encode vs as a list, and decode into a list and compare
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 // v2d = reflect.New(bh.SliceType).Elem().Interface()
2806 testUnmarshalErr(&v2d, b, h, t, name)
2807 testDeepEqualErr(v2d, goldSlice[j], t, name)
2808 testReleaseBytes(b)
2809 }
2810
2811 // to ensure that we do not use fast-path for map[intf]string, use a custom string type (for goldMapIS).
2812 // this will allow us to test out the path that sees a []byte where a map has an interface{} type,
2813 // and convert it to a string for the decoded map key.
2814
2815 // encode v2i as a map, and decode into a map and compare
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 // for formats that clearly differentiate binary from string, use v2i
2824 // else use the v2s (with all strings, no []byte)
2825 v2d = nil
2826 // v2d = reflect.New(bh.MapType).Elem().Interface()
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 // encode []byte and decode into one with len < cap.
2847 // get the slices > decDefSliceCap*2, so things like json can go much higher
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 // for each scalar:
2915 // - encode its ptr
2916 // - encode it (non-ptr)
2917 // - check that bytes are same
2918 // - make a copy (using reflect)
2919 // - check that same
2920 // - set zero on it
2921 // - check that its equal to 0 value
2922 // - decode into new
2923 // - compare to original
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 // store b1 into b, as b1 slice is reused for next marshal
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 // decode the nil value into rv2, and test that it is the zero value
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 // test that we can decode an encoded nil into it
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 // test setZero for *Raw and reflect.Value
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 // note: we cannot test reflect.Value's because they might point to different pointers
3006 // and reflect.DeepEqual doesn't honor that.
3007 // testDeepEqualErr(rv, reflect.ValueOf(&r0), t, "raw-reflect-zeroed")
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 // Use a valid string to test some extents of json string decoding
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 // encode missingFielderT2, decode into missingFielderT1, encode it out again, decode into new missingFielderT2, compare
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 // test canonical interaction - with structs having some missing fields and some regular fields
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{} // value to encode
3144 M int16 // maxdepth
3145 S bool // use swallow (decode into typed struct with only A1)
3146 E interface{} // error to find
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 // var s []interface{}
3162 // s = append(s, []interface{})
3163 // s[0] = append(s[0], []interface{})
3164 // s[0][0] = append(s[0][0], []interface{})
3165 // s[0][0][0] = append(s[0][0][0], []interface{})
3166 // s[0][0][0][0] = append(s[0][0][0][0], []interface{})
3167 // return s
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 // MARKER: 20200925: swallow doesn't track depth anymore
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 // decode into something that just triggers swallow
3221 testReleaseBytes(b1)
3222 }
3223 }
3224
3225 func doTestMultipleEncDec(t *testing.T, h Handle) {
3226 defer testSetup(t, &h)()
3227 name := h.Name()
3228 // encode a string multiple times.
3229 // decode it multiple times.
3230 // ensure we get the value each time
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 // Need to test edge case where bytes are encoded as an array
3264 // (not using optimized []byte native format)
3265
3266 // encode []int8 (or int32 or any numeric type) with all positive numbers
3267 // decode it into []byte
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 // Note: We cannot use TestStrucFlex because it has omitempty values,
3296 // Meaning that sometimes, encoded and decoded will not be same.
3297 // {
3298 // var ts1 = newTestStrucFlex(2, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
3299 // var ts2 TestStrucFlex
3300 // bs := testMarshalErr(ts1, h, t, name)
3301 // fmt.Printf("\n\n%s\n\n", bs)
3302 // testUnmarshalErr(&ts2, bs, h, t, name)
3303 // testDeepEqualErr(ts1, &ts2, t, name)
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 // MARKER: should be 0 but binc symbols do not work well
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 // if _, ok := h.(*JsonHandle); ok {
3352 // xdebugf("bs1: %s, bs2: %s", bs1, bs2)
3353 // }
3354 // if bcok {
3355 // xdebug2f("-------------")
3356 // xdebugf("v: %#v, m: %#v", v, m)
3357 // xdebugf("bs1: %v", bs1)
3358 // xdebugf("bs2: %v", bs2)
3359 // xdebugf("bs1==bs2: %v", bytes.Equal(bs1, bs2))
3360 // testDeepEqualErr(bs1, bs2, t, name+"")
3361 // xdebug2f("-------------")
3362 // return
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 // name := h.Name()
3399
3400 // Tests:
3401 // - RawToString
3402 // - StringToRaw
3403 // - MapValueReset
3404 // - DeleteOnMapValue (skipped - no longer supported)
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 // encode: StringToRaw
3429 // decode: RawToString
3430
3431 // compare encoded v1 to encoded v2, while setting StringToRaw to b
3432 fne := func(v1, v2 interface{}, b bool) {
3433 bh.StringToRaw = b
3434 bs1 = testMarshalErr(v1, h, t, "")
3435 // bs1 = []byte(string(bs1))
3436 bs2 = testMarshalErr(v2, h, t, "")
3437 testDeepEqualErr(bs1, bs2, t, "")
3438 testReleaseBytes(bs1)
3439 testReleaseBytes(bs2)
3440 }
3441
3442 // encoded v1, decode naked and compare to v2
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 // m3 := map[[3]byte]string{[3]byte{'k', 'e', 'y'}: sv0}
3466 m4 := map[[3]byte][]byte{[3]byte{'k', 'e', 'y'}: bv0}
3467 m5 := map[string][]byte{"key": bv0}
3468 // m6 := map[string]string{"key": sv0}
3469
3470 m7 := map[interface{}]interface{}{"key": sv0}
3471 m8 := map[interface{}]interface{}{"key": bv0}
3472
3473 // StringToRaw=true
3474 fne(m1, m4, true)
3475
3476 // StringToRaw=false
3477 // compare encoded m2 to encoded m5
3478 fne(m2, m5, false)
3479
3480 // json doesn't work well with StringToRaw and RawToString
3481 // when dealing with interfaces, because it cannot decipher
3482 // that a string should be treated as base64.
3483 if jok {
3484 goto MAP_VALUE_RESET
3485 }
3486
3487 // if msgpack, always set WriteExt = RawToString
3488
3489 // StringToRaw=true (RawToString=true)
3490 // encoded m1, decode naked and compare to m5
3491 fnd(m2, m7, true, true, true)
3492
3493 // StringToRaw=true (RawToString=false)
3494 // encoded m1, decode naked and compare to m6
3495 fnd(m1, m8, true, false, false)
3496
3497 // StringToRaw=false, RawToString=true
3498 // encode m1, decode naked, and compare to m6
3499 fnd(m2, m7, false, true, true)
3500
3501 MAP_VALUE_RESET:
3502 // set MapValueReset, and then decode i
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 // if !(m9["key"] == m1["key"]
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 // t1 = struct {
3522 // key string
3523 // }{ key: sv0 }
3524 // t2 := struct {
3525 // key []byte
3526 // }{ key: bv1 }
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 // Also test out decoding string values into naked interface
3573 b = `{"true": true, "false": false, "null": null, "7700000000000000000": 7700000000000000000}`
3574 const num = 7700000000000000000 // 77
3575 var golden2 = map[interface{}]interface{}{
3576 true: true,
3577 false: false,
3578 nil: nil,
3579 // uint64(num):uint64(num),
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 // v3 = v2
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 // fmt.Printf("\n-----------\n%s\n------------\n%s\n-------------\n", txt1Tab, txtSpaces)
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 __doTestBufioDecReader(t *testing.T, bufsize int) {
3726 bufsizehalf := (bufsize + 1) / 2
3727
3728 // try to read 85 bytes in chunks of 7 at a time.
3729 var s = strings.Repeat("01234'56789 ", 5)
3730 // fmt.Printf("s: %s\n", s)
3731 var r = strings.NewReader(s)
3732 var br bufioDecReader
3733 var blist bytesFreelist
3734 br.reset(r, bufsize, &blist)
3735 b, err := ioutil.ReadAll(br.r)
3736 if err != nil {
3737 panic(err)
3738 }
3739 var s2 = string(b)
3740 // fmt.Printf("s==s2: %v, len(s): %v, len(b): %v, len(s2): %v\n", s == s2, len(s), len(b), len(s2))
3741 if s != s2 {
3742 t.Logf("not equal: \ns: %s\ns2: %s", s, s2)
3743 t.FailNow()
3744 }
3745 // Now, test search functions for skip, readTo and readUntil
3746 // readUntil ', readTo ', skip whitespace. 3 times in a loop, each time compare the token and/or outs
3747 // readUntil: see: 56789
3748 var out []byte
3749 var token byte
3750 br.reset(strings.NewReader(s), bufsizehalf, &blist)
3751 // println()
3752 for _, v2 := range [...]string{
3753 `01234'`,
3754 `56789 01234'`,
3755 `56789 01234'`,
3756 `56789 01234'`,
3757 } {
3758 out = br.readUntil('\'')
3759 testDeepEqualErr(string(out), v2[:len(v2)-1], t, "-")
3760 // fmt.Printf("readUntil: out: `%s`\n", out)
3761 }
3762 br.reset(strings.NewReader(s), bufsizehalf, &blist)
3763 // println()
3764
3765 // 20200915: readTo is not longer available
3766 // for range [4]struct{}{} {
3767 // out = br.readTo(&numCharBitset)
3768 // testDeepEqualErr(string(out), `01234`, t, "-")
3769 // // fmt.Printf("readTo: out: `%s`\n", out)
3770 // out = br.readUntil('\'', true)
3771 // testDeepEqualErr(string(out), "'", t, "-")
3772 // // fmt.Printf("readUntil: out: `%s`\n", out)
3773 // out = br.readTo(&numCharBitset)
3774 // testDeepEqualErr(string(out), `56789`, t, "-")
3775 // // fmt.Printf("readTo: out: `%s`\n", out)
3776 // out = br.readUntil('0', true)
3777 // testDeepEqualErr(string(out), ` 0`, t, "-")
3778 // // fmt.Printf("readUntil: out: `%s`\n", out)
3779 // br.unreadn1()
3780 // }
3781 br.reset(strings.NewReader(s), bufsizehalf, &blist)
3782 for range [4]struct{}{} {
3783 token = br.skipWhitespace() // br.skip(&whitespaceCharBitset)
3784 testDeepEqualErr(token, byte('0'), t, "-")
3785 out = br.readUntil(' ')
3786 testDeepEqualErr(string(out), `1234'56789`, t, "-")
3787 }
3788 }
3789
3790 func doTestPreferArrayOverSlice(t *testing.T, h Handle) {
3791 defer testSetup(t, &h)()
3792 // encode a slice, decode it with PreferArrayOverSlice
3793 // if codecgen, skip the test (as codecgen doesn't work with PreferArrayOverSlice)
3794 if codecgen {
3795 t.Skip("skipping ... prefer array over slice is not supported in codecgen mode")
3796 }
3797 bh := testBasicHandle(h)
3798 paos := bh.PreferArrayOverSlice
3799 styp := bh.SliceType
3800 defer func() {
3801 bh.PreferArrayOverSlice = paos
3802 bh.SliceType = styp
3803 }()
3804 bh.PreferArrayOverSlice = true
3805 bh.SliceType = reflect.TypeOf(([]bool)(nil))
3806
3807 s2 := [4]bool{true, false, true, false}
3808 s := s2[:]
3809 var v interface{}
3810 bs := testMarshalErr(s, h, t, t.Name())
3811 testUnmarshalErr(&v, bs, h, t, t.Name())
3812 testDeepEqualErr(s2, v, t, t.Name())
3813 testReleaseBytes(bs)
3814 }
3815
3816 func doTestZeroCopyBytes(t *testing.T, h Handle) {
3817 defer testSetup(t, &h)()
3818 // jsonhandle and cborhandle with indefiniteLength do not support inline bytes, so skip them.
3819 if _, ok := h.(*JsonHandle); ok {
3820 t.Skipf("skipping ... zero copy bytes not supported by json handle")
3821 }
3822 if ch, ok := h.(*CborHandle); ok && ch.IndefiniteLength {
3823 t.Skipf("skipping ... zero copy bytes not supported by cbor handle with IndefiniteLength=true")
3824 }
3825
3826 bh := testBasicHandle(h)
3827 zc := bh.ZeroCopy
3828 defer func() {
3829 bh.ZeroCopy = zc
3830 }()
3831 bh.ZeroCopy = true
3832
3833 s := []byte("hello")
3834 var v []byte
3835 bs := testMarshalErr(s, h, t, t.Name())
3836
3837 // Note: this test only works for decoding from []byte, so cannot use testUnmarshalErr
3838 NewDecoderBytes(bs, h).MustDecode(&v)
3839 // testUnmarshalErr(&v, bs, h, t, t.Name())
3840
3841 // validate that bs and s points into the bs stream
3842 for i := range bs {
3843 if &bs[i] == &v[0] {
3844 return
3845 }
3846 }
3847
3848 // if not match, then a failure happened.
3849 if len(bs) > 0 && len(v) > 0 {
3850 t.Logf("%s: ZeroCopy=true, but decoded (%p) is not slice of input: (%p)", h.Name(), &v[0], &bs[0])
3851 } else {
3852 t.Logf("%s: ZeroCopy=true, but decoded OR input slice is empty: %v, %v", h.Name(), v, bs)
3853 }
3854 testReleaseBytes(bs)
3855 t.FailNow()
3856 }
3857
3858 func doTestNextValueBytes(t *testing.T, h Handle) {
3859 defer testSetup(t, &h)()
3860
3861 bh := testBasicHandle(h)
3862
3863 // - encode uint, int, float, bool, struct, map, slice, string - all separated by nil
3864 // - use nextvaluebytes to grab he's got each one, and decode it, and compare
3865 var inputs = []interface{}{
3866 uint64(7777),
3867 int64(9999),
3868 float64(12.25),
3869 true,
3870 false,
3871 map[string]uint64{"1": 1, "22": 22, "333": 333, "4444": 4444},
3872 []string{"1", "22", "333", "4444"},
3873 // use *TestStruc, not *TestStrucFlex, as *TestStrucFlex is harder to compare with deep equal
3874 // Remember: *TestStruc was separated for this reason, affording comparing against other libraries
3875 newTestStruc(testDepth, testNumRepeatString, false, false, true),
3876 "1223334444",
3877 }
3878 var out []byte
3879
3880 for i, v := range inputs {
3881 _ = i
3882 bs := testMarshalErr(v, h, t, "nextvaluebytes")
3883 out = append(out, bs...)
3884 bs2 := testMarshalErr(nil, h, t, "nextvaluebytes")
3885 out = append(out, bs2...)
3886 testReleaseBytes(bs)
3887 testReleaseBytes(bs2)
3888 }
3889 // out = append(out, []byte("----")...)
3890
3891 var valueBytes = make([][]byte, len(inputs)*2)
3892
3893 d, oldReadBufferSize := testSharedCodecDecoder(out, h, testBasicHandle(h))
3894 for i := 0; i < len(inputs)*2; i++ {
3895 valueBytes[i] = d.d.nextValueBytes([]byte{})
3896 // bs := d.d.nextValueBytes([]byte{})
3897 // valueBytes[i] = make([]byte, len(bs))
3898 // copy(valueBytes[i], bs)
3899 }
3900 if testUseIoEncDec >= 0 {
3901 bh.ReaderBufferSize = oldReadBufferSize
3902 }
3903
3904 defer func(b bool) { bh.InterfaceReset = b }(bh.InterfaceReset)
3905 bh.InterfaceReset = false
3906
3907 var result interface{}
3908 for i := 0; i < len(inputs); i++ {
3909 // result = reflect.New(reflect.TypeOf(inputs[i])).Elem().Interface()
3910 result = reflect.Zero(reflect.TypeOf(inputs[i])).Interface()
3911 testUnmarshalErr(&result, valueBytes[i*2], h, t, "nextvaluebytes")
3912 testDeepEqualErr(inputs[i], result, t, "nextvaluebytes-1")
3913 result = nil
3914 testUnmarshalErr(&result, valueBytes[(i*2)+1], h, t, "nextvaluebytes")
3915 testDeepEqualErr(nil, result, t, "nextvaluebytes-2")
3916 }
3917 }
3918
3919 func doTestNumbers(t *testing.T, h Handle) {
3920 defer testSetup(t, &h)()
3921 __doTestIntegers(t, h)
3922 __doTestFloats(t, h)
3923 __doTestIntegerFloatConversions(t, h)
3924 }
3925
3926 func __doTestIntegers(t *testing.T, h Handle) {
3927 // handle SignedInteger=true|false
3928 // decode into an interface{}
3929
3930 bh := testBasicHandle(h)
3931
3932 oldSignedInteger := bh.SignedInteger
3933 var oldPreferFloat bool
3934 var oldNoFixedNum bool
3935 jh, jok := h.(*JsonHandle)
3936 mh, mok := h.(*MsgpackHandle)
3937 if jok {
3938 oldPreferFloat = jh.PreferFloat
3939 }
3940 if mok {
3941 oldNoFixedNum = mh.NoFixedNum
3942 mh.NoFixedNum = true
3943 }
3944
3945 defer func() {
3946 bh.SignedInteger = oldSignedInteger
3947 if jok {
3948 jh.PreferFloat = oldPreferFloat
3949 }
3950 if mok {
3951 mh.NoFixedNum = oldNoFixedNum
3952 }
3953 }()
3954
3955 // var vi int64
3956 // var ui uint64
3957 var ii interface{}
3958
3959 for _, v := range testUintsToParse {
3960 if jok {
3961 jh.PreferFloat = false
3962 }
3963 b := testMarshalErr(v, h, t, "test-integers")
3964 ii = nil
3965 bh.SignedInteger = true
3966 testUnmarshalErr(&ii, b, h, t, "test-integers")
3967 testDeepEqualErr(ii, int64(v), t, "test-integers-signed")
3968 ii = nil
3969 bh.SignedInteger = false
3970 testUnmarshalErr(&ii, b, h, t, "test-integers")
3971 testDeepEqualErr(ii, uint64(v), t, "test-integers-unsigned")
3972 ii = nil
3973 if jok {
3974 jh.PreferFloat = true
3975 testUnmarshalErr(&ii, b, h, t, "test-integers")
3976 testDeepEqualErr(ii, float64(v), t, "test-integers-float")
3977 }
3978 testReleaseBytes(b)
3979 }
3980 }
3981
3982 var testUintsToParse = []uint64{
3983 // Note: use large integers, as some formats store small integers in an agnostic
3984 // way, where its not clear if signed or unsigned.
3985 2,
3986 2048,
3987 1<<63 - 4,
3988 1800000000e-2,
3989 18000000e+2,
3990 4.56e+4, // tests float32 exact parsing
3991 4.56e+16,
3992 }
3993
3994 var testFloatsToParse = []float64{
3995 3,
3996 math.NaN(),
3997 math.Inf(1),
3998 math.Inf(-1),
3999 4.56e+4, // tests float32 exact parsing
4000 4.56e+18, // float32 parsing - exp > 10
4001 4.56e+10,
4002 4.56e+30,
4003 1.01234567890123456789e+30,
4004 1.32e+5,
4005 1.32e-5,
4006 0e+01234567890123456789,
4007 1.7976931348623157e308,
4008 -1.7976931348623157e+308,
4009 1e308,
4010 1e-308,
4011 1.694649e-317,
4012 // 1e-4294967296,
4013 2.2250738585072012e-308,
4014 4.630813248087435e+307,
4015 1.00000000000000011102230246251565404236316680908203125,
4016 1.00000000000000033306690738754696212708950042724609375,
4017 }
4018
4019 func __doTestFloats(t *testing.T, h Handle) {
4020 _, jok := h.(*JsonHandle)
4021
4022 f64s := testFloatsToParse
4023 const unusedVal = 9999 // use this as a marker
4024
4025 // marshall it, unmarshal it, compare to original
4026 // Note: JSON encodes NaN, inf, -inf as null, which is decoded as zero value (ie 0).
4027 for _, f64 := range f64s {
4028 {
4029 f := f64
4030 var w float64 = unusedVal
4031 b := testMarshalErr(f, h, t, "test-floats-enc")
4032 testUnmarshalErr(&w, b, h, t, "test-floats-dec")
4033 // we only check json for float64, as it doesn't differentiate
4034 if (jok && (math.IsNaN(f64) || math.IsInf(f64, 0)) && w != 0) ||
4035 (!jok && w != f && !math.IsNaN(float64(f))) {
4036 t.Logf("error testing float64: %v, decoded as: %v", f, w)
4037 t.FailNow()
4038 }
4039 var wi interface{}
4040 testUnmarshalErr(&wi, b, h, t, "test-floats-dec")
4041 if (jok && (math.IsNaN(f64) || math.IsInf(f64, 0)) && wi != nil) ||
4042 (!jok && wi.(float64) != f && !math.IsNaN(float64(f))) {
4043 t.Logf("error testing float64: %v, decoded as: %v", f, wi)
4044 t.FailNow()
4045 }
4046 testReleaseBytes(b)
4047 }
4048 {
4049 f := float32(f64)
4050 var w float32 = unusedVal
4051 b := testMarshalErr(f, h, t, "test-floats-enc")
4052 // xdebug2f("test float: of %v, encoded as: %s", f, b)
4053 testUnmarshalErr(&w, b, h, t, "test-floats-dec")
4054 if (jok && (math.IsNaN(f64) || math.IsInf(f64, 0)) && w != 0) ||
4055 (!jok && w != f && !math.IsNaN(float64(f))) {
4056 t.Logf("error testing float32: %v, decoded as: %v", f, w)
4057 t.FailNow()
4058 }
4059 testReleaseBytes(b)
4060 }
4061 }
4062 }
4063
4064 func __doTestIntegerFloatConversions(t *testing.T, h Handle) {
4065 if !testRecoverPanicToErr {
4066 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
4067 }
4068 type tI struct{ N int }
4069 type tU struct{ N uint }
4070 type tF struct{ N float64 }
4071
4072 type elem struct {
4073 in interface{}
4074 out interface{}
4075 err string
4076 }
4077 tests := []elem{
4078 // good
4079 {tI{5}, tF{5.0}, ""},
4080 {tU{5}, tF{5.0}, ""},
4081 {tF{5.0}, tU{5}, ""},
4082 {tF{5.0}, tI{5}, ""},
4083 {tF{-5.0}, tI{-5}, ""},
4084 // test negative number into unsigned integer
4085 {tI{-5}, tU{5}, "error"},
4086 {tF{-5.0}, tU{5}, "error"},
4087 // test fractional float into integer
4088 {tF{-5.7}, tU{5}, "error"},
4089 {tF{-5.7}, tI{5}, "error"},
4090 }
4091 for _, v := range tests {
4092 r := reflect.New(reflect.TypeOf(v.out))
4093 b := testMarshalErr(v.in, h, t, "")
4094 err := testUnmarshal(r.Interface(), b, h)
4095 if v.err == "" {
4096 testDeepEqualErr(err, nil, t, "")
4097 testDeepEqualErr(r.Elem().Interface(), v.out, t, "")
4098 } else if err == nil {
4099 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)
4100 t.FailNow()
4101 }
4102 }
4103 }
4104
4105 func doTestStructFieldInfoToArray(t *testing.T, h Handle) {
4106 if !testRecoverPanicToErr {
4107 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
4108 }
4109 defer testSetup(t, &h)()
4110 bh := testBasicHandle(h)
4111
4112 defer func(b bool) { bh.CheckCircularRef = b }(bh.CheckCircularRef)
4113 bh.CheckCircularRef = true
4114
4115 var vs = Sstructsmall{A: 99}
4116 var vb = Sstructbig{
4117 A: 77,
4118 B: true,
4119 c: "ccc 3 ccc",
4120 Ssmallptr: &vs,
4121 Ssmall: vs,
4122 }
4123 vb.Sptr = &vb
4124
4125 vba := SstructbigToArray{
4126 A: vb.A,
4127 B: vb.B,
4128 c: vb.c,
4129 Ssmallptr: vb.Ssmallptr,
4130 Ssmall: vb.Ssmall,
4131 Sptr: vb.Sptr,
4132 }
4133
4134 var b []byte
4135 var err error
4136 if !codecgen {
4137 // codecgen doesn't support CheckCircularRef, and these types are codecgen'ed
4138 b, err = testMarshal(&vba, h)
4139 testReleaseBytes(b)
4140 if err == nil || !strings.Contains(err.Error(), "circular reference found") {
4141 t.Logf("expect circular reference error, got: %v", err)
4142 t.FailNow()
4143 }
4144 }
4145
4146 vb2 := vb
4147 vb.Sptr = nil // so we stop having the circular reference error
4148 vba.Sptr = &vb2
4149
4150 var ss []interface{}
4151
4152 // bh.CheckCircularRef = false
4153 b = testMarshalErr(&vba, h, t, "-")
4154 testUnmarshalErr(&ss, b, h, t, "-")
4155 testDeepEqualErr(ss[1], true, t, "-")
4156 testReleaseBytes(b)
4157 }
4158
4159 func doTestDesc(t *testing.T, h Handle, m map[byte]string) {
4160 defer testSetup(t, &h)()
4161 for k, v := range m {
4162 if s := h.desc(k); s != v {
4163 t.Logf("error describing descriptor: '%q' i.e. 0x%x, expected '%s', got '%s'", k, k, v, s)
4164 t.FailNow()
4165 }
4166 }
4167 }
4168
4169 func TestBufioDecReader(t *testing.T) {
4170 doTestBufioDecReader(t)
4171 }
4172
4173 func doTestBufioDecReader(t *testing.T) {
4174 defer testSetup(t, nil)()
4175 __doTestBufioDecReader(t, 13)
4176 __doTestBufioDecReader(t, 3)
4177 __doTestBufioDecReader(t, 5)
4178 __doTestBufioDecReader(t, 127)
4179 }
4180
4181 func TestAtomic(t *testing.T) {
4182 defer testSetup(t, nil)()
4183 // load, store, load, confirm
4184 if true {
4185 var a atomicTypeInfoSlice
4186 l := a.load()
4187 if l != nil {
4188 t.Logf("atomic fail: %T, expected load return nil, received: %v", a, l)
4189 t.FailNow()
4190 }
4191 l = append(l, rtid2ti{})
4192 a.store(l)
4193 l = a.load()
4194 if len(l) != 1 {
4195 t.Logf("atomic fail: %T, expected load to have length 1, received: %d", a, len(l))
4196 t.FailNow()
4197 }
4198 }
4199 if true {
4200 var a atomicRtidFnSlice
4201 l := a.load()
4202 if l != nil {
4203 t.Logf("atomic fail: %T, expected load return nil, received: %v", a, l)
4204 t.FailNow()
4205 }
4206 l = append(l, codecRtidFn{})
4207 a.store(l)
4208 l = a.load()
4209 if len(l) != 1 {
4210 t.Logf("atomic fail: %T, expected load to have length 1, received: %d", a, len(l))
4211 t.FailNow()
4212 }
4213 }
4214 if true {
4215 var a atomicClsErr
4216 l := a.load()
4217 if l.err != nil {
4218 t.Logf("atomic fail: %T, expected load return clsErr = nil, received: %v", a, l.err)
4219 t.FailNow()
4220 }
4221 l.err = io.EOF
4222 a.store(l)
4223 l = a.load()
4224 if l.err != io.EOF {
4225 t.Logf("atomic fail: %T, expected clsErr = io.EOF, received: %v", a, l.err)
4226 t.FailNow()
4227 }
4228 }
4229 }
4230
4231 // -----------
4232
4233 func doTestJsonLargeInteger(t *testing.T, h Handle) {
4234 if !testRecoverPanicToErr {
4235 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
4236 }
4237 defer testSetup(t, &h)()
4238 jh := h.(*JsonHandle)
4239 for _, i := range []uint8{'L', 'A', 0} {
4240 for _, j := range []interface{}{
4241 int64(1 << 60),
4242 -int64(1 << 60),
4243 0,
4244 1 << 20,
4245 -(1 << 20),
4246 uint64(1 << 60),
4247 uint(0),
4248 uint(1 << 20),
4249 int64(1840000e-2),
4250 uint64(1840000e+2),
4251 } {
4252 __doTestJsonLargeInteger(t, j, i, jh)
4253 }
4254 }
4255
4256 oldIAS := jh.IntegerAsString
4257 defer func() { jh.IntegerAsString = oldIAS }()
4258 jh.IntegerAsString = 0
4259
4260 type tt struct {
4261 s string
4262 canI, canUi bool
4263 i int64
4264 ui uint64
4265 }
4266
4267 var i int64
4268 var ui uint64
4269 var err error
4270 var d *Decoder = NewDecoderBytes(nil, jh)
4271 for _, v := range []tt{
4272 {"0", true, true, 0, 0},
4273 {"0000", true, true, 0, 0},
4274 {"0.00e+2", true, true, 0, 0},
4275 {"000e-2", true, true, 0, 0},
4276 {"0.00e-2", true, true, 0, 0},
4277
4278 {"9223372036854775807", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
4279 {"92233720368547758.07e+2", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
4280 {"922337203685477580700e-2", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
4281 {"9223372.036854775807E+12", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
4282 {"9223372036854775807000000000000E-12", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
4283 {"0.9223372036854775807E+19", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
4284 {"92233720368547758070000000000000000000E-19", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
4285 {"0.000009223372036854775807E+24", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
4286 {"9223372036854775807000000000000000000000000E-24", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
4287
4288 {"-9223372036854775808", true, false, math.MinInt64, 0}, // minint64
4289 {"-92233720368547758.08e+2", true, false, math.MinInt64, 0}, // minint64
4290 {"-922337203685477580800E-2", true, false, math.MinInt64, 0}, // minint64
4291 {"-9223372.036854775808e+12", true, false, math.MinInt64, 0}, // minint64
4292 {"-9223372036854775808000000000000E-12", true, false, math.MinInt64, 0}, // minint64
4293 {"-0.9223372036854775808e+19", true, false, math.MinInt64, 0}, // minint64
4294 {"-92233720368547758080000000000000000000E-19", true, false, math.MinInt64, 0}, // minint64
4295 {"-0.000009223372036854775808e+24", true, false, math.MinInt64, 0}, // minint64
4296 {"-9223372036854775808000000000000000000000000E-24", true, false, math.MinInt64, 0}, // minint64
4297
4298 {"18446744073709551615", false, true, 0, math.MaxUint64}, // maxuint64
4299 {"18446744.073709551615E+12", false, true, 0, math.MaxUint64}, // maxuint64
4300 {"18446744073709551615000000000000E-12", false, true, 0, math.MaxUint64}, // maxuint64
4301 {"0.000018446744073709551615E+24", false, true, 0, math.MaxUint64}, // maxuint64
4302 {"18446744073709551615000000000000000000000000E-24", false, true, 0, math.MaxUint64}, // maxuint64
4303
4304 // Add test for limit of uint64 where last digit is 0
4305 {"18446744073709551610", false, true, 0, math.MaxUint64 - 5}, // maxuint64
4306 {"18446744.073709551610E+12", false, true, 0, math.MaxUint64 - 5}, // maxuint64
4307 {"18446744073709551610000000000000E-12", false, true, 0, math.MaxUint64 - 5}, // maxuint64
4308 {"0.000018446744073709551610E+24", false, true, 0, math.MaxUint64 - 5}, // maxuint64
4309 {"18446744073709551610000000000000000000000000E-24", false, true, 0, math.MaxUint64 - 5}, // maxuint64
4310 // {"", true, true},
4311 } {
4312 if v.s == "" {
4313 continue
4314 }
4315 d.ResetBytes([]byte(v.s))
4316 err = d.Decode(&ui)
4317 if (v.canUi && err != nil) || (!v.canUi && err == nil) || (v.canUi && err == nil && v.ui != ui) {
4318 t.Logf("Failing to decode %s (as unsigned): %v", v.s, err)
4319 t.FailNow()
4320 }
4321
4322 d.ResetBytes([]byte(v.s))
4323 err = d.Decode(&i)
4324 if (v.canI && err != nil) || (!v.canI && err == nil) || (v.canI && err == nil && v.i != i) {
4325 t.Logf("Failing to decode %s (as signed): %v", v.s, err)
4326 t.FailNow()
4327 }
4328 }
4329 }
4330
4331 func doTestJsonInvalidUnicode(t *testing.T, h Handle) {
4332 if !testRecoverPanicToErr {
4333 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
4334 }
4335 defer testSetup(t, &h)()
4336 // t.Skipf("new json implementation does not handle bad unicode robustly")
4337 jh := h.(*JsonHandle)
4338
4339 var err error
4340
4341 // ---- test unmarshal ---
4342 var m = map[string]string{
4343 `"\udc49\u0430abc"`: "\uFFFDabc",
4344 `"\udc49\u0430"`: "\uFFFD",
4345 `"\udc49abc"`: "\uFFFDabc",
4346 `"\udc49"`: "\uFFFD",
4347 `"\udZ49\u0430abc"`: "\uFFFD\u0430abc",
4348 `"\udcG9\u0430"`: "\uFFFD\u0430",
4349 `"\uHc49abc"`: "\uFFFDabc",
4350 `"\uKc49"`: "\uFFFD",
4351 }
4352
4353 for k, v := range m {
4354 // call testUnmarshal directly, so we can check for EOF
4355 // testUnmarshalErr(&s, []byte(k), jh, t, "-")
4356 var s string
4357 err = testUnmarshal(&s, []byte(k), jh)
4358 if err != nil {
4359 if err == io.EOF {
4360 continue
4361 }
4362 t.Logf("%s: unmarshal failed: %v", "-", err)
4363 t.FailNow()
4364 }
4365
4366 if s != v {
4367 t.Logf("unmarshal: not equal: %q, %q", v, s)
4368 t.FailNow()
4369 }
4370 }
4371
4372 // test some valid edge cases
4373 m = map[string]string{
4374 `"az\uD834\udD1E"`: "az𝄞",
4375 `"n\ud834\uDD1en"`: "n\U0001D11En", // "\uf09d849e", // "\UD834DD1E" // U+1DD1E g clef
4376
4377 `"a\\\"\/\"\b\f\n\r\"\tz"`: "a\\\"/\"\b\f\n\r\"\tz",
4378 }
4379 for k, v := range m {
4380 var s string
4381 err = testUnmarshal(&s, []byte(k), jh)
4382 if err != nil {
4383 t.Logf("%s: unmarshal failed: %v", "-", err)
4384 t.FailNow()
4385 }
4386
4387 if s != v {
4388 t.Logf("unmarshal: not equal: %q, %q", v, s)
4389 t.FailNow()
4390 }
4391 }
4392
4393 // ---- test marshal ---
4394 var b = []byte{'"', 0xef, 0xbf, 0xbd} // this is " and unicode.ReplacementChar (as bytes)
4395 var m2 = map[string][]byte{
4396 string([]byte{0xef, 0xbf, 0xbd}): append(b, '"'),
4397 string([]byte{0xef, 0xbf, 0xbd, 0x0, 0x0}): append(b, `\u0000\u0000"`...),
4398
4399 "a\\\"/\"\b\f\n\r\"\tz": []byte(`"a\\\"/\"\b\f\n\r\"\tz"`),
4400
4401 // our encoder doesn't support encoding using only ascii ... so need to use the utf-8 version
4402 // "n\U0001D11En": []byte(`"n\uD834\uDD1En"`),
4403 // "az𝄞": []byte(`"az\uD834\uDD1E"`),
4404 "n\U0001D11En": []byte(`"n𝄞n"`),
4405 "az𝄞": []byte(`"az𝄞"`),
4406
4407 string([]byte{129, 129}): []byte(`"\uFFFD\uFFFD"`),
4408 }
4409
4410 for k, v := range m2 {
4411 b, err = testMarshal(k, jh)
4412 if err != nil {
4413 t.Logf("%s: marshal failed: %v", "-", err)
4414 t.FailNow()
4415 }
4416
4417 if !bytes.Equal(b, v) {
4418 t.Logf("marshal: not equal: %q, %q", v, b)
4419 t.FailNow()
4420 }
4421 testReleaseBytes(b)
4422 }
4423 }
4424
4425 func doTestJsonNumberParsing(t *testing.T, h Handle) {
4426 defer testSetup(t, &h)()
4427 parseInt64_reader := func(r readFloatResult) (v int64, fail bool) {
4428 u, fail := parseUint64_reader(r)
4429 if fail {
4430 return
4431 }
4432 if r.neg {
4433 v = -int64(u)
4434 } else {
4435 v = int64(u)
4436 }
4437 return
4438 }
4439 for _, f64 := range testFloatsToParse {
4440 // using large prec might make a non-exact float ..., while small prec might make lose some precision.
4441 // However, we can do a check, and only check if the (u)int64 and float64 can be converted to one another
4442 // without losing any precision. Then we can use any precision for the tests.
4443 var precs = [...]int{32, -1}
4444 var r readFloatResult
4445 var fail bool
4446 var fs uint64
4447 var fsi int64
4448 var fint, ffrac float64
4449 _ = ffrac
4450 var bv []byte
4451 for _, prec := range precs {
4452 f := f64
4453 if math.IsNaN(f64) || math.IsInf(f64, 0) {
4454 goto F32
4455 }
4456 bv = strconv.AppendFloat(nil, f, 'E', prec, 64)
4457 if fs, err := parseFloat64_custom(bv); err != nil || fs != f {
4458 t.Logf("float64 -> float64 error (prec: %v) parsing '%s', got %v, expected %v: %v", prec, bv, fs, f, err)
4459 t.FailNow()
4460 }
4461 // try decoding it a uint64 or int64
4462 fint, ffrac = math.Modf(f)
4463 // xdebug2f(">> fint: %v (ffrac: %v) as uint64: %v as float64: %v", fint, ffrac, uint64(fint), float64(uint64(fint)))
4464 bv = strconv.AppendFloat(bv[:0], fint, 'E', prec, 64)
4465 if f < 0 || fint != float64(uint64(fint)) {
4466 goto F64i
4467 }
4468 r = readFloat(bv, fi64u)
4469 fail = !r.ok
4470 if r.ok {
4471 fs, fail = parseUint64_reader(r)
4472 }
4473 if fail || fs != uint64(fint) {
4474 t.Logf("float64 -> uint64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fs, uint64(fint))
4475 t.FailNow()
4476 }
4477 F64i:
4478 if fint != float64(int64(fint)) {
4479 goto F32
4480 }
4481 r = readFloat(bv, fi64u)
4482 fail = !r.ok
4483 if r.ok {
4484 fsi, fail = parseInt64_reader(r)
4485 }
4486 if fail || fsi != int64(fint) {
4487 t.Logf("float64 -> int64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fsi, int64(fint))
4488 t.FailNow()
4489 }
4490 F32:
4491 f32 := float32(f64)
4492 f64n := float64(f32)
4493 if !(math.IsNaN(f64n) || math.IsInf(f64n, 0)) {
4494 bv := strconv.AppendFloat(nil, f64n, 'E', prec, 32)
4495 if fs, err := parseFloat32_custom(bv); err != nil || fs != f32 {
4496 t.Logf("float32 -> float32 error (prec: %v) parsing '%s', got %v, expected %v: %v", prec, bv, fs, f32, err)
4497 t.FailNow()
4498 }
4499 }
4500 }
4501 }
4502
4503 for _, v := range testUintsToParse {
4504 const prec int = 32 // test with precisions of 32 so formatting doesn't truncate what doesn't fit into float
4505 v = uint64(float64(v)) // use a value that when converted back and forth, remains the same
4506 bv := strconv.AppendFloat(nil, float64(v), 'E', prec, 64)
4507 r := readFloat(bv, fi64u)
4508 if r.ok {
4509 if fs, fail := parseUint64_reader(r); fail || fs != v {
4510 t.Logf("uint64 -> uint64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fs, v)
4511 t.FailNow()
4512 }
4513 } else {
4514 t.Logf("uint64 -> uint64 not ok (prec: %v) parsing '%s', expected %v", prec, bv, v)
4515 t.FailNow()
4516 }
4517 vi := int64(v)
4518 bv = strconv.AppendFloat(nil, float64(vi), 'E', prec, 64)
4519 r = readFloat(bv, fi64u)
4520 if r.ok {
4521 if fs, fail := parseInt64_reader(r); fail || fs != vi {
4522 t.Logf("int64 -> int64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fs, vi)
4523 t.FailNow()
4524 }
4525 } else {
4526 t.Logf("int64 -> int64 not ok (prec: %v) parsing '%s', expected %v", prec, bv, vi)
4527 t.FailNow()
4528 }
4529 }
4530 {
4531 var f64 float64 = 64.0
4532 var f32 float32 = 32.0
4533 var i int = 11
4534 var u64 uint64 = 128
4535
4536 for _, s := range []string{`""`, `null`} {
4537 b := []byte(s)
4538 testUnmarshalErr(&f64, b, h, t, "")
4539 testDeepEqualErr(f64, float64(0), t, "")
4540 testUnmarshalErr(&f32, b, h, t, "")
4541 testDeepEqualErr(f32, float32(0), t, "")
4542 testUnmarshalErr(&i, b, h, t, "")
4543 testDeepEqualErr(i, int(0), t, "")
4544 testUnmarshalErr(&u64, b, h, t, "")
4545 testDeepEqualErr(u64, uint64(0), t, "")
4546 testUnmarshalErr(&u64, b, h, t, "")
4547 testDeepEqualErr(u64, uint64(0), t, "")
4548 }
4549 }
4550 }
4551
4552 func doTestMsgpackDecodeMapAndExtSizeMismatch(t *testing.T, h Handle) {
4553 if !testRecoverPanicToErr {
4554 t.Skip(testSkipIfNotRecoverPanicToErrMsg)
4555 }
4556 defer testSetup(t, &h)()
4557 fn := func(t *testing.T, b []byte, v interface{}) {
4558 if err := NewDecoderBytes(b, h).Decode(v); err != io.EOF && err != io.ErrUnexpectedEOF {
4559 t.Fatalf("expected EOF or ErrUnexpectedEOF, got %v", err)
4560 }
4561 }
4562
4563 // a map claiming to have 0x10eeeeee KV pairs, but only has 1.
4564 var b = []byte{0xdf, 0x10, 0xee, 0xee, 0xee, 0x1, 0xa1, 0x1}
4565 var m1 map[int]string
4566 var m2 map[int][]byte
4567 fn(t, b, &m1)
4568 fn(t, b, &m2)
4569
4570 // an extension claiming to have 0x7fffffff bytes, but only has 1.
4571 b = []byte{0xc9, 0x7f, 0xff, 0xff, 0xff, 0xda, 0x1}
4572 var a interface{}
4573 fn(t, b, &a)
4574
4575 // b = []byte{0x00}
4576 // var s testSelferRecur
4577 // fn(t, b, &s)
4578 }
4579
4580 func TestMapRangeIndex(t *testing.T) {
4581 defer testSetup(t, nil)()
4582 // t.Skip()
4583 type T struct {
4584 I int
4585 S string
4586 B bool
4587 M map[int]T
4588 }
4589
4590 t1 := T{I: 1, B: true, S: "11", M: map[int]T{11: T{I: 11}}}
4591 t2 := T{I: 1, B: true, S: "12", M: map[int]T{12: T{I: 12}}}
4592
4593 // ------
4594
4595 var m1 = map[string]*T{
4596 "11": &t1,
4597 "12": &t2,
4598 }
4599 var m1c = make(map[string]T)
4600 for k, v := range m1 {
4601 m1c[k] = *v
4602 }
4603
4604 fnrv := func(r1, r2 reflect.Value) reflect.Value {
4605 if r1.IsValid() {
4606 return r1
4607 }
4608 return r2
4609 }
4610
4611 // var vx reflect.Value
4612
4613 mt := reflect.TypeOf(m1)
4614 rvk := mapAddrLoopvarRV(mt.Key(), mt.Key().Kind())
4615 rvv := mapAddrLoopvarRV(mt.Elem(), mt.Elem().Kind())
4616 var it mapIter
4617 mapRange(&it, reflect.ValueOf(m1), rvk, rvv, true)
4618 for it.Next() {
4619 k := fnrv(it.Key(), rvk).Interface().(string)
4620 v := fnrv(it.Value(), rvv).Interface().(*T)
4621 testDeepEqualErr(m1[k], v, t, "map-key-eq-it-key")
4622 if _, ok := m1c[k]; ok {
4623 delete(m1c, k)
4624 } else {
4625 t.Logf("unexpected key in map: %v", k)
4626 t.FailNow()
4627 }
4628 }
4629 it.Done()
4630 testDeepEqualErr(len(m1c), 0, t, "all-keys-not-consumed")
4631
4632 // ------
4633
4634 var m2 = map[*T]T{
4635 &t1: t1,
4636 &t2: t2,
4637 }
4638 var m2c = make(map[*T]*T)
4639 for k := range m2 {
4640 m2c[k] = k
4641 }
4642
4643 mt = reflect.TypeOf(m2)
4644 rvk = mapAddrLoopvarRV(mt.Key(), mt.Key().Kind())
4645 rvv = mapAddrLoopvarRV(mt.Elem(), mt.Elem().Kind())
4646 it = mapIter{} // zero it out first, before calling mapRange
4647 mapRange(&it, reflect.ValueOf(m2), rvk, rvv, true)
4648 for it.Next() {
4649 k := fnrv(it.Key(), rvk).Interface().(*T)
4650 v := fnrv(it.Value(), rvv).Interface().(T)
4651 testDeepEqualErr(m2[k], v, t, "map-key-eq-it-key")
4652 if _, ok := m2c[k]; ok {
4653 delete(m2c, k)
4654 } else {
4655 t.Logf("unexpected key in map: %v", k)
4656 t.FailNow()
4657 }
4658 }
4659 it.Done()
4660 testDeepEqualErr(len(m2c), 0, t, "all-keys-not-consumed")
4661
4662 // ---- test mapGet
4663
4664 fnTestMapIndex := func(mi ...interface{}) {
4665 for _, m0 := range mi {
4666 m := reflect.ValueOf(m0)
4667 mkt := m.Type().Key()
4668 mvt := m.Type().Elem()
4669 kfast := mapKeyFastKindFor(mkt.Kind())
4670 rvv := mapAddrLoopvarRV(mvt, mvt.Kind())
4671 visindirect := mapStoresElemIndirect(mvt.Size())
4672 visref := refBitset.isset(byte(mvt.Kind()))
4673
4674 for _, k := range m.MapKeys() {
4675 mg := mapGet(m, k, rvv, kfast, visindirect, visref).Interface()
4676 testDeepEqualErr(m.MapIndex(k).Interface(), mg, t, "map-index-eq")
4677 }
4678 }
4679 }
4680
4681 fnTestMapIndex(m1, m1c, m2, m2c)
4682
4683 // var s string = "hello"
4684 // var tt = &T{I: 3}
4685 // ttTyp := reflect.TypeOf(tt)
4686 // _, _ = tt, ttTyp
4687 // mv := reflect.ValueOf(m)
4688 // it := mapRange(mv, reflect.ValueOf(&s).Elem(), reflect.ValueOf(&tt).Elem(), true) //ok
4689 // it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(reflect.TypeOf(T{})).Elem(), true) // ok
4690 // it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(ttTyp.Elem()), true) // !ok
4691 // it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(reflect.TypeOf(T{})), true) !ok
4692 // it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(reflect.TypeOf(T{})).Elem(), true) // ok
4693
4694 // fmt.Printf("key: %#v\n", it.Key())
4695 // fmt.Printf("exp: %#v\n", mv.MapIndex(it.Key()))
4696 // fmt.Printf("val: %#v\n", it.Value())
4697 // testDeepEqualErr(mv.MapIndex(it.Key()), it.Value().Interface()
4698 }
4699
4700 // ----------
4701
4702 func TestBincCodecsTable(t *testing.T) {
4703 doTestCodecTableOne(t, testBincH)
4704 }
4705
4706 func TestBincCodecsMisc(t *testing.T) {
4707 doTestCodecMiscOne(t, testBincH)
4708 }
4709
4710 func TestBincCodecsEmbeddedPointer(t *testing.T) {
4711 doTestCodecEmbeddedPointer(t, testBincH)
4712 }
4713
4714 func TestBincStdEncIntf(t *testing.T) {
4715 doTestStdEncIntf(t, testBincH)
4716 }
4717
4718 func TestSimpleCodecsTable(t *testing.T) {
4719 doTestCodecTableOne(t, testSimpleH)
4720 }
4721
4722 func TestSimpleCodecsMisc(t *testing.T) {
4723 doTestCodecMiscOne(t, testSimpleH)
4724 }
4725
4726 func TestSimpleCodecsEmbeddedPointer(t *testing.T) {
4727 doTestCodecEmbeddedPointer(t, testSimpleH)
4728 }
4729
4730 func TestSimpleStdEncIntf(t *testing.T) {
4731 doTestStdEncIntf(t, testSimpleH)
4732 }
4733
4734 func TestMsgpackCodecsTable(t *testing.T) {
4735 doTestCodecTableOne(t, testMsgpackH)
4736 }
4737
4738 func TestMsgpackCodecsMisc(t *testing.T) {
4739 doTestCodecMiscOne(t, testMsgpackH)
4740 }
4741
4742 func TestMsgpackCodecsEmbeddedPointer(t *testing.T) {
4743 doTestCodecEmbeddedPointer(t, testMsgpackH)
4744 }
4745
4746 func TestMsgpackStdEncIntf(t *testing.T) {
4747 doTestStdEncIntf(t, testMsgpackH)
4748 }
4749
4750 func TestCborCodecsTable(t *testing.T) {
4751 doTestCodecTableOne(t, testCborH)
4752 }
4753
4754 func TestCborCodecsMisc(t *testing.T) {
4755 doTestCodecMiscOne(t, testCborH)
4756 }
4757
4758 func TestCborCodecsEmbeddedPointer(t *testing.T) {
4759 doTestCodecEmbeddedPointer(t, testCborH)
4760 }
4761
4762 func TestCborCodecChan(t *testing.T) {
4763 doTestCodecChan(t, testCborH)
4764 }
4765
4766 func TestCborStdEncIntf(t *testing.T) {
4767 doTestStdEncIntf(t, testCborH)
4768 }
4769
4770 func TestJsonCodecsTable(t *testing.T) {
4771 doTestCodecTableOne(t, testJsonH)
4772 }
4773
4774 func TestJsonCodecsMisc(t *testing.T) {
4775 doTestCodecMiscOne(t, testJsonH)
4776 }
4777
4778 func TestJsonCodecsEmbeddedPointer(t *testing.T) {
4779 doTestCodecEmbeddedPointer(t, testJsonH)
4780 }
4781
4782 func TestJsonCodecChan(t *testing.T) {
4783 doTestCodecChan(t, testJsonH)
4784 }
4785
4786 func TestJsonStdEncIntf(t *testing.T) {
4787 doTestStdEncIntf(t, testJsonH)
4788 }
4789
4790 // ----- Raw ---------
4791 func TestJsonRaw(t *testing.T) {
4792 doTestRawValue(t, testJsonH)
4793 }
4794 func TestBincRaw(t *testing.T) {
4795 doTestRawValue(t, testBincH)
4796 }
4797 func TestMsgpackRaw(t *testing.T) {
4798 doTestRawValue(t, testMsgpackH)
4799 }
4800 func TestSimpleRaw(t *testing.T) {
4801 doTestRawValue(t, testSimpleH)
4802 }
4803 func TestCborRaw(t *testing.T) {
4804 doTestRawValue(t, testCborH)
4805 }
4806
4807 // ----- ALL (framework based) -----
4808
4809 func TestAllEncCircularRef(t *testing.T) {
4810 doTestEncCircularRef(t, testCborH)
4811 }
4812
4813 func TestAllAnonCycle(t *testing.T) {
4814 doTestAnonCycle(t, testCborH)
4815 }
4816
4817 func TestAllErrWriter(t *testing.T) {
4818 doTestAllErrWriter(t, testCborH, testJsonH)
4819 }
4820
4821 // ----- RPC custom -----
4822
4823 func TestMsgpackRpcSpec(t *testing.T) {
4824 doTestCodecRpcOne(t, MsgpackSpecRpc, testMsgpackH, true, 0)
4825 }
4826
4827 // ----- RPC -----
4828
4829 func TestBincRpcGo(t *testing.T) {
4830 doTestCodecRpcOne(t, GoRpc, testBincH, true, 0)
4831 }
4832
4833 func TestSimpleRpcGo(t *testing.T) {
4834 doTestCodecRpcOne(t, GoRpc, testSimpleH, true, 0)
4835 }
4836
4837 func TestMsgpackRpcGo(t *testing.T) {
4838 doTestCodecRpcOne(t, GoRpc, testMsgpackH, true, 0)
4839 }
4840
4841 func TestCborRpcGo(t *testing.T) {
4842 doTestCodecRpcOne(t, GoRpc, testCborH, true, 0)
4843 }
4844
4845 func TestJsonRpcGo(t *testing.T) {
4846 doTestCodecRpcOne(t, GoRpc, testJsonH, true, 0)
4847 }
4848
4849 // ----- OTHERS -----
4850
4851 func TestBincMapEncodeForCanonical(t *testing.T) {
4852 t.Skipf("skipping ... needs investigation") // MARKER: testing fails??? Need to research
4853 doTestMapEncodeForCanonical(t, testBincH)
4854 }
4855
4856 func TestSimpleMapEncodeForCanonical(t *testing.T) {
4857 doTestMapEncodeForCanonical(t, testSimpleH)
4858 }
4859
4860 func TestMsgpackMapEncodeForCanonical(t *testing.T) {
4861 doTestMapEncodeForCanonical(t, testMsgpackH)
4862 }
4863
4864 func TestCborMapEncodeForCanonical(t *testing.T) {
4865 doTestMapEncodeForCanonical(t, testCborH)
4866 }
4867
4868 func TestJsonMapEncodeForCanonical(t *testing.T) {
4869 doTestMapEncodeForCanonical(t, testJsonH)
4870 }
4871
4872 func TestBincUnderlyingType(t *testing.T) {
4873 testCodecUnderlyingType(t, testBincH)
4874 }
4875
4876 func TestJsonSwallowAndZero(t *testing.T) {
4877 doTestSwallowAndZero(t, testJsonH)
4878 }
4879
4880 func TestCborSwallowAndZero(t *testing.T) {
4881 doTestSwallowAndZero(t, testCborH)
4882 }
4883
4884 func TestMsgpackSwallowAndZero(t *testing.T) {
4885 doTestSwallowAndZero(t, testMsgpackH)
4886 }
4887
4888 func TestBincSwallowAndZero(t *testing.T) {
4889 doTestSwallowAndZero(t, testBincH)
4890 }
4891
4892 func TestSimpleSwallowAndZero(t *testing.T) {
4893 doTestSwallowAndZero(t, testSimpleH)
4894 }
4895
4896 func TestJsonRawExt(t *testing.T) {
4897 doTestRawExt(t, testJsonH)
4898 }
4899
4900 func TestCborRawExt(t *testing.T) {
4901 doTestRawExt(t, testCborH)
4902 }
4903
4904 func TestMsgpackRawExt(t *testing.T) {
4905 doTestRawExt(t, testMsgpackH)
4906 }
4907
4908 func TestBincRawExt(t *testing.T) {
4909 doTestRawExt(t, testBincH)
4910 }
4911
4912 func TestSimpleRawExt(t *testing.T) {
4913 doTestRawExt(t, testSimpleH)
4914 }
4915
4916 func TestJsonMapStructKey(t *testing.T) {
4917 doTestMapStructKey(t, testJsonH)
4918 }
4919
4920 func TestCborMapStructKey(t *testing.T) {
4921 doTestMapStructKey(t, testCborH)
4922 }
4923
4924 func TestMsgpackMapStructKey(t *testing.T) {
4925 doTestMapStructKey(t, testMsgpackH)
4926 }
4927
4928 func TestBincMapStructKey(t *testing.T) {
4929 doTestMapStructKey(t, testBincH)
4930 }
4931
4932 func TestSimpleMapStructKey(t *testing.T) {
4933 doTestMapStructKey(t, testSimpleH)
4934 }
4935
4936 func TestJsonDecodeNilMapValue(t *testing.T) {
4937 doTestDecodeNilMapValue(t, testJsonH)
4938 }
4939
4940 func TestCborDecodeNilMapValue(t *testing.T) {
4941 doTestDecodeNilMapValue(t, testCborH)
4942 }
4943
4944 func TestMsgpackDecodeNilMapValue(t *testing.T) {
4945 doTestDecodeNilMapValue(t, testMsgpackH)
4946 }
4947
4948 func TestBincDecodeNilMapValue(t *testing.T) {
4949 doTestDecodeNilMapValue(t, testBincH)
4950 }
4951
4952 func TestSimpleDecodeNilMapValue(t *testing.T) {
4953 doTestDecodeNilMapValue(t, testSimpleH)
4954 }
4955
4956 func TestJsonEmbeddedFieldPrecedence(t *testing.T) {
4957 doTestEmbeddedFieldPrecedence(t, testJsonH)
4958 }
4959
4960 func TestCborEmbeddedFieldPrecedence(t *testing.T) {
4961 doTestEmbeddedFieldPrecedence(t, testCborH)
4962 }
4963
4964 func TestMsgpackEmbeddedFieldPrecedence(t *testing.T) {
4965 doTestEmbeddedFieldPrecedence(t, testMsgpackH)
4966 }
4967
4968 func TestBincEmbeddedFieldPrecedence(t *testing.T) {
4969 doTestEmbeddedFieldPrecedence(t, testBincH)
4970 }
4971
4972 func TestSimpleEmbeddedFieldPrecedence(t *testing.T) {
4973 doTestEmbeddedFieldPrecedence(t, testSimpleH)
4974 }
4975
4976 func TestJsonLargeContainerLen(t *testing.T) {
4977 doTestLargeContainerLen(t, testJsonH)
4978 }
4979
4980 func TestCborLargeContainerLen(t *testing.T) {
4981 doTestLargeContainerLen(t, testCborH)
4982 }
4983
4984 func TestMsgpackLargeContainerLen(t *testing.T) {
4985 doTestLargeContainerLen(t, testMsgpackH)
4986 }
4987
4988 func TestBincLargeContainerLen(t *testing.T) {
4989 doTestLargeContainerLen(t, testBincH)
4990 }
4991
4992 func TestSimpleLargeContainerLen(t *testing.T) {
4993 doTestLargeContainerLen(t, testSimpleH)
4994 }
4995
4996 func TestJsonTime(t *testing.T) {
4997 doTestTime(t, testJsonH)
4998 }
4999
5000 func TestCborTime(t *testing.T) {
5001 doTestTime(t, testCborH)
5002 }
5003
5004 func TestMsgpackTime(t *testing.T) {
5005 doTestTime(t, testMsgpackH)
5006 }
5007
5008 func TestBincTime(t *testing.T) {
5009 doTestTime(t, testBincH)
5010 }
5011
5012 func TestSimpleTime(t *testing.T) {
5013 doTestTime(t, testSimpleH)
5014 }
5015
5016 func TestJsonUintToInt(t *testing.T) {
5017 doTestUintToInt(t, testJsonH)
5018 }
5019
5020 func TestCborUintToInt(t *testing.T) {
5021 doTestUintToInt(t, testCborH)
5022 }
5023
5024 func TestMsgpackUintToInt(t *testing.T) {
5025 doTestUintToInt(t, testMsgpackH)
5026 }
5027
5028 func TestBincUintToInt(t *testing.T) {
5029 doTestUintToInt(t, testBincH)
5030 }
5031
5032 func TestSimpleUintToInt(t *testing.T) {
5033 doTestUintToInt(t, testSimpleH)
5034 }
5035
5036 func TestJsonDifferentMapOrSliceType(t *testing.T) {
5037 doTestDifferentMapOrSliceType(t, testJsonH)
5038 }
5039
5040 func TestCborDifferentMapOrSliceType(t *testing.T) {
5041 doTestDifferentMapOrSliceType(t, testCborH)
5042 }
5043
5044 func TestMsgpackDifferentMapOrSliceType(t *testing.T) {
5045 doTestDifferentMapOrSliceType(t, testMsgpackH)
5046 }
5047
5048 func TestBincDifferentMapOrSliceType(t *testing.T) {
5049 doTestDifferentMapOrSliceType(t, testBincH)
5050 }
5051
5052 func TestSimpleDifferentMapOrSliceType(t *testing.T) {
5053 doTestDifferentMapOrSliceType(t, testSimpleH)
5054 }
5055
5056 func TestJsonScalars(t *testing.T) {
5057 doTestScalars(t, testJsonH)
5058 }
5059
5060 func TestCborScalars(t *testing.T) {
5061 doTestScalars(t, testCborH)
5062 }
5063
5064 func TestMsgpackScalars(t *testing.T) {
5065 doTestScalars(t, testMsgpackH)
5066 }
5067
5068 func TestBincScalars(t *testing.T) {
5069 doTestScalars(t, testBincH)
5070 }
5071
5072 func TestSimpleScalars(t *testing.T) {
5073 doTestScalars(t, testSimpleH)
5074 }
5075
5076 func TestJsonOmitempty(t *testing.T) {
5077 doTestOmitempty(t, testJsonH)
5078 }
5079
5080 func TestCborOmitempty(t *testing.T) {
5081 doTestOmitempty(t, testCborH)
5082 }
5083
5084 func TestMsgpackOmitempty(t *testing.T) {
5085 doTestOmitempty(t, testMsgpackH)
5086 }
5087
5088 func TestBincOmitempty(t *testing.T) {
5089 doTestOmitempty(t, testBincH)
5090 }
5091
5092 func TestSimpleOmitempty(t *testing.T) {
5093 doTestOmitempty(t, testSimpleH)
5094 }
5095
5096 func TestJsonIntfMapping(t *testing.T) {
5097 doTestIntfMapping(t, testJsonH)
5098 }
5099
5100 func TestCborIntfMapping(t *testing.T) {
5101 doTestIntfMapping(t, testCborH)
5102 }
5103
5104 func TestMsgpackIntfMapping(t *testing.T) {
5105 doTestIntfMapping(t, testMsgpackH)
5106 }
5107
5108 func TestBincIntfMapping(t *testing.T) {
5109 doTestIntfMapping(t, testBincH)
5110 }
5111
5112 func TestSimpleIntfMapping(t *testing.T) {
5113 doTestIntfMapping(t, testSimpleH)
5114 }
5115
5116 func TestJsonMissingFields(t *testing.T) {
5117 doTestMissingFields(t, testJsonH)
5118 }
5119
5120 func TestCborMissingFields(t *testing.T) {
5121 doTestMissingFields(t, testCborH)
5122 }
5123
5124 func TestMsgpackMissingFields(t *testing.T) {
5125 doTestMissingFields(t, testMsgpackH)
5126 }
5127
5128 func TestBincMissingFields(t *testing.T) {
5129 doTestMissingFields(t, testBincH)
5130 }
5131
5132 func TestSimpleMissingFields(t *testing.T) {
5133 doTestMissingFields(t, testSimpleH)
5134 }
5135
5136 func TestJsonMaxDepth(t *testing.T) {
5137 doTestMaxDepth(t, testJsonH)
5138 }
5139
5140 func TestCborMaxDepth(t *testing.T) {
5141 doTestMaxDepth(t, testCborH)
5142 }
5143
5144 func TestMsgpackMaxDepth(t *testing.T) {
5145 doTestMaxDepth(t, testMsgpackH)
5146 }
5147
5148 func TestBincMaxDepth(t *testing.T) {
5149 doTestMaxDepth(t, testBincH)
5150 }
5151
5152 func TestSimpleMaxDepth(t *testing.T) {
5153 doTestMaxDepth(t, testSimpleH)
5154 }
5155
5156 func TestJsonSelfExt(t *testing.T) {
5157 doTestSelfExt(t, testJsonH)
5158 }
5159
5160 func TestCborSelfExt(t *testing.T) {
5161 doTestSelfExt(t, testCborH)
5162 }
5163
5164 func TestMsgpackSelfExt(t *testing.T) {
5165 doTestSelfExt(t, testMsgpackH)
5166 }
5167
5168 func TestBincSelfExt(t *testing.T) {
5169 doTestSelfExt(t, testBincH)
5170 }
5171
5172 func TestSimpleSelfExt(t *testing.T) {
5173 doTestSelfExt(t, testSimpleH)
5174 }
5175
5176 func TestJsonBytesEncodedAsArray(t *testing.T) {
5177 doTestBytesEncodedAsArray(t, testJsonH)
5178 }
5179
5180 func TestCborBytesEncodedAsArray(t *testing.T) {
5181 doTestBytesEncodedAsArray(t, testCborH)
5182 }
5183
5184 func TestMsgpackBytesEncodedAsArray(t *testing.T) {
5185 doTestBytesEncodedAsArray(t, testMsgpackH)
5186 }
5187
5188 func TestBincBytesEncodedAsArray(t *testing.T) {
5189 doTestBytesEncodedAsArray(t, testBincH)
5190 }
5191
5192 func TestSimpleBytesEncodedAsArray(t *testing.T) {
5193 doTestBytesEncodedAsArray(t, testSimpleH)
5194 }
5195
5196 func TestJsonStrucEncDec(t *testing.T) {
5197 doTestStrucEncDec(t, testJsonH)
5198 }
5199
5200 func TestCborStrucEncDec(t *testing.T) {
5201 doTestStrucEncDec(t, testCborH)
5202 }
5203
5204 func TestMsgpackStrucEncDec(t *testing.T) {
5205 doTestStrucEncDec(t, testMsgpackH)
5206 }
5207
5208 func TestBincStrucEncDec(t *testing.T) {
5209 doTestStrucEncDec(t, testBincH)
5210 }
5211
5212 func TestSimpleStrucEncDec(t *testing.T) {
5213 doTestStrucEncDec(t, testSimpleH)
5214 }
5215
5216 func TestJsonRawToStringToRawEtc(t *testing.T) {
5217 doTestRawToStringToRawEtc(t, testJsonH)
5218 }
5219
5220 func TestCborRawToStringToRawEtc(t *testing.T) {
5221 doTestRawToStringToRawEtc(t, testCborH)
5222 }
5223
5224 func TestMsgpackRawToStringToRawEtc(t *testing.T) {
5225 doTestRawToStringToRawEtc(t, testMsgpackH)
5226 }
5227
5228 func TestBincRawToStringToRawEtc(t *testing.T) {
5229 doTestRawToStringToRawEtc(t, testBincH)
5230 }
5231
5232 func TestSimpleRawToStringToRawEtc(t *testing.T) {
5233 doTestRawToStringToRawEtc(t, testSimpleH)
5234 }
5235
5236 func TestJsonStructKeyType(t *testing.T) {
5237 doTestStructKeyType(t, testJsonH)
5238 }
5239
5240 func TestCborStructKeyType(t *testing.T) {
5241 doTestStructKeyType(t, testCborH)
5242 }
5243
5244 func TestMsgpackStructKeyType(t *testing.T) {
5245 doTestStructKeyType(t, testMsgpackH)
5246 }
5247
5248 func TestBincStructKeyType(t *testing.T) {
5249 doTestStructKeyType(t, testBincH)
5250 }
5251
5252 func TestSimpleStructKeyType(t *testing.T) {
5253 doTestStructKeyType(t, testSimpleH)
5254 }
5255
5256 func TestJsonPreferArrayOverSlice(t *testing.T) {
5257 doTestPreferArrayOverSlice(t, testJsonH)
5258 }
5259
5260 func TestCborPreferArrayOverSlice(t *testing.T) {
5261 doTestPreferArrayOverSlice(t, testCborH)
5262 }
5263
5264 func TestMsgpackPreferArrayOverSlice(t *testing.T) {
5265 doTestPreferArrayOverSlice(t, testMsgpackH)
5266 }
5267
5268 func TestBincPreferArrayOverSlice(t *testing.T) {
5269 doTestPreferArrayOverSlice(t, testBincH)
5270 }
5271
5272 func TestSimplePreferArrayOverSlice(t *testing.T) {
5273 doTestPreferArrayOverSlice(t, testSimpleH)
5274 }
5275
5276 func TestJsonZeroCopyBytes(t *testing.T) {
5277 doTestZeroCopyBytes(t, testJsonH)
5278 }
5279
5280 func TestCborZeroCopyBytes(t *testing.T) {
5281 doTestZeroCopyBytes(t, testCborH)
5282 }
5283
5284 func TestMsgpackZeroCopyBytes(t *testing.T) {
5285 doTestZeroCopyBytes(t, testMsgpackH)
5286 }
5287
5288 func TestBincZeroCopyBytes(t *testing.T) {
5289 doTestZeroCopyBytes(t, testBincH)
5290 }
5291
5292 func TestSimpleZeroCopyBytes(t *testing.T) {
5293 doTestZeroCopyBytes(t, testSimpleH)
5294 }
5295
5296 func TestJsonNextValueBytes(t *testing.T) {
5297 doTestNextValueBytes(t, testJsonH)
5298 }
5299
5300 func TestCborNextValueBytes(t *testing.T) {
5301 // x := testCborH.IndefiniteLength
5302 // defer func() { testCborH.IndefiniteLength = x }()
5303
5304 // xdebugf(">>>>> TestCborNextValueBytes: IndefiniteLength = false")
5305 // testCborH.IndefiniteLength = false
5306 // doTestNextValueBytes(t, testCborH)
5307 // xdebugf(">>>>> TestCborNextValueBytes: IndefiniteLength = true")
5308 // testCborH.IndefiniteLength = true
5309 doTestNextValueBytes(t, testCborH)
5310 }
5311
5312 func TestMsgpackNextValueBytes(t *testing.T) {
5313 doTestNextValueBytes(t, testMsgpackH)
5314 }
5315
5316 func TestBincNextValueBytes(t *testing.T) {
5317 doTestNextValueBytes(t, testBincH)
5318 }
5319
5320 func TestSimpleNextValueBytes(t *testing.T) {
5321 doTestNextValueBytes(t, testSimpleH)
5322 }
5323
5324 func TestJsonNumbers(t *testing.T) {
5325 doTestNumbers(t, testJsonH)
5326 }
5327
5328 func TestCborNumbers(t *testing.T) {
5329 doTestNumbers(t, testCborH)
5330 }
5331
5332 func TestMsgpackNumbers(t *testing.T) {
5333 doTestNumbers(t, testMsgpackH)
5334 }
5335
5336 func TestBincNumbers(t *testing.T) {
5337 doTestNumbers(t, testBincH)
5338 }
5339
5340 func TestSimpleNumbers(t *testing.T) {
5341 doTestNumbers(t, testSimpleH)
5342 }
5343
5344 func TestJsonDesc(t *testing.T) {
5345 doTestDesc(t, testJsonH, map[byte]string{'"': `"`, '{': `{`, '}': `}`, '[': `[`, ']': `]`})
5346 }
5347
5348 func TestCborDesc(t *testing.T) {
5349 m := make(map[byte]string)
5350 for k, v := range cbordescMajorNames {
5351 // if k == cborMajorSimpleOrFloat { m[k<<5] = "nil" }
5352 m[k<<5] = v
5353 }
5354 for k, v := range cbordescSimpleNames {
5355 m[k] = v
5356 }
5357 delete(m, cborMajorSimpleOrFloat<<5)
5358 doTestDesc(t, testCborH, m)
5359 }
5360
5361 func TestMsgpackDesc(t *testing.T) {
5362 m := make(map[byte]string)
5363 for k, v := range mpdescNames {
5364 m[k] = v
5365 }
5366 m[mpPosFixNumMin] = "int"
5367 m[mpFixStrMin] = "string|bytes"
5368 m[mpFixArrayMin] = "array"
5369 m[mpFixMapMin] = "map"
5370 m[mpFixExt1] = "ext"
5371
5372 doTestDesc(t, testMsgpackH, m)
5373 }
5374
5375 func TestBincDesc(t *testing.T) {
5376 m := make(map[byte]string)
5377 for k, v := range bincdescVdNames {
5378 m[k<<4] = v
5379 }
5380 for k, v := range bincdescSpecialVsNames {
5381 m[k] = v
5382 }
5383 delete(m, bincVdSpecial<<4)
5384 doTestDesc(t, testBincH, m)
5385 }
5386
5387 func TestSimpleDesc(t *testing.T) {
5388 doTestDesc(t, testSimpleH, simpledescNames)
5389 }
5390
5391 func TestJsonStructFieldInfoToArray(t *testing.T) {
5392 doTestStructFieldInfoToArray(t, testJsonH)
5393 }
5394
5395 func TestCborStructFieldInfoToArray(t *testing.T) {
5396 doTestStructFieldInfoToArray(t, testCborH)
5397 }
5398
5399 func TestMsgpackStructFieldInfoToArray(t *testing.T) {
5400 doTestStructFieldInfoToArray(t, testMsgpackH)
5401 }
5402
5403 func TestBincStructFieldInfoToArray(t *testing.T) {
5404 doTestStructFieldInfoToArray(t, testBincH)
5405 }
5406
5407 func TestSimpleStructFieldInfoToArray(t *testing.T) {
5408 doTestStructFieldInfoToArray(t, testSimpleH)
5409 }
5410
5411 // --------
5412
5413 func TestMultipleEncDec(t *testing.T) {
5414 doTestMultipleEncDec(t, testJsonH)
5415 }
5416
5417 func TestJsonEncodeIndent(t *testing.T) {
5418 doTestJsonEncodeIndent(t, testJsonH)
5419 }
5420
5421 func TestJsonDecodeNonStringScalarInStringContext(t *testing.T) {
5422 doTestJsonDecodeNonStringScalarInStringContext(t, testJsonH)
5423 }
5424
5425 func TestJsonLargeInteger(t *testing.T) {
5426 doTestJsonLargeInteger(t, testJsonH)
5427 }
5428
5429 func TestJsonInvalidUnicode(t *testing.T) {
5430 doTestJsonInvalidUnicode(t, testJsonH)
5431 }
5432
5433 func TestJsonNumberParsing(t *testing.T) {
5434 doTestJsonNumberParsing(t, testJsonH)
5435 }
5436
5437 func TestMsgpackDecodeMapAndExtSizeMismatch(t *testing.T) {
5438 doTestMsgpackDecodeMapAndExtSizeMismatch(t, testMsgpackH)
5439 }