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