Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create DeepNestingParserTest.java #577

Merged
merged 3 commits into from
Mar 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.fasterxml.jackson.dataformat.xml.stream.dos;

import com.ctc.wstx.stax.WstxInputFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;

public class DeepNestingParserTest extends XmlTestBase {

public void testDeepDoc() throws Exception
{
final XmlMapper xmlMapper = newMapper();
final String XML = createDeepNestedDoc(1050);
try (JsonParser p = xmlMapper.createParser(XML)) {
JsonToken jt;
while ((jt = p.nextToken()) != null) {

}
fail("expected JsonParseException");
} catch (JsonParseException e) {
assertTrue(e.getMessage().contains("Maximum Element Depth limit (1000) Exceeded"));
}
}

public void testDeepDocWithCustomDepthLimit() throws Exception
{
final WstxInputFactory wstxInputFactory = new WstxInputFactory();
wstxInputFactory.getConfig().setMaxElementDepth(2000);
final XmlMapper xmlMapper = new XmlMapper(wstxInputFactory);
final String XML = createDeepNestedDoc(1050);
try (JsonParser p = xmlMapper.createParser(XML)) {
JsonToken jt;
while ((jt = p.nextToken()) != null) {

}
}
}

private String createDeepNestedDoc(final int depth) {
StringBuilder sb = new StringBuilder();
sb.append("<root>");
for (int i = 0; i < depth; i++) {
sb.append("<leaf>");
}
sb.append("abc");
for (int i = 0; i < depth; i++) {
sb.append("</leaf>");
}
sb.append("</root>");
return sb.toString();
}
}