test/java/rmi/server/RMISocketFactory/useSocketFactory/activatable/UseCustomSocketFactory.java

Print this page




  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  * @build TestLibrary Echo EchoImpl EchoImpl_Stub
  32  * @run main/othervm/policy=security.policy/timeout=360 UseCustomSocketFactory
  33  */
  34 
  35 import java.io.*;

  36 import java.rmi.*;
  37 import java.rmi.activation.*;
  38 import java.rmi.server.*;
  39 import java.rmi.registry.*;
  40 
  41 public class UseCustomSocketFactory {
  42     static final int REGISTRY_PORT = TestLibrary.getUnusedRandomPort();
  43 
  44     static String[] protocol = new String[] { "", "compress", "xor" };
  45 
  46     public static void main(String[] args) {
  47 
  48         System.out.println("\nRegression test for bug 4115696\n");
  49 
  50         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  51 
  52         try {
  53             LocateRegistry.createRegistry(REGISTRY_PORT);
  54         } catch (Exception e) {
  55             TestLibrary.bomb("creating registry", e);
  56         }
  57 
  58         RMID rmid = null;
  59 
  60         try {
  61             rmid = RMID.createRMID(true);
  62             rmid.addArguments(new String[] {
  63                 "-C-Djava.security.policy=" +
  64                     TestParams.defaultGroupPolicy +
  65                     " -C-Djava.security.manager=java.rmi.RMISecurityManager "});
  66             rmid.start();
  67 
  68             Echo[] echo = spawnAndTest(rmid.getPort());
  69             reactivateAndTest(echo);
  70         } catch (IOException e) {
  71             TestLibrary.bomb("creating rmid", e);
  72         } finally {
  73             if (rmid != null)
  74                 rmid.destroy();
  75         }
  76     }
  77 
  78     private static Echo[] spawnAndTest(int rmidPort) {
  79 
  80         System.err.println("\nCreate Test-->");
  81 
  82         Echo[] echo = new Echo[protocol.length];
  83 
  84         for (int i = 0; i < protocol.length; i++) {
  85             JavaVM serverVM = new JavaVM("EchoImpl",
  86                                          "-Djava.security.policy=" +
  87                                          TestParams.defaultPolicy +
  88                                          " -Drmi.registry.port=" +
  89                                          REGISTRY_PORT +
  90                                          " -Djava.rmi.activation.port=" +
  91                                          rmidPort,
  92                                          protocol[i]);
  93 
  94             System.err.println("\nusing protocol: " +
  95                                (protocol[i] == "" ? "none" : protocol[i]));
  96 
  97             try {
  98                 /* spawn VM for EchoServer */
  99                 serverVM.start();
 100 
 101                 /* lookup server */
 102                 int tries = 12;        // need enough tries for slow machine.
 103                 echo[i] = null;


 104                 do {
 105                     try {
 106                         echo[i] = (Echo) Naming.lookup("//:" + REGISTRY_PORT +
 107                                                        "/EchoServer");
 108                         break;
 109                     } catch (NotBoundException e) {
 110                         try {
 111                             Thread.sleep(2000);
 112                         } catch (Exception ignore) {
 113                         }
 114                         continue;
 115                     }
 116                 } while (--tries > 0);
 117 
 118                 if (echo[i] == null)
 119                     TestLibrary.bomb("server not bound in 12 tries", null);
 120 
 121                 /* invoke remote method and print result*/
 122                 System.err.println("Bound to " + echo[i]);
 123                 byte[] data = ("Greetings, citizen " +
 124                                System.getProperty("user.name") + "!"). getBytes();
 125                 byte[] result = echo[i].echoNot(data);
 126                 for (int j = 0; j < result.length; j++)
 127                     result[j] = (byte) ~result[j];
 128                 System.err.println("Result: " + new String(result));
 129                 echo[i].shutdown();
 130 
 131             } catch (Exception e) {
 132                 TestLibrary.bomb("test failed", e);
 133 
 134             } finally {
 135                 serverVM.destroy();
 136                 try {
 137                     Naming.unbind("//:" + REGISTRY_PORT + "/EchoServer");
 138                 } catch (Exception e) {
 139                     TestLibrary.bomb("unbinding EchoServer", e);
 140 
 141                 }
 142             }
 143         }
 144         return echo;
 145     }
 146 
 147 
 148     private static void reactivateAndTest(Echo[] echo) {
 149 
 150         System.err.println("\nReactivate Test-->");
 151 
 152         for (int i = 0; i < echo.length; i++) {
 153             try {
 154                 System.err.println("\nusing protocol: " +
 155                                    (protocol[i] == "" ? "none" : protocol[i]));
 156                 byte[] data = ("Greetings, citizen " +
 157                                System.getProperty("user.name") + "!").getBytes();
 158                 byte[] result = echo[i].echoNot(data);
 159                 for (int j = 0; j < result.length; j++)
 160                     result[j] = (byte) ~result[j];
 161                 System.err.println("Result: " + new String(result));
 162                 echo[i].shutdown();
 163             } catch (Exception e) {
 164                 TestLibrary.bomb("activating EchoServer for protocol " + protocol[i], e);
 165             }
 166         }
 167     }
 168 }


  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  * @build TestLibrary Echo EchoImpl EchoImpl_Stub
  32  * @run main/othervm/policy=security.policy/timeout=360 UseCustomSocketFactory
  33  */
  34 
  35 import java.io.*;
  36 import java.net.MalformedURLException;
  37 import java.rmi.*;


  38 import java.rmi.registry.*;
  39 
  40 public class UseCustomSocketFactory {
  41     static final int REGISTRY_PORT = TestLibrary.getUnusedRandomPort();
  42 
  43     static String[] protocol = new String[] { "", "compress", "xor" };
  44 
  45     public static void main(String[] args) {
  46 
  47         System.out.println("\nRegression test for bug 4115696\n");
  48 
  49         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  50 
  51         try {
  52             LocateRegistry.createRegistry(REGISTRY_PORT);
  53         } catch (RemoteException e) {
  54             TestLibrary.bomb("creating registry", e);
  55         }
  56 
  57         RMID rmid = null;
  58 
  59         try {
  60             rmid = RMID.createRMID(true);
  61             rmid.addArguments(new String[] {
  62                 "-C-Djava.security.policy=" +
  63                     TestParams.defaultGroupPolicy +
  64                     " -C-Djava.security.manager=java.rmi.RMISecurityManager "});
  65             rmid.start();
  66 
  67             Echo[] echo = spawnAndTest(rmid.getPort());
  68             reactivateAndTest(echo);
  69         } catch (IOException e) {
  70             TestLibrary.bomb("creating rmid", e);
  71         } finally {
  72             if (rmid != null)
  73                 rmid.destroy();
  74         }
  75     }
  76 
  77     private static Echo[] spawnAndTest(int rmidPort) {
  78 
  79         System.err.println("\nCreate Test-->");
  80 
  81         Echo[] echo = new Echo[protocol.length];
  82 
  83         for (int i = 0; i < protocol.length; i++) {
  84             JavaVM serverVM = new JavaVM("EchoImpl",
  85                                          "-Djava.security.policy=" +
  86                                          TestParams.defaultPolicy +
  87                                          " -Drmi.registry.port=" +
  88                                          REGISTRY_PORT +
  89                                          " -Djava.rmi.activation.port=" +
  90                                          rmidPort,
  91                                          protocol[i]);
  92 
  93             System.err.println("\nusing protocol: " +
  94                     ("".equals(protocol[i]) ? "none" : protocol[i]));
  95 
  96             try {
  97                 /* spawn VM for EchoServer */
  98                 serverVM.start();
  99 
 100                 /* lookup server */

 101                 echo[i] = null;
 102                 // 24 seconds timeout
 103                 long stopTime = System.currentTimeMillis() + 24000;
 104                 do {
 105                     try {
 106                         echo[i] = (Echo) Naming.lookup("//:" + REGISTRY_PORT +
 107                                                        "/EchoServer");
 108                         break;
 109                     } catch (NotBoundException e) {
 110                         try {
 111                             Thread.sleep(200);
 112                         } catch (InterruptedException ignore) {
 113                         }

 114                     }
 115                 } while (System.currentTimeMillis() < stopTime);             
 116 
 117                 if (echo[i] == null)
 118                     TestLibrary.bomb("server not bound in 120 tries", null);
 119 
 120                 /* invoke remote method and print result*/
 121                 System.err.println("Bound to " + echo[i]);
 122                 byte[] data = ("Greetings, citizen " +
 123                                System.getProperty("user.name") + "!"). getBytes();
 124                 byte[] result = echo[i].echoNot(data);
 125                 for (int j = 0; j < result.length; j++)
 126                     result[j] = (byte) ~result[j];
 127                 System.err.println("Result: " + new String(result));
 128                 echo[i].shutdown();
 129 
 130             } catch (Exception e) {
 131                 TestLibrary.bomb("test failed", e);
 132 
 133             } finally {
 134                 serverVM.destroy();
 135                 try {
 136                     Naming.unbind("//:" + REGISTRY_PORT + "/EchoServer");
 137                 } catch (RemoteException | NotBoundException | MalformedURLException e) {
 138                     TestLibrary.bomb("unbinding EchoServer", e);

 139                 }
 140             }
 141         }
 142         return echo;
 143     }
 144 
 145 
 146     private static void reactivateAndTest(Echo[] echo) {
 147 
 148         System.err.println("\nReactivate Test-->");
 149 
 150         for (int i = 0; i < echo.length; i++) {
 151             try {
 152                 System.err.println("\nusing protocol: " +
 153                            ("".equals(protocol[i]) ? "none" : protocol[i]));
 154                 byte[] data = ("Greetings, citizen " +
 155                                System.getProperty("user.name") + "!").getBytes();
 156                 byte[] result = echo[i].echoNot(data);
 157                 for (int j = 0; j < result.length; j++)
 158                     result[j] = (byte) ~result[j];
 159                 System.err.println("Result: " + new String(result));
 160                 echo[i].shutdown();
 161             } catch (Exception e) {
 162                 TestLibrary.bomb("activating EchoServer for protocol " + protocol[i], e);
 163             }
 164         }
 165     }
 166 }