use super::*;
#[test]
fn a_replaced_path_does_not_redirect_private_operations() {
let scratch = tempfile::tempdir().unwrap();
let original = scratch.path().join("state");
let moved = scratch.path().join("moved");
super::super::create_private_dir(&original).unwrap();
let dir = PinnedDir::open(&original).unwrap();
let _descendant = dir.create_child(OsStr::new("catalog")).unwrap();
let (held, replacement) = replace_pinned_path(&original, &moved);
if let Some(replacement) = replacement {
PinnedDir::open(replacement)
.unwrap()
.create_child(OsStr::new("catalog"))
.unwrap();
}
let child = dir.child(OsStr::new("catalog")).unwrap();
for content in [b"before".as_slice(), b"after"] {
child
.replace(OsStr::new("settings"), |file| file.write_all(content))
.unwrap();
}
assert_eq!(
std::fs::read(held.join("catalog/settings")).unwrap(),
b"after"
);
assert!(replacement.is_none_or(|path| !path.join("catalog/settings").exists()));
dir.create_child(OsStr::new("tasks")).unwrap();
assert!(held.join("tasks").is_dir());
assert!(!replacement.unwrap_or(&moved).join("tasks").exists());
child.remove(OsStr::new("settings")).unwrap();
assert!(!held.join("catalog/settings").exists());
assert!(
dir.file(OsStr::new("../escape"), PinnedFile::Create)
.is_err()
);
}
#[cfg(unix)]
fn replace_pinned_path<'a>(original: &'a Path, moved: &'a Path) -> (&'a Path, Option<&'a Path>) {
std::fs::rename(original, moved).unwrap();
super::super::create_private_dir(original).unwrap();
(moved, Some(original))
}
#[cfg(windows)]
fn replace_pinned_path<'a>(original: &'a Path, moved: &Path) -> (&'a Path, Option<&'a Path>) {
use windows_sys::Win32::Foundation::{ERROR_ACCESS_DENIED, ERROR_SHARING_VIOLATION};
let error = std::fs::rename(original, moved).unwrap_err();
assert!(
matches!(
error.raw_os_error().map(|code| code as u32),
Some(ERROR_ACCESS_DENIED | ERROR_SHARING_VIOLATION)
),
"{error}"
);
assert!(!moved.try_exists().unwrap());
(original, None)
}
#[cfg(unix)]
#[test]
fn pinned_creation_restores_all_owner_bits_under_a_restrictive_umask() {
let root = tempfile::tempdir().unwrap();
let parent = root.path().join("parent");
super::super::create_private_dir(&parent).unwrap();
let output = std::process::Command::new("/bin/sh")
.args(["-c", "umask 777; exec \"$@\"", "fixture"])
.arg(std::env::current_exe().unwrap())
.args([
"--ignored",
"--exact",
"platform::private::pinned::tests::restricted_umask_child",
])
.env("PINNED_UMASK_TEST", "1")
.current_dir(parent)
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
assert!(root.path().join("parent/child/owner-file").is_file());
}
#[cfg(unix)]
#[test]
#[ignore = "launched only with a restrictive Unix umask"]
fn restricted_umask_child() {
if std::env::var_os("PINNED_UMASK_TEST").is_some() {
let parent = PinnedDir::open(&std::env::current_dir().unwrap()).unwrap();
let child = parent.create_child(OsStr::new("child")).unwrap();
child
.file(OsStr::new("owner-file"), PinnedFile::Create)
.unwrap()
.write_all(b"owned")
.unwrap();
}
}