test: cover archive and seek error paths
Linux / verify (pull_request) Successful in 2m11s
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 1m28s
Linux / linux-static-cli (pull_request) Successful in 5m27s
Linux / release-cli (pull_request) Skipped

This commit is contained in:
Pavlo Ratushnyi
2026-10-07 02:38:44 +02:00
parent ed4199464d
commit 11f42fa03a
2 changed files with 115 additions and 3 deletions
+17 -3
View File
@@ -519,11 +519,12 @@ pub fn resolve_verified(base_archive: &Path, overlay_archive: &Path) -> Result<R
.iter()
.find(|record| record.name == source.name)
.ok_or("overlay KAR tensor absent from provenance")?;
if replacements.insert(source.name.as_str(), index).is_some()
|| record.derived_offset != source.offset
// `verify_with_sources` rejects duplicate archive names before this loop.
replacements.insert(source.name.as_str(), index);
// Strict sidecar JSON parsing rejects non-finite and out-of-range f64 values.
if record.derived_offset != source.offset
|| record.f32_sha256 != hex::encode(source.digest)
|| record.section_bytes != source.section_bytes
|| !record.relative_l2.is_finite()
|| record.relative_l2 < 0.0
|| source.dtype != 0
{
@@ -1340,6 +1341,19 @@ mod tests {
static TEST_SEQUENCE: AtomicU64 = AtomicU64::new(0);
#[test]
fn strict_json_rejects_non_finite_overlay_error_values() {
for number in ["NaN", "Infinity", "-Infinity", "1e999", "-1e999"] {
let json = format!(
r#"{{"schema":"strata-next/kar-overlay-provenance-v1","base_archive_sha256":"base","overlay_archive_sha256":"overlay","levels":2,"records":[{{"name":"weight","derived_offset":0,"f32_sha256":"digest","section_bytes":0,"relative_l2":{number}}}]}}"#
);
assert!(
serde_json::from_str::<OverlayProvenance>(&json).is_err(),
"{number}"
);
}
}
struct TestDir(PathBuf);
impl TestDir {
+98
View File
@@ -3047,6 +3047,84 @@ mod tests {
);
}
#[cfg(target_os = "linux")]
#[test]
fn nonseekable_sources_return_seek_errors_for_sharded_and_single_reads() {
assert_nonseekable_sharded_read_fails(&overlay_fixture());
assert_nonseekable_single_read_fails(&overlay_fixture());
}
#[cfg(target_os = "linux")]
fn assert_nonseekable_sharded_read_fails(fixture: &OverlayFixture) {
let path = fixture.directory.join("nonseekable-shard.gguf");
let raw = 1.25f32.to_le_bytes();
fs::write(&path, one_shard("weight", 0, 1, &raw)).unwrap();
let index = gguf::tensor_index(&path).unwrap();
let tensor = &index.tensors[0];
let identity = derived_mixed::ShardedIdentity {
derived_sha256: [0; 32],
derived_bytes: 4,
shards: Vec::new(),
tensors: vec![sharded_identity_for(tensor, &raw, &[1.25])],
};
let pipe = fixture.directory.join("nonseekable-sharded.pipe");
assert!(read_sharded_weights(&identity, &index, tensor, &mut open_fifo(&pipe), 0).is_err());
fs::remove_file(pipe).unwrap();
}
#[cfg(target_os = "linux")]
fn assert_nonseekable_single_read_fails(fixture: &OverlayFixture) {
let path = fixture.directory.join("nonseekable-single.gguf");
let bytes = two_tensor_f32_gguf();
fs::write(&path, &bytes).unwrap();
let index = gguf::tensor_index(&path).unwrap();
let source = SingleSourcePrepare {
source_path: &path,
destination: Path::new("unused.kar"),
levels: 2,
kind: PreviewKind::F32,
};
let identity = single_source_identity_for(&path, &index, &bytes);
let staging_path = fixture.directory.join("nonseekable-staging");
fs::create_dir(&staging_path).unwrap();
let staging = StagingDir(staging_path);
let request = SingleTensorRequest {
source: &source,
index: &index,
identity: &identity,
staging: &staging,
tensor: &index.tensors[0],
number: 0,
};
let mut progress = SingleTensorProgress {
total_elements: 0,
maximum_relative_l2: 0.0,
prepared: Vec::new(),
};
let pipe = fixture.directory.join("nonseekable-single.pipe");
assert!(prepare_single_tensor(request, &mut open_fifo(&pipe), &mut progress).is_err());
fs::remove_file(pipe).unwrap();
}
#[cfg(target_os = "linux")]
fn open_fifo(path: &Path) -> File {
use std::os::unix::ffi::OsStrExt;
let name = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap();
// SAFETY: `name` is NUL-terminated and points to this test's private FIFO path.
assert_eq!(unsafe { libc::mkfifo(name.as_ptr(), 0o600) }, 0);
let writer_path = path.to_path_buf();
let writer = std::thread::spawn(move || {
fs::OpenOptions::new()
.write(true)
.open(writer_path)
.unwrap()
});
let reader = File::open(path).unwrap();
drop(writer.join().unwrap());
reader
}
fn assert_bad_shard_counts(index: &gguf::TensorIndex, shard: &derived_mixed::ShardIdentity) {
let wrong_count = derived_mixed::ShardIdentity {
name: shard.name.clone(),
@@ -5074,6 +5152,26 @@ mod tests {
assert_overlay_record_source_mismatch(&overlay_fixture());
}
#[test]
fn resolver_rejects_archive_bytes_changed_after_provenance() {
let fixture = overlay_fixture();
fixture.prepare().unwrap();
assert_archive_hash_mismatch(&fixture.base, &fixture.overlay, &fixture.base);
assert_archive_hash_mismatch(&fixture.base, &fixture.overlay, &fixture.overlay);
}
fn assert_archive_hash_mismatch(base: &Path, overlay: &Path, changed: &Path) {
let original = fs::read(changed).unwrap();
let mut corrupt = original.clone();
corrupt[0] ^= 1;
fs::write(changed, corrupt).unwrap();
let error = crate::kar_overlay::resolve_verified(base, overlay)
.err()
.unwrap();
assert!(error.contains("archive differs from its provenance"));
fs::write(changed, original).unwrap();
}
fn assert_overlay_record_source_mismatch(fixture: &OverlayFixture) {
fixture.prepare().unwrap();
for mismatch in 0..4 {