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 }