1 /*
   2  * Copyright (c) 2020, 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  * @requires vm.cds
  27  * @summary Test class loader constraint checks for archived classes (dynamic archive)
  28  * @library /test/lib
  29  *          /test/hotspot/jtreg/runtime/cds/appcds
  30  *          /test/hotspot/jtreg/runtime/cds/appcds/test-classes
  31  *          /test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive
  32  * @modules java.base/jdk.internal.misc
  33  *          jdk.httpserver
  34  * @build sun.hotspot.WhiteBox
  35  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  36  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. DynamicLoaderConstraintsTest
  37  */
  38 
  39 import com.sun.net.httpserver.HttpExchange;
  40 import com.sun.net.httpserver.HttpHandler;
  41 import jdk.test.lib.Asserts;
  42 
  43 public class DynamicLoaderConstraintsTest extends DynamicArchiveTestBase {
  44     static String mainClass = LoaderConstraintsApp.class.getName();
  45     static String appJar = null;
  46     static String appClasses[] = {
  47         mainClass,
  48         HttpHandler.class.getName(),
  49         HttpExchange.class.getName(),
  50         Asserts.class.getName(),
  51         MyHttpHandler.class.getName(),
  52         MyHttpHandlerB.class.getName(),
  53         MyHttpHandlerC.class.getName(),
  54         MyClassLoader.class.getName()
  55     };
  56 
  57     public static void main(String[] args) throws Exception {
  58         runTest(DynamicLoaderConstraintsTest::doTest);
  59     }
  60 
  61     static void doTest() throws Exception  {
  62         appJar = ClassFileInstaller.writeJar("loader_constraints.jar", appClasses);
  63         doTest(false);
  64         doTest(true);
  65     }
  66 
  67     /*
  68      * errorInDump:
  69      * true:  Even when dumping the archive, execute the code that would cause
  70      *        LinkageError, to see how the VM can handle such error during
  71      *        dump time.
  72      * false: At dump time, simply load all the necessary test classes without
  73      *        causing LinkageError. This ensures the test classes will be
  74      *        archived so we can test CDS's handling of loader constraints during
  75      *        run time.
  76      */
  77     static void doTest(boolean errorInDump) throws Exception  {
  78         for (int i = 1; i <= 3; i++) {
  79             String topArchiveName = getNewArchiveName();
  80             String testCase = Integer.toString(i);
  81             String cmdLine[] = new String[] {
  82                 "-cp", appJar,
  83                 "--add-modules",
  84                 "java.base,jdk.httpserver",
  85                 "--add-exports",
  86                 "java.base/jdk.internal.misc=ALL-UNNAMED",
  87                 "-Xlog:class+load,class+loader+constraints",
  88                 mainClass, testCase
  89             };
  90 
  91             String[] dumpCmdLine = cmdLine;
  92             if (!errorInDump) {
  93                 dumpCmdLine = TestCommon.concat(dumpCmdLine, "loadClassOnly");
  94             }
  95 
  96             dump(topArchiveName, dumpCmdLine).assertNormalExit();
  97             run(topArchiveName, cmdLine).assertNormalExit();
  98         }
  99     }
 100 }