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  * @test
26  * @summary JVM should be able to handle full path (directory path plus
27  *          class name) or directory path longer than MAX_PATH specified
28  *          in -Xbootclasspath/a on windows.
29  * @library /test/lib
30  * @modules java.base/jdk.internal.misc
31  *          java.management
32  *          jdk.jartool/sun.tools.jar
33  * @run main LongBCP
34  */
35 
36 import java.io.File;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Arrays;
40 import jdk.test.lib.compiler.CompilerUtils;
41 import jdk.test.lib.process.ProcessTools;
42 import jdk.test.lib.process.OutputAnalyzer;
43 
44 public class LongBCP {
45 
46     private static final int MAX_PATH = 260;
47 
main(String args[])48     public static void main(String args[]) throws Exception {
49         Path sourceDir = Paths.get(System.getProperty("test.src"), "test-classes");
50         Path classDir = Paths.get(System.getProperty("test.classes"));
51         Path destDir = classDir;
52 
53         // create a sub-path so that the destDir length is almost MAX_PATH
54         // so that the full path (with the class name) will exceed MAX_PATH
55         int subDirLen = MAX_PATH - classDir.toString().length() - 2;
56         if (subDirLen > 0) {
57             char[] chars = new char[subDirLen];
58             Arrays.fill(chars, 'x');
59             String subPath = new String(chars);
60             destDir = Paths.get(System.getProperty("test.classes"), subPath);
61         }
62 
63         CompilerUtils.compile(sourceDir, destDir);
64 
65         String bootCP = "-Xbootclasspath/a:" + destDir.toString();
66         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
67             bootCP, "Hello");
68 
69         OutputAnalyzer output = new OutputAnalyzer(pb.start());
70         output.shouldContain("Hello World")
71               .shouldHaveExitValue(0);
72 
73         // increase the length of destDir to slightly over MAX_PATH
74         destDir = Paths.get(destDir.toString(), "xxxxx");
75         CompilerUtils.compile(sourceDir, destDir);
76 
77         bootCP = "-Xbootclasspath/a:" + destDir.toString();
78         pb = ProcessTools.createJavaProcessBuilder(
79             bootCP, "Hello");
80 
81         output = new OutputAnalyzer(pb.start());
82         output.shouldContain("Hello World")
83               .shouldHaveExitValue(0);
84 
85         // create a hello.jar
86         sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
87         String helloJar = destDir.toString() + File.separator + "hello.jar";
88         if (!jarTool.run(new String[]
89             {"-cf", helloJar, "-C", destDir.toString(), "Hello.class"})) {
90             throw new RuntimeException("Could not write the Hello jar file");
91         }
92 
93         // run with long bootclasspath to hello.jar
94         bootCP = "-Xbootclasspath/a:" + helloJar;
95         pb = ProcessTools.createJavaProcessBuilder(
96             bootCP, "Hello");
97 
98         output = new OutputAnalyzer(pb.start());
99         output.shouldContain("Hello World")
100               .shouldHaveExitValue(0);
101 
102         // relative path tests
103         // We currently cannot handle relative path specified in the
104         // -Xbootclasspath/a on windows.
105         //
106         // relative path length within the 256 limit
107         char[] chars = new char[255];
108         Arrays.fill(chars, 'y');
109         String subPath = new String(chars);
110         destDir = Paths.get(".", subPath);
111 
112         CompilerUtils.compile(sourceDir, destDir);
113 
114         bootCP = "-Xbootclasspath/a:" + destDir.toString();
115         pb = ProcessTools.createJavaProcessBuilder(
116             bootCP, "Hello");
117 
118         output = new OutputAnalyzer(pb.start());
119         output.shouldContain("Hello World")
120               .shouldHaveExitValue(0);
121 
122         // total relative path length exceeds MAX_PATH
123         destDir = Paths.get(destDir.toString(), "yyyyyyyy");
124 
125         CompilerUtils.compile(sourceDir, destDir);
126 
127         bootCP = "-Xbootclasspath/a:" + destDir.toString();
128         pb = ProcessTools.createJavaProcessBuilder(
129             bootCP, "Hello");
130 
131         output = new OutputAnalyzer(pb.start());
132         output.shouldContain("Hello World")
133               .shouldHaveExitValue(0);
134     }
135 }
136