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