test/script/trusted/JDK-8008305.js

Thu, 26 Sep 2013 10:43:59 -0700

author
cl
date
Thu, 26 Sep 2013 10:43:59 -0700
changeset 558
d1e2050e575e
parent 99
d5f74bd2dc20
child 560
a62172fe5bae
permissions
-rw-r--r--

Added tag jdk8-b109 for changeset 6ec2f9e5ed5b

sundar@99 1 /*
sundar@99 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
sundar@99 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
sundar@99 4 *
sundar@99 5 * This code is free software; you can redistribute it and/or modify it
sundar@99 6 * under the terms of the GNU General Public License version 2 only, as
sundar@99 7 * published by the Free Software Foundation.
sundar@99 8 *
sundar@99 9 * This code is distributed in the hope that it will be useful, but WITHOUT
sundar@99 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
sundar@99 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
sundar@99 12 * version 2 for more details (a copy is included in the LICENSE file that
sundar@99 13 * accompanied this code).
sundar@99 14 *
sundar@99 15 * You should have received a copy of the GNU General Public License version
sundar@99 16 * 2 along with this work; if not, write to the Free Software Foundation,
sundar@99 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
sundar@99 18 *
sundar@99 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
sundar@99 20 * or visit www.oracle.com if you need additional information or have any
sundar@99 21 * questions.
sundar@99 22 */
sundar@99 23
sundar@99 24 /**
sundar@99 25 * JDK-8008305: ScriptEngine.eval should offer the ability to provide a codebase *
sundar@99 26 * @test
sundar@99 27 * @run
sundar@99 28 */
sundar@99 29
sundar@99 30 var URLReader = Java.type("jdk.nashorn.api.scripting.URLReader");
sundar@99 31 var File = Java.type("java.io.File");
sundar@99 32 var FileReader = Java.type("java.io.FileReader");
sundar@99 33 var ScriptEngineManager = Java.type("javax.script.ScriptEngineManager");
sundar@99 34 var SecurityException = Java.type("java.lang.SecurityException");
sundar@99 35
sundar@99 36 var m = new ScriptEngineManager();
sundar@99 37 var e = m.getEngineByName("nashorn");
sundar@99 38
sundar@99 39
sundar@99 40 // subtest script file
sundar@99 41 var scriptFile = new File(__DIR__ + "JDK-8008305_subtest.js");
sundar@99 42
sundar@99 43 // evaluate the subtest via a URLReader
sundar@99 44 var res = e.eval(new URLReader(scriptFile.toURI().toURL()));
sundar@99 45
sundar@99 46 // subtest should execute with AllPermission and so return absolute path
sundar@99 47 if (! res.equals(new File(".").getAbsolutePath())) {
sundar@99 48 fail("eval result is not equal to expected value");
sundar@99 49 }
sundar@99 50
sundar@99 51 // try same subtest without URLReader and so it runs with null code source
sundar@99 52 try {
sundar@99 53 e.eval(new FileReader(scriptFile));
sundar@99 54 fail("Expected SecurityException from script!");
sundar@99 55 } catch (e) {
sundar@99 56 if (! (e instanceof SecurityException)) {
sundar@99 57 faile("Expected SecurityException, but got " + e);
sundar@99 58 }
sundar@99 59 }

mercurial