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