| 1 | use super::*; |
| 2 | use std::{io::Read, process::Child, sync::mpsc, time::Duration}; |
| 3 | |
| 4 | struct Running(Child); |
| 5 | impl Drop for Running { |
| 6 | fn drop(&mut self) { |
| 7 | let _ = self.0.kill(); |
| 8 | let _ = self.0.wait(); |
| 9 | } |
| 10 | } |
| 11 | fn run( |
| 12 | s: &Scratch, |
| 13 | start: GuardedStart, |
| 14 | respond: impl FnMut(&Message) -> Option<Vec<Message>>, |
| 15 | ) -> Vec<Message> { |
| 16 | run_fault(s, start, None, respond) |
| 17 | } |
| 18 | fn run_fault( |
| 19 | s: &Scratch, |
| 20 | start: GuardedStart, |
| 21 | fault: Option<&str>, |
| 22 | mut respond: impl FnMut(&Message) -> Option<Vec<Message>>, |
| 23 | ) -> Vec<Message> { |
| 24 | let mut command = s.worker(); |
| 25 | #[cfg(target_os = "linux")] |
| 26 | if let Some(fault) = fault { |
| 27 | let shim = s.0.join("io_fault.so"); |
| 28 | assert!( |
| 29 | Command::new("cc") |
| 30 | .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) |
| 31 | .arg(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join( |
| 32 | if fault.starts_with("clock-") { |
| 33 | "../../tests/rust-archive-worker/progress_clock.c" |
| 34 | } else { |
| 35 | "../../tests/rust-archive-worker/io_fault.c" |
| 36 | } |
| 37 | )) |
| 38 | .args(["-ldl", "-o"]) |
| 39 | .arg(&shim) |
| 40 | .status() |
| 41 | .unwrap() |
| 42 | .success() |
| 43 | ); |
| 44 | command |
| 45 | .env("LD_PRELOAD", shim) |
| 46 | .env("ZIPELO_TEST_FAULT", fault) |
| 47 | .env("ZIPELO_TEST_MARKER", s.0.join("marker")); |
| 48 | } |
| 49 | #[cfg(not(target_os = "linux"))] |
| 50 | assert!(fault.is_none()); |
| 51 | let mut child = Running( |
| 52 | command |
| 53 | .stdin(Stdio::piped()) |
| 54 | .stdout(Stdio::piped()) |
| 55 | .stderr(Stdio::piped()) |
| 56 | .spawn() |
| 57 | .unwrap(), |
| 58 | ); |
| 59 | let mut writer = Some(Writer::new(child.0.stdin.take().unwrap(), 17)); |
| 60 | writer |
| 61 | .as_mut() |
| 62 | .unwrap() |
| 63 | .send(&Message::StartGuarded(start)) |
| 64 | .unwrap(); |
| 65 | let stdout = child.0.stdout.take().unwrap(); |
| 66 | let mut stderr = child.0.stderr.take().unwrap(); |
| 67 | let (send, receive) = mpsc::channel(); |
| 68 | let reader = std::thread::spawn(move || { |
| 69 | let mut reader = Reader::new(stdout); |
| 70 | loop { |
| 71 | let frame = reader.read(); |
| 72 | let done = !matches!(frame, Ok(Some(_))); |
| 73 | if send.send(frame).is_err() || done { |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | }); |
| 78 | let mut events = Vec::new(); |
| 79 | loop { |
| 80 | let Some((job, _, event)) = receive |
| 81 | .recv_timeout(Duration::from_secs(20)) |
| 82 | .expect("worker timeout") |
| 83 | .unwrap() |
| 84 | else { |
| 85 | break; |
| 86 | }; |
| 87 | assert_eq!(job, 17); |
| 88 | if let Some(messages) = respond(&event) { |
| 89 | if messages.is_empty() { |
| 90 | writer = None; |
| 91 | } else { |
| 92 | for message in messages { |
| 93 | writer.as_mut().unwrap().send(&message).unwrap(); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | let done = matches!(event, Message::Terminal(_)); |
| 98 | events.push(event); |
| 99 | if done { |
| 100 | writer = None; |
| 101 | } |
| 102 | } |
| 103 | assert!(child.0.wait().unwrap().success(), "{events:?}"); |
| 104 | let mut errors = Vec::new(); |
| 105 | stderr.read_to_end(&mut errors).unwrap(); |
| 106 | assert!(errors.is_empty(), "{errors:?}"); |
| 107 | reader.join().unwrap(); |
| 108 | assert!(matches!(events.last(), Some(Message::Terminal(_)))); |
| 109 | events |
| 110 | } |
| 111 | fn request(s: &Scratch, output: u64, ceiling: u64) -> GuardedStart { |
| 112 | GuardedStart { |
| 113 | start: Start { |
| 114 | action: Action::Extract { |
| 115 | archive: s.path("input"), |
| 116 | destination: s.path("output"), |
| 117 | selection: Selection::All, |
| 118 | list_id: None, |
| 119 | gzip_output_name: None, |
| 120 | }, |
| 121 | limits: Limits { |
| 122 | output, |
| 123 | ..Limits::default() |
| 124 | }, |
| 125 | }, |
| 126 | ceiling: Limits { |
| 127 | output: ceiling, |
| 128 | entries: 200_000, |
| 129 | ..Limits::default() |
| 130 | }, |
| 131 | interactive: false, |
| 132 | } |
| 133 | } |
| 134 | fn setup() -> Scratch { |
| 135 | let s = Scratch::new(); |
| 136 | fs::write(s.0.join("input"), include_bytes!("fixtures/deflate.zip")).unwrap(); |
| 137 | fs::create_dir(s.0.join("output")).unwrap(); |
| 138 | #[cfg(unix)] |
| 139 | { |
| 140 | use std::os::unix::fs::PermissionsExt; |
| 141 | fs::set_permissions(s.0.join("output"), fs::Permissions::from_mode(0o700)).unwrap(); |
| 142 | } |
| 143 | s |
| 144 | } |
| 145 | fn approve(event: &Message) -> Option<Vec<Message>> { |
| 146 | if let Message::ResourceRequired { |
| 147 | challenge_id, |
| 148 | scope, |
| 149 | .. |
| 150 | } = event |
| 151 | { |
| 152 | Some(vec![Message::ResourceReply { |
| 153 | challenge_id: *challenge_id, |
| 154 | scope: *scope, |
| 155 | allow: true, |
| 156 | }]) |
| 157 | } else { |
| 158 | None |
| 159 | } |
| 160 | } |
| 161 | #[test] |
| 162 | fn scaled_output_boundaries_and_approval() { |
| 163 | for limit in [ |
| 164 | PAYLOAD.len() as u64 + 1, |
| 165 | PAYLOAD.len() as u64, |
| 166 | PAYLOAD.len() as u64 - 1, |
| 167 | ] { |
| 168 | let s = setup(); |
| 169 | let events = run(&s, request(&s, limit, 64), approve); |
| 170 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 171 | assert_eq!( |
| 172 | events |
| 173 | .iter() |
| 174 | .filter(|e| matches!(e, Message::ResourceRequired { .. })) |
| 175 | .count(), |
| 176 | 1 |
| 177 | ); |
| 178 | assert_eq!( |
| 179 | fs::read(s.0.join("output/folder/hello.txt")).unwrap(), |
| 180 | PAYLOAD |
| 181 | ); |
| 182 | } |
| 183 | } |
| 184 | #[test] |
| 185 | fn decline_cancel_and_eof_publish_nothing() { |
| 186 | for mode in 0..3 { |
| 187 | let s = setup(); |
| 188 | let events = run(&s, request(&s, 16, 64), |event| { |
| 189 | if let Message::ResourceRequired { |
| 190 | challenge_id, |
| 191 | scope, |
| 192 | usage, |
| 193 | .. |
| 194 | } = event |
| 195 | { |
| 196 | assert_eq!(usage.estimated_bytes, None); |
| 197 | assert_eq!(usage.actual_bytes, None); |
| 198 | assert!(usage.available_bytes.is_some()); |
| 199 | Some(match mode { |
| 200 | 0 => vec![Message::ResourceReply { |
| 201 | challenge_id: *challenge_id, |
| 202 | scope: *scope, |
| 203 | allow: false, |
| 204 | }], |
| 205 | 1 => vec![Message::Cancel], |
| 206 | _ => vec![], |
| 207 | }) |
| 208 | } else { |
| 209 | None |
| 210 | } |
| 211 | }); |
| 212 | assert_eq!( |
| 213 | terminal(&events).outcome, |
| 214 | if mode == 2 { |
| 215 | Outcome::Failed |
| 216 | } else { |
| 217 | Outcome::Cancelled |
| 218 | }, |
| 219 | "{events:?}" |
| 220 | ); |
| 221 | assert_eq!(terminal(&events).publication, Publication::None); |
| 222 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 223 | if mode == 1 { |
| 224 | assert!(events.iter().any(|e| matches!( |
| 225 | e, |
| 226 | Message::CancelAck { |
| 227 | status: CancelStatus::Requested, |
| 228 | .. |
| 229 | } |
| 230 | ))); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | #[test] |
| 235 | fn stale_duplicate_wrong_scope_and_premature_replies_are_rejected() { |
| 236 | for mode in 0..4 { |
| 237 | let s = setup(); |
| 238 | let events = run(&s, request(&s, 16, 64), |event| { |
| 239 | if mode == 3 && matches!(event, Message::Accepted { .. }) { |
| 240 | return Some(vec![Message::ResourceReply { |
| 241 | challenge_id: 99, |
| 242 | scope: ResourceScope::default(), |
| 243 | allow: true, |
| 244 | }]); |
| 245 | } |
| 246 | if let Message::ResourceRequired { |
| 247 | challenge_id, |
| 248 | scope, |
| 249 | .. |
| 250 | } = event |
| 251 | { |
| 252 | let reply = |id, scope| Message::ResourceReply { |
| 253 | challenge_id: id, |
| 254 | scope, |
| 255 | allow: true, |
| 256 | }; |
| 257 | Some(match mode { |
| 258 | 0 => vec![reply(challenge_id + 1, *scope)], |
| 259 | 1 => vec![reply(*challenge_id, ResourceScope::default())], |
| 260 | 2 => vec![reply(*challenge_id, *scope), reply(*challenge_id, *scope)], |
| 261 | _ => vec![], |
| 262 | }) |
| 263 | } else { |
| 264 | None |
| 265 | } |
| 266 | }); |
| 267 | assert_eq!( |
| 268 | terminal(&events).error, |
| 269 | ErrorCode::Protocol, |
| 270 | "{mode}: {events:?}" |
| 271 | ); |
| 272 | assert_eq!(terminal(&events).publication, Publication::None); |
| 273 | } |
| 274 | } |
| 275 | #[test] |
| 276 | fn ceiling_bytes_and_entries_cannot_be_approved_away() { |
| 277 | for entries in [false, true] { |
| 278 | let s = setup(); |
| 279 | let mut start = request(&s, 1, 16); |
| 280 | if entries { |
| 281 | start.start.limits.output = 64; |
| 282 | start.ceiling.output = 64; |
| 283 | start.start.limits.entries = 1; |
| 284 | start.ceiling.entries = 2; |
| 285 | let members = (0..3) |
| 286 | .map(|id| archive::Member { |
| 287 | entry: Entry { |
| 288 | entry_id: id, |
| 289 | name: format!("empty{id}"), |
| 290 | kind: EntryKind::File, |
| 291 | size: 0, |
| 292 | compressed_size: 0, |
| 293 | encrypted: false, |
| 294 | }, |
| 295 | data: Vec::new(), |
| 296 | }) |
| 297 | .collect::<Vec<_>>(); |
| 298 | let zip = |
| 299 | archive::create_zip(&members, Method::Store, Limits::default(), &mut || Ok(())) |
| 300 | .unwrap(); |
| 301 | fs::write(s.0.join("input"), zip).unwrap(); |
| 302 | } |
| 303 | let mut approved = false; |
| 304 | let events = run(&s, start, |event| { |
| 305 | if matches!(event, Message::ResourceRequired { .. }) { |
| 306 | approved = true; |
| 307 | } |
| 308 | approve(event) |
| 309 | }); |
| 310 | assert!(approved); |
| 311 | assert_eq!(terminal(&events).error, ErrorCode::Limit, "{events:?}"); |
| 312 | assert_eq!(terminal(&events).created_count, 0); |
| 313 | } |
| 314 | } |
| 315 | #[test] |
| 316 | fn entries_have_a_separate_approval() { |
| 317 | let s = setup(); |
| 318 | let mut start = request(&s, 64, 64); |
| 319 | start.start.limits.entries = 1; |
| 320 | start.ceiling.entries = 3; |
| 321 | let events = run(&s, start, |event| { |
| 322 | if let Message::ResourceRequired { scope, .. } = event { |
| 323 | assert!(scope.entries); |
| 324 | assert!(!scope.output); |
| 325 | } |
| 326 | approve(event) |
| 327 | }); |
| 328 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 329 | } |
| 330 | #[test] |
| 331 | fn resource_then_password_then_conflict_use_distinct_questions() { |
| 332 | let s = setup(); |
| 333 | fs::write( |
| 334 | s.0.join("input"), |
| 335 | include_bytes!("fixtures/aes2-deflate.zip"), |
| 336 | ) |
| 337 | .unwrap(); |
| 338 | fs::create_dir_all(s.0.join("output/folder")).unwrap(); |
| 339 | fs::write(s.0.join("output/payload.bin"), b"old").unwrap(); |
| 340 | let mut start = request(&s, 16, 8192); |
| 341 | start.interactive = true; |
| 342 | let mut ids = Vec::new(); |
| 343 | let events = run(&s, start, |event| match event { |
| 344 | Message::ResourceRequired { challenge_id, .. } => { |
| 345 | ids.push(*challenge_id); |
| 346 | approve(event) |
| 347 | } |
| 348 | Message::PasswordRequired { challenge_id, .. } => { |
| 349 | ids.push(*challenge_id); |
| 350 | Some(vec![Message::Password { |
| 351 | challenge_id: *challenge_id, |
| 352 | secret: Secret(TEST_PASSWORD.to_vec()), |
| 353 | }]) |
| 354 | } |
| 355 | Message::ConflictRequired { challenge_id, .. } => { |
| 356 | ids.push(*challenge_id); |
| 357 | Some(vec![Message::ConflictReply { |
| 358 | challenge_id: *challenge_id, |
| 359 | choice: ConflictChoice::Overwrite, |
| 360 | apply_to_remaining: false, |
| 361 | }]) |
| 362 | } |
| 363 | _ => None, |
| 364 | }); |
| 365 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 366 | assert!(ids.len() >= 3, "{events:?}"); |
| 367 | assert!(ids.windows(2).all(|w| w[0] < w[1])); |
| 368 | } |
| 369 | #[test] |
| 370 | fn create_source_total_is_approved_before_password() { |
| 371 | let s = setup(); |
| 372 | fs::write(s.0.join("source"), b"12345678").unwrap(); |
| 373 | let mut start = request(&s, 7, 8); |
| 374 | start.start.action = Action::CreateZip { |
| 375 | sources: vec![Source { |
| 376 | path: s.path("source"), |
| 377 | archive_name: "source".into(), |
| 378 | }], |
| 379 | output_file: s.path("new.zip"), |
| 380 | method: Method::Store, |
| 381 | encryption: Encryption::Aes256, |
| 382 | }; |
| 383 | let mut approved = false; |
| 384 | let events = run(&s, start, |event| match event { |
| 385 | Message::ResourceRequired { action, .. } => { |
| 386 | assert_eq!(*action, ActionKind::CreateZip); |
| 387 | approved = true; |
| 388 | approve(event) |
| 389 | } |
| 390 | Message::PasswordRequired { challenge_id, .. } => { |
| 391 | assert!(approved); |
| 392 | Some(vec![Message::Password { |
| 393 | challenge_id: *challenge_id, |
| 394 | secret: Secret(b"secret".to_vec()), |
| 395 | }]) |
| 396 | } |
| 397 | _ => None, |
| 398 | }); |
| 399 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 400 | assert!(s.0.join("new.zip").exists()); |
| 401 | } |
| 402 | #[test] |
| 403 | fn guarded_wire_round_trip_and_invalid_ceiling() { |
| 404 | let s = setup(); |
| 405 | let start = request(&s, 16, 64); |
| 406 | let mut bytes = Vec::new(); |
| 407 | Writer::new(&mut bytes, 17) |
| 408 | .send(&Message::StartGuarded(start.clone())) |
| 409 | .unwrap(); |
| 410 | assert_eq!(&bytes[6..8], &6u16.to_le_bytes()); |
| 411 | match Reader::new(bytes.as_slice()).read().unwrap().unwrap().2 { |
| 412 | Message::StartGuarded(decoded) => assert_eq!(decoded, start), |
| 413 | other => panic!("{other:?}"), |
| 414 | } |
| 415 | let mut invalid = start; |
| 416 | invalid.ceiling.output = 64 * 1024 * 1024 * 1024 + 1; |
| 417 | assert!(!invalid.valid()); |
| 418 | assert!( |
| 419 | Writer::new(Vec::new(), 17) |
| 420 | .send(&Message::StartGuarded(invalid)) |
| 421 | .is_err() |
| 422 | ); |
| 423 | let mut reply = Vec::new(); |
| 424 | Writer::new(&mut reply, 17) |
| 425 | .send(&Message::ResourceReply { |
| 426 | challenge_id: 4, |
| 427 | scope: ResourceScope { |
| 428 | ratio: true, |
| 429 | ..ResourceScope::default() |
| 430 | }, |
| 431 | allow: true, |
| 432 | }) |
| 433 | .unwrap(); |
| 434 | assert_eq!(&reply[6..8], &7u16.to_le_bytes()); |
| 435 | reply[43] = 2; |
| 436 | assert!(Reader::new(reply.as_slice()).read().is_err()); |
| 437 | } |
| 438 | |
| 439 | fn generate(s: &Scratch, case: &str) { |
| 440 | let result = Command::new("python3") |
| 441 | .arg( |
| 442 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 443 | .join("../../tests/rust-archive-worker/guarded_cases.py"), |
| 444 | ) |
| 445 | .arg(&s.0) |
| 446 | .arg(case) |
| 447 | .output() |
| 448 | .unwrap(); |
| 449 | assert!( |
| 450 | result.status.success(), |
| 451 | "{}", |
| 452 | String::from_utf8_lossy(&result.stderr) |
| 453 | ); |
| 454 | } |
| 455 | #[test] |
| 456 | fn compressed_formats_keep_limits_after_permission() { |
| 457 | for kind in ["gz", "tar", "targz", "bz2", "xz", "7z"] { |
| 458 | let s = setup(); |
| 459 | match kind { |
| 460 | "gz" => fs::write(s.0.join("input"), include_bytes!("fixtures/sample.gz")).unwrap(), |
| 461 | "tar" => fs::write(s.0.join("input"), include_bytes!("fixtures/sample.tar")).unwrap(), |
| 462 | "targz" => { |
| 463 | fs::write(s.0.join("input"), include_bytes!("fixtures/sample.tar.gz")).unwrap() |
| 464 | } |
| 465 | "bz2" | "xz" => generate(&s, kind), |
| 466 | "7z" => fs::write( |
| 467 | s.0.join("input"), |
| 468 | fs::read( |
| 469 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 470 | .join("../../tests/rust-archive-worker/fixtures/7z/copy.7z"), |
| 471 | ) |
| 472 | .unwrap(), |
| 473 | ) |
| 474 | .unwrap(), |
| 475 | _ => unreachable!(), |
| 476 | } |
| 477 | let mut start = request(&s, 16, 65536); |
| 478 | if kind == "gz" { |
| 479 | if let Action::Extract { |
| 480 | gzip_output_name, .. |
| 481 | } = &mut start.start.action |
| 482 | { |
| 483 | *gzip_output_name = Some("payload".into()); |
| 484 | } |
| 485 | } |
| 486 | let events = run(&s, start, approve); |
| 487 | assert_eq!( |
| 488 | terminal(&events).outcome, |
| 489 | Outcome::Completed, |
| 490 | "{kind}: {events:?}" |
| 491 | ); |
| 492 | } |
| 493 | } |
| 494 | #[test] |
| 495 | fn small_high_ratio_stream_requires_another_scope_and_cancels_promptly() { |
| 496 | let s = setup(); |
| 497 | generate(&s, "bomb"); |
| 498 | assert!(fs::metadata(s.0.join("input")).unwrap().len() < 1024 * 1024); |
| 499 | let mut start = request(&s, 16 * 1024 * 1024 * 1024, 64 * 1024 * 1024 * 1024); |
| 500 | if let Action::Extract { |
| 501 | gzip_output_name, .. |
| 502 | } = &mut start.start.action |
| 503 | { |
| 504 | *gzip_output_name = Some("payload".into()); |
| 505 | } |
| 506 | let mut ratio = false; |
| 507 | let mut cancelled = None; |
| 508 | let events = run(&s, start, |event| { |
| 509 | if let Message::ResourceRequired { scope, usage, .. } = event { |
| 510 | if scope.ratio { |
| 511 | ratio = true; |
| 512 | assert!(!scope.output && !scope.entries); |
| 513 | assert!(usage.actual_bytes.unwrap() >= 256 * 1024 * 1024); |
| 514 | assert!(usage.compressed_bytes.unwrap() < 1024 * 1024); |
| 515 | cancelled = Some(std::time::Instant::now()); |
| 516 | return Some(vec![Message::Cancel]); |
| 517 | } |
| 518 | } |
| 519 | if matches!(event, Message::CancelAck { .. }) { |
| 520 | assert!(cancelled.unwrap().elapsed() < Duration::from_secs(2)); |
| 521 | } |
| 522 | approve(event) |
| 523 | }); |
| 524 | assert!(ratio); |
| 525 | assert_eq!(terminal(&events).outcome, Outcome::Cancelled, "{events:?}"); |
| 526 | assert_eq!(terminal(&events).created_count, 0); |
| 527 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 528 | } |
| 529 | |
| 530 | #[cfg(target_os = "linux")] |
| 531 | #[test] |
| 532 | fn disk_space_and_write_failures_remain_enforced_after_permission() { |
| 533 | for fault in ["space-destination", "codec-full", "full"] { |
| 534 | let s = setup(); |
| 535 | let mut permitted = false; |
| 536 | let events = run_fault(&s, request(&s, 16, 64), Some(fault), |event| { |
| 537 | if matches!(event, Message::ResourceRequired { .. }) { |
| 538 | permitted = true; |
| 539 | } |
| 540 | approve(event) |
| 541 | }); |
| 542 | assert!(permitted); |
| 543 | assert_eq!( |
| 544 | terminal(&events).error, |
| 545 | ErrorCode::DiskFull, |
| 546 | "{fault}: {events:?}" |
| 547 | ); |
| 548 | assert_eq!(terminal(&events).cleanup, Cleanup::Complete, "{events:?}"); |
| 549 | assert!(!s.0.join("output/folder/hello.txt").exists()); |
| 550 | assert!(!fs::read_dir(s.0.join("output")).unwrap().any(|e| { |
| 551 | e.unwrap() |
| 552 | .file_name() |
| 553 | .to_string_lossy() |
| 554 | .starts_with(".zipelo-") |
| 555 | })); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn split_zip_ratio_requires_permission_before_decoding() { |
| 561 | let s = setup(); |
| 562 | generate(&s, "split-zip"); |
| 563 | assert!(fs::metadata(s.0.join("input")).unwrap().len() < 300_000); |
| 564 | let mut start = request(&s, Limits::default().output, Limits::default().output); |
| 565 | start.ceiling.entries = Limits::default().entries; |
| 566 | for mode in 0..3 { |
| 567 | let outcome = if mode == 2 { |
| 568 | Outcome::Completed |
| 569 | } else { |
| 570 | Outcome::Cancelled |
| 571 | }; |
| 572 | let mut asked = false; |
| 573 | let events = run(&s, start.clone(), |event| { |
| 574 | if let Message::ResourceRequired { |
| 575 | challenge_id, |
| 576 | scope, |
| 577 | usage, |
| 578 | .. |
| 579 | } = event |
| 580 | { |
| 581 | assert!(!asked); |
| 582 | asked = true; |
| 583 | assert_eq!( |
| 584 | *scope, |
| 585 | ResourceScope { |
| 586 | ratio: true, |
| 587 | ..ResourceScope::default() |
| 588 | } |
| 589 | ); |
| 590 | assert_eq!(usage.estimated_bytes, Some(258 * 1024 * 1024)); |
| 591 | assert_eq!(usage.ratio_output_bytes, usage.estimated_bytes); |
| 592 | assert_eq!(usage.actual_bytes, None); |
| 593 | assert!(usage.compressed_bytes.unwrap() < 300_000); |
| 594 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 595 | if mode == 0 { |
| 596 | return Some(vec![Message::Cancel]); |
| 597 | } |
| 598 | return Some(vec![Message::ResourceReply { |
| 599 | challenge_id: *challenge_id, |
| 600 | scope: *scope, |
| 601 | allow: outcome == Outcome::Completed, |
| 602 | }]); |
| 603 | } |
| 604 | None |
| 605 | }); |
| 606 | assert!(asked); |
| 607 | assert_eq!(terminal(&events).outcome, outcome, "{events:?}"); |
| 608 | assert_eq!( |
| 609 | terminal(&events).created_count, |
| 610 | if outcome == Outcome::Completed { 2 } else { 0 } |
| 611 | ); |
| 612 | } |
| 613 | for name in ["first.bin", "second.bin"] { |
| 614 | let mut file = fs::File::open(s.0.join("output").join(name)).unwrap(); |
| 615 | assert_eq!(file.metadata().unwrap().len(), 129 * 1024 * 1024); |
| 616 | let mut block = [0; 65536]; |
| 617 | loop { |
| 618 | let n = file.read(&mut block).unwrap(); |
| 619 | if n == 0 { |
| 620 | break; |
| 621 | } |
| 622 | assert!(block[..n].iter().all(|b| *b == 0)); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | #[cfg(target_os = "linux")] |
| 628 | fn progress_archive(s: &Scratch, kind: EntryKind, size: u64) { |
| 629 | if kind == EntryKind::Directory { |
| 630 | generate(s, "empty-directories"); |
| 631 | return; |
| 632 | } |
| 633 | let members = (0..36) |
| 634 | .map(|entry_id| archive::Member { |
| 635 | entry: Entry { |
| 636 | entry_id, |
| 637 | name: format!("entry-{entry_id}"), |
| 638 | kind, |
| 639 | size, |
| 640 | compressed_size: size, |
| 641 | encrypted: false, |
| 642 | }, |
| 643 | data: vec![0; size as usize], |
| 644 | }) |
| 645 | .collect::<Vec<_>>(); |
| 646 | let zip = |
| 647 | archive::create_zip(&members, Method::Store, Limits::default(), &mut || Ok(())).unwrap(); |
| 648 | fs::write(s.0.join("input"), zip).unwrap(); |
| 649 | } |
| 650 | |
| 651 | #[cfg(target_os = "linux")] |
| 652 | #[test] |
| 653 | fn completed_empty_files_and_directories_refresh_idle_deadline() { |
| 654 | for (kind, size) in [ |
| 655 | (EntryKind::File, 0), |
| 656 | (EntryKind::File, 1), |
| 657 | (EntryKind::Directory, 0), |
| 658 | ] { |
| 659 | let s = setup(); |
| 660 | progress_archive(&s, kind, size); |
| 661 | let events = run_fault(&s, request(&s, 64, 128), Some("clock-progress"), approve); |
| 662 | assert_eq!( |
| 663 | terminal(&events).outcome, |
| 664 | Outcome::Completed, |
| 665 | "{kind:?}: {events:?}" |
| 666 | ); |
| 667 | assert_eq!(terminal(&events).created_count, 36); |
| 668 | assert_eq!(terminal(&events).durability, Durability::Synced); |
| 669 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 36); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | #[cfg(target_os = "linux")] |
| 674 | #[test] |
| 675 | fn publication_still_stops_after_sixty_seconds_without_progress() { |
| 676 | let s = setup(); |
| 677 | progress_archive(&s, EntryKind::File, 0); |
| 678 | let events = run_fault(&s, request(&s, 64, 128), Some("clock-stall"), approve); |
| 679 | assert_eq!(terminal(&events).outcome, Outcome::Failed, "{events:?}"); |
| 680 | assert_eq!(terminal(&events).error, ErrorCode::Limit); |
| 681 | assert_eq!(terminal(&events).created_count, 0); |
| 682 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 683 | } |