1 /*
2  * Copyright (c) 2006, 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 5021246
27  * @summary Check that class downloading is supported by RMI connector
28  * @author Eamonn McManus
29  *
30  * @run main RMIDownloadTest receive without
31  * @run main RMIDownloadTest send without
32  * @run main RMIDownloadTest receive with
33  * @run main RMIDownloadTest send with
34  */
35 
36 /*
37  * This test checks that class downloading is supported by the RMI connector.
38  * We copy a precompiled class file into the temporary directory (which we
39  * assume is not in the classpath).  We also create an instance of that
40  * class using a hardcoded ClassLoader.  Then we try to get a remote attribute
41  * that returns that instance, and we try to set the remote attribute to the
42  * instance.  In both cases, this will only work if the class can be downloaded
43  * based on the codebase that we have set to the temporary directory.  We also
44  * test that it does *not* work when the codebase is not set, in case the test
45  * is succeeding for some other reason.
46  *
47  * We run the test four times, for each combination of (send, receive) x
48  * (with-codebase, without-codebase).  Doing all four tests within the same
49  * run doesn't work, probably because RMI remembers the codebase property
50  * setting at some point.
51  */
52 
53 import java.io.File;
54 import java.io.FileOutputStream;
55 import java.io.OutputStream;
56 import java.lang.management.ManagementFactory;
57 import java.net.URL;
58 import java.security.Permission;
59 import java.util.Arrays;
60 import javax.management.Attribute;
61 import javax.management.MBeanServer;
62 import javax.management.MBeanServerConnection;
63 import javax.management.ObjectName;
64 import javax.management.remote.JMXConnector;
65 import javax.management.remote.JMXConnectorFactory;
66 import javax.management.remote.JMXConnectorServer;
67 import javax.management.remote.JMXConnectorServerFactory;
68 import javax.management.remote.JMXServiceURL;
69 
70 public class RMIDownloadTest {
71     /* Following byte array was produced from this class:
72      *
73      * public class Zooby implements java.io.Serializable {}
74      *
75      * by this program:
76      *
77      * public class MakeZooby {
78      *     public static void main(String[] args) throws Exception {
79      *         int b;
80      *         for (int offset = 0; (b = System.in.read()) >= 0; offset++) {
81      *             System.out.print((byte) b + "," +
82      *                              ((offset % 16) == 15 ? '\n' : ' '));
83      *         }
84      *         System.out.println();
85      *     }
86      * }
87      */
isInstance(Object o, String cn)88     private static final byte[] zoobyClassBytes = {
89         -54, -2, -70, -66, 0, 0, 0, 49, 0, 12, 10, 0, 3, 0, 8, 7,
90         0, 9, 7, 0, 10, 7, 0, 11, 1, 0, 6, 60, 105, 110, 105, 116,
91         62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 12, 0,
92         5, 0, 6, 1, 0, 5, 90, 111, 111, 98, 121, 1, 0, 16, 106, 97,
93         118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 1, 0,
94         20, 106, 97, 118, 97, 47, 105, 111, 47, 83, 101, 114, 105, 97, 108, 105,
95         122, 97, 98, 108, 101, 0, 33, 0, 2, 0, 3, 0, 1, 0, 4, 0,
96         0, 0, 1, 0, 1, 0, 5, 0, 6, 0, 1, 0, 7, 0, 0, 0,
97         17, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0,
98         0, 0, 0, 0,
99     };
100 
101     private static class ZoobyClassLoader extends ClassLoader {
102         protected Class<?> findClass(String name) throws ClassNotFoundException {
103             if (name.equals("Zooby")) {
104                 return super.defineClass(name, zoobyClassBytes,
105                         0, zoobyClassBytes.length);
106             } else
107                 throw new ClassNotFoundException(name);
108         }
109     }
110 
111 
112     private static MBeanServer pmbs;
113     private static ObjectName getSetName;
114     private static GetSet getSetInstance;
115 
116     public static void main(String[] args) throws Exception {
117         int sendIndex = -1;
118         int withIndex = -1;
119         if (args.length == 2) {
120             sendIndex =
121                     Arrays.asList("send", "receive").indexOf(args[0]);
122             withIndex =
123                     Arrays.asList("with", "without").indexOf(args[1]);
124         }
125         if (sendIndex < 0 || withIndex < 0)
126             throw new Exception("Usage: RMIDownloadTest (send|receive) (with|without)");
127         final boolean send = (sendIndex == 0);
128         final boolean with = (withIndex == 0);
129 
130         pmbs = ManagementFactory.getPlatformMBeanServer();
131         getSetName = new ObjectName(":type=GetSet");
132         getSetInstance = new GetSet();
133         pmbs.registerMBean(getSetInstance, getSetName);
134 
test(String proto)135         System.setSecurityManager(new LaidBackSecurityManager());
136 
137 //        System.setProperty("sun.rmi.loader.logLevel", "VERBOSE");
138 
139         String tmpdir = System.getProperty("java.io.tmpdir");
140         String classfile = tmpdir + File.separator + "Zooby.class";
141         File zoobyFile = new File(classfile);
142         zoobyFile.deleteOnExit();
143         OutputStream os = new FileOutputStream(zoobyFile);
144         for (byte b : zoobyClassBytes)
145             os.write(b);
146         os.close();
147 
148         // Check that we can't load the Zooby class from the classpath
149         try {
150             Class.forName("Zooby");
151             throw new Exception("Class \"Zooby\" is in the classpath!");
152         } catch (ClassNotFoundException e) {
153             // OK: expected
154         }
155 
156         if (send)
157             System.out.println("Testing we can send an object from client to server");
158         else
159             System.out.println("Testing we can receive an object from server to client");
160 
161         if (with) {
162             // Test with the codebase property.  Downloading should work.
163             URL zoobyURL = zoobyFile.getParentFile().toURI().toURL();
164             System.setProperty("java.rmi.server.codebase", zoobyURL.toString());
165             System.out.println("Testing with codebase, should work");
166             System.out.println("Codebase is " +
167                     System.getProperty("java.rmi.server.codebase"));
168             test(send, true);
169         } else {
170             // Test without setting the codebase property.
171             // This should not work; if it does it probably means java.io.tmpdir
172             // is in the classpath.
173             System.out.println("Testing without codebase, should fail");
174             test(send, false);
175         }
176 
177     }
178 
179     private static void test(boolean send, boolean shouldWork) throws Exception {
180         try {
181             testWithException(send);
182         } catch (Exception e) {
183             if (shouldWork)
184                 throw e;
185             System.out.println("Got exception as expected: " + e);
186             return;
187         }
188         if (!shouldWork)
189             throw new Exception("Test passed without codebase but should not");
190     }
191 
192     private static void testWithException(boolean send)
193     throws Exception {
194         ClassLoader zoobyCL = new ZoobyClassLoader();
195         Class<?> zoobyClass = Class.forName("Zooby", false, zoobyCL);
196         Object zooby = zoobyClass.newInstance();
197 
198         JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///");
199         JMXConnectorServer cs =
200                 JMXConnectorServerFactory.newJMXConnectorServer(url, null, pmbs);
201         cs.start();
202         JMXServiceURL addr = cs.getAddress();
203         JMXConnector cc = JMXConnectorFactory.connect(addr);
204         MBeanServerConnection mbsc = cc.getMBeanServerConnection();
205 
206         Object rzooby;
207         if (send) {
208             System.out.println("Sending object...");
209             mbsc.setAttribute(getSetName, new Attribute("It", zooby));
210             rzooby = getSetInstance.getIt();
211         } else {
212             System.out.println("Receiving object...");
213             getSetInstance.setIt(zooby);
214             rzooby = mbsc.getAttribute(getSetName, "It");
215         }
216 
217         if (!rzooby.getClass().getName().equals("Zooby")) {
218             throw new Exception("FAILED: remote object is not a Zooby");
219         }
220         if (rzooby.getClass().getClassLoader() ==
221                 zooby.getClass().getClassLoader()) {
222             throw new Exception("FAILED: same class loader: " +
223                     zooby.getClass().getClassLoader());
224         }
225 
226         cc.close();
227         cs.stop();
228     }
229 
230     public static interface GetSetMBean {
231         public Object getIt();
232         public void setIt(Object x);
233     }
234 
235     public static class GetSet implements GetSetMBean {
236         public GetSet() {
237         }
238 
239         public Object getIt() {
240             return what;
241         }
242 
243         public void setIt(Object x) {
244             this.what = x;
245         }
246 
247         private Object what;
248     }
249 
250     public static class LaidBackSecurityManager extends SecurityManager {
251         public void checkPermission(Permission perm) {
252             // OK, dude
253         }
254     }
255 }
256