use super::{BackendSet, Host, Os, Transition};
use crate::elevation::{Auth, Backend, Privilege};
use crate::error::{ElevationErrorKind, Error};
use std::path::PathBuf;
fn reject_error(t: Transition) -> Error {
match t {
Transition::Reject { error } => error,
other => panic!("expected Reject, got {other:?}"),
}
}
fn is_unsupported(t: Transition) -> bool {
matches!(
t,
Transition::Reject {
error: Error::Unsupported { .. }
}
)
}
fn win_host(elevated: bool) -> Host {
Host {
elevated,
has_tty: false,
available: BackendSet::default(),
os: Os::Windows,
arg_max: None,
pkexec_version: crate::elevation::pkexec::PkexecVersion::NotProbed,
pkexec_pin: None,
}
}
fn all_backends() -> BackendSet {
BackendSet {
sudo: Some(PathBuf::from("/usr/bin/sudo")),
doas: Some(PathBuf::from("/usr/bin/doas")),
pkexec: Some(PathBuf::from("/usr/bin/pkexec")),
osascript: None,
}
}
fn unix_host(available: BackendSet, elevated: bool, has_tty: bool) -> Host {
Host {
elevated,
has_tty,
available,
os: Os::Linux,
arg_max: None,
pkexec_version: crate::elevation::pkexec::PkexecVersion::NotProbed,
pkexec_pin: None,
}
}
#[skuld::test]
fn unprivileged_target_runs_as_is() {
let h = unix_host(all_backends(), false, true);
assert!(matches!(
h.plan(Privilege::Unprivileged, Backend::Auto, Auth::Interactive),
Transition::RunAsIs
));
}
#[skuld::test]
fn already_elevated_runs_as_is() {
let h = unix_host(all_backends(), true, true);
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Auto, Auth::Interactive),
Transition::RunAsIs
));
}
#[skuld::test]
fn auto_prefers_sudo_then_doas() {
let h = unix_host(all_backends(), false, true);
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Auto, Auth::Interactive),
Transition::ElevatePosix {
backend: Backend::Sudo,
..
}
));
let h = unix_host(
BackendSet {
sudo: None,
doas: Some(PathBuf::from("/usr/bin/doas")),
pkexec: None,
osascript: None,
},
false,
true,
);
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Auto, Auth::Interactive),
Transition::ElevatePosix {
backend: Backend::Doas,
..
}
));
}
#[skuld::test]
fn auto_never_selects_pkexec() {
let h = unix_host(
BackendSet {
sudo: None,
doas: None,
pkexec: Some(PathBuf::from("/usr/bin/pkexec")),
osascript: None,
},
false,
true,
);
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Auto, Auth::Interactive),
Transition::Reject { .. }
));
}
#[skuld::test]
fn resolved_transition_carries_the_absolute_backend_path() {
let h = unix_host(all_backends(), false, true);
match h.plan(Privilege::Elevated, Backend::Doas, Auth::Interactive) {
Transition::ElevatePosix {
backend: Backend::Doas,
path,
..
} => {
assert_eq!(path, PathBuf::from("/usr/bin/doas"));
}
other => panic!("expected doas ElevatePosix, got {other:?}"),
}
}
#[skuld::test]
fn windows_unprivileged_elevates_via_uac() {
let h = Host {
elevated: false,
has_tty: false,
available: BackendSet::default(),
os: Os::Windows,
arg_max: None,
pkexec_version: crate::elevation::pkexec::PkexecVersion::NotProbed,
pkexec_pin: None,
};
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Auto, Auth::Interactive),
Transition::ElevateWindows { .. }
));
}
#[skuld::test]
fn structural_posix_matrix_is_privilege_independent() {
let cases: &[(Backend, Auth)] = &[
(Backend::Doas, Auth::Askpass(PathBuf::from("/x"))),
(Backend::Doas, Auth::Stdin(crate::elevation::Secret::new("p"))),
(Backend::Pkexec, Auth::Interactive),
(Backend::Pkexec, Auth::NonInteractive),
(Backend::Pkexec, Auth::Askpass(PathBuf::from("/x"))),
(Backend::Sudo, Auth::Gui),
(Backend::Doas, Auth::Gui),
(Backend::Auto, Auth::Gui),
];
for (backend, auth) in cases {
for elevated in [false, true] {
let h = unix_host(all_backends(), elevated, true);
assert!(
is_unsupported(h.plan(Privilege::Elevated, *backend, auth.clone())),
"expected Unsupported for {backend:?} + {auth:?} (elevated={elevated})",
);
}
}
}
#[skuld::test]
fn structural_windows_matrix_is_privilege_independent() {
for elevated in [false, true] {
assert!(is_unsupported(win_host(elevated).plan(
Privilege::Elevated,
Backend::Sudo,
Auth::Interactive
)));
assert!(is_unsupported(win_host(elevated).plan(
Privilege::Elevated,
Backend::Auto,
Auth::NonInteractive
)));
assert!(is_unsupported(win_host(elevated).plan(
Privilege::Elevated,
Backend::Auto,
Auth::Askpass(PathBuf::from("/x"))
)));
assert!(is_unsupported(win_host(elevated).plan(
Privilege::Elevated,
Backend::Auto,
Auth::Stdin(crate::elevation::Secret::new("p"))
)));
}
}
#[skuld::test]
fn windows_accepts_only_interactive_and_gui() {
assert!(matches!(
win_host(false).plan(Privilege::Elevated, Backend::Auto, Auth::Interactive),
Transition::ElevateWindows { .. }
));
assert!(matches!(
win_host(false).plan(Privilege::Elevated, Backend::Auto, Auth::Gui),
Transition::ElevateWindows { .. }
));
}
#[skuld::test]
fn interactive_without_tty_is_no_tty() {
let h = unix_host(all_backends(), false, false);
let e = reject_error(h.plan(Privilege::Elevated, Backend::Sudo, Auth::Interactive));
assert!(
matches!(
e,
Error::Elevation {
kind: ElevationErrorKind::NoTty,
..
}
),
"{e}"
);
}
#[skuld::test]
fn noninteractive_without_tty_is_allowed() {
let h = unix_host(all_backends(), false, false);
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Sudo, Auth::NonInteractive),
Transition::ElevatePosix {
backend: Backend::Sudo,
..
}
));
}
#[skuld::test]
fn auto_resolving_to_doas_rejects_stdin() {
for elevated in [false, true] {
let h = unix_host(
BackendSet {
sudo: None,
doas: Some(PathBuf::from("/usr/bin/doas")),
pkexec: None,
osascript: None,
},
elevated,
true,
);
assert!(
is_unsupported(h.plan(
Privilege::Elevated,
Backend::Auto,
Auth::Stdin(crate::elevation::Secret::new("p"))
)),
"expected Unsupported for Auto+Stdin on a doas-only host (elevated={elevated})",
);
}
}
#[skuld::test]
fn pkexec_with_gui_is_accepted() {
let h = unix_host(all_backends(), false, true);
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Pkexec, Auth::Gui),
Transition::ElevatePosix {
backend: Backend::Pkexec,
..
}
));
}
#[skuld::test]
fn pkexec_off_linux_is_refused_naming_linux() {
for elevated in [false, true] {
let h = Host {
os: Os::Unix,
..unix_host(all_backends(), elevated, true)
};
match reject_error(h.plan(Privilege::Elevated, Backend::Pkexec, Auth::Gui)) {
Error::Unsupported { detail, .. } => {
assert!(detail.contains("Linux"), "{detail}");
assert!(!detail.contains("121"), "{detail}");
}
other => panic!("expected Unsupported, got {other:?}"),
}
}
}
#[skuld::test]
fn an_unresolvable_pkexec_says_so_rather_than_not_on_path() {
let mut h = unix_host(
BackendSet {
pkexec: None,
..all_backends()
},
false,
true,
);
h.pkexec_version = crate::elevation::pkexec::PkexecVersion::Unresolved {
path: "/opt/bin/pkexec".into(),
error: "Too many levels of symbolic links (os error 40)".into(),
};
match reject_error(h.plan(Privilege::Elevated, Backend::Pkexec, Auth::Gui)) {
Error::Elevation {
kind: ElevationErrorKind::BackendUnavailable,
detail,
} => {
assert!(detail.contains("/opt/bin/pkexec"), "{detail}");
assert!(detail.contains("Too many levels of symbolic links"), "{detail}");
assert!(!detail.contains("not on PATH"), "{detail}");
}
other => panic!("expected BackendUnavailable, got {other:?}"),
}
}
fn macos_host(elevated: bool, has_tty: bool) -> Host {
Host {
elevated,
has_tty,
available: BackendSet {
sudo: Some(PathBuf::from("/usr/bin/sudo")),
doas: None,
pkexec: None,
osascript: Some(PathBuf::from("/usr/bin/osascript")),
},
os: Os::MacOs,
arg_max: Some(1_048_576),
pkexec_version: crate::elevation::pkexec::PkexecVersion::NotProbed,
pkexec_pin: None,
}
}
fn unsupported_platform(t: Transition) -> &'static str {
match reject_error(t) {
Error::Unsupported { platform, .. } => platform,
other => panic!("expected Unsupported, got {other}"),
}
}
#[skuld::test]
fn macos_pkexec_is_unsupported_not_backend_unavailable() {
let mut h = macos_host(false, true);
h.available.pkexec = Some(PathBuf::from("/usr/bin/pkexec"));
assert_eq!(
unsupported_platform(h.plan(Privilege::Elevated, Backend::Pkexec, Auth::Gui)),
"macos"
);
}
#[skuld::test]
fn macos_keeps_the_backends_that_really_do_run_there() {
let mut h = macos_host(false, true);
h.available.doas = Some(PathBuf::from("/usr/local/bin/doas"));
for backend in [Backend::Sudo, Backend::Doas] {
assert!(
matches!(
h.plan(Privilege::Elevated, backend, Auth::Interactive),
Transition::ElevatePosix { .. }
),
"{backend:?} must still be usable on macOS"
);
}
}
#[skuld::test]
fn macos_sudo_still_works_like_other_unix() {
let h = macos_host(false, true);
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Auto, Auth::Interactive),
Transition::ElevatePosix {
backend: Backend::Sudo,
..
}
));
}
#[skuld::test]
fn non_macos_unix_gui_still_requires_pkexec() {
let h = unix_host(all_backends(), false, true);
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Pkexec, Auth::Gui),
Transition::ElevatePosix {
backend: Backend::Pkexec,
..
}
));
assert!(is_unsupported(h.plan(Privilege::Elevated, Backend::Sudo, Auth::Gui)));
}
#[skuld::test]
fn macos_gui_resolves_to_the_osascript_transition() {
let h = macos_host(false, false);
match h.plan(Privilege::Elevated, Backend::Auto, Auth::Gui) {
Transition::ElevateMacosGui { osascript, arg_max } => {
assert_eq!(osascript, PathBuf::from("/usr/bin/osascript"));
assert_eq!(arg_max, Some(1_048_576));
}
other => panic!("expected ElevateMacosGui, got {other:?}"),
}
}
#[skuld::test]
fn macos_gui_needs_no_controlling_terminal() {
let h = macos_host(false, false);
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Auto, Auth::Gui),
Transition::ElevateMacosGui { .. }
));
assert!(matches!(
reject_error(h.plan(Privilege::Elevated, Backend::Auto, Auth::Interactive)),
Error::Elevation {
kind: ElevationErrorKind::NoTty,
..
}
));
}
#[skuld::test]
fn macos_gui_with_a_non_auto_backend_is_still_unsupported() {
for backend in [Backend::Sudo, Backend::Doas] {
let h = macos_host(false, true);
match reject_error(h.plan(Privilege::Elevated, backend, Auth::Gui)) {
Error::Unsupported { platform, detail, .. } => {
assert_eq!(platform, "macos", "{backend:?}");
assert!(detail.contains("Backend::Auto"), "{backend:?}: {detail}");
}
other => panic!("expected Unsupported for {backend:?}, got {other}"),
}
}
}
#[skuld::test]
fn macos_gui_without_osascript_is_a_backend_problem_not_a_platform_one() {
let mut h = macos_host(false, true);
h.available.osascript = None;
assert!(matches!(
reject_error(h.plan(Privilege::Elevated, Backend::Auto, Auth::Gui)),
Error::Elevation {
kind: ElevationErrorKind::BackendUnavailable,
..
}
));
}
#[skuld::test]
fn macos_gui_already_elevated_runs_as_is() {
let h = macos_host(true, false);
assert!(matches!(
h.plan(Privilege::Elevated, Backend::Auto, Auth::Gui),
Transition::RunAsIs
));
}