Path: blob/master/test/jdk/java/io/pathNames/GeneralWin32.java
41149 views
/*1* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 4032066 4039597 4046914 4054511 4065189 4109131 4875229 6983520 800925825@summary General exhaustive test of win32 pathname handling26@author Mark Reinhold2728@build General GeneralWin3229@run main/timeout=600 GeneralWin3230*/3132import java.io.*;3334public class GeneralWin32 extends General {353637/**38* Hardwired UNC pathnames used for testing39*40* This test attempts to use the host and share names defined in this class41* to test UNC pathnames. The test will not fail if the host or share42* don't exist, but it will print a warning saying that it was unable to43* test UNC pathnames completely.44*/45private static final String EXISTENT_UNC_HOST = "pc-cup01";46private static final String EXISTENT_UNC_SHARE = "pcdist";47private static final String NONEXISTENT_UNC_HOST = "non-existent-unc-host";48private static final String NONEXISTENT_UNC_SHARE = "bogus-share";4950/* Pathnames relative to working directory */5152private static void checkCaseLookup() throws IOException {53/* Use long names here to avoid 8.3 format, which Samba servers often54force to lowercase */55File r = new File (workSubDir, "XyZzY0123");56File d = new File(r, "FOO_bar_BAZ");57File f = new File(d, "GLORPified");58if (!f.exists()) {59if (!d.exists()) {60if (!d.mkdirs()) {61throw new RuntimeException("Can't create directory " + d);62}63}64OutputStream o = new FileOutputStream(f);65o.close();66}67File f2 = new File(d.getParent(), "mumble"); /* For later ud tests */68if (!f2.exists()) {69OutputStream o = new FileOutputStream(f2);70o.close();71}7273/* Computing the canonical path of a Win32 file should expose the true74case of filenames, rather than just using the input case */75File y = new File(userDir, f.getPath());76String ans = y.getPath();77check(ans, workSubDir + File.separator + "XyZzY0123\\FOO_bar_BAZ\\GLORPified");78check(ans, workSubDir + File.separator + "xyzzy0123\\foo_bar_baz\\glorpified");79check(ans, workSubDir + File.separator + "XYZZY0123\\FOO_BAR_BAZ\\GLORPIFIED");80}8182private static void checkWild(File f) throws Exception {83try {84f.getCanonicalPath();85} catch (IOException x) {86return;87}88throw new Exception("Wildcard path not rejected: " + f);89}9091private static void checkWildCards() throws Exception {92File d = new File(userDir).getCanonicalFile();93checkWild(new File(d, "*.*"));94checkWild(new File(d, "*.???"));95checkWild(new File(new File(d, "*.*"), "foo"));96}9798private static void checkRelativePaths(int depth) throws Exception {99checkCaseLookup();100checkWildCards();101// Make sure that an empty relative path is tested102checkNames(1, true, userDir + File.separator, "");103checkNames(depth, true, baseDir + File.separator,104relative + File.separator);105}106107108/* Pathnames with drive specifiers */109110private static char findInactiveDrive() {111for (char d = 'Z'; d >= 'E'; d--) {112File df = new File(d + ":\\");113if (!df.exists()) {114return d;115}116}117throw new RuntimeException("Can't find an inactive drive");118}119120private static char findActiveDrive() {121for (char d = 'C'; d <= 'Z'; d--) {122File df = new File(d + ":\\");123if (df.exists()) return d;124}125throw new RuntimeException("Can't find an active drive");126}127128private static void checkDrive(int depth, String drive, boolean exists)129throws Exception130{131File df = new File(drive);132String ans = exists ? df.getAbsolutePath() : drive;133if (!ans.endsWith("\\"))134ans = ans + "\\";135checkNames(depth, false, ans, drive);136}137138private static void checkDrivePaths(int depth) throws Exception {139checkDrive(depth, findActiveDrive() + ":" + workSubDir + File.separator, true);140checkDrive(depth, findInactiveDrive() + ":", false);141}142143144/* UNC pathnames */145146private static void checkUncPaths(int depth) throws Exception {147String s = ("\\\\" + NONEXISTENT_UNC_HOST148+ "\\" + NONEXISTENT_UNC_SHARE);149ensureNon(s);150checkSlashes(depth, false, s, s);151152s = "\\\\" + EXISTENT_UNC_HOST + "\\" + EXISTENT_UNC_SHARE;153if (!(new File(s)).exists()) {154System.err.println("WARNING: " + s +155" does not exist, unable to test UNC pathnames");156return;157}158159checkSlashes(depth, false, s, s);160}161162163public static void main(String[] args) throws Exception {164if (File.separatorChar != '\\') {165/* This test is only valid on win32 systems */166return;167}168if (args.length > 0) debug = true;169170initTestData(3);171172checkRelativePaths(3);173checkDrivePaths(2);174checkUncPaths(2);175}176}177178179