Files
pruefwerk/compliance-core/tests/sbom_bsi.rs
T
sharang 77d27646b8
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 1m11s
CI / Deploy Dashboard (push) Successful in 4m1s
CI / Deploy MCP (push) Successful in 3m12s
CI / Deploy Agent (push) Successful in 5m57s
feat(sbom): component creator from registries and purls (#508, part B2) (#512)
2026-10-06 15:03:16 +00:00

140 lines
4.7 KiB
Rust

//! The CycloneDX SBOM scored by sbomqs against BSI TR-03183-2 v2.1 (#508).
//! Needs the sbomqs binary (`SBOMQS`, else `sbomqs` on PATH), so it is
//! ignored by default: `cargo test -p compliance-core --test sbom_bsi --
//! --ignored --nocapture`. Prints every check and asserts the ones the
//! document's shape meets; the rest need data the SBOM does not have (#508
//! lists them).
#![allow(clippy::expect_used, clippy::unwrap_used)]
use compliance_core::models::{ComponentHash, SbomEntry};
use compliance_core::sbom_docs;
/// One registry package per ecosystem whose file its purl names.
fn entries() -> Vec<SbomEntry> {
[
(
"log4j-core",
"2.17.1",
"maven",
"pkg:maven/org.apache.logging.log4j/log4j-core@2.17.1",
"Apache-2.0",
),
(
"core",
"16.2.0",
"npm",
"pkg:npm/%40angular/core@16.2.0",
"MIT",
),
(
"Newtonsoft.Json",
"13.0.3",
"nuget",
"pkg:nuget/Newtonsoft.Json@13.0.3",
"MIT",
),
(
"serde",
"1.0.200",
"cargo",
"pkg:cargo/serde@1.0.200",
"MIT OR Apache-2.0",
),
]
.into_iter()
.map(|(name, version, manager, purl, license)| {
let mut e = SbomEntry::new("t".into(), name.into(), version.into(), manager.into());
e.purl = Some(purl.into());
e.license = Some(license.into());
e.manufacturer_url = Some(format!("https://project.example/{name}"));
// What the lockfiles give (#508): npm and NuGet a SHA-512, Cargo a
// SHA-256, Maven nothing.
let (alg, len) = match manager {
"npm" | "nuget" => ("SHA-512", 128),
"cargo" => ("SHA-256", 64),
_ => return e,
};
e.hashes = vec![ComponentHash {
alg: alg.into(),
content: "ab".repeat(len / 2),
}];
e.download_url = Some(format!("https://registry.example/{name}-{version}"));
e
})
.collect()
}
#[test]
#[ignore = "needs the sbomqs binary"]
fn the_document_shape_meets_the_bsi_checks_it_can() {
let sbomqs = std::env::var("SBOMQS").unwrap_or_else(|_| "sbomqs".into());
let dir = std::env::temp_dir().join(format!("sbom-bsi-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("sbom.cdx.json");
let doc = sbom_docs::cyclonedx(&entries(), "Pump Remote");
std::fs::write(&path, serde_json::to_vec_pretty(&doc).unwrap()).unwrap();
let output = std::process::Command::new(&sbomqs)
.args(["score", "--json", "--profile", "bsi-v2.1"])
.arg(&path)
.output()
.expect("sbomqs runs");
std::fs::remove_dir_all(&dir).ok();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
let out: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
let profile = &out["files"][0]["profiles"][0];
println!(
"{} {} {}",
profile["profile"], profile["score"], profile["grade"]
);
let mut scores = std::collections::HashMap::new();
for f in profile["features"].as_array().unwrap() {
println!(
"{:>5} {:<28} required={:<5} {}",
f["score"],
f["key"].as_str().unwrap(),
f["required"],
f["description"]
);
scores.insert(
f["key"].as_str().unwrap().to_string(),
f["score"].as_f64().unwrap(),
);
}
for key in [
"sbom_spec_version",
"sbom_creator",
"sbom_timestamp",
"sbom_uri",
"comp_name",
"comp_version",
"comp_depth",
"comp_other_identifiers",
] {
assert_eq!(scores.get(key).copied(), Some(10.0), "{key}");
}
// Met by every component but the product. The product is a logical
// component in the TR's terms (section 3.2.2): it has no file, so no
// filename or file properties, and Prüfwerk knows neither its licence nor
// its manufacturer's web address.
// sbomqs 2.1.2 counts it like the others.
for key in [
"comp_creator",
"comp_filename",
"comp_executable_prop",
"comp_archive_prop",
"comp_structured_prop",
"comp_original_licenses",
] {
assert_eq!(scores.get(key).copied(), Some(8.0), "{key}");
}
// The deployable hash counts only as SHA-512 on a download: npm and
// NuGet (2 of 5); the URL counts for Cargo too (3 of 5).
assert_eq!(scores.get("comp_deployable_hash").copied(), Some(4.0));
assert_eq!(scores.get("comp_download_url").copied(), Some(6.0));
}