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