< prev index next >

test/tools/launcher/modules/patch/systemmodules/PatchSystemModules.java

Print this page


   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)


   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
  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.testlibrary.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     private static final Path PATCH_SRC_DIR = TEST_SRC.resolve("src1");
  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         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         // create new copy of m1.jar
  90         jar("--create",
  91             "--file=" + NEW_M1_JAR.toString(),
  92             "-C", MODS_DIR.resolve("m1").toString(), ".");
  93     }
  94 
  95     /*
  96      * Test patching system module and user module on module path
  97      */
  98     @Test
  99     public void test() throws Throwable {
 100         Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE);
 101         Path patchedM2 = PATCH_DIR.resolve("m2");
 102 
 103         Path home = Paths.get(JAVA_HOME);
 104         runTest(home,
 105                 "--module-path", MODS_DIR.toString(),
 106                 "-m", "m1/p1.Main", "1");
 107         runTest(home,
 108                 "--patch-module", "java.base=" + patchedJavaBase,
 109                 "--module-path", MODS_DIR.toString(),
 110                 "-m", "m1/p1.Main", "1");
 111 
 112         runTest(home,
 113                 "--patch-module", "m2=" + patchedM2.toString(),
 114                 "--module-path", MODS_DIR.toString(),
 115                 "-m", "m1/p1.Main", "2");
 116     }
 117 
 118     /*
 119      * Test --patch-module on a custom image
 120      */
 121     @Test
 122     public void testImage() throws Throwable {
 123         if (Files.notExists(JMODS))
 124             return;
 125 
 126         Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE);
 127         Path patchedM2 = PATCH_DIR.resolve("m2");
 128 
 129         runTest(IMAGE,
 130                 "-m", "m1/p1.Main", "1");
 131         runTest(IMAGE,
 132                 "--patch-module", "java.base=" + patchedJavaBase,
 133                 "-m", "m1/p1.Main", "1");
 134         runTest(IMAGE,
 135                 "--patch-module", "m2=" + patchedM2.toString(),
 136                 "-m", "m1/p1.Main", "2");
 137     }
 138 
 139     /*
 140      * Test a module linked in a system hashed in ModuleHashes attribute
 141      * cannot be upgraded
 142      */
 143     @Test
 144     public void upgradeHashedModule() throws Throwable {
 145         if (Files.notExists(JMODS))
 146             return;
 147 
 148         // Fail to upgrade m1.jar with mismatched hash
 149         runTestWithExitCode(getJava(IMAGE),
 150                 "--upgrade-module-path", NEW_M1_JAR.toString(),
 151                 "-m", "m1/p1.Main", "ShouldNeverRun");
 152 
 153         // test when SystemModules fast path is not enabled, i.e. exploded image
 154         runTestWithExitCode(getJava(IMAGE),
 155                 "--patch-module", "java.base=" + PATCH_DIR.resolve(JAVA_BASE),
 156                 "--upgrade-module-path", NEW_M1_JAR.toString(),
 157                 "-m", "m1/p1.Main", "ShouldNeverRun");
 158     }
 159 
 160     /*
 161      * Test a module linked in a system hashed in ModuleHashes attribute
 162      * cannot be upgraded combining with --patch-module and --upgrade-module-path
 163      */
 164     @Test
 165     public void patchHashedModule() throws Throwable {
 166         if (Files.notExists(JMODS))
 167             return;
 168 
 169         // --patch-module does not disable hash check.
 170         // Test that a hashed module cannot be upgraded.
 171         runTestWithExitCode(getJava(IMAGE),
 172                 "--patch-module", "m1=.jar",
 173                 "--upgrade-module-path", NEW_M1_JAR.toString(),
 174                 "-m", "m1/p1.Main", "ShouldNeverRun");
 175 
 176         // test when SystemModules fast path is not enabled, i.e. exploded image
 177         runTestWithExitCode(getJava(IMAGE),
 178                 "--patch-module", "java.base=" + PATCH_DIR.resolve(JAVA_BASE),
 179                 "--patch-module", "m1=.jar",
 180                 "--upgrade-module-path", NEW_M1_JAR.toString(),
 181                 "-m", "m1/p1.Main", "ShouldNeverRun");
 182     }
 183 
 184     private void runTestWithExitCode(String... options) throws Throwable {
 185         assertTrue(executeCommand(options)
 186                         .outputTo(System.out)
 187                         .errorTo(System.out)
 188                         .shouldContain("differs to expected hash")
 189                         .getExitValue() != 0);
 190     }
 191 
 192     private void runTest(Path image, String... opts) throws Throwable {
 193         String[] options =
 194             Stream.concat(Stream.of(getJava(image)),
 195                           Stream.of(opts))
 196                   .toArray(String[]::new);
 197 
 198         ProcessBuilder pb = new ProcessBuilder(options);
 199         int exitValue =  executeCommand(pb)
 200                             .outputTo(System.out)
 201                             .errorTo(System.out)


< prev index next >