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 and TESTNAME should be loaded/dumped/used
  78 
  79         // Test 1: -XX:-UseAppCDS - No effect, dumping loaded classes includes "test" classes
  80         dumpLoadedClasses(false, new String[] { BOOTCLASS, TESTNAME },
  81                           new String[0]);
  82 
  83         // Test 2: -XX:+UseAppCDS - 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: -XX:-UseAppCDS - No effect, "test" classes in classlist are dumped 
  90         dumpArchive(false, new String[] { BOOTCLASS, TESTNAME },
  91                     new String[0]);
  92 
  93         // Test 4: -XX:+UseAppCDS - "test" classes in classlist are dumped
  94         dumpArchive(true, new String[] { BOOTCLASS, TESTNAME },
  95                     new String[0]);
  96 
  97         // Next tests rely on the archive we just dumped
  98 
  99         // Test 5: -XX:-UseAppCDS - No effect, using archive containing "test" loads them
 100         useArchive(false, new String[] { BOOTCLASS, TESTNAME },
 101                    new String[0]);
 102 
 103         // Test 6: -XX:+UseAppCDS - Using archive containing "test" classes loads them
 104         useArchive(true, new String[] { BOOTCLASS, TESTNAME },
 105                    new String[0]);
 106     }
 107 
 108     public static List<String> toClassNames(String filename) throws IOException {
 109         ArrayList<String> classes = new ArrayList<>();
 110         try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)))) {
 111             for (; ; ) {
 112                 String line = br.readLine();
 113                 if (line == null) {
 114                     break;
 115                 }
 116                 classes.add(line.replaceAll("/", "."));
 117             }
 118         }
 119         return classes;
 120     }
 121 
 122     static void dumpLoadedClasses(boolean useAppCDS, String[] expectedClasses,
 123                                   String[] unexpectedClasses) throws Exception {
 124         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
 125           TestCommon.makeCommandLineForAppCDS(
 126             "-XX:DumpLoadedClassList=" + CLASSLIST_FILE,
 127             "-cp",
 128             TESTJAR,
 129             (useAppCDS) ? "-XX:+UseAppCDS" : "-XX:-UseAppCDS",
 130             TESTNAME,
 131             TEST_OUT));
 132 
 133         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-loaded-classes")
 134             .shouldHaveExitValue(0).shouldContain(TEST_OUT);
 135 
 136         List<String> dumpedClasses = toClassNames(CLASSLIST_FILE);
 137 
 138         for (String clazz : expectedClasses) {
 139             if (!dumpedClasses.contains(clazz)) {
 140                 throw new RuntimeException(clazz + " missing in " +
 141                                            CLASSLIST_FILE);
 142             }
 143         }
 144         for (String clazz : unexpectedClasses) {
 145             if (dumpedClasses.contains(clazz)) {
 146                 throw new RuntimeException("Unexpectedly found " + clazz +
 147                                            " in " + CLASSLIST_FILE);
 148             }
 149         }
 150     }
 151 
 152     static void dumpArchive(boolean useAppCDS, String[] expectedClasses,
 153                             String[] unexpectedClasses) throws Exception {
 154         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
 155           TestCommon.makeCommandLineForAppCDS(
 156             "-cp",
 157             TESTJAR,
 158             useAppCDS ? "-XX:+UseAppCDS" : "-XX:-UseAppCDS",
 159             "-XX:SharedClassListFile=" + CLASSLIST_FILE,
 160             "-XX:SharedArchiveFile=" + ARCHIVE_FILE,
 161             "-Xlog:cds",
 162             "-Xshare:dump"));
 163 
 164         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-archive")
 165             .shouldHaveExitValue(0);
 166 
 167         for (String clazz : expectedClasses) {
 168             String failed = "Preload Warning: Cannot find " + clazz;
 169             output.shouldNotContain(failed);
 170         }
 171         for (String clazz : unexpectedClasses) {
 172             String failed = "Preload Warning: Cannot find " + clazz;
 173             output.shouldContain(failed);
 174         }
 175     }
 176 
 177     static void useArchive(boolean useAppCDS, String[] expectedClasses,
 178                            String[] unexpectedClasses) throws Exception {
 179         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
 180           TestCommon.makeCommandLineForAppCDS(
 181             "-cp",
 182             TESTJAR,
 183             useAppCDS ? "-XX:+UseAppCDS" : "-XX:-UseAppCDS",
 184             "-XX:SharedArchiveFile=" + ARCHIVE_FILE,
 185             "-verbose:class",
 186             "-Xshare:on",
 187             TESTNAME,
 188             TEST_OUT));
 189 
 190         OutputAnalyzer output = TestCommon.executeAndLog(pb, "use-archive");
 191         if (CDSTestUtils.isUnableToMap(output))
 192             System.out.println("Unable to map: test case skipped");
 193         else
 194             output.shouldHaveExitValue(0).shouldContain(TEST_OUT);
 195 
 196         // Quote the class name in the regex as it may contain $
 197         String prefix = ".class,load. ";
 198         String archive_suffix = ".*source: shared objects file.*";
 199         String jar_suffix = ".*source: .*\\.jar";
 200 
 201         for (String clazz : expectedClasses) {
 202             String pattern = prefix + clazz + archive_suffix;
 203             try {
 204                 output.shouldMatch(pattern);
 205             } catch (Exception e) {
 206                 TestCommon.checkCommonExecExceptions(output, e);
 207             }
 208         }
 209 
 210         for (String clazz : unexpectedClasses) {
 211             String pattern = prefix + clazz + archive_suffix;
 212             try {
 213                 output.shouldNotMatch(pattern);
 214             } catch (Exception e) {
 215                 TestCommon.checkCommonExecExceptions(output, e);
 216             }
 217             pattern = prefix + clazz + jar_suffix;
 218             try {
 219                 output.shouldMatch(pattern);
 220             } catch (Exception e) {
 221                 TestCommon.checkCommonExecExceptions(output, e);
 222             }
 223         }
 224     }
 225 }