use std::path::PathBuf;
use crate::install::ManifestVerifier;
use crate::manifest::LayerManifest;
use crate::store::{InstalledLayer, Store, manifest_digest};
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const PIN_MANIFEST_VERSION: i64 = 1;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PayloadStatus {
Verified {
name: String,
path: PathBuf,
digest: String,
},
AbsentFromLayer {
name: String,
layer: String,
available: Vec<String>,
},
NoEntryForPlatform {
name: String,
layer: String,
platform: String,
platforms: Vec<String>,
},
MissingFromStore {
name: String,
path: PathBuf,
digest: String,
},
DigestMismatch {
name: String,
path: PathBuf,
signed: String,
found: String,
},
LayerNotAuthentic { layer: String, reason: String },
Unreadable {
name: String,
path: PathBuf,
reason: String,
},
}
impl PayloadStatus {
pub fn is_verified(&self) -> bool {
matches!(self, PayloadStatus::Verified { .. })
}
}
pub fn payload_status(
store: &Store,
layer: &InstalledLayer,
verifier: &dyn ManifestVerifier,
name: &str,
platform: &str,
) -> PayloadStatus {
let not_authentic = |reason: String| PayloadStatus::LayerNotAuthentic {
layer: layer.layer.to_string(),
reason,
};
let envelope_path = layer.root.join(crate::reverify::ENVELOPE_FILE);
let envelope = match std::fs::read(&envelope_path) {
Ok(bytes) => bytes,
Err(e) => return not_authentic(format!("{}: {e}", envelope_path.display())),
};
let signed = match verifier.verify(&envelope) {
Ok(payload) => payload,
Err(e) => return not_authentic(e.to_string()),
};
let manifest_path = layer.root.join("layer.json");
match std::fs::read(&manifest_path) {
Ok(stored) if stored == signed => {}
Ok(_) => {
return not_authentic(
"the stored manifest is not the one the envelope signed".to_string(),
);
}
Err(e) => return not_authentic(format!("{}: {e}", manifest_path.display())),
}
let manifest = match LayerManifest::parse(&signed) {
Ok(m) => m,
Err(e) => return not_authentic(e.to_string()),
};
let named: Vec<_> = manifest
.entries
.iter()
.filter(|e| e.annotations.get("eu.pulseengine.tool").map(String::as_str) == Some(name))
.collect();
if named.is_empty() {
let mut available: Vec<String> = manifest
.entries
.iter()
.filter_map(|e| e.annotations.get("eu.pulseengine.tool").cloned())
.collect();
available.sort();
available.dedup();
return PayloadStatus::AbsentFromLayer {
name: name.to_string(),
layer: layer.layer.to_string(),
available,
};
}
let Some(entry) = named.iter().find(|e| {
crate::platform::entry_matches(
e.annotations
.get(crate::platform::ANN_PLATFORM)
.map(String::as_str),
platform,
)
}) else {
let mut platforms: Vec<String> = named
.iter()
.filter_map(|e| e.annotations.get(crate::platform::ANN_PLATFORM).cloned())
.collect();
platforms.sort();
platforms.dedup();
return PayloadStatus::NoEntryForPlatform {
name: name.to_string(),
layer: layer.layer.to_string(),
platform: platform.to_string(),
platforms,
};
};
let Some(path) = store.entry_path(layer, entry) else {
return PayloadStatus::MissingFromStore {
name: name.to_string(),
path: layer.root.clone(),
digest: entry.digest.clone(),
};
};
let bytes = match std::fs::read(&path) {
Ok(b) => b,
Err(e) => {
return PayloadStatus::Unreadable {
name: name.to_string(),
path,
reason: e.to_string(),
};
}
};
let found = manifest_digest(&bytes);
if found != entry.digest {
return PayloadStatus::DigestMismatch {
name: name.to_string(),
path,
signed: entry.digest.clone(),
found,
};
}
PayloadStatus::Verified {
name: name.to_string(),
path,
digest: entry.digest.clone(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::install::{InstallPolicy, install};
use crate::manifest::fixtures::manifest_with_tools;
use crate::pin::Pin;
use crate::rollback::HighWaterMarks;
use crate::source::MemorySource;
use crate::verify::{PinnedKeyVerifier, generate_root_keypair, sign_layer_manifest};
struct Installed {
_tmp: tempfile::TempDir,
store: Store,
layer: InstalledLayer,
verifier: PinnedKeyVerifier,
}
fn installed_layer() -> Installed {
let (sk, pk) = generate_root_keypair();
let synth = b"synth-bytes".to_vec();
let blob_digest = manifest_digest(&synth);
let payload = manifest_with_tools(
"2026.07.0",
"qualified",
1,
"2026-07-31T09:14:00Z",
&[("synth", &blob_digest)],
);
let envelope = sign_layer_manifest(&payload, &sk, "varve-root-1").unwrap();
let source = MemorySource::new()
.with_manifest(envelope.as_bytes())
.with_blob(&blob_digest, &synth);
let pin = Pin::parse(
"manifest-version = 1\n[toolchain]\nchannel = \"qualified\"\nlayer = \"2026.07.0\"\n",
"varve.toml",
)
.unwrap();
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("root");
let store = Store::at(&root);
let mut marks = HighWaterMarks::load(&root).unwrap();
let verifier = PinnedKeyVerifier::from_public_key_bytes(&pk).unwrap();
let policy = InstallPolicy {
index: None,
now: "2026-08-07T00:00:00Z",
staleness_threshold_days: 90,
platform: "test-platform",
};
let outcome = install(&pin, &source, &verifier, &store, &mut marks, &policy).unwrap();
let layer = store.get(&outcome.digest).unwrap().unwrap();
Installed {
_tmp: tmp,
store,
layer,
verifier,
}
}
#[test]
fn a_present_and_intact_payload_is_verified_against_its_signed_digest() {
let ctx = installed_layer();
let got = payload_status(
&ctx.store,
&ctx.layer,
&ctx.verifier,
"synth",
"test-platform",
);
match &got {
PayloadStatus::Verified { name, path, digest } => {
assert_eq!(name, "synth");
assert!(path.exists(), "the verified path must be usable");
assert_eq!(digest, &manifest_digest(b"synth-bytes"));
}
other => panic!("expected Verified, got {other:?}"),
}
assert!(got.is_verified());
}
#[test]
fn a_tampered_payload_is_a_digest_mismatch_and_never_an_absence() {
let ctx = installed_layer();
let PayloadStatus::Verified { path, digest, .. } = payload_status(
&ctx.store,
&ctx.layer,
&ctx.verifier,
"synth",
"test-platform",
) else {
panic!("precondition: the fixture must verify before it is tampered with");
};
std::fs::write(&path, b"tampered").unwrap();
let got = payload_status(
&ctx.store,
&ctx.layer,
&ctx.verifier,
"synth",
"test-platform",
);
match &got {
PayloadStatus::DigestMismatch {
signed,
found,
name,
..
} => {
assert_eq!(name, "synth");
assert_eq!(signed, &digest, "the SIGNED digest is reported verbatim");
assert_eq!(found, &manifest_digest(b"tampered"));
assert_ne!(signed, found);
}
other => panic!("tampering must not be reported as {other:?}"),
}
assert!(!got.is_verified());
assert!(
!matches!(got, PayloadStatus::AbsentFromLayer { .. }),
"an integrity failure reported as absence is the fail-open bug"
);
}
#[test]
fn a_name_the_layer_does_not_carry_is_absent_and_says_what_is_there() {
let ctx = installed_layer();
match payload_status(
&ctx.store,
&ctx.layer,
&ctx.verifier,
"meld",
"test-platform",
) {
PayloadStatus::AbsentFromLayer {
name,
layer,
available,
} => {
assert_eq!(name, "meld");
assert_eq!(layer, "2026.07.0");
assert_eq!(
available,
vec!["synth".to_string()],
"absence must name what IS carried, or the consumer's next \
question is unanswerable"
);
}
other => panic!("expected AbsentFromLayer, got {other:?}"),
}
}
#[test]
fn a_payload_missing_from_the_store_is_distinct_from_one_the_layer_never_named() {
let ctx = installed_layer();
let PayloadStatus::Verified { path, .. } = payload_status(
&ctx.store,
&ctx.layer,
&ctx.verifier,
"synth",
"test-platform",
) else {
panic!("precondition");
};
std::fs::remove_file(&path).unwrap();
let got = payload_status(
&ctx.store,
&ctx.layer,
&ctx.verifier,
"synth",
"test-platform",
);
assert!(
matches!(got, PayloadStatus::MissingFromStore { .. }),
"a deleted payload the manifest NAMES is a broken install, not an \
absence and not tampering — got {got:?}"
);
}
#[cfg(unix)]
fn premise_unavailable() -> bool {
use std::os::unix::fs::PermissionsExt;
let Ok(dir) = tempfile::tempdir() else {
return true;
};
let probe = dir.path().join("probe");
if std::fs::write(&probe, b"x").is_err() {
return true;
}
if std::fs::set_permissions(&probe, std::fs::Permissions::from_mode(0o000)).is_err() {
return true;
}
let readable = std::fs::read(&probe).is_ok();
let _ = std::fs::set_permissions(&probe, std::fs::Permissions::from_mode(0o644));
readable
}
#[cfg(unix)]
#[test]
fn an_unreadable_payload_is_not_reported_as_missing() {
use std::os::unix::fs::PermissionsExt;
if premise_unavailable() {
eprintln!("skipped: mode 000 does not deny reads here");
return;
}
let ctx = installed_layer();
let PayloadStatus::Verified { path, .. } = payload_status(
&ctx.store,
&ctx.layer,
&ctx.verifier,
"synth",
"test-platform",
) else {
panic!("precondition: the fixture must verify first");
};
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o000)).unwrap();
let got = payload_status(
&ctx.store,
&ctx.layer,
&ctx.verifier,
"synth",
"test-platform",
);
let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644));
match &got {
PayloadStatus::Unreadable { name, reason, .. } => {
assert_eq!(name, "synth");
assert!(!reason.is_empty(), "the fault must be NAMED, not implied");
}
other => panic!(
"an unreadable payload must not be reported as {other:?} — a permissions \
fault presented as absence is the fail-open confusion this module exists \
to prevent"
),
}
assert!(!got.is_verified());
}
#[test]
fn a_layer_whose_manifest_was_swapped_can_report_nothing_as_verified() {
let ctx = installed_layer();
let manifest_path = ctx.layer.root.join("layer.json");
let mut doctored = std::fs::read(&manifest_path).unwrap();
doctored.extend_from_slice(b"\n");
std::fs::write(&manifest_path, &doctored).unwrap();
let got = payload_status(
&ctx.store,
&ctx.layer,
&ctx.verifier,
"synth",
"test-platform",
);
assert!(
matches!(got, PayloadStatus::LayerNotAuthentic { .. }),
"a manifest that is not what the envelope signed voids every answer \
about the layer's contents — got {got:?}"
);
assert!(!got.is_verified());
}
#[test]
fn a_stranger_root_cannot_make_a_payload_verified() {
let ctx = installed_layer();
let (_sk, other_pk) = generate_root_keypair();
let stranger = PinnedKeyVerifier::from_public_key_bytes(&other_pk).unwrap();
let got = payload_status(&ctx.store, &ctx.layer, &stranger, "synth", "test-platform");
assert!(
matches!(got, PayloadStatus::LayerNotAuthentic { .. }),
"the trust root decides, and a stranger's root decides nothing — got {got:?}"
);
}
#[test]
fn a_payload_built_for_another_platform_is_not_reported_as_absent() {
use crate::manifest::fixtures::manifest_with_platform_tools;
let (sk, pk) = generate_root_keypair();
let blob = b"linux-only-bytes".to_vec();
let digest = manifest_digest(&blob);
let payload = manifest_with_platform_tools(
"2026.07.0",
"qualified",
1,
"2026-07-31T09:14:00Z",
&[("meld", &digest, Some("x86_64-unknown-linux-gnu"))],
);
let envelope = sign_layer_manifest(&payload, &sk, "varve-root-1").unwrap();
let source = MemorySource::new()
.with_manifest(envelope.as_bytes())
.with_blob(&digest, &blob);
let pin = Pin::parse(
"manifest-version = 1\n[toolchain]\nchannel = \"qualified\"\nlayer = \"2026.07.0\"\n",
"varve.toml",
)
.unwrap();
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("root");
let store = Store::at(&root);
let mut marks = HighWaterMarks::load(&root).unwrap();
let verifier = PinnedKeyVerifier::from_public_key_bytes(&pk).unwrap();
let outcome = install(
&pin,
&source,
&verifier,
&store,
&mut marks,
&InstallPolicy {
index: None,
now: "2026-08-07T00:00:00Z",
staleness_threshold_days: 90,
platform: "x86_64-unknown-linux-gnu",
},
)
.unwrap();
let layer = store.get(&outcome.digest).unwrap().unwrap();
match payload_status(&store, &layer, &verifier, "meld", "aarch64-apple-darwin") {
PayloadStatus::NoEntryForPlatform {
name,
platform,
platforms,
..
} => {
assert_eq!(name, "meld");
assert_eq!(platform, "aarch64-apple-darwin");
assert_eq!(
platforms,
vec!["x86_64-unknown-linux-gnu".to_string()],
"it must say which platforms the layer DOES carry"
);
}
other => panic!("expected NoEntryForPlatform, got {other:?}"),
}
}
#[test]
fn the_crate_states_its_version_and_the_pin_format_it_understands() {
assert!(!VERSION.is_empty());
assert_eq!(VERSION, env!("CARGO_PKG_VERSION"));
assert_eq!(PIN_MANIFEST_VERSION, 1);
let newer = format!(
"manifest-version = {}\n[toolchain]\nchannel = \"rolling\"\nlayer = \"2026.07.0\"\n",
PIN_MANIFEST_VERSION + 1
);
assert!(
Pin::parse(&newer, "varve.toml").is_err(),
"a pin one version newer than PIN_MANIFEST_VERSION must be refused, or \
the constant is not describing the parser"
);
}
}