-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bit more testing on JavaProps max-nesting constraints handling (i…
…t works)
- Loading branch information
1 parent
f4389b1
commit 6f42bb1
Showing
3 changed files
with
105 additions
and
51 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...com/fasterxml/jackson/dataformat/javaprop/constraints/DeeplyNestedPropsReadWriteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.fasterxml.jackson.dataformat.javaprop.constraints; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.StreamReadConstraints; | ||
import com.fasterxml.jackson.core.StreamWriteConstraints; | ||
import com.fasterxml.jackson.core.exc.StreamConstraintsException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsFactory; | ||
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsMapper; | ||
import com.fasterxml.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(); | ||
} | ||
} |
50 changes: 0 additions & 50 deletions
50
...rties/src/test/java/com/fasterxml/jackson/dataformat/javaprop/dos/DeepNestParserTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters