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 4127826
  26  *
  27  * @summary synopsis: need to download factories for use with custom socket
  28  * types
  29  * @author Ann Wollrath
  30  *
  31  * @library ../../../../testlibrary
  32  * @build TestLibrary RMID JavaVM StreamPipe
  33  * @build Echo
  34  * @build EchoImpl
  35  * @build EchoImpl_Stub
  36  * @build UseCustomSocketFactory
  37  * @run main/othervm/policy=security.policy/timeout=120 UseCustomSocketFactory
  38  */
  39 
  40 import java.io.*;
  41 import java.rmi.*;
  42 import java.rmi.server.*;
  43 import java.rmi.registry.*;
  44 
  45 public class UseCustomSocketFactory {
  46 
  47     public static void main(String[] args) {
  48 
  49         int registryPort = -1;
  50 
  51         String[] protocol = new String[] { "", "compress", "xor" };
  52 
  53         System.out.println("\nRegression test for bug 4127826\n");
  54 
  55         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  56 
  57         try {
  58             Registry registry = TestLibrary.createRegistryOnUnusedPort();
  59             registryPort = TestLibrary.getRegistryPort(registry);
  60         } catch (Exception e) {
  61             TestLibrary.bomb("creating registry", e);
  62         }
  63 
  64         for (int i = 0; i < protocol.length; i++) {
  65 
  66             System.err.println("test policy: " +
  67                                TestParams.defaultPolicy);
  68 
  69             JavaVM serverVM = new JavaVM("EchoImpl",
  70                                          "-Djava.security.policy=" +
  71                                          TestParams.defaultPolicy +
  72                                          " -Drmi.registry.port=" +
  73                                          registryPort,
  74                                          protocol[i]);
  75             System.err.println("\nusing protocol: " +
  76                                (protocol[i] == "" ? "none" : protocol[i]));
  77 
  78             try {
  79                 /* spawn VM for EchoServer */
  80                 serverVM.start();
  81 
  82                 /* lookup server */
  83                 int tries = 8;
  84                 Echo obj = null;
  85                 do {
  86                     try {
  87                         obj = (Echo) Naming.lookup("//:" + registryPort +
  88                                                    "/EchoServer");
  89                         break;
  90                     } catch (NotBoundException e) {
  91                         try {
  92                             Thread.sleep(2000);
  93                         } catch (Exception ignore) {
  94                         }
  95                         continue;
  96                     }
  97                 } while (--tries > 0);
  98 
  99                 if (obj == null)
 100                     TestLibrary.bomb("server not bound in 8 tries", null);
 101 
 102                 /* invoke remote method and print result*/
 103                 System.err.println("Bound to " + obj);
 104                 byte[] data = ("Greetings, citizen " +
 105                                System.getProperty("user.name") + "!"). getBytes();
 106                 byte[] result = obj.echoNot(data);
 107                 for (int j = 0; j < result.length; j++)
 108                     result[j] = (byte) ~result[j];
 109                 System.err.println("Result: " + new String(result));
 110 
 111             } catch (Exception e) {
 112                 TestLibrary.bomb("test failed", e);
 113 
 114             } finally {
 115                 serverVM.destroy();
 116                 try {
 117                     Naming.unbind("//:" + registryPort +
 118                                   "/EchoServer");
 119                 } catch (Exception e) {
 120                     TestLibrary.bomb("unbinding EchoServer", e);
 121 
 122                 }
 123             }
 124         }
 125     }
 126 }