test/hotspot/jtreg/runtime/appcds/BootClassPathMismatch.java
branchJDK-8200758-branch
changeset 57588 dac8f245de8e
parent 57587 16c4975e9e09
parent 57586 f459f98aa30d
child 57591 6805e0ef7453
equal deleted inserted replaced
57587:16c4975e9e09 57588:dac8f245de8e
     1 /*
       
     2  * Copyright (c) 2014, 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 bootclasspath mismatch test.
       
    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  * @compile test-classes/Hello.java
       
    34  * @run driver BootClassPathMismatch
       
    35  */
       
    36 
       
    37 import jdk.test.lib.Platform;
       
    38 import jdk.test.lib.cds.CDSOptions;
       
    39 import jdk.test.lib.cds.CDSTestUtils;
       
    40 import jdk.test.lib.process.OutputAnalyzer;
       
    41 import java.io.File;
       
    42 import java.nio.file.Files;
       
    43 import java.nio.file.FileAlreadyExistsException;
       
    44 import java.nio.file.StandardCopyOption;
       
    45 import java.nio.file.Path;
       
    46 import java.nio.file.Paths;
       
    47 import java.nio.file.attribute.FileTime;
       
    48 
       
    49 
       
    50 public class BootClassPathMismatch {
       
    51     private static final String mismatchMessage = "shared class paths mismatch";
       
    52 
       
    53     public static void main(String[] args) throws Exception {
       
    54         JarBuilder.getOrCreateHelloJar();
       
    55         copyHelloToNewDir();
       
    56 
       
    57         BootClassPathMismatch test = new BootClassPathMismatch();
       
    58         test.testBootClassPathMismatch();
       
    59         test.testBootClassPathMismatchWithAppClass();
       
    60         test.testBootClassPathMismatchWithBadPath();
       
    61         if (!TestCommon.isDynamicArchive()) {
       
    62             // this test is not applicable to dynamic archive since
       
    63             // there is no class to be archived in the top archive
       
    64             test.testBootClassPathMatchWithAppend();
       
    65         }
       
    66         test.testBootClassPathMatch();
       
    67     }
       
    68 
       
    69     /* Archive contains boot classes only, with Hello class on -Xbootclasspath/a path.
       
    70      *
       
    71      * Error should be detected if:
       
    72      * dump time: -Xbootclasspath/a:${testdir}/hello.jar
       
    73      * run-time : -Xbootclasspath/a:${testdir}/newdir/hello.jar
       
    74      *
       
    75      * or
       
    76      * dump time: -Xbootclasspath/a:${testdir}/newdir/hello.jar
       
    77      * run-time : -Xbootclasspath/a:${testdir}/hello.jar
       
    78      */
       
    79     public void testBootClassPathMismatch() throws Exception {
       
    80         String appJar = JarBuilder.getOrCreateHelloJar();
       
    81         String appClasses[] = {"Hello"};
       
    82         String testDir = TestCommon.getTestDir("newdir");
       
    83         String otherJar = testDir + File.separator + "hello.jar";
       
    84 
       
    85         TestCommon.dump(appJar, appClasses, "-Xbootclasspath/a:" + appJar);
       
    86         TestCommon.run(
       
    87                 "-Xlog:cds",
       
    88                 "-cp", appJar, "-Xbootclasspath/a:" + otherJar, "Hello")
       
    89             .assertAbnormalExit(mismatchMessage);
       
    90 
       
    91         TestCommon.dump(appJar, appClasses, "-Xbootclasspath/a:" + otherJar);
       
    92         TestCommon.run(
       
    93                 "-Xlog:cds",
       
    94                 "-cp", appJar, "-Xbootclasspath/a:" + appJar, "Hello")
       
    95             .assertAbnormalExit(mismatchMessage);
       
    96     }
       
    97 
       
    98     /* Archive contains boot classes only.
       
    99      *
       
   100      * Error should be detected if:
       
   101      * dump time: -Xbootclasspath/a:${testdir}/newdir/hello.jar
       
   102      * run-time : -Xbootclasspath/a:${testdir}/newdir/hello.jar1
       
   103      */
       
   104     public void testBootClassPathMismatchWithBadPath() throws Exception {
       
   105         String appClasses[] = {"Hello"};
       
   106         String testDir = TestCommon.getTestDir("newdir");
       
   107         String appJar = testDir + File.separator + "hello.jar";
       
   108         String otherJar = testDir + File.separator + "hello.jar1";
       
   109 
       
   110         TestCommon.dump(appJar, appClasses, "-Xbootclasspath/a:" + appJar);
       
   111         TestCommon.run(
       
   112                 "-Xlog:cds",
       
   113                 "-cp", appJar, "-Xbootclasspath/a:" + otherJar, "Hello")
       
   114             .assertAbnormalExit(mismatchMessage);
       
   115     }
       
   116 
       
   117     /* Archive contains boot classes only, with Hello loaded from -Xbootclasspath/a at dump time.
       
   118      *
       
   119      * No error if:
       
   120      * dump time: -Xbootclasspath/a:${testdir}/hello.jar
       
   121      * run-time : -Xbootclasspath/a:${testdir}/hello.jar
       
   122      */
       
   123     public void testBootClassPathMatch() throws Exception {
       
   124         String appJar = TestCommon.getTestJar("hello.jar");
       
   125         String appClasses[] = {"Hello"};
       
   126         TestCommon.dump(
       
   127             appJar, appClasses, "-Xbootclasspath/a:" + appJar);
       
   128         TestCommon.run(
       
   129                 "-cp", appJar, "-verbose:class",
       
   130                 "-Xbootclasspath/a:" + appJar, "Hello")
       
   131             .assertNormalExit("[class,load] Hello source: shared objects file");
       
   132 
       
   133         // test relative path to appJar
       
   134         String newJar = TestCommon.composeRelPath(appJar);
       
   135         TestCommon.run(
       
   136                 "-cp", newJar, "-verbose:class",
       
   137                 "-Xbootclasspath/a:" + newJar, "Hello")
       
   138             .assertNormalExit("[class,load] Hello source: shared objects file");
       
   139 
       
   140         int idx = appJar.lastIndexOf(File.separator);
       
   141         String jarName = appJar.substring(idx + 1);
       
   142         String jarDir = appJar.substring(0, idx);
       
   143         // relative path starting with "."
       
   144         TestCommon.runWithRelativePath(
       
   145             jarDir,
       
   146             "-Xshare:on",
       
   147             "-XX:SharedArchiveFile=" + TestCommon.getCurrentArchiveName(),
       
   148             "-cp", "." + File.separator + jarName,
       
   149             "-Xbootclasspath/a:" + "." + File.separator + jarName,
       
   150             "-Xlog:class+load=trace,class+path=info",
       
   151             "Hello")
       
   152             .assertNormalExit(output -> {
       
   153                 output.shouldContain("Hello source: shared objects file")
       
   154                       .shouldHaveExitValue(0);
       
   155                 });
       
   156 
       
   157         // relative path starting with ".."
       
   158         idx = jarDir.lastIndexOf(File.separator);
       
   159         String jarSubDir = jarDir.substring(idx + 1);
       
   160         TestCommon.runWithRelativePath(
       
   161             jarDir,
       
   162             "-Xshare:on",
       
   163             "-XX:SharedArchiveFile=" + TestCommon.getCurrentArchiveName(),
       
   164             "-cp", ".." + File.separator + jarSubDir + File.separator + jarName,
       
   165             "-Xbootclasspath/a:" + ".." + File.separator + jarSubDir + File.separator + jarName,
       
   166             "-Xlog:class+load=trace,class+path=info",
       
   167             "Hello")
       
   168             .assertNormalExit(output -> {
       
   169                 output.shouldContain("Hello source: shared objects file")
       
   170                       .shouldHaveExitValue(0);
       
   171                 });
       
   172 
       
   173         // test sym link to appJar
       
   174         if (!Platform.isWindows()) {
       
   175             File linkedJar = TestCommon.createSymLink(appJar);
       
   176             TestCommon.run(
       
   177                     "-cp", linkedJar.getPath(), "-verbose:class",
       
   178                     "-Xbootclasspath/a:" + linkedJar.getPath(), "Hello")
       
   179                 .assertNormalExit("[class,load] Hello source: shared objects file");
       
   180         }
       
   181     }
       
   182 
       
   183     /* Archive contains boot classes only, runtime add -Xbootclasspath/a path.
       
   184      *
       
   185      * No error:
       
   186      * dump time: No -Xbootclasspath/a
       
   187      * run-time : -Xbootclasspath/a:${testdir}/hello.jar
       
   188      */
       
   189     public void testBootClassPathMatchWithAppend() throws Exception {
       
   190       CDSOptions opts = new CDSOptions().setUseVersion(false);
       
   191       OutputAnalyzer out = CDSTestUtils.createArchive(opts);
       
   192       CDSTestUtils.checkDump(out);
       
   193 
       
   194       String appJar = JarBuilder.getOrCreateHelloJar();
       
   195       opts.addPrefix("-Xbootclasspath/a:" + appJar, "-showversion").addSuffix("Hello");
       
   196       CDSTestUtils.runWithArchiveAndCheck(opts);
       
   197     }
       
   198 
       
   199     /* Archive contains app classes, with Hello on -cp path at dump time.
       
   200      *
       
   201      * Error should be detected if:
       
   202      * dump time: <no bootclasspath specified>
       
   203      * run-time : -Xbootclasspath/a:${testdir}/hello.jar
       
   204      */
       
   205     public void testBootClassPathMismatchWithAppClass() throws Exception {
       
   206         String appJar = JarBuilder.getOrCreateHelloJar();
       
   207         String appClasses[] = {"Hello"};
       
   208         TestCommon.dump(appJar, appClasses);
       
   209         TestCommon.run(
       
   210                 "-Xlog:cds",
       
   211                 "-cp", appJar, "-Xbootclasspath/a:" + appJar, "Hello")
       
   212             .assertAbnormalExit(mismatchMessage);
       
   213 
       
   214         // test relative path to appJar
       
   215         String newJar = TestCommon.composeRelPath(appJar);
       
   216         TestCommon.run(
       
   217                 "-cp", newJar, "-Xbootclasspath/a:" + newJar, "Hello")
       
   218             .assertAbnormalExit(mismatchMessage);
       
   219     }
       
   220 
       
   221     private static void copyHelloToNewDir() throws Exception {
       
   222         String classDir = System.getProperty("test.classes");
       
   223         String dstDir = classDir + File.separator + "newdir";
       
   224         try {
       
   225             Files.createDirectory(Paths.get(dstDir));
       
   226         } catch (FileAlreadyExistsException e) { }
       
   227 
       
   228         // copy as hello.jar
       
   229         Path dstPath = Paths.get(dstDir, "hello.jar");
       
   230         Files.copy(Paths.get(classDir, "hello.jar"),
       
   231             dstPath,
       
   232             StandardCopyOption.REPLACE_EXISTING);
       
   233 
       
   234         File helloJar = dstPath.toFile();
       
   235         long modTime = helloJar.lastModified();
       
   236 
       
   237         // copy as hello.jar1
       
   238         Path dstPath2 = Paths.get(dstDir, "hello.jar1");
       
   239         Files.copy(Paths.get(classDir, "hello.jar"),
       
   240             dstPath2,
       
   241             StandardCopyOption.REPLACE_EXISTING);
       
   242 
       
   243         // On Windows, we rely on the file size, creation time, and
       
   244         // modification time in order to differentiate between 2 files.
       
   245         // Setting a different modification time on hello.jar1 so that this test
       
   246         // runs more reliably on Windows.
       
   247         modTime += 10000;
       
   248         Files.setAttribute(dstPath2, "lastModifiedTime", FileTime.fromMillis(modTime));
       
   249     }
       
   250 }