Path: blob/master/test/jdk/tools/launcher/modules/patch/systemmodules/PatchSystemModules.java
41159 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8157068 817784426* @summary Patch java.base and user module with ModuleHashes attribute27* @library /test/lib28* @modules jdk.compiler29* @build jdk.test.lib.compiler.CompilerUtils30* jdk.test.lib.util.FileUtils31* jdk.test.lib.Platform32* @run testng PatchSystemModules33*/3435import java.io.File;36import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.util.ArrayList;40import java.util.List;41import java.util.stream.Stream;4243import jdk.test.lib.compiler.CompilerUtils;44import jdk.test.lib.util.FileUtils;45import jdk.test.lib.JDKToolFinder;46import org.testng.annotations.BeforeTest;47import org.testng.annotations.Test;4849import static jdk.test.lib.process.ProcessTools.executeCommand;50import static org.testng.Assert.*;5152public class PatchSystemModules {53private static final String JAVA_HOME = System.getProperty("java.home");5455private static final Path TEST_SRC = Paths.get(System.getProperty("test.src"));5657private static final Path JMODS = Paths.get(JAVA_HOME, "jmods");58private static final Path MODS_DIR = Paths.get("mods");59private static final Path JARS_DIR = Paths.get("jars");60private static final Path PATCH_DIR = Paths.get("patches");61private static final Path IMAGE = Paths.get("image");62private static final Path NEW_M1_JAR = JARS_DIR.resolve("new_m1.jar");6364private static final String JAVA_BASE = "java.base";65private final String[] modules = new String[] { "m1", "m2" };6667@BeforeTest68private void setup() throws Throwable {69Path src = TEST_SRC.resolve("src");70Path src1 = TEST_SRC.resolve("src1");7172for (String name : modules) {73assertTrue(CompilerUtils.compile(src.resolve(name),74MODS_DIR,75"--module-source-path", src.toString()));76}7778// compile patched source79String patchDir = src1.resolve(JAVA_BASE).toString();80assertTrue(CompilerUtils.compile(src1.resolve(JAVA_BASE),81PATCH_DIR.resolve(JAVA_BASE),82"--patch-module", "java.base=" + patchDir));83assertTrue(CompilerUtils.compile(src1.resolve("m2"),84PATCH_DIR.resolve("m2")));8586createJars();8788// create an image with only m1 and m289if (Files.exists(JMODS)) {90// create an image with m1,m291createImage();92}9394// compile a different version of m195Path tmp = Paths.get("tmp");96assertTrue(CompilerUtils.compile(src1.resolve("m1"), tmp,97"--module-path", MODS_DIR.toString(),98"--module-source-path", src1.toString()));99100// package new_m1.jar101jar("--create",102"--file=" + NEW_M1_JAR.toString(),103"-C", tmp.resolve("m1").toString(), ".");104}105106/*107* Test patching system module and user module on module path108*/109@Test110public void test() throws Throwable {111Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE);112Path patchedM2 = PATCH_DIR.resolve("m2");113114Path home = Paths.get(JAVA_HOME);115runTest(home,116"--module-path", MODS_DIR.toString(),117"-m", "m1/p1.Main", "1");118runTest(home,119"--patch-module", "java.base=" + patchedJavaBase,120"--module-path", MODS_DIR.toString(),121"-m", "m1/p1.Main", "1");122123runTest(home,124"--patch-module", "m2=" + patchedM2.toString(),125"--module-path", MODS_DIR.toString(),126"-m", "m1/p1.Main", "2");127}128129/*130* Test --patch-module on a custom image131*/132@Test133public void testImage() throws Throwable {134if (Files.notExists(JMODS))135return;136137Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE);138Path patchedM2 = PATCH_DIR.resolve("m2");139140runTest(IMAGE,141"-m", "m1/p1.Main", "1");142runTest(IMAGE,143"--patch-module", "java.base=" + patchedJavaBase,144"-m", "m1/p1.Main", "1");145runTest(IMAGE,146"--patch-module", "m2=" + patchedM2.toString(),147"-m", "m1/p1.Main", "2");148}149150/*151* Test a module linked in a system hashed in ModuleHashes attribute152* cannot be upgraded153*/154@Test155public void upgradeHashedModule() throws Throwable {156if (Files.notExists(JMODS))157return;158159// Fail to upgrade m1.jar with mismatched hash160runTestWithExitCode(getJava(IMAGE),161"--upgrade-module-path", NEW_M1_JAR.toString(),162"-m", "m1/p1.Main");163164// test when SystemModules fast path is not enabled, i.e. exploded image165runTestWithExitCode(getJava(IMAGE),166"--patch-module", "java.base=" + PATCH_DIR.resolve(JAVA_BASE),167"--upgrade-module-path", NEW_M1_JAR.toString(),168"-m", "m1/p1.Main");169}170171/*172* Test a module linked in a system hashed in ModuleHashes attribute173* cannot be upgraded combining with --patch-module and --upgrade-module-path174*/175@Test176public void patchHashedModule() throws Throwable {177if (Files.notExists(JMODS))178return;179180// --patch-module does not disable hash check.181// Test that a hashed module cannot be upgraded.182runTestWithExitCode(getJava(IMAGE),183"--patch-module", "m1=.jar",184"--upgrade-module-path", NEW_M1_JAR.toString(),185"-m", "m1/p1.Main");186187// test when SystemModules fast path is not enabled, i.e. exploded image188runTestWithExitCode(getJava(IMAGE),189"--patch-module", "java.base=" + PATCH_DIR.resolve(JAVA_BASE),190"--patch-module", "m1=.jar",191"--upgrade-module-path", NEW_M1_JAR.toString(),192"-m", "m1/p1.Main");193}194195private void runTestWithExitCode(String... options) throws Throwable {196assertTrue(executeCommand(options)197.outputTo(System.out)198.errorTo(System.out)199.shouldContain("differs to expected hash")200.getExitValue() != 0);201}202203private void runTest(Path image, String... opts) throws Throwable {204String[] options =205Stream.concat(Stream.of(getJava(image)),206Stream.of(opts))207.toArray(String[]::new);208209ProcessBuilder pb = new ProcessBuilder(options);210int exitValue = executeCommand(pb)211.outputTo(System.out)212.errorTo(System.out)213.getExitValue();214215assertTrue(exitValue == 0);216}217218static void createJars() throws Throwable {219FileUtils.deleteFileTreeUnchecked(JARS_DIR);220221Files.createDirectories(JARS_DIR);222Path m1 = JARS_DIR.resolve("m1.jar");223Path m2 = JARS_DIR.resolve("m2.jar");224225// hash m1 in m2's Hashes attribute226jar("--create",227"--file=" + m1.toString(),228"-C", MODS_DIR.resolve("m1").toString(), ".");229230jar("--create",231"--file=" + m2.toString(),232"--module-path", JARS_DIR.toString(),233"--hash-modules", "m1",234"-C", MODS_DIR.resolve("m2").toString(), ".");235}236237static void createImage() throws Throwable {238FileUtils.deleteFileTreeUnchecked(IMAGE);239240String mpath = JARS_DIR.toString() + File.pathSeparator + JMODS.toString();241execTool("jlink", "--module-path", mpath,242"--add-modules", "m1",243"--output", IMAGE.toString());244}245246static void jar(String... args) throws Throwable {247execTool("jar", args);248}249250static void execTool(String tool, String... args) throws Throwable {251String path = JDKToolFinder.getJDKTool(tool);252List<String> commands = new ArrayList<>();253commands.add(path);254Stream.of(args).forEach(commands::add);255ProcessBuilder pb = new ProcessBuilder(commands);256int exitValue = executeCommand(pb)257.outputTo(System.out)258.errorTo(System.out)259.shouldNotContain("no module is recorded in hash")260.getExitValue();261262assertTrue(exitValue == 0);263}264265static String getJava(Path image) {266boolean isWindows = System.getProperty("os.name").startsWith("Windows");267Path java = image.resolve("bin").resolve(isWindows ? "java.exe" : "java");268if (Files.notExists(java))269throw new RuntimeException(java + " not found");270return java.toAbsolutePath().toString();271}272}273274275