1 /*
   2  * Copyright (c) 2015, 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 java.io.StringReader;
  27 import java.io.StringWriter;
  28 
  29 import javax.xml.transform.Source;
  30 import javax.xml.transform.Transformer;
  31 import javax.xml.transform.TransformerException;
  32 import javax.xml.transform.TransformerFactory;
  33 import javax.xml.transform.URIResolver;
  34 import javax.xml.transform.stream.StreamResult;
  35 import javax.xml.transform.stream.StreamSource;
  36 
  37 import org.testng.Assert;
  38 import org.testng.annotations.DataProvider;
  39 import org.testng.annotations.Listeners;
  40 import org.testng.annotations.Test;
  41 
  42 import static org.testng.Assert.assertEquals;
  43 import static jaxp.library.JAXPTestUtilities.runWithAllPerm;
  44 import static jaxp.library.JAXPTestUtilities.clearSystemProperty;
  45 import static jaxp.library.JAXPTestUtilities.setSystemProperty;
  46 import static jaxp.library.JAXPTestUtilities.getSystemProperty;
  47 
  48 /*
  49  * @test
  50  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  51  * @compile DocumentExtFunc.java
  52  * @run testng/othervm -DrunSecMngr=true transform.XSLTFunctionsTest
  53  * @run testng/othervm transform.XSLTFunctionsTest
  54  * @summary This class contains tests for XSLT functions.
  55  */
  56 
  57 @Listeners({jaxp.library.BasePolicy.class}) 
  58 public class XSLTFunctionsTest {
  59   
  60     /**
  61      * @bug 8161454
  62      * Verifies that the new / correct name is supported, as is the old / incorrect
  63      * one for compatibility
  64      */ 
  65     @Test 
  66     public void testNameChange() {
  67         
  68         boolean feature;
  69         TransformerFactory tf = TransformerFactory.newInstance();
  70         feature = tf.getFeature(ORACLE_ENABLE_EXTENSION_FUNCTION);
  71         System.out.println("Default setting: " + feature);
  72         // The default: true if no SecurityManager, false otherwise
  73         Assert.assertTrue(feature == getDefault());
  74         
  75         setSystemProperty(SP_ENABLE_EXTENSION_FUNCTION, getDefaultOpposite());
  76         tf = TransformerFactory.newInstance();
  77         feature = tf.getFeature(ORACLE_ENABLE_EXTENSION_FUNCTION);
  78         System.out.println("After setting " + SP_ENABLE_EXTENSION_FUNCTION + ": " + feature);
  79         clearSystemProperty(SP_ENABLE_EXTENSION_FUNCTION);
  80         // old/incorrect name is still supported
  81         Assert.assertTrue(feature != getDefault());
  82         
  83         setSystemProperty(SP_ENABLE_EXTENSION_FUNCTION_SPEC, getDefaultOpposite());
  84         tf = TransformerFactory.newInstance();
  85         feature = tf.getFeature(ORACLE_ENABLE_EXTENSION_FUNCTION);
  86         System.out.println("After setting " + SP_ENABLE_EXTENSION_FUNCTION_SPEC + ": " + feature);
  87         clearSystemProperty(SP_ENABLE_EXTENSION_FUNCTION_SPEC);
  88         // new/correct name is effective
  89         Assert.assertTrue(feature != getDefault());
  90     }
  91     
  92     final boolean isSecure;
  93     {
  94         String runSecMngr = getSystemProperty("runSecMngr");
  95         isSecure = runSecMngr != null && runSecMngr.equals("true");
  96     }
  97 
  98     // The default: true if no SecurityManager, false otherwise  
  99     private boolean getDefault() {
 100         if (isSecure) {
 101             return false;
 102         } else { 
 103             return true;
 104         }
 105     }
 106 
 107     // Gets a String value that is opposite to the default value
 108     private String getDefaultOpposite() {
 109         if (isSecure) {
 110             return "true";
 111         } else {
 112             return "false";
 113         }
 114     }
 115 
 116     /**
 117      * @bug 8062518 8153082
 118      * Verifies that a reference to the DTM created by XSLT document function is
 119      * actually read from the DTM by an extension function.
 120      * @param xml Content of xml file to process
 121      * @param xsl stylesheet content that loads external document {@code externalDoc}
 122      *        with XSLT 'document' function and then reads it with
 123      *        DocumentExtFunc.test() function
 124      * @param externalDoc Content of the external xml document
 125      * @param expectedResult Expected transformation result
 126      **/
 127     @Test(dataProvider = "document")
 128     public void testDocument(final String xml, final String xsl,
 129                              final String externalDoc, final String expectedResult) throws Exception {
 130         // Prepare sources for transormation
 131         Source src = new StreamSource(new StringReader(xml));
 132         Source xslsrc = new StreamSource(new StringReader(xsl));
 133 
 134         // Create factory and transformer
 135         TransformerFactory tf = TransformerFactory.newInstance();
 136         tf.setFeature(ORACLE_ENABLE_EXTENSION_FUNCTION, true);
 137         tf.setAttribute(EXTENSION_CLASS_LOADER, 
 138                 runWithAllPerm(() -> Thread.currentThread().getContextClassLoader()));
 139         Transformer t = tf.newTransformer( xslsrc );
 140         t.setErrorListener(tf.getErrorListener());
 141 
 142         // Set URI Resolver to return the newly constructed xml
 143         // stream source object from xml test string
 144         t.setURIResolver(new URIResolver() {
 145             @Override
 146             public Source resolve(String href, String base)
 147                     throws TransformerException {
 148                 if (href.contains("externalDoc")) {
 149                     return new StreamSource(new StringReader(externalDoc));
 150                 } else {
 151                     return new StreamSource(new StringReader(xml));
 152                 }
 153             }
 154         });
 155 
 156         // Prepare output stream
 157         StringWriter xmlResultString = new StringWriter();
 158         StreamResult xmlResultStream = new StreamResult(xmlResultString);
 159 
 160         //Transform the xml
 161         t.transform(src, xmlResultStream);
 162 
 163         // If the document can't be accessed and the bug is in place then
 164         // reported exception will be thrown during transformation
 165         System.out.println("Transformation result:"+xmlResultString.toString().trim());
 166 
 167         // Check the result - it should contain two (node name, node values) entries -
 168         // one for original document, another for a document created with
 169         // call to 'document' function
 170         assertEquals(xmlResultString.toString().trim(), expectedResult);
 171     }
 172 
 173     @DataProvider(name = "document")
 174     public static Object[][] documentTestData() {
 175         return new Object[][] {
 176             {documentTestXml, documentTestXsl, documentTestExternalDoc, documentTesteExpectedResult},
 177         };
 178     }
 179 
 180     static final String documentTestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>Doc</Test>";
 181 
 182     static final String documentTestExternalDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>External Doc</Test>";
 183 
 184     static final String documentTestXsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 185             + "<xsl:transform version=\"1.0\""
 186             + " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" "
 187             + " xmlns:cfunc=\"http://xml.apache.org/xalan/java/\">"
 188             + "<xsl:template match=\"/\">"
 189             + "<xsl:element name=\"root\">"
 190             + "<xsl:variable name=\"other_doc\" select=\"document('externalDoc')\"/>"
 191             + "<!-- Source -->"
 192             + "<xsl:value-of select=\"cfunc:transform.DocumentExtFunc.test(/Test)\"/>"
 193             + "<!-- document() -->"
 194             + "<xsl:value-of select=\"cfunc:transform.DocumentExtFunc.test($other_doc/Test)\"/>"
 195             + "</xsl:element></xsl:template></xsl:transform>";
 196 
 197     static final String documentTesteExpectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 198                                                     + "<root>[Test:Doc][Test:External Doc]</root>";
 199 
 200     public static final String ORACLE_JAXP_PROPERTY_PREFIX =
 201         "http://www.oracle.com/xml/jaxp/properties/";
 202     /**
 203      * Feature enableExtensionFunctions
 204      */
 205     public static final String ORACLE_ENABLE_EXTENSION_FUNCTION =
 206             ORACLE_JAXP_PROPERTY_PREFIX + "enableExtensionFunctions";
 207     static final String SP_ENABLE_EXTENSION_FUNCTION = "javax.xml.enableExtensionFunctions";
 208     // This is the correct name by the spec
 209     static final String SP_ENABLE_EXTENSION_FUNCTION_SPEC = "jdk.xml.enableExtensionFunctions";
 210     private static final String EXTENSION_CLASS_LOADER = "jdk.xml.transform.extensionClassLoader";
 211 }
 212