1 /*
   2  * Copyright (c) 2016, 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 /*
  25  * @test
  26  * @bug 8146975
  27  * @summary test RMI-IIOP with value object return
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.*
  30  * @compile -addmods java.corba Test.java Test3.java Test4.java
  31  *    HelloInterface.java HelloServer.java
  32  *    HelloClient.java HelloImpl.java _HelloImpl_Tie.java _HelloInterface_Stub.java
  33  *    RmiIiopReturnValueTest.java
  34  * @run main/othervm -addmods java.corba
  35  *    -Djava.naming.provider.url=iiop://localhost:5050
  36  *    -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
  37  *    RmiIiopReturnValueTest -port 5049
  38  * @run main/othervm/secure=java.lang.SecurityManager/policy=jtreg.test.policy
  39  *    -addmods java.corba -Djava.naming.provider.url=iiop://localhost:4050
  40  *    -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
  41  *    RmiIiopReturnValueTest -port 4049
  42  */
  43 
  44 
  45 import java.util.ArrayList;
  46 import java.util.Arrays;
  47 import java.util.List;
  48 import jdk.testlibrary.JDKToolFinder;
  49 import jdk.testlibrary.JDKToolLauncher;
  50 
  51 public class RmiIiopReturnValueTest {
  52 
  53     static final String ORBD = JDKToolFinder.getTestJDKTool("orbd");
  54     static final String JAVA = JDKToolFinder.getTestJDKTool("java");
  55     static final JDKToolLauncher orbdLauncher = JDKToolLauncher.createUsingTestJDK("orbd");
  56     static final String CLASSPATH = System.getProperty("java.class.path");
  57     static final int FIVE_SECONDS = 5000;
  58 
  59     private static Throwable clientException;
  60     private static boolean exceptionInClient;
  61     private static Process orbdProcess;
  62     private static Process rmiServerProcess;
  63 
  64     public static void main(String[] args) throws Exception {
  65         startTestComponents();
  66         stopTestComponents();
  67         System.err.println("Test completed OK ");
  68     }
  69 
  70     static void startTestComponents () throws Exception {
  71         startOrbd();
  72         Thread.sleep(FIVE_SECONDS);
  73         startRmiIiopServer();
  74         Thread.sleep(FIVE_SECONDS);
  75         executeRmiIiopClient();
  76     }
  77 
  78     private static void stopTestComponents() throws Exception {
  79         stopRmiIiopServer();
  80         stopOrbd();
  81         if (exceptionInClient) {
  82             throw new RuntimeException(clientException);
  83         } else if (!isResponseReceived()) {
  84             throw new RuntimeException("Expected Response not received");
  85         }
  86     }
  87 
  88     static void startOrbd() throws Exception {
  89         if (System.getSecurityManager() == null) {
  90             System.out.println("\nStarting orbd with NS port 5050 and activation 5049 ");
  91             // orbd -ORBInitialHost localhost -ORBInitialPort 5050 -port 5049
  92             orbdLauncher.addToolArg("-ORBInitialHost").addToolArg("localhost")
  93                     .addToolArg("-ORBInitialPort").addToolArg("5050")
  94                     .addToolArg("-port").addToolArg("5049");
  95         } else {
  96             System.out.println("\nStarting orbd with NS port 4050 and activation 4049 ");
  97             // orbd -ORBInitialHost localhost -ORBInitialPort 4050 -port 4049
  98             orbdLauncher.addToolArg("-ORBInitialHost").addToolArg("localhost")
  99                     .addToolArg("-ORBInitialPort").addToolArg("4050")
 100                     .addToolArg("-port").addToolArg("4049");
 101         }
 102 
 103         System.out.println("RmiIiopReturnValueTest: Executing: " + Arrays.asList(orbdLauncher.getCommand()));
 104         ProcessBuilder pb = new ProcessBuilder(orbdLauncher.getCommand());
 105         pb.redirectError(ProcessBuilder.Redirect.INHERIT);
 106         orbdProcess = pb.start();
 107     }
 108 
 109 
 110     static void startRmiIiopServer() throws Exception {
 111         System.out.println("\nStarting RmiIiopServer");
 112 
 113         List<String> commands = new ArrayList<>();
 114         if (System.getSecurityManager() == null) {
 115             // java -cp .
 116             // -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
 117             // -Djava.naming.provider.url=iiop://localhost:5050 HelloServer
 118             // -port 5049
 119             commands.add(RmiIiopReturnValueTest.JAVA);
 120             commands.add("-addmods");
 121             commands.add("java.corba");
 122             commands.add("-Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory");
 123             commands.add("-Djava.naming.provider.url=iiop://localhost:5050");
 124             commands.add("-cp");
 125             commands.add(RmiIiopReturnValueTest.CLASSPATH);
 126             commands.add("HelloServer");
 127             commands.add("-port");
 128             commands.add("5049");
 129 
 130         } else {
 131             // java -cp .
 132             // -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
 133             // -Djava.naming.provider.url=iiop://localhost:4050 HelloServer
 134             // -port 4049
 135             commands.add(RmiIiopReturnValueTest.JAVA);
 136             commands.add("-addmods");
 137             commands.add("java.corba");
 138             commands.add("-Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory");
 139             commands.add("-Djava.naming.provider.url=iiop://localhost:4050");
 140             commands.add("-cp");
 141             commands.add(RmiIiopReturnValueTest.CLASSPATH);
 142             commands.add("HelloServer");
 143             commands.add("-port");
 144             commands.add("4049");
 145 
 146         }
 147 
 148         System.out.println("RmiIiopReturnValueTest: Executing: " + commands);
 149         ProcessBuilder pb = new ProcessBuilder(commands);
 150         pb.redirectError(ProcessBuilder.Redirect.INHERIT);
 151         rmiServerProcess = pb.start();
 152     }
 153 
 154     static boolean isResponseReceived() {
 155         return HelloClient.isResponseReceived();
 156     }
 157 
 158     static void stopRmiIiopServer() throws Exception {
 159         if (rmiServerProcess != null) {
 160             System.out.println("RmiIiopReturnValueTest.stopRmiIiopServer: destroy rmiServerProcess");
 161             rmiServerProcess.destroyForcibly();
 162             rmiServerProcess.waitFor();
 163             System.out.println("serverProcess exitCode:"
 164                 + rmiServerProcess.exitValue());
 165         }
 166     }
 167 
 168     static void stopOrbd() throws Exception {
 169         System.out.println("RmiIiopReturnValueTest.stopOrbd: destroy orbdProcess ");
 170         orbdProcess.destroyForcibly();
 171         orbdProcess.waitFor();
 172         System.out.println("orbd exitCode:"
 173             + orbdProcess.exitValue());
 174     }
 175 
 176     static void executeRmiIiopClient() throws Exception {
 177         System.out.println("RmiIiopReturnValueTest.executeRmiIiopClient: HelloClient.executeRmiClientCall");
 178         try {
 179             HelloClient.executeRmiClientCall();
 180         } catch (Throwable t) {
 181             clientException = t;
 182             exceptionInClient = true;
 183         }
 184     }
 185 }