1 /*
   2  * Copyright (c) 2014, 2018, 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 /*
  26  * @test
  27  * @summary AppCDS handling of prohibited package.
  28  * @requires vm.cds
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  *          jdk.jartool/sun.tools.jar
  33  * @compile test-classes/ProhibitedHelper.java test-classes/Prohibited.jasm
  34  * @run main ProhibitedPackage
  35  */
  36 
  37 import jdk.test.lib.cds.CDSOptions;
  38 import jdk.test.lib.Platform;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 
  41 public class ProhibitedPackage {
  42 
  43     public static void main(String[] args) throws Exception {
  44         JarBuilder.build("prohibited_pkg", "java/lang/Prohibited", "ProhibitedHelper");
  45 
  46         String appJar = TestCommon.getTestJar("prohibited_pkg.jar");
  47 
  48         // Test support for customer loaders
  49         if (Platform.areCustomLoadersSupportedForCDS()) {
  50             String classlist[] = new String[] {
  51                 "java/lang/Object id: 1",
  52                 "java/lang/Prohibited id: 2 super: 1 source: " + appJar
  53             };
  54 
  55             // Make sure a class in a prohibited package for a custom loader
  56             // will be ignored during dumping.
  57             TestCommon.dump(appJar,  classlist, "-Xlog:cds")
  58                 .shouldContain("Dumping")
  59                 .shouldContain("[cds] Prohibited package for non-bootstrap classes: java/lang/Prohibited.class")
  60                 .shouldHaveExitValue(0);
  61         }
  62 
  63 
  64         // Make sure a class in a prohibited package for a non-custom loader
  65         // will be ignored during dumping.
  66         TestCommon.dump(appJar,
  67                         TestCommon.list("java/lang/Prohibited", "ProhibitedHelper"),
  68                         "-Xlog:class+load")
  69             .shouldContain("Dumping")
  70             .shouldNotContain("[info][class,load] java.lang.Prohibited source: ")
  71             .shouldHaveExitValue(0);
  72 
  73         // Try loading the class in a prohibited package with various -Xshare
  74         // modes. The class shouldn't be loaded and appropriate exceptions
  75         // are expected.
  76 
  77         OutputAnalyzer output;
  78 
  79         // -Xshare:on
  80         TestCommon.run(
  81             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
  82             "-cp", appJar, "-Xlog:class+load=info", "ProhibitedHelper")
  83           .assertNormalExit("Prohibited package name: java.lang");
  84 
  85         // -Xshare:auto
  86         output = TestCommon.execAuto(
  87             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
  88             "-cp", appJar, "-Xlog:class+load=info", "ProhibitedHelper");
  89         CDSOptions opts = (new CDSOptions()).setXShareMode("auto");
  90         TestCommon.checkExec(output, opts, "Prohibited package name: java.lang");
  91 
  92         // -Xshare:off
  93         output = TestCommon.execOff(
  94             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
  95             "-cp", appJar, "-Xlog:class+load=info", "ProhibitedHelper");
  96         output.shouldContain("Prohibited package name: java.lang");
  97     }
  98 }