use windows::Win32::Foundation::ERROR_ACCESS_DENIED;
use crate::child::Child;
use crate::containment::windows::JobHandle;
use crate::containment::Attached;
use crate::error::{ChildFate, Error};
use crate::identity::{Liveness, ProcessId};
use crate::test_child::{expect_may_have_started_parts, held_uncontained_blocker};
fn refusing_child(kill_on_drop: bool) -> (Child, crate::PipeWriter, ProcessId) {
let (mut child, stdin) = held_uncontained_blocker(kill_on_drop);
child.attached = Attached::JobObject(JobHandle::create_without_set_access_for_test());
let id = child.id().expect(crate::test_child::IDENTITY_READ);
(child, stdin, id)
}
#[track_caller]
fn assert_access_denied(error: &Error) {
let Error::Io(io) = error else {
panic!("expected Io, got {error:?}");
};
assert_eq!(io.raw_os_error(), Some(ERROR_ACCESS_DENIED.0 as i32), "{io:?}");
}
#[skuld::test]
fn a_refused_detach_returns_the_child_and_the_error() {
let (child, _stdin, id) = refusing_child(true);
let (child, error) = child.detach().expect_err("the disarm was refused");
assert_access_denied(&error);
assert_eq!(child.id(), Some(id));
assert_eq!(id.is_alive(), Liveness::Alive, "a refused detach kills nothing");
}
#[skuld::test]
fn the_child_a_refused_detach_gave_back_is_killed_on_drop() {
let (child, _stdin, id) = refusing_child(true);
let (child, _error) = child.detach().expect_err("the disarm was refused");
drop(child);
assert_eq!(id.is_alive(), Liveness::Dead);
}
#[skuld::test]
fn detaching_a_child_that_is_already_disarmed_is_ok_without_asking_the_job() {
let (child, stdin, id) = refusing_child(false);
child.detach().expect("the child was already disarmed");
assert_eq!(id.is_alive(), Liveness::Alive);
drop(stdin);
}
#[skuld::test]
fn a_refused_disarm_fails_a_kill_on_drop_false_spawn_and_kills_the_child() {
let (child, stdin, id) = refusing_child(false);
let failure = child.commit_kill_on_drop().expect_err("the disarm was refused");
let (cause, fate, handed_back) = expect_may_have_started_parts(failure.into(), stdin);
assert_access_denied(&cause);
assert_eq!(fate, ChildFate::Reaped);
assert!(handed_back.is_none(), "the teardown handed the child back");
assert_eq!(id.is_alive(), Liveness::Dead);
}
#[skuld::test]
fn a_kill_on_drop_spawn_is_not_disarmed() {
let (child, _stdin, id) = refusing_child(true);
let child = child.commit_kill_on_drop().expect("nothing was asked of the job");
assert_eq!(id.is_alive(), Liveness::Alive);
drop(child);
}