Path: blob/master/test/jdk/javax/management/remote/mandatory/loading/RMIDownloadTest.java
41159 views
/*1* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 502124626* @summary Check that class downloading is supported by RMI connector27* @author Eamonn McManus28*29* @run main/othervm -Djava.security.manager=allow RMIDownloadTest receive without30* @run main/othervm -Djava.security.manager=allow RMIDownloadTest send without31* @run main/othervm -Djava.security.manager=allow RMIDownloadTest receive with32* @run main/othervm -Djava.security.manager=allow RMIDownloadTest send with33*/3435/*36* This test checks that class downloading is supported by the RMI connector.37* We copy a precompiled class file into the temporary directory (which we38* assume is not in the classpath). We also create an instance of that39* class using a hardcoded ClassLoader. Then we try to get a remote attribute40* that returns that instance, and we try to set the remote attribute to the41* instance. In both cases, this will only work if the class can be downloaded42* based on the codebase that we have set to the temporary directory. We also43* test that it does *not* work when the codebase is not set, in case the test44* is succeeding for some other reason.45*46* We run the test four times, for each combination of (send, receive) x47* (with-codebase, without-codebase). Doing all four tests within the same48* run doesn't work, probably because RMI remembers the codebase property49* setting at some point.50*/5152import java.io.File;53import java.io.FileOutputStream;54import java.io.OutputStream;55import java.lang.management.ManagementFactory;56import java.net.URL;57import java.security.Permission;58import java.util.Arrays;59import javax.management.Attribute;60import javax.management.MBeanServer;61import javax.management.MBeanServerConnection;62import javax.management.ObjectName;63import javax.management.remote.JMXConnector;64import javax.management.remote.JMXConnectorFactory;65import javax.management.remote.JMXConnectorServer;66import javax.management.remote.JMXConnectorServerFactory;67import javax.management.remote.JMXServiceURL;6869public class RMIDownloadTest {70/* Following byte array was produced from this class:71*72* public class Zooby implements java.io.Serializable {}73*74* by this program:75*76* public class MakeZooby {77* public static void main(String[] args) throws Exception {78* int b;79* for (int offset = 0; (b = System.in.read()) >= 0; offset++) {80* System.out.print((byte) b + "," +81* ((offset % 16) == 15 ? '\n' : ' '));82* }83* System.out.println();84* }85* }86*/87private static final byte[] zoobyClassBytes = {88-54, -2, -70, -66, 0, 0, 0, 49, 0, 12, 10, 0, 3, 0, 8, 7,890, 9, 7, 0, 10, 7, 0, 11, 1, 0, 6, 60, 105, 110, 105, 116,9062, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 12, 0,915, 0, 6, 1, 0, 5, 90, 111, 111, 98, 121, 1, 0, 16, 106, 97,92118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 1, 0,9320, 106, 97, 118, 97, 47, 105, 111, 47, 83, 101, 114, 105, 97, 108, 105,94122, 97, 98, 108, 101, 0, 33, 0, 2, 0, 3, 0, 1, 0, 4, 0,950, 0, 1, 0, 1, 0, 5, 0, 6, 0, 1, 0, 7, 0, 0, 0,9617, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0,970, 0, 0, 0,98};99100private static class ZoobyClassLoader extends ClassLoader {101protected Class<?> findClass(String name) throws ClassNotFoundException {102if (name.equals("Zooby")) {103return super.defineClass(name, zoobyClassBytes,1040, zoobyClassBytes.length);105} else106throw new ClassNotFoundException(name);107}108}109110111private static MBeanServer pmbs;112private static ObjectName getSetName;113private static GetSet getSetInstance;114115public static void main(String[] args) throws Exception {116int sendIndex = -1;117int withIndex = -1;118if (args.length == 2) {119sendIndex =120Arrays.asList("send", "receive").indexOf(args[0]);121withIndex =122Arrays.asList("with", "without").indexOf(args[1]);123}124if (sendIndex < 0 || withIndex < 0)125throw new Exception("Usage: RMIDownloadTest (send|receive) (with|without)");126final boolean send = (sendIndex == 0);127final boolean with = (withIndex == 0);128129pmbs = ManagementFactory.getPlatformMBeanServer();130getSetName = new ObjectName(":type=GetSet");131getSetInstance = new GetSet();132pmbs.registerMBean(getSetInstance, getSetName);133134System.setSecurityManager(new LaidBackSecurityManager());135136// System.setProperty("sun.rmi.loader.logLevel", "VERBOSE");137138String tmpdir = System.getProperty("java.io.tmpdir");139String classfile = tmpdir + File.separator + "Zooby.class";140File zoobyFile = new File(classfile);141zoobyFile.deleteOnExit();142OutputStream os = new FileOutputStream(zoobyFile);143for (byte b : zoobyClassBytes)144os.write(b);145os.close();146147// Check that we can't load the Zooby class from the classpath148try {149Class.forName("Zooby");150throw new Exception("Class \"Zooby\" is in the classpath!");151} catch (ClassNotFoundException e) {152// OK: expected153}154155if (send)156System.out.println("Testing we can send an object from client to server");157else158System.out.println("Testing we can receive an object from server to client");159160if (with) {161// Test with the codebase property. Downloading should work.162URL zoobyURL = zoobyFile.getParentFile().toURI().toURL();163System.setProperty("java.rmi.server.codebase", zoobyURL.toString());164System.out.println("Testing with codebase, should work");165System.out.println("Codebase is " +166System.getProperty("java.rmi.server.codebase"));167test(send, true);168} else {169// Test without setting the codebase property.170// This should not work; if it does it probably means java.io.tmpdir171// is in the classpath.172System.out.println("Testing without codebase, should fail");173test(send, false);174}175176}177178private static void test(boolean send, boolean shouldWork) throws Exception {179try {180testWithException(send);181} catch (Exception e) {182if (shouldWork)183throw e;184System.out.println("Got exception as expected: " + e);185return;186}187if (!shouldWork)188throw new Exception("Test passed without codebase but should not");189}190191private static void testWithException(boolean send)192throws Exception {193ClassLoader zoobyCL = new ZoobyClassLoader();194Class<?> zoobyClass = Class.forName("Zooby", false, zoobyCL);195Object zooby = zoobyClass.newInstance();196197JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///");198JMXConnectorServer cs =199JMXConnectorServerFactory.newJMXConnectorServer(url, null, pmbs);200cs.start();201JMXServiceURL addr = cs.getAddress();202JMXConnector cc = JMXConnectorFactory.connect(addr);203MBeanServerConnection mbsc = cc.getMBeanServerConnection();204205Object rzooby;206if (send) {207System.out.println("Sending object...");208mbsc.setAttribute(getSetName, new Attribute("It", zooby));209rzooby = getSetInstance.getIt();210} else {211System.out.println("Receiving object...");212getSetInstance.setIt(zooby);213rzooby = mbsc.getAttribute(getSetName, "It");214}215216if (!rzooby.getClass().getName().equals("Zooby")) {217throw new Exception("FAILED: remote object is not a Zooby");218}219if (rzooby.getClass().getClassLoader() ==220zooby.getClass().getClassLoader()) {221throw new Exception("FAILED: same class loader: " +222zooby.getClass().getClassLoader());223}224225cc.close();226cs.stop();227}228229public static interface GetSetMBean {230public Object getIt();231public void setIt(Object x);232}233234public static class GetSet implements GetSetMBean {235public GetSet() {236}237238public Object getIt() {239return what;240}241242public void setIt(Object x) {243this.what = x;244}245246private Object what;247}248249public static class LaidBackSecurityManager extends SecurityManager {250public void checkPermission(Permission perm) {251// OK, dude252}253}254}255256257