1 /*
  2  * Copyright (c) 2024, 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  * @requires vm.cds.write.archived.java.heap
 28  * @library /test/jdk/lib/testlibrary /test/lib
 29  *          /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 30  * @build ArchivedProtectionDomains
 31  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app1.jar ArchivedProtectionDomainsApp
 32  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app2.jar pkg3.Package3A pkg3.Package3B
 33  * @run driver ArchivedProtectionDomains STATIC
 34  * @run driver ArchivedProtectionDomains STATIC allowSecurityManager
 35  */
 36 
 37 import java.io.File;
 38 import java.security.ProtectionDomain;
 39 import jdk.test.lib.cds.CDSAppTester;
 40 import jdk.test.lib.helpers.ClassFileInstaller;
 41 import jdk.test.lib.process.OutputAnalyzer;
 42 import jdk.test.lib.StringArrayUtils;
 43 
 44 import pkg3.Package3A;
 45 import pkg3.Package3B;
 46 
 47 public class ArchivedProtectionDomains {
 48     static final String app1Jar = ClassFileInstaller.getJarPath("app1.jar");
 49     static final String app2Jar = ClassFileInstaller.getJarPath("app2.jar");
 50     static final String mainClass = "ArchivedProtectionDomainsApp";
 51     static boolean allowSecurityManager = false;
 52 
 53     public static void main(String[] args) throws Exception {
 54         Tester t = new Tester();
 55 
 56         if (args.length > 1 && args[1].equals("allowSecurityManager")) {
 57             allowSecurityManager = true;
 58         }
 59         t.run(new String[] {args[0]});
 60     }
 61 
 62     static class Tester extends CDSAppTester {
 63         public Tester() {
 64             super(mainClass);
 65         }
 66 
 67         @Override
 68         public String classpath(RunMode runMode) {
 69             return app1Jar + File.pathSeparator + app2Jar;
 70         }
 71 
 72         @Override
 73         public String[] vmArgs(RunMode runMode) {
 74             String[] args = StringArrayUtils.concat(
 75                 "-Dcds.debug.archived.protection.domains=true",
 76                 "-XX:+AOTClassLinking",
 77                 "-XX:+ArchiveProtectionDomains",
 78                 "-Xlog:cds+protectiondomain");
 79             if (allowSecurityManager) {
 80                 args = StringArrayUtils.concat(args, "-Djava.security.manager=allow");
 81             }
 82             return args;
 83         }
 84 
 85         @Override
 86         public String[] appCommandLine(RunMode runMode) {
 87             return new String[] {
 88                 mainClass,
 89                 runMode.name()
 90             };
 91         }
 92 
 93         @Override
 94         public void checkExecution(OutputAnalyzer out, RunMode runMode) {
 95             if (allowSecurityManager) {
 96                 out.shouldNotContain("Archiving ProtectionDomain ");
 97                 out.shouldNotMatch("Archived protection domain for .* = found");
 98             } else {
 99                 assertArchived(out, runMode);
100             }
101         }
102 
103         void assertArchived(OutputAnalyzer out, RunMode runMode) {
104             if (runMode.isStaticDump()) {
105                 out.shouldMatch("Archiving ProtectionDomain .jrt:/jdk.compiler .* for jdk.internal.loader.ClassLoaders.AppClassLoader");
106                 out.shouldMatch("Archiving ProtectionDomain .*app1.jar .* for jdk.internal.loader.ClassLoaders.AppClassLoader");
107                 out.shouldMatch("Archiving ProtectionDomain .*app2.jar .* for jdk.internal.loader.ClassLoaders.AppClassLoader");
108             } else if (runMode.isProductionRun()) {
109                 out.shouldContain("Archived protection domain for ArchivedProtectionDomainsApp = found");
110                 out.shouldContain("Archived protection domain for pkg3.Package3A = found");
111                 out.shouldContain("Archived protection domain for com.sun.tools.javac.Main = found");
112                 out.shouldContain(ArchivedProtectionDomainsApp.msg);
113 
114                 // This class should not be archived.
115                 out.shouldNotContain("Archived protection domain for pkg3.Package3B = found");
116                 out.shouldNotContain("Archived protection domain for pkg3.Package3B = none");
117             }
118         }
119     }
120 }
121 
122 class ArchivedProtectionDomainsApp {
123     public static String msg = "ProtectionDomain for archived class equals to that of non-archived class";
124     public static void main(String args[]) throws Exception {
125         Class jc = Class.forName("com.sun.tools.javac.Main");
126         ProtectionDomain x = Package3A.class.getProtectionDomain();
127         if (args[0].equals("PRODUCTION")) {
128             ProtectionDomain y = Package3B.class.getProtectionDomain();
129             System.out.println("ProtectionDomain for com.sun.tools.javac.Main: " + jc.getProtectionDomain());
130             System.out.println("ProtectionDomain for archived class: " + x);
131             System.out.println("ProtectionDomain for non-archived class: " + y);
132             if (x == y) {
133                 System.out.println(msg);
134             } else {
135                 throw new RuntimeException("Unexpected for archived package");
136             }
137         }
138     }
139 }