author | iignatyev |
Tue, 16 Dec 2014 02:14:11 +0300 | |
changeset 28198 | 1e41fbf783e3 |
parent 21849 | 3039fc1ed457 |
permissions | -rw-r--r-- |
21364 | 1 |
/* |
2 |
* Copyright (c) 2013, 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 |
* @test |
|
25 |
* @bug 8004476 |
|
26 |
* @summary test XSLT extension functions |
|
27 |
* @run main/othervm XSLTExFuncTest |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.StringWriter; |
|
31 |
import java.security.AllPermission; |
|
32 |
import java.security.CodeSource; |
|
33 |
import java.security.Permission; |
|
34 |
import java.security.PermissionCollection; |
|
35 |
import java.security.Permissions; |
|
36 |
import java.security.Policy; |
|
37 |
import java.security.ProtectionDomain; |
|
38 |
import javax.xml.transform.*; |
|
39 |
import javax.xml.transform.sax.SAXSource; |
|
40 |
import javax.xml.transform.stream.StreamResult; |
|
41 |
import org.xml.sax.InputSource; |
|
42 |
||
43 |
/** |
|
44 |
* test XSLT extension functions |
|
45 |
* |
|
46 |
* @author huizhe.wang@oracle.com |
|
47 |
*/ |
|
48 |
public class XSLTExFuncTest extends TestBase { |
|
49 |
||
50 |
final static String ENABLE_EXTENSION_FUNCTIONS = "http://www.oracle.com/xml/jaxp/properties/enableExtensionFunctions"; |
|
51 |
final static String CLASSNAME = "DocumentBuilderFactoryImpl"; |
|
52 |
||
53 |
/** |
|
54 |
* Creates a new instance of StreamReader |
|
55 |
*/ |
|
56 |
public XSLTExFuncTest(String name) { |
|
57 |
super(name); |
|
58 |
} |
|
59 |
boolean hasSM; |
|
60 |
String xslFile, xslFileId; |
|
61 |
String xmlFile, xmlFileId; |
|
62 |
||
63 |
protected void setUp() { |
|
64 |
super.setUp(); |
|
65 |
xmlFile = filepath + "/tokenize.xml"; |
|
66 |
xslFile = filepath + "/tokenize.xsl"; |
|
67 |
||
68 |
/** |
|
21849
3039fc1ed457
8027973: javax/xml/jaxp/transform/jdk8004476/XSLTExFuncTest.java hangs (win)
joehw
parents:
21364
diff
changeset
|
69 |
* On Windows platform it needs triple '/' for valid URL while double '/' is enough on Linux or Solaris. |
3039fc1ed457
8027973: javax/xml/jaxp/transform/jdk8004476/XSLTExFuncTest.java hangs (win)
joehw
parents:
21364
diff
changeset
|
70 |
* Here use file:/// directly to make it work on Windows and it will not impact other platforms. |
21364 | 71 |
*/ |
21849
3039fc1ed457
8027973: javax/xml/jaxp/transform/jdk8004476/XSLTExFuncTest.java hangs (win)
joehw
parents:
21364
diff
changeset
|
72 |
xslFileId = "file:///" + xslFile; |
21364 | 73 |
} |
74 |
||
75 |
/** |
|
76 |
* @param args the command line arguments |
|
77 |
*/ |
|
78 |
public static void main(String[] args) { |
|
79 |
XSLTExFuncTest test = new XSLTExFuncTest("OneTest"); |
|
80 |
test.setUp(); |
|
81 |
||
82 |
test.testExtFunc(); |
|
83 |
test.testExtFuncNotAllowed(); |
|
84 |
test.testEnableExtFunc(); |
|
85 |
test.testTemplatesEnableExtFunc(); |
|
86 |
test.tearDown(); |
|
87 |
||
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* by default, extension function is enabled |
|
92 |
*/ |
|
93 |
public void testExtFunc() { |
|
94 |
TransformerFactory factory = TransformerFactory.newInstance(); |
|
95 |
||
96 |
try { |
|
97 |
transform(factory); |
|
98 |
System.out.println("testExtFunc: OK"); |
|
99 |
} catch (TransformerConfigurationException e) { |
|
100 |
fail(e.getMessage()); |
|
101 |
} catch (TransformerException ex) { |
|
102 |
fail(ex.getMessage()); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
/** |
|
107 |
* Security is enabled, extension function not allowed |
|
108 |
*/ |
|
109 |
public void testExtFuncNotAllowed() { |
|
110 |
Policy p = new SimplePolicy(new AllPermission()); |
|
111 |
Policy.setPolicy(p); |
|
112 |
System.setSecurityManager(new SecurityManager()); |
|
113 |
TransformerFactory factory = TransformerFactory.newInstance(); |
|
114 |
||
115 |
try { |
|
116 |
transform(factory); |
|
117 |
} catch (TransformerConfigurationException e) { |
|
118 |
fail(e.getMessage()); |
|
119 |
} catch (TransformerException ex) { |
|
120 |
//expected since extension function is disallowed |
|
121 |
System.out.println("testExtFuncNotAllowed: OK"); |
|
122 |
} finally { |
|
123 |
System.setSecurityManager(null); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Security is enabled, use new feature: enableExtensionFunctions |
|
129 |
*/ |
|
130 |
public void testEnableExtFunc() { |
|
131 |
Policy p = new SimplePolicy(new AllPermission()); |
|
132 |
Policy.setPolicy(p); |
|
133 |
System.setSecurityManager(new SecurityManager()); |
|
134 |
TransformerFactory factory = TransformerFactory.newInstance(); |
|
135 |
||
136 |
/** |
|
137 |
* Use of the extension function 'http://exslt.org/strings:tokenize' is |
|
138 |
* not allowed when the secure processing feature is set to true. |
|
139 |
* Attempt to use the new property to enable extension function |
|
140 |
*/ |
|
141 |
boolean isExtensionSupported = enableExtensionFunction(factory); |
|
142 |
||
143 |
try { |
|
144 |
transform(factory); |
|
145 |
System.out.println("testEnableExt: OK"); |
|
146 |
} catch (TransformerConfigurationException e) { |
|
147 |
fail(e.getMessage()); |
|
148 |
} catch (TransformerException e) { |
|
149 |
fail(e.getMessage()); |
|
150 |
} finally { |
|
151 |
System.setSecurityManager(null); |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* use Templates template = factory.newTemplates(new StreamSource( new |
|
157 |
* FileInputStream(xslFilename))); // Use the template to create a |
|
158 |
* transformer Transformer xformer = template.newTransformer(); |
|
159 |
* |
|
160 |
* @param factory |
|
161 |
* @return |
|
162 |
*/ |
|
163 |
/** |
|
164 |
* Security is enabled, use new feature: enableExtensionFunctions Use the |
|
165 |
* template to create a transformer |
|
166 |
*/ |
|
167 |
public void testTemplatesEnableExtFunc() { |
|
168 |
Policy p = new SimplePolicy(new AllPermission()); |
|
169 |
Policy.setPolicy(p); |
|
170 |
System.setSecurityManager(new SecurityManager()); |
|
171 |
TransformerFactory factory = TransformerFactory.newInstance(); |
|
172 |
||
173 |
/** |
|
174 |
* Use of the extension function 'http://exslt.org/strings:tokenize' is |
|
175 |
* not allowed when the secure processing feature is set to true. |
|
176 |
* Attempt to use the new property to enable extension function |
|
177 |
*/ |
|
178 |
boolean isExtensionSupported = enableExtensionFunction(factory); |
|
179 |
||
180 |
try { |
|
181 |
SAXSource xslSource = new SAXSource(new InputSource(xslFile)); |
|
182 |
xslSource.setSystemId(xslFileId); |
|
183 |
Templates template = factory.newTemplates(xslSource); |
|
184 |
Transformer transformer = template.newTransformer(); |
|
185 |
StringWriter stringResult = new StringWriter(); |
|
186 |
Result result = new StreamResult(stringResult); |
|
187 |
transformer.transform(new SAXSource(new InputSource(xmlFile)), result); |
|
188 |
System.out.println("testTemplatesEnableExtFunc: OK"); |
|
189 |
} catch (TransformerConfigurationException e) { |
|
190 |
fail(e.getMessage()); |
|
191 |
} catch (TransformerException e) { |
|
192 |
fail(e.getMessage()); |
|
193 |
} finally { |
|
194 |
System.setSecurityManager(null); |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
boolean enableExtensionFunction(TransformerFactory factory) { |
|
199 |
boolean isSupported = true; |
|
200 |
try { |
|
201 |
factory.setFeature(ENABLE_EXTENSION_FUNCTIONS, true); |
|
202 |
} catch (TransformerConfigurationException ex) { |
|
203 |
isSupported = false; |
|
204 |
} |
|
205 |
return isSupported; |
|
206 |
} |
|
207 |
||
208 |
void transform(TransformerFactory factory) throws TransformerConfigurationException, TransformerException { |
|
209 |
SAXSource xslSource = new SAXSource(new InputSource(xslFile)); |
|
210 |
xslSource.setSystemId(xslFileId); |
|
211 |
Transformer transformer = factory.newTransformer(xslSource); |
|
212 |
StringWriter stringResult = new StringWriter(); |
|
213 |
Result result = new StreamResult(stringResult); |
|
214 |
transformer.transform(new SAXSource(new InputSource(xmlFile)), result); |
|
215 |
} |
|
216 |
||
217 |
class SimplePolicy extends Policy { |
|
218 |
||
219 |
private final Permissions perms; |
|
220 |
||
221 |
public SimplePolicy(Permission... permissions) { |
|
222 |
perms = new Permissions(); |
|
223 |
for (Permission permission : permissions) { |
|
224 |
perms.add(permission); |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
@Override |
|
229 |
public PermissionCollection getPermissions(CodeSource cs) { |
|
230 |
return perms; |
|
231 |
} |
|
232 |
||
233 |
@Override |
|
234 |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
|
235 |
return perms; |
|
236 |
} |
|
237 |
||
238 |
@Override |
|
239 |
public boolean implies(ProtectionDomain pd, Permission p) { |
|
240 |
return perms.implies(p); |
|
241 |
} |
|
242 |
||
243 |
//for older jdk |
|
244 |
@Override |
|
245 |
public void refresh() { |
|
246 |
} |
|
247 |
} |
|
248 |
} |