8028631: Improve the test coverage to the pathname handling on unix-like platforms
authordxu
Tue, 19 Nov 2013 13:22:50 -0800
changeset 21824 123a828f30dc
parent 21823 2100a5feec29
child 21825 b221b57b93bc
8028631: Improve the test coverage to the pathname handling on unix-like platforms Summary: Add GeneralSolaris.java testcase and fix the concurrency issue Reviewed-by: lancea, chegar, alanb
jdk/test/java/io/pathNames/General.java
jdk/test/java/io/pathNames/GeneralSolaris.java
jdk/test/java/io/pathNames/GeneralWin32.java
--- a/jdk/test/java/io/pathNames/General.java	Tue Nov 19 20:10:58 2013 +0100
+++ b/jdk/test/java/io/pathNames/General.java	Tue Nov 19 13:22:50 2013 -0800
@@ -39,12 +39,40 @@
 
     private static int gensymCounter = 0;
 
+    protected static final String userDir = System.getProperty("user.dir");
+
+    protected static String baseDir = null;
+    protected static String relative = null;
 
     /* Generate a filename unique to this run */
-    protected static String gensym() {
+    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);
+        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
@@ -214,7 +242,7 @@
 
 
     /** Hash table of input pathnames, used to detect duplicates */
-    private static Hashtable checked = new Hashtable();
+    private static Hashtable<String, String> checked = new Hashtable<>();
 
     /**
      * Check the given pathname.  Its canonical pathname should be the given
@@ -272,7 +300,7 @@
 
 
     /** Check a single slash case, plus its children */
-    public static void checkSlash(int depth, boolean create,
+    private static void checkSlash(int depth, boolean create,
                                   String ans, String ask, String slash)
         throws Exception
     {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/io/pathNames/GeneralSolaris.java	Tue Nov 19 13:22:50 2013 -0800
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2013, 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.
+ */
+
+/* @test
+   @bug 4035924 4095767
+   @summary General exhaustive test of solaris pathname handling
+   @author Mark Reinhold
+
+   @build General GeneralSolaris
+   @run main GeneralSolaris
+ */
+
+import java.io.*;
+import java.util.*;
+import java.nio.file.*;
+import java.nio.file.attribute.*;
+
+public class GeneralSolaris extends General {
+
+    private static void checkUnreadable() throws Exception {
+        Path file = Paths.get(baseDir, "unreadableFile");
+        Path dir = Paths.get(baseDir, "unreadableDir");
+        Set<PosixFilePermission> perms = PosixFilePermissions.fromString("---------");
+        FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
+        Files.createFile(file, attr);
+        Files.createDirectory(dir, attr);
+
+        String unreadableFile = file.toString();
+        String unreadableDir = dir.toString();
+
+        checkSlashes(2, false, unreadableDir, unreadableDir);
+        checkSlashes(2, false, unreadableFile, unreadableFile);
+
+        Files.delete(file);
+        Files.delete(dir);
+    }
+
+    private static void checkPaths() throws Exception {
+        // Make sure that an empty relative path is tested
+        checkNames(1, true, userDir + File.separator, "");
+        checkNames(3, true, baseDir + File.separator,
+                   relative + File.separator);
+
+        checkSlashes(2, true, baseDir, baseDir);
+    }
+
+    public static void main(String[] args) throws Exception {
+        if (File.separatorChar != '/') {
+            /* This test is only valid on Unix systems */
+            return;
+        }
+        if (args.length > 0) debug = true;
+
+        initTestData(3);
+        checkUnreadable();
+        checkPaths();
+    }
+}
--- a/jdk/test/java/io/pathNames/GeneralWin32.java	Tue Nov 19 20:10:58 2013 +0100
+++ b/jdk/test/java/io/pathNames/GeneralWin32.java	Tue Nov 19 13:22:50 2013 -0800
@@ -47,29 +47,24 @@
     private static final String EXISTENT_UNC_SHARE = "pcdist";
     private static final String NONEXISTENT_UNC_HOST = "non-existent-unc-host";
     private static final String NONEXISTENT_UNC_SHARE = "bogus-share";
-    private static final int DEPTH = 2;
-    private static String baseDir = null;
-    private static String userDir = null;
-    private static String relative = null;
 
     /* Pathnames relative to working directory */
 
     private static void checkCaseLookup() throws IOException {
         /* Use long names here to avoid 8.3 format, which Samba servers often
            force to lowercase */
-        File d1 = new File(relative, "XyZzY0123");
-        File d2 = new File(d1, "FOO_bar_BAZ");
-        File f = new File(d2, "GLORPified");
+        File d = new File("XyZzY0123", "FOO_bar_BAZ");
+        File f = new File(d, "GLORPified");
         if (!f.exists()) {
-            if (!d2.exists()) {
-                if (!d2.mkdirs()) {
-                    throw new RuntimeException("Can't create directory " + d2);
+            if (!d.exists()) {
+                if (!d.mkdirs()) {
+                    throw new RuntimeException("Can't create directory " + d);
                 }
             }
             OutputStream o = new FileOutputStream(f);
             o.close();
         }
-        File f2 = new File(d2.getParent(), "mumble"); /* For later ud tests */
+        File f2 = new File(d.getParent(), "mumble"); /* For later ud tests */
         if (!f2.exists()) {
             OutputStream o = new FileOutputStream(f2);
             o.close();
@@ -79,9 +74,9 @@
            case of filenames, rather than just using the input case */
         File y = new File(userDir, f.getPath());
         String ans = y.getPath();
-        check(ans, relative + "XyZzY0123\\FOO_bar_BAZ\\GLORPified");
-        check(ans, relative + "xyzzy0123\\foo_bar_baz\\glorpified");
-        check(ans, relative + "XYZZY0123\\FOO_BAR_BAZ\\GLORPIFIED");
+        check(ans, "XyZzY0123\\FOO_bar_BAZ\\GLORPified");
+        check(ans, "xyzzy0123\\foo_bar_baz\\glorpified");
+        check(ans, "XYZZY0123\\FOO_BAR_BAZ\\GLORPIFIED");
     }
 
     private static void checkWild(File f) throws Exception {
@@ -94,16 +89,19 @@
     }
 
     private static void checkWildCards() throws Exception {
-        File d = new File(baseDir).getCanonicalFile();
+        File d = new File(userDir).getCanonicalFile();
         checkWild(new File(d, "*.*"));
         checkWild(new File(d, "*.???"));
         checkWild(new File(new File(d, "*.*"), "foo"));
     }
 
-    private static void checkRelativePaths() throws Exception {
+    private static void checkRelativePaths(int depth) throws Exception {
         checkCaseLookup();
         checkWildCards();
-        checkNames(3, true, baseDir, relative);
+        // Make sure that an empty relative path is tested
+        checkNames(1, true, userDir + File.separator, "");
+        checkNames(depth, true, baseDir + File.separator,
+                   relative + File.separator);
     }
 
 
@@ -135,22 +133,22 @@
         String ans = exists ? df.getAbsolutePath() : d;
         if (!ans.endsWith("\\"))
             ans = ans + "\\";
-        checkNames(depth, false, ans + relative, d + relative);
+        checkNames(depth, false, ans, d);
     }
 
-    private static void checkDrivePaths() throws Exception {
-        checkDrive(2, findActiveDrive(), true);
-        checkDrive(2, findInactiveDrive(), false);
+    private static void checkDrivePaths(int depth) throws Exception {
+        checkDrive(depth, findActiveDrive(), true);
+        checkDrive(depth, findInactiveDrive(), false);
     }
 
 
     /* UNC pathnames */
 
-    private static void checkUncPaths() throws Exception {
+    private static void checkUncPaths(int depth) throws Exception {
         String s = ("\\\\" + NONEXISTENT_UNC_HOST
                     + "\\" + NONEXISTENT_UNC_SHARE);
         ensureNon(s);
-        checkSlashes(DEPTH, false, s, s);
+        checkSlashes(depth, false, s, s);
 
         s = "\\\\" + EXISTENT_UNC_HOST + "\\" + EXISTENT_UNC_SHARE;
         if (!(new File(s)).exists()) {
@@ -159,7 +157,7 @@
             return;
         }
 
-        checkSlashes(DEPTH, false, s, s);
+        checkSlashes(depth, false, s, s);
     }
 
 
@@ -169,34 +167,11 @@
             return;
         }
         if (args.length > 0) debug = true;
-        userDir = System.getProperty("user.dir") + '\\';
-        baseDir = initTestData(6) + '\\';
-        relative = baseDir.substring(userDir.length());
-        checkRelativePaths();
-        checkDrivePaths();
-        checkUncPaths();
-    }
+
+        initTestData(3);
 
-    private static String initTestData(int maxDepth) throws IOException {
-        File parent = new File(userDir);
-        String baseDir = null;
-        maxDepth = maxDepth < DEPTH + 2 ? DEPTH + 2 : maxDepth;
-        for (int i = 0; i < maxDepth; i ++) {
-            File dir1 = new File(parent, gensym());
-            dir1.mkdir();
-            if (i != 0) {
-                File dir2 = new File(parent, gensym());
-                dir2.mkdir();
-                File f1 = new File(parent, gensym());
-                f1.createNewFile();
-                File f2 = new File(parent, gensym());
-                f2.createNewFile();
-            }
-            if (i == DEPTH + 1) {
-                baseDir = dir1.getAbsolutePath();
-            }
-            parent = dir1;
-        }
-        return baseDir;
+        checkRelativePaths(3);
+        checkDrivePaths(2);
+        checkUncPaths(2);
     }
 }