codec_test.go 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  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. import (
  20. "bytes"
  21. "encoding/gob"
  22. "fmt"
  23. "io/ioutil"
  24. "math"
  25. "math/rand"
  26. "net"
  27. "net/rpc"
  28. "os"
  29. "os/exec"
  30. "path/filepath"
  31. "reflect"
  32. "runtime"
  33. "strconv"
  34. "strings"
  35. "sync/atomic"
  36. "testing"
  37. "time"
  38. )
  39. func init() {
  40. testPreInitFns = append(testPreInitFns, testInit)
  41. }
  42. // make this a mapbyslice
  43. type testMbsT []interface{}
  44. func (_ testMbsT) MapBySlice() {}
  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. testTableNumPrimitives int
  56. testTableIdxTime int
  57. testTableNumMaps int
  58. )
  59. var (
  60. skipVerifyVal interface{} = &(struct{}{})
  61. testMapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
  62. // For Go Time, do not use a descriptive timezone.
  63. // It's unnecessary, and makes it harder to do a reflect.DeepEqual.
  64. // The Offset already tells what the offset should be, if not on UTC and unknown zone name.
  65. timeLoc = time.FixedZone("", -8*60*60) // UTC-08:00 //time.UTC-8
  66. timeToCompare1 = time.Date(2012, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
  67. timeToCompare2 = time.Date(1900, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
  68. timeToCompare3 = time.Unix(0, 270).UTC() // use value that must be encoded as uint64 for nanoseconds (for cbor/msgpack comparison)
  69. //timeToCompare4 = time.Time{}.UTC() // does not work well with simple cbor time encoding (overflow)
  70. timeToCompare4 = time.Unix(-2013855848, 4223).UTC()
  71. table []interface{} // main items we encode
  72. tableVerify []interface{} // we verify encoded things against this after decode
  73. tableTestNilVerify []interface{} // for nil interface, use this to verify (rules are different)
  74. tablePythonVerify []interface{} // for verifying for python, since Python sometimes
  75. // will encode a float32 as float64, or large int as uint
  76. testRpcInt = new(TestRpcInt)
  77. )
  78. func testByteBuf(in []byte) *bytes.Buffer {
  79. return bytes.NewBuffer(in)
  80. }
  81. type TestABC struct {
  82. A, B, C string
  83. }
  84. func (x *TestABC) MarshalBinary() ([]byte, error) {
  85. return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
  86. }
  87. func (x *TestABC) MarshalText() ([]byte, error) {
  88. return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
  89. }
  90. func (x *TestABC) MarshalJSON() ([]byte, error) {
  91. return []byte(fmt.Sprintf(`"%s %s %s"`, x.A, x.B, x.C)), nil
  92. }
  93. func (x *TestABC) UnmarshalBinary(data []byte) (err error) {
  94. ss := strings.Split(string(data), " ")
  95. x.A, x.B, x.C = ss[0], ss[1], ss[2]
  96. return
  97. }
  98. func (x *TestABC) UnmarshalText(data []byte) (err error) {
  99. return x.UnmarshalBinary(data)
  100. }
  101. func (x *TestABC) UnmarshalJSON(data []byte) (err error) {
  102. return x.UnmarshalBinary(data[1 : len(data)-1])
  103. }
  104. type TestABC2 struct {
  105. A, B, C string
  106. }
  107. func (x TestABC2) MarshalText() ([]byte, error) {
  108. return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
  109. }
  110. func (x *TestABC2) UnmarshalText(data []byte) (err error) {
  111. ss := strings.Split(string(data), " ")
  112. x.A, x.B, x.C = ss[0], ss[1], ss[2]
  113. return
  114. // _, err = fmt.Sscanf(string(data), "%s %s %s", &x.A, &x.B, &x.C)
  115. }
  116. type TestSimplish struct {
  117. Ii int
  118. Ss string
  119. Ar [2]*TestSimplish
  120. Sl []*TestSimplish
  121. Mm map[string]*TestSimplish
  122. }
  123. type TestRpcABC struct {
  124. A, B, C string
  125. }
  126. type TestRpcInt struct {
  127. i int
  128. }
  129. func (r *TestRpcInt) Update(n int, res *int) error { r.i = n; *res = r.i; return nil }
  130. func (r *TestRpcInt) Square(ignore int, res *int) error { *res = r.i * r.i; return nil }
  131. func (r *TestRpcInt) Mult(n int, res *int) error { *res = r.i * n; return nil }
  132. func (r *TestRpcInt) EchoStruct(arg TestRpcABC, res *string) error {
  133. *res = fmt.Sprintf("%#v", arg)
  134. return nil
  135. }
  136. func (r *TestRpcInt) Echo123(args []string, res *string) error {
  137. *res = fmt.Sprintf("%#v", args)
  138. return nil
  139. }
  140. type TestRawValue struct {
  141. R Raw
  142. I int
  143. }
  144. type testUnixNanoTimeExt struct {
  145. // keep timestamp here, so that do not incur interface-conversion costs
  146. ts int64
  147. }
  148. // func (x *testUnixNanoTimeExt) WriteExt(interface{}) []byte { panic("unsupported") }
  149. // func (x *testUnixNanoTimeExt) ReadExt(interface{}, []byte) { panic("unsupported") }
  150. func (x *testUnixNanoTimeExt) ConvertExt(v interface{}) interface{} {
  151. switch v2 := v.(type) {
  152. case time.Time:
  153. x.ts = v2.UTC().UnixNano()
  154. case *time.Time:
  155. x.ts = v2.UTC().UnixNano()
  156. default:
  157. panic(fmt.Sprintf("unsupported format for time conversion: expecting time.Time; got %T", v))
  158. }
  159. return &x.ts
  160. }
  161. func (x *testUnixNanoTimeExt) UpdateExt(dest interface{}, v interface{}) {
  162. // fmt.Printf("testUnixNanoTimeExt.UpdateExt: v: %v\n", v)
  163. tt := dest.(*time.Time)
  164. switch v2 := v.(type) {
  165. case int64:
  166. *tt = time.Unix(0, v2).UTC()
  167. case *int64:
  168. *tt = time.Unix(0, *v2).UTC()
  169. case uint64:
  170. *tt = time.Unix(0, int64(v2)).UTC()
  171. case *uint64:
  172. *tt = time.Unix(0, int64(*v2)).UTC()
  173. //case float64:
  174. //case string:
  175. default:
  176. panic(fmt.Sprintf("unsupported format for time conversion: expecting int64/uint64; got %T", v))
  177. }
  178. // fmt.Printf("testUnixNanoTimeExt.UpdateExt: v: %v, tt: %#v\n", v, tt)
  179. }
  180. func testVerifyVal(v interface{}, arg testVerifyArg) (v2 interface{}) {
  181. //for python msgpack,
  182. // - all positive integers are unsigned 64-bit ints
  183. // - all floats are float64
  184. switch iv := v.(type) {
  185. case int8:
  186. if iv >= 0 {
  187. v2 = uint64(iv)
  188. } else {
  189. v2 = int64(iv)
  190. }
  191. case int16:
  192. if iv >= 0 {
  193. v2 = uint64(iv)
  194. } else {
  195. v2 = int64(iv)
  196. }
  197. case int32:
  198. if iv >= 0 {
  199. v2 = uint64(iv)
  200. } else {
  201. v2 = int64(iv)
  202. }
  203. case int64:
  204. if iv >= 0 {
  205. v2 = uint64(iv)
  206. } else {
  207. v2 = int64(iv)
  208. }
  209. case uint8:
  210. v2 = uint64(iv)
  211. case uint16:
  212. v2 = uint64(iv)
  213. case uint32:
  214. v2 = uint64(iv)
  215. case uint64:
  216. v2 = uint64(iv)
  217. case float32:
  218. v2 = float64(iv)
  219. case float64:
  220. v2 = float64(iv)
  221. case []interface{}:
  222. m2 := make([]interface{}, len(iv))
  223. for j, vj := range iv {
  224. m2[j] = testVerifyVal(vj, arg)
  225. }
  226. v2 = m2
  227. case testMbsT:
  228. m2 := make([]interface{}, len(iv))
  229. for j, vj := range iv {
  230. m2[j] = testVerifyVal(vj, arg)
  231. }
  232. v2 = testMbsT(m2)
  233. case map[string]bool:
  234. switch arg {
  235. case testVerifyMapTypeSame:
  236. m2 := make(map[string]bool)
  237. for kj, kv := range iv {
  238. m2[kj] = kv
  239. }
  240. v2 = m2
  241. case testVerifyMapTypeStrIntf, testVerifyForPython:
  242. m2 := make(map[string]interface{})
  243. for kj, kv := range iv {
  244. m2[kj] = kv
  245. }
  246. v2 = m2
  247. case testVerifyMapTypeIntfIntf:
  248. m2 := make(map[interface{}]interface{})
  249. for kj, kv := range iv {
  250. m2[kj] = kv
  251. }
  252. v2 = m2
  253. }
  254. case map[string]interface{}:
  255. switch arg {
  256. case testVerifyMapTypeSame:
  257. m2 := make(map[string]interface{})
  258. for kj, kv := range iv {
  259. m2[kj] = testVerifyVal(kv, arg)
  260. }
  261. v2 = m2
  262. case testVerifyMapTypeStrIntf, testVerifyForPython:
  263. m2 := make(map[string]interface{})
  264. for kj, kv := range iv {
  265. m2[kj] = testVerifyVal(kv, arg)
  266. }
  267. v2 = m2
  268. case testVerifyMapTypeIntfIntf:
  269. m2 := make(map[interface{}]interface{})
  270. for kj, kv := range iv {
  271. m2[kj] = testVerifyVal(kv, arg)
  272. }
  273. v2 = m2
  274. }
  275. case map[interface{}]interface{}:
  276. m2 := make(map[interface{}]interface{})
  277. for kj, kv := range iv {
  278. m2[testVerifyVal(kj, arg)] = testVerifyVal(kv, arg)
  279. }
  280. v2 = m2
  281. case time.Time:
  282. switch arg {
  283. case testVerifyForPython:
  284. if iv2 := iv.UnixNano(); iv2 >= 0 {
  285. v2 = uint64(iv2)
  286. } else {
  287. v2 = int64(iv2)
  288. }
  289. default:
  290. v2 = v
  291. }
  292. default:
  293. v2 = v
  294. }
  295. return
  296. }
  297. func testInit() {
  298. gob.Register(new(TestStruc))
  299. if testInitDebug {
  300. ts0 := newTestStruc(2, false, !testSkipIntf, false)
  301. fmt.Printf("====> depth: %v, ts: %#v\n", 2, ts0)
  302. }
  303. for _, v := range testHandles {
  304. bh := v.getBasicHandle()
  305. bh.InternString = testInternStr
  306. bh.Canonical = testCanonical
  307. bh.CheckCircularRef = testCheckCircRef
  308. bh.StructToArray = testStructToArray
  309. bh.MaxInitLen = testMaxInitLen
  310. // mostly doing this for binc
  311. if testWriteNoSymbols {
  312. bh.AsSymbols = AsSymbolNone
  313. } else {
  314. bh.AsSymbols = AsSymbolAll
  315. }
  316. }
  317. testJsonH.Indent = int8(testJsonIndent)
  318. testJsonH.HTMLCharsAsIs = testJsonHTMLCharsAsIs
  319. testJsonH.PreferFloat = testJsonPreferFloat
  320. testMsgpackH.RawToString = true
  321. // testMsgpackH.AddExt(byteSliceTyp, 0, testMsgpackH.BinaryEncodeExt, testMsgpackH.BinaryDecodeExt)
  322. // testMsgpackH.AddExt(timeTyp, 1, testMsgpackH.TimeEncodeExt, testMsgpackH.TimeDecodeExt)
  323. // add extensions for msgpack, simple for time.Time, so we can encode/decode same way.
  324. // use different flavors of XXXExt calls, including deprecated ones.
  325. // NOTE:
  326. // DO NOT set extensions for JsonH, so we can test json(M|Unm)arshal support.
  327. var (
  328. timeExtEncFn = func(rv reflect.Value) (bs []byte, err error) {
  329. defer panicToErr(&err)
  330. bs = timeExt{}.WriteExt(rv.Interface())
  331. return
  332. }
  333. timeExtDecFn = func(rv reflect.Value, bs []byte) (err error) {
  334. defer panicToErr(&err)
  335. timeExt{}.ReadExt(rv.Interface(), bs)
  336. return
  337. }
  338. )
  339. testSimpleH.AddExt(timeTyp, 1, timeExtEncFn, timeExtDecFn)
  340. testMsgpackH.SetBytesExt(timeTyp, 1, timeExt{})
  341. testCborH.SetInterfaceExt(timeTyp, 1, &testUnixNanoTimeExt{})
  342. // testJsonH.SetInterfaceExt(timeTyp, 1, &testUnixNanoTimeExt{})
  343. // primitives MUST be an even number, so it can be used as a mapBySlice also.
  344. primitives := []interface{}{
  345. int8(-8),
  346. int16(-1616),
  347. int32(-32323232),
  348. int64(-6464646464646464),
  349. uint8(192),
  350. uint16(1616),
  351. uint32(32323232),
  352. uint64(6464646464646464),
  353. byte(192),
  354. float32(-3232.0),
  355. float64(-6464646464.0),
  356. float32(3232.0),
  357. float64(6464.0),
  358. float64(6464646464.0),
  359. false,
  360. true,
  361. "null",
  362. nil,
  363. "some&day>some<day",
  364. timeToCompare1,
  365. "",
  366. timeToCompare2,
  367. "bytestring",
  368. timeToCompare3,
  369. "none",
  370. timeToCompare4,
  371. }
  372. maps := []interface{}{
  373. map[string]bool{
  374. "true": true,
  375. "false": false,
  376. },
  377. map[string]interface{}{
  378. "true": "True",
  379. "false": false,
  380. "uint16(1616)": uint16(1616),
  381. },
  382. //add a complex combo map in here. (map has list which has map)
  383. //note that after the first thing, everything else should be generic.
  384. map[string]interface{}{
  385. "list": []interface{}{
  386. int16(1616),
  387. int32(32323232),
  388. true,
  389. float32(-3232.0),
  390. map[string]interface{}{
  391. "TRUE": true,
  392. "FALSE": false,
  393. },
  394. []interface{}{true, false},
  395. },
  396. "int32": int32(32323232),
  397. "bool": true,
  398. "LONG STRING": `
  399. 1234567890 1234567890
  400. 1234567890 1234567890
  401. 1234567890 1234567890
  402. ABCDEDFGHIJKLMNOPQRSTUVWXYZ
  403. abcdedfghijklmnopqrstuvwxyz
  404. ABCDEDFGHIJKLMNOPQRSTUVWXYZ
  405. abcdedfghijklmnopqrstuvwxyz
  406. "ABCDEDFGHIJKLMNOPQRSTUVWXYZ"
  407. ' a tab '
  408. \a\b\c\d\e
  409. \b\f\n\r\t all literally
  410. ugorji
  411. `,
  412. "SHORT STRING": "1234567890",
  413. },
  414. map[interface{}]interface{}{
  415. true: "true",
  416. uint8(138): false,
  417. "false": uint8(200),
  418. },
  419. }
  420. testTableNumPrimitives = len(primitives)
  421. testTableIdxTime = testTableNumPrimitives - 8
  422. testTableNumMaps = len(maps)
  423. table = []interface{}{}
  424. table = append(table, primitives...)
  425. table = append(table, primitives)
  426. table = append(table, testMbsT(primitives))
  427. table = append(table, maps...)
  428. table = append(table, newTestStruc(0, false, !testSkipIntf, false))
  429. tableVerify = make([]interface{}, len(table))
  430. tableTestNilVerify = make([]interface{}, len(table))
  431. tablePythonVerify = make([]interface{}, len(table))
  432. lp := testTableNumPrimitives + 4
  433. av := tableVerify
  434. for i, v := range table {
  435. if i == lp {
  436. av[i] = skipVerifyVal
  437. continue
  438. }
  439. //av[i] = testVerifyVal(v, testVerifyMapTypeSame)
  440. switch v.(type) {
  441. case []interface{}:
  442. av[i] = testVerifyVal(v, testVerifyMapTypeSame)
  443. case testMbsT:
  444. av[i] = testVerifyVal(v, testVerifyMapTypeSame)
  445. case map[string]interface{}:
  446. av[i] = testVerifyVal(v, testVerifyMapTypeSame)
  447. case map[interface{}]interface{}:
  448. av[i] = testVerifyVal(v, testVerifyMapTypeSame)
  449. default:
  450. av[i] = v
  451. }
  452. }
  453. av = tableTestNilVerify
  454. for i, v := range table {
  455. if i > lp {
  456. av[i] = skipVerifyVal
  457. continue
  458. }
  459. av[i] = testVerifyVal(v, testVerifyMapTypeStrIntf)
  460. }
  461. av = tablePythonVerify
  462. for i, v := range table {
  463. if i == testTableNumPrimitives+1 || i > lp { // testTableNumPrimitives+1 is the mapBySlice
  464. av[i] = skipVerifyVal
  465. continue
  466. }
  467. av[i] = testVerifyVal(v, testVerifyForPython)
  468. }
  469. // only do the python verify up to the maps, skipping the last 2 maps.
  470. tablePythonVerify = tablePythonVerify[:testTableNumPrimitives+2+testTableNumMaps-2]
  471. }
  472. func testUnmarshal(v interface{}, data []byte, h Handle) (err error) {
  473. return testCodecDecode(data, v, h)
  474. }
  475. func testMarshal(v interface{}, h Handle) (bs []byte, err error) {
  476. return testCodecEncode(v, nil, testByteBuf, h)
  477. }
  478. func testMarshalErr(v interface{}, h Handle, t *testing.T, name string) (bs []byte, err error) {
  479. if bs, err = testMarshal(v, h); err != nil {
  480. logT(t, "Error encoding %s: %v, Err: %v", name, v, err)
  481. failT(t)
  482. }
  483. return
  484. }
  485. func testUnmarshalErr(v interface{}, data []byte, h Handle, t *testing.T, name string) (err error) {
  486. if err = testUnmarshal(v, data, h); err != nil {
  487. logT(t, "Error Decoding into %s: %v, Err: %v", name, v, err)
  488. failT(t)
  489. }
  490. return
  491. }
  492. func testDeepEqualErr(v1, v2 interface{}, t *testing.T, name string) (err error) {
  493. if err = deepEqual(v1, v2); err == nil {
  494. logT(t, "%s: values equal", name)
  495. } else {
  496. logT(t, "%s: values not equal: %v. 1: %v, 2: %v", name, err, v1, v2)
  497. failT(t)
  498. }
  499. return
  500. }
  501. // doTestCodecTableOne allows us test for different variations based on arguments passed.
  502. func doTestCodecTableOne(t *testing.T, testNil bool, h Handle,
  503. vs []interface{}, vsVerify []interface{}) {
  504. //if testNil, then just test for when a pointer to a nil interface{} is passed. It should work.
  505. //Current setup allows us test (at least manually) the nil interface or typed interface.
  506. logT(t, "================ TestNil: %v ================\n", testNil)
  507. for i, v0 := range vs {
  508. logT(t, "..............................................")
  509. logT(t, " Testing: #%d:, %T, %#v\n", i, v0, v0)
  510. b0, err := testMarshalErr(v0, h, t, "v0")
  511. if err != nil {
  512. continue
  513. }
  514. if h.isBinary() {
  515. logT(t, " Encoded bytes: len: %v, %v\n", len(b0), b0)
  516. } else {
  517. logT(t, " Encoded string: len: %v, %v\n", len(string(b0)), string(b0))
  518. // println("########### encoded string: " + string(b0))
  519. }
  520. var v1 interface{}
  521. if testNil {
  522. err = testUnmarshal(&v1, b0, h)
  523. } else {
  524. if v0 != nil {
  525. v0rt := reflect.TypeOf(v0) // ptr
  526. rv1 := reflect.New(v0rt)
  527. err = testUnmarshal(rv1.Interface(), b0, h)
  528. v1 = rv1.Elem().Interface()
  529. // v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
  530. }
  531. }
  532. logT(t, " v1 returned: %T, %#v", v1, v1)
  533. // if v1 != nil {
  534. // logT(t, " v1 returned: %T, %#v", v1, v1)
  535. // //we always indirect, because ptr to typed value may be passed (if not testNil)
  536. // v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
  537. // }
  538. if err != nil {
  539. logT(t, "-------- Error: %v. Partial return: %v", err, v1)
  540. failT(t)
  541. continue
  542. }
  543. v0check := vsVerify[i]
  544. if v0check == skipVerifyVal {
  545. logT(t, " Nil Check skipped: Decoded: %T, %#v\n", v1, v1)
  546. continue
  547. }
  548. if err = deepEqual(v0check, v1); err == nil {
  549. logT(t, "++++++++ Before and After marshal matched\n")
  550. } else {
  551. // logT(t, "-------- Before and After marshal do not match: Error: %v"+
  552. // " ====> GOLDEN: (%T) %#v, DECODED: (%T) %#v\n", err, v0check, v0check, v1, v1)
  553. logT(t, "-------- Before and After marshal do not match: Error: %v", err)
  554. logT(t, " ....... GOLDEN: (%T) %#v", v0check, v0check)
  555. logT(t, " ....... DECODED: (%T) %#v", v1, v1)
  556. failT(t)
  557. }
  558. }
  559. }
  560. func testCodecTableOne(t *testing.T, h Handle) {
  561. testOnce.Do(testInitAll)
  562. // func TestMsgpackAllExperimental(t *testing.T) {
  563. // dopts := testDecOpts(nil, nil, false, true, true),
  564. numPrim, numMap, idxTime, idxMap := testTableNumPrimitives, testTableNumMaps, testTableIdxTime, testTableNumPrimitives+2
  565. //println("#################")
  566. switch v := h.(type) {
  567. case *MsgpackHandle:
  568. var oldWriteExt, oldRawToString bool
  569. oldWriteExt, v.WriteExt = v.WriteExt, true
  570. oldRawToString, v.RawToString = v.RawToString, true
  571. doTestCodecTableOne(t, false, h, table, tableVerify)
  572. v.WriteExt, v.RawToString = oldWriteExt, oldRawToString
  573. case *JsonHandle:
  574. //skip []interface{} containing time.Time, as it encodes as a number, but cannot decode back to time.Time.
  575. //As there is no real support for extension tags in json, this must be skipped.
  576. doTestCodecTableOne(t, false, h, table[:numPrim], tableVerify[:numPrim])
  577. doTestCodecTableOne(t, false, h, table[idxMap:], tableVerify[idxMap:])
  578. default:
  579. doTestCodecTableOne(t, false, h, table, tableVerify)
  580. }
  581. // func TestMsgpackAll(t *testing.T) {
  582. // //skip []interface{} containing time.Time
  583. // doTestCodecTableOne(t, false, h, table[:numPrim], tableVerify[:numPrim])
  584. // doTestCodecTableOne(t, false, h, table[numPrim+1:], tableVerify[numPrim+1:])
  585. // func TestMsgpackNilStringMap(t *testing.T) {
  586. var oldMapType reflect.Type
  587. v := h.getBasicHandle()
  588. oldMapType, v.MapType = v.MapType, testMapStrIntfTyp
  589. //skip time.Time, []interface{} containing time.Time, last map, and newStruc
  590. doTestCodecTableOne(t, true, h, table[:idxTime], tableTestNilVerify[:idxTime])
  591. doTestCodecTableOne(t, true, h, table[idxMap:idxMap+numMap-1], tableTestNilVerify[idxMap:idxMap+numMap-1])
  592. v.MapType = oldMapType
  593. // func TestMsgpackNilIntf(t *testing.T) {
  594. //do last map and newStruc
  595. idx2 := idxMap + numMap - 1
  596. doTestCodecTableOne(t, true, h, table[idx2:], tableTestNilVerify[idx2:])
  597. //TODO? What is this one?
  598. //doTestCodecTableOne(t, true, h, table[17:18], tableTestNilVerify[17:18])
  599. }
  600. func testCodecMiscOne(t *testing.T, h Handle) {
  601. testOnce.Do(testInitAll)
  602. b, err := testMarshalErr(32, h, t, "32")
  603. // Cannot do this nil one, because faster type assertion decoding will panic
  604. // var i *int32
  605. // if err = testUnmarshal(b, i, nil); err == nil {
  606. // logT(t, "------- Expecting error because we cannot unmarshal to int32 nil ptr")
  607. // failT(t)
  608. // }
  609. var i2 int32 = 0
  610. err = testUnmarshalErr(&i2, b, h, t, "int32-ptr")
  611. if i2 != int32(32) {
  612. logT(t, "------- didn't unmarshal to 32: Received: %d", i2)
  613. failT(t)
  614. }
  615. // func TestMsgpackDecodePtr(t *testing.T) {
  616. ts := newTestStruc(0, false, !testSkipIntf, false)
  617. b, err = testMarshalErr(ts, h, t, "pointer-to-struct")
  618. if len(b) < 40 {
  619. logT(t, "------- Size must be > 40. Size: %d", len(b))
  620. failT(t)
  621. }
  622. if h.isBinary() {
  623. logT(t, "------- b: %v", b)
  624. } else {
  625. logT(t, "------- b: %s", b)
  626. }
  627. ts2 := new(TestStruc)
  628. err = testUnmarshalErr(ts2, b, h, t, "pointer-to-struct")
  629. if ts2.I64 != math.MaxInt64*2/3 {
  630. logT(t, "------- Unmarshal wrong. Expect I64 = 64. Got: %v", ts2.I64)
  631. failT(t)
  632. }
  633. // func TestMsgpackIntfDecode(t *testing.T) {
  634. m := map[string]int{"A": 2, "B": 3}
  635. p := []interface{}{m}
  636. bs, err := testMarshalErr(p, h, t, "p")
  637. m2 := map[string]int{}
  638. p2 := []interface{}{m2}
  639. err = testUnmarshalErr(&p2, bs, h, t, "&p2")
  640. if m2["A"] != 2 || m2["B"] != 3 {
  641. logT(t, "m2 not as expected: expecting: %v, got: %v", m, m2)
  642. failT(t)
  643. }
  644. // log("m: %v, m2: %v, p: %v, p2: %v", m, m2, p, p2)
  645. checkEqualT(t, p, p2, "p=p2")
  646. checkEqualT(t, m, m2, "m=m2")
  647. if err = deepEqual(p, p2); err == nil {
  648. logT(t, "p and p2 match")
  649. } else {
  650. logT(t, "Not Equal: %v. p: %v, p2: %v", err, p, p2)
  651. failT(t)
  652. }
  653. if err = deepEqual(m, m2); err == nil {
  654. logT(t, "m and m2 match")
  655. } else {
  656. logT(t, "Not Equal: %v. m: %v, m2: %v", err, m, m2)
  657. failT(t)
  658. }
  659. // func TestMsgpackDecodeStructSubset(t *testing.T) {
  660. // test that we can decode a subset of the stream
  661. mm := map[string]interface{}{"A": 5, "B": 99, "C": 333}
  662. bs, err = testMarshalErr(mm, h, t, "mm")
  663. type ttt struct {
  664. A uint8
  665. C int32
  666. }
  667. var t2 ttt
  668. testUnmarshalErr(&t2, bs, h, t, "t2")
  669. t3 := ttt{5, 333}
  670. checkEqualT(t, t2, t3, "t2=t3")
  671. // println(">>>>>")
  672. // test simple arrays, non-addressable arrays, slices
  673. type tarr struct {
  674. A int64
  675. B [3]int64
  676. C []byte
  677. D [3]byte
  678. }
  679. var tarr0 = tarr{1, [3]int64{2, 3, 4}, []byte{4, 5, 6}, [3]byte{7, 8, 9}}
  680. // test both pointer and non-pointer (value)
  681. for _, tarr1 := range []interface{}{tarr0, &tarr0} {
  682. bs, err = testMarshalErr(tarr1, h, t, "tarr1")
  683. if err != nil {
  684. logT(t, "Error marshalling: %v", err)
  685. failT(t)
  686. }
  687. if _, ok := h.(*JsonHandle); ok {
  688. logT(t, "Marshal as: %s", bs)
  689. }
  690. var tarr2 tarr
  691. testUnmarshalErr(&tarr2, bs, h, t, "tarr2")
  692. checkEqualT(t, tarr0, tarr2, "tarr0=tarr2")
  693. // fmt.Printf(">>>> err: %v. tarr1: %v, tarr2: %v\n", err, tarr0, tarr2)
  694. }
  695. // test byte array, even if empty (msgpack only)
  696. if h == testMsgpackH {
  697. type ystruct struct {
  698. Anarray []byte
  699. }
  700. var ya = ystruct{}
  701. testUnmarshalErr(&ya, []byte{0x91, 0x90}, h, t, "ya")
  702. }
  703. }
  704. func testCodecEmbeddedPointer(t *testing.T, h Handle) {
  705. testOnce.Do(testInitAll)
  706. type Z int
  707. type A struct {
  708. AnInt int
  709. }
  710. type B struct {
  711. *Z
  712. *A
  713. MoreInt int
  714. }
  715. var z Z = 4
  716. x1 := &B{&z, &A{5}, 6}
  717. bs, err := testMarshalErr(x1, h, t, "x1")
  718. // fmt.Printf("buf: len(%v): %x\n", buf.Len(), buf.Bytes())
  719. var x2 = new(B)
  720. err = testUnmarshalErr(x2, bs, h, t, "x2")
  721. err = checkEqualT(t, x1, x2, "x1=x2")
  722. _ = err
  723. }
  724. func testCodecUnderlyingType(t *testing.T, h Handle) {
  725. testOnce.Do(testInitAll)
  726. // Manual Test.
  727. // Run by hand, with accompanying print statements in fast-path.go
  728. // to ensure that the fast functions are called.
  729. type T1 map[string]string
  730. v := T1{"1": "1s", "2": "2s"}
  731. var bs []byte
  732. var err error
  733. NewEncoderBytes(&bs, h).MustEncode(v)
  734. if err != nil {
  735. logT(t, "Error during encode: %v", err)
  736. failT(t)
  737. }
  738. var v2 T1
  739. NewDecoderBytes(bs, h).MustDecode(&v2)
  740. if err != nil {
  741. logT(t, "Error during decode: %v", err)
  742. failT(t)
  743. }
  744. }
  745. func testCodecChan(t *testing.T, h Handle) {
  746. // - send a slice []*int64 (sl1) into an chan (ch1) with cap > len(s1)
  747. // - encode ch1 as a stream array
  748. // - decode a chan (ch2), with cap > len(s1) from the stream array
  749. // - receive from ch2 into slice sl2
  750. // - compare sl1 and sl2
  751. // - do this for codecs: json, cbor (covers all types)
  752. sl1 := make([]*int64, 4)
  753. for i := range sl1 {
  754. var j int64 = int64(i)
  755. sl1[i] = &j
  756. }
  757. ch1 := make(chan *int64, 4)
  758. for _, j := range sl1 {
  759. ch1 <- j
  760. }
  761. var bs []byte
  762. NewEncoderBytes(&bs, h).MustEncode(ch1)
  763. // if !h.isBinary() {
  764. // fmt.Printf("before: len(ch1): %v, bs: %s\n", len(ch1), bs)
  765. // }
  766. // var ch2 chan *int64 // this will block if json, etc.
  767. ch2 := make(chan *int64, 8)
  768. NewDecoderBytes(bs, h).MustDecode(&ch2)
  769. // logT(t, "Len(ch2): %v", len(ch2))
  770. // fmt.Printf("after: len(ch2): %v, ch2: %v\n", len(ch2), ch2)
  771. close(ch2)
  772. var sl2 []*int64
  773. for j := range ch2 {
  774. sl2 = append(sl2, j)
  775. }
  776. if err := deepEqual(sl1, sl2); err != nil {
  777. logT(t, "Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
  778. failT(t)
  779. }
  780. }
  781. func testCodecRpcOne(t *testing.T, rr Rpc, h Handle, doRequest bool, exitSleepMs time.Duration,
  782. ) (port int) {
  783. testOnce.Do(testInitAll)
  784. if testSkipRPCTests {
  785. return
  786. }
  787. // rpc needs EOF, which is sent via a panic, and so must be recovered.
  788. if !recoverPanicToErr {
  789. logT(t, "EXPECTED. set recoverPanicToErr=true, since rpc needs EOF")
  790. failT(t)
  791. }
  792. srv := rpc.NewServer()
  793. srv.Register(testRpcInt)
  794. ln, err := net.Listen("tcp", "127.0.0.1:0")
  795. // log("listener: %v", ln.Addr())
  796. checkErrT(t, err)
  797. port = (ln.Addr().(*net.TCPAddr)).Port
  798. // var opts *DecoderOptions
  799. // opts := testDecOpts
  800. // opts.MapType = mapStrIntfTyp
  801. // opts.RawToString = false
  802. serverExitChan := make(chan bool, 1)
  803. var serverExitFlag uint64 = 0
  804. serverFn := func() {
  805. for {
  806. conn1, err1 := ln.Accept()
  807. // if err1 != nil {
  808. // //fmt.Printf("accept err1: %v\n", err1)
  809. // continue
  810. // }
  811. if atomic.LoadUint64(&serverExitFlag) == 1 {
  812. serverExitChan <- true
  813. conn1.Close()
  814. return // exit serverFn goroutine
  815. }
  816. if err1 == nil {
  817. var sc rpc.ServerCodec = rr.ServerCodec(conn1, h)
  818. srv.ServeCodec(sc)
  819. }
  820. }
  821. }
  822. clientFn := func(cc rpc.ClientCodec) {
  823. cl := rpc.NewClientWithCodec(cc)
  824. defer cl.Close()
  825. // defer func() { println("##### client closing"); cl.Close() }()
  826. var up, sq, mult int
  827. var rstr string
  828. // log("Calling client")
  829. checkErrT(t, cl.Call("TestRpcInt.Update", 5, &up))
  830. // log("Called TestRpcInt.Update")
  831. checkEqualT(t, testRpcInt.i, 5, "testRpcInt.i=5")
  832. checkEqualT(t, up, 5, "up=5")
  833. checkErrT(t, cl.Call("TestRpcInt.Square", 1, &sq))
  834. checkEqualT(t, sq, 25, "sq=25")
  835. checkErrT(t, cl.Call("TestRpcInt.Mult", 20, &mult))
  836. checkEqualT(t, mult, 100, "mult=100")
  837. checkErrT(t, cl.Call("TestRpcInt.EchoStruct", TestRpcABC{"Aa", "Bb", "Cc"}, &rstr))
  838. checkEqualT(t, rstr, fmt.Sprintf("%#v", TestRpcABC{"Aa", "Bb", "Cc"}), "rstr=")
  839. checkErrT(t, cl.Call("TestRpcInt.Echo123", []string{"A1", "B2", "C3"}, &rstr))
  840. checkEqualT(t, rstr, fmt.Sprintf("%#v", []string{"A1", "B2", "C3"}), "rstr=")
  841. }
  842. connFn := func() (bs net.Conn) {
  843. // log("calling f1")
  844. bs, err2 := net.Dial(ln.Addr().Network(), ln.Addr().String())
  845. //fmt.Printf("f1. bs: %v, err2: %v\n", bs, err2)
  846. checkErrT(t, err2)
  847. return
  848. }
  849. exitFn := func() {
  850. atomic.StoreUint64(&serverExitFlag, 1)
  851. bs := connFn()
  852. <-serverExitChan
  853. bs.Close()
  854. // serverExitChan <- true
  855. }
  856. go serverFn()
  857. runtime.Gosched()
  858. //time.Sleep(100 * time.Millisecond)
  859. if exitSleepMs == 0 {
  860. defer ln.Close()
  861. defer exitFn()
  862. }
  863. if doRequest {
  864. bs := connFn()
  865. cc := rr.ClientCodec(bs, h)
  866. clientFn(cc)
  867. }
  868. if exitSleepMs != 0 {
  869. go func() {
  870. defer ln.Close()
  871. time.Sleep(exitSleepMs)
  872. exitFn()
  873. }()
  874. }
  875. return
  876. }
  877. func doTestMapEncodeForCanonical(t *testing.T, name string, h Handle) {
  878. v1 := map[string]interface{}{
  879. "a": 1,
  880. "b": "hello",
  881. "c": map[string]interface{}{
  882. "c/a": 1,
  883. "c/b": "world",
  884. "c/c": []int{1, 2, 3, 4},
  885. "c/d": map[string]interface{}{
  886. "c/d/a": "fdisajfoidsajfopdjsaopfjdsapofda",
  887. "c/d/b": "fdsafjdposakfodpsakfopdsakfpodsakfpodksaopfkdsopafkdopsa",
  888. "c/d/c": "poir02 ir30qif4p03qir0pogjfpoaerfgjp ofke[padfk[ewapf kdp[afep[aw",
  889. "c/d/d": "fdsopafkd[sa f-32qor-=4qeof -afo-erfo r-eafo 4e- o r4-qwo ag",
  890. "c/d/e": "kfep[a sfkr0[paf[a foe-[wq ewpfao-q ro3-q ro-4qof4-qor 3-e orfkropzjbvoisdb",
  891. "c/d/f": "",
  892. },
  893. "c/e": map[int]string{
  894. 1: "1",
  895. 22: "22",
  896. 333: "333",
  897. 4444: "4444",
  898. 55555: "55555",
  899. },
  900. "c/f": map[string]int{
  901. "1": 1,
  902. "22": 22,
  903. "333": 333,
  904. "4444": 4444,
  905. "55555": 55555,
  906. },
  907. },
  908. }
  909. var v2 map[string]interface{}
  910. var b1, b2 []byte
  911. // encode v1 into b1, decode b1 into v2, encode v2 into b2, compare b1 and b2
  912. bh := h.getBasicHandle()
  913. if !bh.Canonical {
  914. bh.Canonical = true
  915. defer func() { bh.Canonical = false }()
  916. }
  917. e1 := NewEncoderBytes(&b1, h)
  918. e1.MustEncode(v1)
  919. d1 := NewDecoderBytes(b1, h)
  920. d1.MustDecode(&v2)
  921. e2 := NewEncoderBytes(&b2, h)
  922. e2.MustEncode(v2)
  923. if !bytes.Equal(b1, b2) {
  924. logT(t, "Unequal bytes: %v VS %v", b1, b2)
  925. failT(t)
  926. }
  927. }
  928. func doTestStdEncIntf(t *testing.T, name string, h Handle) {
  929. args := [][2]interface{}{
  930. {&TestABC{"A", "BB", "CCC"}, new(TestABC)},
  931. {&TestABC2{"AAA", "BB", "C"}, new(TestABC2)},
  932. }
  933. for _, a := range args {
  934. var b []byte
  935. e := NewEncoderBytes(&b, h)
  936. e.MustEncode(a[0])
  937. d := NewDecoderBytes(b, h)
  938. d.MustDecode(a[1])
  939. if err := deepEqual(a[0], a[1]); err == nil {
  940. logT(t, "++++ Objects match")
  941. } else {
  942. logT(t, "---- Objects do not match: y1: %v, err: %v", a[1], err)
  943. failT(t)
  944. }
  945. }
  946. }
  947. func doTestEncCircularRef(t *testing.T, name string, h Handle) {
  948. type T1 struct {
  949. S string
  950. B bool
  951. T interface{}
  952. }
  953. type T2 struct {
  954. S string
  955. T *T1
  956. }
  957. type T3 struct {
  958. S string
  959. T *T2
  960. }
  961. t1 := T1{"t1", true, nil}
  962. t2 := T2{"t2", &t1}
  963. t3 := T3{"t3", &t2}
  964. t1.T = &t3
  965. var bs []byte
  966. var err error
  967. bh := h.getBasicHandle()
  968. if !bh.CheckCircularRef {
  969. bh.CheckCircularRef = true
  970. defer func() { bh.CheckCircularRef = false }()
  971. }
  972. err = NewEncoderBytes(&bs, h).Encode(&t3)
  973. if err == nil {
  974. logT(t, "expecting error due to circular reference. found none")
  975. failT(t)
  976. }
  977. if x := err.Error(); strings.Contains(x, "circular") || strings.Contains(x, "cyclic") {
  978. logT(t, "error detected as expected: %v", x)
  979. } else {
  980. logT(t, "error detected was not as expected: %v", x)
  981. failT(t)
  982. }
  983. }
  984. // TestAnonCycleT{1,2,3} types are used to test anonymous cycles.
  985. // They are top-level, so that they can have circular references.
  986. type (
  987. TestAnonCycleT1 struct {
  988. S string
  989. TestAnonCycleT2
  990. }
  991. TestAnonCycleT2 struct {
  992. S2 string
  993. TestAnonCycleT3
  994. }
  995. TestAnonCycleT3 struct {
  996. *TestAnonCycleT1
  997. }
  998. )
  999. func doTestAnonCycle(t *testing.T, name string, h Handle) {
  1000. var x TestAnonCycleT1
  1001. x.S = "hello"
  1002. x.TestAnonCycleT2.S2 = "hello.2"
  1003. x.TestAnonCycleT2.TestAnonCycleT3.TestAnonCycleT1 = &x
  1004. // just check that you can get typeInfo for T1
  1005. rt := reflect.TypeOf((*TestAnonCycleT1)(nil)).Elem()
  1006. rtid := rt2id(rt)
  1007. pti := h.getBasicHandle().getTypeInfo(rtid, rt)
  1008. logT(t, "pti: %v", pti)
  1009. }
  1010. func doTestJsonLargeInteger(t *testing.T, v interface{}, ias uint8) {
  1011. logT(t, "Running doTestJsonLargeInteger: v: %#v, ias: %c", v, ias)
  1012. oldIAS := testJsonH.IntegerAsString
  1013. defer func() { testJsonH.IntegerAsString = oldIAS }()
  1014. testJsonH.IntegerAsString = ias
  1015. var vu uint
  1016. var vi int
  1017. var vb bool
  1018. var b []byte
  1019. e := NewEncoderBytes(&b, testJsonH)
  1020. e.MustEncode(v)
  1021. e.MustEncode(true)
  1022. d := NewDecoderBytes(b, testJsonH)
  1023. // below, we validate that the json string or number was encoded,
  1024. // then decode, and validate that the correct value was decoded.
  1025. fnStrChk := func() {
  1026. // check that output started with ", and ended with "true
  1027. if !(b[0] == '"' && string(b[len(b)-5:]) == `"true`) {
  1028. logT(t, "Expecting a JSON string, got: %s", b)
  1029. failT(t)
  1030. }
  1031. }
  1032. switch ias {
  1033. case 'L':
  1034. switch v2 := v.(type) {
  1035. case int:
  1036. v2n := int64(v2) // done to work with 32-bit OS
  1037. if v2n > 1<<53 || (v2n < 0 && -v2n > 1<<53) {
  1038. fnStrChk()
  1039. }
  1040. case uint:
  1041. v2n := uint64(v2) // done to work with 32-bit OS
  1042. if v2n > 1<<53 {
  1043. fnStrChk()
  1044. }
  1045. }
  1046. case 'A':
  1047. fnStrChk()
  1048. default:
  1049. // check that output doesn't contain " at all
  1050. for _, i := range b {
  1051. if i == '"' {
  1052. logT(t, "Expecting a JSON Number without quotation: got: %s", b)
  1053. failT(t)
  1054. }
  1055. }
  1056. }
  1057. switch v2 := v.(type) {
  1058. case int:
  1059. d.MustDecode(&vi)
  1060. d.MustDecode(&vb)
  1061. // check that vb = true, and vi == v2
  1062. if !(vb && vi == v2) {
  1063. logT(t, "Expecting equal values from %s: got golden: %v, decoded: %v", b, v2, vi)
  1064. failT(t)
  1065. }
  1066. case uint:
  1067. d.MustDecode(&vu)
  1068. d.MustDecode(&vb)
  1069. // check that vb = true, and vi == v2
  1070. if !(vb && vu == v2) {
  1071. logT(t, "Expecting equal values from %s: got golden: %v, decoded: %v", b, v2, vu)
  1072. failT(t)
  1073. }
  1074. // fmt.Printf("%v: %s, decode: %d, bool: %v, equal_on_decode: %v\n", v, b, vu, vb, vu == v.(uint))
  1075. }
  1076. }
  1077. func doTestRawValue(t *testing.T, name string, h Handle) {
  1078. bh := h.getBasicHandle()
  1079. if !bh.Raw {
  1080. bh.Raw = true
  1081. defer func() { bh.Raw = false }()
  1082. }
  1083. var i, i2 int
  1084. var v, v2 TestRawValue
  1085. var bs, bs2 []byte
  1086. i = 1234 //1234567890
  1087. v = TestRawValue{I: i}
  1088. e := NewEncoderBytes(&bs, h)
  1089. e.MustEncode(v.I)
  1090. logT(t, ">>> raw: %v\n", bs)
  1091. v.R = Raw(bs)
  1092. e.ResetBytes(&bs2)
  1093. e.MustEncode(v)
  1094. logT(t, ">>> bs2: %v\n", bs2)
  1095. d := NewDecoderBytes(bs2, h)
  1096. d.MustDecode(&v2)
  1097. d.ResetBytes(v2.R)
  1098. logT(t, ">>> v2.R: %v\n", ([]byte)(v2.R))
  1099. d.MustDecode(&i2)
  1100. logT(t, ">>> Encoded %v, decoded %v\n", i, i2)
  1101. // logT(t, "Encoded %v, decoded %v", i, i2)
  1102. if i != i2 {
  1103. logT(t, "Error: encoded %v, decoded %v", i, i2)
  1104. failT(t)
  1105. }
  1106. }
  1107. // Comprehensive testing that generates data encoded from python handle (cbor, msgpack),
  1108. // and validates that our code can read and write it out accordingly.
  1109. // We keep this unexported here, and put actual test in ext_dep_test.go.
  1110. // This way, it can be excluded by excluding file completely.
  1111. func doTestPythonGenStreams(t *testing.T, name string, h Handle) {
  1112. logT(t, "TestPythonGenStreams-%v", name)
  1113. tmpdir, err := ioutil.TempDir("", "golang-"+name+"-test")
  1114. if err != nil {
  1115. logT(t, "-------- Unable to create temp directory\n")
  1116. failT(t)
  1117. }
  1118. defer os.RemoveAll(tmpdir)
  1119. logT(t, "tmpdir: %v", tmpdir)
  1120. cmd := exec.Command("python", "test.py", "testdata", tmpdir)
  1121. //cmd.Stdin = strings.NewReader("some input")
  1122. //cmd.Stdout = &out
  1123. var cmdout []byte
  1124. if cmdout, err = cmd.CombinedOutput(); err != nil {
  1125. logT(t, "-------- Error running test.py testdata. Err: %v", err)
  1126. logT(t, " %v", string(cmdout))
  1127. failT(t)
  1128. }
  1129. bh := h.getBasicHandle()
  1130. oldMapType := bh.MapType
  1131. for i, v := range tablePythonVerify {
  1132. // if v == uint64(0) && h == testMsgpackH {
  1133. // v = int64(0)
  1134. // }
  1135. bh.MapType = oldMapType
  1136. //load up the golden file based on number
  1137. //decode it
  1138. //compare to in-mem object
  1139. //encode it again
  1140. //compare to output stream
  1141. logT(t, "..............................................")
  1142. logT(t, " Testing: #%d: %T, %#v\n", i, v, v)
  1143. var bss []byte
  1144. bss, err = ioutil.ReadFile(filepath.Join(tmpdir, strconv.Itoa(i)+"."+name+".golden"))
  1145. if err != nil {
  1146. logT(t, "-------- Error reading golden file: %d. Err: %v", i, err)
  1147. failT(t)
  1148. continue
  1149. }
  1150. bh.MapType = testMapStrIntfTyp
  1151. var v1 interface{}
  1152. if err = testUnmarshal(&v1, bss, h); err != nil {
  1153. logT(t, "-------- Error decoding stream: %d: Err: %v", i, err)
  1154. failT(t)
  1155. continue
  1156. }
  1157. if v == skipVerifyVal {
  1158. continue
  1159. }
  1160. //no need to indirect, because we pass a nil ptr, so we already have the value
  1161. //if v1 != nil { v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface() }
  1162. if err = deepEqual(v, v1); err == nil {
  1163. logT(t, "++++++++ Objects match: %T, %v", v, v)
  1164. } else {
  1165. logT(t, "-------- Objects do not match: %v. Source: %T. Decoded: %T", err, v, v1)
  1166. logT(t, "-------- GOLDEN: %#v", v)
  1167. // logT(t, "-------- DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
  1168. logT(t, "-------- DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
  1169. failT(t)
  1170. }
  1171. bsb, err := testMarshal(v1, h)
  1172. if err != nil {
  1173. logT(t, "Error encoding to stream: %d: Err: %v", i, err)
  1174. failT(t)
  1175. continue
  1176. }
  1177. if err = deepEqual(bsb, bss); err == nil {
  1178. logT(t, "++++++++ Bytes match")
  1179. } else {
  1180. logT(t, "???????? Bytes do not match. %v.", err)
  1181. xs := "--------"
  1182. if reflect.ValueOf(v).Kind() == reflect.Map {
  1183. xs = " "
  1184. logT(t, "%s It's a map. Ok that they don't match (dependent on ordering).", xs)
  1185. } else {
  1186. logT(t, "%s It's not a map. They should match.", xs)
  1187. failT(t)
  1188. }
  1189. logT(t, "%s FROM_FILE: %4d] %v", xs, len(bss), bss)
  1190. logT(t, "%s ENCODED: %4d] %v", xs, len(bsb), bsb)
  1191. }
  1192. }
  1193. bh.MapType = oldMapType
  1194. }
  1195. // To test MsgpackSpecRpc, we test 3 scenarios:
  1196. // - Go Client to Go RPC Service (contained within TestMsgpackRpcSpec)
  1197. // - Go client to Python RPC Service (contained within doTestMsgpackRpcSpecGoClientToPythonSvc)
  1198. // - Python Client to Go RPC Service (contained within doTestMsgpackRpcSpecPythonClientToGoSvc)
  1199. //
  1200. // This allows us test the different calling conventions
  1201. // - Go Service requires only one argument
  1202. // - Python Service allows multiple arguments
  1203. func doTestMsgpackRpcSpecGoClientToPythonSvc(t *testing.T) {
  1204. if testSkipRPCTests {
  1205. return
  1206. }
  1207. // openPorts are between 6700 and 6800
  1208. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  1209. openPort := strconv.FormatInt(6700+r.Int63n(99), 10)
  1210. // openPort := "6792"
  1211. cmd := exec.Command("python", "test.py", "rpc-server", openPort, "4")
  1212. checkErrT(t, cmd.Start())
  1213. bs, err2 := net.Dial("tcp", ":"+openPort)
  1214. for i := 0; i < 10 && err2 != nil; i++ {
  1215. time.Sleep(50 * time.Millisecond) // time for python rpc server to start
  1216. bs, err2 = net.Dial("tcp", ":"+openPort)
  1217. }
  1218. checkErrT(t, err2)
  1219. cc := MsgpackSpecRpc.ClientCodec(bs, testMsgpackH)
  1220. cl := rpc.NewClientWithCodec(cc)
  1221. defer cl.Close()
  1222. var rstr string
  1223. checkErrT(t, cl.Call("EchoStruct", TestRpcABC{"Aa", "Bb", "Cc"}, &rstr))
  1224. //checkEqualT(t, rstr, "{'A': 'Aa', 'B': 'Bb', 'C': 'Cc'}")
  1225. var mArgs MsgpackSpecRpcMultiArgs = []interface{}{"A1", "B2", "C3"}
  1226. checkErrT(t, cl.Call("Echo123", mArgs, &rstr))
  1227. checkEqualT(t, rstr, "1:A1 2:B2 3:C3", "rstr=")
  1228. cmd.Process.Kill()
  1229. }
  1230. func doTestMsgpackRpcSpecPythonClientToGoSvc(t *testing.T) {
  1231. if testSkipRPCTests {
  1232. return
  1233. }
  1234. port := testCodecRpcOne(t, MsgpackSpecRpc, testMsgpackH, false, 1*time.Second)
  1235. //time.Sleep(1000 * time.Millisecond)
  1236. cmd := exec.Command("python", "test.py", "rpc-client-go-service", strconv.Itoa(port))
  1237. var cmdout []byte
  1238. var err error
  1239. if cmdout, err = cmd.CombinedOutput(); err != nil {
  1240. logT(t, "-------- Error running test.py rpc-client-go-service. Err: %v", err)
  1241. logT(t, " %v", string(cmdout))
  1242. failT(t)
  1243. }
  1244. checkEqualT(t, string(cmdout),
  1245. fmt.Sprintf("%#v\n%#v\n", []string{"A1", "B2", "C3"}, TestRpcABC{"Aa", "Bb", "Cc"}), "cmdout=")
  1246. }
  1247. func testRandomFillRV(v reflect.Value) {
  1248. fneg := func() int64 {
  1249. i := rand.Intn(1)
  1250. if i == 1 {
  1251. return 1
  1252. }
  1253. return -1
  1254. }
  1255. switch v.Kind() {
  1256. case reflect.Invalid:
  1257. case reflect.Ptr:
  1258. if v.IsNil() {
  1259. v.Set(reflect.New(v.Type().Elem()))
  1260. }
  1261. testRandomFillRV(v.Elem())
  1262. case reflect.Interface:
  1263. if v.IsNil() {
  1264. v.Set(reflect.ValueOf("nothing"))
  1265. } else {
  1266. testRandomFillRV(v.Elem())
  1267. }
  1268. case reflect.Struct:
  1269. for i, n := 0, v.NumField(); i < n; i++ {
  1270. testRandomFillRV(v.Field(i))
  1271. }
  1272. case reflect.Slice:
  1273. if v.IsNil() {
  1274. v.Set(reflect.MakeSlice(v.Type(), 4, 4))
  1275. }
  1276. fallthrough
  1277. case reflect.Array:
  1278. for i, n := 0, v.Len(); i < n; i++ {
  1279. testRandomFillRV(v.Index(i))
  1280. }
  1281. case reflect.Map:
  1282. if v.IsNil() {
  1283. v.Set(reflect.MakeMap(v.Type()))
  1284. }
  1285. if v.Len() == 0 {
  1286. kt, vt := v.Type().Key(), v.Type().Elem()
  1287. for i := 0; i < 4; i++ {
  1288. k0 := reflect.New(kt).Elem()
  1289. v0 := reflect.New(vt).Elem()
  1290. testRandomFillRV(k0)
  1291. testRandomFillRV(v0)
  1292. v.SetMapIndex(k0, v0)
  1293. }
  1294. } else {
  1295. for _, k := range v.MapKeys() {
  1296. testRandomFillRV(v.MapIndex(k))
  1297. }
  1298. }
  1299. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1300. v.SetInt(fneg() * rand.Int63n(127))
  1301. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1302. v.SetUint(uint64(rand.Int63n(255)))
  1303. case reflect.Bool:
  1304. if fneg() == 1 {
  1305. v.SetBool(true)
  1306. } else {
  1307. v.SetBool(false)
  1308. }
  1309. case reflect.Float32, reflect.Float64:
  1310. v.SetFloat(float64(fneg()) * float64(rand.Float32()))
  1311. case reflect.String:
  1312. v.SetString(strings.Repeat(strconv.FormatInt(rand.Int63n(99), 10), rand.Intn(8)))
  1313. default:
  1314. panic(fmt.Errorf("testRandomFillRV: unsupported type: %v", v.Kind()))
  1315. }
  1316. }
  1317. func testMammoth(t *testing.T, name string, h Handle) {
  1318. testOnce.Do(testInitAll)
  1319. var m, m2 TestMammoth
  1320. testRandomFillRV(reflect.ValueOf(&m).Elem())
  1321. b, _ := testMarshalErr(&m, h, t, "mammoth-"+name)
  1322. testUnmarshalErr(&m2, b, h, t, "mammoth-"+name)
  1323. // fmt.Printf("m2: %v", &m2)
  1324. testDeepEqualErr(&m, &m2, t, "mammoth-"+name)
  1325. }
  1326. // -----------------
  1327. func TestBincCodecsTable(t *testing.T) {
  1328. testCodecTableOne(t, testBincH)
  1329. }
  1330. func TestBincCodecsMisc(t *testing.T) {
  1331. testCodecMiscOne(t, testBincH)
  1332. }
  1333. func TestBincCodecsEmbeddedPointer(t *testing.T) {
  1334. testCodecEmbeddedPointer(t, testBincH)
  1335. }
  1336. func TestBincStdEncIntf(t *testing.T) {
  1337. doTestStdEncIntf(t, "binc", testBincH)
  1338. }
  1339. func TestBincMammoth(t *testing.T) {
  1340. testMammoth(t, "binc", testBincH)
  1341. }
  1342. func TestSimpleCodecsTable(t *testing.T) {
  1343. testCodecTableOne(t, testSimpleH)
  1344. }
  1345. func TestSimpleCodecsMisc(t *testing.T) {
  1346. testCodecMiscOne(t, testSimpleH)
  1347. }
  1348. func TestSimpleCodecsEmbeddedPointer(t *testing.T) {
  1349. testCodecEmbeddedPointer(t, testSimpleH)
  1350. }
  1351. func TestSimpleStdEncIntf(t *testing.T) {
  1352. doTestStdEncIntf(t, "simple", testSimpleH)
  1353. }
  1354. func TestSimpleMammoth(t *testing.T) {
  1355. testMammoth(t, "simple", testSimpleH)
  1356. }
  1357. func TestMsgpackCodecsTable(t *testing.T) {
  1358. testCodecTableOne(t, testMsgpackH)
  1359. }
  1360. func TestMsgpackCodecsMisc(t *testing.T) {
  1361. testCodecMiscOne(t, testMsgpackH)
  1362. }
  1363. func TestMsgpackCodecsEmbeddedPointer(t *testing.T) {
  1364. testCodecEmbeddedPointer(t, testMsgpackH)
  1365. }
  1366. func TestMsgpackStdEncIntf(t *testing.T) {
  1367. doTestStdEncIntf(t, "msgpack", testMsgpackH)
  1368. }
  1369. func TestMsgpackMammoth(t *testing.T) {
  1370. testMammoth(t, "msgpack", testMsgpackH)
  1371. }
  1372. func TestCborCodecsTable(t *testing.T) {
  1373. testCodecTableOne(t, testCborH)
  1374. }
  1375. func TestCborCodecsMisc(t *testing.T) {
  1376. testCodecMiscOne(t, testCborH)
  1377. }
  1378. func TestCborCodecsEmbeddedPointer(t *testing.T) {
  1379. testCodecEmbeddedPointer(t, testCborH)
  1380. }
  1381. func TestCborMapEncodeForCanonical(t *testing.T) {
  1382. doTestMapEncodeForCanonical(t, "cbor", testCborH)
  1383. }
  1384. func TestCborCodecChan(t *testing.T) {
  1385. testCodecChan(t, testCborH)
  1386. }
  1387. func TestCborStdEncIntf(t *testing.T) {
  1388. doTestStdEncIntf(t, "cbor", testCborH)
  1389. }
  1390. func TestCborMammoth(t *testing.T) {
  1391. testMammoth(t, "cbor", testCborH)
  1392. }
  1393. func TestJsonCodecsTable(t *testing.T) {
  1394. testCodecTableOne(t, testJsonH)
  1395. }
  1396. func TestJsonCodecsMisc(t *testing.T) {
  1397. testCodecMiscOne(t, testJsonH)
  1398. }
  1399. func TestJsonCodecsEmbeddedPointer(t *testing.T) {
  1400. testCodecEmbeddedPointer(t, testJsonH)
  1401. }
  1402. func TestJsonCodecChan(t *testing.T) {
  1403. testCodecChan(t, testJsonH)
  1404. }
  1405. func TestJsonStdEncIntf(t *testing.T) {
  1406. doTestStdEncIntf(t, "json", testJsonH)
  1407. }
  1408. func TestJsonMammoth(t *testing.T) {
  1409. testMammoth(t, "json", testJsonH)
  1410. }
  1411. // ----- Raw ---------
  1412. func TestJsonRaw(t *testing.T) {
  1413. doTestRawValue(t, "json", testJsonH)
  1414. }
  1415. func TestBincRaw(t *testing.T) {
  1416. doTestRawValue(t, "binc", testBincH)
  1417. }
  1418. func TestMsgpackRaw(t *testing.T) {
  1419. doTestRawValue(t, "msgpack", testMsgpackH)
  1420. }
  1421. func TestSimpleRaw(t *testing.T) {
  1422. doTestRawValue(t, "simple", testSimpleH)
  1423. }
  1424. func TestCborRaw(t *testing.T) {
  1425. doTestRawValue(t, "cbor", testCborH)
  1426. }
  1427. // ----- ALL (framework based) -----
  1428. func TestAllEncCircularRef(t *testing.T) {
  1429. doTestEncCircularRef(t, "cbor", testCborH)
  1430. }
  1431. func TestAllAnonCycle(t *testing.T) {
  1432. doTestAnonCycle(t, "cbor", testCborH)
  1433. }
  1434. // ----- RPC -----
  1435. func TestBincRpcGo(t *testing.T) {
  1436. testCodecRpcOne(t, GoRpc, testBincH, true, 0)
  1437. }
  1438. func TestSimpleRpcGo(t *testing.T) {
  1439. testCodecRpcOne(t, GoRpc, testSimpleH, true, 0)
  1440. }
  1441. func TestMsgpackRpcGo(t *testing.T) {
  1442. testCodecRpcOne(t, GoRpc, testMsgpackH, true, 0)
  1443. }
  1444. func TestCborRpcGo(t *testing.T) {
  1445. testCodecRpcOne(t, GoRpc, testCborH, true, 0)
  1446. }
  1447. func TestJsonRpcGo(t *testing.T) {
  1448. testCodecRpcOne(t, GoRpc, testJsonH, true, 0)
  1449. }
  1450. func TestMsgpackRpcSpec(t *testing.T) {
  1451. testCodecRpcOne(t, MsgpackSpecRpc, testMsgpackH, true, 0)
  1452. }
  1453. func TestBincUnderlyingType(t *testing.T) {
  1454. testCodecUnderlyingType(t, testBincH)
  1455. }
  1456. func TestJsonLargeInteger(t *testing.T) {
  1457. for _, i := range []uint8{'L', 'A', 0} {
  1458. for _, j := range []interface{}{
  1459. int64(1 << 60),
  1460. -int64(1 << 60),
  1461. 0,
  1462. 1 << 20,
  1463. -(1 << 20),
  1464. uint64(1 << 60),
  1465. uint(0),
  1466. uint(1 << 20),
  1467. } {
  1468. doTestJsonLargeInteger(t, j, i)
  1469. }
  1470. }
  1471. }
  1472. func TestJsonDecodeNonStringScalarInStringContext(t *testing.T) {
  1473. var b = `{"s.true": "true", "b.true": true, "s.false": "false", "b.false": false, "s.10": "10", "i.10": 10, "i.-10": -10}`
  1474. 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"}
  1475. var m map[string]string
  1476. d := NewDecoderBytes([]byte(b), testJsonH)
  1477. d.MustDecode(&m)
  1478. if err := deepEqual(golden, m); err == nil {
  1479. logT(t, "++++ match: decoded: %#v", m)
  1480. } else {
  1481. logT(t, "---- mismatch: %v ==> golden: %#v, decoded: %#v", err, golden, m)
  1482. failT(t)
  1483. }
  1484. }
  1485. func TestJsonEncodeIndent(t *testing.T) {
  1486. v := TestSimplish{
  1487. Ii: -794,
  1488. Ss: `A Man is
  1489. after the new line
  1490. after new line and tab
  1491. `,
  1492. }
  1493. v2 := v
  1494. v.Mm = make(map[string]*TestSimplish)
  1495. for i := 0; i < len(v.Ar); i++ {
  1496. v3 := v2
  1497. v3.Ii += (i * 4)
  1498. v3.Ss = fmt.Sprintf("%d - %s", v3.Ii, v3.Ss)
  1499. if i%2 == 0 {
  1500. v.Ar[i] = &v3
  1501. }
  1502. // v3 = v2
  1503. v.Sl = append(v.Sl, &v3)
  1504. v.Mm[strconv.FormatInt(int64(i), 10)] = &v3
  1505. }
  1506. oldcan := testJsonH.Canonical
  1507. oldIndent := testJsonH.Indent
  1508. oldS2A := testJsonH.StructToArray
  1509. defer func() {
  1510. testJsonH.Canonical = oldcan
  1511. testJsonH.Indent = oldIndent
  1512. testJsonH.StructToArray = oldS2A
  1513. }()
  1514. testJsonH.Canonical = true
  1515. testJsonH.Indent = -1
  1516. testJsonH.StructToArray = false
  1517. var bs []byte
  1518. NewEncoderBytes(&bs, testJsonH).MustEncode(&v)
  1519. txt1Tab := string(bs)
  1520. bs = nil
  1521. testJsonH.Indent = 120
  1522. NewEncoderBytes(&bs, testJsonH).MustEncode(&v)
  1523. txtSpaces := string(bs)
  1524. // fmt.Printf("\n-----------\n%s\n------------\n%s\n-------------\n", txt1Tab, txtSpaces)
  1525. goldenResultTab := `{
  1526. "Ar": [
  1527. {
  1528. "Ar": [
  1529. null,
  1530. null
  1531. ],
  1532. "Ii": -794,
  1533. "Mm": null,
  1534. "Sl": null,
  1535. "Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
  1536. },
  1537. null
  1538. ],
  1539. "Ii": -794,
  1540. "Mm": {
  1541. "0": {
  1542. "Ar": [
  1543. null,
  1544. null
  1545. ],
  1546. "Ii": -794,
  1547. "Mm": null,
  1548. "Sl": null,
  1549. "Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
  1550. },
  1551. "1": {
  1552. "Ar": [
  1553. null,
  1554. null
  1555. ],
  1556. "Ii": -790,
  1557. "Mm": null,
  1558. "Sl": null,
  1559. "Ss": "-790 - A Man is\nafter the new line\n\tafter new line and tab\n"
  1560. }
  1561. },
  1562. "Sl": [
  1563. {
  1564. "Ar": [
  1565. null,
  1566. null
  1567. ],
  1568. "Ii": -794,
  1569. "Mm": null,
  1570. "Sl": null,
  1571. "Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
  1572. },
  1573. {
  1574. "Ar": [
  1575. null,
  1576. null
  1577. ],
  1578. "Ii": -790,
  1579. "Mm": null,
  1580. "Sl": null,
  1581. "Ss": "-790 - A Man is\nafter the new line\n\tafter new line and tab\n"
  1582. }
  1583. ],
  1584. "Ss": "A Man is\nafter the new line\n\tafter new line and tab\n"
  1585. }`
  1586. if txt1Tab != goldenResultTab {
  1587. logT(t, "decoded indented with tabs != expected: \nexpected: %s\nencoded: %s", goldenResultTab, txt1Tab)
  1588. failT(t)
  1589. }
  1590. if txtSpaces != strings.Replace(goldenResultTab, "\t", strings.Repeat(" ", 120), -1) {
  1591. logT(t, "decoded indented with spaces != expected: \nexpected: %s\nencoded: %s", goldenResultTab, txtSpaces)
  1592. failT(t)
  1593. }
  1594. }
  1595. // TODO:
  1596. // Add Tests for:
  1597. // - decoding empty list/map in stream into a nil slice/map
  1598. // - binary(M|Unm)arsher support for time.Time (e.g. cbor encoding)
  1599. // - text(M|Unm)arshaler support for time.Time (e.g. json encoding)
  1600. // - non fast-path scenarios e.g. map[string]uint16, []customStruct.
  1601. // Expand cbor to include indefinite length stuff for this non-fast-path types.
  1602. // This may not be necessary, since we have the manual tests (fastpathEnabled=false) to test/validate with.
  1603. // - CodecSelfer
  1604. // Ensure it is called when (en|de)coding interface{} or reflect.Value (2 different codepaths).
  1605. // - interfaces: textMarshaler, binaryMarshaler, codecSelfer
  1606. // - struct tags:
  1607. // on anonymous fields, _struct (all fields), etc
  1608. // - codecgen of struct containing channels.
  1609. //
  1610. // Add negative tests for failure conditions:
  1611. // - bad input with large array length prefix