use super::check_parent;
use std::cell::Cell;
use std::fs;
use std::os::unix::{fs::PermissionsExt, net::UnixListener};
thread_local! {
pub(super) static EFFECTIVE_UID: Cell<u32> = Cell::new(rustix::process::geteuid().as_raw());
}
#[test]
fn round2_socket_owned_by_another_user_is_refused_without_connecting()
-> Result<(), Box<dyn std::error::Error>> {
let folder = tempfile::Builder::new()
.prefix("door-owner-")
.tempdir_in("/tmp")?;
fs::set_permissions(folder.path(), fs::Permissions::from_mode(0o777))?;
let path = folder.path().join("assistant.sock");
let listener = UnixListener::bind(&path)?;
listener.set_nonblocking(true)?;
let user = rustix::process::geteuid().as_raw();
EFFECTIVE_UID.with(|uid| uid.set(user ^ 1));
let result = check_parent(&path);
EFFECTIVE_UID.with(|uid| uid.set(user));
assert!(result.is_err(), "another user's socket was accepted");
let error = result.err().ok_or("socket refusal missing")?;
assert_eq!(error.code, "assistant_socket_exists");
assert_eq!(error.message, "the existing path is not an owned socket");
assert_eq!(
listener.accept().err().ok_or("socket was asked")?.kind(),
std::io::ErrorKind::WouldBlock
);
Ok(())
}