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