|
| 1 | +/* |
| 2 | + * Copyright (c) 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 | +import jdk.test.lib.process.ProcessTools; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.UncheckedIOException; |
| 29 | +import java.nio.file.*; |
| 30 | + |
| 31 | +import java.nio.file.attribute.BasicFileAttributes; |
| 32 | +import java.util.Optional; |
| 33 | + |
| 34 | +/* |
| 35 | + * @test |
| 36 | + * @summary Make sure that jimages are consistent when created by jlink. Copies test jdk and runs against original. |
| 37 | + * @bug 8252730 |
| 38 | + * @modules jdk.jlink |
| 39 | + * jdk.management |
| 40 | + * jdk.unsupported |
| 41 | + * jdk.charsets |
| 42 | + * @library /test/lib |
| 43 | + * @run main JLinkReproducible3Test |
| 44 | + */ |
| 45 | +public class JLinkReproducible3Test { |
| 46 | + |
| 47 | + public static void main(String[] args) throws Exception { |
| 48 | + Path image1 = Paths.get("./image1"); |
| 49 | + Path image2 = Paths.get("./image2"); |
| 50 | + |
| 51 | + Path copyJdk1Dir = Path.of("./copy-jdk1-tmpdir"); |
| 52 | + Files.createDirectory(copyJdk1Dir); |
| 53 | + |
| 54 | + Path copyJdk2Dir = Path.of("./copy-jdk2-tmpdir"); |
| 55 | + Files.createDirectory(copyJdk2Dir); |
| 56 | + |
| 57 | + Path jdkTestDir = Path.of( |
| 58 | + Optional.of( |
| 59 | + System.getProperty("test.jdk")) |
| 60 | + .orElseThrow(() -> new RuntimeException("Couldn't load JDK Test Dir")) |
| 61 | + ); |
| 62 | + |
| 63 | + copyJDK(jdkTestDir, copyJdk1Dir); |
| 64 | + copyJDK(jdkTestDir, copyJdk2Dir); |
| 65 | + |
| 66 | + Path copiedJlink1 = Optional.of( |
| 67 | + Paths.get(copyJdk1Dir.toString(), "bin", "jlink")) |
| 68 | + .orElseThrow(() -> new RuntimeException("Unable to load copied jlink") |
| 69 | + ); |
| 70 | + |
| 71 | + Path copiedJlink2 = Optional.of( |
| 72 | + Paths.get(copyJdk2Dir.toString(), "bin", "jlink")) |
| 73 | + .orElseThrow(() -> new RuntimeException("Unable to load copied jlink") |
| 74 | + ); |
| 75 | + |
| 76 | + runCopiedJlink(copiedJlink1.toString(), "--add-modules", "java.base,jdk.management,jdk.unsupported,jdk.charsets", "--output", image1.toString()); |
| 77 | + runCopiedJlink(copiedJlink2.toString(), "--add-modules", "java.base,jdk.management,jdk.unsupported,jdk.charsets", "--output", image2.toString()); |
| 78 | + |
| 79 | + long mismatch = Files.mismatch(image1.resolve("lib").resolve("modules"), image2.resolve("lib").resolve("modules")); |
| 80 | + if (mismatch != -1L) { |
| 81 | + throw new RuntimeException("jlink producing inconsistent result in modules. Mismatch in modules file occurred at byte position " + mismatch); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static void runCopiedJlink(String... args) throws Exception { |
| 86 | + var process = new ProcessBuilder(args); |
| 87 | + var res = ProcessTools.executeProcess(process); |
| 88 | + res.shouldHaveExitValue(0); |
| 89 | + } |
| 90 | + |
| 91 | + private static void copyJDK(Path src, Path dst) throws Exception { |
| 92 | + Files.walk(src).skip(1).forEach(file -> { |
| 93 | + try { |
| 94 | + Files.copy(file, dst.resolve(src.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES); |
| 95 | + } catch (IOException ioe) { |
| 96 | + throw new UncheckedIOException(ioe); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments