use std::collections::HashMap;
use antigen_fingerprint::Fingerprint;
use crate::finding::{Finding, Provenance};
use crate::scan::ScanReport;
include!(concat!(env!("OUT_DIR"), "/stdlib_catalog.rs"));
#[derive(Debug, Clone)]
pub struct CatalogEntry {
pub name: String,
pub fingerprint: Fingerprint,
pub provenance: Provenance,
}
#[must_use]
pub fn stdlib_catalog() -> Vec<(String, Fingerprint)> {
stdlib_catalog_entries()
.into_iter()
.map(|e| (e.name, e.fingerprint))
.collect()
}
#[must_use]
pub fn stdlib_catalog_entries() -> Vec<CatalogEntry> {
stdlib_catalog_checked().unwrap_or_else(|e| {
panic!("antigen bug: a bundled stdlib fingerprint failed to parse: {e}")
})
}
pub fn stdlib_catalog_checked() -> syn::Result<Vec<CatalogEntry>> {
STDLIB_CATALOG
.iter()
.map(|(name, fp_str, prov_str)| {
let fingerprint = Fingerprint::parse(fp_str)?;
let provenance = Provenance::from_variant_str(prov_str).unwrap_or(Provenance::DEFAULT);
Ok(CatalogEntry {
name: (*name).to_string(),
fingerprint,
provenance,
})
})
.collect()
}
#[must_use]
pub const fn stdlib_catalog_len() -> usize {
STDLIB_CATALOG.len()
}
fn catalog_provenance_map() -> HashMap<String, Provenance> {
stdlib_catalog_entries()
.into_iter()
.map(|e| (e.name, e.provenance))
.collect()
}
#[must_use]
pub fn bundled_catalog_findings(report: &ScanReport) -> Vec<Finding> {
crate::scan::catalog_match_findings_with_source(
report,
&catalog_provenance_map(),
"scan:bundled-catalog",
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::finding::{FindingBody, OriginStage};
use crate::scan::MatchKind;
#[test]
fn bundled_catalog_is_non_empty() {
assert!(
stdlib_catalog_len() > 0,
"the bundled stdlib catalog must ship at least one flagship fingerprint"
);
}
#[test]
fn every_bundled_fingerprint_parses() {
let entries = stdlib_catalog_checked().expect("every bundled fingerprint parses");
assert_eq!(entries.len(), stdlib_catalog_len());
}
#[test]
fn flagship_provenance_is_in_the_honest_verified_core() {
let entries = stdlib_catalog_entries();
for e in &entries {
assert!(
matches!(
e.provenance,
Provenance::Constructable | Provenance::Encountered
),
"bundled flagship `{}` must carry a verified-core provenance, got {:?}",
e.name,
e.provenance
);
}
}
#[test]
fn known_flagship_present() {
let names: Vec<String> = stdlib_catalog_entries()
.into_iter()
.map(|e| e.name)
.collect();
assert!(
names.iter().any(|n| n == "panic-in-drop"),
"expected the `panic-in-drop` flagship in the bundled catalog; got {names:?}"
);
}
fn e0_fixture() -> std::path::PathBuf {
std::path::Path::new("tests")
.join("fixtures")
.join("e0_bundled_catalog_consumer")
}
#[test]
fn zero_declaration_crate_without_bundled_catalog_is_a_false_all_clear() {
let report = crate::scan::scan_workspace(&e0_fixture(), None).expect("scan succeeds");
assert!(
report.antigens.is_empty(),
"fixture is a zero-declaration consumer crate"
);
let fp_matches = report
.presentations
.iter()
.filter(|p| p.match_kind == MatchKind::FingerprintMatch)
.count();
assert_eq!(
fp_matches, 0,
"without the bundled catalog, a zero-declaration crate gets zero matches (the false all-clear)"
);
}
#[test]
fn bundled_catalog_scan_finds_real_failure_classes() {
let report = crate::scan::scan_workspace_bundled_catalog(&e0_fixture(), None, true)
.expect("bundled-catalog scan succeeds");
let findings = bundled_catalog_findings(&report);
assert!(
!findings.is_empty(),
"the bundled catalog must surface >=1 real finding on the consumer fixture; got none"
);
assert!(
findings.iter().any(|f| {
matches!(&f.body, FindingBody::FingerprintMatch { class, .. } if class == "panic-in-drop")
}),
"expected a panic-in-drop match on the fixture's UnwindBomb; got {:?}",
findings.iter().map(|f| &f.body).collect::<Vec<_>>()
);
}
#[test]
fn every_bundled_finding_carries_a_verified_core_provenance() {
let report = crate::scan::scan_workspace_bundled_catalog(&e0_fixture(), None, true)
.expect("bundled-catalog scan succeeds");
let findings = bundled_catalog_findings(&report);
assert!(!findings.is_empty(), "precondition: >=1 finding");
for f in &findings {
assert!(
matches!(
f.class_provenance,
Provenance::Constructable | Provenance::Encountered
),
"every bundled finding must carry a verified-core provenance; {f:?} did not"
);
}
}
#[test]
fn no_bundled_finding_claims_an_audited_defense_verdict() {
let report = crate::scan::scan_workspace_bundled_catalog(&e0_fixture(), None, true)
.expect("bundled-catalog scan succeeds");
let findings = bundled_catalog_findings(&report);
assert!(!findings.is_empty(), "precondition: >=1 finding");
for f in &findings {
assert!(
!matches!(f.body, FindingBody::DialVerdict { .. }),
"a bundled-catalog match must NEVER masquerade as an audited DialVerdict; {f:?}"
);
assert_eq!(
f.origin_stage,
OriginStage::Scan,
"a bundled match is scan-emitted, not audit-emitted"
);
}
}
#[test]
fn bundled_catalog_spares_the_clean_sibling() {
let report = crate::scan::scan_workspace_bundled_catalog(&e0_fixture(), None, true)
.expect("bundled-catalog scan succeeds");
let findings = bundled_catalog_findings(&report);
for f in &findings {
if matches!(&f.body, FindingBody::FingerprintMatch { class, .. } if class == "panic-in-drop")
{
assert!(
!f.file.contains("CleanGuard"),
"panic-in-drop must spare the clean sibling"
);
}
}
let drop_matches = findings
.iter()
.filter(|f| matches!(&f.body, FindingBody::FingerprintMatch { class, .. } if class == "panic-in-drop"))
.count();
assert_eq!(
drop_matches, 1,
"exactly one panic-in-drop site (UnwindBomb) — the clean sibling is spared"
);
}
}