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