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