1 /**
   2  * Copyright (c) 2016, 2017, 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 /*
  25  * @test
  26  * @bug 8157068 8177844
  27  * @summary Patch java.base and user module with ModuleHashes attribute
  28  * @library /lib/testlibrary /test/lib
  29  * @modules jdk.compiler
  30  * @build jdk.test.lib.compiler.CompilerUtils
  31  * @run testng PatchSystemModules
  32  */
  33 
  34 import java.io.File;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.util.ArrayList;
  39 import java.util.List;
  40 import java.util.stream.Stream;
  41 
  42 import jdk.test.lib.compiler.CompilerUtils;
  43 import jdk.test.lib.util.FileUtils;
  44 import jdk.testlibrary.JDKToolFinder;
  45 import org.testng.annotations.BeforeTest;
  46 import org.testng.annotations.Test;
  47 
  48 import static jdk.testlibrary.ProcessTools.executeCommand;
  49 import static org.testng.Assert.*;
  50 
  51 public class PatchSystemModules {
  52     private static final String JAVA_HOME = System.getProperty("java.home");
  53 
  54     private static final Path TEST_SRC = Paths.get(System.getProperty("test.src"));
  55 
  56     private static final Path JMODS = Paths.get(JAVA_HOME, "jmods");
  57     private static final Path MODS_DIR = Paths.get("mods");
  58     private static final Path JARS_DIR = Paths.get("jars");
  59     private static final Path PATCH_DIR = Paths.get("patches");
  60     private static final Path IMAGE = Paths.get("image");
  61     private static final Path NEW_M1_JAR = JARS_DIR.resolve("new_m1.jar");
  62 
  63     private static final String JAVA_BASE = "java.base";
  64     private final String[] modules = new String[] { "m1", "m2" };
  65 
  66     @BeforeTest
  67     private void setup() throws Throwable {
  68         Path src = TEST_SRC.resolve("src");
  69         Path src1 = TEST_SRC.resolve("src1");
  70 
  71         for (String name : modules) {
  72             assertTrue(CompilerUtils.compile(src.resolve(name),
  73                                              MODS_DIR,
  74                                              "--module-source-path", src.toString()));
  75         }
  76 
  77         // compile patched source
  78         String patchDir = src1.resolve(JAVA_BASE).toString();
  79         assertTrue(CompilerUtils.compile(src1.resolve(JAVA_BASE),
  80                                          PATCH_DIR.resolve(JAVA_BASE),
  81                                          "--patch-module", "java.base=" + patchDir));
  82         assertTrue(CompilerUtils.compile(src1.resolve("m2"),
  83                                          PATCH_DIR.resolve("m2")));
  84 
  85         createJars();
  86 
  87         // create an image with only m1 and m2
  88         if (Files.exists(JMODS)) {
  89             // create an image with m1,m2
  90             createImage();
  91         }
  92 
  93         // compile a different version of m1
  94         Path tmp = Paths.get("tmp");
  95         assertTrue(CompilerUtils.compile(src1.resolve("m1"), tmp,
  96                                          "--module-path", MODS_DIR.toString(),
  97                                          "--module-source-path", src1.toString()));
  98 
  99         // package new_m1.jar
 100         jar("--create",
 101             "--file=" + NEW_M1_JAR.toString(),
 102             "-C", tmp.resolve("m1").toString(), ".");
 103     }
 104 
 105     /*
 106      * Test patching system module and user module on module path
 107      */
 108     @Test
 109     public void test() throws Throwable {
 110         Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE);
 111         Path patchedM2 = PATCH_DIR.resolve("m2");
 112 
 113         Path home = Paths.get(JAVA_HOME);
 114         runTest(home,
 115                 "--module-path", MODS_DIR.toString(),
 116                 "-m", "m1/p1.Main", "1");
 117         runTest(home,
 118                 "--patch-module", "java.base=" + patchedJavaBase,
 119                 "--module-path", MODS_DIR.toString(),
 120                 "-m", "m1/p1.Main", "1");
 121 
 122         runTest(home,
 123                 "--patch-module", "m2=" + patchedM2.toString(),
 124                 "--module-path", MODS_DIR.toString(),
 125                 "-m", "m1/p1.Main", "2");
 126     }
 127 
 128     /*
 129      * Test --patch-module on a custom image
 130      */
 131     @Test
 132     public void testImage() throws Throwable {
 133         if (Files.notExists(JMODS))
 134             return;
 135 
 136         Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE);
 137         Path patchedM2 = PATCH_DIR.resolve("m2");
 138 
 139         runTest(IMAGE,
 140                 "-m", "m1/p1.Main", "1");
 141         runTest(IMAGE,
 142                 "--patch-module", "java.base=" + patchedJavaBase,
 143                 "-m", "m1/p1.Main", "1");
 144         runTest(IMAGE,
 145                 "--patch-module", "m2=" + patchedM2.toString(),
 146                 "-m", "m1/p1.Main", "2");
 147     }
 148 
 149     /*
 150      * Test a module linked in a system hashed in ModuleHashes attribute
 151      * cannot be upgraded
 152      */
 153     @Test
 154     public void upgradeHashedModule() throws Throwable {
 155         if (Files.notExists(JMODS))
 156             return;
 157 
 158         // Fail to upgrade m1.jar with mismatched hash
 159         runTestWithExitCode(getJava(IMAGE),
 160                 "--upgrade-module-path", NEW_M1_JAR.toString(),
 161                 "-m", "m1/p1.Main");
 162 
 163         // test when SystemModules fast path is not enabled, i.e. exploded image
 164         runTestWithExitCode(getJava(IMAGE),
 165                 "--patch-module", "java.base=" + PATCH_DIR.resolve(JAVA_BASE),
 166                 "--upgrade-module-path", NEW_M1_JAR.toString(),
 167                 "-m", "m1/p1.Main");
 168     }
 169 
 170     /*
 171      * Test a module linked in a system hashed in ModuleHashes attribute
 172      * cannot be upgraded combining with --patch-module and --upgrade-module-path
 173      */
 174     @Test
 175     public void patchHashedModule() throws Throwable {
 176         if (Files.notExists(JMODS))
 177             return;
 178 
 179         // --patch-module does not disable hash check.
 180         // Test that a hashed module cannot be upgraded.
 181         runTestWithExitCode(getJava(IMAGE),
 182                 "--patch-module", "m1=.jar",
 183                 "--upgrade-module-path", NEW_M1_JAR.toString(),
 184                 "-m", "m1/p1.Main");
 185 
 186         // test when SystemModules fast path is not enabled, i.e. exploded image
 187         runTestWithExitCode(getJava(IMAGE),
 188                 "--patch-module", "java.base=" + PATCH_DIR.resolve(JAVA_BASE),
 189                 "--patch-module", "m1=.jar",
 190                 "--upgrade-module-path", NEW_M1_JAR.toString(),
 191                 "-m", "m1/p1.Main");
 192     }
 193 
 194     private void runTestWithExitCode(String... options) throws Throwable {
 195         assertTrue(executeCommand(options)
 196                         .outputTo(System.out)
 197                         .errorTo(System.out)
 198                         .shouldContain("differs to expected hash")
 199                         .getExitValue() != 0);
 200     }
 201 
 202     private void runTest(Path image, String... opts) throws Throwable {
 203         String[] options =
 204             Stream.concat(Stream.of(getJava(image)),
 205                           Stream.of(opts))
 206                   .toArray(String[]::new);
 207 
 208         ProcessBuilder pb = new ProcessBuilder(options);
 209         int exitValue =  executeCommand(pb)
 210                             .outputTo(System.out)
 211                             .errorTo(System.out)
 212                             .getExitValue();
 213 
 214         assertTrue(exitValue == 0);
 215     }
 216 
 217     static void createJars() throws Throwable {
 218         FileUtils.deleteFileTreeUnchecked(JARS_DIR);
 219 
 220         Files.createDirectories(JARS_DIR);
 221         Path m1 = JARS_DIR.resolve("m1.jar");
 222         Path m2 = JARS_DIR.resolve("m2.jar");
 223 
 224         // hash m1 in m2's Hashes attribute
 225         jar("--create",
 226             "--file=" + m1.toString(),
 227             "-C", MODS_DIR.resolve("m1").toString(), ".");
 228 
 229         jar("--create",
 230             "--file=" + m2.toString(),
 231             "--module-path", JARS_DIR.toString(),
 232             "--hash-modules", "m1",
 233             "-C", MODS_DIR.resolve("m2").toString(), ".");
 234     }
 235 
 236     static void createImage() throws Throwable {
 237         FileUtils.deleteFileTreeUnchecked(IMAGE);
 238 
 239         String mpath = JARS_DIR.toString() + File.pathSeparator + JMODS.toString();
 240         execTool("jlink", "--module-path", mpath,
 241                  "--add-modules", "m1",
 242                  "--output", IMAGE.toString());
 243     }
 244 
 245     static void jar(String... args) throws Throwable {
 246         execTool("jar", args);
 247     }
 248 
 249     static void execTool(String tool, String... args) throws Throwable {
 250         String path = JDKToolFinder.getJDKTool(tool);
 251         List<String> commands = new ArrayList<>();
 252         commands.add(path);
 253         Stream.of(args).forEach(commands::add);
 254         ProcessBuilder pb = new ProcessBuilder(commands);
 255         int exitValue =  executeCommand(pb)
 256             .outputTo(System.out)
 257             .errorTo(System.out)
 258             .shouldNotContain("no module is recorded in hash")
 259             .getExitValue();
 260 
 261         assertTrue(exitValue == 0);
 262     }
 263 
 264     static String getJava(Path image) {
 265         boolean isWindows = System.getProperty("os.name").startsWith("Windows");
 266         Path java = image.resolve("bin").resolve(isWindows ? "java.exe" : "java");
 267         if (Files.notExists(java))
 268             throw new RuntimeException(java + " not found");
 269         return java.toAbsolutePath().toString();
 270     }
 271 }