cbor_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/hex"
  8. "math"
  9. "os"
  10. "regexp"
  11. "strings"
  12. "testing"
  13. )
  14. func TestCborIndefiniteLength(t *testing.T) {
  15. oldMapType := testCborH.MapType
  16. defer func() {
  17. testCborH.MapType = oldMapType
  18. }()
  19. testCborH.MapType = testMapStrIntfTyp
  20. // var (
  21. // M1 map[string][]byte
  22. // M2 map[uint64]bool
  23. // L1 []interface{}
  24. // S1 []string
  25. // B1 []byte
  26. // )
  27. var v, vv interface{}
  28. // define it (v), encode it using indefinite lengths, decode it (vv), compare v to vv
  29. v = map[string]interface{}{
  30. "one-byte-key": []byte{1, 2, 3, 4, 5, 6},
  31. "two-string-key": "two-value",
  32. "three-list-key": []interface{}{true, false, uint64(1), int64(-1)},
  33. }
  34. var buf bytes.Buffer
  35. // buf.Reset()
  36. e := NewEncoder(&buf, testCborH)
  37. buf.WriteByte(cborBdIndefiniteMap)
  38. //----
  39. buf.WriteByte(cborBdIndefiniteString)
  40. e.MustEncode("one-")
  41. e.MustEncode("byte-")
  42. e.MustEncode("key")
  43. buf.WriteByte(cborBdBreak)
  44. buf.WriteByte(cborBdIndefiniteBytes)
  45. e.MustEncode([]byte{1, 2, 3})
  46. e.MustEncode([]byte{4, 5, 6})
  47. buf.WriteByte(cborBdBreak)
  48. //----
  49. buf.WriteByte(cborBdIndefiniteString)
  50. e.MustEncode("two-")
  51. e.MustEncode("string-")
  52. e.MustEncode("key")
  53. buf.WriteByte(cborBdBreak)
  54. buf.WriteByte(cborBdIndefiniteString)
  55. e.MustEncode([]byte("two-")) // encode as bytes, to check robustness of code
  56. e.MustEncode([]byte("value"))
  57. buf.WriteByte(cborBdBreak)
  58. //----
  59. buf.WriteByte(cborBdIndefiniteString)
  60. e.MustEncode("three-")
  61. e.MustEncode("list-")
  62. e.MustEncode("key")
  63. buf.WriteByte(cborBdBreak)
  64. buf.WriteByte(cborBdIndefiniteArray)
  65. e.MustEncode(true)
  66. e.MustEncode(false)
  67. e.MustEncode(uint64(1))
  68. e.MustEncode(int64(-1))
  69. buf.WriteByte(cborBdBreak)
  70. buf.WriteByte(cborBdBreak) // close map
  71. NewDecoderBytes(buf.Bytes(), testCborH).MustDecode(&vv)
  72. if err := deepEqual(v, vv); err != nil {
  73. t.Logf("-------- Before and After marshal do not match: Error: %v", err)
  74. if testVerbose {
  75. t.Logf(" ....... GOLDEN: (%T) %#v", v, v)
  76. t.Logf(" ....... DECODED: (%T) %#v", vv, vv)
  77. }
  78. t.FailNow()
  79. }
  80. }
  81. type testCborGolden struct {
  82. Base64 string `codec:"cbor"`
  83. Hex string `codec:"hex"`
  84. Roundtrip bool `codec:"roundtrip"`
  85. Decoded interface{} `codec:"decoded"`
  86. Diagnostic string `codec:"diagnostic"`
  87. Skip bool `codec:"skip"`
  88. }
  89. // Some tests are skipped because they include numbers outside the range of int64/uint64
  90. func TestCborGoldens(t *testing.T) {
  91. oldMapType := testCborH.MapType
  92. defer func() {
  93. testCborH.MapType = oldMapType
  94. }()
  95. testCborH.MapType = testMapStrIntfTyp
  96. // decode test-cbor-goldens.json into a list of []*testCborGolden
  97. // for each one,
  98. // - decode hex into []byte bs
  99. // - decode bs into interface{} v
  100. // - compare both using deepequal
  101. // - for any miss, record it
  102. var gs []*testCborGolden
  103. f, err := os.Open("test-cbor-goldens.json")
  104. if err != nil {
  105. t.Logf("error opening test-cbor-goldens.json: %v", err)
  106. t.FailNow()
  107. }
  108. defer f.Close()
  109. jh := new(JsonHandle)
  110. jh.MapType = testMapStrIntfTyp
  111. // d := NewDecoder(f, jh)
  112. d := NewDecoder(bufio.NewReader(f), jh)
  113. // err = d.Decode(&gs)
  114. d.MustDecode(&gs)
  115. if err != nil {
  116. t.Logf("error json decoding test-cbor-goldens.json: %v", err)
  117. t.FailNow()
  118. }
  119. tagregex := regexp.MustCompile(`[\d]+\(.+?\)`)
  120. hexregex := regexp.MustCompile(`h'([0-9a-fA-F]*)'`)
  121. for i, g := range gs {
  122. // fmt.Printf("%v, skip: %v, isTag: %v, %s\n", i, g.Skip, tagregex.MatchString(g.Diagnostic), g.Diagnostic)
  123. // skip tags or simple or those with prefix, as we can't verify them.
  124. if g.Skip || strings.HasPrefix(g.Diagnostic, "simple(") || tagregex.MatchString(g.Diagnostic) {
  125. // fmt.Printf("%v: skipped\n", i)
  126. if testVerbose {
  127. t.Logf("[%v] skipping because skip=true OR unsupported simple value or Tag Value", i)
  128. }
  129. continue
  130. }
  131. // println("++++++++++++", i, "g.Diagnostic", g.Diagnostic)
  132. if hexregex.MatchString(g.Diagnostic) {
  133. // println(i, "g.Diagnostic matched hex")
  134. if s2 := g.Diagnostic[2 : len(g.Diagnostic)-1]; s2 == "" {
  135. g.Decoded = zeroByteSlice
  136. } else if bs2, err2 := hex.DecodeString(s2); err2 == nil {
  137. g.Decoded = bs2
  138. }
  139. // fmt.Printf("%v: hex: %v\n", i, g.Decoded)
  140. }
  141. bs, err := hex.DecodeString(g.Hex)
  142. if err != nil {
  143. t.Logf("[%v] error hex decoding %s [%v]: %v", i, g.Hex, g.Hex, err)
  144. t.FailNow()
  145. }
  146. var v interface{}
  147. NewDecoderBytes(bs, testCborH).MustDecode(&v)
  148. if _, ok := v.(RawExt); ok {
  149. continue
  150. }
  151. // check the diagnostics to compare
  152. switch g.Diagnostic {
  153. case "Infinity":
  154. b := math.IsInf(v.(float64), 1)
  155. testCborError(t, i, math.Inf(1), v, nil, &b)
  156. case "-Infinity":
  157. b := math.IsInf(v.(float64), -1)
  158. testCborError(t, i, math.Inf(-1), v, nil, &b)
  159. case "NaN":
  160. // println(i, "checking NaN")
  161. b := math.IsNaN(v.(float64))
  162. testCborError(t, i, math.NaN(), v, nil, &b)
  163. case "undefined":
  164. b := v == nil
  165. testCborError(t, i, nil, v, nil, &b)
  166. default:
  167. v0 := g.Decoded
  168. // testCborCoerceJsonNumber(rv4i(&v0))
  169. testCborError(t, i, v0, v, deepEqual(v0, v), nil)
  170. }
  171. }
  172. }
  173. func testCborError(t *testing.T, i int, v0, v1 interface{}, err error, equal *bool) {
  174. if err == nil && equal == nil {
  175. // fmt.Printf("%v testCborError passed (err and equal nil)\n", i)
  176. return
  177. }
  178. if err != nil {
  179. t.Logf("[%v] deepEqual error: %v", i, err)
  180. if testVerbose {
  181. t.Logf(" ....... GOLDEN: (%T) %#v", v0, v0)
  182. t.Logf(" ....... DECODED: (%T) %#v", v1, v1)
  183. }
  184. t.FailNow()
  185. }
  186. if equal != nil && !*equal {
  187. t.Logf("[%v] values not equal", i)
  188. if testVerbose {
  189. t.Logf(" ....... GOLDEN: (%T) %#v", v0, v0)
  190. t.Logf(" ....... DECODED: (%T) %#v", v1, v1)
  191. }
  192. t.FailNow()
  193. }
  194. // fmt.Printf("%v testCborError passed (checks passed)\n", i)
  195. }
  196. func TestCborHalfFloat(t *testing.T) {
  197. m := map[uint16]float64{
  198. // using examples from
  199. // https://en.wikipedia.org/wiki/Half-precision_floating-point_format
  200. 0x3c00: 1,
  201. 0x3c01: 1 + math.Pow(2, -10),
  202. 0xc000: -2,
  203. 0x7bff: 65504,
  204. 0x0400: math.Pow(2, -14),
  205. 0x03ff: math.Pow(2, -14) - math.Pow(2, -24),
  206. 0x0001: math.Pow(2, -24),
  207. 0x0000: 0,
  208. 0x8000: -0.0,
  209. }
  210. var ba [3]byte
  211. ba[0] = cborBdFloat16
  212. var res float64
  213. for k, v := range m {
  214. res = 0
  215. bigen.PutUint16(ba[1:], k)
  216. testUnmarshalErr(&res, ba[:3], testCborH, t, "-")
  217. if res == v {
  218. if testVerbose {
  219. t.Logf("equal floats: from %x %b, %v", k, k, v)
  220. }
  221. } else {
  222. t.Logf("unequal floats: from %x %b, %v != %v", k, k, res, v)
  223. t.FailNow()
  224. }
  225. }
  226. }
  227. func TestCborSkipTags(t *testing.T) {
  228. type Tcbortags struct {
  229. A string
  230. M map[string]interface{}
  231. // A []interface{}
  232. }
  233. var b8 [8]byte
  234. var w bytesEncAppender
  235. w.b = []byte{}
  236. // To make it easier,
  237. // - use tags between math.MaxUint8 and math.MaxUint16 (incl SelfDesc)
  238. // - use 1 char strings for key names
  239. // - use 3-6 char strings for map keys
  240. // - use integers that fit in 2 bytes (between 0x20 and 0xff)
  241. var tags = [...]uint64{math.MaxUint8 * 2, math.MaxUint8 * 8, 55799, math.MaxUint16 / 2}
  242. var tagIdx int
  243. var doAddTag bool
  244. addTagFn8To16 := func() {
  245. if !doAddTag {
  246. return
  247. }
  248. // writes a tag between MaxUint8 and MaxUint16 (culled from cborEncDriver.encUint)
  249. w.writen1(cborBaseTag + 0x19)
  250. // bigenHelper.writeUint16
  251. bigen.PutUint16(b8[:2], uint16(tags[tagIdx%len(tags)]))
  252. w.writeb(b8[:2])
  253. tagIdx++
  254. }
  255. var v Tcbortags
  256. v.A = "cbor"
  257. v.M = make(map[string]interface{})
  258. v.M["111"] = uint64(111)
  259. v.M["111.11"] = 111.11
  260. v.M["true"] = true
  261. // v.A = append(v.A, 222, 22.22, "true")
  262. // make stream manually (interspacing tags around it)
  263. // WriteMapStart - e.encLen(cborBaseMap, length) - encUint(length, bd)
  264. // EncodeStringEnc - e.encStringBytesS(cborBaseString, v)
  265. fnEncode := func() {
  266. w.b = w.b[:0]
  267. addTagFn8To16()
  268. // write v (Tcbortags, with 3 fields = map with 3 entries)
  269. w.writen1(2 + cborBaseMap) // 3 fields = 3 entries
  270. // write v.A
  271. var s = "A"
  272. w.writen1(byte(len(s)) + cborBaseString)
  273. w.writestr(s)
  274. w.writen1(byte(len(v.A)) + cborBaseString)
  275. w.writestr(v.A)
  276. //w.writen1(0)
  277. addTagFn8To16()
  278. s = "M"
  279. w.writen1(byte(len(s)) + cborBaseString)
  280. w.writestr(s)
  281. addTagFn8To16()
  282. w.writen1(byte(len(v.M)) + cborBaseMap)
  283. addTagFn8To16()
  284. s = "111"
  285. w.writen1(byte(len(s)) + cborBaseString)
  286. w.writestr(s)
  287. w.writen2(cborBaseUint+0x18, uint8(111))
  288. addTagFn8To16()
  289. s = "111.11"
  290. w.writen1(byte(len(s)) + cborBaseString)
  291. w.writestr(s)
  292. w.writen1(cborBdFloat64)
  293. bigen.PutUint64(b8[:8], math.Float64bits(111.11))
  294. w.writeb(b8[:8])
  295. addTagFn8To16()
  296. s = "true"
  297. w.writen1(byte(len(s)) + cborBaseString)
  298. w.writestr(s)
  299. w.writen1(cborBdTrue)
  300. }
  301. var h CborHandle
  302. h.SkipUnexpectedTags = true
  303. h.Canonical = true
  304. var gold []byte
  305. NewEncoderBytes(&gold, &h).MustEncode(v)
  306. // xdebug2f("encoded: gold: %v", gold)
  307. // w.b is the encoded bytes
  308. var v2 Tcbortags
  309. doAddTag = false
  310. fnEncode()
  311. // xdebug2f("manual: no-tags: %v", w.b)
  312. testDeepEqualErr(gold, w.b, t, "cbor-skip-tags--bytes---")
  313. NewDecoderBytes(w.b, &h).MustDecode(&v2)
  314. testDeepEqualErr(v, v2, t, "cbor-skip-tags--no-tags-")
  315. var v3 Tcbortags
  316. doAddTag = true
  317. fnEncode()
  318. // xdebug2f("manual: has-tags: %v", w.b)
  319. NewDecoderBytes(w.b, &h).MustDecode(&v3)
  320. testDeepEqualErr(v, v2, t, "cbor-skip-tags--has-tags")
  321. // Github 300 - tests naked path
  322. {
  323. expected := []interface{}{"x", uint64(0x0)}
  324. toDecode := []byte{0x82, 0x61, 0x78, 0x00}
  325. var raw interface{}
  326. NewDecoderBytes(toDecode, &h).MustDecode(&raw)
  327. testDeepEqualErr(expected, raw, t, "cbor-skip-tags--gh-300---no-skips")
  328. toDecode = []byte{0xd9, 0xd9, 0xf7, 0x82, 0x61, 0x78, 0x00}
  329. raw = nil
  330. NewDecoderBytes(toDecode, &h).MustDecode(&raw)
  331. testDeepEqualErr(expected, raw, t, "cbor-skip-tags--gh-300--has-skips")
  332. }
  333. }