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.process.OutputAnalyzer;
  38 import java.io.File;
  39 import java.nio.file.Files;
  40 import java.nio.file.FileAlreadyExistsException;
  41 import java.nio.file.StandardCopyOption;
  42 import java.nio.file.Paths;
  43 
  44 
  45 public class BootClassPathMismatch {
  46     private static final String mismatchMessage = "shared class paths mismatch";
  47 
  48     public static void main(String[] args) throws Exception {
  49         JarBuilder.getOrCreateHelloJar();
  50         copyHelloToNewDir();
  51 
  52         BootClassPathMismatch test = new BootClassPathMismatch();
  53         test.testBootClassPathMismatch();
  54         test.testBootClassPathMismatch2();
  55         test.testBootClassPathMatch();
  56     }
  57 
  58     /* Error should be detected if:
  59      * dump time: -Xbootclasspath/a:${testdir}/hello.jar
  60      * run-time : -Xbootclasspath/a:${testdir}/newdir/hello.jar
  61      */
  62     public void testBootClassPathMismatch() throws Exception {
  63         String appJar = JarBuilder.getOrCreateHelloJar();
  64         String appClasses[] = {"Hello"};
  65         TestCommon.dump(
  66             appJar, appClasses, "-Xbootclasspath/a:" + appJar);
  67         String testDir = TestCommon.getTestDir("newdir");
  68         String otherJar = testDir + File.separator + "hello.jar";
  69         TestCommon.run(
  70                 "-cp", appJar, "-verbose:class", "-Xbootclasspath/a:" + otherJar, "Hello")
  71             .assertAbnormalExit(mismatchMessage);
  72     }
  73 
  74     /* Error should be detected if:
  75      * dump time: <no bootclasspath specified>
  76      * run-time : -Xbootclasspath/a:${testdir}/hello.jar
  77      */
  78     public void testBootClassPathMismatch2() throws Exception {
  79         String appJar = JarBuilder.getOrCreateHelloJar();
  80         String appClasses[] = {"Hello"};
  81         TestCommon.dump(appJar, appClasses);
  82         TestCommon.run(
  83                 "-cp", appJar, "-verbose:class", "-Xbootclasspath/a:" + appJar, "Hello")
  84             .assertAbnormalExit(mismatchMessage);
  85     }
  86 
  87     /* No error if:
  88      * dump time: -Xbootclasspath/a:${testdir}/hello.jar
  89      * run-time : -Xbootclasspath/a:${testdir}/hello.jar
  90      */
  91     public void testBootClassPathMatch() throws Exception {
  92         String appJar = TestCommon.getTestJar("hello.jar");
  93         String appClasses[] = {"Hello"};
  94         TestCommon.dump(
  95             appJar, appClasses, "-Xbootclasspath/a:" + appJar);
  96         TestCommon.run(
  97                 "-cp", appJar, "-verbose:class",
  98                 "-Xbootclasspath/a:" + appJar, "Hello")
  99             .assertNormalExit("[class,load] Hello source: shared objects file");
 100     }
 101 
 102     private static void copyHelloToNewDir() throws Exception {
 103         String classDir = System.getProperty("test.classes");
 104         String dstDir = classDir + File.separator + "newdir";
 105         try {
 106             Files.createDirectory(Paths.get(dstDir));
 107         } catch (FileAlreadyExistsException e) { }
 108 
 109         Files.copy(Paths.get(classDir, "hello.jar"),
 110             Paths.get(dstDir, "hello.jar"),
 111             StandardCopyOption.REPLACE_EXISTING);
 112     }
 113 }