1 /* 2 * Copyright (c) 2024, Red Hat, Inc. 3 * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 import java.io.IOException; 26 import java.nio.file.FileVisitResult; 27 import java.nio.file.Files; 28 import java.nio.file.Path; 29 import java.nio.file.SimpleFileVisitor; 30 import java.nio.file.attribute.BasicFileAttributes; 31 import java.util.ArrayList; 32 import java.util.Arrays; 33 import java.util.Collections; 34 import java.util.List; 35 import java.util.Random; 36 import java.util.Set; 37 import java.util.function.Predicate; 38 import java.util.stream.Collectors; 39 40 import tests.Helper; 41 import tests.JImageGenerator; 42 43 44 /* 45 * @test 46 * @summary Compare packaged-modules jlink with a run-time image based jlink to 47 * produce the same result 48 * @requires (jlink.packagedModules & vm.compMode != "Xcomp" & os.maxMemory >= 2g) 49 * @library ../../lib /test/lib 50 * @enablePreview 51 * @modules java.base/jdk.internal.jimage 52 * jdk.jlink/jdk.tools.jlink.internal 53 * jdk.jlink/jdk.tools.jlink.plugin 54 * jdk.jlink/jdk.tools.jimage 55 * jdk.jlink 56 * @build tests.* jdk.test.lib.process.OutputAnalyzer 57 * jdk.test.lib.process.ProcessTools 58 * @run main/othervm/timeout=1200 -ea -esa -Xmx1g PackagedModulesVsRuntimeImageLinkTest 59 */ 60 public class PackagedModulesVsRuntimeImageLinkTest extends AbstractLinkableRuntimeTest { 61 62 public static void main(String[] args) throws Exception { 63 PackagedModulesVsRuntimeImageLinkTest test = new PackagedModulesVsRuntimeImageLinkTest(); 64 test.run(); 65 } 66 67 @Override 68 void runTest(Helper helper, boolean isLinkableRuntime) throws Exception { 69 // create a java.se using jmod-less approach 70 BaseJlinkSpecBuilder builder = new BaseJlinkSpecBuilder() 71 .helper(helper) 72 .name("java-se-jmodless") 73 .addModule("java.se") 74 .validatingModule("java.se"); 75 if (isLinkableRuntime) { 76 builder.setLinkableRuntime(); 77 } 78 Path javaSEruntimeLink = createJavaImageRuntimeLink(builder.build()); 79 // create a java.se using packaged modules (jmod-full) 80 Path javaSEJmodFull = JImageGenerator.getJLinkTask() 81 .output(helper.createNewImageDir("java-se-jmodfull")) 82 .addMods("java.se").call().assertSuccess(); 83 84 compareRecursively(javaSEruntimeLink, javaSEJmodFull); 85 } 86 87 // Visit all files in the given directories checking that they're byte-by-byte identical 88 private static void compareRecursively(Path javaSEJmodLess, 89 Path javaSEJmodFull) throws IOException, AssertionError { 90 FilesCapturingVisitor jmodFullVisitor = new FilesCapturingVisitor(javaSEJmodFull); 91 FilesCapturingVisitor jmodLessVisitor = new FilesCapturingVisitor(javaSEJmodLess); 92 Files.walkFileTree(javaSEJmodFull, jmodFullVisitor); 93 Files.walkFileTree(javaSEJmodLess, jmodLessVisitor); 94 List<String> jmodFullFiles = jmodFullVisitor.filesVisited(); 95 List<String> jmodLessFiles = jmodLessVisitor.filesVisited(); 96 Collections.sort(jmodFullFiles); 97 Collections.sort(jmodLessFiles); 98 99 if (jmodFullFiles.size() != jmodLessFiles.size()) { 100 throw new AssertionError(String.format("Size of files different for jmod-less (%d) vs jmod-full (%d) java.se jlink", jmodLessFiles.size(), jmodFullFiles.size())); 101 } 102 String jimageFile = Path.of("lib").resolve("modules").toString(); 103 // Compare all files except the modules image 104 for (int i = 0; i < jmodFullFiles.size(); i++) { 105 String jmodFullPath = jmodFullFiles.get(i); 106 String jmodLessPath = jmodLessFiles.get(i); 107 if (!jmodFullPath.equals(jmodLessPath)) { 108 throw new AssertionError(String.format("jmod-full path (%s) != jmod-less path (%s)", jmodFullPath, jmodLessPath)); 109 } 110 if (jmodFullPath.equals(jimageFile)) { 111 continue; 112 } 113 Path a = javaSEJmodFull.resolve(Path.of(jmodFullPath)); 114 Path b = javaSEJmodLess.resolve(Path.of(jmodLessPath)); 115 if (Files.mismatch(a, b) != -1L) { 116 handleFileMismatch(a, b); 117 } 118 } 119 // Compare jimage contents by iterating its entries and comparing their 120 // paths and content bytes 121 // 122 // Note: The files aren't byte-by-byte comparable (probably due to string hashing 123 // and offset differences in container bytes) 124 Path jimageJmodLess = javaSEJmodLess.resolve(Path.of("lib")).resolve(Path.of("modules")); 125 Path jimageJmodFull = javaSEJmodFull.resolve(Path.of("lib")).resolve(Path.of("modules")); 126 List<String> jimageContentJmodLess = JImageHelper.listContents(jimageJmodLess); 127 List<String> jimageContentJmodFull = JImageHelper.listContents(jimageJmodFull); 128 assertSameContent("jmod-less", jimageContentJmodLess, "jmod-full", jimageContentJmodFull); 129 // Both lists are same size, with same names, so enumerate either. 130 for (int i = 0; i < jimageContentJmodFull.size(); i++) { 131 if (!jimageContentJmodFull.get(i).equals(jimageContentJmodLess.get(i))) { 132 throw new AssertionError(String.format("Jimage content differs at index %d: jmod-full was: '%s' jmod-less was: '%s'", 133 i, 134 jimageContentJmodFull.get(i), 135 jimageContentJmodLess.get(i) 136 )); 137 } 138 String loc = jimageContentJmodFull.get(i); 139 if (isTreeInfoResource(loc)) { 140 // Skip container bytes as those are offsets to the content 141 // of the container which might be different between jlink runs. 142 continue; 143 } 144 byte[] resBytesFull = JImageHelper.getLocationBytes(loc, jimageJmodFull); 145 byte[] resBytesLess = JImageHelper.getLocationBytes(loc, jimageJmodLess); 146 if (resBytesFull.length != resBytesLess.length || Arrays.mismatch(resBytesFull, resBytesLess) != -1) { 147 throw new AssertionError("Content bytes mismatch for " + loc); 148 } 149 } 150 } 151 152 // Helper to assert the content of two jimage files are the same and provide 153 // useful debug information otherwise. 154 private static void assertSameContent( 155 String lhsLabel, List<String> lhsNames, String rhsLabel, List<String> rhsNames) { 156 157 List<String> lhsOnly = 158 lhsNames.stream().filter(Predicate.not(Set.copyOf(rhsNames)::contains)).toList(); 159 List<String> rhsOnly = 160 rhsNames.stream().filter(Predicate.not(Set.copyOf(lhsNames)::contains)).toList(); 161 if (!lhsOnly.isEmpty() || !rhsOnly.isEmpty()) { 162 String message = String.format( 163 "jimage content differs for %s (%d) v. %s (%d)", 164 lhsLabel, lhsNames.size(), rhsLabel, rhsNames.size()); 165 if (!lhsOnly.isEmpty()) { 166 message += "\nOnly in " + lhsLabel + ":\n\t" + String.join("\n\t", lhsOnly); 167 } 168 if (!rhsOnly.isEmpty()) { 169 message += "\nOnly in " + rhsLabel + ":\n\t" + String.join("\n\t", rhsOnly); 170 } 171 throw new AssertionError(message); 172 } 173 } 174 175 private static boolean isTreeInfoResource(String path) { 176 return pathStartsWith(path, "/packages") || pathStartsWith(path, "/modules"); 177 } 178 179 // Handle both "<prefix>" and "<prefix>/...". 180 private static boolean pathStartsWith(String path, String prefix) { 181 int plen = prefix.length(); 182 return path.startsWith(prefix) && (path.length() == plen || path.charAt(plen) == '/'); 183 } 184 185 private static void handleFileMismatch(Path a, Path b) { 186 throw new AssertionError("Files mismatch: " + a + " vs. " + b); 187 } 188 189 static class FilesCapturingVisitor extends SimpleFileVisitor<Path> { 190 private final Path basePath; 191 private final List<String> filePaths = new ArrayList<>(); 192 public FilesCapturingVisitor(Path basePath) { 193 this.basePath = basePath; 194 } 195 196 @Override 197 public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) { 198 Path relative = basePath.relativize(path); 199 filePaths.add(relative.toString()); 200 return FileVisitResult.CONTINUE; 201 } 202 203 List<String> filesVisited() { 204 return filePaths; 205 } 206 } 207 208 } --- EOF ---