1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package parsers; 24 25 import java.io.StringReader; 26 import javax.xml.parsers.DocumentBuilder; 27 import javax.xml.parsers.DocumentBuilderFactory; 28 import javax.xml.stream.XMLInputFactory; 29 import javax.xml.stream.XMLStreamReader; 30 import org.testng.annotations.DataProvider; 31 import org.testng.annotations.Listeners; 32 import org.testng.annotations.Test; 33 import org.xml.sax.InputSource; 34 35 /** 36 * @test 37 * @bug 8169450 38 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 39 * @run testng/othervm -DrunSecMngr=true parsers.BaseParsingTest 40 * @run testng/othervm parsers.BaseParsingTest 41 * @summary Tests that verify base parsing 42 */ 43 @Listeners({jaxp.library.BasePolicy.class}) 44 public class BaseParsingTest { 45 46 @DataProvider(name = "xmlDeclarations") 47 public static Object[][] xmlDeclarations() { 48 return new Object[][]{ 49 {"<?xml version=\"1.0\"?><root><test>t</test></root>"}, 50 {"<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><test>t</test></root>"}, 51 {"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone='yes'?><root><test>t</test></root>"}, 52 {"<?xml\n" 53 + " version=\"1.0\"?>\n" 54 + "<root>\n" 55 + " <test>t</test>\n" 56 + "</root>"}, 57 {"<?xml\n" 126 * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' 127 * [24] VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"') 128 * [25] Eq ::= S? '=' S? [26] VersionNum ::= '1.' [0-9]+ 129 * 130 * @param xml the test xml 131 * @throws Exception if the parser fails to parse the xml 132 */ 133 @Test(dataProvider = "xmlDeclarations") 134 public void test(String xml) throws Exception { 135 XMLInputFactory xif = XMLInputFactory.newDefaultFactory(); 136 XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml)); 137 while (xsr.hasNext()) { 138 xsr.next(); 139 } 140 } 141 142 /** 143 * @bug 8169450 144 * This particular issue does not appear in DOM parsing since the spaces are 145 * normalized during version detection. This test case then serves as a guard 146 * against such an issue from occuring in the version detection. 147 * 148 * @param xml the test xml 149 * @throws Exception if the parser fails to parse the xml 150 */ 151 @Test(dataProvider = "xmlDeclarations") 152 public void testWithDOM(String xml) throws Exception { 153 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 154 dbf.setValidating(true); 155 DocumentBuilder db = dbf.newDocumentBuilder(); 156 db.parse(new InputSource(new StringReader(xml))); 157 } 158 } | 1 /* 2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package parsers; 24 25 import java.io.ByteArrayInputStream; 26 import java.io.StringReader; 27 import javax.xml.parsers.DocumentBuilder; 28 import javax.xml.parsers.DocumentBuilderFactory; 29 import javax.xml.stream.XMLInputFactory; 30 import javax.xml.stream.XMLStreamReader; 31 import static org.testng.Assert.assertEquals; 32 import org.testng.annotations.DataProvider; 33 import org.testng.annotations.Listeners; 34 import org.testng.annotations.Test; 35 import org.w3c.dom.Document; 36 import org.xml.sax.InputSource; 37 38 /** 39 * @test 40 * @bug 8169450 8222415 41 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 42 * @run testng/othervm -DrunSecMngr=true parsers.BaseParsingTest 43 * @run testng/othervm parsers.BaseParsingTest 44 * @summary Tests that verify base parsing 45 */ 46 @Listeners({jaxp.library.BasePolicy.class}) 47 public class BaseParsingTest { 48 49 @DataProvider(name = "xmlDeclarations") 50 public static Object[][] xmlDeclarations() { 51 return new Object[][]{ 52 {"<?xml version=\"1.0\"?><root><test>t</test></root>"}, 53 {"<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><test>t</test></root>"}, 54 {"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone='yes'?><root><test>t</test></root>"}, 55 {"<?xml\n" 56 + " version=\"1.0\"?>\n" 57 + "<root>\n" 58 + " <test>t</test>\n" 59 + "</root>"}, 60 {"<?xml\n" 129 * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' 130 * [24] VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"') 131 * [25] Eq ::= S? '=' S? [26] VersionNum ::= '1.' [0-9]+ 132 * 133 * @param xml the test xml 134 * @throws Exception if the parser fails to parse the xml 135 */ 136 @Test(dataProvider = "xmlDeclarations") 137 public void test(String xml) throws Exception { 138 XMLInputFactory xif = XMLInputFactory.newDefaultFactory(); 139 XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml)); 140 while (xsr.hasNext()) { 141 xsr.next(); 142 } 143 } 144 145 /** 146 * @bug 8169450 147 * This particular issue does not appear in DOM parsing since the spaces are 148 * normalized during version detection. This test case then serves as a guard 149 * against such an issue from occurring in the version detection. 150 * 151 * @param xml the test xml 152 * @throws Exception if the parser fails to parse the xml 153 */ 154 @Test(dataProvider = "xmlDeclarations") 155 public void testWithDOM(String xml) throws Exception { 156 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 157 DocumentBuilder db = dbf.newDocumentBuilder(); 158 db.parse(new InputSource(new StringReader(xml))); 159 } 160 161 /** 162 * @bug 8222415 163 * Verifies that the parser is configured properly for UTF-16BE or LE. 164 * @throws Exception 165 */ 166 @Test 167 public void testEncoding() throws Exception { 168 ByteArrayInputStream bis = new ByteArrayInputStream( 169 "<?xml version=\"1.0\" encoding=\"UTF-16\"?> <a/>".getBytes("UnicodeLittle")); 170 InputSource is = new InputSource(bis); 171 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 172 DocumentBuilder db = dbf.newDocumentBuilder(); 173 174 Document doc = db.parse(is); 175 assertEquals("UTF-16LE", doc.getInputEncoding()); 176 } 177 } |