1 /*
   2  * Copyright (c) 2018, 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 /*
  26  * @test
  27  * @summary Test that CDS still works when the JDK is moved to a new directory
  28  * @requires vm.cds
  29  * @requires os.family == "linux"
  30  * @library /test/lib
  31  * @modules java.base/jdk.internal.misc
  32  *          java.management
  33  *          jdk.jartool/sun.tools.jar
  34  * @compile test-classes/Hello.java
  35  * @run main MoveJDKTest
  36  */
  37 
  38 // This test works only on Linux because it depends on symlinks and the name of the hotspot DLL (libjvm.so).
  39 // It probably doesn't work on Windows.
  40 // TODO: change libjvm.so to libjvm.dylib on MacOS, before adding "@requires os.family == mac"
  41 // TODO: test on solaris, before adding "@requires os.family == solaris"
  42 
  43 import java.io.File;
  44 import java.nio.file.Files;
  45 import java.nio.file.Path;
  46 import java.nio.file.Paths;
  47 import java.nio.file.StandardCopyOption;
  48 import jdk.test.lib.process.OutputAnalyzer;
  49 
  50 public class MoveJDKTest {
  51     public static void main(String[] args) throws Exception {
  52         String java_home_src = System.getProperty("java.home");
  53         String java_home_dst = System.getProperty("user.dir") + File.separator + "moved_jdk";
  54 
  55         TestCommon.startNewArchiveName();
  56         String jsaFile = TestCommon.getCurrentArchiveName();
  57         String jsaOpt = "-XX:SharedArchiveFile=" + jsaFile;
  58         {
  59             ProcessBuilder pb = makeBuilder(java_home_src + "/bin/java", "-Xshare:dump", jsaOpt);
  60             TestCommon.executeAndLog(pb, "dump");
  61         }
  62         {
  63             ProcessBuilder pb = makeBuilder(java_home_src + "/bin/java",
  64                                             "-Xshare:auto",
  65                                             jsaOpt,
  66                                             "-Xlog:class+path=info",
  67                                             "-version");
  68             OutputAnalyzer out = TestCommon.executeAndLog(pb, "exec-src");
  69             out.shouldNotContain("shared class paths mismatch");
  70             out.shouldNotContain("BOOT classpath mismatch");
  71         }
  72 
  73         clone(new File(java_home_src), new File(java_home_dst));
  74         System.out.println("============== Cloned JDK at " + java_home_dst);
  75 
  76         // Test runtime with cloned JDK
  77         {
  78             ProcessBuilder pb = makeBuilder(java_home_dst + "/bin/java",
  79                                             "-Xshare:auto",
  80                                             jsaOpt,
  81                                             "-Xlog:class+path=info",
  82                                             "-version");
  83             OutputAnalyzer out = TestCommon.executeAndLog(pb, "exec-dst");
  84             out.shouldNotContain("shared class paths mismatch");
  85             out.shouldNotContain("BOOT classpath mismatch");
  86         }
  87 
  88         // Test with bad JAR file name, hello.modules
  89         String helloJar = JarBuilder.getOrCreateHelloJar();
  90         String fake_modules = copyFakeModulesFromHelloJar();
  91         String dumptimeBootAppendOpt = "-Xbootclasspath/a:" + fake_modules;
  92         {
  93             ProcessBuilder pb = makeBuilder(java_home_src + "/bin/java",
  94                                             "-Xshare:dump",
  95                                             dumptimeBootAppendOpt,
  96                                             jsaOpt);
  97             TestCommon.executeAndLog(pb, "dump");
  98         }
  99         {
 100             String runtimeBootAppendOpt = dumptimeBootAppendOpt + System.getProperty("path.separator") + helloJar;
 101             ProcessBuilder pb = makeBuilder(java_home_dst + "/bin/java",
 102                                             "-Xshare:auto",
 103                                             runtimeBootAppendOpt,
 104                                             jsaOpt,
 105                                             "-Xlog:class+path=info",
 106                                             "-version");
 107             OutputAnalyzer out = TestCommon.executeAndLog(pb, "exec-dst");
 108             out.shouldNotContain("shared class paths mismatch");
 109             out.shouldNotContain("BOOT classpath mismatch");
 110         }
 111 
 112         // Test with no modules image in the <java home>/lib directory
 113         renameModulesFile(java_home_dst);
 114         {
 115             ProcessBuilder pb = makeBuilder(java_home_dst + "/bin/java",
 116                                             "-version");
 117             OutputAnalyzer out = TestCommon.executeAndLog(pb, "exec-missing-modules");
 118             out.shouldHaveExitValue(1);
 119             out.shouldContain("Failed setting boot class path.");
 120         }
 121     }
 122 
 123     // Do a cheap clone of the JDK. Most files can be sym-linked. However, $JAVA_HOME/bin/java and $JAVA_HOME/lib/.../libjvm.so"
 124     // must be copied, because the java.home property is derived from the canonicalized paths of these 2 files.
 125     static void clone(File src, File dst) throws Exception {
 126         if (dst.exists()) {
 127             if (!dst.isDirectory()) {
 128                 throw new RuntimeException("Not a directory :" + dst);
 129             }
 130         } else {
 131             if (!dst.mkdir()) {
 132                 throw new RuntimeException("Cannot create directory: " + dst);
 133             }
 134         }
 135         for (String child : src.list()) {
 136             if (child.equals(".") || child.equals("..")) {
 137                 continue;
 138             }
 139 
 140             File child_src = new File(src, child);
 141             File child_dst = new File(dst, child);
 142             if (child_dst.exists()) {
 143                 throw new RuntimeException("Already exists: " + child_dst);
 144             }
 145             if (child_src.isFile()) {
 146                 if (child.equals("libjvm.so") || child.equals("java")) {
 147                     Files.copy(child_src.toPath(), /* copy data to -> */ child_dst.toPath());
 148                 } else {
 149                     Files.createSymbolicLink(child_dst.toPath(),  /* link to -> */ child_src.toPath());
 150                 }
 151             } else {
 152                 clone(child_src, child_dst);
 153             }
 154         }
 155     }
 156 
 157     static void renameModulesFile(String javaHome) throws Exception {
 158         String modulesDir = javaHome + File.separator + "lib";
 159         File origModules = new File(modulesDir, "modules");
 160         if (!origModules.exists()) {
 161             throw new RuntimeException("modules file not found");
 162         }
 163 
 164         File renamedModules = new File(modulesDir, "orig_modules");
 165         if (renamedModules.exists()) {
 166             throw new RuntimeException("found orig_modules unexpectedly");
 167         }
 168 
 169         boolean success = origModules.renameTo(renamedModules);
 170         if (!success) {
 171             throw new RuntimeException("rename modules file failed");
 172         }
 173     }
 174 
 175     static ProcessBuilder makeBuilder(String... args) throws Exception {
 176         System.out.print("[");
 177         for (String s : args) {
 178             System.out.print(" " + s);
 179         }
 180         System.out.println(" ]");
 181         return new ProcessBuilder(args);
 182     }
 183 
 184     private static String copyFakeModulesFromHelloJar() throws Exception {
 185         String classDir = System.getProperty("test.classes");
 186         String newFile = "hello.modules";
 187         String path = classDir + File.separator + newFile;
 188 
 189         Files.copy(Paths.get(classDir, "hello.jar"),
 190             Paths.get(classDir, newFile),
 191             StandardCopyOption.REPLACE_EXISTING);
 192         return path;
 193     }
 194 }