use bvisor::{linux, macos, wasm, windows};
use bvisor::{
BoundaryRequirement, CanonicalPolicy, Capability, Enforcement, EnvEntry, EnvPolicy, FdPolicy,
NetDest, NetPolicy, RequirementKind, SpawnPolicy, SupportMatrix,
};
struct Sample {
name: &'static str,
key: RequirementKind,
canonical: CanonicalPolicy,
}
fn key_of(cap: Capability) -> RequirementKind {
RequirementKind::of(&BoundaryRequirement::Capability(cap))
}
fn dest(host: &str, port: u16) -> NetDest {
NetDest {
host: host.to_string(),
port,
}
}
fn samples() -> Vec<Sample> {
vec![
Sample {
name: "fd-none",
key: key_of(Capability::InheritedFds {
policy: FdPolicy::None,
}),
canonical: CanonicalPolicy::of_fd(&FdPolicy::None),
},
Sample {
name: "fd-only-empty",
key: key_of(Capability::InheritedFds {
policy: FdPolicy::Only(vec![]),
}),
canonical: CanonicalPolicy::of_fd(&FdPolicy::Only(vec![])),
},
Sample {
name: "fd-only-13",
key: key_of(Capability::InheritedFds {
policy: FdPolicy::Only(vec![1, 3]),
}),
canonical: CanonicalPolicy::of_fd(&FdPolicy::Only(vec![1, 3])),
},
Sample {
name: "fd-only-13-alias",
key: key_of(Capability::InheritedFds {
policy: FdPolicy::Only(vec![3, 1, 3]),
}),
canonical: CanonicalPolicy::of_fd(&FdPolicy::Only(vec![3, 1, 3])),
},
Sample {
name: "spawn-deny-new-tasks",
key: key_of(Capability::ChildSpawn {
policy: SpawnPolicy::DenyNewTasks,
}),
canonical: CanonicalPolicy::of_spawn(&SpawnPolicy::DenyNewTasks),
},
Sample {
name: "spawn-allow-threads",
key: key_of(Capability::ChildSpawn {
policy: SpawnPolicy::AllowThreadsWithinBoundary,
}),
canonical: CanonicalPolicy::of_spawn(&SpawnPolicy::AllowThreadsWithinBoundary),
},
Sample {
name: "spawn-allow-descendants",
key: key_of(Capability::ChildSpawn {
policy: SpawnPolicy::AllowDescendantsWithinBoundary,
}),
canonical: CanonicalPolicy::of_spawn(&SpawnPolicy::AllowDescendantsWithinBoundary),
},
Sample {
name: "env-empty",
key: key_of(Capability::Environment {
policy: EnvPolicy::Exact(vec![]),
}),
canonical: CanonicalPolicy::of_env(&EnvPolicy::Exact(vec![])),
},
Sample {
name: "env-entries",
key: key_of(Capability::Environment {
policy: EnvPolicy::Exact(vec![EnvEntry::literal("PATH", "/usr/bin")]),
}),
canonical: CanonicalPolicy::of_env(&EnvPolicy::Exact(vec![EnvEntry::literal(
"PATH", "/usr/bin",
)])),
},
Sample {
name: "net-deny",
key: key_of(Capability::Network {
policy: NetPolicy::DenyAll,
}),
canonical: CanonicalPolicy::of_net(&NetPolicy::DenyAll),
},
Sample {
name: "net-allow",
key: key_of(Capability::Network {
policy: NetPolicy::AllowList(vec![dest("example.com", 443)]),
}),
canonical: CanonicalPolicy::of_net(&NetPolicy::AllowList(vec![dest(
"example.com",
443,
)])),
},
Sample {
name: "net-allow-alias",
key: key_of(Capability::Network {
policy: NetPolicy::AllowList(vec![dest("b.example", 80), dest("a.example", 80)]),
}),
canonical: CanonicalPolicy::of_net(&NetPolicy::AllowList(vec![
dest("b.example", 80),
dest("a.example", 80),
])),
},
]
}
fn variant(canonical: &CanonicalPolicy) -> &[u8] {
let bytes = canonical.as_bytes();
&bytes[..bytes.len().min(2)]
}
#[derive(Debug, PartialEq, Eq)]
enum InjectivityViolation {
NotAFunction { a: &'static str, b: &'static str },
Collapse { a: &'static str, b: &'static str },
}
impl InjectivityViolation {
fn describe(&self) -> String {
match self {
Self::NotAFunction { a, b } => {
format!("{a} and {b} share a canonical variant but map to DIFFERENT keys")
}
Self::Collapse { a, b } => {
format!("{a} and {b} share a key but have DISTINCT canonical variants (collapse)")
}
}
}
}
fn check_injectivity(
samples: &[Sample],
key_of_sample: &dyn Fn(&Sample) -> RequirementKind,
) -> Vec<InjectivityViolation> {
let mut violations = Vec::new();
for a in samples {
for b in samples {
let same_variant = variant(&a.canonical) == variant(&b.canonical);
let same_key = key_of_sample(a) == key_of_sample(b);
if same_variant && !same_key {
violations.push(InjectivityViolation::NotAFunction {
a: a.name,
b: b.name,
});
}
if same_key && !same_variant {
violations.push(InjectivityViolation::Collapse {
a: a.name,
b: b.name,
});
}
}
}
violations
}
fn production_key(sample: &Sample) -> RequirementKind {
sample.key
}
#[test]
fn production_policy_to_key_map_is_injective_across_all_families() {
let samples = samples();
let violations = check_injectivity(&samples, &production_key);
let messages: Vec<String> = violations
.iter()
.map(InjectivityViolation::describe)
.collect();
assert!(
messages.is_empty(),
"production policy→key map is not injective: {messages:?}"
);
}
#[test]
fn the_split_keys_are_genuinely_distinct() {
let distinct = [
(
"InheritedFds None vs Only",
key_of(Capability::InheritedFds {
policy: FdPolicy::None,
}) != key_of(Capability::InheritedFds {
policy: FdPolicy::Only(vec![]),
}),
),
(
"ChildSpawn DenyNewTasks vs AllowDescendants",
key_of(Capability::ChildSpawn {
policy: SpawnPolicy::DenyNewTasks,
}) != key_of(Capability::ChildSpawn {
policy: SpawnPolicy::AllowDescendantsWithinBoundary,
}),
),
(
"Network DenyAll vs AllowList",
key_of(Capability::Network {
policy: NetPolicy::DenyAll,
}) != key_of(Capability::Network {
policy: NetPolicy::AllowList(vec![dest("h", 1)]),
}),
),
];
let unsplit: Vec<&str> = distinct
.iter()
.filter(|(_, ok)| !ok)
.map(|(name, _)| *name)
.collect();
assert!(
unsplit.is_empty(),
"these distinct semantics must map to DISTINCT keys but did not: {unsplit:?}"
);
}
#[test]
fn the_injectivity_check_flags_a_policy_blind_collapse() {
let samples = samples();
let violations = check_injectivity(&samples, &policy_blind_key);
assert!(
violations
.iter()
.any(|v| matches!(v, InjectivityViolation::Collapse { .. })),
"a policy-blind key map must be flagged as a collapse, got {violations:?}"
);
}
fn policy_blind_key(sample: &Sample) -> RequirementKind {
match sample.name {
"fd-none" | "fd-only-empty" | "fd-only-13" | "fd-only-13-alias" => {
RequirementKind::InheritedFdsNone
}
_ => sample.key,
}
}
#[cfg(gauntlet_red_fixture)]
#[test]
fn injective_collapse_red_fixture_policy_blind_map_must_escape() {
let samples = samples();
let violations = check_injectivity(&samples, &policy_blind_key);
assert!(
violations.is_empty(),
"RED FIXTURE: asserts the (illegal) no-collapse-found outcome on a policy-blind map; \
MUST fail because a biting injectivity check always catches the planted \
InheritedFds::None/::Only fusion"
);
}
#[derive(Debug, PartialEq, Eq)]
struct SilentGap {
backend: &'static str,
kind: RequirementKind,
}
fn check_completeness(backend: &'static str, matrix: &SupportMatrix) -> Vec<SilentGap> {
RequirementKind::ALL
.into_iter()
.filter(|&kind| !matrix.declares(kind))
.map(|kind| SilentGap { backend, kind })
.collect()
}
fn backend_matrices() -> Vec<(&'static str, SupportMatrix)> {
vec![
("linux", linux::support_matrix()),
("macos", macos::support_matrix()),
("wasm", wasm::support_matrix()),
("windows", windows::support_matrix()),
]
}
#[test]
fn every_backend_declares_every_requirement_kind() {
let mut gaps = Vec::new();
for (backend, matrix) in backend_matrices() {
gaps.extend(check_completeness(backend, &matrix));
}
let messages: Vec<String> = gaps
.iter()
.map(|g| {
format!(
"{} has a SILENT GAP for {:?} (no explicit claim)",
g.backend, g.kind
)
})
.collect();
assert!(
messages.is_empty(),
"per-profile completeness violated — every key must carry an explicit claim: {messages:?}"
);
}
#[test]
fn completeness_is_over_the_full_key_set_and_counts_unsupported() {
for (backend, matrix) in backend_matrices() {
assert_eq!(
matrix.declared_kinds().len(),
RequirementKind::ALL.len(),
"{backend} must declare exactly the full ALL key set"
);
}
let macos = macos::support_matrix();
assert!(
macos.declares(RequirementKind::ChildSpawnDenyNewTasks),
"macOS must EXPLICITLY declare ChildSpawnDenyNewTasks (an Unsupported answer still counts)"
);
assert_eq!(
macos
.best_case_for(RequirementKind::ChildSpawnDenyNewTasks)
.enforcement,
Enforcement::Unsupported,
"macOS ChildSpawnDenyNewTasks is the explicit Unsupported answer"
);
}
#[test]
fn the_completeness_check_flags_a_dropped_key() {
let gaps = check_completeness("linux-with-dropped-key", &matrix_missing_one_key());
assert!(
gaps.iter().any(|g| g.kind == RequirementKind::Kill),
"a matrix missing Kill must be flagged as a silent gap, got {gaps:?}"
);
}
fn matrix_missing_one_key() -> SupportMatrix {
use std::collections::BTreeMap;
let full = linux::support_matrix();
let mut best = BTreeMap::new();
for kind in full.declared_kinds() {
if kind == RequirementKind::Kill {
continue; }
best.insert(kind, full.best_case_for(kind));
}
SupportMatrix::from_best_case(best)
}
#[cfg(gauntlet_red_fixture)]
#[test]
fn support_completeness_red_fixture_dropped_key_must_escape() {
let gaps = check_completeness("linux-with-dropped-key", &matrix_missing_one_key());
assert!(
gaps.is_empty(),
"RED FIXTURE: asserts the (illegal) no-gap outcome on a matrix missing Kill; MUST fail \
because a biting completeness check always catches the dropped key"
);
}
#[test]
fn aspiration_matrix_may_outrun_the_proven_ceiling() {
let linux = linux::support_matrix();
assert_eq!(
linux
.best_case_for(RequirementKind::NetworkDenyAll)
.enforcement,
Enforcement::Enforced,
"the aspiration matrix advertises NetworkDenyAll Enforced (the §1 aspiration claim)"
);
assert!(
linux.declares(RequirementKind::NetworkDenyAll),
"the aspiration claim is an EXPLICIT, declared cell (auditable), not a silent default"
);
}