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.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         // We currently cannot handle relative path specified in the
 106         // -Xbootclasspath/a on windows.
 107         //
 108         // relative path length within the file system limit
 109         int fn_max_length = 255;
 110         // In AUFS file system, the maximal file name length is 242
 111         FileStore store = Files.getFileStore(new File(".").toPath());
 112         String fs_type = store.type();
 113         if ("aufs".equals(fs_type)) {
 114             fn_max_length = 242;
 115         }
 116         char[] chars = new char[fn_max_length];
 117         Arrays.fill(chars, 'y');
 118         String subPath = new String(chars);
 119         destDir = Paths.get(".", subPath);
 120 
 121         CompilerUtils.compile(sourceDir, destDir);
 122 
 123         bootCP = "-Xbootclasspath/a:" + destDir.toString();
 124         pb = ProcessTools.createJavaProcessBuilder(
 125             bootCP, "Hello");
 126 
 127         output = new OutputAnalyzer(pb.start());
 128         output.shouldContain("Hello World")
 129               .shouldHaveExitValue(0);
 130 
 131         // total relative path length exceeds MAX_PATH
 132         destDir = Paths.get(destDir.toString(), "yyyyyyyy");
 133 
 134         CompilerUtils.compile(sourceDir, destDir);
 135 
 136         bootCP = "-Xbootclasspath/a:" + destDir.toString();
 137         pb = ProcessTools.createJavaProcessBuilder(
 138             bootCP, "Hello");
 139 
 140         output = new OutputAnalyzer(pb.start());
 141         output.shouldContain("Hello World")
 142               .shouldHaveExitValue(0);
 143     }
 144 }