Path: blob/master/test/hotspot/jtreg/runtime/LoadClass/LongBCP.java
40942 views
/*1* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary JVM should be able to handle full path (directory path plus26* class name) or directory path longer than MAX_PATH specified27* in -Xbootclasspath/a on windows.28* @library /test/lib29* @modules java.base/jdk.internal.misc30* java.management31* jdk.jartool/sun.tools.jar32* @run driver LongBCP33*/3435import java.io.File;36import java.nio.file.Files;37import java.nio.file.FileStore;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.util.Arrays;41import java.util.spi.ToolProvider;42import jdk.test.lib.compiler.CompilerUtils;43import jdk.test.lib.process.ProcessTools;44import jdk.test.lib.process.OutputAnalyzer;4546public class LongBCP {4748private static final int MAX_PATH = 260;4950private static final ToolProvider JAR = ToolProvider.findFirst("jar")51.orElseThrow(() -> new RuntimeException("ToolProvider for jar not found"));5253public static void main(String args[]) throws Exception {54Path sourceDir = Paths.get(System.getProperty("test.src"), "test-classes");55Path classDir = Paths.get(System.getProperty("test.classes"));56Path destDir = classDir;5758// create a sub-path so that the destDir length is almost MAX_PATH59// so that the full path (with the class name) will exceed MAX_PATH60int subDirLen = MAX_PATH - classDir.toString().length() - 2;61if (subDirLen > 0) {62char[] chars = new char[subDirLen];63Arrays.fill(chars, 'x');64String subPath = new String(chars);65destDir = Paths.get(System.getProperty("test.classes"), subPath);66}6768CompilerUtils.compile(sourceDir, destDir);6970String bootCP = "-Xbootclasspath/a:" + destDir.toString();71ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(72bootCP, "Hello");7374OutputAnalyzer output = new OutputAnalyzer(pb.start());75output.shouldContain("Hello World")76.shouldHaveExitValue(0);7778// increase the length of destDir to slightly over MAX_PATH79destDir = Paths.get(destDir.toString(), "xxxxx");80CompilerUtils.compile(sourceDir, destDir);8182bootCP = "-Xbootclasspath/a:" + destDir.toString();83pb = ProcessTools.createJavaProcessBuilder(84bootCP, "Hello");8586output = new OutputAnalyzer(pb.start());87output.shouldContain("Hello World")88.shouldHaveExitValue(0);8990// create a hello.jar91String helloJar = destDir.toString() + File.separator + "hello.jar";92if (JAR.run(System.out, System.err, "-cf", helloJar, "-C", destDir.toString(), "Hello.class") != 0) {93throw new RuntimeException("jar operation for hello.jar failed");94}9596// run with long bootclasspath to hello.jar97bootCP = "-Xbootclasspath/a:" + helloJar;98pb = ProcessTools.createJavaProcessBuilder(99bootCP, "Hello");100101output = new OutputAnalyzer(pb.start());102output.shouldContain("Hello World")103.shouldHaveExitValue(0);104105// relative path tests106//107// relative path length within the file system limit108int fn_max_length = 255;109// In AUFS file system, the maximal file name length is 242110FileStore store = Files.getFileStore(new File(".").toPath());111String fs_type = store.type();112if ("aufs".equals(fs_type)) {113fn_max_length = 242;114}115char[] chars = new char[fn_max_length];116Arrays.fill(chars, 'y');117String subPath = new String(chars);118destDir = Paths.get(".", subPath);119120CompilerUtils.compile(sourceDir, destDir);121122bootCP = "-Xbootclasspath/a:" + destDir.toString();123pb = ProcessTools.createJavaProcessBuilder(124bootCP, "Hello");125126output = new OutputAnalyzer(pb.start());127output.shouldContain("Hello World")128.shouldHaveExitValue(0);129130// Test a relative path for a jar file < MAX_PATH, but where the131// absolute path is > MAX_PATH.132Path jarDir = Paths.get(".");133for (int i = 0; i < 21; ++i) {134jarDir = jarDir.resolve("0123456789");135}136Files.createDirectories(jarDir);137Path jarPath = jarDir.resolve("hello.jar");138Files.copy(Paths.get(helloJar), jarPath);139bootCP = "-Xbootclasspath/a:" + jarPath.toString();140pb = ProcessTools.createJavaProcessBuilder(bootCP, "Hello");141142output = new OutputAnalyzer(pb.start());143output.shouldContain("Hello World")144.shouldHaveExitValue(0);145146// total relative path length exceeds MAX_PATH147destDir = Paths.get(destDir.toString(), "yyyyyyyy");148149CompilerUtils.compile(sourceDir, destDir);150151bootCP = "-Xbootclasspath/a:" + destDir.toString();152pb = ProcessTools.createJavaProcessBuilder(153bootCP, "Hello");154155output = new OutputAnalyzer(pb.start());156output.shouldContain("Hello World")157.shouldHaveExitValue(0);158}159}160161162