Skip to content

Commit bef0b52

Browse files
committed
test: add unit tests for MAX_GEOMETRY_DEPTH limit in GeoJsonParser
Add three tests to GeoJsonParserTest to verify the fix for StackOverflowError caused by deeply nested GeometryCollection objects (introduced in the accompanying fix commit): - testDeeplyNestedGeometryCollection_doesNotThrowStackOverflow: Ensures parsing 2000-level nesting no longer throws StackOverflowError - testGeometryBeyondMaxDepth_returnsNull: Ensures geometry exceeding MAX_GEOMETRY_DEPTH (20) returns null instead of crashing - testShallowNestedGeometryCollection_parsedCorrectly: Ensures normal shallow nesting still parses correctly (regression guard) Relates to: #1699
1 parent 0b04ac5 commit bef0b52

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

library/src/test/java/com/google/maps/android/data/geojson/GeoJsonParserTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,51 @@ public void testInvalidGeometry() throws Exception {
386386
parser = new GeoJsonParser(invalidGeometryInvalidCoordinatesString());
387387
assertEquals(0, parser.getFeatures().size());
388388
}
389+
390+
// ---------------------------------------------------------------
391+
// Tests for fix: StackOverflowError on deeply nested GeometryCollection
392+
// PR #1699
393+
// ---------------------------------------------------------------
394+
395+
private static JSONObject buildNestedGeometryCollection(int depth) throws Exception {
396+
JSONObject innermost = new JSONObject();
397+
innermost.put("type", "Point");
398+
innermost.put("coordinates", new org.json.JSONArray("[0, 0]"));
399+
400+
JSONObject current = innermost;
401+
for (int i = 0; i < depth; i++) {
402+
JSONObject wrapper = new JSONObject();
403+
wrapper.put("type", "GeometryCollection");
404+
wrapper.put("geometries", new org.json.JSONArray().put(current));
405+
current = wrapper;
406+
}
407+
return current;
408+
}
409+
410+
@Test
411+
public void testDeeplyNestedGeometryCollection_doesNotThrowStackOverflow() throws Exception {
412+
JSONObject deeplyNested = buildNestedGeometryCollection(2000);
413+
try {
414+
GeoJsonParser.parseGeometry(deeplyNested);
415+
} catch (StackOverflowError e) {
416+
throw new AssertionError(
417+
"StackOverflowError thrown for deeply nested GeometryCollection — fix tidak bekerja!", e);
418+
}
419+
}
420+
421+
@Test
422+
public void testGeometryBeyondMaxDepth_returnsNull() throws Exception {
423+
JSONObject tooDeep = buildNestedGeometryCollection(21);
424+
Geometry result = GeoJsonParser.parseGeometry(tooDeep);
425+
assertNull("Geometry melebihi MAX_GEOMETRY_DEPTH seharusnya null", result);
426+
}
427+
428+
@Test
429+
public void testShallowNestedGeometryCollection_parsedCorrectly() throws Exception {
430+
JSONObject shallow = buildNestedGeometryCollection(3);
431+
Geometry result = GeoJsonParser.parseGeometry(shallow);
432+
assertNotNull("GeometryCollection dengan nesting normal seharusnya tidak null", result);
433+
assertTrue("Tipe geometry harus GeoJsonGeometryCollection",
434+
result instanceof GeoJsonGeometryCollection);
435+
}
389436
}

0 commit comments

Comments
 (0)