PageRenderTime 72ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/XomParserBehaviourTest.java

https://bitbucket.org/jwalton/xml-parser-sanity-check
Java | 100 lines | 79 code | 21 blank | 0 comment | 0 complexity | bbd4f5bbeaee12558bea74ea2b5fcd97 MD5 | raw file
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.StringReader;
  4. import javax.xml.parsers.ParserConfigurationException;
  5. import com.atlassian.security.xml.UntrustedXmlParserFactory;
  6. import org.hamcrest.CoreMatchers;
  7. import org.junit.Test;
  8. import org.junit.matchers.JUnitMatchers;
  9. import org.xml.sax.InputSource;
  10. import org.xml.sax.SAXException;
  11. import nu.xom.Builder;
  12. import nu.xom.Document;
  13. import nu.xom.ParsingException;
  14. import static org.junit.Assert.assertEquals;
  15. import static org.junit.Assert.assertFalse;
  16. import static org.junit.Assert.assertNotNull;
  17. import static org.junit.Assert.assertThat;
  18. public class XomParserBehaviourTest
  19. {
  20. Builder newBuilder() throws ParserConfigurationException, SAXException
  21. {
  22. return new Builder(UntrustedXmlParserFactory.newXmlReader());
  23. }
  24. @Test
  25. public void testXomParserIsBroken() throws IOException, ParsingException
  26. {
  27. final InputStream in = this.getClass().getResourceAsStream("/evil.xml");
  28. assertNotNull("Could not load /evil.xml as stream.", in);
  29. Builder builder = new Builder();
  30. Document doc = builder.build(in);
  31. assertEquals("Did not load SYSTEM entity containing TOP SECRET", "TOP SECRET", doc.getValue().trim());
  32. }
  33. @Test
  34. public void testXomParserIsSafe() throws Exception
  35. {
  36. final InputStream in = this.getClass().getResourceAsStream("/evil.xml");
  37. assertNotNull("Could not load /evil.xml as stream.", in);
  38. Builder builder = newBuilder();
  39. Document doc = builder.build(in);
  40. assertThat(doc.getValue(), CoreMatchers.not(JUnitMatchers.containsString("TOP SECRET")));
  41. }
  42. @Test
  43. public void parseDocumentExpandsAmpersand() throws Exception
  44. {
  45. Document d = newBuilder().build(new StringReader(SampleXmlDocuments.AMPERSAND_DOCUMENT));
  46. assertEquals("&", d.getRootElement().getValue());
  47. }
  48. @Test(expected = ParsingException.class, timeout=1000)
  49. public void parseBillionLaughsDoesNotExhaustMemory() throws Exception
  50. {
  51. newBuilder().build(new StringReader(SampleXmlDocuments.BILLION_LAUGHS));
  52. }
  53. @Test
  54. public void externalEntityIsNotIncludedInResults() throws Exception
  55. {
  56. Document d = newBuilder().build(new StringReader(SampleXmlDocuments.externalResourceEntity()));
  57. assertEquals("", d.getRootElement().getValue());
  58. }
  59. @Test
  60. public void externalEntityIsNotRead() throws Exception
  61. {
  62. HttpAttemptDetector detector = new HttpAttemptDetector();
  63. new Thread(detector).start();
  64. Document d = newBuilder().build(
  65. new StringReader(SampleXmlDocuments.externalResourceEntity(detector.getUrl())));
  66. assertFalse(detector.wasAttempted());
  67. }
  68. @Test
  69. public void dtdUriPointsToUrl() throws Exception
  70. {
  71. HttpAttemptDetector detector = new HttpAttemptDetector();
  72. new Thread(detector).start();
  73. String s = SampleXmlDocuments.externalUrlDtd(detector.getUrl());
  74. Document d = newBuilder().build(new StringReader(s));
  75. assertEquals("root", d.getRootElement().getLocalName());
  76. assertEquals(0, d.getRootElement().getChildCount());
  77. assertFalse("I don't want to see HTTP connection attempts", detector.wasAttempted());
  78. }
  79. }