1 /* 2 * Copyright (c) 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 8168423 27 * @summary Different types of ClassLoader running with(out) SecurityManager and 28 * (in)valid security policy file. 29 * @library /lib/testlibrary 30 * @modules java.base/jdk.internal.module 31 * @build JarUtils CompilerUtils 32 * @run main/timeout=240 ClassLoaderTest 33 */ 34 import java.io.File; 35 import java.io.OutputStream; 36 import java.nio.file.Files; 37 import java.nio.file.Path; 38 import java.nio.file.Paths; 39 import java.nio.file.StandardCopyOption; 40 import java.util.Map; 41 import java.util.HashMap; 42 import java.lang.module.ModuleDescriptor; 43 import jdk.internal.module.ModuleInfoWriter; 44 import jdk.testlibrary.ProcessTools; 45 46 public class ClassLoaderTest { 47 48 private static final String SRC = System.getProperty("test.src"); 49 private static final Path CL_SRC = Paths.get(SRC, "TestClassLoader.java"); 50 private static final Path C_SRC = Paths.get(SRC, "TestClient.java"); 51 private static final Path CL_BIN = Paths.get("classes", "clbin"); 52 private static final Path C_BIN = Paths.get("classes", "cbin"); 53 private static final Path ARTIFACT_DIR = Paths.get("jars"); 54 private static final Path VALID_POLICY = Paths.get(SRC, "valid.policy"); 55 private static final Path INVALID_POLICY 56 = Paths.get(SRC, "malformed.policy"); 57 private static final Path NO_POLICY = null; 58 private static final String LOCALE = "-Duser.language=en -Duser.region=US"; 59 /* 60 * Here is the naming convention followed for each jar. 61 * cl.jar - Regular custom class loader jar. 62 * mcl.jar - Modular custom class loader jar. 63 * c.jar - Regular client jar. 64 * mc.jar - Modular client jar. 65 * amc.jar - Modular client referring automated custom class loader jar. 66 */ 67 private static final Path CL_JAR = ARTIFACT_DIR.resolve("cl.jar"); 68 private static final Path MCL_JAR = ARTIFACT_DIR.resolve("mcl.jar"); 69 private static final Path C_JAR = ARTIFACT_DIR.resolve("c.jar"); 70 private static final Path MC_JAR = ARTIFACT_DIR.resolve("mc.jar"); 71 private static final Path AMC_JAR = ARTIFACT_DIR.resolve("amc.jar"); 72 private static final Map<String, String> MSG_MAP = new HashMap<>(); 73 74 static { 75 // This mapping help process finding expected message based 76 // on the key passed as argument while executing java command. 77 MSG_MAP.put("MissingModule", "Module cl not found, required by mc"); 78 MSG_MAP.put("ErrorPolicy", "java.security.policy: error parsing file"); 79 MSG_MAP.put( 80 "SystemCL", "jdk.internal.loader.ClassLoaders$AppClassLoader"); 81 MSG_MAP.put("CustomCL", "cl.TestClassLoader"); 82 } 83 84 public static void main(String[] args) throws Exception { 85 86 // Generates regular and modular jars before start processing it. 87 setUp(); 88 processForEachPolicyFile(); 89 } 90 91 /** 92 * Test cases are based on the following logic, 93 * for (policyFile : {"NO_POLICY", "VALID", "MALFORMED"}) { 94 * for (classLoader : {"SystemClassLoader", "CustomClassLoader"}){ 95 * for (clientModule : {"NAMED", "AUTOMATIC", "UNNAMED"}) { 96 * for (classLoaderModule : {"NAMED", "AUTOMATIC", "UNNAMED"}) { 97 * Create and run java command for each possible Test case 98 * } 99 * } 100 * } 101 * } 102 */ 103 private static void processForEachPolicyFile() throws Exception { 104 105 final String regCLloc = CL_JAR.toFile().getAbsolutePath(); 106 final String modCLloc = MCL_JAR.toFile().getAbsolutePath(); 107 final String regCloc = C_JAR.toFile().getAbsolutePath(); 108 final String modCloc = MC_JAR.toFile().getAbsolutePath(); 109 final String autoModCloc = AMC_JAR.toFile().getAbsolutePath(); 110 final String separator = File.pathSeparator; 111 112 for (Path policy 113 : new Path[]{NO_POLICY, VALID_POLICY, INVALID_POLICY}) { 114 final String policyFile = (policy != null) 115 ? policy.toFile().getAbsolutePath() : null; 116 final boolean malformedPolicy 117 = (policy == null) ? false : policy.equals(INVALID_POLICY); 118 119 for (boolean useSCL : new boolean[]{true, false}) { 120 final String clVmArg = (useSCL) ? "" 121 : "-Djava.system.class.loader=cl.TestClassLoader"; 122 final String autoAddModArg 123 = (useSCL) ? "" : "--add-modules=cl"; 124 final String addmodArg = (useSCL) ? "" : "--add-modules=mcl"; 125 final String sMArg = (policy != null) ? String.format( 126 "-Djava.security.manager -Djava.security.policy=%s", 127 policyFile) : ""; 128 final String smMsg = (policy != null) ? "With SecurityManager" 129 : "Without SecurityManager"; 130 final String expectedResult = ((!malformedPolicy) 131 ? ((useSCL) ? "PASS SystemCL" : "PASS CustomCL") 132 : "FAIL ErrorPolicy"); 133 134 // NAMED-NAMED, NAMED-AUTOMATIC, NAMED-UNNAMED 135 System.out.printf("Case:- Modular Client and %s %s%n", 136 ((useSCL) ? "SystemClassLoader" 137 : "Modular CustomClassLoader"), smMsg); 138 execute(new String[]{String.format( 139 "--module-path %s%s%s %s %s %s -m mc/c.TestClient", 140 modCloc, separator, modCLloc, LOCALE, clVmArg, sMArg), 141 expectedResult}); 142 System.out.printf("Case:- Modular Client and %s %s%n", ((useSCL) 143 ? "SystemClassLoader" 144 : "Automatic modular CustomClassLoader"), smMsg); 145 execute(new String[]{String.format( 146 "--module-path %s%s%s %s %s %s -m mc/c.TestClient", 147 autoModCloc, separator, regCLloc, LOCALE, clVmArg, sMArg), 148 expectedResult}); 149 System.out.printf("Case:- Modular Client and %s %s%n", ((useSCL) 150 ? "SystemClassLoader" 151 : "Unknown modular CustomClassLoader"), smMsg); 152 execute(new String[]{String.format( 153 "--module-path %s -cp %s %s %s %s -m mc/c.TestClient", 154 autoModCloc, regCLloc, LOCALE, clVmArg, sMArg), 155 "FAIL MissingModule"}); 156 157 // AUTOMATIC-NAMED, AUTOMATIC-AUTOMATIC, AUTOMATIC-UNNAMED 158 System.out.printf("Case:- Automated modular Client and %s %s%n", 159 ((useSCL) ? "SystemClassLoader" 160 : "Modular CustomClassLoader"), smMsg); 161 execute(new String[]{String.format( 162 "--module-path %s%s%s %s %s %s %s -m c/c.TestClient", 163 regCloc, separator, modCLloc, addmodArg, LOCALE, clVmArg, 164 sMArg), expectedResult}); 165 System.out.printf("Case:- Automated modular Client and %s %s%n", 166 ((useSCL) ? "SystemClassLoader" 167 : "Automatic modular CustomClassLoader"), 168 smMsg); 169 execute(new String[]{String.format( 170 "--module-path %s%s%s %s %s %s %s -m c/c.TestClient", 171 regCloc, separator, regCLloc, autoAddModArg, LOCALE, 172 clVmArg, sMArg), expectedResult}); 173 System.out.printf("Case:- Automated modular Client and %s %s%n", 174 ((useSCL) ? "SystemClassLoader" 175 : "Unknown modular CustomClassLoader"), smMsg); 176 execute(new String[]{String.format( 177 "--module-path %s -cp %s %s %s %s -m c/c.TestClient", 178 regCloc, regCLloc, LOCALE, clVmArg, sMArg), 179 expectedResult}); 180 181 // UNNAMED-NAMED, UNNAMED-AUTOMATIC, UNNAMED-UNNAMED 182 System.out.printf("Case:- Unknown modular Client and %s %s%n", 183 ((useSCL) ? "SystemClassLoader" 184 : "Modular CustomClassLoader"), smMsg); 185 execute(new String[]{String.format( 186 "-cp %s --module-path %s %s %s %s %s c.TestClient", 187 regCloc, modCLloc, addmodArg, LOCALE, clVmArg, sMArg), 188 expectedResult}); 189 System.out.printf("Case:- Unknown modular Client and %s %s%n", 190 ((useSCL) ? "SystemClassLoader" 191 : "Automatic modular CustomClassLoader"), 192 smMsg); 193 execute(new String[]{String.format( 194 "-cp %s --module-path %s %s %s %s %s c.TestClient", 195 regCloc, regCLloc, autoAddModArg, LOCALE, clVmArg, sMArg), 196 expectedResult}); 197 System.out.printf("Case:- Unknown modular Client and %s %s%n", 198 ((useSCL) ? "SystemClassLoader" 199 : "Unknown modular CustomClassLoader"), smMsg); 200 execute(new String[]{String.format( 201 "-cp %s%s%s %s %s %s c.TestClient", regCloc, separator, 202 regCLloc, LOCALE, clVmArg, sMArg), expectedResult}); 203 204 // Regular jars in module-path and Modular jars in class-path. 205 System.out.printf("Case:- Regular Client and %s " 206 + "inside --module-path %s.%n", ((useSCL) 207 ? "SystemClassLoader" 208 : "Unknown modular CustomClassLoader"), smMsg); 209 execute(new String[]{String.format( 210 "--module-path %s%s%s %s %s %s %s -m c/c.TestClient", 211 regCloc, separator, regCLloc, autoAddModArg, LOCALE, 212 clVmArg, sMArg), expectedResult}); 213 System.out.printf("Case:- Modular Client and %s in -cp %s%n", 214 ((useSCL) ? "SystemClassLoader" 215 : "Modular CustomClassLoader"), smMsg); 216 execute(new String[]{String.format( 217 "-cp %s%s%s %s %s %s c.TestClient", modCloc, separator, 218 modCLloc, LOCALE, clVmArg, sMArg), expectedResult}); 219 } 220 } 221 } 222 223 /** 224 * Execute with command arguments and process the result. 225 */ 226 private static void execute(String[] args) throws Exception { 227 228 String status = null; 229 String msgKey = null; 230 if ((args != null && args.length > 1)) { 231 String[] secArgs = args[1].split("\\s+"); 232 status = (secArgs.length > 0) ? secArgs[0] : null; 233 msgKey = (secArgs.length > 1) ? secArgs[1] : null; 234 } 235 String out = ProcessTools.executeTestJvm(args[0].split("\\s+")) 236 .getOutput(); 237 // Handle response. 238 if ((status != null && "PASS".equals(status) && msgKey != null 239 && out.contains(MSG_MAP.get(msgKey)))) { 240 System.out.printf("PASS: Expected Result: %s.%n", 241 MSG_MAP.get(msgKey)); 242 } else if ((status != null && "FAIL".equals(status) && msgKey != null 243 && out.contains(MSG_MAP.get(msgKey)))) { 244 System.out.printf("PASS: Expected Failure: %s.%n", 245 MSG_MAP.get(msgKey)); 246 } else if (out.contains("Exception") || out.contains("Error")) { 247 System.out.printf("OUTPUT: %s", out); 248 throw new RuntimeException("FAIL: Unknown Exception."); 249 } else { 250 System.out.printf("OUTPUT: %s", out); 251 throw new RuntimeException("FAIL: Unknown Test case found"); 252 } 253 } 254 255 /** 256 * Creates regular/modular jar files for TestClient and TestClassLoader. 257 */ 258 private static void setUp() throws Exception { 259 260 boolean compiled = CompilerUtils.compile(CL_SRC, CL_BIN); 261 compiled &= CompilerUtils.compile(C_SRC, C_BIN); 262 if (!compiled) { 263 throw new RuntimeException("Test Setup failed."); 264 } 265 // Generate regular jar files for TestClient and TestClassLoader 266 JarUtils.createJarFile(CL_JAR, CL_BIN); 267 JarUtils.createJarFile(C_JAR, C_BIN); 268 // Generate modular jar files for TestClient and TestClassLoader with 269 // their corresponding ModuleDescriptor. 270 Files.copy(CL_JAR, MCL_JAR, StandardCopyOption.REPLACE_EXISTING); 271 updateModuleDescr(MCL_JAR, ModuleDescriptor.newModule("mcl") 272 .exports("cl").requires("java.base").build()); 273 Files.copy(C_JAR, MC_JAR, StandardCopyOption.REPLACE_EXISTING); 274 updateModuleDescr(MC_JAR, ModuleDescriptor.newModule("mc") 275 .exports("c").requires("java.base").requires("mcl").build()); 276 Files.copy(C_JAR, AMC_JAR, StandardCopyOption.REPLACE_EXISTING); 277 updateModuleDescr(AMC_JAR, ModuleDescriptor.newModule("mc") 278 .exports("c").requires("java.base").requires("cl").build()); 279 } 280 281 /** 282 * Update regular jars and include module-info.class inside it to make 283 * modular jars. 284 */ 285 private static void updateModuleDescr(Path jar, ModuleDescriptor mDescr) 286 throws Exception { 287 if (mDescr != null) { 288 Path dir = Files.createTempDirectory("tmp"); 289 Path mi = dir.resolve("module-info.class"); 290 try (OutputStream out = Files.newOutputStream(mi)) { 291 ModuleInfoWriter.write(mDescr, out); 292 } 293 System.out.format("Adding 'module-info.class' to jar '%s'%n", jar); 294 JarUtils.updateJarFile(jar, dir); 295 } 296 } 297 }