Path: blob/master/test/jdk/java/io/File/MaxPathLength.java
41149 views
/*1* Copyright (c) 2002, 2013, 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/* @test24@bug 4759207 4403166 4165006 4403166 6182812 6274272 716001325@summary Test to see if win32 path length can be greater than 26026*/2728import java.io.*;29import java.nio.file.Files;30import java.nio.file.Path;31import java.nio.file.DirectoryNotEmptyException;3233public class MaxPathLength {34private static String sep = File.separator;35private static String pathComponent = sep +36"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";37private static String fileName =38"areallylongfilenamethatsforsur";39private static boolean isWindows = false;4041private static final int MAX_LENGTH = 256;4243private static int counter = 0;4445public static void main(String[] args) throws Exception {46String osName = System.getProperty("os.name");47if (osName.startsWith("Windows")) {48isWindows = true;49}5051for (int i = 4; i < 7; i++) {52String name = fileName;53while (name.length() < MAX_LENGTH) {54testLongPath (i, name, false);55testLongPath (i, name, true);56name = getNextName(name);57}58}5960// test long paths on windows61// And these long pathes cannot be handled on Solaris and Mac platforms62if (isWindows) {63String name = fileName;64while (name.length() < MAX_LENGTH) {65testLongPath (20, name, false);66testLongPath (20, name, true);67name = getNextName(name);68}69}70}7172private static String getNextName(String fName) {73return (fName.length() < MAX_LENGTH/2) ? fName + fName74: fName + "A";75}7677static void testLongPath(int max, String fn,78boolean tryAbsolute) throws Exception {79String[] created = new String[max];80String pathString = ".";81for (int i = 0; i < max -1; i++) {82pathString = pathString + pathComponent + (counter++);83created[max - 1 -i] = pathString;84}8586File dirFile = new File(pathString);87File f = new File(pathString + sep + fn);8889String tPath = f.getPath();90if (tryAbsolute) {91tPath = f.getCanonicalPath();92}93created[0] = tPath;9495//for getCanonicalPath testing on win3296File fu = new File(pathString + sep + fn.toUpperCase());9798if (dirFile.exists()) {99System.err.println("Warning: Test directory structure exists already!");100return;101}102103try {104Files.createDirectories(dirFile.toPath());105106if (tryAbsolute)107dirFile = new File(dirFile.getCanonicalPath());108if (!dirFile.isDirectory())109throw new RuntimeException ("File.isDirectory() failed");110f = new File(tPath);111if (!f.createNewFile()) {112throw new RuntimeException ("File.createNewFile() failed");113}114115if (!f.exists())116throw new RuntimeException ("File.exists() failed");117if (!f.isFile())118throw new RuntimeException ("File.isFile() failed");119if (!f.canRead())120throw new RuntimeException ("File.canRead() failed");121if (!f.canWrite())122throw new RuntimeException ("File.canWrite() failed");123124if (!f.delete())125throw new RuntimeException ("File.delete() failed");126127FileOutputStream fos = new FileOutputStream(f);128fos.write(1);129fos.close();130131if (f.length() != 1)132throw new RuntimeException ("File.length() failed");133long time = System.currentTimeMillis();134if (!f.setLastModified(time))135throw new RuntimeException ("File.setLastModified() failed");136if (f.lastModified() == 0) {137throw new RuntimeException ("File.lastModified() failed");138}139String[] list = dirFile.list();140if (list == null || !fn.equals(list[0])) {141throw new RuntimeException ("File.list() failed");142}143144File[] flist = dirFile.listFiles();145if (flist == null || !fn.equals(flist[0].getName()))146throw new RuntimeException ("File.listFiles() failed");147148if (isWindows &&149!fu.getCanonicalPath().equals(f.getCanonicalPath()))150throw new RuntimeException ("getCanonicalPath() failed");151152char[] cc = tPath.toCharArray();153cc[cc.length-1] = 'B';154File nf = new File(new String(cc));155if (!f.renameTo(nf)) {156/*there is a known issue that renameTo fails if157(1)the path is a UNC path and158(2)the path length is bigger than 1092159so don't stop if above are true160*/161String abPath = f.getAbsolutePath();162if (!abPath.startsWith("\\\\") ||163abPath.length() < 1093) {164throw new RuntimeException ("File.renameTo() failed for lenth="165+ abPath.length());166}167} else {168if (!nf.canRead())169throw new RuntimeException ("Renamed file is not readable");170if (!nf.canWrite())171throw new RuntimeException ("Renamed file is not writable");172if (nf.length() != 1)173throw new RuntimeException ("Renamed file's size is not correct");174if (!nf.renameTo(f)) {175created[0] = nf.getPath();176}177/* add a script to test these two if we got a regression later178if (!f.setReadOnly())179throw new RuntimeException ("File.setReadOnly() failed");180f.deleteOnExit();181*/182}183} finally {184// Clean up185for (int i = 0; i < max; i++) {186Path p = (new File(created[i])).toPath();187try {188Files.deleteIfExists(p);189// Test if the file is really deleted and wait for 1 second at most190for (int j = 0; j < 10 && Files.exists(p); j++) {191Thread.sleep(100);192}193} catch (DirectoryNotEmptyException ex) {194// Give up the clean-up, let jtreg handle it.195System.err.println("Dir, " + p + ", is not empty");196break;197}198}199}200}201}202203204