undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,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 + "..");
}
}
}
}

View file

@ -0,0 +1,75 @@
/*
* Copyright (c) 2013, 2025, 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
@requires (os.family != "windows")
@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 (args.length > 0) debug = true;
initTestData(3);
checkUnreadable();
checkPaths();
}
}

View file

@ -0,0 +1,174 @@
/*
* Copyright (c) 1998, 2025, 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 4032066 4039597 4046914 4054511 4065189 4109131 4875229 6983520 8009258
@summary General exhaustive test of win32 pathname handling
@requires (os.family == "windows")
@author Mark Reinhold
@build General GeneralWin32
@run main/timeout=600 GeneralWin32
*/
import java.io.*;
public class GeneralWin32 extends General {
/**
* Hardwired UNC pathnames used for testing
*
* This test attempts to use the host and share names defined in this class
* to test UNC pathnames. The test will not fail if the host or share
* don't exist, but it will print a warning saying that it was unable to
* test UNC pathnames completely.
*/
private static final String EXISTENT_UNC_HOST = "pc-cup01";
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";
/* 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 r = new File (workSubDir, "XyZzY0123");
File d = new File(r, "FOO_bar_BAZ");
File f = new File(d, "GLORPified");
if (!f.exists()) {
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(d.getParent(), "mumble"); /* For later ud tests */
if (!f2.exists()) {
OutputStream o = new FileOutputStream(f2);
o.close();
}
/* Computing the canonical path of a Win32 file should expose the true
case of filenames, rather than just using the input case */
File y = new File(userDir, f.getPath());
String ans = y.getPath();
check(ans, workSubDir + File.separator + "XyZzY0123\\FOO_bar_BAZ\\GLORPified");
check(ans, workSubDir + File.separator + "xyzzy0123\\foo_bar_baz\\glorpified");
check(ans, workSubDir + File.separator + "XYZZY0123\\FOO_BAR_BAZ\\GLORPIFIED");
}
private static void checkWild(File f) throws Exception {
try {
f.getCanonicalPath();
} catch (IOException x) {
return;
}
throw new Exception("Wildcard path not rejected: " + f);
}
private static void checkWildCards() throws Exception {
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(int depth) throws Exception {
checkCaseLookup();
checkWildCards();
// Make sure that an empty relative path is tested
checkNames(1, true, userDir + File.separator, "");
checkNames(depth, true, baseDir + File.separator,
relative + File.separator);
}
/* Pathnames with drive specifiers */
private static char findInactiveDrive() {
for (char d = 'Z'; d >= 'E'; d--) {
File df = new File(d + ":\\");
if (!df.exists()) {
return d;
}
}
throw new RuntimeException("Can't find an inactive drive");
}
private static char findActiveDrive() {
for (char d = 'C'; d <= 'Z'; d--) {
File df = new File(d + ":\\");
if (df.exists()) return d;
}
throw new RuntimeException("Can't find an active drive");
}
private static void checkDrive(int depth, String drive, boolean exists)
throws Exception
{
File df = new File(drive);
String ans = exists ? df.getAbsolutePath() : drive;
if (!ans.endsWith("\\"))
ans = ans + "\\";
checkNames(depth, false, ans, drive);
}
private static void checkDrivePaths(int depth) throws Exception {
checkDrive(depth, findActiveDrive() + ":" + workSubDir + File.separator, true);
checkDrive(depth, findInactiveDrive() + ":", false);
}
/* UNC pathnames */
private static void checkUncPaths(int depth) throws Exception {
String s = ("\\\\" + NONEXISTENT_UNC_HOST
+ "\\" + NONEXISTENT_UNC_SHARE);
ensureNon(s);
checkSlashes(depth, false, s, s);
s = "\\\\" + EXISTENT_UNC_HOST + "\\" + EXISTENT_UNC_SHARE;
if (!(new File(s)).exists()) {
System.err.println("WARNING: " + s +
" does not exist, unable to test UNC pathnames");
return;
}
checkSlashes(depth, false, s, s);
}
public static void main(String[] args) throws Exception {
if (args.length > 0) debug = true;
initTestData(3);
checkRelativePaths(3);
checkDrivePaths(2);
checkUncPaths(2);
}
}

View file

@ -0,0 +1,120 @@
/*
* Copyright (c) 2001, 2025, 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 4427862
* @summary Ensure that trailing slashes are ignored when opening files
* @requires (os.family != "windows")
*/
import java.io.*;
public class TrailingSlash {
static PrintStream log = System.err;
static int failures = 0;
static void check(String what, String fns,
boolean expected, boolean threw)
{
if (expected == threw) {
log.println(" FAIL: new " + what + "(\"" + fns + "\") "
+ (expected ? "failed" : "succeeded"));
failures++;
}
}
static void go(String fns, boolean fis, boolean raf, boolean fos)
throws IOException
{
boolean threw;
threw = false;
try {
new FileInputStream(fns).close();
log.println(" FileInputStream okay");
} catch (IOException x) {
log.println(" FileInputStream: " + x);
threw = true;
}
check("FileInputStream", fns, fis, threw);
threw = false;
try {
new RandomAccessFile(fns, "r").close();
log.println(" RandomAccessFile okay");
} catch (IOException x) {
log.println(" RandomAccessFile: " + x);
threw = true;
}
check("RandomAccessFile", fns, raf, threw);
threw = false;
try {
new FileOutputStream(fns).close();
log.println(" FileOutputStream okay");
} catch (IOException x) {
log.println(" FileOutputStream: " + x);
threw = true;
}
check("FileOutputStream", fns, fos, threw);
}
static void go(String fn, String fns) throws Exception {
log.println("Test case: " + fns);
File f = new File(fn);
f.delete();
if (f.exists())
throw new Exception("Can't delete " + f);
log.println(" " + fn + " does not exist");
go(fns, false, false, true);
f.delete();
f.mkdir();
log.println(" " + fn + " is a directory");
go(fns, false, false, false);
f.delete();
f.createNewFile();
log.println(" " + fn + " is a file");
go(fns, true, true, true);
}
public static void main(String[] args) throws Exception {
go("xyzzy", "xyzzy");
go("xyzzy", "xyzzy/");
go("xyzzy", "xyzzy//");
if (failures > 0)
throw new Exception(failures + " failures");
}
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 1998, 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 4138774
@summary Make sure that a bad drive letter doesn't cause an exception
*/
public class BadDriveLetter {
public static void main(String[] args) {
System.err.println(new java.io.File(".:").getAbsolutePath());
}
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 1998, 2025, 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 4096648
@summary Make sure that isDirectory and lastModified work on "x:"
@requires (os.family == "windows")
*/
import java.io.File;
public class DriveOnly {
public static void main(String[] args) throws Exception {
File f = new File("").getCanonicalFile();
while (f.getParent() != null) f = f.getParentFile();
String p = f.getPath().substring(0, 2);
if (!(Character.isLetter(p.charAt(0)) && (p.charAt(1) == ':'))) {
System.err.println("No current drive, cannot run test");
return;
}
f = new File(p);
if (!f.isDirectory())
throw new Exception("\"" + f + "\" is not a directory");
if (f.lastModified() == 0)
throw new Exception("\"" + f + "\" has no last-modified time");
}
}

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 1997, 2025, 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 4070044 4164823
@summary Check getCanonicalPath's treatment of drive-relative paths (win32)
@requires (os.family == "windows")
*/
import java.io.*;
public class DriveRelativePath {
static void fail(String s) {
throw new RuntimeException(s);
}
public static void main(String[] args) throws IOException {
File f = new File("foo");
String c = f.getCanonicalPath();
System.err.println(c);
int di = c.indexOf(':');
if (di == -1) fail("No drive in canonical path");
String drive = c.substring(0, di + 1);
File f2 = new File(drive + "foo");
System.err.println(f2);
String c2 = f2.getCanonicalPath();
System.err.println(c2);
if (!c2.equals(c)) fail("Canonical path mismatch: \""
+ f2 + "\" maps to \""
+ c2 + "\"; it should map to \""
+ c + "\"");
}
}

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 1998, 2025, 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 4065189
@summary Check that win32 pathnames of the form "C:\\"
can be listed by the File.list method
@requires (os.family == "windows")
@author Mark Reinhold
*/
import java.io.*;
public class DriveSlash {
public static void main(String[] args) throws Exception {
File f = new File("c:\\");
System.err.println(f.getCanonicalPath());
String[] fs = f.list();
if (fs == null) {
throw new Exception("File.list returned null");
}
for (int i = 0; i < fs.length; i++) {
System.err.print(" " + fs[i]);
}
System.err.println();
}
}

View file

@ -0,0 +1,76 @@
/*
* Copyright (c) 1998, 2025, 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 4042592 4042593
@requires (os.family == "windows")
* @summary Test operation of rename and delete on win32
*/
import java.io.*;
/**
* This class tests to see if java.io.file rename() method
* operates properly with non canonical pathnames
* and then tests delete() method with non canonical pathnames
*/
public class RenameDelete {
public static void main(String[] args) throws Exception {
boolean success = false;
//construct a test file in this location
File f1 = new File(".");
StringBuffer location = new StringBuffer("\\");
location.append(f1.getCanonicalPath());
StringBuffer fromLocation = new StringBuffer(location.toString()+"\\From");
StringBuffer toLocation = new StringBuffer(location.toString()+"\\To");
f1 = new File(fromLocation.toString());
File f2 = new File(toLocation.toString());
if(f1.exists() || f2.exists()) {
System.err.println("Directories exist -- test not valid");
return;
}
System.err.println("Create:"+f1.mkdir());
System.err.println("Exist as directory:"+f1.exists()+" "+f1.isDirectory());
success = f1.renameTo(f2);
System.err.println("Rename:"+success);
if (!success)
throw new RuntimeException("File method rename did not function");
success = f2.delete();
System.err.println("Delete:"+success);
if (!success)
throw new RuntimeException("File method delete did not function");
}
}

View file

@ -0,0 +1,91 @@
/*
* Copyright (c) 1998, 2025, 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 4039597
@summary Check that pathnames containing double-byte characters are not
corrupted by win32 path processing
@requires (os.family == "windows")
@author Mark Reinhold
@library /test/lib
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import jtreg.SkippedException;
public class SJIS {
private static void rm(File f) {
if (!f.delete()) throw new RuntimeException("Can't delete " + f);
}
private static void touch(File f) throws IOException {
OutputStream o = new FileOutputStream(f);
o.close();
}
public static void main(String[] args) throws Exception {
/* This test is only valid on win32 systems
that use the SJIS encoding */
String enc = System.getProperty("native.encoding");
if ((enc == null) || !enc.equals("MS932")) {
throw new SkippedException(
"native.encoding(%s) is not MS932".formatted(enc));
}
File f = new File("\u30BD");
if (f.exists()) rm(f);
System.err.println(f.getCanonicalPath());
touch(f);
System.err.println(f.getCanonicalPath());
rm(f);
if (!f.mkdir()) {
throw new Exception("Can't create directory " + f);
}
File f2 = new File(f, "\u30BD");
System.err.println(f2.getCanonicalPath());
touch(f2);
String cfn = f2.getCanonicalPath();
if (!(new File(cfn)).exists()) {
throw new Exception(cfn + " not found");
}
File d = new File(".");
String[] fs = d.list();
if (fs == null) System.err.println("No files listed");
for (int i = 0; i < fs.length; i++) {
System.err.println(fs[i]);
}
}
}

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2005, 2025, 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 6344646
* @summary tests that WinNTFileSystem.hashCode() uses
* locale independent case mapping.
* @requires (os.family == "windows")
*/
import java.io.*;
import java.util.*;
public class bug6344646 {
public static void main(String[] s) {
Locale reservedLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.of("lt"));
File f1 = new File("J\u0301");
File f2 = new File("j\u0301");
if (f1.hashCode() != f2.hashCode()) {
throw new RuntimeException("File.hashCode() for \"J\u0301\" " +
"and \"j\u0301\" should be the same");
}
} finally {
// restore the reserved locale
Locale.setDefault(reservedLocale);
}
}
}