codecs_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style 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. // Some hints:
  17. // - python msgpack encodes positive numbers as uints, so use uints below
  18. // for positive numbers.
  19. import (
  20. "reflect"
  21. "testing"
  22. "net/rpc"
  23. "bytes"
  24. "time"
  25. "os"
  26. "os/exec"
  27. "io/ioutil"
  28. "path/filepath"
  29. "strconv"
  30. "net"
  31. "fmt"
  32. "flag"
  33. "encoding/gob"
  34. )
  35. var (
  36. testInitDebug bool
  37. testUseIoEncDec bool
  38. _ = fmt.Printf
  39. skipVerifyVal interface{} = &(struct{}{})
  40. timeLoc = time.FixedZone("UTC-08:00", -8*60*60) //time.UTC
  41. timeToCompare = time.Date(2012, 2, 2, 2, 2, 2, 2000, timeLoc) //time.Time{}
  42. //"2012-02-02T02:02:02.000002000Z" //1328148122000002
  43. timeToCompareAs interface{} = timeToCompare.UnixNano()
  44. table []interface{} // main items we encode
  45. tableVerify []interface{} // we verify encoded things against this after decode
  46. tableTestNilVerify []interface{} // for nil interface, use this to verify (rules are different)
  47. tablePythonVerify []interface{} // for verifying for python, since Python sometimes
  48. // will encode a float32 as float64, or large int as uint
  49. testRpcInt = new(TestRpcInt)
  50. testMsgpackH = &MsgpackHandle{}
  51. testBincH = &BincHandle{}
  52. )
  53. func init() {
  54. // delete(testDecOpts.ExtFuncs, timeTyp)
  55. flag.BoolVar(&testInitDebug, "tdbg", false, "Test Debug")
  56. flag.BoolVar(&testUseIoEncDec, "tio", false, "Use IO Reader/Writer for Marshal/Unmarshal")
  57. flag.Parse()
  58. gob.Register(new(TestStruc))
  59. if testInitDebug {
  60. ts0 := newTestStruc(2, false)
  61. fmt.Printf("====> depth: %v, ts: %#v\n", 2, ts0)
  62. }
  63. testMsgpackH.AddExt(byteSliceTyp, 0, testMsgpackH.BinaryEncodeExt, testMsgpackH.BinaryDecodeExt)
  64. testMsgpackH.AddExt(timeTyp, 1, testMsgpackH.TimeEncodeExt, testMsgpackH.TimeDecodeExt)
  65. }
  66. type AnonInTestStruc struct {
  67. AS string
  68. AI64 int64
  69. AI16 int16
  70. AUi64 uint64
  71. ASslice []string
  72. AI64slice []int64
  73. }
  74. type TestStruc struct {
  75. S string
  76. I64 int64
  77. I16 int16
  78. Ui64 uint64
  79. Ui8 uint8
  80. B bool
  81. By byte
  82. Sslice []string
  83. I64slice []int64
  84. I16slice []int16
  85. Ui64slice []uint64
  86. Ui8slice []uint8
  87. Bslice []bool
  88. Byslice []byte
  89. Islice []interface{}
  90. Iptrslice []*int64
  91. AnonInTestStruc
  92. //M map[interface{}]interface{} `json:"-",bson:"-"`
  93. Ms map[string]interface{}
  94. Msi64 map[string]int64
  95. Nintf interface{} //don't set this, so we can test for nil
  96. T time.Time
  97. Nmap map[string]bool //don't set this, so we can test for nil
  98. Nslice []byte //don't set this, so we can test for nil
  99. Nint64 *int64 //don't set this, so we can test for nil
  100. Mtsptr map[string]*TestStruc
  101. Mts map[string]TestStruc
  102. Its []*TestStruc
  103. Nteststruc *TestStruc
  104. }
  105. type TestRpcInt struct {
  106. i int
  107. }
  108. func (r *TestRpcInt) Update(n int, res *int) error { r.i = n; *res = r.i; return nil }
  109. func (r *TestRpcInt) Square(ignore int, res *int) error { *res = r.i * r.i; return nil }
  110. func (r *TestRpcInt) Mult(n int, res *int) error { *res = r.i * n; return nil }
  111. func init() {
  112. primitives := []interface{} {
  113. int8(-8),
  114. int16(-1616),
  115. int32(-32323232),
  116. int64(-6464646464646464),
  117. uint8(192),
  118. uint16(1616),
  119. uint32(32323232),
  120. uint64(6464646464646464),
  121. byte(192),
  122. float32(-3232.0),
  123. float64(-6464646464.0),
  124. float32(3232.0),
  125. float64(6464646464.0),
  126. false,
  127. true,
  128. nil,
  129. timeToCompare,
  130. "someday",
  131. "",
  132. "bytestring",
  133. }
  134. mapsAndStrucs := []interface{}{
  135. map[string]bool{
  136. "true":true,
  137. "false":false,
  138. },
  139. map[string]interface{}{
  140. "true": "True",
  141. "false": false,
  142. "uint16(1616)": uint16(1616),
  143. },
  144. //add a complex combo map in here. (map has list which has map)
  145. //note that after the first thing, everything else should be generic.
  146. map[string]interface{}{
  147. "list": []interface{}{
  148. int16(1616),
  149. int32(32323232),
  150. true,
  151. float32(-3232.0),
  152. map[string]interface{} {
  153. "TRUE":true,
  154. "FALSE":false,
  155. },
  156. []interface{}{true, false},
  157. },
  158. "int32": int32(32323232),
  159. "bool": true,
  160. "LONG STRING": "123456789012345678901234567890123456789012345678901234567890",
  161. "SHORT STRING": "1234567890",
  162. },
  163. map[interface{}]interface{}{
  164. true: "true",
  165. uint8(8): false,
  166. "false": uint8(0),
  167. },
  168. newTestStruc(0, false),
  169. }
  170. table = []interface{}{}
  171. table = append(table, primitives...) //0-19 are primitives
  172. table = append(table, primitives) //20 is a list of primitives
  173. table = append(table, mapsAndStrucs...) //21-24 are maps. 25 is a *struct
  174. // we verify against the same table, but skip 23
  175. // because interface{} equality is not defined exact for exact objects or nil.
  176. var a, b []interface{}
  177. var c map[string]interface{}
  178. a = make([]interface{}, len(table))
  179. copy(a, table)
  180. b = make([]interface{}, len(a[20].([]interface{})))
  181. copy(b, a[20].([]interface{}))
  182. // b[0], b[4], b[8], b[16], b[19] = int8(-8), int8(8), int8(8),
  183. // // []interface {}{int32(1328148122), int16(2000)}, "bytestring"
  184. // timeToCompare, "bytestring"
  185. //b[4], b[8] = int8(8), int8(8)
  186. //b[4] = int8(8)
  187. a[20] = b
  188. a[23] = skipVerifyVal
  189. //a[25] = skipVerifyVal
  190. tableVerify = a
  191. //when decoding into nil, for testing,
  192. //we treat each []byte as string, and uint < 127 are decoded as int8.
  193. a = make([]interface{}, len(tableVerify))
  194. copy(a, tableVerify)
  195. //a[0], a[4], a[8], a[16], a[19] = int8(-8), int8(8), int8(8), timeToCompare, "bytestring"
  196. a[0], a[16], a[19] = int8(-8), timeToCompare, "bytestring"
  197. a[21] = map[string]interface{}{"true":true, "false":false}
  198. a[23] = table[23]
  199. a[25] = skipVerifyVal
  200. tableTestNilVerify = a
  201. //python msgpack encodes large positive numbers as unsigned, and all floats as float64
  202. a = make([]interface{}, len(tableTestNilVerify)-2)
  203. copy(a, tableTestNilVerify)
  204. a[23] = table[23]
  205. a[9], a[11], a[16] = float64(-3232.0), float64(3232.0), uint64(1328148122000002)
  206. b = make([]interface{}, len(a[20].([]interface{})))
  207. copy(b, a[20].([]interface{}))
  208. //b[4], b[8] = int8(8), int8(8)
  209. b[9], b[11], b[16] = float64(-3232.0), float64(3232.0), uint64(1328148122000002)
  210. a[20] = b
  211. c = make(map[string]interface{})
  212. for k, v := range a[23].(map[string]interface{}) {
  213. c[k] = v
  214. }
  215. a[23] = c
  216. c["int32"] = uint32(32323232)
  217. b = c["list"].([]interface{})
  218. b[0], b[1], b[3] = uint16(1616), uint32(32323232), float64(-3232.0)
  219. tablePythonVerify = a
  220. }
  221. func testUnmarshal(v interface{}, data []byte, h Handle) error {
  222. if testUseIoEncDec {
  223. return NewDecoder(bytes.NewBuffer(data), h).Decode(v)
  224. }
  225. return NewDecoderBytes(data, h).Decode(v)
  226. }
  227. func testMarshal(v interface{}, h Handle) (bs []byte, err error) {
  228. if testUseIoEncDec {
  229. var buf bytes.Buffer
  230. err = NewEncoder(&buf, h).Encode(v)
  231. bs = buf.Bytes()
  232. return
  233. }
  234. err = NewEncoderBytes(&bs, h).Encode(v)
  235. return
  236. }
  237. func newTestStruc(depth int, bench bool) (ts *TestStruc) {
  238. var i64a, i64b, i64c, i64d int64 = 64, 6464, 646464, 64646464
  239. ts = &TestStruc {
  240. S: "some string",
  241. I64: 64,
  242. I16: 16,
  243. Ui64: 64,
  244. Ui8: 160,
  245. B: true,
  246. By: 5,
  247. Sslice: []string{"one", "two", "three"},
  248. I64slice: []int64{1, 2, 3},
  249. I16slice: []int16{4, 5, 6},
  250. Ui64slice: []uint64{7, 8, 9},
  251. Ui8slice: []uint8{10, 11, 12},
  252. Bslice: []bool{true, false, true, false},
  253. Byslice: []byte{13, 14, 15},
  254. Islice: []interface{}{"true", true, "no", false, uint8(88), float64(0.4)},
  255. Ms: map[string]interface{}{
  256. "true": "true",
  257. "int64(9)": false,
  258. },
  259. Msi64: map[string]int64{
  260. "one": 1,
  261. "two": 2,
  262. },
  263. T: timeToCompare,
  264. AnonInTestStruc: AnonInTestStruc{
  265. AS: "A-String",
  266. AI64: 64,
  267. AI16: 16,
  268. AUi64: 64,
  269. ASslice: []string{"Aone", "Atwo", "Athree"},
  270. AI64slice: []int64{1, 2, 3},
  271. },
  272. }
  273. //For benchmarks, some things will not work.
  274. if !bench {
  275. //json and bson require string keys in maps
  276. //ts.M = map[interface{}]interface{}{
  277. // true: "true",
  278. // int8(9): false,
  279. //}
  280. //gob cannot encode nil in element in array (encodeArray: nil element)
  281. ts.Iptrslice = []*int64{nil, &i64a, nil, &i64b, nil, &i64c, nil, &i64d, nil}
  282. // ts.Iptrslice = nil
  283. }
  284. if depth > 0 {
  285. depth--
  286. if ts.Mtsptr == nil {
  287. ts.Mtsptr = make(map[string]*TestStruc)
  288. }
  289. if ts.Mts == nil {
  290. ts.Mts = make(map[string]TestStruc)
  291. }
  292. ts.Mtsptr["0"] = newTestStruc(depth, bench)
  293. ts.Mts["0"] = *(ts.Mtsptr["0"])
  294. ts.Its = append(ts.Its, ts.Mtsptr["0"])
  295. }
  296. return
  297. }
  298. // doTestCodecTableOne allows us test for different variations based on arguments passed.
  299. func doTestCodecTableOne(t *testing.T, testNil bool, h Handle,
  300. vs []interface{}, vsVerify []interface{}) {
  301. //if testNil, then just test for when a pointer to a nil interface{} is passed. It should work.
  302. //Current setup allows us test (at least manually) the nil interface or typed interface.
  303. logT(t, "================ TestNil: %v ================\n", testNil)
  304. for i, v0 := range vs {
  305. logT(t, "..............................................")
  306. logT(t, " Testing: #%d:, %T, %#v\n", i, v0, v0)
  307. b0, err := testMarshal(v0, h)
  308. if err != nil {
  309. logT(t, err.Error())
  310. failT(t)
  311. continue
  312. }
  313. logT(t, " Encoded bytes: len: %v, %v\n", len(b0), b0)
  314. var v1 interface{}
  315. if testNil {
  316. err = testUnmarshal(&v1, b0, h)
  317. } else {
  318. if v0 != nil {
  319. v0rt := reflect.TypeOf(v0) // ptr
  320. rv1 := reflect.New(v0rt)
  321. err = testUnmarshal(rv1.Interface(), b0, h)
  322. v1 = rv1.Elem().Interface()
  323. // v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
  324. }
  325. }
  326. logT(t, " v1 returned: %T, %#v", v1, v1)
  327. // if v1 != nil {
  328. // logT(t, " v1 returned: %T, %#v", v1, v1)
  329. // //we always indirect, because ptr to typed value may be passed (if not testNil)
  330. // v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
  331. // }
  332. if err != nil {
  333. logT(t, "-------- Error: %v. Partial return: %v", err, v1)
  334. failT(t)
  335. continue
  336. }
  337. v0check := vsVerify[i]
  338. if v0check == skipVerifyVal {
  339. logT(t, " Nil Check skipped: Decoded: %T, %#v\n", v1, v1)
  340. continue
  341. }
  342. if err = deepEqual(v0check, v1); err == nil {
  343. logT(t, "++++++++ Before and After marshal matched\n")
  344. } else {
  345. logT(t, "-------- Before and After marshal do not match: Error: %v" +
  346. " ====> AGAINST: (%T) %#v, DECODED: (%T) %#v\n", err, v0check, v0check, v1, v1)
  347. failT(t)
  348. }
  349. }
  350. }
  351. func testCodecTableOne(t *testing.T, h Handle) {
  352. // func TestMsgpackAllExperimental(t *testing.T) {
  353. // dopts := testDecOpts(nil, nil, false, true, true),
  354. var oldWriteExt bool
  355. switch v := h.(type) {
  356. case *MsgpackHandle:
  357. oldWriteExt = v.WriteExt
  358. v.WriteExt = true
  359. }
  360. doTestCodecTableOne(t, false, h, table, tableVerify)
  361. switch v := h.(type) {
  362. case *MsgpackHandle:
  363. v.WriteExt = oldWriteExt
  364. }
  365. // func TestMsgpackAll(t *testing.T) {
  366. doTestCodecTableOne(t, false, h, table[:20], tableVerify[:20])
  367. doTestCodecTableOne(t, false, h, table[21:], tableVerify[21:])
  368. // func TestMsgpackNilStringMap(t *testing.T) {
  369. var oldMapType reflect.Type
  370. switch v := h.(type) {
  371. case *MsgpackHandle:
  372. oldMapType = v.MapType
  373. v.MapType = mapStringIntfTyp
  374. case *BincHandle:
  375. oldMapType = v.MapType
  376. v.MapType = mapStringIntfTyp
  377. }
  378. //skip #16 (time.Time), and #20 ([]interface{} containing time.Time)
  379. doTestCodecTableOne(t, true, h, table[:16], tableTestNilVerify[:16])
  380. doTestCodecTableOne(t, true, h, table[17:20], tableTestNilVerify[17:20])
  381. doTestCodecTableOne(t, true, h, table[21:24], tableTestNilVerify[21:24])
  382. switch v := h.(type) {
  383. case *MsgpackHandle:
  384. v.MapType = oldMapType
  385. case *BincHandle:
  386. v.MapType = oldMapType
  387. }
  388. // func TestMsgpackNilIntf(t *testing.T) {
  389. doTestCodecTableOne(t, true, h, table[24:], tableTestNilVerify[24:])
  390. doTestCodecTableOne(t, true, h, table[17:18], tableTestNilVerify[17:18])
  391. }
  392. func testCodecMiscOne(t *testing.T, h Handle) {
  393. b, err := testMarshal(32, h)
  394. // Cannot do this nil one, because faster type assertion decoding will panic
  395. // var i *int32
  396. // if err = testUnmarshal(b, i, nil); err == nil {
  397. // logT(t, "------- Expecting error because we cannot unmarshal to int32 nil ptr")
  398. // t.FailNow()
  399. // }
  400. var i2 int32 = 0
  401. if err = testUnmarshal(&i2, b, h); err != nil {
  402. logT(t, "------- Cannot unmarshal to int32 ptr. Error: %v", err)
  403. t.FailNow()
  404. }
  405. if i2 != int32(32) {
  406. logT(t, "------- didn't unmarshal to 32: Received: %d", i2)
  407. t.FailNow()
  408. }
  409. // func TestMsgpackDecodePtr(t *testing.T) {
  410. ts := newTestStruc(0, false)
  411. b, err = testMarshal(ts, h)
  412. if err != nil {
  413. logT(t, "------- Cannot Marshal pointer to struct. Error: %v", err)
  414. t.FailNow()
  415. } else if len(b) < 40 {
  416. logT(t, "------- Size must be > 40. Size: %d", len(b))
  417. t.FailNow()
  418. }
  419. logT(t, "------- b: %v", b)
  420. ts2 := new(TestStruc)
  421. err = testUnmarshal(ts2, b, h)
  422. if err != nil {
  423. logT(t, "------- Cannot Unmarshal pointer to struct. Error: %v", err)
  424. t.FailNow()
  425. } else if ts2.I64 != 64 {
  426. logT(t, "------- Unmarshal wrong. Expect I64 = 64. Got: %v", ts2.I64)
  427. t.FailNow()
  428. }
  429. // func TestMsgpackIntfDecode(t *testing.T) {
  430. m := map[string]int{"A":2, "B":3, }
  431. p := []interface{}{m}
  432. bs, err := testMarshal(p, h)
  433. if err != nil {
  434. logT(t, "Error marshalling p: %v, Err: %v", p, err)
  435. t.FailNow()
  436. }
  437. m2 := map[string]int{}
  438. p2 := []interface{}{m2}
  439. err = testUnmarshal(&p2, bs, h)
  440. if err != nil {
  441. logT(t, "Error unmarshalling into &p2: %v, Err: %v", p2, err)
  442. t.FailNow()
  443. }
  444. if m2["A"] != 2 || m2["B"] != 3 {
  445. logT(t, "m2 not as expected: expecting: %v, got: %v", m, m2)
  446. t.FailNow()
  447. }
  448. // log("m: %v, m2: %v, p: %v, p2: %v", m, m2, p, p2)
  449. if err = deepEqual(p, p2); err == nil {
  450. logT(t, "p and p2 match")
  451. } else {
  452. logT(t, "Not Equal: %v. p: %v, p2: %v", err, p, p2)
  453. t.FailNow()
  454. }
  455. if err = deepEqual(m, m2); err == nil {
  456. logT(t, "m and m2 match")
  457. } else {
  458. logT(t, "Not Equal: %v. m: %v, m2: %v", err, m, m2)
  459. t.FailNow()
  460. }
  461. // func TestMsgpackDecodeStructSubset(t *testing.T) {
  462. // test that we can decode a subset of the stream
  463. mm := map[string]interface{}{"A": 5, "B": 99, "C": 333, }
  464. bs, err = testMarshal(mm, h)
  465. if err != nil {
  466. logT(t, "Error marshalling m: %v, Err: %v", mm, err)
  467. t.FailNow()
  468. }
  469. type ttt struct {
  470. A uint8
  471. C int32
  472. }
  473. var t2 ttt
  474. err = testUnmarshal(&t2, bs, h)
  475. if err != nil {
  476. logT(t, "Error unmarshalling into &t2: %v, Err: %v", t2, err)
  477. t.FailNow()
  478. }
  479. t3 := ttt{5, 333}
  480. if err = deepEqual(t2, t3); err != nil {
  481. logT(t, "Not Equal: %v. t2: %v, t3: %v", err, t2, t3)
  482. t.FailNow()
  483. }
  484. }
  485. func doTestRpcOne(t *testing.T, rr Rpc, h Handle, callClose, doRequest, doExit bool) {
  486. srv := rpc.NewServer()
  487. srv.Register(testRpcInt)
  488. ln, err := net.Listen("tcp", "127.0.0.1:0")
  489. // log("listener: %v", ln.Addr())
  490. checkErrT(t, err)
  491. defer ln.Close()
  492. // var opts *DecoderOptions
  493. // opts := testDecOpts
  494. // opts.MapType = mapStringIntfTyp
  495. // opts.RawToString = false
  496. serverExitChan := make(chan bool, 1)
  497. serverFn := func() {
  498. for {
  499. conn1, err1 := ln.Accept()
  500. if err1 != nil {
  501. continue
  502. }
  503. bs := make([]byte, 1)
  504. n1, err1 := conn1.Read(bs)
  505. if n1 != 1 || err1 != nil {
  506. conn1.Close()
  507. continue
  508. }
  509. var sc rpc.ServerCodec
  510. switch bs[0] {
  511. case 'R':
  512. sc = rr.ServerCodec(conn1, h)
  513. case 'X':
  514. serverExitChan <- true
  515. // <- serverExitChan
  516. conn1.Close()
  517. return // exit serverFn goroutine
  518. }
  519. if sc == nil {
  520. conn1.Close()
  521. continue
  522. }
  523. srv.ServeCodec(sc)
  524. // for {
  525. // if err1 = srv.ServeRequest(sc); err1 != nil {
  526. // break
  527. // }
  528. // }
  529. // if callClose {
  530. // sc.Close()
  531. // }
  532. }
  533. }
  534. clientFn := func(cc rpc.ClientCodec) {
  535. cl := rpc.NewClientWithCodec(cc)
  536. if callClose {
  537. defer cl.Close()
  538. }
  539. var up, sq, mult int
  540. // log("Calling client")
  541. checkErrT(t, cl.Call("TestRpcInt.Update", 5, &up))
  542. // log("Called TestRpcInt.Update")
  543. checkEqualT(t, testRpcInt.i, 5)
  544. checkEqualT(t, up, 5)
  545. checkErrT(t, cl.Call("TestRpcInt.Square", 1, &sq))
  546. checkEqualT(t, sq, 25)
  547. checkErrT(t, cl.Call("TestRpcInt.Mult", 20, &mult))
  548. checkEqualT(t, mult, 100)
  549. }
  550. connFn := func(req byte) (bs net.Conn) {
  551. // log("calling f1")
  552. bs, err2 := net.Dial(ln.Addr().Network(), ln.Addr().String())
  553. // log("f1. bs: %v, err2: %v", bs, err2)
  554. checkErrT(t, err2)
  555. n1, err2 := bs.Write([]byte{req})
  556. checkErrT(t, err2)
  557. checkEqualT(t, n1, 1)
  558. return
  559. }
  560. go serverFn()
  561. if doRequest {
  562. bs := connFn('R')
  563. cc := rr.ClientCodec(bs, h)
  564. clientFn(cc)
  565. }
  566. if doExit {
  567. bs := connFn('X')
  568. <- serverExitChan
  569. bs.Close()
  570. // serverExitChan <- true
  571. }
  572. }
  573. // Comprehensive testing that generates data encoded from python msgpack,
  574. // and validates that our code can read and write it out accordingly.
  575. // We keep this unexported here, and put actual test in ext_dep_test.go.
  576. // This way, it can be excluded by excluding file completely.
  577. func doTestMsgpackPythonGenStreams(t *testing.T) {
  578. logT(t, "TestPythonGenStreams")
  579. tmpdir, err := ioutil.TempDir("", "golang-msgpack-test")
  580. if err != nil {
  581. logT(t, "-------- Unable to create temp directory\n")
  582. t.FailNow()
  583. }
  584. defer os.RemoveAll(tmpdir)
  585. logT(t, "tmpdir: %v", tmpdir)
  586. cmd := exec.Command("python", "msgpack_test.py", "testdata", tmpdir)
  587. //cmd.Stdin = strings.NewReader("some input")
  588. //cmd.Stdout = &out
  589. var cmdout []byte
  590. if cmdout, err = cmd.CombinedOutput(); err != nil {
  591. logT(t, "-------- Error running python build.py. Err: %v", err)
  592. logT(t, " %v", string(cmdout))
  593. t.FailNow()
  594. }
  595. oldMapType := testMsgpackH.MapType
  596. for i, v := range tablePythonVerify {
  597. testMsgpackH.MapType = oldMapType
  598. //load up the golden file based on number
  599. //decode it
  600. //compare to in-mem object
  601. //encode it again
  602. //compare to output stream
  603. logT(t, "..............................................")
  604. logT(t, " Testing: #%d: %T, %#v\n", i, v, v)
  605. var bss []byte
  606. bss, err = ioutil.ReadFile(filepath.Join(tmpdir, strconv.Itoa(i) + ".golden"))
  607. if err != nil {
  608. logT(t, "-------- Error reading golden file: %d. Err: %v", i, err)
  609. failT(t)
  610. continue
  611. }
  612. testMsgpackH.MapType = mapStringIntfTyp
  613. var v1 interface{}
  614. if err = testUnmarshal(&v1, bss, testMsgpackH); err != nil {
  615. logT(t, "-------- Error decoding stream: %d: Err: %v", i, err)
  616. failT(t)
  617. continue
  618. }
  619. if v == skipVerifyVal {
  620. continue
  621. }
  622. //no need to indirect, because we pass a nil ptr, so we already have the value
  623. //if v1 != nil { v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface() }
  624. if err = deepEqual(v, v1); err == nil {
  625. logT(t, "++++++++ Objects match")
  626. } else {
  627. logT(t, "-------- Objects do not match: %v. Source: %T. Decoded: %T", err, v, v1)
  628. logT(t, "-------- AGAINST: %#v", v)
  629. logT(t, "-------- DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
  630. failT(t)
  631. }
  632. bsb, err := testMarshal(v1, testMsgpackH)
  633. if err != nil {
  634. logT(t, "Error encoding to stream: %d: Err: %v", i, err)
  635. failT(t)
  636. continue
  637. }
  638. if err = deepEqual(bsb, bss); err == nil {
  639. logT(t, "++++++++ Bytes match")
  640. } else {
  641. logT(t, "???????? Bytes do not match. %v.", err)
  642. xs := "--------"
  643. if reflect.ValueOf(v).Kind() == reflect.Map {
  644. xs = " "
  645. logT(t, "%s It's a map. Ok that they don't match (dependent on ordering).", xs)
  646. } else {
  647. logT(t, "%s It's not a map. They should match.", xs)
  648. failT(t)
  649. }
  650. logT(t, "%s FROM_FILE: %4d] %v", xs, len(bss), bss)
  651. logT(t, "%s ENCODED: %4d] %v", xs, len(bsb), bsb)
  652. }
  653. }
  654. testMsgpackH.MapType = oldMapType
  655. }
  656. func TestCodecs(t *testing.T) {
  657. testCodecTableOne(t, testMsgpackH)
  658. testCodecMiscOne(t, testMsgpackH)
  659. testCodecTableOne(t, testBincH)
  660. testCodecMiscOne(t, testBincH)
  661. }
  662. func TestRpcs(t *testing.T) {
  663. doTestRpcOne(t, MsgpackSpecRpc{}, testMsgpackH, true, true, true)
  664. doTestRpcOne(t, GoRpc{}, testMsgpackH, true, true, true)
  665. doTestRpcOne(t, GoRpc{}, testBincH, true, true, true)
  666. }