use std::cell::Cell;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use super::{absolutise_exact_on, complete_on, Completed, DriveDirs};
use crate::child::spawn::windows_raw::env_snapshot::EnvSnapshot;
use crate::error::Error;
fn no_drive(_: &std::ffi::OsStr) -> Result<Option<OsString>, Error> {
Ok(None)
}
fn real_drive(drive: &std::ffi::OsStr) -> Result<Option<OsString>, Error> {
let snapshot = EnvSnapshot::read()?;
Ok(DriveDirs::new(&snapshot).get(drive))
}
fn snapshot_with(var: &str) -> EnvSnapshot {
EnvSnapshot::from_block(format!("{var}\0\0").encode_utf16().collect())
}
fn letter_of(p: &Path) -> char {
p.to_string_lossy().chars().next().unwrap().to_ascii_uppercase()
}
fn win32_full_path(token: &str) -> std::io::Result<PathBuf> {
use std::os::windows::ffi::{OsStrExt, OsStringExt};
use windows::core::PCWSTR;
use windows::Win32::Storage::FileSystem::GetFullPathNameW;
let wide: Vec<u16> = std::ffi::OsStr::new(token).encode_wide().chain([0]).collect();
let mut buf = vec![0u16; 1];
loop {
let n = unsafe { GetFullPathNameW(PCWSTR(wide.as_ptr()), Some(&mut buf), None) } as usize;
if n == 0 {
return Err(std::io::Error::last_os_error());
}
if n < buf.len() {
buf.truncate(n);
return Ok(PathBuf::from(OsString::from_wide(&buf)));
}
buf.resize(n, 0);
}
}
#[skuld::test]
fn matches_get_full_path_name_given_the_real_cwd() {
let cwd = std::env::current_dir().unwrap();
let letter = letter_of(&cwd);
let other = if letter == 'Q' { 'R' } else { 'Q' };
for token in [
"tool.exe".to_owned(),
r".\a\..\tool.exe".to_owned(),
r"\tool.exe".to_owned(),
r"sub\tool.exe.".to_owned(),
format!("{letter}:tool.exe"),
format!("{}:tool.exe", letter.to_ascii_lowercase()),
format!("{other}:tool.exe"),
r"C:\abs\tool.exe".to_owned(),
r"\\tool.exe".to_owned(),
"//tool.exe".to_owned(),
r"\/tool.exe".to_owned(),
r"/\tool.exe".to_owned(),
r"\\srv\\x.exe".to_owned(),
r"\\srv\shr\tool.exe".to_owned(),
format!(r"{letter}:D:\x.exe"),
"C:D:x.exe".to_owned(),
r"\:x.exe".to_owned(),
"/:x.exe".to_owned(),
r"\:\x.exe".to_owned(),
"1:tool.exe".to_owned(),
] {
let outcome = |r: Result<PathBuf, Error>| match r {
Ok(p) => Ok(p),
Err(Error::Io(e)) => Err(e.kind()),
Err(other) => panic!("{token:?}: unexpected error {other:?}"),
};
let names_no_file = |p: &std::ffi::OsStr| crate::resolve::names_no_file(p, true);
let oracle = || {
if names_no_file(token.as_ref()) {
return Err(std::io::ErrorKind::InvalidInput);
}
let full = win32_full_path(&token).map_err(|e| e.kind())?;
if names_no_file(full.as_os_str()) {
return Err(std::io::ErrorKind::InvalidInput);
}
Ok(full)
};
let want = oracle();
let got = outcome(absolutise_exact_on(Path::new(&token), || Ok(cwd.clone()), real_drive).map(|c| c.path));
assert_eq!(got, want, "{token:?}");
}
}
fn counted<'a>(base: &str, reads: &'a Cell<u32>) -> impl FnOnce() -> Result<PathBuf, Error> + 'a {
let base = PathBuf::from(base);
move || {
reads.set(reads.get() + 1);
Ok(base)
}
}
#[skuld::test]
fn a_relative_or_rooted_path_uses_one_read() {
for (path, base, want) in [
(r"sub\tool.exe", r"C:\base", r"C:\base\sub\tool.exe"),
(r"\tool.exe", r"C:\base", r"C:\tool.exe"),
(r"\tool.exe", r"\\srv\shr\d", r"\\srv\shr\tool.exe"),
("C:tool.exe", r"C:\base", r"C:\base\tool.exe"),
("c:tool.exe", r"C:\base", r"C:\base\tool.exe"),
(r"C:D:\evil.exe", r"C:\base", r"C:\base\D:\evil.exe"),
("\u{e9}:tool.exe", "\u{c9}:\\dir", "\u{c9}:\\dir\\tool.exe"),
(r"\:x.exe", r"C:\base", r"C:\:x.exe"),
] {
let reads = Cell::new(0);
let got = complete_on(Path::new(path), counted(base, &reads), no_drive).unwrap();
assert_eq!(got.path, PathBuf::from(want), "{path:?} on {base:?}");
assert!(got.used_cwd, "{path:?}");
assert_eq!(reads.get(), 1, "{path:?}");
}
}
#[skuld::test]
fn another_drives_relative_path_does_not_use_the_cwd() {
let qcwd = tempfile::tempdir().unwrap();
let qcwd = qcwd.path().to_str().unwrap();
let under_qcwd = format!(r"{qcwd}\tool.exe");
for (drive, want) in [(None, r"Q:\tool.exe"), (Some(qcwd), under_qcwd.as_str())] {
let reads = Cell::new(0);
let got = complete_on(Path::new("Q:tool.exe"), counted(r"D:\base", &reads), |d| {
assert_eq!(d, "Q:");
Ok(drive.map(OsString::from))
})
.unwrap();
assert_eq!(got.path, PathBuf::from(want));
assert!(!got.used_cwd);
assert_eq!(reads.get(), 1, "the cwd is read once, to learn the current drive");
}
}
#[skuld::test]
fn an_absolute_or_unc_shaped_path_reads_nothing() {
for path in [
r"C:\abs\tool.exe",
"C:/abs/tool.exe",
r"\\srv\shr\tool.exe",
r"\\?\C:\abs\tool.exe",
r"\\tool.exe",
"//tool.exe",
] {
let got = complete_on(
Path::new(path),
|| panic!("{path:?} must not read the cwd"),
|_| panic!("{path:?} must not read a drive's cwd"),
)
.unwrap();
assert!(!got.used_cwd, "{path:?}");
}
}
#[skuld::test]
fn completed_is_what_a_caller_reads() {
let Completed { path, used_cwd } = complete_on(Path::new(r"C:\a\..\b"), || unreachable!(), no_drive).unwrap();
assert_eq!(path, PathBuf::from(r"C:\b"));
assert!(!used_cwd);
}
#[skuld::test]
fn a_digit_drive_takes_that_drives_directory() {
let reads = Cell::new(0);
let got = complete_on(Path::new("1:tool.exe"), counted(r"C:\base", &reads), no_drive).unwrap();
assert_eq!(got.path, PathBuf::from(r"1:\tool.exe"));
assert!(!got.used_cwd);
}
#[skuld::test]
fn a_drive_directory_is_used_only_when_fully_qualified() {
for (value, want) in [
(r"Q:\qcwd", r"Q:\qcwd\tool.exe"),
(r"\\srv\shr\d", r"\\srv\shr\d\tool.exe"),
("Q:rel", r"Q:\tool.exe"),
("rel", r"Q:\tool.exe"),
(r"\rooted", r"Q:\tool.exe"),
] {
let got = complete_on(
Path::new("Q:tool.exe"),
|| Ok(PathBuf::from(r"D:\base")),
|_| Ok(Some(OsString::from(value))),
)
.unwrap();
assert_eq!(got.path, PathBuf::from(want), "=Q:={value:?}");
}
}
#[skuld::test]
fn only_an_existing_fully_qualified_drive_directory_is_used() {
let dir = tempfile::tempdir().unwrap();
let existing = dir.path().to_str().unwrap().to_owned();
let file = dir.path().join("afile");
std::fs::write(&file, b"x").unwrap();
let file = file.to_str().unwrap().to_owned();
let gone = format!(r"{existing}\gone");
let under_existing = format!(r"{existing}\tool.exe");
for (value, want) in [
(existing.as_str(), under_existing.as_str()),
(gone.as_str(), r"Q:\tool.exe"),
(file.as_str(), r"Q:\tool.exe"),
("Q:rel", r"Q:\tool.exe"),
("rel", r"Q:\tool.exe"),
(r"\rooted", r"Q:\tool.exe"),
] {
let snapshot = snapshot_with(&format!("=Q:={value}"));
let got = super::effective_cwd(Some(Path::new("Q:tool.exe")), &DriveDirs::new(&snapshot), || {
Ok(PathBuf::from(r"D:\base"))
})
.unwrap();
assert_eq!(got, PathBuf::from(want), "=Q:={value:?}");
}
}
#[skuld::test]
fn a_drive_directory_is_probed_once() {
thread_local! {
static PROBES: Cell<usize> = const { Cell::new(0) };
}
fn probe(_: &Path) -> bool {
PROBES.with(|p| p.set(p.get() + 1));
true
}
let snapshot = snapshot_with(r"=Q:=Q:\qcwd");
let dirs = DriveDirs::with_probe(&snapshot, probe);
for drive in ["Q:", "q:", "Q:"] {
assert_eq!(dirs.get(drive.as_ref()), Some(OsString::from(r"Q:\qcwd")), "{drive}");
}
let relative = snapshot_with("=R:=rel");
assert_eq!(DriveDirs::with_probe(&relative, probe).get("R:".as_ref()), None);
assert_eq!(PROBES.with(Cell::get), 1);
}
#[skuld::test]
fn a_relative_name_on_a_verbatim_unc_cwd_collapses_as_win32_does() {
let cwd = || Ok(PathBuf::from(r"\\?\UNC\srv\shr\d"));
for (name, want) in [
(r"..\t.exe", r"\\?\UNC\srv\shr\t.exe"),
(r"..\..\t.exe", r"\\?\UNC\srv\t.exe"),
(r"..\..\..\t.exe", r"\\?\UNC\t.exe"),
] {
let got = complete_on(Path::new(name), cwd, no_drive).unwrap();
assert_eq!(got.path, PathBuf::from(want), "{name:?}");
}
let exact = absolutise_exact_on(Path::new(r"..\t.exe"), cwd, no_drive).unwrap();
assert_eq!(exact.path, PathBuf::from(r"\\?\UNC\srv\shr\t.exe"));
for name in [r"..\..\t.exe", r"..\..\..\t.exe"] {
match absolutise_exact_on(Path::new(name), cwd, no_drive) {
Err(Error::Io(e)) => assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput, "{name:?}: {e}"),
Err(other) => panic!("{name:?}: expected Io, got {other:?}"),
Ok(done) => panic!("{name:?}: must be refused, got {:?}", done.path),
}
}
}
#[skuld::test]
fn a_relative_name_on_a_verbatim_drive_cwd_collapses_as_win32_does() {
let got = complete_on(Path::new(r"..\..\t.exe"), || Ok(PathBuf::from(r"\\?\C:\d")), no_drive).unwrap();
assert_eq!(got.path, PathBuf::from(r"\\?\t.exe"));
}
#[skuld::test]
fn a_rooted_name_on_a_verbatim_cwd_is_refused() {
for cwd in [r"\\?\UNC\srv\shr\d", r"\\?\C:\d"] {
for complete in [complete_on, absolutise_exact_on] {
match complete(Path::new(r"\t.exe"), || Ok(PathBuf::from(cwd)), no_drive) {
Err(Error::Io(e)) => assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput, "{cwd:?}: {e}"),
Err(other) => panic!("{cwd:?}: expected Io, got {other:?}"),
Ok(done) => panic!("{cwd:?}: must be refused, got {:?}", done.path),
}
}
}
}
fn empty_env() -> EnvSnapshot {
EnvSnapshot::from_block(vec![0])
}
#[skuld::test]
fn the_effective_cwd_completes_current_dir_as_win32_does() {
for (cmd_cwd, want, want_reads) in [
(None, r"1:\x", 1),
(Some(r"\work"), r"1:\work", 1),
(Some("sub"), r"1:\x\sub", 1),
(Some("D:sub"), r"D:\sub", 1),
(Some(r"C:\abs"), r"C:\abs", 0),
] {
let reads = Cell::new(0);
let got = super::effective_cwd(
cmd_cwd.map(Path::new),
&DriveDirs::new(&empty_env()),
counted(r"1:\x", &reads),
)
.unwrap();
assert_eq!(got, PathBuf::from(want), "{cmd_cwd:?}");
assert_eq!(reads.get(), want_reads, "{cmd_cwd:?}");
}
}
#[skuld::test]
fn the_effective_cwd_reads_a_drive_directory_from_the_given_snapshot() {
let qcwd = tempfile::tempdir().unwrap();
let qcwd = qcwd.path().to_str().unwrap();
let block: Vec<u16> = format!("=Q:={qcwd}\0\0").encode_utf16().collect();
let got = super::effective_cwd(
Some(Path::new("Q:sub")),
&DriveDirs::new(&EnvSnapshot::from_block(block)),
|| Ok(PathBuf::from(r"D:\x")),
)
.unwrap();
assert_eq!(got, PathBuf::from(format!(r"{qcwd}\sub")));
}
#[skuld::test]
fn the_effective_cwd_without_a_current_dir_is_one_read() {
let reads = Cell::new(0);
let got = super::effective_cwd(None, &DriveDirs::new(&empty_env()), counted(r"C:\x", &reads)).unwrap();
assert_eq!(got, PathBuf::from(r"C:\x"));
assert_eq!(reads.get(), 1);
}
#[skuld::test]
fn the_effective_cwd_refuses_a_share_less_unc_current_dir() {
for dir in [r"\\server", "//server", r"\\?\UNC\srv", r"\\?\UNC\", r"\\?\UNC\srv/shr"] {
match super::effective_cwd(Some(Path::new(dir)), &DriveDirs::new(&empty_env()), || {
panic!("{dir:?} must not read the cwd")
}) {
Err(Error::Io(e)) => assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput, "{dir:?}: {e}"),
other => panic!("{dir:?}: expected Io(InvalidInput), got {other:?}"),
}
}
}
#[skuld::test]
fn the_effective_cwd_refuses_a_nul_before_completing() {
use std::os::windows::ffi::OsStringExt;
let dir = OsString::from_wide(&"sub\0x".encode_utf16().collect::<Vec<u16>>());
match super::effective_cwd(Some(Path::new(&dir)), &DriveDirs::new(&empty_env()), || {
panic!("a NUL-bearing directory must not read the cwd")
}) {
Err(Error::Io(e)) => {
assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput, "{e}");
assert!(e.to_string().contains("working directory"), "{e}");
}
other => panic!("expected Io(InvalidInput), got {other:?}"),
}
}
#[skuld::test]
fn a_verbatim_current_dir_finds_a_dot_relative_name() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("tool.exe"), b"x").unwrap();
let mut verbatim = OsString::from(r"\\?\");
verbatim.push(dir.path());
let got = super::resolve_executable(Path::new("./tool.exe"), Some(Path::new(&verbatim)), None).unwrap();
assert_eq!(got, Path::new(&verbatim).join("tool.exe"));
}
#[cfg(debug_assertions)] #[skuld::test]
#[should_panic(expected = "path to complete")]
fn a_nul_reaching_get_full_path_name_is_a_contract_violation() {
use std::os::windows::ffi::OsStringExt;
let p = OsString::from_wide(&"C:\\a\0b".encode_utf16().collect::<Vec<u16>>());
_ = complete_on(Path::new(&p), || unreachable!(), no_drive);
}
const VERBATIM_REWRITES: &[(&str, &str)] = &[
(r"\\?\C:\t\a", r"\\?\C:\t\a"),
(r"\\?\C:\t\a.", r"\\?\C:\t\a"),
(r"\\?\C:\t\x\..\y", r"\\?\C:\t\y"),
(r"\\?\C:\t\a ", r"\\?\C:\t\a"),
];
#[skuld::test]
fn get_full_path_name_rewrites_a_verbatim_path_as_measured() {
for (path, want) in VERBATIM_REWRITES {
assert_eq!(
super::full_path_name(Path::new(path)).unwrap(),
PathBuf::from(want),
"{path:?}"
);
}
}
#[skuld::test]
fn a_verbatim_directory_is_kept_or_refused_never_rewritten() {
for (path, rewritten) in VERBATIM_REWRITES {
match complete_on(Path::new(path), || panic!("must not read the cwd"), no_drive) {
Ok(got) => {
assert_eq!(path, rewritten, "{path:?} is rewritten, so it must be refused");
assert_eq!(got.path, PathBuf::from(path), "{path:?}");
}
Err(Error::Io(e)) => {
assert_ne!(path, rewritten, "{path:?} is not rewritten, so it must be kept: {e}");
assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput, "{path:?}: {e}");
}
Err(other) => panic!("{path:?}: {other:?}"),
}
}
}
#[skuld::test]
fn a_verbatim_exact_token_is_taken_as_written() {
for token in [r"\\?\C:\t\tool.exe.", r"\\?\C:\t\x\..\tool.exe", r"\\?\C:\t\..."] {
let got = absolutise_exact_on(Path::new(token), || panic!("must not read the cwd"), no_drive).unwrap();
assert_eq!(got.path, PathBuf::from(token), "{token:?}");
}
}
#[skuld::test]
fn a_relative_name_on_a_verbatim_process_cwd_is_normalised() {
let cwd = || Ok(PathBuf::from(r"\\?\C:\d"));
let got = absolutise_exact_on(Path::new("tool.exe."), cwd, no_drive).unwrap();
assert_eq!(got.path, PathBuf::from(r"\\?\C:\d\tool.exe"));
let got = super::effective_cwd(Some(Path::new("sub.")), &DriveDirs::new(&empty_env()), cwd).unwrap();
assert_eq!(got, PathBuf::from(r"\\?\C:\d\sub"));
}