test: cover overlay provenance validation paths
Linux / verify (pull_request) Successful in 2m8s
Linux / changed-scope-coverage (pull_request) Skipped
Linux / keyless-cuda (pull_request) Skipped
Linux / hosted-cuda-package (pull_request) Skipped
Linux / linux-cli-container (pull_request) Successful in 1m23s
Linux / linux-static-cli (pull_request) Successful in 5m23s
Linux / release-cli (pull_request) Skipped

This commit is contained in:
Pavlo Ratushnyi
2026-10-07 02:26:10 +02:00
parent 65a01a8516
commit ed4199464d
2 changed files with 152 additions and 3 deletions
+1 -3
View File
@@ -530,9 +530,7 @@ pub fn resolve_verified(base_archive: &Path, overlay_archive: &Path) -> Result<R
return Err(format!("{}: overlay source mapping differs", source.name));
}
}
if replacements.len() != overlay.records.len() {
return Err("duplicate overlay provenance tensor".into());
}
// The earlier source-count check and per-source lookup enforce this count.
let mut records = Vec::with_capacity(base_sources.len());
for (index, source) in base_sources.iter().enumerate() {
let replacement = replacements.get(source.name.as_str()).copied();
+151
View File
@@ -4707,6 +4707,36 @@ mod tests {
assert!(!legacy_staging_path(&output).exists());
}
#[test]
fn sharded_provenance_conflict_after_publish_removes_only_archive() {
assert_sharded_provenance_conflict_cleanup(&overlay_fixture());
}
fn assert_sharded_provenance_conflict_cleanup(fixture: &OverlayFixture) {
let output = fixture.directory.join("published.kar");
fs::copy(&fixture.base, &output).unwrap();
let summary = kar::verify(&output).unwrap();
let identity =
derived_mixed::fingerprint_shards(&fixture.manifest, &fixture.directory).unwrap();
let sidecar = provenance_path(&output).unwrap();
let reserved = b"another publisher's provenance";
fs::write(&sidecar, reserved).unwrap();
let request = ShardedArchiveRequest {
identity: &identity,
prepared: &[],
architecture: "qwen35",
destination: &output,
levels: 2,
maximum_relative_l2: 0.0,
};
assert!(
publish_sharded_provenance(&request, summary, hash_file(&output).unwrap()).is_err()
);
assert!(!output.exists());
assert_eq!(fs::read(sidecar).unwrap(), reserved);
}
#[test]
fn preview_kind_names_are_stable_for_validation_errors() {
assert_eq!(preview_kind_name(PreviewKind::F32), "F32");
@@ -5039,6 +5069,127 @@ mod tests {
assert!(crate::kar_overlay::resolve_verified(&fixture.base, &fixture.overlay).is_err());
}
#[test]
fn resolver_rejects_overlay_record_from_different_source_tensor() {
assert_overlay_record_source_mismatch(&overlay_fixture());
}
fn assert_overlay_record_source_mismatch(fixture: &OverlayFixture) {
fixture.prepare().unwrap();
for mismatch in 0..4 {
let archive = fixture
.directory
.join(format!("mismatched-overlay-{mismatch}.kar"));
write_mismatched_overlay_archive(fixture, &archive, mismatch);
let (_, records) = kar::verify_with_sources(&archive).unwrap();
write_mismatched_overlay_sidecar(fixture, &archive, &records[0]);
assert_overlay_source_mismatch(&fixture.base, &archive, mismatch);
}
}
fn assert_overlay_source_mismatch(base: &Path, archive: &Path, mismatch: u8) {
let error = crate::kar_overlay::resolve_verified(base, archive)
.err()
.unwrap();
let expected = if mismatch == 3 {
"overlay source mapping differs"
} else {
"overlay replaces a different source tensor"
};
assert!(error.contains(expected), "{mismatch}: {error}");
}
fn write_mismatched_overlay_archive(
fixture: &OverlayFixture,
destination: &Path,
mismatch: u8,
) {
let (base, sources) = kar::verify_with_sources(&fixture.base).unwrap();
let source = sources
.iter()
.find(|record| record.name == "dense")
.unwrap();
let raw = mismatched_overlay_payload(fixture, mismatch);
let dimensions = mismatched_overlay_dimensions(source, mismatch);
let tensor = mismatched_overlay_tensor(source, &raw, &dimensions, mismatch);
write_mismatched_overlay_with_base(destination, base, tensor);
}
fn mismatched_overlay_payload(fixture: &OverlayFixture, mismatch: u8) -> Vec<u8> {
let dtype = u8::from(mismatch == 3);
if dtype == 0 {
fixture
.values
.iter()
.flat_map(|value| value.to_le_bytes())
.collect()
} else {
vec![0; fixture.values.len() * 2]
}
}
fn mismatched_overlay_dimensions(source: &kar::TensorSource, mismatch: u8) -> Vec<u64> {
if mismatch == 2 {
vec![128, 2]
} else {
source.dimensions.clone()
}
}
fn mismatched_overlay_tensor<'a>(
source: &kar::TensorSource,
raw: &'a [u8],
dimensions: &'a [u64],
mismatch: u8,
) -> kar_writer::Tensor<'a> {
kar_writer::Tensor {
name: "dense",
dims: dimensions,
source_dtype: u8::from(mismatch == 3),
source_digest: if mismatch == 0 {
[0xa5; 32]
} else {
source.digest
},
source_offset: source.offset + u64::from(mismatch == 1) * 4,
data: kar_writer::TensorData::Raw(raw),
}
}
fn write_mismatched_overlay_with_base(
destination: &Path,
base: kar::Summary,
tensor: kar_writer::Tensor<'_>,
) {
let identity = kar_writer::SourceIdentity {
name: "qwen35",
architecture: "qwen35",
builder: "test",
digest: base.source_digest,
total_elements: base.source_total_elements,
bytes: base.source_bytes,
};
kar_writer::write_archive(destination, &identity, &[tensor]).unwrap();
}
fn write_mismatched_overlay_sidecar(
fixture: &OverlayFixture,
archive: &Path,
record: &kar::TensorSource,
) {
let mut value = read_json(&fixture.overlay_sidecar());
value["overlay_archive_sha256"] =
serde_json::json!(hex::encode(hash_file(archive).unwrap()));
value["records"][0]["f32_sha256"] = serde_json::json!(hex::encode(record.digest));
value["records"][0]["derived_offset"] = serde_json::json!(record.offset);
value["records"][0]["section_bytes"] = serde_json::json!(record.section_bytes);
fs::write(
provenance_path(archive).unwrap(),
serde_json::to_vec(&value).unwrap(),
)
.unwrap();
}
#[test]
fn resolver_rejects_malformed_base_sidecar() {
let fixture = overlay_fixture();