|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Huawei Technologies Co., Ltd. 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 | + * @test |
| 26 | + * @bug 8240903 |
| 27 | + * @summary Test consistency of moduleHashes attribute between builds |
| 28 | + * @library /test/lib |
| 29 | + * @run testng HashesOrderTest |
| 30 | + */ |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.InputStream; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.List; |
| 40 | +import java.util.spi.ToolProvider; |
| 41 | +import java.util.stream.Collectors; |
| 42 | + |
| 43 | +import jdk.test.lib.compiler.ModuleInfoMaker; |
| 44 | + |
| 45 | +import org.testng.annotations.Test; |
| 46 | + |
| 47 | +import static org.testng.Assert.assertEquals; |
| 48 | + |
| 49 | +public class HashesOrderTest { |
| 50 | + private ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod") |
| 51 | + .orElseThrow(() -> |
| 52 | + new RuntimeException("jmod tool not found") |
| 53 | + ); |
| 54 | + |
| 55 | + // buffer size used for reading and writing |
| 56 | + private static final int BUFFER_SIZE = 8192; |
| 57 | + |
| 58 | + private int NUM_MODULES = 64; |
| 59 | + private Path mods; |
| 60 | + private Path lib1; |
| 61 | + private Path lib2; |
| 62 | + private ModuleInfoMaker builder; |
| 63 | + |
| 64 | + @Test |
| 65 | + public void test() throws Exception { |
| 66 | + mods = Path.of("mods"); |
| 67 | + lib1 = Path.of("lib1"); |
| 68 | + lib2 = Path.of("lib2"); |
| 69 | + builder = new ModuleInfoMaker(Path.of("src")); |
| 70 | + |
| 71 | + Files.createDirectories(mods); |
| 72 | + Files.createDirectories(lib1); |
| 73 | + Files.createDirectories(lib2); |
| 74 | + |
| 75 | + makeModule("ma"); |
| 76 | + String moduleName; |
| 77 | + for (int i = 0; i < NUM_MODULES; i++) { |
| 78 | + moduleName = "m" + i + "b"; |
| 79 | + makeModule(moduleName, "ma"); |
| 80 | + makeJmod(moduleName, lib1); |
| 81 | + makeJmod(moduleName, lib2); |
| 82 | + } |
| 83 | + makeJmod("ma", lib1, "--module-path", lib1.toString(), |
| 84 | + "--hash-modules", ".*"); |
| 85 | + Path jmod1 = lib1.resolve("ma.jmod"); |
| 86 | + |
| 87 | + makeJmod("ma", lib2, "--module-path", lib2.toString(), |
| 88 | + "--hash-modules", ".*"); |
| 89 | + Path jmod2 = lib2.resolve("ma.jmod"); |
| 90 | + |
| 91 | + assertEquals(mismatch(jmod1, jmod2), -1); |
| 92 | + } |
| 93 | + |
| 94 | + private void makeModule(String mn, String... deps) |
| 95 | + throws IOException |
| 96 | + { |
| 97 | + StringBuilder sb = new StringBuilder(); |
| 98 | + sb.append("module ") |
| 99 | + .append(mn) |
| 100 | + .append(" {") |
| 101 | + .append("\n"); |
| 102 | + Arrays.stream(deps) |
| 103 | + .forEach(req -> { |
| 104 | + sb.append(" requires "); |
| 105 | + sb.append(req) |
| 106 | + .append(";\n"); |
| 107 | + }); |
| 108 | + sb.append("}\n"); |
| 109 | + builder.writeJavaFiles(mn, sb.toString()); |
| 110 | + builder.compile(mn, mods); |
| 111 | + } |
| 112 | + |
| 113 | + private void makeJmod(String moduleName, Path libName, String... options) { |
| 114 | + Path mclasses = mods.resolve(moduleName); |
| 115 | + Path outfile = libName.resolve(moduleName + ".jmod"); |
| 116 | + List<String> args = new ArrayList<>(); |
| 117 | + args.add("create"); |
| 118 | + Collections.addAll(args, options); |
| 119 | + Collections.addAll(args, "--class-path", mclasses.toString(), |
| 120 | + outfile.toString()); |
| 121 | + |
| 122 | + runJmod(args); |
| 123 | + } |
| 124 | + |
| 125 | + private void runJmod(List<String> args) { |
| 126 | + runJmod(args.toArray(new String[args.size()])); |
| 127 | + } |
| 128 | + |
| 129 | + private void runJmod(String... args) { |
| 130 | + int rc = JMOD_TOOL.run(System.out, System.out, args); |
| 131 | + System.out.println("jmod " + Arrays.stream(args).collect(Collectors.joining(" "))); |
| 132 | + if (rc != 0) { |
| 133 | + throw new AssertionError("jmod failed: rc = " + rc); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private long mismatch(Path path, Path path2) throws IOException { |
| 138 | + if (Files.isSameFile(path, path2)) { |
| 139 | + return -1; |
| 140 | + } |
| 141 | + byte[] buffer1 = new byte[BUFFER_SIZE]; |
| 142 | + byte[] buffer2 = new byte[BUFFER_SIZE]; |
| 143 | + try (InputStream in1 = Files.newInputStream(path); |
| 144 | + InputStream in2 = Files.newInputStream(path2);) { |
| 145 | + long totalRead = 0; |
| 146 | + while (true) { |
| 147 | + int nRead1 = in1.readNBytes(buffer1, 0, BUFFER_SIZE); |
| 148 | + int nRead2 = in2.readNBytes(buffer2, 0, BUFFER_SIZE); |
| 149 | + |
| 150 | + int i = Arrays.mismatch(buffer1, 0, nRead1, buffer2, 0, nRead2); |
| 151 | + if (i > -1) { |
| 152 | + return totalRead + i; |
| 153 | + } |
| 154 | + if (nRead1 < BUFFER_SIZE) { |
| 155 | + // we've reached the end of the files, but found no mismatch |
| 156 | + return -1; |
| 157 | + } |
| 158 | + totalRead += nRead1; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments