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" 61 + " version=\"1.0\"\n" 62 + " encoding=\"UTF-8\"?>\n" 63 + "<root>\n" 64 + " <test>t</test>\n" 65 + "</root>"}, 66 {"<?xml\n" 67 + " version=\"1.0\"\n" 68 + " encoding=\"UTF-8\"\n" 69 + " standalone=\"yes\"?>\n" 70 + "<root>\n" 71 + " <test>t</test>\n" 72 + "</root>"}, 73 {"<?xml\n" 74 + " version\n" 75 + "=\n" 76 + "\"1.0\"\n" 77 + " encoding\n" 78 + "=\n" 79 + "\"UTF-8\"\n" 80 + " standalone\n" 81 + "=\n" 82 + "\"yes\"?>\n" 83 + "<root>\n" 84 + " <test>t</test>\n" 85 + "</root>"}, 86 {"<?xml version=\"1.1\"?><root><test>t</test></root>"}, 87 {"<?xml version=\"1.1\" encoding=\"UTF-8\"?><root><test>t</test></root>"}, 88 {"<?xml version=\"1.1\" encoding=\"UTF-8\" standalone='yes'?><root><test>t</test></root>"}, 89 {"<?xml\n" 90 + " version=\"1.1\"?>\n" 91 + "<root>\n" 92 + " <test>t</test>\n" 93 + "</root>"}, 94 {"<?xml\n" 95 + " version=\"1.1\"\n" 96 + " encoding=\"UTF-8\"?>\n" 97 + "<root>\n" 98 + " <test>t</test>\n" 99 + "</root>"}, 100 {"<?xml\n" 101 + " version=\"1.1\"\n" 102 + " encoding=\"UTF-8\"\n" 103 + " standalone=\"yes\"?>\n" 104 + "<root>\n" 105 + " <test>t</test>\n" 106 + "</root>"}, 107 {"<?xml\n" 108 + " version\n" 109 + "=\n" 110 + "\"1.1\"\n" 111 + " encoding\n" 112 + "=\n" 113 + "\"UTF-8\"\n" 114 + " standalone\n" 115 + "=\n" 116 + "\"yes\"?>\n" 117 + "<root>\n" 118 + " <test>t</test>\n" 119 + "</root>"} 120 }; 121 } 122 123 /** 124 * @bug 8169450 125 * Verifies that the parser successfully parses the declarations provided in 126 * xmlDeclarations. Exception would otherwise be thrown as reported in 8169450. 127 * 128 * XML Declaration according to https://www.w3.org/TR/REC-xml/#NT-XMLDecl 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 }