| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package codec |
| 5 |
|
| 6 |
import ( |
| 7 |
"bufio" |
| 8 |
"bytes" |
| 9 |
"encoding/hex" |
| 10 |
"math" |
| 11 |
"os" |
| 12 |
"reflect" |
| 13 |
"regexp" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
) |
| 17 |
|
| 18 |
func TestCborIndefiniteLength(t *testing.T) { |
| 19 |
var h Handle = testCborH |
| 20 |
defer testSetup(t, &h)() |
| 21 |
bh := testBasicHandle(h) |
| 22 |
defer func(oldMapType reflect.Type) { |
| 23 |
bh.MapType = oldMapType |
| 24 |
}(bh.MapType) |
| 25 |
bh.MapType = testMapStrIntfTyp |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
var v, vv interface{} |
| 34 |
|
| 35 |
v = map[string]interface{}{ |
| 36 |
"one-byte-key": []byte{1, 2, 3, 4, 5, 6}, |
| 37 |
"two-string-key": "two-value", |
| 38 |
"three-list-key": []interface{}{true, false, uint64(1), int64(-1)}, |
| 39 |
} |
| 40 |
var buf bytes.Buffer |
| 41 |
|
| 42 |
e := NewEncoder(&buf, h) |
| 43 |
buf.WriteByte(cborBdIndefiniteMap) |
| 44 |
|
| 45 |
buf.WriteByte(cborBdIndefiniteString) |
| 46 |
e.MustEncode("one-") |
| 47 |
e.MustEncode("byte-") |
| 48 |
e.MustEncode("key") |
| 49 |
buf.WriteByte(cborBdBreak) |
| 50 |
|
| 51 |
buf.WriteByte(cborBdIndefiniteBytes) |
| 52 |
e.MustEncode([]byte{1, 2, 3}) |
| 53 |
e.MustEncode([]byte{4, 5, 6}) |
| 54 |
buf.WriteByte(cborBdBreak) |
| 55 |
|
| 56 |
|
| 57 |
buf.WriteByte(cborBdIndefiniteString) |
| 58 |
e.MustEncode("two-") |
| 59 |
e.MustEncode("string-") |
| 60 |
e.MustEncode("key") |
| 61 |
buf.WriteByte(cborBdBreak) |
| 62 |
|
| 63 |
buf.WriteByte(cborBdIndefiniteString) |
| 64 |
e.MustEncode([]byte("two-")) |
| 65 |
e.MustEncode([]byte("value")) |
| 66 |
buf.WriteByte(cborBdBreak) |
| 67 |
|
| 68 |
|
| 69 |
buf.WriteByte(cborBdIndefiniteString) |
| 70 |
e.MustEncode("three-") |
| 71 |
e.MustEncode("list-") |
| 72 |
e.MustEncode("key") |
| 73 |
buf.WriteByte(cborBdBreak) |
| 74 |
|
| 75 |
buf.WriteByte(cborBdIndefiniteArray) |
| 76 |
e.MustEncode(true) |
| 77 |
e.MustEncode(false) |
| 78 |
e.MustEncode(uint64(1)) |
| 79 |
e.MustEncode(int64(-1)) |
| 80 |
buf.WriteByte(cborBdBreak) |
| 81 |
|
| 82 |
buf.WriteByte(cborBdBreak) |
| 83 |
|
| 84 |
NewDecoderBytes(buf.Bytes(), h).MustDecode(&vv) |
| 85 |
if err := deepEqual(v, vv); err != nil { |
| 86 |
t.Logf("-------- Before and After marshal do not match: Error: %v", err) |
| 87 |
if testVerbose { |
| 88 |
t.Logf(" ....... GOLDEN: (%T) %#v", v, v) |
| 89 |
t.Logf(" ....... DECODED: (%T) %#v", vv, vv) |
| 90 |
} |
| 91 |
t.FailNow() |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
type testCborGolden struct { |
| 96 |
Base64 string `codec:"cbor"` |
| 97 |
Hex string `codec:"hex"` |
| 98 |
Roundtrip bool `codec:"roundtrip"` |
| 99 |
Decoded interface{} `codec:"decoded"` |
| 100 |
Diagnostic string `codec:"diagnostic"` |
| 101 |
Skip bool `codec:"skip"` |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func TestCborGoldens(t *testing.T) { |
| 106 |
var h Handle = testCborH |
| 107 |
defer testSetup(t, &h)() |
| 108 |
bh := testBasicHandle(h) |
| 109 |
defer func(oldMapType reflect.Type) { |
| 110 |
bh.MapType = oldMapType |
| 111 |
}(bh.MapType) |
| 112 |
bh.MapType = testMapStrIntfTyp |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
var gs []*testCborGolden |
| 121 |
f, err := os.Open("test-cbor-goldens.json") |
| 122 |
if err != nil { |
| 123 |
t.Logf("error opening test-cbor-goldens.json: %v", err) |
| 124 |
t.FailNow() |
| 125 |
} |
| 126 |
defer f.Close() |
| 127 |
jh := new(JsonHandle) |
| 128 |
jh.MapType = testMapStrIntfTyp |
| 129 |
|
| 130 |
d := NewDecoder(bufio.NewReader(f), jh) |
| 131 |
|
| 132 |
d.MustDecode(&gs) |
| 133 |
if err != nil { |
| 134 |
t.Logf("error json decoding test-cbor-goldens.json: %v", err) |
| 135 |
t.FailNow() |
| 136 |
} |
| 137 |
|
| 138 |
tagregex := regexp.MustCompile(`[\d]+\(.+?\)`) |
| 139 |
hexregex := regexp.MustCompile(`h'([0-9a-fA-F]*)'`) |
| 140 |
for i, g := range gs { |
| 141 |
|
| 142 |
|
| 143 |
if g.Skip || strings.HasPrefix(g.Diagnostic, "simple(") || tagregex.MatchString(g.Diagnostic) { |
| 144 |
|
| 145 |
if testVerbose { |
| 146 |
t.Logf("[%v] skipping because skip=true OR unsupported simple value or Tag Value", i) |
| 147 |
} |
| 148 |
continue |
| 149 |
} |
| 150 |
|
| 151 |
if hexregex.MatchString(g.Diagnostic) { |
| 152 |
|
| 153 |
if s2 := g.Diagnostic[2 : len(g.Diagnostic)-1]; s2 == "" { |
| 154 |
g.Decoded = zeroByteSlice |
| 155 |
} else if bs2, err2 := hex.DecodeString(s2); err2 == nil { |
| 156 |
g.Decoded = bs2 |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
bs, err := hex.DecodeString(g.Hex) |
| 161 |
if err != nil { |
| 162 |
t.Logf("[%v] error hex decoding %s [%v]: %v", i, g.Hex, g.Hex, err) |
| 163 |
t.FailNow() |
| 164 |
} |
| 165 |
var v interface{} |
| 166 |
NewDecoderBytes(bs, h).MustDecode(&v) |
| 167 |
if _, ok := v.(RawExt); ok { |
| 168 |
continue |
| 169 |
} |
| 170 |
|
| 171 |
switch g.Diagnostic { |
| 172 |
case "Infinity": |
| 173 |
b := math.IsInf(v.(float64), 1) |
| 174 |
testCborError(t, i, math.Inf(1), v, nil, &b) |
| 175 |
case "-Infinity": |
| 176 |
b := math.IsInf(v.(float64), -1) |
| 177 |
testCborError(t, i, math.Inf(-1), v, nil, &b) |
| 178 |
case "NaN": |
| 179 |
|
| 180 |
b := math.IsNaN(v.(float64)) |
| 181 |
testCborError(t, i, math.NaN(), v, nil, &b) |
| 182 |
case "undefined": |
| 183 |
b := v == nil |
| 184 |
testCborError(t, i, nil, v, nil, &b) |
| 185 |
default: |
| 186 |
v0 := g.Decoded |
| 187 |
|
| 188 |
testCborError(t, i, v0, v, deepEqual(v0, v), nil) |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func testCborError(t *testing.T, i int, v0, v1 interface{}, err error, equal *bool) { |
| 194 |
if err == nil && equal == nil { |
| 195 |
|
| 196 |
return |
| 197 |
} |
| 198 |
if err != nil { |
| 199 |
t.Logf("[%v] deepEqual error: %v", i, err) |
| 200 |
if testVerbose { |
| 201 |
t.Logf(" ....... GOLDEN: (%T) %#v", v0, v0) |
| 202 |
t.Logf(" ....... DECODED: (%T) %#v", v1, v1) |
| 203 |
} |
| 204 |
t.FailNow() |
| 205 |
} |
| 206 |
if equal != nil && !*equal { |
| 207 |
t.Logf("[%v] values not equal", i) |
| 208 |
if testVerbose { |
| 209 |
t.Logf(" ....... GOLDEN: (%T) %#v", v0, v0) |
| 210 |
t.Logf(" ....... DECODED: (%T) %#v", v1, v1) |
| 211 |
} |
| 212 |
t.FailNow() |
| 213 |
} |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
func TestCborHalfFloat(t *testing.T) { |
| 218 |
var h Handle = testCborH |
| 219 |
defer testSetup(t, &h)() |
| 220 |
m := map[uint16]float64{ |
| 221 |
|
| 222 |
|
| 223 |
0x3c00: 1, |
| 224 |
0x3c01: 1 + math.Pow(2, -10), |
| 225 |
0xc000: -2, |
| 226 |
0x7bff: 65504, |
| 227 |
0x0400: math.Pow(2, -14), |
| 228 |
0x03ff: math.Pow(2, -14) - math.Pow(2, -24), |
| 229 |
0x0001: math.Pow(2, -24), |
| 230 |
0x0000: 0, |
| 231 |
0x8000: -0.0, |
| 232 |
} |
| 233 |
var ba [3]byte |
| 234 |
ba[0] = cborBdFloat16 |
| 235 |
var res float64 |
| 236 |
for k, v := range m { |
| 237 |
res = 0 |
| 238 |
bigenstd.PutUint16(ba[1:], k) |
| 239 |
testUnmarshalErr(&res, ba[:3], h, t, "-") |
| 240 |
if res == v { |
| 241 |
if testVerbose { |
| 242 |
t.Logf("equal floats: from %x %b, %v", k, k, v) |
| 243 |
} |
| 244 |
} else { |
| 245 |
t.Logf("unequal floats: from %x %b, %v != %v", k, k, res, v) |
| 246 |
t.FailNow() |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
func TestCborSkipTags(t *testing.T) { |
| 252 |
defer testSetup(t, nil)() |
| 253 |
type Tcbortags struct { |
| 254 |
A string |
| 255 |
M map[string]interface{} |
| 256 |
|
| 257 |
} |
| 258 |
var b8 [8]byte |
| 259 |
var w bytesEncAppender |
| 260 |
w.b = []byte{} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
var tags = [...]uint64{math.MaxUint8 * 2, math.MaxUint8 * 8, 55799, math.MaxUint16 / 2} |
| 269 |
var tagIdx int |
| 270 |
var doAddTag bool |
| 271 |
addTagFn8To16 := func() { |
| 272 |
if !doAddTag { |
| 273 |
return |
| 274 |
} |
| 275 |
|
| 276 |
w.writen1(cborBaseTag + 0x19) |
| 277 |
|
| 278 |
bigenstd.PutUint16(b8[:2], uint16(tags[tagIdx%len(tags)])) |
| 279 |
w.writeb(b8[:2]) |
| 280 |
tagIdx++ |
| 281 |
} |
| 282 |
|
| 283 |
var v Tcbortags |
| 284 |
v.A = "cbor" |
| 285 |
v.M = make(map[string]interface{}) |
| 286 |
v.M["111"] = uint64(111) |
| 287 |
v.M["111.11"] = 111.11 |
| 288 |
v.M["true"] = true |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
fnEncode := func() { |
| 296 |
w.b = w.b[:0] |
| 297 |
addTagFn8To16() |
| 298 |
|
| 299 |
w.writen1(2 + cborBaseMap) |
| 300 |
|
| 301 |
var s = "A" |
| 302 |
w.writen1(byte(len(s)) + cborBaseString) |
| 303 |
w.writestr(s) |
| 304 |
w.writen1(byte(len(v.A)) + cborBaseString) |
| 305 |
w.writestr(v.A) |
| 306 |
|
| 307 |
|
| 308 |
addTagFn8To16() |
| 309 |
s = "M" |
| 310 |
w.writen1(byte(len(s)) + cborBaseString) |
| 311 |
w.writestr(s) |
| 312 |
|
| 313 |
addTagFn8To16() |
| 314 |
w.writen1(byte(len(v.M)) + cborBaseMap) |
| 315 |
|
| 316 |
addTagFn8To16() |
| 317 |
s = "111" |
| 318 |
w.writen1(byte(len(s)) + cborBaseString) |
| 319 |
w.writestr(s) |
| 320 |
w.writen2(cborBaseUint+0x18, uint8(111)) |
| 321 |
|
| 322 |
addTagFn8To16() |
| 323 |
s = "111.11" |
| 324 |
w.writen1(byte(len(s)) + cborBaseString) |
| 325 |
w.writestr(s) |
| 326 |
w.writen1(cborBdFloat64) |
| 327 |
bigenstd.PutUint64(b8[:8], math.Float64bits(111.11)) |
| 328 |
w.writeb(b8[:8]) |
| 329 |
|
| 330 |
addTagFn8To16() |
| 331 |
s = "true" |
| 332 |
w.writen1(byte(len(s)) + cborBaseString) |
| 333 |
w.writestr(s) |
| 334 |
w.writen1(cborBdTrue) |
| 335 |
} |
| 336 |
|
| 337 |
var h CborHandle |
| 338 |
h.SkipUnexpectedTags = true |
| 339 |
h.Canonical = true |
| 340 |
|
| 341 |
var gold []byte |
| 342 |
NewEncoderBytes(&gold, &h).MustEncode(v) |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
var v2 Tcbortags |
| 347 |
doAddTag = false |
| 348 |
fnEncode() |
| 349 |
|
| 350 |
|
| 351 |
testDeepEqualErr(gold, w.b, t, "cbor-skip-tags--bytes---") |
| 352 |
NewDecoderBytes(w.b, &h).MustDecode(&v2) |
| 353 |
testDeepEqualErr(v, v2, t, "cbor-skip-tags--no-tags-") |
| 354 |
|
| 355 |
var v3 Tcbortags |
| 356 |
doAddTag = true |
| 357 |
fnEncode() |
| 358 |
|
| 359 |
NewDecoderBytes(w.b, &h).MustDecode(&v3) |
| 360 |
testDeepEqualErr(v, v2, t, "cbor-skip-tags--has-tags") |
| 361 |
|
| 362 |
|
| 363 |
{ |
| 364 |
expected := []interface{}{"x", uint64(0x0)} |
| 365 |
toDecode := []byte{0x82, 0x61, 0x78, 0x00} |
| 366 |
|
| 367 |
var raw interface{} |
| 368 |
|
| 369 |
NewDecoderBytes(toDecode, &h).MustDecode(&raw) |
| 370 |
testDeepEqualErr(expected, raw, t, "cbor-skip-tags--gh-300---no-skips") |
| 371 |
|
| 372 |
toDecode = []byte{0xd9, 0xd9, 0xf7, 0x82, 0x61, 0x78, 0x00} |
| 373 |
raw = nil |
| 374 |
NewDecoderBytes(toDecode, &h).MustDecode(&raw) |
| 375 |
testDeepEqualErr(expected, raw, t, "cbor-skip-tags--gh-300--has-skips") |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
func TestCborMalformed(t *testing.T) { |
| 380 |
if !testRecoverPanicToErr { |
| 381 |
t.Skip(testSkipIfNotRecoverPanicToErrMsg) |
| 382 |
} |
| 383 |
var h Handle = testCborH |
| 384 |
defer testSetup(t, &h)() |
| 385 |
var bad = [][]byte{ |
| 386 |
[]byte("\x9b\x00\x00000000"), |
| 387 |
[]byte("\x9b\x00\x00\x81112233"), |
| 388 |
} |
| 389 |
|
| 390 |
var out interface{} |
| 391 |
for _, v := range bad { |
| 392 |
out = nil |
| 393 |
err := testUnmarshal(&out, v, h) |
| 394 |
if err == nil { |
| 395 |
t.Logf("missing expected error decoding malformed cbor") |
| 396 |
t.FailNow() |
| 397 |
} |
| 398 |
} |
| 399 |
} |