1 /*
  2  * Copyright (c) 2020, 2021, 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 id=default-cl
 26  * @requires vm.cds
 27  * @summary Test class loader constraint checks for archived classes
 28  * @library /test/lib
 29  *          /test/hotspot/jtreg/runtime/cds/appcds
 30  *          /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 31  * @modules java.base/jdk.internal.misc
 32  *          jdk.httpserver
 33  * @run driver LoaderConstraintsTest
 34  */
 35 
 36 /**
 37  * @test id=custom-cl
 38  * @requires vm.cds.custom.loaders
 39  * @summary Test class loader constraint checks for archived classes with custom class loader
 40  * @bug 8267347
 41  * @library /test/lib
 42  *          /test/hotspot/jtreg/runtime/cds/appcds
 43  *          /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 44  * @modules java.base/jdk.internal.misc
 45  *          jdk.httpserver
 46  * @run driver LoaderConstraintsTest custom
 47  */
 48 
 49 
 50 import com.sun.net.httpserver.HttpExchange;
 51 import com.sun.net.httpserver.HttpHandler;
 52 import jdk.test.lib.Asserts;
 53 import jdk.test.lib.helpers.ClassFileInstaller;
 54 
 55 public class LoaderConstraintsTest  {
 56     static String mainClass = LoaderConstraintsApp.class.getName();
 57     static String httpHandlerClass = HttpHandler.class.getName().replace(".", "/");
 58     static String httpExchangeClass = HttpExchange.class.getName().replace(".", "/");
 59     static String appJar = null;
 60     static String appClasses[] = {
 61         mainClass,
 62         httpHandlerClass,
 63         httpExchangeClass,
 64         Asserts.class.getName(),
 65         MyHttpHandler.class.getName(),
 66         MyHttpHandlerB.class.getName(),
 67         MyHttpHandlerC.class.getName(),
 68         MyClassLoader.class.getName()
 69     };
 70 
 71     static String loaderMainClass = CustomAppLoader.class.getName();
 72     static String loaderJar = null;
 73     static String loaderClasses[] = {
 74         loaderMainClass
 75     };
 76 
 77     static void doTest() throws Exception  {
 78         TestCommon.dump(appJar, appClasses, "-Xlog:cds");
 79         String cmdLine[] =
 80             TestCommon.concat("-cp", appJar,
 81                               "-Xlog:cds",
 82                               "-Xlog:class+loader+constraints=debug",
 83                               "--add-exports",
 84                               "java.base/jdk.internal.misc=ALL-UNNAMED",
 85                               mainClass);
 86         runWithArchive(cmdLine, "1");
 87         runWithArchive(cmdLine, "2");
 88         runWithArchive(cmdLine, "3");
 89     }
 90 
 91     // Same as doTest, except that LoaderConstraintsApp and MyHttpHandler* are loaded
 92     // by a custom loader. This is test case for JDK-8267347.
 93     static void doTestCustomLoader() throws Exception  {
 94         String src = " source: " + appJar;
 95         String classList[] =
 96             TestCommon.concat(loaderClasses,
 97                               "java/lang/Object id: 1",
 98                               mainClass + " id: 2 super: 1" + src,
 99                               httpHandlerClass + " id: 3",
100                               "MyHttpHandler id: 5 super: 1 interfaces: 3" + src,
101                               "MyHttpHandlerB id: 6 super: 1 interfaces: 3" + src,
102                               "MyHttpHandlerC id: 7 super: 1 interfaces: 3" + src);
103         TestCommon.dump(loaderJar, classList, "-Xlog:cds");
104 
105         String cmdLine[] =
106             TestCommon.concat("-cp", loaderJar,
107                               "-Xlog:cds",
108                               "-Xlog:class+loader+constraints=debug",
109                               "--add-exports",
110                               "java.base/jdk.internal.misc=ALL-UNNAMED",
111                               loaderMainClass, appJar, mainClass);
112         runWithArchive(cmdLine, "1");
113         runWithArchive(cmdLine, "2");
114         runWithArchive(cmdLine, "3");
115     }
116 
117     static void runWithArchive(String[] optsMain, String arg) throws Exception {
118         String cmd[] = TestCommon.concat(optsMain, arg);
119         TestCommon.run(cmd).assertNormalExit();
120     }
121 
122     public static void main(String... args) throws Exception {
123         appJar = ClassFileInstaller.writeJar("loader_constraints.jar", appClasses);
124         if (args.length == 0) {
125             doTest();
126         } else {
127             loaderJar = ClassFileInstaller.writeJar("custom_app_loader.jar", loaderClasses);
128             doTestCustomLoader();
129         }
130     }
131 }
132