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 {
|
|
41 |
|
|
42 |
private final String codeCache = "build/nashorn_code_cache";
|
|
43 |
|
|
44 |
@Test
|
|
45 |
public void runExternalJsTest() {
|
|
46 |
String[] paths = new String[]{
|
|
47 |
"test/script/basic/compile-octane.js",
|
|
48 |
"test/script/basic/jquery.js",
|
|
49 |
"test/script/basic/prototype.js",
|
|
50 |
"test/script/basic/runsunspider.js",
|
|
51 |
"test/script/basic/underscore.js",
|
|
52 |
"test/script/basic/yui.js",
|
|
53 |
"test/script/basic/run-octane.js"
|
|
54 |
};
|
|
55 |
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
|
|
56 |
for (String path : paths) {
|
|
57 |
ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"}, getClass().getClassLoader(), getClassFilter());
|
|
58 |
try {
|
|
59 |
engine.eval(new URLReader(new File(path).toURI().toURL()));
|
|
60 |
} catch (Exception e) {
|
|
61 |
fail("Script " + path + " fails with exception :" + e.getMessage());
|
|
62 |
}
|
|
63 |
}
|
|
64 |
}
|
|
65 |
|
|
66 |
@Test
|
|
67 |
public void noJavaOptionTest() {
|
|
68 |
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
|
|
69 |
ScriptEngine engine = factory.getScriptEngine(new String[]{"--no-java"}, getClass().getClassLoader(), getClassFilter());
|
|
70 |
try {
|
|
71 |
engine.eval("var str = Java.type('java.lang.String');");
|
|
72 |
fail("TypeError should have been thrown");
|
|
73 |
} catch (ScriptException exc) {
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
@Test
|
|
78 |
public void securityTest() {
|
|
79 |
if (System.getSecurityManager() == null) {
|
|
80 |
return;
|
|
81 |
}
|
|
82 |
|
|
83 |
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
|
|
84 |
ScriptEngine engine = factory.getScriptEngine(getClassFilter());
|
|
85 |
try {
|
|
86 |
engine.eval("var thread = Java.type('sun.misc.Unsafe')");
|
|
87 |
fail("SecurityException should have been thrown");
|
|
88 |
} catch (final Exception exc) {
|
|
89 |
}
|
|
90 |
try {
|
|
91 |
engine.eval("var thread = new sun.misc.Unsafe()");
|
|
92 |
fail("SecurityException should have been thrown");
|
|
93 |
} catch (final Exception exc) {
|
|
94 |
}
|
|
95 |
try {
|
|
96 |
engine.eval("var thread = Java.extend(sun.misc.Unsafe, {})");
|
|
97 |
fail("TypeError should have been thrown");
|
|
98 |
} catch (final Exception exc) {
|
|
99 |
}
|
|
100 |
try {
|
|
101 |
engine.eval("java.lang.System.exit(0)");
|
|
102 |
fail("SecurityException should have been thrown");
|
|
103 |
} catch (final Exception exc) {
|
|
104 |
}
|
|
105 |
|
|
106 |
}
|
|
107 |
|
|
108 |
@Test
|
|
109 |
public void persistentCacheTest() {
|
|
110 |
System.setProperty("nashorn.persistent.code.cache", codeCache);
|
|
111 |
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
|
|
112 |
ScriptEngine engine = factory.getScriptEngine(
|
|
113 |
new String[]{"--persistent-code-cache"},
|
|
114 |
getClass().getClassLoader(),
|
|
115 |
getClassFilter()
|
|
116 |
);
|
|
117 |
String testScript = "var a = Java.type('java.lang.String');" + generateCodeForPersistentStore();
|
|
118 |
try {
|
|
119 |
engine.eval(testScript);
|
|
120 |
} catch (final ScriptException exc) {
|
|
121 |
fail(exc.getMessage());
|
|
122 |
}
|
|
123 |
ScriptEngine engineSafe = factory.getScriptEngine(
|
|
124 |
new String[]{"--persistent-code-cache"},
|
|
125 |
getClass().getClassLoader(),
|
|
126 |
new ClassFilter() {
|
|
127 |
@Override
|
|
128 |
public boolean exposeToScripts(String s) {
|
|
129 |
return false;
|
|
130 |
}
|
|
131 |
}
|
|
132 |
);
|
|
133 |
try {
|
|
134 |
engineSafe.eval(testScript);
|
|
135 |
fail("ClassNotFoundException should have been thrown");
|
|
136 |
} catch (final Exception exc) {
|
|
137 |
if (!(exc.getCause() instanceof ClassNotFoundException)) {
|
|
138 |
fail("ClassNotFoundException expected");
|
|
139 |
}
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
private String generateCodeForPersistentStore() {
|
|
144 |
StringBuilder stringBuilder = new StringBuilder();
|
|
145 |
for (int i=0; i < 100; i++) {
|
|
146 |
stringBuilder.append("function i")
|
|
147 |
.append(i)
|
|
148 |
.append("(y, z) { var x")
|
|
149 |
.append(i)
|
|
150 |
.append(" = ")
|
|
151 |
.append(i)
|
|
152 |
.append(";}");
|
|
153 |
}
|
|
154 |
return stringBuilder.toString();
|
|
155 |
}
|
|
156 |
|
|
157 |
private ClassFilter getClassFilter() {
|
|
158 |
return new ClassFilter() {
|
|
159 |
@Override
|
|
160 |
public boolean exposeToScripts(String s) {
|
|
161 |
return true;
|
|
162 |
}
|
|
163 |
};
|
|
164 |
}
|
|
165 |
}
|