#![cfg(test)]
use crate::aee_seal::{
build_sealed_run, DropAccounting, NotSealEligible, ObservationEnvironment, ProxyDenial,
ProxyEnforcement, Vantage, COLLECTION_PATH_JSONRPC_PROXY, COLLECTION_PATH_LANDLOCK_TCP_CONNECT,
};
use crate::aee_seal_envelope::{
check_substrate_scope, sign_seal, verify_seal, KeyRole, SealEnvelope, TrustedObservationKey,
};
use crate::enforcement_health_v1::{EnforcementHealthV1, Probe, ReasonCode};
use ed25519_dalek::SigningKey;
const PARITY: &str =
include_str!("../../../scripts/experiments/fixtures/aee-landlock-seal/derivation-parity.json");
const SEALED_AT: &str = "2026-08-05T00:00:00Z";
const SUBSTRATE: &str = "assay-landlock-substrate";
fn environment() -> ObservationEnvironment {
let p: serde_json::Value = serde_json::from_str(PARITY).expect("parity vectors parse");
let e = &p["environment"];
let g = |k: &str| e[k].as_str().expect("digest").to_string();
ObservationEnvironment {
subject_digest: g("subject"),
substrate_digest: g("substrate"),
corpus_digest: g("corpus"),
catch_policy_digest: g("catchPolicy"),
observation_vocabulary_digest: g("observationVocabulary"),
run_entropy_digest: g("runEntropy"),
network_posture: e["networkPosture"].clone(),
}
}
fn armed_run() -> EnforcementHealthV1 {
on_disk(EnforcementHealthV1::landlock_active(
4,
vec![443],
Some(Probe {
kind: "real_block".into(),
transport: "ipv4".into(),
blocked_action: "tcp_connect".into(),
blocked_port: 4444,
blocked_errno: "EACCES".into(),
listener_reached: false,
}),
Some("restrictions_held".to_string()),
))
}
fn armed_without_probe() -> EnforcementHealthV1 {
on_disk(EnforcementHealthV1::landlock_active(
4,
vec![443],
None,
Some("restrictions_held".to_string()),
))
}
fn never_armed() -> EnforcementHealthV1 {
on_disk(EnforcementHealthV1::landlock_failed(
4,
ReasonCode::RestrictSelfFailed,
"landlock restrict_self failed in the enforcing child",
true,
true,
))
}
fn on_disk(health: EnforcementHealthV1) -> EnforcementHealthV1 {
let json = serde_json::to_string(&health).expect("health serialises as sandbox writes it");
let back: EnforcementHealthV1 =
serde_json::from_str(&json).expect("and the sealer reads it back");
assert_eq!(
back, health,
"the artifact must survive the trip to the sealer"
);
back
}
fn signing_key() -> SigningKey {
SigningKey::from_bytes(&[7u8; 32])
}
fn trusted(k: &SigningKey, paths: &[&str]) -> TrustedObservationKey {
TrustedObservationKey {
keyid: "observer-1".into(),
role: KeyRole::SubstrateObservation,
verifying_key: k.verifying_key(),
collection_paths: paths.iter().map(|p| (*p).to_string()).collect(),
substrate: SUBSTRATE.into(),
}
}
fn seal_and_sign(health: &EnforcementHealthV1, path: &str, k: &SigningKey) -> SealEnvelope {
let sealed = build_sealed_run(
Vantage::Landlock(health),
&environment(),
&[],
SEALED_AT,
&DropAccounting::SynchronousProbe,
path,
)
.expect("an armed run with a run-end block is seal-eligible");
sign_seal(&sealed.seal, k, "observer-1", KeyRole::SubstrateObservation)
.expect("the production envelope signs it")
}
#[test]
fn a_run_emits_a_signed_seal_and_a_consumer_verifies_it() {
let health = armed_run();
let k = signing_key();
let envelope = seal_and_sign(&health, COLLECTION_PATH_LANDLOCK_TCP_CONNECT, &k);
let trusted = trusted(&k, &[COLLECTION_PATH_LANDLOCK_TCP_CONNECT]);
check_substrate_scope(&trusted, SUBSTRATE).expect("the statement's substrate is in scope");
let payload = verify_seal(&envelope, &trusted).expect("a consumer verifies the seal");
assert!(
payload.aee_still_armed,
"the run was still armed at seal time"
);
assert_eq!(payload.aee_drop_count, 0);
assert_eq!(
payload.assay_collection_path,
COLLECTION_PATH_LANDLOCK_TCP_CONNECT
);
assert_eq!(
payload.assay_drop_proof_basis, "asserted",
"a synchronous probe verified nothing about a queue, and says so"
);
}
#[test]
fn an_edited_seal_does_not_verify() {
let health = armed_run();
let k = signing_key();
let mut envelope = seal_and_sign(&health, COLLECTION_PATH_LANDLOCK_TCP_CONNECT, &k);
let decoded = envelope.payload.clone();
envelope.payload = decoded.replace('0', "1");
assert_ne!(envelope.payload, decoded, "the edit must have applied");
verify_seal(
&envelope,
&trusted(&k, &[COLLECTION_PATH_LANDLOCK_TCP_CONNECT]),
)
.expect_err("an edited payload must not verify");
}
#[test]
fn a_run_that_cannot_prove_enforcement_is_refused_rather_than_sealed() {
let never_armed = never_armed();
match build_sealed_run(
Vantage::Landlock(&never_armed),
&environment(),
&[],
SEALED_AT,
&DropAccounting::SynchronousProbe,
COLLECTION_PATH_LANDLOCK_TCP_CONNECT,
) {
Err(NotSealEligible::NotArmed { .. }) => {}
other => panic!("a run that never armed must refuse, got {other:?}"),
}
let no_probe = armed_without_probe();
match build_sealed_run(
Vantage::Landlock(&no_probe),
&environment(),
&[],
SEALED_AT,
&DropAccounting::SynchronousProbe,
COLLECTION_PATH_LANDLOCK_TCP_CONNECT,
) {
Err(NotSealEligible::NoRunEndProbe) => {}
other => panic!("an armed run with no run-end probe must refuse, got {other:?}"),
}
}
#[test]
fn a_second_collection_path_verifies_under_the_same_key_and_substrate() {
const SECOND_PATH: &str = "jsonrpc-proxy";
let health = armed_run();
let k = signing_key();
let first = seal_and_sign(&health, COLLECTION_PATH_LANDLOCK_TCP_CONNECT, &k);
let second = seal_and_sign(&health, SECOND_PATH, &k);
let one_key = trusted(&k, &[COLLECTION_PATH_LANDLOCK_TCP_CONNECT, SECOND_PATH]);
let a = verify_seal(&first, &one_key).expect("first path verifies");
let b = verify_seal(&second, &one_key).expect("second path verifies under the same key");
assert_eq!(
a.assay_collection_path,
COLLECTION_PATH_LANDLOCK_TCP_CONNECT
);
assert_eq!(b.assay_collection_path, SECOND_PATH);
assert_eq!(
a.aee_run_binding, b.aee_run_binding,
"one substrate, one run: the binding must resolve to the same value on both paths, which \
is what a second key would break"
);
let narrow = trusted(&k, &[COLLECTION_PATH_LANDLOCK_TCP_CONNECT]);
verify_seal(&second, &narrow).expect_err("a path outside the key's scope must be refused");
}
fn enforcing_proxy_that_denied() -> ProxyEnforcement {
ProxyEnforcement {
enforcing: true,
failure: None,
denial: Some(ProxyDenial {
tool: "exec".into(),
reason_code: "E_TOOL_DENIED".into(),
upstream_reached: false,
}),
}
}
fn seal_proxy(e: &ProxyEnforcement, k: &SigningKey) -> SealEnvelope {
let sealed = build_sealed_run(
Vantage::JsonRpcProxy(e),
&environment(),
&[],
SEALED_AT,
&DropAccounting::SynchronousProbe,
COLLECTION_PATH_JSONRPC_PROXY,
)
.expect("an enforcing proxy with a run-end denial is seal-eligible");
sign_seal(&sealed.seal, k, "observer-1", KeyRole::SubstrateObservation)
.expect("the production envelope signs it")
}
#[test]
fn the_proxy_is_a_second_vantage_under_the_same_key_and_substrate() {
let k = signing_key();
let landlock = seal_and_sign(&armed_run(), COLLECTION_PATH_LANDLOCK_TCP_CONNECT, &k);
let proxy = seal_proxy(&enforcing_proxy_that_denied(), &k);
let one_key = trusted(
&k,
&[
COLLECTION_PATH_LANDLOCK_TCP_CONNECT,
COLLECTION_PATH_JSONRPC_PROXY,
],
);
check_substrate_scope(&one_key, SUBSTRATE).expect("one substrate");
let a = verify_seal(&landlock, &one_key).expect("kernel vantage verifies");
let b = verify_seal(&proxy, &one_key).expect("proxy vantage verifies under the same key");
assert_eq!(
a.assay_collection_path,
COLLECTION_PATH_LANDLOCK_TCP_CONNECT
);
assert_eq!(b.assay_collection_path, COLLECTION_PATH_JSONRPC_PROXY);
assert_eq!(
a.aee_run_binding, b.aee_run_binding,
"one substrate, one run: two vantages must bind to the same run, which is what a second \
key would break"
);
assert_eq!(a.assay_source_schema, "assay.enforcement_health.v1");
assert_eq!(b.assay_source_schema, "assay.enforcement_decision.v0");
assert_ne!(
a.assay_seal_scope, b.assay_seal_scope,
"the two vantages cover different things and say so"
);
}
#[test]
fn the_proxy_vantage_declines_the_claim_the_kernel_vantage_can_make() {
let k = signing_key();
let landlock = verify_seal(
&seal_and_sign(&armed_run(), COLLECTION_PATH_LANDLOCK_TCP_CONNECT, &k),
&trusted(&k, &[COLLECTION_PATH_LANDLOCK_TCP_CONNECT]),
)
.expect("verifies");
let proxy = verify_seal(
&seal_proxy(&enforcing_proxy_that_denied(), &k),
&trusted(&k, &[COLLECTION_PATH_JSONRPC_PROXY]),
)
.expect("verifies");
const BYPASS: &str = "does not prove enforcement could not be bypassed rather than shed";
assert!(
proxy.assay_non_claims.iter().any(|c| c == BYPASS),
"the proxy seal must decline the non-shedding claim: {:?}",
proxy.assay_non_claims
);
assert!(
!landlock.assay_non_claims.iter().any(|c| c == BYPASS),
"and the kernel seal must not, since its shedding probe establishes it"
);
for claim in &landlock.assay_non_claims {
assert!(
proxy.assay_non_claims.contains(claim),
"the proxy owes every standing non-claim too, missing: {claim}"
);
}
}
#[test]
fn a_proxy_that_cannot_prove_a_denial_is_refused() {
let enforcing_with_denial = |upstream_reached: bool| ProxyEnforcement {
enforcing: true,
failure: None,
denial: Some(ProxyDenial {
tool: "exec".into(),
reason_code: "E_TOOL_DENIED".into(),
upstream_reached,
}),
};
let refuse = |e: &ProxyEnforcement| {
build_sealed_run(
Vantage::JsonRpcProxy(e),
&environment(),
&[],
SEALED_AT,
&DropAccounting::SynchronousProbe,
COLLECTION_PATH_JSONRPC_PROXY,
)
.expect_err("must refuse rather than seal")
};
let never = ProxyEnforcement {
enforcing: false,
failure: Some("policy failed to load".into()),
denial: None,
};
assert!(
matches!(refuse(&never), NotSealEligible::NotArmed { .. }),
"a proxy that never enforced must refuse as NotArmed"
);
let contradictory = ProxyEnforcement {
enforcing: true,
failure: Some("policy failed to load".into()),
denial: Some(ProxyDenial {
tool: "exec".into(),
reason_code: "E_TOOL_DENIED".into(),
upstream_reached: false,
}),
};
assert!(
matches!(
refuse(&contradictory),
NotSealEligible::RecordSelfContradictory { .. }
),
"enforcing with a recorded failure is self-contradictory"
);
let nothing_denied = ProxyEnforcement {
enforcing: true,
failure: None,
denial: None,
};
assert!(
matches!(refuse(¬hing_denied), NotSealEligible::NoRunEndProbe),
"a proxy that denied nothing has no run-end proof"
);
assert!(
matches!(
refuse(&enforcing_with_denial(true)),
NotSealEligible::ProbeReachedListener
),
"a denial the call survived is not a denial"
);
}
#[test]
fn every_seal_carries_the_coverage_indistinguishability_ceiling() {
const CEILING: &str = "does not distinguish withdrawn coverage from coverage never held";
let k = signing_key();
let landlock = verify_seal(
&seal_and_sign(&armed_run(), COLLECTION_PATH_LANDLOCK_TCP_CONNECT, &k),
&trusted(&k, &[COLLECTION_PATH_LANDLOCK_TCP_CONNECT]),
)
.expect("verifies");
assert!(
landlock.assay_non_claims.iter().any(|c| c == CEILING),
"the kernel seal must carry the ceiling: {:?}",
landlock.assay_non_claims
);
assert_eq!(
landlock
.assay_non_claims
.iter()
.filter(|c| c.as_str() == CEILING)
.count(),
1,
"carried once, not appended twice by two paths"
);
}
#[test]
fn the_observed_set_recomputes_from_the_emitted_records() {
use crate::aee_seal::{
build_sealed_run, observed_set, records_ndjson, ObservationRecord, OBSERVATION_PAYLOAD_TYPE,
};
let prior = vec![
ObservationRecord {
payload: serde_json::json!({"aeeKind": "interception", "aeeVersion": "0.7", "n": 1}),
payload_type: OBSERVATION_PAYLOAD_TYPE.to_string(),
},
ObservationRecord {
payload: serde_json::json!({"aeeKind": "examination", "aeeVersion": "0.7", "n": 2}),
payload_type: OBSERVATION_PAYLOAD_TYPE.to_string(),
},
];
let run = build_sealed_run(
Vantage::Landlock(&armed_run()),
&environment(),
&prior,
SEALED_AT,
&DropAccounting::SynchronousProbe,
COLLECTION_PATH_LANDLOCK_TCP_CONNECT,
)
.expect("seal-eligible");
assert!(
run.records.len() >= 3,
"the fixture must carry more than one record or the loop is untested: {}",
run.records.len()
);
let ndjson = records_ndjson(&run.records).expect("records serialise");
assert!(
!ndjson.is_empty(),
"a run with a run-end probe emits at least the examination record"
);
let parsed: Vec<ObservationRecord> = ndjson
.lines()
.filter(|l| !l.is_empty())
.map(|l| serde_json::from_str(l).expect("emitted record parses back"))
.collect();
assert_eq!(
parsed.len(),
run.records.len(),
"no record lost on the wire"
);
assert_eq!(
observed_set(&parsed).expect("recomputes"),
run.seal.aee_observed_set,
"the seal's commitment must be checkable from the records the run wrote, not only from \
the values it held in memory"
);
}