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 import jdk.test.lib.Platform;
 55 
 56 public class LoaderConstraintsTest  {
 57     static String mainClass = LoaderConstraintsApp.class.getName();
 58     static String httpHandlerClass = HttpHandler.class.getName().replace(".", "/");
 59     static String httpExchangeClass = HttpExchange.class.getName().replace(".", "/");
 60     static String appJar = null;
 61     static String appClasses[] = {
 62         mainClass,
 63         httpHandlerClass,
 64         httpExchangeClass,
 65         Asserts.class.getName(),
 66         MyHttpHandler.class.getName(),
 67         MyHttpHandlerB.class.getName(),
 68         MyHttpHandlerC.class.getName(),
 69         MyClassLoader.class.getName()
 70     };
 71 
 72     static String loaderMainClass = CustomAppLoader.class.getName();
 73     static String loaderJar = null;
 74     static String loaderClasses[] = {
 75         loaderMainClass
 76     };
 77 
 78     static void doTest() throws Exception  {
 79         TestCommon.dump(appJar, appClasses, "-Xlog:cds");
 80         String cmdLine[] =
 81             TestCommon.concat("-cp", appJar,
 82                               "-Xlog:cds",
 83                               "-Xlog:class+loader+constraints=debug",
 84                               "--add-exports",
 85                               "java.base/jdk.internal.misc=ALL-UNNAMED",
 86                               mainClass);
 87         runWithArchive(cmdLine, "1");
 88         runWithArchive(cmdLine, "2");
 89         runWithArchive(cmdLine, "3");
 90     }
 91 
 92     // Same as doTest, except that LoaderConstraintsApp and MyHttpHandler* are loaded
 93     // by a custom loader. This is test case for JDK-8267347.
 94     static void doTestCustomLoader() throws Exception  {
 95         String src = " source: " + appJar;
 96         String classList[] =
 97             TestCommon.concat(loaderClasses,
 98                               "java/lang/Object id: 1",
 99                               mainClass + " id: 2 super: 1" + src,
100                               httpHandlerClass + " id: 3",
101                               "MyHttpHandler id: 5 super: 1 interfaces: 3" + src,
102                               "MyHttpHandlerB id: 6 super: 1 interfaces: 3" + src,
103                               "MyHttpHandlerC id: 7 super: 1 interfaces: 3" + src);
104         TestCommon.dump(loaderJar, classList, "-Xlog:cds");
105 
106         String cmdLine[] =
107             TestCommon.concat("-cp", loaderJar,
108                               "-Xlog:cds",
109                               "-Xlog:class+loader+constraints=debug",
110                               "--add-exports",
111                               "java.base/jdk.internal.misc=ALL-UNNAMED",
112                               loaderMainClass, appJar, mainClass);
113         runWithArchive(cmdLine, "1");
114         runWithArchive(cmdLine, "2");
115         runWithArchive(cmdLine, "3");
116     }
117 
118     static void runWithArchive(String[] optsMain, String arg) throws Exception {
119         String cmd[] = TestCommon.concat(optsMain, arg);
120         TestCommon.run(cmd).assertNormalExit();
121     }
122 
123     public static void main(String... args) throws Exception {
124         appJar = ClassFileInstaller.writeJar("loader_constraints.jar", appClasses);
125         if (args.length == 0) {
126             doTest();
127         } else {
128             loaderJar = ClassFileInstaller.writeJar("custom_app_loader.jar", loaderClasses);
129             doTestCustomLoader();
130         }
131     }
132 }
133