use crate::error::Error;
use crate::identity::{HostKernel, ProcessId, Resolved};
#[cfg(unix)]
use crate::test_child::{live_exiting_member as live_member, release_unsignalled as release};
#[cfg(windows)]
use crate::test_child::{live_findstr_blocker as live_member, release_findstr_unterminated as release};
use crate::Recursive;
#[cfg(windows)]
#[skuld::test]
fn parent_and_children_of_an_unassessable_anchor_are_unassessable() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(parent_and_children_of_an_unassessable_anchor_are_unassessable),
crate::test_spawn::spawn,
) else {
return;
};
use windows::Win32::System::Threading::PROCESS_SYNCHRONIZE;
let child = crate::identity::windows_fixture::spawn_restricted(&done, PROCESS_SYNCHRONIZE.0);
let id = crate::identity::windows_identity_from_handle_result(child.handle(), child.pid())
.expect("the owned handle always yields an identity");
assert!(child.is_running(), "precondition: the subject must be live");
assert_eq!(
id.exists(),
crate::identity::Existence::Unknown,
"precondition: unassessable"
);
assert!(matches!(id.parent(), Err(Error::Unassessable { .. })));
for recursive in [Recursive::No, Recursive::Yes] {
assert!(matches!(id.children(recursive), Err(Error::Unassessable { .. })));
}
}
#[cfg(windows)]
#[skuld::test]
fn parent_of_a_process_whose_parent_is_access_denied_is_unassessable_naming_the_ppid() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(
parent_of_a_process_whose_parent_is_access_denied_is_unassessable_naming_the_ppid
),
crate::test_spawn::spawn,
) else {
return;
};
use windows::Win32::System::Threading::PROCESS_SYNCHRONIZE;
let parent = crate::identity::windows_fixture::spawn_restricted_shell(&done, PROCESS_SYNCHRONIZE.0);
let kid_pid = parent.wait_for_child();
assert!(parent.is_running(), "precondition: the denied parent must be live");
let crate::identity::Resolved::Found(kid) = ProcessId::of(kid_pid) else {
panic!("the grandchild is not DACL-restricted, so it must resolve");
};
assert_eq!(
ProcessId::of(parent.pid()),
crate::identity::Resolved::Unknown,
"precondition: the parent must be unassessable"
);
match kid.parent() {
Err(Error::Unassessable { detail, .. }) => {
assert!(detail.contains(&format!("ppid {}", parent.pid())), "{detail}");
}
other => panic!("an unassessable ppid must be Unassessable, got {other:?}"),
}
}
#[skuld::test]
fn parent_and_children_of_a_gone_anchor_are_ok_and_empty() {
let real = ProcessId::current();
let stale = real.with_other_value();
assert!(matches!(stale.parent(), Ok(None)));
for recursive in [Recursive::No, Recursive::Yes] {
assert_eq!(stale.children(recursive).expect("gone is not an error"), Vec::new());
}
}
#[cfg(unix)]
#[skuld::test]
fn an_anchor_that_is_access_denied_is_unassessable_blaming_access() {
let (child, id) = live_member();
#[cfg(target_os = "linux")]
let id = crate::identity::ticks_id(id.pid());
let expected = format!("pid {} exists but could not be queried", id.pid());
for result in [
id.parent_with(&crate::identity::unreadable(id.pid())).map(|_| ()),
id.children_with(&crate::identity::unreadable_anchor(id.pid()), Recursive::No)
.map(|_| ()),
id.children_with(&crate::identity::unreadable_anchor(id.pid()), Recursive::Yes)
.map(|_| ()),
] {
match result {
Err(Error::Unassessable { detail, .. }) => assert!(detail.contains(&expected), "{detail}"),
other => panic!("expected Unassessable, got {other:?}"),
}
}
release(child);
}
#[cfg(unix)]
#[skuld::test]
fn a_parent_that_is_access_denied_is_unassessable_naming_the_ppid() {
let (child, id) = live_member();
let me = std::process::id();
let kernel = crate::identity::unreadable(me);
match id.parent_with(&kernel) {
Err(Error::Unassessable { detail, .. }) => {
assert!(
detail.contains(&format!("ppid {me} exists but could not be queried")),
"{detail}"
)
}
other => panic!("expected Unassessable, got {other:?}"),
}
release(child);
}
#[skuld::test]
fn the_parent_of_a_spawned_child_is_the_current_process() {
let (child, kid) = live_member();
let parent = kid.parent().expect("the parent is readable");
release(child);
assert_eq!(parent, Some(ProcessId::current()));
}
#[cfg(target_os = "macos")]
#[skuld::test]
fn parent_reads_its_process_again_under_the_parent() {
use crate::identity::FakeKernel;
let (child, kid) = live_member();
let kernel = FakeKernel::over(&HostKernel);
let parent = kid.parent_with(&kernel).expect("the parent is readable");
release(child);
assert_eq!(parent, Some(ProcessId::current()));
assert_eq!(kernel.record_reads(kid.pid()), 2, "the child, read twice");
assert_eq!(
kernel.record_reads(std::process::id()),
2,
"the parent, read once and proved held once"
);
}
#[skuld::test]
fn a_candidate_is_gone_under_a_parent_that_did_not_create_it() {
let (x_child, x) = live_member();
let (y_child, y) = live_member();
let (read, _) = ProcessId::of_ordered_with(&HostKernel, y.pid(), Some(x), None, None);
release(y_child);
release(x_child);
assert_eq!(read, Resolved::Gone);
}
#[skuld::test]
fn a_candidate_is_gone_under_the_right_parent_pid_with_another_key() {
let (child, kid) = live_member();
let other_parent = Some(ProcessId::current().with_other_value());
let (read, _) = ProcessId::of_ordered_with(&HostKernel, kid.pid(), other_parent, None, None);
release(child);
assert_eq!(read, Resolved::Gone);
}
#[cfg(unix)]
#[skuld::test]
fn a_candidate_is_unknown_when_its_parent_cannot_be_proved() {
let (child, kid) = live_member();
let me = std::process::id();
#[cfg(target_os = "linux")]
let parent = crate::identity::ticks_id(me);
#[cfg(not(target_os = "linux"))]
let parent = ProcessId::current();
let kernel = crate::identity::unreadable(me);
let (read, cause) = ProcessId::of_ordered_with(&kernel, kid.pid(), Some(parent), None, None);
release(child);
assert_eq!(read, Resolved::Unknown);
assert_eq!(cause.blamed_parent(), Some(me));
}
#[cfg(windows)]
#[skuld::test]
fn the_parent_of_an_exited_process_whose_handle_is_held_is_its_creator() {
let (mut child, id) = live_member();
child.kill().expect("terminate the child");
child.wait().expect("the child's exit is recorded");
crate::test_child::parents_once_unlisted(id.pid());
assert_eq!(
id.parent().expect("the creator is readable"),
Some(ProcessId::current())
);
}
#[skuld::test]
fn children_of_the_current_process_contain_a_spawned_child() {
let (child, kid) = live_member();
let direct = ProcessId::current()
.children(Recursive::No)
.expect("children are enumerable");
assert!(direct.contains(&kid), "{kid:?} not among {direct:?}");
release(child);
}