-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CBOR: support deep nesting constraints (#361)
- Loading branch information
Showing
4 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
110 changes: 110 additions & 0 deletions
110
.../src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/dos/DeepNestingParserTest.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,110 @@ | ||
package com.fasterxml.jackson.dataformat.cbor.parse.dos; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.core.StreamReadConstraints; | ||
import com.fasterxml.jackson.core.exc.StreamConstraintsException; | ||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory; | ||
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Unit tests for deeply nested JSON | ||
*/ | ||
public class DeepNestingParserTest extends CBORTestBase | ||
{ | ||
public void testDeeplyNestedObjects() throws Exception | ||
{ | ||
final int depth = 1500; | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
genDeepDoc(out, depth); | ||
try (JsonParser jp = cborParser(out)) { | ||
JsonToken jt; | ||
while ((jt = jp.nextToken()) != null) { | ||
|
||
} | ||
fail("expected StreamConstraintsException"); | ||
} catch (StreamConstraintsException e) { | ||
assertEquals("Depth (1001) exceeds the maximum allowed nesting depth (1000)", e.getMessage()); | ||
} | ||
} | ||
|
||
public void testDeeplyNestedObjectsWithUnconstrainedMapper() throws Exception | ||
{ | ||
final int depth = 1500; | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
genDeepDoc(out, depth); | ||
CBORFactory cborFactory = CBORFactory.builder() | ||
.streamReadConstraints(StreamReadConstraints.builder().maxNestingDepth(Integer.MAX_VALUE).build()) | ||
.build(); | ||
try (JsonParser jp = cborParser(cborFactory, out)) { | ||
JsonToken jt; | ||
while ((jt = jp.nextToken()) != null) { | ||
|
||
} | ||
} | ||
} | ||
|
||
public void testDeeplyNestedArrays() throws Exception | ||
{ | ||
final int depth = 750; | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
genDeepArrayDoc(out, depth); | ||
try (JsonParser jp = cborParser(out)) { | ||
JsonToken jt; | ||
while ((jt = jp.nextToken()) != null) { | ||
|
||
} | ||
fail("expected StreamConstraintsException"); | ||
} catch (StreamConstraintsException e) { | ||
assertEquals("Depth (1001) exceeds the maximum allowed nesting depth (1000)", e.getMessage()); | ||
} | ||
} | ||
|
||
public void testDeeplyNestedArraysWithUnconstrainedMapper() throws Exception | ||
{ | ||
final int depth = 750; | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
genDeepArrayDoc(out, depth); | ||
CBORFactory cborFactory = CBORFactory.builder() | ||
.streamReadConstraints(StreamReadConstraints.builder().maxNestingDepth(Integer.MAX_VALUE).build()) | ||
.build(); | ||
try (JsonParser jp = cborParser(cborFactory, out)) { | ||
JsonToken jt; | ||
while ((jt = jp.nextToken()) != null) { | ||
|
||
} | ||
} | ||
} | ||
|
||
private void genDeepDoc(final ByteArrayOutputStream out, final int depth) throws IOException { | ||
try (JsonGenerator gen = cborGenerator(out)) { | ||
for (int i = 0; i < depth; i++) { | ||
gen.writeStartObject(); | ||
gen.writeFieldName("a"); | ||
} | ||
gen.writeString("val"); | ||
for (int i = 0; i < depth; i++) { | ||
gen.writeEndObject(); | ||
} | ||
} | ||
} | ||
|
||
private void genDeepArrayDoc(final ByteArrayOutputStream out, final int depth) throws IOException { | ||
try (JsonGenerator gen = cborGenerator(out)) { | ||
for (int i = 0; i < depth; i++) { | ||
gen.writeStartObject(); | ||
gen.writeFieldName("a"); | ||
gen.writeStartArray(); | ||
} | ||
gen.writeString("val"); | ||
for (int i = 0; i < depth; i++) { | ||
gen.writeEndArray(); | ||
gen.writeEndObject(); | ||
} | ||
} | ||
} | ||
} |