1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/**
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8160286
* @summary Test the recording and checking of module hashes
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.base/jdk.internal.module
* jdk.compiler
* jdk.jartool
* jdk.jlink
* @build jdk.test.lib.compiler.ModuleInfoMaker
* jdk.test.lib.compiler.CompilerUtils
* @run testng HashesTest
*/
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReader;
import java.lang.module.ModuleReference;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.spi.ToolProvider;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jdk.internal.module.ModuleInfo;
import jdk.internal.module.ModuleHashes;
import jdk.internal.module.ModulePath;
import jdk.test.lib.compiler.ModuleInfoMaker;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import static java.lang.module.ModuleDescriptor.Requires.Modifier.*;
public class HashesTest {
static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
.orElseThrow(() ->
new RuntimeException("jmod tool not found")
);
static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
.orElseThrow(() ->
new RuntimeException("jar tool not found")
);
private final Path mods;
private final Path srcDir;
private final Path lib;
private final ModuleInfoMaker builder;
HashesTest(Path dest) throws IOException {
if (Files.exists(dest)) {
deleteDirectory(dest);
}
this.mods = dest.resolve("mods");
this.srcDir = dest.resolve("src");
this.lib = dest.resolve("lib");
this.builder = new ModuleInfoMaker(srcDir);
Files.createDirectories(lib);
Files.createDirectories(mods);
}
@Test
public static void test() throws IOException {
Path dest = Paths.get("test");
HashesTest ht = new HashesTest(dest);
// create modules for test cases
ht.makeModule("m2");
ht.makeModule("m3");
ht.makeModule("m1", "m2", "m3");
ht.makeModule("org.bar", TRANSITIVE, "m1");
ht.makeModule("org.foo", TRANSITIVE, "org.bar");
// create JMOD for m1, m2, m3
ht.makeJmod("m2");
ht.makeJmod("m3");
// no hash is recorded since m1 has outgoing edges
ht.jmodHashModules("m1", ".*");
// no hash is recorded in m1, m2, m3
assertTrue(ht.hashes("m1") == null);
assertTrue(ht.hashes("m2") == null);
assertTrue(ht.hashes("m3") == null);
// hash m1 in m2
ht.jmodHashModules("m2", "m1");
ht.checkHashes("m2", "m1");
// hash m1 in m2
ht.jmodHashModules("m2", ".*");
ht.checkHashes("m2", "m1");
// create m2.jmod with no hash
ht.makeJmod("m2");
// run jmod hash command to hash m1 in m2 and m3
runJmod(List.of("hash", "--module-path", ht.lib.toString(),
"--hash-modules", ".*"));
ht.checkHashes("m2", "m1");
ht.checkHashes("m3", "m1");
// check transitive requires
ht.makeJmod("org.bar");
ht.makeJmod("org.foo");
ht.jmodHashModules("org.bar", "org.*");
ht.checkHashes("org.bar", "org.foo");
ht.jmodHashModules( "m3", ".*");
ht.checkHashes("m3", "org.foo", "org.bar", "m1");
}
@Test
public static void multiBaseModules() throws IOException {
Path dest = Paths.get("test2");
HashesTest ht = new HashesTest(dest);
/*
* y2 -----------> y1
* |______
* | |
* V V
* z3 -> z2
* | |
* | V
* |---> z1
*/
ht.makeModule("z1");
ht.makeModule("z2", "z1");
ht.makeModule("z3", "z1", "z2");
ht.makeModule("y1");
ht.makeModule("y2", "y1", "z2", "z3");
Set<String> ys = Set.of("y1", "y2");
Set<String> zs = Set.of("z1", "z2", "z3");
// create JMOD files
Stream.concat(ys.stream(), zs.stream()).forEach(ht::makeJmod);
// run jmod hash command
runJmod(List.of("hash", "--module-path", ht.lib.toString(),
"--hash-modules", ".*"));
/*
* z1 and y1 are the modules with hashes recorded.
*/
ht.checkHashes("y1", "y2");
ht.checkHashes("z1", "z2", "z3", "y2");
Stream.concat(ys.stream(), zs.stream())
.filter(mn -> !mn.equals("y1") && !mn.equals("z1"))
.forEach(mn -> assertTrue(ht.hashes(mn) == null));
}
@Test
public static void mixJmodAndJarFile() throws IOException {
Path dest = Paths.get("test3");
HashesTest ht = new HashesTest(dest);
/*
* j3 -----------> j2
* |______
* | |
* V V
* m3 -> m2
* | |
* | V
* |---> m1 -> j1 -> jdk.jlink
*/
ht.makeModule("j1");
ht.makeModule("j2");
ht.makeModule("m1", "j1");
ht.makeModule("m2", "m1");
ht.makeModule("m3", "m1", "m2");
ht.makeModule("j3", "j2", "m2", "m3");
Set<String> jars = Set.of("j1", "j2", "j3");
Set<String> jmods = Set.of("m1", "m2", "m3");
// create JMOD and JAR files
jars.forEach(ht::makeJar);
jmods.forEach(ht::makeJmod);
// run jmod hash command
runJmod(List.of("hash", "--module-path", ht.lib.toString(),
"--hash-modules", "^j.*|^m.*"));
/*
* j1 and j2 are the modules with hashes recorded.
*/
ht.checkHashes("j2", "j3");
ht.checkHashes("j1", "m1", "m2", "m3", "j3");
Stream.concat(jars.stream(), jmods.stream())
.filter(mn -> !mn.equals("j1") && !mn.equals("j2"))
.forEach(mn -> assertTrue(ht.hashes(mn) == null));
}
@Test
public static void testImageJmods() throws IOException {
Path mpath = Paths.get(System.getProperty("java.home"), "jmods");
if (!Files.exists(mpath)) {
return;
}
Path dest = Paths.get("test5");
HashesTest ht = new HashesTest(dest);
ht.makeModule("m1", "jdk.compiler", "jdk.attach");
ht.makeModule("m2", "m1");
ht.makeModule("m3", "java.compiler");
ht.makeJmod("m1");
ht.makeJmod("m2");
runJmod(List.of("hash",
"--module-path",
mpath.toString() + File.pathSeparator + ht.lib.toString(),
"--hash-modules", ".*"));
validateImageJmodsTest(ht, mpath);
}
@Test
public static void testImageJmods1() throws IOException {
Path mpath = Paths.get(System.getProperty("java.home"), "jmods");
if (!Files.exists(mpath)) {
return;
}
Path dest = Paths.get("test6");
HashesTest ht = new HashesTest(dest);
ht.makeModule("m1", "jdk.compiler", "jdk.attach");
ht.makeModule("m2", "m1");
ht.makeModule("m3", "java.compiler");
ht.makeJar("m2");
ht.makeJar("m1",
"--module-path",
mpath.toString() + File.pathSeparator + ht.lib.toString(),
"--hash-modules", ".*");
validateImageJmodsTest(ht, mpath);
}
private static void validateImageJmodsTest(HashesTest ht, Path mpath)
throws IOException
{
// hash is recorded in m1 and not any other packaged modules on module path
ht.checkHashes("m1", "m2");
assertTrue(ht.hashes("m2") == null);
// should not override any JDK packaged modules
ModuleFinder finder = ModulePath.of(Runtime.version(), true, mpath);
assertTrue(ht.hashes(finder,"jdk.compiler") == null);
assertTrue(ht.hashes(finder,"jdk.attach") == null);
}
private void checkHashes(String mn, String... hashModules) throws IOException {
ModuleHashes hashes = hashes(mn);
assertTrue(hashes.names().equals(Set.of(hashModules)));
}
private ModuleHashes hashes(String name) {
ModuleFinder finder = ModulePath.of(Runtime.version(), true, lib);
return hashes(finder, name);
}
private ModuleHashes hashes(ModuleFinder finder, String name) {
ModuleReference mref = finder.find(name).orElseThrow(RuntimeException::new);
try {
ModuleReader reader = mref.open();
try (InputStream in = reader.open("module-info.class").get()) {
ModuleHashes hashes = ModuleInfo.read(in, null).recordedHashes();
System.out.format("hashes in module %s %s%n", name,
(hashes != null) ? "present" : "absent");
if (hashes != null) {
hashes.names().stream().sorted().forEach(n ->
System.out.format(" %s %s%n", n, toHex(hashes.hashFor(n)))
);
}
return hashes;
} finally {
reader.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private String toHex(byte[] ba) {
StringBuilder sb = new StringBuilder(ba.length);
for (byte b: ba) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
private void deleteDirectory(Path dir) throws IOException {
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException
{
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException
{
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
private void makeModule(String mn, String... deps) throws IOException {
makeModule(mn, null, deps);
}
private void makeModule(String mn, ModuleDescriptor.Requires.Modifier mod, String... deps)
throws IOException
{
if (mod != null && mod != TRANSITIVE && mod != STATIC) {
throw new IllegalArgumentException(mod.toString());
}
StringBuilder sb = new StringBuilder();
sb.append("module ")
.append(mn)
.append(" {")
.append("\n");
Arrays.stream(deps)
.forEach(req -> {
sb.append(" requires ");
if (mod != null) {
sb.append(mod.toString().toLowerCase())
.append(" ");
}
sb.append(req)
.append(";\n");
});
sb.append("}\n");
builder.writeJavaFiles(mn, sb.toString());
builder.compile(mn, mods);
}
private void jmodHashModules(String moduleName, String hashModulesPattern) {
makeJmod(moduleName, "--module-path", lib.toString(),
"--hash-modules", hashModulesPattern);
}
private void makeJmod(String moduleName, String... options) {
Path mclasses = mods.resolve(moduleName);
Path outfile = lib.resolve(moduleName + ".jmod");
List<String> args = new ArrayList<>();
args.add("create");
Collections.addAll(args, options);
Collections.addAll(args, "--class-path", mclasses.toString(),
outfile.toString());
if (Files.exists(outfile)) {
try {
Files.delete(outfile);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
runJmod(args);
}
private static void runJmod(List<String> args) {
int rc = JMOD_TOOL.run(System.out, System.out, args.toArray(new String[args.size()]));
System.out.println("jmod " + args.stream().collect(Collectors.joining(" ")));
if (rc != 0) {
throw new AssertionError("jmod failed: rc = " + rc);
}
}
private void makeJar(String moduleName, String... options) {
Path mclasses = mods.resolve(moduleName);
Path outfile = lib.resolve(moduleName + ".jar");
List<String> args = new ArrayList<>();
Stream.concat(Stream.of("--create",
"--file=" + outfile.toString()),
Arrays.stream(options))
.forEach(args::add);
args.add("-C");
args.add(mclasses.toString());
args.add(".");
if (Files.exists(outfile)) {
try {
Files.delete(outfile);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
int rc = JAR_TOOL.run(System.out, System.out, args.toArray(new String[args.size()]));
System.out.println("jar " + args.stream().collect(Collectors.joining(" ")));
if (rc != 0) {
throw new AssertionError("jar failed: rc = " + rc);
}
}
}
|