codec_test.go 40 KB

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