Skip to content

Commit

Permalink
Merge branch '2.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 28, 2024
2 parents 7cd81a4 + 66b21dc commit ea65963
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/test/java/tools/jackson/dataformat/xml/dos/TokenCountTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package tools.jackson.dataformat.xml.dos;

import tools.jackson.core.JsonParser;
import tools.jackson.core.StreamReadConstraints;
import tools.jackson.core.exc.StreamConstraintsException;
import tools.jackson.dataformat.xml.XmlFactory;
import tools.jackson.dataformat.xml.XmlMapper;
import tools.jackson.dataformat.xml.XmlTestBase;

public class TokenCountTest extends XmlTestBase
{
final XmlMapper XML_MAPPER;
{
final XmlFactory factory = XmlFactory.builder()
// token count is only checked when maxTokenCount is set
.streamReadConstraints(StreamReadConstraints.builder()
.maxTokenCount(1000)
.build())
.build();
XML_MAPPER = mapperBuilder(factory).build();
}

public void testTokenCount10() throws Exception
{
final String XML = createDeepNestedDoc(10);
try (JsonParser p = XML_MAPPER.createParser(XML)) {
while (p.nextToken() != null) { }
assertEquals(31, p.currentTokenCount());
}
}

public void testTokenCount100() throws Exception
{
final String XML = createDeepNestedDoc(100);
try (JsonParser p = XML_MAPPER.createParser(XML)) {
while (p.nextToken() != null) { }
assertEquals(301, p.currentTokenCount());
}
}

public void testDeepDoc() throws Exception
{
final String XML = createDeepNestedDoc(1000);
try (JsonParser p = XML_MAPPER.createParser(XML)) {
while (p.nextToken() != null) { }
fail("expected StreamReadException");
} catch (StreamConstraintsException e) {
assertTrue(e.getMessage().contains("Token count (1001) exceeds the maximum allowed"));
}
}

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

0 comments on commit ea65963

Please sign in to comment.