1 /**
   2  * Copyright (c) 2016, 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
  27  * @summary Patch java.base and user module with Hashes attribute tied with
  28  *          other module.
  29  * @library /lib/testlibrary
  30  * @modules jdk.compiler
  31  * @build CompilerUtils
  32  * @run testng PatchSystemModules
  33  */
  34 
  35 import java.io.File;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 import java.util.ArrayList;
  40 import java.util.List;
  41 import java.util.stream.Stream;
  42 
  43 import jdk.testlibrary.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     private static final Path PATCH_SRC_DIR = TEST_SRC.resolve("src1");
  56 
  57     private static final Path JMODS = Paths.get(JAVA_HOME, "jmods");
  58     private static final Path MODS_DIR = Paths.get("mods");
  59     private static final Path JARS_DIR = Paths.get("jars");
  60     private static final Path PATCH_DIR = Paths.get("patches");
  61     private static final Path IMAGE = Paths.get("image");
  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         for (String name : modules) {
  70             assertTrue(CompilerUtils.compile(src.resolve(name),
  71                                              MODS_DIR,
  72                                              "--module-source-path", src.toString()));
  73         }
  74 
  75         // compile patched source
  76         String patchDir = PATCH_SRC_DIR.resolve(JAVA_BASE).toString();
  77         assertTrue(CompilerUtils.compile(PATCH_SRC_DIR.resolve(JAVA_BASE),
  78                                          PATCH_DIR.resolve(JAVA_BASE),
  79                                          "--patch-module", "java.base=" + patchDir));
  80         assertTrue(CompilerUtils.compile(PATCH_SRC_DIR.resolve("m2"),
  81                                          PATCH_DIR.resolve("m2")));
  82 
  83         // create an image with only m1 and m2
  84         if (Files.exists(JMODS)) {
  85             // create an image with m1,m2
  86             createImage();
  87         }
  88     }
  89 
  90     @Test
  91     public void test() throws Throwable {
  92         Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE);
  93         Path patchedM2 = PATCH_DIR.resolve("m2");
  94 
  95         Path home = Paths.get(JAVA_HOME);
  96         runTest(home,
  97                 "--module-path", MODS_DIR.toString(),
  98                 "-m", "m1/p1.Main", "1");
  99         runTest(home,
 100                 "--patch-module", "java.base=" + patchedJavaBase,
 101                 "--module-path", MODS_DIR.toString(),
 102                 "-m", "m1/p1.Main", "1");
 103 
 104         runTest(home,
 105                 "--patch-module", "m2=" + patchedM2.toString(),
 106                 "--module-path", MODS_DIR.toString(),
 107                 "-m", "m1/p1.Main", "2");
 108     }
 109 
 110     @Test
 111     public void testImage() throws Throwable {
 112         if (Files.notExists(JMODS))
 113             return;
 114 
 115         Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE);
 116         Path patchedM2 = PATCH_DIR.resolve("m2");
 117 
 118         runTest(IMAGE,
 119                 "-m", "m1/p1.Main", "1");
 120         runTest(IMAGE,
 121                 "--patch-module", "java.base=" + patchedJavaBase,
 122                 "-m", "m1/p1.Main", "1");
 123         runTest(IMAGE,
 124                 "--patch-module", "m2=" + patchedM2.toString(),
 125                 "-m", "m1/p1.Main", "2");
 126     }
 127 
 128     @Test
 129     public void upgradeTiedModule() throws Throwable {
 130         if (Files.notExists(JMODS))
 131             return;
 132 
 133         Path m1 = MODS_DIR.resolve("m1.jar");
 134 
 135         // create another m1.jar
 136         jar("--create",
 137             "--file=" + m1.toString(),
 138             "-C", MODS_DIR.resolve("m1").toString(), ".");
 139 
 140         // Fail to upgrade m1.jar with mismatched hash
 141         runTestWithExitCode(getJava(IMAGE),
 142                 "--upgrade-module-path", m1.toString(),
 143                 "-m", "m1/p1.Main");
 144 
 145         runTestWithExitCode(getJava(IMAGE),
 146                 "--patch-module", "java.base=" + PATCH_DIR.resolve(JAVA_BASE),
 147                 "--upgrade-module-path", m1.toString(),
 148                 "-m", "m1/p1.Main", "1");
 149     }
 150 
 151     private void runTestWithExitCode(String... options) throws Throwable {
 152         assertTrue(executeCommand(options)
 153                         .outputTo(System.out)
 154                         .errorTo(System.out)
 155                         .shouldContain("differs to expected hash")
 156                         .getExitValue() != 0);
 157     }
 158 
 159     private void runTest(Path image, String... opts) throws Throwable {
 160         String[] options =
 161             Stream.concat(Stream.of(getJava(image)),
 162                           Stream.of(opts))
 163                   .toArray(String[]::new);
 164 
 165         ProcessBuilder pb = new ProcessBuilder(options);
 166         int exitValue =  executeCommand(pb)
 167                             .outputTo(System.out)
 168                             .errorTo(System.out)
 169                             .getExitValue();
 170 
 171         assertTrue(exitValue == 0);
 172     }
 173 
 174     static void createImage() throws Throwable {
 175         FileUtils.deleteFileTreeUnchecked(JARS_DIR);
 176         FileUtils.deleteFileTreeUnchecked(IMAGE);
 177 
 178         Files.createDirectories(JARS_DIR);
 179         Path m1 = JARS_DIR.resolve("m1.jar");
 180         Path m2 = JARS_DIR.resolve("m2.jar");
 181 
 182         // hash m1 in m2's Hashes attribute
 183         jar("--create",
 184             "--file=" + m1.toString(),
 185             "-C", MODS_DIR.resolve("m1").toString(), ".");
 186 
 187         jar("--create",
 188             "--file=" + m2.toString(),
 189             "--module-path", JARS_DIR.toString(),
 190             "--hash-modules", "m1",
 191             "-C", MODS_DIR.resolve("m2").toString(), ".");
 192 
 193 
 194         String mpath = JARS_DIR.toString() + File.pathSeparator + JMODS.toString();
 195         execTool("jlink", "--module-path", mpath,
 196                  "--add-modules", "m1",
 197                  "--output", IMAGE.toString());
 198     }
 199 
 200     static void jar(String... args) throws Throwable {
 201         execTool("jar", args);
 202     }
 203 
 204     static void execTool(String tool, String... args) throws Throwable {
 205         String path = JDKToolFinder.getJDKTool(tool);
 206         List<String> commands = new ArrayList<>();
 207         commands.add(path);
 208         Stream.of(args).forEach(commands::add);
 209         ProcessBuilder pb = new ProcessBuilder(commands);
 210         int exitValue =  executeCommand(pb)
 211             .outputTo(System.out)
 212             .errorTo(System.out)
 213             .shouldNotContain("no module is recorded in hash")
 214             .getExitValue();
 215 
 216         assertTrue(exitValue == 0);
 217     }
 218 
 219     static String getJava(Path image) {
 220         boolean isWindows = System.getProperty("os.name").startsWith("Windows");
 221         Path java = image.resolve("bin").resolve(isWindows ? "java.exe" : "java");
 222         if (Files.notExists(java))
 223             throw new RuntimeException(java + " not found");
 224         return java.toAbsolutePath().toString();
 225     }
 226 }