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