1 /*
  2  * Copyright (c) 2017, 2023, 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  * @enablePreview
 30  * @modules java.base/jdk.internal.module
 31  * @library /test/lib
 32  * @build jdk.test.lib.util.JarUtils
 33  * @build TestClassLoader TestClient
 34  * @run main ClassLoaderTest -noPolicy
 35  * @run main ClassLoaderTest -validPolicy
 36  * @run main ClassLoaderTest -invalidPolicy
 37  * @run main ClassLoaderTest -noPolicy      -customSCL
 38  * @run main ClassLoaderTest -validPolicy   -customSCL
 39  * @run main ClassLoaderTest -invalidPolicy -customSCL
 40  */
 41 import java.io.File;
 42 import java.io.OutputStream;
 43 import java.nio.file.Files;
 44 import java.nio.file.Path;
 45 import java.nio.file.Paths;
 46 import java.nio.file.StandardCopyOption;
 47 import java.util.stream.Stream;
 48 import java.lang.module.ModuleDescriptor;
 49 import java.util.Collections;
 50 import java.util.LinkedList;
 51 import java.util.List;
 52 import jdk.test.lib.process.ProcessTools;
 53 import jdk.test.lib.util.JarUtils;
 54 import jdk.test.lib.util.ModuleInfoWriter;
 55 
 56 public class ClassLoaderTest {
 57 
 58     private static final String SRC = System.getProperty("test.src");
 59     private static final Path TEST_CLASSES =
 60             Paths.get(System.getProperty("test.classes"));
 61     private static final Path ARTIFACT_DIR = Paths.get("jars");
 62     private static final Path VALID_POLICY = Paths.get(SRC, "valid.policy");
 63     private static final Path INVALID_POLICY
 64             = Paths.get(SRC, "malformed.policy");
 65     /*
 66      * Here is the naming convention followed for each jar.
 67      * cl.jar   - Regular custom class loader jar.
 68      * mcl.jar  - Modular custom class loader jar.
 69      * c.jar    - Regular client jar.
 70      * mc.jar   - Modular client jar.
 71      * amc.jar  - Modular client referring automated custom class loader jar.
 72      */
 73     private static final Path CL_JAR = ARTIFACT_DIR.resolve("cl.jar");
 74     private static final Path MCL_JAR = ARTIFACT_DIR.resolve("mcl.jar");
 75     private static final Path C_JAR = ARTIFACT_DIR.resolve("c.jar");
 76     private static final Path MC_JAR = ARTIFACT_DIR.resolve("mc.jar");
 77     private static final Path AMC_JAR = ARTIFACT_DIR.resolve("amc.jar");
 78 
 79     // Expected output messages
 80     private static final String MISSING_MODULE =
 81             "Module cl not found, required by mc";
 82     private static final String POLICY_ERROR =
 83             "java.security.policy: error parsing file";
 84     private static final String SYSTEM_CL_MSG =
 85             "jdk.internal.loader.ClassLoaders$AppClassLoader";
 86     private static final String CUSTOM_CL_MSG = "cl.TestClassLoader";
 87 
 88     // Member vars
 89     private final boolean useSCL;       // Use default system loader, or custom
 90     private final String smMsg;         // Security manager message, or ""
 91     private final String autoAddModArg; // Flag to add cl modules, or ""
 92     private final String addmodArg;     // Flag to add mcl modules, or ""
 93     private final String expectedStatus;// Expected exit status from client
 94     private final String expectedMsg;   // Expected output message from client
 95 
 96     // Common set of VM arguments used in all test cases
 97     private final List<String> commonArgs;
 98 
 99     public ClassLoaderTest(Path policy, boolean useSCL) {
100         this.useSCL = useSCL;
101 
102         List<String> argList = new LinkedList<>();
103         argList.add("-Duser.language=en");
104         argList.add("-Duser.region=US");
105 
106         boolean malformedPolicy = false;
107         if (policy == null) {
108             smMsg = "Without SecurityManager";
109         } else {
110             malformedPolicy = policy.equals(INVALID_POLICY);
111             argList.add("-Djava.security.manager");
112             argList.add("-Djava.security.policy=" +
113                     policy.toFile().getAbsolutePath());
114             smMsg = "With SecurityManager";
115         }
116 
117         if (useSCL) {
118             autoAddModArg = "";
119             addmodArg = "";
120         } else {
121             argList.add("-Djava.system.class.loader=cl.TestClassLoader");
122             autoAddModArg = "--add-modules=cl";
123             addmodArg = "--add-modules=mcl";
124         }
125 
126         if (malformedPolicy) {
127             expectedStatus = "FAIL";
128             expectedMsg = POLICY_ERROR;
129         } else if (useSCL) {
130             expectedStatus = "PASS";
131             expectedMsg = SYSTEM_CL_MSG;
132         } else {
133             expectedStatus = "PASS";
134             expectedMsg = CUSTOM_CL_MSG;
135         }
136         commonArgs = Collections.unmodifiableList(argList);
137     }
138 
139     public static void main(String[] args) throws Exception {
140         Path policy;
141         if (args[0].equals("-noPolicy")) {
142             policy = null;
143         } else if (args[0].equals("-validPolicy")) {
144             policy = VALID_POLICY;
145         } else if (args[0].equals("-invalidPolicy")) {
146             policy = INVALID_POLICY;
147         } else {
148             throw new RuntimeException("Unknown policy arg: " + args[0]);
149         }
150 
151         boolean useSystemLoader = true;
152         if (args.length > 1) {
153             if (args[1].equals("-customSCL")) {
154                 useSystemLoader = false;
155             } else {
156                 throw new RuntimeException("Unknown custom loader arg: " + args[1]);
157             }
158         }
159 
160         ClassLoaderTest test = new ClassLoaderTest(policy, useSystemLoader);
161         setUp();
162         test.processForPolicyFile();
163     }
164 
165     /**
166      * Test cases are based on the following logic,
167      *  given: a policyFile in {none, valid, malformed} and
168      *         a classLoader in {SystemClassLoader, CustomClassLoader}:
169      *  for (clientModule : {"NAMED", "UNNAMED"}) {
170      *      for (classLoaderModule : {"NAMED", "UNNAMED"}) {
171      *          Create and run java command for each possible Test case
172      *      }
173      *  }
174      */
175     private void processForPolicyFile() throws Exception {
176         final String regLoaderLoc = CL_JAR.toFile().getAbsolutePath();
177         final String modLoadrLoc = MCL_JAR.toFile().getAbsolutePath();
178         final String regClientLoc = C_JAR.toFile().getAbsolutePath();
179         final String modClientLoc = MC_JAR.toFile().getAbsolutePath();
180         final String autoModCloc = AMC_JAR.toFile().getAbsolutePath();
181         final String separator = File.pathSeparator;
182 
183         // NAMED-NAMED:
184         System.out.println("Case:- Modular Client and " +
185                 ((useSCL) ? "SystemClassLoader"
186                         : "Modular CustomClassLoader") + " " + smMsg);
187         execute("--module-path", modClientLoc + separator + modLoadrLoc, "-m",
188                 "mc/c.TestClient");
189 
190         // NAMED-UNNAMED:
191         System.out.println("Case:- Modular Client and " + ((useSCL)
192                 ? "SystemClassLoader"
193                 : "Unknown modular CustomClassLoader") + " " + smMsg);
194         execute(new String[] {"--module-path", autoModCloc, "-cp", regLoaderLoc,
195                 "-m", "mc/c.TestClient"},
196                 "FAIL", MISSING_MODULE);
197 
198         // UNNAMED-NAMED:
199         System.out.println("Case:- Unknown modular Client and " +
200                 ((useSCL) ? "SystemClassLoader"
201                       : "Modular CustomClassLoader") + " " + smMsg);
202         execute("-cp", regClientLoc, "--module-path", modLoadrLoc, addmodArg,
203                 "c.TestClient");
204 
205         // UNNAMED-UNNAMED:
206         System.out.println("Case:- Unknown modular Client and " +
207                 ((useSCL) ? "SystemClassLoader"
208                         : "Unknown modular CustomClassLoader") + " " + smMsg);
209         execute("-cp", regClientLoc + separator + regLoaderLoc, "c.TestClient");
210 
211         // Regular jars in module-path
212         System.out.println("Case:- Regular Client and " + ((useSCL)
213                 ? "SystemClassLoader"
214                 : "Unknown modular CustomClassLoader") +
215                 " inside --module-path " + smMsg);
216         execute("--module-path", regClientLoc + separator + regLoaderLoc,
217                 autoAddModArg, "-m", "c/c.TestClient");
218 
219         // Modular jars in class-path
220         System.out.println("Case:- Modular Client and " +
221                 ((useSCL) ? "SystemClassLoader"
222                         : "Modular CustomClassLoader") + " in -cp " + smMsg);
223         execute("-cp", modClientLoc + separator + modLoadrLoc, "c.TestClient");
224     }
225 
226     private void execute(String... args) throws Exception {
227         execute(args, this.expectedStatus, this.expectedMsg);
228     }
229 
230     /**
231      * Execute with command arguments and process the result.
232      */
233     private void execute(String[] args, String status, String msg) throws Exception {
234 
235         // Combine with commonArgs, and perform sanity check
236         String[] safeArgs = Stream.concat(commonArgs.stream(), Stream.of(args))
237                 .filter(s -> {
238                     if (s.contains(" ")) { throw new RuntimeException("No spaces in args");}
239                     return !s.isEmpty();
240                 }).toArray(String[]::new);
241         String out = ProcessTools.executeTestJava(safeArgs).getOutput();
242         // Handle response.
243         if ("PASS".equals(status) && out.contains(msg)) {
244             System.out.println("PASS: Expected Result: " + msg);
245         } else if ("FAIL".equals(status) && out.contains(msg)) {
246             System.out.printf("PASS: Expected Failure: " +  msg);
247         } else if (out.contains("Exception") || out.contains("Error")) {
248             System.out.printf("OUTPUT: %s", out);
249             throw new RuntimeException("FAIL: Unknown Exception.");
250         } else {
251             System.out.printf("OUTPUT: %s", out);
252             throw new RuntimeException("FAIL: Unknown Test case found");
253         }
254     }
255 
256     /**
257      * Creates regular/modular jar files for TestClient and TestClassLoader.
258      */
259     private static void setUp() throws Exception {
260 
261         // Generate regular jar files for TestClient and TestClassLoader
262         JarUtils.createJarFile(CL_JAR, TEST_CLASSES,
263                                "cl/TestClassLoader.class");
264         JarUtils.createJarFile(C_JAR, TEST_CLASSES,
265                                "c/TestClient.class");
266         // Generate modular jar files for TestClient and TestClassLoader with
267         // their corresponding ModuleDescriptor.
268         Files.copy(CL_JAR, MCL_JAR,
269                 StandardCopyOption.REPLACE_EXISTING);
270         updateModuleDescr(MCL_JAR, ModuleDescriptor.newModule("mcl")
271                 .exports("cl").requires("java.base").build());
272         Files.copy(C_JAR, MC_JAR,
273                 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,
277                 StandardCopyOption.REPLACE_EXISTING);
278         updateModuleDescr(AMC_JAR, ModuleDescriptor.newModule("mc")
279                 .exports("c").requires("java.base").requires("cl").build());
280     }
281 
282     /**
283      * Update regular jars and include module-info.class inside it to make
284      * modular jars.
285      */
286     private static void updateModuleDescr(Path jar, ModuleDescriptor mDescr)
287             throws Exception {
288         if (mDescr != null) {
289             Path dir = Files.createTempDirectory("tmp");
290             Path mi = dir.resolve("module-info.class");
291             try (OutputStream out = Files.newOutputStream(mi)) {
292                 ModuleInfoWriter.write(mDescr, out);
293             }
294             System.out.format("Adding 'module-info.class' to jar '%s'%n", jar);
295             JarUtils.updateJarFile(jar, dir);
296         }
297     }
298 }