< prev index next >

test/jaxp/javax/xml/jaxp/unittest/transform/CR6551600Test.java

Print this page


   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 transform;
  25 
  26 import static jaxp.library.JAXPTestUtilities.getSystemProperty;
  27 
  28 import java.io.File;
  29 import java.io.FilePermission;



  30 
  31 import javax.xml.parsers.DocumentBuilder;
  32 import javax.xml.parsers.DocumentBuilderFactory;
  33 import javax.xml.transform.Transformer;
  34 import javax.xml.transform.TransformerFactory;
  35 import javax.xml.transform.dom.DOMSource;
  36 import javax.xml.transform.stream.StreamResult;
  37 
  38 import jaxp.library.JAXPTestUtilities;
  39 
  40 import org.testng.Assert;
  41 import org.testng.annotations.Listeners;
  42 import org.testng.annotations.Test;
  43 import org.w3c.dom.Document;
  44 import org.w3c.dom.Element;
  45 


  46 /*
  47  * @test
  48  * @bug 6551600

  49  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  50  * @run testng/othervm -DrunSecMngr=true transform.CR6551600Test
  51  * @run testng/othervm transform.CR6551600Test
  52  * @summary Test using UNC path as StreamResult.
  53  */
  54 @Listeners({ jaxp.library.BasePolicy.class })
  55 public class CR6551600Test {
  56 
  57     @Test
  58     public final void testUNCPath() {
  59         boolean isWindows = getSystemProperty("os.name").contains("Windows");
  60         JAXPTestUtilities.runWithTmpPermission(() -> {
  61             String hostName = "";
  62             try {
  63                 hostName = java.net.InetAddress.getLocalHost().getHostName();
  64             } catch (java.net.UnknownHostException e) {
  65                 // falls through
  66             }
  67 
  68             String path = isWindows ? "\\\\" + hostName + "\\C$\\xslt_unc_test.xml" : "///tmp/test.xml";





  69 


  70             try {
  71                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  72                 DocumentBuilder builder = factory.newDocumentBuilder();
  73                 Document doc = builder.newDocument();
  74                 Element root = doc.createElement("test");
  75                 doc.appendChild(root);
  76                 // create an identity transform
  77                 Transformer t = TransformerFactory.newInstance().newTransformer();
  78                 File f = new File(path);
  79                 StreamResult result = new StreamResult(f);
  80                 DOMSource source = new DOMSource(doc);
  81                 System.out.println("Writing to " + f);
  82                 t.transform(source, result);
  83             } catch (Exception e) {
  84                 // unexpected failure
  85                 e.printStackTrace();
  86                 Assert.fail(e.toString());
  87             }
  88 
  89             File file = new File(path);
  90             if (file.exists()) {
  91                 file.deleteOnExit();
  92             }
  93         }, isWindows ? new FilePermission("//localhost/C$/xslt_unc_test.xml", "read,write,delete")
  94                 : new FilePermission("///tmp/test.xml", "read,write,delete"));













  95     }
  96 }
   1 /*
   2  * Copyright (c) 2014, 2018, 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 transform;
  25 


  26 import java.io.File;
  27 import java.io.FilePermission;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 
  32 import javax.xml.parsers.DocumentBuilder;
  33 import javax.xml.parsers.DocumentBuilderFactory;
  34 import javax.xml.transform.Transformer;
  35 import javax.xml.transform.TransformerFactory;
  36 import javax.xml.transform.dom.DOMSource;
  37 import javax.xml.transform.stream.StreamResult;
  38 


  39 import org.testng.Assert;
  40 import org.testng.annotations.Listeners;
  41 import org.testng.annotations.Test;
  42 import org.w3c.dom.Document;
  43 import org.w3c.dom.Element;
  44 
  45 import jaxp.library.JAXPTestUtilities;
  46 
  47 /*
  48  * @test
  49  * @bug 6551600
  50  * @requires os.family == "windows"
  51  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  52  * @run testng/othervm -DrunSecMngr=true transform.CR6551600Test
  53  * @run testng/othervm transform.CR6551600Test
  54  * @summary Test using UNC path as StreamResult.
  55  */
  56 @Listeners({ jaxp.library.BasePolicy.class })
  57 public class CR6551600Test {
  58 
  59     @Test
  60     public final void testUNCPath() {
  61         var hostName = "";


  62         try {
  63             hostName = java.net.InetAddress.getLocalHost().getHostName();
  64         } catch (java.net.UnknownHostException e) {
  65             // falls through
  66         }
  67 
  68         var uncPath = "\\\\" + hostName + "\\C$\\temp\\";
  69 
  70         if (!checkAccess(uncPath)) {
  71             System.out.println("Cannot access UNC path. Test exits.");
  72             return;
  73         }
  74 
  75         var uncFilePath = uncPath + "xslt_unc_test.xml";
  76         JAXPTestUtilities.runWithTmpPermission(() -> {
  77             try {
  78                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  79                 DocumentBuilder builder = factory.newDocumentBuilder();
  80                 Document doc = builder.newDocument();
  81                 Element root = doc.createElement("test");
  82                 doc.appendChild(root);
  83                 // create an identity transform
  84                 Transformer t = TransformerFactory.newInstance().newTransformer();
  85                 File f = new File(uncFilePath);
  86                 StreamResult result = new StreamResult(f);
  87                 DOMSource source = new DOMSource(doc);
  88                 System.out.println("Writing to " + f);
  89                 t.transform(source, result);
  90             } catch (Exception e) {
  91                 // unexpected failure
  92                 e.printStackTrace();
  93                 Assert.fail(e.toString());
  94             }
  95 
  96             File file = new File(uncFilePath);
  97             if (file.exists()) {
  98                 file.deleteOnExit();
  99             }
 100         }, new FilePermission(uncFilePath, "read,write,delete"));
 101     }
 102 
 103     private boolean checkAccess(String path) {
 104         return JAXPTestUtilities.runWithTmpPermission(() -> {
 105             try {
 106                 Path tmepFile = Files.createTempFile(Paths.get(path), "test", "6551600");
 107                 Files.deleteIfExists(tmepFile);
 108                 return true;
 109             } catch (Exception e) {
 110                 System.out.println("Access check failed.");
 111                 e.printStackTrace();
 112                 return false;
 113             }
 114         }, new FilePermission(path + "*", "read,write,delete"));
 115     }
 116 }
< prev index next >