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.io.IOException;
  26 import java.nio.file.FileSystem;
  27 import java.nio.file.FileSystems;
  28 import java.nio.file.Files;
  29 import java.nio.file.LinkOption;
  30 import java.nio.file.Path;
  31 import java.nio.file.StandardCopyOption;
  32 import java.nio.file.attribute.PosixFilePermission;
  33 import java.util.HashSet;
  34 import java.util.Set;
  35 import java.util.concurrent.TimeUnit;
  36 import java.util.concurrent.atomic.AtomicReference;
  37 
  38 import jdk.testlibrary.ProcessTools;
  39 
  40 /**
  41  * @test
  42  * @bug 6434402 8004926
  43  * @library /lib/testlibrary
  44  * @build jdk.testlibrary.*
  45  * @build TestManager TestApplication CustomLauncherTest
  46  * @run main/othervm CustomLauncherTest
  47  * @author Jaroslav Bachorik
  48  */
  49 public class CustomLauncherTest {
  50     private static final  String TEST_CLASSPATH = System.getProperty("test.class.path");
  51     private static final  String TEST_JDK = System.getProperty("test.jdk");
  52     private static final  String WORK_DIR = System.getProperty("user.dir");
  53 
  54     private static final  String TEST_SRC = System.getProperty("test.src");
  55     private static final  String OSNAME = System.getProperty("os.name");
  56     private static final  String ARCH;
  57     private static final  String LIBARCH;
  58 
  59     static {
  60         // magic with os.arch
  61         String osarch = System.getProperty("os.arch");
  62         switch (osarch) {
  63             case "i386":
  64             case "i486":
  65             case "i586":
  66             case "i686":
  67             case "i786":
  68             case "i886":
  69             case "i986": {
  70                 ARCH = "i586";
  71                 break;
  72             }
  73             case "x86_64":
  74             case "amd64": {
  75                 ARCH = "amd64";
  76                 break;
  77             }
  78             case "sparc":
  79                 ARCH = "sparcv9";
  80                 break;
  81             default: {
  82                 ARCH = osarch;
  83             }
  84         }
  85         LIBARCH = ARCH.equals("i586") ? "i386" : ARCH;
  86     }
  87 
  88     public static void main(String[] args) throws Exception {
  89         if (TEST_CLASSPATH == null || TEST_CLASSPATH.isEmpty()) {
  90             System.out.println("Test is designed to be run from jtreg only");
  91             return;
  92         }
  93 
  94         if (getPlatform() == null) {
  95             System.out.println("Test not designed to run on this operating " +
  96                                 "system (" + OSNAME + "), skipping...");
  97             return;
  98         }
  99 
 100         final FileSystem FS = FileSystems.getDefault();
 101 
 102         Path libjvmPath = findLibjvm(FS);
 103         if (libjvmPath == null) {
 104             throw new Error("Unable to locate 'libjvm.so' in " + TEST_JDK);
 105         }
 106 
 107         Process serverPrc = null, clientPrc = null;
 108 
 109         try {
 110             String[] launcher = getLauncher();
 111 
 112             System.out.println("Starting custom launcher:");
 113             System.out.println("=========================");
 114             System.out.println("  launcher  : " + launcher[0]);
 115             System.out.println("  libjvm    : " + libjvmPath.toString());
 116             System.out.println("  classpath : " + TEST_CLASSPATH);
 117             ProcessBuilder server = new ProcessBuilder(
 118                 launcher[1],
 119                 libjvmPath.toString(),
 120                 TEST_CLASSPATH,
 121                 "TestApplication"
 122             );
 123 
 124             final AtomicReference<String> port = new AtomicReference<>();
 125 
 126             serverPrc = ProcessTools.startProcess(
 127                 "Launcher",
 128                 server,
 129                 (String line) -> {
 130                     if (line.startsWith("port:")) {
 131                          port.set(line.split("\\:")[1]);
 132                     } else if (line.startsWith("waiting")) {
 133                          return true;
 134                     }
 135                     return false;
 136                 },
 137                 5,
 138                 TimeUnit.SECONDS
 139             );
 140 
 141             System.out.println("Attaching test manager:");
 142             System.out.println("=========================");
 143             System.out.println("  PID           : " + serverPrc.getPid());
 144             System.out.println("  shutdown port : " + port.get());
 145 
 146             ProcessBuilder client = ProcessTools.createJavaProcessBuilder(
 147                 "-cp",
 148                 TEST_CLASSPATH,
 149                 "TestManager",
 150                 String.valueOf(serverPrc.getPid()),
 151                 port.get(),
 152                 "true"
 153             );
 154 
 155             clientPrc = ProcessTools.startProcess(
 156                 "TestManager",
 157                 client,
 158                 (String line) -> line.startsWith("Starting TestManager for PID"),
 159                 10,
 160                 TimeUnit.SECONDS
 161             );
 162 
 163             int clientExitCode = clientPrc.waitFor();
 164             int serverExitCode = serverPrc.waitFor();
 165 
 166             if (clientExitCode != 0 || serverExitCode != 0) {
 167                 throw new Error("Test failed");
 168             }
 169         } finally {
 170             if (clientPrc != null) {
 171                 clientPrc.destroy();
 172                 clientPrc.waitFor();
 173             }
 174             if (serverPrc != null) {
 175                 serverPrc.destroy();
 176                 serverPrc.waitFor();
 177             }
 178         }
 179     }
 180 
 181     private static Path findLibjvm(FileSystem FS) {
 182         Path libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "jre", "lib", LIBARCH));
 183         if (libjvmPath == null) {
 184             libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "lib", LIBARCH));
 185         }
 186         return libjvmPath;
 187     }
 188 
 189     private static Path findLibjvm(Path libPath) {
 190         // ARCH/libjvm.so -> ARCH/server/libjvm.so -> ARCH/client/libjvm.so
 191         Path libjvmPath = libPath.resolve("libjvm.so");
 192         if (isFileOk(libjvmPath)) {
 193             return libjvmPath;
 194         }
 195         libjvmPath = libPath.resolve("server/libjvm.so");
 196         if (isFileOk(libjvmPath)) {
 197             return libjvmPath;
 198         }
 199         libjvmPath = libPath.resolve("client/libjvm.so");
 200         if (isFileOk(libPath)) {
 201             return libjvmPath;
 202         }
 203 
 204         return null;
 205     }
 206 
 207     private static boolean isFileOk(Path path) {
 208         return Files.isRegularFile(path) && Files.isReadable(path);
 209     }
 210 
 211     private static String getPlatform() {
 212         String platform = null;
 213         switch (OSNAME.toLowerCase()) {
 214             case "linux": {
 215                 platform = "linux";
 216                 break;
 217             }
 218             case "sunos": {
 219                 platform = "solaris";
 220                 break;
 221             }
 222             default: {
 223                 platform = null;
 224             }
 225         }
 226 
 227         return platform;
 228     }
 229 
 230     private static String[] getLauncher() throws IOException {
 231         String platform = getPlatform();
 232         if (platform == null) {
 233             return null;
 234         }
 235 
 236         String launcher = TEST_SRC + File.separator + platform + "-" + ARCH +
 237                           File.separator + "launcher";
 238 
 239         final FileSystem FS = FileSystems.getDefault();
 240         Path launcherPath = FS.getPath(launcher);
 241 
 242         final boolean hasLauncher = Files.isRegularFile(launcherPath, LinkOption.NOFOLLOW_LINKS)&&
 243                                     Files.isReadable(launcherPath);
 244         if (!hasLauncher) {
 245             System.out.println("Launcher [" + launcher + "] does not exist. Skipping the test.");
 246             return null;
 247         }
 248 
 249         // It is impossible to store an executable file in the source control
 250         // We need to copy the launcher to the working directory
 251         // and set the executable flag
 252         Path localLauncherPath = FS.getPath(WORK_DIR, "launcher");
 253         Files.copy(launcherPath, localLauncherPath,
 254                    StandardCopyOption.REPLACE_EXISTING,
 255                    StandardCopyOption.COPY_ATTRIBUTES);
 256         if (!Files.isExecutable(localLauncherPath)) {
 257             Set<PosixFilePermission> perms = new HashSet<>(
 258                 Files.getPosixFilePermissions(
 259                     localLauncherPath,
 260                     LinkOption.NOFOLLOW_LINKS
 261                 )
 262             );
 263             perms.add(PosixFilePermission.OWNER_EXECUTE);
 264             Files.setPosixFilePermissions(localLauncherPath, perms);
 265         }
 266         return new String[] {launcher, localLauncherPath.toAbsolutePath().toString()};
 267     }
 268 }