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