mxc-sdk 1.2.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Host backend-availability probe — the read-only [`available_backends`] API.
//!
//! Reports only the containment backends the current host can run. Answers "what can I
//! use here?"; a backend's absence means "not currently usable, for any reason".
//! Separate from [`platform_support`](crate::mxc_engine::platform_support), which answers the
//! narrower "what can `mxc-sdk` itself launch?" question and reports no tier.

use crate::mxc_common::models::ContainmentBackend;
use serde::Serialize;

/// Optional feature supported by a containment backend on the current host.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub enum BackendCapability {
    /// Denial capture (`captureDenials`) on ProcessContainer and Seatbelt.
    CaptureDenials,
    /// Native `filesystem.deniedPaths` enforcement at the reported tier.
    FilesystemDeniedPaths,
    /// Native `processContainer.filesystem.enumeratePaths` enforcement.
    FilesystemEnumeratePaths,
    /// With explicit host-loopback allow, the baseline is policy-permitted stateful outbound
    /// loopback. Native Windows ingress support adds bidirectional access, including
    /// container-hosted loopback servers.
    IngressHostLoopbackAllow,
    /// Bubblewrap proxy-only egress in a private network namespace.
    ProxyEnforcement,
    /// Identity-less proxy support on loopback.
    /// Requires explicit host-loopback allow; not general ingress support.
    IdentitylessLoopbackProxy,
}

/// One host-available backend, plus its effective isolation tier (if any).
///
/// Serializes to camelCase JSON such as `{"backend":"seatbelt"}` or
/// `{"backend":"processcontainer","tier":"base-container","capabilities":
/// ["captureDenials"]}`; empty optional fields are omitted.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AvailableBackend {
    /// Backend identifier, such as `"seatbelt"` or `"processcontainer"`.
    pub backend: String,
    /// Native ProcessContainer reports `"base-container"`; `None`, and omitted
    /// from JSON, for other backends.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tier: Option<String>,
    /// Optional features supported by the reported tier.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub capabilities: Vec<BackendCapability>,
    /// Diagnostics for a capability this host cannot offer.
    ///
    /// Not a guarantee for every absent capability: only checks that produce a
    /// reason populate this. Bubblewrap's `ProxyEnforcement` does, since its
    /// dependency walk names what is missing; Windows omits `CaptureDenials`
    /// without a warning.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<String>,
}

impl AvailableBackend {
    fn tierless(backend: &str) -> Self {
        Self {
            backend: backend.to_string(),
            tier: None,
            capabilities: Vec::new(),
            warnings: Vec::new(),
        }
    }
}

/// Probe the host and return only the backends it can currently run.
///
/// An empty `Vec` is a normal result (unsupported platform, or Linux with
/// neither `bwrap` nor `lxc`), not an error. Order is stable but callers should
/// match by `backend` name, not position.
///
/// Not cached — read once at startup, not in a hot loop.
pub fn available_backends() -> Vec<AvailableBackend> {
    #[cfg(target_os = "macos")]
    {
        macos_backends()
    }
    #[cfg(target_os = "linux")]
    {
        linux_backends()
    }
    #[cfg(target_os = "windows")]
    {
        use crate::process_container_common::base_container_runner::BaseContainerRunner;

        let mut support = ProcessContainerCapabilities {
            capture_denials: BaseContainerRunner::is_native_capture_available(),
            filesystem_denied_paths: BaseContainerRunner::supports_native_denied_paths(),
            filesystem_enumerate_paths: BaseContainerRunner::supports_enumerate_paths(),
            ingress_host_loopback_allow: BaseContainerRunner::supports_ingress_host_loopback_allow(
            ),
            ..Default::default()
        };
        match BaseContainerRunner::supports_identityless_loopback_proxy() {
            Ok(supported) => support.identityless_loopback_proxy = supported,
            Err(error) => support.warnings.push(format!(
                "failed to query identity-less loopback proxy support: {error}"
            )),
        }
        windows_backends(
            BaseContainerRunner::is_base_container_api_present(),
            support,
            WindowsHostAvailability {
                windows_sandbox:
                    crate::windows_sandbox_lifecycle::availability::is_windows_sandbox_available(),
                wslc: wslc_host_available(),
                isolation_session: isolation_session_host_available(),
                hyperlight: hyperlight_host_available(),
                microvm: microvm_host_available(),
            },
        )
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    {
        Vec::new()
    }
}

/// Serialize [`available_backends`] for the `--available-backends` CLI surface.
///
/// Distinct from `wxc-exec --probe`, which emits ProcessContainer diagnostics
/// object; this is the backend-availability array.
pub fn to_json_pretty(backends: &[AvailableBackend]) -> Result<String, serde_json::Error> {
    serde_json::to_string_pretty(backends)
}

#[cfg(target_os = "macos")]
fn macos_backends() -> Vec<AvailableBackend> {
    macos_backends_with(
        crate::seatbelt_common::seatbelt_runner::sandbox_init_available(),
        crate::seatbelt_common::denial_capture::log_tool_available(),
    )
}

/// Split from [`macos_backends`] so the reporting is testable without
/// depending on what the test host has installed.
#[cfg(target_os = "macos")]
fn macos_backends_with(
    sandbox_init_available: bool,
    log_tool_available: bool,
) -> Vec<AvailableBackend> {
    let mut backends = Vec::new();
    if sandbox_init_available {
        let mut seatbelt = AvailableBackend::tierless(ContainmentBackend::Seatbelt.wire_name());
        if log_tool_available {
            seatbelt
                .capabilities
                .push(BackendCapability::CaptureDenials);
        }
        backends.push(seatbelt);
    }
    backends
}

#[cfg(target_os = "linux")]
fn linux_backends() -> Vec<AvailableBackend> {
    linux_backends_with(
        LinuxHostAvailability {
            bubblewrap: crate::bwrap_common::bwrap_version::probe_bwrap().is_ok(),
            lxc: crate::lxc_common::availability::is_lxc_available(),
            hyperlight: hyperlight_host_available(),
        },
        crate::bwrap_common::proxy_network::probe_proxy_enforcement,
    )
}

/// Host checks for the Linux backends, resolved by [`available_backends`] and
/// injected so the reporting is testable without a host that has (or lacks)
/// them. Named fields so a detector cannot be wired to the wrong backend.
#[cfg(target_os = "linux")]
#[derive(Debug, Clone, Copy, Default)]
struct LinuxHostAvailability {
    bubblewrap: bool,
    lxc: bool,
    hyperlight: bool,
}

/// Split from [`linux_backends`] so the reporting is testable without a host
/// that has (or lacks) the backends. `proxy_enforcement` stays a closure
/// because the dependency walk costs several subprocess spawns and is only
/// meaningful once `bwrap` itself is usable.
#[cfg(target_os = "linux")]
fn linux_backends_with(
    hosts: LinuxHostAvailability,
    proxy_enforcement: impl FnOnce() -> Result<(), String>,
) -> Vec<AvailableBackend> {
    let mut backends = Vec::new();
    if hosts.bubblewrap {
        backends.push(bubblewrap_backend(proxy_enforcement()));
    }
    if hosts.lxc {
        backends.push(AvailableBackend::tierless(
            ContainmentBackend::Lxc.wire_name(),
        ));
    }
    if hosts.hyperlight {
        backends.push(AvailableBackend::tierless(
            ContainmentBackend::Hyperlight.wire_name(),
        ));
    }
    backends
}

/// Split from [`linux_backends`] so the reporting is testable without a host
/// that has (or lacks) the private-network dependencies.
#[cfg(target_os = "linux")]
fn bubblewrap_backend(proxy_enforcement: Result<(), String>) -> AvailableBackend {
    let mut backend = AvailableBackend::tierless(ContainmentBackend::Bubblewrap.wire_name());
    match proxy_enforcement {
        Ok(()) => backend
            .capabilities
            .push(BackendCapability::ProxyEnforcement),
        Err(reason) => backend.warnings.push(reason),
    }
    backend
}

#[cfg(target_os = "windows")]
#[derive(Debug, Clone, Default)]
struct ProcessContainerCapabilities {
    capture_denials: bool,
    filesystem_denied_paths: bool,
    filesystem_enumerate_paths: bool,
    ingress_host_loopback_allow: bool,
    identityless_loopback_proxy: bool,
    warnings: Vec<String>,
}

/// Host checks for the optional Windows backends, resolved by
/// [`available_backends`] and injected so the reporting is testable without a
/// host that has (or lacks) them. A field is `false` when its feature or
/// architecture gate is off.
#[cfg(target_os = "windows")]
#[derive(Debug, Clone, Copy, Default)]
struct WindowsHostAvailability {
    windows_sandbox: bool,
    wslc: bool,
    isolation_session: bool,
    hyperlight: bool,
    microvm: bool,
}

#[cfg(target_os = "windows")]
pub(super) fn microvm_host_available() -> bool {
    #[cfg(all(feature = "microvm", target_arch = "x86_64"))]
    {
        crate::backend_modules::nvx::is_available()
    }
    #[cfg(not(all(feature = "microvm", target_arch = "x86_64")))]
    {
        false
    }
}

#[cfg(target_os = "windows")]
fn wslc_host_available() -> bool {
    // Requires the host to actually run it (WSL2 + the WSLC runtime present),
    // matching `platform_support()` and the runner preflight. `WslcSdk::load()`
    // alone only proves the DLL and its exports resolve.
    #[cfg(feature = "wslc")]
    {
        crate::wslc_common::is_available()
    }
    #[cfg(not(feature = "wslc"))]
    {
        false
    }
}

#[cfg(target_os = "windows")]
fn isolation_session_host_available() -> bool {
    // Available when the `IsoSessionOps` WinRT class is registered on the OS.
    #[cfg(feature = "isolation_session")]
    {
        crate::isolation_session_common::availability::is_isolation_session_available()
    }
    #[cfg(not(feature = "isolation_session"))]
    {
        false
    }
}

/// Hyperlight needs x86_64 and a hypervisor: WHP on Windows, KVM on Linux.
/// Both are checked here so a missing hypervisor never reaches the delay-loaded
/// WHP import or a VM boot.
#[cfg(any(target_os = "windows", target_os = "linux"))]
fn hyperlight_host_available() -> bool {
    #[cfg(all(feature = "hyperlight", target_arch = "x86_64", target_os = "windows"))]
    {
        crate::hyperlight_common::is_whp_available()
    }
    #[cfg(all(feature = "hyperlight", target_arch = "x86_64", target_os = "linux"))]
    {
        crate::hyperlight_common::is_kvm_available()
    }
    #[cfg(not(all(feature = "hyperlight", target_arch = "x86_64")))]
    {
        false
    }
}

#[cfg(target_os = "windows")]
fn windows_backends(
    base_container_usable: bool,
    support: ProcessContainerCapabilities,
    hosts: WindowsHostAvailability,
) -> Vec<AvailableBackend> {
    let mut capabilities = Vec::new();
    if support.capture_denials {
        capabilities.push(BackendCapability::CaptureDenials);
    }
    if support.filesystem_denied_paths {
        capabilities.push(BackendCapability::FilesystemDeniedPaths);
    }
    if support.filesystem_enumerate_paths {
        capabilities.push(BackendCapability::FilesystemEnumeratePaths);
    }
    if support.ingress_host_loopback_allow {
        capabilities.push(BackendCapability::IngressHostLoopbackAllow);
    }
    if support.identityless_loopback_proxy {
        capabilities.push(BackendCapability::IdentitylessLoopbackProxy);
    }
    let mut backends = Vec::new();
    if base_container_usable {
        backends.push(AvailableBackend {
            backend: ContainmentBackend::ProcessContainer.wire_name().to_string(),
            tier: Some("base-container".to_string()),
            capabilities,
            warnings: support.warnings,
        });
    }

    if hosts.windows_sandbox {
        backends.push(AvailableBackend::tierless(
            ContainmentBackend::WindowsSandbox.wire_name(),
        ));
    }

    if hosts.wslc {
        backends.push(AvailableBackend::tierless(
            ContainmentBackend::Wslc.wire_name(),
        ));
    }

    if hosts.isolation_session {
        backends.push(AvailableBackend::tierless(
            ContainmentBackend::IsolationSession.wire_name(),
        ));
    }

    if hosts.hyperlight {
        backends.push(AvailableBackend::tierless(
            ContainmentBackend::Hyperlight.wire_name(),
        ));
    }

    if hosts.microvm {
        backends.push(AvailableBackend::tierless(
            ContainmentBackend::MicroVm.wire_name(),
        ));
    }

    backends
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mxc_engine::backend_registry::{self, ProbeCoverage};

    #[cfg(target_os = "windows")]
    const CANONICAL_TIERS: [&str; 1] = ["base-container"];

    /// Runs the real platform reporting with every host check forced to pass,
    /// so the assertions below exercise production wiring rather than whatever
    /// this particular host happens to have installed.
    #[cfg(target_os = "windows")]
    fn backends_with_host_checks_passing() -> Vec<AvailableBackend> {
        windows_backends(
            true,
            ProcessContainerCapabilities::default(),
            WindowsHostAvailability {
                windows_sandbox: true,
                wslc: true,
                isolation_session: true,
                hyperlight: true,
                microvm: true,
            },
        )
    }

    #[cfg(target_os = "linux")]
    fn backends_with_host_checks_passing() -> Vec<AvailableBackend> {
        linux_backends_with(
            LinuxHostAvailability {
                bubblewrap: true,
                lxc: true,
                hyperlight: true,
            },
            || Ok(()),
        )
    }

    #[cfg(target_os = "macos")]
    fn backends_with_host_checks_passing() -> Vec<AvailableBackend> {
        macos_backends_with(true, true)
    }

    fn reported_names(backends: &[AvailableBackend]) -> Vec<&str> {
        backends
            .iter()
            .map(|entry| entry.backend.as_str())
            .collect()
    }

    /// Enabling all host checks at once cannot tell one detector's wiring from
    /// another's, so each is enabled alone and must produce exactly its own
    /// backend. A detector wired to the wrong branch fails here.
    #[cfg(target_os = "linux")]
    #[test]
    fn each_linux_host_check_reports_only_its_own_backend() {
        let cases = [
            (
                "bubblewrap",
                LinuxHostAvailability {
                    bubblewrap: true,
                    ..Default::default()
                },
            ),
            (
                "lxc",
                LinuxHostAvailability {
                    lxc: true,
                    ..Default::default()
                },
            ),
            (
                "hyperlight",
                LinuxHostAvailability {
                    hyperlight: true,
                    ..Default::default()
                },
            ),
        ];
        for (expected, hosts) in cases {
            let reported = linux_backends_with(hosts, || Ok(()));
            assert_eq!(reported_names(&reported), vec![expected]);
        }
        assert!(linux_backends_with(LinuxHostAvailability::default(), || Ok(())).is_empty());
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn each_windows_host_check_reports_only_its_own_backend() {
        let cases = [
            (
                "windows_sandbox",
                WindowsHostAvailability {
                    windows_sandbox: true,
                    ..Default::default()
                },
            ),
            (
                "wslc",
                WindowsHostAvailability {
                    wslc: true,
                    ..Default::default()
                },
            ),
            (
                "isolation_session",
                WindowsHostAvailability {
                    isolation_session: true,
                    ..Default::default()
                },
            ),
            (
                "hyperlight",
                WindowsHostAvailability {
                    hyperlight: true,
                    ..Default::default()
                },
            ),
        ];
        for (expected, hosts) in cases {
            for base_container_usable in [false, true] {
                let reported = windows_backends(
                    base_container_usable,
                    ProcessContainerCapabilities::default(),
                    hosts,
                );
                let mut expected_names = Vec::new();
                if base_container_usable {
                    expected_names.push("processcontainer");
                }
                expected_names.push(expected);
                assert_eq!(reported_names(&reported), expected_names);
            }
        }
        let reported = windows_backends(
            true,
            ProcessContainerCapabilities::default(),
            WindowsHostAvailability::default(),
        );
        assert_eq!(reported_names(&reported), vec!["processcontainer"]);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn the_macos_host_check_reports_only_seatbelt() {
        assert_eq!(
            reported_names(&macos_backends_with(true, true)),
            vec!["seatbelt"]
        );
        assert!(macos_backends_with(false, true).is_empty());
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn seatbelt_reports_capture_denials_only_with_the_log_tool() {
        assert_eq!(
            macos_backends_with(true, true)[0].capabilities,
            vec![BackendCapability::CaptureDenials]
        );
        assert!(macos_backends_with(true, false)[0].capabilities.is_empty());
    }

    /// Holds each backend in the real [`available_backends`] to its own
    /// detector, catching a disconnected or misassigned detector.
    #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
    fn assert_probe_matches_detectors(detectors: &[(&str, bool)]) {
        let backends = available_backends();
        let reported = reported_names(&backends);
        for (name, detected) in detectors {
            assert_eq!(
                reported.contains(name),
                *detected,
                "{name}: the probe reported {}, but its detector returned {detected}",
                reported.contains(name)
            );
        }
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn the_linux_probe_reports_what_its_detectors_found() {
        assert_probe_matches_detectors(&[
            (
                ContainmentBackend::Bubblewrap.wire_name(),
                crate::bwrap_common::bwrap_version::probe_bwrap().is_ok(),
            ),
            (
                ContainmentBackend::Lxc.wire_name(),
                crate::lxc_common::availability::is_lxc_available(),
            ),
            (
                ContainmentBackend::Hyperlight.wire_name(),
                hyperlight_host_available(),
            ),
        ]);
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn the_windows_probe_reports_what_its_detectors_found() {
        let _guard = crate::mxc_common::test_env::lock();
        assert_probe_matches_detectors(&[
            (
                ContainmentBackend::ProcessContainer.wire_name(),
                crate::process_container_common::base_container_runner::BaseContainerRunner::is_base_container_api_present(),
            ),
            (
                ContainmentBackend::WindowsSandbox.wire_name(),
                crate::windows_sandbox_lifecycle::availability::is_windows_sandbox_available(),
            ),
            (ContainmentBackend::Wslc.wire_name(), wslc_host_available()),
            (
                ContainmentBackend::IsolationSession.wire_name(),
                isolation_session_host_available(),
            ),
            (
                ContainmentBackend::Hyperlight.wire_name(),
                hyperlight_host_available(),
            ),
        ]);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn the_macos_probe_reports_what_its_detector_found() {
        assert_probe_matches_detectors(&[(
            ContainmentBackend::Seatbelt.wire_name(),
            crate::seatbelt_common::seatbelt_runner::sandbox_init_available(),
        )]);
    }

    /// The forward half of the probe-coverage gate: every backend the registry
    /// claims to detect on this target must actually be emitted by the real
    /// reporting function once its host check passes. Registering a backend as
    /// `Detected` without adding a branch to `available_backends()` fails here.
    #[test]
    fn every_detected_backend_is_reported_when_its_host_checks_pass() {
        let reported = backends_with_host_checks_passing();
        for name in backend_registry::detected_on_current_target() {
            assert!(
                reported.iter().any(|entry| entry.backend == name),
                "{name} is registered as detected on {}, but the probe never reports it even \
                 with every host check passing",
                std::env::consts::OS
            );
        }
    }

    #[test]
    fn tier_is_omitted_from_json_when_none() {
        let backend = AvailableBackend::tierless("seatbelt");
        let json = serde_json::to_string(&backend).expect("serializes");
        assert_eq!(json, r#"{"backend":"seatbelt"}"#);
    }

    #[test]
    fn tier_is_serialized_in_camel_case_when_present() {
        let backend = AvailableBackend {
            backend: "processcontainer".to_string(),
            tier: Some("base-container".to_string()),
            capabilities: Vec::new(),
            warnings: Vec::new(),
        };
        let json = serde_json::to_string(&backend).expect("serializes");
        assert_eq!(
            json,
            r#"{"backend":"processcontainer","tier":"base-container"}"#
        );
    }

    #[test]
    fn capabilities_are_serialized_in_camel_case_when_present() {
        let backend = AvailableBackend {
            backend: "processcontainer".to_string(),
            tier: Some("base-container".to_string()),
            capabilities: vec![BackendCapability::CaptureDenials],
            warnings: Vec::new(),
        };
        let json = serde_json::to_string(&backend).expect("serializes");
        assert_eq!(
            json,
            r#"{"backend":"processcontainer","tier":"base-container","capabilities":["captureDenials"]}"#
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn bubblewrap_reports_proxy_enforcement_capability_when_probe_succeeds() {
        let backend = bubblewrap_backend(Ok(()));
        assert_eq!(backend.backend, "bubblewrap");
        assert_eq!(
            backend.capabilities,
            vec![BackendCapability::ProxyEnforcement]
        );
        assert!(backend.warnings.is_empty());
        let json = serde_json::to_string(&backend).expect("serializes");
        assert_eq!(
            json,
            r#"{"backend":"bubblewrap","capabilities":["proxyEnforcement"]}"#
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn bubblewrap_reports_reason_instead_of_capability_when_probe_fails() {
        let backend = bubblewrap_backend(Err("slirp4netns not found".to_string()));
        assert!(backend.capabilities.is_empty());
        assert_eq!(backend.warnings, vec!["slirp4netns not found".to_string()]);
        let json = serde_json::to_string(&backend).expect("serializes");
        assert_eq!(
            json,
            r#"{"backend":"bubblewrap","warnings":["slirp4netns not found"]}"#
        );
    }

    #[test]
    fn to_json_pretty_emits_an_array() {
        let rendered =
            to_json_pretty(&[AvailableBackend::tierless("seatbelt")]).expect("serializes");
        assert!(
            rendered.starts_with('['),
            "probe output must be a JSON array"
        );
        assert!(rendered.contains("\"seatbelt\""));
    }

    #[test]
    fn policy_capabilities_are_serialized_in_camel_case() {
        assert_eq!(
            serde_json::to_string(&BackendCapability::FilesystemDeniedPaths).expect("serializes"),
            r#""filesystemDeniedPaths""#
        );
        assert_eq!(
            serde_json::to_string(&BackendCapability::IngressHostLoopbackAllow)
                .expect("serializes"),
            r#""ingressHostLoopbackAllow""#
        );
        assert_eq!(
            serde_json::to_string(&BackendCapability::IdentitylessLoopbackProxy)
                .expect("serializes"),
            r#""identitylessLoopbackProxy""#
        );
    }

    #[test]
    fn every_reported_backend_uses_a_canonical_name() {
        let known = backend_registry::all_containment_names();
        let reported = backends_with_host_checks_passing();
        assert!(
            !reported.is_empty(),
            "every target reports at least one backend once its host checks pass"
        );
        for entry in reported {
            assert!(
                known.contains(&entry.backend),
                "reported backend {:?} is not a canonical backend name",
                entry.backend
            );
        }
    }

    /// The reverse half of the gate: a backend registered as having no host
    /// detector must stay absent even with every host check forced to pass.
    #[test]
    fn backends_without_a_host_detector_are_never_reported() {
        let reported = backends_with_host_checks_passing();
        for entry in backend_registry::all() {
            if let ProbeCoverage::NotDetected(reason) = &entry.probe_coverage {
                let name = entry.backend.wire_name();
                assert!(
                    !reported.iter().any(|entry| entry.backend == name),
                    "{name} is reported by available_backends() but registered as undetected: \
                     {reason}"
                );
            }
        }
    }

    /// Only Windows ProcessContainer reports a tier.
    #[cfg(not(target_os = "windows"))]
    #[test]
    fn no_backend_reports_a_tier_off_windows() {
        let reported = backends_with_host_checks_passing();
        assert!(
            !reported.is_empty(),
            "every target reports at least one backend once its host checks pass"
        );
        for entry in reported {
            assert_eq!(
                entry.tier, None,
                "{} reported a tier, but no non-Windows backend has an isolation tier",
                entry.backend
            );
        }
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn every_reported_tier_is_a_canonical_tier_string() {
        for entry in backends_with_host_checks_passing() {
            if let Some(tier) = entry.tier {
                assert!(
                    CANONICAL_TIERS.contains(&tier.as_str()),
                    "reported tier {tier:?} is not a supported ProcessContainer tier"
                );
            }
        }
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_reports_processcontainer_with_a_tier_when_available() {
        let _guard = crate::mxc_common::test_env::lock();
        let backends = available_backends();
        let pc = backends.iter().find(|b| b.backend == "processcontainer");
        assert_eq!(
            pc.is_some(),
            crate::process_container_common::base_container_runner::BaseContainerRunner::is_base_container_api_present()
        );
        let Some(pc) = pc else {
            return;
        };
        let tier = pc.tier.as_deref().expect("processcontainer carries a tier");
        assert!(
            CANONICAL_TIERS.contains(&tier),
            "unexpected processcontainer tier: {tier:?}"
        );
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_reports_capture_denials_from_probe_result() {
        for capture_denials_usable in [false, true] {
            let backends = windows_backends(
                true,
                ProcessContainerCapabilities {
                    capture_denials: capture_denials_usable,
                    ..Default::default()
                },
                WindowsHostAvailability::default(),
            );
            let process_container = backends
                .iter()
                .find(|backend| backend.backend == "processcontainer")
                .expect("processcontainer must always be reported on Windows");
            assert_eq!(
                process_container
                    .capabilities
                    .contains(&BackendCapability::CaptureDenials),
                capture_denials_usable
            );
        }
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_reports_policy_capabilities_for_base_container() {
        let backends = windows_backends(
            true,
            ProcessContainerCapabilities {
                filesystem_denied_paths: true,
                filesystem_enumerate_paths: true,
                ingress_host_loopback_allow: true,
                identityless_loopback_proxy: true,
                ..Default::default()
            },
            WindowsHostAvailability::default(),
        );
        let process_container = backends
            .iter()
            .find(|backend| backend.backend == "processcontainer")
            .expect("processcontainer must always be reported on Windows");

        assert_eq!(
            process_container.capabilities,
            vec![
                BackendCapability::FilesystemDeniedPaths,
                BackendCapability::FilesystemEnumeratePaths,
                BackendCapability::IngressHostLoopbackAllow,
                BackendCapability::IdentitylessLoopbackProxy,
            ]
        );
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_omits_unavailable_processcontainer() {
        let backends = windows_backends(
            false,
            ProcessContainerCapabilities {
                capture_denials: true,
                filesystem_denied_paths: true,
                filesystem_enumerate_paths: true,
                ingress_host_loopback_allow: true,
                identityless_loopback_proxy: true,
                ..Default::default()
            },
            WindowsHostAvailability::default(),
        );
        assert!(backends.is_empty());
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_reports_identityless_proxy_with_or_without_general_ingress_support() {
        for (supported, ingress_supported) in [(false, false), (true, false), (true, true)] {
            let backends = windows_backends(
                true,
                ProcessContainerCapabilities {
                    identityless_loopback_proxy: supported,
                    ingress_host_loopback_allow: ingress_supported,
                    ..Default::default()
                },
                WindowsHostAvailability::default(),
            );
            let process_container = &backends[0];
            assert_eq!(
                process_container
                    .capabilities
                    .contains(&BackendCapability::IdentitylessLoopbackProxy),
                supported
            );
            assert_eq!(
                process_container
                    .capabilities
                    .contains(&BackendCapability::IngressHostLoopbackAllow),
                ingress_supported
            );
        }
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_preserves_capability_probe_warnings() {
        let warning = "failed to query identity-less loopback proxy support".to_string();
        let backends = windows_backends(
            true,
            ProcessContainerCapabilities {
                warnings: vec![warning.clone()],
                ..Default::default()
            },
            WindowsHostAvailability::default(),
        );
        assert_eq!(backends[0].warnings, vec![warning]);
        assert!(!backends[0]
            .capabilities
            .contains(&BackendCapability::IdentitylessLoopbackProxy));
    }

    #[cfg(not(target_os = "windows"))]
    #[test]
    fn processcontainer_never_appears_off_windows() {
        assert!(backends_with_host_checks_passing()
            .iter()
            .all(|b| b.backend != "processcontainer"));
    }
}