1 /*
2 * Copyright (c) 1998, 2013, 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 /*
25 @summary Common definitions for general exhaustive pathname tests
26 @author Mark Reinhold
27 */
28
29 import java.io.*;
30 import java.util.*;
31 import java.nio.file.*;
32
33
34 public class General {
35
36 public static boolean debug = false;
37
38 private static boolean win32 = (File.separatorChar == '\\');
39
40 private static int gensymCounter = 0;
41
42 protected static final String userDir = System.getProperty("user.dir");
43
44 protected static String baseDir = null;
45 protected static String relative = null;
46
47 /* Generate a filename unique to this run */
48 private static String gensym() {
49 return "x." + ++gensymCounter;
50 }
51
52 /**
53 * Create files and folders in the test working directory.
54 * The purpose is to make sure the test will not go out of
55 * its user dir when walking the file tree.
56 *
57 * @param depth The number of directory levels to be created under
58 * the user directory. It should be the maximum value
59 * of the depths passed to checkNames method (including
60 * direct or indirect calling) in a whole test.
61 */
62 protected static void initTestData(int depth) throws IOException {
320 if (depth == 0) return;
321
322 checkSlash(depth, create, ans, ask, "/");
323 checkSlash(depth, create, ans, ask, "//");
324 checkSlash(depth, create, ans, ask, "///");
325 if (win32) {
326 checkSlash(depth, create, ans, ask, "\\");
327 checkSlash(depth, create, ans, ask, "\\\\");
328 checkSlash(depth, create, ans, ask, "\\/");
329 checkSlash(depth, create, ans, ask, "/\\");
330 checkSlash(depth, create, ans, ask, "\\\\\\");
331 }
332 }
333
334
335 /** Check name cases for the given ask string */
336 public static void checkNames(int depth, boolean create,
337 String ans, String ask)
338 throws Exception
339 {
340 int d = depth - 1;
341 File f = new File(ans);
342 String n;
343
344 /* Normal name */
345 if (f.exists()) {
346 if (Files.isDirectory(f.toPath(), LinkOption.NOFOLLOW_LINKS) && f.list() != null) {
347 if ((n = findSomeFile(ans, create)) != null)
348 checkSlashes(d, create, ans + n, ask + n);
349 if ((n = findSomeDir(ans, create)) != null)
350 checkSlashes(d, create, ans + n, ask + n);
351 }
352 n = findNon(ans);
353 checkSlashes(d, create, ans + n, ask + n);
354 } else {
355 n = "foo" + depth;
356 checkSlashes(d, create, ans + n, ask + n);
357 }
358
359 /* "." */
|
1 /*
2 * Copyright (c) 1998, 2016, 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 /*
25 @summary Common definitions for general exhaustive pathname tests
26 @author Mark Reinhold
27 */
28
29 import java.io.*;
30 import java.util.*;
31 import java.nio.file.*;
32
33
34 public class General {
35
36 public static boolean debug = false;
37
38 private static boolean win32 = (File.separatorChar == '\\');
39
40 private static int gensymCounter = 0;
41
42 protected static final String userDir = System.getProperty("user.dir");
43 private static final Path userDirPath = Paths.get(userDir).toAbsolutePath();
44
45 protected static String baseDir = null;
46 protected static String relative = null;
47
48 /* Generate a filename unique to this run */
49 private static String gensym() {
50 return "x." + ++gensymCounter;
51 }
52
53 /**
54 * Create files and folders in the test working directory.
55 * The purpose is to make sure the test will not go out of
56 * its user dir when walking the file tree.
57 *
58 * @param depth The number of directory levels to be created under
59 * the user directory. It should be the maximum value
60 * of the depths passed to checkNames method (including
61 * direct or indirect calling) in a whole test.
62 */
63 protected static void initTestData(int depth) throws IOException {
321 if (depth == 0) return;
322
323 checkSlash(depth, create, ans, ask, "/");
324 checkSlash(depth, create, ans, ask, "//");
325 checkSlash(depth, create, ans, ask, "///");
326 if (win32) {
327 checkSlash(depth, create, ans, ask, "\\");
328 checkSlash(depth, create, ans, ask, "\\\\");
329 checkSlash(depth, create, ans, ask, "\\/");
330 checkSlash(depth, create, ans, ask, "/\\");
331 checkSlash(depth, create, ans, ask, "\\\\\\");
332 }
333 }
334
335
336 /** Check name cases for the given ask string */
337 public static void checkNames(int depth, boolean create,
338 String ans, String ask)
339 throws Exception
340 {
341 if (!Paths.get(ans).toAbsolutePath().startsWith(userDirPath)) {
342 return;
343 }
344
345 int d = depth - 1;
346 File f = new File(ans);
347 String n;
348
349 /* Normal name */
350 if (f.exists()) {
351 if (Files.isDirectory(f.toPath(), LinkOption.NOFOLLOW_LINKS) && f.list() != null) {
352 if ((n = findSomeFile(ans, create)) != null)
353 checkSlashes(d, create, ans + n, ask + n);
354 if ((n = findSomeDir(ans, create)) != null)
355 checkSlashes(d, create, ans + n, ask + n);
356 }
357 n = findNon(ans);
358 checkSlashes(d, create, ans + n, ask + n);
359 } else {
360 n = "foo" + depth;
361 checkSlashes(d, create, ans + n, ask + n);
362 }
363
364 /* "." */
|