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