| 1 | use super::*; |
| 2 | use std::{fs::File, io::Read}; |
| 3 | |
| 4 | fn generate(s: &Scratch, kind: &str, size: u64) { |
| 5 | assert!( |
| 6 | Command::new("python3") |
| 7 | .arg( |
| 8 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 9 | .join("../../tests/rust-archive-worker/hardening_cases.py") |
| 10 | ) |
| 11 | .arg(&s.0) |
| 12 | .arg(kind) |
| 13 | .arg(size.to_string()) |
| 14 | .status() |
| 15 | .unwrap() |
| 16 | .success() |
| 17 | ); |
| 18 | } |
| 19 | fn start(s: &Scratch, gzip: bool, output: u64) -> Start { |
| 20 | fs::DirBuilder::new().create(s.0.join("output")).unwrap(); |
| 21 | #[cfg(unix)] |
| 22 | { |
| 23 | use std::os::unix::fs::PermissionsExt; |
| 24 | fs::set_permissions(s.0.join("output"), fs::Permissions::from_mode(0o700)).unwrap(); |
| 25 | } |
| 26 | Start { |
| 27 | action: Action::Extract { |
| 28 | archive: s.path("input"), |
| 29 | destination: s.path("output"), |
| 30 | selection: Selection::All, |
| 31 | list_id: None, |
| 32 | gzip_output_name: gzip.then(|| "payload.bin".to_owned()), |
| 33 | }, |
| 34 | limits: Limits { |
| 35 | output, |
| 36 | ..Limits::default() |
| 37 | }, |
| 38 | } |
| 39 | } |
| 40 | fn assert_zero_file(path: &std::path::Path, length: u64) { |
| 41 | let mut file = File::open(path).unwrap(); |
| 42 | assert_eq!(file.metadata().unwrap().len(), length); |
| 43 | let mut buffer = [0; 65536]; |
| 44 | let mut total = 0; |
| 45 | loop { |
| 46 | let n = file.read(&mut buffer).unwrap(); |
| 47 | if n == 0 { |
| 48 | break; |
| 49 | } |
| 50 | assert!(buffer[..n].iter().all(|b| *b == 0)); |
| 51 | total += n as u64; |
| 52 | } |
| 53 | assert_eq!(total, length); |
| 54 | } |
| 55 | #[test] |
| 56 | fn streaming_defaults_and_legacy_seven_zip_caps() { |
| 57 | assert_eq!(Limits::default().input, u64::MAX); |
| 58 | assert_eq!(Limits::default().output, 16 * 1024 * 1024 * 1024); |
| 59 | assert!( |
| 60 | !Limits { |
| 61 | output: 16 * 1024 * 1024 * 1024 + 1, |
| 62 | ..Limits::default() |
| 63 | } |
| 64 | .valid() |
| 65 | ); |
| 66 | let s = Scratch::new(); |
| 67 | let file = File::create(s.0.join("input")).unwrap(); |
| 68 | (&file).write_all(b"7z\xbc\xaf\x27\x1c").unwrap(); |
| 69 | file.set_len(1024 * 1024 * 1024).unwrap(); |
| 70 | let events = execute(start(&s, false, Limits::default().output)); |
| 71 | assert_eq!(terminal(&events).error, ErrorCode::Limit); |
| 72 | assert_eq!(terminal(&events).created_count, 0); |
| 73 | } |
| 74 | #[test] |
| 75 | fn streaming_zip64_local_headers_and_chunk_boundaries() { |
| 76 | let s = Scratch::new(); |
| 77 | generate(&s, "zip64", 9 * 1024 * 1024 + 13); |
| 78 | let events = execute(start(&s, false, Limits::default().output)); |
| 79 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 80 | assert_zero_file(&s.0.join("output/payload.bin"), 9 * 1024 * 1024 + 13); |
| 81 | } |
| 82 | #[test] |
| 83 | fn generated_zero_deflate_zip_round_trips_through_list_and_extract() { |
| 84 | let s = Scratch::new(); |
| 85 | fs::write(s.0.join("source"), vec![0; 2 * 1024 * 1024]).unwrap(); |
| 86 | let events = execute(Start { |
| 87 | action: Action::CreateZip { |
| 88 | sources: vec![Source { |
| 89 | path: s.path("source"), |
| 90 | archive_name: "payload.bin".into(), |
| 91 | }], |
| 92 | output_file: s.path("input"), |
| 93 | method: Method::Deflate, |
| 94 | encryption: Encryption::None, |
| 95 | }, |
| 96 | limits: Limits::default(), |
| 97 | }); |
| 98 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 99 | let events = execute(Start { |
| 100 | action: Action::List { |
| 101 | archive: s.path("input"), |
| 102 | }, |
| 103 | limits: Limits::default(), |
| 104 | }); |
| 105 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 106 | let list_id = events |
| 107 | .iter() |
| 108 | .find_map(|e| match e { |
| 109 | Message::ListEnd { list_id, .. } => Some(list_id.clone()), |
| 110 | _ => None, |
| 111 | }) |
| 112 | .unwrap(); |
| 113 | assert!(events.iter().any(|e| matches!(e, Message::Entry(entry) |
| 114 | if entry.size == 2 * 1024 * 1024 && entry.size > entry.compressed_size * 1000))); |
| 115 | let mut request = start(&s, false, Limits::default().output); |
| 116 | if let Action::Extract { list_id: id, .. } = &mut request.action { |
| 117 | *id = Some(list_id); |
| 118 | } |
| 119 | let events = execute(request); |
| 120 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 121 | assert_zero_file(&s.0.join("output/payload.bin"), 2 * 1024 * 1024); |
| 122 | } |
| 123 | #[test] |
| 124 | fn streaming_high_ratio_zip_extracts_below_total_limit() { |
| 125 | let s = Scratch::new(); |
| 126 | generate(&s, "bomb", 16 * 1024 * 1024); |
| 127 | let events = execute(start(&s, false, Limits::default().output)); |
| 128 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 129 | assert_zero_file(&s.0.join("output/payload.bin"), 16 * 1024 * 1024); |
| 130 | } |
| 131 | #[test] |
| 132 | fn streaming_tar_and_gzip_truncation_never_publish() { |
| 133 | for kind in ["tar", "targz", "gzip"] { |
| 134 | let s = Scratch::new(); |
| 135 | generate(&s, kind, 1024 * 1024 + 1); |
| 136 | let file = fs::OpenOptions::new() |
| 137 | .write(true) |
| 138 | .open(s.0.join("input")) |
| 139 | .unwrap(); |
| 140 | file.set_len(file.metadata().unwrap().len() - 1).unwrap(); |
| 141 | let events = execute(start(&s, kind == "gzip", Limits::default().output)); |
| 142 | assert_eq!( |
| 143 | terminal(&events).outcome, |
| 144 | Outcome::Failed, |
| 145 | "{kind}: {events:?}" |
| 146 | ); |
| 147 | assert_eq!(terminal(&events).created_count, 0); |
| 148 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn zip64_directory_and_descriptor_interoperate() { |
| 154 | for kind in ["central64", "descriptor64"] { |
| 155 | let s = Scratch::new(); |
| 156 | generate(&s, kind, 65536 + 19); |
| 157 | let events = execute(start(&s, false, Limits::default().output)); |
| 158 | assert_eq!( |
| 159 | terminal(&events).outcome, |
| 160 | Outcome::Completed, |
| 161 | "{kind}: {events:?}" |
| 162 | ); |
| 163 | assert_zero_file(&s.0.join("output/payload.bin"), 65536 + 19); |
| 164 | } |
| 165 | } |
| 166 | #[test] |
| 167 | fn zip64_offset_only_keeps_32_bit_descriptors() { |
| 168 | for kind in ["offset64-descriptor-signed", "offset64-descriptor-unsigned"] { |
| 169 | let s = Scratch::new(); |
| 170 | generate(&s, kind, 65536 + 19); |
| 171 | let events = execute(Start { |
| 172 | action: Action::List { |
| 173 | archive: s.path("input"), |
| 174 | }, |
| 175 | limits: Limits::default(), |
| 176 | }); |
| 177 | assert_eq!( |
| 178 | terminal(&events).outcome, |
| 179 | Outcome::Completed, |
| 180 | "{kind}: {events:?}" |
| 181 | ); |
| 182 | let events = execute(start(&s, false, Limits::default().output)); |
| 183 | assert_eq!( |
| 184 | terminal(&events).outcome, |
| 185 | Outcome::Completed, |
| 186 | "{kind}: {events:?}" |
| 187 | ); |
| 188 | assert_zero_file(&s.0.join("output/payload.bin"), 65536 + 19); |
| 189 | } |
| 190 | } |
| 191 | #[test] |
| 192 | fn zip64_locator_with_ordinary_eocd_fields_interoperates() { |
| 193 | let s = Scratch::new(); |
| 194 | generate(&s, "locator64", 20); |
| 195 | assert_eq!(fs::metadata(s.0.join("input")).unwrap().len(), 260); |
| 196 | let events = execute(Start { |
| 197 | action: Action::List { |
| 198 | archive: s.path("input"), |
| 199 | }, |
| 200 | limits: Limits::default(), |
| 201 | }); |
| 202 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 203 | let events = execute(start(&s, false, Limits::default().output)); |
| 204 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 205 | assert_zero_file(&s.0.join("output/payload.bin"), 20); |
| 206 | } |
| 207 | #[test] |
| 208 | fn zip64_locator_corruption_never_publishes() { |
| 209 | for (kind, error) in [ |
| 210 | ("locator64-bad-offset", ErrorCode::Integrity), |
| 211 | ("locator64-bad-size", ErrorCode::Integrity), |
| 212 | ("locator64-conflicting-count", ErrorCode::Integrity), |
| 213 | ("locator64-multidisk", ErrorCode::UnsupportedFeature), |
| 214 | ] { |
| 215 | let s = Scratch::new(); |
| 216 | generate(&s, kind, 20); |
| 217 | let events = execute(start(&s, false, Limits::default().output)); |
| 218 | assert_eq!(terminal(&events).error, error, "{kind}: {events:?}"); |
| 219 | assert_eq!(terminal(&events).publication, Publication::None); |
| 220 | assert_eq!(terminal(&events).cleanup, Cleanup::Complete); |
| 221 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 222 | } |
| 223 | } |
| 224 | #[test] |
| 225 | fn classic_directory_comment_is_not_a_zip64_locator() { |
| 226 | let s = Scratch::new(); |
| 227 | generate(&s, "classic-locator-decoy", 20); |
| 228 | let events = execute(start(&s, false, Limits::default().output)); |
| 229 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 230 | assert_zero_file(&s.0.join("output/payload.bin"), 20); |
| 231 | } |
| 232 | #[test] |
| 233 | fn untrusted_zip64_u64_sizes_and_offsets_never_allocate_or_publish() { |
| 234 | for (kind, error) in [ |
| 235 | ("central64-size-overflow", ErrorCode::Limit), |
| 236 | ("central64-offset-overflow", ErrorCode::Integrity), |
| 237 | ] { |
| 238 | let s = Scratch::new(); |
| 239 | generate(&s, kind, 1024); |
| 240 | let events = execute(start(&s, false, Limits::default().output)); |
| 241 | assert_eq!(terminal(&events).error, error, "{kind}: {events:?}"); |
| 242 | assert_eq!(terminal(&events).publication, Publication::None); |
| 243 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 244 | } |
| 245 | } |
| 246 | #[test] |
| 247 | fn gzip_high_ratio_members_extract_below_total_limit() { |
| 248 | let s = Scratch::new(); |
| 249 | generate(&s, "gzip-bomb-joined", 16 * 1024 * 1024); |
| 250 | let events = execute(start(&s, true, Limits::default().output)); |
| 251 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 252 | assert_zero_file(&s.0.join("output/payload.bin"), 32 * 1024 * 1024); |
| 253 | } |
| 254 | #[test] |
| 255 | fn streamed_aes_counter_survives_many_read_chunks() { |
| 256 | let s = Scratch::new(); |
| 257 | generate(&s, "store", 2 * 1024 * 1024 + 17); |
| 258 | fs::rename(s.0.join("input"), s.0.join("source")).unwrap(); |
| 259 | let events = interactive( |
| 260 | Start { |
| 261 | action: Action::CreateZip { |
| 262 | sources: vec![Source { |
| 263 | path: s.path("source"), |
| 264 | archive_name: "payload.bin".to_owned(), |
| 265 | }], |
| 266 | output_file: s.path("input"), |
| 267 | method: Method::Store, |
| 268 | encryption: Encryption::Aes256, |
| 269 | }, |
| 270 | limits: Limits::default(), |
| 271 | }, |
| 272 | |id, _, _| password_reply(id, TEST_PASSWORD), |
| 273 | ); |
| 274 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 275 | let events = interactive(start(&s, false, Limits::default().output), |id, _, _| { |
| 276 | password_reply(id, TEST_PASSWORD) |
| 277 | }); |
| 278 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 279 | let mut source = File::open(s.0.join("source")).unwrap(); |
| 280 | let mut output = File::open(s.0.join("output/payload.bin")).unwrap(); |
| 281 | let mut a = [0; 65536]; |
| 282 | let mut b = [0; 65536]; |
| 283 | loop { |
| 284 | let n = source.read(&mut a).unwrap(); |
| 285 | output.read_exact(&mut b[..n]).unwrap(); |
| 286 | assert_eq!(a[..n], b[..n]); |
| 287 | if n == 0 { |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | assert_eq!( |
| 292 | source.metadata().unwrap().len(), |
| 293 | output.metadata().unwrap().len() |
| 294 | ); |
| 295 | } |
| 296 | |
| 297 | #[cfg(target_os = "linux")] |
| 298 | mod linux { |
| 299 | use super::*; |
| 300 | use std::{ |
| 301 | os::raw::c_long, |
| 302 | time::{Duration, Instant}, |
| 303 | }; |
| 304 | #[repr(C)] |
| 305 | #[derive(Default)] |
| 306 | struct Usage { |
| 307 | user: [c_long; 2], |
| 308 | system: [c_long; 2], |
| 309 | max_rss: c_long, |
| 310 | rest: [c_long; 13], |
| 311 | } |
| 312 | unsafe extern "C" { |
| 313 | fn wait4(pid: i32, status: *mut i32, options: i32, usage: *mut Usage) -> i32; |
| 314 | fn prctl(option: i32, ...) -> i32; |
| 315 | fn kill(pid: i32, signal: i32) -> i32; |
| 316 | } |
| 317 | fn command() -> Command { |
| 318 | let worker = std::env::var_os("ZIPELO_BASELINE_WORKER") |
| 319 | .map(PathBuf::from) |
| 320 | .unwrap_or_else(|| PathBuf::from(env!("CARGO_BIN_EXE_zipelo-archive-worker"))); |
| 321 | Command::new(worker) |
| 322 | } |
| 323 | fn staging(s: &Scratch) -> PathBuf { |
| 324 | use std::os::unix::fs::DirBuilderExt; |
| 325 | let path = s.0.join("staging"); |
| 326 | fs::DirBuilder::new().mode(0o700).create(&path).unwrap(); |
| 327 | path |
| 328 | } |
| 329 | fn spawn(s: &Scratch, mut start: Start, mode: Option<&str>) -> std::process::Child { |
| 330 | if std::env::var_os("ZIPELO_BASELINE_WORKER").is_some() { |
| 331 | start.limits = Limits { |
| 332 | input: 64 * 1024 * 1024, |
| 333 | output: 128 * 1024 * 1024, |
| 334 | entries: 10000, |
| 335 | }; |
| 336 | } |
| 337 | let mut cmd = command(); |
| 338 | cmd.env("TMPDIR", staging(s)) |
| 339 | .stdin(Stdio::piped()) |
| 340 | .stdout(File::create(s.0.join("events")).unwrap()) |
| 341 | .stderr(File::create(s.0.join("stderr")).unwrap()); |
| 342 | if let Some(mode) = mode { |
| 343 | cmd.env("LD_PRELOAD", shim(s)) |
| 344 | .env("ZIPELO_TEST_FAULT", mode) |
| 345 | .env("ZIPELO_TEST_MARKER", s.0.join("paused")) |
| 346 | .env("ZIPELO_TEST_RELEASE", s.0.join("release")); |
| 347 | } |
| 348 | let mut child = cmd.spawn().unwrap(); |
| 349 | Writer::new(child.stdin.take().unwrap(), 17) |
| 350 | .send(&Message::Start(start)) |
| 351 | .unwrap(); |
| 352 | child |
| 353 | } |
| 354 | fn wait(pid: u32) -> (i32, u64) { |
| 355 | let deadline = Instant::now() + Duration::from_secs(320); |
| 356 | loop { |
| 357 | let mut status = 0; |
| 358 | let mut usage = Usage::default(); |
| 359 | let r = unsafe { wait4(pid as i32, &mut status, 1, &mut usage) }; |
| 360 | if r == pid as i32 { |
| 361 | return (status, usage.max_rss as u64 * 1024); |
| 362 | } |
| 363 | assert!( |
| 364 | r >= 0 || std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted |
| 365 | ); |
| 366 | if Instant::now() > deadline { |
| 367 | unsafe { |
| 368 | kill(pid as i32, 9); |
| 369 | wait4(pid as i32, &mut status, 0, &mut usage); |
| 370 | } |
| 371 | panic!("worker timeout"); |
| 372 | } |
| 373 | std::thread::sleep(Duration::from_millis(10)); |
| 374 | } |
| 375 | } |
| 376 | fn events(s: &Scratch) -> Vec<Message> { |
| 377 | assert!(fs::read(s.0.join("stderr")).unwrap().is_empty()); |
| 378 | let mut reader = Reader::new(File::open(s.0.join("events")).unwrap()); |
| 379 | let mut result = Vec::new(); |
| 380 | while let Some((_, _, message)) = reader.read().unwrap() { |
| 381 | result.push(message); |
| 382 | } |
| 383 | result |
| 384 | } |
| 385 | fn shim(s: &Scratch) -> PathBuf { |
| 386 | let path = s.0.join("io_fault.so"); |
| 387 | assert!( |
| 388 | Command::new("cc") |
| 389 | .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) |
| 390 | .arg( |
| 391 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 392 | .join("../../tests/rust-archive-worker/io_fault.c") |
| 393 | ) |
| 394 | .arg("-ldl") |
| 395 | .arg("-o") |
| 396 | .arg(&path) |
| 397 | .status() |
| 398 | .unwrap() |
| 399 | .success() |
| 400 | ); |
| 401 | path |
| 402 | } |
| 403 | #[test] |
| 404 | fn gigabyte_formats_stay_below_64_mib_peak_rss() { |
| 405 | let mut measurements = Vec::new(); |
| 406 | for kind in ["store", "deflate", "tar", "gzip", "targz"] { |
| 407 | let s = Scratch::new(); |
| 408 | generate(&s, kind, 1024 * 1024 * 1024); |
| 409 | let request = start(&s, kind == "gzip", Limits::default().output); |
| 410 | let child = spawn(&s, request, None); |
| 411 | let (status, rss) = wait(child.id()); |
| 412 | assert_eq!(status, 0, "{kind}"); |
| 413 | let events = events(&s); |
| 414 | assert_eq!( |
| 415 | terminal(&events).outcome, |
| 416 | Outcome::Completed, |
| 417 | "{kind}: {events:?}" |
| 418 | ); |
| 419 | assert_eq!(terminal(&events).publication, Publication::Complete); |
| 420 | assert!(rss <= 64 * 1024 * 1024, "{kind}: RSS {rss}"); |
| 421 | assert_zero_file(&s.0.join("output/payload.bin"), 1024 * 1024 * 1024); |
| 422 | measurements.push(format!("{kind}: {rss}")); |
| 423 | } |
| 424 | println!( |
| 425 | "1 GiB extraction peak RSS bytes: {}", |
| 426 | measurements.join(", ") |
| 427 | ); |
| 428 | fs::write( |
| 429 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/ci/streaming-rss.log"), |
| 430 | measurements.join("\n") + "\n", |
| 431 | ) |
| 432 | .unwrap(); |
| 433 | } |
| 434 | #[test] |
| 435 | fn enospc_at_create_or_after_partial_write_cleans_output() { |
| 436 | for mode in [ |
| 437 | "create-full", |
| 438 | "full", |
| 439 | "stage-full", |
| 440 | "temp-full", |
| 441 | "sync-full", |
| 442 | ] { |
| 443 | let s = Scratch::new(); |
| 444 | generate(&s, "store", 1024 * 1024); |
| 445 | fs::write(s.0.join("preserve"), b"original").unwrap(); |
| 446 | let request = start(&s, false, Limits::default().output); |
| 447 | let child = spawn(&s, request, Some(mode)); |
| 448 | assert_eq!(wait(child.id()).0, 0); |
| 449 | let events = events(&s); |
| 450 | let t = terminal(&events); |
| 451 | assert_eq!(t.error, ErrorCode::DiskFull, "{mode}: {events:?}"); |
| 452 | assert_eq!( |
| 453 | t.detail, |
| 454 | "디스크 여유 공간이 부족합니다. 공간을 확보한 뒤 다시 시도하세요" |
| 455 | ); |
| 456 | assert_eq!(t.publication, Publication::None); |
| 457 | assert_eq!(t.retained_partial_count, 0); |
| 458 | assert_eq!(t.cleanup, Cleanup::Complete); |
| 459 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 460 | assert_eq!(fs::read(s.0.join("preserve")).unwrap(), b"original"); |
| 461 | } |
| 462 | } |
| 463 | #[test] |
| 464 | fn disk_space_preflight_rejects_without_outputs_and_closes_staging() { |
| 465 | for mode in [ |
| 466 | "space-stage", |
| 467 | "space-destination", |
| 468 | "space-combined", |
| 469 | "space-error", |
| 470 | ] { |
| 471 | let s = Scratch::new(); |
| 472 | generate(&s, "store", 1024 * 1024); |
| 473 | let request = start(&s, false, Limits::default().output); |
| 474 | let child = spawn(&s, request, Some(mode)); |
| 475 | assert_eq!(wait(child.id()).0, 0); |
| 476 | let events = events(&s); |
| 477 | let t = terminal(&events); |
| 478 | let expected = if mode == "space-error" { |
| 479 | ErrorCode::Io |
| 480 | } else { |
| 481 | ErrorCode::DiskFull |
| 482 | }; |
| 483 | assert_eq!(t.error, expected, "{mode}: {events:?}"); |
| 484 | assert_eq!(t.publication, Publication::None); |
| 485 | assert_eq!(t.cleanup, Cleanup::Complete); |
| 486 | assert_eq!(t.created_count, 0); |
| 487 | assert!(t.detail.contains("디스크 여유 공간")); |
| 488 | assert!(s.0.join("paused").exists()); |
| 489 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 490 | assert_eq!(fs::read_dir(s.0.join("staging")).unwrap().count(), 0); |
| 491 | } |
| 492 | } |
| 493 | #[test] |
| 494 | fn disk_full_during_directory_sync_retains_reported_outputs() { |
| 495 | for nested in [false, true] { |
| 496 | let s = Scratch::new(); |
| 497 | fs::write(s.0.join("source"), vec![0; 1024]).unwrap(); |
| 498 | let name = if nested { |
| 499 | "folder/payload.bin" |
| 500 | } else { |
| 501 | "payload.bin" |
| 502 | }; |
| 503 | let created = execute(Start { |
| 504 | action: Action::CreateZip { |
| 505 | sources: vec![Source { |
| 506 | path: s.path("source"), |
| 507 | archive_name: name.into(), |
| 508 | }], |
| 509 | output_file: s.path("input"), |
| 510 | method: Method::Store, |
| 511 | encryption: Encryption::None, |
| 512 | }, |
| 513 | limits: Limits::default(), |
| 514 | }); |
| 515 | assert_eq!(terminal(&created).outcome, Outcome::Completed); |
| 516 | let request = start(&s, false, Limits::default().output); |
| 517 | let child = spawn(&s, request, Some("dir-sync-full")); |
| 518 | assert_eq!(wait(child.id()).0, 0); |
| 519 | let events = events(&s); |
| 520 | let t = terminal(&events); |
| 521 | assert_eq!(t.error, ErrorCode::DiskFull, "{events:?}"); |
| 522 | assert_eq!(t.publication, Publication::Partial); |
| 523 | assert_eq!(t.created_count, 1); |
| 524 | assert_eq!(t.durability, Durability::Failed); |
| 525 | assert_eq!(t.cleanup, Cleanup::Complete); |
| 526 | assert_eq!(t.retained_partial_count, 0); |
| 527 | assert_eq!(fs::read_dir(s.0.join("staging")).unwrap().count(), 0); |
| 528 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 1); |
| 529 | if nested { |
| 530 | assert_eq!(fs::read_dir(s.0.join("output/folder")).unwrap().count(), 0); |
| 531 | } else { |
| 532 | assert_zero_file(&s.0.join("output/payload.bin"), 1024); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | #[test] |
| 537 | fn permission_and_generic_write_errors_remain_io() { |
| 538 | for mode in ["create-denied", "write-io"] { |
| 539 | let s = Scratch::new(); |
| 540 | generate(&s, "store", 1024 * 1024); |
| 541 | let request = start(&s, false, Limits::default().output); |
| 542 | let child = spawn(&s, request, Some(mode)); |
| 543 | assert_eq!(wait(child.id()).0, 0); |
| 544 | let events = events(&s); |
| 545 | let t = terminal(&events); |
| 546 | assert_eq!(t.error, ErrorCode::Io, "{mode}: {events:?}"); |
| 547 | assert_eq!(t.publication, Publication::None); |
| 548 | assert_eq!(t.retained_partial_count, 0); |
| 549 | assert_eq!(t.cleanup, Cleanup::Complete); |
| 550 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 551 | assert_eq!(fs::read_dir(s.0.join("staging")).unwrap().count(), 0); |
| 552 | } |
| 553 | } |
| 554 | #[test] |
| 555 | fn anonymous_tempfile_fallback_unlinks_before_use() { |
| 556 | let s = Scratch::new(); |
| 557 | generate(&s, "store", 1024 * 1024); |
| 558 | let request = start(&s, false, Limits::default().output); |
| 559 | let child = spawn(&s, request, Some("fallback")); |
| 560 | assert_eq!(wait(child.id()).0, 0); |
| 561 | let events = events(&s); |
| 562 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 563 | assert!(s.0.join("paused").exists()); |
| 564 | assert_eq!(fs::read_dir(s.0.join("staging")).unwrap().count(), 0); |
| 565 | assert_zero_file(&s.0.join("output/payload.bin"), 1024 * 1024); |
| 566 | } |
| 567 | struct Subreaper(i32); |
| 568 | impl Subreaper { |
| 569 | fn new() -> Self { |
| 570 | let mut old = 0; |
| 571 | assert_eq!(unsafe { prctl(37, &mut old as *mut i32, 0, 0, 0) }, 0); |
| 572 | assert_eq!(unsafe { prctl(36, 1, 0, 0, 0) }, 0); |
| 573 | Self(old) |
| 574 | } |
| 575 | } |
| 576 | impl Drop for Subreaper { |
| 577 | fn drop(&mut self) { |
| 578 | assert_eq!(unsafe { prctl(36, self.0, 0, 0, 0) }, 0); |
| 579 | } |
| 580 | } |
| 581 | #[test] |
| 582 | fn orphan_app_helper() { |
| 583 | let Some(path) = std::env::var_os("ZIPELO_TEST_APP_HELPER") else { |
| 584 | return; |
| 585 | }; |
| 586 | let s = std::mem::ManuallyDrop::new(Scratch(PathBuf::from(path))); |
| 587 | let mut request = Start { |
| 588 | action: Action::Extract { |
| 589 | archive: s.path("input"), |
| 590 | destination: s.path("output"), |
| 591 | selection: Selection::All, |
| 592 | list_id: None, |
| 593 | gzip_output_name: None, |
| 594 | }, |
| 595 | limits: Limits::default(), |
| 596 | }; |
| 597 | if std::env::var_os("ZIPELO_BASELINE_WORKER").is_some() { |
| 598 | request.limits = Limits { |
| 599 | input: 64 * 1024 * 1024, |
| 600 | output: 128 * 1024 * 1024, |
| 601 | entries: 10000, |
| 602 | }; |
| 603 | } |
| 604 | let mut cmd = command(); |
| 605 | cmd.env("TMPDIR", staging(&s)) |
| 606 | .stdin(Stdio::piped()) |
| 607 | .stdout(File::create(s.0.join("events")).unwrap()) |
| 608 | .stderr(File::create(s.0.join("stderr")).unwrap()) |
| 609 | .env("LD_PRELOAD", s.0.join("io_fault.so")) |
| 610 | .env("ZIPELO_TEST_FAULT", "pause") |
| 611 | .env("ZIPELO_TEST_MARKER", s.0.join("paused")) |
| 612 | .env("ZIPELO_TEST_RELEASE", s.0.join("release")); |
| 613 | let mut child = cmd.spawn().unwrap(); |
| 614 | fs::write(s.0.join("worker.pid"), child.id().to_string()).unwrap(); |
| 615 | let mut writer = Writer::new(child.stdin.take().unwrap(), 17); |
| 616 | writer.send(&Message::Start(request)).unwrap(); |
| 617 | loop { |
| 618 | std::thread::sleep(Duration::from_millis(20)); |
| 619 | } |
| 620 | } |
| 621 | #[test] |
| 622 | fn app_death_stops_worker_and_removes_partial_temp() { |
| 623 | let _guard = Subreaper::new(); |
| 624 | let s = Scratch::new(); |
| 625 | generate(&s, "store", 1024 * 1024); |
| 626 | let _ = start(&s, false, Limits::default().output); |
| 627 | shim(&s); |
| 628 | let mut helper = Command::new(std::env::current_exe().unwrap()) |
| 629 | .args([ |
| 630 | "--exact", |
| 631 | "hardening::linux::orphan_app_helper", |
| 632 | "--nocapture", |
| 633 | ]) |
| 634 | .env("ZIPELO_TEST_APP_HELPER", &s.0) |
| 635 | .stdout(Stdio::null()) |
| 636 | .stderr(Stdio::null()) |
| 637 | .spawn() |
| 638 | .unwrap(); |
| 639 | let deadline = Instant::now() + Duration::from_secs(20); |
| 640 | while !s.0.join("paused").exists() { |
| 641 | if Instant::now() > deadline { |
| 642 | helper.kill().unwrap(); |
| 643 | helper.wait().unwrap(); |
| 644 | if let Ok(pid) = fs::read_to_string(s.0.join("worker.pid")) { |
| 645 | unsafe { |
| 646 | kill(pid.parse().unwrap(), 9); |
| 647 | } |
| 648 | wait(pid.trim().parse().unwrap()); |
| 649 | } |
| 650 | panic!("worker did not reach publication"); |
| 651 | } |
| 652 | std::thread::sleep(Duration::from_millis(10)); |
| 653 | } |
| 654 | let pid = fs::read_to_string(s.0.join("worker.pid")) |
| 655 | .unwrap() |
| 656 | .parse::<u32>() |
| 657 | .unwrap(); |
| 658 | helper.kill().unwrap(); |
| 659 | helper.wait().unwrap(); |
| 660 | fs::write(s.0.join("release"), b"").unwrap(); |
| 661 | assert_eq!(wait(pid).0, 0); |
| 662 | let events = events(&s); |
| 663 | assert_eq!(terminal(&events).error, ErrorCode::Io, "{events:?}"); |
| 664 | assert_eq!(terminal(&events).publication, Publication::None); |
| 665 | assert_eq!(terminal(&events).cleanup, Cleanup::Complete); |
| 666 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 667 | } |
| 668 | } |