nashorn/test/src/jdk/nashorn/internal/runtime/ClassFilterTest.java
author yan
Tue, 26 Aug 2014 13:26:15 +0400
changeset 26242 f6707443717b
child 26247 ec63f6d0eee4
permissions -rw-r--r--
8055811: Tests for Nashorn ClassFilter Support Reviewed-by: sundar, attila Contributed-by: Sergey Lugovoy <sergey.lugovoy@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
     1
/*
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
     4
 *
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    10
 *
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    15
 * accompanied this code).
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    16
 *
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    20
 *
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    23
 * questions.
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    24
 */
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    25
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    27
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    28
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    29
import jdk.nashorn.api.scripting.ClassFilter;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    30
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    31
import jdk.nashorn.api.scripting.URLReader;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    32
import org.testng.annotations.Test;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    33
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    34
import javax.script.ScriptEngine;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    35
import javax.script.ScriptException;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    36
import java.io.File;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    37
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    38
import static org.testng.Assert.fail;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    39
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    40
public class ClassFilterTest {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    41
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    42
    private final String codeCache = "build/nashorn_code_cache";
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    43
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    44
    @Test
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    45
    public void runExternalJsTest() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    46
        String[] paths = new String[]{
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    47
                "test/script/basic/compile-octane.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    48
                "test/script/basic/jquery.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    49
                "test/script/basic/prototype.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    50
                "test/script/basic/runsunspider.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    51
                "test/script/basic/underscore.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    52
                "test/script/basic/yui.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    53
                "test/script/basic/run-octane.js"
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    54
        };
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    55
        NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    56
        for (String path : paths) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    57
            ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"}, getClass().getClassLoader(), getClassFilter());
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    58
            try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    59
                engine.eval(new URLReader(new File(path).toURI().toURL()));
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    60
            } catch (Exception e) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    61
                fail("Script " + path + " fails with exception :" + e.getMessage());
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    62
            }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    63
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    64
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    65
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    66
    @Test
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    67
    public void noJavaOptionTest() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    68
        NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    69
        ScriptEngine engine = factory.getScriptEngine(new String[]{"--no-java"}, getClass().getClassLoader(), getClassFilter());
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    70
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    71
            engine.eval("var str = Java.type('java.lang.String');");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    72
            fail("TypeError should have been thrown");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    73
        } catch (ScriptException exc) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    74
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    75
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    76
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    77
    @Test
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    78
    public void securityTest() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    79
        if (System.getSecurityManager() == null) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    80
            return;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    81
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    82
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    83
        NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    84
        ScriptEngine engine = factory.getScriptEngine(getClassFilter());
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    85
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    86
            engine.eval("var thread = Java.type('sun.misc.Unsafe')");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    87
            fail("SecurityException should have been thrown");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    88
        } catch (final Exception exc) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    89
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    90
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    91
            engine.eval("var thread = new sun.misc.Unsafe()");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    92
            fail("SecurityException should have been thrown");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    93
        } catch (final Exception exc) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    94
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    95
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    96
            engine.eval("var thread = Java.extend(sun.misc.Unsafe, {})");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    97
            fail("TypeError should have been thrown");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    98
        } catch (final Exception exc) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    99
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   100
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   101
            engine.eval("java.lang.System.exit(0)");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   102
            fail("SecurityException should have been thrown");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   103
        } catch (final Exception exc) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   104
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   105
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   106
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   107
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   108
    @Test
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   109
    public void persistentCacheTest() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   110
        System.setProperty("nashorn.persistent.code.cache", codeCache);
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   111
        NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   112
        ScriptEngine engine = factory.getScriptEngine(
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   113
                new String[]{"--persistent-code-cache"},
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   114
                getClass().getClassLoader(),
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   115
                getClassFilter()
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   116
        );
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   117
        String testScript = "var a = Java.type('java.lang.String');" + generateCodeForPersistentStore();
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   118
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   119
            engine.eval(testScript);
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   120
        } catch (final ScriptException exc) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   121
            fail(exc.getMessage());
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   122
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   123
        ScriptEngine engineSafe = factory.getScriptEngine(
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   124
                new String[]{"--persistent-code-cache"},
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   125
                getClass().getClassLoader(),
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   126
                new ClassFilter() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   127
                    @Override
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   128
                    public boolean exposeToScripts(String s) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   129
                        return false;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   130
                    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   131
                }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   132
        );
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   133
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   134
            engineSafe.eval(testScript);
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   135
            fail("ClassNotFoundException should have been thrown");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   136
        } catch (final Exception exc) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   137
            if (!(exc.getCause() instanceof ClassNotFoundException)) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   138
                fail("ClassNotFoundException expected");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   139
            }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   140
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   141
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   142
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   143
    private String generateCodeForPersistentStore() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   144
        StringBuilder stringBuilder = new StringBuilder();
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   145
        for (int i=0; i < 100; i++) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   146
            stringBuilder.append("function i")
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   147
                    .append(i)
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   148
                    .append("(y, z) { var x")
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   149
                    .append(i)
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   150
                    .append(" = ")
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   151
                    .append(i)
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   152
                    .append(";}");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   153
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   154
        return stringBuilder.toString();
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   155
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   156
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   157
    private ClassFilter getClassFilter() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   158
        return new ClassFilter() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   159
            @Override
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   160
            public boolean exposeToScripts(String s) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   161
                return true;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   162
            }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   163
        };
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   164
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   165
}