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.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/Main2.jasm 35 * @compile src/javax/activation/UnsupportedDataTypeException2.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/Main2"; 77 private static final String PLATFORM_ARCHIVE_CLASS = "javax/activation/UnsupportedDataTypeException2"; 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 // Only a class that belongs to a module which is not defined by default 115 // can be found. In this case the PLATFORM_ARCHIVE_CLASS belongs 116 // to the java.activation which is not defined by default; it is the only 117 // class can be found during dumping. 118 for (String archiveClass : ARCHIVE_CLASSES) { 119 if (archiveClass.equals(PLATFORM_ARCHIVE_CLASS)) { 120 output1.shouldNotContain("Preload Warning: Cannot find " + archiveClass); 121 } else { 122 output1.shouldContain("Preload Warning: Cannot find " + archiveClass); 123 } 124 } 125 126 testArchiveName = TestCommon.getCurrentArchiveName(); 127 } 128 129 // #1: Archived classpath class in same package as jimage app class. With AppCDS. 130 // Should fail to load. 131 public void testAppClassWithAppCDS() throws Exception { 132 OutputAnalyzer output = TestCommon.exec( 133 appJar, MAIN_CLASS, 134 "Test #1", APP_ARCHIVE_CLASS, "false"); // last 3 args passed to test 135 TestCommon.checkExec(output); 136 } 137 138 // #2: Archived classpath class in same package as jimage app class. Without AppCDS. 139 // Should fail to load. 140 public void testAppClassWithoutAppCDS() throws Exception { 141 CDSOptions opts = (new CDSOptions()) 142 .addPrefix("-cp", appJar) 143 .setArchiveName(testArchiveName) 144 .addSuffix(MAIN_CLASS, "Test #2", APP_ARCHIVE_CLASS, "false"); 145 146 CDSTestUtils.runWithArchiveAndCheck(opts); 147 } 148 149 // For tests #3 and #4, we need to "--add-modules java.activation" since the 150 // java.activation module won't be defined by default. 151 152 // #3: Archived classpath class in same package as jimage ext class. With AppCDS. 153 // Should fail to load. 154 public void testExtClassWithAppCDS() throws Exception { 155 OutputAnalyzer output = TestCommon.exec( 156 appJar, "--add-modules", "java.activation", MAIN_CLASS, 157 "Test #3", PLATFORM_ARCHIVE_CLASS, "false"); // last 3 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 // Should fail to load. 163 public void testExtClassWithoutAppCDS() throws Exception { 164 CDSOptions opts = (new CDSOptions()) 165 .addPrefix("-cp", appJar, "--add-modules", "java.activation") 166 .setArchiveName(testArchiveName) 167 .addSuffix(MAIN_CLASS, "Test #4", PLATFORM_ARCHIVE_CLASS, "false"); 168 169 CDSTestUtils.runWithArchiveAndCheck(opts); 170 } 171 172 // #5: Archived classpath class in same package as jimage app class. With AppCDS. 173 // Should load because --limit-modules is used. 174 public void testAppClassWithLimitModsWithAppCDS() throws Exception { 175 OutputAnalyzer output = TestCommon.exec( 176 appJar, 177 "--limit-modules", "java.base", 178 MAIN_CLASS, 179 "Test #5", APP_ARCHIVE_CLASS, "true"); // last 3 args passed to test 180 TestCommon.checkExec(output); 181 } 182 183 // #6: Archived classpath class in same package as jimage app class. Without AppCDS. 184 // Should load 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 191 CDSTestUtils.runWithArchiveAndCheck(opts); 192 } 193 194 // #7: Archived classpath class in same package as jimage ext class. With AppCDS. 195 // Should load because --limit-modules is used. 196 public void testExtClassWithLimitModsWithAppCDS() throws Exception { 197 OutputAnalyzer output = TestCommon.exec( 198 appJar, 199 "--limit-modules", "java.base", 200 MAIN_CLASS, 201 "Test #7", PLATFORM_ARCHIVE_CLASS, "true"); // last 3 args passed to test 202 TestCommon.checkExec(output); 203 } 204 205 // #8: Archived classpath class in same package as jimage ext class. Without AppCDS. 206 // Should load because --limit-modules is used. 207 public void testExtClassWithLimitModsWithoutAppCDS() throws Exception { 208 CDSOptions opts = (new CDSOptions()) 209 .addPrefix("-cp", appJar, "--limit-modules", "java.base") 210 .setArchiveName(testArchiveName) 211 .addSuffix(MAIN_CLASS, "Test #8", PLATFORM_ARCHIVE_CLASS, "true"); 212 213 CDSTestUtils.runWithArchiveAndCheck(opts); 214 } 215 216 // #9: Archived classpath class with same name as jimage app class. With AppCDS. 217 // Should load because --limit-modules is used. 218 public void testReplacingJImageClassWithAppCDS() throws Exception { 219 OutputAnalyzer output = TestCommon.exec( 220 appJar, 221 "--limit-modules", "java.base", "-XX:+TraceClassLoading", 222 MAIN_CLASS, 223 "Test #9", JIMAGE_CLASS, "true"); // last 3 args passed to test 224 TestCommon.checkExec(output); 225 } 226 227 // #10: Archived classpath class with same name as jimage app class. Without AppCDS. 228 // Should load because --limit-modules is used. Note the archive will actually contain 229 // the original jimage version of the class, but AppCDS should refuse to load it 230 // since --limit-modules is used. This should result in the -cp version being used. 231 public void testReplacingJImageClassWithoutAppCDS() throws Exception { 232 CDSOptions opts = (new CDSOptions()) 233 .addPrefix("-cp", appJar, "--limit-modules", "java.base") 234 .setArchiveName(testArchiveName) 235 .addSuffix(MAIN_CLASS, "Test #10", JIMAGE_CLASS, "true"); 236 237 CDSTestUtils.runWithArchiveAndCheck(opts); 238 } 239 240 }