1/*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4924683
27 * @summary Check RMI/JRMP stubs can be deserialized using user's loader
28 * @author Eamonn McManus
29 *
30 * @run clean DeserializeEncodedURLTest SingleClassLoader
31 * @run build DeserializeEncodedURLTest SingleClassLoader
32 * @run main DeserializeEncodedURLTest
33 */
34
35import java.io.*;
36import java.rmi.*;
37import java.util.*;
38import javax.management.*;
39import javax.management.remote.*;
40import javax.management.remote.rmi.*;
41
42/*
43  Test that the RMI connector client can handle a URL of the form
44  where the serialized RMIServer stub is encoded directly in the URL,
45  when the class of that stub is known to the supplied
46  DEFAULT_CLASS_LOADER but not to the calling code's class loader.
47  This is an unusual usage, and is not explicitly specified in the JMX
48  Remote API, but it is potentially useful where client and server
49  agree to a code base for mutant stubs (that e.g. use a different
50  protocol or include debugging or optimization).
51
52  We make an RMI connector server by giving it an instance of an
53  RMIJRMPServerImpl subclass that manufactures mutant stubs.  These
54  stubs are known to a special loader (mutantLoader) but not to this
55  test's loader.  We set up the client's default loader to
56  mutantLoader, and check that it can deserialize the stub containing
57  the mutant stub.
58
59  This test incidentally creates the connector server as an MBean
60  rather than using the JMXConnectorServerFactory, just because I'm
61  not sure we have coverage of that elsewhere.
62*/
63public class DeserializeEncodedURLTest {
64    private static final ClassLoader mutantLoader =
65        new SingleClassLoader("SubMutantRMIServerStub",
66                              MutantRMIServerStub.class,
67                              MutantRMIServerStub.class.getClassLoader());
68    private static final Class subMutantRMIServerStubClass;
69    static {
70        try {
71            subMutantRMIServerStubClass =
72                mutantLoader.loadClass("SubMutantRMIServerStub");
73        } catch (ClassNotFoundException e) {
74            throw new Error(e);
75        }
76    }
77
78    public static void main(String[] args) throws Exception {
79        System.out.println("Check that we can deserialize a mutant stub " +
80                           "from an RMI connector URL even when the stub's " +
81                           "class is known to the user's default loader " +
82                           "but not the caller's loader");
83
84        System.out.println("Create RMI connector server as an MBean");
85
86        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
87        ObjectName csName = new ObjectName("test:type=RMIConnectorServer");
88        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
89        RMIServerImpl impl = new MutantRMIServerImpl();
90        mbs.createMBean("javax.management.remote.rmi.RMIConnectorServer",
91                        csName,
92                        new Object[] {url, null, impl, null},
93                        new String[] {JMXServiceURL.class.getName(),
94                                      Map.class.getName(),
95                                      RMIServerImpl.class.getName(),
96                                      MBeanServer.class.getName()});
97        mbs.invoke(csName, "start", new Object[0], new String[0]);
98
99        JMXServiceURL address =
100            (JMXServiceURL) mbs.getAttribute(csName, "Address");
101
102        System.out.println("Address with mutant stub: " + address);
103
104        Map env = new HashMap();
105        env.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER, mutantLoader);
106        JMXConnector conn = JMXConnectorFactory.newJMXConnector(address, env);
107
108        System.out.println("Client successfully created with this address");
109        System.out.println("Try to connect newly-created client");
110
111        try {
112            conn.connect();
113            System.out.println("TEST FAILS: Connect worked but should not " +
114                               "have");
115            System.exit(1);
116        } catch (MutantException e) {
117            System.out.println("Caught MutantException as expected");
118        } catch (Exception e) {
119            System.out.println("TEST FAILS: Caught unexpected exception:");
120            e.printStackTrace(System.out);
121            System.exit(1);
122        }
123
124        mbs.invoke(csName, "stop", new Object[0], new String[0]);
125        System.out.println("Test passed");
126    }
127
128    private static class MutantException extends IOException {}
129
130    public static class MutantRMIServerStub
131            implements RMIServer, Serializable {
132        public MutantRMIServerStub() {}
133
134        public String getVersion() {
135            return "1.0 BOGUS";
136        }
137
138        public RMIConnection newClient(Object credentials) throws IOException {
139            throw new MutantException();
140        }
141    }
142
143    private static class MutantRMIServerImpl extends RMIJRMPServerImpl {
144        public MutantRMIServerImpl() throws IOException {
145            super(0, null, null, null);
146        }
147
148        public Remote toStub() throws IOException {
149            try {
150                return (Remote) subMutantRMIServerStubClass.newInstance();
151            } catch (Exception e) {
152                IOException ioe =
153                    new IOException("Couldn't make submutant stub");
154                ioe.initCause(e);
155                throw ioe;
156            }
157        }
158    }
159}
160