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 
 113     // Do a cheap clone of the JDK. Most files can be sym-linked. However, $JAVA_HOME/bin/java and $JAVA_HOME/lib/.../libjvm.so"
 114     // must be copied, because the java.home property is derived from the canonicalized paths of these 2 files.
 115     static void clone(File src, File dst) throws Exception {
 116         if (dst.exists()) {
 117             if (!dst.isDirectory()) {
 118                 throw new RuntimeException("Not a directory :" + dst);
 119             }
 120         } else {
 121             if (!dst.mkdir()) {
 122                 throw new RuntimeException("Cannot create directory: " + dst);
 123             }
 124         }
 125         for (String child : src.list()) {
 126             if (child.equals(".") || child.equals("..")) {
 127                 continue;
 128             }
 129 
 130             File child_src = new File(src, child);
 131             File child_dst = new File(dst, child);
 132             if (child_dst.exists()) {
 133                 throw new RuntimeException("Already exists: " + child_dst);
 134             }
 135             if (child_src.isFile()) {
 136                 if (child.equals("libjvm.so") || child.equals("java")) {
 137                     Files.copy(child_src.toPath(), /* copy data to -> */ child_dst.toPath());
 138                 } else {
 139                     Files.createSymbolicLink(child_dst.toPath(),  /* link to -> */ child_src.toPath());
 140                 }
 141             } else {
 142                 clone(child_src, child_dst);
 143             }
 144         }
 145     }
 146 
 147     static ProcessBuilder makeBuilder(String... args) throws Exception {
 148         System.out.print("[");
 149         for (String s : args) {
 150             System.out.print(" " + s);
 151         }
 152         System.out.println(" ]");
 153         return new ProcessBuilder(args);
 154     }
 155 
 156     private static String copyFakeModulesFromHelloJar() throws Exception {
 157         String classDir = System.getProperty("test.classes");
 158         String newFile = "hello.modules";
 159         String path = classDir + File.separator + newFile;
 160 
 161         Files.copy(Paths.get(classDir, "hello.jar"),
 162             Paths.get(classDir, newFile),
 163             StandardCopyOption.REPLACE_EXISTING);
 164         return path;
 165     }
 166 }