|
| 1 | +package tools.jackson.dataformat.smile.dos; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | + |
| 6 | +import tools.jackson.core.*; |
| 7 | +import tools.jackson.core.exc.StreamConstraintsException; |
| 8 | +import tools.jackson.dataformat.smile.BaseTestForSmile; |
| 9 | +import tools.jackson.dataformat.smile.SmileFactory; |
| 10 | +import tools.jackson.dataformat.smile.databind.SmileMapper; |
| 11 | + |
| 12 | +/** |
| 13 | + * Unit tests for deeply nested JSON |
| 14 | + */ |
| 15 | +public class DeepNestingParserTest extends BaseTestForSmile |
| 16 | +{ |
| 17 | + SmileMapper DEFAULT_MAPPER = newSmileMapper(); |
| 18 | + |
| 19 | + SmileMapper UNCONSTRAINED_MAPPER; |
| 20 | + { |
| 21 | + SmileFactory smileFactory = SmileFactory.builder() |
| 22 | + .streamReadConstraints(StreamReadConstraints.builder().maxNestingDepth(Integer.MAX_VALUE).build()) |
| 23 | + .build(); |
| 24 | + UNCONSTRAINED_MAPPER = new SmileMapper(smileFactory); |
| 25 | + } |
| 26 | + |
| 27 | + public void testDeeplyNestedObjects() throws Exception |
| 28 | + { |
| 29 | + final int depth = 1500; |
| 30 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 31 | + genDeepDoc(out, depth); |
| 32 | + try (JsonParser jp = DEFAULT_MAPPER.createParser(out.toByteArray())) { |
| 33 | + JsonToken jt; |
| 34 | + while ((jt = jp.nextToken()) != null) { |
| 35 | + |
| 36 | + } |
| 37 | + fail("expected StreamConstraintsException"); |
| 38 | + } catch (StreamConstraintsException e) { |
| 39 | + assertEquals("Depth (1001) exceeds the maximum allowed nesting depth (1000)", e.getMessage()); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public void testDeeplyNestedObjectsWithUnconstrainedMapper() throws Exception |
| 44 | + { |
| 45 | + final int depth = 1500; |
| 46 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 47 | + genDeepDoc(out, depth); |
| 48 | + try (JsonParser jp = UNCONSTRAINED_MAPPER.createParser(out.toByteArray())) { |
| 49 | + JsonToken jt; |
| 50 | + while ((jt = jp.nextToken()) != null) { |
| 51 | + |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public void testDeeplyNestedArrays() throws Exception |
| 57 | + { |
| 58 | + final int depth = 750; |
| 59 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 60 | + genDeepArrayDoc(out, depth); |
| 61 | + try (JsonParser jp = DEFAULT_MAPPER.createParser(out.toByteArray())) { |
| 62 | + JsonToken jt; |
| 63 | + while ((jt = jp.nextToken()) != null) { |
| 64 | + |
| 65 | + } |
| 66 | + fail("expected StreamConstraintsException"); |
| 67 | + } catch (StreamConstraintsException e) { |
| 68 | + assertEquals("Depth (1001) exceeds the maximum allowed nesting depth (1000)", e.getMessage()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public void testDeeplyNestedArraysWithUnconstrainedMapper() throws Exception |
| 73 | + { |
| 74 | + final int depth = 750; |
| 75 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 76 | + genDeepArrayDoc(out, depth); |
| 77 | + try (JsonParser jp = UNCONSTRAINED_MAPPER.createParser(out.toByteArray())) { |
| 78 | + JsonToken jt; |
| 79 | + while ((jt = jp.nextToken()) != null) { |
| 80 | + |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private void genDeepDoc(final ByteArrayOutputStream out, final int depth) throws IOException { |
| 86 | + try (JsonGenerator gen = _smileGenerator(out, true)) { |
| 87 | + for (int i = 0; i < depth; i++) { |
| 88 | + gen.writeStartObject(); |
| 89 | + gen.writeName("a"); |
| 90 | + } |
| 91 | + gen.writeString("val"); |
| 92 | + for (int i = 0; i < depth; i++) { |
| 93 | + gen.writeEndObject(); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void genDeepArrayDoc(final ByteArrayOutputStream out, final int depth) throws IOException { |
| 99 | + try (JsonGenerator gen = _smileGenerator(out, true)) { |
| 100 | + for (int i = 0; i < depth; i++) { |
| 101 | + gen.writeStartObject(); |
| 102 | + gen.writeName("a"); |
| 103 | + gen.writeStartArray(); |
| 104 | + } |
| 105 | + gen.writeString("val"); |
| 106 | + for (int i = 0; i < depth; i++) { |
| 107 | + gen.writeEndArray(); |
| 108 | + gen.writeEndObject(); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments