use std::path::{Path, PathBuf};
use super::{DirectoryHandle, OpenFailure, VolumeIdentity};
struct TempDir {
path: PathBuf,
}
impl TempDir {
fn new(label: &str) -> Self {
let nonce = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("clock")
.as_nanos();
let path = std::env::temp_dir().join(format!(
"windows-file-watcher-{label}-{}-{nonce}",
std::process::id()
));
std::fs::create_dir(&path).expect("create temp dir");
Self { path }
}
fn path(&self) -> &Path {
&self.path
}
fn cleanup(self) {
let _ = std::fs::remove_dir_all(&self.path);
}
}
#[test]
fn opens_a_plain_directory() {
let dir = TempDir::new("open-plain");
assert!(DirectoryHandle::open(dir.path()).is_ok());
dir.cleanup();
}
#[test]
fn opens_the_system_temp_directory() {
assert!(DirectoryHandle::open(&std::env::temp_dir()).is_ok());
}
#[test]
fn opens_the_current_directory_by_relative_path() {
assert!(
DirectoryHandle::open(Path::new(".")).is_ok(),
"a relative path must be accepted; Win32 resolves it"
);
}
#[test]
fn opens_a_nested_directory() {
let dir = TempDir::new("open-nested");
let nested = dir.path().join("a").join("b");
std::fs::create_dir_all(&nested).expect("create nested");
assert!(DirectoryHandle::open(&nested).is_ok());
dir.cleanup();
}
#[test]
fn opens_a_directory_whose_name_is_not_ascii() {
let dir = TempDir::new("open-unicode");
let child = dir.path().join("caf\u{e9}-\u{1f600}-\u{10437}");
std::fs::create_dir(&child).expect("create unicode-named dir");
assert!(DirectoryHandle::open(&child).is_ok());
dir.cleanup();
}
#[test]
fn opens_a_directory_with_a_trailing_separator() {
let dir = TempDir::new("open-trailing");
let with_sep = format!("{}\\", dir.path().display());
assert!(DirectoryHandle::open(Path::new(&with_sep)).is_ok());
dir.cleanup();
}
#[test]
fn opens_a_directory_containing_files() {
let dir = TempDir::new("open-populated");
for index in 0..8 {
std::fs::write(dir.path().join(format!("file-{index}.txt")), b"x").expect("write");
}
assert!(DirectoryHandle::open(dir.path()).is_ok());
dir.cleanup();
}
#[test]
fn the_handle_is_usable_and_reports_itself() {
let dir = TempDir::new("open-usable");
let handle = DirectoryHandle::open(dir.path()).expect("open");
assert!(!handle.as_raw().is_null(), "a live handle is never null");
assert!(
format!("{handle:?}").contains("DirectoryHandle"),
"Debug names the type"
);
dir.cleanup();
}
#[test]
fn the_share_mode_admits_a_second_watcher() {
let dir = TempDir::new("open-shared");
let first = DirectoryHandle::open(dir.path()).expect("first open");
let second = DirectoryHandle::open(dir.path()).expect("second open");
assert_ne!(
first.as_raw(),
second.as_raw(),
"the two opens are distinct handles"
);
dir.cleanup();
}
#[test]
fn the_share_mode_admits_writers_while_watching() {
let dir = TempDir::new("open-writable");
let handle = DirectoryHandle::open(dir.path()).expect("open");
std::fs::write(dir.path().join("created-while-open.txt"), b"data")
.expect("create a file while the directory handle is open");
drop(handle);
dir.cleanup();
}
#[test]
fn many_sequential_opens_do_not_exhaust_handles() {
let dir = TempDir::new("open-repeat");
for _ in 0..256 {
let handle = DirectoryHandle::open(dir.path()).expect("open");
drop(handle);
}
dir.cleanup();
}
#[test]
fn a_missing_leaf_classifies_as_not_found() {
let dir = TempDir::new("missing-leaf");
let missing = dir.path().join("does-not-exist");
let error = DirectoryHandle::open(&missing).expect_err("must fail");
assert_eq!(error.failure(), OpenFailure::NotFound);
assert!(
error.failure().is_retryable(),
"a path may legitimately appear later"
);
dir.cleanup();
}
#[test]
fn a_missing_intermediate_component_classifies_as_not_found() {
let dir = TempDir::new("missing-parent");
let missing = dir.path().join("absent-parent").join("leaf");
let error = DirectoryHandle::open(&missing).expect_err("must fail");
assert_eq!(error.failure(), OpenFailure::NotFound);
dir.cleanup();
}
#[test]
fn a_regular_file_classifies_as_not_a_directory() {
let dir = TempDir::new("not-a-dir");
let file = dir.path().join("regular.txt");
std::fs::write(&file, b"contents").expect("write file");
let error = DirectoryHandle::open(&file).expect_err("must fail");
assert_eq!(error.failure(), OpenFailure::NotADirectory);
assert!(
!error.failure().is_retryable(),
"a file will never become a directory"
);
dir.cleanup();
}
#[test]
fn an_interior_nul_classifies_as_invalid_path_without_calling_win32() {
let dir = TempDir::new("interior-nul");
assert!(DirectoryHandle::open(dir.path()).is_ok());
let mut poisoned = std::ffi::OsString::from(dir.path());
poisoned.push(unsafe_nul());
poisoned.push("trailing");
let error = DirectoryHandle::open(Path::new(&poisoned)).expect_err("must fail");
assert_eq!(error.failure(), OpenFailure::InvalidPath);
assert!(!error.failure().is_retryable());
dir.cleanup();
}
fn unsafe_nul() -> std::ffi::OsString {
use std::os::windows::ffi::OsStringExt;
std::ffi::OsString::from_wide(&[0u16])
}
#[test]
fn an_empty_path_fails_and_is_classified() {
let error = DirectoryHandle::open(Path::new("")).expect_err("must fail");
assert!(
matches!(
error.failure(),
OpenFailure::NotFound | OpenFailure::InvalidPath | OpenFailure::Retryable
),
"unexpected classification: {:?}",
error.failure()
);
}
#[test]
fn a_failure_preserves_the_underlying_os_error() {
let dir = TempDir::new("preserve-os-error");
let missing = dir.path().join("nope");
let error = DirectoryHandle::open(&missing).expect_err("must fail");
let underlying = std::error::Error::source(&error)
.and_then(|source| source.downcast_ref::<std::io::Error>())
.expect("the original error is kept as the source");
assert!(
underlying.raw_os_error().is_some(),
"the original OS error code is kept for diagnostics"
);
assert!(
format!("{error}").contains("NotFound"),
"Display names the classification"
);
dir.cleanup();
}
#[test]
fn every_failure_class_agrees_with_its_retry_policy() {
assert!(OpenFailure::NotFound.is_retryable());
assert!(OpenFailure::Unsupported.is_retryable());
assert!(OpenFailure::Retryable.is_retryable());
assert!(!OpenFailure::NotADirectory.is_retryable());
assert!(!OpenFailure::InvalidPath.is_retryable());
}
#[test]
fn reopen_by_id_preserves_identity_while_the_original_stays_open() {
let dir = TempDir::new("reopen-same-identity");
let original = DirectoryHandle::open(dir.path()).expect("open");
let original_identity = original.identity();
let hint = unsafe { std::os::windows::io::BorrowedHandle::borrow_raw(original.as_raw()) };
let reopened = DirectoryHandle::reopen_by_id(hint, original_identity.file_reference())
.expect("OpenFileById against a live handle's own file reference");
assert_eq!(
reopened.identity(),
original_identity,
"OpenFileById must reopen the same object its file reference already names"
);
assert_eq!(original.identity(), original_identity);
drop(reopened);
drop(original);
dir.cleanup();
}
#[test]
fn reopen_by_id_survives_the_directory_being_deleted_from_under_it() {
let dir = TempDir::new("reopen-deleted");
let original = DirectoryHandle::open(dir.path()).expect("open");
let original_identity = original.identity();
std::fs::remove_dir(dir.path()).expect("unlink the directory while the handle is still open");
let hint = unsafe { std::os::windows::io::BorrowedHandle::borrow_raw(original.as_raw()) };
let reopened = DirectoryHandle::reopen_by_id(hint, original_identity.file_reference())
.expect("OpenFileById against a delete-pending object's own file reference");
assert_eq!(
reopened.identity(),
original_identity,
"OpenFileById reopens the same (delete-pending) object its file reference names, \
never a different one that happens to appear at the original path later"
);
drop(reopened);
drop(original);
}
#[test]
fn reopen_by_id_ignores_a_new_directory_recreated_at_the_same_path() {
let dir = TempDir::new("reopen-recreated");
let original = DirectoryHandle::open(dir.path()).expect("open");
let original_identity = original.identity();
std::fs::remove_dir(dir.path()).expect("unlink the directory while the handle is still open");
std::fs::create_dir(dir.path()).expect("recreate a new directory at the same path");
let fresh_identity = DirectoryHandle::open(dir.path())
.expect("open the recreated directory")
.identity();
assert_ne!(
original_identity, fresh_identity,
"a recreated directory must have a genuinely different identity for this test to mean anything"
);
let hint = unsafe { std::os::windows::io::BorrowedHandle::borrow_raw(original.as_raw()) };
let reopened = DirectoryHandle::reopen_by_id(hint, original_identity.file_reference())
.expect("OpenFileById against a live file reference");
assert_eq!(
reopened.identity(),
original_identity,
"OpenFileById must never silently switch to a different object recreated at the same path"
);
drop(reopened);
drop(original);
dir.cleanup();
}
#[test]
fn reopen_by_id_follows_the_directory_if_it_is_renamed_and_canonical_path_detects_it() {
let parent = TempDir::new("reopen-rename-parent");
let original_path = parent.path().join("original");
std::fs::create_dir(&original_path).expect("create the directory to be renamed");
let original = DirectoryHandle::open(&original_path).expect("open");
let original_identity = original.identity();
let path_before = original
.canonical_path()
.expect("query the path before the rename");
let renamed_path = parent.path().join("renamed");
std::fs::rename(&original_path, &renamed_path).expect("rename while the handle is open");
let hint = unsafe { std::os::windows::io::BorrowedHandle::borrow_raw(original.as_raw()) };
let reopened = DirectoryHandle::reopen_by_id(hint, original_identity.file_reference())
.expect("OpenFileById against a live file reference");
assert_eq!(
reopened.identity(),
original_identity,
"a rename does not change the object's own identity"
);
let path_after = reopened
.canonical_path()
.expect("query the path after the rename");
assert_ne!(
path_before, path_after,
"OpenFileById follows the object to its new location, so the canonical path must \
change -- this is exactly what `reopen_via_existing_handle` must detect and refuse"
);
drop(reopened);
drop(original);
parent.cleanup();
}
#[test]
fn volume_identity_equality_is_on_the_serial_alone() {
let a = VolumeIdentity::synthetic(1, "NTFS", "SAME-LABEL");
let b = VolumeIdentity::synthetic(1, "FAT32", "DIFFERENT-LABEL");
assert_eq!(
a, b,
"the same serial is the same volume, regardless of label/filesystem \
(a mere rename must not look like a media swap)"
);
let c = VolumeIdentity::synthetic(2, "NTFS", "SAME-LABEL");
assert_ne!(
a, c,
"a different serial is different media, even with an identical \
label/filesystem (media swapped for other media sharing a label \
must not go undetected)"
);
}
#[cfg(feature = "test-util")]
#[test]
fn volume_identity_for_test_is_the_public_synthetic_seam() {
let a = VolumeIdentity::for_test(0x1234, "NTFS", "System");
let b = VolumeIdentity::for_test(0x1234, "ReFS", "Data");
let c = VolumeIdentity::for_test(0x9999, "NTFS", "System");
assert_eq!(a.filesystem_name(), "NTFS");
assert_eq!(a.volume_label(), "System");
assert_eq!(a, b);
assert_ne!(a, c);
}