use super::super::{detect_with, find_fixed_front, front_of};
#[cfg(target_os = "linux")]
use super::super::{rewrite_with_host, run_version_probe};
use crate::elevation::plan::{FrontState, Host, Os, Transition};
use crate::elevation::{Auth, Backend, Privilege};
use crate::error::{ElevationErrorKind, Error};
use std::ffi::OsString;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
fn plant(dir: &Path, name: &str, mode: u32) -> PathBuf {
let path = dir.join(name);
std::fs::write(&path, b"#!/bin/sh\n").unwrap();
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(mode)).unwrap();
path
}
fn looping_entry(root: &Path) -> PathBuf {
let entry = root.join("loop");
std::os::unix::fs::symlink("loop", &entry).unwrap();
let e = std::fs::metadata(entry.join("sudo")).unwrap_err();
assert_eq!(
e.raw_os_error(),
Some(libc::ELOOP),
"precondition: the entry cannot be examined"
);
entry
}
fn path_of(dirs: &[&Path]) -> OsString {
std::env::join_paths(dirs).unwrap()
}
fn detect(path_var: &OsString, backend: Backend, auth: &Auth) -> Host {
detect_with(
Some(path_var.as_os_str()),
Os::Linux,
false,
backend,
auth,
|_| panic!("canonicalised a pkexec"),
|_| panic!("probed a pkexec"),
)
}
fn is_found(state: &FrontState, path: &Path) -> bool {
matches!(state, FrontState::Found(found) if found == path)
}
fn is_unexamined_by(state: &FrontState, code: i32) -> bool {
matches!(state, FrontState::Unexamined(error) if crate::error::first_os_code(error) == Some(code))
}
fn unavailable(plan: Transition) -> (String, Option<i32>, String) {
match plan {
Transition::Reject { error } => match &error {
Error::Elevation {
kind: ElevationErrorKind::BackendUnavailable,
detail,
source,
} => (
detail.clone(),
source.as_ref().and_then(|s| crate::error::first_os_code(&**s)),
crate::error::Report(&error).to_string(),
),
other => panic!("expected BackendUnavailable, got {other:?}"),
},
other => panic!("expected BackendUnavailable, got {other:?}"),
}
}
#[skuld::test]
fn the_file_the_lookup_finds_is_the_front() {
let dir = tempfile::tempdir().unwrap();
let sudo = plant(dir.path(), "sudo", 0o644);
let doas = plant(dir.path(), "doas", 0o755);
let pkexec = plant(dir.path(), "pkexec", 0o644);
let path = path_of(&[dir.path()]);
let host = detect(&path, Backend::Auto, &Auth::NonInteractive);
assert!(is_found(&host.available.sudo, &sudo), "{:?}", host.available.sudo);
assert!(is_found(&host.available.doas, &doas), "{:?}", host.available.doas);
assert!(is_found(&host.available.pkexec, &pkexec), "{:?}", host.available.pkexec);
match host.plan(Privilege::Elevated, Backend::Auto, Auth::NonInteractive) {
Transition::ElevatePosix { backend, path, .. } => assert_eq!((backend, path), (Backend::Sudo, sudo)),
other => panic!("expected the sudo found, got {other:?}"),
}
}
#[skuld::test]
fn a_miss_is_an_absent_front_without_an_error() {
let empty = tempfile::tempdir().unwrap();
let path = path_of(&[empty.path()]);
let host = detect(&path, Backend::Auto, &Auth::NonInteractive);
for front in [&host.available.sudo, &host.available.doas, &host.available.pkexec] {
assert!(matches!(front, FrontState::Absent), "{front:?}");
}
let (detail, cause, _) = unavailable(host.plan(Privilege::Elevated, Backend::Auto, Auth::NonInteractive));
assert!(detail.contains("no sudo/doas on PATH"), "{detail}");
assert_eq!(cause, None);
}
#[skuld::test]
fn a_stop_whose_cause_is_not_found_is_not_an_absent_front() {
let cause = std::io::Error::new(std::io::ErrorKind::NotFound, "a cause that reads as a miss");
let stop = Error::Io(crate::error::io_context("could not tell, so the search stops", cause));
let state = front_of(Err(stop));
assert!(matches!(state, FrontState::Unexamined(_)), "{state:?}");
let miss = Error::Io(std::io::Error::from_raw_os_error(libc::ENOENT));
let state = front_of(Err(miss));
assert!(matches!(state, FrontState::Absent), "{state:?}");
}
#[skuld::test]
fn an_error_that_is_not_a_miss_is_the_cause_when_the_plan_picks_the_front() {
let root = tempfile::tempdir().unwrap();
let looping = looping_entry(root.path());
let good = root.path().join("good");
std::fs::create_dir(&good).unwrap();
plant(&good, "sudo", 0o755);
let path = path_of(&[&looping, &good]);
for backend in [Backend::Auto, Backend::Sudo] {
let host = detect(&path, backend, &Auth::NonInteractive);
assert!(
is_unexamined_by(&host.available.sudo, libc::ELOOP),
"{backend:?}: {:?}",
host.available.sudo
);
let (detail, cause, report) = unavailable(host.plan(Privilege::Elevated, backend, Auth::NonInteractive));
assert!(detail.contains("could not be found or opened"), "{backend:?}: {detail}");
assert_eq!(cause, Some(libc::ELOOP), "{backend:?}");
assert!(report.contains(&format!("{looping:?}")), "{backend:?}: {report}");
}
}
#[skuld::test]
fn an_error_that_is_not_a_miss_is_the_cause_when_the_plan_picks_pkexec() {
let root = tempfile::tempdir().unwrap();
let looping = looping_entry(root.path());
let good = root.path().join("good");
std::fs::create_dir(&good).unwrap();
plant(&good, "pkexec", 0o755);
let path = path_of(&[&looping, &good]);
let host = detect(&path, Backend::Pkexec, &Auth::Gui);
assert!(
is_unexamined_by(&host.available.pkexec, libc::ELOOP),
"{:?}",
host.available.pkexec
);
assert!(host.pkexec_pin.is_none());
let (_, cause, _) = unavailable(host.plan(Privilege::Elevated, Backend::Pkexec, Auth::Gui));
assert_eq!(cause, Some(libc::ELOOP));
}
#[skuld::test]
fn an_error_for_a_front_the_plan_does_not_pick_fails_nothing() {
let root = tempfile::tempdir().unwrap();
let looping = looping_entry(root.path());
let good = root.path().join("good");
std::fs::create_dir(&good).unwrap();
let sudo = plant(&good, "sudo", 0o755);
let path = path_of(&[&good, &looping]);
for backend in [Backend::Auto, Backend::Sudo] {
let host = detect(&path, backend, &Auth::NonInteractive);
assert!(
is_found(&host.available.sudo, &sudo),
"{backend:?}: {:?}",
host.available.sudo
);
for front in [&host.available.doas, &host.available.pkexec] {
assert!(
is_unexamined_by(front, libc::ELOOP),
"{backend:?}: the searches that went past the match stop on the entry: {front:?}"
);
}
match host.plan(Privilege::Elevated, backend, Auth::NonInteractive) {
Transition::ElevatePosix {
backend: picked, path, ..
} => {
assert_eq!((picked, path), (Backend::Sudo, sudo.clone()), "{backend:?}")
}
other => panic!("{backend:?}: expected the sudo found, got {other:?}"),
}
}
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_pkexec_without_the_execute_bit_is_backend_unavailable_with_the_launch_error() {
let dir = tempfile::tempdir().unwrap();
plant(dir.path(), "pkexec", 0o644);
let path = path_of(&[dir.path()]);
let host = detect_with(
Some(path.as_os_str()),
Os::Linux,
false,
Backend::Pkexec,
&Auth::Gui,
|p| std::fs::canonicalize(p),
run_version_probe,
);
let mut cmd = crate::test_child::command(crate::test_child::Console::Hidden);
cmd.arg("id")
.elevation_backend(Backend::Pkexec)
.elevation_auth(Auth::Gui);
match rewrite_with_host(&mut cmd, &host) {
Err(Error::Elevation {
kind: ElevationErrorKind::BackendUnavailable,
source: Some(cause),
..
}) => assert_eq!(crate::error::first_os_code(&cause), Some(libc::EACCES)),
Err(other) => panic!("expected BackendUnavailable caused by EACCES, got {other:?}"),
Ok(_) => panic!("expected BackendUnavailable caused by EACCES, got a rewrite"),
}
}
#[skuld::test]
fn a_sudo_without_the_execute_bit_is_found_and_its_failed_launch_is_backend_unavailable() {
let dir = tempfile::tempdir().unwrap();
let sudo = plant(dir.path(), "sudo", 0o644);
let path = path_of(&[dir.path()]);
let host = detect(&path, Backend::Sudo, &Auth::NonInteractive);
let Transition::ElevatePosix { path: front, .. } =
host.plan(Privilege::Elevated, Backend::Sudo, Auth::NonInteractive)
else {
panic!("expected the sudo found to be planned");
};
assert_eq!(front, sudo);
let launch = crate::test_spawn::spawn(&mut std::process::Command::new(&front))
.expect_err("a file without the execute bit cannot be launched");
match crate::elevation::remap_derived_spawn_error(Error::Io(launch), &front) {
Error::Elevation {
kind: ElevationErrorKind::BackendUnavailable,
source: Some(cause),
..
} => assert_eq!(crate::error::first_os_code(&cause), Some(libc::EACCES)),
other => panic!("expected BackendUnavailable caused by EACCES, got {other:?}"),
}
}
#[skuld::test]
fn a_fixed_front_is_found_absent_or_unexamined() {
let root = tempfile::tempdir().unwrap();
let file = plant(root.path(), "osascript", 0o644);
assert!(is_found(&find_fixed_front(&file), &file));
for absent in [root.path().join("missing"), root.path().to_path_buf()] {
let state = find_fixed_front(&absent);
assert!(matches!(state, FrontState::Absent), "{absent:?}: {state:?}");
}
let unreadable = looping_entry(root.path()).join("osascript");
let state = find_fixed_front(&unreadable);
assert!(is_unexamined_by(&state, libc::ELOOP), "{state:?}");
let FrontState::Unexamined(cause) = &state else {
unreachable!("{state:?}")
};
let report = crate::error::Report(&**cause).to_string();
assert!(
report.contains(&format!("{unreadable:?}")),
"{unreadable:?} is not named: {report}"
);
}