Path: blob/master/test/jdk/javax/xml/jaxp/transform/8004476/XPathExFuncTest.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 XPath extension functions26* @run main/othervm -Djava.security.manager=allow XPathExFuncTest27*/28import java.io.FileInputStream;29import java.io.InputStream;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 java.util.Iterator;38import java.util.List;39import javax.xml.XMLConstants;40import javax.xml.namespace.NamespaceContext;41import javax.xml.namespace.QName;42import javax.xml.parsers.DocumentBuilder;43import javax.xml.parsers.DocumentBuilderFactory;44import javax.xml.xpath.XPath;45import javax.xml.xpath.XPathExpressionException;46import javax.xml.xpath.XPathFactory;47import javax.xml.xpath.XPathFactoryConfigurationException;48import javax.xml.xpath.XPathFunction;49import javax.xml.xpath.XPathFunctionException;50import javax.xml.xpath.XPathFunctionResolver;51import org.w3c.dom.Document;5253/**54* test XPath extension functions55*56* @author [email protected]57*/58public class XPathExFuncTest extends TestBase {5960final static String ENABLE_EXTENSION_FUNCTIONS = "http://www.oracle.com/xml/jaxp/properties/enableExtensionFunctions";61final static String CLASSNAME = "DocumentBuilderFactoryImpl";62final String XPATH_EXPRESSION = "ext:helloWorld()";6364/**65* Creates a new instance of StreamReader66*/67public XPathExFuncTest(String name) {68super(name);69}70boolean hasSM;71String xslFile, xslFileId;72String xmlFile, xmlFileId;7374protected void setUp() {75super.setUp();76xmlFile = filepath + "/SecureProcessingTest.xml";7778}7980/**81* @param args the command line arguments82*/83public static void main(String[] args) {84XPathExFuncTest test = new XPathExFuncTest("OneTest");85test.setUp();8687test.testExtFunc();88test.testExtFuncNotAllowed();89test.testEnableExtFunc();90test.tearDown();9192}9394/**95* by default, extension function is enabled96*/97public void testExtFunc() {9899try {100evaluate(false);101System.out.println("testExtFunc: OK");102} catch (XPathFactoryConfigurationException e) {103fail(e.getMessage());104} catch (XPathExpressionException e) {105fail(e.getMessage());106}107}108109/**110* Security is enabled, extension function not allowed111*/112public void testExtFuncNotAllowed() {113Policy p = new SimplePolicy(new AllPermission());114Policy.setPolicy(p);115System.setSecurityManager(new SecurityManager());116117try {118evaluate(false);119} catch (XPathFactoryConfigurationException e) {120fail(e.getMessage());121} catch (XPathExpressionException ex) {122//expected since extension function is disallowed123System.out.println("testExtFuncNotAllowed: OK");124} finally {125System.setSecurityManager(null);126}127}128129/**130* Security is enabled, use new feature: enableExtensionFunctions131*/132public void testEnableExtFunc() {133Policy p = new SimplePolicy(new AllPermission());134Policy.setPolicy(p);135System.setSecurityManager(new SecurityManager());136137138try {139evaluate(true);140System.out.println("testEnableExt: OK");141} catch (XPathFactoryConfigurationException e) {142fail(e.getMessage());143} catch (XPathExpressionException e) {144fail(e.getMessage());145} finally {146System.setSecurityManager(null);147}148}149150Document getDocument() {151// the xml source152DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();153DocumentBuilder documentBuilder = null;154Document document = null;155156try {157documentBuilder = documentBuilderFactory.newDocumentBuilder();158InputStream xmlStream = new FileInputStream(xmlFile);159document = documentBuilder.parse(xmlStream);160} catch (Exception e) {161fail(e.toString());162}163return document;164}165166void evaluate(boolean enableExt) throws XPathFactoryConfigurationException, XPathExpressionException {167Document document = getDocument();168169XPathFactory xPathFactory = XPathFactory.newInstance();170/**171* Use of the extension function 'http://exslt.org/strings:tokenize' is172* not allowed when the secure processing feature is set to true.173* Attempt to use the new property to enable extension function174*/175if (enableExt) {176boolean isExtensionSupported = enableExtensionFunction(xPathFactory);177}178179xPathFactory.setXPathFunctionResolver(new MyXPathFunctionResolver());180if (System.getSecurityManager() == null) {181xPathFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);182}183184XPath xPath = xPathFactory.newXPath();185xPath.setNamespaceContext(new MyNamespaceContext());186187String xPathResult = xPath.evaluate(XPATH_EXPRESSION, document);188System.out.println(189"XPath result (enableExtensionFunction == " + enableExt + ") = \""190+ xPathResult191+ "\"");192}193194public class MyXPathFunctionResolver195implements XPathFunctionResolver {196197public XPathFunction resolveFunction(QName functionName, int arity) {198199// not a real ewsolver, always return a default XPathFunction200return new MyXPathFunction();201}202}203204public class MyXPathFunction205implements XPathFunction {206207public Object evaluate(List list) throws XPathFunctionException {208209return "Hello World";210}211}212213public class MyNamespaceContext implements NamespaceContext {214215public String getNamespaceURI(String prefix) {216if (prefix == null) {217throw new IllegalArgumentException("The prefix cannot be null.");218}219220if (prefix.equals("ext")) {221return "http://ext.com";222} else {223return null;224}225}226227public String getPrefix(String namespace) {228229if (namespace == null) {230throw new IllegalArgumentException("The namespace uri cannot be null.");231}232233if (namespace.equals("http://ext.com")) {234return "ext";235} else {236return null;237}238}239240public Iterator getPrefixes(String namespace) {241return null;242}243}244245boolean enableExtensionFunction(XPathFactory factory) {246boolean isSupported = true;247try {248factory.setFeature(ENABLE_EXTENSION_FUNCTIONS, true);249} catch (XPathFactoryConfigurationException ex) {250isSupported = false;251}252return isSupported;253}254255class SimplePolicy extends Policy {256257private final Permissions perms;258259public SimplePolicy(Permission... permissions) {260perms = new Permissions();261for (Permission permission : permissions) {262perms.add(permission);263}264}265266@Override267public PermissionCollection getPermissions(CodeSource cs) {268return perms;269}270271@Override272public PermissionCollection getPermissions(ProtectionDomain pd) {273return perms;274}275276@Override277public boolean implies(ProtectionDomain pd, Permission p) {278return perms.implies(p);279}280281//for older jdk282@Override283public void refresh() {284}285}286}287288289