PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/jdk/test/java/io/File/MaxPathLength.java

#
Java | 220 lines | 165 code | 17 blank | 38 comment | 46 complexity | 1c2300b4bd9723b620bd23b31b53b1b1 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  1. /*
  2. * Copyright (c) 2002, 2005, 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. /* @test
  24. @bug 4759207 4403166 4165006 4403166 6182812 6274272
  25. @summary Test to see if win32 path length can be greater than 260
  26. */
  27. import java.io.*;
  28. public class MaxPathLength {
  29. private static String sep = File.separator;
  30. private static String pathComponent = sep +
  31. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  32. private static String fileName =
  33. "areallylongfilenamethatsforsur";
  34. private static boolean isWindows = false;
  35. private static long totalSpace;
  36. private static long freeSpace;
  37. private static long usableSpace;
  38. private static long ONEMEGA = 1024*1024;
  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. if (osName.startsWith("Windows 9") ||
  44. osName.startsWith("Windows Me"))
  45. return; // win9x/Me cannot handle long paths
  46. }
  47. if (osName.startsWith("SunOS")) {
  48. return; // We don't run this test on Solaris either.
  49. // Some Solaris machines have very "slow" disk
  50. // access performance which causes this one
  51. // to timeout.
  52. }
  53. if (isWindows) {
  54. File f = new File(".");
  55. totalSpace = f.getTotalSpace()/ONEMEGA;
  56. freeSpace = f.getFreeSpace()/ONEMEGA;
  57. usableSpace = f.getUsableSpace()/ONEMEGA;
  58. }
  59. for (int i = 4; i < 7; i++) {
  60. String name = fileName;
  61. while (name.length() < 256) {
  62. testLongPath (i, name, false);
  63. testLongPath (i, name, true);
  64. name += "A";
  65. }
  66. }
  67. // Testing below will not be run if "-extra" is not
  68. if (args.length == 0 ||
  69. !"-extra".equals(args[0]) ||
  70. !isWindows)
  71. return;
  72. /* Testings below should not be run on a remote
  73. dir that exists on a Solaris machine */
  74. for (int i = 20; i < 21; i++) {
  75. String name = fileName;
  76. while (name.length() < 256) {
  77. testLongPath (i, name, false);
  78. testLongPath (i, name, true);
  79. name += "A";
  80. }
  81. }
  82. }
  83. private static int lastMax = 0;
  84. static void testLongPath(int max, String fn,
  85. boolean tryAbsolute) throws Exception {
  86. String[] created = new String[max];
  87. String pathString = ".";
  88. for (int i = 0; i < max -1; i++) {
  89. pathString = pathString + pathComponent;
  90. created[max - 1 -i] = pathString;
  91. }
  92. File dirFile = new File(pathString);
  93. File f = new File(pathString + sep + fn);
  94. String tPath = f.getPath();
  95. if (tryAbsolute) {
  96. tPath = f.getCanonicalPath();
  97. }
  98. created[0] = tPath;
  99. //for getCanonicalPath testing on win32
  100. File fu = new File(pathString + sep + fn.toUpperCase());
  101. if (dirFile.exists()) {
  102. System.err.println("Warning: Test directory structure exists already!");
  103. return;
  104. }
  105. boolean couldMakeTestDirectory = dirFile.mkdirs();
  106. if (!couldMakeTestDirectory) {
  107. throw new RuntimeException ("Could not create test directory structure");
  108. }
  109. try {
  110. if (tryAbsolute)
  111. dirFile = new File(dirFile.getCanonicalPath());
  112. if (!dirFile.isDirectory())
  113. throw new RuntimeException ("File.isDirectory() failed");
  114. if (isWindows && lastMax != max) {
  115. long diff = totalSpace - dirFile.getTotalSpace()/ONEMEGA;
  116. if (diff < -5 || diff > 5)
  117. throw new RuntimeException ("File.getTotalSpace() failed");
  118. diff = freeSpace - dirFile.getFreeSpace()/ONEMEGA;
  119. if (diff < -5 || diff > 5)
  120. throw new RuntimeException ("File.getFreeSpace() failed");
  121. diff = usableSpace - dirFile.getUsableSpace()/ONEMEGA;
  122. if (diff < -5 || diff > 5)
  123. throw new RuntimeException ("File.getUsableSpace() failed");
  124. lastMax = max;
  125. }
  126. f = new File(tPath);
  127. if (!f.createNewFile()) {
  128. throw new RuntimeException ("File.createNewFile() failed");
  129. }
  130. if (!f.exists())
  131. throw new RuntimeException ("File.exists() failed");
  132. if (!f.isFile())
  133. throw new RuntimeException ("File.isFile() failed");
  134. if (!f.canRead())
  135. throw new RuntimeException ("File.canRead() failed");
  136. if (!f.canWrite())
  137. throw new RuntimeException ("File.canWrite() failed");
  138. if (!f.delete())
  139. throw new RuntimeException ("File.delete() failed");
  140. FileOutputStream fos = new FileOutputStream(f);
  141. fos.write(1);
  142. fos.close();
  143. if (f.length() != 1)
  144. throw new RuntimeException ("File.length() failed");
  145. long time = System.currentTimeMillis();
  146. if (!f.setLastModified(time))
  147. throw new RuntimeException ("File.setLastModified() failed");
  148. if (f.lastModified() == 0) {
  149. throw new RuntimeException ("File.lastModified() failed");
  150. }
  151. String[] list = dirFile.list();
  152. if (list == null || !fn.equals(list[0])) {
  153. throw new RuntimeException ("File.list() failed");
  154. }
  155. File[] flist = dirFile.listFiles();
  156. if (flist == null || !fn.equals(flist[0].getName()))
  157. throw new RuntimeException ("File.listFiles() failed");
  158. if (isWindows &&
  159. !fu.getCanonicalPath().equals(f.getCanonicalPath()))
  160. throw new RuntimeException ("getCanonicalPath() failed");
  161. char[] cc = tPath.toCharArray();
  162. cc[cc.length-1] = 'B';
  163. File nf = new File(new String(cc));
  164. if (!f.renameTo(nf)) {
  165. /*there is a known issue that renameTo fails if
  166. (1)the path is a UNC path and
  167. (2)the path length is bigger than 1092
  168. so don't stop if above are true
  169. */
  170. String abPath = f.getAbsolutePath();
  171. if (!abPath.startsWith("\\\\") ||
  172. abPath.length() < 1093) {
  173. throw new RuntimeException ("File.renameTo() failed for lenth="
  174. + abPath.length());
  175. }
  176. return;
  177. }
  178. if (!nf.canRead())
  179. throw new RuntimeException ("Renamed file is not readable");
  180. if (!nf.canWrite())
  181. throw new RuntimeException ("Renamed file is not writable");
  182. if (nf.length() != 1)
  183. throw new RuntimeException ("Renamed file's size is not correct");
  184. nf.renameTo(f);
  185. /* add a script to test these two if we got a regression later
  186. if (!f.setReadOnly())
  187. throw new RuntimeException ("File.setReadOnly() failed");
  188. f.deleteOnExit();
  189. */
  190. } finally {
  191. // Clean up
  192. for (int i = 0; i < max; i++) {
  193. pathString = created[i];
  194. // Only works with completex canonical paths
  195. File df = new File(pathString);
  196. pathString = df.getCanonicalPath();
  197. df = new File(pathString);
  198. if (!df.delete())
  199. System.out.printf("Delete failed->%s\n", pathString);
  200. }
  201. }
  202. }
  203. }