| 1 | use std::{ |
| 2 | fs, |
| 3 | io::Write, |
| 4 | path::PathBuf, |
| 5 | process::{Command, Stdio}, |
| 6 | sync::atomic::{AtomicU64, Ordering}, |
| 7 | }; |
| 8 | use zipelo_archive_worker::{ |
| 9 | archive, |
| 10 | protocol::*, |
| 11 | wire::{Reader, Writer}, |
| 12 | }; |
| 13 | static NEXT: AtomicU64 = AtomicU64::new(0); |
| 14 | struct Scratch(PathBuf); |
| 15 | impl Scratch { |
| 16 | fn new() -> Self { |
| 17 | let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 18 | .parent() |
| 19 | .unwrap() |
| 20 | .parent() |
| 21 | .unwrap() |
| 22 | .join("tests/rust-archive-worker/runs") |
| 23 | .join(format!( |
| 24 | "{}-{}", |
| 25 | std::process::id(), |
| 26 | NEXT.fetch_add(1, Ordering::Relaxed) |
| 27 | )); |
| 28 | fs::create_dir_all(path.parent().unwrap()).unwrap(); |
| 29 | let builder = fs::DirBuilder::new(); |
| 30 | #[cfg(unix)] |
| 31 | let mut builder = builder; |
| 32 | #[cfg(unix)] |
| 33 | { |
| 34 | use std::os::unix::fs::DirBuilderExt; |
| 35 | builder.mode(0o700); |
| 36 | } |
| 37 | builder.create(&path).unwrap(); |
| 38 | Self(path) |
| 39 | } |
| 40 | fn path(&self, name: &str) -> NativePath { |
| 41 | NativePath::from_path(&self.0.join(name)) |
| 42 | } |
| 43 | } |
| 44 | impl Drop for Scratch { |
| 45 | fn drop(&mut self) { |
| 46 | fs::remove_dir_all(&self.0).unwrap(); |
| 47 | } |
| 48 | } |
| 49 | fn execute(start: Start) -> Vec<Message> { |
| 50 | let mut data = Vec::new(); |
| 51 | Writer::new(&mut data, 17) |
| 52 | .send(&Message::Start(start)) |
| 53 | .unwrap(); |
| 54 | let mut command = Command::new(env!("CARGO_BIN_EXE_zipelo-archive-worker")); |
| 55 | command |
| 56 | .stdin(Stdio::piped()) |
| 57 | .stdout(Stdio::piped()) |
| 58 | .stderr(Stdio::piped()); |
| 59 | #[cfg(target_os = "linux")] |
| 60 | { |
| 61 | use std::os::unix::process::CommandExt; |
| 62 | #[repr(C)] |
| 63 | struct Limit { |
| 64 | soft: usize, |
| 65 | hard: usize, |
| 66 | } |
| 67 | unsafe extern "C" { |
| 68 | fn setrlimit(resource: i32, limit: *const Limit) -> i32; |
| 69 | } |
| 70 | // 자식 프로세스의 주소 공간만 512 MiB로 제한함. |
| 71 | unsafe { |
| 72 | command.pre_exec(|| { |
| 73 | let limit = Limit { |
| 74 | soft: 512 * 1024 * 1024, |
| 75 | hard: 512 * 1024 * 1024, |
| 76 | }; |
| 77 | if setrlimit(9, &limit) == 0 { |
| 78 | Ok(()) |
| 79 | } else { |
| 80 | Err(std::io::Error::last_os_error()) |
| 81 | } |
| 82 | }); |
| 83 | } |
| 84 | } |
| 85 | let mut child = command.spawn().unwrap(); |
| 86 | child.stdin.take().unwrap().write_all(&data).unwrap(); |
| 87 | let output = child.wait_with_output().unwrap(); |
| 88 | assert!(output.status.success(), "{:?}", output.stderr); |
| 89 | assert!(output.stderr.is_empty()); |
| 90 | let mut reader = Reader::new(output.stdout.as_slice()); |
| 91 | let mut events = Vec::new(); |
| 92 | while let Some((job, _, event)) = reader.read().unwrap() { |
| 93 | assert_eq!(job, 17); |
| 94 | events.push(event); |
| 95 | } |
| 96 | assert!(matches!(events.last(), Some(Message::Terminal(_)))); |
| 97 | events |
| 98 | } |
| 99 | fn terminal(events: &[Message]) -> &Terminal { |
| 100 | match events.last().unwrap() { |
| 101 | Message::Terminal(t) => t, |
| 102 | _ => panic!("missing terminal"), |
| 103 | } |
| 104 | } |
| 105 | fn extract( |
| 106 | s: &Scratch, |
| 107 | bytes: &[u8], |
| 108 | gzip: Option<&str>, |
| 109 | selection: Selection, |
| 110 | list_id: Option<ListId>, |
| 111 | limits: Limits, |
| 112 | ) -> Vec<Message> { |
| 113 | fs::write(s.0.join("input"), bytes).unwrap(); |
| 114 | if !s.0.join("output").exists() { |
| 115 | let d = fs::DirBuilder::new(); |
| 116 | #[cfg(unix)] |
| 117 | let mut d = d; |
| 118 | #[cfg(unix)] |
| 119 | { |
| 120 | use std::os::unix::fs::DirBuilderExt; |
| 121 | d.mode(0o700); |
| 122 | } |
| 123 | d.create(s.0.join("output")).unwrap(); |
| 124 | } |
| 125 | execute(Start { |
| 126 | action: Action::Extract { |
| 127 | archive: s.path("input"), |
| 128 | destination: s.path("output"), |
| 129 | selection, |
| 130 | list_id, |
| 131 | gzip_output_name: gzip.map(str::to_owned), |
| 132 | }, |
| 133 | limits, |
| 134 | }) |
| 135 | } |
| 136 | const PAYLOAD: &[u8] = b"Zipelo worker interoperability\n"; |
| 137 | fn basic(bytes: &[u8], gzip: Option<&str>, output: &str) { |
| 138 | let s = Scratch::new(); |
| 139 | let events = extract(&s, bytes, gzip, Selection::All, None, Limits::default()); |
| 140 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 141 | assert_eq!(terminal(&events).publication, Publication::Complete); |
| 142 | assert_eq!(fs::read(s.0.join("output").join(output)).unwrap(), PAYLOAD); |
| 143 | } |
| 144 | #[test] |
| 145 | fn stored_zip() { |
| 146 | basic( |
| 147 | include_bytes!("fixtures/store.zip"), |
| 148 | None, |
| 149 | "folder/hello.txt", |
| 150 | ); |
| 151 | } |
| 152 | #[test] |
| 153 | fn deflated_zip() { |
| 154 | basic( |
| 155 | include_bytes!("fixtures/deflate.zip"), |
| 156 | None, |
| 157 | "folder/hello.txt", |
| 158 | ); |
| 159 | } |
| 160 | #[test] |
| 161 | fn tar() { |
| 162 | basic( |
| 163 | include_bytes!("fixtures/sample.tar"), |
| 164 | None, |
| 165 | "folder/hello.txt", |
| 166 | ); |
| 167 | } |
| 168 | #[test] |
| 169 | fn tar_gzip() { |
| 170 | basic( |
| 171 | include_bytes!("fixtures/sample.tar.gz"), |
| 172 | None, |
| 173 | "folder/hello.txt", |
| 174 | ); |
| 175 | } |
| 176 | #[test] |
| 177 | fn gzip_explicit_name_ignores_fname() { |
| 178 | basic( |
| 179 | include_bytes!("fixtures/sample.gz"), |
| 180 | Some("chosen.txt"), |
| 181 | "chosen.txt", |
| 182 | ); |
| 183 | } |
| 184 | #[test] |
| 185 | fn gzip_concatenated_members() { |
| 186 | let s = Scratch::new(); |
| 187 | let events = extract( |
| 188 | &s, |
| 189 | include_bytes!("fixtures/concat.gz"), |
| 190 | Some("combined"), |
| 191 | Selection::All, |
| 192 | None, |
| 193 | Limits::default(), |
| 194 | ); |
| 195 | assert_eq!(terminal(&events).outcome, Outcome::Completed); |
| 196 | assert_eq!(fs::read(s.0.join("output/combined")).unwrap(), b"onetwo"); |
| 197 | } |
| 198 | #[test] |
| 199 | fn gzip_requires_leaf() { |
| 200 | for name in [None, Some("../escape"), Some("a/b")] { |
| 201 | let s = Scratch::new(); |
| 202 | let e = extract( |
| 203 | &s, |
| 204 | include_bytes!("fixtures/sample.gz"), |
| 205 | name, |
| 206 | Selection::All, |
| 207 | None, |
| 208 | Limits::default(), |
| 209 | ); |
| 210 | assert_eq!(terminal(&e).error, ErrorCode::InvalidRequest); |
| 211 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 212 | } |
| 213 | } |
| 214 | #[test] |
| 215 | fn archive_rejects_gzip_name() { |
| 216 | let s = Scratch::new(); |
| 217 | let e = extract( |
| 218 | &s, |
| 219 | include_bytes!("fixtures/store.zip"), |
| 220 | Some("a"), |
| 221 | Selection::All, |
| 222 | None, |
| 223 | Limits::default(), |
| 224 | ); |
| 225 | assert_eq!(terminal(&e).error, ErrorCode::InvalidRequest); |
| 226 | } |
| 227 | #[test] |
| 228 | fn no_clobber_and_preflight_no_partial_outputs() { |
| 229 | let s = Scratch::new(); |
| 230 | fs::create_dir(s.0.join("output")).unwrap(); |
| 231 | #[cfg(unix)] |
| 232 | { |
| 233 | use std::os::unix::fs::PermissionsExt; |
| 234 | fs::set_permissions(s.0.join("output"), fs::Permissions::from_mode(0o700)).unwrap(); |
| 235 | } |
| 236 | fs::create_dir(s.0.join("output/folder")).unwrap(); |
| 237 | #[cfg(unix)] |
| 238 | { |
| 239 | use std::os::unix::fs::PermissionsExt; |
| 240 | fs::set_permissions(s.0.join("output/folder"), fs::Permissions::from_mode(0o700)).unwrap(); |
| 241 | } |
| 242 | fs::write(s.0.join("output/folder/hello.txt"), b"preserve").unwrap(); |
| 243 | let e = extract( |
| 244 | &s, |
| 245 | include_bytes!("fixtures/store.zip"), |
| 246 | None, |
| 247 | Selection::All, |
| 248 | None, |
| 249 | Limits::default(), |
| 250 | ); |
| 251 | assert_eq!(terminal(&e).error, ErrorCode::Conflict); |
| 252 | assert_eq!( |
| 253 | fs::read(s.0.join("output/folder/hello.txt")).unwrap(), |
| 254 | b"preserve" |
| 255 | ); |
| 256 | assert!(!s.0.join("output/empty").exists()); |
| 257 | } |
| 258 | #[test] |
| 259 | fn unsafe_archives_reject_without_writes() { |
| 260 | for bytes in [ |
| 261 | include_bytes!("fixtures/traversal.zip").as_slice(), |
| 262 | include_bytes!("fixtures/absolute.zip"), |
| 263 | include_bytes!("fixtures/drive.zip"), |
| 264 | include_bytes!("fixtures/reserved.zip"), |
| 265 | include_bytes!("fixtures/link.zip"), |
| 266 | include_bytes!("fixtures/collision.zip"), |
| 267 | include_bytes!("fixtures/parent-collision.zip"), |
| 268 | ] { |
| 269 | let s = Scratch::new(); |
| 270 | let e = extract(&s, bytes, None, Selection::All, None, Limits::default()); |
| 271 | assert_eq!(terminal(&e).outcome, Outcome::Failed, "{e:?}"); |
| 272 | assert_eq!(terminal(&e).created_count, 0); |
| 273 | assert_eq!(terminal(&e).publication, Publication::None); |
| 274 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 275 | } |
| 276 | } |
| 277 | #[test] |
| 278 | fn bomb_rejects_before_writes() { |
| 279 | let s = Scratch::new(); |
| 280 | let e = extract( |
| 281 | &s, |
| 282 | include_bytes!("fixtures/bomb.zip"), |
| 283 | None, |
| 284 | Selection::All, |
| 285 | None, |
| 286 | Limits { |
| 287 | output: 1024, |
| 288 | ..Limits::default() |
| 289 | }, |
| 290 | ); |
| 291 | assert_eq!(terminal(&e).error, ErrorCode::Limit); |
| 292 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 293 | } |
| 294 | #[test] |
| 295 | fn corrupt_crc_and_truncation_reject_without_writes() { |
| 296 | let mut zip = include_bytes!("fixtures/store.zip").to_vec(); |
| 297 | let at = zip |
| 298 | .windows(PAYLOAD.len()) |
| 299 | .position(|b| b == PAYLOAD) |
| 300 | .unwrap(); |
| 301 | zip[at] ^= 1; |
| 302 | for bytes in [zip.as_slice(), &include_bytes!("fixtures/store.zip")[..40]] { |
| 303 | let s = Scratch::new(); |
| 304 | let e = extract(&s, bytes, None, Selection::All, None, Limits::default()); |
| 305 | assert_eq!(terminal(&e).outcome, Outcome::Failed); |
| 306 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 307 | } |
| 308 | } |
| 309 | fn listing(s: &Scratch) -> ListId { |
| 310 | let e = execute(Start { |
| 311 | action: Action::List { |
| 312 | archive: s.path("input"), |
| 313 | }, |
| 314 | limits: Limits::default(), |
| 315 | }); |
| 316 | assert_eq!(terminal(&e).outcome, Outcome::Completed); |
| 317 | e.into_iter() |
| 318 | .find_map(|m| { |
| 319 | if let Message::ListEnd { list_id, .. } = m { |
| 320 | Some(list_id) |
| 321 | } else { |
| 322 | None |
| 323 | } |
| 324 | }) |
| 325 | .unwrap() |
| 326 | } |
| 327 | #[test] |
| 328 | fn list_and_selected_extract() { |
| 329 | let s = Scratch::new(); |
| 330 | let bytes = include_bytes!("fixtures/store.zip"); |
| 331 | fs::write(s.0.join("input"), bytes).unwrap(); |
| 332 | let id = listing(&s); |
| 333 | let e = extract( |
| 334 | &s, |
| 335 | bytes, |
| 336 | None, |
| 337 | Selection::EntryIds(vec![0]), |
| 338 | Some(id), |
| 339 | Limits::default(), |
| 340 | ); |
| 341 | assert_eq!(terminal(&e).outcome, Outcome::Completed); |
| 342 | assert!(!s.0.join("output/empty").exists()); |
| 343 | assert_eq!( |
| 344 | fs::read(s.0.join("output/folder/hello.txt")).unwrap(), |
| 345 | PAYLOAD |
| 346 | ); |
| 347 | } |
| 348 | #[test] |
| 349 | fn stale_list_content_rejected() { |
| 350 | let s = Scratch::new(); |
| 351 | fs::write(s.0.join("input"), include_bytes!("fixtures/store.zip")).unwrap(); |
| 352 | let mut id = listing(&s); |
| 353 | id.archive_sha256[0] ^= 1; |
| 354 | let e = extract( |
| 355 | &s, |
| 356 | include_bytes!("fixtures/store.zip"), |
| 357 | None, |
| 358 | Selection::EntryIds(vec![0]), |
| 359 | Some(id), |
| 360 | Limits::default(), |
| 361 | ); |
| 362 | assert_eq!(terminal(&e).error, ErrorCode::SourceChanged); |
| 363 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 364 | } |
| 365 | #[test] |
| 366 | fn missing_duplicate_and_out_of_range_selection() { |
| 367 | for ids in [vec![], vec![0, 0], vec![999]] { |
| 368 | let s = Scratch::new(); |
| 369 | fs::write(s.0.join("input"), include_bytes!("fixtures/store.zip")).unwrap(); |
| 370 | let id = listing(&s); |
| 371 | let e = extract( |
| 372 | &s, |
| 373 | include_bytes!("fixtures/store.zip"), |
| 374 | None, |
| 375 | Selection::EntryIds(ids), |
| 376 | Some(id), |
| 377 | Limits::default(), |
| 378 | ); |
| 379 | assert_eq!(terminal(&e).error, ErrorCode::InvalidRequest); |
| 380 | } |
| 381 | let s = Scratch::new(); |
| 382 | let e = extract( |
| 383 | &s, |
| 384 | include_bytes!("fixtures/store.zip"), |
| 385 | None, |
| 386 | Selection::EntryIds(vec![0]), |
| 387 | None, |
| 388 | Limits::default(), |
| 389 | ); |
| 390 | assert_eq!(terminal(&e).error, ErrorCode::InvalidRequest); |
| 391 | } |
| 392 | #[test] |
| 393 | fn create_store_and_deflate_zip() { |
| 394 | for method in [Method::Store, Method::Deflate] { |
| 395 | let s = Scratch::new(); |
| 396 | fs::write(s.0.join("source"), PAYLOAD).unwrap(); |
| 397 | let e = execute(Start { |
| 398 | action: Action::CreateZip { |
| 399 | sources: vec![Source { |
| 400 | path: s.path("source"), |
| 401 | archive_name: "dir/hello.txt".into(), |
| 402 | }], |
| 403 | output_file: s.path("result.zip"), |
| 404 | method, |
| 405 | encryption: Encryption::None, |
| 406 | }, |
| 407 | limits: Limits::default(), |
| 408 | }); |
| 409 | assert_eq!(terminal(&e).outcome, Outcome::Completed, "{e:?}"); |
| 410 | let bytes = fs::read(s.0.join("result.zip")).unwrap(); |
| 411 | let parsed = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())).unwrap(); |
| 412 | assert_eq!(parsed.members[0].data, PAYLOAD); |
| 413 | } |
| 414 | } |
| 415 | #[test] |
| 416 | fn encrypted_creation_is_explicitly_unsupported() { |
| 417 | let s = Scratch::new(); |
| 418 | let e = execute(Start { |
| 419 | action: Action::CreateZip { |
| 420 | sources: vec![], |
| 421 | output_file: s.path("result.zip"), |
| 422 | method: Method::Store, |
| 423 | encryption: Encryption::Aes256, |
| 424 | }, |
| 425 | limits: Limits::default(), |
| 426 | }); |
| 427 | assert_eq!(terminal(&e).error, ErrorCode::UnsupportedFeature); |
| 428 | assert!(!s.0.join("result.zip").exists()); |
| 429 | } |
| 430 | #[test] |
| 431 | fn name_policy() { |
| 432 | for name in [ |
| 433 | "../a", "/a", "a\\b", "C:a", "a/../b", "a.", "a ", "COM1.txt", "a//b", "a/./b", |
| 434 | ] { |
| 435 | assert!(archive::valid_name(name, false).is_err(), "{name}"); |
| 436 | } |
| 437 | assert!(archive::valid_name("folder/한글.txt", false).is_ok()); |
| 438 | } |
| 439 | #[test] |
| 440 | fn frame_round_trip_and_secret_redaction() { |
| 441 | let mut b = Vec::new(); |
| 442 | Writer::new(&mut b, 42).send(&Message::Cancel).unwrap(); |
| 443 | let mut reader = Reader::new(b.as_slice()); |
| 444 | assert!(matches!( |
| 445 | reader.read().unwrap(), |
| 446 | Some((42, 0, Message::Cancel)) |
| 447 | )); |
| 448 | assert!(reader.read().unwrap().is_none()); |
| 449 | assert!( |
| 450 | !format!( |
| 451 | "{:?}", |
| 452 | Message::Password { |
| 453 | challenge_id: 1, |
| 454 | secret: Secret(b"private-password".to_vec()) |
| 455 | } |
| 456 | ) |
| 457 | .contains("private-password") |
| 458 | ); |
| 459 | } |
| 460 | #[test] |
| 461 | fn invalid_headers_are_rejected() { |
| 462 | let mut b = Vec::new(); |
| 463 | Writer::new(&mut b, 42).send(&Message::Cancel).unwrap(); |
| 464 | for (offset, value) in [(0, 0), (4, 2), (12, 1), (16, 0), (24, 1)] { |
| 465 | let mut bad = b.clone(); |
| 466 | bad[offset] = value; |
| 467 | assert!(Reader::new(bad.as_slice()).read().is_err()); |
| 468 | } |
| 469 | let mut big = b.clone(); |
| 470 | big[8..12].copy_from_slice(&1_048_577u32.to_le_bytes()); |
| 471 | assert!(Reader::new(big.as_slice()).read().is_err()); |
| 472 | assert!(Reader::new(&b[..16]).read().is_err()); |
| 473 | } |
| 474 | #[test] |
| 475 | fn terminal_is_final_frame() { |
| 476 | let t = Terminal { |
| 477 | outcome: Outcome::Failed, |
| 478 | error: ErrorCode::Io, |
| 479 | publication: Publication::Uncertain, |
| 480 | created_count: 0, |
| 481 | retained_partial_count: 0, |
| 482 | durability: Durability::Unconfirmed, |
| 483 | cleanup: Cleanup::Unconfirmed, |
| 484 | detail: "uncertain".into(), |
| 485 | }; |
| 486 | let mut b = Vec::new(); |
| 487 | let mut w = Writer::new(&mut b, 1); |
| 488 | w.send(&Message::Terminal(t)).unwrap(); |
| 489 | assert!(w.send(&Message::Cancel).is_err()); |
| 490 | } |
| 491 | #[cfg(unix)] |
| 492 | #[test] |
| 493 | fn symlink_parent_escape_is_rejected() { |
| 494 | use std::os::unix::fs::symlink; |
| 495 | let s = Scratch::new(); |
| 496 | let d = fs::DirBuilder::new(); |
| 497 | #[cfg(unix)] |
| 498 | let mut d = d; |
| 499 | use std::os::unix::fs::DirBuilderExt; |
| 500 | d.mode(0o700); |
| 501 | d.create(s.0.join("output")).unwrap(); |
| 502 | d.create(s.0.join("outside")).unwrap(); |
| 503 | symlink(s.0.join("outside"), s.0.join("output/folder")).unwrap(); |
| 504 | let e = extract( |
| 505 | &s, |
| 506 | include_bytes!("fixtures/store.zip"), |
| 507 | None, |
| 508 | Selection::All, |
| 509 | None, |
| 510 | Limits::default(), |
| 511 | ); |
| 512 | assert_eq!(terminal(&e).error, ErrorCode::UnsafePath); |
| 513 | assert_eq!(fs::read_dir(s.0.join("outside")).unwrap().count(), 0); |
| 514 | } |
| 515 | |
| 516 | #[test] |
| 517 | fn changed_source_is_reopen_error_before_decode() { |
| 518 | let s = Scratch::new(); |
| 519 | let bytes = include_bytes!("fixtures/store.zip"); |
| 520 | fs::write(s.0.join("input"), bytes).unwrap(); |
| 521 | let id = listing(&s); |
| 522 | let mut changed = bytes.to_vec(); |
| 523 | changed[40] ^= 1; |
| 524 | let e = extract( |
| 525 | &s, |
| 526 | &changed, |
| 527 | None, |
| 528 | Selection::EntryIds(vec![0]), |
| 529 | Some(id), |
| 530 | Limits::default(), |
| 531 | ); |
| 532 | assert_eq!(terminal(&e).error, ErrorCode::SourceChanged); |
| 533 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 534 | } |
| 535 | #[test] |
| 536 | fn cancel_ack_and_terminal_are_distinct() { |
| 537 | let s = Scratch::new(); |
| 538 | fs::write(s.0.join("source"), vec![0u8; 16 * 1024 * 1024]).unwrap(); |
| 539 | let start = Start { |
| 540 | action: Action::CreateZip { |
| 541 | sources: vec![Source { |
| 542 | path: s.path("source"), |
| 543 | archive_name: "data".into(), |
| 544 | }], |
| 545 | output_file: s.path("result.zip"), |
| 546 | method: Method::Deflate, |
| 547 | encryption: Encryption::None, |
| 548 | }, |
| 549 | limits: Limits::default(), |
| 550 | }; |
| 551 | let mut child = Command::new(env!("CARGO_BIN_EXE_zipelo-archive-worker")) |
| 552 | .stdin(Stdio::piped()) |
| 553 | .stdout(Stdio::piped()) |
| 554 | .stderr(Stdio::piped()) |
| 555 | .spawn() |
| 556 | .unwrap(); |
| 557 | let mut writer = Writer::new(child.stdin.take().unwrap(), 19); |
| 558 | let mut reader = Reader::new(child.stdout.take().unwrap()); |
| 559 | writer.send(&Message::Start(start)).unwrap(); |
| 560 | assert!(matches!( |
| 561 | reader.read().unwrap(), |
| 562 | Some((19, 0, Message::Accepted { .. })) |
| 563 | )); |
| 564 | writer.send(&Message::Cancel).unwrap(); |
| 565 | writer.send(&Message::Cancel).unwrap(); |
| 566 | let mut events = Vec::new(); |
| 567 | while let Some((_, _, m)) = reader.read().unwrap() { |
| 568 | events.push(m); |
| 569 | } |
| 570 | assert!(child.wait().unwrap().success()); |
| 571 | let terminal = terminal(&events); |
| 572 | assert!(matches!( |
| 573 | terminal.outcome, |
| 574 | Outcome::Cancelled | Outcome::Completed |
| 575 | )); |
| 576 | for m in &events { |
| 577 | if let Message::CancelAck { |
| 578 | request_sequence, |
| 579 | status, |
| 580 | } = m |
| 581 | { |
| 582 | assert!([1, 2].contains(request_sequence)); |
| 583 | assert!(matches!( |
| 584 | status, |
| 585 | CancelStatus::Requested |
| 586 | | CancelStatus::AlreadyRequested |
| 587 | | CancelStatus::TooLate |
| 588 | | CancelStatus::Finished |
| 589 | )); |
| 590 | } |
| 591 | } |
| 592 | assert!( |
| 593 | events |
| 594 | .iter() |
| 595 | .any(|m| matches!(m, Message::CancelAck { .. })), |
| 596 | "cancel must be consumed during bounded source read: {events:?}" |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | #[test] |
| 601 | fn encrypted_zip_rejects_without_outputs() { |
| 602 | let mut bytes = include_bytes!("fixtures/store.zip").to_vec(); |
| 603 | bytes[6] |= 1; |
| 604 | let central = bytes.windows(4).position(|b| b == b"PK\x01\x02").unwrap(); |
| 605 | bytes[central + 8] |= 1; |
| 606 | let s = Scratch::new(); |
| 607 | let e = extract(&s, &bytes, None, Selection::All, None, Limits::default()); |
| 608 | assert_eq!(terminal(&e).error, ErrorCode::UnsupportedFeature); |
| 609 | assert_eq!(terminal(&e).publication, Publication::None); |
| 610 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 611 | } |
| 612 | #[test] |
| 613 | fn uncertain_report_survives_codec() { |
| 614 | let t = Terminal { |
| 615 | outcome: Outcome::Failed, |
| 616 | error: ErrorCode::Io, |
| 617 | publication: Publication::Uncertain, |
| 618 | created_count: 2, |
| 619 | retained_partial_count: 1, |
| 620 | durability: Durability::Failed, |
| 621 | cleanup: Cleanup::Failed, |
| 622 | detail: "publication uncertain".into(), |
| 623 | }; |
| 624 | let mut bytes = Vec::new(); |
| 625 | Writer::new(&mut bytes, 88) |
| 626 | .send(&Message::Terminal(t.clone())) |
| 627 | .unwrap(); |
| 628 | let mut reader = Reader::new(bytes.as_slice()); |
| 629 | match reader.read().unwrap() { |
| 630 | Some((88, 0, Message::Terminal(actual))) => assert_eq!(actual, t), |
| 631 | other => panic!("{other:?}"), |
| 632 | }; |
| 633 | assert!(reader.read().unwrap().is_none()); |
| 634 | } |
| 635 | |
| 636 | fn path_fixture_zip(names: &[String]) -> Vec<u8> { |
| 637 | let mut output = Vec::new(); |
| 638 | let mut central = Vec::new(); |
| 639 | for name in names { |
| 640 | let offset = output.len() as u32; |
| 641 | output.extend(b"PK\x03\x04"); |
| 642 | for value in [20u16, 0x0800, 0, 0, 33] { |
| 643 | output.extend(value.to_le_bytes()); |
| 644 | } |
| 645 | for value in [0u32, 0, 0] { |
| 646 | output.extend(value.to_le_bytes()); |
| 647 | } |
| 648 | output.extend((name.len() as u16).to_le_bytes()); |
| 649 | output.extend(0u16.to_le_bytes()); |
| 650 | output.extend(name.as_bytes()); |
| 651 | central.extend(b"PK\x01\x02"); |
| 652 | for value in [0x0314u16, 20, 0x0800, 0, 0, 33] { |
| 653 | central.extend(value.to_le_bytes()); |
| 654 | } |
| 655 | for value in [0u32, 0, 0] { |
| 656 | central.extend(value.to_le_bytes()); |
| 657 | } |
| 658 | central.extend((name.len() as u16).to_le_bytes()); |
| 659 | for value in [0u16, 0, 0, 0] { |
| 660 | central.extend(value.to_le_bytes()); |
| 661 | } |
| 662 | central.extend((0o100600u32 << 16).to_le_bytes()); |
| 663 | central.extend(offset.to_le_bytes()); |
| 664 | central.extend(name.as_bytes()); |
| 665 | } |
| 666 | let offset = output.len() as u32; |
| 667 | output.extend(¢ral); |
| 668 | output.extend(b"PK\x05\x06"); |
| 669 | for value in [0u16, 0, names.len() as u16, names.len() as u16] { |
| 670 | output.extend(value.to_le_bytes()); |
| 671 | } |
| 672 | output.extend((central.len() as u32).to_le_bytes()); |
| 673 | output.extend(offset.to_le_bytes()); |
| 674 | output.extend(0u16.to_le_bytes()); |
| 675 | output |
| 676 | } |
| 677 | |
| 678 | fn assert_metadata_limit(names: &[String]) { |
| 679 | let scratch = Scratch::new(); |
| 680 | let events = extract( |
| 681 | &scratch, |
| 682 | &path_fixture_zip(names), |
| 683 | None, |
| 684 | Selection::All, |
| 685 | None, |
| 686 | Limits::default(), |
| 687 | ); |
| 688 | let result = terminal(&events); |
| 689 | assert_eq!(result.outcome, Outcome::Failed, "{events:?}"); |
| 690 | assert_eq!(result.error, ErrorCode::Limit, "{events:?}"); |
| 691 | assert_eq!(result.publication, Publication::None); |
| 692 | assert_eq!(result.created_count, 0); |
| 693 | assert_eq!(fs::read_dir(scratch.0.join("output")).unwrap().count(), 0); |
| 694 | } |
| 695 | |
| 696 | #[test] |
| 697 | fn deep_zip_returns_limit_without_abort_or_writes() { |
| 698 | let names = (0..100) |
| 699 | .map(|i| format!("{}file{i:03}", "a/".repeat(2000))) |
| 700 | .collect::<Vec<_>>(); |
| 701 | assert_metadata_limit(&names); |
| 702 | } |
| 703 | |
| 704 | #[test] |
| 705 | fn shallow_zip_parent_metadata_is_bounded() { |
| 706 | let names = (0..500) |
| 707 | .map(|i| { |
| 708 | format!( |
| 709 | "d{i:04}{}/{}file", |
| 710 | "a".repeat(180), |
| 711 | format!("{}/", "b".repeat(200)).repeat(7) |
| 712 | ) |
| 713 | }) |
| 714 | .collect::<Vec<_>>(); |
| 715 | assert!( |
| 716 | names |
| 717 | .iter() |
| 718 | .all(|n| n.split('/').count() <= archive::MAX_PATH_DEPTH) |
| 719 | ); |
| 720 | assert!( |
| 721 | archive::parse( |
| 722 | &path_fixture_zip(&names), |
| 723 | Limits::default(), |
| 724 | false, |
| 725 | &mut || Ok(()) |
| 726 | ) |
| 727 | .is_ok() |
| 728 | ); |
| 729 | assert_metadata_limit(&names); |
| 730 | } |
| 731 | |
| 732 | #[test] |
| 733 | fn aggregate_archive_name_metadata_is_bounded() { |
| 734 | let names = (0..1100) |
| 735 | .map(|i| { |
| 736 | format!( |
| 737 | "d{i:04}{}/{}file", |
| 738 | "a".repeat(180), |
| 739 | format!("{}/", "b".repeat(200)).repeat(9) |
| 740 | ) |
| 741 | }) |
| 742 | .collect::<Vec<_>>(); |
| 743 | let bytes = path_fixture_zip(&names); |
| 744 | assert!((bytes.len() as u64) < Limits::default().input); |
| 745 | assert!(matches!( |
| 746 | archive::parse(&bytes, Limits::default(), false, &mut || Ok(())), |
| 747 | Err(ErrorCode::Limit) |
| 748 | )); |
| 749 | assert_metadata_limit(&names); |
| 750 | } |
| 751 | |
| 752 | #[test] |
| 753 | fn path_depth_boundary_is_explicit() { |
| 754 | let accepted = format!("{}file", "a/".repeat(archive::MAX_PATH_DEPTH - 1)); |
| 755 | let rejected = format!("{}file", "a/".repeat(archive::MAX_PATH_DEPTH)); |
| 756 | assert!(archive::valid_name(&accepted, false).is_ok()); |
| 757 | assert_eq!(archive::valid_name(&rejected, false), Err(ErrorCode::Limit)); |
| 758 | } |
| 759 | |
| 760 | fn zeros_gzip(length: usize) -> Vec<u8> { |
| 761 | let mut gzip = vec![31, 139, 8, 0, 0, 0, 0, 0, 0, 255]; |
| 762 | let mut left = length; |
| 763 | loop { |
| 764 | let size = left.min(u16::MAX as usize) as u16; |
| 765 | left -= size as usize; |
| 766 | gzip.push(u8::from(left == 0)); |
| 767 | gzip.extend(size.to_le_bytes()); |
| 768 | gzip.extend((!size).to_le_bytes()); |
| 769 | gzip.resize(gzip.len() + size as usize, 0); |
| 770 | if left == 0 { |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | gzip.extend(zlib_rs::crc32::crc32(0, &vec![0; length]).to_le_bytes()); |
| 775 | gzip.extend((length as u32).to_le_bytes()); |
| 776 | gzip |
| 777 | } |
| 778 | |
| 779 | #[test] |
| 780 | fn zero_payload_gzip_is_not_tar() { |
| 781 | for length in [512, 1024, 1536] { |
| 782 | let scratch = Scratch::new(); |
| 783 | let bytes = zeros_gzip(length); |
| 784 | let parsed = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())).unwrap(); |
| 785 | assert_eq!(parsed.format, Format::Gzip); |
| 786 | let events = extract( |
| 787 | &scratch, |
| 788 | &bytes, |
| 789 | Some("zeros.bin"), |
| 790 | Selection::All, |
| 791 | None, |
| 792 | Limits::default(), |
| 793 | ); |
| 794 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 795 | assert_eq!( |
| 796 | fs::read(scratch.0.join("output/zeros.bin")).unwrap(), |
| 797 | vec![0; length] |
| 798 | ); |
| 799 | fs::write(scratch.0.join("input"), &bytes).unwrap(); |
| 800 | let list_id = listing(&scratch); |
| 801 | assert_eq!(list_id.format, Format::Gzip); |
| 802 | } |
| 803 | } |