Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/registry/interfaceHash/InterfaceHash.java
38828 views
/*1* Copyright (c) 2001, 2012, 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* @build TestLibrary ReferenceRegistryStub39* @run main/othervm InterfaceHash40*/4142import java.lang.reflect.Constructor;43import java.lang.reflect.Method;44import java.rmi.Remote;45import java.rmi.registry.Registry;46import java.rmi.registry.LocateRegistry;47import java.rmi.server.ObjID;48import java.rmi.server.Operation;49import java.rmi.server.RemoteCall;50import java.rmi.server.RemoteObject;51import java.rmi.server.RemoteRef;52import java.util.Arrays;5354import sun.rmi.server.UnicastRef;55import sun.rmi.transport.LiveRef;56import sun.rmi.transport.tcp.TCPEndpoint;5758public class InterfaceHash {5960private static final int PORT = TestLibrary.getUnusedRandomPort();61private static final String NAME = "WMM";6263public static void main(String[] args) throws Exception {64System.err.println("\nRegression test for bug 4472769");6566System.err.println(67"\n=== verifying that J2SE registry's skeleton uses" +68"\ncorrect interface hash and operation numbers:");6970Registry testImpl = LocateRegistry.createRegistry(PORT);71System.err.println("created test registry on port " + PORT);7273RemoteRef ref = new UnicastRef(74new LiveRef(new ObjID(ObjID.REGISTRY_ID),75new TCPEndpoint("", PORT), false));76Registry referenceStub = new ReferenceRegistryStub(ref);77System.err.println("created reference registry stub: " +78referenceStub);7980referenceStub.bind(NAME, referenceStub);81System.err.println("bound name \"" + NAME + "\" in registry");8283String[] list = referenceStub.list();84System.err.println("list of registry contents: " +85Arrays.asList(list));86if (list.length != 1 || !list[0].equals(NAME)) {87throw new RuntimeException(88"TEST FAILED: unexpected list contents");89}9091Registry result = (Registry) referenceStub.lookup(NAME);92System.err.println("lookup of name \"" + NAME + "\" returned: " +93result);94if (!result.equals(referenceStub)) {95throw new RuntimeException(96"TEST FAILED: unexpected lookup result");97}9899referenceStub.rebind(NAME, referenceStub);100referenceStub.unbind(NAME);101System.err.println("unbound name \"" + NAME + "\"");102103list = referenceStub.list();104System.err.println("list of registry contents: " +105Arrays.asList(list));106if (list.length != 0) {107throw new RuntimeException("TEST FAILED: list not empty");108}109110System.err.println("\n=== verifying that J2SE registry's stub uses" +111"correct interface hash:");112113class FakeRemoteRef implements RemoteRef {114long hash;115int opnum;116public RemoteCall newCall(RemoteObject obj, Operation[] op,117int opnum, long hash)118{119this.hash = hash;120this.opnum = opnum;121throw new UnsupportedOperationException();122}123public void invoke(RemoteCall call) { }124public void done(RemoteCall call) { }125public Object invoke(Remote obj, Method method,126Object[] args, long hash)127{128throw new UnsupportedOperationException();129}130public String getRefClass(java.io.ObjectOutput out) {131return "FakeRemoteRef";132}133public int remoteHashCode() { return 1013; }134public boolean remoteEquals(RemoteRef obj) { return false; }135public String remoteToString() { return "FakeRemoteRef"; }136public void writeExternal(java.io.ObjectOutput out) { }137public void readExternal(java.io.ObjectInput in) { }138}139FakeRemoteRef f = new FakeRemoteRef();140141Registry testRegistry = LocateRegistry.getRegistry(PORT);142System.err.println("created original test registry stub: " +143testRegistry);144145Class stubClass = testRegistry.getClass();146System.err.println("test registry stub class: " + stubClass);147148Constructor cons = stubClass.getConstructor(149new Class[] { RemoteRef.class });150Registry testStub = (Registry) cons.newInstance(151new Object[] { f });152System.err.println("created new instrumented test registry stub: " +153testStub);154155System.err.println("invoking bind:");156try {157testStub.bind(NAME, referenceStub);158} catch (UnsupportedOperationException e) {159}160System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);161if (f.hash != 4905912898345647071L) {162throw new RuntimeException("TEST FAILED: wrong interface hash");163} else if (f.opnum != 0) {164throw new RuntimeException("TEST FAILED: wrong operation number");165}166167System.err.println("invoking list:");168try {169testStub.list();170} catch (UnsupportedOperationException e) {171}172System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);173if (f.hash != 4905912898345647071L) {174throw new RuntimeException("TEST FAILED: wrong interface hash");175} else if (f.opnum != 1) {176throw new RuntimeException("TEST FAILED: wrong operation number");177}178179System.err.println("invoking lookup:");180try {181testStub.lookup(NAME);182} catch (UnsupportedOperationException e) {183}184System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);185if (f.hash != 4905912898345647071L) {186throw new RuntimeException("TEST FAILED: wrong interface hash");187} else if (f.opnum != 2) {188throw new RuntimeException("TEST FAILED: wrong operation number");189}190191System.err.println("invoking rebind:");192try {193testStub.rebind(NAME, referenceStub);194} catch (UnsupportedOperationException e) {195}196System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);197if (f.hash != 4905912898345647071L) {198throw new RuntimeException("TEST FAILED: wrong interface hash");199} else if (f.opnum != 3) {200throw new RuntimeException("TEST FAILED: wrong operation number");201}202203System.err.println("invoking unbind:");204try {205testStub.unbind(NAME);206} catch (UnsupportedOperationException e) {207}208System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);209if (f.hash != 4905912898345647071L) {210throw new RuntimeException("TEST FAILED: wrong interface hash");211} else if (f.opnum != 4) {212throw new RuntimeException("TEST FAILED: wrong operation number");213}214215System.err.println("TEST PASSED");216}217}218219220