|
1 /* |
|
2 * Copyright (c) 2010, 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. 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 static org.testng.Assert.assertTrue; |
|
30 import static org.testng.Assert.fail; |
|
31 |
|
32 import javax.script.ScriptEngine; |
|
33 import javax.script.ScriptEngineFactory; |
|
34 import javax.script.ScriptEngineManager; |
|
35 import javax.script.ScriptException; |
|
36 import jdk.nashorn.api.scripting.NashornScriptEngineFactory; |
|
37 import org.testng.annotations.Test; |
|
38 |
|
39 /** |
|
40 * Tests for trusted client usage of nashorn script engine factory extension API |
|
41 */ |
|
42 public class TrustedScriptEngineTest { |
|
43 private static class MyClassLoader extends ClassLoader { |
|
44 // to check if script engine uses the specified class loader |
|
45 private final boolean[] reached = new boolean[1]; |
|
46 |
|
47 @Override |
|
48 protected Class findClass(final String name) throws ClassNotFoundException { |
|
49 // flag that it reached here |
|
50 reached[0] = true; |
|
51 return super.findClass(name); |
|
52 } |
|
53 |
|
54 public boolean reached() { |
|
55 return reached[0]; |
|
56 } |
|
57 }; |
|
58 |
|
59 // These are for "private" extension API of NashornScriptEngineFactory that |
|
60 // accepts a ClassLoader and/or command line options. |
|
61 |
|
62 @Test |
|
63 public void factoryClassLoaderTest() { |
|
64 final ScriptEngineManager sm = new ScriptEngineManager(); |
|
65 for (ScriptEngineFactory fac : sm.getEngineFactories()) { |
|
66 if (fac instanceof NashornScriptEngineFactory) { |
|
67 final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; |
|
68 final MyClassLoader loader = new MyClassLoader(); |
|
69 // set the classloader as app class loader |
|
70 final ScriptEngine e = nfac.getScriptEngine(loader); |
|
71 try { |
|
72 e.eval("Packages.foo"); |
|
73 // check that the class loader was attempted |
|
74 assertTrue(loader.reached(), "did not reach class loader!"); |
|
75 } catch (final ScriptException se) { |
|
76 se.printStackTrace(); |
|
77 fail(se.getMessage()); |
|
78 } |
|
79 return; |
|
80 } |
|
81 } |
|
82 |
|
83 fail("Cannot find nashorn factory!"); |
|
84 } |
|
85 |
|
86 @Test |
|
87 public void factoryClassLoaderAndOptionsTest() { |
|
88 final ScriptEngineManager sm = new ScriptEngineManager(); |
|
89 for (ScriptEngineFactory fac : sm.getEngineFactories()) { |
|
90 if (fac instanceof NashornScriptEngineFactory) { |
|
91 final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; |
|
92 final String[] options = new String[] { "-strict" }; |
|
93 final MyClassLoader loader = new MyClassLoader(); |
|
94 // set the classloader as app class loader |
|
95 final ScriptEngine e = nfac.getScriptEngine(options, loader); |
|
96 try { |
|
97 e.eval("Packages.foo"); |
|
98 // check that the class loader was attempted |
|
99 assertTrue(loader.reached(), "did not reach class loader!"); |
|
100 } catch (final ScriptException se) { |
|
101 se.printStackTrace(); |
|
102 fail(se.getMessage()); |
|
103 } |
|
104 |
|
105 try { |
|
106 // strict mode - delete of a var should throw SyntaxError |
|
107 e.eval("var d = 2; delete d;"); |
|
108 } catch (final ScriptException se) { |
|
109 // check that the error message contains "SyntaxError" |
|
110 assertTrue(se.getMessage().contains("SyntaxError")); |
|
111 } |
|
112 |
|
113 return; |
|
114 } |
|
115 } |
|
116 |
|
117 fail("Cannot find nashorn factory!"); |
|
118 } |
|
119 } |