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