Path: blob/master/test/jdk/java/rmi/registry/interfaceHash/InterfaceHash.java
41153 views
/*1* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 447276925* @summary Stubs and skeletons used to implement the RMI registry26* implementation and the bootstrap stubs must always follow certain27* "well known" protocols so that they otherwise need not be in sync--28* in other words, a registry stub from any arbitrary J2SE vendor and29* version must be able to communicate with a registry skeleton from30* any other arbitrary J2SE vendor and version. In addition to31* (unfortunately) using the old "-v1.1" stub/skeleton invocation32* protocol, with its unevolvable operation number scheme, they must33* always use the same value for the -v1.1 stub/skeleton34* "interface hash": 4905912898345647071L.35*36* @author Peter Jones37* @library ../../testlibrary38* @modules java.rmi/sun.rmi.registry39* java.rmi/sun.rmi.server40* java.rmi/sun.rmi.transport41* java.rmi/sun.rmi.transport.tcp42* @build TestLibrary ReferenceRegistryStub43* @run main/othervm InterfaceHash44*/4546import java.lang.reflect.Constructor;47import java.lang.reflect.Method;48import java.rmi.Remote;49import java.rmi.registry.Registry;50import java.rmi.registry.LocateRegistry;51import java.rmi.server.ObjID;52import java.rmi.server.Operation;53import java.rmi.server.RemoteCall;54import java.rmi.server.RemoteObject;55import java.rmi.server.RemoteRef;56import java.util.Arrays;5758import sun.rmi.server.UnicastRef;59import sun.rmi.transport.LiveRef;60import sun.rmi.transport.tcp.TCPEndpoint;6162public class InterfaceHash {6364private static final String NAME = "WMM";6566public static void main(String[] args) throws Exception {67System.err.println("\nRegression test for bug 4472769");6869System.err.println(70"\n=== verifying that J2SE registry's skeleton uses" +71"\ncorrect interface hash and operation numbers:");7273Registry testImpl = TestLibrary.createRegistryOnEphemeralPort();74int regPort = TestLibrary.getRegistryPort(testImpl);75System.err.println("created test registry on port " + regPort);7677RemoteRef ref = new UnicastRef(78new LiveRef(new ObjID(ObjID.REGISTRY_ID),79new TCPEndpoint("", regPort), false));80Registry referenceStub = new ReferenceRegistryStub(ref);81System.err.println("created reference registry stub: " +82referenceStub);8384referenceStub.bind(NAME, referenceStub);85System.err.println("bound name \"" + NAME + "\" in registry");8687String[] list = referenceStub.list();88System.err.println("list of registry contents: " +89Arrays.asList(list));90if (list.length != 1 || !list[0].equals(NAME)) {91throw new RuntimeException(92"TEST FAILED: unexpected list contents");93}9495Registry result = (Registry) referenceStub.lookup(NAME);96System.err.println("lookup of name \"" + NAME + "\" returned: " +97result);98if (!result.equals(referenceStub)) {99throw new RuntimeException(100"TEST FAILED: unexpected lookup result");101}102103referenceStub.rebind(NAME, referenceStub);104referenceStub.unbind(NAME);105System.err.println("unbound name \"" + NAME + "\"");106107list = referenceStub.list();108System.err.println("list of registry contents: " +109Arrays.asList(list));110if (list.length != 0) {111throw new RuntimeException("TEST FAILED: list not empty");112}113114System.err.println("\n=== verifying that J2SE registry's stub uses" +115"correct interface hash:");116117class FakeRemoteRef implements RemoteRef {118long hash;119int opnum;120public RemoteCall newCall(RemoteObject obj, Operation[] op,121int opnum, long hash)122{123this.hash = hash;124this.opnum = opnum;125throw new UnsupportedOperationException();126}127public void invoke(RemoteCall call) { }128public void done(RemoteCall call) { }129public Object invoke(Remote obj, Method method,130Object[] args, long hash)131{132throw new UnsupportedOperationException();133}134public String getRefClass(java.io.ObjectOutput out) {135return "FakeRemoteRef";136}137public int remoteHashCode() { return 1013; }138public boolean remoteEquals(RemoteRef obj) { return false; }139public String remoteToString() { return "FakeRemoteRef"; }140public void writeExternal(java.io.ObjectOutput out) { }141public void readExternal(java.io.ObjectInput in) { }142}143FakeRemoteRef f = new FakeRemoteRef();144145Registry testRegistry = LocateRegistry.getRegistry(regPort);146System.err.println("created original test registry stub: " +147testRegistry);148149Class stubClass = testRegistry.getClass();150System.err.println("test registry stub class: " + stubClass);151152Constructor cons = stubClass.getConstructor(153new Class[] { RemoteRef.class });154Registry testStub = (Registry) cons.newInstance(155new Object[] { f });156System.err.println("created new instrumented test registry stub: " +157testStub);158159System.err.println("invoking bind:");160try {161testStub.bind(NAME, referenceStub);162} catch (UnsupportedOperationException e) {163}164System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);165if (f.hash != 4905912898345647071L) {166throw new RuntimeException("TEST FAILED: wrong interface hash");167} else if (f.opnum != 0) {168throw new RuntimeException("TEST FAILED: wrong operation number");169}170171System.err.println("invoking list:");172try {173testStub.list();174} catch (UnsupportedOperationException e) {175}176System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);177if (f.hash != 4905912898345647071L) {178throw new RuntimeException("TEST FAILED: wrong interface hash");179} else if (f.opnum != 1) {180throw new RuntimeException("TEST FAILED: wrong operation number");181}182183System.err.println("invoking lookup:");184try {185testStub.lookup(NAME);186} catch (UnsupportedOperationException e) {187}188System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);189if (f.hash != 4905912898345647071L) {190throw new RuntimeException("TEST FAILED: wrong interface hash");191} else if (f.opnum != 2) {192throw new RuntimeException("TEST FAILED: wrong operation number");193}194195System.err.println("invoking rebind:");196try {197testStub.rebind(NAME, referenceStub);198} catch (UnsupportedOperationException e) {199}200System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);201if (f.hash != 4905912898345647071L) {202throw new RuntimeException("TEST FAILED: wrong interface hash");203} else if (f.opnum != 3) {204throw new RuntimeException("TEST FAILED: wrong operation number");205}206207System.err.println("invoking unbind:");208try {209testStub.unbind(NAME);210} catch (UnsupportedOperationException e) {211}212System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);213if (f.hash != 4905912898345647071L) {214throw new RuntimeException("TEST FAILED: wrong interface hash");215} else if (f.opnum != 4) {216throw new RuntimeException("TEST FAILED: wrong operation number");217}218219System.err.println("TEST PASSED");220}221}222223224