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