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  * @modules java.rmi/sun.rmi.registry
  33  *          java.rmi/sun.rmi.server
  34  *          java.rmi/sun.rmi.transport
  35  *          java.rmi/sun.rmi.transport.tcp
  36  * @build TestLibrary RMID JavaVM Echo EchoImpl EchoImpl_Stub
  37  * @run main/othervm/policy=security.policy/timeout=120 UseCustomSocketFactory
  38  */
  39 
  40 import java.io.IOException;
  41 import java.net.MalformedURLException;
  42 import java.rmi.*;
  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[] protocols = 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 (RemoteException e) {
  61             TestLibrary.bomb("creating registry", e);
  62         }
  63         for (String protocol : protocols) {
  64             System.err.println("test policy: " +
  65                     TestParams.defaultPolicy);
  66             JavaVM serverVM = new JavaVM("EchoImpl", "-Djava.security.policy=" +
  67                     TestParams.defaultPolicy +
  68                     " -Drmi.registry.port=" +
  69                     registryPort, protocol);
  70             System.err.println("\nusing protocol: " +
  71                     ("".equals(protocol) ? "none" : protocol));
  72             try {
  73                 /* spawn VM for EchoServer */
  74                 serverVM.start();
  75 
  76                 /* lookup server */
  77                 Echo obj = null;
  78                 // 16 seconds timeout
  79                 long stopTime = System.currentTimeMillis() + 16000;
  80                 do {
  81                     try {
  82                         obj = (Echo) Naming.lookup("//:" + registryPort +
  83                                                    "/EchoServer");
  84                         break;
  85                     } catch (NotBoundException e) {
  86                         try {
  87                             Thread.sleep(200);
  88                         } catch (InterruptedException ignore) {
  89                         }
  90                     }
  91                 } while (System.currentTimeMillis() < stopTime);
  92 
  93                 if (obj == null)
  94                     TestLibrary.bomb("server not bound in 8 tries", null);
  95 
  96                 /* invoke remote method and print result*/
  97                 System.err.println("Bound to " + obj);
  98                 byte[] data = ("Greetings, citizen " +
  99                         System.getProperty("user.name") + "!"). getBytes();
 100                 byte[] result = obj.echoNot(data);
 101                 for (int j = 0; j < result.length; j++)
 102                     result[j] = (byte) ~result[j];
 103                 System.err.println("Result: " + new String(result));
 104 
 105             } catch (IOException e) {
 106                 TestLibrary.bomb("test failed", e);
 107 
 108             } finally {
 109                 serverVM.destroy();
 110                 try {
 111                     Naming.unbind("//:" + registryPort +
 112                             "/EchoServer");
 113                 } catch (RemoteException | NotBoundException | MalformedURLException e) {
 114                     TestLibrary.bomb("unbinding EchoServer", e);
 115 
 116                 }
 117             }
 118         }
 119     }
 120 }