1 /*
2  * Copyright (c) 2001, 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 4472769
26  * @summary Stubs and skeletons used to implement the RMI registry
27  * implementation and the bootstrap stubs must always follow certain
28  * "well known" protocols so that they otherwise need not be in sync--
29  * in other words, a registry stub from any arbitrary J2SE vendor and
30  * version must be able to communicate with a registry skeleton from
31  * any other arbitrary J2SE vendor and version.  In addition to
32  * (unfortunately) using the old "-v1.1" stub/skeleton invocation
33  * protocol, with its unevolvable operation number scheme, they must
34  * always use the same value for the -v1.1 stub/skeleton
35  * "interface hash": 4905912898345647071L.
36  *
37  * @author Peter Jones
38  * @library ../../testlibrary
39  * @modules java.rmi/sun.rmi.registry
40  *          java.rmi/sun.rmi.server
41  *          java.rmi/sun.rmi.transport
42  *          java.rmi/sun.rmi.transport.tcp
43  * @build TestLibrary ReferenceRegistryStub
44  * @run main/othervm InterfaceHash
45  */
46 
47 import java.lang.reflect.Constructor;
48 import java.lang.reflect.Method;
49 import java.rmi.Remote;
50 import java.rmi.registry.Registry;
51 import java.rmi.registry.LocateRegistry;
52 import java.rmi.server.ObjID;
53 import java.rmi.server.Operation;
54 import java.rmi.server.RemoteCall;
55 import java.rmi.server.RemoteObject;
56 import java.rmi.server.RemoteRef;
57 import java.util.Arrays;
58 
59 import sun.rmi.server.UnicastRef;
60 import sun.rmi.transport.LiveRef;
61 import sun.rmi.transport.tcp.TCPEndpoint;
62 
63 public class InterfaceHash {
64 
65     private static final String NAME = "WMM";
66 
main(String[] args)67     public static void main(String[] args) throws Exception {
68         System.err.println("\nRegression test for bug 4472769");
69 
70         System.err.println(
71             "\n=== verifying that J2SE registry's skeleton uses" +
72             "\ncorrect interface hash and operation numbers:");
73 
74         Registry testImpl = TestLibrary.createRegistryOnEphemeralPort();
75         int regPort = TestLibrary.getRegistryPort(testImpl);
76         System.err.println("created test registry on port " + regPort);
77 
78         RemoteRef ref = new UnicastRef(
79             new LiveRef(new ObjID(ObjID.REGISTRY_ID),
80                         new TCPEndpoint("", regPort), false));
81         Registry referenceStub = new ReferenceRegistryStub(ref);
82         System.err.println("created reference registry stub: " +
83                            referenceStub);
84 
85         referenceStub.bind(NAME, referenceStub);
86         System.err.println("bound name \"" + NAME + "\" in registry");
87 
88         String[] list = referenceStub.list();
89         System.err.println("list of registry contents: " +
90                            Arrays.asList(list));
91         if (list.length != 1 || !list[0].equals(NAME)) {
92             throw new RuntimeException(
93                 "TEST FAILED: unexpected list contents");
94         }
95 
96         Registry result = (Registry) referenceStub.lookup(NAME);
97         System.err.println("lookup of name \"" + NAME + "\" returned: " +
98                            result);
99         if (!result.equals(referenceStub)) {
100             throw new RuntimeException(
101                 "TEST FAILED: unexpected lookup result");
102         }
103 
104         referenceStub.rebind(NAME, referenceStub);
105         referenceStub.unbind(NAME);
106         System.err.println("unbound name \"" + NAME + "\"");
107 
108         list = referenceStub.list();
109         System.err.println("list of registry contents: " +
110                            Arrays.asList(list));
111         if (list.length != 0) {
112             throw new RuntimeException("TEST FAILED: list not empty");
113         }
114 
115         System.err.println("\n=== verifying that J2SE registry's stub uses" +
116                            "correct interface hash:");
117 
118         class FakeRemoteRef implements RemoteRef {
119             long hash;
120             int opnum;
121             public RemoteCall newCall(RemoteObject obj, Operation[] op,
122                                       int opnum, long hash)
123             {
124                 this.hash = hash;
125                 this.opnum = opnum;
126                 throw new UnsupportedOperationException();
127             }
128             public void invoke(RemoteCall call) { }
129             public void done(RemoteCall call) { }
130             public Object invoke(Remote obj, Method method,
131                                  Object[] args, long hash)
132             {
133                 throw new UnsupportedOperationException();
134             }
135             public String getRefClass(java.io.ObjectOutput out) {
136                 return "FakeRemoteRef";
137             }
138             public int remoteHashCode() { return 1013; }
139             public boolean remoteEquals(RemoteRef obj) { return false; }
140             public String remoteToString() { return "FakeRemoteRef"; }
141             public void writeExternal(java.io.ObjectOutput out) { }
142             public void readExternal(java.io.ObjectInput in) { }
143         }
144         FakeRemoteRef f = new FakeRemoteRef();
145 
146         Registry testRegistry = LocateRegistry.getRegistry(regPort);
147         System.err.println("created original test registry stub: " +
148                            testRegistry);
149 
150         Class stubClass = testRegistry.getClass();
151         System.err.println("test registry stub class: " + stubClass);
152 
153         Constructor cons = stubClass.getConstructor(
154             new Class[] { RemoteRef.class });
155         Registry testStub = (Registry) cons.newInstance(
156             new Object[] { f });
157         System.err.println("created new instrumented test registry stub: " +
158                            testStub);
159 
160         System.err.println("invoking bind:");
161         try {
162             testStub.bind(NAME, referenceStub);
163         } catch (UnsupportedOperationException e) {
164         }
165         System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
166         if (f.hash != 4905912898345647071L) {
167             throw new RuntimeException("TEST FAILED: wrong interface hash");
168         } else if (f.opnum != 0) {
169             throw new RuntimeException("TEST FAILED: wrong operation number");
170         }
171 
172         System.err.println("invoking list:");
173         try {
174             testStub.list();
175         } catch (UnsupportedOperationException e) {
176         }
177         System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
178         if (f.hash != 4905912898345647071L) {
179             throw new RuntimeException("TEST FAILED: wrong interface hash");
180         } else if (f.opnum != 1) {
181             throw new RuntimeException("TEST FAILED: wrong operation number");
182         }
183 
184         System.err.println("invoking lookup:");
185         try {
186             testStub.lookup(NAME);
187         } catch (UnsupportedOperationException e) {
188         }
189         System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
190         if (f.hash != 4905912898345647071L) {
191             throw new RuntimeException("TEST FAILED: wrong interface hash");
192         } else if (f.opnum != 2) {
193             throw new RuntimeException("TEST FAILED: wrong operation number");
194         }
195 
196         System.err.println("invoking rebind:");
197         try {
198             testStub.rebind(NAME, referenceStub);
199         } catch (UnsupportedOperationException e) {
200         }
201         System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
202         if (f.hash != 4905912898345647071L) {
203             throw new RuntimeException("TEST FAILED: wrong interface hash");
204         } else if (f.opnum != 3) {
205             throw new RuntimeException("TEST FAILED: wrong operation number");
206         }
207 
208         System.err.println("invoking unbind:");
209         try {
210             testStub.unbind(NAME);
211         } catch (UnsupportedOperationException e) {
212         }
213         System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
214         if (f.hash != 4905912898345647071L) {
215             throw new RuntimeException("TEST FAILED: wrong interface hash");
216         } else if (f.opnum != 4) {
217             throw new RuntimeException("TEST FAILED: wrong operation number");
218         }
219 
220         System.err.println("TEST PASSED");
221     }
222 }
223