use super::*;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
fn invalid_input_message(r: Result<Anchored, Error>) -> String {
match r {
Err(Error::Io(e)) if e.kind() == std::io::ErrorKind::InvalidInput => e.to_string(),
other => panic!("expected Io(InvalidInput), got {other:?}"),
}
}
#[skuld::test]
fn an_absolute_program_is_returned_unchanged() {
let got = anchor_posix(OsStr::new("/usr/bin/id"), Some(Path::new("rel"))).unwrap();
assert_eq!(got.program, Path::new("/usr/bin/id"));
}
#[skuld::test]
fn no_extension_is_appended_and_no_existence_is_checked() {
let got = anchor_posix(OsStr::new("no-such-file-983471"), Some(Path::new("/nonexistent-dir"))).unwrap();
assert_eq!(got.program.as_os_str(), OsStr::new("./no-such-file-983471"));
}
#[skuld::test]
fn backslashes_and_drive_letters_are_ordinary_bytes() {
let got = anchor_posix(OsStr::new(r"C:\t\tool"), Some(Path::new("/work"))).unwrap();
assert_eq!(got.program.as_os_str(), OsStr::new(r"./C:\t\tool"));
assert!(got.enter);
}
#[skuld::test]
fn a_name_that_names_no_file_is_refused() {
for n in ["", ".", "..", "dir/", "a/.", "a/..", "/"] {
let msg = invalid_input_message(anchor_posix(OsStr::new(n), None));
assert!(msg.contains("names no file"), "{n:?}: {msg}");
}
}
#[skuld::test]
fn an_interior_nul_is_refused_and_named() {
for n in ["to\0ol", "a\0b", "x\0/"] {
let msg = invalid_input_message(anchor_posix(OsStr::new(n), None));
assert!(msg.contains("NUL"), "{n:?}: {msg}");
assert!(!msg.contains('\0'), "the refusal must not carry a raw NUL: {msg:?}");
}
}
#[skuld::test]
fn a_relative_program_is_anchored_to_the_childs_cwd() {
for (program, want) in [("tool", "./tool"), ("bin/tool", "bin/tool"), ("./tool", "./tool")] {
let got = anchor_posix(OsStr::new(program), Some(Path::new("sub"))).unwrap();
assert_eq!(got.program.as_os_str(), OsStr::new(want), "{program}");
assert_eq!(
got,
Anchored {
program: PathBuf::from(want),
cwd: Some(PathBuf::from("sub")),
enter: true,
}
);
}
}
#[skuld::test]
fn an_absolute_program_is_not_anchored() {
let got = anchor_posix(OsStr::new("/usr/bin/id"), Some(Path::new("sub"))).unwrap();
assert!(!got.enter);
assert_eq!(got.program, Path::new("/usr/bin/id"));
}