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