1 /* 2 * Copyright (c) 2017, 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 Handling of directories in -cp is based on the classlist 28 * @requires vm.cds 29 * @library /test/lib 30 * @modules jdk.jartool/sun.tools.jar 31 * @compile test-classes/Hello.java 32 * @compile test-classes/Super.java 33 * @run driver DirClasspathTest 34 */ 35 36 import jdk.test.lib.Platform; 37 import jdk.test.lib.process.OutputAnalyzer; 38 import java.io.File; 39 import java.nio.file.Files; 40 import java.nio.file.Path; 41 import java.nio.file.Paths; 42 import java.util.Arrays; 43 44 public class DirClasspathTest { 45 private static final int MAX_PATH = 260; 46 47 // We add helloJar into the classpath to be compatible with TestCommon.DYNAMIC_DUMP 48 static OutputAnalyzer doDump(String path, String classList[], 49 String... suffix) throws Exception { 50 String helloJar = JarBuilder.getOrCreateHelloJar(); 51 return TestCommon.dump(helloJar + File.pathSeparator + path, classList, suffix); 52 } 53 54 public static void main(String[] args) throws Exception { 55 File dir = new File(System.getProperty("user.dir")); 56 File emptydir = new File(dir, "emptydir"); 57 emptydir.mkdir(); 58 59 60 ///////////////////////////////////////////////////////////////// 61 // The classlist only contains boot class in following test cases 62 ///////////////////////////////////////////////////////////////// 63 String bootClassList[] = {"java/lang/Object"}; 64 65 // Empty dir in -cp: should be OK 66 OutputAnalyzer output; 67 output = doDump(emptydir.getPath(), bootClassList, "-Xlog:class+path=info"); 68 TestCommon.checkDump(output); 69 70 // Long path to empty dir in -cp: should be OK 71 Path classDir = Paths.get(System.getProperty("test.classes")); 72 Path destDir = classDir; 73 int subDirLen = MAX_PATH - classDir.toString().length() - 2; 74 if (subDirLen > 0) { 75 char[] chars = new char[subDirLen]; 76 Arrays.fill(chars, 'x'); 77 String subPath = new String(chars); 78 destDir = Paths.get(System.getProperty("test.classes"), subPath); 79 } 80 File longDir = destDir.toFile(); 81 longDir.mkdir(); 82 File subDir = new File(longDir, "subdir"); 83 subDir.mkdir(); 84 output = doDump(subDir.getPath(), bootClassList, "-Xlog:class+path=info"); 85 TestCommon.checkDump(output); 86 87 // Non-empty dir in -cp: should be OK 88 // <dir> is not empty because it has at least one subdirectory, i.e., <emptydir> 89 output = doDump(dir.getPath(), bootClassList, "-Xlog:class+path=info"); 90 TestCommon.checkDump(output); 91 92 // Long path to non-empty dir in -cp: should be OK 93 // <dir> is not empty because it has at least one subdirectory, i.e., <emptydir> 94 output = doDump(longDir.getPath(), bootClassList, "-Xlog:class+path=info"); 95 TestCommon.checkDump(output); 96 97 ///////////////////////////////////////////////////////////////// 98 // The classlist contains non-boot class in following test cases 99 ///////////////////////////////////////////////////////////////// 100 String appClassList[] = {"java/lang/Object", "com/sun/tools/javac/Main"}; 101 102 // Non-empty dir in -cp: should be OK (as long as no classes were loaded from there) 103 output = doDump(dir.getPath(), appClassList, "-Xlog:class+path=info"); 104 TestCommon.checkDump(output); 105 106 // Long path to non-empty dir in -cp: should be OK (as long as no classes were loaded from there) 107 output = doDump(longDir.getPath(), appClassList, "-Xlog:class+path=info"); 108 TestCommon.checkDump(output); 109 110 ///////////////////////////////////////////////////////////////// 111 // Loading an app class from a directory 112 ///////////////////////////////////////////////////////////////// 113 String appClassList2[] = {"Super", "java/lang/Object", "com/sun/tools/javac/Main"}; 114 // Non-empty dir in -cp: should report error if a class is loaded from it 115 output = doDump(classDir.toString(), appClassList2, "-Xlog:class+path=info,class+load=trace"); 116 output.shouldNotHaveExitValue(0); 117 output.shouldContain("Cannot have non-empty directory in paths"); 118 119 // Long path to non-empty dir in -cp: should report error if a class is loaded from it 120 File srcClass = new File(classDir.toFile(), "Super.class"); 121 File destClass = new File(longDir, "Super.class"); 122 Files.copy(srcClass.toPath(), destClass.toPath()); 123 output = doDump(longDir.getPath(), appClassList2, "-Xlog:class+path=info"); 124 output.shouldNotHaveExitValue(0); 125 output.shouldContain("Cannot have non-empty directory in paths"); 126 } 127 }