1 /* 2 * Copyright (c) 2015, 2018, 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.cds & !vm.graal.enabled 28 * @library ../.. 29 * @library /test/lib 30 * @modules java.base/jdk.internal.misc 31 * @modules jdk.jartool/sun.tools.jar 32 * @compile src/jdk/test/Main.java 33 * @compile src/com/sun/tools/javac/Main.jasm 34 * @compile src/com/sun/tools/javac/MyMain.jasm 35 * @compile ../../../SharedArchiveFile/javax/annotation/processing/FilerException.jasm 36 * @run main ClassPathTests 37 * @summary AppCDS tests for testing classpath/package conflicts 38 */ 39 40 /* 41 * These tests will verify that AppCDS will correctly handle archived classes 42 * on the classpath that are in a package that is also exported by the jimage. 43 * These classes should fail to load unless --limit-modules is used to hide the 44 * package exported by the jimage. There are 8 variants of this test: 45 * - With a jimage app package and with a jimage ext package 46 * - With --limit-modules and without --limit-modules 47 * - With AppCDS and without AppCDS (to verify behaviour is the same for both). 48 * 49 * There is also a 9th test to verify that when --limit-modules is used, a jimage 50 * class in the archive can be replaced by a classpath class with the 51 * same name and package. 52 */ 53 54 import java.lang.reflect.Method; 55 import java.nio.file.Path; 56 import java.nio.file.Paths; 57 58 import jdk.test.lib.Asserts; 59 import jdk.test.lib.cds.CDSOptions; 60 import jdk.test.lib.cds.CDSTestUtils; 61 import jdk.test.lib.process.ProcessTools; 62 import jdk.test.lib.process.OutputAnalyzer; 63 64 65 public class ClassPathTests { 66 private static final String TEST_SRC = System.getProperty("test.src"); 67 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 68 private static final Path CLASSES_DIR = Paths.get("classes"); 69 70 // the test module 71 private static final String MAIN_CLASS = "jdk.test.Main"; 72 private static final String LIMITMODS_MAIN_CLASS = "jdk.test.LimitModsMain"; 73 74 // test classes to archive. These are both in UPGRADED_MODULES 75 private static final String JIMAGE_CLASS = "com/sun/tools/javac/Main"; 76 private static final String APP_ARCHIVE_CLASS = "com/sun/tools/javac/MyMain"; 77 private static final String PLATFORM_ARCHIVE_CLASS = "javax/annotation/processing/FilerException"; 78 private static final String[] ARCHIVE_CLASSES = {APP_ARCHIVE_CLASS, PLATFORM_ARCHIVE_CLASS, JIMAGE_CLASS}; 79 private static final int NUMBER_OF_TEST_CASES = 10; 80 81 private static String appJar; 82 private static String testArchiveName; 83 84 85 public static void main(String[] args) throws Exception { 86 ClassPathTests tests = new ClassPathTests(); 87 tests.dumpArchive(); 88 89 Method[] methods = tests.getClass().getDeclaredMethods(); 90 int numOfTestMethodsRun = 0; 91 for (Method m : methods) { 92 if (m.getName().startsWith("test")) { 93 System.out.println("About to run test method: " + m.getName()); 94 m.invoke(tests); 95 numOfTestMethodsRun++; 96 } 97 } 98 99 Asserts.assertTrue((numOfTestMethodsRun == NUMBER_OF_TEST_CASES), 100 "Expected " + NUMBER_OF_TEST_CASES + " test methods to run, actual number is " 101 + numOfTestMethodsRun); 102 } 103 104 private void dumpArchive() throws Exception { 105 // Create a jar file with all the classes related to this test. 106 JarBuilder.build( "classpathtests", 107 APP_ARCHIVE_CLASS, PLATFORM_ARCHIVE_CLASS, JIMAGE_CLASS, 108 "jdk/test/Main"); 109 appJar = TestCommon.getTestJar("classpathtests.jar"); 110 111 // dump the archive with altnernate jdk.comiler and jdk.activation classes in the class list 112 OutputAnalyzer output1 = TestCommon.dump(appJar, TestCommon.list(ARCHIVE_CLASSES)); 113 TestCommon.checkDump(output1); 114 // The PLATFORM_ARCHIVE_CLASS belongs to the java.compiler 115 // module will be found from the module during dumping. 116 // The JIMAGE_CLASS will be found from the jdk.compiler module during 117 // dumping. 118 // The APP_ARCHIVE_CLASS, which belongs to a package within the 119 // jdk.compiler module, will not be found during dump time. 120 for (String archiveClass : ARCHIVE_CLASSES) { 121 if (archiveClass.equals(APP_ARCHIVE_CLASS)) { 122 output1.shouldContain("Preload Warning: Cannot find " + archiveClass); 123 } else { 124 output1.shouldNotContain("Preload Warning: Cannot find " + archiveClass); 125 } 126 } 127 128 testArchiveName = TestCommon.getCurrentArchiveName(); 129 } 130 131 // #1: Archived classpath class in same package as jimage app class. With AppCDS. 132 // Should fail to load. 133 public void testAppClassWithAppCDS() throws Exception { 134 OutputAnalyzer output = TestCommon.exec( 135 appJar, MAIN_CLASS, 136 "Test #1", APP_ARCHIVE_CLASS, "false"); // last 3 args passed to test 137 TestCommon.checkExec(output); 138 } 139 140 // #2: Archived classpath class in same package as jimage app class. Without AppCDS. 141 // Should fail to load. 142 public void testAppClassWithoutAppCDS() throws Exception { 143 CDSOptions opts = (new CDSOptions()) 144 .addPrefix("-cp", appJar) 145 .setArchiveName(testArchiveName) 146 .addSuffix(MAIN_CLASS, "Test #2", APP_ARCHIVE_CLASS, "false"); 147 148 CDSTestUtils.runWithArchiveAndCheck(opts); 149 } 150 151 // #3: Archived classpath class in same package as jimage ext class. With AppCDS. 152 // The class should be loaded from the module. 153 public void testExtClassWithAppCDS() throws Exception { 154 OutputAnalyzer output = TestCommon.exec( 155 appJar, MAIN_CLASS, 156 "Test #3", PLATFORM_ARCHIVE_CLASS, "true", "EXT"); // last 4 args passed to test 157 TestCommon.checkExec(output); 158 } 159 160 // #4: Archived classpath class in same package as jimage ext class. Without AppCDS. 161 // The class should be loaded from the module. 162 public void testExtClassWithoutAppCDS() throws Exception { 163 CDSOptions opts = (new CDSOptions()) 164 .addPrefix("-cp", appJar) 165 .setArchiveName(testArchiveName) 166 .addSuffix(MAIN_CLASS, "Test #4", PLATFORM_ARCHIVE_CLASS, "true", "EXT"); 167 168 CDSTestUtils.runWithArchiveAndCheck(opts); 169 } 170 171 // #5: Archived classpath class in same package as jimage app class. With AppCDS. 172 // Should load with CDS disabled because --limit-modules is used. 173 public void testAppClassWithLimitModsWithAppCDS() throws Exception { 174 TestCommon.run("-cp", appJar, 175 "--limit-modules", "java.base", 176 MAIN_CLASS, 177 "Test #5", APP_ARCHIVE_CLASS, "true") // last 3 args passed to test 178 .assertSilentlyDisabledCDS(out -> { 179 out.shouldHaveExitValue(0); 180 }); 181 } 182 183 // #6: Archived classpath class in same package as jimage app class. Without AppCDS. 184 // Should load with CDS disabled because --limit-modules is used. 185 public void testAppClassWithLimitModsWithoutAppCDS() throws Exception { 186 CDSOptions opts = (new CDSOptions()) 187 .addPrefix("-cp", appJar, "--limit-modules", "java.base") 188 .setArchiveName(testArchiveName) 189 .addSuffix(MAIN_CLASS, "Test #6", APP_ARCHIVE_CLASS, "true"); 190 CDSTestUtils.run(opts) 191 .assertSilentlyDisabledCDS(out -> { 192 out.shouldHaveExitValue(0); 193 }); 194 } 195 196 // #7: Archived classpath class in same package as jimage ext class. With AppCDS. 197 // Should load with CDS disabled because --limit-modules is used. 198 public void testExtClassWithLimitModsWithAppCDS() throws Exception { 199 TestCommon.run("-cp", appJar, 200 "--limit-modules", "java.base", 201 MAIN_CLASS, 202 "Test #7", PLATFORM_ARCHIVE_CLASS, "true") // last 3 args passed to test 203 .assertSilentlyDisabledCDS(out -> { 204 out.shouldHaveExitValue(0); 205 }); 206 } 207 208 // #8: Archived classpath class in same package as jimage ext class. Without AppCDS. 209 // Should load with CDS disabled because --limit-modules is used. 210 public void testExtClassWithLimitModsWithoutAppCDS() throws Exception { 211 CDSOptions opts = (new CDSOptions()) 212 .addPrefix("-cp", appJar, "--limit-modules", "java.base") 213 .setArchiveName(testArchiveName) 214 .addSuffix(MAIN_CLASS, "Test #8", PLATFORM_ARCHIVE_CLASS, "true"); 215 CDSTestUtils.run(opts) 216 .assertSilentlyDisabledCDS(out -> { 217 out.shouldHaveExitValue(0); 218 }); 219 } 220 221 // #9: Archived classpath class with same name as jimage app class. With AppCDS. 222 // Should load with CDS disabled because --limit-modules is used. 223 public void testReplacingJImageClassWithAppCDS() throws Exception { 224 TestCommon.run("-cp", appJar, 225 "--limit-modules", "java.base", 226 MAIN_CLASS, 227 "Test #9", JIMAGE_CLASS, "true") // last 3 args passed to test 228 .assertSilentlyDisabledCDS(out -> { 229 out.shouldHaveExitValue(0); 230 }); 231 } 232 233 // #10: Archived classpath class with same name as jimage app class. Without AppCDS. 234 // Should load because --limit-modules is used. Note the archive will actually contain 235 // the original jimage version of the class, but AppCDS should refuse to load it 236 // since --limit-modules is used. This should result in the -cp version being used. 237 public void testReplacingJImageClassWithoutAppCDS() throws Exception { 238 CDSOptions opts = (new CDSOptions()) 239 .addPrefix("-cp", appJar, "--limit-modules", "java.base") 240 .setArchiveName(testArchiveName) 241 .addSuffix(MAIN_CLASS, "Test #10", JIMAGE_CLASS, "true"); 242 CDSTestUtils.run(opts) 243 .assertSilentlyDisabledCDS(out -> { 244 out.shouldHaveExitValue(0); 245 }); 246 } 247 }