1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/*
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@summary Common definitions for general exhaustive pathname tests
@author Mark Reinhold
*/
import java.io.*;
import java.util.*;
import java.nio.file.*;
public class General {
public static boolean debug = false;
private static boolean win32 = (File.separatorChar == '\\');
private static int gensymCounter = 0;
protected static final String userDir = System.getProperty("user.dir");
protected static final String workSubDir = "tmp";
protected static String baseDir = null;
protected static String relative = null;
/* Generate a filename unique to this run */
private static String gensym() {
return "x." + ++gensymCounter;
}
/**
* Create files and folders in the test working directory.
* The purpose is to make sure the test will not go out of
* its user dir when walking the file tree.
*
* @param depth The number of directory levels to be created under
* the user directory. It should be the maximum value
* of the depths passed to checkNames method (including
* direct or indirect calling) in a whole test.
*/
protected static void initTestData(int depth) throws IOException {
File parent = new File(userDir + File.separator + workSubDir);
if (!parent.mkdir()) {
throw new IOException("Fail to create directory: " + parent);
}
for (int i = 0; i < depth; i++) {
File tmp = new File(parent, gensym());
tmp.createNewFile();
tmp = new File(parent, gensym());
if (tmp.mkdir())
parent = tmp;
else
throw new IOException("Fail to create directory, " + tmp);
}
baseDir = parent.getAbsolutePath();
relative = baseDir.substring(userDir.length() + 1);
}
/**
* Find a file in the given subdirectory, or descend into further
* subdirectories, if any, if no file is found here. Return null if no
* file can be found anywhere beneath the given subdirectory.
* @param dir Directory at which we started
* @param subdir Subdirectory that we're exploring
* @param dl Listing of subdirectory
*/
private static String findSomeFile(String dir, String subdir, String[] dl) {
for (int i = 0; i < dl.length; i++) {
File f = new File(subdir, dl[i]);
File df = new File(dir, f.getPath());
if (Files.isRegularFile(df.toPath(), LinkOption.NOFOLLOW_LINKS)) {
return f.getPath();
}
}
for (int i = 0; i < dl.length; i++) {
File f = (subdir.length() == 0) ? new File(dl[i])
: new File(subdir, dl[i]);
File df = new File(dir, f.getPath());
if (Files.isDirectory(df.toPath(), LinkOption.NOFOLLOW_LINKS)) {
String[] dl2 = df.list();
if (dl2 != null) {
String ff = findSomeFile(dir, f.getPath(), dl2);
if (ff != null) return ff;
}
}
}
return null;
}
/**
* Construct a string that names a file in the given directory. If create
* is true, then create a file if none is found, and throw an exception if
* that is not possible; otherwise, return null if no file can be found.
*/
private static String findSomeFile(String dir, boolean create) {
File d = new File(dir);
String[] dl = d.list();
if (dl == null) {
throw new RuntimeException("Can't list " + dir);
}
for (int i = 0; i < dl.length; i++) {
File f = new File(dir, dl[i]);
if (Files.isRegularFile(f.toPath(), LinkOption.NOFOLLOW_LINKS)) {
return dl[i];
}
}
String f = findSomeFile(dir, "", dl);
if (f != null) {
return f;
}
if (create) {
File nf = new File(d, gensym());
OutputStream os;
try {
os = new FileOutputStream(nf);
os.close();
} catch (IOException x) {
throw new RuntimeException("Can't create a file in " + dir);
}
return nf.getName();
}
return null;
}
/**
* Construct a string that names a subdirectory of the given directory.
* If create is true, then create a subdirectory if none is found, and
* throw an exception if that is not possible; otherwise, return null if
* no subdirectory can be found.
*/
private static String findSomeDir(String dir, boolean create) {
File d = new File(dir);
String[] dl = d.list();
if (dl == null) {
throw new RuntimeException("Can't list " + dir);
}
for (int i = 0; i < dl.length; i++) {
File f = new File(d, dl[i]);
if (Files.isDirectory(f.toPath(), LinkOption.NOFOLLOW_LINKS)) {
String[] dl2 = f.list();
if (dl2 == null || dl2.length >= 250) {
/* Heuristic to avoid scanning huge directories */
continue;
}
return dl[i];
}
}
if (create) {
File sd = new File(d, gensym());
if (sd.mkdir()) return sd.getName();
}
return null;
}
/** Construct a string that does not name a file in the given directory */
private static String findNon(String dir) {
File d = new File(dir);
String[] x = new String[] { "foo", "bar", "baz" };
for (int i = 0; i < x.length; i++) {
File f = new File(d, x[i]);
if (!f.exists()) {
return x[i];
}
}
for (int i = 0; i < 1024; i++) {
String n = "xx" + Integer.toString(i);
File f = new File(d, n);
if (!f.exists()) {
return n;
}
}
throw new RuntimeException("Can't find a non-existent file in " + dir);
}
/** Ensure that the named file does not exist */
public static void ensureNon(String fn) {
if ((new File(fn)).exists()) {
throw new RuntimeException("Test path " + fn + " exists");
}
}
/** Tell whether the given character is a "slash" on this platform */
private static boolean isSlash(char x) {
if (x == File.separatorChar) return true;
if (win32 && (x == '/')) return true;
return false;
}
/**
* Trim trailing slashes from the given string, but leave singleton slashes
* alone (they denote root directories)
*/
private static String trimTrailingSlashes(String s) {
int n = s.length();
if (n == 0) return s;
n--;
while ((n > 0) && isSlash(s.charAt(n))) {
if ((n >= 1) && s.charAt(n - 1) == ':') break;
n--;
}
return s.substring(0, n + 1);
}
/** Concatenate two paths, trimming slashes as needed */
private static String pathConcat(String a, String b) {
if (a.length() == 0) return b;
if (b.length() == 0) return a;
if (isSlash(a.charAt(a.length() - 1))
|| isSlash(b.charAt(0))
|| (win32 && (a.charAt(a.length() - 1) == ':'))) {
return a + b;
} else {
return a + File.separatorChar + b;
}
}
/** Hash table of input pathnames, used to detect duplicates */
private static Hashtable<String, String> checked = new Hashtable<>();
/**
* Check the given pathname. Its canonical pathname should be the given
* answer. If the path names a file that exists and is readable, then
* FileInputStream and RandomAccessFile should both be able to open it.
*/
public static void check(String answer, String path) throws IOException {
String ans = trimTrailingSlashes(answer);
if (path.length() == 0) return;
if (checked.get(path) != null) {
System.err.println("DUP " + path);
return;
}
checked.put(path, path);
String cpath;
try {
File f = new File(path);
cpath = f.getCanonicalPath();
if (f.exists() && f.isFile() && f.canRead()) {
InputStream in = new FileInputStream(path);
in.close();
RandomAccessFile raf = new RandomAccessFile(path, "r");
raf.close();
}
} catch (IOException x) {
System.err.println(ans + " <-- " + path + " ==> " + x);
if (debug) return;
else throw x;
}
if (cpath.equals(ans)) {
System.err.println(ans + " <== " + path);
} else {
System.err.println(ans + " <-- " + path + " ==> " + cpath + " MISMATCH");
if (!debug) {
throw new RuntimeException("Mismatch: " + path + " ==> " + cpath +
", should be " + ans);
}
}
}
/*
* The following three mutually-recursive methods generate and check a tree
* of filenames of arbitrary depth. Each method has (at least) these
* arguments:
*
* int depth Remaining tree depth
* boolean create Controls whether test files and directories
* will be created as needed
* String ans Expected answer for the check method (above)
* String ask Input pathname to be passed to the check method
*/
/** Check a single slash case, plus its children */
private static void checkSlash(int depth, boolean create,
String ans, String ask, String slash)
throws Exception
{
check(ans, ask + slash);
checkNames(depth, create,
ans.endsWith(File.separator) ? ans : ans + File.separator,
ask + slash);
}
/** Check slash cases for the given ask string */
public static void checkSlashes(int depth, boolean create,
String ans, String ask)
throws Exception
{
check(ans, ask);
if (depth == 0) return;
checkSlash(depth, create, ans, ask, "/");
checkSlash(depth, create, ans, ask, "//");
checkSlash(depth, create, ans, ask, "///");
if (win32) {
checkSlash(depth, create, ans, ask, "\\");
checkSlash(depth, create, ans, ask, "\\\\");
checkSlash(depth, create, ans, ask, "\\/");
checkSlash(depth, create, ans, ask, "/\\");
checkSlash(depth, create, ans, ask, "\\\\\\");
}
}
/** Check name cases for the given ask string */
public static void checkNames(int depth, boolean create,
String ans, String ask)
throws Exception
{
int d = depth - 1;
File f = new File(ans);
String n;
/* Normal name */
if (f.exists()) {
if (Files.isDirectory(f.toPath(), LinkOption.NOFOLLOW_LINKS) && f.list() != null) {
if ((n = findSomeFile(ans, create)) != null)
checkSlashes(d, create, ans + n, ask + n);
if ((n = findSomeDir(ans, create)) != null)
checkSlashes(d, create, ans + n, ask + n);
}
n = findNon(ans);
checkSlashes(d, create, ans + n, ask + n);
} else {
n = "foo" + depth;
checkSlashes(d, create, ans + n, ask + n);
}
/* "." */
checkSlashes(d, create, trimTrailingSlashes(ans), ask + ".");
/* ".." */
if ((n = f.getParent()) != null) {
String n2;
if (win32
&& ((n2 = f.getParentFile().getParent()) != null)
&& n2.equals("\\\\")) {
/* Win32 resolves \\foo\bar\.. to \\foo\bar */
checkSlashes(d, create, ans, ask + "..");
} else {
checkSlashes(d, create, n, ask + "..");
}
}
else {
if (win32)
checkSlashes(d, create, ans, ask + "..");
else {
// Fix for 4237875. We must ensure that we are sufficiently
// deep in the path hierarchy to test parents this high up
File thisPath = new File(ask);
File nextPath = new File(ask + "..");
if (!thisPath.getCanonicalPath().equals(nextPath.getCanonicalPath()))
checkSlashes(d, create, ans + "..", ask + "..");
}
}
}
}
|