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 package jdk.dynalink.test; 26 27 import java.lang.invoke.CallSite; 28 import java.lang.invoke.MethodHandle; 29 import java.lang.invoke.MethodHandles; 30 import java.lang.invoke.MethodType; 31 import java.util.List; 32 import java.util.ServiceConfigurationError; 33 import jdk.dynalink.CallSiteDescriptor; 34 import jdk.dynalink.DynamicLinker; 35 import jdk.dynalink.DynamicLinkerFactory; 36 import jdk.dynalink.NoSuchDynamicMethodException; 37 import jdk.dynalink.Operation; 38 import jdk.dynalink.StandardOperation; 39 import jdk.dynalink.linker.GuardingDynamicLinker; 40 import jdk.dynalink.linker.LinkRequest; 41 import jdk.dynalink.linker.LinkerServices; 42 import jdk.dynalink.support.SimpleRelinkableCallSite; 43 import jdk.dynalink.linker.GuardedInvocation; 44 import org.testng.Assert; 45 import org.testng.annotations.Test; 46 47 @SuppressWarnings("javadoc") 48 public class DynamicLinkerFactoryTest { 49 50 private static DynamicLinkerFactory newDynamicLinkerFactory(final boolean resetClassLoader) { 51 final DynamicLinkerFactory factory = new DynamicLinkerFactory(); 52 if (resetClassLoader) { 53 factory.setClassLoader(null); 54 } 55 return factory; 56 } 57 58 @Test 59 public void callSiteCreationTest() { 60 final DynamicLinkerFactory factory = newDynamicLinkerFactory(true); 61 final DynamicLinker linker = factory.createLinker(); 62 final StandardOperation[] operations = StandardOperation.values(); 63 final MethodType mt = MethodType.methodType(Object.class, Object.class); 236 // we should still get one error due to untrusted dynamic linker exporter! 237 checkOneAutoLoadingError(factory); 238 239 final MethodType mt = MethodType.methodType(Object.class, Object.class); 240 // create a callsite with TestLinkerOperation 241 final CallSite cs = linker.link(new SimpleRelinkableCallSite(new CallSiteDescriptor( 242 MethodHandles.publicLookup(), new TestLinkerOperation(), mt))); 243 boolean reachedAutoLinker = false; 244 245 246 try { 247 cs.getTarget().invoke(new Object()); 248 } catch (ReachedAutoLoadedDynamicLinkerException e) { 249 // TrustedGuardingDynamicLinkerExporter threw exception on TestLinkerOperation as expected! 250 reachedAutoLinker = true; 251 } catch (Throwable th) { 252 throw new RuntimeException(th); 253 } 254 255 Assert.assertTrue(reachedAutoLinker); 256 } 257 } | 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 package jdk.dynalink.test; 26 27 import java.lang.invoke.CallSite; 28 import java.lang.invoke.MethodHandle; 29 import java.lang.invoke.MethodHandles; 30 import java.lang.invoke.MethodType; 31 import java.util.List; 32 import java.util.ServiceConfigurationError; 33 import javax.script.ScriptEngine; 34 import javax.script.ScriptEngineManager; 35 import jdk.dynalink.CallSiteDescriptor; 36 import jdk.dynalink.DynamicLinker; 37 import jdk.dynalink.DynamicLinkerFactory; 38 import jdk.dynalink.NoSuchDynamicMethodException; 39 import jdk.dynalink.NamedOperation; 40 import jdk.dynalink.Operation; 41 import jdk.dynalink.StandardOperation; 42 import jdk.dynalink.linker.GuardingDynamicLinker; 43 import jdk.dynalink.linker.LinkRequest; 44 import jdk.dynalink.linker.LinkerServices; 45 import jdk.dynalink.support.SimpleRelinkableCallSite; 46 import jdk.dynalink.linker.GuardedInvocation; 47 import jdk.nashorn.api.scripting.AbstractJSObject; 48 import org.testng.Assert; 49 import org.testng.annotations.Test; 50 51 @SuppressWarnings("javadoc") 52 public class DynamicLinkerFactoryTest { 53 54 private static DynamicLinkerFactory newDynamicLinkerFactory(final boolean resetClassLoader) { 55 final DynamicLinkerFactory factory = new DynamicLinkerFactory(); 56 if (resetClassLoader) { 57 factory.setClassLoader(null); 58 } 59 return factory; 60 } 61 62 @Test 63 public void callSiteCreationTest() { 64 final DynamicLinkerFactory factory = newDynamicLinkerFactory(true); 65 final DynamicLinker linker = factory.createLinker(); 66 final StandardOperation[] operations = StandardOperation.values(); 67 final MethodType mt = MethodType.methodType(Object.class, Object.class); 240 // we should still get one error due to untrusted dynamic linker exporter! 241 checkOneAutoLoadingError(factory); 242 243 final MethodType mt = MethodType.methodType(Object.class, Object.class); 244 // create a callsite with TestLinkerOperation 245 final CallSite cs = linker.link(new SimpleRelinkableCallSite(new CallSiteDescriptor( 246 MethodHandles.publicLookup(), new TestLinkerOperation(), mt))); 247 boolean reachedAutoLinker = false; 248 249 250 try { 251 cs.getTarget().invoke(new Object()); 252 } catch (ReachedAutoLoadedDynamicLinkerException e) { 253 // TrustedGuardingDynamicLinkerExporter threw exception on TestLinkerOperation as expected! 254 reachedAutoLinker = true; 255 } catch (Throwable th) { 256 throw new RuntimeException(th); 257 } 258 259 Assert.assertTrue(reachedAutoLinker); 260 } 261 262 @Test 263 public void nashornExportedLinkersTest() { 264 final DynamicLinkerFactory factory = newDynamicLinkerFactory(false); 265 final DynamicLinker linker = factory.createLinker(); 266 267 final MethodType mt = MethodType.methodType(Object.class, Object.class); 268 final NamedOperation op = new NamedOperation(StandardOperation.GET_PROPERTY, "foo"); 269 final CallSite cs = linker.link(new SimpleRelinkableCallSite(new CallSiteDescriptor( 270 MethodHandles.publicLookup(), op, mt))); 271 final boolean[] reachedGetMember = new boolean[1]; 272 // check that the nashorn exported linker can be used for user defined JSObject 273 Object obj = new AbstractJSObject() { 274 @Override 275 public Object getMember(String name) { 276 reachedGetMember[0] = true; 277 return name.equals("foo")? "bar" : "<unknown>"; 278 } 279 }; 280 281 Object value = null; 282 try { 283 value = cs.getTarget().invoke(obj); 284 } catch (Throwable th) { 285 throw new RuntimeException(th); 286 } 287 288 Assert.assertTrue(reachedGetMember[0]); 289 Assert.assertEquals(value, "bar"); 290 291 // check that the nashorn exported linker can be used for ScriptObjectMirror 292 final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); 293 try { 294 obj = engine.eval("({ foo: 'hello' })"); 295 value = cs.getTarget().invoke(obj); 296 } catch (Throwable th) { 297 throw new RuntimeException(th); 298 } 299 Assert.assertEquals(value, "hello"); 300 } 301 } |