codec_test.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. // Test works by using a slice of interfaces.
  5. // It can test for encoding/decoding into/from a nil interface{}
  6. // or passing the object to encode/decode into.
  7. //
  8. // There are basically 2 main tests here.
  9. // First test internally encodes and decodes things and verifies that
  10. // the artifact was as expected.
  11. // Second test will use python msgpack to create a bunch of golden files,
  12. // read those files, and compare them to what it should be. It then
  13. // writes those files back out and compares the byte streams.
  14. //
  15. // Taken together, the tests are pretty extensive.
  16. //
  17. // The following manual tests must be done:
  18. // - TestCodecUnderlyingType
  19. // - Set fastpathEnabled to false and run tests (to ensure that regular reflection works).
  20. // We don't want to use a variable there so that code is ellided.
  21. import (
  22. "bytes"
  23. "encoding/gob"
  24. "flag"
  25. "fmt"
  26. "io/ioutil"
  27. "math"
  28. "math/rand"
  29. "net"
  30. "net/rpc"
  31. "os"
  32. "os/exec"
  33. "path/filepath"
  34. "reflect"
  35. "runtime"
  36. "strconv"
  37. "sync/atomic"
  38. "testing"
  39. "time"
  40. )
  41. func init() {
  42. testInitFlags()
  43. testPreInitFns = append(testPreInitFns, testInit)
  44. }
  45. type testVerifyArg int
  46. const (
  47. testVerifyMapTypeSame testVerifyArg = iota
  48. testVerifyMapTypeStrIntf
  49. testVerifyMapTypeIntfIntf
  50. // testVerifySliceIntf
  51. testVerifyForPython
  52. )
  53. const testSkipRPCTests = false
  54. var (
  55. testVerbose bool
  56. testInitDebug bool
  57. testUseIoEncDec bool
  58. testStructToArray bool
  59. testCanonical bool
  60. testUseReset bool
  61. testWriteNoSymbols bool
  62. testSkipIntf bool
  63. testInternStr bool
  64. testUseMust bool
  65. skipVerifyVal interface{} = &(struct{}{})
  66. testMapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
  67. // For Go Time, do not use a descriptive timezone.
  68. // It's unnecessary, and makes it harder to do a reflect.DeepEqual.
  69. // The Offset already tells what the offset should be, if not on UTC and unknown zone name.
  70. timeLoc = time.FixedZone("", -8*60*60) // UTC-08:00 //time.UTC-8
  71. timeToCompare1 = time.Date(2012, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
  72. timeToCompare2 = time.Date(1900, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
  73. timeToCompare3 = time.Unix(0, 270).UTC() // use value that must be encoded as uint64 for nanoseconds (for cbor/msgpack comparison)
  74. //timeToCompare4 = time.Time{}.UTC() // does not work well with simple cbor time encoding (overflow)
  75. timeToCompare4 = time.Unix(-2013855848, 4223).UTC()
  76. table []interface{} // main items we encode
  77. tableVerify []interface{} // we verify encoded things against this after decode
  78. tableTestNilVerify []interface{} // for nil interface, use this to verify (rules are different)
  79. tablePythonVerify []interface{} // for verifying for python, since Python sometimes
  80. // will encode a float32 as float64, or large int as uint
  81. testRpcInt = new(TestRpcInt)
  82. )
  83. func testInitFlags() {
  84. // delete(testDecOpts.ExtFuncs, timeTyp)
  85. flag.BoolVar(&testVerbose, "tv", false, "Test Verbose")
  86. flag.BoolVar(&testInitDebug, "tg", false, "Test Init Debug")
  87. flag.BoolVar(&testUseIoEncDec, "ti", false, "Use IO Reader/Writer for Marshal/Unmarshal")
  88. flag.BoolVar(&testStructToArray, "ts", false, "Set StructToArray option")
  89. flag.BoolVar(&testWriteNoSymbols, "tn", false, "Set NoSymbols option")
  90. flag.BoolVar(&testCanonical, "tc", false, "Set Canonical option")
  91. flag.BoolVar(&testInternStr, "te", false, "Set InternStr option")
  92. flag.BoolVar(&testSkipIntf, "tf", false, "Skip Interfaces")
  93. flag.BoolVar(&testUseReset, "tr", false, "Use Reset")
  94. flag.BoolVar(&testUseMust, "tm", true, "Use Must(En|De)code")
  95. }
  96. func testByteBuf(in []byte) *bytes.Buffer {
  97. return bytes.NewBuffer(in)
  98. }
  99. type TestABC struct {
  100. A, B, C string
  101. }
  102. type TestRpcInt struct {
  103. i int
  104. }
  105. func (r *TestRpcInt) Update(n int, res *int) error { r.i = n; *res = r.i; return nil }
  106. func (r *TestRpcInt) Square(ignore int, res *int) error { *res = r.i * r.i; return nil }
  107. func (r *TestRpcInt) Mult(n int, res *int) error { *res = r.i * n; return nil }
  108. func (r *TestRpcInt) EchoStruct(arg TestABC, res *string) error {
  109. *res = fmt.Sprintf("%#v", arg)
  110. return nil
  111. }
  112. func (r *TestRpcInt) Echo123(args []string, res *string) error {
  113. *res = fmt.Sprintf("%#v", args)
  114. return nil
  115. }
  116. type testUnixNanoTimeExt struct {
  117. // keep timestamp here, so that do not incur interface-conversion costs
  118. ts int64
  119. }
  120. // func (x *testUnixNanoTimeExt) WriteExt(interface{}) []byte { panic("unsupported") }
  121. // func (x *testUnixNanoTimeExt) ReadExt(interface{}, []byte) { panic("unsupported") }
  122. func (x *testUnixNanoTimeExt) ConvertExt(v interface{}) interface{} {
  123. switch v2 := v.(type) {
  124. case time.Time:
  125. x.ts = v2.UTC().UnixNano()
  126. case *time.Time:
  127. x.ts = v2.UTC().UnixNano()
  128. default:
  129. panic(fmt.Sprintf("unsupported format for time conversion: expecting time.Time; got %T", v))
  130. }
  131. return &x.ts
  132. }
  133. func (x *testUnixNanoTimeExt) UpdateExt(dest interface{}, v interface{}) {
  134. // fmt.Printf("testUnixNanoTimeExt.UpdateExt: v: %v\n", v)
  135. tt := dest.(*time.Time)
  136. switch v2 := v.(type) {
  137. case int64:
  138. *tt = time.Unix(0, v2).UTC()
  139. case *int64:
  140. *tt = time.Unix(0, *v2).UTC()
  141. case uint64:
  142. *tt = time.Unix(0, int64(v2)).UTC()
  143. case *uint64:
  144. *tt = time.Unix(0, int64(*v2)).UTC()
  145. //case float64:
  146. //case string:
  147. default:
  148. panic(fmt.Sprintf("unsupported format for time conversion: expecting int64/uint64; got %T", v))
  149. }
  150. // fmt.Printf("testUnixNanoTimeExt.UpdateExt: v: %v, tt: %#v\n", v, tt)
  151. }
  152. func testVerifyVal(v interface{}, arg testVerifyArg) (v2 interface{}) {
  153. //for python msgpack,
  154. // - all positive integers are unsigned 64-bit ints
  155. // - all floats are float64
  156. switch iv := v.(type) {
  157. case int8:
  158. if iv >= 0 {
  159. v2 = uint64(iv)
  160. } else {
  161. v2 = int64(iv)
  162. }
  163. case int16:
  164. if iv >= 0 {
  165. v2 = uint64(iv)
  166. } else {
  167. v2 = int64(iv)
  168. }
  169. case int32:
  170. if iv >= 0 {
  171. v2 = uint64(iv)
  172. } else {
  173. v2 = int64(iv)
  174. }
  175. case int64:
  176. if iv >= 0 {
  177. v2 = uint64(iv)
  178. } else {
  179. v2 = int64(iv)
  180. }
  181. case uint8:
  182. v2 = uint64(iv)
  183. case uint16:
  184. v2 = uint64(iv)
  185. case uint32:
  186. v2 = uint64(iv)
  187. case uint64:
  188. v2 = uint64(iv)
  189. case float32:
  190. v2 = float64(iv)
  191. case float64:
  192. v2 = float64(iv)
  193. case []interface{}:
  194. m2 := make([]interface{}, len(iv))
  195. for j, vj := range iv {
  196. m2[j] = testVerifyVal(vj, arg)
  197. }
  198. v2 = m2
  199. case map[string]bool:
  200. switch arg {
  201. case testVerifyMapTypeSame:
  202. m2 := make(map[string]bool)
  203. for kj, kv := range iv {
  204. m2[kj] = kv
  205. }
  206. v2 = m2
  207. case testVerifyMapTypeStrIntf, testVerifyForPython:
  208. m2 := make(map[string]interface{})
  209. for kj, kv := range iv {
  210. m2[kj] = kv
  211. }
  212. v2 = m2
  213. case testVerifyMapTypeIntfIntf:
  214. m2 := make(map[interface{}]interface{})
  215. for kj, kv := range iv {
  216. m2[kj] = kv
  217. }
  218. v2 = m2
  219. }
  220. case map[string]interface{}:
  221. switch arg {
  222. case testVerifyMapTypeSame:
  223. m2 := make(map[string]interface{})
  224. for kj, kv := range iv {
  225. m2[kj] = testVerifyVal(kv, arg)
  226. }
  227. v2 = m2
  228. case testVerifyMapTypeStrIntf, testVerifyForPython:
  229. m2 := make(map[string]interface{})
  230. for kj, kv := range iv {
  231. m2[kj] = testVerifyVal(kv, arg)
  232. }
  233. v2 = m2
  234. case testVerifyMapTypeIntfIntf:
  235. m2 := make(map[interface{}]interface{})
  236. for kj, kv := range iv {
  237. m2[kj] = testVerifyVal(kv, arg)
  238. }
  239. v2 = m2
  240. }
  241. case map[interface{}]interface{}:
  242. m2 := make(map[interface{}]interface{})
  243. for kj, kv := range iv {
  244. m2[testVerifyVal(kj, arg)] = testVerifyVal(kv, arg)
  245. }
  246. v2 = m2
  247. case time.Time:
  248. switch arg {
  249. case testVerifyForPython:
  250. if iv2 := iv.UnixNano(); iv2 >= 0 {
  251. v2 = uint64(iv2)
  252. } else {
  253. v2 = int64(iv2)
  254. }
  255. default:
  256. v2 = v
  257. }
  258. default:
  259. v2 = v
  260. }
  261. return
  262. }
  263. func testInit() {
  264. gob.Register(new(TestStruc))
  265. if testInitDebug {
  266. ts0 := newTestStruc(2, false, !testSkipIntf, false)
  267. fmt.Printf("====> depth: %v, ts: %#v\n", 2, ts0)
  268. }
  269. for _, v := range testHandles {
  270. bh := v.getBasicHandle()
  271. bh.InternString = testInternStr
  272. bh.Canonical = testCanonical
  273. bh.StructToArray = testStructToArray
  274. // mostly doing this for binc
  275. if testWriteNoSymbols {
  276. bh.AsSymbols = AsSymbolNone
  277. } else {
  278. bh.AsSymbols = AsSymbolAll
  279. }
  280. }
  281. testMsgpackH.RawToString = true
  282. // testMsgpackH.AddExt(byteSliceTyp, 0, testMsgpackH.BinaryEncodeExt, testMsgpackH.BinaryDecodeExt)
  283. // testMsgpackH.AddExt(timeTyp, 1, testMsgpackH.TimeEncodeExt, testMsgpackH.TimeDecodeExt)
  284. timeEncExt := func(rv reflect.Value) (bs []byte, err error) {
  285. defer panicToErr(&err)
  286. bs = timeExt{}.WriteExt(rv.Interface())
  287. return
  288. }
  289. timeDecExt := func(rv reflect.Value, bs []byte) (err error) {
  290. defer panicToErr(&err)
  291. timeExt{}.ReadExt(rv.Interface(), bs)
  292. return
  293. }
  294. // add extensions for msgpack, simple for time.Time, so we can encode/decode same way.
  295. // use different flavors of XXXExt calls, including deprecated ones.
  296. testSimpleH.AddExt(timeTyp, 1, timeEncExt, timeDecExt)
  297. testMsgpackH.SetBytesExt(timeTyp, 1, timeExt{})
  298. testCborH.SetInterfaceExt(timeTyp, 1, &testUnixNanoTimeExt{})
  299. testJsonH.SetInterfaceExt(timeTyp, 1, &testUnixNanoTimeExt{})
  300. primitives := []interface{}{
  301. int8(-8),
  302. int16(-1616),
  303. int32(-32323232),
  304. int64(-6464646464646464),
  305. uint8(192),
  306. uint16(1616),
  307. uint32(32323232),
  308. uint64(6464646464646464),
  309. byte(192),
  310. float32(-3232.0),
  311. float64(-6464646464.0),
  312. float32(3232.0),
  313. float64(6464646464.0),
  314. false,
  315. true,
  316. nil,
  317. "someday",
  318. "",
  319. "bytestring",
  320. timeToCompare1,
  321. timeToCompare2,
  322. timeToCompare3,
  323. timeToCompare4,
  324. }
  325. mapsAndStrucs := []interface{}{
  326. map[string]bool{
  327. "true": true,
  328. "false": false,
  329. },
  330. map[string]interface{}{
  331. "true": "True",
  332. "false": false,
  333. "uint16(1616)": uint16(1616),
  334. },
  335. //add a complex combo map in here. (map has list which has map)
  336. //note that after the first thing, everything else should be generic.
  337. map[string]interface{}{
  338. "list": []interface{}{
  339. int16(1616),
  340. int32(32323232),
  341. true,
  342. float32(-3232.0),
  343. map[string]interface{}{
  344. "TRUE": true,
  345. "FALSE": false,
  346. },
  347. []interface{}{true, false},
  348. },
  349. "int32": int32(32323232),
  350. "bool": true,
  351. "LONG STRING": "123456789012345678901234567890123456789012345678901234567890",
  352. "SHORT STRING": "1234567890",
  353. },
  354. map[interface{}]interface{}{
  355. true: "true",
  356. uint8(138): false,
  357. "false": uint8(200),
  358. },
  359. newTestStruc(0, false, !testSkipIntf, false),
  360. }
  361. table = []interface{}{}
  362. table = append(table, primitives...) //0-19 are primitives
  363. table = append(table, primitives) //20 is a list of primitives
  364. table = append(table, mapsAndStrucs...) //21-24 are maps. 25 is a *struct
  365. tableVerify = make([]interface{}, len(table))
  366. tableTestNilVerify = make([]interface{}, len(table))
  367. tablePythonVerify = make([]interface{}, len(table))
  368. lp := len(primitives)
  369. av := tableVerify
  370. for i, v := range table {
  371. if i == lp+3 {
  372. av[i] = skipVerifyVal
  373. continue
  374. }
  375. //av[i] = testVerifyVal(v, testVerifyMapTypeSame)
  376. switch v.(type) {
  377. case []interface{}:
  378. av[i] = testVerifyVal(v, testVerifyMapTypeSame)
  379. case map[string]interface{}:
  380. av[i] = testVerifyVal(v, testVerifyMapTypeSame)
  381. case map[interface{}]interface{}:
  382. av[i] = testVerifyVal(v, testVerifyMapTypeSame)
  383. default:
  384. av[i] = v
  385. }
  386. }
  387. av = tableTestNilVerify
  388. for i, v := range table {
  389. if i > lp+3 {
  390. av[i] = skipVerifyVal
  391. continue
  392. }
  393. av[i] = testVerifyVal(v, testVerifyMapTypeStrIntf)
  394. }
  395. av = tablePythonVerify
  396. for i, v := range table {
  397. if i > lp+3 {
  398. av[i] = skipVerifyVal
  399. continue
  400. }
  401. av[i] = testVerifyVal(v, testVerifyForPython)
  402. }
  403. tablePythonVerify = tablePythonVerify[:24]
  404. }
  405. func testUnmarshal(v interface{}, data []byte, h Handle) (err error) {
  406. return testCodecDecode(data, v, h)
  407. }
  408. func testMarshal(v interface{}, h Handle) (bs []byte, err error) {
  409. return testCodecEncode(v, nil, testByteBuf, h)
  410. }
  411. func testMarshalErr(v interface{}, h Handle, t *testing.T, name string) (bs []byte, err error) {
  412. if bs, err = testMarshal(v, h); err != nil {
  413. logT(t, "Error encoding %s: %v, Err: %v", name, v, err)
  414. t.FailNow()
  415. }
  416. return
  417. }
  418. func testUnmarshalErr(v interface{}, data []byte, h Handle, t *testing.T, name string) (err error) {
  419. if err = testUnmarshal(v, data, h); err != nil {
  420. logT(t, "Error Decoding into %s: %v, Err: %v", name, v, err)
  421. t.FailNow()
  422. }
  423. return
  424. }
  425. // doTestCodecTableOne allows us test for different variations based on arguments passed.
  426. func doTestCodecTableOne(t *testing.T, testNil bool, h Handle,
  427. vs []interface{}, vsVerify []interface{}) {
  428. //if testNil, then just test for when a pointer to a nil interface{} is passed. It should work.
  429. //Current setup allows us test (at least manually) the nil interface or typed interface.
  430. logT(t, "================ TestNil: %v ================\n", testNil)
  431. for i, v0 := range vs {
  432. logT(t, "..............................................")
  433. logT(t, " Testing: #%d:, %T, %#v\n", i, v0, v0)
  434. b0, err := testMarshalErr(v0, h, t, "v0")
  435. if err != nil {
  436. continue
  437. }
  438. if h.isBinary() {
  439. logT(t, " Encoded bytes: len: %v, %v\n", len(b0), b0)
  440. } else {
  441. logT(t, " Encoded string: len: %v, %v\n", len(string(b0)), string(b0))
  442. // println("########### encoded string: " + string(b0))
  443. }
  444. var v1 interface{}
  445. if testNil {
  446. err = testUnmarshal(&v1, b0, h)
  447. } else {
  448. if v0 != nil {
  449. v0rt := reflect.TypeOf(v0) // ptr
  450. rv1 := reflect.New(v0rt)
  451. err = testUnmarshal(rv1.Interface(), b0, h)
  452. v1 = rv1.Elem().Interface()
  453. // v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
  454. }
  455. }
  456. logT(t, " v1 returned: %T, %#v", v1, v1)
  457. // if v1 != nil {
  458. // logT(t, " v1 returned: %T, %#v", v1, v1)
  459. // //we always indirect, because ptr to typed value may be passed (if not testNil)
  460. // v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
  461. // }
  462. if err != nil {
  463. logT(t, "-------- Error: %v. Partial return: %v", err, v1)
  464. failT(t)
  465. continue
  466. }
  467. v0check := vsVerify[i]
  468. if v0check == skipVerifyVal {
  469. logT(t, " Nil Check skipped: Decoded: %T, %#v\n", v1, v1)
  470. continue
  471. }
  472. if err = deepEqual(v0check, v1); err == nil {
  473. logT(t, "++++++++ Before and After marshal matched\n")
  474. } else {
  475. // logT(t, "-------- Before and After marshal do not match: Error: %v"+
  476. // " ====> GOLDEN: (%T) %#v, DECODED: (%T) %#v\n", err, v0check, v0check, v1, v1)
  477. logT(t, "-------- Before and After marshal do not match: Error: %v", err)
  478. logT(t, " ....... GOLDEN: (%T) %#v", v0check, v0check)
  479. logT(t, " ....... DECODED: (%T) %#v", v1, v1)
  480. failT(t)
  481. }
  482. }
  483. }
  484. func testCodecTableOne(t *testing.T, h Handle) {
  485. testOnce.Do(testInitAll)
  486. // func TestMsgpackAllExperimental(t *testing.T) {
  487. // dopts := testDecOpts(nil, nil, false, true, true),
  488. idxTime, numPrim, numMap := 19, 23, 4
  489. //println("#################")
  490. switch v := h.(type) {
  491. case *MsgpackHandle:
  492. var oldWriteExt, oldRawToString bool
  493. oldWriteExt, v.WriteExt = v.WriteExt, true
  494. oldRawToString, v.RawToString = v.RawToString, true
  495. doTestCodecTableOne(t, false, h, table, tableVerify)
  496. v.WriteExt, v.RawToString = oldWriteExt, oldRawToString
  497. case *JsonHandle:
  498. //skip []interface{} containing time.Time, as it encodes as a number, but cannot decode back to time.Time.
  499. //As there is no real support for extension tags in json, this must be skipped.
  500. doTestCodecTableOne(t, false, h, table[:numPrim], tableVerify[:numPrim])
  501. doTestCodecTableOne(t, false, h, table[numPrim+1:], tableVerify[numPrim+1:])
  502. default:
  503. doTestCodecTableOne(t, false, h, table, tableVerify)
  504. }
  505. // func TestMsgpackAll(t *testing.T) {
  506. // //skip []interface{} containing time.Time
  507. // doTestCodecTableOne(t, false, h, table[:numPrim], tableVerify[:numPrim])
  508. // doTestCodecTableOne(t, false, h, table[numPrim+1:], tableVerify[numPrim+1:])
  509. // func TestMsgpackNilStringMap(t *testing.T) {
  510. var oldMapType reflect.Type
  511. v := h.getBasicHandle()
  512. oldMapType, v.MapType = v.MapType, testMapStrIntfTyp
  513. //skip time.Time, []interface{} containing time.Time, last map, and newStruc
  514. doTestCodecTableOne(t, true, h, table[:idxTime], tableTestNilVerify[:idxTime])
  515. doTestCodecTableOne(t, true, h, table[numPrim+1:numPrim+numMap], tableTestNilVerify[numPrim+1:numPrim+numMap])
  516. v.MapType = oldMapType
  517. // func TestMsgpackNilIntf(t *testing.T) {
  518. //do newTestStruc and last element of map
  519. doTestCodecTableOne(t, true, h, table[numPrim+numMap:], tableTestNilVerify[numPrim+numMap:])
  520. //TODO? What is this one?
  521. //doTestCodecTableOne(t, true, h, table[17:18], tableTestNilVerify[17:18])
  522. }
  523. func testCodecMiscOne(t *testing.T, h Handle) {
  524. testOnce.Do(testInitAll)
  525. b, err := testMarshalErr(32, h, t, "32")
  526. // Cannot do this nil one, because faster type assertion decoding will panic
  527. // var i *int32
  528. // if err = testUnmarshal(b, i, nil); err == nil {
  529. // logT(t, "------- Expecting error because we cannot unmarshal to int32 nil ptr")
  530. // t.FailNow()
  531. // }
  532. var i2 int32 = 0
  533. err = testUnmarshalErr(&i2, b, h, t, "int32-ptr")
  534. if i2 != int32(32) {
  535. logT(t, "------- didn't unmarshal to 32: Received: %d", i2)
  536. t.FailNow()
  537. }
  538. // func TestMsgpackDecodePtr(t *testing.T) {
  539. ts := newTestStruc(0, false, !testSkipIntf, false)
  540. b, err = testMarshalErr(ts, h, t, "pointer-to-struct")
  541. if len(b) < 40 {
  542. logT(t, "------- Size must be > 40. Size: %d", len(b))
  543. t.FailNow()
  544. }
  545. if h.isBinary() {
  546. logT(t, "------- b: %v", b)
  547. } else {
  548. logT(t, "------- b: %s", b)
  549. }
  550. ts2 := new(TestStruc)
  551. err = testUnmarshalErr(ts2, b, h, t, "pointer-to-struct")
  552. if ts2.I64 != math.MaxInt64*2/3 {
  553. logT(t, "------- Unmarshal wrong. Expect I64 = 64. Got: %v", ts2.I64)
  554. t.FailNow()
  555. }
  556. // func TestMsgpackIntfDecode(t *testing.T) {
  557. m := map[string]int{"A": 2, "B": 3}
  558. p := []interface{}{m}
  559. bs, err := testMarshalErr(p, h, t, "p")
  560. m2 := map[string]int{}
  561. p2 := []interface{}{m2}
  562. err = testUnmarshalErr(&p2, bs, h, t, "&p2")
  563. if m2["A"] != 2 || m2["B"] != 3 {
  564. logT(t, "m2 not as expected: expecting: %v, got: %v", m, m2)
  565. t.FailNow()
  566. }
  567. // log("m: %v, m2: %v, p: %v, p2: %v", m, m2, p, p2)
  568. checkEqualT(t, p, p2, "p=p2")
  569. checkEqualT(t, m, m2, "m=m2")
  570. if err = deepEqual(p, p2); err == nil {
  571. logT(t, "p and p2 match")
  572. } else {
  573. logT(t, "Not Equal: %v. p: %v, p2: %v", err, p, p2)
  574. t.FailNow()
  575. }
  576. if err = deepEqual(m, m2); err == nil {
  577. logT(t, "m and m2 match")
  578. } else {
  579. logT(t, "Not Equal: %v. m: %v, m2: %v", err, m, m2)
  580. t.FailNow()
  581. }
  582. // func TestMsgpackDecodeStructSubset(t *testing.T) {
  583. // test that we can decode a subset of the stream
  584. mm := map[string]interface{}{"A": 5, "B": 99, "C": 333}
  585. bs, err = testMarshalErr(mm, h, t, "mm")
  586. type ttt struct {
  587. A uint8
  588. C int32
  589. }
  590. var t2 ttt
  591. testUnmarshalErr(&t2, bs, h, t, "t2")
  592. t3 := ttt{5, 333}
  593. checkEqualT(t, t2, t3, "t2=t3")
  594. // println(">>>>>")
  595. // test simple arrays, non-addressable arrays, slices
  596. type tarr struct {
  597. A int64
  598. B [3]int64
  599. C []byte
  600. D [3]byte
  601. }
  602. var tarr0 = tarr{1, [3]int64{2, 3, 4}, []byte{4, 5, 6}, [3]byte{7, 8, 9}}
  603. // test both pointer and non-pointer (value)
  604. for _, tarr1 := range []interface{}{tarr0, &tarr0} {
  605. bs, err = testMarshalErr(tarr1, h, t, "tarr1")
  606. if err != nil {
  607. logT(t, "Error marshalling: %v", err)
  608. t.FailNow()
  609. }
  610. if _, ok := h.(*JsonHandle); ok {
  611. logT(t, "Marshal as: %s", bs)
  612. }
  613. var tarr2 tarr
  614. testUnmarshalErr(&tarr2, bs, h, t, "tarr2")
  615. checkEqualT(t, tarr0, tarr2, "tarr0=tarr2")
  616. // fmt.Printf(">>>> err: %v. tarr1: %v, tarr2: %v\n", err, tarr0, tarr2)
  617. }
  618. // test byte array, even if empty (msgpack only)
  619. if h == testMsgpackH {
  620. type ystruct struct {
  621. Anarray []byte
  622. }
  623. var ya = ystruct{}
  624. testUnmarshalErr(&ya, []byte{0x91, 0x90}, h, t, "ya")
  625. }
  626. }
  627. func testCodecEmbeddedPointer(t *testing.T, h Handle) {
  628. testOnce.Do(testInitAll)
  629. type Z int
  630. type A struct {
  631. AnInt int
  632. }
  633. type B struct {
  634. *Z
  635. *A
  636. MoreInt int
  637. }
  638. var z Z = 4
  639. x1 := &B{&z, &A{5}, 6}
  640. bs, err := testMarshalErr(x1, h, t, "x1")
  641. // fmt.Printf("buf: len(%v): %x\n", buf.Len(), buf.Bytes())
  642. var x2 = new(B)
  643. err = testUnmarshalErr(x2, bs, h, t, "x2")
  644. err = checkEqualT(t, x1, x2, "x1=x2")
  645. _ = err
  646. }
  647. func testCodecUnderlyingType(t *testing.T, h Handle) {
  648. testOnce.Do(testInitAll)
  649. // Manual Test.
  650. // Run by hand, with accompanying print statements in fast-path.go
  651. // to ensure that the fast functions are called.
  652. type T1 map[string]string
  653. v := T1{"1": "1s", "2": "2s"}
  654. var bs []byte
  655. var err error
  656. NewEncoderBytes(&bs, h).MustEncode(v)
  657. if err != nil {
  658. logT(t, "Error during encode: %v", err)
  659. failT(t)
  660. }
  661. var v2 T1
  662. NewDecoderBytes(bs, h).MustDecode(&v2)
  663. if err != nil {
  664. logT(t, "Error during decode: %v", err)
  665. failT(t)
  666. }
  667. }
  668. func testCodecChan(t *testing.T, h Handle) {
  669. // - send a slice []*int64 (sl1) into an chan (ch1) with cap > len(s1)
  670. // - encode ch1 as a stream array
  671. // - decode a chan (ch2), with cap > len(s1) from the stream array
  672. // - receive from ch2 into slice sl2
  673. // - compare sl1 and sl2
  674. // - do this for codecs: json, cbor (covers all types)
  675. sl1 := make([]*int64, 4)
  676. for i := range sl1 {
  677. var j int64 = int64(i)
  678. sl1[i] = &j
  679. }
  680. ch1 := make(chan *int64, 4)
  681. for _, j := range sl1 {
  682. ch1 <- j
  683. }
  684. var bs []byte
  685. NewEncoderBytes(&bs, h).MustEncode(ch1)
  686. // if !h.isBinary() {
  687. // fmt.Printf("before: len(ch1): %v, bs: %s\n", len(ch1), bs)
  688. // }
  689. // var ch2 chan *int64 // this will block if json, etc.
  690. ch2 := make(chan *int64, 8)
  691. NewDecoderBytes(bs, h).MustDecode(&ch2)
  692. // logT(t, "Len(ch2): %v", len(ch2))
  693. // fmt.Printf("after: len(ch2): %v, ch2: %v\n", len(ch2), ch2)
  694. close(ch2)
  695. var sl2 []*int64
  696. for j := range ch2 {
  697. sl2 = append(sl2, j)
  698. }
  699. if err := deepEqual(sl1, sl2); err != nil {
  700. logT(t, "Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
  701. failT(t)
  702. }
  703. }
  704. func testCodecRpcOne(t *testing.T, rr Rpc, h Handle, doRequest bool, exitSleepMs time.Duration,
  705. ) (port int) {
  706. testOnce.Do(testInitAll)
  707. if testSkipRPCTests {
  708. return
  709. }
  710. // rpc needs EOF, which is sent via a panic, and so must be recovered.
  711. if !recoverPanicToErr {
  712. logT(t, "EXPECTED. set recoverPanicToErr=true, since rpc needs EOF")
  713. t.FailNow()
  714. }
  715. srv := rpc.NewServer()
  716. srv.Register(testRpcInt)
  717. ln, err := net.Listen("tcp", "127.0.0.1:0")
  718. // log("listener: %v", ln.Addr())
  719. checkErrT(t, err)
  720. port = (ln.Addr().(*net.TCPAddr)).Port
  721. // var opts *DecoderOptions
  722. // opts := testDecOpts
  723. // opts.MapType = mapStrIntfTyp
  724. // opts.RawToString = false
  725. serverExitChan := make(chan bool, 1)
  726. var serverExitFlag uint64 = 0
  727. serverFn := func() {
  728. for {
  729. conn1, err1 := ln.Accept()
  730. // if err1 != nil {
  731. // //fmt.Printf("accept err1: %v\n", err1)
  732. // continue
  733. // }
  734. if atomic.LoadUint64(&serverExitFlag) == 1 {
  735. serverExitChan <- true
  736. conn1.Close()
  737. return // exit serverFn goroutine
  738. }
  739. if err1 == nil {
  740. var sc rpc.ServerCodec = rr.ServerCodec(conn1, h)
  741. srv.ServeCodec(sc)
  742. }
  743. }
  744. }
  745. clientFn := func(cc rpc.ClientCodec) {
  746. cl := rpc.NewClientWithCodec(cc)
  747. defer cl.Close()
  748. // defer func() { println("##### client closing"); cl.Close() }()
  749. var up, sq, mult int
  750. var rstr string
  751. // log("Calling client")
  752. checkErrT(t, cl.Call("TestRpcInt.Update", 5, &up))
  753. // log("Called TestRpcInt.Update")
  754. checkEqualT(t, testRpcInt.i, 5, "testRpcInt.i=5")
  755. checkEqualT(t, up, 5, "up=5")
  756. checkErrT(t, cl.Call("TestRpcInt.Square", 1, &sq))
  757. checkEqualT(t, sq, 25, "sq=25")
  758. checkErrT(t, cl.Call("TestRpcInt.Mult", 20, &mult))
  759. checkEqualT(t, mult, 100, "mult=100")
  760. checkErrT(t, cl.Call("TestRpcInt.EchoStruct", TestABC{"Aa", "Bb", "Cc"}, &rstr))
  761. checkEqualT(t, rstr, fmt.Sprintf("%#v", TestABC{"Aa", "Bb", "Cc"}), "rstr=")
  762. checkErrT(t, cl.Call("TestRpcInt.Echo123", []string{"A1", "B2", "C3"}, &rstr))
  763. checkEqualT(t, rstr, fmt.Sprintf("%#v", []string{"A1", "B2", "C3"}), "rstr=")
  764. }
  765. connFn := func() (bs net.Conn) {
  766. // log("calling f1")
  767. bs, err2 := net.Dial(ln.Addr().Network(), ln.Addr().String())
  768. //fmt.Printf("f1. bs: %v, err2: %v\n", bs, err2)
  769. checkErrT(t, err2)
  770. return
  771. }
  772. exitFn := func() {
  773. atomic.StoreUint64(&serverExitFlag, 1)
  774. bs := connFn()
  775. <-serverExitChan
  776. bs.Close()
  777. // serverExitChan <- true
  778. }
  779. go serverFn()
  780. runtime.Gosched()
  781. //time.Sleep(100 * time.Millisecond)
  782. if exitSleepMs == 0 {
  783. defer ln.Close()
  784. defer exitFn()
  785. }
  786. if doRequest {
  787. bs := connFn()
  788. cc := rr.ClientCodec(bs, h)
  789. clientFn(cc)
  790. }
  791. if exitSleepMs != 0 {
  792. go func() {
  793. defer ln.Close()
  794. time.Sleep(exitSleepMs)
  795. exitFn()
  796. }()
  797. }
  798. return
  799. }
  800. func doTestMapEncodeForCanonical(t *testing.T, name string, h Handle) {
  801. v1 := map[string]interface{}{
  802. "a": 1,
  803. "b": "hello",
  804. "c": map[string]interface{}{
  805. "c/a": 1,
  806. "c/b": "world",
  807. "c/c": []int{1, 2, 3, 4},
  808. "c/d": map[string]interface{}{
  809. "c/d/a": "fdisajfoidsajfopdjsaopfjdsapofda",
  810. "c/d/b": "fdsafjdposakfodpsakfopdsakfpodsakfpodksaopfkdsopafkdopsa",
  811. "c/d/c": "poir02 ir30qif4p03qir0pogjfpoaerfgjp ofke[padfk[ewapf kdp[afep[aw",
  812. "c/d/d": "fdsopafkd[sa f-32qor-=4qeof -afo-erfo r-eafo 4e- o r4-qwo ag",
  813. "c/d/e": "kfep[a sfkr0[paf[a foe-[wq ewpfao-q ro3-q ro-4qof4-qor 3-e orfkropzjbvoisdb",
  814. "c/d/f": "",
  815. },
  816. "c/e": map[int]string{
  817. 1: "1",
  818. 22: "22",
  819. 333: "333",
  820. 4444: "4444",
  821. 55555: "55555",
  822. },
  823. "c/f": map[string]int{
  824. "1": 1,
  825. "22": 22,
  826. "333": 333,
  827. "4444": 4444,
  828. "55555": 55555,
  829. },
  830. },
  831. }
  832. var v2 map[string]interface{}
  833. var b1, b2 []byte
  834. // encode v1 into b1, decode b1 into v2, encode v2 into b2, compare b1 and b2
  835. bh := h.getBasicHandle()
  836. if !bh.Canonical {
  837. bh.Canonical = true
  838. defer func() { bh.Canonical = false }()
  839. }
  840. e1 := NewEncoderBytes(&b1, h)
  841. e1.MustEncode(v1)
  842. d1 := NewDecoderBytes(b1, h)
  843. d1.MustDecode(&v2)
  844. e2 := NewEncoderBytes(&b2, h)
  845. e2.MustEncode(v2)
  846. if !bytes.Equal(b1, b2) {
  847. logT(t, "Unequal bytes: %v VS %v", b1, b2)
  848. t.FailNow()
  849. }
  850. }
  851. // Comprehensive testing that generates data encoded from python handle (cbor, msgpack),
  852. // and validates that our code can read and write it out accordingly.
  853. // We keep this unexported here, and put actual test in ext_dep_test.go.
  854. // This way, it can be excluded by excluding file completely.
  855. func doTestPythonGenStreams(t *testing.T, name string, h Handle) {
  856. logT(t, "TestPythonGenStreams-%v", name)
  857. tmpdir, err := ioutil.TempDir("", "golang-"+name+"-test")
  858. if err != nil {
  859. logT(t, "-------- Unable to create temp directory\n")
  860. t.FailNow()
  861. }
  862. defer os.RemoveAll(tmpdir)
  863. logT(t, "tmpdir: %v", tmpdir)
  864. cmd := exec.Command("python", "test.py", "testdata", tmpdir)
  865. //cmd.Stdin = strings.NewReader("some input")
  866. //cmd.Stdout = &out
  867. var cmdout []byte
  868. if cmdout, err = cmd.CombinedOutput(); err != nil {
  869. logT(t, "-------- Error running test.py testdata. Err: %v", err)
  870. logT(t, " %v", string(cmdout))
  871. t.FailNow()
  872. }
  873. bh := h.getBasicHandle()
  874. oldMapType := bh.MapType
  875. for i, v := range tablePythonVerify {
  876. // if v == uint64(0) && h == testMsgpackH {
  877. // v = int64(0)
  878. // }
  879. bh.MapType = oldMapType
  880. //load up the golden file based on number
  881. //decode it
  882. //compare to in-mem object
  883. //encode it again
  884. //compare to output stream
  885. logT(t, "..............................................")
  886. logT(t, " Testing: #%d: %T, %#v\n", i, v, v)
  887. var bss []byte
  888. bss, err = ioutil.ReadFile(filepath.Join(tmpdir, strconv.Itoa(i)+"."+name+".golden"))
  889. if err != nil {
  890. logT(t, "-------- Error reading golden file: %d. Err: %v", i, err)
  891. failT(t)
  892. continue
  893. }
  894. bh.MapType = testMapStrIntfTyp
  895. var v1 interface{}
  896. if err = testUnmarshal(&v1, bss, h); err != nil {
  897. logT(t, "-------- Error decoding stream: %d: Err: %v", i, err)
  898. failT(t)
  899. continue
  900. }
  901. if v == skipVerifyVal {
  902. continue
  903. }
  904. //no need to indirect, because we pass a nil ptr, so we already have the value
  905. //if v1 != nil { v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface() }
  906. if err = deepEqual(v, v1); err == nil {
  907. logT(t, "++++++++ Objects match: %T, %v", v, v)
  908. } else {
  909. logT(t, "-------- Objects do not match: %v. Source: %T. Decoded: %T", err, v, v1)
  910. logT(t, "-------- GOLDEN: %#v", v)
  911. // logT(t, "-------- DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
  912. logT(t, "-------- DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
  913. failT(t)
  914. }
  915. bsb, err := testMarshal(v1, h)
  916. if err != nil {
  917. logT(t, "Error encoding to stream: %d: Err: %v", i, err)
  918. failT(t)
  919. continue
  920. }
  921. if err = deepEqual(bsb, bss); err == nil {
  922. logT(t, "++++++++ Bytes match")
  923. } else {
  924. logT(t, "???????? Bytes do not match. %v.", err)
  925. xs := "--------"
  926. if reflect.ValueOf(v).Kind() == reflect.Map {
  927. xs = " "
  928. logT(t, "%s It's a map. Ok that they don't match (dependent on ordering).", xs)
  929. } else {
  930. logT(t, "%s It's not a map. They should match.", xs)
  931. failT(t)
  932. }
  933. logT(t, "%s FROM_FILE: %4d] %v", xs, len(bss), bss)
  934. logT(t, "%s ENCODED: %4d] %v", xs, len(bsb), bsb)
  935. }
  936. }
  937. bh.MapType = oldMapType
  938. }
  939. // To test MsgpackSpecRpc, we test 3 scenarios:
  940. // - Go Client to Go RPC Service (contained within TestMsgpackRpcSpec)
  941. // - Go client to Python RPC Service (contained within doTestMsgpackRpcSpecGoClientToPythonSvc)
  942. // - Python Client to Go RPC Service (contained within doTestMsgpackRpcSpecPythonClientToGoSvc)
  943. //
  944. // This allows us test the different calling conventions
  945. // - Go Service requires only one argument
  946. // - Python Service allows multiple arguments
  947. func doTestMsgpackRpcSpecGoClientToPythonSvc(t *testing.T) {
  948. if testSkipRPCTests {
  949. return
  950. }
  951. // openPorts are between 6700 and 6800
  952. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  953. openPort := strconv.FormatInt(6700+r.Int63n(99), 10)
  954. // openPort := "6792"
  955. cmd := exec.Command("python", "test.py", "rpc-server", openPort, "4")
  956. checkErrT(t, cmd.Start())
  957. bs, err2 := net.Dial("tcp", ":"+openPort)
  958. for i := 0; i < 10 && err2 != nil; i++ {
  959. time.Sleep(50 * time.Millisecond) // time for python rpc server to start
  960. bs, err2 = net.Dial("tcp", ":"+openPort)
  961. }
  962. checkErrT(t, err2)
  963. cc := MsgpackSpecRpc.ClientCodec(bs, testMsgpackH)
  964. cl := rpc.NewClientWithCodec(cc)
  965. defer cl.Close()
  966. var rstr string
  967. checkErrT(t, cl.Call("EchoStruct", TestABC{"Aa", "Bb", "Cc"}, &rstr))
  968. //checkEqualT(t, rstr, "{'A': 'Aa', 'B': 'Bb', 'C': 'Cc'}")
  969. var mArgs MsgpackSpecRpcMultiArgs = []interface{}{"A1", "B2", "C3"}
  970. checkErrT(t, cl.Call("Echo123", mArgs, &rstr))
  971. checkEqualT(t, rstr, "1:A1 2:B2 3:C3", "rstr=")
  972. cmd.Process.Kill()
  973. }
  974. func doTestMsgpackRpcSpecPythonClientToGoSvc(t *testing.T) {
  975. if testSkipRPCTests {
  976. return
  977. }
  978. port := testCodecRpcOne(t, MsgpackSpecRpc, testMsgpackH, false, 1*time.Second)
  979. //time.Sleep(1000 * time.Millisecond)
  980. cmd := exec.Command("python", "test.py", "rpc-client-go-service", strconv.Itoa(port))
  981. var cmdout []byte
  982. var err error
  983. if cmdout, err = cmd.CombinedOutput(); err != nil {
  984. logT(t, "-------- Error running test.py rpc-client-go-service. Err: %v", err)
  985. logT(t, " %v", string(cmdout))
  986. t.FailNow()
  987. }
  988. checkEqualT(t, string(cmdout),
  989. fmt.Sprintf("%#v\n%#v\n", []string{"A1", "B2", "C3"}, TestABC{"Aa", "Bb", "Cc"}), "cmdout=")
  990. }
  991. func TestBincCodecsTable(t *testing.T) {
  992. testCodecTableOne(t, testBincH)
  993. }
  994. func TestBincCodecsMisc(t *testing.T) {
  995. testCodecMiscOne(t, testBincH)
  996. }
  997. func TestBincCodecsEmbeddedPointer(t *testing.T) {
  998. testCodecEmbeddedPointer(t, testBincH)
  999. }
  1000. func TestSimpleCodecsTable(t *testing.T) {
  1001. testCodecTableOne(t, testSimpleH)
  1002. }
  1003. func TestSimpleCodecsMisc(t *testing.T) {
  1004. testCodecMiscOne(t, testSimpleH)
  1005. }
  1006. func TestSimpleCodecsEmbeddedPointer(t *testing.T) {
  1007. testCodecEmbeddedPointer(t, testSimpleH)
  1008. }
  1009. func TestMsgpackCodecsTable(t *testing.T) {
  1010. testCodecTableOne(t, testMsgpackH)
  1011. }
  1012. func TestMsgpackCodecsMisc(t *testing.T) {
  1013. testCodecMiscOne(t, testMsgpackH)
  1014. }
  1015. func TestMsgpackCodecsEmbeddedPointer(t *testing.T) {
  1016. testCodecEmbeddedPointer(t, testMsgpackH)
  1017. }
  1018. func TestCborCodecsTable(t *testing.T) {
  1019. testCodecTableOne(t, testCborH)
  1020. }
  1021. func TestCborCodecsMisc(t *testing.T) {
  1022. testCodecMiscOne(t, testCborH)
  1023. }
  1024. func TestCborCodecsEmbeddedPointer(t *testing.T) {
  1025. testCodecEmbeddedPointer(t, testCborH)
  1026. }
  1027. func TestCborMapEncodeForCanonical(t *testing.T) {
  1028. doTestMapEncodeForCanonical(t, "cbor", testCborH)
  1029. }
  1030. func TestJsonCodecsTable(t *testing.T) {
  1031. testCodecTableOne(t, testJsonH)
  1032. }
  1033. func TestJsonCodecsMisc(t *testing.T) {
  1034. testCodecMiscOne(t, testJsonH)
  1035. }
  1036. func TestJsonCodecsEmbeddedPointer(t *testing.T) {
  1037. testCodecEmbeddedPointer(t, testJsonH)
  1038. }
  1039. func TestJsonCodecChan(t *testing.T) {
  1040. testCodecChan(t, testJsonH)
  1041. }
  1042. func TestCborCodecChan(t *testing.T) {
  1043. testCodecChan(t, testCborH)
  1044. }
  1045. // ----- RPC -----
  1046. func TestBincRpcGo(t *testing.T) {
  1047. testCodecRpcOne(t, GoRpc, testBincH, true, 0)
  1048. }
  1049. func TestSimpleRpcGo(t *testing.T) {
  1050. testCodecRpcOne(t, GoRpc, testSimpleH, true, 0)
  1051. }
  1052. func TestMsgpackRpcGo(t *testing.T) {
  1053. testCodecRpcOne(t, GoRpc, testMsgpackH, true, 0)
  1054. }
  1055. func TestCborRpcGo(t *testing.T) {
  1056. testCodecRpcOne(t, GoRpc, testCborH, true, 0)
  1057. }
  1058. func TestJsonRpcGo(t *testing.T) {
  1059. testCodecRpcOne(t, GoRpc, testJsonH, true, 0)
  1060. }
  1061. func TestMsgpackRpcSpec(t *testing.T) {
  1062. testCodecRpcOne(t, MsgpackSpecRpc, testMsgpackH, true, 0)
  1063. }
  1064. func TestBincUnderlyingType(t *testing.T) {
  1065. testCodecUnderlyingType(t, testBincH)
  1066. }
  1067. // TODO:
  1068. // Add Tests for:
  1069. // - decoding empty list/map in stream into a nil slice/map
  1070. // - binary(M|Unm)arsher support for time.Time (e.g. cbor encoding)
  1071. // - text(M|Unm)arshaler support for time.Time (e.g. json encoding)
  1072. // - non fast-path scenarios e.g. map[string]uint16, []customStruct.
  1073. // Expand cbor to include indefinite length stuff for this non-fast-path types.
  1074. // This may not be necessary, since we have the manual tests (fastpathEnabled=false) to test/validate with.
  1075. // - CodecSelfer
  1076. // Ensure it is called when (en|de)coding interface{} or reflect.Value (2 different codepaths).
  1077. // - interfaces: textMarshaler, binaryMarshaler, codecSelfer
  1078. // - struct tags:
  1079. // on anonymous fields, _struct (all fields), etc
  1080. // - codecgen of struct containing channels.
  1081. //
  1082. // Cleanup tests:
  1083. // - The are brittle in their handling of validation and skipping