Refactor chmod_runtime_dir to chmod_dir(path); test uses tempdir
Split the chmod helper so tests can exercise it on a private tempdir instead of mutating the user's XDG_RUNTIME_DIR. The thin chmod_runtime_dir() wrapper still picks runtime_dir() for the production path (called from session.rs).
This commit is contained in:
parent
6028987a6e
commit
0ad3e3335e
@ -468,12 +468,18 @@ pub fn runtime_dir() -> PathBuf {
|
|||||||
/// protocol (including `Command::Type`, which is wide-open text injection
|
/// protocol (including `Command::Type`, which is wide-open text injection
|
||||||
/// into game windows). Permissions are the cheapest defense.
|
/// into game windows). Permissions are the cheapest defense.
|
||||||
pub fn chmod_runtime_dir() {
|
pub fn chmod_runtime_dir() {
|
||||||
|
chmod_dir(&runtime_dir());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `chmod 0o700` an arbitrary directory. Split out from
|
||||||
|
/// [`chmod_runtime_dir`] so tests can exercise it on a tempdir they own
|
||||||
|
/// without mutating the user's live `XDG_RUNTIME_DIR`.
|
||||||
|
pub fn chmod_dir(path: &std::path::Path) {
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
{
|
{
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
let dir = runtime_dir();
|
|
||||||
let _ = std::fs::set_permissions(
|
let _ = std::fs::set_permissions(
|
||||||
&dir,
|
path,
|
||||||
std::fs::Permissions::from_mode(0o700),
|
std::fs::Permissions::from_mode(0o700),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -541,20 +547,24 @@ fn chmod_socket_sets_0o600() {
|
|||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
#[test]
|
#[test]
|
||||||
fn chmod_runtime_dir_sets_0o700() {
|
fn chmod_dir_sets_0o700_on_a_tempdir() {
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
// Save and restore the real dir perms around the test so we don't break
|
// Use a private tempdir so the test never mutates the user's
|
||||||
// the live session if it happens to share XDG_RUNTIME_DIR.
|
// XDG_RUNTIME_DIR (which is what runtime_dir() resolves to).
|
||||||
let dir = runtime_dir();
|
let unique = format!(
|
||||||
let _ = std::fs::create_dir_all(&dir);
|
"enboxer-chmod-{}-{}",
|
||||||
let saved = std::fs::metadata(&dir).ok().map(|m| m.permissions().mode() & 0o777);
|
std::process::id(),
|
||||||
// Force 0o755 so the helper actually has to change it.
|
std::time::SystemTime::now()
|
||||||
let _ = std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755));
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
chmod_runtime_dir();
|
.map(|d| d.as_nanos())
|
||||||
|
.unwrap_or(0)
|
||||||
|
);
|
||||||
|
let dir = std::env::temp_dir().join(unique);
|
||||||
|
std::fs::create_dir_all(&dir).unwrap();
|
||||||
|
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||||
|
chmod_dir(&dir);
|
||||||
let m = std::fs::metadata(&dir).unwrap().permissions().mode() & 0o777;
|
let m = std::fs::metadata(&dir).unwrap().permissions().mode() & 0o777;
|
||||||
assert_eq!(m, 0o700, "expected 0o700, got {m:o}");
|
assert_eq!(m, 0o700, "expected 0o700, got {m:o}");
|
||||||
if let Some(s) = saved {
|
let _ = std::fs::remove_dir(&dir);
|
||||||
let _ = std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(s));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user