Browse Source

codec: add tests for size mismatch for maps and extensions (using msgpack codec)

Ugorji Nwoke 7 years ago
parent
commit
54e65a16b7
2 changed files with 26 additions and 1 deletions
  1. 20 0
      codec/codec_test.go
  2. 6 1
      codec/z_all_test.go

+ 20 - 0
codec/codec_test.go

@@ -2659,6 +2659,26 @@ func TestJsonInvalidUnicode(t *testing.T) {
 	}
 }
 
+func TestMsgpackDecodeMapAndExtSizeMismatch(t *testing.T) {
+	fn := func(t *testing.T, b []byte, v interface{}) {
+		if err := NewDecoderBytes(b, testMsgpackH).Decode(v); err != io.EOF && err != io.ErrUnexpectedEOF {
+			t.Fatalf("expected EOF or ErrUnexpectedEOF, got %v", err)
+		}
+	}
+
+	// a map claiming to have 0x10eeeeee KV pairs, but only has 1.
+	var b = []byte{0xdf, 0x10, 0xee, 0xee, 0xee, 0x1, 0xa1, 0x1}
+	var m1 map[int]string
+	var m2 map[int][]byte
+	fn(t, b, &m1)
+	fn(t, b, &m2)
+
+	// an extension claiming to have 0x7fffffff bytes, but only has 1.
+	b = []byte{0xc9, 0x7f, 0xff, 0xff, 0xff, 0xda, 0x1}
+	var a interface{}
+	fn(t, b, &a)
+}
+
 // ----------
 
 func TestBincCodecsTable(t *testing.T) {

+ 6 - 1
codec/z_all_test.go

@@ -252,6 +252,7 @@ func testCodecGroup(t *testing.T) {
 
 	t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
 	t.Run("TestCborHalfFloat", TestCborHalfFloat)
+	t.Run("TestMsgpackDecodeMapAndExtSizeMismatch", TestMsgpackDecodeMapAndExtSizeMismatch)
 	// <tear-down code>
 }
 
@@ -275,7 +276,6 @@ func testJsonGroup(t *testing.T) {
 	t.Run("TestJsonEmbeddedFieldPrecedence", TestJsonEmbeddedFieldPrecedence)
 	t.Run("TestJsonLargeContainerLen", TestJsonLargeContainerLen)
 	t.Run("TestJsonMammothMapsAndSlices", TestJsonMammothMapsAndSlices)
-	t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
 	t.Run("TestJsonTime", TestJsonTime)
 	t.Run("TestJsonUintToInt", TestJsonUintToInt)
 	t.Run("TestJsonDifferentMapOrSliceType", TestJsonDifferentMapOrSliceType)
@@ -284,6 +284,8 @@ func testJsonGroup(t *testing.T) {
 	t.Run("TestJsonIntfMapping", TestJsonIntfMapping)
 	t.Run("TestJsonMissingFields", TestJsonMissingFields)
 	t.Run("TestJsonMaxDepth", TestJsonMaxDepth)
+
+	t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
 }
 
 func testBincGroup(t *testing.T) {
@@ -339,6 +341,7 @@ func testCborGroup(t *testing.T) {
 	t.Run("TestCborIntfMapping", TestCborIntfMapping)
 	t.Run("TestCborMissingFields", TestCborMissingFields)
 	t.Run("TestCborMaxDepth", TestCborMaxDepth)
+
 	t.Run("TestCborHalfFloat", TestCborHalfFloat)
 }
 
@@ -366,6 +369,8 @@ func testMsgpackGroup(t *testing.T) {
 	t.Run("TestMsgpackIntfMapping", TestMsgpackIntfMapping)
 	t.Run("TestMsgpackMissingFields", TestMsgpackMissingFields)
 	t.Run("TestMsgpackMaxDepth", TestMsgpackMaxDepth)
+
+	t.Run("TestMsgpackDecodeMapAndExtSizeMismatch", TestMsgpackDecodeMapAndExtSizeMismatch)
 }
 
 func testSimpleGroup(t *testing.T) {