2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
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 |
* @run clean DeserializeEncodedURLTest SingleClassLoader
|
|
30 |
* @run build DeserializeEncodedURLTest SingleClassLoader
|
|
31 |
* @run main DeserializeEncodedURLTest
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.io.*;
|
|
35 |
import java.rmi.*;
|
|
36 |
import java.util.*;
|
|
37 |
import javax.management.*;
|
|
38 |
import javax.management.remote.*;
|
|
39 |
import javax.management.remote.rmi.*;
|
|
40 |
|
|
41 |
/*
|
|
42 |
Test that the RMI connector client can handle a URL of the form
|
|
43 |
where the serialized RMIServer stub is encoded directly in the URL,
|
|
44 |
when the class of that stub is known to the supplied
|
|
45 |
DEFAULT_CLASS_LOADER but not to the calling code's class loader.
|
|
46 |
This is an unusual usage, and is not explicitly specified in the JMX
|
|
47 |
Remote API, but it is potentially useful where client and server
|
|
48 |
agree to a code base for mutant stubs (that e.g. use a different
|
|
49 |
protocol or include debugging or optimization).
|
|
50 |
|
|
51 |
We make an RMI connector server by giving it an instance of an
|
|
52 |
RMIJRMPServerImpl subclass that manufactures mutant stubs. These
|
|
53 |
stubs are known to a special loader (mutantLoader) but not to this
|
|
54 |
test's loader. We set up the client's default loader to
|
|
55 |
mutantLoader, and check that it can deserialize the stub containing
|
|
56 |
the mutant stub.
|
|
57 |
|
|
58 |
This test incidentally creates the connector server as an MBean
|
|
59 |
rather than using the JMXConnectorServerFactory, just because I'm
|
|
60 |
not sure we have coverage of that elsewhere.
|
|
61 |
*/
|
|
62 |
public class DeserializeEncodedURLTest {
|
|
63 |
private static final ClassLoader mutantLoader =
|
|
64 |
new SingleClassLoader("SubMutantRMIServerStub",
|
|
65 |
MutantRMIServerStub.class,
|
|
66 |
MutantRMIServerStub.class.getClassLoader());
|
|
67 |
private static final Class subMutantRMIServerStubClass;
|
|
68 |
static {
|
|
69 |
try {
|
|
70 |
subMutantRMIServerStubClass =
|
|
71 |
mutantLoader.loadClass("SubMutantRMIServerStub");
|
|
72 |
} catch (ClassNotFoundException e) {
|
|
73 |
throw new Error(e);
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
public static void main(String[] args) throws Exception {
|
|
78 |
System.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");
|
|
82 |
|
|
83 |
System.out.println("Create RMI connector server as an MBean");
|
|
84 |
|
|
85 |
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
|
|
86 |
ObjectName csName = new ObjectName("test:type=RMIConnectorServer");
|
|
87 |
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
|
|
88 |
RMIServerImpl impl = new MutantRMIServerImpl();
|
|
89 |
mbs.createMBean("javax.management.remote.rmi.RMIConnectorServer",
|
|
90 |
csName,
|
|
91 |
new Object[] {url, null, impl, null},
|
|
92 |
new String[] {JMXServiceURL.class.getName(),
|
|
93 |
Map.class.getName(),
|
|
94 |
RMIServerImpl.class.getName(),
|
|
95 |
MBeanServer.class.getName()});
|
|
96 |
mbs.invoke(csName, "start", new Object[0], new String[0]);
|
|
97 |
|
|
98 |
JMXServiceURL address =
|
|
99 |
(JMXServiceURL) mbs.getAttribute(csName, "Address");
|
|
100 |
|
|
101 |
System.out.println("Address with mutant stub: " + address);
|
|
102 |
|
|
103 |
Map env = new HashMap();
|
|
104 |
env.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER, mutantLoader);
|
|
105 |
JMXConnector conn = JMXConnectorFactory.newJMXConnector(address, env);
|
|
106 |
|
|
107 |
System.out.println("Client successfully created with this address");
|
|
108 |
System.out.println("Try to connect newly-created client");
|
|
109 |
|
|
110 |
try {
|
|
111 |
conn.connect();
|
|
112 |
System.out.println("TEST FAILS: Connect worked but should not " +
|
|
113 |
"have");
|
|
114 |
System.exit(1);
|
|
115 |
} catch (MutantException e) {
|
|
116 |
System.out.println("Caught MutantException as expected");
|
|
117 |
} catch (Exception e) {
|
|
118 |
System.out.println("TEST FAILS: Caught unexpected exception:");
|
|
119 |
e.printStackTrace(System.out);
|
|
120 |
System.exit(1);
|
|
121 |
}
|
|
122 |
|
|
123 |
mbs.invoke(csName, "stop", new Object[0], new String[0]);
|
|
124 |
System.out.println("Test passed");
|
|
125 |
}
|
|
126 |
|
|
127 |
private static class MutantException extends IOException {}
|
|
128 |
|
|
129 |
public static class MutantRMIServerStub
|
|
130 |
implements RMIServer, Serializable {
|
|
131 |
public MutantRMIServerStub() {}
|
|
132 |
|
|
133 |
public String getVersion() {
|
|
134 |
return "1.0 BOGUS";
|
|
135 |
}
|
|
136 |
|
|
137 |
public RMIConnection newClient(Object credentials) throws IOException {
|
|
138 |
throw new MutantException();
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
private static class MutantRMIServerImpl extends RMIJRMPServerImpl {
|
|
143 |
public MutantRMIServerImpl() throws IOException {
|
|
144 |
super(0, null, null, null);
|
|
145 |
}
|
|
146 |
|
|
147 |
public Remote toStub() throws IOException {
|
|
148 |
try {
|
|
149 |
return (Remote) subMutantRMIServerStubClass.newInstance();
|
|
150 |
} catch (Exception e) {
|
|
151 |
IOException ioe =
|
|
152 |
new IOException("Couldn't make submutant stub");
|
|
153 |
ioe.initCause(e);
|
|
154 |
throw ioe;
|
|
155 |
}
|
|
156 |
}
|
|
157 |
}
|
|
158 |
}
|