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