test/hotspot/jtreg/runtime/appcds/DirClasspathTest.java
author ccheung
Fri, 17 May 2019 08:29:55 -0700
changeset 54927 1512d88b24c6
parent 51990 6003e034cdd8
permissions -rw-r--r--
8207812: Implement Dynamic CDS Archive Summary: Improve the usability of AppCDS Reviewed-by: acorn, jiangli, mseledtsov Contributed-by: ioi.lam@oracle.com, jianglizhou@google.com, calvin.cheung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
     1
/*
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
     4
 *
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
     8
 *
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    13
 * accompanied this code).
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    14
 *
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    18
 *
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    21
 * questions.
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    22
 *
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    23
 */
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    24
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    25
/*
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    26
 * @test
49931
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    27
 * @summary Handling of directories in -cp is based on the classlist
48469
7312ae4465d6 8193672: [test] Enhance vm.cds property to check for all conditions required to run CDS tests
iklam
parents: 48191
diff changeset
    28
 * @requires vm.cds
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    29
 * @library /test/lib
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    30
 * @modules jdk.jartool/sun.tools.jar
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
    31
 * @compile test-classes/Hello.java
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    32
 * @compile test-classes/Super.java
51990
6003e034cdd8 8209946: [TESTBUG] CDS tests should use "@run driver"
iklam
parents: 51439
diff changeset
    33
 * @run driver DirClasspathTest
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    34
 */
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    35
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    36
import jdk.test.lib.Platform;
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    37
import jdk.test.lib.process.OutputAnalyzer;
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    38
import java.io.File;
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
    39
import java.nio.file.Files;
48611
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    40
import java.nio.file.Path;
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    41
import java.nio.file.Paths;
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    42
import java.util.Arrays;
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    43
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    44
public class DirClasspathTest {
48611
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    45
    private static final int MAX_PATH = 260;
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    46
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    47
    // We add helloJar into the classpath to be compatible with TestCommon.DYNAMIC_DUMP
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    48
    static OutputAnalyzer doDump(String path, String classList[],
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    49
                                        String... suffix) throws Exception {
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    50
        String helloJar = JarBuilder.getOrCreateHelloJar();
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    51
        return TestCommon.dump(helloJar + File.pathSeparator + path, classList, suffix);
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    52
    }
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    53
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    54
    public static void main(String[] args) throws Exception {
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    55
        File dir = new File(System.getProperty("user.dir"));
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    56
        File emptydir = new File(dir, "emptydir");
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    57
        emptydir.mkdir();
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    58
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    59
49931
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    60
        /////////////////////////////////////////////////////////////////
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    61
        // The classlist only contains boot class in following test cases
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    62
        /////////////////////////////////////////////////////////////////
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    63
        String bootClassList[] = {"java/lang/Object"};
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    64
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    65
        // Empty dir in -cp: should be OK
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    66
        OutputAnalyzer output;
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    67
        output = doDump(emptydir.getPath(), bootClassList, "-Xlog:class+path=info");
48611
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    68
        TestCommon.checkDump(output);
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    69
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    70
        // Long path to empty dir in -cp: should be OK
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    71
        Path classDir = Paths.get(System.getProperty("test.classes"));
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    72
        Path destDir = classDir;
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    73
        int subDirLen = MAX_PATH - classDir.toString().length() - 2;
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    74
        if (subDirLen > 0) {
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    75
            char[] chars = new char[subDirLen];
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    76
            Arrays.fill(chars, 'x');
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    77
            String subPath = new String(chars);
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    78
            destDir = Paths.get(System.getProperty("test.classes"), subPath);
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    79
        }
48611
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    80
        File longDir = destDir.toFile();
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    81
        longDir.mkdir();
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    82
        File subDir = new File(longDir, "subdir");
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    83
        subDir.mkdir();
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    84
        output = doDump(subDir.getPath(), bootClassList, "-Xlog:class+path=info");
49931
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    85
        TestCommon.checkDump(output);
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    86
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    87
        // Non-empty dir in -cp: should be OK
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    88
        // <dir> is not empty because it has at least one subdirectory, i.e., <emptydir>
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    89
        output = doDump(dir.getPath(), bootClassList, "-Xlog:class+path=info");
49931
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    90
        TestCommon.checkDump(output);
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    91
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    92
        // Long path to non-empty dir in -cp: should be OK
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    93
        // <dir> is not empty because it has at least one subdirectory, i.e., <emptydir>
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
    94
        output = doDump(longDir.getPath(), bootClassList, "-Xlog:class+path=info");
48611
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
    95
        TestCommon.checkDump(output);
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
    96
49931
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    97
        /////////////////////////////////////////////////////////////////
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    98
        // The classlist contains non-boot class in following test cases
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
    99
        /////////////////////////////////////////////////////////////////
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
   100
        String appClassList[] = {"java/lang/Object", "com/sun/tools/javac/Main"};
48611
4d7a4fad8190 8192927: os::dir_is_empty is incorrect on Windows
ccheung
parents: 48469
diff changeset
   101
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   102
        // Non-empty dir in -cp: should be OK (as long as no classes were loaded from there)
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
   103
        output = doDump(dir.getPath(), appClassList, "-Xlog:class+path=info");
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   104
        TestCommon.checkDump(output);
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   105
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   106
        // Long path to non-empty dir in -cp: should be OK (as long as no classes were loaded from there)
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
   107
        output = doDump(longDir.getPath(), appClassList, "-Xlog:class+path=info");
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   108
        TestCommon.checkDump(output);
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   109
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   110
        /////////////////////////////////////////////////////////////////
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   111
        // Loading an app class from a directory
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   112
        /////////////////////////////////////////////////////////////////
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
   113
        String appClassList2[] = {"Super", "java/lang/Object", "com/sun/tools/javac/Main"};
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   114
        // Non-empty dir in -cp: should report error if a class is loaded from it
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
   115
        output = doDump(classDir.toString(), appClassList2, "-Xlog:class+path=info,class+load=trace");
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
   116
        output.shouldNotHaveExitValue(0);
49931
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
   117
        output.shouldContain("Cannot have non-empty directory in paths");
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
   118
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   119
        // Long path to non-empty dir in -cp: should report error if a class is loaded from it
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
   120
        File srcClass = new File(classDir.toFile(), "Super.class");
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
   121
        File destClass = new File(longDir, "Super.class");
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 49931
diff changeset
   122
        Files.copy(srcClass.toPath(), destClass.toPath());
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51990
diff changeset
   123
        output = doDump(longDir.getPath(), appClassList2, "-Xlog:class+path=info");
49931
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
   124
        output.shouldNotHaveExitValue(0);
840e26123940 8193213: Make the UseAppCDS option obsolete.
jiangli
parents: 48611
diff changeset
   125
        output.shouldContain("Cannot have non-empty directory in paths");
48191
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
   126
    }
d8a62bea95d3 8190809: JVM crashes while generating appcds for classpath with empty directory entry
iklam
parents:
diff changeset
   127
}