blob: 780426d15cca8b02c59efddbe292d79276946bba [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-2000 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 @summary Common definitions for general exhaustive pathname tests
26 @author Mark Reinhold
27 */
28
29import java.io.*;
30import java.util.*;
31
32
33public class General {
34
35 public static boolean debug = false;
36
37 private static boolean win32 = (File.separatorChar == '\\');
38
39 private static int gensymCounter = 0;
40
41
42 /* Generate a filename unique to this run */
43 private static String gensym() {
44 return "x." + ++gensymCounter;
45 }
46
47
48 /**
49 * Find a file in the given subdirectory, or descend into further
50 * subdirectories, if any, if no file is found here. Return null if no
51 * file can be found anywhere beneath the given subdirectory.
52 * @param dir Directory at which we started
53 * @param subdir Subdirectory that we're exploring
54 * @param dl Listing of subdirectory
55 */
56 private static String findSomeFile(String dir, String subdir, String[] dl) {
57 for (int i = 0; i < dl.length; i++) {
58 File f = new File(subdir, dl[i]);
59 File df = new File(dir, f.getPath());
60 if (df.exists() && df.isFile()) {
61 return f.getPath();
62 }
63 }
64 for (int i = 0; i < dl.length; i++) {
65 File f = (subdir.length() == 0) ? new File(dl[i])
66 : new File(subdir, dl[i]);
67 File df = new File(dir, f.getPath());
68 if (df.exists() && df.isDirectory()) {
69 String[] dl2 = df.list();
70 if (dl2 != null) {
71 String ff = findSomeFile(dir, f.getPath(), dl2);
72 if (ff != null) return ff;
73 }
74 }
75 }
76 return null;
77 }
78
79
80 /**
81 * Construct a string that names a file in the given directory. If create
82 * is true, then create a file if none is found, and throw an exception if
83 * that is not possible; otherwise, return null if no file can be found.
84 */
85 private static String findSomeFile(String dir, boolean create) {
86 File d = new File(dir);
87 String[] dl = d.list();
88 if (dl == null) {
89 throw new RuntimeException("Can't list " + dir);
90 }
91 for (int i = 0; i < dl.length; i++) {
92 File f = new File(dir, dl[i]);
93 if (f.isFile()) {
94 return dl[i];
95 }
96 }
97 String f = findSomeFile(dir, "", dl);
98 if (f != null) {
99 return f;
100 }
101 if (create) {
102 File nf = new File(d, gensym());
103 OutputStream os;
104 try {
105 os = new FileOutputStream(nf);
106 os.close();
107 } catch (IOException x) {
108 throw new RuntimeException("Can't create a file in " + dir);
109 }
110 return nf.getName();
111 }
112 return null;
113 }
114
115
116 /**
117 * Construct a string that names a subdirectory of the given directory.
118 * If create is true, then create a subdirectory if none is found, and
119 * throw an exception if that is not possible; otherwise, return null if
120 * no subdirectory can be found.
121 */
122 private static String findSomeDir(String dir, boolean create) {
123 File d = new File(dir);
124 String[] dl = d.list();
125 if (dl == null) {
126 throw new RuntimeException("Can't list " + dir);
127 }
128 for (int i = 0; i < dl.length; i++) {
129 File f = new File(d, dl[i]);
130 if (f.isDirectory() && f.canRead()) {
131 String[] dl2 = f.list();
132 if (dl2.length >= 250) {
133 /* Heuristic to avoid scanning huge directories */
134 continue;
135 }
136 return dl[i];
137 }
138 }
139 if (create) {
140 File sd = new File(d, gensym());
141 if (sd.mkdir()) return sd.getName();
142 }
143 return null;
144 }
145
146
147 /** Construct a string that does not name a file in the given directory */
148 private static String findNon(String dir) {
149 File d = new File(dir);
150 String[] x = new String[] { "foo", "bar", "baz" };
151 for (int i = 0; i < x.length; i++) {
152 File f = new File(d, x[i]);
153 if (!f.exists()) {
154 return x[i];
155 }
156 }
157 for (int i = 0; i < 1024; i++) {
158 String n = "xx" + Integer.toString(i);
159 File f = new File(d, n);
160 if (!f.exists()) {
161 return n;
162 }
163 }
164 throw new RuntimeException("Can't find a non-existent file in " + dir);
165 }
166
167
168 /** Ensure that the named file does not exist */
169 public static void ensureNon(String fn) {
170 if ((new File(fn)).exists()) {
171 throw new RuntimeException("Test path " + fn + " exists");
172 }
173 }
174
175
176 /** Tell whether the given character is a "slash" on this platform */
177 private static boolean isSlash(char x) {
178 if (x == File.separatorChar) return true;
179 if (win32 && (x == '/')) return true;
180 return false;
181 }
182
183
184 /**
185 * Trim trailing slashes from the given string, but leave singleton slashes
186 * alone (they denote root directories)
187 */
188 private static String trimTrailingSlashes(String s) {
189 int n = s.length();
190 if (n == 0) return s;
191 n--;
192 while ((n > 0) && isSlash(s.charAt(n))) {
193 if ((n >= 1) && s.charAt(n - 1) == ':') break;
194 n--;
195 }
196 return s.substring(0, n + 1);
197 }
198
199
200 /** Concatenate two paths, trimming slashes as needed */
201 private static String pathConcat(String a, String b) {
202 if (a.length() == 0) return b;
203 if (b.length() == 0) return a;
204 if (isSlash(a.charAt(a.length() - 1))
205 || isSlash(b.charAt(0))
206 || (win32 && (a.charAt(a.length() - 1) == ':'))) {
207 return a + b;
208 } else {
209 return a + File.separatorChar + b;
210 }
211 }
212
213
214
215 /** Hash table of input pathnames, used to detect duplicates */
216 private static Hashtable checked = new Hashtable();
217
218 /**
219 * Check the given pathname. Its canonical pathname should be the given
220 * answer. If the path names a file that exists and is readable, then
221 * FileInputStream and RandomAccessFile should both be able to open it.
222 */
223 public static void check(String answer, String path) throws IOException {
224 String ans = trimTrailingSlashes(answer);
225 if (path.length() == 0) return;
226 if (checked.get(path) != null) {
227 System.err.println("DUP " + path);
228 return;
229 }
230 checked.put(path, path);
231
232 String cpath;
233 try {
234 File f = new File(path);
235 cpath = f.getCanonicalPath();
236 if (f.exists() && f.isFile() && f.canRead()) {
237 InputStream in = new FileInputStream(path);
238 in.close();
239 RandomAccessFile raf = new RandomAccessFile(path, "r");
240 raf.close();
241 }
242 } catch (IOException x) {
243 System.err.println(ans + " <-- " + path + " ==> " + x);
244 if (debug) return;
245 else throw x;
246 }
247 if (cpath.equals(ans)) {
248 System.err.println(ans + " <== " + path);
249 } else {
250 System.err.println(ans + " <-- " + path + " ==> " + cpath + " MISMATCH");
251 if (!debug) {
252 throw new RuntimeException("Mismatch: " + path + " ==> " + cpath +
253 ", should be " + ans);
254 }
255 }
256 }
257
258
259
260 /*
261 * The following three mutually-recursive methods generate and check a tree
262 * of filenames of arbitrary depth. Each method has (at least) these
263 * arguments:
264 *
265 * int depth Remaining tree depth
266 * boolean create Controls whether test files and directories
267 * will be created as needed
268 * String ans Expected answer for the check method (above)
269 * String ask Input pathname to be passed to the check method
270 */
271
272
273 /** Check a single slash case, plus its children */
274 public static void checkSlash(int depth, boolean create,
275 String ans, String ask, String slash)
276 throws Exception
277 {
278 check(ans, ask + slash);
279 checkNames(depth, create,
280 ans.endsWith(File.separator) ? ans : ans + File.separator,
281 ask + slash);
282 }
283
284
285 /** Check slash cases for the given ask string */
286 public static void checkSlashes(int depth, boolean create,
287 String ans, String ask)
288 throws Exception
289 {
290 check(ans, ask);
291 if (depth == 0) return;
292
293 checkSlash(depth, create, ans, ask, "/");
294 checkSlash(depth, create, ans, ask, "//");
295 checkSlash(depth, create, ans, ask, "///");
296 if (win32) {
297 checkSlash(depth, create, ans, ask, "\\");
298 checkSlash(depth, create, ans, ask, "\\\\");
299 checkSlash(depth, create, ans, ask, "\\/");
300 checkSlash(depth, create, ans, ask, "/\\");
301 checkSlash(depth, create, ans, ask, "\\\\\\");
302 }
303 }
304
305
306 /** Check name cases for the given ask string */
307 public static void checkNames(int depth, boolean create,
308 String ans, String ask)
309 throws Exception
310 {
311 int d = depth - 1;
312 File f = new File(ans);
313 String n;
314
315 /* Normal name */
316 if (f.exists()) {
317 if (f.isDirectory() && f.canRead()) {
318 if ((n = findSomeFile(ans, create)) != null)
319 checkSlashes(d, create, ans + n, ask + n);
320 if ((n = findSomeDir(ans, create)) != null)
321 checkSlashes(d, create, ans + n, ask + n);
322 }
323 n = findNon(ans);
324 checkSlashes(d, create, ans + n, ask + n);
325 } else {
326 n = "foo" + depth;
327 checkSlashes(d, create, ans + n, ask + n);
328 }
329
330 /* "." */
331 checkSlashes(d, create, trimTrailingSlashes(ans), ask + ".");
332
333 /* ".." */
334 if ((n = f.getParent()) != null) {
335 String n2;
336 if (win32
337 && ((n2 = f.getParentFile().getParent()) != null)
338 && n2.equals("\\\\")) {
339 /* Win32 resolves \\foo\bar\.. to \\foo\bar */
340 checkSlashes(d, create, ans, ask + "..");
341 } else {
342 checkSlashes(d, create, n, ask + "..");
343 }
344 }
345 else {
346 if (win32)
347 checkSlashes(d, create, ans, ask + "..");
348 else {
349 // Fix for 4237875. We must ensure that we are sufficiently
350 // deep in the path hierarchy to test parents this high up
351 File thisPath = new File(ask);
352 File nextPath = new File(ask + "..");
353 if (!thisPath.getCanonicalPath().equals(nextPath.getCanonicalPath()))
354 checkSlashes(d, create, ans + "..", ask + "..");
355 }
356 }
357 }
358}