1 /*
   2  * Copyright (c) 2013, 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 import java.io.File;
  25 import java.nio.file.FileSystem;
  26 import java.nio.file.FileSystems;
  27 import java.nio.file.Files;
  28 import java.nio.file.Path;
  29 import java.util.concurrent.TimeUnit;
  30 import java.util.concurrent.atomic.AtomicReference;
  31 
  32 import jdk.testlibrary.ProcessTools;
  33 
  34 /**
  35  * @test
  36  * @bug 6434402 8004926
  37  * @library /lib/testlibrary
  38  * @build TestManager TestApplication CustomLauncherTest
  39  * @run main CustomLauncherTest
  40  * @author Jaroslav Bachorik
  41  */
  42 public class CustomLauncherTest {
  43     private static final  String TEST_CLASSES = System.getProperty("test.classes");
  44     private static final  String TEST_JDK = System.getProperty("test.jdk");
  45 
  46     private static final  String TEST_SRC = System.getProperty("test.src");
  47     private static final  String OSNAME = System.getProperty("os.name");
  48     private static final  String ARCH;
  49     private static final  String LIBARCH;
  50 
  51     static {
  52         // magic with os.arch
  53         String osarch = System.getProperty("os.arch");
  54         switch (osarch) {
  55             case "i386":
  56             case "i486":
  57             case "i586":
  58             case "i686":
  59             case "i786":
  60             case "i886":
  61             case "i986": {
  62                 ARCH = "i586";
  63                 break;
  64             }
  65             case "x86_64":
  66             case "amd64": {
  67                 ARCH = "amd64";
  68                 break;
  69             }
  70             default: {
  71                 ARCH = osarch;
  72             }
  73         }
  74         LIBARCH = ARCH.equals("i586") ? "i386" : ARCH;
  75     }
  76 
  77     public static void main(String[] args) throws Exception {
  78         if (TEST_CLASSES == null || TEST_CLASSES.isEmpty()) {
  79             System.out.println("Test is designed to be run from jtreg only");
  80             return;
  81         }
  82 
  83         String PLATFORM = "";
  84         switch (OSNAME.toLowerCase()) {
  85             case "linux": {
  86                 PLATFORM = "linux";
  87                 break;
  88             }
  89             case "sunos": {
  90                 PLATFORM = "solaris";
  91                 break;
  92             }
  93             default: {
  94                 System.out.println("Test not designed to run on this operating " +
  95                                    "system (" + OSNAME + "), skipping...");
  96                 return;
  97             }
  98         }
  99 
 100         String LAUNCHER = TEST_SRC + File.separator + PLATFORM + "-" + ARCH +
 101                           File.separator + "launcher";
 102 
 103         final FileSystem FS = FileSystems.getDefault();
 104         final boolean hasLauncher = Files.isExecutable(FS.getPath(LAUNCHER));
 105         if (!hasLauncher) {
 106             System.out.println("Launcher [" + LAUNCHER + "] does not exist. Skipping the test.");
 107             return;
 108         }
 109 
 110         Path libjvmPath = findLibjvm(FS);
 111         if (libjvmPath == null) {
 112             throw new Error("Unable to locate 'libjvm.so' in " + TEST_JDK);
 113         }
 114 
 115         Process serverPrc = null, clientPrc = null;
 116 
 117         try {
 118             System.out.println("Starting custom launcher:");
 119             System.out.println("=========================");
 120             System.out.println("  launcher  : " + LAUNCHER);
 121             System.out.println("  libjvm    : " + libjvmPath.toString());
 122             System.out.println("  classpath : " + TEST_CLASSES);
 123             ProcessBuilder server = new ProcessBuilder(LAUNCHER, libjvmPath.toString(), TEST_CLASSES, "TestApplication");
 124 
 125             final AtomicReference<String> port = new AtomicReference<>();
 126             final AtomicReference<String> pid = new AtomicReference<>();
 127 
 128             serverPrc = ProcessTools.startProcess(
 129                 "Launcher",
 130                 server,
 131                 (String line) -> {
 132                     if (line.startsWith("port:")) {
 133                          port.set(line.split("\\:")[1]);
 134                      } else  if (line.startsWith("pid:")) {
 135                          pid.set(line.split("\\:")[1]);
 136                      } else if (line.startsWith("waiting")) {
 137                          return true;
 138                      }
 139                      return false;
 140                 },
 141                 5,
 142                 TimeUnit.SECONDS
 143             );
 144 
 145             System.out.println("Attaching test manager:");
 146             System.out.println("=========================");
 147             System.out.println("  PID           : " + pid.get());
 148             System.out.println("  shutdown port : " + port.get());
 149 
 150             ProcessBuilder client = ProcessTools.createJavaProcessBuilder(
 151                 "-cp",
 152                 TEST_CLASSES +
 153                     File.pathSeparator +
 154                     TEST_JDK +
 155                     File.separator +
 156                     "lib" +
 157                     File.separator +
 158                     "tools.jar",
 159                 "TestManager",
 160                 pid.get(),
 161                 port.get(),
 162                 "true"
 163             );
 164 
 165             clientPrc = ProcessTools.startProcess(
 166                 "TestManager",
 167                 client,
 168                 (String line) -> line.startsWith("Starting TestManager for PID"),
 169                 10,
 170                 TimeUnit.SECONDS
 171             );
 172 
 173             int clientExitCode = clientPrc.waitFor();
 174             int serverExitCode = serverPrc.waitFor();
 175 
 176             if (clientExitCode != 0 || serverExitCode != 0) {
 177                 throw new Error("Test failed");
 178             }
 179         } finally {
 180             if (clientPrc != null) {
 181                 clientPrc.destroy();
 182                 clientPrc.waitFor();
 183             }
 184             if (serverPrc != null) {
 185                 serverPrc.destroy();
 186                 serverPrc.waitFor();
 187             }
 188         }
 189     }
 190 
 191     private static Path findLibjvm(FileSystem FS) {
 192         Path libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "jre", "lib", LIBARCH));
 193         if (libjvmPath == null) {
 194             libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "lib", LIBARCH));
 195         }
 196         return libjvmPath;
 197     }
 198 
 199     private static Path findLibjvm(Path libPath) {
 200         // ARCH/libjvm.so -> ARCH/server/libjvm.so -> ARCH/client/libjvm.so
 201         Path libjvmPath = libPath.resolve("libjvm.so");
 202         if (isFileOk(libjvmPath)) {
 203             return libjvmPath;
 204         }
 205         libjvmPath = libPath.resolve("server/libjvm.so");
 206         if (isFileOk(libjvmPath)) {
 207             return libjvmPath;
 208         }
 209         libjvmPath = libPath.resolve("client/libjvm.so");
 210         if (isFileOk(libPath)) {
 211             return libjvmPath;
 212         }
 213 
 214         return null;
 215     }
 216 
 217     private static boolean isFileOk(Path path) {
 218         return Files.isRegularFile(path) && Files.isReadable(path);
 219     }
 220 }