1 /* 2 * Copyright (c) 2015, 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 /** 26 * @test 27 * @requires (vm.opt.UseCompressedOops == null) | (vm.opt.UseCompressedOops == true) 28 * @library ../.. 29 * @library /test/lib 30 * @modules java.base/jdk.internal.misc 31 * @modules jdk.jartool/sun.tools.jar 32 * jdk.internal.jvmstat/sun.jvmstat.monitor 33 * @compile LimitModsHelper.java 34 * @compile ../../test-classes/java/net/HttpCookie.jasm 35 * @compile ../../test-classes/jdk/dynalink/DynamicLinker.jasm 36 * @compile ../../test-classes/com/sun/tools/javac/Main.jasm 37 * @build sun.hotspot.WhiteBox 38 * @run main ClassFileInstaller sun.hotspot.WhiteBox 39 * @run main LimitModsTests 40 * @summary AppCDS tests for excluding class in module by using --limit-modules. 41 */ 42 43 /** 44 * This is for testing the --limit-modules option with AppCDS. 45 * This test assumes the following defining class loader, module, class relations: 46 * class loader module class 47 * ----------------------------------------------------- 48 * boot java.base java/net/HttpCookie 49 * platform jdk.dynalink jdk/dynalink/DynamicLinker 50 * app jdk.compiler com/sun/tools/javac/Main 51 * 52 * This test dumps the above 3 classes into a shared archive. 53 * Then it will run the following 4 -limit-modules scenarios: 54 * 1. without --limit-modules 55 * All 3 classes should be loaded successfully. 56 * All 3 classes should be loaded by the appropriate class loader. 57 * All 3 classes should be found in the shared archive. 58 * 2. --limit-modules java.base,jdk.dynalink 59 * The loading of the com/sun/tools/javac/Main class should fail. 60 * The other 2 classes should be loaded successfully and by the appropriate class loader. 61 * The other 2 classes should be found in the shared archive. 62 * 3. --limit-modules java.base,jdk.compiler 63 * The loading of the jdk/nio/dynalink/DynamicLinker class should fail. 64 * The other 2 classes should be loaded successfully and by the appropriate class loader. 65 * The other 2 classes should be found in the shared archive. 66 * 4. --limit-modules jdk.dynalink,jdk.compiler 67 * The java.base module can't be excluded. 68 * The results for this case is the same as for case #1. 69 */ 70 71 import java.io.File; 72 import java.nio.file.Path; 73 import java.nio.file.Paths; 74 75 import jdk.test.lib.process.ProcessTools; 76 import jdk.test.lib.process.OutputAnalyzer; 77 78 79 public class LimitModsTests { 80 81 // the module that is limited 82 private static final String[] LIMIT_MODULES = {"java.base", "jdk.dynalink", "jdk.compiler"}; 83 84 // test classes to archive. 85 private static final String BOOT_ARCHIVE_CLASS = "java/net/HttpCookie"; 86 private static final String PLATFORM_ARCHIVE_CLASS = "jdk/dynalink/DynamicLinker"; 87 private static final String APP_ARCHIVE_CLASS = "com/sun/tools/javac/Main"; 88 private static final String[] ARCHIVE_CLASSES = { 89 BOOT_ARCHIVE_CLASS, PLATFORM_ARCHIVE_CLASS, APP_ARCHIVE_CLASS}; 90 private String bootClassPath = null; 91 private String whiteBoxJar = null; 92 private String helperJar = null; 93 private String appJar = null; 94 private OutputAnalyzer output = null; 95 96 public static void main(String[] args) throws Exception { 97 LimitModsTests tests = new LimitModsTests(); 98 tests.dumpArchive(); 99 tests.runTestNoLimitMods(); 100 tests.runTestLimitMods(); 101 } 102 103 void dumpArchive() throws Exception { 104 JarBuilder.build("limitModsTest", BOOT_ARCHIVE_CLASS, PLATFORM_ARCHIVE_CLASS, APP_ARCHIVE_CLASS); 105 JarBuilder.build(true, "WhiteBox", "sun/hotspot/WhiteBox"); 106 JarBuilder.build("limitModsHelper", "LimitModsHelper"); 107 108 appJar = TestCommon.getTestJar("limitModsTest.jar"); 109 whiteBoxJar = TestCommon.getTestJar("WhiteBox.jar"); 110 helperJar = TestCommon.getTestJar("limitModsHelper.jar"); 111 bootClassPath = "-Xbootclasspath/a:" + whiteBoxJar; 112 // Dump the test classes into the archive 113 OutputAnalyzer output1 = TestCommon.dump(appJar, TestCommon.list(ARCHIVE_CLASSES), bootClassPath); 114 TestCommon.checkDump(output1); 115 // Make sure all the classes where successfully archived. 116 for (String archiveClass : ARCHIVE_CLASSES) { 117 output1.shouldNotContain("Preload Warning: Cannot find " + archiveClass); 118 } 119 } 120 121 // run the test without --limit-modules 122 public void runTestNoLimitMods() throws Exception { 123 output = TestCommon.exec( 124 appJar + File.pathSeparator + helperJar, 125 "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", bootClassPath, 126 "LimitModsHelper", 127 BOOT_ARCHIVE_CLASS, PLATFORM_ARCHIVE_CLASS, APP_ARCHIVE_CLASS, "-1"); // last 4 args passed to test 128 TestCommon.checkExec(output); 129 } 130 131 // run the test with --limit-modules 132 // 133 // --limit-modules jdk.dynalink,jdk.compiler 134 // It seems we can't exclude the java.base module. For this case, 135 // although the java.base module isn't in --limit-modules, the class 136 // in the java.base module (java.net.HttpCookie) can also be found. 137 // 138 // --limit-modules java.base,jdk.dynalink 139 // --limit-modules java.base,jdk.compiler 140 public void runTestLimitMods() throws Exception { 141 String limitMods = null; 142 for (int excludeModIdx = 0; excludeModIdx < 3; excludeModIdx++) { 143 for (int includeModIdx = 0; includeModIdx < 3; includeModIdx++) { 144 if (includeModIdx != excludeModIdx) { 145 if (limitMods != null) { 146 limitMods += ","; 147 limitMods += LIMIT_MODULES[includeModIdx]; 148 } else { 149 limitMods = LIMIT_MODULES[includeModIdx]; 150 } 151 } 152 } 153 output = TestCommon.exec( 154 appJar + File.pathSeparator + helperJar, 155 "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", bootClassPath, 156 "--limit-modules", limitMods, 157 "LimitModsHelper", 158 BOOT_ARCHIVE_CLASS, PLATFORM_ARCHIVE_CLASS, APP_ARCHIVE_CLASS, 159 Integer.toString(excludeModIdx)); // last 4 args passed to test 160 TestCommon.checkExec(output); 161 limitMods = null; 162 } 163 } 164 }