| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.tools; |
| | 3 | |
| | 4 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| | 5 | import org.junit.Rule; |
| | 6 | import org.junit.Test; |
| | 7 | import org.openstreetmap.josm.TestUtils; |
| | 8 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| | 9 | import org.xml.sax.InputSource; |
| | 10 | import org.xml.sax.SAXException; |
| | 11 | import org.xml.sax.helpers.DefaultHandler; |
| | 12 | |
| | 13 | import javax.xml.parsers.ParserConfigurationException; |
| | 14 | import javax.xml.transform.Transformer; |
| | 15 | import javax.xml.transform.TransformerException; |
| | 16 | import javax.xml.transform.stream.StreamResult; |
| | 17 | import javax.xml.transform.stream.StreamSource; |
| | 18 | import java.io.FileInputStream; |
| | 19 | import java.io.IOException; |
| | 20 | import java.io.StringWriter; |
| | 21 | |
| | 22 | import static org.junit.Assert.assertEquals; |
| | 23 | import static org.junit.Assert.assertNotNull; |
| | 24 | import static org.junit.Assert.fail; |
| | 25 | |
| | 26 | /** |
| | 27 | * Unit tests of {@link XmlUtils} class. |
| | 28 | */ |
| | 29 | public class XmlUtilsTest { |
| | 30 | |
| | 31 | /** |
| | 32 | * Use default, basic test rules. |
| | 33 | */ |
| | 34 | @Rule |
| | 35 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| | 36 | public JOSMTestRules rules = new JOSMTestRules(); |
| | 37 | |
| | 38 | @Test |
| | 39 | public void testExternalEntitiesParsingDom() throws IOException, ParserConfigurationException { |
| | 40 | try { |
| | 41 | final String source = TestUtils.getTestDataRoot() + "dom_external_entity.xml"; |
| | 42 | XmlUtils.parseSafeDOM(new FileInputStream(source)); |
| | 43 | fail("Parsing a document with external entities should not be allowed."); |
| | 44 | } catch (SAXException e) { |
| | 45 | assertEquals("External Entity: Failed to read external document 'passwd', because 'file' access is not allowed due to restriction set by the accessExternalDTD property.", e.getMessage()); |
| | 46 | } |
| | 47 | } |
| | 48 | |
| | 49 | @Test |
| | 50 | public void testExternalEntitiesSaxParser() throws IOException, ParserConfigurationException { |
| | 51 | try { |
| | 52 | final String source = TestUtils.getTestDataRoot() + "dom_external_entity.xml"; |
| | 53 | final DefaultHandler handler = new DefaultHandler(); |
| | 54 | XmlUtils.parseSafeSAX(new InputSource(new FileInputStream(source)), handler); |
| | 55 | fail("Parsing a document with external entities should not be allowed."); |
| | 56 | } catch (SAXException e) { |
| | 57 | assertEquals("External Entity: Failed to read external document 'passwd', because 'file' access is not allowed due to restriction set by the accessExternalDTD property.", e.getMessage()); |
| | 58 | } |
| | 59 | } |
| | 60 | |
| | 61 | @Test |
| | 62 | public void testExternalEntitiesTransformer() throws IOException { |
| | 63 | try { |
| | 64 | final String source = TestUtils.getTestDataRoot() + "dom_external_entity.xml"; |
| | 65 | final Transformer transformer = XmlUtils.newSafeTransformerFactory().newTransformer(); |
| | 66 | transformer.transform(new StreamSource(new FileInputStream(source)), new StreamResult(new StringWriter())); |
| | 67 | fail("Parsing a document with external entities should not be allowed."); |
| | 68 | } catch (TransformerException e) { |
| | 69 | assertNotNull(e.getCause()); |
| | 70 | assertEquals("External Entity: Failed to read external document 'passwd', because 'file' access is not allowed due to restriction set by the accessExternalDTD property.", e.getCause().getMessage()); |
| | 71 | } |
| | 72 | } |
| | 73 | } |
| | 74 | No newline at end of file |