1 /*
   2  * Copyright (c) 1998, 2014, 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 4115696
  26 
  27  * @summary synopsis: cannot use socket factories with Activatable objects
  28  * @author Ann Wollrath
  29  *
  30  * @library ../../../../testlibrary
  31  * @modules java.rmi/sun.rmi.registry
  32  *          java.rmi/sun.rmi.server
  33  *          java.rmi/sun.rmi.transport
  34  *          java.rmi/sun.rmi.transport.tcp
  35  * @build TestLibrary Echo EchoImpl EchoImpl_Stub
  36  * @run main/othervm/policy=security.policy/timeout=360 UseCustomSocketFactory
  37  */
  38 
  39 import java.io.*;
  40 import java.net.MalformedURLException;
  41 import java.rmi.*;
  42 import java.rmi.registry.*;
  43 
  44 public class UseCustomSocketFactory {
  45     static final int REGISTRY_PORT = TestLibrary.getUnusedRandomPort();
  46 
  47     static String[] protocol = new String[] { "", "compress", "xor" };
  48 
  49     public static void main(String[] args) {
  50 
  51         System.out.println("\nRegression test for bug 4115696\n");
  52 
  53         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  54 
  55         try {
  56             LocateRegistry.createRegistry(REGISTRY_PORT);
  57         } catch (RemoteException e) {
  58             TestLibrary.bomb("creating registry", e);
  59         }
  60 
  61         RMID rmid = null;
  62 
  63         try {
  64             rmid = RMID.createRMID();
  65             rmid.addArguments(new String[] {
  66                 "-C-Djava.security.policy=" +
  67                     TestParams.defaultGroupPolicy +
  68                     " -C-Djava.security.manager=java.rmi.RMISecurityManager "});
  69             rmid.start();
  70 
  71             Echo[] echo = spawnAndTest(rmid.getPort());
  72             reactivateAndTest(echo);
  73         } catch (IOException e) {
  74             TestLibrary.bomb("creating rmid", e);
  75         } finally {
  76             if (rmid != null)
  77                 rmid.destroy();
  78         }
  79     }
  80 
  81     private static Echo[] spawnAndTest(int rmidPort) {
  82 
  83         System.err.println("\nCreate Test-->");
  84 
  85         Echo[] echo = new Echo[protocol.length];
  86 
  87         for (int i = 0; i < protocol.length; i++) {
  88             JavaVM serverVM = new JavaVM("EchoImpl",
  89                                          "-Djava.security.policy=" +
  90                                          TestParams.defaultPolicy +
  91                                          " -Drmi.registry.port=" +
  92                                          REGISTRY_PORT +
  93                                          " -Djava.rmi.activation.port=" +
  94                                          rmidPort,
  95                                          protocol[i]);
  96 
  97             System.err.println("\nusing protocol: " +
  98                     ("".equals(protocol[i]) ? "none" : protocol[i]));
  99 
 100             try {
 101                 /* spawn VM for EchoServer */
 102                 serverVM.start();
 103 
 104                 /* lookup server */
 105                 echo[i] = null;
 106                 // 24 seconds timeout
 107                 long stopTime = System.currentTimeMillis() + 24000;
 108                 do {
 109                     try {
 110                         echo[i] = (Echo) Naming.lookup("//:" + REGISTRY_PORT +
 111                                                        "/EchoServer");
 112                         break;
 113                     } catch (NotBoundException e) {
 114                         try {
 115                             Thread.sleep(200);
 116                         } catch (InterruptedException ignore) {
 117                         }
 118                     }
 119                 } while (System.currentTimeMillis() < stopTime);
 120 
 121                 if (echo[i] == null)
 122                     TestLibrary.bomb("server not bound in 120 tries", null);
 123 
 124                 /* invoke remote method and print result*/
 125                 System.err.println("Bound to " + echo[i]);
 126                 byte[] data = ("Greetings, citizen " +
 127                                System.getProperty("user.name") + "!"). getBytes();
 128                 byte[] result = echo[i].echoNot(data);
 129                 for (int j = 0; j < result.length; j++)
 130                     result[j] = (byte) ~result[j];
 131                 System.err.println("Result: " + new String(result));
 132                 echo[i].shutdown();
 133 
 134             } catch (Exception e) {
 135                 TestLibrary.bomb("test failed", e);
 136 
 137             } finally {
 138                 serverVM.destroy();
 139                 try {
 140                     Naming.unbind("//:" + REGISTRY_PORT + "/EchoServer");
 141                 } catch (RemoteException | NotBoundException | MalformedURLException e) {
 142                     TestLibrary.bomb("unbinding EchoServer", e);
 143                 }
 144             }
 145         }
 146         return echo;
 147     }
 148 
 149 
 150     private static void reactivateAndTest(Echo[] echo) {
 151 
 152         System.err.println("\nReactivate Test-->");
 153 
 154         for (int i = 0; i < echo.length; i++) {
 155             try {
 156                 System.err.println("\nusing protocol: " +
 157                            ("".equals(protocol[i]) ? "none" : protocol[i]));
 158                 byte[] data = ("Greetings, citizen " +
 159                                System.getProperty("user.name") + "!").getBytes();
 160                 byte[] result = echo[i].echoNot(data);
 161                 for (int j = 0; j < result.length; j++)
 162                     result[j] = (byte) ~result[j];
 163                 System.err.println("Result: " + new String(result));
 164                 echo[i].shutdown();
 165             } catch (Exception e) {
 166                 TestLibrary.bomb("activating EchoServer for protocol " + protocol[i], e);
 167             }
 168         }
 169     }
 170 }