cosca 0.9.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
use windows::Win32::Foundation::ERROR_ACCESS_DENIED;
use windows::Win32::System::Threading::{
    PROCESS_ACCESS_RIGHTS, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_SYNCHRONIZE, PROCESS_TERMINATE,
};

use super::{creation_token, current_token, exit_look, is_running, open_dropping_refused, start_token, Look, Opened};
use crate::identity::windows_fixture::{spawn_restricted, spawn_restricted_shell};
use crate::identity::{refusing_the_next_terminate, HostKernel, Key, Liveness, Resolved};
use crate::os_process::windows::CREATE_PROCESS_ACCESS;

/// One look at whether `id`'s process has exited, through the open: gone or recycled
/// is exited.
fn exited_now(id: crate::identity::ProcessId) -> Result<bool, crate::error::Error> {
    crate::test_child::exited_through_open(id, Some(std::time::Duration::ZERO))
}

#[skuld::test]
fn start_token_of_current_process_is_stable() {
    let pid = std::process::id();
    let a = start_token(pid);
    assert!(matches!(a, Resolved::Found(_)), "our own token must resolve: {a:?}");
    assert_eq!(a, start_token(pid), "the same pid must yield the same token");
}

/// `current_token` reads the pseudo-handle, so it must agree with the by-pid read on a
/// process that permits both — and, unlike it, can never be denied.
#[skuld::test]
fn current_token_agrees_with_the_by_pid_read() {
    assert_eq!(current_token(), start_token(std::process::id()));
}

#[skuld::test]
fn is_running_alive_for_self_dead_for_wrong_token() {
    let pid = std::process::id();
    let Resolved::Found(tok) = start_token(pid) else {
        panic!("our own token must resolve");
    };
    assert_eq!(is_running(pid, tok), Liveness::Alive, "we are obviously running");
    let wrong = Key::from_raw(tok.raw().wrapping_add(1));
    assert_eq!(
        is_running(pid, wrong),
        Liveness::Dead,
        "a wrong token is a different process"
    );
}

/// A LIVE process we may not open at all: ERROR_ACCESS_DENIED must NOT read as absence.
#[skuld::test]
fn denied_query_limited_reads_unknown_not_gone() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(denied_query_limited_reads_unknown_not_gone),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let child = spawn_restricted(&done, PROCESS_SYNCHRONIZE.0);
    let token = creation_token(child.handle()).expect("the owned handle can always read the token");
    assert!(child.is_running(), "precondition: the subject must be live");
    assert_eq!(
        start_token(child.pid()),
        Resolved::Unknown,
        "an access-denied live process must not resolve as Gone"
    );
    assert_eq!(
        is_running(child.pid(), token),
        Liveness::Unknown,
        "an access-denied live process must not read as Dead"
    );
    assert!(child.is_running(), "and it must still have been live throughout");
}

/// QUERY_LIMITED granted, SYNCHRONIZE denied: the identity resolves, but the signaled state
/// cannot be read and GetExitCodeProcess reports STILL_ACTIVE, which does not distinguish
/// "running" from "exited with code 259". Unknown, not Alive, not Dead.
#[skuld::test]
fn denied_synchronize_resolves_identity_but_liveness_is_unknown_while_running() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(denied_synchronize_resolves_identity_but_liveness_is_unknown_while_running),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let child = spawn_restricted(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
    let token = creation_token(child.handle()).expect("owned handle reads the token");
    assert!(child.is_running(), "precondition: the subject must be live");
    assert_eq!(start_token(child.pid()), Resolved::Found(token));
    assert_eq!(
        is_running(child.pid(), token),
        Liveness::Unknown,
        "SYNCHRONIZE is denied and STILL_ACTIVE is ambiguous — never Alive, never Dead"
    );
    assert!(child.is_running());
}

/// The same process after it exits with a concrete code: GetExitCodeProcess PROVES the exit
/// through QUERY_LIMITED alone, so this must be Dead — not Unknown, or every already-dead
/// `kill` on such a process would report failure.
#[skuld::test]
fn exited_process_denying_synchronize_reads_dead() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(exited_process_denying_synchronize_reads_dead),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let child = spawn_restricted(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
    let token = creation_token(child.handle()).expect("owned handle reads the token");
    child.terminate(); // exit code 1
    assert!(!child.is_running(), "precondition: the subject must have exited");
    assert_eq!(is_running(child.pid(), token), Liveness::Dead);
}

/// A pid that cannot exist classifies as definitely-gone, not Unknown — otherwise the
/// tri-state would degenerate to always-Unknown.
#[skuld::test]
fn nonexistent_pid_is_gone_not_unknown() {
    // OpenProcess for an unused pid returns ERROR_INVALID_PARAMETER, the "no such process"
    // signal. The value is chosen, not arbitrary: Windows allocates pids from a low, densely
    // packed table (multiples of 4), so a value near u32::MAX is unallocatable rather than
    // merely unlikely — the "gone" precondition holds by construction, the way the denied
    // tests assert theirs by fixture.
    let ghost = 0xFFFF_FFF0u32;
    assert_eq!(start_token(ghost), Resolved::Gone);
    assert_eq!(is_running(ghost, Key::from_raw(1)), Liveness::Dead);
}

/// `Process::kill` documents "a real failure (no rights / access-denied on a live process)
/// => Err". An access-denied live process must therefore not produce Ok.
#[skuld::test]
fn kill_of_an_access_denied_live_process_is_an_error() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(kill_of_an_access_denied_live_process_is_an_error),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let child = crate::identity::windows_fixture::spawn_unkillable(&done);
    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");
    let err = crate::os_process::open(id).expect_err("a process we may not open must not read as gone");
    let crate::error::Error::Unassessable { source, .. } = err else {
        panic!("a denial must not read as an I/O failure: {err:?}");
    };
    let io = source.expect("the OS error is preserved");
    assert_eq!(
        crate::error::first_os_code(&io),
        Some(ERROR_ACCESS_DENIED.0 as i32),
        "{io}"
    );
    assert!(child.is_running(), "and it must still be running — nothing killed it");
}

/// `Process::wait` must not report an exit it never observed.
#[skuld::test]
fn waiting_on_an_access_denied_live_process_is_an_error() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(waiting_on_an_access_denied_live_process_is_an_error),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let child = crate::identity::windows_fixture::spawn_unkillable(&done);
    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");
    let err = exited_now(id).expect_err("an unopenable live process must not be reported as exited");
    let crate::error::Error::Unassessable { source, .. } = err else {
        panic!("a denial must not read as an I/O failure: {err:?}");
    };
    let io = source.expect("the OS error is preserved");
    assert_eq!(
        crate::error::first_os_code(&io),
        Some(ERROR_ACCESS_DENIED.0 as i32),
        "{io}"
    );
}

/// The other side of the same branch: `Denied` plus a provably-Dead target must still report
/// success, or every already-exited kill or wait on such a process would start failing. The
/// two entry points need DIFFERENT fixtures, because their masks differ.
#[skuld::test]
fn wait_of_a_synchronize_denied_but_exited_process_reports_exited() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(wait_of_a_synchronize_denied_but_exited_process_reports_exited),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let child = spawn_restricted(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
    let id = crate::identity::windows_identity_from_handle_result(child.handle(), child.pid())
        .expect("the owned handle always yields an identity");
    child.terminate();
    assert!(!child.is_running(), "precondition: the subject must have exited");
    assert_eq!(id.is_alive(), Liveness::Dead, "precondition: provably dead");
    assert!(exited_now(id).expect("waiting on an already-exited process is success"));
}

#[skuld::test]
fn kill_of_a_terminate_denied_but_exited_process_reports_success() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(kill_of_a_terminate_denied_but_exited_process_reports_success),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    use windows::Win32::System::Threading::PROCESS_TERMINATE;
    let child = crate::identity::windows_fixture::spawn_query_only(&done);
    let id = crate::identity::windows_identity_from_handle_result(child.handle(), child.pid())
        .expect("the owned handle always yields an identity");
    // Precondition: the terminate right must actually be refused, or this exercises the
    // Opened::Found path and never reaches the arm under test.
    assert!(
        matches!(
            super::open_classified(child.pid(), PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION),
            super::Opened::Denied(_)
        ),
        "precondition: the open with the terminate right must be denied"
    );
    child.terminate();
    assert!(!child.is_running(), "precondition: the subject must have exited");
    assert_eq!(id.is_alive(), Liveness::Dead, "precondition: provably dead");
    let process = crate::os_process::open(id)
        .expect("the open of an exited process")
        .expect("the exited process is still held by its fixture");
    crate::os_process::kill(&*process).expect("killing an already-exited process is success");
}

const OPTIONAL: [PROCESS_ACCESS_RIGHTS; 2] = [PROCESS_SYNCHRONIZE, PROCESS_TERMINATE];

/// The rights `open_dropping_refused` grants on `pid`, the handle closed. `None`: the open is
/// denied.
fn granted_on(pid: u32) -> Option<PROCESS_ACCESS_RIGHTS> {
    match open_dropping_refused(pid, &OPTIONAL) {
        (Opened::Found(handle), granted) => {
            super::close(handle);
            Some(granted)
        }
        (Opened::Denied(_), _) => None,
        (Opened::Gone, _) => panic!("pid {pid} is live"),
    }
}

/// Each refused right is dropped on its own, down to the query right; a process that refuses even
/// that is denied.
///
/// Mutant: one refusal drops every optional right, or the search stops at the first denial.
#[skuld::test]
fn open_dropping_refused_drops_each_refused_right() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(open_dropping_refused_drops_each_refused_right),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let query = PROCESS_QUERY_LIMITED_INFORMATION;
    let query_only = crate::identity::windows_fixture::spawn_query_only(&done);
    assert_eq!(granted_on(query_only.pid()), Some(query));
    let restricted = spawn_restricted(&done, query.0);
    assert_eq!(granted_on(restricted.pid()), Some(query | PROCESS_TERMINATE));
    assert_eq!(
        granted_on(std::process::id()),
        Some(query | PROCESS_SYNCHRONIZE | PROCESS_TERMINATE)
    );
    let unkillable = crate::identity::windows_fixture::spawn_unkillable(&done);
    assert_eq!(granted_on(unkillable.pid()), None, "QUERY itself is refused");
}

/// What `is_running` must say for a look: the mapping of `exit_look`'s answer.
fn liveness_of(look: windows::core::Result<Look>) -> Liveness {
    match look {
        Ok(Look::Running) => Liveness::Alive,
        Ok(Look::Exited) => Liveness::Dead,
        Err(_) => Liveness::Unknown,
    }
}

/// `is_running` is `exit_look` over the handle `open_dropping_refused` opened, with the token check
/// and the denial in front.
///
/// Mutant: `is_running` keeps its own look, which drifts from `exit_look`.
#[skuld::test]
fn exit_look_agrees_with_is_running() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(exit_look_agrees_with_is_running),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let look_at = |pid: u32| {
        let (Opened::Found(handle), granted) = open_dropping_refused(pid, &[PROCESS_SYNCHRONIZE]) else {
            panic!("pid {pid} opens with QUERY");
        };
        let look = exit_look(handle, granted.0 & PROCESS_SYNCHRONIZE.0 != 0);
        super::close(handle);
        look
    };
    let token_of = |pid: u32| match start_token(pid) {
        Resolved::Found(t) => t,
        other => panic!("pid {pid} resolves: {other:?}"),
    };

    let me = std::process::id();
    assert_eq!(is_running(me, token_of(me)), liveness_of(look_at(me)));
    assert_eq!(is_running(me, token_of(me)), Liveness::Alive);

    let query_only = crate::identity::windows_fixture::spawn_query_only(&done);
    let token = token_of(query_only.pid());
    assert_eq!(
        is_running(query_only.pid(), token),
        liveness_of(look_at(query_only.pid()))
    );
    assert_eq!(
        is_running(query_only.pid(), token),
        Liveness::Unknown,
        "running, unprovable"
    );
    query_only.terminate();
    assert_eq!(
        is_running(query_only.pid(), token),
        liveness_of(look_at(query_only.pid()))
    );
    assert_eq!(
        is_running(query_only.pid(), token),
        Liveness::Dead,
        "an exit code proves it"
    );

    let unkillable = crate::identity::windows_fixture::spawn_unkillable(&done);
    let token = creation_token(unkillable.handle()).expect("the owned handle reads the token");
    assert_eq!(
        is_running(unkillable.pid(), token),
        Liveness::Unknown,
        "QUERY refused: no look"
    );
}

/// A stale identity over a pid whose occupant we CAN read: the token comparison runs, finds
/// a different process, and reports the original exited.
#[skuld::test]
fn wait_on_a_stale_identity_over_a_readable_pid_reports_exited() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(wait_on_a_stale_identity_over_a_readable_pid_reports_exited),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let child = spawn_restricted(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
    let real = crate::identity::windows_identity_from_handle_result(child.handle(), child.pid())
        .expect("the owned handle always yields an identity");
    let stale = real.with_other_value();
    assert!(child.is_running(), "precondition: the pid's occupant is live");
    assert!(exited_now(stale).expect("a readable stranger on the pid proves the stale identity is gone"));
}

/// The other half of the documented asymmetry: when the pid's occupant cannot be read
/// either, the original's exit cannot be established. `Err`, not a guessed `Ok`.
#[skuld::test]
fn wait_on_a_stale_identity_over_an_unreadable_pid_is_an_error() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(wait_on_a_stale_identity_over_an_unreadable_pid_is_an_error),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let child = crate::identity::windows_fixture::spawn_unkillable(&done);
    let real = crate::identity::windows_identity_from_handle_result(child.handle(), child.pid())
        .expect("the owned handle always yields an identity");
    let stale = real.with_other_value();
    assert!(
        child.is_running(),
        "precondition: the pid's occupant is live and unreadable"
    );
    exited_now(stale).expect_err("an unreadable occupant cannot prove the original identity exited");
}

// terminate_held =====

/// A child that blocks on its piped stdin until the test ends it.
fn spawn_blocked_child() -> std::process::Child {
    crate::test_spawn::spawn(
        std::process::Command::new("cmd")
            .args(["/C", "more"])
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null()),
    )
    .expect("spawn cmd /C more")
}

fn handle_of(child: &std::process::Child) -> windows::Win32::Foundation::HANDLE {
    use std::os::windows::io::AsRawHandle;
    windows::Win32::Foundation::HANDLE(child.as_raw_handle())
}

fn end(mut child: std::process::Child) {
    child.kill().expect("end the fixture");
    child.wait().expect("reap the fixture");
}

/// A second handle to `child`'s process opened with `rights` (never `PROCESS_TERMINATE`), closed
/// on drop.
struct Limited(
    windows::Win32::Foundation::HANDLE,
    windows::Win32::System::Threading::PROCESS_ACCESS_RIGHTS,
);

impl Limited {
    fn of(child: &std::process::Child, rights: windows::Win32::System::Threading::PROCESS_ACCESS_RIGHTS) -> Limited {
        // SAFETY: `child` is live and its handle pins the pid, so the open names this process.
        let h = unsafe { windows::Win32::System::Threading::OpenProcess(rights, false, child.id()) }
            .expect("open a limited handle on the fixture");
        Limited(h, rights)
    }

    /// `terminate_held` on the handle, as the one that opened it with the rights it asked for.
    fn terminate(&self) -> super::Termination {
        super::terminate_held(&HostKernel, self.0, self.1)
    }
}

impl Drop for Limited {
    fn drop(&mut self) {
        super::close(self.0);
    }
}

/// The `FakeKernel`'s terminate row refuses a process that stays alive and unsignalled.
///
/// Mutant: a refusal is `Exited` only for a signalled process, so this one is `Failed`.
#[skuld::test]
fn a_refusal_on_a_handle_holding_terminate_is_the_exit() {
    let child = spawn_blocked_child();
    let kernel = refusing_the_next_terminate();
    let outcome = super::terminate_held(&kernel, handle_of(&child), CREATE_PROCESS_ACCESS);
    end(child);
    assert!(
        kernel.all_rows_taken(),
        "the kill reached TerminateProcess to be refused"
    );
    assert!(
        matches!(outcome, super::Termination::Exited),
        "got a failure or a terminate"
    );
}

/// The same for a process that has exited and was signalled: the real refusal, no row.
///
/// Mutant: a refusal is `Exited` only while the process is unsignalled.
#[skuld::test]
fn a_real_refusal_on_a_handle_holding_terminate_of_an_exited_process_is_the_exit() {
    let mut child = spawn_blocked_child();
    child.kill().expect("end the fixture");
    child.wait().expect("reap the fixture");
    let outcome = super::terminate_held(&HostKernel, handle_of(&child), CREATE_PROCESS_ACCESS);
    assert!(
        matches!(outcome, super::Termination::Exited),
        "got a failure or a terminate"
    );
}

/// A handle without `PROCESS_TERMINATE` is refused for good, for a live process: the refusal is
/// not the exit, whatever the caller thinks the handle holds.
///
/// Mutant: every `ERROR_ACCESS_DENIED` reads as the exit.
#[skuld::test]
fn a_refusal_on_a_handle_without_terminate_is_failed_while_the_process_runs() {
    let child = spawn_blocked_child();
    let limited = Limited::of(&child, PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION);
    let outcome = limited.terminate();
    drop(limited);
    end(child);
    let super::Termination::Failed(e) = outcome else {
        panic!("a live process is not an exit");
    };
    assert!(super::is_access_denied(&e), "{e:?}");
}

/// As above with the `FakeKernel`'s terminate row refusing, the process being exiting but
/// unsignalled: a handle without the right cannot show the exit.
///
/// Mutant: the granted access is not consulted, so the row's refusal reads as the exit.
#[skuld::test]
fn an_exiting_refusal_on_a_handle_without_terminate_is_failed_while_unsignalled() {
    let child = spawn_blocked_child();
    let limited = Limited::of(&child, PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION);
    let kernel = refusing_the_next_terminate();
    let outcome = super::terminate_held(&kernel, limited.0, limited.1);
    drop(limited);
    end(child);
    assert!(
        kernel.all_rows_taken(),
        "the kill reached TerminateProcess to be refused"
    );
    let super::Termination::Failed(e) = outcome else {
        panic!("an unsignalled process is not shown to have exited");
    };
    assert!(super::is_access_denied(&e), "{e:?}");
}

/// A handle with neither `PROCESS_TERMINATE` nor `SYNCHRONIZE` to a running process: `Failed` with
/// the refusal, not `Exited`. Its exit code, `STILL_ACTIVE`, cannot be told from exit code 259.
///
/// Mutant: `STILL_ACTIVE` reads as the exit.
#[skuld::test]
fn a_refusal_on_a_handle_without_terminate_or_synchronize_is_failed() {
    let child = spawn_blocked_child();
    let limited = Limited::of(&child, PROCESS_QUERY_LIMITED_INFORMATION);
    let outcome = limited.terminate();
    drop(limited);
    end(child);
    let super::Termination::Failed(e) = outcome else {
        panic!("a handle that cannot be waited on shows no exit");
    };
    assert!(super::is_access_denied(&e), "{e:?}");
}

/// A handle without `PROCESS_TERMINATE` to a process that has exited: its signalled state is the
/// proof of the exit.
///
/// Mutant: the signalled read is dropped, so the refusal stays `Failed`.
#[skuld::test]
fn a_refusal_on_a_handle_without_terminate_is_the_exit_once_the_process_is_signalled() {
    let mut child = spawn_blocked_child();
    let limited = Limited::of(&child, PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION);
    child.kill().expect("end the fixture");
    // Reap through a real wait: the process object is signalled when this returns.
    child.wait().expect("reap the fixture");
    let outcome = limited.terminate();
    drop(limited);
    assert!(
        matches!(outcome, super::Termination::Exited),
        "a signalled process is the exit"
    );
}

/// A handle with neither `PROCESS_TERMINATE` nor `SYNCHRONIZE` to a process that has exited: its
/// exit code is the proof of the exit.
///
/// Mutant: the exit code is not read, so the refusal stays `Failed`.
#[skuld::test]
fn a_refusal_on_a_handle_without_terminate_or_synchronize_is_the_exit_once_the_code_is_recorded() {
    let mut child = spawn_blocked_child();
    let limited = Limited::of(&child, PROCESS_QUERY_LIMITED_INFORMATION);
    child.kill().expect("end the fixture");
    // Reap through a real wait: the exit code is recorded when this returns.
    child.wait().expect("reap the fixture");
    let outcome = limited.terminate();
    drop(limited);
    assert!(
        matches!(outcome, super::Termination::Exited),
        "an exit code other than STILL_ACTIVE is the exit"
    );
}

// order key =====

fn order_key_of(id: crate::identity::ProcessId) -> crate::identity::OrderKey {
    match id.order_key().0 {
        Resolved::Found(key) => key,
        other => panic!("the order key of {id:?} should read, got {other:?}"),
    }
}

/// The order key is the process sequence number, not the creation time that is the id's key.
///
/// Mutant: the order key is the id's key.
#[skuld::test]
fn the_order_key_is_not_the_creation_time() {
    let me = crate::identity::ProcessId::current();
    let (read, _) =
        crate::identity::ProcessId::of_ordered_with(&crate::identity::HostKernel, me.pid(), None, None, None);
    let Resolved::Found((id, key)) = read else {
        panic!("our own order key must read, got {read:?}");
    };
    assert_eq!(id, me, "the id of the ordered read is the id of the by-pid read");
    assert_ne!(key, Some(crate::identity::OrderKey::for_test(me.key().raw())));
    assert_eq!(key, Some(order_key_of(me)), "both reads of one process agree");
}

/// The fact the tree walk relies on: a process's sequence number is greater than its creator's.
///
/// Mutant: the order key is a value that does not grow from creator to child.
#[skuld::test]
fn a_child_orders_after_its_creator_and_a_grandchild_after_the_child() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(a_child_orders_after_its_creator_and_a_grandchild_after_the_child),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let child = spawn_restricted_shell(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
    let grandchild = child.wait_for_child();
    let me = crate::identity::ProcessId::current();
    let child_id = crate::identity::ProcessId::of(child.pid())
        .found()
        .expect("the child is ours");
    let grandchild_id = crate::identity::ProcessId::of(grandchild)
        .found()
        .expect("the grandchild is ours");
    let keys = (order_key_of(me), order_key_of(child_id), order_key_of(grandchild_id));
    assert!(keys.0 < keys.1, "child {:?} after creator {:?}", keys.1, keys.0);
    assert!(keys.1 < keys.2, "grandchild {:?} after child {:?}", keys.2, keys.1);
}

/// A pid that another process holds has no order key for the id that was read before.
///
/// Mutant: the order key of a recycled pid is read as the new holder's.
#[skuld::test]
fn the_order_key_of_a_stale_id_is_gone() {
    let stale = crate::identity::ProcessId::current().with_other_value();
    assert_eq!(stale.order_key().0, Resolved::Gone);
}

/// Mutant: the walk drops a genuine descendant under the sequence-number order.
#[skuld::test]
fn the_walk_keeps_a_real_child_and_grandchild_of_a_real_spawned_root() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(the_walk_keeps_a_real_child_and_grandchild_of_a_real_spawned_root),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let child = spawn_restricted_shell(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
    let grandchild = child.wait_for_child();
    let root = crate::identity::ProcessId::of(child.pid())
        .found()
        .expect("the root is ours");
    let direct = root.children(crate::Recursive::No);
    let below_us = crate::identity::ProcessId::current().children(crate::Recursive::Yes);
    let direct = direct.expect("the root's children are enumerable");
    assert!(
        direct.iter().any(|id| id.pid() == grandchild),
        "{grandchild} not among {direct:?}"
    );
    let below_us = below_us.expect("our descendants are enumerable");
    for pid in [root.pid(), grandchild] {
        assert!(
            below_us.iter().any(|id| id.pid() == pid),
            "{pid} not among {below_us:?}"
        );
    }
}

// the two reads of one handle =====

const REFUSED: windows::Win32::Foundation::NTSTATUS = windows::Win32::Foundation::NTSTATUS(0xC000_0004_u32 as i32);
const ACCESS_DENIED: windows::Win32::Foundation::NTSTATUS =
    windows::Win32::Foundation::NTSTATUS(0xC000_0022_u32 as i32);

fn refused_key() -> windows::core::Result<Key> {
    Err(windows::core::Error::from_hresult(windows::core::HRESULT::from_win32(
        windows::Win32::Foundation::ERROR_ACCESS_DENIED.0,
    )))
}

/// The error text of a read's cause, for the subject `pid 7`.
fn cause_text(cause: crate::identity::UnknownCause) -> Option<String> {
    cause.into_error("pid 7").map(|e| e.to_string())
}

/// `ERROR_BAD_LENGTH`, the Win32 code of `REFUSED` (`STATUS_INFO_LENGTH_MISMATCH`).
const REFUSED_CODE: i32 = windows::Win32::Foundation::ERROR_BAD_LENGTH.0 as i32;

/// The Win32 code a read's cause carries as its source.
fn cause_code(cause: crate::identity::UnknownCause) -> Option<i32> {
    cause.into_error("pid 7").and_then(|e| crate::error::first_os_code(&e))
}

/// Both reads answered: the id's key and the order key, from one handle.
///
/// Mutant: the order key is the key.
#[skuld::test]
fn two_good_reads_are_the_key_and_the_sequence_number() {
    let key = Key::for_test(5);
    for expected in [None, Some(key)] {
        let (read, cause) = super::combine(7, expected, Ok(key), Ok(9));
        assert_eq!(read, Resolved::Found((key, crate::identity::OrderKey::for_test(9))));
        assert_eq!(cause_text(cause), None);
    }
}

/// A refused sequence-number query on the process asked about is `Unknown`, with its status.
///
/// Mutant: the refusal reads as the generic "access denied?" one.
#[skuld::test]
fn a_refused_sequence_number_on_the_expected_process_is_unknown_with_its_status() {
    let key = Key::for_test(5);
    for expected in [None, Some(key)] {
        let (read, cause) = super::combine(7, expected, Ok(key), Err(REFUSED));
        assert_eq!(read, Resolved::Unknown);
        let error = cause.into_error("pid 7").expect("the refusal is the cause");
        let text = error.to_string();
        assert!(
            text.contains("pid 7") && text.contains("process sequence number query failed"),
            "{text}"
        );
        assert_eq!(
            crate::error::first_os_code(&error),
            Some(REFUSED_CODE),
            "the status is the source"
        );
    }
}

/// Another process's key is `Gone` whatever the sequence-number query said: a recycled pid whose
/// new holder refuses the query is not an unreadable root.
///
/// Mutant: the sequence-number result is looked at before the key.
#[skuld::test]
fn another_processs_key_is_gone_whether_or_not_its_sequence_number_reads() {
    let (read, cause) = super::combine(7, Some(Key::for_test(5)), Ok(Key::for_test(6)), Err(REFUSED));
    assert_eq!(read, Resolved::Gone);
    assert_eq!(cause_text(cause), None);
    let (read, _) = super::combine(7, Some(Key::for_test(5)), Ok(Key::for_test(6)), Ok(9));
    assert_eq!(read, Resolved::Gone);
}

/// A key that cannot be read leaves nothing to compare: `Unknown`. When the sequence-number query
/// was refused too, the cause keeps its status and the log keeps the key's failure.
///
/// Mutant: the second failure is dropped.
#[skuld::test]
fn two_failed_reads_keep_both_failures() {
    crate::log_capture::install();
    let mark = crate::log_capture::mark();
    let (read, cause) = super::combine(70_007, Some(Key::for_test(5)), refused_key(), Err(REFUSED));
    assert_eq!(read, Resolved::Unknown);
    assert!(
        cause_code(cause) == Some(REFUSED_CODE),
        "the sequence-number refusal is the cause"
    );
    assert!(
        crate::log_capture::contains_since(mark, "GetProcessTimes failed on an opened handle for pid 70007"),
        "the key's failure is logged"
    );

    let (read, cause) = super::combine(70_008, None, refused_key(), Ok(9));
    assert_eq!(read, Resolved::Unknown);
    assert_eq!(cause_text(cause), None, "a key refusal alone has no status to name");
}

/// `read` answers for every process this caller can open: the CI pin of a private
/// `NtQueryInformationProcess` class (`what`) on the runner's real process population. Read-only.
/// An elevated caller opens more, so an elevated runner covers processes an unelevated one cannot.
fn assert_reads_on_every_process_that_opens<T>(
    what: &str,
    read: fn(windows::Win32::Foundation::HANDLE) -> Result<T, windows::Win32::Foundation::NTSTATUS>,
) {
    let parents =
        crate::containment::enumerate::process_parents(&crate::identity::HostKernel).expect("the process snapshot");
    let (mut opened, mut refused, mut gone) = (0usize, 0usize, 0usize);
    let mut failures = Vec::new();
    for (pid, _) in parents {
        match super::open_classified(pid, PROCESS_QUERY_LIMITED_INFORMATION) {
            Opened::Found(handle) => {
                opened += 1;
                if let Err(status) = read(handle) {
                    failures.push(format!("pid {pid}: NTSTATUS {:#010x}", status.0 as u32));
                }
                super::close(handle);
            }
            Opened::Denied(_) => refused += 1,
            Opened::Gone => gone += 1,
        }
    }
    eprintln!("{what} read on {opened} processes; {refused} refused to open, {gone} gone");
    assert!(opened > 0, "no process could be opened: {refused} refused, {gone} gone");
    assert!(
        failures.is_empty(),
        "{what} failed on {} of {opened} opened processes ({refused} refused to open): {failures:?}",
        failures.len()
    );
}

/// Class 92 answers for every process this caller can open.
///
/// Mutant: the class is refused for a kind of process (a protected or system one).
#[skuld::test]
fn the_sequence_number_reads_on_every_process_that_opens() {
    assert_reads_on_every_process_that_opens("class 92", super::sequence_number);
}

/// Class 0 answers for every process this caller can open.
///
/// Mutant: the class is refused for a kind of process (a protected or system one).
#[skuld::test]
fn the_creator_pid_reads_on_every_process_that_opens() {
    assert_reads_on_every_process_that_opens("class 0", super::creator_pid);
}

/// The creator pid is the pid of the process that made this one, and still is once that process has
/// exited: Windows never reparents. The fixture shell is terminated while its `more` lives on in the
/// fixture's job.
///
/// Mutant: the creator read names the current parent, or is lost with the creator.
#[skuld::test]
fn the_creator_pid_survives_the_creators_exit() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(the_creator_pid_survives_the_creators_exit),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let shell = spawn_restricted_shell(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
    let grandchild = shell.wait_for_child();
    let Opened::Found(handle) = super::open_classified(grandchild, PROCESS_QUERY_LIMITED_INFORMATION) else {
        panic!("the grandchild is ours and openable");
    };
    let before = super::creator_pid(handle);
    shell.terminate();
    let after = super::creator_pid(handle);
    super::close(handle);
    assert_eq!(before, Ok(shell.pid() as usize));
    assert_eq!(after, Ok(shell.pid() as usize));
}

/// The candidate's creator is the claimed parent, but the parent's pid cannot be opened to prove it
/// still holds it: the read is `Unknown`, blaming the parent and not the candidate. The parent is a
/// real shell that grants no query right, and the candidate is the `more` it launched.
///
/// Mutant: an unprovable parent reads as held (`Found`) or as gone, or blames the candidate.
#[skuld::test]
fn a_candidate_is_unknown_when_its_parent_cannot_be_opened_to_prove_it() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(a_candidate_is_unknown_when_its_parent_cannot_be_opened_to_prove_it),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let shell = spawn_restricted_shell(&done, PROCESS_SYNCHRONIZE.0);
    let candidate = shell.wait_for_child();
    assert!(shell.is_running(), "precondition: the parent must be live");
    // The shell cannot be read by pid, so its id is stated; only its pid is looked up.
    let parent = crate::identity::ProcessId::from_parts_for_test(shell.pid(), Key::for_test(1));
    let (read, cause) =
        crate::identity::ProcessId::of_ordered_with(&crate::identity::HostKernel, candidate, Some(parent), None, None);
    assert_eq!(read, Resolved::Unknown);
    assert_eq!(cause.blamed_parent(), Some(shell.pid()));
}

/// A refused creator query is an unreadable parent number that carries its `NTSTATUS` in the cause;
/// an answered one is the number, and adds nothing. (How the walk words the status is pinned in
/// `treewalk_tests`.)
///
/// The query on an opened handle cannot be made to refuse here, so the row is over the function that
/// builds the cause from its answer. Mutant: `creator_read` drops the status from the cause.
#[skuld::test]
fn a_refused_creator_query_puts_its_status_in_the_cause() {
    use super::{creator_read, ParentRead};
    use crate::identity::UnknownCause;

    let (parent, cause) = creator_read(7, Err(REFUSED), UnknownCause::default());
    assert_eq!(parent, ParentRead::Unreadable);
    assert_eq!(cause.creator_status, Some(REFUSED));

    let (parent, cause) = creator_read(7, Ok(5), UnknownCause::default());
    assert_eq!(parent, ParentRead::Is(5));
    assert_eq!(cause.creator_status, None);
}

/// A cause that already holds a refused sequence-number query keeps it as the error when the
/// creator query is refused too: the sequence-number query is named first.
///
/// Mutant: the creator status takes precedence over the sequence status in `into_error`.
#[skuld::test]
fn a_refused_creator_query_does_not_displace_a_refused_sequence_number_in_the_error() {
    use super::{creator_read, ParentRead};
    use crate::identity::UnknownCause;

    let (parent, cause) = creator_read(7, Err(ACCESS_DENIED), UnknownCause::of_sequence_status(REFUSED));
    assert_eq!(parent, ParentRead::Unreadable);
    let error = cause.into_error("pid 7").expect("a refusal is the cause");
    let text = error.to_string();
    assert!(
        text.contains("process sequence number query failed") && !text.contains("creator"),
        "{text}"
    );
    assert_eq!(
        crate::error::first_os_code(&error),
        Some(REFUSED_CODE),
        "the sequence status, not the creator's"
    );
}