1 /*
   2  * Copyright (c) 2015, 2016, 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 
  24 package parsers;
  25 
  26 import static jaxp.library.JAXPTestUtilities.runWithAllPerm;
  27 import static org.testng.Assert.assertTrue;
  28 
  29 import java.io.StringReader;
  30 import java.util.Locale;
  31 
  32 import javax.xml.parsers.DocumentBuilder;
  33 import javax.xml.parsers.DocumentBuilderFactory;
  34 
  35 import org.testng.annotations.AfterClass;
  36 import org.testng.annotations.BeforeClass;
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Listeners;
  39 import org.testng.annotations.Test;
  40 import org.xml.sax.InputSource;
  41 import org.xml.sax.SAXException;
  42 
  43 /**
  44  * @test
  45  * @bug 8073385
  46  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  47  * @run testng/othervm -DrunSecMngr=true parsers.Bug8073385
  48  * @run testng/othervm parsers.Bug8073385
  49  * @summary test that invalid XML character exception string contains
  50  *     information about character value, element and attribute names
  51  */
  52 @Listeners({jaxp.library.BasePolicy.class})
  53 public class Bug8073385 {
  54 
  55     private Locale defLoc;
  56 
  57     @BeforeClass
  58     public void setup() {
  59         defLoc = Locale.getDefault();
  60         runWithAllPerm(() -> Locale.setDefault(Locale.ENGLISH));
  61     }
  62 
  63     @AfterClass
  64     public void cleanup() {
  65         runWithAllPerm(() -> Locale.setDefault(defLoc));
  66     }
  67 
  68     @DataProvider(name = "illegalCharactersData")
  69     public static Object[][] illegalCharactersData() {
  70         return new Object[][]{
  71             {0x00},
  72             {0xFFFE},
  73             {0xFFFF}
  74         };
  75     }
  76 
  77     @Test(dataProvider = "illegalCharactersData")
  78     public void test(int character) throws Exception {
  79         // Construct the XML document as a String
  80         int[] cps = new int[]{character};
  81         String txt = new String(cps, 0, cps.length);
  82         String inxml = "<topElement attTest=\'" + txt + "\'/>";
  83         String exceptionText = "NO EXCEPTION OBSERVED";
  84         String hexString = "0x" + Integer.toHexString(character);
  85 
  86         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  87         dbf.setNamespaceAware(true);
  88         dbf.setValidating(false);
  89         DocumentBuilder db = dbf.newDocumentBuilder();
  90         InputSource isrc = new InputSource(new StringReader(inxml));
  91 
  92         try {
  93             db.parse(isrc);
  94         } catch (SAXException e) {
  95             exceptionText = e.toString();
  96         }
  97         System.out.println("Got Exception:" + exceptionText);
  98         assertTrue(exceptionText.contains("attribute \"attTest\""));
  99         assertTrue(exceptionText.contains("element is \"topElement\""));
 100         assertTrue(exceptionText.contains("Unicode: " + hexString));
 101     }
 102 }
 103