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