1 /*
   2  * Copyright (c) 1998, 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 /* @test
  25  * @bug 4148850
  26  *
  27  * @summary synopsis: need to obtain remote references securely
  28  *
  29  * @author Laird Dornin; code borrowed from Ann Wollrath
  30  *
  31  * @library ../../../../testlibrary
  32  * @build Hello
  33  * @build HelloImpl
  34  * @build HelloImpl_Stub
  35  * @build TestLibrary
  36  * @build UseCustomSocketFactory
  37  * @build Compress
  38  * @run main/othervm/policy=security.policy/timeout=240 UseCustomSocketFactory
  39  */
  40 
  41 import java.io.*;
  42 import java.rmi.*;
  43 import java.rmi.server.*;
  44 import java.rmi.registry.*;
  45 
  46 /**
  47  * Test ensures that the rmiregistry is capable of running over customx
  48  * (i.e. compression) client and server socket factories.
  49  */
  50 public class UseCustomSocketFactory {
  51 
  52     Hello hello = null;
  53 
  54     public static void main(String[] args) {
  55 
  56         Registry registry = null;
  57         HelloImpl impl = null;
  58 
  59         System.out.println("\nRegression test for bug 4148850\n");
  60 
  61         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  62         int registryPort = TestLibrary.getUnusedRandomPort();
  63 
  64         try {
  65             impl = new HelloImpl();
  66 
  67             /* Make sure that the rmiregistry can communicate over a
  68              * custom socket.  Ensure that the functionality exists to
  69              * allow the rmiregistry to be secure.
  70              */
  71             registry = LocateRegistry.
  72                 createRegistry(registryPort,
  73                                new Compress.CompressRMIClientSocketFactory(),
  74                                new Compress.CompressRMIServerSocketFactory());
  75             registry.rebind("/HelloServer", impl);
  76             checkStub(registry, "RMIServerSocket");
  77 
  78         } catch (Exception e) {
  79             TestLibrary.bomb("creating registry", e);
  80         }
  81 
  82         JavaVM serverVM = new JavaVM("HelloImpl",
  83                                      "-Djava.security.policy=" +
  84                                      TestParams.defaultPolicy +
  85                                      " -Drmi.registry.port=" +
  86                                      registryPort,
  87                                      "");
  88 
  89         try {
  90 
  91             /*
  92              * spawn VM for HelloServer which will download a client socket
  93              * factory
  94              */
  95             serverVM.start();
  96 
  97             synchronized (impl) {
  98 
  99                 System.out.println("waiting for remote notification");
 100 
 101                 if (!HelloImpl.clientCalledSuccessfully) {
 102                     impl.wait(75 * 1000);
 103                 }
 104 
 105                 if (!HelloImpl.clientCalledSuccessfully) {
 106                     throw new RuntimeException("Client did not execute call in time...");
 107                 }
 108             }
 109 
 110             System.err.println("\nRegression test for bug 4148850 passed.\n ");
 111 
 112         } catch (Exception e) {
 113             TestLibrary.bomb("test failed", e);
 114 
 115         } finally {
 116             serverVM.destroy();
 117             try {
 118                 registry.unbind("/HelloServer");
 119             } catch (Exception e) {
 120                 TestLibrary.bomb("unbinding HelloServer", e);
 121             }
 122             TestLibrary.unexport(registry);
 123             TestLibrary.unexport(impl);
 124             impl = null;
 125             registry = null;
 126         }
 127     }
 128 
 129     static void checkStub(Object stub, String toCheck) throws RemoteException {
 130         System.err.println("Ensuring that the stub contains a socket factory string: " +
 131                            toCheck);
 132         System.err.println(stub);
 133         if (stub.toString().indexOf(toCheck) < 0) {
 134             throw new RemoteException("RemoteStub.toString() did not contain instance of "
 135                                       + toCheck);
 136         }
 137     }
 138 }