-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/test/java/com/fasterxml/jackson/dataformat/xml/stream/dos/TokenCountTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |