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