author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 46001 | nashorn/test/src/jdk/nashorn/internal/runtime/test/ClassFilterTest.java@f7865942462f |
child 47217 | 72e3ae9a25eb |
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 |
||
29758
cb8621d74d60
8076646: nashorn tests should avoid using package names used by nashorn sources
sundar
parents:
27361
diff
changeset
|
26 |
package jdk.nashorn.internal.runtime.test; |
26242 | 27 |
|
46001
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
28 |
import static org.testng.Assert.assertEquals; |
27361 | 29 |
import static org.testng.Assert.fail; |
30 |
import java.io.File; |
|
31 |
import javax.script.ScriptEngine; |
|
32 |
import javax.script.ScriptException; |
|
26242 | 33 |
import jdk.nashorn.api.scripting.ClassFilter; |
34 |
import jdk.nashorn.api.scripting.NashornScriptEngineFactory; |
|
35 |
import jdk.nashorn.api.scripting.URLReader; |
|
26982
ff5dd57a40f2
8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents:
26247
diff
changeset
|
36 |
import jdk.nashorn.internal.test.framework.TestFinder; |
26242 | 37 |
import org.testng.annotations.Test; |
38 |
||
27361 | 39 |
@SuppressWarnings("javadoc") |
26242 | 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() { |
27361 | 51 |
final String[] paths = new String[]{ |
26242 | 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 |
}; |
|
27361 | 60 |
final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); |
61 |
for (final String path : paths) { |
|
62 |
final ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"}, getClass().getClassLoader(), getClassFilter()); |
|
26242 | 63 |
try { |
64 |
engine.eval(new URLReader(new File(path).toURI().toURL())); |
|
27361 | 65 |
} catch (final Exception e) { |
26242 | 66 |
fail("Script " + path + " fails with exception :" + e.getMessage()); |
67 |
} |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
@Test |
|
72 |
public void noJavaOptionTest() { |
|
27361 | 73 |
final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); |
74 |
final ScriptEngine engine = factory.getScriptEngine(new String[]{"--no-java"}, getClass().getClassLoader(), getClassFilter()); |
|
26242 | 75 |
try { |
76 |
engine.eval("var str = Java.type('java.lang.String');"); |
|
77 |
fail("TypeError should have been thrown"); |
|
27361 | 78 |
} catch (final ScriptException e) { |
79 |
//emtpy |
|
26242 | 80 |
} |
46001
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
81 |
try { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
82 |
engine.eval("Java"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
83 |
fail("TypeError should have been thrown"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
84 |
} catch (final ScriptException e) { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
85 |
//emtpy |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
86 |
} |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
87 |
try { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
88 |
engine.eval("JavaImporter"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
89 |
fail("TypeError should have been thrown"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
90 |
} catch (final ScriptException e) { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
91 |
//emtpy |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
92 |
} |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
93 |
try { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
94 |
engine.eval("Packages"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
95 |
fail("TypeError should have been thrown"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
96 |
} catch (final ScriptException e) { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
97 |
//emtpy |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
98 |
} |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
99 |
try { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
100 |
engine.eval("com"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
101 |
fail("TypeError should have been thrown"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
102 |
} catch (final ScriptException e) { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
103 |
//emtpy |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
104 |
} |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
105 |
try { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
106 |
engine.eval("edu"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
107 |
fail("TypeError should have been thrown"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
108 |
} catch (final ScriptException e) { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
109 |
//emtpy |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
110 |
} |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
111 |
try { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
112 |
engine.eval("java"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
113 |
fail("TypeError should have been thrown"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
114 |
} catch (final ScriptException e) { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
115 |
//emtpy |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
116 |
} |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
117 |
try { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
118 |
engine.eval("javafx"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
119 |
fail("TypeError should have been thrown"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
120 |
} catch (final ScriptException e) { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
121 |
//emtpy |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
122 |
} |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
123 |
try { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
124 |
engine.eval("javax"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
125 |
fail("TypeError should have been thrown"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
126 |
} catch (final ScriptException e) { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
127 |
//emtpy |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
128 |
} |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
129 |
try { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
130 |
engine.eval("org"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
131 |
fail("TypeError should have been thrown"); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
132 |
} catch (final ScriptException e) { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
133 |
//emtpy |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
134 |
} |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
135 |
try { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
136 |
assertEquals(engine.eval("Java = this[\"__LINE__\"]; Java === this[\"__LINE__\"]"), Boolean.TRUE); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
137 |
} catch (final ScriptException e) { |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
138 |
fail("Unexpected exception", e); |
f7865942462f
8171539: Better script accessibility for JavaScript
hannesw
parents:
29758
diff
changeset
|
139 |
} |
26242 | 140 |
} |
141 |
||
142 |
@Test |
|
143 |
public void securityTest() { |
|
144 |
if (System.getSecurityManager() == null) { |
|
145 |
return; |
|
146 |
} |
|
147 |
||
27361 | 148 |
final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); |
149 |
final ScriptEngine engine = factory.getScriptEngine(getClassFilter()); |
|
26242 | 150 |
try { |
151 |
engine.eval("var thread = Java.type('sun.misc.Unsafe')"); |
|
152 |
fail("SecurityException should have been thrown"); |
|
27361 | 153 |
} catch (final Exception e) { |
154 |
//empty |
|
26242 | 155 |
} |
156 |
try { |
|
157 |
engine.eval("var thread = new sun.misc.Unsafe()"); |
|
158 |
fail("SecurityException should have been thrown"); |
|
27361 | 159 |
} catch (final Exception e) { |
160 |
//empty |
|
26242 | 161 |
} |
162 |
try { |
|
163 |
engine.eval("var thread = Java.extend(sun.misc.Unsafe, {})"); |
|
164 |
fail("TypeError should have been thrown"); |
|
27361 | 165 |
} catch (final Exception e) { |
166 |
//empty |
|
26242 | 167 |
} |
168 |
try { |
|
169 |
engine.eval("java.lang.System.exit(0)"); |
|
170 |
fail("SecurityException should have been thrown"); |
|
27361 | 171 |
} catch (final Exception e) { |
172 |
//empty |
|
26242 | 173 |
} |
174 |
||
175 |
} |
|
176 |
||
177 |
@Test |
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
try { |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
182 |
persistentCacheTestImpl(); |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
183 |
} finally { |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
184 |
if (oldCodeCache != null) { |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
185 |
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
|
186 |
} |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
187 |
} |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
188 |
} |
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
189 |
|
ec63f6d0eee4
8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents:
26242
diff
changeset
|
190 |
private void persistentCacheTestImpl() { |
27361 | 191 |
final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); |
192 |
final ScriptEngine engine = factory.getScriptEngine( |
|
26982
ff5dd57a40f2
8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents:
26247
diff
changeset
|
193 |
TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}), |
ff5dd57a40f2
8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents:
26247
diff
changeset
|
194 |
getClass().getClassLoader(), |
ff5dd57a40f2
8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents:
26247
diff
changeset
|
195 |
getClassFilter() |
26242 | 196 |
); |
27361 | 197 |
final String testScript = "var a = Java.type('java.lang.String');" + generateCodeForPersistentStore(); |
26242 | 198 |
try { |
199 |
engine.eval(testScript); |
|
200 |
} catch (final ScriptException exc) { |
|
201 |
fail(exc.getMessage()); |
|
202 |
} |
|
27361 | 203 |
final ScriptEngine engineSafe = factory.getScriptEngine( |
26982
ff5dd57a40f2
8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents:
26247
diff
changeset
|
204 |
TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}), |
26242 | 205 |
getClass().getClassLoader(), |
206 |
new ClassFilter() { |
|
207 |
@Override |
|
27361 | 208 |
public boolean exposeToScripts(final String s) { |
26242 | 209 |
return false; |
210 |
} |
|
211 |
} |
|
212 |
); |
|
213 |
try { |
|
214 |
engineSafe.eval(testScript); |
|
215 |
fail("ClassNotFoundException should have been thrown"); |
|
216 |
} catch (final Exception exc) { |
|
217 |
if (!(exc.getCause() instanceof ClassNotFoundException)) { |
|
26982
ff5dd57a40f2
8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents:
26247
diff
changeset
|
218 |
fail("ClassNotFoundException expected, got " + exc.getClass()); |
26242 | 219 |
} |
220 |
} |
|
221 |
} |
|
222 |
||
27361 | 223 |
private static String generateCodeForPersistentStore() { |
224 |
final StringBuilder stringBuilder = new StringBuilder(); |
|
26242 | 225 |
for (int i=0; i < 100; i++) { |
226 |
stringBuilder.append("function i") |
|
227 |
.append(i) |
|
228 |
.append("(y, z) { var x") |
|
229 |
.append(i) |
|
230 |
.append(" = ") |
|
231 |
.append(i) |
|
232 |
.append(";}"); |
|
233 |
} |
|
234 |
return stringBuilder.toString(); |
|
235 |
} |
|
236 |
||
27361 | 237 |
private static ClassFilter getClassFilter() { |
26242 | 238 |
return new ClassFilter() { |
239 |
@Override |
|
27361 | 240 |
public boolean exposeToScripts(final String s) { |
26242 | 241 |
return true; |
242 |
} |
|
243 |
}; |
|
244 |
} |
|
245 |
} |