Path: blob/master/test/jdk/javax/xml/jaxp/transform/8004476/XSLTExFuncTest.java
41154 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/**23* @test24* @bug 800447625* @summary test XSLT extension functions26* @run main/othervm -Djava.security.manager=allow XSLTExFuncTest27*/2829import java.io.StringWriter;30import java.security.AllPermission;31import java.security.CodeSource;32import java.security.Permission;33import java.security.PermissionCollection;34import java.security.Permissions;35import java.security.Policy;36import java.security.ProtectionDomain;37import javax.xml.transform.*;38import javax.xml.transform.sax.SAXSource;39import javax.xml.transform.stream.StreamResult;40import org.xml.sax.InputSource;4142/**43* test XSLT extension functions44*45* @author [email protected]46*/47public class XSLTExFuncTest extends TestBase {4849final static String ENABLE_EXTENSION_FUNCTIONS = "http://www.oracle.com/xml/jaxp/properties/enableExtensionFunctions";50final static String CLASSNAME = "DocumentBuilderFactoryImpl";5152/**53* Creates a new instance of StreamReader54*/55public XSLTExFuncTest(String name) {56super(name);57}58boolean hasSM;59String xslFile, xslFileId;60String xmlFile, xmlFileId;6162protected void setUp() {63super.setUp();64xmlFile = filepath + "/tokenize.xml";65xslFile = filepath + "/tokenize.xsl";6667/**68* On Windows platform it needs triple '/' for valid URL while double '/' is enough on Linux or Solaris.69* Here use file:/// directly to make it work on Windows and it will not impact other platforms.70*/71xslFileId = "file:///" + xslFile;72}7374/**75* @param args the command line arguments76*/77public static void main(String[] args) {78XSLTExFuncTest test = new XSLTExFuncTest("OneTest");79test.setUp();8081test.testExtFunc();82test.testExtFuncNotAllowed();83test.testEnableExtFunc();84test.testTemplatesEnableExtFunc();85test.tearDown();8687}8889/**90* by default, extension function is enabled91*/92public void testExtFunc() {93TransformerFactory factory = TransformerFactory.newInstance();9495try {96transform(factory);97System.out.println("testExtFunc: OK");98} catch (TransformerConfigurationException e) {99fail(e.getMessage());100} catch (TransformerException ex) {101fail(ex.getMessage());102}103}104105/**106* Security is enabled, extension function not allowed107*/108public void testExtFuncNotAllowed() {109Policy p = new SimplePolicy(new AllPermission());110Policy.setPolicy(p);111System.setSecurityManager(new SecurityManager());112TransformerFactory factory = TransformerFactory.newInstance();113114try {115transform(factory);116} catch (TransformerConfigurationException e) {117fail(e.getMessage());118} catch (TransformerException ex) {119//expected since extension function is disallowed120System.out.println("testExtFuncNotAllowed: OK");121} finally {122System.setSecurityManager(null);123}124}125126/**127* Security is enabled, use new feature: enableExtensionFunctions128*/129public void testEnableExtFunc() {130Policy p = new SimplePolicy(new AllPermission());131Policy.setPolicy(p);132System.setSecurityManager(new SecurityManager());133TransformerFactory factory = TransformerFactory.newInstance();134135/**136* Use of the extension function 'http://exslt.org/strings:tokenize' is137* not allowed when the secure processing feature is set to true.138* Attempt to use the new property to enable extension function139*/140boolean isExtensionSupported = enableExtensionFunction(factory);141142try {143transform(factory);144System.out.println("testEnableExt: OK");145} catch (TransformerConfigurationException e) {146fail(e.getMessage());147} catch (TransformerException e) {148fail(e.getMessage());149} finally {150System.setSecurityManager(null);151}152}153154/**155* use Templates template = factory.newTemplates(new StreamSource( new156* FileInputStream(xslFilename))); // Use the template to create a157* transformer Transformer xformer = template.newTransformer();158*159* @param factory160* @return161*/162/**163* Security is enabled, use new feature: enableExtensionFunctions Use the164* template to create a transformer165*/166public void testTemplatesEnableExtFunc() {167Policy p = new SimplePolicy(new AllPermission());168Policy.setPolicy(p);169System.setSecurityManager(new SecurityManager());170TransformerFactory factory = TransformerFactory.newInstance();171172/**173* Use of the extension function 'http://exslt.org/strings:tokenize' is174* not allowed when the secure processing feature is set to true.175* Attempt to use the new property to enable extension function176*/177boolean isExtensionSupported = enableExtensionFunction(factory);178179try {180SAXSource xslSource = new SAXSource(new InputSource(xslFile));181xslSource.setSystemId(xslFileId);182Templates template = factory.newTemplates(xslSource);183Transformer transformer = template.newTransformer();184StringWriter stringResult = new StringWriter();185Result result = new StreamResult(stringResult);186transformer.transform(new SAXSource(new InputSource(xmlFile)), result);187System.out.println("testTemplatesEnableExtFunc: OK");188} catch (TransformerConfigurationException e) {189fail(e.getMessage());190} catch (TransformerException e) {191fail(e.getMessage());192} finally {193System.setSecurityManager(null);194}195}196197boolean enableExtensionFunction(TransformerFactory factory) {198boolean isSupported = true;199try {200factory.setFeature(ENABLE_EXTENSION_FUNCTIONS, true);201} catch (TransformerConfigurationException ex) {202isSupported = false;203}204return isSupported;205}206207void transform(TransformerFactory factory) throws TransformerConfigurationException, TransformerException {208SAXSource xslSource = new SAXSource(new InputSource(xslFile));209xslSource.setSystemId(xslFileId);210Transformer transformer = factory.newTransformer(xslSource);211StringWriter stringResult = new StringWriter();212Result result = new StreamResult(stringResult);213transformer.transform(new SAXSource(new InputSource(xmlFile)), result);214}215216class SimplePolicy extends Policy {217218private final Permissions perms;219220public SimplePolicy(Permission... permissions) {221perms = new Permissions();222for (Permission permission : permissions) {223perms.add(permission);224}225}226227@Override228public PermissionCollection getPermissions(CodeSource cs) {229return perms;230}231232@Override233public PermissionCollection getPermissions(ProtectionDomain pd) {234return perms;235}236237@Override238public boolean implies(ProtectionDomain pd, Permission p) {239return perms.implies(p);240}241242//for older jdk243@Override244public void refresh() {245}246}247}248249250