PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/StaxParserBehaviourTest.java

https://bitbucket.org/jwalton/xml-parser-sanity-check
Java | 104 lines | 67 code | 25 blank | 12 comment | 7 complexity | 95ddba9dce2e6252bb8f64ec11c139eb MD5 | raw file
  1. import java.io.StringReader;
  2. import javax.xml.stream.XMLInputFactory;
  3. import javax.xml.stream.XMLStreamConstants;
  4. import javax.xml.stream.XMLStreamException;
  5. import javax.xml.stream.XMLStreamReader;
  6. import com.atlassian.security.xml.UntrustedXmlParserFactory;
  7. import org.junit.Test;
  8. import static org.junit.Assert.assertEquals;
  9. import static org.junit.Assert.assertFalse;
  10. public class StaxParserBehaviourTest
  11. {
  12. // XMLInputFactory newFactory() throws Exception;
  13. // return XMLInputFactory.newFactory();
  14. XMLStreamReader createReader(String input) throws Exception
  15. {
  16. XMLInputFactory fac = UntrustedXmlParserFactory.newXmlInputFactory();
  17. XMLStreamReader sr = fac.createXMLStreamReader(new StringReader(input));
  18. return sr;
  19. }
  20. // XMLStreamReader createReader(String input) throws Exception
  21. // {
  22. // XMLInputFactory fac = newFactory();
  23. // fac.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
  24. //// fac.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
  25. //
  26. // XMLStreamReader sr = fac.createXMLStreamReader(new StringReader(input));
  27. // return sr;
  28. // }
  29. static String gatherCharacters(XMLStreamReader sr) throws XMLStreamException
  30. {
  31. StringBuilder sb = new StringBuilder();
  32. while (sr.next() != XMLStreamConstants.END_DOCUMENT)
  33. {
  34. if (sr.getEventType() == XMLStreamConstants.CHARACTERS)
  35. {
  36. if (sr.hasText())
  37. {
  38. sb.append(sr.getText());
  39. }
  40. }
  41. }
  42. return sb.toString();
  43. }
  44. @Test
  45. public void parseDocumentExpandsAmpersand() throws Exception
  46. {
  47. XMLStreamReader sr = createReader(SampleXmlDocuments.AMPERSAND_DOCUMENT);
  48. assertEquals("&", gatherCharacters(sr));
  49. }
  50. @Test(expected = XMLStreamException.class, timeout = 1000)
  51. public void parseBillionLaughsDoesNotExhaustTime() throws Exception
  52. {
  53. XMLStreamReader sr = createReader(SampleXmlDocuments.BILLION_LAUGHS);
  54. while (sr.next() != XMLStreamConstants.END_DOCUMENT);
  55. }
  56. @Test(expected = XMLStreamException.class)
  57. public void externalEntityIsNotRead() throws Exception
  58. {
  59. XMLStreamReader sr = createReader(SampleXmlDocuments.externalResourceEntity());
  60. String text = gatherCharacters(sr);
  61. // System.out.println(text);
  62. }
  63. @Test
  64. public void dtdUriPointsToFile() throws Exception
  65. {
  66. XMLStreamReader sr = createReader(SampleXmlDocuments.EXTERNAL_DTD);
  67. assertEquals("", gatherCharacters(sr));
  68. }
  69. @Test
  70. public void dtdUriPointsToUrl() throws Exception
  71. {
  72. HttpAttemptDetector detector = new HttpAttemptDetector();
  73. new Thread(detector).start();
  74. String s = SampleXmlDocuments.externalUrlDtd(detector.getUrl());
  75. XMLStreamReader sr = createReader(s);
  76. assertEquals("", gatherCharacters(sr));
  77. assertFalse("I don't want to see HTTP connection attempts", detector.wasAttempted());
  78. }
  79. }