3867N/A/*
3867N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
3867N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3867N/A *
3867N/A * This code is free software; you can redistribute it and/or modify it
3867N/A * under the terms of the GNU General Public License version 2 only, as
3867N/A * published by the Free Software Foundation.
3867N/A *
3867N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3867N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3867N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3867N/A * version 2 for more details (a copy is included in the LICENSE file that
3867N/A * accompanied this code).
3867N/A *
3867N/A * You should have received a copy of the GNU General Public License version
3867N/A * 2 along with this work; if not, write to the Free Software Foundation,
3867N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3867N/A *
3867N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3867N/A * or visit www.oracle.com if you need additional information or have any
3867N/A * questions.
3867N/A */
3867N/A
3867N/A/* @test
3867N/A * @bug 4472769
3867N/A * @summary Stubs and skeletons used to implement the RMI registry
3867N/A * implementation and the bootstrap stubs must always follow certain
3867N/A * "well known" protocols so that they otherwise need not be in sync--
3867N/A * in other words, a registry stub from any arbitrary J2SE vendor and
3867N/A * version must be able to communicate with a registry skeleton from
3867N/A * any other arbitrary J2SE vendor and version. In addition to
3867N/A * (unfortunately) using the old "-v1.1" stub/skeleton invocation
3867N/A * protocol, with its unevolvable operation number scheme, they must
3867N/A * always use the same value for the -v1.1 stub/skeleton
3867N/A * "interface hash": 4905912898345647071L.
3867N/A *
3867N/A * @author Peter Jones
3867N/A * @library ../../testlibrary
3867N/A * @build TestLibrary ReferenceRegistryStub
3867N/A * @run main/othervm InterfaceHash
3867N/A */
3867N/A
3867N/Aimport java.lang.reflect.Constructor;
3867N/Aimport java.lang.reflect.Method;
3867N/Aimport java.rmi.Remote;
3867N/Aimport java.rmi.registry.Registry;
3867N/Aimport java.rmi.registry.LocateRegistry;
3867N/Aimport java.rmi.server.ObjID;
3867N/Aimport java.rmi.server.Operation;
3867N/Aimport java.rmi.server.RemoteCall;
3867N/Aimport java.rmi.server.RemoteObject;
3867N/Aimport java.rmi.server.RemoteRef;
3867N/Aimport java.util.Arrays;
3867N/A
3867N/Aimport sun.rmi.server.UnicastRef;
4254N/Aimport sun.rmi.transport.LiveRef;
3867N/Aimport sun.rmi.transport.tcp.TCPEndpoint;
3867N/A
3867N/Apublic class InterfaceHash {
3867N/A
3867N/A private static final int PORT = TestLibrary.getUnusedRandomPort();
4254N/A private static final String NAME = "WMM";
4254N/A
4254N/A public static void main(String[] args) throws Exception {
4254N/A System.err.println("\nRegression test for bug 4472769");
4254N/A
4254N/A System.err.println(
4254N/A "\n=== verifying that J2SE registry's skeleton uses" +
4254N/A "\ncorrect interface hash and operation numbers:");
4254N/A
3867N/A Registry testImpl = LocateRegistry.createRegistry(PORT);
3867N/A System.err.println("created test registry on port " + PORT);
3867N/A
3867N/A RemoteRef ref = new UnicastRef(
3867N/A new LiveRef(new ObjID(ObjID.REGISTRY_ID),
3867N/A new TCPEndpoint("", PORT), false));
4254N/A Registry referenceStub = new ReferenceRegistryStub(ref);
4254N/A System.err.println("created reference registry stub: " +
4254N/A referenceStub);
3867N/A
3867N/A referenceStub.bind(NAME, referenceStub);
3867N/A System.err.println("bound name \"" + NAME + "\" in registry");
3867N/A
3867N/A String[] list = referenceStub.list();
3867N/A System.err.println("list of registry contents: " +
3867N/A Arrays.asList(list));
3867N/A if (list.length != 1 || !list[0].equals(NAME)) {
3867N/A throw new RuntimeException(
3867N/A "TEST FAILED: unexpected list contents");
3867N/A }
3867N/A
4032N/A Registry result = (Registry) referenceStub.lookup(NAME);
3867N/A System.err.println("lookup of name \"" + NAME + "\" returned: " +
3867N/A result);
4032N/A if (!result.equals(referenceStub)) {
3867N/A throw new RuntimeException(
3867N/A "TEST FAILED: unexpected lookup result");
3867N/A }
3867N/A
3867N/A referenceStub.rebind(NAME, referenceStub);
3867N/A referenceStub.unbind(NAME);
3867N/A System.err.println("unbound name \"" + NAME + "\"");
3867N/A
3867N/A list = referenceStub.list();
3867N/A System.err.println("list of registry contents: " +
3867N/A Arrays.asList(list));
3867N/A if (list.length != 0) {
3867N/A throw new RuntimeException("TEST FAILED: list not empty");
3867N/A }
3867N/A
3867N/A System.err.println("\n=== verifying that J2SE registry's stub uses" +
3867N/A "correct interface hash:");
3867N/A
3867N/A class FakeRemoteRef implements RemoteRef {
3867N/A long hash;
3867N/A int opnum;
3867N/A public RemoteCall newCall(RemoteObject obj, Operation[] op,
3867N/A int opnum, long hash)
3867N/A {
3867N/A this.hash = hash;
3867N/A this.opnum = opnum;
3867N/A throw new UnsupportedOperationException();
3867N/A }
3867N/A public void invoke(RemoteCall call) { }
3867N/A public void done(RemoteCall call) { }
3867N/A public Object invoke(Remote obj, Method method,
3867N/A Object[] args, long hash)
3867N/A {
3867N/A throw new UnsupportedOperationException();
3867N/A }
3867N/A public String getRefClass(java.io.ObjectOutput out) {
4254N/A return "FakeRemoteRef";
4254N/A }
4254N/A public int remoteHashCode() { return 1013; }
3867N/A public boolean remoteEquals(RemoteRef obj) { return false; }
3867N/A public String remoteToString() { return "FakeRemoteRef"; }
3867N/A public void writeExternal(java.io.ObjectOutput out) { }
3867N/A public void readExternal(java.io.ObjectInput in) { }
3867N/A }
3867N/A FakeRemoteRef f = new FakeRemoteRef();
3867N/A
3867N/A Registry testRegistry = LocateRegistry.getRegistry(PORT);
3867N/A System.err.println("created original test registry stub: " +
3867N/A testRegistry);
3867N/A
3867N/A Class stubClass = testRegistry.getClass();
3867N/A System.err.println("test registry stub class: " + stubClass);
3867N/A
3867N/A Constructor cons = stubClass.getConstructor(
3884N/A new Class[] { RemoteRef.class });
3867N/A Registry testStub = (Registry) cons.newInstance(
3867N/A new Object[] { f });
3884N/A System.err.println("created new instrumented test registry stub: " +
3867N/A testStub);
3867N/A
4254N/A System.err.println("invoking bind:");
4254N/A try {
4254N/A testStub.bind(NAME, referenceStub);
3867N/A } catch (UnsupportedOperationException e) {
3867N/A }
3867N/A System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
5072N/A if (f.hash != 4905912898345647071L) {
3867N/A throw new RuntimeException("TEST FAILED: wrong interface hash");
4386N/A } else if (f.opnum != 0) {
4386N/A throw new RuntimeException("TEST FAILED: wrong operation number");
4386N/A }
5072N/A
5072N/A System.err.println("invoking list:");
5072N/A try {
3867N/A testStub.list();
3867N/A } catch (UnsupportedOperationException e) {
3867N/A }
3867N/A System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
3867N/A if (f.hash != 4905912898345647071L) {
3867N/A throw new RuntimeException("TEST FAILED: wrong interface hash");
3867N/A } else if (f.opnum != 1) {
3867N/A throw new RuntimeException("TEST FAILED: wrong operation number");
3867N/A }
3867N/A
3867N/A System.err.println("invoking lookup:");
3867N/A try {
3867N/A testStub.lookup(NAME);
3867N/A } catch (UnsupportedOperationException e) {
3867N/A }
3867N/A System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
5072N/A if (f.hash != 4905912898345647071L) {
3867N/A throw new RuntimeException("TEST FAILED: wrong interface hash");
3867N/A } else if (f.opnum != 2) {
3867N/A throw new RuntimeException("TEST FAILED: wrong operation number");
3867N/A }
3867N/A
3867N/A System.err.println("invoking rebind:");
5072N/A try {
3867N/A testStub.rebind(NAME, referenceStub);
3867N/A } catch (UnsupportedOperationException e) {
5072N/A }
3867N/A System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
3867N/A if (f.hash != 4905912898345647071L) {
3867N/A throw new RuntimeException("TEST FAILED: wrong interface hash");
3867N/A } else if (f.opnum != 3) {
5072N/A throw new RuntimeException("TEST FAILED: wrong operation number");
5072N/A }
5072N/A
5072N/A System.err.println("invoking unbind:");
5072N/A try {
5072N/A testStub.unbind(NAME);
5072N/A } catch (UnsupportedOperationException e) {
5072N/A }
3867N/A System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
3867N/A if (f.hash != 4905912898345647071L) {
3867N/A throw new RuntimeException("TEST FAILED: wrong interface hash");
5072N/A } else if (f.opnum != 4) {
3867N/A throw new RuntimeException("TEST FAILED: wrong operation number");
3867N/A }
3867N/A
3867N/A System.err.println("TEST PASSED");
3867N/A }
3867N/A}
3867N/A