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.util.PropertyPermission;
  32 
  33 import javax.xml.parsers.SAXParserFactory;
  34 
  35 import org.testng.Assert;
  36 import org.testng.annotations.Listeners;
  37 import org.testng.annotations.Test;
  38 import org.xml.sax.Attributes;
  39 import org.xml.sax.SAXException;
  40 import org.xml.sax.helpers.DefaultHandler;
  41 
  42 /*
  43  * @test
  44  * @bug 6341770
  45  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  46  * @run testng/othervm -DrunSecMngr=true parsers.Bug6341770
  47  * @run testng/othervm parsers.Bug6341770
  48  * @summary Test external entity linked to non-ASCII base URL.
  49  */
  50 @Listeners({jaxp.library.FilePolicy.class})
  51 public class Bug6341770 {
  52 
  53     // naming a file "aux" would fail on windows.
  54     @Test
  55     public void testNonAsciiURI() {
  56         try {
  57             File dir = new File("sko\u0159ice");
  58             dir.delete();
  59             dir.mkdir();
  60             File main = new File(dir, "main.xml");
  61             PrintWriter w = new PrintWriter(new FileWriter(main));
  62             w.println("<!DOCTYPE r [<!ENTITY aux SYSTEM \"aux1.xml\">]>");
  63             w.println("<r>&aux;</r>");
  64             w.flush();
  65             w.close();
  66             File aux = new File(dir, "aux1.xml");
  67             w = new PrintWriter(new FileWriter(aux));
  68             w.println("<x/>");
  69             w.flush();
  70             w.close();
  71             System.out.println("Parsing: " + main);
  72             tryRunWithTmpPermission(
  73                     () -> SAXParserFactory.newInstance().newSAXParser().parse(main, new DefaultHandler() {
  74                         public void startElement(String uri, String localname, String qname, Attributes attr)
  75                                 throws SAXException {
  76                             System.out.println("encountered <" + qname + ">");
  77                         }
  78                     }), new PropertyPermission("user.dir", "read"));
  79         } catch (Exception e) {
  80             e.printStackTrace();
  81             Assert.fail("Exception: " + e.getMessage());
  82         }
  83         System.out.println("OK.");
  84     }
  85 }
  86