Files
craft-libs/crates/atomic/tests/atomic_tests.rs
T
skylartaylor 1d281f38c1 atomic: a write-only file's unreadable attributes are dropped, as documented and tested (review)
On macOS a mode-0200 file's owner can list none of its attribute names either, so they are dropped, as
before attributes were kept. An ACL that denies the data but lets the names be listed still refuses the
save. Both are now tested on macOS. Also: the pages key doc gives the macOS exception for Cmd+Home/End.
2026-11-24 04:48:57 +00:00

148 lines
6.1 KiB
Rust

//! Integration tests of craft-atomic: atomic tests.
#![cfg(unix)]
use craft_atomic::{atomic_write, atomic_write_with};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
struct TempDir(PathBuf);
impl TempDir {
fn new(tag: &str) -> Self {
static N: AtomicU64 = AtomicU64::new(0);
let d = std::env::temp_dir().join(format!("craft-atomic-int-{tag}-{}-{}", std::process::id(), N.fetch_add(1, Ordering::Relaxed)));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).ok();
Self(d)
}
}
impl Drop for TempDir {
fn drop(&mut self) {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(&self.0, std::fs::Permissions::from_mode(0o755));
}
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn assert_no_temp(dir: &Path) {
let leftovers: Vec<_> = std::fs::read_dir(dir)
.ok()
.map(|entries| entries.flatten().map(|e| e.file_name().to_string_lossy().into_owned()).filter(|n| n.ends_with(".craft_tmp")).collect())
.unwrap_or_default();
assert!(leftovers.is_empty(), "temporary files left behind: {leftovers:?}");
}
const ORIGINAL: &[u8] = b"original precious document";
#[cfg(unix)]
#[test]
fn dangling_relative_symlink_stays_a_link() {
let d = TempDir::new("dangling");
std::fs::create_dir(d.0.join("real")).ok();
let link = d.0.join("link.txt");
std::os::unix::fs::symlink("real/doc.txt", &link).ok();
assert!(atomic_write(&link, b"created through the link").is_ok());
assert!(std::fs::symlink_metadata(&link).map(|m| m.file_type().is_symlink()).unwrap_or(false));
assert_eq!(std::fs::read(d.0.join("real/doc.txt")).ok(), Some(b"created through the link".to_vec()));
assert_no_temp(&d.0);
assert_no_temp(&d.0.join("real"));
}
#[cfg(unix)]
#[test]
fn read_only_target_is_not_replaced() {
use std::os::unix::fs::PermissionsExt;
let d = TempDir::new("read_only");
let p = d.0.join("locked.txt");
std::fs::write(&p, ORIGINAL).ok();
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o444)).ok();
// Root may write the file anyway; the check only means something for an ordinary user.
if std::fs::OpenOptions::new().write(true).open(&p).is_ok() {
return;
}
let msg = atomic_write(&p, b"new").err().map(|e| e.to_string()).unwrap_or_default();
assert!(msg.contains("read-only"), "{msg}");
assert_eq!(std::fs::read(&p).ok(), Some(ORIGINAL.to_vec()));
assert_no_temp(&d.0);
}
#[cfg(unix)]
#[test]
fn private_staging_preserves_existing_modes_after_callback() -> io::Result<()> {
use std::fs::Permissions;
use std::os::unix::fs::PermissionsExt;
let d = TempDir::new("private_staging");
std::fs::set_permissions(&d.0, Permissions::from_mode(0o755))?;
for mode in [0o600, 0o640, 0o664, 0o200] {
let p = d.0.join(format!("doc-{mode:o}.craft"));
std::fs::write(&p, ORIGINAL)?;
std::fs::set_permissions(&p, Permissions::from_mode(mode))?;
atomic_write_with(&p, |f| {
assert_eq!(f.metadata()?.permissions().mode() & 0o777, 0o600, "stage must be private before the callback, even for a shared destination");
// Settings writers restrict the stage themselves; this must not change the
// destination mode that atomic replacement promises to preserve.
f.set_permissions(Permissions::from_mode(0o600))?;
f.write_all(b"new private payload")?;
assert_eq!(f.metadata()?.permissions().mode() & 0o777, 0o600, "payload must stay private while writing");
Ok(())
})?;
assert_eq!(std::fs::metadata(&p)?.permissions().mode() & 0o777, mode);
std::fs::set_permissions(&p, Permissions::from_mode(0o600))?;
assert_eq!(std::fs::read(&p)?, b"new private payload");
}
assert_no_temp(&d.0);
Ok(())
}
/// Extended attribute names on `path`, read by path.
#[cfg(target_os = "macos")]
fn xattr_names(path: &Path) -> Vec<Vec<u8>> {
let mut names = vec![0u8; 64 * 1024];
let len = rustix::fs::listxattr(path, names.as_mut_slice()).unwrap_or(0);
names.truncate(len);
names.split(|b| *b == 0).filter(|n| !n.is_empty()).map(<[u8]>::to_vec).collect()
}
/// A write-only file (mode 0200) saves: macOS lets its owner neither read its data nor list its
/// attributes, so its attributes are dropped, as before attributes were kept.
#[cfg(target_os = "macos")]
#[test]
fn a_write_only_file_saves_and_drops_the_attributes_it_cannot_read() -> io::Result<()> {
use std::os::unix::fs::PermissionsExt;
let d = TempDir::new("write_only_xattrs");
let p = d.0.join("doc.craft");
std::fs::write(&p, ORIGINAL)?;
rustix::fs::setxattr(&p, "com.craft.t", b"tag", rustix::fs::XattrFlags::empty())?;
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o200))?;
atomic_write(&p, b"new")?;
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o600))?;
assert_eq!(std::fs::read(&p)?, b"new");
assert!(!xattr_names(&p).iter().any(|n| n == b"com.craft.t"), "the unreadable attribute is dropped");
Ok(())
}
/// A file whose data an ACL denies but whose attribute names can be listed is not replaced: its
/// attributes can't be copied without its data, and the save refuses rather than drop them.
#[cfg(target_os = "macos")]
#[test]
fn an_unreadable_file_with_listable_attributes_is_not_replaced() -> io::Result<()> {
let d = TempDir::new("acl_denied_xattrs");
let p = d.0.join("doc.craft");
std::fs::write(&p, ORIGINAL)?;
rustix::fs::setxattr(&p, "com.craft.t", b"tag", rustix::fs::XattrFlags::empty())?;
let denied = std::process::Command::new("/bin/chmod").arg("+a").arg("everyone deny read").arg(&p).status()?;
assert!(denied.success(), "the ACL is set");
let result = atomic_write(&p, b"new");
let _ = std::process::Command::new("/bin/chmod").arg("-N").arg(&p).status();
assert!(result.is_err(), "the save is refused");
assert_eq!(std::fs::read(&p)?, ORIGINAL, "the original is unchanged");
assert!(xattr_names(&p).iter().any(|n| n == b"com.craft.t"), "and keeps its attribute");
Ok(())
}