|
1 | 1 | package com.cybersource.ws.client; |
2 | 2 |
|
3 | 3 | import org.junit.Before; |
| 4 | +import org.junit.Rule; |
4 | 5 | import org.junit.Test; |
5 | 6 | import static org.junit.Assert.*; |
| 7 | + |
| 8 | +import org.junit.function.ThrowingRunnable; |
| 9 | +import org.junit.rules.ExpectedException; |
6 | 10 | import org.w3c.dom.Document; |
7 | 11 | import org.w3c.dom.Node; |
| 12 | +import org.w3c.dom.Text; |
| 13 | +import org.xml.sax.SAXException; |
| 14 | +import org.xml.sax.SAXParseException; |
8 | 15 |
|
| 16 | +import javax.xml.parsers.DocumentBuilder; |
| 17 | +import javax.xml.parsers.ParserConfigurationException; |
9 | 18 | import java.io.*; |
10 | 19 | import java.net.URL; |
11 | 20 | import java.util.*; |
12 | 21 |
|
13 | 22 | public class UtilityTest extends BaseTest { |
14 | 23 | String propertiesFilename; |
15 | 24 | Properties properties; |
| 25 | + private static final String ELEM_NVP_REPLY = "nvpReply"; |
16 | 26 |
|
17 | 27 | @Before |
18 | 28 | public void setUp() { |
@@ -128,7 +138,64 @@ public void testMapToString_error() { |
128 | 138 | assertTrue(result.isEmpty()); |
129 | 139 | } |
130 | 140 |
|
| 141 | + @Test |
| 142 | + public void testNewDocumentBuilder_validate() { |
| 143 | + String testData1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 144 | + "<!DOCTYPE example [\n" + |
| 145 | + " <!ENTITY file SYSTEM \"file:///secrets.txt\" >\n" + |
| 146 | + "]>\n" + |
| 147 | + "<example>&file;</example>"; |
| 148 | + |
| 149 | + String testData2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 150 | + "<!DOCTYPE example [\n" + |
| 151 | + " <!ENTITY file SYSTEM \"file:////etc/shadow\" >\n" + |
| 152 | + "]>\n" + |
| 153 | + "<example>&file;</example>"; |
| 154 | + |
| 155 | + String testData3 = "<?xml version=\"1.0\"?>" + |
| 156 | + "<w3resource>" + |
| 157 | + "<design>html xhtml css svg xml</design>" + |
| 158 | + "<programming>php mysql</programming>" + |
| 159 | + "</w3resource>"; |
| 160 | + |
| 161 | + try { |
| 162 | + DocumentBuilder docBuilder = Utility.newDocumentBuilder(); |
| 163 | + |
| 164 | + InputStream testStream = new ByteArrayInputStream(testData1.getBytes()); |
| 165 | + Document testDoc = docBuilder.parse(testStream); |
| 166 | + } catch (ParserConfigurationException e) { |
| 167 | + throw new RuntimeException(e); |
| 168 | + } catch (IOException e) { |
| 169 | + throw new RuntimeException(e); |
| 170 | + } catch (SAXException e) { |
| 171 | + assertSame(e.getClass(), SAXParseException.class); |
| 172 | + } |
131 | 173 |
|
| 174 | + try { |
| 175 | + DocumentBuilder docBuilder = Utility.newDocumentBuilder(); |
| 176 | + |
| 177 | + InputStream testStream = new ByteArrayInputStream(testData2.getBytes()); |
| 178 | + Document testDoc = docBuilder.parse(testStream); |
| 179 | + } catch (ParserConfigurationException e) { |
| 180 | + throw new RuntimeException(e); |
| 181 | + } catch (IOException e) { |
| 182 | + throw new RuntimeException(e); |
| 183 | + } catch (SAXException e) { |
| 184 | + assertSame(e.getClass(), SAXParseException.class); |
| 185 | + } |
132 | 186 |
|
133 | | - |
| 187 | + try { |
| 188 | + DocumentBuilder docBuilder = Utility.newDocumentBuilder(); |
| 189 | + |
| 190 | + InputStream testStream = new ByteArrayInputStream(testData3.getBytes()); |
| 191 | + Document testDoc = docBuilder.parse(testStream); |
| 192 | + assertNotNull(testDoc); |
| 193 | + } catch (ParserConfigurationException e) { |
| 194 | + throw new RuntimeException(e); |
| 195 | + } catch (IOException e) { |
| 196 | + throw new RuntimeException(e); |
| 197 | + } catch (SAXException e) { |
| 198 | + throw new RuntimeException(e); |
| 199 | + } |
| 200 | + } |
134 | 201 | } |
0 commit comments