1 /*
   2  * Copyright (c) 2014, 2017, 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  * AppCDS does not support uncompressed oops
  29  * @requires (vm.opt.UseCompressedOops == null) | (vm.opt.UseCompressedOops == true)
  30  * @library /test/lib
  31  * @modules java.base/jdk.internal.misc
  32  *          java.management
  33  *          jdk.jartool/sun.tools.jar
  34  * @compile test-classes/ProhibitedHelper.java test-classes/Prohibited.jasm
  35  * @run main ProhibitedPackage
  36  */
  37 
  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         // AppCDS for custom loader is only supported on linux-x64 and
  49         // Solaris 64-bit platforms.
  50         if ((Platform.isLinux() || Platform.isSolaris()) &&
  51             Platform.is64bit()) {
  52             String classlist[] = new String[] {
  53                 "java/lang/Object id: 1",
  54                 "java/lang/Prohibited id: 2 super: 1 source: " + appJar
  55             };
  56 
  57             // Make sure a class in a prohibited package for a custom loader
  58             // will be ignored during dumping.
  59             TestCommon.dump(appJar,
  60                             classlist,
  61                             "-XX:+PrintSystemDictionaryAtExit")
  62                 .shouldContain("Dumping")
  63                 .shouldNotContain("java.lang.Prohibited")
  64                 .shouldHaveExitValue(0);
  65         }
  66 
  67 
  68         // Make sure a class in a prohibited package for a non-custom loader
  69         // will be ignored during dumping.
  70         TestCommon.dump(appJar,
  71                         TestCommon.list("java/lang/Prohibited", "ProhibitedHelper"),
  72                         "-XX:+PrintSystemDictionaryAtExit")
  73             .shouldContain("Dumping")
  74             .shouldNotContain("java.lang.Prohibited")
  75             .shouldHaveExitValue(0);
  76 
  77         // Try loading the class in a prohibited package with various -Xshare
  78         // modes. The class shouldn't be loaded and appropriate exceptions
  79         // are expected.
  80 
  81         OutputAnalyzer output;
  82 
  83         // -Xshare:on
  84         output = TestCommon.execCommon(
  85             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
  86             "-cp", appJar, "-Xlog:class+load=info", "ProhibitedHelper");
  87         TestCommon.checkExec(output, "Prohibited package name: java.lang");
  88 
  89         // -Xshare:auto
  90         output = TestCommon.execAuto(
  91             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
  92             "-cp", appJar, "-Xlog:class+load=info", "ProhibitedHelper");
  93         TestCommon.checkExec(output, "Prohibited package name: java.lang");
  94 
  95         // -Xshare:off
  96         output = TestCommon.execOff(
  97             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
  98             "-cp", appJar, "-Xlog:class+load=info", "ProhibitedHelper");
  99         output.shouldContain("Prohibited package name: java.lang");
 100     }
 101 }