aboutsummaryrefslogtreecommitdiff
path: root/test/src/jdk/nashorn/internal/runtime/ClassFilterTest.java
blob: 5c69141dfc1ca670f90e70ed892150f37bb0a698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.nashorn.internal.runtime;

import static org.testng.Assert.fail;
import java.io.File;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import jdk.nashorn.api.scripting.ClassFilter;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import jdk.nashorn.api.scripting.URLReader;
import jdk.nashorn.internal.test.framework.TestFinder;
import org.testng.annotations.Test;

@SuppressWarnings("javadoc")
public class ClassFilterTest {
    private static final String NASHORN_CODE_CACHE = "nashorn.persistent.code.cache";
    private static final String CLASSFILTER_CODE_CACHE = "build/classfilter_nashorn_code_cache";

    // @Test
    // This test takes too much time for basic "ant clean test" run.
    // Given that "allow-all-java-classes" is equivalent to no java class
    // filter and external tests don't access any java, not sure if this
    // test contributes much. We need faster "ant clean test" cycle for
    // developers.
    public void runExternalJsTest() {
        final String[] paths = new String[]{
                "test/script/basic/compile-octane.js",
                "test/script/basic/jquery.js",
                "test/script/basic/prototype.js",
                "test/script/basic/runsunspider.js",
                "test/script/basic/underscore.js",
                "test/script/basic/yui.js",
                "test/script/basic/run-octane.js"
        };
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        for (final String path : paths) {
            final ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"}, getClass().getClassLoader(), getClassFilter());
            try {
                engine.eval(new URLReader(new File(path).toURI().toURL()));
            } catch (final Exception e) {
                fail("Script " + path + " fails with exception :" + e.getMessage());
            }
        }
    }

    @Test
    public void noJavaOptionTest() {
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        final ScriptEngine engine = factory.getScriptEngine(new String[]{"--no-java"}, getClass().getClassLoader(), getClassFilter());
        try {
            engine.eval("var str = Java.type('java.lang.String');");
            fail("TypeError should have been thrown");
        } catch (final ScriptException e) {
            //emtpy
        }
    }

    @Test
    public void securityTest() {
        if (System.getSecurityManager() == null) {
            return;
        }

        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        final ScriptEngine engine = factory.getScriptEngine(getClassFilter());
        try {
            engine.eval("var thread = Java.type('sun.misc.Unsafe')");
            fail("SecurityException should have been thrown");
        } catch (final Exception e) {
            //empty
        }
        try {
            engine.eval("var thread = new sun.misc.Unsafe()");
            fail("SecurityException should have been thrown");
        } catch (final Exception e) {
            //empty
        }
        try {
            engine.eval("var thread = Java.extend(sun.misc.Unsafe, {})");
            fail("TypeError should have been thrown");
        } catch (final Exception e) {
            //empty
        }
        try {
            engine.eval("java.lang.System.exit(0)");
            fail("SecurityException should have been thrown");
        } catch (final Exception e) {
            //empty
        }

    }

    @Test
    public void persistentCacheTest() {
        final String oldCodeCache = System.getProperty(NASHORN_CODE_CACHE);
        System.setProperty(NASHORN_CODE_CACHE, CLASSFILTER_CODE_CACHE);
        try {
            persistentCacheTestImpl();
        } finally {
            if (oldCodeCache != null) {
                System.setProperty(NASHORN_CODE_CACHE, oldCodeCache);
            }
        }
    }

    private void persistentCacheTestImpl() {
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
        final ScriptEngine engine = factory.getScriptEngine(
              TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}),
                  getClass().getClassLoader(),
                  getClassFilter()
        );
        final String testScript = "var a = Java.type('java.lang.String');" + generateCodeForPersistentStore();
        try {
            engine.eval(testScript);
        } catch (final ScriptException exc) {
            fail(exc.getMessage());
        }
        final ScriptEngine engineSafe = factory.getScriptEngine(
                TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}),
                getClass().getClassLoader(),
                new ClassFilter() {
                    @Override
                    public boolean exposeToScripts(final String s) {
                        return false;
                    }
                }
        );
        try {
            engineSafe.eval(testScript);
            fail("ClassNotFoundException should have been thrown");
        } catch (final Exception exc) {
            if (!(exc.getCause() instanceof ClassNotFoundException)) {
                fail("ClassNotFoundException expected, got " + exc.getClass());
            }
        }
    }

    private static String generateCodeForPersistentStore() {
        final StringBuilder stringBuilder = new StringBuilder();
        for (int i=0; i < 100; i++) {
            stringBuilder.append("function i")
                    .append(i)
                    .append("(y, z) { var x")
                    .append(i)
                    .append(" = ")
                    .append(i)
                    .append(";}");
        }
        return stringBuilder.toString();
    }

    private static ClassFilter getClassFilter() {
        return new ClassFilter() {
            @Override
            public boolean exposeToScripts(final String s) {
                return true;
            }
        };
    }
}