Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/loading/RMIDownloadTest.java
38867 views
/*1* Copyright (c) 2006, 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* @run main RMIDownloadTest receive without29* @run main RMIDownloadTest send without30* @run main RMIDownloadTest receive with31* @run main RMIDownloadTest send with32*/3334/*35* This test checks that class downloading is supported by the RMI connector.36* We copy a precompiled class file into the temporary directory (which we37* assume is not in the classpath). We also create an instance of that38* class using a hardcoded ClassLoader. Then we try to get a remote attribute39* that returns that instance, and we try to set the remote attribute to the40* instance. In both cases, this will only work if the class can be downloaded41* based on the codebase that we have set to the temporary directory. We also42* test that it does *not* work when the codebase is not set, in case the test43* is succeeding for some other reason.44*45* We run the test four times, for each combination of (send, receive) x46* (with-codebase, without-codebase). Doing all four tests within the same47* run doesn't work, probably because RMI remembers the codebase property48* setting at some point.49*/5051import java.io.File;52import java.io.FileOutputStream;53import java.io.OutputStream;54import java.lang.management.ManagementFactory;55import java.net.URL;56import java.security.Permission;57import java.util.Arrays;58import javax.management.Attribute;59import javax.management.MBeanServer;60import javax.management.MBeanServerConnection;61import javax.management.ObjectName;62import javax.management.remote.JMXConnector;63import javax.management.remote.JMXConnectorFactory;64import javax.management.remote.JMXConnectorServer;65import javax.management.remote.JMXConnectorServerFactory;66import javax.management.remote.JMXServiceURL;6768public class RMIDownloadTest {69/* Following byte array was produced from this class:70*71* public class Zooby implements java.io.Serializable {}72*73* by this program:74*75* public class MakeZooby {76* public static void main(String[] args) throws Exception {77* int b;78* for (int offset = 0; (b = System.in.read()) >= 0; offset++) {79* System.out.print((byte) b + "," +80* ((offset % 16) == 15 ? '\n' : ' '));81* }82* System.out.println();83* }84* }85*/86private static final byte[] zoobyClassBytes = {87-54, -2, -70, -66, 0, 0, 0, 49, 0, 12, 10, 0, 3, 0, 8, 7,880, 9, 7, 0, 10, 7, 0, 11, 1, 0, 6, 60, 105, 110, 105, 116,8962, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 12, 0,905, 0, 6, 1, 0, 5, 90, 111, 111, 98, 121, 1, 0, 16, 106, 97,91118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 1, 0,9220, 106, 97, 118, 97, 47, 105, 111, 47, 83, 101, 114, 105, 97, 108, 105,93122, 97, 98, 108, 101, 0, 33, 0, 2, 0, 3, 0, 1, 0, 4, 0,940, 0, 1, 0, 1, 0, 5, 0, 6, 0, 1, 0, 7, 0, 0, 0,9517, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0,960, 0, 0, 0,97};9899private static class ZoobyClassLoader extends ClassLoader {100protected Class<?> findClass(String name) throws ClassNotFoundException {101if (name.equals("Zooby")) {102return super.defineClass(name, zoobyClassBytes,1030, zoobyClassBytes.length);104} else105throw new ClassNotFoundException(name);106}107}108109110private static MBeanServer pmbs;111private static ObjectName getSetName;112private static GetSet getSetInstance;113114public static void main(String[] args) throws Exception {115int sendIndex = -1;116int withIndex = -1;117if (args.length == 2) {118sendIndex =119Arrays.asList("send", "receive").indexOf(args[0]);120withIndex =121Arrays.asList("with", "without").indexOf(args[1]);122}123if (sendIndex < 0 || withIndex < 0)124throw new Exception("Usage: RMIDownloadTest (send|receive) (with|without)");125final boolean send = (sendIndex == 0);126final boolean with = (withIndex == 0);127128pmbs = ManagementFactory.getPlatformMBeanServer();129getSetName = new ObjectName(":type=GetSet");130getSetInstance = new GetSet();131pmbs.registerMBean(getSetInstance, getSetName);132133System.setSecurityManager(new LaidBackSecurityManager());134135// System.setProperty("sun.rmi.loader.logLevel", "VERBOSE");136137String tmpdir = System.getProperty("java.io.tmpdir");138String classfile = tmpdir + File.separator + "Zooby.class";139File zoobyFile = new File(classfile);140zoobyFile.deleteOnExit();141OutputStream os = new FileOutputStream(zoobyFile);142for (byte b : zoobyClassBytes)143os.write(b);144os.close();145146// Check that we can't load the Zooby class from the classpath147try {148Class.forName("Zooby");149throw new Exception("Class \"Zooby\" is in the classpath!");150} catch (ClassNotFoundException e) {151// OK: expected152}153154if (send)155System.out.println("Testing we can send an object from client to server");156else157System.out.println("Testing we can receive an object from server to client");158159if (with) {160// Test with the codebase property. Downloading should work.161URL zoobyURL = zoobyFile.getParentFile().toURI().toURL();162System.setProperty("java.rmi.server.codebase", zoobyURL.toString());163System.out.println("Testing with codebase, should work");164System.out.println("Codebase is " +165System.getProperty("java.rmi.server.codebase"));166test(send, true);167} else {168// Test without setting the codebase property.169// This should not work; if it does it probably means java.io.tmpdir170// is in the classpath.171System.out.println("Testing without codebase, should fail");172test(send, false);173}174175}176177private static void test(boolean send, boolean shouldWork) throws Exception {178try {179testWithException(send);180} catch (Exception e) {181if (shouldWork)182throw e;183System.out.println("Got exception as expected: " + e);184return;185}186if (!shouldWork)187throw new Exception("Test passed without codebase but should not");188}189190private static void testWithException(boolean send)191throws Exception {192ClassLoader zoobyCL = new ZoobyClassLoader();193Class<?> zoobyClass = Class.forName("Zooby", false, zoobyCL);194Object zooby = zoobyClass.newInstance();195196JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///");197JMXConnectorServer cs =198JMXConnectorServerFactory.newJMXConnectorServer(url, null, pmbs);199cs.start();200JMXServiceURL addr = cs.getAddress();201JMXConnector cc = JMXConnectorFactory.connect(addr);202MBeanServerConnection mbsc = cc.getMBeanServerConnection();203204Object rzooby;205if (send) {206System.out.println("Sending object...");207mbsc.setAttribute(getSetName, new Attribute("It", zooby));208rzooby = getSetInstance.getIt();209} else {210System.out.println("Receiving object...");211getSetInstance.setIt(zooby);212rzooby = mbsc.getAttribute(getSetName, "It");213}214215if (!rzooby.getClass().getName().equals("Zooby")) {216throw new Exception("FAILED: remote object is not a Zooby");217}218if (rzooby.getClass().getClassLoader() ==219zooby.getClass().getClassLoader()) {220throw new Exception("FAILED: same class loader: " +221zooby.getClass().getClassLoader());222}223224cc.close();225cs.stop();226}227228public static interface GetSetMBean {229public Object getIt();230public void setIt(Object x);231}232233public static class GetSet implements GetSetMBean {234public GetSet() {235}236237public Object getIt() {238return what;239}240241public void setIt(Object x) {242this.what = x;243}244245private Object what;246}247248public static class LaidBackSecurityManager extends SecurityManager {249public void checkPermission(Permission perm) {250// OK, dude251}252}253}254255256