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 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 main BootClassPathMismatch
  35  */
  36 
  37 import jdk.test.lib.cds.CDSOptions;
  38 import jdk.test.lib.cds.CDSTestUtils;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 import java.io.File;
  41 import java.nio.file.Files;
  42 import java.nio.file.FileAlreadyExistsException;
  43 import java.nio.file.StandardCopyOption;
  44 import java.nio.file.Paths;
  45 
  46 
  47 public class BootClassPathMismatch {
  48     private static final String mismatchMessage = "shared class paths mismatch";
  49 
  50     public static void main(String[] args) throws Exception {
  51         JarBuilder.getOrCreateHelloJar();
  52         copyHelloToNewDir();
  53 
  54         BootClassPathMismatch test = new BootClassPathMismatch();
  55         test.testBootClassPathMismatch();
  56         test.testBootClassPathMismatch2();
  57         test.testBootClassPathMismatchWithAppend();
  58         test.testBootClassPathMatch();
  59     }
  60 
  61     /* Archive contains boot classes only, with Hello class on -Xbootclasspath/a path.
  62      *
  63      * Error should be detected if:
  64      * dump time: -Xbootclasspath/a:${testdir}/hello.jar
  65      * run-time : -Xbootclasspath/a:${testdir}/newdir/hello.jar
  66      */
  67     public void testBootClassPathMismatch() throws Exception {
  68         String appJar = JarBuilder.getOrCreateHelloJar();
  69         String appClasses[] = {"Hello"};
  70         TestCommon.dump(
  71             appJar, appClasses, "-Xbootclasspath/a:" + appJar);
  72         String testDir = TestCommon.getTestDir("newdir");
  73         String otherJar = testDir + File.separator + "hello.jar";
  74         TestCommon.run(
  75                 "-cp", appJar, "-verbose:class", "-Xbootclasspath/a:" + otherJar, "Hello")
  76             .assertAbnormalExit(mismatchMessage);
  77     }
  78 
  79     /* Archive contains boot classes only, with Hello loaded from -Xbootclasspath/a at dump time.
  80      *
  81      * No error if:
  82      * dump time: -Xbootclasspath/a:${testdir}/hello.jar
  83      * run-time : -Xbootclasspath/a:${testdir}/hello.jar
  84      */
  85     public void testBootClassPathMatch() throws Exception {
  86         String appJar = TestCommon.getTestJar("hello.jar");
  87         String appClasses[] = {"Hello"};
  88         TestCommon.dump(
  89             appJar, appClasses, "-Xbootclasspath/a:" + appJar);
  90         TestCommon.run(
  91                 "-cp", appJar, "-verbose:class",
  92                 "-Xbootclasspath/a:" + appJar, "Hello")
  93             .assertNormalExit("[class,load] Hello source: shared objects file");
  94     }
  95 
  96     /* Archive contains boot classes only, runtime add -Xbootclasspath/a path.
  97      *
  98      * No error:
  99      * dump time: No -Xbootclasspath/a
 100      * run-time : -Xbootclasspath/a:${testdir}/hello.jar
 101      */
 102     public void testBootClassPathMismatchWithAppend() throws Exception {
 103       CDSOptions opts = new CDSOptions().setUseVersion(false);
 104       OutputAnalyzer out = CDSTestUtils.createArchive(opts);
 105       CDSTestUtils.checkDump(out);
 106 
 107       String appJar = JarBuilder.getOrCreateHelloJar();
 108       opts.addPrefix("-Xbootclasspath/a:" + appJar, "-showversion").addSuffix("Hello");
 109       CDSTestUtils.runWithArchiveAndCheck(opts);
 110     }
 111 
 112     /* Archive contains app classes, with Hello on -cp path at dump time.
 113      *
 114      * Error should be detected if:
 115      * dump time: <no bootclasspath specified>
 116      * run-time : -Xbootclasspath/a:${testdir}/hello.jar
 117      */
 118     public void testBootClassPathMismatch2() throws Exception {
 119         String appJar = JarBuilder.getOrCreateHelloJar();
 120         String appClasses[] = {"Hello"};
 121         TestCommon.dump(appJar, appClasses);
 122         TestCommon.run(
 123                 "-cp", appJar, "-verbose:class", "-Xbootclasspath/a:" + appJar, "Hello")
 124             .assertAbnormalExit(mismatchMessage);
 125     }
 126 
 127     private static void copyHelloToNewDir() throws Exception {
 128         String classDir = System.getProperty("test.classes");
 129         String dstDir = classDir + File.separator + "newdir";
 130         try {
 131             Files.createDirectory(Paths.get(dstDir));
 132         } catch (FileAlreadyExistsException e) { }
 133 
 134         Files.copy(Paths.get(classDir, "hello.jar"),
 135             Paths.get(dstDir, "hello.jar"),
 136             StandardCopyOption.REPLACE_EXISTING);
 137     }
 138 }