1 /*
   2  * Copyright (c) 2014, 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.tryRunWithTmpPermission;
  27 
  28 import java.io.File;
  29 import java.io.FileWriter;
  30 import java.io.PrintWriter;
  31 import java.nio.file.Paths;
  32 import java.util.PropertyPermission;
  33 
  34 import javax.xml.parsers.SAXParserFactory;
  35 
  36 import org.testng.Assert;
  37 import org.testng.annotations.Listeners;
  38 import org.testng.annotations.Test;
  39 import org.xml.sax.Attributes;
  40 import org.xml.sax.SAXException;
  41 import org.xml.sax.helpers.DefaultHandler;
  42 
  43 /*
  44  * @test
  45  * @bug 6341770
  46  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  47  * @run testng/othervm -DrunSecMngr=true parsers.Bug6341770
  48  * @run testng/othervm parsers.Bug6341770
  49  * @summary Test external entity linked to non-ASCII base URL.
  50  */
  51 @Listeners({jaxp.library.FilePolicy.class})
  52 public class Bug6341770 {
  53 
  54     // naming a file "aux" would fail on windows.
  55     @Test
  56     public void testNonAsciiURI() {
  57         if (!isNonAsciiSupported()) {
  58             // @bug 8167478
  59             // if it doesn't support non-ascii, the following test is invalid even if test is passed.
  60             System.out.println("Current environment doesn't support non-ascii, exit the test.");
  61             return;
  62         }
  63         try {
  64             File dir = new File(ALPHA);
  65             dir.delete();
  66             dir.mkdir();
  67             File main = new File(dir, "main.xml");
  68             PrintWriter w = new PrintWriter(new FileWriter(main));
  69             w.println("<!DOCTYPE r [<!ENTITY aux SYSTEM \"aux1.xml\">]>");
  70             w.println("<r>&aux;</r>");
  71             w.flush();
  72             w.close();
  73             File aux = new File(dir, "aux1.xml");
  74             w = new PrintWriter(new FileWriter(aux));
  75             w.println("<x/>");
  76             w.flush();
  77             w.close();
  78             System.out.println("Parsing: " + main);
  79             tryRunWithTmpPermission(
  80                     () -> SAXParserFactory.newInstance().newSAXParser().parse(main, new DefaultHandler() {
  81                         public void startElement(String uri, String localname, String qname, Attributes attr)
  82                                 throws SAXException {
  83                             System.out.println("encountered <" + qname + ">");
  84                         }
  85                     }), new PropertyPermission("user.dir", "read"));
  86         } catch (Exception e) {
  87             e.printStackTrace();
  88             Assert.fail("Exception: " + e.getMessage());
  89         }
  90         System.out.println("OK.");
  91     }
  92 
  93     private boolean isNonAsciiSupported() {
  94         // Use Paths.get method to test if the path is valid in current environment
  95         try {
  96             Paths.get(ALPHA);
  97             return true;
  98         } catch (Exception e) {
  99             return false;
 100         }
 101     }
 102 
 103     // Select alpha because it's a very common non-ascii character in different charsets.
 104     // That this test can run in as many as possible environments if it's possible.
 105     private static final String ALPHA = "\u03b1";
 106 }