test/nashorn/src/jdk/nashorn/internal/runtime/test/ClassFilterTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46001 nashorn/test/src/jdk/nashorn/internal/runtime/test/ClassFilterTest.java@f7865942462f
child 47217 72e3ae9a25eb
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
29758
cb8621d74d60 8076646: nashorn tests should avoid using package names used by nashorn sources
sundar
parents: 27361
diff changeset
    26
package jdk.nashorn.internal.runtime.test;
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    27
46001
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    28
import static org.testng.Assert.assertEquals;
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    29
import static org.testng.Assert.fail;
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    30
import java.io.File;
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    31
import javax.script.ScriptEngine;
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    32
import javax.script.ScriptException;
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    33
import jdk.nashorn.api.scripting.ClassFilter;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    34
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    35
import jdk.nashorn.api.scripting.URLReader;
26982
ff5dd57a40f2 8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents: 26247
diff changeset
    36
import jdk.nashorn.internal.test.framework.TestFinder;
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    37
import org.testng.annotations.Test;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    38
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    39
@SuppressWarnings("javadoc")
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    40
public class ClassFilterTest {
26247
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
    41
    private static final String NASHORN_CODE_CACHE = "nashorn.persistent.code.cache";
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
    42
    private static final String CLASSFILTER_CODE_CACHE = "build/classfilter_nashorn_code_cache";
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    43
26247
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
    44
    // @Test
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
    45
    // This test takes too much time for basic "ant clean test" run.
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
    46
    // Given that "allow-all-java-classes" is equivalent to no java class
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
    47
    // filter and external tests don't access any java, not sure if this
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
    48
    // test contributes much. We need faster "ant clean test" cycle for
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
    49
    // developers.
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    50
    public void runExternalJsTest() {
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    51
        final String[] paths = new String[]{
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    52
                "test/script/basic/compile-octane.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    53
                "test/script/basic/jquery.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    54
                "test/script/basic/prototype.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    55
                "test/script/basic/runsunspider.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    56
                "test/script/basic/underscore.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    57
                "test/script/basic/yui.js",
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    58
                "test/script/basic/run-octane.js"
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    59
        };
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    60
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    61
        for (final String path : paths) {
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    62
            final ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"}, getClass().getClassLoader(), getClassFilter());
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    63
            try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    64
                engine.eval(new URLReader(new File(path).toURI().toURL()));
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    65
            } catch (final Exception e) {
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    66
                fail("Script " + path + " fails with exception :" + e.getMessage());
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    67
            }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    68
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    69
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    70
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    71
    @Test
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    72
    public void noJavaOptionTest() {
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    73
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    74
        final ScriptEngine engine = factory.getScriptEngine(new String[]{"--no-java"}, getClass().getClassLoader(), getClassFilter());
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    75
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    76
            engine.eval("var str = Java.type('java.lang.String');");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    77
            fail("TypeError should have been thrown");
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    78
        } catch (final ScriptException e) {
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
    79
            //emtpy
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
    80
        }
46001
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    81
        try {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    82
            engine.eval("Java");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    83
            fail("TypeError should have been thrown");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    84
        } catch (final ScriptException e) {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    85
            //emtpy
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    86
        }
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    87
        try {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    88
            engine.eval("JavaImporter");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    89
            fail("TypeError should have been thrown");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    90
        } catch (final ScriptException e) {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    91
            //emtpy
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    92
        }
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    93
        try {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    94
            engine.eval("Packages");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    95
            fail("TypeError should have been thrown");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    96
        } catch (final ScriptException e) {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    97
            //emtpy
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    98
        }
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
    99
        try {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   100
            engine.eval("com");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   101
            fail("TypeError should have been thrown");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   102
        } catch (final ScriptException e) {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   103
            //emtpy
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   104
        }
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   105
        try {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   106
            engine.eval("edu");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   107
            fail("TypeError should have been thrown");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   108
        } catch (final ScriptException e) {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   109
            //emtpy
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   110
        }
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   111
        try {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   112
            engine.eval("java");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   113
            fail("TypeError should have been thrown");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   114
        } catch (final ScriptException e) {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   115
            //emtpy
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   116
        }
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   117
        try {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   118
            engine.eval("javafx");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   119
            fail("TypeError should have been thrown");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   120
        } catch (final ScriptException e) {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   121
            //emtpy
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   122
        }
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   123
        try {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   124
            engine.eval("javax");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   125
            fail("TypeError should have been thrown");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   126
        } catch (final ScriptException e) {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   127
            //emtpy
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   128
        }
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   129
        try {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   130
            engine.eval("org");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   131
            fail("TypeError should have been thrown");
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   132
        } catch (final ScriptException e) {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   133
            //emtpy
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   134
        }
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   135
        try {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   136
            assertEquals(engine.eval("Java = this[\"__LINE__\"]; Java === this[\"__LINE__\"]"), Boolean.TRUE);
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   137
        } catch (final ScriptException e) {
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   138
            fail("Unexpected exception", e);
f7865942462f 8171539: Better script accessibility for JavaScript
hannesw
parents: 29758
diff changeset
   139
        }
26242
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
    @Test
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   143
    public void securityTest() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   144
        if (System.getSecurityManager() == null) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   145
            return;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   146
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   147
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   148
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   149
        final ScriptEngine engine = factory.getScriptEngine(getClassFilter());
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   150
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   151
            engine.eval("var thread = Java.type('sun.misc.Unsafe')");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   152
            fail("SecurityException should have been thrown");
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   153
        } catch (final Exception e) {
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   154
            //empty
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   155
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   156
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   157
            engine.eval("var thread = new sun.misc.Unsafe()");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   158
            fail("SecurityException should have been thrown");
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   159
        } catch (final Exception e) {
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   160
            //empty
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   161
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   162
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   163
            engine.eval("var thread = Java.extend(sun.misc.Unsafe, {})");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   164
            fail("TypeError should have been thrown");
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   165
        } catch (final Exception e) {
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   166
            //empty
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   167
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   168
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   169
            engine.eval("java.lang.System.exit(0)");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   170
            fail("SecurityException should have been thrown");
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   171
        } catch (final Exception e) {
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   172
            //empty
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   173
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   174
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   175
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   176
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   177
    @Test
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   178
    public void persistentCacheTest() {
26247
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   179
        final String oldCodeCache = System.getProperty(NASHORN_CODE_CACHE);
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   180
        System.setProperty(NASHORN_CODE_CACHE, CLASSFILTER_CODE_CACHE);
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   181
        try {
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   182
            persistentCacheTestImpl();
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   183
        } finally {
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   184
            if (oldCodeCache != null) {
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   185
                System.setProperty(NASHORN_CODE_CACHE, oldCodeCache);
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   186
            }
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   187
        }
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   188
    }
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   189
ec63f6d0eee4 8056050: runExternalJsTest method in test/jdk/nashorn/internal/runtime/ClassFilter.java slows down "ant test"
sundar
parents: 26242
diff changeset
   190
    private void persistentCacheTestImpl() {
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   191
        final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   192
        final ScriptEngine engine = factory.getScriptEngine(
26982
ff5dd57a40f2 8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents: 26247
diff changeset
   193
              TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}),
ff5dd57a40f2 8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents: 26247
diff changeset
   194
                  getClass().getClassLoader(),
ff5dd57a40f2 8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents: 26247
diff changeset
   195
                  getClassFilter()
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   196
        );
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   197
        final String testScript = "var a = Java.type('java.lang.String');" + generateCodeForPersistentStore();
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   198
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   199
            engine.eval(testScript);
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   200
        } catch (final ScriptException exc) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   201
            fail(exc.getMessage());
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   202
        }
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   203
        final ScriptEngine engineSafe = factory.getScriptEngine(
26982
ff5dd57a40f2 8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents: 26247
diff changeset
   204
                TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}),
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   205
                getClass().getClassLoader(),
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   206
                new ClassFilter() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   207
                    @Override
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   208
                    public boolean exposeToScripts(final String s) {
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   209
                        return false;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   210
                    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   211
                }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   212
        );
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   213
        try {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   214
            engineSafe.eval(testScript);
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   215
            fail("ClassNotFoundException should have been thrown");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   216
        } catch (final Exception exc) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   217
            if (!(exc.getCause() instanceof ClassNotFoundException)) {
26982
ff5dd57a40f2 8059811: Turn off optimistic typing by default and add both ant test-pessimistic and ant test-optimistic sub-test suites.
lagergren
parents: 26247
diff changeset
   218
                fail("ClassNotFoundException expected, got " + exc.getClass());
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   219
            }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   220
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   221
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   222
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   223
    private static String generateCodeForPersistentStore() {
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   224
        final StringBuilder stringBuilder = new StringBuilder();
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   225
        for (int i=0; i < 100; i++) {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   226
            stringBuilder.append("function i")
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   227
                    .append(i)
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   228
                    .append("(y, z) { var x")
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   229
                    .append(i)
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   230
                    .append(" = ")
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   231
                    .append(i)
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   232
                    .append(";}");
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   233
        }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   234
        return stringBuilder.toString();
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   235
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   236
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   237
    private static ClassFilter getClassFilter() {
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   238
        return new ClassFilter() {
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   239
            @Override
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26982
diff changeset
   240
            public boolean exposeToScripts(final String s) {
26242
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   241
                return true;
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   242
            }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   243
        };
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   244
    }
f6707443717b 8055811: Tests for Nashorn ClassFilter Support
yan
parents:
diff changeset
   245
}