|
| 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