1 /*
   2  * Copyright (c) 2019, 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 Some negative tests for the SharedArchiveFile option
  28  * @requires vm.cds
  29  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds /test/hotspot/jtreg/runtime/cds/appcds/test-classes
  30  * @build Hello
  31  * @build sun.hotspot.WhiteBox
  32  * @run driver ClassFileInstaller -jar hello.jar Hello
  33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  34  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. SharedArchiveFileOption
  35  */
  36 
  37 import java.io.File;
  38 
  39 public class SharedArchiveFileOption extends DynamicArchiveTestBase {
  40     public static void main(String[] args) throws Exception {
  41         runTest(SharedArchiveFileOption::testCustomBase);
  42     }
  43 
  44     static String baseArchiveName2;
  45     static void testCustomBase() throws Exception {
  46         String topArchiveName = getNewArchiveName("top");
  47         String baseArchiveName = getNewArchiveName("base");
  48         baseArchiveName2 = getNewArchiveName("base2");
  49         TestCommon.dumpBaseArchive(baseArchiveName);
  50         TestCommon.dumpBaseArchive(baseArchiveName2);
  51         doTest(baseArchiveName, topArchiveName);
  52     }
  53 
  54     private static void doTest(String baseArchiveName, String topArchiveName) throws Exception {
  55         String appJar = ClassFileInstaller.getJarPath("hello.jar");
  56         String mainClass = "Hello";
  57         String dummyArchiveName = getNewArchiveName("dummy");
  58 
  59         // -Xshare:dump specified with -XX:ArchiveClassesAtExit
  60         dump2(dummyArchiveName, dummyArchiveName,
  61             "-Xlog:cds",
  62             "-Xlog:cds+dynamic=debug",
  63             "-Xshare:dump",
  64             "-cp", appJar, mainClass)
  65             .assertAbnormalExit(output -> {
  66                     output.shouldContain("-XX:ArchiveClassesAtExit cannot be used with -Xshare:dump");
  67                 });
  68 
  69         // more than 1 archive file specified in -XX:SharedArchiveFile during
  70         // dynamic dumpgin
  71         String dummyArchives = dummyArchiveName + File.pathSeparator + dummyArchiveName;
  72         dump2(dummyArchives, dummyArchiveName,
  73             "-Xlog:cds",
  74             "-Xlog:cds+dynamic=debug",
  75             "-cp", appJar, mainClass)
  76             .assertAbnormalExit(output -> {
  77                     output.shouldContain("Cannot have more than 1 archive file specified in -XX:SharedArchiveFile during CDS dumping");
  78                 });
  79 
  80         // normal dynamic archive dumping
  81         dump2(baseArchiveName, topArchiveName,
  82             "-Xlog:cds",
  83             "-Xlog:cds+dynamic=debug",
  84             "-cp", appJar, mainClass)
  85             .assertNormalExit(output -> {
  86                     output.shouldContain("Buffer-space to target-space delta")
  87                            .shouldContain("Written dynamic archive 0x");
  88                 });
  89 
  90         // same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit
  91         dump2(baseArchiveName, baseArchiveName,
  92             "-Xlog:cds",
  93             "-Xlog:cds+dynamic=debug",
  94             "-cp", appJar, mainClass)
  95             .assertAbnormalExit(output -> {
  96                     output.shouldContain("Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit: "
  97                         + baseArchiveName);
  98                 });
  99 
 100 
 101         // a top archive specified in the base archive position
 102         run2(topArchiveName, baseArchiveName,
 103             "-Xlog:class+load",
 104             "-Xlog:cds+dynamic=debug,cds=debug",
 105             "-cp", appJar, mainClass)
 106             .assertAbnormalExit(output -> {
 107                     output.shouldMatch("Not a base shared archive:.*top.*.jsa");
 108                 });
 109 
 110         // a base archive specified in the top archive position
 111         run2(baseArchiveName, baseArchiveName2,
 112             "-Xlog:class+load",
 113             "-Xlog:cds+dynamic=debug,cds=debug",
 114             "-cp", appJar, mainClass)
 115             .assertAbnormalExit(output -> {
 116                     output.shouldMatch("Not a top shared archive:.*base.*.jsa");
 117                 });
 118 
 119         // more than 2 archives specified in the -XX:ShareArchiveFile option
 120         String baseArchives = baseArchiveName + File.pathSeparator + baseArchiveName2;
 121         run2(baseArchives, topArchiveName,
 122             "-Xlog:class+load",
 123             "-Xlog:cds+dynamic=debug,cds=debug",
 124             "-cp", appJar, mainClass)
 125             .assertAbnormalExit(output -> {
 126                     output.shouldContain(
 127                         "Cannot have more than 2 archive files specified in the -XX:SharedArchiveFile option");
 128                 });
 129 
 130         // base archive not specified
 131         final String topArchive = File.pathSeparator + topArchiveName;
 132         run2(topArchive, null,
 133             "-Xlog:class+load",
 134             "-Xlog:cds+dynamic=debug,cds=debug",
 135             "-cp", appJar, mainClass)
 136             .assertAbnormalExit(output -> {
 137                     output.shouldContain(
 138                         "Base archive was not specified: " + topArchive);
 139                 });
 140 
 141         // top archive not specified
 142         final String baseArchive = baseArchiveName + File.pathSeparator;
 143         run2(baseArchive, null,
 144             "-Xlog:class+load",
 145             "-Xlog:cds+dynamic=debug,cds=debug",
 146             "-cp", appJar, mainClass)
 147             .assertAbnormalExit(output -> {
 148                     output.shouldContain(
 149                         "Top archive was not specified: " + baseArchive);
 150                 });
 151     }
 152 }