diff --git a/control-plane/src/kar_overlay.rs b/control-plane/src/kar_overlay.rs index efe40bf1..44a3ef62 100644 --- a/control-plane/src/kar_overlay.rs +++ b/control-plane/src/kar_overlay.rs @@ -1362,6 +1362,16 @@ mod tests { } } + #[test] + fn resolver_rejects_missing_archive_parent_before_opening_sidecars() { + let directory = TestDir::new("missing-parent"); + let missing = directory.0.join("missing"); + assert!(resolve_verified(&missing.join("base.kar"), &missing.join("overlay.kar")).is_err()); + assert!( + resolve_verified(&directory.0.join("base.kar"), &missing.join("overlay.kar")).is_err() + ); + } + fn candidate_value( schema: &str, levels: u8, diff --git a/control-plane/src/kar_writer.rs b/control-plane/src/kar_writer.rs index 9d0fe1e8..dd1a8648 100644 --- a/control-plane/src/kar_writer.rs +++ b/control-plane/src/kar_writer.rs @@ -2420,6 +2420,20 @@ mod tests { fs::remove_dir_all(root).unwrap(); } + #[cfg(unix)] + #[test] + fn sticky_private_archive_parent_allows_shared_directory_mode() { + let root = test_dir("sticky-private-parent"); + let parent = fs::canonicalize(&root).unwrap(); + fs::set_permissions(&root, fs::Permissions::from_mode(0o770)).unwrap(); + assert!(validate_private_archive_parent(&parent).is_err()); + fs::set_permissions(&root, fs::Permissions::from_mode(0o1770)).unwrap(); + assert_eq!(fs::metadata(&root).unwrap().mode() & 0o1777, 0o1770); + validate_private_archive_parent(&parent).unwrap(); + fs::set_permissions(&root, fs::Permissions::from_mode(0o700)).unwrap(); + fs::remove_dir_all(root).unwrap(); + } + #[cfg(unix)] #[test] fn archive_temp_metadata_matching_fails_closed_on_metadata_errors() { diff --git a/control-plane/src/prepare_preview.rs b/control-plane/src/prepare_preview.rs index 4ca106d8..2a5d1407 100644 --- a/control-plane/src/prepare_preview.rs +++ b/control-plane/src/prepare_preview.rs @@ -2956,6 +2956,145 @@ mod tests { assert_changed_size_source_rejected(&request, &index, &identity); } + #[test] + fn single_tensor_read_rejects_source_truncated_after_indexing() { + let fixture = overlay_fixture(); + let path = fixture.directory.join("truncated-after-index.gguf"); + let original = two_tensor_f32_gguf(); + fs::write(&path, &original).unwrap(); + let index = gguf::tensor_index(&path).unwrap(); + let identity = single_source_identity_for(&path, &index, &original); + let request = SingleSourcePrepare { + source_path: &path, + destination: Path::new("unused.kar"), + levels: 2, + kind: PreviewKind::F32, + }; + let staging_path = fixture.directory.join("staging"); + fs::create_dir(&staging_path).unwrap(); + assert_truncated_tensor_read_fails(&path, &request, &index, &identity, &staging_path); + } + + #[test] + fn prepared_shard_verification_detects_size_and_digest_changes() { + let fixture = overlay_fixture(); + let path = fixture.directory.join("prepared-check.gguf"); + fs::write(&path, b"verified source").unwrap(); + let source = File::open(&path).unwrap(); + let mut shard = derived_mixed::ShardIdentity { + name: "prepared-check.gguf".into(), + gguf_sha256: hash_file(&path).unwrap(), + gguf_bytes: fs::metadata(&path).unwrap().len(), + tensor_count: 1, + }; + shard.gguf_bytes += 1; + assert!(verify_prepared_shard(&path, &shard, &source).is_err()); + shard.gguf_bytes -= 1; + fs::write(&path, b"changed source!").unwrap(); + assert!(verify_prepared_shard(&path, &shard, &source).is_err()); + } + + #[test] + fn shard_index_rejects_architecture_count_and_size_drift() { + let fixture = overlay_fixture(); + let path = fixture.directory.join("index-check.gguf"); + fs::write(&path, two_tensor_f32_gguf()).unwrap(); + let mut index = gguf::tensor_index(&path).unwrap(); + let shard = derived_mixed::ShardIdentity { + name: "index-check.gguf".into(), + gguf_sha256: hash_file(&path).unwrap(), + gguf_bytes: index.summary.file_size, + tensor_count: index.tensors.len(), + }; + assert!(check_shard_index(&index, &shard, &mut None).is_ok()); + assert!(check_shard_index(&index, &shard, &mut Some("other".into())).is_err()); + assert_bad_shard_counts(&index, &shard); + index.summary.architecture = None; + assert!(check_shard_index(&index, &shard, &mut None).is_ok()); + } + + #[test] + fn sharded_tensor_read_rejects_payload_truncated_after_indexing() { + let fixture = overlay_fixture(); + let path = fixture.directory.join("truncated-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])], + }; + assert_sharded_payload_read_fails(&path, &index, tensor, &identity); + } + + fn assert_sharded_payload_read_fails( + path: &Path, + index: &gguf::TensorIndex, + tensor: &gguf::TensorInfo, + identity: &derived_mixed::ShardedIdentity, + ) { + fs::write( + path, + &fs::read(path).unwrap()[..index.summary.data_start as usize], + ) + .unwrap(); + assert!( + read_sharded_weights(identity, index, tensor, &mut File::open(path).unwrap(), 0) + .is_err() + ); + } + + fn assert_bad_shard_counts(index: &gguf::TensorIndex, shard: &derived_mixed::ShardIdentity) { + let wrong_count = derived_mixed::ShardIdentity { + name: shard.name.clone(), + gguf_sha256: shard.gguf_sha256, + gguf_bytes: shard.gguf_bytes, + tensor_count: shard.tensor_count + 1, + }; + assert!(check_shard_index(index, &wrong_count, &mut None).is_err()); + let wrong_size = derived_mixed::ShardIdentity { + name: shard.name.clone(), + gguf_sha256: shard.gguf_sha256, + gguf_bytes: shard.gguf_bytes + 1, + tensor_count: shard.tensor_count, + }; + assert!(check_shard_index(index, &wrong_size, &mut None).is_err()); + } + + fn assert_truncated_tensor_read_fails( + path: &Path, + source: &SingleSourcePrepare<'_>, + index: &gguf::TensorIndex, + identity: &SingleSourceIdentity, + staging_path: &Path, + ) { + fs::write( + path, + &fs::read(path).unwrap()[..index.summary.data_start as usize], + ) + .unwrap(); + let mut source_file = File::open(path).unwrap(); + let mut progress = SingleTensorProgress { + total_elements: 0, + maximum_relative_l2: 0.0, + prepared: Vec::new(), + }; + let staging = StagingDir(staging_path.to_path_buf()); + let request = SingleTensorRequest { + source, + index, + identity, + staging: &staging, + tensor: &index.tensors[0], + number: 0, + }; + assert!(prepare_single_tensor(request, &mut source_file, &mut progress).is_err()); + assert!(progress.prepared.is_empty()); + } + fn single_source_identity_for( path: &Path, index: &gguf::TensorIndex, @@ -4832,6 +4971,23 @@ mod tests { reject_corrupt_overlay_provenance(&fixture, &result); } + #[test] + fn resolver_rejects_malformed_overlay_sidecar() { + let fixture = overlay_fixture(); + fixture.prepare().unwrap(); + fs::write(fixture.overlay_sidecar(), b"{").unwrap(); + assert!(crate::kar_overlay::resolve_verified(&fixture.base, &fixture.overlay).is_err()); + } + + #[test] + fn resolver_rejects_malformed_base_sidecar() { + let fixture = overlay_fixture(); + fixture.prepare().unwrap(); + let sidecar = fixture.base.with_file_name("base.kar.provenance.json"); + fs::write(sidecar, b"{").unwrap(); + assert!(crate::kar_overlay::resolve_verified(&fixture.base, &fixture.overlay).is_err()); + } + #[test] fn overlay_publication_recovers_each_process_death_boundary() { for stage in 1..=4 { @@ -5137,7 +5293,8 @@ mod tests { } fn bad_parent_coverage(value: &mut serde_json::Value) { - value["partitioned_tensors"][0]["parent_dimensions"] = serde_json::json!([511]); + value["partitioned_tensors"][0]["parts"][1]["dimensions"] = serde_json::json!([255]); + value["partitioned_tensors"][0]["parts"][1]["derived_bytes"] = serde_json::json!(255 * 4); } fn base_part_collision(value: &mut serde_json::Value) { @@ -5195,7 +5352,7 @@ mod tests { fn duplicate_overlay_record(value: &mut serde_json::Value) { let first = value["records"][0].clone(); - value["records"][1] = first; + value["records"].as_array_mut().unwrap().push(first); } fn bad_overlay_record(value: &mut serde_json::Value) {