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