1 /*
   2  * Copyright (c) 2017, 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  * @run main LongBCP
  33  */
  34 
  35 import java.io.File;
  36 import java.nio.file.Files;
  37 import java.nio.file.FileStore;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.Arrays;
  41 import jdk.test.lib.Platform;
  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         // relative path tests
  88         // We currently cannot handle relative path specified in the
  89         // -Xbootclasspath/a on windows.
  90         //
  91         // relative path length within the file system limit
  92         int fn_max_length = 255;
  93         // In AUFS file system, the maximal file name length is 242
  94         FileStore store = Files.getFileStore(new File(".").toPath());
  95         String fs_type = store.type();
  96         if ("aufs".equals(fs_type)) {
  97             fn_max_length = 242;
  98         }
  99         char[] chars = new char[fn_max_length];
 100         Arrays.fill(chars, 'y');
 101         String subPath = new String(chars);
 102         destDir = Paths.get(".", subPath);
 103 
 104         CompilerUtils.compile(sourceDir, destDir);
 105 
 106         bootCP = "-Xbootclasspath/a:" + destDir.toString();
 107         pb = ProcessTools.createJavaProcessBuilder(
 108             bootCP, "Hello");
 109 
 110         output = new OutputAnalyzer(pb.start());
 111         if (!Platform.isWindows()) {
 112             output.shouldContain("Hello World")
 113                   .shouldHaveExitValue(0);
 114         } else {
 115             output.shouldContain("Could not find or load main class Hello")
 116                   .shouldHaveExitValue(1);
 117         }
 118 
 119         // total relative path length exceeds MAX_PATH
 120         destDir = Paths.get(destDir.toString(), "yyyyyyyy");
 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         if (!Platform.isWindows()) {
 130             output.shouldContain("Hello World")
 131                   .shouldHaveExitValue(0);
 132         } else {
 133             output.shouldContain("Could not find or load main class Hello")
 134                   .shouldHaveExitValue(1);
 135         }
 136     }
 137 }