1 /*
   2  * Copyright (c) 2016, 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  * @test
  26  * @requires vm.cds & !vm.graal.enabled
  27  * @summary Testing -Xbootclasspath/a support for CDS
  28  * @requires vm.cds
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  33  * @compile javax/sound/sampled/MyClass.jasm
  34  * @compile org/omg/CORBA/Context.jasm
  35  * @compile nonjdk/myPackage/MyClass.java
  36  * @build LoadClass
  37  * @run main/othervm BootAppendTests
  38  */
  39 
  40 import java.io.File;
  41 import java.io.FileOutputStream;
  42 import java.io.IOException;
  43 import java.io.PrintStream;
  44 
  45 import java.nio.file.Path;
  46 import java.nio.file.Paths;
  47 
  48 import jdk.test.lib.cds.CDSOptions;
  49 import jdk.test.lib.cds.CDSTestUtils;
  50 import jdk.test.lib.process.ProcessTools;
  51 import jdk.test.lib.process.OutputAnalyzer;
  52 
  53 public class BootAppendTests {
  54     private static final String APP_CLASS = "LoadClass";
  55     private static final String BOOT_APPEND_MODULE_CLASS = "javax/sound/sampled/MyClass";
  56     private static final String BOOT_APPEND_DUPLICATE_MODULE_CLASS = "org/omg/CORBA/Context";
  57     private static final String BOOT_APPEND_CLASS = "nonjdk/myPackage/MyClass";
  58     private static final String BOOT_APPEND_MODULE_CLASS_NAME =
  59         BOOT_APPEND_MODULE_CLASS.replace('/', '.');
  60     private static final String BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME =
  61         BOOT_APPEND_DUPLICATE_MODULE_CLASS.replace('/', '.');
  62     private static final String BOOT_APPEND_CLASS_NAME =
  63         BOOT_APPEND_CLASS.replace('/', '.');
  64     private static final String[] ARCHIVE_CLASSES =
  65         {BOOT_APPEND_MODULE_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS, BOOT_APPEND_CLASS};
  66 
  67     private static final String modes[] = {"on", "off"};
  68 
  69     private static String appJar;
  70     private static String bootAppendJar;
  71 
  72     public static void main(String... args) throws Exception {
  73         dumpArchive();
  74 
  75         logTestCase("1");
  76         testBootAppendModuleClass();
  77 
  78         logTestCase("2");
  79         testBootAppendDuplicateModuleClass();
  80 
  81         logTestCase("3");
  82         testBootAppendExcludedModuleClass();
  83 
  84         logTestCase("4");
  85         testBootAppendDuplicateExcludedModuleClass();
  86 
  87         logTestCase("5");
  88         testBootAppendClass();
  89 
  90         logTestCase("6");
  91         testBootAppendExtraDir();
  92     }
  93 
  94     private static void logTestCase(String msg) {
  95         System.out.println();
  96         System.out.printf("TESTCASE: %s", msg);
  97         System.out.println();
  98     }
  99 
 100     static void dumpArchive() throws Exception {
 101         // create the classlist
 102         File classlist = CDSTestUtils.makeClassList(ARCHIVE_CLASSES);
 103 
 104         // build jar files
 105         appJar = ClassFileInstaller.writeJar("app.jar", APP_CLASS);
 106         bootAppendJar = ClassFileInstaller.writeJar("bootAppend.jar",
 107             BOOT_APPEND_MODULE_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS, BOOT_APPEND_CLASS);
 108 
 109 
 110         OutputAnalyzer out = CDSTestUtils.createArchiveAndCheck(
 111                                  "-Xbootclasspath/a:" + bootAppendJar,
 112                                  "-XX:SharedClassListFile=" + classlist.getPath());
 113         // Make sure all the classes were successfully archived.
 114         for (String archiveClass : ARCHIVE_CLASSES) {
 115             out.shouldNotContain("Preload Warning: Cannot find " + archiveClass);
 116         }
 117     }
 118 
 119     // Test #1: If a class on -Xbootclasspath/a is from a package defined in
 120     //          bootmodules, the class is not loaded at runtime.
 121     //          Verify the behavior is the same when the class is archived
 122     //          with CDS enabled at runtime.
 123     //
 124     //          The javax.sound.sampled package is defined in the java.desktop module.
 125     //          The archived javax.sound.sampled.MyClass from the -Xbootclasspath/a
 126     //          should not be loaded at runtime.
 127     public static void testBootAppendModuleClass() throws Exception {
 128         for (String mode : modes) {
 129             CDSOptions opts = (new CDSOptions())
 130                 .setXShareMode(mode).setUseVersion(false)
 131                 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-cp", appJar, "-showversion")
 132                 .addSuffix(APP_CLASS, BOOT_APPEND_MODULE_CLASS_NAME);
 133 
 134             OutputAnalyzer out = CDSTestUtils.runWithArchive(opts);
 135             CDSTestUtils.checkExec(out, opts, "java.lang.ClassNotFoundException: javax.sound.sampled.MyClass");
 136         }
 137     }
 138 
 139     // Test #2: If a class on -Xbootclasspath/a has the same fully qualified
 140     //          name as a class defined in boot modules, the class is not loaded
 141     //          from -Xbootclasspath/a. Verify the behavior is the same at runtime
 142     //          when CDS is enabled.
 143     //
 144     //          The org.omg.CORBA.Context is a boot module class. The class on
 145     //          the -Xbootclasspath/a path that has the same fully-qualified name
 146     //          should not be loaded at runtime when CDS is enabled.
 147     //          The one from the boot modules should be loaded instead.
 148     public static void testBootAppendDuplicateModuleClass() throws Exception {
 149         for (String mode : modes) {
 150             CDSOptions opts = (new CDSOptions())
 151                 .setXShareMode(mode).setUseVersion(false)
 152                 .addPrefix("--add-modules", "java.corba", "-showversion",
 153                            "-Xbootclasspath/a:" + bootAppendJar, "-cp", appJar)
 154                 .addSuffix("-Xlog:class+load=info",
 155                            APP_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME);
 156 
 157             OutputAnalyzer out = CDSTestUtils.runWithArchive(opts);
 158             CDSTestUtils.checkExec(out, opts, "[class,load] org.omg.CORBA.Context source: jrt:/java.corba");
 159         }
 160     }
 161 
 162     // Test #3: If a class on -Xbootclasspath/a is from a package defined in boot modules,
 163     //          the class can be loaded from -Xbootclasspath/a when the module is excluded
 164     //          using --limit-modules. Verify the behavior is the same at runtime when CDS
 165     //          is enabled.
 166     //
 167     //          The java.desktop module is excluded using --limit-modules at runtime,
 168     //          javax.sound.sampled.MyClass is archived from -Xbootclasspath/a. It can be
 169     //          loaded from the archive at runtime.
 170     public static void testBootAppendExcludedModuleClass() throws Exception {
 171         for (String mode : modes) {
 172             CDSOptions opts = (new CDSOptions())
 173                 .setXShareMode(mode).setUseVersion(false)
 174                 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion",
 175                            "--limit-modules=java.base", "-cp", appJar)
 176                 .addSuffix("-Xlog:class+load=info",
 177                            APP_CLASS, BOOT_APPEND_MODULE_CLASS_NAME);
 178 
 179             OutputAnalyzer out = CDSTestUtils.runWithArchive(opts);
 180             CDSTestUtils.checkExec(out, opts, "[class,load] javax.sound.sampled.MyClass");
 181 
 182             // When CDS is enabled, the shared class should be loaded from the archive.
 183             if (mode.equals("on")) {
 184                 CDSTestUtils.checkExec(out, opts, "[class,load] javax.sound.sampled.MyClass source: shared objects file");
 185             }
 186         }
 187     }
 188 
 189     // Test #4: If a class on -Xbootclasspath/a has the same fully qualified
 190     //          name as a class defined in boot modules, the class is loaded
 191     //          from -Xbootclasspath/a when the boot module is excluded using
 192     //          --limit-modules. Verify the behavior is the same at runtime
 193     //          when CDS is enabled.
 194     //
 195     //          The org.omg.CORBA.Context is a boot module class. The class
 196     //          on -Xbootclasspath/a that has the same fully-qualified name
 197     //          as org.omg.CORBA.Context can be loaded at runtime when
 198     //          java.corba is excluded.
 199     public static void testBootAppendDuplicateExcludedModuleClass() throws Exception {
 200         for (String mode : modes) {
 201             CDSOptions opts = (new CDSOptions())
 202                 .setXShareMode(mode).setUseVersion(false)
 203                 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion",
 204                            "--limit-modules=java.base", "-cp", appJar)
 205                 .addSuffix("-Xlog:class+load=info",
 206                            APP_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME);
 207 
 208             OutputAnalyzer out = CDSTestUtils.runWithArchive(opts);
 209             CDSTestUtils.checkExec(out, opts, "[class,load] org.omg.CORBA.Context");
 210             if (!CDSTestUtils.isUnableToMap(out)) {
 211                 if (mode.equals("off")) {
 212                     out.shouldMatch(".*\\[class,load\\] org.omg.CORBA.Context source:.*bootAppend.jar");
 213                 } else {
 214                     CDSTestUtils.checkExec(out, opts, "[class,load] org.omg.CORBA.Context source: shared objects file");
 215                 }
 216             }
 217         }
 218     }
 219 
 220     // Test #5: If a class on -Xbootclasspath/a is not from named modules,
 221     //          the class can be loaded at runtime. Verify the behavior is
 222     //          the same at runtime when CDS is enabled.
 223     //
 224     //          The nonjdk.myPackage is not defined in named modules. The
 225     //          archived nonjdk.myPackage.MyClass from -Xbootclasspath/a
 226     //          can be loaded at runtime when CDS is enabled.
 227     public static void testBootAppendClass() throws Exception {
 228         for (String mode : modes) {
 229             CDSOptions opts = (new CDSOptions())
 230                 .setXShareMode(mode).setUseVersion(false)
 231                 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion",
 232                            "--limit-modules=java.base", "-cp", appJar)
 233                 .addSuffix("-Xlog:class+load=info",
 234                            APP_CLASS, BOOT_APPEND_CLASS_NAME);
 235 
 236             OutputAnalyzer out = CDSTestUtils.runWithArchive(opts);
 237             CDSTestUtils.checkExec(out, opts, "[class,load] nonjdk.myPackage.MyClass");
 238 
 239             // If CDS is enabled, the nonjdk.myPackage.MyClass should be loaded
 240             // from the shared archive.
 241             if (mode.equals("on")) {
 242                 CDSTestUtils.checkExec(out, opts,
 243                     "[class,load] nonjdk.myPackage.MyClass source: shared objects file");
 244             }
 245         }
 246     }
 247 
 248     // Test #6: This is similar to Test #5. During runtime, an extra dir
 249     //          is appended to the bootclasspath. It should not invalidate
 250     //          the shared archive.
 251     public static void testBootAppendExtraDir() throws Exception {
 252         for (String mode : modes) {
 253             CDSOptions opts = (new CDSOptions())
 254                 .setXShareMode(mode).setUseVersion(false)
 255                 .addPrefix("-Xbootclasspath/a:" + bootAppendJar + File.pathSeparator + appJar,
 256                            "-showversion", "--limit-modules=java.base", "-cp", appJar)
 257                 .addSuffix("-Xlog:class+load=info",
 258                            APP_CLASS, BOOT_APPEND_CLASS_NAME);
 259 
 260             OutputAnalyzer out = CDSTestUtils.runWithArchive(opts);
 261             CDSTestUtils.checkExec(out, opts, "[class,load] nonjdk.myPackage.MyClass");
 262 
 263             // If CDS is enabled, the nonjdk.myPackage.MyClass should be loaded
 264             // from the shared archive.
 265             if (mode.equals("on")) {
 266                 CDSTestUtils.checkExec(out, opts,
 267                     "[class,load] nonjdk.myPackage.MyClass source: shared objects file");
 268             }
 269         }
 270     }
 271 }