Skip to content

Commit

Permalink
Add not well-formed CBOR data tests from 7049bis
Browse files Browse the repository at this point in the history
Closes: #54
  • Loading branch information
fxamacker committed Dec 3, 2019
1 parent a0a5290 commit ac1c292
Showing 1 changed file with 101 additions and 33 deletions.
134 changes: 101 additions & 33 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,41 +928,98 @@ var invalidCBORUnmarshalTests = []struct {
}{
{"data is nil", []byte(nil), "EOF", false},
{"data is empty", []byte{}, "EOF", false},
{"incomplete header, want 2 bytes", []byte{0x18}, "unexpected EOF", false},
{"incomplete header, want 3 bytes", []byte{0x19, 0x03}, "unexpected EOF", false},
{"incomplete header, want 5 bytes", []byte{0x1a, 0x00, 0x0f, 0x42}, "unexpected EOF", false},
{"incomplete header, want 9 bytes", []byte{0x1b, 0x00, 0x00, 0x00, 0xe8, 0xd4, 0xa5, 0x10}, "unexpected EOF", false},
{"data type and additional information mismatch", []byte{0x1c}, "cbor: invalid additional information", true},
{"data type and additional information mismatch", []byte{0x3f}, "cbor: invalid additional information", true},
{"unexpected \"break\" code", []byte{0xff}, "cbor: unexpected \"break\" code", false},
{"byte string: reach EOF before completing payload", []byte{0x48, 0x00, 0x01, 0x02, 0x03}, "unexpected EOF", false},
{"array: reach EOF before completing payload", []byte{0x88, 0x00, 0x01, 0x02, 0x03}, "unexpected EOF", false},
{"map: reach EOF before completing payload", []byte{0xa8, 0x00, 0x01, 0x02, 0x03}, "unexpected EOF", false},
{"array: invalid element", []byte{0x81, 0x1f}, "cbor: invalid additional information", true},
{"map: invalid element", []byte{0xa1, 0x00, 0x1f}, "cbor: invalid additional information", true},
{"tag: no tagged data item", []byte{0xc0}, "unexpected EOF", false},
{"indefinite-length byte string: element type is not byte string", []byte{0x5f, 0x42, 0x01, 0x02, 0x62, 0x61, 0x62}, "wrong element type", true},
{"indefinite-length array: no \"break\" code", []byte{0x9f, 0x01, 0x02, 0x03, 0x04, 0x05}, "unexpected EOF", false},
{"indefinite-length array: invalid element", []byte{0x9f, 0x1f}, "cbor: invalid additional information", true},
{"indefinite-length array: incomplete element", []byte{0x9f, 0x19, 0x03}, "unexpected EOF", false},
{"indefinite-length map: no \"break\" code", []byte{0xbf, 0x01}, "unexpected EOF", false},
{"indefinite-length map: read \"break\" code before completing key-value pair", []byte{0xbf, 0x01, 0xff}, "cbor: unexpected \"break\" code", false},
{"indefinite-length map: invalid element", []byte{0xbf, 0x01, 0x1f}, "cbor: invalid additional information", true},
{"invalid simple value", []byte{0xf8, 0x18}, "cbor: invalid simple value 24 for type primitives", true},
// Data from 7049bis G.1
{"Definite length maps and arrays not closed with enough items", hexDecode("818181818181818181818200a1a20102a100a2000000"), "unexpected EOF", false},
{"Indefinite length strings not closed by a break stop code", hexDecode("5f41007f6100"), "unexpected EOF", false},
{"Indefinite length maps and arrays not closed by a break stop code", hexDecode("9f9f0102bfbf01020102819f9f80009f9f9f9f9fffffffff9f819f819f9fffffff"), "unexpected EOF", false},
{"Reserved additional information values", hexDecode("1c1d1e3c3d3e5c5d5e7c7d7e9c9d9ebcbdbedcdddefcfdfe"), "cbor: invalid additional information 28 for type positive integer", false},
{"Reserved two-byte encodings of simple types", hexDecode("f800f801f818f81f"), "cbor: invalid simple value 0 for type primitives", false},
{"Indefinite length string chunks not of the correct type", hexDecode("5f00ff5f21ff5f6100ff5f80ff5fa0ff5fc000ff5fe0ff7f4100ff"), "cbor: wrong element type positive integer for indefinite-length byte string", false},
{"Indefinite length string chunks not definite length", hexDecode("5f5f4100ffff7f7f6100ffff"), "cbor: indefinite-length byte string chunk is not definite-length", false},
{"Break occurring on its own outside of an indefinite length item", hexDecode("ff"), "cbor: unexpected \"break\" code", false},
{"Break occurring in a definite length array or map or a tag", hexDecode("81ff8200ffa1ffa1ff00a100ffa20000ff9f81ff9f829f819f9fffffffff"), "cbor: unexpected \"break\" code", false},
{"Break in indefinite length map would lead to odd number of items (break in a value position)", hexDecode("bf00ffbf000000ff"), "cbor: unexpected \"break\" code", false},
{"Major type 0 with additional information 31", hexDecode("1f3fdf"), "cbor: invalid additional information 31 for type positive integer", false},
{"Major type 1 with additional information 31", hexDecode("3f3fdf"), "cbor: invalid additional information 31 for type negative integer", false},
{"Major type 6 with additional information 31", hexDecode("df3fdf"), "cbor: invalid additional information 31 for type tag", false},
// Premature end of the input
{"End of input in a head", hexDecode("18"), "unexpected EOF", false},
{"End of input in a head", hexDecode("19"), "unexpected EOF", false},
{"End of input in a head", hexDecode("1a"), "unexpected EOF", false},
{"End of input in a head", hexDecode("1b"), "unexpected EOF", false},
{"End of input in a head", hexDecode("1901"), "unexpected EOF", false},
{"End of input in a head", hexDecode("1a0102"), "unexpected EOF", false},
{"End of input in a head", hexDecode("1b01020304050607"), "unexpected EOF", false},
{"End of input in a head", hexDecode("38"), "unexpected EOF", false},
{"End of input in a head", hexDecode("58"), "unexpected EOF", false},
{"End of input in a head", hexDecode("78"), "unexpected EOF", false},
{"End of input in a head", hexDecode("98"), "unexpected EOF", false},
{"End of input in a head", hexDecode("9a01ff00"), "unexpected EOF", false},
{"End of input in a head", hexDecode("b8"), "unexpected EOF", false},
{"End of input in a head", hexDecode("d8"), "unexpected EOF", false},
{"End of input in a head", hexDecode("f8"), "unexpected EOF", false},
{"End of input in a head", hexDecode("f900"), "unexpected EOF", false},
{"End of input in a head", hexDecode("fa0000"), "unexpected EOF", false},
{"End of input in a head", hexDecode("fb000000"), "unexpected EOF", false},
{"Definite length strings with short data", hexDecode("41"), "unexpected EOF", false},
{"Definite length strings with short data", hexDecode("61"), "unexpected EOF", false},
{"Definite length strings with short data", hexDecode("5affffffff00"), "unexpected EOF", false},
{"Definite length strings with short data", hexDecode("5bffffffffffffffff010203"), "cbor: byte string length 18446744073709551615 is too large, causing integer overflow", false},
{"Definite length strings with short data", hexDecode("7affffffff00"), "unexpected EOF", false},
{"Definite length strings with short data", hexDecode("7b7fffffffffffffff010203"), "unexpected EOF", false},
{"Definite length maps and arrays not closed with enough items", hexDecode("81"), "unexpected EOF", false},
{"Definite length maps and arrays not closed with enough items", hexDecode("818181818181818181"), "unexpected EOF", false},
{"Definite length maps and arrays not closed with enough items", hexDecode("8200"), "unexpected EOF", false},
{"Definite length maps and arrays not closed with enough items", hexDecode("a1"), "unexpected EOF", false},
{"Definite length maps and arrays not closed with enough items", hexDecode("a20102"), "unexpected EOF", false},
{"Definite length maps and arrays not closed with enough items", hexDecode("a100"), "unexpected EOF", false},
{"Definite length maps and arrays not closed with enough items", hexDecode("a2000000"), "unexpected EOF", false},
{"Indefinite length strings not closed by a break stop code", hexDecode("5f4100"), "unexpected EOF", false},
{"Indefinite length strings not closed by a break stop code", hexDecode("7f6100"), "unexpected EOF", false},
{"Indefinite length maps and arrays not closed by a break stop code", hexDecode("9f"), "unexpected EOF", false},
{"Indefinite length maps and arrays not closed by a break stop code", hexDecode("9f0102"), "unexpected EOF", false},
{"Indefinite length maps and arrays not closed by a break stop code", hexDecode("bf"), "unexpected EOF", false},
{"Indefinite length maps and arrays not closed by a break stop code", hexDecode("bf01020102"), "unexpected EOF", false},
{"Indefinite length maps and arrays not closed by a break stop code", hexDecode("819f"), "unexpected EOF", false},
{"Indefinite length maps and arrays not closed by a break stop code", hexDecode("9f8000"), "unexpected EOF", false},
{"Indefinite length maps and arrays not closed by a break stop code", hexDecode("9f9f9f9f9fffffffff"), "unexpected EOF", false},
{"Indefinite length maps and arrays not closed by a break stop code", hexDecode("9f819f819f9fffffff"), "unexpected EOF", false},
// Five subkinds of well-formedness error kind 3 (syntax error)
{"Reserved additional information values", hexDecode("3e"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("5c"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("5d"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("5e"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("7c"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("7d"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("7e"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("9c"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("9d"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("9e"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("bc"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("bd"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("be"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("dc"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("dd"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("de"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("fc"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("fd"), "cbor: invalid additional information", true},
{"Reserved additional information values", hexDecode("fe"), "cbor: invalid additional information", true},
{"Reserved two-byte encodings of simple types", hexDecode("f800"), "cbor: invalid simple value 0 for type primitives", true},
{"Reserved two-byte encodings of simple types", hexDecode("f801"), "cbor: invalid simple value 1 for type primitives", true},
{"Reserved two-byte encodings of simple types", hexDecode("f818"), "cbor: invalid simple value 24 for type primitives", true},
{"Reserved two-byte encodings of simple types", hexDecode("f81f"), "cbor: invalid simple value 31 for type primitives", true},
{"Indefinite length string chunks not of the correct type", hexDecode("5f00ff"), "cbor: wrong element type positive integer for indefinite-length byte string", false},
{"Indefinite length string chunks not of the correct type", hexDecode("5f21ff"), "cbor: wrong element type negative integer for indefinite-length byte string", false},
{"Indefinite length string chunks not of the correct type", hexDecode("5f6100ff"), "cbor: wrong element type UTF-8 text string for indefinite-length byte string", false},
{"Indefinite length string chunks not of the correct type", hexDecode("5f80ff"), "cbor: wrong element type array for indefinite-length byte string", false},
{"Indefinite length string chunks not of the correct type", hexDecode("5fa0ff"), "cbor: wrong element type map for indefinite-length byte string", false},
{"Indefinite length string chunks not of the correct type", hexDecode("5fc000ff"), "cbor: wrong element type positive integer for indefinite-length byte string", false},
{"Indefinite length string chunks not of the correct type", hexDecode("5fe0ff"), "cbor: wrong element type primitives for indefinite-length byte string", false},
{"Indefinite length string chunks not of the correct type", hexDecode("7f4100ff"), "cbor: wrong element type byte string for indefinite-length UTF-8 text string", false},
{"Indefinite length string chunks not definite length", hexDecode("5f5f4100ffff"), "cbor: indefinite-length byte string chunk is not definite-length", false},
{"Indefinite length string chunks not definite length", hexDecode("7f7f6100ffff"), "cbor: indefinite-length UTF-8 text string chunk is not definite-length", false},
{"Break occurring on its own outside of an indefinite length item", hexDecode("ff"), "cbor: unexpected \"break\" code", true},
{"Break occurring in a definite length array or map or a tag", hexDecode("81ff"), "cbor: unexpected \"break\" code", true},
{"Break occurring in a definite length array or map or a tag", hexDecode("8200ff"), "cbor: unexpected \"break\" code", true},
{"Break occurring in a definite length array or map or a tag", hexDecode("a1ff"), "cbor: unexpected \"break\" code", true},
{"Break occurring in a definite length array or map or a tag", hexDecode("a1ff00"), "cbor: unexpected \"break\" code", true},
{"Break occurring in a definite length array or map or a tag", hexDecode("a100ff"), "cbor: unexpected \"break\" code", true},
{"Break occurring in a definite length array or map or a tag", hexDecode("a20000ff"), "cbor: unexpected \"break\" code", true},
{"Break occurring in a definite length array or map or a tag", hexDecode("9f81ff"), "cbor: unexpected \"break\" code", true},
{"Break occurring in a definite length array or map or a tag", hexDecode("9f829f819f9fffffffff"), "cbor: unexpected \"break\" code", true},
{"Break in indefinite length map would lead to odd number of items (break in a value position)", hexDecode("bf00ff"), "cbor: unexpected \"break\" code", true},
{"Break in indefinite length map would lead to odd number of items (break in a value position)", hexDecode("bf000000ff"), "cbor: unexpected \"break\" code", true},
{"Major type 0 with additional information 31", hexDecode("1f"), "cbor: invalid additional information 31 for type positive integer", true},
{"Major type 1 with additional information 31", hexDecode("3f"), "cbor: invalid additional information 31 for type negative integer", true},
{"Major type 6 with additional information 31", hexDecode("df"), "cbor: invalid additional information 31 for type tag", true},
}

func TestInvalidCBORUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -1000,6 +1057,17 @@ func TestInvalidUTF8TextString(t *testing.T) {
}
}

func TestUnmarshalTaggedString(t *testing.T) {
cborData := hexDecode("5fc64401020304ff")
want := []byte{1, 2, 3, 4}
var v interface{}
if err := cbor.Unmarshal(cborData, &v); err != nil {
t.Errorf("Unmarshal(0x%0x) returns error %q", cborData, err)
} else if !reflect.DeepEqual(v, want) {
t.Errorf("Unmarshal(0x%0x) = %v (%T), want %v (%T)", cborData, v, v, want, want)
}
}

func TestUnmarshalStruct(t *testing.T) {
want := outer{
IntField: 123,
Expand Down

0 comments on commit ac1c292

Please sign in to comment.