< prev index next >

test/java/lang/ClassLoader/securityManager/ClassLoaderTest.java

Print this page

        

*** 26,63 **** * @bug 8168423 * @summary Different types of ClassLoader running with(out) SecurityManager and * (in)valid security policy file. * @library /lib/testlibrary * @modules java.base/jdk.internal.module ! * @build JarUtils CompilerUtils ! * @run main/timeout=240 ClassLoaderTest */ import java.io.File; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; ! import java.util.Map; ! import java.util.HashMap; import java.lang.module.ModuleDescriptor; import jdk.internal.module.ModuleInfoWriter; import jdk.testlibrary.ProcessTools; public class ClassLoaderTest { private static final String SRC = System.getProperty("test.src"); ! private static final Path CL_SRC = Paths.get(SRC, "TestClassLoader.java"); ! private static final Path C_SRC = Paths.get(SRC, "TestClient.java"); ! private static final Path CL_BIN = Paths.get("classes", "clbin"); ! private static final Path C_BIN = Paths.get("classes", "cbin"); private static final Path ARTIFACT_DIR = Paths.get("jars"); private static final Path VALID_POLICY = Paths.get(SRC, "valid.policy"); private static final Path INVALID_POLICY = Paths.get(SRC, "malformed.policy"); - private static final Path NO_POLICY = null; - private static final String LOCALE = "-Duser.language=en -Duser.region=US"; /* * Here is the naming convention followed for each jar. * cl.jar - Regular custom class loader jar. * mcl.jar - Modular custom class loader jar. * c.jar - Regular client jar. --- 26,67 ---- * @bug 8168423 * @summary Different types of ClassLoader running with(out) SecurityManager and * (in)valid security policy file. * @library /lib/testlibrary * @modules java.base/jdk.internal.module ! * @build JarUtils ! * @build TestClassLoader TestClient ! * @run main ClassLoaderTest -noPolicy ! * @run main ClassLoaderTest -validPolicy ! * @run main ClassLoaderTest -invalidPolicy ! * @run main ClassLoaderTest -noPolicy -customSCL ! * @run main ClassLoaderTest -validPolicy -customSCL ! * @run main ClassLoaderTest -invalidPolicy -customSCL */ import java.io.File; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; ! import java.util.stream.Stream; import java.lang.module.ModuleDescriptor; + import java.util.Collections; + import java.util.LinkedList; + import java.util.List; import jdk.internal.module.ModuleInfoWriter; import jdk.testlibrary.ProcessTools; public class ClassLoaderTest { private static final String SRC = System.getProperty("test.src"); ! private static final Path TEST_CLASSES = ! Paths.get(System.getProperty("test.classes")); private static final Path ARTIFACT_DIR = Paths.get("jars"); private static final Path VALID_POLICY = Paths.get(SRC, "valid.policy"); private static final Path INVALID_POLICY = Paths.get(SRC, "malformed.policy"); /* * Here is the naming convention followed for each jar. * cl.jar - Regular custom class loader jar. * mcl.jar - Modular custom class loader jar. * c.jar - Regular client jar.
*** 67,250 **** private static final Path CL_JAR = ARTIFACT_DIR.resolve("cl.jar"); private static final Path MCL_JAR = ARTIFACT_DIR.resolve("mcl.jar"); private static final Path C_JAR = ARTIFACT_DIR.resolve("c.jar"); private static final Path MC_JAR = ARTIFACT_DIR.resolve("mc.jar"); private static final Path AMC_JAR = ARTIFACT_DIR.resolve("amc.jar"); - private static final Map<String, String> MSG_MAP = new HashMap<>(); ! static { ! // This mapping help process finding expected message based ! // on the key passed as argument while executing java command. ! MSG_MAP.put("MissingModule", "Module cl not found, required by mc"); ! MSG_MAP.put("ErrorPolicy", "java.security.policy: error parsing file"); ! MSG_MAP.put( ! "SystemCL", "jdk.internal.loader.ClassLoaders$AppClassLoader"); ! MSG_MAP.put("CustomCL", "cl.TestClassLoader"); } public static void main(String[] args) throws Exception { ! // Generates regular and modular jars before start processing it. setUp(); ! processForEachPolicyFile(); } /** * Test cases are based on the following logic, ! * for (policyFile : {"NO_POLICY", "VALID", "MALFORMED"}) { ! * for (classLoader : {"SystemClassLoader", "CustomClassLoader"}){ ! * for (clientModule : {"NAMED", "AUTOMATIC", "UNNAMED"}) { ! * for (classLoaderModule : {"NAMED", "AUTOMATIC", "UNNAMED"}) { * Create and run java command for each possible Test case * } * } - * } - * } */ ! private static void processForEachPolicyFile() throws Exception { ! ! final String regCLloc = CL_JAR.toFile().getAbsolutePath(); ! final String modCLloc = MCL_JAR.toFile().getAbsolutePath(); ! final String regCloc = C_JAR.toFile().getAbsolutePath(); ! final String modCloc = MC_JAR.toFile().getAbsolutePath(); final String autoModCloc = AMC_JAR.toFile().getAbsolutePath(); final String separator = File.pathSeparator; ! for (Path policy ! : new Path[]{NO_POLICY, VALID_POLICY, INVALID_POLICY}) { ! final String policyFile = (policy != null) ! ? policy.toFile().getAbsolutePath() : null; ! final boolean malformedPolicy ! = (policy == null) ? false : policy.equals(INVALID_POLICY); ! ! for (boolean useSCL : new boolean[]{true, false}) { ! final String clVmArg = (useSCL) ? "" ! : "-Djava.system.class.loader=cl.TestClassLoader"; ! final String autoAddModArg ! = (useSCL) ? "" : "--add-modules=cl"; ! final String addmodArg = (useSCL) ? "" : "--add-modules=mcl"; ! final String sMArg = (policy != null) ? String.format( ! "-Djava.security.manager -Djava.security.policy=%s", ! policyFile) : ""; ! final String smMsg = (policy != null) ? "With SecurityManager" ! : "Without SecurityManager"; ! final String expectedResult = ((!malformedPolicy) ! ? ((useSCL) ? "PASS SystemCL" : "PASS CustomCL") ! : "FAIL ErrorPolicy"); ! ! // NAMED-NAMED, NAMED-AUTOMATIC, NAMED-UNNAMED ! System.out.printf("Case:- Modular Client and %s %s%n", ((useSCL) ? "SystemClassLoader" ! : "Modular CustomClassLoader"), smMsg); ! execute(new String[]{String.format( ! "--module-path %s%s%s %s %s %s -m mc/c.TestClient", ! modCloc, separator, modCLloc, LOCALE, clVmArg, sMArg), ! expectedResult}); ! System.out.printf("Case:- Modular Client and %s %s%n", ((useSCL) ! ? "SystemClassLoader" ! : "Automatic modular CustomClassLoader"), smMsg); ! execute(new String[]{String.format( ! "--module-path %s%s%s %s %s %s -m mc/c.TestClient", ! autoModCloc, separator, regCLloc, LOCALE, clVmArg, sMArg), ! expectedResult}); ! System.out.printf("Case:- Modular Client and %s %s%n", ((useSCL) ? "SystemClassLoader" ! : "Unknown modular CustomClassLoader"), smMsg); ! execute(new String[]{String.format( ! "--module-path %s -cp %s %s %s %s -m mc/c.TestClient", ! autoModCloc, regCLloc, LOCALE, clVmArg, sMArg), ! "FAIL MissingModule"}); ! // AUTOMATIC-NAMED, AUTOMATIC-AUTOMATIC, AUTOMATIC-UNNAMED ! System.out.printf("Case:- Automated modular Client and %s %s%n", ! ((useSCL) ? "SystemClassLoader" ! : "Modular CustomClassLoader"), smMsg); ! execute(new String[]{String.format( ! "--module-path %s%s%s %s %s %s %s -m c/c.TestClient", ! regCloc, separator, modCLloc, addmodArg, LOCALE, clVmArg, ! sMArg), expectedResult}); ! System.out.printf("Case:- Automated modular Client and %s %s%n", ((useSCL) ? "SystemClassLoader" ! : "Automatic modular CustomClassLoader"), ! smMsg); ! execute(new String[]{String.format( ! "--module-path %s%s%s %s %s %s %s -m c/c.TestClient", ! regCloc, separator, regCLloc, autoAddModArg, LOCALE, ! clVmArg, sMArg), expectedResult}); ! System.out.printf("Case:- Automated modular Client and %s %s%n", ! ((useSCL) ? "SystemClassLoader" ! : "Unknown modular CustomClassLoader"), smMsg); ! execute(new String[]{String.format( ! "--module-path %s -cp %s %s %s %s -m c/c.TestClient", ! regCloc, regCLloc, LOCALE, clVmArg, sMArg), ! expectedResult}); ! // UNNAMED-NAMED, UNNAMED-AUTOMATIC, UNNAMED-UNNAMED ! System.out.printf("Case:- Unknown modular Client and %s %s%n", ! ((useSCL) ? "SystemClassLoader" ! : "Modular CustomClassLoader"), smMsg); ! execute(new String[]{String.format( ! "-cp %s --module-path %s %s %s %s %s c.TestClient", ! regCloc, modCLloc, addmodArg, LOCALE, clVmArg, sMArg), ! expectedResult}); ! System.out.printf("Case:- Unknown modular Client and %s %s%n", ((useSCL) ? "SystemClassLoader" ! : "Automatic modular CustomClassLoader"), ! smMsg); ! execute(new String[]{String.format( ! "-cp %s --module-path %s %s %s %s %s c.TestClient", ! regCloc, regCLloc, autoAddModArg, LOCALE, clVmArg, sMArg), ! expectedResult}); ! System.out.printf("Case:- Unknown modular Client and %s %s%n", ! ((useSCL) ? "SystemClassLoader" ! : "Unknown modular CustomClassLoader"), smMsg); ! execute(new String[]{String.format( ! "-cp %s%s%s %s %s %s c.TestClient", regCloc, separator, ! regCLloc, LOCALE, clVmArg, sMArg), expectedResult}); ! ! // Regular jars in module-path and Modular jars in class-path. ! System.out.printf("Case:- Regular Client and %s " ! + "inside --module-path %s.%n", ((useSCL) ? "SystemClassLoader" ! : "Unknown modular CustomClassLoader"), smMsg); ! execute(new String[]{String.format( ! "--module-path %s%s%s %s %s %s %s -m c/c.TestClient", ! regCloc, separator, regCLloc, autoAddModArg, LOCALE, ! clVmArg, sMArg), expectedResult}); ! System.out.printf("Case:- Modular Client and %s in -cp %s%n", ((useSCL) ? "SystemClassLoader" ! : "Modular CustomClassLoader"), smMsg); ! execute(new String[]{String.format( ! "-cp %s%s%s %s %s %s c.TestClient", modCloc, separator, ! modCLloc, LOCALE, clVmArg, sMArg), expectedResult}); ! } } } /** * Execute with command arguments and process the result. */ ! private static void execute(String[] args) throws Exception { ! String status = null; ! String msgKey = null; ! if ((args != null && args.length > 1)) { ! String[] secArgs = args[1].split("\\s+"); ! status = (secArgs.length > 0) ? secArgs[0] : null; ! msgKey = (secArgs.length > 1) ? secArgs[1] : null; ! } ! String out = ProcessTools.executeTestJvm(args[0].split("\\s+")) ! .getOutput(); // Handle response. ! if ((status != null && "PASS".equals(status) && msgKey != null ! && out.contains(MSG_MAP.get(msgKey)))) { ! System.out.printf("PASS: Expected Result: %s.%n", ! MSG_MAP.get(msgKey)); ! } else if ((status != null && "FAIL".equals(status) && msgKey != null ! && out.contains(MSG_MAP.get(msgKey)))) { ! System.out.printf("PASS: Expected Failure: %s.%n", ! MSG_MAP.get(msgKey)); } else if (out.contains("Exception") || out.contains("Error")) { System.out.printf("OUTPUT: %s", out); throw new RuntimeException("FAIL: Unknown Exception."); } else { System.out.printf("OUTPUT: %s", out); --- 71,250 ---- private static final Path CL_JAR = ARTIFACT_DIR.resolve("cl.jar"); private static final Path MCL_JAR = ARTIFACT_DIR.resolve("mcl.jar"); private static final Path C_JAR = ARTIFACT_DIR.resolve("c.jar"); private static final Path MC_JAR = ARTIFACT_DIR.resolve("mc.jar"); private static final Path AMC_JAR = ARTIFACT_DIR.resolve("amc.jar"); ! // Expected output messages ! private static final String MISSING_MODULE = ! "Module cl not found, required by mc"; ! private static final String POLICY_ERROR = ! "java.security.policy: error parsing file"; ! private static final String SYSTEM_CL_MSG = ! "jdk.internal.loader.ClassLoaders$AppClassLoader"; ! private static final String CUSTOM_CL_MSG = "cl.TestClassLoader"; ! ! // Member vars ! private final boolean useSCL; // Use default system loader, or custom ! private final String smMsg; // Security manager message, or "" ! private final String autoAddModArg; // Flag to add cl modules, or "" ! private final String addmodArg; // Flag to add mcl modules, or "" ! private final String expectedStatus;// Expected exit status from client ! private final String expectedMsg; // Expected output message from client ! ! // Common set of VM arguments used in all test cases ! private final List<String> COMMON_ARGS; ! ! public ClassLoaderTest(Path policy, boolean useSCL) { ! this.useSCL = useSCL; ! ! List<String> argList = new LinkedList<>(); ! argList.add("-Duser.language=en"); ! argList.add("-Duser.region=US"); ! ! boolean malformedPolicy = false; ! if (policy == null) { ! smMsg = "Without SecurityManager"; ! } else { ! malformedPolicy = policy.equals(INVALID_POLICY); ! argList.add("-Djava.security.manager"); ! argList.add("-Djava.security.policy=" + ! policy.toFile().getAbsolutePath()); ! smMsg = "With SecurityManager"; ! } ! ! if (useSCL) { ! autoAddModArg = ""; ! addmodArg = ""; ! } else { ! argList.add("-Djava.system.class.loader=cl.TestClassLoader"); ! autoAddModArg = "--add-modules=cl"; ! addmodArg = "--add-modules=mcl"; ! } ! ! if (malformedPolicy) { ! expectedStatus = "FAIL"; ! expectedMsg = POLICY_ERROR; ! } else if (useSCL) { ! expectedStatus = "PASS"; ! expectedMsg = SYSTEM_CL_MSG; ! } else { ! expectedStatus = "PASS"; ! expectedMsg = CUSTOM_CL_MSG; ! } ! ! COMMON_ARGS = Collections.unmodifiableList(argList); } public static void main(String[] args) throws Exception { + Path policy; + if (args[0].equals("-noPolicy")) { + policy = null; + } else if (args[0].equals("-validPolicy")) { + policy = VALID_POLICY; + } else if (args[0].equals("-invalidPolicy")) { + policy = INVALID_POLICY; + } else { + throw new RuntimeException("Unknown policy arg: " + args[0]); + } + + boolean useSystemLoader = true; + if (args.length > 1) { + if (args[1].equals("-customSCL")) { + useSystemLoader = false; + } else { + throw new RuntimeException("Unknown custom loader arg: " + args[1]); + } + } ! ClassLoaderTest test = new ClassLoaderTest(policy, useSystemLoader); setUp(); ! test.processForPolicyFile(); } /** * Test cases are based on the following logic, ! * given: a policyFile in {none, valid, malformed} and ! * a classLoader in {SystemClassLoader, CustomClassLoader}: ! * for (clientModule : {"NAMED", "UNNAMED"}) { ! * for (classLoaderModule : {"NAMED", "UNNAMED"}) { * Create and run java command for each possible Test case * } * } */ ! private void processForPolicyFile() throws Exception { ! final String regLoaderLoc = CL_JAR.toFile().getAbsolutePath(); ! final String modLoadrLoc = MCL_JAR.toFile().getAbsolutePath(); ! final String regClientLoc = C_JAR.toFile().getAbsolutePath(); ! final String modClientLoc = MC_JAR.toFile().getAbsolutePath(); final String autoModCloc = AMC_JAR.toFile().getAbsolutePath(); final String separator = File.pathSeparator; ! // NAMED-NAMED: ! System.out.println("Case:- Modular Client and " + ((useSCL) ? "SystemClassLoader" ! : "Modular CustomClassLoader") + " " + smMsg); ! execute("--module-path", modClientLoc + separator + modLoadrLoc, "-m", ! "mc/c.TestClient"); ! ! // NAMED-UNNAMED: ! System.out.println("Case:- Modular Client and " + ((useSCL) ? "SystemClassLoader" ! : "Unknown modular CustomClassLoader") + " " + smMsg); ! execute(new String[] {"--module-path", autoModCloc, "-cp", regLoaderLoc, ! "-m", "mc/c.TestClient"}, ! "FAIL", MISSING_MODULE); ! // UNNAMED-NAMED: ! System.out.println("Case:- Unknown modular Client and " + ((useSCL) ? "SystemClassLoader" ! : "Modular CustomClassLoader") + " " + smMsg); ! execute("-cp", regClientLoc, "--module-path", modLoadrLoc, addmodArg, ! "c.TestClient"); ! // UNNAMED-UNNAMED: ! System.out.println("Case:- Unknown modular Client and " + ((useSCL) ? "SystemClassLoader" ! : "Unknown modular CustomClassLoader") + " " + smMsg); ! execute("-cp", regClientLoc + separator + regLoaderLoc, "c.TestClient"); ! ! // Regular jars in module-path ! System.out.println("Case:- Regular Client and " + ((useSCL) ? "SystemClassLoader" ! : "Unknown modular CustomClassLoader") + ! " inside --module-path " + smMsg); ! execute("--module-path", regClientLoc + separator + regLoaderLoc, ! autoAddModArg, "-m", "c/c.TestClient"); ! ! // Modular jars in class-path ! System.out.println("Case:- Modular Client and " + ((useSCL) ? "SystemClassLoader" ! : "Modular CustomClassLoader") + " in -cp " + smMsg); ! execute("-cp", modClientLoc + separator + modLoadrLoc, "c.TestClient"); } + + private void execute(String... args) throws Exception { + execute(args, this.expectedStatus, this.expectedMsg); } /** * Execute with command arguments and process the result. */ ! private void execute(String[] args, String status, String msg) throws Exception { ! // Combine with COMMON_ARGS, and perform sanity check ! String[] safeArgs = Stream.concat(COMMON_ARGS.stream(), Stream.of(args)) ! .filter(s -> { ! if (s.contains(" ")) { throw new RuntimeException("No spaces in args");} ! return !s.equals(""); ! }).toArray(String[]::new); ! String out = ProcessTools.executeTestJvm(safeArgs).getOutput(); // Handle response. ! if ("PASS".equals(status) && out.contains(msg)) { ! System.out.println("PASS: Expected Result: " + msg); ! } else if ("FAIL".equals(status) && out.contains(msg)) { ! System.out.printf("PASS: Expected Failure: " + msg); } else if (out.contains("Exception") || out.contains("Error")) { System.out.printf("OUTPUT: %s", out); throw new RuntimeException("FAIL: Unknown Exception."); } else { System.out.printf("OUTPUT: %s", out);
*** 255,281 **** /** * Creates regular/modular jar files for TestClient and TestClassLoader. */ private static void setUp() throws Exception { - boolean compiled = CompilerUtils.compile(CL_SRC, CL_BIN); - compiled &= CompilerUtils.compile(C_SRC, C_BIN); - if (!compiled) { - throw new RuntimeException("Test Setup failed."); - } // Generate regular jar files for TestClient and TestClassLoader ! JarUtils.createJarFile(CL_JAR, CL_BIN); ! JarUtils.createJarFile(C_JAR, C_BIN); // Generate modular jar files for TestClient and TestClassLoader with // their corresponding ModuleDescriptor. ! Files.copy(CL_JAR, MCL_JAR, StandardCopyOption.REPLACE_EXISTING); updateModuleDescr(MCL_JAR, ModuleDescriptor.newModule("mcl") .exports("cl").requires("java.base").build()); ! Files.copy(C_JAR, MC_JAR, StandardCopyOption.REPLACE_EXISTING); updateModuleDescr(MC_JAR, ModuleDescriptor.newModule("mc") .exports("c").requires("java.base").requires("mcl").build()); ! Files.copy(C_JAR, AMC_JAR, StandardCopyOption.REPLACE_EXISTING); updateModuleDescr(AMC_JAR, ModuleDescriptor.newModule("mc") .exports("c").requires("java.base").requires("cl").build()); } /** --- 255,281 ---- /** * Creates regular/modular jar files for TestClient and TestClassLoader. */ private static void setUp() throws Exception { // Generate regular jar files for TestClient and TestClassLoader ! JarUtils.createJarFile(CL_JAR, TEST_CLASSES, ! "cl/TestClassLoader.class"); ! JarUtils.createJarFile(C_JAR, TEST_CLASSES, ! "c/TestClient.class"); // Generate modular jar files for TestClient and TestClassLoader with // their corresponding ModuleDescriptor. ! Files.copy(CL_JAR, MCL_JAR, ! StandardCopyOption.REPLACE_EXISTING); updateModuleDescr(MCL_JAR, ModuleDescriptor.newModule("mcl") .exports("cl").requires("java.base").build()); ! Files.copy(C_JAR, MC_JAR, ! StandardCopyOption.REPLACE_EXISTING); updateModuleDescr(MC_JAR, ModuleDescriptor.newModule("mc") .exports("c").requires("java.base").requires("mcl").build()); ! Files.copy(C_JAR, AMC_JAR, ! StandardCopyOption.REPLACE_EXISTING); updateModuleDescr(AMC_JAR, ModuleDescriptor.newModule("mc") .exports("c").requires("java.base").requires("cl").build()); } /**
< prev index next >