Merge branch 'stleary:master' into feature-disable-whitespace-trim

This commit is contained in:
Keaton
2024-01-18 09:17:26 +02:00
committed by GitHub
15 changed files with 393 additions and 30 deletions

View File

@@ -28,9 +28,11 @@ import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONParserConfiguration;
import org.json.JSONPointerException;
import org.json.JSONString;
import org.json.JSONTokener;
import org.json.ParserConfiguration;
import org.json.junit.data.MyJsonString;
import org.junit.Ignore;
import org.junit.Test;
@@ -1417,4 +1419,78 @@ public class JSONArrayTest {
.put(2);
assertFalse(ja1.similar(ja3));
}
@Test(expected = JSONException.class)
public void testRecursiveDepth() {
HashMap<String, Object> map = new HashMap<>();
map.put("t", map);
new JSONArray().put(map);
}
@Test(expected = JSONException.class)
public void testRecursiveDepthAtPosition() {
HashMap<String, Object> map = new HashMap<>();
map.put("t", map);
new JSONArray().put(0, map);
}
@Test(expected = JSONException.class)
public void testRecursiveDepthArray() {
ArrayList<Object> array = new ArrayList<>();
array.add(array);
new JSONArray(array);
}
@Test
public void testRecursiveDepthAtPositionDefaultObject() {
HashMap<String, Object> map = JSONObjectTest.buildNestedMap(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);
new JSONArray().put(0, map);
}
@Test
public void testRecursiveDepthAtPosition1000Object() {
HashMap<String, Object> map = JSONObjectTest.buildNestedMap(1000);
new JSONArray().put(0, map, new JSONParserConfiguration().withMaxNestingDepth(1000));
}
@Test(expected = JSONException.class)
public void testRecursiveDepthAtPosition1001Object() {
HashMap<String, Object> map = JSONObjectTest.buildNestedMap(1001);
new JSONArray().put(0, map);
}
@Test(expected = JSONException.class)
public void testRecursiveDepthArrayLimitedMaps() {
ArrayList<Object> array = new ArrayList<>();
array.add(array);
new JSONArray(array);
}
@Test
public void testRecursiveDepthArrayForDefaultLevels() {
ArrayList<Object> array = buildNestedArray(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);
new JSONArray(array, new JSONParserConfiguration());
}
@Test
public void testRecursiveDepthArrayFor1000Levels() {
ArrayList<Object> array = buildNestedArray(1000);
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000);
new JSONArray(array, parserConfiguration);
}
@Test(expected = JSONException.class)
public void testRecursiveDepthArrayFor1001Levels() {
ArrayList<Object> array = buildNestedArray(1001);
new JSONArray(array);
}
public static ArrayList<Object> buildNestedArray(int maxDepth) {
if (maxDepth <= 0) {
return new ArrayList<>();
}
ArrayList<Object> nestedArray = new ArrayList<>();
nestedArray.add(buildNestedArray(maxDepth - 1));
return nestedArray;
}
}

View File

@@ -32,8 +32,10 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONPointerException;
import org.json.JSONParserConfiguration;
import org.json.JSONString;
import org.json.JSONTokener;
import org.json.ParserConfiguration;
import org.json.XML;
import org.json.junit.data.BrokenToString;
import org.json.junit.data.ExceptionalBean;
@@ -3718,4 +3720,101 @@ public class JSONObjectTest {
assertThrows(JSONException.class, () -> new JSONObject(bean));
}
}
@Test(expected = JSONException.class)
public void issue743SerializationMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("t", map);
JSONObject object = new JSONObject(map);
String jsonString = object.toString();
}
@Test(expected = JSONException.class)
public void testCircularReferenceMultipleLevel() {
HashMap<String, Object> inside = new HashMap<>();
HashMap<String, Object> jsonObject = new HashMap<>();
inside.put("inside", jsonObject);
jsonObject.put("test", inside);
new JSONObject(jsonObject);
}
@Test
public void issue743SerializationMapWith512Objects() {
HashMap<String, Object> map = buildNestedMap(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);
JSONObject object = new JSONObject(map);
String jsonString = object.toString();
}
@Test
public void issue743SerializationMapWith1000Objects() {
HashMap<String, Object> map = buildNestedMap(1000);
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000);
JSONObject object = new JSONObject(map, parserConfiguration);
String jsonString = object.toString();
}
@Test(expected = JSONException.class)
public void issue743SerializationMapWith1001Objects() {
HashMap<String, Object> map = buildNestedMap(1001);
JSONObject object = new JSONObject(map);
String jsonString = object.toString();
}
@Test(expected = JSONException.class)
public void testCircleReferenceFirstLevel() {
Map<Object, Object> jsonObject = new HashMap<>();
jsonObject.put("test", jsonObject);
new JSONObject(jsonObject, new JSONParserConfiguration());
}
@Test(expected = StackOverflowError.class)
public void testCircleReferenceMultiplyLevel_notConfigured_expectedStackOverflow() {
Map<Object, Object> inside = new HashMap<>();
Map<Object, Object> 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<Object, Object> inside = new HashMap<>();
Map<Object, Object> jsonObject = new HashMap<>();
inside.put("test", jsonObject);
jsonObject.put("test", inside);
new JSONObject(jsonObject, new JSONParserConfiguration());
}
@Test
public void testDifferentKeySameInstanceNotACircleReference() {
Map<Object, Object> map1 = new HashMap<>();
Map<Object, Object> map2 = new HashMap<>();
map1.put("test1", map2);
map1.put("test2", map2);
new JSONObject(map1);
}
/**
* Method to build nested map of max maxDepth
*
* @param maxDepth
* @return
*/
public static HashMap<String, Object> buildNestedMap(int maxDepth) {
if (maxDepth <= 0) {
return new HashMap<>();
}
HashMap<String, Object> nestedMap = new HashMap<>();
nestedMap.put("t", buildNestedMap(maxDepth - 1));
return nestedMap;
}
}

View File

@@ -12,7 +12,7 @@ import java.util.List;
*/
public class WeirdList {
/** */
private final List<Integer> list = new ArrayList();
private final List<Integer> list = new ArrayList<>();
/**
* @param vals
@@ -25,14 +25,14 @@ public class WeirdList {
* @return a copy of the list
*/
public List<Integer> get() {
return new ArrayList(this.list);
return new ArrayList<>(this.list);
}
/**
* @return a copy of the list
*/
public List<Integer> getALL() {
return new ArrayList(this.list);
return new ArrayList<>(this.list);
}
/**