test/script/trusted/JDK-8008305.js

Wed, 12 Feb 2014 11:16:35 -0800

author
asaha
date
Wed, 12 Feb 2014 11:16:35 -0800
changeset 806
7e11bf43f4a2
parent 560
a62172fe5bae
child 952
6d5471a497fb
child 962
ac62e33a99b0
permissions
-rw-r--r--

Added tag jdk8u11-b00 for changeset d88b60cdc8f3

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@560 57 fail("Expected SecurityException, but got " + e);
sundar@99 58 }
sundar@99 59 }

mercurial