|
| 1 | +package com.fasterxml.jackson.dataformat.xml.stream.dos; |
| 2 | + |
| 3 | +import com.ctc.wstx.stax.WstxInputFactory; |
| 4 | +import com.fasterxml.jackson.core.JsonParseException; |
| 5 | +import com.fasterxml.jackson.core.JsonParser; |
| 6 | +import com.fasterxml.jackson.core.JsonToken; |
| 7 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 8 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 9 | + |
| 10 | +public class DeepNestingParserTest extends XmlTestBase { |
| 11 | + |
| 12 | + public void testDeepDoc() throws Exception |
| 13 | + { |
| 14 | + final XmlMapper xmlMapper = newMapper(); |
| 15 | + final String XML = createDeepNestedDoc(1050); |
| 16 | + try (JsonParser p = xmlMapper.createParser(XML)) { |
| 17 | + JsonToken jt; |
| 18 | + while ((jt = p.nextToken()) != null) { |
| 19 | + |
| 20 | + } |
| 21 | + fail("expected JsonParseException"); |
| 22 | + } catch (JsonParseException e) { |
| 23 | + assertTrue(e.getMessage().contains("Maximum Element Depth limit (1000) Exceeded")); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public void testDeepDocWithCustomDepthLimit() throws Exception |
| 28 | + { |
| 29 | + final WstxInputFactory wstxInputFactory = new WstxInputFactory(); |
| 30 | + wstxInputFactory.getConfig().setMaxElementDepth(2000); |
| 31 | + final XmlMapper xmlMapper = new XmlMapper(wstxInputFactory); |
| 32 | + final String XML = createDeepNestedDoc(1050); |
| 33 | + try (JsonParser p = xmlMapper.createParser(XML)) { |
| 34 | + JsonToken jt; |
| 35 | + while ((jt = p.nextToken()) != null) { |
| 36 | + |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private String createDeepNestedDoc(final int depth) { |
| 42 | + StringBuilder sb = new StringBuilder(); |
| 43 | + sb.append("<root>"); |
| 44 | + for (int i = 0; i < depth; i++) { |
| 45 | + sb.append("<leaf>"); |
| 46 | + } |
| 47 | + sb.append("abc"); |
| 48 | + for (int i = 0; i < depth; i++) { |
| 49 | + sb.append("</leaf>"); |
| 50 | + } |
| 51 | + sb.append("</root>"); |
| 52 | + return sb.toString(); |
| 53 | + } |
| 54 | +} |
0 commit comments