1 /*
   2  * Copyright (c) 1999, 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 /*
  25  * @test
  26  * @bug 4251010
  27  * @summary equals does not works on stub objects created with
  28  *           custom socket AndFactory
  29  * @author Laird Dornin
  30  *
  31  * @library ../../../testlibrary
  32  * @build TestLibrary
  33  * @run main/othervm/timeout=40 VerifyRemoteEquals
  34  */
  35 
  36 import java.io.*;
  37 import java.net.*;
  38 import java.rmi.*;
  39 import java.rmi.server.*;
  40 
  41 /**
  42  * Test ensures that a stub that has never been serialized and one
  43  * that has can be .equals().  Test also ensures that stubs with
  44  * custom socket factories can be .equals() with equivalent stubs.
  45  */
  46 public class VerifyRemoteEquals {
  47 
  48     /**
  49      * Remote interface.
  50      */
  51     public interface Test extends Remote {
  52     }
  53 
  54     /**
  55      * Implementation of Remote interface passing custom socket
  56      * factories
  57      */
  58     public static final class TestImpl
  59         extends UnicastRemoteObject implements Test
  60     {
  61         public TestImpl() throws RemoteException {
  62             super();
  63         }
  64 
  65         public TestImpl(RMIClientSocketFactory clientFactory,
  66                         RMIServerSocketFactory serverFactory)
  67             throws RemoteException
  68         {
  69 
  70             super(0, clientFactory, serverFactory);
  71         }
  72 
  73         public TestImpl(RMISocketFactory factory)
  74             throws RemoteException
  75         {
  76             super(0, factory, factory);
  77         }
  78     }
  79 
  80     /**
  81      * Remote interface for retrieving Test object.
  82      */
  83     public interface TestHome extends Remote {
  84         public Test get() throws RemoteException;
  85     }
  86 
  87     /**
  88      * Implementation of interface TestHome.
  89      */
  90     public static final class TestHomeImpl
  91         extends UnicastRemoteObject implements TestHome
  92     {
  93         private Test test;
  94 
  95         public TestHomeImpl(Test test)
  96             throws RemoteException {
  97 
  98             super();
  99 
 100             this.test = test;
 101         }
 102 
 103         public Test get() {
 104             return test;
 105         }
 106     }
 107 
 108     /**
 109      * Custom server socket factory.
 110      */
 111     public static final class ServerSocketAndFactory
 112         extends ServerSocket implements RMIServerSocketFactory, Serializable
 113     {
 114         ServerSocketAndFactory() throws IOException, java.net.UnknownHostException {
 115             // I am forced to do something useless with the parent
 116             // constructor
 117             super(0);
 118         }
 119         ServerSocketAndFactory(int port) throws IOException,
 120             java.net.UnknownHostException
 121         {
 122             super(port);
 123         }
 124 
 125         public ServerSocket createServerSocket(int port)
 126             throws IOException
 127         {
 128 
 129             return new ServerSocketAndFactory(port);
 130         }
 131 
 132         public int hashCode() {
 133             return getLocalPort();
 134         }
 135 
 136         public boolean equals(Object obj) {
 137             if (obj instanceof ServerSocketAndFactory) {
 138                 ServerSocketAndFactory ssf = (ServerSocketAndFactory) obj;
 139                 if (getLocalPort() == ssf.getLocalPort()) {
 140                     return true;
 141                 }
 142             }
 143             return false;
 144         }
 145     }
 146 
 147     /**
 148      * Custom socket factory.
 149      */
 150     public static final class ClientSocketAndFactory
 151         extends Socket implements RMIClientSocketFactory, Serializable
 152     {
 153         ClientSocketAndFactory() {
 154         }
 155         ClientSocketAndFactory(String host, int port) throws IOException {
 156             super(host, port);
 157         }
 158 
 159         public Socket createSocket(String host, int port)
 160             throws IOException {
 161 
 162             return new ClientSocketAndFactory(host, port);
 163         }
 164 
 165         public int hashCode() {
 166             return getPort();
 167         }
 168 
 169         public boolean equals(Object obj) {
 170 
 171             if (obj instanceof ClientSocketAndFactory) {
 172                 ClientSocketAndFactory csf = (ClientSocketAndFactory) obj;
 173                 if (getPort() == csf.getPort()) {
 174                     return true;
 175                 }
 176             }
 177 
 178             return false;
 179         }
 180     }
 181 
 182     public static void main(String[] args) {
 183 
 184         try {
 185             System.out.println("\n\nRegression test for, 4251010\n\n");
 186 
 187             Test test = new TestImpl(new ClientSocketAndFactory(),
 188                                      new ServerSocketAndFactory());
 189             TestHome home = new TestHomeImpl(test);
 190 
 191             Test test0 = ((Test) RemoteObject.toStub(test));
 192             Test test1 = ((TestHome) RemoteObject.toStub(home)).get();
 193             Test test2 = ((TestHome) RemoteObject.toStub(home)).get();
 194             Test test3 = ((Test) (new MarshalledObject(test)).get());
 195 
 196             if (test0.equals(test1)) {
 197                 System.out.println("test0, test1, stubs equal");
 198             } else {
 199                 TestLibrary.bomb("test0, test1, stubs not equal");
 200             }
 201 
 202             if (test1.equals(test2)) {
 203                 System.out.println("test1, test2, stubs equal");
 204             } else {
 205                 TestLibrary.bomb("test1, test2, stubs not equal");
 206             }
 207 
 208             // explicitly compare an unmarshalled object with toStub
 209             // return
 210             if (test2.equals(test3)) {
 211                 System.out.println("test2, test3, stubs equal");
 212             } else {
 213                 TestLibrary.bomb("test2, test3, stubs not equal");
 214             }
 215 
 216             test0 = null;
 217             test1 = null;
 218             test2 = null;
 219             test3 = null;
 220 
 221             TestLibrary.unexport(test);
 222             TestLibrary.unexport(home);
 223 
 224             System.err.println("test passed: stubs were equal");
 225 
 226         } catch (Exception e) {
 227             TestLibrary.bomb("test got unexpected exception", e);
 228         }
 229     }
 230 }