Path: blob/master/test/jdk/javax/management/remote/mandatory/loading/DeserializeEncodedURLTest.java
41159 views
/*1* Copyright (c) 2003, 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 492468326* @summary Check RMI/JRMP stubs can be deserialized using user's loader27* @author Eamonn McManus28*29* @run clean DeserializeEncodedURLTest SingleClassLoader30* @run build DeserializeEncodedURLTest SingleClassLoader31* @run main DeserializeEncodedURLTest32*/3334import java.io.*;35import java.rmi.*;36import java.util.*;37import javax.management.*;38import javax.management.remote.*;39import javax.management.remote.rmi.*;4041/*42Test that the RMI connector client can handle a URL of the form43where the serialized RMIServer stub is encoded directly in the URL,44when the class of that stub is known to the supplied45DEFAULT_CLASS_LOADER but not to the calling code's class loader.46This is an unusual usage, and is not explicitly specified in the JMX47Remote API, but it is potentially useful where client and server48agree to a code base for mutant stubs (that e.g. use a different49protocol or include debugging or optimization).5051We make an RMI connector server by giving it an instance of an52RMIJRMPServerImpl subclass that manufactures mutant stubs. These53stubs are known to a special loader (mutantLoader) but not to this54test's loader. We set up the client's default loader to55mutantLoader, and check that it can deserialize the stub containing56the mutant stub.5758This test incidentally creates the connector server as an MBean59rather than using the JMXConnectorServerFactory, just because I'm60not sure we have coverage of that elsewhere.61*/62public class DeserializeEncodedURLTest {63private static final ClassLoader mutantLoader =64new SingleClassLoader("SubMutantRMIServerStub",65MutantRMIServerStub.class,66MutantRMIServerStub.class.getClassLoader());67private static final Class subMutantRMIServerStubClass;68static {69try {70subMutantRMIServerStubClass =71mutantLoader.loadClass("SubMutantRMIServerStub");72} catch (ClassNotFoundException e) {73throw new Error(e);74}75}7677public static void main(String[] args) throws Exception {78System.out.println("Check that we can deserialize a mutant stub " +79"from an RMI connector URL even when the stub's " +80"class is known to the user's default loader " +81"but not the caller's loader");8283System.out.println("Create RMI connector server as an MBean");8485MBeanServer mbs = MBeanServerFactory.createMBeanServer();86ObjectName csName = new ObjectName("test:type=RMIConnectorServer");87JMXServiceURL url = new JMXServiceURL("rmi", null, 0);88RMIServerImpl impl = new MutantRMIServerImpl();89mbs.createMBean("javax.management.remote.rmi.RMIConnectorServer",90csName,91new Object[] {url, null, impl, null},92new String[] {JMXServiceURL.class.getName(),93Map.class.getName(),94RMIServerImpl.class.getName(),95MBeanServer.class.getName()});96mbs.invoke(csName, "start", new Object[0], new String[0]);9798JMXServiceURL address =99(JMXServiceURL) mbs.getAttribute(csName, "Address");100101System.out.println("Address with mutant stub: " + address);102103Map env = new HashMap();104env.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER, mutantLoader);105JMXConnector conn = JMXConnectorFactory.newJMXConnector(address, env);106107System.out.println("Client successfully created with this address");108System.out.println("Try to connect newly-created client");109110try {111conn.connect();112System.out.println("TEST FAILS: Connect worked but should not " +113"have");114System.exit(1);115} catch (MutantException e) {116System.out.println("Caught MutantException as expected");117} catch (Exception e) {118System.out.println("TEST FAILS: Caught unexpected exception:");119e.printStackTrace(System.out);120System.exit(1);121}122123mbs.invoke(csName, "stop", new Object[0], new String[0]);124System.out.println("Test passed");125}126127private static class MutantException extends IOException {}128129public static class MutantRMIServerStub130implements RMIServer, Serializable {131public MutantRMIServerStub() {}132133public String getVersion() {134return "1.0 BOGUS";135}136137public RMIConnection newClient(Object credentials) throws IOException {138throw new MutantException();139}140}141142private static class MutantRMIServerImpl extends RMIJRMPServerImpl {143public MutantRMIServerImpl() throws IOException {144super(0, null, null, null);145}146147public Remote toStub() throws IOException {148try {149return (Remote) subMutantRMIServerStubClass.newInstance();150} catch (Exception e) {151IOException ioe =152new IOException("Couldn't make submutant stub");153ioe.initCause(e);154throw ioe;155}156}157}158}159160161