diff --git a/properties/src/test/java/tools/jackson/dataformat/javaprop/constraints/DeeplyNestedPropsReadWriteTest.java b/properties/src/test/java/tools/jackson/dataformat/javaprop/constraints/DeeplyNestedPropsReadWriteTest.java new file mode 100644 index 000000000..e69be4f27 --- /dev/null +++ b/properties/src/test/java/tools/jackson/dataformat/javaprop/constraints/DeeplyNestedPropsReadWriteTest.java @@ -0,0 +1,104 @@ +package tools.jackson.dataformat.javaprop.constraints; + +import tools.jackson.core.*; +import tools.jackson.core.exc.StreamConstraintsException; + +import tools.jackson.databind.JsonNode; +import tools.jackson.databind.ObjectMapper; +import tools.jackson.databind.node.ObjectNode; +import tools.jackson.dataformat.javaprop.JavaPropsFactory; +import tools.jackson.dataformat.javaprop.JavaPropsMapper; +import tools.jackson.dataformat.javaprop.ModuleTestBase; + +public class DeeplyNestedPropsReadWriteTest extends ModuleTestBase +{ + private final JavaPropsMapper PROPS_MAPPER_VANILLA = newPropertiesMapper(); + + private final JavaPropsMapper PROPS_MAPPER_CONSTR = new JavaPropsMapper( + JavaPropsFactory.builder() + // Use higher limit for writing to simplify testing setup + .streamReadConstraints(StreamReadConstraints.builder() + .maxNestingDepth(10).build()) + .streamWriteConstraints(StreamWriteConstraints.builder() + .maxNestingDepth(12).build()) + .build() + ); + + public void testDeepNestingRead() throws Exception + { + final String DOC = PROPS_MAPPER_CONSTR.writeValueAsString(createDeepNestedDoc(11)); + try (JsonParser p = PROPS_MAPPER_CONSTR.createParser(DOC)) { + _testDeepNestingRead(p); + } + } + + private void _testDeepNestingRead(JsonParser p) throws Exception + { + try { + while (p.nextToken() != null) { } + fail("expected StreamConstraintsException"); + } catch (StreamConstraintsException e) { + assertEquals("Document nesting depth (11) exceeds the maximum allowed (10, from `StreamReadConstraints.getMaxNestingDepth()`)", + e.getMessage()); + } + } + + public void testDeepNestingWrite() throws Exception + { + final JsonNode docRoot = createDeepNestedDoc(13); + try { + PROPS_MAPPER_CONSTR.writeValueAsString(docRoot); + fail("Should not pass"); + } catch (StreamConstraintsException e) { + assertEquals("Document nesting depth (13) exceeds the maximum allowed (12, from `StreamWriteConstraints.getMaxNestingDepth()`)", + e.getMessage()); + } + } + + public void testDeeplyNestedReadVanilla() throws Exception { + final int depth = 1500; + final String doc = createDeepNestedString(depth); + try (JsonParser p = PROPS_MAPPER_VANILLA.createParser(doc)) { + while (p.nextToken() != null) { } + fail("expected StreamConstraintsException"); + } catch (StreamConstraintsException e) { + String exceptionPrefix = String.format("Document nesting depth (%d) exceeds the maximum allowed", + StreamReadConstraints.DEFAULT_MAX_DEPTH + 1); + assertTrue("unexpected exception message: " + e.getMessage(), + e.getMessage().startsWith(exceptionPrefix)); + } + } + + public void testDeeplyNestedReadWithUnconstrainedMapper() throws Exception { + final int depth = 1500; + final String doc = createDeepNestedString(depth); + final JavaPropsFactory factory = JavaPropsFactory.builder() + .streamReadConstraints(StreamReadConstraints.builder().maxNestingDepth(Integer.MAX_VALUE).build()) + .build(); + final ObjectMapper mapper = propertiesMapperBuilder(factory).build(); + try (JsonParser p = mapper.createParser(doc)) { + while (p.nextToken() != null) { } + } + } + + private JsonNode createDeepNestedDoc(final int depth) throws Exception + { + final ObjectNode root = PROPS_MAPPER_VANILLA.createObjectNode(); + ObjectNode curr = root; + for (int i = 0; i < depth; ++i) { + curr = curr.putObject("nested"+i); + } + curr.put("value", 42); + return root; + } + + private String createDeepNestedString(final int depth) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < depth; i++) { + if (i > 1) sb.append('.'); + sb.append('a'); + } + sb.append("=val"); + return sb.toString(); + } +} diff --git a/properties/src/test/java/tools/jackson/dataformat/javaprop/dos/DeepNestParserTest.java b/properties/src/test/java/tools/jackson/dataformat/javaprop/dos/DeepNestParserTest.java deleted file mode 100644 index 8ec396c77..000000000 --- a/properties/src/test/java/tools/jackson/dataformat/javaprop/dos/DeepNestParserTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package tools.jackson.dataformat.javaprop.dos; - -import tools.jackson.core.*; -import tools.jackson.core.exc.StreamConstraintsException; - -import tools.jackson.databind.ObjectMapper; -import tools.jackson.dataformat.javaprop.JavaPropsFactory; -import tools.jackson.dataformat.javaprop.ModuleTestBase; - -public class DeepNestParserTest extends ModuleTestBase { - - public void testDeeplyNestedData() throws Exception { - final int depth = 1500; - final String doc = genDeeplyNestedData(depth); - final ObjectMapper mapper = newPropertiesMapper(); - try (JsonParser p = mapper.createParser(doc)) { - while (p.nextToken() != null) { } - fail("expected StreamConstraintsException"); - } catch (StreamConstraintsException e) { - String exceptionPrefix = String.format("Document nesting depth (%d) exceeds the maximum allowed", - StreamReadConstraints.DEFAULT_MAX_DEPTH + 1); - assertTrue("unexpected exception message: " + e.getMessage(), - e.getMessage().startsWith(exceptionPrefix)); - } - } - - public void testDeeplyNestedDataWithUnconstrainedMapper() throws Exception { - final int depth = 1500; - final String doc = genDeeplyNestedData(depth); - final JavaPropsFactory factory = JavaPropsFactory.builder() - .streamReadConstraints(StreamReadConstraints.builder().maxNestingDepth(Integer.MAX_VALUE).build()) - .build(); - final ObjectMapper mapper = propertiesMapperBuilder(factory).build(); - try (JsonParser p = mapper.createParser(doc)) { - while (p.nextToken() != null) { } - } - } - - private String genDeeplyNestedData(final int depth) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < depth; i++) { - if (i > 1) sb.append('.'); - sb.append('a'); - } - sb.append("=val"); - return sb.toString(); - } -} diff --git a/yaml/src/test/java/tools/jackson/dataformat/yaml/constraints/DeeplyNestedYAMLReadWriteTest.java b/yaml/src/test/java/tools/jackson/dataformat/yaml/constraints/DeeplyNestedYAMLReadWriteTest.java index b3b67e955..bfc0d2065 100644 --- a/yaml/src/test/java/tools/jackson/dataformat/yaml/constraints/DeeplyNestedYAMLReadWriteTest.java +++ b/yaml/src/test/java/tools/jackson/dataformat/yaml/constraints/DeeplyNestedYAMLReadWriteTest.java @@ -53,7 +53,6 @@ public void testDeepNestingWrite() throws Exception YAML_MAPPER.writeValueAsString(docRoot); fail("Should not pass"); } catch (StreamConstraintsException e) { - e.printStackTrace(); assertEquals("Document nesting depth (13) exceeds the maximum allowed (12, from `StreamWriteConstraints.getMaxNestingDepth()`)", e.getMessage()); }