From dcac3bc18e2acbdf617f3debab234bae857fe5b8 Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Tue, 28 Nov 2023 10:29:30 +0530 Subject: [PATCH] Adding test case for nested json with depth of 999, 1000, 1001 --- .../java/org/json/junit/JSONArrayTest.java | 52 +++++++++++++++++++ .../java/org/json/junit/JSONObjectTest.java | 37 +++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 44a1d7b..5f50fc7 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -1438,4 +1438,56 @@ public class JSONArrayTest { array.add(array); new JSONArray(array); } + + @Test + public void testRecursiveDepthAtPosition999Object() { + HashMap map = JSONObjectTest.buildNestedMap(999); + new JSONArray().put(0, map); + } + + @Test + public void testRecursiveDepthAtPosition1000Object() { + HashMap map = JSONObjectTest.buildNestedMap(1000); + new JSONArray().put(0, map); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthAtPosition1001Object() { + HashMap map = JSONObjectTest.buildNestedMap(1001); + new JSONArray().put(0, map); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthArrayLimitedMaps() { + ArrayList array = new ArrayList<>(); + array.add(array); + new JSONArray(array); + } + + @Test + public void testRecursiveDepthArrayFor999Levels() { + ArrayList array = buildNestedArray(999); + new JSONArray(array); + } + + @Test + public void testRecursiveDepthArrayFor1000Levels() { + ArrayList array = buildNestedArray(1000); + new JSONArray(array); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthArrayFor1001Levels() { + ArrayList array = buildNestedArray(1001); + new JSONArray(array); + } + + public static ArrayList buildNestedArray(int maxDepth) { + if (maxDepth <= 0) { + return new ArrayList<>(); + } + ArrayList nestedArray = new ArrayList<>(); + nestedArray.add(buildNestedArray(maxDepth - 1)); + return nestedArray; + } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index dff4503..e157fd5 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3735,4 +3735,41 @@ public class JSONObjectTest { jsonObject.put("test", inside); new JSONObject(jsonObject); } + + @Test + public void issue743SerializationMapWith999Objects() { + HashMap map = buildNestedMap(999); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + @Test + public void issue743SerializationMapWith1000Objects() { + HashMap map = buildNestedMap(1000); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + @Test(expected = JSONException.class) + public void issue743SerializationMapWith1001Objects() { + HashMap map = buildNestedMap(1001); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + /** + * Method to build nested map of max maxDepth + * + * @param maxDepth + * @return + */ + public static HashMap buildNestedMap(int maxDepth) { + if (maxDepth <= 0) { + return new HashMap<>(); + } + HashMap nestedMap = new HashMap<>(); + nestedMap.put("t", buildNestedMap(maxDepth - 1)); + return nestedMap; + } + }