nashorn/test/src/jdk/nashorn/internal/runtime/ClassFilterTest.java
changeset 29784 b9220497eb43
parent 29783 db33e568f107
parent 29770 6415d011ad02
child 29785 da950f343762
equal deleted inserted replaced
29783:db33e568f107 29784:b9220497eb43
     1 /*
       
     2  * Copyright (c) 2014, 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 import static org.testng.Assert.fail;
       
    29 import java.io.File;
       
    30 import javax.script.ScriptEngine;
       
    31 import javax.script.ScriptException;
       
    32 import jdk.nashorn.api.scripting.ClassFilter;
       
    33 import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
       
    34 import jdk.nashorn.api.scripting.URLReader;
       
    35 import jdk.nashorn.internal.test.framework.TestFinder;
       
    36 import org.testng.annotations.Test;
       
    37 
       
    38 @SuppressWarnings("javadoc")
       
    39 public class ClassFilterTest {
       
    40     private static final String NASHORN_CODE_CACHE = "nashorn.persistent.code.cache";
       
    41     private static final String CLASSFILTER_CODE_CACHE = "build/classfilter_nashorn_code_cache";
       
    42 
       
    43     // @Test
       
    44     // This test takes too much time for basic "ant clean test" run.
       
    45     // Given that "allow-all-java-classes" is equivalent to no java class
       
    46     // filter and external tests don't access any java, not sure if this
       
    47     // test contributes much. We need faster "ant clean test" cycle for
       
    48     // developers.
       
    49     public void runExternalJsTest() {
       
    50         final String[] paths = new String[]{
       
    51                 "test/script/basic/compile-octane.js",
       
    52                 "test/script/basic/jquery.js",
       
    53                 "test/script/basic/prototype.js",
       
    54                 "test/script/basic/runsunspider.js",
       
    55                 "test/script/basic/underscore.js",
       
    56                 "test/script/basic/yui.js",
       
    57                 "test/script/basic/run-octane.js"
       
    58         };
       
    59         final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
       
    60         for (final String path : paths) {
       
    61             final ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"}, getClass().getClassLoader(), getClassFilter());
       
    62             try {
       
    63                 engine.eval(new URLReader(new File(path).toURI().toURL()));
       
    64             } catch (final Exception e) {
       
    65                 fail("Script " + path + " fails with exception :" + e.getMessage());
       
    66             }
       
    67         }
       
    68     }
       
    69 
       
    70     @Test
       
    71     public void noJavaOptionTest() {
       
    72         final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
       
    73         final ScriptEngine engine = factory.getScriptEngine(new String[]{"--no-java"}, getClass().getClassLoader(), getClassFilter());
       
    74         try {
       
    75             engine.eval("var str = Java.type('java.lang.String');");
       
    76             fail("TypeError should have been thrown");
       
    77         } catch (final ScriptException e) {
       
    78             //emtpy
       
    79         }
       
    80     }
       
    81 
       
    82     @Test
       
    83     public void securityTest() {
       
    84         if (System.getSecurityManager() == null) {
       
    85             return;
       
    86         }
       
    87 
       
    88         final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
       
    89         final ScriptEngine engine = factory.getScriptEngine(getClassFilter());
       
    90         try {
       
    91             engine.eval("var thread = Java.type('sun.misc.Unsafe')");
       
    92             fail("SecurityException should have been thrown");
       
    93         } catch (final Exception e) {
       
    94             //empty
       
    95         }
       
    96         try {
       
    97             engine.eval("var thread = new sun.misc.Unsafe()");
       
    98             fail("SecurityException should have been thrown");
       
    99         } catch (final Exception e) {
       
   100             //empty
       
   101         }
       
   102         try {
       
   103             engine.eval("var thread = Java.extend(sun.misc.Unsafe, {})");
       
   104             fail("TypeError should have been thrown");
       
   105         } catch (final Exception e) {
       
   106             //empty
       
   107         }
       
   108         try {
       
   109             engine.eval("java.lang.System.exit(0)");
       
   110             fail("SecurityException should have been thrown");
       
   111         } catch (final Exception e) {
       
   112             //empty
       
   113         }
       
   114 
       
   115     }
       
   116 
       
   117     @Test
       
   118     public void persistentCacheTest() {
       
   119         final String oldCodeCache = System.getProperty(NASHORN_CODE_CACHE);
       
   120         System.setProperty(NASHORN_CODE_CACHE, CLASSFILTER_CODE_CACHE);
       
   121         try {
       
   122             persistentCacheTestImpl();
       
   123         } finally {
       
   124             if (oldCodeCache != null) {
       
   125                 System.setProperty(NASHORN_CODE_CACHE, oldCodeCache);
       
   126             }
       
   127         }
       
   128     }
       
   129 
       
   130     private void persistentCacheTestImpl() {
       
   131         final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
       
   132         final ScriptEngine engine = factory.getScriptEngine(
       
   133               TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}),
       
   134                   getClass().getClassLoader(),
       
   135                   getClassFilter()
       
   136         );
       
   137         final String testScript = "var a = Java.type('java.lang.String');" + generateCodeForPersistentStore();
       
   138         try {
       
   139             engine.eval(testScript);
       
   140         } catch (final ScriptException exc) {
       
   141             fail(exc.getMessage());
       
   142         }
       
   143         final ScriptEngine engineSafe = factory.getScriptEngine(
       
   144                 TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}),
       
   145                 getClass().getClassLoader(),
       
   146                 new ClassFilter() {
       
   147                     @Override
       
   148                     public boolean exposeToScripts(final String s) {
       
   149                         return false;
       
   150                     }
       
   151                 }
       
   152         );
       
   153         try {
       
   154             engineSafe.eval(testScript);
       
   155             fail("ClassNotFoundException should have been thrown");
       
   156         } catch (final Exception exc) {
       
   157             if (!(exc.getCause() instanceof ClassNotFoundException)) {
       
   158                 fail("ClassNotFoundException expected, got " + exc.getClass());
       
   159             }
       
   160         }
       
   161     }
       
   162 
       
   163     private static String generateCodeForPersistentStore() {
       
   164         final StringBuilder stringBuilder = new StringBuilder();
       
   165         for (int i=0; i < 100; i++) {
       
   166             stringBuilder.append("function i")
       
   167                     .append(i)
       
   168                     .append("(y, z) { var x")
       
   169                     .append(i)
       
   170                     .append(" = ")
       
   171                     .append(i)
       
   172                     .append(";}");
       
   173         }
       
   174         return stringBuilder.toString();
       
   175     }
       
   176 
       
   177     private static ClassFilter getClassFilter() {
       
   178         return new ClassFilter() {
       
   179             @Override
       
   180             public boolean exposeToScripts(final String s) {
       
   181                 return true;
       
   182             }
       
   183         };
       
   184     }
       
   185 }