test/java/io/pathNames/GeneralWin32.java

Print this page




  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 /* @test
  25    @bug 4032066 4039597 4046914 4054511 4065189 4109131 4875229 6983520
  26    @summary General exhaustive test of win32 pathname handling
  27    @author Mark Reinhold
  28 
  29    @build General GeneralWin32
  30    @run main/timeout=600 GeneralWin32
  31  */
  32 
  33 import java.io.*;
  34 import java.util.*;
  35 
  36 
  37 public class GeneralWin32 extends General {
  38 
  39 
  40     /**
  41      * Hardwired UNC pathnames used for testing
  42      *
  43      * This test attempts to use the host and share names defined in this class
  44      * to test UNC pathnames.  The test will not fail if the host or share
  45      * don't exist, but it will print a warning saying that it was unable to
  46      * test UNC pathnames completely.
  47      */
  48     private static final String EXISTENT_UNC_HOST = "pc-cup01";
  49     private static final String EXISTENT_UNC_SHARE = "pcdist";
  50     private static final String NONEXISTENT_UNC_HOST = "non-existent-unc-host";
  51     private static final String NONEXISTENT_UNC_SHARE = "bogus-share";
  52 


  53 
  54     /* Pathnames relative to working directory */
  55 
  56     private static void checkCaseLookup(String ud) throws IOException {
  57         /* Use long names here to avoid 8.3 format, which Samba servers often
  58            force to lowercase */
  59         File d = new File("XyZzY0123", "FOO_bar_BAZ");
  60         File f = new File(d, "GLORPified");


  61         if (!f.exists()) {
  62             if (!d.exists()) {
  63                 if (!d.mkdirs()) {
  64                     throw new RuntimeException("Can't create directory " + d);
  65                 }
  66             }
  67             OutputStream o = new FileOutputStream(f);
  68             o.close();
  69         }
  70         File f2 = new File(d.getParent(), "mumble"); /* For later ud tests */
  71         if (!f2.exists()) {
  72             OutputStream o = new FileOutputStream(f2);
  73             o.close();
  74         }
  75 
  76         /* Computing the canonical path of a Win32 file should expose the true
  77            case of filenames, rather than just using the input case */
  78         File y = new File(ud, f.getPath());
  79         String ans = y.getPath();
  80         check(ans, "XyZzY0123\\FOO_bar_BAZ\\GLORPified");
  81         check(ans, "xyzzy0123\\foo_bar_baz\\glorpified");
  82         check(ans, "XYZZY0123\\FOO_BAR_BAZ\\GLORPIFIED");
  83     }
  84 
  85     private static void checkWild(File f) throws Exception {
  86         try {
  87             f.getCanonicalPath();
  88         } catch (IOException x) {
  89             return;
  90         }
  91         throw new Exception("Wildcard path not rejected: " + f);
  92     }
  93 
  94     private static void checkWildCards(String ud) throws Exception {
  95         File d = new File(ud).getCanonicalFile();
  96         checkWild(new File(d, "*.*"));
  97         checkWild(new File(d, "*.???"));
  98         checkWild(new File(new File(d, "*.*"), "foo"));
  99     }
 100 
 101     private static void checkRelativePaths() throws Exception {
 102         String ud = System.getProperty("user.dir").replace('/', '\\');
 103         checkCaseLookup(ud);
 104         checkWildCards(ud);
 105         checkNames(3, true, ud + "\\", "");
 106     }
 107 
 108 
 109     /* Pathnames with drive specifiers */
 110 
 111     private static char findInactiveDrive() {
 112         for (char d = 'Z'; d >= 'E'; d--) {
 113             File df = new File(d + ":\\");
 114             if (!df.exists()) {
 115                 return d;
 116             }
 117         }
 118         throw new RuntimeException("Can't find an inactive drive");
 119     }
 120 
 121     private static char findActiveDrive() {
 122         for (char d = 'C'; d <= 'Z'; d--) {
 123             File df = new File(d + ":\\");
 124             if (df.exists()) return d;
 125         }
 126         throw new RuntimeException("Can't find an active drive");
 127     }
 128 
 129     private static void checkDrive(int depth, char drive, boolean exists)
 130         throws Exception
 131     {
 132         String d = drive + ":";
 133         File df = new File(d);
 134         String ans = exists ? df.getAbsolutePath() : d;
 135         if (!ans.endsWith("\\"))
 136             ans = ans + "\\";
 137         checkNames(depth, false, ans, d);

 138     }
 139 
 140     private static void checkDrivePaths() throws Exception {
 141         checkDrive(2, findActiveDrive(), true);
 142         checkDrive(2, findInactiveDrive(), false);
 143     }
 144 
 145 
 146     /* UNC pathnames */
 147 
 148     private static void checkUncPaths() throws Exception {
 149         String s = ("\\\\" + NONEXISTENT_UNC_HOST
 150                     + "\\" + NONEXISTENT_UNC_SHARE);
 151         ensureNon(s);
 152         checkSlashes(2, false, s, s);
 153 
 154         s = "\\\\" + EXISTENT_UNC_HOST + "\\" + EXISTENT_UNC_SHARE;
 155         if (!(new File(s)).exists()) {
 156             System.err.println("WARNING: " + s +
 157                                " does not exist, unable to test UNC pathnames");
 158             return;
 159         }
 160 
 161         checkSlashes(2, false, s, s);
 162     }
 163 
 164 
 165     public static void main(String[] args) throws Exception {
 166         if (File.separatorChar != '\\') {
 167             /* This test is only valid on win32 systems */
 168             return;
 169         }
 170         if (args.length > 0) debug = true;


 171         checkRelativePaths();
 172         checkDrivePaths();
 173         checkUncPaths();
 174     }
 175 






















 176 }


  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 /* @test
  25    @bug 4032066 4039597 4046914 4054511 4065189 4109131 4875229 6983520
  26    @summary General exhaustive test of win32 pathname handling
  27    @author Mark Reinhold
  28 
  29    @build General GeneralWin32
  30    @run main/timeout=600 GeneralWin32
  31  */
  32 
  33 import java.io.*;


  34 
  35 public class GeneralWin32 extends General {
  36 
  37 
  38     /**
  39      * Hardwired UNC pathnames used for testing
  40      *
  41      * This test attempts to use the host and share names defined in this class
  42      * to test UNC pathnames.  The test will not fail if the host or share
  43      * don't exist, but it will print a warning saying that it was unable to
  44      * test UNC pathnames completely.
  45      */
  46     private static final String EXISTENT_UNC_HOST = "pc-cup01";
  47     private static final String EXISTENT_UNC_SHARE = "pcdist";
  48     private static final String NONEXISTENT_UNC_HOST = "non-existent-unc-host";
  49     private static final String NONEXISTENT_UNC_SHARE = "bogus-share";
  50     private static final int DEPTH = 2;
  51     private static String baseDir = null;
  52     private static String userDir = null;
  53     
  54     /* Pathnames relative to working directory */
  55 
  56     private static void checkCaseLookup() throws IOException {
  57         /* Use long names here to avoid 8.3 format, which Samba servers often
  58            force to lowercase */
  59         String relative = baseDir.substring(userDir.length() + 1);
  60         File d1 = new File(relative, "XyZzY0123");
  61         File d2 = new File(d1, "FOO_bar_BAZ");
  62         File f = new File(d2, "GLORPified");
  63         if (!f.exists()) {
  64             if (!d2.exists()) {
  65                 if (!d2.mkdirs()) {
  66                     throw new RuntimeException("Can't create directory " + d2);
  67                 }
  68             }
  69             OutputStream o = new FileOutputStream(f);
  70             o.close();
  71         }
  72         File f2 = new File(d2.getParent(), "mumble"); /* For later ud tests */
  73         if (!f2.exists()) {
  74             OutputStream o = new FileOutputStream(f2);
  75             o.close();
  76         }
  77 
  78         /* Computing the canonical path of a Win32 file should expose the true
  79            case of filenames, rather than just using the input case */
  80         File y = new File(userDir, f.getPath());
  81         String ans = y.getPath();
  82         check(ans, relative + "\\" + "XyZzY0123\\FOO_bar_BAZ\\GLORPified");
  83         check(ans, relative + "\\" + "xyzzy0123\\foo_bar_baz\\glorpified");
  84         check(ans, relative + "\\" + "XYZZY0123\\FOO_BAR_BAZ\\GLORPIFIED");
  85     }
  86 
  87     private static void checkWild(File f) throws Exception {
  88         try {
  89             f.getCanonicalPath();
  90         } catch (IOException x) {
  91             return;
  92         }
  93         throw new Exception("Wildcard path not rejected: " + f);
  94     }
  95 
  96     private static void checkWildCards() throws Exception {
  97         File d = new File(baseDir).getCanonicalFile();
  98         checkWild(new File(d, "*.*"));
  99         checkWild(new File(d, "*.???"));
 100         checkWild(new File(new File(d, "*.*"), "foo"));
 101     }
 102 
 103     private static void checkRelativePaths() throws Exception {
 104         checkCaseLookup();
 105         checkWildCards();
 106         String relative = baseDir.substring(userDir.length() + 1);
 107         checkNames(3, true, baseDir.toString(), relative);
 108     }
 109 
 110 
 111     /* Pathnames with drive specifiers */
 112 
 113     private static char findInactiveDrive() {
 114         for (char d = 'Z'; d >= 'E'; d--) {
 115             File df = new File(d + ":\\");
 116             if (!df.exists()) {
 117                 return d;
 118             }
 119         }
 120         throw new RuntimeException("Can't find an inactive drive");
 121     }
 122 
 123     private static char findActiveDrive() {
 124         for (char d = 'C'; d <= 'Z'; d--) {
 125             File df = new File(d + ":\\");
 126             if (df.exists()) return d;
 127         }
 128         throw new RuntimeException("Can't find an active drive");
 129     }
 130 
 131     private static void checkDrive(int depth, char drive, boolean exists)
 132         throws Exception
 133     {
 134         String d = drive + ":";
 135         File df = new File(d);
 136         String ans = exists ? df.getAbsolutePath() : d;
 137         if (!ans.endsWith("\\"))
 138             ans = ans + "\\";
 139         String relative = baseDir.substring(userDir.length() + 1);
 140         checkNames(depth, false, ans + relative, d + relative);
 141     }
 142 
 143     private static void checkDrivePaths() throws Exception {
 144         checkDrive(2, findActiveDrive(), true);
 145         checkDrive(2, findInactiveDrive(), false);
 146     }
 147 
 148 
 149     /* UNC pathnames */
 150 
 151     private static void checkUncPaths() throws Exception {
 152         String s = ("\\\\" + NONEXISTENT_UNC_HOST
 153                     + "\\" + NONEXISTENT_UNC_SHARE);
 154         ensureNon(s);
 155         checkSlashes(DEPTH, false, s, s);
 156 
 157         s = "\\\\" + EXISTENT_UNC_HOST + "\\" + EXISTENT_UNC_SHARE;
 158         if (!(new File(s)).exists()) {
 159             System.err.println("WARNING: " + s +
 160                                " does not exist, unable to test UNC pathnames");
 161             return;
 162         }
 163 
 164         checkSlashes(DEPTH, false, s, s);
 165     }
 166 
 167 
 168     public static void main(String[] args) throws Exception {
 169         if (File.separatorChar != '\\') {
 170             /* This test is only valid on win32 systems */
 171             return;
 172         }
 173         if (args.length > 0) debug = true;
 174         userDir = System.getProperty("user.dir");
 175         baseDir = initTestData(6);
 176         checkRelativePaths();
 177         checkDrivePaths();
 178         checkUncPaths();
 179     }
 180 
 181     private static String initTestData(int maxDepth) throws IOException {
 182         File parent = new File(System.getProperty("user.dir"));
 183         String baseDir = null;
 184         maxDepth = maxDepth < DEPTH + 2 ? DEPTH + 2 : maxDepth;
 185         for (int i = 0; i < maxDepth; i ++) {
 186             File dir1 = new File(parent, gensym());
 187             dir1.mkdir();
 188             if (i != 0) {
 189                 File dir2 = new File(parent, gensym());
 190                 dir2.mkdir();
 191                 File f1 = new File(parent, gensym());
 192                 f1.createNewFile();
 193                 File f2 = new File(parent, gensym());
 194                 f2.createNewFile();
 195             } 
 196             if (i == DEPTH + 1) {
 197                 baseDir = dir1.getAbsolutePath();
 198             }
 199             parent = dir1;
 200         }
 201         return baseDir;
 202     }
 203 }