Skip to content

Commit

Permalink
add token count test (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Jun 28, 2024
1 parent 68bf1cb commit 66b21dc
Showing 1 changed file with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.fasterxml.jackson.dataformat.xml.stream.dos;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.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 66b21dc

Please sign in to comment.