ソースを参照

codec: add tests for MaxDepth

Ugorji Nwoke 7 年 前
コミット
8333dd4495
2 ファイル変更101 行追加0 行削除
  1. 91 0
      codec/codec_test.go
  2. 10 0
      codec/z_all_test.go

+ 91 - 0
codec/codec_test.go

@@ -2331,6 +2331,77 @@ func doTestMissingFields(t *testing.T, name string, h Handle) {
 	testDeepEqualErr(v1, v3, t, name+"-missing-cmp-2")
 }
 
+func doTestMaxDepth(t *testing.T, name string, h Handle) {
+	testOnce.Do(testInitAll)
+	type T struct {
+		I interface{} // value to encode
+		M int16       // maxdepth
+		E interface{} // error to find
+	}
+	var table []T
+	var sfunc = func(n int) (s [1]interface{}, s1 *[1]interface{}) {
+		s1 = &s
+		for i := 0; i < n; i++ {
+			var s0 [1]interface{}
+			s1[0] = &s0
+			s1 = &s0
+		}
+		// xdebugf("sfunc s: %v", s)
+		return
+		// var s []interface{}
+		// s = append(s, []interface{})
+		// s[0] = append(s[0], []interface{})
+		// s[0][0] = append(s[0][0], []interface{})
+		// s[0][0][0] = append(s[0][0][0], []interface{})
+		// s[0][0][0][0] = append(s[0][0][0][0], []interface{})
+		// return s
+	}
+	var mfunc = func(n int) (m map[string]interface{}, mlast map[string]interface{}) {
+		m = make(map[string]interface{})
+		mlast = make(map[string]interface{})
+		m["0"] = mlast
+		for i := 1; i < n; i++ {
+			m0 := make(map[string]interface{})
+			mlast[strconv.FormatInt(int64(i), 10)] = m0
+			mlast = m0
+		}
+		// xdebugf("mfunc m: %v", m)
+		return
+	}
+	s, s1 := sfunc(5)
+	m, _ := mfunc(5)
+	s1[0] = m
+
+	table = append(table, T{s, 0, nil})
+	table = append(table, T{s, 256, nil})
+	table = append(table, T{s, 7, errMaxDepthExceeded})
+	table = append(table, T{s, 15, nil})
+
+	defer func(n int16, b bool) {
+		h.getBasicHandle().MaxDepth = n
+		testUseMust = b
+	}(h.getBasicHandle().MaxDepth, testUseMust)
+
+	testUseMust = false
+	for i, v := range table {
+		h.getBasicHandle().MaxDepth = v.M
+		b1 := testMarshalErr(v.I, h, t, name+"-maxdepth-enc"+strconv.FormatInt(int64(i), 10))
+		// xdebugf("b1: %s", b1)
+		var v2 interface{}
+		err := testUnmarshal(&v2, b1, h)
+		if err1, ok := err.(decodeError); ok {
+			err = err1.codecError
+		}
+		var err0 interface{} = err
+		if err1, ok := err.(codecError); ok {
+			err0 = err1.err
+		}
+		if err0 != v.E {
+			failT(t, "Unexpected error testing max depth for depth %d: expected %v, received %v", v.M, v.E, err)
+		}
+	}
+}
+
 // -----------------
 
 func TestJsonDecodeNonStringScalarInStringContext(t *testing.T) {
@@ -3028,6 +3099,26 @@ func TestSimpleMissingFields(t *testing.T) {
 	doTestMissingFields(t, "simple", testSimpleH)
 }
 
+func TestJsonMaxDepth(t *testing.T) {
+	doTestMaxDepth(t, "json", testJsonH)
+}
+
+func TestCborMaxDepth(t *testing.T) {
+	doTestMaxDepth(t, "cbor", testCborH)
+}
+
+func TestMsgpackMaxDepth(t *testing.T) {
+	doTestMaxDepth(t, "msgpack", testMsgpackH)
+}
+
+func TestBincMaxDepth(t *testing.T) {
+	doTestMaxDepth(t, "binc", testBincH)
+}
+
+func TestSimpleMaxDepth(t *testing.T) {
+	doTestMaxDepth(t, "simple", testSimpleH)
+}
+
 // TODO:
 //
 // Add Tests for:

+ 10 - 0
codec/z_all_test.go

@@ -244,6 +244,11 @@ func testCodecGroup(t *testing.T) {
 	t.Run("TestMsgpackMissingFields", TestMsgpackMissingFields)
 	t.Run("TestBincMissingFields", TestBincMissingFields)
 	t.Run("TestSimpleMissingFields", TestSimpleMissingFields)
+	t.Run("TestJsonMaxDepth", TestJsonMaxDepth)
+	t.Run("TestCborMaxDepth", TestCborMaxDepth)
+	t.Run("TestMsgpackMaxDepth", TestMsgpackMaxDepth)
+	t.Run("TestBincMaxDepth", TestBincMaxDepth)
+	t.Run("TestSimpleMaxDepth", TestSimpleMaxDepth)
 
 	t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
 	t.Run("TestCborHalfFloat", TestCborHalfFloat)
@@ -278,6 +283,7 @@ func testJsonGroup(t *testing.T) {
 	t.Run("TestJsonOmitempty", TestJsonOmitempty)
 	t.Run("TestJsonIntfMapping", TestJsonIntfMapping)
 	t.Run("TestJsonMissingFields", TestJsonMissingFields)
+	t.Run("TestJsonMaxDepth", TestJsonMaxDepth)
 }
 
 func testBincGroup(t *testing.T) {
@@ -304,6 +310,7 @@ func testBincGroup(t *testing.T) {
 	t.Run("TestBincOmitempty", TestBincOmitempty)
 	t.Run("TestBincIntfMapping", TestBincIntfMapping)
 	t.Run("TestBincMissingFields", TestBincMissingFields)
+	t.Run("TestBincMaxDepth", TestBincMaxDepth)
 }
 
 func testCborGroup(t *testing.T) {
@@ -331,6 +338,7 @@ func testCborGroup(t *testing.T) {
 	t.Run("TestCborOmitempty", TestCborOmitempty)
 	t.Run("TestCborIntfMapping", TestCborIntfMapping)
 	t.Run("TestCborMissingFields", TestCborMissingFields)
+	t.Run("TestCborMaxDepth", TestCborMaxDepth)
 	t.Run("TestCborHalfFloat", TestCborHalfFloat)
 }
 
@@ -357,6 +365,7 @@ func testMsgpackGroup(t *testing.T) {
 	t.Run("TestMsgpackOmitempty", TestMsgpackOmitempty)
 	t.Run("TestMsgpackIntfMapping", TestMsgpackIntfMapping)
 	t.Run("TestMsgpackMissingFields", TestMsgpackMissingFields)
+	t.Run("TestMsgpackMaxDepth", TestMsgpackMaxDepth)
 }
 
 func testSimpleGroup(t *testing.T) {
@@ -381,6 +390,7 @@ func testSimpleGroup(t *testing.T) {
 	t.Run("TestSimpleOmitempty", TestSimpleOmitempty)
 	t.Run("TestSimpleIntfMapping", TestSimpleIntfMapping)
 	t.Run("TestSimpleMissingFields", TestSimpleMissingFields)
+	t.Run("TestSimpleMaxDepth", TestSimpleMaxDepth)
 }
 
 func testSimpleMammothGroup(t *testing.T) {