1/* 2 * Copyright (c) 2015, 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 26package jdk.nashorn.internal.runtime.test; 27 28import static org.testng.Assert.assertFalse; 29import static org.testng.Assert.assertTrue; 30 31import java.util.Map; 32import javax.script.Bindings; 33import jdk.nashorn.api.scripting.JSObject; 34import jdk.nashorn.api.scripting.ScriptObjectMirror; 35import jdk.nashorn.internal.objects.Global; 36import jdk.nashorn.internal.objects.NativeArray; 37import jdk.nashorn.internal.runtime.Context; 38import jdk.nashorn.internal.runtime.ErrorManager; 39import jdk.nashorn.internal.runtime.ScriptObject; 40import jdk.nashorn.internal.runtime.linker.Bootstrap; 41import jdk.nashorn.internal.runtime.options.Options; 42import org.testng.annotations.AfterClass; 43import org.testng.annotations.BeforeClass; 44import org.testng.annotations.Test; 45 46/** 47 * @test 48 * @bug 8078414 49 * @summary Test that arbitrary classes can't be converted to mirror's superclasses/interfaces. 50 * @modules jdk.scripting.nashorn/jdk.nashorn.internal.runtime 51 * @modules jdk.scripting.nashorn/jdk.nashorn.internal.runtime.linker 52 * @modules jdk.scripting.nashorn/jdk.nashorn.internal.runtime.options 53 * @modules jdk.scripting.nashorn/jdk.nashorn.internal.objects 54 * @run testng jdk.nashorn.internal.runtime.test.JDK_8078414_Test 55 */ 56public class JDK_8078414_Test { 57 private static Context cx; 58 private static Global oldGlobal; 59 60 @BeforeClass 61 public static void beforeClass() { 62 // We must have a Context for the DynamicLinker that Bootstrap.getLinkerServices() will use 63 oldGlobal = Context.getGlobal(); 64 cx = new Context(new Options(""), new ErrorManager(), null); 65 Context.setGlobal(cx.createGlobal()); 66 } 67 68 @AfterClass 69 public static void afterClass() { 70 Context.setGlobal(oldGlobal); 71 oldGlobal = null; 72 cx = null; 73 } 74 75 @Test 76 public void testCanNotConvertArbitraryClassToMirror() { 77 assertCanNotConvert(Double.class, Map.class); 78 assertCanNotConvert(Double.class, Bindings.class); 79 assertCanNotConvert(Double.class, JSObject.class); 80 assertCanNotConvert(Double.class, ScriptObjectMirror.class); 81 } 82 83 @Test 84 public void testCanConvertObjectToMirror() { 85 assertCanConvertToMirror(Object.class); 86 } 87 88 @Test 89 public void testCanConvertScriptObjectToMirror() { 90 assertCanConvertToMirror(ScriptObject.class); 91 } 92 93 @Test 94 public void testCanConvertScriptObjectSubclassToMirror() { 95 assertCanConvertToMirror(NativeArray.class); 96 } 97 98 @Test 99 public void testCanConvertArbitraryInterfaceToMirror() { 100 // We allow arbitrary interface classes, depending on what implements them, to end up being 101 // convertible to ScriptObjectMirror, as an implementation can theoretically pass an 102 // "instanceof ScriptObject" guard. 103 assertCanConvertToMirror(TestInterface.class); 104 } 105 106 public static interface TestInterface { 107 } 108 109 private static boolean canConvert(final Class<?> from, final Class<?> to) { 110 return Bootstrap.getLinkerServices().canConvert(from, to); 111 } 112 113 private static void assertCanConvert(final Class<?> from, final Class<?> to) { 114 assertTrue(canConvert(from, to)); 115 } 116 117 private static void assertCanNotConvert(final Class<?> from, final Class<?> to) { 118 assertFalse(canConvert(from, to)); 119 } 120 121 private static void assertCanConvertToMirror(final Class<?> from) { 122 assertCanConvert(from, Map.class); 123 assertCanConvert(from, Bindings.class); 124 assertCanConvert(from, JSObject.class); 125 assertCanConvert(from, ScriptObjectMirror.class); 126 } 127} 128