JDK-8158467.js revision 1712:65b4db247568
1/*
2 * Copyright (c) 2016, 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/**
25 * JDK-8158467: AccessControlException is thrown on public Java class access if "script app loader" is set to null
26 *
27 * @option -scripting
28 * @test
29 * @run
30 */
31
32var Factory = Java.type("jdk.nashorn.api.scripting.NashornScriptEngineFactory");
33var fac = new Factory();
34
35// This script has to be given RuntimePermission("nashorn.setConfig")
36var e = fac["getScriptEngine(java.lang.ClassLoader)"](null);
37
38print(e.eval("java.lang.System"));
39print(e.eval("({ foo: 42})").foo);
40print((e.eval("function(x) x*x"))(31));
41
42e.put("output", print);
43var runnable = e.eval(<<EOF
44    new java.lang.Runnable() {
45        run: function() {
46            output("hello Runnable");
47        }
48    }
49EOF);
50
51runnable.run();
52
53var obj = e.eval(<<EOF
54new (Java.extend(Java.type("java.lang.Object"))) {
55    hashCode: function() 33,
56    toString: function() "I'm object"
57}
58EOF);
59
60print(obj.hashCode());
61print(obj.toString());
62
63// should throw SecurityException!
64try {
65    e.eval("Packages.jdk.internal");
66} catch (ex) {
67    print(ex);
68}
69
70// should throw SecurityException!
71try {
72    e.eval("Java.type('jdk.internal.misc.Unsafe')");
73} catch (ex) {
74    print(ex);
75}
76
77// should throw SecurityException!
78try {
79    e.eval("Java.type('jdk.nashorn.internal.Context')");
80} catch (ex) {
81    print(ex);
82}
83
84// should throw ClassNotFoundException as null is script
85// "app loader" [and not platform loader which loads nashorn]
86e.eval(<<EOF
87try {
88    Java.type('jdk.nashorn.api.scripting.JSObject');
89} catch (ex) {
90    output(ex);
91}
92EOF);
93