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 driver LongBCP
  34  */
  35 
  36 import java.io.File;
  37 import java.nio.file.Files;
  38 import java.nio.file.FileStore;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.util.Arrays;
  42 import jdk.test.lib.compiler.CompilerUtils;
  43 import jdk.test.lib.process.ProcessTools;
  44 import jdk.test.lib.process.OutputAnalyzer;
  45 
  46 public class LongBCP {
  47 
  48     private static final int MAX_PATH = 260;
  49 
  50     public static void main(String args[]) throws Exception {
  51         Path sourceDir = Paths.get(System.getProperty("test.src"), "test-classes");
  52         Path classDir = Paths.get(System.getProperty("test.classes"));
  53         Path destDir = classDir;
  54 
  55         // create a sub-path so that the destDir length is almost MAX_PATH
  56         // so that the full path (with the class name) will exceed MAX_PATH
  57         int subDirLen = MAX_PATH - classDir.toString().length() - 2;
  58         if (subDirLen > 0) {
  59             char[] chars = new char[subDirLen];
  60             Arrays.fill(chars, 'x');
  61             String subPath = new String(chars);
  62             destDir = Paths.get(System.getProperty("test.classes"), subPath);
  63         }
  64 
  65         CompilerUtils.compile(sourceDir, destDir);
  66 
  67         String bootCP = "-Xbootclasspath/a:" + destDir.toString();
  68         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  69             bootCP, "Hello");
  70 
  71         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  72         output.shouldContain("Hello World")
  73               .shouldHaveExitValue(0);
  74 
  75         // increase the length of destDir to slightly over MAX_PATH
  76         destDir = Paths.get(destDir.toString(), "xxxxx");
  77         CompilerUtils.compile(sourceDir, destDir);
  78 
  79         bootCP = "-Xbootclasspath/a:" + destDir.toString();
  80         pb = ProcessTools.createJavaProcessBuilder(
  81             bootCP, "Hello");
  82 
  83         output = new OutputAnalyzer(pb.start());
  84         output.shouldContain("Hello World")
  85               .shouldHaveExitValue(0);
  86 
  87         // create a hello.jar
  88         sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
  89         String helloJar = destDir.toString() + File.separator + "hello.jar";
  90         if (!jarTool.run(new String[]
  91             {"-cf", helloJar, "-C", destDir.toString(), "Hello.class"})) {
  92             throw new RuntimeException("Could not write the Hello jar file");
  93         }
  94 
  95         // run with long bootclasspath to hello.jar
  96         bootCP = "-Xbootclasspath/a:" + helloJar;
  97         pb = ProcessTools.createJavaProcessBuilder(
  98             bootCP, "Hello");
  99 
 100         output = new OutputAnalyzer(pb.start());
 101         output.shouldContain("Hello World")
 102               .shouldHaveExitValue(0);
 103 
 104         // relative path tests
 105         //
 106         // relative path length within the file system limit
 107         int fn_max_length = 255;
 108         // In AUFS file system, the maximal file name length is 242
 109         FileStore store = Files.getFileStore(new File(".").toPath());
 110         String fs_type = store.type();
 111         if ("aufs".equals(fs_type)) {
 112             fn_max_length = 242;
 113         }
 114         char[] chars = new char[fn_max_length];
 115         Arrays.fill(chars, 'y');
 116         String subPath = new String(chars);
 117         destDir = Paths.get(".", subPath);
 118 
 119         CompilerUtils.compile(sourceDir, destDir);
 120 
 121         bootCP = "-Xbootclasspath/a:" + destDir.toString();
 122         pb = ProcessTools.createJavaProcessBuilder(
 123             bootCP, "Hello");
 124 
 125         output = new OutputAnalyzer(pb.start());
 126         output.shouldContain("Hello World")
 127               .shouldHaveExitValue(0);
 128 
 129         // Test a relative path for a jar file < MAX_PATH, but where the
 130         // absolute path is > MAX_PATH.
 131         Path jarDir = Paths.get(".");
 132         for (int i = 0; i < 21; ++i) {
 133             jarDir = jarDir.resolve("0123456789");
 134         }
 135         Files.createDirectories(jarDir);
 136         Path jarPath = jarDir.resolve("hello.jar");
 137         Files.copy(Paths.get(helloJar), jarPath);
 138         bootCP = "-Xbootclasspath/a:" + jarPath.toString();
 139         pb = ProcessTools.createJavaProcessBuilder(bootCP, "Hello");
 140 
 141         output = new OutputAnalyzer(pb.start());
 142         output.shouldContain("Hello World")
 143               .shouldHaveExitValue(0);
 144 
 145         // total relative path length exceeds MAX_PATH
 146         destDir = Paths.get(destDir.toString(), "yyyyyyyy");
 147 
 148         CompilerUtils.compile(sourceDir, destDir);
 149 
 150         bootCP = "-Xbootclasspath/a:" + destDir.toString();
 151         pb = ProcessTools.createJavaProcessBuilder(
 152             bootCP, "Hello");
 153 
 154         output = new OutputAnalyzer(pb.start());
 155         output.shouldContain("Hello World")
 156               .shouldHaveExitValue(0);
 157     }
 158 }