1 /*
   2  * Copyright (c) 2010, 2013, 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-8008305: ScriptEngine.eval should offer the ability to provide a codebase *
  26  * @test
  27  * @run
  28  */
  29 
  30 var URLReader = Java.type("jdk.nashorn.api.scripting.URLReader");
  31 var File = Java.type("java.io.File");
  32 var FileReader = Java.type("java.io.FileReader");
  33 var ScriptEngineManager = Java.type("javax.script.ScriptEngineManager");
  34 var SecurityException = Java.type("java.lang.SecurityException");
  35 
  36 var m = new ScriptEngineManager();
  37 var e = m.getEngineByName("nashorn");
  38 
  39 
  40 // subtest script file
  41 var scriptFile = new File(__DIR__ + "JDK-8008305_subtest.js");
  42 
  43 // evaluate the subtest via a URLReader
  44 var res = e.eval(new URLReader(scriptFile.toURI().toURL()));
  45 
  46 // subtest should execute with AllPermission and so return absolute path
  47 if (! res.equals(new File(".").getAbsolutePath())) {
  48     fail("eval result is not equal to expected value");
  49 }
  50 
  51 // try same subtest without URLReader and so it runs with null code source
  52 try {
  53     e.eval(new FileReader(scriptFile));
  54     fail("Expected SecurityException from script!");
  55 } catch (e) {
  56     if (! (e instanceof SecurityException)) {
  57         fail("Expected SecurityException, but got " + e);
  58     }
  59 }