MaxPathLength.java revision 6073:cea72c2bf071
1/*
2 * Copyright (c) 2002, 2012, 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/* @test
25   @bug 4759207 4403166 4165006 4403166 6182812 6274272
26   @summary Test to see if win32 path length can be greater than 260
27 */
28
29import java.io.*;
30
31public class MaxPathLength {
32    private static String sep = File.separator;
33    private static String pathComponent = sep +
34        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
35    private static String fileName =
36                 "areallylongfilenamethatsforsur";
37    private static boolean isWindows = false;
38
39    public static void main(String[] args) throws Exception {
40        String osName = System.getProperty("os.name");
41        if (osName.startsWith("Windows")) {
42            isWindows = true;
43        }
44
45        for (int i = 4; i < 7; i++) {
46            String name = fileName;
47            while (name.length() < 256) {
48                testLongPath (i, name, false);
49                testLongPath (i, name, true);
50                name += "A";
51            }
52        }
53
54        // test long paths on windows
55        if (isWindows) {
56            String name = fileName;
57            while (name.length() < 256) {
58                testLongPath (20, name, false);
59                testLongPath (20, name, true);
60                name += "A";
61            }
62        }
63    }
64
65    static void testLongPath(int max, String fn,
66                             boolean tryAbsolute) throws Exception {
67        String[] created = new String[max];
68        String pathString = ".";
69        for (int i = 0; i < max -1; i++) {
70            pathString = pathString + pathComponent;
71            created[max - 1 -i] = pathString;
72
73        }
74        File dirFile = new File(pathString);
75        File f = new File(pathString + sep + fn);
76
77        String tPath = f.getPath();
78        if (tryAbsolute) {
79            tPath = f.getCanonicalPath();
80        }
81        created[0] = tPath;
82
83        //for getCanonicalPath testing on win32
84        File fu = new File(pathString + sep + fn.toUpperCase());
85
86        if (dirFile.exists()) {
87            System.err.println("Warning: Test directory structure exists already!");
88            return;
89        }
90        boolean couldMakeTestDirectory = dirFile.mkdirs();
91        if (!couldMakeTestDirectory) {
92            throw new RuntimeException ("Could not create test directory structure");
93        }
94        try {
95            if (tryAbsolute)
96                dirFile = new File(dirFile.getCanonicalPath());
97            if (!dirFile.isDirectory())
98                throw new RuntimeException ("File.isDirectory() failed");
99            f = new File(tPath);
100            if (!f.createNewFile()) {
101                throw new RuntimeException ("File.createNewFile() failed");
102            }
103            if (!f.exists())
104                throw new RuntimeException ("File.exists() failed");
105            if (!f.isFile())
106                throw new RuntimeException ("File.isFile() failed");
107            if (!f.canRead())
108                throw new RuntimeException ("File.canRead() failed");
109            if (!f.canWrite())
110                throw new RuntimeException ("File.canWrite() failed");
111            if (!f.delete())
112                throw new RuntimeException ("File.delete() failed");
113            FileOutputStream fos = new FileOutputStream(f);
114            fos.write(1);
115            fos.close();
116            if (f.length() != 1)
117                throw new RuntimeException ("File.length() failed");
118            long time = System.currentTimeMillis();
119            if (!f.setLastModified(time))
120                throw new RuntimeException ("File.setLastModified() failed");
121            if (f.lastModified() == 0) {
122                throw new RuntimeException ("File.lastModified() failed");
123            }
124            String[] list = dirFile.list();
125            if (list == null || !fn.equals(list[0])) {
126                throw new RuntimeException ("File.list() failed");
127            }
128
129            File[] flist = dirFile.listFiles();
130            if (flist == null || !fn.equals(flist[0].getName()))
131                throw new RuntimeException ("File.listFiles() failed");
132
133            if (isWindows &&
134                !fu.getCanonicalPath().equals(f.getCanonicalPath()))
135                throw new RuntimeException ("getCanonicalPath() failed");
136
137            char[] cc = tPath.toCharArray();
138            cc[cc.length-1] = 'B';
139            File nf = new File(new String(cc));
140            if (!f.renameTo(nf)) {
141                /*there is a known issue that renameTo fails if
142                  (1)the path is a UNC path and
143                  (2)the path length is bigger than 1092
144                  so don't stop if above are true
145                */
146                String abPath = f.getAbsolutePath();
147                if (!abPath.startsWith("\\\\") ||
148                    abPath.length() < 1093) {
149                    throw new RuntimeException ("File.renameTo() failed for lenth="
150                                                + abPath.length());
151                }
152                return;
153            }
154            if (!nf.canRead())
155                throw new RuntimeException ("Renamed file is not readable");
156            if (!nf.canWrite())
157                throw new RuntimeException ("Renamed file is not writable");
158            if (nf.length() != 1)
159                throw new RuntimeException ("Renamed file's size is not correct");
160            nf.renameTo(f);
161            /* add a script to test these two if we got a regression later
162            if (!f.setReadOnly())
163                throw new RuntimeException ("File.setReadOnly() failed");
164            f.deleteOnExit();
165            */
166        } finally {
167            // Clean up
168            for (int i = 0; i < max; i++) {
169                pathString = created[i];
170                // Only works with completex canonical paths
171                File df = new File(pathString);
172                pathString = df.getCanonicalPath();
173                df = new File(pathString);
174                if (!df.delete())
175                    System.out.printf("Delete failed->%s\n", pathString);
176            }
177        }
178    }
179}
180