1 /*
2  * Copyright (c) 1998, 2016, 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 /* @test
25  * @bug 4155894
26  *
27  * @summary remote references can't be downloaded
28  * @author Ann Wollrath
29  *
30  * test currently needs RMISecurityManager because of
31  * 4180392
32  *
33  * @library ../../testlibrary
34  * @modules java.rmi/sun.rmi.registry
35  *          java.rmi/sun.rmi.server
36  *          java.rmi/sun.rmi.transport
37  *          java.rmi/sun.rmi.transport.tcp
38  * @build TestLibrary Ping UseCustomRef_Stub UseCustomRef_Skel
39  * @run main/othervm/policy=security.policy/secure=java.rmi.RMISecurityManager/timeout=120 UseCustomRef
40  *
41  * This test was failing to run because the synthetic access
42  * control context used by the application class loader to find and define
43  * CustomServerRef does not have accessClassInPackage.sun.rmi.server runtime
44  * permission necessary to load its superclass sun.rmi.server.UnicastServerRef,
45  * even though this test's code is granted that permission in its policy file.
46  * That bug number is 4256530
47  */
48 
49 import java.io.*;
50 import java.rmi.*;
51 import java.rmi.server.*;
52 import java.rmi.registry.*;
53 import sun.rmi.transport.LiveRef;
54 
55 public class UseCustomRef
56         extends RemoteServer
57         implements Ping
58 {
59 
UseCustomRef()60     public UseCustomRef() throws RemoteException {
61         exportObject();
62     }
63 
exportObject()64     public void exportObject() throws RemoteException {
65         ref = new CustomServerRef(new LiveRef(0));
66         ((ServerRef) ref).exportObject(this, null);
67     }
68 
getRef()69     public RemoteRef getRef() { return ref; }
70 
ping()71     public void ping() {}
72 
receiveAndPing(Ping p)73     public void receiveAndPing(Ping p) throws RemoteException {
74         p.ping();
75     }
76 
main(String[] args)77     public static void main(String[] args) {
78         Ping obj = null;
79         Registry registry = null;
80 
81         try {
82             /*
83              * create registry
84              */
85             TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
86 
87             System.err.println("creating Registry...");
88 
89             registry = TestLibrary.createRegistryOnEphemeralPort();
90             int port = TestLibrary.getRegistryPort(registry);
91             /*
92              * create object with custom ref and bind in registry
93              */
94             System.err.println("creating UseCustomRef...");
95             UseCustomRef cr = new UseCustomRef();
96             RemoteRef ref = cr.getRef();
97             if (!(ref instanceof CustomServerRef)) {
98                 TestLibrary.bomb("test failed: reference not " +
99                                 "instanceof CustomServerRef");
100             }
101 
102             String name = "//:" + port + "/UseCustomRef";
103             //      String name = "UseCustomRef";
104             System.err.println("binding object in registry...");
105             Naming.rebind(name, cr);
106 
107             /*
108              * look up object and invoke its ping method
109              */
110             System.err.println("ping object...");
111             obj = (Ping) Naming.lookup(name);
112             obj.ping();
113 
114             /*
115              * pass object with custom ref in remote call
116              */
117             System.err.println("pass object in remote call...");
118             obj.receiveAndPing(cr);
119 
120             /*
121              * write remote object with custom ref to output stream
122              */
123             System.err.println("writing remote object to stream...");
124             ByteArrayOutputStream bout = new ByteArrayOutputStream();
125             ObjectOutputStream out = new ObjectOutputStream(bout);
126             out.writeObject(cr);
127             out.flush();
128             out.close();
129 
130             /*
131              * read back remote object from output stream
132              */
133             System.err.println("reading remote object from stream...");
134             ObjectInputStream in = new ObjectInputStream(
135                 new ByteArrayInputStream(bout.toByteArray()));
136             cr = (UseCustomRef) in.readObject();
137 
138             /*
139              * re-export object and ping
140              */
141             System.err.println("re-export object read...");
142             cr.exportObject();
143             System.err.println("look up object again...");
144             Naming.rebind(name, cr);
145             System.err.println("ping object read...");
146             obj = (Ping) Naming.lookup(name);
147             obj.ping();
148             System.err.println("TEST PASSED");
149             Naming.unbind(name);
150             cr = null;
151 
152         } catch (Exception e) {
153             TestLibrary.bomb("test failed with exception: ", e);
154         } finally {
155             TestLibrary.unexport(obj);
156             TestLibrary.unexport(registry);
157 
158             registry = null;
159             obj = null;
160         }
161     }
162 
163     public static class CustomServerRef
164         extends sun.rmi.server.UnicastServerRef
165     {
CustomServerRef()166         public CustomServerRef() {}
167 
CustomServerRef(LiveRef ref)168         public CustomServerRef(LiveRef ref) {
169             super(ref);
170         }
171         /*****
172         public CustomServerRef(int port,
173                                RMIClientSocketFactory csf,
174                                RMIServerSocketFactory ssf)
175         {
176             super (new LiveRef(port, csf, ssf));
177         }
178         *****/
179 
getRefClass(ObjectOutput out)180         public String getRefClass(ObjectOutput out) {
181             return "";
182         }
183 
unmarshalCustomCallData(ObjectInput in)184         protected void unmarshalCustomCallData(ObjectInput in)
185             throws IOException, ClassNotFoundException
186         {
187             System.err.println("unmarshalling call data...");
188             String s = (String) (in.readObject());
189             System.err.println(s);
190         }
191 
getClientRef()192         protected RemoteRef getClientRef() {
193             return new CustomRef(ref);
194         }
195     }
196 
197     public static class CustomRef extends sun.rmi.server.UnicastRef {
198 
CustomRef()199         public CustomRef() {
200         }
201 
CustomRef(sun.rmi.transport.LiveRef ref)202         public CustomRef(sun.rmi.transport.LiveRef ref) {
203             super(ref);
204         }
205 
marshalCustomCallData(ObjectOutput out)206         protected void marshalCustomCallData(ObjectOutput out)
207             throws IOException
208         {
209             // this custom data ensures that a custom server
210             // ref has written the relevant information.
211             System.err.println("marshalling call data...");
212             out.writeObject("hello there.");
213         }
214 
getRefClass(ObjectOutput out)215         public String getRefClass(ObjectOutput out) {
216             return "";
217         }
218 
219     }
220 }
221