codec_test.go 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484
  1. // Copyright (c) 2012-2018 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. import (
  5. "bufio"
  6. "bytes"
  7. "encoding/gob"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "math"
  13. "math/rand"
  14. "net"
  15. "net/rpc"
  16. "os"
  17. "os/exec"
  18. "path/filepath"
  19. "reflect"
  20. "runtime"
  21. "strconv"
  22. "strings"
  23. "sync/atomic"
  24. "testing"
  25. "time"
  26. )
  27. func init() {
  28. testPreInitFns = append(testPreInitFns, testInit)
  29. // fmt.Printf("sizeof: Decoder: %v, Encoder: %v, decNaked: %v\n",
  30. // reflect.TypeOf((*Decoder)(nil)).Elem().Size(),
  31. // reflect.TypeOf((*Encoder)(nil)).Elem().Size(),
  32. // reflect.TypeOf((*decNaked)(nil)).Elem().Size(),
  33. // )
  34. }
  35. type testCustomStringT string
  36. // make this a mapbyslice
  37. type testMbsT []interface{}
  38. func (testMbsT) MapBySlice() {}
  39. type testMbsCustStrT []testCustomStringT
  40. func (testMbsCustStrT) MapBySlice() {}
  41. // type testSelferRecur struct{}
  42. // func (s *testSelferRecur) CodecEncodeSelf(e *Encoder) {
  43. // e.MustEncode(s)
  44. // }
  45. // func (s *testSelferRecur) CodecDecodeSelf(d *Decoder) {
  46. // d.MustDecode(s)
  47. // }
  48. type testIntfMapI interface {
  49. GetIntfMapV() string
  50. }
  51. type testIntfMapT1 struct {
  52. IntfMapV string
  53. }
  54. func (x *testIntfMapT1) GetIntfMapV() string { return x.IntfMapV }
  55. type testIntfMapT2 struct {
  56. IntfMapV string
  57. }
  58. func (x testIntfMapT2) GetIntfMapV() string { return x.IntfMapV }
  59. var testErrWriterErr = errors.New("testErrWriterErr")
  60. type testErrWriter struct{}
  61. func (x *testErrWriter) Write(p []byte) (int, error) {
  62. return 0, testErrWriterErr
  63. }
  64. // ----
  65. type testVerifyFlag uint8
  66. const (
  67. _ testVerifyFlag = 1 << iota
  68. testVerifyMapTypeSame
  69. testVerifyMapTypeStrIntf
  70. testVerifyMapTypeIntfIntf
  71. // testVerifySliceIntf
  72. testVerifyForPython
  73. testVerifyDoNil
  74. testVerifyTimeAsInteger
  75. )
  76. func (f testVerifyFlag) isset(v testVerifyFlag) bool {
  77. return f&v == v
  78. }
  79. // const testSkipRPCTests = false
  80. var (
  81. testTableNumPrimitives int
  82. testTableIdxTime int
  83. testTableNumMaps int
  84. // set this when running using bufio, etc
  85. testSkipRPCTests = false
  86. )
  87. var (
  88. skipVerifyVal interface{} = &(struct{}{})
  89. testMapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
  90. // For Go Time, do not use a descriptive timezone.
  91. // It's unnecessary, and makes it harder to do a reflect.DeepEqual.
  92. // The Offset already tells what the offset should be, if not on UTC and unknown zone name.
  93. timeLoc = time.FixedZone("", -8*60*60) // UTC-08:00 //time.UTC-8
  94. timeToCompare1 = time.Date(2012, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
  95. timeToCompare2 = time.Date(1900, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
  96. timeToCompare3 = time.Unix(0, 270).UTC() // use value that must be encoded as uint64 for nanoseconds (for cbor/msgpack comparison)
  97. //timeToCompare4 = time.Time{}.UTC() // does not work well with simple cbor time encoding (overflow)
  98. timeToCompare4 = time.Unix(-2013855848, 4223).UTC()
  99. table []interface{} // main items we encode
  100. // will encode a float32 as float64, or large int as uint
  101. testRpcInt = new(TestRpcInt)
  102. )
  103. var wrapInt64Typ = reflect.TypeOf(wrapInt64(0))
  104. var wrapBytesTyp = reflect.TypeOf(wrapBytes(nil))
  105. var testSelfExtTyp = reflect.TypeOf((*TestSelfExtImpl)(nil)).Elem()
  106. var testSelfExt2Typ = reflect.TypeOf((*TestSelfExtImpl2)(nil)).Elem()
  107. func testByteBuf(in []byte) *bytes.Buffer {
  108. return bytes.NewBuffer(in)
  109. }
  110. type TestABC struct {
  111. A, B, C string
  112. }
  113. func (x *TestABC) MarshalBinary() ([]byte, error) {
  114. return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
  115. }
  116. func (x *TestABC) MarshalText() ([]byte, error) {
  117. return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
  118. }
  119. func (x *TestABC) MarshalJSON() ([]byte, error) {
  120. return []byte(fmt.Sprintf(`"%s %s %s"`, x.A, x.B, x.C)), nil
  121. }
  122. func (x *TestABC) UnmarshalBinary(data []byte) (err error) {
  123. ss := strings.Split(string(data), " ")
  124. x.A, x.B, x.C = ss[0], ss[1], ss[2]
  125. return
  126. }
  127. func (x *TestABC) UnmarshalText(data []byte) (err error) {
  128. return x.UnmarshalBinary(data)
  129. }
  130. func (x *TestABC) UnmarshalJSON(data []byte) (err error) {
  131. return x.UnmarshalBinary(data[1 : len(data)-1])
  132. }
  133. type TestABC2 struct {
  134. A, B, C string
  135. }
  136. func (x TestABC2) MarshalText() ([]byte, error) {
  137. return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
  138. }
  139. func (x *TestABC2) UnmarshalText(data []byte) (err error) {
  140. ss := strings.Split(string(data), " ")
  141. x.A, x.B, x.C = ss[0], ss[1], ss[2]
  142. return
  143. // _, err = fmt.Sscanf(string(data), "%s %s %s", &x.A, &x.B, &x.C)
  144. }
  145. type TestSimplish struct {
  146. Ii int
  147. Ss string
  148. Ar [2]*TestSimplish
  149. Sl []*TestSimplish
  150. Mm map[string]*TestSimplish
  151. }
  152. type TestRpcABC struct {
  153. A, B, C string
  154. }
  155. type TestRpcInt struct {
  156. i int
  157. }
  158. func (r *TestRpcInt) Update(n int, res *int) error { r.i = n; *res = r.i; return nil }
  159. func (r *TestRpcInt) Square(ignore int, res *int) error { *res = r.i * r.i; return nil }
  160. func (r *TestRpcInt) Mult(n int, res *int) error { *res = r.i * n; return nil }
  161. func (r *TestRpcInt) EchoStruct(arg TestRpcABC, res *string) error {
  162. *res = fmt.Sprintf("%#v", arg)
  163. return nil
  164. }
  165. func (r *TestRpcInt) Echo123(args []string, res *string) error {
  166. *res = fmt.Sprintf("%#v", args)
  167. return nil
  168. }
  169. type TestRawValue struct {
  170. R Raw
  171. I int
  172. }
  173. // ----
  174. type testUnixNanoTimeExt struct {
  175. // keep timestamp here, so that do not incur interface-conversion costs
  176. // ts int64
  177. }
  178. func (x *testUnixNanoTimeExt) WriteExt(v interface{}) []byte {
  179. v2 := v.(*time.Time)
  180. bs := make([]byte, 8)
  181. bigen.PutUint64(bs, uint64(v2.UnixNano()))
  182. return bs
  183. }
  184. func (x *testUnixNanoTimeExt) ReadExt(v interface{}, bs []byte) {
  185. v2 := v.(*time.Time)
  186. ui := bigen.Uint64(bs)
  187. *v2 = time.Unix(0, int64(ui)).UTC()
  188. }
  189. func (x *testUnixNanoTimeExt) ConvertExt(v interface{}) interface{} {
  190. v2 := v.(*time.Time) // structs are encoded by passing the ptr
  191. return v2.UTC().UnixNano()
  192. }
  193. func (x *testUnixNanoTimeExt) UpdateExt(dest interface{}, v interface{}) {
  194. tt := dest.(*time.Time)
  195. switch v2 := v.(type) {
  196. case int64:
  197. *tt = time.Unix(0, v2).UTC()
  198. case uint64:
  199. *tt = time.Unix(0, int64(v2)).UTC()
  200. //case float64:
  201. //case string:
  202. default:
  203. panic(fmt.Sprintf("unsupported format for time conversion: expecting int64/uint64; got %T", v))
  204. }
  205. }
  206. // ----
  207. type wrapInt64Ext int64
  208. func (x *wrapInt64Ext) WriteExt(v interface{}) []byte {
  209. v2 := uint64(int64(v.(wrapInt64)))
  210. bs := make([]byte, 8)
  211. bigen.PutUint64(bs, v2)
  212. return bs
  213. }
  214. func (x *wrapInt64Ext) ReadExt(v interface{}, bs []byte) {
  215. v2 := v.(*wrapInt64)
  216. ui := bigen.Uint64(bs)
  217. *v2 = wrapInt64(int64(ui))
  218. }
  219. func (x *wrapInt64Ext) ConvertExt(v interface{}) interface{} {
  220. return int64(v.(wrapInt64))
  221. }
  222. func (x *wrapInt64Ext) UpdateExt(dest interface{}, v interface{}) {
  223. v2 := dest.(*wrapInt64)
  224. *v2 = wrapInt64(v.(int64))
  225. }
  226. // ----
  227. type wrapBytesExt struct{}
  228. func (x *wrapBytesExt) WriteExt(v interface{}) []byte {
  229. return ([]byte)(v.(wrapBytes))
  230. }
  231. func (x *wrapBytesExt) ReadExt(v interface{}, bs []byte) {
  232. v2 := v.(*wrapBytes)
  233. *v2 = wrapBytes(bs)
  234. }
  235. func (x *wrapBytesExt) ConvertExt(v interface{}) interface{} {
  236. return ([]byte)(v.(wrapBytes))
  237. }
  238. func (x *wrapBytesExt) UpdateExt(dest interface{}, v interface{}) {
  239. v2 := dest.(*wrapBytes)
  240. // some formats (e.g. json) cannot nakedly determine []byte from string, so expect both
  241. switch v3 := v.(type) {
  242. case []byte:
  243. *v2 = wrapBytes(v3)
  244. case string:
  245. *v2 = wrapBytes([]byte(v3))
  246. default:
  247. panic("UpdateExt for wrapBytesExt expects string or []byte")
  248. }
  249. // *v2 = wrapBytes(v.([]byte))
  250. }
  251. // ----
  252. // timeExt is an extension handler for time.Time, that uses binc model for encoding/decoding time.
  253. // we used binc model, as that is the only custom time representation that we designed ourselves.
  254. type timeExt struct{}
  255. func (x timeExt) WriteExt(v interface{}) (bs []byte) {
  256. switch v2 := v.(type) {
  257. case time.Time:
  258. bs = bincEncodeTime(v2)
  259. case *time.Time:
  260. bs = bincEncodeTime(*v2)
  261. default:
  262. panic(fmt.Errorf("unsupported format for time conversion: expecting time.Time; got %T", v2))
  263. }
  264. return
  265. }
  266. func (x timeExt) ReadExt(v interface{}, bs []byte) {
  267. tt, err := bincDecodeTime(bs)
  268. if err != nil {
  269. panic(err)
  270. }
  271. *(v.(*time.Time)) = tt
  272. }
  273. func (x timeExt) ConvertExt(v interface{}) interface{} {
  274. return x.WriteExt(v)
  275. }
  276. func (x timeExt) UpdateExt(v interface{}, src interface{}) {
  277. x.ReadExt(v, src.([]byte))
  278. }
  279. // ----
  280. func testCodecEncode(ts interface{}, bsIn []byte,
  281. fn func([]byte) *bytes.Buffer, h Handle) (bs []byte, err error) {
  282. return sTestCodecEncode(ts, bsIn, fn, h, basicHandle(h))
  283. }
  284. func testCodecDecode(bs []byte, ts interface{}, h Handle) (err error) {
  285. return sTestCodecDecode(bs, ts, h, basicHandle(h))
  286. }
  287. func checkErrT(t *testing.T, err error) {
  288. if err != nil {
  289. failT(t, err.Error())
  290. }
  291. }
  292. func checkEqualT(t *testing.T, v1 interface{}, v2 interface{}, desc string) {
  293. if err := deepEqual(v1, v2); err != nil {
  294. failT(t, "Not Equal: %s: %v. v1: %v, v2: %v", desc, err, v1, v2)
  295. }
  296. }
  297. func testInit() {
  298. gob.Register(new(TestStrucFlex))
  299. if testInitDebug {
  300. ts0 := newTestStrucFlex(2, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
  301. logTv(nil, "====> depth: %v, ts: %#v\n", 2, ts0)
  302. }
  303. for _, v := range testHandles {
  304. bh := basicHandle(v)
  305. // pre-fill them first
  306. bh.EncodeOptions = testEncodeOptions
  307. bh.DecodeOptions = testDecodeOptions
  308. // bh.InterfaceReset = true
  309. // bh.PreferArrayOverSlice = true
  310. // modify from flag'ish things
  311. bh.MaxInitLen = testMaxInitLen
  312. }
  313. testMsgpackH.WriteExt = true
  314. var tTimeExt timeExt
  315. var tBytesExt wrapBytesExt
  316. var tI64Ext wrapInt64Ext
  317. // create legacy functions suitable for deprecated AddExt functionality,
  318. // and use on some places for testSimpleH e.g. for time.Time and wrapInt64
  319. var (
  320. myExtEncFn = func(x BytesExt, rv reflect.Value) (bs []byte, err error) {
  321. defer panicToErr(errDecoratorDef{}, &err)
  322. bs = x.WriteExt(rv.Interface())
  323. return
  324. }
  325. myExtDecFn = func(x BytesExt, rv reflect.Value, bs []byte) (err error) {
  326. defer panicToErr(errDecoratorDef{}, &err)
  327. x.ReadExt(rv.Interface(), bs)
  328. return
  329. }
  330. timeExtEncFn = func(rv reflect.Value) (bs []byte, err error) { return myExtEncFn(tTimeExt, rv) }
  331. timeExtDecFn = func(rv reflect.Value, bs []byte) (err error) { return myExtDecFn(tTimeExt, rv, bs) }
  332. wrapInt64ExtEncFn = func(rv reflect.Value) (bs []byte, err error) { return myExtEncFn(&tI64Ext, rv) }
  333. wrapInt64ExtDecFn = func(rv reflect.Value, bs []byte) (err error) { return myExtDecFn(&tI64Ext, rv, bs) }
  334. )
  335. chkErr := func(err error) {
  336. if err != nil {
  337. panic(err)
  338. }
  339. }
  340. // time.Time is a native type, so extensions will have no effect.
  341. // However, we add these here to ensure nothing happens.
  342. chkErr(testSimpleH.AddExt(timeTyp, 1, timeExtEncFn, timeExtDecFn))
  343. // testBincH.SetBytesExt(timeTyp, 1, timeExt{}) // time is builtin for binc
  344. chkErr(testMsgpackH.SetBytesExt(timeTyp, 1, timeExt{}))
  345. chkErr(testCborH.SetInterfaceExt(timeTyp, 1, &testUnixNanoTimeExt{}))
  346. // testJsonH.SetInterfaceExt(timeTyp, 1, &testUnixNanoTimeExt{})
  347. // Add extensions for the testSelfExt
  348. chkErr(testSimpleH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
  349. chkErr(testMsgpackH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
  350. chkErr(testBincH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
  351. chkErr(testJsonH.SetInterfaceExt(testSelfExtTyp, 78, SelfExt))
  352. chkErr(testCborH.SetInterfaceExt(testSelfExtTyp, 78, SelfExt))
  353. chkErr(testSimpleH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
  354. chkErr(testMsgpackH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
  355. chkErr(testBincH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
  356. chkErr(testJsonH.SetInterfaceExt(testSelfExt2Typ, 79, SelfExt))
  357. chkErr(testCborH.SetInterfaceExt(testSelfExt2Typ, 79, SelfExt))
  358. // Now, add extensions for the type wrapInt64 and wrapBytes,
  359. // so we can execute the Encode/Decode Ext paths.
  360. chkErr(testSimpleH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
  361. chkErr(testMsgpackH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
  362. chkErr(testBincH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
  363. chkErr(testJsonH.SetInterfaceExt(wrapBytesTyp, 32, &tBytesExt))
  364. chkErr(testCborH.SetInterfaceExt(wrapBytesTyp, 32, &tBytesExt))
  365. chkErr(testSimpleH.AddExt(wrapInt64Typ, 16, wrapInt64ExtEncFn, wrapInt64ExtDecFn))
  366. // chkErr(testSimpleH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
  367. chkErr(testMsgpackH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
  368. chkErr(testBincH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
  369. chkErr(testJsonH.SetInterfaceExt(wrapInt64Typ, 16, &tI64Ext))
  370. chkErr(testCborH.SetInterfaceExt(wrapInt64Typ, 16, &tI64Ext))
  371. // primitives MUST be an even number, so it can be used as a mapBySlice also.
  372. primitives := []interface{}{
  373. int8(-8),
  374. int16(-1616),
  375. int32(-32323232),
  376. int64(-6464646464646464),
  377. uint8(192),
  378. uint16(1616),
  379. uint32(32323232),
  380. uint64(6464646464646464),
  381. byte(192),
  382. float32(-3232.0),
  383. float64(-6464646464.0),
  384. float32(3232.0),
  385. float64(6464.0),
  386. float64(6464646464.0),
  387. false,
  388. true,
  389. "null",
  390. nil,
  391. "some&day>some<day",
  392. timeToCompare1,
  393. "",
  394. timeToCompare2,
  395. "bytestring",
  396. timeToCompare3,
  397. "none",
  398. timeToCompare4,
  399. }
  400. maps := []interface{}{
  401. map[string]bool{
  402. "true": true,
  403. "false": false,
  404. },
  405. map[string]interface{}{
  406. "true": "True",
  407. "false": false,
  408. "uint16(1616)": uint16(1616),
  409. },
  410. //add a complex combo map in here. (map has list which has map)
  411. //note that after the first thing, everything else should be generic.
  412. map[string]interface{}{
  413. "list": []interface{}{
  414. int16(1616),
  415. int32(32323232),
  416. true,
  417. float32(-3232.0),
  418. map[string]interface{}{
  419. "TRUE": true,
  420. "FALSE": false,
  421. },
  422. []interface{}{true, false},
  423. },
  424. "int32": int32(32323232),
  425. "bool": true,
  426. "LONG STRING": `
  427. 1234567890 1234567890
  428. 1234567890 1234567890
  429. 1234567890 1234567890
  430. ABCDEDFGHIJKLMNOPQRSTUVWXYZ
  431. abcdedfghijklmnopqrstuvwxyz
  432. ABCDEDFGHIJKLMNOPQRSTUVWXYZ
  433. abcdedfghijklmnopqrstuvwxyz
  434. "ABCDEDFGHIJKLMNOPQRSTUVWXYZ"
  435. ' a tab '
  436. \a\b\c\d\e
  437. \b\f\n\r\t all literally
  438. ugorji
  439. `,
  440. "SHORT STRING": "1234567890",
  441. },
  442. map[interface{}]interface{}{
  443. true: "true",
  444. uint8(138): false,
  445. false: uint8(200),
  446. },
  447. }
  448. testTableNumPrimitives = len(primitives)
  449. testTableIdxTime = testTableNumPrimitives - 8
  450. testTableNumMaps = len(maps)
  451. table = []interface{}{}
  452. table = append(table, primitives...)
  453. table = append(table, primitives)
  454. table = append(table, testMbsT(primitives))
  455. table = append(table, maps...)
  456. table = append(table, newTestStrucFlex(0, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly))
  457. // table = append(table, newTestStrucFlex(0, testNumRepeatString, true, !testSkipIntf, testMapStringKeyOnly))
  458. }
  459. func testTableVerify(f testVerifyFlag, h Handle) (av []interface{}) {
  460. av = make([]interface{}, len(table))
  461. lp := testTableNumPrimitives + 4
  462. // doNil := f & testVerifyDoNil == testVerifyDoNil
  463. // doPython := f & testVerifyForPython == testVerifyForPython
  464. switch {
  465. case f.isset(testVerifyForPython):
  466. for i, v := range table {
  467. if i == testTableNumPrimitives+1 || i > lp { // testTableNumPrimitives+1 is the mapBySlice
  468. av[i] = skipVerifyVal
  469. continue
  470. }
  471. av[i] = testVerifyVal(v, f, h)
  472. }
  473. // only do the python verify up to the maps, skipping the last 2 maps.
  474. av = av[:testTableNumPrimitives+2+testTableNumMaps-2]
  475. case f.isset(testVerifyDoNil):
  476. for i, v := range table {
  477. if i > lp {
  478. av[i] = skipVerifyVal
  479. continue
  480. }
  481. av[i] = testVerifyVal(v, f, h)
  482. }
  483. default:
  484. for i, v := range table {
  485. if i == lp {
  486. av[i] = skipVerifyVal
  487. continue
  488. }
  489. //av[i] = testVerifyVal(v, testVerifyMapTypeSame)
  490. switch v.(type) {
  491. case []interface{}:
  492. av[i] = testVerifyVal(v, f, h)
  493. case testMbsT:
  494. av[i] = testVerifyVal(v, f, h)
  495. case map[string]interface{}:
  496. av[i] = testVerifyVal(v, f, h)
  497. case map[interface{}]interface{}:
  498. av[i] = testVerifyVal(v, f, h)
  499. case time.Time:
  500. av[i] = testVerifyVal(v, f, h)
  501. default:
  502. av[i] = v
  503. }
  504. }
  505. }
  506. return
  507. }
  508. func testVerifyValInt(v int64, isMsgp bool) (v2 interface{}) {
  509. if isMsgp {
  510. if v >= 0 && v <= 127 {
  511. v2 = uint64(v)
  512. } else {
  513. v2 = int64(v)
  514. }
  515. } else if v >= 0 {
  516. v2 = uint64(v)
  517. } else {
  518. v2 = int64(v)
  519. }
  520. return
  521. }
  522. func testVerifyVal(v interface{}, f testVerifyFlag, h Handle) (v2 interface{}) {
  523. //for python msgpack,
  524. // - all positive integers are unsigned 64-bit ints
  525. // - all floats are float64
  526. _, isMsgp := h.(*MsgpackHandle)
  527. _, isCbor := h.(*CborHandle)
  528. switch iv := v.(type) {
  529. case int8:
  530. v2 = testVerifyValInt(int64(iv), isMsgp)
  531. // fmt.Printf(">>>> is msgp: %v, v: %T, %v ==> v2: %T, %v\n", isMsgp, v, v, v2, v2)
  532. case int16:
  533. v2 = testVerifyValInt(int64(iv), isMsgp)
  534. case int32:
  535. v2 = testVerifyValInt(int64(iv), isMsgp)
  536. case int64:
  537. v2 = testVerifyValInt(int64(iv), isMsgp)
  538. case uint8:
  539. v2 = uint64(iv)
  540. case uint16:
  541. v2 = uint64(iv)
  542. case uint32:
  543. v2 = uint64(iv)
  544. case uint64:
  545. v2 = uint64(iv)
  546. case float32:
  547. v2 = float64(iv)
  548. case float64:
  549. v2 = float64(iv)
  550. case []interface{}:
  551. m2 := make([]interface{}, len(iv))
  552. for j, vj := range iv {
  553. m2[j] = testVerifyVal(vj, f, h)
  554. }
  555. v2 = m2
  556. case testMbsT:
  557. m2 := make([]interface{}, len(iv))
  558. for j, vj := range iv {
  559. m2[j] = testVerifyVal(vj, f, h)
  560. }
  561. v2 = testMbsT(m2)
  562. case map[string]bool:
  563. switch {
  564. case f.isset(testVerifyMapTypeSame):
  565. m2 := make(map[string]bool)
  566. for kj, kv := range iv {
  567. m2[kj] = kv
  568. }
  569. v2 = m2
  570. case f.isset(testVerifyMapTypeStrIntf):
  571. m2 := make(map[string]interface{})
  572. for kj, kv := range iv {
  573. m2[kj] = kv
  574. }
  575. v2 = m2
  576. case f.isset(testVerifyMapTypeIntfIntf):
  577. m2 := make(map[interface{}]interface{})
  578. for kj, kv := range iv {
  579. m2[kj] = kv
  580. }
  581. v2 = m2
  582. }
  583. case map[string]interface{}:
  584. switch {
  585. case f.isset(testVerifyMapTypeSame):
  586. m2 := make(map[string]interface{})
  587. for kj, kv := range iv {
  588. m2[kj] = testVerifyVal(kv, f, h)
  589. }
  590. v2 = m2
  591. case f.isset(testVerifyMapTypeStrIntf):
  592. m2 := make(map[string]interface{})
  593. for kj, kv := range iv {
  594. m2[kj] = testVerifyVal(kv, f, h)
  595. }
  596. v2 = m2
  597. case f.isset(testVerifyMapTypeIntfIntf):
  598. m2 := make(map[interface{}]interface{})
  599. for kj, kv := range iv {
  600. m2[kj] = testVerifyVal(kv, f, h)
  601. }
  602. v2 = m2
  603. }
  604. case map[interface{}]interface{}:
  605. m2 := make(map[interface{}]interface{})
  606. for kj, kv := range iv {
  607. m2[testVerifyVal(kj, f, h)] = testVerifyVal(kv, f, h)
  608. }
  609. v2 = m2
  610. case time.Time:
  611. switch {
  612. case f.isset(testVerifyTimeAsInteger):
  613. if iv2 := iv.UnixNano(); iv2 >= 0 {
  614. v2 = uint64(iv2)
  615. } else {
  616. v2 = int64(iv2)
  617. }
  618. case isMsgp:
  619. v2 = iv.UTC()
  620. case isCbor:
  621. // fmt.Printf("%%%% cbor verifier\n")
  622. v2 = iv.UTC().Round(time.Microsecond)
  623. default:
  624. v2 = v
  625. }
  626. default:
  627. v2 = v
  628. }
  629. return
  630. }
  631. func testUnmarshal(v interface{}, data []byte, h Handle) (err error) {
  632. return testCodecDecode(data, v, h)
  633. }
  634. func testMarshal(v interface{}, h Handle) (bs []byte, err error) {
  635. return testCodecEncode(v, nil, testByteBuf, h)
  636. }
  637. func testMarshalErr(v interface{}, h Handle, t *testing.T, name string) (bs []byte) {
  638. t.Helper()
  639. bs, err := testMarshal(v, h)
  640. if err != nil {
  641. logT("%s: marshal failed: %v", name, err)
  642. failT(t, "Error encoding %s: %v, Err: %v", name, v, err)
  643. }
  644. return
  645. }
  646. func testUnmarshalErr(v interface{}, data []byte, h Handle, t *testing.T, name string) {
  647. t.Helper()
  648. if err := testUnmarshal(v, data, h); err != nil {
  649. logT("%s: unmarshal failed: %v", name, err)
  650. failTv(t, "Error Decoding into %s: %v, Err: %v", name, v, err)
  651. }
  652. }
  653. func testDeepEqualErr(v1, v2 interface{}, t *testing.T, name string) {
  654. t.Helper()
  655. if err := deepEqual(v1, v2); err == nil {
  656. logTv(t, "%s: values equal", name)
  657. } else {
  658. logT(t, "%s: values not equal: %v", name, err)
  659. failTv(t, "%s: values not equal: %v. 1: %#v, 2: %#v", name, err, v1, v2)
  660. }
  661. }
  662. func testReadWriteCloser(c io.ReadWriteCloser) io.ReadWriteCloser {
  663. if testRpcBufsize <= 0 && rand.Int63()%2 == 0 {
  664. return c
  665. }
  666. return struct {
  667. io.Closer
  668. *bufio.Reader
  669. *bufio.Writer
  670. }{c, bufio.NewReaderSize(c, testRpcBufsize), bufio.NewWriterSize(c, testRpcBufsize)}
  671. }
  672. // doTestCodecTableOne allows us test for different variations based on arguments passed.
  673. func doTestCodecTableOne(t *testing.T, testNil bool, h Handle,
  674. vs []interface{}, vsVerify []interface{}) {
  675. //if testNil, then just test for when a pointer to a nil interface{} is passed. It should work.
  676. //Current setup allows us test (at least manually) the nil interface or typed interface.
  677. logTv(t, "================ TestNil: %v ================\n", testNil)
  678. for i, v0 := range vs {
  679. logTv(t, "..............................................")
  680. logTv(t, " Testing: #%d:, %T, %#v\n", i, v0, v0)
  681. // if a TestStrucFlex and we are doing a testNil,
  682. // ensure the fields which are not encodeable are set to nil appropriately
  683. // i.e. MstrUi64TSelf
  684. var mapMstrUi64TSelf map[stringUint64T]*stringUint64T
  685. var mapMsu2wss map[stringUint64T]wrapStringSlice
  686. tsflex, _ := v0.(*TestStrucFlex)
  687. if testNil && tsflex != nil {
  688. mapMstrUi64TSelf = tsflex.MstrUi64TSelf
  689. mapMsu2wss = tsflex.Msu2wss
  690. tsflex.MstrUi64TSelf = nil
  691. tsflex.Msu2wss = nil
  692. }
  693. b0 := testMarshalErr(v0, h, t, "v0")
  694. var b1 = b0
  695. if len(b1) > 256 {
  696. b1 = b1[:256]
  697. }
  698. if h.isBinary() {
  699. logTv(t, " Encoded bytes: len: %v, %v\n", len(b0), b1)
  700. } else {
  701. logTv(t, " Encoded string: len: %v, %s\n", len(b0), b0)
  702. // println("########### encoded string: " + string(b0))
  703. }
  704. var v1 interface{}
  705. var err error
  706. if testNil {
  707. err = testUnmarshal(&v1, b0, h)
  708. if tsflex != nil {
  709. tsflex.MstrUi64TSelf = mapMstrUi64TSelf
  710. tsflex.Msu2wss = mapMsu2wss
  711. }
  712. } else {
  713. if v0 != nil {
  714. v0rt := reflect.TypeOf(v0) // ptr
  715. if v0rt.Kind() == reflect.Ptr {
  716. err = testUnmarshal(v0, b0, h)
  717. v1 = v0
  718. } else {
  719. rv1 := reflect.New(v0rt)
  720. err = testUnmarshal(rv1.Interface(), b0, h)
  721. v1 = rv1.Elem().Interface()
  722. // v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
  723. }
  724. }
  725. }
  726. logTv(t, " v1 returned: %T, %v %#v", v1, v1, v1)
  727. // if v1 != nil {
  728. // logT(t, " v1 returned: %T, %#v", v1, v1)
  729. // //we always indirect, because ptr to typed value may be passed (if not testNil)
  730. // v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
  731. // }
  732. if err != nil {
  733. failT(t, "-------- Error: %v. Partial return: %v", err, v1)
  734. }
  735. v0check := vsVerify[i]
  736. if v0check == skipVerifyVal {
  737. logTv(t, " Nil Check skipped: Decoded: %T, %#v\n", v1, v1)
  738. continue
  739. }
  740. if err = deepEqual(v0check, v1); err == nil {
  741. logTv(t, "++++++++ Before and After marshal matched\n")
  742. } else {
  743. // logT(t, "-------- Before and After marshal do not match: Error: %v"+
  744. // " ====> GOLDEN: (%T) %#v, DECODED: (%T) %#v\n", err, v0check, v0check, v1, v1)
  745. logT(t, "-------- FAIL: Before and After marshal do not match: Error: %v", err)
  746. logTv(t, " ....... GOLDEN: (%T) %v %#v", v0check, v0check, v0check)
  747. logTv(t, " ....... DECODED: (%T) %v %#v", v1, v1, v1)
  748. failT(t)
  749. }
  750. }
  751. }
  752. func testCodecTableOne(t *testing.T, h Handle) {
  753. testOnce.Do(testInitAll)
  754. // func TestMsgpackAllExperimental(t *testing.T) {
  755. // dopts := testDecOpts(nil, nil, false, true, true),
  756. numPrim, numMap, idxTime, idxMap := testTableNumPrimitives, testTableNumMaps, testTableIdxTime, testTableNumPrimitives+2
  757. //println("#################")
  758. tableVerify := testTableVerify(testVerifyMapTypeSame, h)
  759. tableTestNilVerify := testTableVerify(testVerifyDoNil|testVerifyMapTypeStrIntf, h)
  760. switch v := h.(type) {
  761. case *MsgpackHandle:
  762. var oldWriteExt bool
  763. _ = oldWriteExt
  764. oldWriteExt = v.WriteExt
  765. v.WriteExt = true
  766. doTestCodecTableOne(t, false, h, table, tableVerify)
  767. v.WriteExt = oldWriteExt
  768. case *JsonHandle:
  769. //skip []interface{} containing time.Time, as it encodes as a number, but cannot decode back to time.Time.
  770. //As there is no real support for extension tags in json, this must be skipped.
  771. doTestCodecTableOne(t, false, h, table[:numPrim], tableVerify[:numPrim])
  772. doTestCodecTableOne(t, false, h, table[idxMap:], tableVerify[idxMap:])
  773. default:
  774. doTestCodecTableOne(t, false, h, table, tableVerify)
  775. }
  776. // func TestMsgpackAll(t *testing.T) {
  777. // //skip []interface{} containing time.Time
  778. // doTestCodecTableOne(t, false, h, table[:numPrim], tableVerify[:numPrim])
  779. // doTestCodecTableOne(t, false, h, table[numPrim+1:], tableVerify[numPrim+1:])
  780. // func TestMsgpackNilStringMap(t *testing.T) {
  781. var oldMapType reflect.Type
  782. v := basicHandle(h)
  783. oldMapType, v.MapType = v.MapType, testMapStrIntfTyp
  784. // defer func() { v.MapType = oldMapType }()
  785. //skip time.Time, []interface{} containing time.Time, last map, and newStruc
  786. doTestCodecTableOne(t, true, h, table[:idxTime], tableTestNilVerify[:idxTime])
  787. doTestCodecTableOne(t, true, h, table[idxMap:idxMap+numMap-1], tableTestNilVerify[idxMap:idxMap+numMap-1]) // failing one for msgpack
  788. v.MapType = oldMapType
  789. // func TestMsgpackNilIntf(t *testing.T) {
  790. //do last map and newStruc
  791. idx2 := idxMap + numMap - 1
  792. doTestCodecTableOne(t, true, h, table[idx2:], tableTestNilVerify[idx2:])
  793. //TODO? What is this one?
  794. //doTestCodecTableOne(t, true, h, table[17:18], tableTestNilVerify[17:18])
  795. }
  796. func testCodecMiscOne(t *testing.T, h Handle) {
  797. var err error
  798. testOnce.Do(testInitAll)
  799. b := testMarshalErr(32, h, t, "32")
  800. // Cannot do this nil one, because faster type assertion decoding will panic
  801. // var i *int32
  802. // if err = testUnmarshal(b, i, nil); err == nil {
  803. // logT(t, "------- Expecting error because we cannot unmarshal to int32 nil ptr")
  804. // failT(t)
  805. // }
  806. var i2 int32
  807. testUnmarshalErr(&i2, b, h, t, "int32-ptr")
  808. if i2 != int32(32) {
  809. failT(t, "------- didn't unmarshal to 32: Received: %d", i2)
  810. }
  811. // func TestMsgpackDecodePtr(t *testing.T) {
  812. ts := newTestStrucFlex(testDepth, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
  813. b = testMarshalErr(ts, h, t, "pointer-to-struct")
  814. if len(b) < 40 {
  815. failT(t, "------- Size must be > 40. Size: %d", len(b))
  816. }
  817. var b1 = b
  818. if len(b1) > 256 {
  819. b1 = b1[:256]
  820. }
  821. if h.isBinary() {
  822. logTv(t, "------- b: size: %v, value: %v", len(b), b1)
  823. } else {
  824. logTv(t, "------- b: size: %v, value: %s", len(b), b1)
  825. }
  826. ts2 := emptyTestStrucFlex()
  827. testUnmarshalErr(ts2, b, h, t, "pointer-to-struct")
  828. if ts2.I64 != math.MaxInt64*2/3 {
  829. failT(t, "------- Unmarshal wrong. Expect I64 = 64. Got: %v", ts2.I64)
  830. }
  831. // func TestMsgpackIntfDecode(t *testing.T) {
  832. m := map[string]int{"A": 2, "B": 3}
  833. p := []interface{}{m}
  834. bs := testMarshalErr(p, h, t, "p")
  835. m2 := map[string]int{}
  836. p2 := []interface{}{m2}
  837. testUnmarshalErr(&p2, bs, h, t, "&p2")
  838. if m2["A"] != 2 || m2["B"] != 3 {
  839. failT(t, "FAIL: m2 not as expected: expecting: %v, got: %v", m, m2)
  840. }
  841. // log("m: %v, m2: %v, p: %v, p2: %v", m, m2, p, p2)
  842. checkEqualT(t, p, p2, "p=p2")
  843. checkEqualT(t, m, m2, "m=m2")
  844. if err = deepEqual(p, p2); err == nil {
  845. logTv(t, "p and p2 match")
  846. } else {
  847. failT(t, "Not Equal: %v. p: %v, p2: %v", err, p, p2)
  848. }
  849. if err = deepEqual(m, m2); err == nil {
  850. logTv(t, "m and m2 match")
  851. } else {
  852. failT(t, "Not Equal: %v. m: %v, m2: %v", err, m, m2)
  853. }
  854. // func TestMsgpackDecodeStructSubset(t *testing.T) {
  855. // test that we can decode a subset of the stream
  856. mm := map[string]interface{}{"A": 5, "B": 99, "C": 333}
  857. bs = testMarshalErr(mm, h, t, "mm")
  858. type ttt struct {
  859. A uint8
  860. C int32
  861. }
  862. var t2 ttt
  863. testUnmarshalErr(&t2, bs, h, t, "t2")
  864. t3 := ttt{5, 333}
  865. checkEqualT(t, t2, t3, "t2=t3")
  866. // println(">>>>>")
  867. // test simple arrays, non-addressable arrays, slices
  868. type tarr struct {
  869. A int64
  870. B [3]int64
  871. C []byte
  872. D [3]byte
  873. }
  874. var tarr0 = tarr{1, [3]int64{2, 3, 4}, []byte{4, 5, 6}, [3]byte{7, 8, 9}}
  875. // test both pointer and non-pointer (value)
  876. for _, tarr1 := range []interface{}{tarr0, &tarr0} {
  877. bs = testMarshalErr(tarr1, h, t, "tarr1")
  878. if _, ok := h.(*JsonHandle); ok {
  879. logTv(t, "Marshal as: %s", bs)
  880. }
  881. var tarr2 tarr
  882. testUnmarshalErr(&tarr2, bs, h, t, "tarr2")
  883. checkEqualT(t, tarr0, tarr2, "tarr0=tarr2")
  884. }
  885. // test byte array, even if empty (msgpack only)
  886. if h == testMsgpackH {
  887. type ystruct struct {
  888. Anarray []byte
  889. }
  890. var ya = ystruct{}
  891. testUnmarshalErr(&ya, []byte{0x91, 0x90}, h, t, "ya")
  892. }
  893. var tt1, tt2 time.Time
  894. tt2 = time.Now()
  895. bs = testMarshalErr(tt1, h, t, "zero-time-enc")
  896. testUnmarshalErr(&tt2, bs, h, t, "zero-time-dec")
  897. testDeepEqualErr(tt1, tt2, t, "zero-time-eq")
  898. // test encoding a slice of byte (but not []byte) and decoding into a []byte
  899. var sw = []wrapUint8{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'}
  900. var bw []byte // ("ABCDEFGHIJ")
  901. bs = testMarshalErr(sw, h, t, "wrap-bytes-enc")
  902. testUnmarshalErr(&bw, bs, h, t, "wrap-bytes-dec")
  903. testDeepEqualErr(bw, []byte("ABCDEFGHIJ"), t, "wrap-bytes-eq")
  904. }
  905. func testCodecEmbeddedPointer(t *testing.T, h Handle) {
  906. testOnce.Do(testInitAll)
  907. type Z int
  908. type A struct {
  909. AnInt int
  910. }
  911. type B struct {
  912. *Z
  913. *A
  914. MoreInt int
  915. }
  916. var z Z = 4
  917. x1 := &B{&z, &A{5}, 6}
  918. bs := testMarshalErr(x1, h, t, "x1")
  919. var x2 = new(B)
  920. testUnmarshalErr(x2, bs, h, t, "x2")
  921. checkEqualT(t, x1, x2, "x1=x2")
  922. }
  923. func testCodecUnderlyingType(t *testing.T, h Handle) {
  924. testOnce.Do(testInitAll)
  925. // Manual Test.
  926. // Run by hand, with accompanying printf.statements in fast-path.go
  927. // to ensure that the fast functions are called.
  928. type T1 map[string]string
  929. v := T1{"1": "1s", "2": "2s"}
  930. var bs []byte
  931. var err error
  932. NewEncoderBytes(&bs, h).MustEncode(v)
  933. if err != nil {
  934. failT(t, "Error during encode: %v", err)
  935. }
  936. var v2 T1
  937. NewDecoderBytes(bs, h).MustDecode(&v2)
  938. if err != nil {
  939. failT(t, "Error during decode: %v", err)
  940. }
  941. }
  942. func testCodecChan(t *testing.T, h Handle) {
  943. testOnce.Do(testInitAll)
  944. // - send a slice []*int64 (sl1) into an chan (ch1) with cap > len(s1)
  945. // - encode ch1 as a stream array
  946. // - decode a chan (ch2), with cap > len(s1) from the stream array
  947. // - receive from ch2 into slice sl2
  948. // - compare sl1 and sl2
  949. // - do this for codecs: json, cbor (covers all types)
  950. if true {
  951. logTv(t, "*int64")
  952. sl1 := make([]*int64, 4)
  953. for i := range sl1 {
  954. var j int64 = int64(i)
  955. sl1[i] = &j
  956. }
  957. ch1 := make(chan *int64, 4)
  958. for _, j := range sl1 {
  959. ch1 <- j
  960. }
  961. var bs []byte
  962. NewEncoderBytes(&bs, h).MustEncode(ch1)
  963. ch2 := make(chan *int64, 8)
  964. NewDecoderBytes(bs, h).MustDecode(&ch2)
  965. close(ch2)
  966. var sl2 []*int64
  967. for j := range ch2 {
  968. sl2 = append(sl2, j)
  969. }
  970. if err := deepEqual(sl1, sl2); err != nil {
  971. failT(t, "FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
  972. }
  973. }
  974. if true {
  975. logTv(t, "testBytesT []byte - input []byte")
  976. type testBytesT []byte
  977. sl1 := make([]testBytesT, 4)
  978. for i := range sl1 {
  979. var j = []byte(strings.Repeat(strconv.FormatInt(int64(i), 10), i))
  980. sl1[i] = j
  981. }
  982. ch1 := make(chan testBytesT, 4)
  983. for _, j := range sl1 {
  984. ch1 <- j
  985. }
  986. var bs []byte
  987. NewEncoderBytes(&bs, h).MustEncode(ch1)
  988. ch2 := make(chan testBytesT, 8)
  989. NewDecoderBytes(bs, h).MustDecode(&ch2)
  990. close(ch2)
  991. var sl2 []testBytesT
  992. for j := range ch2 {
  993. // logTv(t, ">>>> from chan: is nil? %v, %v", j == nil, j)
  994. sl2 = append(sl2, j)
  995. }
  996. if err := deepEqual(sl1, sl2); err != nil {
  997. failT(t, "FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
  998. }
  999. }
  1000. if true {
  1001. logTv(t, "testBytesT byte - input string/testBytesT")
  1002. type testBytesT byte
  1003. sl1 := make([]testBytesT, 4)
  1004. for i := range sl1 {
  1005. var j = strconv.FormatInt(int64(i), 10)[0]
  1006. sl1[i] = testBytesT(j)
  1007. }
  1008. ch1 := make(chan testBytesT, 4)
  1009. for _, j := range sl1 {
  1010. ch1 <- j
  1011. }
  1012. var bs []byte
  1013. NewEncoderBytes(&bs, h).MustEncode(ch1)
  1014. ch2 := make(chan testBytesT, 8)
  1015. NewDecoderBytes(bs, h).MustDecode(&ch2)
  1016. close(ch2)
  1017. var sl2 []testBytesT
  1018. for j := range ch2 {
  1019. sl2 = append(sl2, j)
  1020. }
  1021. if err := deepEqual(sl1, sl2); err != nil {
  1022. failT(t, "FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
  1023. }
  1024. }
  1025. if true {
  1026. logTv(t, "*[]byte")
  1027. sl1 := make([]byte, 4)
  1028. for i := range sl1 {
  1029. var j = strconv.FormatInt(int64(i), 10)[0]
  1030. sl1[i] = byte(j)
  1031. }
  1032. ch1 := make(chan byte, 4)
  1033. for _, j := range sl1 {
  1034. ch1 <- j
  1035. }
  1036. var bs []byte
  1037. NewEncoderBytes(&bs, h).MustEncode(ch1)
  1038. ch2 := make(chan byte, 8)
  1039. NewDecoderBytes(bs, h).MustDecode(&ch2)
  1040. close(ch2)
  1041. var sl2 []byte
  1042. for j := range ch2 {
  1043. sl2 = append(sl2, j)
  1044. }
  1045. if err := deepEqual(sl1, sl2); err != nil {
  1046. failT(t, "FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
  1047. }
  1048. }
  1049. }
  1050. func testCodecRpcOne(t *testing.T, rr Rpc, h Handle, doRequest bool, exitSleepMs time.Duration,
  1051. ) (port int) {
  1052. testOnce.Do(testInitAll)
  1053. if testSkipRPCTests {
  1054. return
  1055. }
  1056. // rpc needs EOF, which is sent via a panic, and so must be recovered.
  1057. if !recoverPanicToErr {
  1058. failT(t, "EXPECTED. set recoverPanicToErr=true, since rpc needs EOF")
  1059. }
  1060. if jsonH, ok := h.(*JsonHandle); ok && !jsonH.TermWhitespace {
  1061. jsonH.TermWhitespace = true
  1062. defer func() { jsonH.TermWhitespace = false }()
  1063. }
  1064. srv := rpc.NewServer()
  1065. srv.Register(testRpcInt)
  1066. ln, err := net.Listen("tcp", "127.0.0.1:0") // listen on ipv4 localhost
  1067. logT(t, "connFn: addr: %v, network: %v, port: %v", ln.Addr(), ln.Addr().Network(), (ln.Addr().(*net.TCPAddr)).Port)
  1068. // log("listener: %v", ln.Addr())
  1069. checkErrT(t, err)
  1070. port = (ln.Addr().(*net.TCPAddr)).Port
  1071. // var opts *DecoderOptions
  1072. // opts := testDecOpts
  1073. // opts.MapType = mapStrIntfTyp
  1074. serverExitChan := make(chan bool, 1)
  1075. var serverExitFlag uint64
  1076. serverFn := func() {
  1077. for {
  1078. conn1, err1 := ln.Accept()
  1079. // if err1 != nil {
  1080. // //fmt.Printf("accept err1: %v\n", err1)
  1081. // continue
  1082. // }
  1083. if atomic.LoadUint64(&serverExitFlag) == 1 {
  1084. serverExitChan <- true
  1085. if conn1 != nil {
  1086. conn1.Close()
  1087. }
  1088. return // exit serverFn goroutine
  1089. }
  1090. if err1 == nil && conn1 != nil {
  1091. sc := rr.ServerCodec(testReadWriteCloser(conn1), h)
  1092. srv.ServeCodec(sc)
  1093. }
  1094. }
  1095. }
  1096. clientFn := func(cc rpc.ClientCodec) {
  1097. cl := rpc.NewClientWithCodec(cc)
  1098. defer cl.Close()
  1099. // defer func() { println("##### client closing"); cl.Close() }()
  1100. var up, sq, mult int
  1101. var rstr string
  1102. // log("Calling client")
  1103. checkErrT(t, cl.Call("TestRpcInt.Update", 5, &up))
  1104. // log("Called TestRpcInt.Update")
  1105. checkEqualT(t, testRpcInt.i, 5, "testRpcInt.i=5")
  1106. checkEqualT(t, up, 5, "up=5")
  1107. checkErrT(t, cl.Call("TestRpcInt.Square", 1, &sq))
  1108. checkEqualT(t, sq, 25, "sq=25")
  1109. checkErrT(t, cl.Call("TestRpcInt.Mult", 20, &mult))
  1110. checkEqualT(t, mult, 100, "mult=100")
  1111. checkErrT(t, cl.Call("TestRpcInt.EchoStruct", TestRpcABC{"Aa", "Bb", "Cc"}, &rstr))
  1112. checkEqualT(t, rstr, fmt.Sprintf("%#v", TestRpcABC{"Aa", "Bb", "Cc"}), "rstr=")
  1113. checkErrT(t, cl.Call("TestRpcInt.Echo123", []string{"A1", "B2", "C3"}, &rstr))
  1114. checkEqualT(t, rstr, fmt.Sprintf("%#v", []string{"A1", "B2", "C3"}), "rstr=")
  1115. }
  1116. connFn := func() (bs net.Conn) {
  1117. // log("calling f1")
  1118. bs, err2 := net.Dial(ln.Addr().Network(), ln.Addr().String())
  1119. checkErrT(t, err2)
  1120. return
  1121. }
  1122. exitFn := func() {
  1123. atomic.StoreUint64(&serverExitFlag, 1)
  1124. bs := connFn()
  1125. <-serverExitChan
  1126. bs.Close()
  1127. // serverExitChan <- true
  1128. }
  1129. go serverFn()
  1130. runtime.Gosched()
  1131. //time.Sleep(100 * time.Millisecond)
  1132. if exitSleepMs == 0 {
  1133. defer ln.Close()
  1134. defer exitFn()
  1135. }
  1136. if doRequest {
  1137. bs := connFn()
  1138. cc := rr.ClientCodec(testReadWriteCloser(bs), h)
  1139. clientFn(cc)
  1140. }
  1141. if exitSleepMs != 0 {
  1142. go func() {
  1143. defer ln.Close()
  1144. time.Sleep(exitSleepMs)
  1145. exitFn()
  1146. }()
  1147. }
  1148. return
  1149. }
  1150. func doTestMapEncodeForCanonical(t *testing.T, name string, h Handle) {
  1151. testOnce.Do(testInitAll)
  1152. // println("doTestMapEncodeForCanonical")
  1153. v1 := map[stringUint64T]interface{}{
  1154. {"a", 1}: 1,
  1155. {"b", 2}: "hello",
  1156. {"c", 3}: map[string]interface{}{
  1157. "c/a": 1,
  1158. "c/b": "world",
  1159. "c/c": []int{1, 2, 3, 4},
  1160. "c/d": map[string]interface{}{
  1161. "c/d/a": "fdisajfoidsajfopdjsaopfjdsapofda",
  1162. "c/d/b": "fdsafjdposakfodpsakfopdsakfpodsakfpodksaopfkdsopafkdopsa",
  1163. "c/d/c": "poir02 ir30qif4p03qir0pogjfpoaerfgjp ofke[padfk[ewapf kdp[afep[aw",
  1164. "c/d/d": "fdsopafkd[sa f-32qor-=4qeof -afo-erfo r-eafo 4e- o r4-qwo ag",
  1165. "c/d/e": "kfep[a sfkr0[paf[a foe-[wq ewpfao-q ro3-q ro-4qof4-qor 3-e orfkropzjbvoisdb",
  1166. "c/d/f": "",
  1167. },
  1168. "c/e": map[int]string{
  1169. 1: "1",
  1170. 22: "22",
  1171. 333: "333",
  1172. 4444: "4444",
  1173. 55555: "55555",
  1174. },
  1175. "c/f": map[string]int{
  1176. "1": 1,
  1177. "22": 22,
  1178. "333": 333,
  1179. "4444": 4444,
  1180. "55555": 55555,
  1181. },
  1182. "c/g": map[bool]int{
  1183. false: 0,
  1184. true: 1,
  1185. },
  1186. },
  1187. }
  1188. var v2 map[stringUint64T]interface{}
  1189. var b1, b2, b3 []byte
  1190. // encode v1 into b1, decode b1 into v2, encode v2 into b2, and compare b1 and b2.
  1191. // OR
  1192. // encode v1 into b1, decode b1 into v2, encode v2 into b2 and b3, and compare b2 and b3.
  1193. // e.g. when doing cbor indefinite, we may haveto use out-of-band encoding
  1194. // where each key is encoded as an indefinite length string, which makes it not the same
  1195. // order as the strings were lexicographically ordered before.
  1196. var cborIndef bool
  1197. if ch, ok := h.(*CborHandle); ok {
  1198. cborIndef = ch.IndefiniteLength
  1199. }
  1200. bh := basicHandle(h)
  1201. if !bh.Canonical {
  1202. bh.Canonical = true
  1203. defer func() { bh.Canonical = false }()
  1204. }
  1205. e1 := NewEncoderBytes(&b1, h)
  1206. e1.MustEncode(v1)
  1207. d1 := NewDecoderBytes(b1, h)
  1208. d1.MustDecode(&v2)
  1209. // testDeepEqualErr(v1, v2, t, "huh?")
  1210. e2 := NewEncoderBytes(&b2, h)
  1211. e2.MustEncode(v2)
  1212. var b1t, b2t = b1, b2
  1213. if cborIndef {
  1214. e2 = NewEncoderBytes(&b3, h)
  1215. e2.MustEncode(v2)
  1216. b1t, b2t = b2, b3
  1217. }
  1218. if !bytes.Equal(b1t, b2t) {
  1219. failT(t, "Unequal bytes: %v VS %v", b1t, b2t)
  1220. }
  1221. }
  1222. func doTestStdEncIntf(t *testing.T, name string, h Handle) {
  1223. testOnce.Do(testInitAll)
  1224. args := [][2]interface{}{
  1225. {&TestABC{"A", "BB", "CCC"}, new(TestABC)},
  1226. {&TestABC2{"AAA", "BB", "C"}, new(TestABC2)},
  1227. }
  1228. for _, a := range args {
  1229. var b []byte
  1230. e := NewEncoderBytes(&b, h)
  1231. e.MustEncode(a[0])
  1232. d := NewDecoderBytes(b, h)
  1233. d.MustDecode(a[1])
  1234. if err := deepEqual(a[0], a[1]); err == nil {
  1235. logTv(t, "++++ Objects match")
  1236. } else {
  1237. failT(t, "---- FAIL: Objects do not match: y1: %v, err: %v", a[1], err)
  1238. }
  1239. }
  1240. }
  1241. func doTestEncCircularRef(t *testing.T, name string, h Handle) {
  1242. testOnce.Do(testInitAll)
  1243. type T1 struct {
  1244. S string
  1245. B bool
  1246. T interface{}
  1247. }
  1248. type T2 struct {
  1249. S string
  1250. T *T1
  1251. }
  1252. type T3 struct {
  1253. S string
  1254. T *T2
  1255. }
  1256. t1 := T1{"t1", true, nil}
  1257. t2 := T2{"t2", &t1}
  1258. t3 := T3{"t3", &t2}
  1259. t1.T = &t3
  1260. var bs []byte
  1261. var err error
  1262. bh := basicHandle(h)
  1263. if !bh.CheckCircularRef {
  1264. bh.CheckCircularRef = true
  1265. defer func() { bh.CheckCircularRef = false }()
  1266. }
  1267. err = NewEncoderBytes(&bs, h).Encode(&t3)
  1268. if err == nil {
  1269. failT(t, "expecting error due to circular reference. found none")
  1270. }
  1271. if x := err.Error(); strings.Contains(x, "circular") || strings.Contains(x, "cyclic") {
  1272. logTv(t, "error detected as expected: %v", x)
  1273. } else {
  1274. failT(t, "FAIL: error detected was not as expected: %v", x)
  1275. }
  1276. }
  1277. // TestAnonCycleT{1,2,3} types are used to test anonymous cycles.
  1278. // They are top-level, so that they can have circular references.
  1279. type (
  1280. TestAnonCycleT1 struct {
  1281. S string
  1282. TestAnonCycleT2
  1283. }
  1284. TestAnonCycleT2 struct {
  1285. S2 string
  1286. TestAnonCycleT3
  1287. }
  1288. TestAnonCycleT3 struct {
  1289. *TestAnonCycleT1
  1290. }
  1291. )
  1292. func doTestAnonCycle(t *testing.T, name string, h Handle) {
  1293. testOnce.Do(testInitAll)
  1294. var x TestAnonCycleT1
  1295. x.S = "hello"
  1296. x.TestAnonCycleT2.S2 = "hello.2"
  1297. x.TestAnonCycleT2.TestAnonCycleT3.TestAnonCycleT1 = &x
  1298. // just check that you can get typeInfo for T1
  1299. rt := reflect.TypeOf((*TestAnonCycleT1)(nil)).Elem()
  1300. rtid := rt2id(rt)
  1301. pti := basicHandle(h).getTypeInfo(rtid, rt)
  1302. logTv(t, "pti: %v", pti)
  1303. }
  1304. func doTestErrWriter(t *testing.T, name string, h Handle) {
  1305. var ew testErrWriter
  1306. w := bufio.NewWriterSize(&ew, 4)
  1307. enc := NewEncoder(w, h)
  1308. for i := 0; i < 4; i++ {
  1309. err := enc.Encode("ugorji")
  1310. if ev, ok := err.(encodeError); ok {
  1311. err = ev.Cause()
  1312. }
  1313. if err != testErrWriterErr {
  1314. failT(t, "%s: expecting err: %v, received: %v", name, testErrWriterErr, err)
  1315. }
  1316. }
  1317. }
  1318. func doTestJsonLargeInteger(t *testing.T, v interface{}, ias uint8) {
  1319. testOnce.Do(testInitAll)
  1320. logTv(t, "Running doTestJsonLargeInteger: v: %#v, ias: %c", v, ias)
  1321. oldIAS := testJsonH.IntegerAsString
  1322. defer func() { testJsonH.IntegerAsString = oldIAS }()
  1323. testJsonH.IntegerAsString = ias
  1324. var vu uint
  1325. var vi int
  1326. var vb bool
  1327. var b []byte
  1328. e := NewEncoderBytes(&b, testJsonH)
  1329. e.MustEncode(v)
  1330. e.MustEncode(true)
  1331. d := NewDecoderBytes(b, testJsonH)
  1332. // below, we validate that the json string or number was encoded,
  1333. // then decode, and validate that the correct value was decoded.
  1334. fnStrChk := func() {
  1335. // check that output started with ", and ended with " true
  1336. // if !(len(b) >= 7 && b[0] == '"' && string(b[len(b)-7:]) == `" true `) {
  1337. if !(len(b) >= 5 && b[0] == '"' && string(b[len(b)-5:]) == `"true`) {
  1338. failT(t, "Expecting a JSON string, got: '%s'", b)
  1339. }
  1340. }
  1341. switch ias {
  1342. case 'L':
  1343. switch v2 := v.(type) {
  1344. case int:
  1345. v2n := int64(v2) // done to work with 32-bit OS
  1346. if v2n > 1<<53 || (v2n < 0 && -v2n > 1<<53) {
  1347. fnStrChk()
  1348. }
  1349. case uint:
  1350. v2n := uint64(v2) // done to work with 32-bit OS
  1351. if v2n > 1<<53 {
  1352. fnStrChk()
  1353. }
  1354. }
  1355. case 'A':
  1356. fnStrChk()
  1357. default:
  1358. // check that output doesn't contain " at all
  1359. for _, i := range b {
  1360. if i == '"' {
  1361. failT(t, "Expecting a JSON Number without quotation: got: %s", b)
  1362. }
  1363. }
  1364. }
  1365. switch v2 := v.(type) {
  1366. case int:
  1367. d.MustDecode(&vi)
  1368. d.MustDecode(&vb)
  1369. // check that vb = true, and vi == v2
  1370. if !(vb && vi == v2) {
  1371. failT(t, "Expecting equal values from %s: got golden: %v, decoded: %v", b, v2, vi)
  1372. }
  1373. case uint:
  1374. d.MustDecode(&vu)
  1375. d.MustDecode(&vb)
  1376. // check that vb = true, and vi == v2
  1377. if !(vb && vu == v2) {
  1378. failT(t, "Expecting equal values from %s: got golden: %v, decoded: %v", b, v2, vu)
  1379. }
  1380. }
  1381. }
  1382. func doTestRawValue(t *testing.T, name string, h Handle) {
  1383. testOnce.Do(testInitAll)
  1384. bh := basicHandle(h)
  1385. if !bh.Raw {
  1386. bh.Raw = true
  1387. defer func() { bh.Raw = false }()
  1388. }
  1389. var i, i2 int
  1390. var v, v2 TestRawValue
  1391. var bs, bs2 []byte
  1392. i = 1234 //1234567890
  1393. v = TestRawValue{I: i}
  1394. e := NewEncoderBytes(&bs, h)
  1395. e.MustEncode(v.I)
  1396. logTv(t, ">>> raw: %v\n", bs)
  1397. v.R = Raw(bs)
  1398. e.ResetBytes(&bs2)
  1399. e.MustEncode(v)
  1400. logTv(t, ">>> bs2: %v\n", bs2)
  1401. d := NewDecoderBytes(bs2, h)
  1402. d.MustDecode(&v2)
  1403. d.ResetBytes(v2.R)
  1404. logTv(t, ">>> v2.R: %v\n", ([]byte)(v2.R))
  1405. d.MustDecode(&i2)
  1406. logTv(t, ">>> Encoded %v, decoded %v\n", i, i2)
  1407. // logT(t, "Encoded %v, decoded %v", i, i2)
  1408. if i != i2 {
  1409. failT(t, "Error: encoded %v, decoded %v", i, i2)
  1410. }
  1411. }
  1412. // Comprehensive testing that generates data encoded from python handle (cbor, msgpack),
  1413. // and validates that our code can read and write it out accordingly.
  1414. // We keep this unexported here, and put actual test in ext_dep_test.go.
  1415. // This way, it can be excluded by excluding file completely.
  1416. func doTestPythonGenStreams(t *testing.T, name string, h Handle) {
  1417. testOnce.Do(testInitAll)
  1418. logT(t, "TestPythonGenStreams-%v", name)
  1419. tmpdir, err := ioutil.TempDir("", "golang-"+name+"-test")
  1420. if err != nil {
  1421. failT(t, "-------- Unable to create temp directory\n")
  1422. }
  1423. defer os.RemoveAll(tmpdir)
  1424. logTv(t, "tmpdir: %v", tmpdir)
  1425. cmd := exec.Command("python", "test.py", "testdata", tmpdir)
  1426. //cmd.Stdin = strings.NewReader("some input")
  1427. //cmd.Stdout = &out
  1428. var cmdout []byte
  1429. if cmdout, err = cmd.CombinedOutput(); err != nil {
  1430. logT(t, "-------- Error running test.py testdata. Err: %v", err)
  1431. logT(t, " %v", string(cmdout))
  1432. failT(t)
  1433. }
  1434. bh := basicHandle(h)
  1435. oldMapType := bh.MapType
  1436. tablePythonVerify := testTableVerify(testVerifyForPython|testVerifyTimeAsInteger|testVerifyMapTypeStrIntf, h)
  1437. for i, v := range tablePythonVerify {
  1438. // if v == uint64(0) && h == testMsgpackH {
  1439. // v = int64(0)
  1440. // }
  1441. bh.MapType = oldMapType
  1442. //load up the golden file based on number
  1443. //decode it
  1444. //compare to in-mem object
  1445. //encode it again
  1446. //compare to output stream
  1447. logTv(t, "..............................................")
  1448. logT(t, " Testing: #%d: %T, %#v\n", i, v, v)
  1449. var bss []byte
  1450. bss, err = ioutil.ReadFile(filepath.Join(tmpdir, strconv.Itoa(i)+"."+name+".golden"))
  1451. if err != nil {
  1452. failT(t, "-------- Error reading golden file: %d. Err: %v", i, err)
  1453. continue
  1454. }
  1455. bh.MapType = testMapStrIntfTyp
  1456. var v1 interface{}
  1457. if err = testUnmarshal(&v1, bss, h); err != nil {
  1458. failT(t, "-------- Error decoding stream: %d: Err: %v", i, err)
  1459. continue
  1460. }
  1461. if v == skipVerifyVal {
  1462. continue
  1463. }
  1464. //no need to indirect, because we pass a nil ptr, so we already have the value
  1465. //if v1 != nil { v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface() }
  1466. if err = deepEqual(v, v1); err == nil {
  1467. logTv(t, "++++++++ Objects match: %T, %v", v, v)
  1468. } else {
  1469. logT(t, "-------- FAIL: Objects do not match: %v. Source: %T. Decoded: %T", err, v, v1)
  1470. logTv(t, "-------- GOLDEN: %#v", v)
  1471. // logT(t, "-------- DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
  1472. logTv(t, "-------- DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
  1473. failT(t)
  1474. }
  1475. bsb, err := testMarshal(v1, h)
  1476. if err != nil {
  1477. failT(t, "Error encoding to stream: %d: Err: %v", i, err)
  1478. continue
  1479. }
  1480. if err = deepEqual(bsb, bss); err == nil {
  1481. logTv(t, "++++++++ Bytes match")
  1482. } else {
  1483. logT(t, "???????? FAIL: Bytes do not match. %v.", err)
  1484. xs := "--------"
  1485. if reflect.ValueOf(v).Kind() == reflect.Map {
  1486. xs = " "
  1487. logT(t, "%s It's a map. Ok that they don't match (dependent on ordering).", xs)
  1488. } else {
  1489. failT(t, "%s It's not a map. They should match.", xs)
  1490. }
  1491. logTv(t, "%s FROM_FILE: %4d] %v", xs, len(bss), bss)
  1492. logTv(t, "%s ENCODED: %4d] %v", xs, len(bsb), bsb)
  1493. }
  1494. }
  1495. bh.MapType = oldMapType
  1496. }
  1497. // To test MsgpackSpecRpc, we test 3 scenarios:
  1498. // - Go Client to Go RPC Service (contained within TestMsgpackRpcSpec)
  1499. // - Go client to Python RPC Service (contained within doTestMsgpackRpcSpecGoClientToPythonSvc)
  1500. // - Python Client to Go RPC Service (contained within doTestMsgpackRpcSpecPythonClientToGoSvc)
  1501. //
  1502. // This allows us test the different calling conventions
  1503. // - Go Service requires only one argument
  1504. // - Python Service allows multiple arguments
  1505. func doTestMsgpackRpcSpecGoClientToPythonSvc(t *testing.T) {
  1506. if testSkipRPCTests {
  1507. return
  1508. }
  1509. testOnce.Do(testInitAll)
  1510. // openPorts are between 6700 and 6800
  1511. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  1512. openPort := strconv.FormatInt(6700+r.Int63n(99), 10)
  1513. // openPort := "6792"
  1514. cmd := exec.Command("python", "test.py", "rpc-server", openPort, "4")
  1515. checkErrT(t, cmd.Start())
  1516. bs, err2 := net.Dial("tcp", ":"+openPort)
  1517. for i := 0; i < 10 && err2 != nil; i++ {
  1518. time.Sleep(50 * time.Millisecond) // time for python rpc server to start
  1519. bs, err2 = net.Dial("tcp", ":"+openPort)
  1520. }
  1521. checkErrT(t, err2)
  1522. cc := MsgpackSpecRpc.ClientCodec(testReadWriteCloser(bs), testMsgpackH)
  1523. cl := rpc.NewClientWithCodec(cc)
  1524. defer cl.Close()
  1525. var rstr string
  1526. checkErrT(t, cl.Call("EchoStruct", TestRpcABC{"Aa", "Bb", "Cc"}, &rstr))
  1527. //checkEqualT(t, rstr, "{'A': 'Aa', 'B': 'Bb', 'C': 'Cc'}")
  1528. var mArgs MsgpackSpecRpcMultiArgs = []interface{}{"A1", "B2", "C3"}
  1529. checkErrT(t, cl.Call("Echo123", mArgs, &rstr))
  1530. checkEqualT(t, rstr, "1:A1 2:B2 3:C3", "rstr=")
  1531. cmd.Process.Kill()
  1532. }
  1533. func doTestMsgpackRpcSpecPythonClientToGoSvc(t *testing.T) {
  1534. if testSkipRPCTests {
  1535. return
  1536. }
  1537. testOnce.Do(testInitAll)
  1538. port := testCodecRpcOne(t, MsgpackSpecRpc, testMsgpackH, false, 1*time.Second)
  1539. //time.Sleep(1000 * time.Millisecond)
  1540. cmd := exec.Command("python", "test.py", "rpc-client-go-service", strconv.Itoa(port))
  1541. var cmdout []byte
  1542. var err error
  1543. if cmdout, err = cmd.CombinedOutput(); err != nil {
  1544. logT(t, "-------- Error running test.py rpc-client-go-service. Err: %v", err)
  1545. logT(t, " %v", string(cmdout))
  1546. failT(t)
  1547. }
  1548. checkEqualT(t, string(cmdout),
  1549. fmt.Sprintf("%#v\n%#v\n", []string{"A1", "B2", "C3"}, TestRpcABC{"Aa", "Bb", "Cc"}), "cmdout=")
  1550. }
  1551. func doTestSwallowAndZero(t *testing.T, h Handle) {
  1552. testOnce.Do(testInitAll)
  1553. v1 := newTestStrucFlex(testDepth, testNumRepeatString, false, false, testMapStringKeyOnly)
  1554. var b1 []byte
  1555. e1 := NewEncoderBytes(&b1, h)
  1556. e1.MustEncode(v1)
  1557. d1 := NewDecoderBytes(b1, h)
  1558. d1.swallow()
  1559. if d1.r().numread() != uint(len(b1)) {
  1560. failT(t, "swallow didn't consume all encoded bytes: %v out of %v", d1.r().numread(), len(b1))
  1561. }
  1562. setZero(v1)
  1563. testDeepEqualErr(v1, &TestStrucFlex{}, t, "filled-and-zeroed")
  1564. }
  1565. func doTestRawExt(t *testing.T, h Handle) {
  1566. testOnce.Do(testInitAll)
  1567. var b []byte
  1568. var v RawExt // interface{}
  1569. _, isJson := h.(*JsonHandle)
  1570. _, isCbor := h.(*CborHandle)
  1571. bh := basicHandle(h)
  1572. // isValuer := isJson || isCbor
  1573. // _ = isValuer
  1574. for _, r := range []RawExt{
  1575. {Tag: 99, Value: "9999", Data: []byte("9999")},
  1576. } {
  1577. e := NewEncoderBytes(&b, h)
  1578. e.MustEncode(&r)
  1579. // fmt.Printf(">>>> rawext: isnil? %v, %d - %v\n", b == nil, len(b), b)
  1580. d := NewDecoderBytes(b, h)
  1581. d.MustDecode(&v)
  1582. var r2 = r
  1583. switch {
  1584. case isJson:
  1585. r2.Tag = 0
  1586. r2.Data = nil
  1587. case isCbor:
  1588. r2.Data = nil
  1589. default:
  1590. r2.Value = nil
  1591. }
  1592. testDeepEqualErr(v, r2, t, "rawext-default")
  1593. // switch h.(type) {
  1594. // case *JsonHandle:
  1595. // testDeepEqualErr(r.Value, v, t, "rawext-json")
  1596. // default:
  1597. // var r2 = r
  1598. // if isValuer {
  1599. // r2.Data = nil
  1600. // } else {
  1601. // r2.Value = nil
  1602. // }
  1603. // testDeepEqualErr(v, r2, t, "rawext-default")
  1604. // }
  1605. }
  1606. // Add testing for Raw also
  1607. if b != nil {
  1608. b = b[:0]
  1609. }
  1610. oldRawMode := bh.Raw
  1611. defer func() { bh.Raw = oldRawMode }()
  1612. bh.Raw = true
  1613. var v2 Raw
  1614. for _, s := range []string{
  1615. "goodbye",
  1616. "hello",
  1617. } {
  1618. e := NewEncoderBytes(&b, h)
  1619. e.MustEncode(&s)
  1620. // fmt.Printf(">>>> rawext: isnil? %v, %d - %v\n", b == nil, len(b), b)
  1621. var r Raw = make([]byte, len(b))
  1622. copy(r, b)
  1623. d := NewDecoderBytes(b, h)
  1624. d.MustDecode(&v2)
  1625. testDeepEqualErr(v2, r, t, "raw-default")
  1626. }
  1627. }
  1628. // func doTestTimeExt(t *testing.T, h Handle) {
  1629. // var t = time.Now()
  1630. // // add time ext to the handle
  1631. // }
  1632. func doTestMapStructKey(t *testing.T, h Handle) {
  1633. testOnce.Do(testInitAll)
  1634. var b []byte
  1635. var v interface{} // map[stringUint64T]wrapUint64Slice // interface{}
  1636. bh := basicHandle(h)
  1637. m := map[stringUint64T]wrapUint64Slice{
  1638. {"55555", 55555}: []wrapUint64{12345},
  1639. {"333", 333}: []wrapUint64{123},
  1640. }
  1641. oldCanonical := bh.Canonical
  1642. oldMapType := bh.MapType
  1643. defer func() {
  1644. bh.Canonical = oldCanonical
  1645. bh.MapType = oldMapType
  1646. }()
  1647. bh.MapType = reflect.TypeOf((*map[stringUint64T]wrapUint64Slice)(nil)).Elem()
  1648. for _, bv := range [2]bool{true, false} {
  1649. b, v = nil, nil
  1650. bh.Canonical = bv
  1651. e := NewEncoderBytes(&b, h)
  1652. e.MustEncode(m)
  1653. d := NewDecoderBytes(b, h)
  1654. d.MustDecode(&v)
  1655. testDeepEqualErr(v, m, t, "map-structkey")
  1656. }
  1657. }
  1658. func doTestDecodeNilMapValue(t *testing.T, h Handle) {
  1659. testOnce.Do(testInitAll)
  1660. type Struct struct {
  1661. Field map[uint16]map[uint32]struct{}
  1662. }
  1663. bh := basicHandle(h)
  1664. oldMapType := bh.MapType
  1665. oldDeleteOnNilMapValue := bh.DeleteOnNilMapValue
  1666. defer func() {
  1667. bh.MapType = oldMapType
  1668. bh.DeleteOnNilMapValue = oldDeleteOnNilMapValue
  1669. }()
  1670. bh.MapType = reflect.TypeOf(map[interface{}]interface{}(nil))
  1671. bh.DeleteOnNilMapValue = false
  1672. _, isJsonHandle := h.(*JsonHandle)
  1673. toEncode := Struct{Field: map[uint16]map[uint32]struct{}{
  1674. 1: nil,
  1675. }}
  1676. bs, err := testMarshal(toEncode, h)
  1677. if err != nil {
  1678. failT(t, "Error encoding: %v, Err: %v", toEncode, err)
  1679. }
  1680. if isJsonHandle {
  1681. logT(t, "json encoded: %s\n", bs)
  1682. }
  1683. var decoded Struct
  1684. err = testUnmarshal(&decoded, bs, h)
  1685. if err != nil {
  1686. failT(t, "Error decoding: %v", err)
  1687. }
  1688. if !reflect.DeepEqual(decoded, toEncode) {
  1689. failT(t, "Decoded value %#v != %#v", decoded, toEncode)
  1690. }
  1691. }
  1692. func doTestEmbeddedFieldPrecedence(t *testing.T, h Handle) {
  1693. testOnce.Do(testInitAll)
  1694. type Embedded struct {
  1695. Field byte
  1696. }
  1697. type Struct struct {
  1698. Field byte
  1699. Embedded
  1700. }
  1701. toEncode := Struct{
  1702. Field: 1,
  1703. Embedded: Embedded{Field: 2},
  1704. }
  1705. _, isJsonHandle := h.(*JsonHandle)
  1706. handle := basicHandle(h)
  1707. oldMapType := handle.MapType
  1708. defer func() { handle.MapType = oldMapType }()
  1709. handle.MapType = reflect.TypeOf(map[interface{}]interface{}(nil))
  1710. bs, err := testMarshal(toEncode, h)
  1711. if err != nil {
  1712. failT(t, "Error encoding: %v, Err: %v", toEncode, err)
  1713. }
  1714. var decoded Struct
  1715. err = testUnmarshal(&decoded, bs, h)
  1716. if err != nil {
  1717. failT(t, "Error decoding: %v", err)
  1718. }
  1719. if decoded.Field != toEncode.Field {
  1720. logT(t, "Decoded result %v != %v", decoded.Field, toEncode.Field) // hex to look at what was encoded
  1721. if isJsonHandle {
  1722. logT(t, "JSON encoded as: %s", bs) // hex to look at what was encoded
  1723. }
  1724. failT(t)
  1725. }
  1726. }
  1727. func doTestLargeContainerLen(t *testing.T, h Handle) {
  1728. testOnce.Do(testInitAll)
  1729. m := make(map[int][]struct{})
  1730. for i := range []int{
  1731. 0, 1,
  1732. math.MaxInt8, math.MaxInt8 + 4, math.MaxInt8 - 4,
  1733. math.MaxInt16, math.MaxInt16 + 4, math.MaxInt16 - 4,
  1734. math.MaxInt32, math.MaxInt32 - 4,
  1735. // math.MaxInt32 + 4, // bombs on 32-bit
  1736. // math.MaxInt64, math.MaxInt64 - 4, // bombs on 32-bit
  1737. math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
  1738. math.MaxUint16, math.MaxUint16 + 4, math.MaxUint16 - 4,
  1739. // math.MaxUint32, math.MaxUint32 + 4, math.MaxUint32 - 4, // bombs on 32-bit
  1740. } {
  1741. m[i] = make([]struct{}, i)
  1742. }
  1743. bs := testMarshalErr(m, h, t, "-")
  1744. var m2 = make(map[int][]struct{})
  1745. testUnmarshalErr(m2, bs, h, t, "-")
  1746. testDeepEqualErr(m, m2, t, "-")
  1747. // do same tests for large strings (encoded as symbols or not)
  1748. // skip if 32-bit or not using unsafe mode
  1749. if safeMode || (32<<(^uint(0)>>63)) < 64 {
  1750. return
  1751. }
  1752. // now, want to do tests for large strings, which
  1753. // could be encoded as symbols.
  1754. // to do this, we create a simple one-field struct,
  1755. // use use flags to switch from symbols to non-symbols
  1756. hbinc, okbinc := h.(*BincHandle)
  1757. if okbinc {
  1758. oldAsSymbols := hbinc.AsSymbols
  1759. defer func() { hbinc.AsSymbols = oldAsSymbols }()
  1760. }
  1761. var out []byte = make([]byte, 0, math.MaxUint16*3/2)
  1762. var in []byte = make([]byte, math.MaxUint16*3/2)
  1763. for i := range in {
  1764. in[i] = 'A'
  1765. }
  1766. e := NewEncoder(nil, h)
  1767. for _, i := range []int{
  1768. 0, 1, 4, 8, 12, 16, 28, 32, 36,
  1769. math.MaxInt8, math.MaxInt8 + 4, math.MaxInt8 - 4,
  1770. math.MaxInt16, math.MaxInt16 + 4, math.MaxInt16 - 4,
  1771. math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
  1772. math.MaxUint16, math.MaxUint16 + 4, math.MaxUint16 - 4,
  1773. } {
  1774. var m1, m2 map[string]bool
  1775. m1 = make(map[string]bool, 1)
  1776. var s1 = stringView(in[:i])
  1777. // fmt.Printf("testcontainerlen: large string: i: %v, |%s|\n", i, s1)
  1778. m1[s1] = true
  1779. if okbinc {
  1780. hbinc.AsSymbols = 2
  1781. }
  1782. out = out[:0]
  1783. e.ResetBytes(&out)
  1784. e.MustEncode(m1)
  1785. // bs, _ = testMarshalErr(m1, h, t, "-")
  1786. m2 = make(map[string]bool, 1)
  1787. testUnmarshalErr(m2, out, h, t, "no-symbols")
  1788. testDeepEqualErr(m1, m2, t, "no-symbols")
  1789. if okbinc {
  1790. // now, do as symbols
  1791. hbinc.AsSymbols = 1
  1792. out = out[:0]
  1793. e.ResetBytes(&out)
  1794. e.MustEncode(m1)
  1795. // bs, _ = testMarshalErr(m1, h, t, "-")
  1796. m2 = make(map[string]bool, 1)
  1797. testUnmarshalErr(m2, out, h, t, "symbols")
  1798. testDeepEqualErr(m1, m2, t, "symbols")
  1799. }
  1800. }
  1801. }
  1802. func testRandomFillRV(v reflect.Value) {
  1803. testOnce.Do(testInitAll)
  1804. fneg := func() int64 {
  1805. i := rand.Intn(1)
  1806. if i == 1 {
  1807. return 1
  1808. }
  1809. return -1
  1810. }
  1811. switch v.Kind() {
  1812. case reflect.Invalid:
  1813. case reflect.Ptr:
  1814. if v.IsNil() {
  1815. v.Set(reflect.New(v.Type().Elem()))
  1816. }
  1817. testRandomFillRV(v.Elem())
  1818. case reflect.Interface:
  1819. if v.IsNil() {
  1820. v.Set(reflect.ValueOf("nothing"))
  1821. } else {
  1822. testRandomFillRV(v.Elem())
  1823. }
  1824. case reflect.Struct:
  1825. for i, n := 0, v.NumField(); i < n; i++ {
  1826. testRandomFillRV(v.Field(i))
  1827. }
  1828. case reflect.Slice:
  1829. if v.IsNil() {
  1830. v.Set(reflect.MakeSlice(v.Type(), 4, 4))
  1831. }
  1832. fallthrough
  1833. case reflect.Array:
  1834. for i, n := 0, v.Len(); i < n; i++ {
  1835. testRandomFillRV(v.Index(i))
  1836. }
  1837. case reflect.Map:
  1838. if v.IsNil() {
  1839. v.Set(reflect.MakeMap(v.Type()))
  1840. }
  1841. if v.Len() == 0 {
  1842. kt, vt := v.Type().Key(), v.Type().Elem()
  1843. for i := 0; i < 4; i++ {
  1844. k0 := reflect.New(kt).Elem()
  1845. v0 := reflect.New(vt).Elem()
  1846. testRandomFillRV(k0)
  1847. testRandomFillRV(v0)
  1848. v.SetMapIndex(k0, v0)
  1849. }
  1850. } else {
  1851. for _, k := range v.MapKeys() {
  1852. testRandomFillRV(v.MapIndex(k))
  1853. }
  1854. }
  1855. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1856. v.SetInt(fneg() * rand.Int63n(127))
  1857. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1858. v.SetUint(uint64(rand.Int63n(255)))
  1859. case reflect.Bool:
  1860. v.SetBool(fneg() == 1)
  1861. case reflect.Float32, reflect.Float64:
  1862. v.SetFloat(float64(fneg()) * float64(rand.Float32()))
  1863. case reflect.String:
  1864. // ensure this string can test the extent of json string decoding
  1865. v.SetString(strings.Repeat(strconv.FormatInt(rand.Int63n(99), 10), rand.Intn(8)) +
  1866. "- ABC \x41=\x42 \u2318 - \r \b \f - \u2028 and \u2029 .")
  1867. default:
  1868. panic(fmt.Errorf("testRandomFillRV: unsupported type: %v", v.Kind()))
  1869. }
  1870. }
  1871. func testMammoth(t *testing.T, name string, h Handle) {
  1872. testOnce.Do(testInitAll)
  1873. var b []byte
  1874. var m, m2 TestMammoth
  1875. testRandomFillRV(reflect.ValueOf(&m).Elem())
  1876. b = testMarshalErr(&m, h, t, "mammoth-"+name)
  1877. // xdebugf("%s", b)
  1878. testUnmarshalErr(&m2, b, h, t, "mammoth-"+name)
  1879. testDeepEqualErr(&m, &m2, t, "mammoth-"+name)
  1880. var mm, mm2 TestMammoth2Wrapper
  1881. testRandomFillRV(reflect.ValueOf(&mm).Elem())
  1882. b = testMarshalErr(&mm, h, t, "mammoth2-"+name)
  1883. // os.Stderr.Write([]byte("\n\n\n\n" + string(b) + "\n\n\n\n"))
  1884. testUnmarshalErr(&mm2, b, h, t, "mammoth2-"+name)
  1885. testDeepEqualErr(&mm, &mm2, t, "mammoth2-"+name)
  1886. // testMammoth2(t, name, h)
  1887. }
  1888. func testTime(t *testing.T, name string, h Handle) {
  1889. testOnce.Do(testInitAll)
  1890. // test time which uses the time.go implementation (ie Binc)
  1891. var tt, tt2 time.Time
  1892. // time in 1990
  1893. tt = time.Unix(20*366*24*60*60, 1000*900).In(time.FixedZone("UGO", -5*60*60))
  1894. // fmt.Printf("time tt: %v\n", tt)
  1895. b := testMarshalErr(tt, h, t, "time-"+name)
  1896. testUnmarshalErr(&tt2, b, h, t, "time-"+name)
  1897. // per go documentation, test time with .Equal not ==
  1898. if !tt2.Equal(tt) {
  1899. failT(t, "%s: values not equal: 1: %v, 2: %v", name, tt2, tt)
  1900. }
  1901. // testDeepEqualErr(tt.UTC(), tt2, t, "time-"+name)
  1902. }
  1903. func testUintToInt(t *testing.T, name string, h Handle) {
  1904. testOnce.Do(testInitAll)
  1905. var golden = [...]int64{
  1906. 0, 1, 22, 333, 4444, 55555, 666666,
  1907. // msgpack ones
  1908. 24, 128,
  1909. // standard ones
  1910. math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
  1911. math.MaxUint16, math.MaxUint16 + 4, math.MaxUint16 - 4,
  1912. math.MaxUint32, math.MaxUint32 + 4, math.MaxUint32 - 4,
  1913. math.MaxInt8, math.MaxInt8 + 4, math.MaxInt8 - 4,
  1914. math.MaxInt16, math.MaxInt16 + 4, math.MaxInt16 - 4,
  1915. math.MaxInt32, math.MaxInt32 + 4, math.MaxInt32 - 4,
  1916. math.MaxInt64, math.MaxInt64 - 4,
  1917. }
  1918. var ui uint64
  1919. var fi float64
  1920. var b []byte
  1921. for _, i := range golden {
  1922. ui = 0
  1923. b = testMarshalErr(i, h, t, "int2uint-"+name)
  1924. testUnmarshalErr(&ui, b, h, t, "int2uint-"+name)
  1925. if ui != uint64(i) {
  1926. failT(t, "%s: values not equal: %v, %v", name, ui, uint64(i))
  1927. }
  1928. i = 0
  1929. b = testMarshalErr(ui, h, t, "uint2int-"+name)
  1930. testUnmarshalErr(&i, b, h, t, "uint2int-"+name)
  1931. if i != int64(ui) {
  1932. failT(t, "%s: values not equal: %v, %v", name, i, int64(ui))
  1933. }
  1934. fi = 0
  1935. b = testMarshalErr(i, h, t, "int2float-"+name)
  1936. testUnmarshalErr(&fi, b, h, t, "int2float-"+name)
  1937. if fi != float64(i) {
  1938. failT(t, "%s: values not equal: %v, %v", name, fi, float64(i))
  1939. }
  1940. }
  1941. }
  1942. func doTestDifferentMapOrSliceType(t *testing.T, name string, h Handle) {
  1943. testOnce.Do(testInitAll)
  1944. // - maptype, slicetype: diff from map[string]intf, map[intf]intf or []intf, etc
  1945. // include map[interface{}]string where some keys are []byte.
  1946. // To test, take a sequence of []byte and string, and decode into []string and []interface.
  1947. // Also, decode into map[string]string, map[string]interface{}, map[interface{}]string
  1948. bh := basicHandle(h)
  1949. oldM, oldS := bh.MapType, bh.SliceType
  1950. defer func() { bh.MapType, bh.SliceType = oldM, oldS }()
  1951. var b []byte
  1952. var vi = []interface{}{
  1953. "hello 1",
  1954. []byte("hello 2"),
  1955. "hello 3",
  1956. []byte("hello 4"),
  1957. "hello 5",
  1958. }
  1959. var vs []string
  1960. var v2i, v2s testMbsT
  1961. var v2ss testMbsCustStrT
  1962. // encode it as a map or as a slice
  1963. for i, v := range vi {
  1964. vv, ok := v.(string)
  1965. if !ok {
  1966. vv = string(v.([]byte))
  1967. }
  1968. vs = append(vs, vv)
  1969. v2i = append(v2i, v, strconv.FormatInt(int64(i+1), 10))
  1970. v2s = append(v2s, vv, strconv.FormatInt(int64(i+1), 10))
  1971. v2ss = append(v2ss, testCustomStringT(vv), testCustomStringT(strconv.FormatInt(int64(i+1), 10)))
  1972. }
  1973. var v2d interface{}
  1974. // encode vs as a list, and decode into a list and compare
  1975. var goldSliceS = []string{"hello 1", "hello 2", "hello 3", "hello 4", "hello 5"}
  1976. var goldSliceI = []interface{}{"hello 1", "hello 2", "hello 3", "hello 4", "hello 5"}
  1977. var goldSlice = []interface{}{goldSliceS, goldSliceI}
  1978. for j, g := range goldSlice {
  1979. bh.SliceType = reflect.TypeOf(g)
  1980. name := fmt.Sprintf("slice-%s-%v", name, j+1)
  1981. b = testMarshalErr(vs, h, t, name)
  1982. v2d = nil
  1983. // v2d = reflect.New(bh.SliceType).Elem().Interface()
  1984. testUnmarshalErr(&v2d, b, h, t, name)
  1985. testDeepEqualErr(v2d, goldSlice[j], t, name)
  1986. }
  1987. // to ensure that we do not use fast-path for map[intf]string, use a custom string type (for goldMapIS).
  1988. // this will allow us to test out the path that sees a []byte where a map has an interface{} type,
  1989. // and convert it to a string for the decoded map key.
  1990. // encode v2i as a map, and decode into a map and compare
  1991. var goldMapSS = map[string]string{"hello 1": "1", "hello 2": "2", "hello 3": "3", "hello 4": "4", "hello 5": "5"}
  1992. var goldMapSI = map[string]interface{}{"hello 1": "1", "hello 2": "2", "hello 3": "3", "hello 4": "4", "hello 5": "5"}
  1993. var goldMapIS = map[interface{}]testCustomStringT{"hello 1": "1", "hello 2": "2", "hello 3": "3", "hello 4": "4", "hello 5": "5"}
  1994. var goldMap = []interface{}{goldMapSS, goldMapSI, goldMapIS}
  1995. for j, g := range goldMap {
  1996. bh.MapType = reflect.TypeOf(g)
  1997. name := fmt.Sprintf("map-%s-%v", name, j+1)
  1998. // for formats that clearly differentiate binary from string, use v2i
  1999. // else use the v2s (with all strings, no []byte)
  2000. v2d = nil
  2001. // v2d = reflect.New(bh.MapType).Elem().Interface()
  2002. switch h.(type) {
  2003. case *MsgpackHandle, *BincHandle, *CborHandle:
  2004. b = testMarshalErr(v2i, h, t, name)
  2005. testUnmarshalErr(&v2d, b, h, t, name)
  2006. testDeepEqualErr(v2d, goldMap[j], t, name)
  2007. default:
  2008. b = testMarshalErr(v2s, h, t, name)
  2009. testUnmarshalErr(&v2d, b, h, t, name)
  2010. testDeepEqualErr(v2d, goldMap[j], t, name)
  2011. b = testMarshalErr(v2ss, h, t, name)
  2012. v2d = nil
  2013. testUnmarshalErr(&v2d, b, h, t, name)
  2014. testDeepEqualErr(v2d, goldMap[j], t, name)
  2015. }
  2016. }
  2017. }
  2018. func doTestScalars(t *testing.T, name string, h Handle) {
  2019. testOnce.Do(testInitAll)
  2020. // for each scalar:
  2021. // - encode its ptr
  2022. // - encode it (non-ptr)
  2023. // - check that bytes are same
  2024. // - make a copy (using reflect)
  2025. // - check that same
  2026. // - set zero on it
  2027. // - check that its equal to 0 value
  2028. // - decode into new
  2029. // - compare to original
  2030. bh := basicHandle(h)
  2031. if !bh.Canonical {
  2032. bh.Canonical = true
  2033. defer func() { bh.Canonical = false }()
  2034. }
  2035. vi := []interface{}{
  2036. int(0),
  2037. int8(0),
  2038. int16(0),
  2039. int32(0),
  2040. int64(0),
  2041. uint(0),
  2042. uint8(0),
  2043. uint16(0),
  2044. uint32(0),
  2045. uint64(0),
  2046. uintptr(0),
  2047. float32(0),
  2048. float64(0),
  2049. bool(false),
  2050. string(""),
  2051. []byte(nil),
  2052. }
  2053. for _, v := range fastpathAV {
  2054. vi = append(vi, reflect.Zero(v.rt).Interface())
  2055. }
  2056. for _, v := range vi {
  2057. rv := reflect.New(reflect.TypeOf(v)).Elem()
  2058. testRandomFillRV(rv)
  2059. v = rv.Interface()
  2060. rv2 := reflect.New(rv.Type())
  2061. rv2.Elem().Set(rv)
  2062. vp := rv2.Interface()
  2063. var tname string
  2064. switch rv.Kind() {
  2065. case reflect.Map:
  2066. tname = "map[" + rv.Type().Key().Name() + "]" + rv.Type().Elem().Name()
  2067. case reflect.Slice:
  2068. tname = "[]" + rv.Type().Elem().Name()
  2069. default:
  2070. tname = rv.Type().Name()
  2071. }
  2072. var b, b1, b2 []byte
  2073. b1 = testMarshalErr(v, h, t, tname+"-enc")
  2074. // store b1 into b, as b1 slice is reused for next marshal
  2075. b = make([]byte, len(b1))
  2076. copy(b, b1)
  2077. b2 = testMarshalErr(vp, h, t, tname+"-enc-ptr")
  2078. testDeepEqualErr(b1, b2, t, tname+"-enc-eq")
  2079. setZero(vp)
  2080. testDeepEqualErr(rv2.Elem().Interface(), reflect.Zero(rv.Type()).Interface(), t, tname+"-enc-eq-zero-ref")
  2081. vp = rv2.Interface()
  2082. testUnmarshalErr(vp, b, h, t, tname+"-dec")
  2083. testDeepEqualErr(rv2.Elem().Interface(), v, t, tname+"-dec-eq")
  2084. }
  2085. }
  2086. func doTestIntfMapping(t *testing.T, name string, h Handle) {
  2087. testOnce.Do(testInitAll)
  2088. rti := reflect.TypeOf((*testIntfMapI)(nil)).Elem()
  2089. defer func() { basicHandle(h).Intf2Impl(rti, nil) }()
  2090. type T9 struct {
  2091. I testIntfMapI
  2092. }
  2093. for i, v := range []testIntfMapI{
  2094. // Use a valid string to test some extents of json string decoding
  2095. &testIntfMapT1{"ABC \x41=\x42 \u2318 - \r \b \f - \u2028 and \u2029 ."},
  2096. testIntfMapT2{"DEF"},
  2097. } {
  2098. if err := basicHandle(h).Intf2Impl(rti, reflect.TypeOf(v)); err != nil {
  2099. failT(t, "Error mapping %v to %T", rti, v)
  2100. }
  2101. var v1, v2 T9
  2102. v1 = T9{v}
  2103. b := testMarshalErr(v1, h, t, name+"-enc-"+strconv.Itoa(i))
  2104. testUnmarshalErr(&v2, b, h, t, name+"-dec-"+strconv.Itoa(i))
  2105. testDeepEqualErr(v1, v2, t, name+"-dec-eq-"+strconv.Itoa(i))
  2106. }
  2107. }
  2108. func doTestOmitempty(t *testing.T, name string, h Handle) {
  2109. testOnce.Do(testInitAll)
  2110. if basicHandle(h).StructToArray {
  2111. t.Skipf("Skipping OmitEmpty test when StructToArray=true")
  2112. }
  2113. type T1 struct {
  2114. A int `codec:"a"`
  2115. B *int `codec:"b,omitempty"`
  2116. C int `codec:"c,omitempty"`
  2117. }
  2118. type T2 struct {
  2119. A int `codec:"a"`
  2120. }
  2121. var v1 T1
  2122. var v2 T2
  2123. b1 := testMarshalErr(v1, h, t, name+"-omitempty")
  2124. b2 := testMarshalErr(v2, h, t, name+"-no-omitempty-trunc")
  2125. testDeepEqualErr(b1, b2, t, name+"-omitempty-cmp")
  2126. }
  2127. func doTestMissingFields(t *testing.T, name string, h Handle) {
  2128. testOnce.Do(testInitAll)
  2129. if codecgen {
  2130. t.Skipf("Skipping Missing Fields tests as it is not honored by codecgen")
  2131. }
  2132. if basicHandle(h).StructToArray {
  2133. t.Skipf("Skipping Missing Fields test when StructToArray=true")
  2134. }
  2135. // encode missingFielderT2, decode into missingFielderT1, encode it out again, decode into new missingFielderT2, compare
  2136. v1 := missingFielderT2{S: "true seven eight", B: true, F: 777.0, I: -888}
  2137. b1 := testMarshalErr(v1, h, t, name+"-missing-enc-2")
  2138. // xdebugf("marshal into b1: %s", b1)
  2139. var v2 missingFielderT1
  2140. testUnmarshalErr(&v2, b1, h, t, name+"-missing-dec-1")
  2141. // xdebugf("unmarshal into v2: %v", v2)
  2142. b2 := testMarshalErr(&v2, h, t, name+"-missing-enc-1")
  2143. // xdebugf("marshal into b2: %s", b2)
  2144. var v3 missingFielderT2
  2145. testUnmarshalErr(&v3, b2, h, t, name+"-missing-dec-2")
  2146. // xdebugf("unmarshal into v3: %v", v3)
  2147. testDeepEqualErr(v1, v3, t, name+"-missing-cmp-2")
  2148. }
  2149. func doTestMaxDepth(t *testing.T, name string, h Handle) {
  2150. testOnce.Do(testInitAll)
  2151. type T struct {
  2152. I interface{} // value to encode
  2153. M int16 // maxdepth
  2154. S bool // use swallow (decode into typed struct with only A1)
  2155. E interface{} // error to find
  2156. }
  2157. type T1 struct {
  2158. A1 *T1
  2159. }
  2160. var table []T
  2161. var sfunc = func(n int) (s [1]interface{}, s1 *[1]interface{}) {
  2162. s1 = &s
  2163. for i := 0; i < n; i++ {
  2164. var s0 [1]interface{}
  2165. s1[0] = &s0
  2166. s1 = &s0
  2167. }
  2168. // xdebugf("sfunc s: %v", s)
  2169. return
  2170. // var s []interface{}
  2171. // s = append(s, []interface{})
  2172. // s[0] = append(s[0], []interface{})
  2173. // s[0][0] = append(s[0][0], []interface{})
  2174. // s[0][0][0] = append(s[0][0][0], []interface{})
  2175. // s[0][0][0][0] = append(s[0][0][0][0], []interface{})
  2176. // return s
  2177. }
  2178. var mfunc = func(n int) (m map[string]interface{}, mlast map[string]interface{}) {
  2179. m = make(map[string]interface{})
  2180. mlast = make(map[string]interface{})
  2181. m["A0"] = mlast
  2182. for i := 1; i < n; i++ {
  2183. m0 := make(map[string]interface{})
  2184. mlast["A"+strconv.FormatInt(int64(i), 10)] = m0
  2185. mlast = m0
  2186. }
  2187. // xdebugf("mfunc m: %v", m)
  2188. return
  2189. }
  2190. s, s1 := sfunc(5)
  2191. m, _ := mfunc(5)
  2192. m99, _ := mfunc(99)
  2193. s1[0] = m
  2194. table = append(table, T{s, 0, false, nil})
  2195. table = append(table, T{s, 256, false, nil})
  2196. table = append(table, T{s, 7, false, errMaxDepthExceeded})
  2197. table = append(table, T{s, 15, false, nil})
  2198. table = append(table, T{m99, 15, true, errMaxDepthExceeded})
  2199. table = append(table, T{m99, 215, true, nil})
  2200. defer func(n int16, b bool) {
  2201. basicHandle(h).MaxDepth = n
  2202. testUseMust = b
  2203. }(basicHandle(h).MaxDepth, testUseMust)
  2204. testUseMust = false
  2205. for i, v := range table {
  2206. basicHandle(h).MaxDepth = v.M
  2207. b1 := testMarshalErr(v.I, h, t, name+"-maxdepth-enc"+strconv.FormatInt(int64(i), 10))
  2208. // xdebugf("b1: %s", b1)
  2209. var err error
  2210. if v.S {
  2211. var v2 T1
  2212. err = testUnmarshal(&v2, b1, h)
  2213. } else {
  2214. var v2 interface{}
  2215. err = testUnmarshal(&v2, b1, h)
  2216. }
  2217. if err1, ok := err.(decodeError); ok {
  2218. err = err1.codecError
  2219. }
  2220. var err0 interface{} = err
  2221. if err1, ok := err.(codecError); ok {
  2222. err0 = err1.err
  2223. }
  2224. if err0 != v.E {
  2225. failT(t, "Unexpected error testing max depth for depth %d: expected %v, received %v", v.M, v.E, err)
  2226. }
  2227. // decode into something that just triggers swallow
  2228. }
  2229. }
  2230. func doTestMultipleEncDec(t *testing.T, name string, h Handle) {
  2231. testOnce.Do(testInitAll)
  2232. // encode a string multiple times.
  2233. // decode it multiple times.
  2234. // ensure we get the value each time
  2235. var s1 = "ugorji"
  2236. var s2 = "nwoke"
  2237. var s11, s21 string
  2238. var buf bytes.Buffer
  2239. e := NewEncoder(&buf, h)
  2240. e.MustEncode(s1)
  2241. e.MustEncode(s2)
  2242. d := NewDecoder(&buf, h)
  2243. d.MustDecode(&s11)
  2244. d.MustDecode(&s21)
  2245. testDeepEqualErr(s1, s11, t, name+"-multiple-encode")
  2246. testDeepEqualErr(s2, s21, t, name+"-multiple-encode")
  2247. }
  2248. func doTestSelfExt(t *testing.T, name string, h Handle) {
  2249. testOnce.Do(testInitAll)
  2250. var ts TestSelfExtImpl
  2251. ts.S = "ugorji"
  2252. ts.I = 5678
  2253. ts.B = true
  2254. var ts2 TestSelfExtImpl
  2255. bs := testMarshalErr(&ts, h, t, name)
  2256. testUnmarshalErr(&ts2, bs, h, t, name)
  2257. testDeepEqualErr(&ts, &ts2, t, name)
  2258. }
  2259. func doTestBytesEncodedAsArray(t *testing.T, name string, h Handle) {
  2260. testOnce.Do(testInitAll)
  2261. // Need to test edge case where bytes are encoded as an array
  2262. // (not using optimized []byte native format)
  2263. // encode []int8 (or int32 or any numeric type) with all positive numbers
  2264. // decode it into []uint8
  2265. var in = make([]int32, 128)
  2266. var un = make([]uint8, 128)
  2267. for i := range in {
  2268. in[i] = int32(i)
  2269. un[i] = uint8(i)
  2270. }
  2271. var out []byte
  2272. bs := testMarshalErr(&in, h, t, name)
  2273. testUnmarshalErr(&out, bs, h, t, name)
  2274. // xdebugf("in: %v", in)
  2275. // xdebug2f("out: %v\n", out)
  2276. testDeepEqualErr(un, out, t, name)
  2277. }
  2278. func doTestStrucEncDec(t *testing.T, name string, h Handle) {
  2279. testOnce.Do(testInitAll)
  2280. {
  2281. var ts1 = newTestStruc(2, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
  2282. var ts2 TestStruc
  2283. bs := testMarshalErr(ts1, h, t, name)
  2284. testUnmarshalErr(&ts2, bs, h, t, name)
  2285. testDeepEqualErr(ts1, &ts2, t, name)
  2286. }
  2287. // Note: We cannot use TestStrucFlex because it has omitempty values,
  2288. // Meaning that sometimes, encoded and decoded will not be same.
  2289. // {
  2290. // var ts1 = newTestStrucFlex(2, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
  2291. // var ts2 TestStrucFlex
  2292. // bs := testMarshalErr(ts1, h, t, name)
  2293. // fmt.Printf("\n\n%s\n\n", bs)
  2294. // testUnmarshalErr(&ts2, bs, h, t, name)
  2295. // testDeepEqualErr(ts1, &ts2, t, name)
  2296. // }
  2297. }
  2298. // -----------------
  2299. func TestJsonDecodeNonStringScalarInStringContext(t *testing.T) {
  2300. testOnce.Do(testInitAll)
  2301. var b = `{"s.true": "true", "b.true": true, "s.false": "false", "b.false": false, "s.10": "10", "i.10": 10, "i.-10": -10}`
  2302. 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"}
  2303. var m map[string]string
  2304. d := NewDecoderBytes([]byte(b), testJsonH)
  2305. d.MustDecode(&m)
  2306. if err := deepEqual(golden, m); err == nil {
  2307. logTv(t, "++++ match: decoded: %#v", m)
  2308. } else {
  2309. failT(t, "---- mismatch: %v ==> golden: %#v, decoded: %#v", err, golden, m)
  2310. }
  2311. }
  2312. func TestJsonEncodeIndent(t *testing.T) {
  2313. testOnce.Do(testInitAll)
  2314. v := TestSimplish{
  2315. Ii: -794,
  2316. Ss: `A Man is
  2317. after the new line
  2318. after new line and tab
  2319. `,
  2320. }
  2321. v2 := v
  2322. v.Mm = make(map[string]*TestSimplish)
  2323. for i := 0; i < len(v.Ar); i++ {
  2324. v3 := v2
  2325. v3.Ii += (i * 4)
  2326. v3.Ss = fmt.Sprintf("%d - %s", v3.Ii, v3.Ss)
  2327. if i%2 == 0 {
  2328. v.Ar[i] = &v3
  2329. }
  2330. // v3 = v2
  2331. v.Sl = append(v.Sl, &v3)
  2332. v.Mm[strconv.FormatInt(int64(i), 10)] = &v3
  2333. }
  2334. oldcan := testJsonH.Canonical
  2335. oldIndent := testJsonH.Indent
  2336. oldS2A := testJsonH.StructToArray
  2337. defer func() {
  2338. testJsonH.Canonical = oldcan
  2339. testJsonH.Indent = oldIndent
  2340. testJsonH.StructToArray = oldS2A
  2341. }()
  2342. testJsonH.Canonical = true
  2343. testJsonH.Indent = -1
  2344. testJsonH.StructToArray = false
  2345. var bs []byte
  2346. NewEncoderBytes(&bs, testJsonH).MustEncode(&v)
  2347. txt1Tab := string(bs)
  2348. bs = nil
  2349. testJsonH.Indent = 120
  2350. NewEncoderBytes(&bs, testJsonH).MustEncode(&v)
  2351. txtSpaces := string(bs)
  2352. // fmt.Printf("\n-----------\n%s\n------------\n%s\n-------------\n", txt1Tab, txtSpaces)
  2353. goldenResultTab := `{
  2354. "Ar": [
  2355. {
  2356. "Ar": [
  2357. null,
  2358. null
  2359. ],
  2360. "Ii": -794,
  2361. "Mm": null,
  2362. "Sl": null,
  2363. "Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
  2364. },
  2365. null
  2366. ],
  2367. "Ii": -794,
  2368. "Mm": {
  2369. "0": {
  2370. "Ar": [
  2371. null,
  2372. null
  2373. ],
  2374. "Ii": -794,
  2375. "Mm": null,
  2376. "Sl": null,
  2377. "Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
  2378. },
  2379. "1": {
  2380. "Ar": [
  2381. null,
  2382. null
  2383. ],
  2384. "Ii": -790,
  2385. "Mm": null,
  2386. "Sl": null,
  2387. "Ss": "-790 - A Man is\nafter the new line\n\tafter new line and tab\n"
  2388. }
  2389. },
  2390. "Sl": [
  2391. {
  2392. "Ar": [
  2393. null,
  2394. null
  2395. ],
  2396. "Ii": -794,
  2397. "Mm": null,
  2398. "Sl": null,
  2399. "Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
  2400. },
  2401. {
  2402. "Ar": [
  2403. null,
  2404. null
  2405. ],
  2406. "Ii": -790,
  2407. "Mm": null,
  2408. "Sl": null,
  2409. "Ss": "-790 - A Man is\nafter the new line\n\tafter new line and tab\n"
  2410. }
  2411. ],
  2412. "Ss": "A Man is\nafter the new line\n\tafter new line and tab\n"
  2413. }`
  2414. if txt1Tab != goldenResultTab {
  2415. failT(t, "decoded indented with tabs != expected: \nexpected: %s\nencoded: %s", goldenResultTab, txt1Tab)
  2416. }
  2417. if txtSpaces != strings.Replace(goldenResultTab, "\t", strings.Repeat(" ", 120), -1) {
  2418. failT(t, "decoded indented with spaces != expected: \nexpected: %s\nencoded: %s", goldenResultTab, txtSpaces)
  2419. }
  2420. }
  2421. func TestBufioDecReader(t *testing.T) {
  2422. testOnce.Do(testInitAll)
  2423. // try to read 85 bytes in chunks of 7 at a time.
  2424. var s = strings.Repeat("01234'56789 ", 5)
  2425. // fmt.Printf("s: %s\n", s)
  2426. var r = strings.NewReader(s)
  2427. var br = &bufioDecReader{buf: make([]byte, 0, 13)}
  2428. br.r = r
  2429. b, err := ioutil.ReadAll(br.r)
  2430. if err != nil {
  2431. panic(err)
  2432. }
  2433. var s2 = string(b)
  2434. // fmt.Printf("s==s2: %v, len(s): %v, len(b): %v, len(s2): %v\n", s == s2, len(s), len(b), len(s2))
  2435. if s != s2 {
  2436. failT(t, "not equal: \ns: %s\ns2: %s", s, s2)
  2437. }
  2438. // Now, test search functions for skip, readTo and readUntil
  2439. // readUntil ', readTo ', skip whitespace. 3 times in a loop, each time compare the token and/or outs
  2440. // readUntil: see: 56789
  2441. var out []byte
  2442. var token byte
  2443. br = &bufioDecReader{buf: make([]byte, 0, 7)}
  2444. br.r = strings.NewReader(s)
  2445. // println()
  2446. for _, v2 := range [...]string{
  2447. `01234'`,
  2448. `56789 01234'`,
  2449. `56789 01234'`,
  2450. `56789 01234'`,
  2451. } {
  2452. out = br.readUntil(nil, '\'')
  2453. testDeepEqualErr(string(out), v2, t, "-")
  2454. // fmt.Printf("readUntil: out: `%s`\n", out)
  2455. }
  2456. br = &bufioDecReader{buf: make([]byte, 0, 7)}
  2457. br.r = strings.NewReader(s)
  2458. // println()
  2459. for range [4]struct{}{} {
  2460. out = br.readTo(nil, &jsonNumSet)
  2461. testDeepEqualErr(string(out), `01234`, t, "-")
  2462. // fmt.Printf("readTo: out: `%s`\n", out)
  2463. out = br.readUntil(nil, '\'')
  2464. testDeepEqualErr(string(out), "'", t, "-")
  2465. // fmt.Printf("readUntil: out: `%s`\n", out)
  2466. out = br.readTo(nil, &jsonNumSet)
  2467. testDeepEqualErr(string(out), `56789`, t, "-")
  2468. // fmt.Printf("readTo: out: `%s`\n", out)
  2469. out = br.readUntil(nil, '0')
  2470. testDeepEqualErr(string(out), ` 0`, t, "-")
  2471. // fmt.Printf("readUntil: out: `%s`\n", out)
  2472. br.unreadn1()
  2473. }
  2474. br = &bufioDecReader{buf: make([]byte, 0, 7)}
  2475. br.r = strings.NewReader(s)
  2476. // println()
  2477. for range [4]struct{}{} {
  2478. out = br.readUntil(nil, ' ')
  2479. testDeepEqualErr(string(out), `01234'56789 `, t, "-")
  2480. // fmt.Printf("readUntil: out: |%s|\n", out)
  2481. token = br.skip(&jsonCharWhitespaceSet)
  2482. testDeepEqualErr(token, byte('0'), t, "-")
  2483. // fmt.Printf("skip: token: '%c'\n", token)
  2484. br.unreadn1()
  2485. }
  2486. // println()
  2487. }
  2488. func TestAtomic(t *testing.T) {
  2489. testOnce.Do(testInitAll)
  2490. // load, store, load, confirm
  2491. if true {
  2492. var a atomicTypeInfoSlice
  2493. l := a.load()
  2494. if l != nil {
  2495. failT(t, "atomic fail: %T, expected load return nil, received: %v", a, l)
  2496. }
  2497. l = append(l, rtid2ti{})
  2498. a.store(l)
  2499. l = a.load()
  2500. if len(l) != 1 {
  2501. failT(t, "atomic fail: %T, expected load to have length 1, received: %d", a, len(l))
  2502. }
  2503. }
  2504. if true {
  2505. var a atomicRtidFnSlice
  2506. l := a.load()
  2507. if l != nil {
  2508. failT(t, "atomic fail: %T, expected load return nil, received: %v", a, l)
  2509. }
  2510. l = append(l, codecRtidFn{})
  2511. a.store(l)
  2512. l = a.load()
  2513. if len(l) != 1 {
  2514. failT(t, "atomic fail: %T, expected load to have length 1, received: %d", a, len(l))
  2515. }
  2516. }
  2517. if true {
  2518. var a atomicClsErr
  2519. l := a.load()
  2520. if l.errClosed != nil {
  2521. failT(t, "atomic fail: %T, expected load return clsErr = nil, received: %v", a, l.errClosed)
  2522. }
  2523. l.errClosed = io.EOF
  2524. a.store(l)
  2525. l = a.load()
  2526. if l.errClosed != io.EOF {
  2527. failT(t, "atomic fail: %T, expected clsErr = io.EOF, received: %v", a, l.errClosed)
  2528. }
  2529. }
  2530. }
  2531. // -----------
  2532. func TestJsonLargeInteger(t *testing.T) {
  2533. testOnce.Do(testInitAll)
  2534. for _, i := range []uint8{'L', 'A', 0} {
  2535. for _, j := range []interface{}{
  2536. int64(1 << 60),
  2537. -int64(1 << 60),
  2538. 0,
  2539. 1 << 20,
  2540. -(1 << 20),
  2541. uint64(1 << 60),
  2542. uint(0),
  2543. uint(1 << 20),
  2544. } {
  2545. doTestJsonLargeInteger(t, j, i)
  2546. }
  2547. }
  2548. }
  2549. func TestJsonInvalidUnicode(t *testing.T) {
  2550. testOnce.Do(testInitAll)
  2551. var m = map[string]string{
  2552. `"\udc49\u0430abc"`: "\uFFFDabc",
  2553. `"\udc49\u0430"`: "\uFFFD",
  2554. `"\udc49abc"`: "\uFFFDabc",
  2555. `"\udc49"`: "\uFFFD",
  2556. `"\udZ49\u0430abc"`: "\uFFFD\u0430abc",
  2557. `"\udcG9\u0430"`: "\uFFFD\u0430",
  2558. `"\uHc49abc"`: "\uFFFDabc",
  2559. `"\uKc49"`: "\uFFFD",
  2560. // ``: "",
  2561. }
  2562. for k, v := range m {
  2563. // println("k = ", k)
  2564. var s string
  2565. testUnmarshalErr(&s, []byte(k), testJsonH, t, "-")
  2566. if s != v {
  2567. failT(t, "not equal: %q, %q", v, s)
  2568. }
  2569. }
  2570. }
  2571. func TestMsgpackDecodeMapAndExtSizeMismatch(t *testing.T) {
  2572. fn := func(t *testing.T, b []byte, v interface{}) {
  2573. if err := NewDecoderBytes(b, testMsgpackH).Decode(v); err != io.EOF && err != io.ErrUnexpectedEOF {
  2574. t.Fatalf("expected EOF or ErrUnexpectedEOF, got %v", err)
  2575. }
  2576. }
  2577. // a map claiming to have 0x10eeeeee KV pairs, but only has 1.
  2578. var b = []byte{0xdf, 0x10, 0xee, 0xee, 0xee, 0x1, 0xa1, 0x1}
  2579. var m1 map[int]string
  2580. var m2 map[int][]byte
  2581. fn(t, b, &m1)
  2582. fn(t, b, &m2)
  2583. // an extension claiming to have 0x7fffffff bytes, but only has 1.
  2584. b = []byte{0xc9, 0x7f, 0xff, 0xff, 0xff, 0xda, 0x1}
  2585. var a interface{}
  2586. fn(t, b, &a)
  2587. // b = []byte{0x00}
  2588. // var s testSelferRecur
  2589. // fn(t, b, &s)
  2590. }
  2591. func TestMapRangeIndex(t *testing.T) {
  2592. // t.Skip()
  2593. type T struct {
  2594. I int
  2595. S string
  2596. B bool
  2597. M map[int]T
  2598. }
  2599. t1 := T{I: 1, B: true, S: "11", M: map[int]T{11: T{I: 11}}}
  2600. t2 := T{I: 1, B: true, S: "12", M: map[int]T{12: T{I: 12}}}
  2601. // ------
  2602. var m1 = map[string]*T{
  2603. "11": &t1,
  2604. "12": &t2,
  2605. }
  2606. var m1c = make(map[string]T)
  2607. for k, v := range m1 {
  2608. m1c[k] = *v
  2609. }
  2610. mt := reflect.TypeOf(m1)
  2611. it := mapRange(reflect.ValueOf(m1), mapAddressableRV(mt.Key()), mapAddressableRV(mt.Elem()), true)
  2612. for it.Next() {
  2613. k := it.Key().Interface().(string)
  2614. v := it.Value().Interface().(*T)
  2615. testDeepEqualErr(m1[k], v, t, "map-key-eq-it-key")
  2616. if _, ok := m1c[k]; ok {
  2617. delete(m1c, k)
  2618. } else {
  2619. failT(t, "unexpected key in map: %v", k)
  2620. }
  2621. }
  2622. testDeepEqualErr(len(m1c), 0, t, "all-keys-not-consumed")
  2623. // ------
  2624. var m2 = map[*T]T{
  2625. &t1: t1,
  2626. &t2: t2,
  2627. }
  2628. var m2c = make(map[*T]*T)
  2629. for k := range m2 {
  2630. m2c[k] = k
  2631. }
  2632. mt = reflect.TypeOf(m2)
  2633. it = mapRange(reflect.ValueOf(m2), mapAddressableRV(mt.Key()), mapAddressableRV(mt.Elem()), true)
  2634. for it.Next() {
  2635. k := it.Key().Interface().(*T)
  2636. v := it.Value().Interface().(T)
  2637. testDeepEqualErr(m2[k], v, t, "map-key-eq-it-key")
  2638. if _, ok := m2c[k]; ok {
  2639. delete(m2c, k)
  2640. } else {
  2641. failT(t, "unexpected key in map: %v", k)
  2642. }
  2643. }
  2644. testDeepEqualErr(len(m2c), 0, t, "all-keys-not-consumed")
  2645. // ---- test mapGet
  2646. fnTestMapIndex := func(mi ...interface{}) {
  2647. for _, m0 := range mi {
  2648. m := reflect.ValueOf(m0)
  2649. rvv := mapAddressableRV(m.Type().Elem())
  2650. for _, k := range m.MapKeys() {
  2651. testDeepEqualErr(m.MapIndex(k).Interface(), mapGet(m, k, rvv).Interface(), t, "map-index-eq")
  2652. }
  2653. }
  2654. }
  2655. fnTestMapIndex(m1, m1c, m2, m2c)
  2656. // var s string = "hello"
  2657. // var tt = &T{I: 3}
  2658. // ttTyp := reflect.TypeOf(tt)
  2659. // _, _ = tt, ttTyp
  2660. // mv := reflect.ValueOf(m)
  2661. // it := mapRange(mv, reflect.ValueOf(&s).Elem(), reflect.ValueOf(&tt).Elem(), true) //ok
  2662. // it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(reflect.TypeOf(T{})).Elem(), true) // ok
  2663. // it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(ttTyp.Elem()), true) // !ok
  2664. // it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(reflect.TypeOf(T{})), true) !ok
  2665. // it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(reflect.TypeOf(T{})).Elem(), true) // ok
  2666. // fmt.Printf("key: %#v\n", it.Key())
  2667. // fmt.Printf("exp: %#v\n", mv.MapIndex(it.Key()))
  2668. // fmt.Printf("val: %#v\n", it.Value())
  2669. // testDeepEqualErr(mv.MapIndex(it.Key()), it.Value().Interface()
  2670. }
  2671. // ----------
  2672. func TestBincCodecsTable(t *testing.T) {
  2673. testCodecTableOne(t, testBincH)
  2674. }
  2675. func TestBincCodecsMisc(t *testing.T) {
  2676. testCodecMiscOne(t, testBincH)
  2677. }
  2678. func TestBincCodecsEmbeddedPointer(t *testing.T) {
  2679. testCodecEmbeddedPointer(t, testBincH)
  2680. }
  2681. func TestBincStdEncIntf(t *testing.T) {
  2682. doTestStdEncIntf(t, "binc", testBincH)
  2683. }
  2684. func TestBincMammoth(t *testing.T) {
  2685. testMammoth(t, "binc", testBincH)
  2686. }
  2687. func TestSimpleCodecsTable(t *testing.T) {
  2688. testCodecTableOne(t, testSimpleH)
  2689. }
  2690. func TestSimpleCodecsMisc(t *testing.T) {
  2691. testCodecMiscOne(t, testSimpleH)
  2692. }
  2693. func TestSimpleCodecsEmbeddedPointer(t *testing.T) {
  2694. testCodecEmbeddedPointer(t, testSimpleH)
  2695. }
  2696. func TestSimpleStdEncIntf(t *testing.T) {
  2697. doTestStdEncIntf(t, "simple", testSimpleH)
  2698. }
  2699. func TestSimpleMammoth(t *testing.T) {
  2700. testMammoth(t, "simple", testSimpleH)
  2701. }
  2702. func TestMsgpackCodecsTable(t *testing.T) {
  2703. testCodecTableOne(t, testMsgpackH)
  2704. }
  2705. func TestMsgpackCodecsMisc(t *testing.T) {
  2706. testCodecMiscOne(t, testMsgpackH)
  2707. }
  2708. func TestMsgpackCodecsEmbeddedPointer(t *testing.T) {
  2709. testCodecEmbeddedPointer(t, testMsgpackH)
  2710. }
  2711. func TestMsgpackStdEncIntf(t *testing.T) {
  2712. doTestStdEncIntf(t, "msgpack", testMsgpackH)
  2713. }
  2714. func TestMsgpackMammoth(t *testing.T) {
  2715. testMammoth(t, "msgpack", testMsgpackH)
  2716. }
  2717. func TestCborCodecsTable(t *testing.T) {
  2718. testCodecTableOne(t, testCborH)
  2719. }
  2720. func TestCborCodecsMisc(t *testing.T) {
  2721. testCodecMiscOne(t, testCborH)
  2722. }
  2723. func TestCborCodecsEmbeddedPointer(t *testing.T) {
  2724. testCodecEmbeddedPointer(t, testCborH)
  2725. }
  2726. func TestCborMapEncodeForCanonical(t *testing.T) {
  2727. doTestMapEncodeForCanonical(t, "cbor", testCborH)
  2728. }
  2729. func TestCborCodecChan(t *testing.T) {
  2730. testCodecChan(t, testCborH)
  2731. }
  2732. func TestCborStdEncIntf(t *testing.T) {
  2733. doTestStdEncIntf(t, "cbor", testCborH)
  2734. }
  2735. func TestCborMammoth(t *testing.T) {
  2736. testMammoth(t, "cbor", testCborH)
  2737. }
  2738. func TestJsonCodecsTable(t *testing.T) {
  2739. testCodecTableOne(t, testJsonH)
  2740. }
  2741. func TestJsonCodecsMisc(t *testing.T) {
  2742. testCodecMiscOne(t, testJsonH)
  2743. }
  2744. func TestJsonCodecsEmbeddedPointer(t *testing.T) {
  2745. testCodecEmbeddedPointer(t, testJsonH)
  2746. }
  2747. func TestJsonCodecChan(t *testing.T) {
  2748. testCodecChan(t, testJsonH)
  2749. }
  2750. func TestJsonStdEncIntf(t *testing.T) {
  2751. doTestStdEncIntf(t, "json", testJsonH)
  2752. }
  2753. func TestJsonMammoth(t *testing.T) {
  2754. testMammoth(t, "json", testJsonH)
  2755. }
  2756. // ----- Raw ---------
  2757. func TestJsonRaw(t *testing.T) {
  2758. doTestRawValue(t, "json", testJsonH)
  2759. }
  2760. func TestBincRaw(t *testing.T) {
  2761. doTestRawValue(t, "binc", testBincH)
  2762. }
  2763. func TestMsgpackRaw(t *testing.T) {
  2764. doTestRawValue(t, "msgpack", testMsgpackH)
  2765. }
  2766. func TestSimpleRaw(t *testing.T) {
  2767. doTestRawValue(t, "simple", testSimpleH)
  2768. }
  2769. func TestCborRaw(t *testing.T) {
  2770. doTestRawValue(t, "cbor", testCborH)
  2771. }
  2772. // ----- ALL (framework based) -----
  2773. func TestAllEncCircularRef(t *testing.T) {
  2774. doTestEncCircularRef(t, "cbor", testCborH)
  2775. }
  2776. func TestAllAnonCycle(t *testing.T) {
  2777. doTestAnonCycle(t, "cbor", testCborH)
  2778. }
  2779. func TestAllErrWriter(t *testing.T) {
  2780. doTestErrWriter(t, "cbor", testCborH)
  2781. doTestErrWriter(t, "json", testJsonH)
  2782. }
  2783. // ----- RPC -----
  2784. func TestBincRpcGo(t *testing.T) {
  2785. testCodecRpcOne(t, GoRpc, testBincH, true, 0)
  2786. }
  2787. func TestSimpleRpcGo(t *testing.T) {
  2788. testCodecRpcOne(t, GoRpc, testSimpleH, true, 0)
  2789. }
  2790. func TestMsgpackRpcGo(t *testing.T) {
  2791. testCodecRpcOne(t, GoRpc, testMsgpackH, true, 0)
  2792. }
  2793. func TestCborRpcGo(t *testing.T) {
  2794. testCodecRpcOne(t, GoRpc, testCborH, true, 0)
  2795. }
  2796. func TestJsonRpcGo(t *testing.T) {
  2797. testCodecRpcOne(t, GoRpc, testJsonH, true, 0)
  2798. }
  2799. func TestMsgpackRpcSpec(t *testing.T) {
  2800. testCodecRpcOne(t, MsgpackSpecRpc, testMsgpackH, true, 0)
  2801. }
  2802. func TestBincUnderlyingType(t *testing.T) {
  2803. testCodecUnderlyingType(t, testBincH)
  2804. }
  2805. func TestJsonSwallowAndZero(t *testing.T) {
  2806. doTestSwallowAndZero(t, testJsonH)
  2807. }
  2808. func TestCborSwallowAndZero(t *testing.T) {
  2809. doTestSwallowAndZero(t, testCborH)
  2810. }
  2811. func TestMsgpackSwallowAndZero(t *testing.T) {
  2812. doTestSwallowAndZero(t, testMsgpackH)
  2813. }
  2814. func TestBincSwallowAndZero(t *testing.T) {
  2815. doTestSwallowAndZero(t, testBincH)
  2816. }
  2817. func TestSimpleSwallowAndZero(t *testing.T) {
  2818. doTestSwallowAndZero(t, testSimpleH)
  2819. }
  2820. func TestJsonRawExt(t *testing.T) {
  2821. doTestRawExt(t, testJsonH)
  2822. }
  2823. func TestCborRawExt(t *testing.T) {
  2824. doTestRawExt(t, testCborH)
  2825. }
  2826. func TestMsgpackRawExt(t *testing.T) {
  2827. doTestRawExt(t, testMsgpackH)
  2828. }
  2829. func TestBincRawExt(t *testing.T) {
  2830. doTestRawExt(t, testBincH)
  2831. }
  2832. func TestSimpleRawExt(t *testing.T) {
  2833. doTestRawExt(t, testSimpleH)
  2834. }
  2835. func TestJsonMapStructKey(t *testing.T) {
  2836. doTestMapStructKey(t, testJsonH)
  2837. }
  2838. func TestCborMapStructKey(t *testing.T) {
  2839. doTestMapStructKey(t, testCborH)
  2840. }
  2841. func TestMsgpackMapStructKey(t *testing.T) {
  2842. doTestMapStructKey(t, testMsgpackH)
  2843. }
  2844. func TestBincMapStructKey(t *testing.T) {
  2845. doTestMapStructKey(t, testBincH)
  2846. }
  2847. func TestSimpleMapStructKey(t *testing.T) {
  2848. doTestMapStructKey(t, testSimpleH)
  2849. }
  2850. func TestJsonDecodeNilMapValue(t *testing.T) {
  2851. doTestDecodeNilMapValue(t, testJsonH)
  2852. }
  2853. func TestCborDecodeNilMapValue(t *testing.T) {
  2854. doTestDecodeNilMapValue(t, testCborH)
  2855. }
  2856. func TestMsgpackDecodeNilMapValue(t *testing.T) {
  2857. doTestDecodeNilMapValue(t, testMsgpackH)
  2858. }
  2859. func TestBincDecodeNilMapValue(t *testing.T) {
  2860. doTestDecodeNilMapValue(t, testBincH)
  2861. }
  2862. func TestSimpleDecodeNilMapValue(t *testing.T) {
  2863. doTestDecodeNilMapValue(t, testSimpleH)
  2864. }
  2865. func TestJsonEmbeddedFieldPrecedence(t *testing.T) {
  2866. doTestEmbeddedFieldPrecedence(t, testJsonH)
  2867. }
  2868. func TestCborEmbeddedFieldPrecedence(t *testing.T) {
  2869. doTestEmbeddedFieldPrecedence(t, testCborH)
  2870. }
  2871. func TestMsgpackEmbeddedFieldPrecedence(t *testing.T) {
  2872. doTestEmbeddedFieldPrecedence(t, testMsgpackH)
  2873. }
  2874. func TestBincEmbeddedFieldPrecedence(t *testing.T) {
  2875. doTestEmbeddedFieldPrecedence(t, testBincH)
  2876. }
  2877. func TestSimpleEmbeddedFieldPrecedence(t *testing.T) {
  2878. doTestEmbeddedFieldPrecedence(t, testSimpleH)
  2879. }
  2880. func TestJsonLargeContainerLen(t *testing.T) {
  2881. doTestLargeContainerLen(t, testJsonH)
  2882. }
  2883. func TestCborLargeContainerLen(t *testing.T) {
  2884. doTestLargeContainerLen(t, testCborH)
  2885. }
  2886. func TestMsgpackLargeContainerLen(t *testing.T) {
  2887. doTestLargeContainerLen(t, testMsgpackH)
  2888. }
  2889. func TestBincLargeContainerLen(t *testing.T) {
  2890. doTestLargeContainerLen(t, testBincH)
  2891. }
  2892. func TestSimpleLargeContainerLen(t *testing.T) {
  2893. doTestLargeContainerLen(t, testSimpleH)
  2894. }
  2895. func TestJsonMammothMapsAndSlices(t *testing.T) {
  2896. doTestMammothMapsAndSlices(t, testJsonH)
  2897. }
  2898. func TestCborMammothMapsAndSlices(t *testing.T) {
  2899. doTestMammothMapsAndSlices(t, testCborH)
  2900. }
  2901. func TestMsgpackMammothMapsAndSlices(t *testing.T) {
  2902. old1 := testMsgpackH.WriteExt
  2903. defer func() { testMsgpackH.WriteExt = old1 }()
  2904. testMsgpackH.WriteExt = true
  2905. doTestMammothMapsAndSlices(t, testMsgpackH)
  2906. }
  2907. func TestBincMammothMapsAndSlices(t *testing.T) {
  2908. doTestMammothMapsAndSlices(t, testBincH)
  2909. }
  2910. func TestSimpleMammothMapsAndSlices(t *testing.T) {
  2911. doTestMammothMapsAndSlices(t, testSimpleH)
  2912. }
  2913. func TestJsonTime(t *testing.T) {
  2914. testTime(t, "json", testJsonH)
  2915. }
  2916. func TestCborTime(t *testing.T) {
  2917. testTime(t, "cbor", testCborH)
  2918. }
  2919. func TestMsgpackTime(t *testing.T) {
  2920. testTime(t, "msgpack", testMsgpackH)
  2921. }
  2922. func TestBincTime(t *testing.T) {
  2923. testTime(t, "binc", testBincH)
  2924. }
  2925. func TestSimpleTime(t *testing.T) {
  2926. testTime(t, "simple", testSimpleH)
  2927. }
  2928. func TestJsonUintToInt(t *testing.T) {
  2929. testUintToInt(t, "json", testJsonH)
  2930. }
  2931. func TestCborUintToInt(t *testing.T) {
  2932. testUintToInt(t, "cbor", testCborH)
  2933. }
  2934. func TestMsgpackUintToInt(t *testing.T) {
  2935. testUintToInt(t, "msgpack", testMsgpackH)
  2936. }
  2937. func TestBincUintToInt(t *testing.T) {
  2938. testUintToInt(t, "binc", testBincH)
  2939. }
  2940. func TestSimpleUintToInt(t *testing.T) {
  2941. testUintToInt(t, "simple", testSimpleH)
  2942. }
  2943. func TestJsonDifferentMapOrSliceType(t *testing.T) {
  2944. doTestDifferentMapOrSliceType(t, "json", testJsonH)
  2945. }
  2946. func TestCborDifferentMapOrSliceType(t *testing.T) {
  2947. doTestDifferentMapOrSliceType(t, "cbor", testCborH)
  2948. }
  2949. func TestMsgpackDifferentMapOrSliceType(t *testing.T) {
  2950. doTestDifferentMapOrSliceType(t, "msgpack", testMsgpackH)
  2951. }
  2952. func TestBincDifferentMapOrSliceType(t *testing.T) {
  2953. doTestDifferentMapOrSliceType(t, "binc", testBincH)
  2954. }
  2955. func TestSimpleDifferentMapOrSliceType(t *testing.T) {
  2956. doTestDifferentMapOrSliceType(t, "simple", testSimpleH)
  2957. }
  2958. func TestJsonScalars(t *testing.T) {
  2959. doTestScalars(t, "json", testJsonH)
  2960. }
  2961. func TestCborScalars(t *testing.T) {
  2962. doTestScalars(t, "cbor", testCborH)
  2963. }
  2964. func TestMsgpackScalars(t *testing.T) {
  2965. doTestScalars(t, "msgpack", testMsgpackH)
  2966. }
  2967. func TestBincScalars(t *testing.T) {
  2968. doTestScalars(t, "binc", testBincH)
  2969. }
  2970. func TestSimpleScalars(t *testing.T) {
  2971. doTestScalars(t, "simple", testSimpleH)
  2972. }
  2973. func TestJsonOmitempty(t *testing.T) {
  2974. doTestOmitempty(t, "json", testJsonH)
  2975. }
  2976. func TestCborOmitempty(t *testing.T) {
  2977. doTestOmitempty(t, "cbor", testCborH)
  2978. }
  2979. func TestMsgpackOmitempty(t *testing.T) {
  2980. doTestOmitempty(t, "msgpack", testMsgpackH)
  2981. }
  2982. func TestBincOmitempty(t *testing.T) {
  2983. doTestOmitempty(t, "binc", testBincH)
  2984. }
  2985. func TestSimpleOmitempty(t *testing.T) {
  2986. doTestOmitempty(t, "simple", testSimpleH)
  2987. }
  2988. func TestJsonIntfMapping(t *testing.T) {
  2989. doTestIntfMapping(t, "json", testJsonH)
  2990. }
  2991. func TestCborIntfMapping(t *testing.T) {
  2992. doTestIntfMapping(t, "cbor", testCborH)
  2993. }
  2994. func TestMsgpackIntfMapping(t *testing.T) {
  2995. doTestIntfMapping(t, "msgpack", testMsgpackH)
  2996. }
  2997. func TestBincIntfMapping(t *testing.T) {
  2998. doTestIntfMapping(t, "binc", testBincH)
  2999. }
  3000. func TestSimpleIntfMapping(t *testing.T) {
  3001. doTestIntfMapping(t, "simple", testSimpleH)
  3002. }
  3003. func TestJsonMissingFields(t *testing.T) {
  3004. doTestMissingFields(t, "json", testJsonH)
  3005. }
  3006. func TestCborMissingFields(t *testing.T) {
  3007. doTestMissingFields(t, "cbor", testCborH)
  3008. }
  3009. func TestMsgpackMissingFields(t *testing.T) {
  3010. doTestMissingFields(t, "msgpack", testMsgpackH)
  3011. }
  3012. func TestBincMissingFields(t *testing.T) {
  3013. doTestMissingFields(t, "binc", testBincH)
  3014. }
  3015. func TestSimpleMissingFields(t *testing.T) {
  3016. doTestMissingFields(t, "simple", testSimpleH)
  3017. }
  3018. func TestJsonMaxDepth(t *testing.T) {
  3019. doTestMaxDepth(t, "json", testJsonH)
  3020. }
  3021. func TestCborMaxDepth(t *testing.T) {
  3022. doTestMaxDepth(t, "cbor", testCborH)
  3023. }
  3024. func TestMsgpackMaxDepth(t *testing.T) {
  3025. doTestMaxDepth(t, "msgpack", testMsgpackH)
  3026. }
  3027. func TestBincMaxDepth(t *testing.T) {
  3028. doTestMaxDepth(t, "binc", testBincH)
  3029. }
  3030. func TestSimpleMaxDepth(t *testing.T) {
  3031. doTestMaxDepth(t, "simple", testSimpleH)
  3032. }
  3033. func TestJsonSelfExt(t *testing.T) {
  3034. doTestSelfExt(t, "json", testJsonH)
  3035. }
  3036. func TestCborSelfExt(t *testing.T) {
  3037. doTestSelfExt(t, "cbor", testCborH)
  3038. }
  3039. func TestMsgpackSelfExt(t *testing.T) {
  3040. doTestSelfExt(t, "msgpack", testMsgpackH)
  3041. }
  3042. func TestBincSelfExt(t *testing.T) {
  3043. doTestSelfExt(t, "binc", testBincH)
  3044. }
  3045. func TestSimpleSelfExt(t *testing.T) {
  3046. doTestSelfExt(t, "simple", testSimpleH)
  3047. }
  3048. func TestJsonBytesEncodedAsArray(t *testing.T) {
  3049. doTestBytesEncodedAsArray(t, "json", testJsonH)
  3050. }
  3051. func TestCborBytesEncodedAsArray(t *testing.T) {
  3052. doTestBytesEncodedAsArray(t, "cbor", testCborH)
  3053. }
  3054. func TestMsgpackBytesEncodedAsArray(t *testing.T) {
  3055. doTestBytesEncodedAsArray(t, "msgpack", testMsgpackH)
  3056. }
  3057. func TestBincBytesEncodedAsArray(t *testing.T) {
  3058. doTestBytesEncodedAsArray(t, "binc", testBincH)
  3059. }
  3060. func TestSimpleBytesEncodedAsArray(t *testing.T) {
  3061. doTestBytesEncodedAsArray(t, "simple", testSimpleH)
  3062. }
  3063. func TestJsonStrucEncDec(t *testing.T) {
  3064. doTestStrucEncDec(t, "json", testJsonH)
  3065. }
  3066. func TestCborStrucEncDec(t *testing.T) {
  3067. doTestStrucEncDec(t, "cbor", testCborH)
  3068. }
  3069. func TestMsgpackStrucEncDec(t *testing.T) {
  3070. doTestStrucEncDec(t, "msgpack", testMsgpackH)
  3071. }
  3072. func TestBincStrucEncDec(t *testing.T) {
  3073. doTestStrucEncDec(t, "binc", testBincH)
  3074. }
  3075. func TestSimpleStrucEncDec(t *testing.T) {
  3076. doTestStrucEncDec(t, "simple", testSimpleH)
  3077. }
  3078. // --------
  3079. func TestMultipleEncDec(t *testing.T) {
  3080. doTestMultipleEncDec(t, "json", testJsonH)
  3081. }
  3082. // TODO:
  3083. //
  3084. // Add Tests for the following:
  3085. // - struct tags: on anonymous fields, _struct (all fields), etc
  3086. // - chan to encode and decode (with support for codecgen also)
  3087. //
  3088. // Add negative tests for failure conditions:
  3089. // - bad input with large array length prefix
  3090. //
  3091. // Add tests for decode.go (standalone)
  3092. // - UnreadByte: only 2 states (z.ls = 2 and z.ls = 1) (0 --> 2 --> 1)
  3093. // - track: z.trb: track, stop track, check
  3094. // - PreferArrayOverSlice???
  3095. // - InterfaceReset
  3096. // - (chan byte) to decode []byte (with mapbyslice track)
  3097. // - decode slice of len 6, 16 into slice of (len 4, cap 8) and (len ) with maxinitlen=6, 8, 16
  3098. // - DeleteOnNilMapValue
  3099. // - decnaked: n.l == nil
  3100. // - ensureDecodeable (try to decode into a non-decodeable thing e.g. a nil interface{},
  3101. //
  3102. // Add tests for encode.go (standalone)
  3103. // - nil and 0-len slices and maps for non-fastpath things