Skip to content

Commit 966ac52

Browse files
committed
Add more constraints tests wrt nesting for CBOR, Smile
1 parent ce3aa21 commit 966ac52

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.fasterxml.jackson.dataformat.cbor.gen.constraints;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.StreamReadConstraints;
5+
import com.fasterxml.jackson.core.StreamWriteConstraints;
6+
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
7+
8+
import com.fasterxml.jackson.databind.JsonNode;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.node.ObjectNode;
11+
12+
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
13+
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
14+
15+
public class DeeplyNestedCBORReadWriteTest extends CBORTestBase
16+
{
17+
private final ObjectMapper MAPPER_VANILLA = cborMapper();
18+
19+
private final ObjectMapper MAPPER_CONSTRAINED = cborMapper(
20+
CBORFactory.builder()
21+
// Use higher limit for writing to simplify testing setup
22+
.streamReadConstraints(StreamReadConstraints.builder()
23+
.maxNestingDepth(10).build())
24+
.streamWriteConstraints(StreamWriteConstraints.builder()
25+
.maxNestingDepth(12).build())
26+
.build()
27+
);
28+
29+
public void testDeepNestingRead() throws Exception
30+
{
31+
final byte[] DOC = MAPPER_CONSTRAINED.writeValueAsBytes(createDeepNestedDoc(11));
32+
try (JsonParser p = MAPPER_CONSTRAINED.createParser(DOC)) {
33+
_testDeepNestingRead(p);
34+
}
35+
}
36+
37+
private void _testDeepNestingRead(JsonParser p) throws Exception
38+
{
39+
try {
40+
while (p.nextToken() != null) { }
41+
fail("expected StreamConstraintsException");
42+
} catch (StreamConstraintsException e) {
43+
assertEquals("Document nesting depth (11) exceeds the maximum allowed (10, from `StreamReadConstraints.getMaxNestingDepth()`)",
44+
e.getMessage());
45+
}
46+
}
47+
48+
public void testDeepNestingWrite() throws Exception
49+
{
50+
final JsonNode docRoot = createDeepNestedDoc(13);
51+
try {
52+
MAPPER_CONSTRAINED.writeValueAsBytes(docRoot);
53+
fail("Should not pass");
54+
} catch (StreamConstraintsException e) {
55+
assertEquals("Document nesting depth (13) exceeds the maximum allowed (12, from `StreamWriteConstraints.getMaxNestingDepth()`)",
56+
e.getMessage());
57+
}
58+
}
59+
60+
private JsonNode createDeepNestedDoc(final int depth) throws Exception
61+
{
62+
final ObjectNode root = MAPPER_VANILLA.createObjectNode();
63+
ObjectNode curr = root;
64+
for (int i = 0; i < depth; ++i) {
65+
curr = curr.putObject("nested"+i);
66+
}
67+
curr.put("value", 42);
68+
return root;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.fasterxml.jackson.dataformat.smile.constraints;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.StreamReadConstraints;
5+
import com.fasterxml.jackson.core.StreamWriteConstraints;
6+
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
7+
8+
import com.fasterxml.jackson.databind.JsonNode;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.node.ObjectNode;
11+
12+
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
13+
import com.fasterxml.jackson.dataformat.smile.databind.SmileMapper;
14+
import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
15+
16+
public class DeeplyNestedSmileReadWriteTest extends BaseTestForSmile
17+
{
18+
private final ObjectMapper MAPPER_VANILLA = smileMapper();
19+
20+
private final ObjectMapper MAPPER_CONSTRAINED = new SmileMapper(
21+
SmileFactory.builder()
22+
// Use higher limit for writing to simplify testing setup
23+
.streamReadConstraints(StreamReadConstraints.builder()
24+
.maxNestingDepth(10).build())
25+
.streamWriteConstraints(StreamWriteConstraints.builder()
26+
.maxNestingDepth(12).build())
27+
.build()
28+
);
29+
30+
public void testDeepNestingRead() throws Exception
31+
{
32+
final byte[] DOC = MAPPER_CONSTRAINED.writeValueAsBytes(createDeepNestedDoc(11));
33+
try (JsonParser p = MAPPER_CONSTRAINED.createParser(DOC)) {
34+
_testDeepNestingRead(p);
35+
}
36+
}
37+
38+
private void _testDeepNestingRead(JsonParser p) throws Exception
39+
{
40+
try {
41+
while (p.nextToken() != null) { }
42+
fail("expected StreamConstraintsException");
43+
} catch (StreamConstraintsException e) {
44+
assertEquals("Document nesting depth (11) exceeds the maximum allowed (10, from `StreamReadConstraints.getMaxNestingDepth()`)",
45+
e.getMessage());
46+
}
47+
}
48+
49+
public void testDeepNestingWrite() throws Exception
50+
{
51+
final JsonNode docRoot = createDeepNestedDoc(13);
52+
try {
53+
MAPPER_CONSTRAINED.writeValueAsBytes(docRoot);
54+
fail("Should not pass");
55+
} catch (StreamConstraintsException e) {
56+
assertEquals("Document nesting depth (13) exceeds the maximum allowed (12, from `StreamWriteConstraints.getMaxNestingDepth()`)",
57+
e.getMessage());
58+
}
59+
}
60+
61+
private JsonNode createDeepNestedDoc(final int depth) throws Exception
62+
{
63+
final ObjectNode root = MAPPER_VANILLA.createObjectNode();
64+
ObjectNode curr = root;
65+
for (int i = 0; i < depth; ++i) {
66+
curr = curr.putObject("nested"+i);
67+
}
68+
curr.put("value", 42);
69+
return root;
70+
}
71+
}

0 commit comments

Comments
 (0)