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 Testing use of UseAppCDS flag
  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  * @build UseAppCDS_Test
  34  * @run main UseAppCDS
  35  */
  36 
  37 import jdk.test.lib.JDKToolLauncher;
  38 import jdk.test.lib.cds.CDSTestUtils;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 import jdk.test.lib.process.ProcessTools;
  41 
  42 import java.util.ArrayList;
  43 import java.util.List;
  44 import java.io.*;
  45 
  46 public class UseAppCDS {
  47 
  48     // Class UseAppCDS_Test is loaded by the App loader
  49 
  50     static final String TEST_OUT = "UseAppCDS_Test.main--executed";
  51 
  52     private static final String TESTJAR = "./test.jar";
  53     private static final String TESTNAME = "UseAppCDS_Test";
  54     private static final String TESTCLASS = TESTNAME + ".class";
  55 
  56     private static final String CLASSES_DIR = System.getProperty("test.classes", ".");
  57     private static final String CLASSLIST_FILE = "./UseAppCDS.classlist";
  58     private static final String ARCHIVE_FILE = "./shared.jsa";
  59     private static final String BOOTCLASS = "java.lang.Class";
  60 
  61     public static void main(String[] args) throws Exception {
  62 
  63         // First create a jar file for the application "test" class
  64         JDKToolLauncher jar = JDKToolLauncher.create("jar")
  65             .addToolArg("-cf")
  66             .addToolArg(TESTJAR)
  67             .addToolArg("-C")
  68             .addToolArg(CLASSES_DIR)
  69             .addToolArg(TESTCLASS);
  70 
  71         ProcessBuilder pb = new ProcessBuilder(jar.getCommand());
  72         TestCommon.executeAndLog(pb, "jar01").shouldHaveExitValue(0);
  73 
  74         pb = new ProcessBuilder(jar.getCommand());
  75         TestCommon.executeAndLog(pb, "jar02").shouldHaveExitValue(0);
  76 
  77         // In all tests the BOOTCLASS should be loaded/dumped/used
  78 
  79         // Test 1: No AppCDS - dumping loaded classes excludes the "test" classes
  80         dumpLoadedClasses(false, new String[] { BOOTCLASS },
  81                           new String[] { TESTNAME });
  82 
  83         // Test 2:    AppCDS - dumping loaded classes includes "test" classes
  84         dumpLoadedClasses(true, new String[] { BOOTCLASS, TESTNAME },
  85                           new String[0]);
  86 
  87         // Next tests rely on the classlist we just dumped
  88 
  89         // Test 3: No AppCDS - "test" classes in classlist ignored when dumping
  90         // Although AppCDS isn't used, all classes will be found during dumping
  91         // after the fix for JDK-8193434. Classes which are not in the boot
  92         // loader dictionary will not be saved into the archive.
  93         dumpArchive(false, new String[] { BOOTCLASS },
  94                     new String[0]);
  95 
  96         // Test 4:    AppCDS - "test" classes in classlist are dumped
  97         dumpArchive(true, new String[] { BOOTCLASS, TESTNAME },
  98                     new String[0]);
  99 
 100         // Next tests rely on the archive we just dumped
 101 
 102         // Test 5: No AppCDS - Using archive containing "test" classes ignores them
 103         useArchive(false, new String[] { BOOTCLASS },
 104                    new String[] { TESTNAME });
 105 
 106         // Test 6:    AppCDS - Using archive containing "test" classes loads them
 107         useArchive(true, new String[] { BOOTCLASS, TESTNAME },
 108                    new String[0]);
 109     }
 110 
 111     public static List<String> toClassNames(String filename) throws IOException {
 112         ArrayList<String> classes = new ArrayList<>();
 113         try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)))) {
 114             for (; ; ) {
 115                 String line = br.readLine();
 116                 if (line == null) {
 117                     break;
 118                 }
 119                 classes.add(line.replaceAll("/", "."));
 120             }
 121         }
 122         return classes;
 123     }
 124 
 125     static void dumpLoadedClasses(boolean useAppCDS, String[] expectedClasses,
 126                                   String[] unexpectedClasses) throws Exception {
 127         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
 128           TestCommon.makeCommandLineForAppCDS(
 129             "-XX:DumpLoadedClassList=" + CLASSLIST_FILE,
 130             "-cp",
 131             TESTJAR,
 132             useAppCDS ? "-XX:+UseAppCDS" : "-XX:-UseAppCDS",
 133             TESTNAME,
 134             TEST_OUT));
 135 
 136         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-loaded-classes")
 137             .shouldHaveExitValue(0).shouldContain(TEST_OUT);
 138 
 139         List<String> dumpedClasses = toClassNames(CLASSLIST_FILE);
 140 
 141         for (String clazz : expectedClasses) {
 142             if (!dumpedClasses.contains(clazz)) {
 143                 throw new RuntimeException(clazz + " missing in " +
 144                                            CLASSLIST_FILE);
 145             }
 146         }
 147         for (String clazz : unexpectedClasses) {
 148             if (dumpedClasses.contains(clazz)) {
 149                 throw new RuntimeException("Unexpectedly found " + clazz +
 150                                            " in " + CLASSLIST_FILE);
 151             }
 152         }
 153     }
 154 
 155     static void dumpArchive(boolean useAppCDS, String[] expectedClasses,
 156                             String[] unexpectedClasses) throws Exception {
 157         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
 158           TestCommon.makeCommandLineForAppCDS(
 159             useAppCDS ? "-XX:-UnlockDiagnosticVMOptions" :
 160                         "-XX:+UnlockDiagnosticVMOptions",
 161             "-cp",
 162             TESTJAR,
 163             useAppCDS ? "-XX:+UseAppCDS" : "-XX:-UseAppCDS",
 164             "-XX:SharedClassListFile=" + CLASSLIST_FILE,
 165             "-XX:SharedArchiveFile=" + ARCHIVE_FILE,
 166             "-Xlog:cds",
 167             "-Xshare:dump"));
 168 
 169         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-archive")
 170             .shouldHaveExitValue(0);
 171 
 172         for (String clazz : expectedClasses) {
 173             String failed = "Preload Warning: Cannot find " + clazz;
 174             output.shouldNotContain(failed);
 175         }
 176         for (String clazz : unexpectedClasses) {
 177             String failed = "Preload Warning: Cannot find " + clazz;
 178             output.shouldContain(failed);
 179         }
 180     }
 181 
 182     static void useArchive(boolean useAppCDS, String[] expectedClasses,
 183                            String[] unexpectedClasses) throws Exception {
 184         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
 185           TestCommon.makeCommandLineForAppCDS(
 186             useAppCDS ? "-XX:-UnlockDiagnosticVMOptions" :
 187                         "-XX:+UnlockDiagnosticVMOptions",
 188             "-cp",
 189             TESTJAR,
 190             useAppCDS ? "-XX:+UseAppCDS" : "-XX:-UseAppCDS",
 191             "-XX:SharedArchiveFile=" + ARCHIVE_FILE,
 192             "-verbose:class",
 193             "-Xshare:on",
 194             TESTNAME,
 195             TEST_OUT));
 196 
 197         OutputAnalyzer output = TestCommon.executeAndLog(pb, "use-archive");
 198         if (CDSTestUtils.isUnableToMap(output))
 199             System.out.println("Unable to map: test case skipped");
 200         else
 201             output.shouldHaveExitValue(0).shouldContain(TEST_OUT);
 202 
 203         // Quote the class name in the regex as it may contain $
 204         String prefix = ".class,load. ";
 205         String archive_suffix = ".*source: shared objects file.*";
 206         String jar_suffix = ".*source: .*\\.jar";
 207 
 208         for (String clazz : expectedClasses) {
 209             String pattern = prefix + clazz + archive_suffix;
 210             try {
 211                 output.shouldMatch(pattern);
 212             } catch (Exception e) {
 213                 TestCommon.checkCommonExecExceptions(output, e);
 214             }
 215         }
 216 
 217         for (String clazz : unexpectedClasses) {
 218             String pattern = prefix + clazz + archive_suffix;
 219             try {
 220                 output.shouldNotMatch(pattern);
 221             } catch (Exception e) {
 222                 TestCommon.checkCommonExecExceptions(output, e);
 223             }
 224             pattern = prefix + clazz + jar_suffix;
 225             try {
 226                 output.shouldMatch(pattern);
 227             } catch (Exception e) {
 228                 TestCommon.checkCommonExecExceptions(output, e);
 229             }
 230         }
 231     }
 232 }