Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/loading/DeserializeEncodedURLTest.java
38867 views
/*1* Copyright (c) 2003, 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* @run clean DeserializeEncodedURLTest SingleClassLoader29* @run build DeserializeEncodedURLTest SingleClassLoader30* @run main DeserializeEncodedURLTest31*/3233import java.io.*;34import java.rmi.*;35import java.util.*;36import javax.management.*;37import javax.management.remote.*;38import javax.management.remote.rmi.*;3940/*41Test that the RMI connector client can handle a URL of the form42where the serialized RMIServer stub is encoded directly in the URL,43when the class of that stub is known to the supplied44DEFAULT_CLASS_LOADER but not to the calling code's class loader.45This is an unusual usage, and is not explicitly specified in the JMX46Remote API, but it is potentially useful where client and server47agree to a code base for mutant stubs (that e.g. use a different48protocol or include debugging or optimization).4950We make an RMI connector server by giving it an instance of an51RMIJRMPServerImpl subclass that manufactures mutant stubs. These52stubs are known to a special loader (mutantLoader) but not to this53test's loader. We set up the client's default loader to54mutantLoader, and check that it can deserialize the stub containing55the mutant stub.5657This test incidentally creates the connector server as an MBean58rather than using the JMXConnectorServerFactory, just because I'm59not sure we have coverage of that elsewhere.60*/61public class DeserializeEncodedURLTest {62private static final ClassLoader mutantLoader =63new SingleClassLoader("SubMutantRMIServerStub",64MutantRMIServerStub.class,65MutantRMIServerStub.class.getClassLoader());66private static final Class subMutantRMIServerStubClass;67static {68try {69subMutantRMIServerStubClass =70mutantLoader.loadClass("SubMutantRMIServerStub");71} catch (ClassNotFoundException e) {72throw new Error(e);73}74}7576public static void main(String[] args) throws Exception {77System.out.println("Check that we can deserialize a mutant stub " +78"from an RMI connector URL even when the stub's " +79"class is known to the user's default loader " +80"but not the caller's loader");8182System.out.println("Create RMI connector server as an MBean");8384MBeanServer mbs = MBeanServerFactory.createMBeanServer();85ObjectName csName = new ObjectName("test:type=RMIConnectorServer");86JMXServiceURL url = new JMXServiceURL("rmi", null, 0);87RMIServerImpl impl = new MutantRMIServerImpl();88mbs.createMBean("javax.management.remote.rmi.RMIConnectorServer",89csName,90new Object[] {url, null, impl, null},91new String[] {JMXServiceURL.class.getName(),92Map.class.getName(),93RMIServerImpl.class.getName(),94MBeanServer.class.getName()});95mbs.invoke(csName, "start", new Object[0], new String[0]);9697JMXServiceURL address =98(JMXServiceURL) mbs.getAttribute(csName, "Address");99100System.out.println("Address with mutant stub: " + address);101102Map env = new HashMap();103env.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER, mutantLoader);104JMXConnector conn = JMXConnectorFactory.newJMXConnector(address, env);105106System.out.println("Client successfully created with this address");107System.out.println("Try to connect newly-created client");108109try {110conn.connect();111System.out.println("TEST FAILS: Connect worked but should not " +112"have");113System.exit(1);114} catch (MutantException e) {115System.out.println("Caught MutantException as expected");116} catch (Exception e) {117System.out.println("TEST FAILS: Caught unexpected exception:");118e.printStackTrace(System.out);119System.exit(1);120}121122mbs.invoke(csName, "stop", new Object[0], new String[0]);123System.out.println("Test passed");124}125126private static class MutantException extends IOException {}127128public static class MutantRMIServerStub129implements RMIServer, Serializable {130public MutantRMIServerStub() {}131132public String getVersion() {133return "1.0 BOGUS";134}135136public RMIConnection newClient(Object credentials) throws IOException {137throw new MutantException();138}139}140141private static class MutantRMIServerImpl extends RMIJRMPServerImpl {142public MutantRMIServerImpl() throws IOException {143super(0, null, null, null);144}145146public Remote toStub() throws IOException {147try {148return (Remote) subMutantRMIServerStubClass.newInstance();149} catch (Exception e) {150IOException ioe =151new IOException("Couldn't make submutant stub");152ioe.initCause(e);153throw ioe;154}155}156}157}158159160