From 86bb0a1a02d49d2d1799153a45f7b8bcf2ed7015 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 30 Dec 2023 17:00:02 -0600 Subject: [PATCH] cleanup-and-merge-tests: pull in unit tests from #809 --- .../java/org/json/junit/JSONObjectTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 96f3673..053f17a 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3760,6 +3760,48 @@ public class JSONObjectTest { String jsonString = object.toString(); } + @Test(expected = JSONException.class) + public void testCircleReferenceFirstLevel() { + Map jsonObject = new HashMap<>(); + + jsonObject.put("test", jsonObject); + + new JSONObject(jsonObject, new JSONParserConfiguration()); + } + + @Test(expected = StackOverflowError.class) + public void testCircleReferenceMultiplyLevel_notConfigured_expectedStackOverflow() { + Map inside = new HashMap<>(); + + Map jsonObject = new HashMap<>(); + inside.put("test", jsonObject); + jsonObject.put("test", inside); + + new JSONObject(jsonObject, new JSONParserConfiguration().withMaxNestingDepth(99999)); + } + + @Test(expected = JSONException.class) + public void testCircleReferenceMultiplyLevel_configured_expectedJSONException() { + Map inside = new HashMap<>(); + + Map jsonObject = new HashMap<>(); + inside.put("test", jsonObject); + jsonObject.put("test", inside); + + new JSONObject(jsonObject, new JSONParserConfiguration()); + } + + @Test + public void testDifferentKeySameInstanceNotACircleReference() { + Map map1 = new HashMap<>(); + Map map2 = new HashMap<>(); + + map1.put("test1", map2); + map1.put("test2", map2); + + new JSONObject(map1); + } + /** * Method to build nested map of max maxDepth *