github.com/patricebensoussan/go/codec@v1.2.99/cbor_test.go (about)

     1  // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
     2  // Use of this source code is governed by a MIT license found in the LICENSE file.
     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  	// var (
    27  	// 	M1 map[string][]byte
    28  	// 	M2 map[uint64]bool
    29  	// 	L1 []interface{}
    30  	// 	S1 []string
    31  	// 	B1 []byte
    32  	// )
    33  	var v, vv interface{}
    34  	// define it (v), encode it using indefinite lengths, decode it (vv), compare v to vv
    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  	// buf.Reset()
    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-")) // encode as bytes, to check robustness of code
    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) // close map
    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  // Some tests are skipped because they include numbers outside the range of int64/uint64
   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  	// decode test-cbor-goldens.json into a list of []*testCborGolden
   115  	// for each one,
   116  	// - decode hex into []byte bs
   117  	// - decode bs into interface{} v
   118  	// - compare both using deepequal
   119  	// - for any miss, record it
   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  	// d := NewDecoder(f, jh)
   130  	d := NewDecoder(bufio.NewReader(f), jh)
   131  	// err = d.Decode(&gs)
   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  		// fmt.Printf("%v, skip: %v, isTag: %v, %s\n", i, g.Skip, tagregex.MatchString(g.Diagnostic), g.Diagnostic)
   142  		// skip tags or simple or those with prefix, as we can't verify them.
   143  		if g.Skip || strings.HasPrefix(g.Diagnostic, "simple(") || tagregex.MatchString(g.Diagnostic) {
   144  			// fmt.Printf("%v: skipped\n", i)
   145  			if testVerbose {
   146  				t.Logf("[%v] skipping because skip=true OR unsupported simple value or Tag Value", i)
   147  			}
   148  			continue
   149  		}
   150  		// println("++++++++++++", i, "g.Diagnostic", g.Diagnostic)
   151  		if hexregex.MatchString(g.Diagnostic) {
   152  			// println(i, "g.Diagnostic matched hex")
   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  			// fmt.Printf("%v: hex: %v\n", i, g.Decoded)
   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  		// check the diagnostics to compare
   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  			// println(i, "checking NaN")
   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  			// testCborCoerceJsonNumber(reflect.ValueOf(&v0))
   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  		// fmt.Printf("%v testCborError passed (err and equal nil)\n", i)
   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  	// fmt.Printf("%v testCborError passed (checks passed)\n", i)
   215  }
   216  
   217  func TestCborHalfFloat(t *testing.T) {
   218  	var h Handle = testCborH
   219  	defer testSetup(t, &h)()
   220  	m := map[uint16]float64{
   221  		// using examples from
   222  		// https://en.wikipedia.org/wiki/Half-precision_floating-point_format
   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  		// A []interface{}
   257  	}
   258  	var b8 [8]byte
   259  	var w bytesEncAppender
   260  	w.b = []byte{}
   261  
   262  	// To make it easier,
   263  	//    - use tags between math.MaxUint8 and math.MaxUint16 (incl SelfDesc)
   264  	//    - use 1 char strings for key names
   265  	//    - use 3-6 char strings for map keys
   266  	//    - use integers that fit in 2 bytes (between 0x20 and 0xff)
   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  		// writes a tag between MaxUint8 and MaxUint16 (culled from cborEncDriver.encUint)
   276  		w.writen1(cborBaseTag + 0x19)
   277  		// bigenHelper.writeUint16
   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  	// v.A = append(v.A, 222, 22.22, "true")
   290  
   291  	// make stream manually (interspacing tags around it)
   292  	// WriteMapStart - e.encLen(cborBaseMap, length) - encUint(length, bd)
   293  	// EncodeStringEnc - e.encStringBytesS(cborBaseString, v)
   294  
   295  	fnEncode := func() {
   296  		w.b = w.b[:0]
   297  		addTagFn8To16()
   298  		// write v (Tcbortags, with 3 fields = map with 3 entries)
   299  		w.writen1(2 + cborBaseMap) // 3 fields = 3 entries
   300  		// write v.A
   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  		//w.writen1(0)
   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  	// xdebug2f("encoded:    gold: %v", gold)
   344  
   345  	// w.b is the encoded bytes
   346  	var v2 Tcbortags
   347  	doAddTag = false
   348  	fnEncode()
   349  	// xdebug2f("manual:  no-tags: %v", w.b)
   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  	// xdebug2f("manual: has-tags: %v", w.b)
   359  	NewDecoderBytes(w.b, &h).MustDecode(&v3)
   360  	testDeepEqualErr(v, v2, t, "cbor-skip-tags--has-tags")
   361  
   362  	// Github 300 - tests naked path
   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  }