< prev index next >

test/javax/xml/jaxp/unittest/parsers/Bug6341770.java

Print this page




  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 }


  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.charset.Charset;
  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         // if equals method returns true, the non-ascii character information was not lost in this environment
  95         return alpha.equals(Charset.defaultCharset().decode(Charset.defaultCharset().encode(alpha)).toString());
  96     }
  97 
  98     // Select alpha because it's a very common non-ascii character in different charsets.
  99     // That this test can run in as many as possible environments if it's possible.
 100     private static final String alpha = "\u03b1";
 101 }
< prev index next >