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.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.util.Arrays;
  38 import jdk.test.lib.Platform;
  39 import jdk.test.lib.compiler.CompilerUtils;
  40 import jdk.test.lib.process.ProcessTools;
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 
  43 public class LongBCP {
  44 
  45     private static final int MAX_PATH = 260;
  46 
  47     public static void main(String args[]) throws Exception {
  48         Path sourceDir = Paths.get(System.getProperty("test.src"), "test-classes");
  49         Path classDir = Paths.get(System.getProperty("test.classes"));
  50         Path destDir = classDir;
  51 
  52         // create a sub-path so that the destDir length is almost MAX_PATH
  53         // so that the full path (with the class name) will exceed MAX_PATH
  54         int subDirLen = MAX_PATH - classDir.toString().length() - 2;
  55         if (subDirLen > 0) {
  56             char[] chars = new char[subDirLen];
  57             Arrays.fill(chars, 'x');
  58             String subPath = new String(chars);
  59             destDir = Paths.get(System.getProperty("test.classes"), subPath);
  60         }
  61 
  62         CompilerUtils.compile(sourceDir, destDir);
  63 
  64         String bootCP = "-Xbootclasspath/a:" + destDir.toString();
  65         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  66             bootCP, "Hello");
  67 
  68         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  69         output.shouldContain("Hello World")
  70               .shouldHaveExitValue(0);
  71 
  72         // increase the length of destDir to slightly over MAX_PATH
  73         destDir = Paths.get(destDir.toString(), "xxxxx");
  74         CompilerUtils.compile(sourceDir, destDir);
  75 
  76         bootCP = "-Xbootclasspath/a:" + destDir.toString();
  77         pb = ProcessTools.createJavaProcessBuilder(
  78             bootCP, "Hello");
  79 
  80         output = new OutputAnalyzer(pb.start());
  81         output.shouldContain("Hello World")
  82               .shouldHaveExitValue(0);
  83 
  84         // relative path tests
  85         // We currently cannot handle relative path specified in the
  86         // -Xbootclasspath/a on windows.
  87         //
  88         // relative path length within the 256 limit
  89         char[] chars = new char[255];
  90         Arrays.fill(chars, 'y');
  91         String subPath = new String(chars);
  92         destDir = Paths.get(".", subPath);
  93 
  94         CompilerUtils.compile(sourceDir, destDir);
  95 
  96         bootCP = "-Xbootclasspath/a:" + destDir.toString();
  97         pb = ProcessTools.createJavaProcessBuilder(
  98             bootCP, "Hello");
  99 
 100         output = new OutputAnalyzer(pb.start());
 101         if (!Platform.isWindows()) {
 102             output.shouldContain("Hello World")
 103                   .shouldHaveExitValue(0);
 104         } else {
 105             output.shouldContain("Could not find or load main class Hello")
 106                   .shouldHaveExitValue(1);
 107         }
 108 
 109         // total relative path length exceeds MAX_PATH
 110         destDir = Paths.get(destDir.toString(), "yyyyyyyy");
 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         if (!Platform.isWindows()) {
 120             output.shouldContain("Hello World")
 121                   .shouldHaveExitValue(0);
 122         } else {
 123             output.shouldContain("Could not find or load main class Hello")
 124                   .shouldHaveExitValue(1);
 125         }
 126     }
 127 }