From 11f42fa03a58bb831be5b69b54e725aa39b0bd75 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Wed, 7 Oct 2026 02:38:44 +0200 Subject: [PATCH] test: cover archive and seek error paths --- control-plane/src/kar_overlay.rs | 20 +++++- control-plane/src/prepare_preview.rs | 98 ++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/control-plane/src/kar_overlay.rs b/control-plane/src/kar_overlay.rs index d1d66809..a9395b4c 100644 --- a/control-plane/src/kar_overlay.rs +++ b/control-plane/src/kar_overlay.rs @@ -519,11 +519,12 @@ pub fn resolve_verified(base_archive: &Path, overlay_archive: &Path) -> Result(&json).is_err(), + "{number}" + ); + } + } + struct TestDir(PathBuf); impl TestDir { diff --git a/control-plane/src/prepare_preview.rs b/control-plane/src/prepare_preview.rs index be1b5fb2..c9c1e4b6 100644 --- a/control-plane/src/prepare_preview.rs +++ b/control-plane/src/prepare_preview.rs @@ -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 {