author | sundar |
Tue, 26 Aug 2014 17:21:17 +0530 | |
changeset 26247 | ec63f6d0eee4 |
parent 26242 | f6707443717b |
child 26982 | ff5dd57a40f2 |
permissions | -rw-r--r-- |
26242 | 1 |
/* |
2 |
* Copyright (c) 2014, 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.runtime; |
|
27 |
||
28 |
||
29 |
import jdk.nashorn.api.scripting.ClassFilter; |
|
30 |
import jdk.nashorn.api.scripting.NashornScriptEngineFactory; |
|
31 |
import jdk.nashorn.api.scripting.URLReader; |
|
32 |
import org.testng.annotations.Test; |
|
33 |
||
34 |
import javax.script.ScriptEngine; |
|
35 |
import javax.script.ScriptException; |
|
36 |
import java.io.File; |
|
37 |
||
38 |
import static org.testng.Assert.fail; |
|
39 |
||
40 |
public class ClassFilterTest { |
|
26247
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
41 |
private static final String NASHORN_CODE_CACHE = "nashorn.persistent.code.cache"; |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
42 |
private static final String CLASSFILTER_CODE_CACHE = "build/classfilter_nashorn_code_cache"; |
26242 | 43 |
|
26247
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
44 |
// @Test |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
45 |
// This test takes too much time for basic "ant clean test" run. |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
46 |
// Given that "allow-all-java-classes" is equivalent to no java class |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
47 |
// filter and external tests don't access any java, not sure if this |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
48 |
// test contributes much. We need faster "ant clean test" cycle for |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
49 |
// developers. |
26242 | 50 |
public void runExternalJsTest() { |
51 |
String[] paths = new String[]{ |
|
52 |
"test/script/basic/compile-octane.js", |
|
53 |
"test/script/basic/jquery.js", |
|
54 |
"test/script/basic/prototype.js", |
|
55 |
"test/script/basic/runsunspider.js", |
|
56 |
"test/script/basic/underscore.js", |
|
57 |
"test/script/basic/yui.js", |
|
58 |
"test/script/basic/run-octane.js" |
|
59 |
}; |
|
60 |
NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); |
|
61 |
for (String path : paths) { |
|
62 |
ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"}, getClass().getClassLoader(), getClassFilter()); |
|
63 |
try { |
|
64 |
engine.eval(new URLReader(new File(path).toURI().toURL())); |
|
65 |
} catch (Exception e) { |
|
66 |
fail("Script " + path + " fails with exception :" + e.getMessage()); |
|
67 |
} |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
@Test |
|
72 |
public void noJavaOptionTest() { |
|
73 |
NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); |
|
74 |
ScriptEngine engine = factory.getScriptEngine(new String[]{"--no-java"}, getClass().getClassLoader(), getClassFilter()); |
|
75 |
try { |
|
76 |
engine.eval("var str = Java.type('java.lang.String');"); |
|
77 |
fail("TypeError should have been thrown"); |
|
78 |
} catch (ScriptException exc) { |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
@Test |
|
83 |
public void securityTest() { |
|
84 |
if (System.getSecurityManager() == null) { |
|
85 |
return; |
|
86 |
} |
|
87 |
||
88 |
NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); |
|
89 |
ScriptEngine engine = factory.getScriptEngine(getClassFilter()); |
|
90 |
try { |
|
91 |
engine.eval("var thread = Java.type('sun.misc.Unsafe')"); |
|
92 |
fail("SecurityException should have been thrown"); |
|
93 |
} catch (final Exception exc) { |
|
94 |
} |
|
95 |
try { |
|
96 |
engine.eval("var thread = new sun.misc.Unsafe()"); |
|
97 |
fail("SecurityException should have been thrown"); |
|
98 |
} catch (final Exception exc) { |
|
99 |
} |
|
100 |
try { |
|
101 |
engine.eval("var thread = Java.extend(sun.misc.Unsafe, {})"); |
|
102 |
fail("TypeError should have been thrown"); |
|
103 |
} catch (final Exception exc) { |
|
104 |
} |
|
105 |
try { |
|
106 |
engine.eval("java.lang.System.exit(0)"); |
|
107 |
fail("SecurityException should have been thrown"); |
|
108 |
} catch (final Exception exc) { |
|
109 |
} |
|
110 |
||
111 |
} |
|
112 |
||
113 |
@Test |
|
114 |
public void persistentCacheTest() { |
|
26247
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
115 |
final String oldCodeCache = System.getProperty(NASHORN_CODE_CACHE); |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
116 |
System.setProperty(NASHORN_CODE_CACHE, CLASSFILTER_CODE_CACHE); |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
117 |
try { |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
118 |
persistentCacheTestImpl(); |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
119 |
} finally { |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
120 |
if (oldCodeCache != null) { |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
121 |
System.setProperty(NASHORN_CODE_CACHE, oldCodeCache); |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
122 |
} |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
123 |
} |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
124 |
} |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
125 |
|
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
126 |
private void persistentCacheTestImpl() { |
26242 | 127 |
NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); |
128 |
ScriptEngine engine = factory.getScriptEngine( |
|
129 |
new String[]{"--persistent-code-cache"}, |
|
130 |
getClass().getClassLoader(), |
|
131 |
getClassFilter() |
|
132 |
); |
|
133 |
String testScript = "var a = Java.type('java.lang.String');" + generateCodeForPersistentStore(); |
|
134 |
try { |
|
135 |
engine.eval(testScript); |
|
136 |
} catch (final ScriptException exc) { |
|
137 |
fail(exc.getMessage()); |
|
138 |
} |
|
139 |
ScriptEngine engineSafe = factory.getScriptEngine( |
|
140 |
new String[]{"--persistent-code-cache"}, |
|
141 |
getClass().getClassLoader(), |
|
142 |
new ClassFilter() { |
|
143 |
@Override |
|
144 |
public boolean exposeToScripts(String s) { |
|
145 |
return false; |
|
146 |
} |
|
147 |
} |
|
148 |
); |
|
149 |
try { |
|
150 |
engineSafe.eval(testScript); |
|
151 |
fail("ClassNotFoundException should have been thrown"); |
|
152 |
} catch (final Exception exc) { |
|
153 |
if (!(exc.getCause() instanceof ClassNotFoundException)) { |
|
154 |
fail("ClassNotFoundException expected"); |
|
155 |
} |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
private String generateCodeForPersistentStore() { |
|
160 |
StringBuilder stringBuilder = new StringBuilder(); |
|
161 |
for (int i=0; i < 100; i++) { |
|
162 |
stringBuilder.append("function i") |
|
163 |
.append(i) |
|
164 |
.append("(y, z) { var x") |
|
165 |
.append(i) |
|
166 |
.append(" = ") |
|
167 |
.append(i) |
|
168 |
.append(";}"); |
|
169 |
} |
|
170 |
return stringBuilder.toString(); |
|
171 |
} |
|
172 |
||
173 |
private ClassFilter getClassFilter() { |
|
174 |
return new ClassFilter() { |
|
175 |
@Override |
|
176 |
public boolean exposeToScripts(String s) { |
|
177 |
return true; |
|
178 |
} |
|
179 |
}; |
|
180 |
} |
|
181 |
} |