| 1 | use super::*; |
| 2 | use sha2::{Digest, Sha256}; |
| 3 | use std::{ |
| 4 | io::Read, |
| 5 | process::{Child, ChildStdin}, |
| 6 | sync::mpsc, |
| 7 | time::{Duration, Instant}, |
| 8 | }; |
| 9 | |
| 10 | struct Running(Child); |
| 11 | impl Drop for Running { |
| 12 | fn drop(&mut self) { |
| 13 | let _ = self.0.kill(); |
| 14 | let _ = self.0.wait(); |
| 15 | } |
| 16 | } |
| 17 | fn directory(s: &Scratch, name: &str) { |
| 18 | let path = s.0.join(name); |
| 19 | fs::create_dir_all(&path).unwrap(); |
| 20 | #[cfg(unix)] |
| 21 | { |
| 22 | use std::os::unix::fs::PermissionsExt; |
| 23 | let mut current = path.as_path(); |
| 24 | while current != s.0 { |
| 25 | fs::set_permissions(current, fs::Permissions::from_mode(0o700)).unwrap(); |
| 26 | current = current.parent().unwrap(); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | fn request(s: &Scratch) -> Start { |
| 31 | Start { |
| 32 | action: Action::Extract { |
| 33 | archive: s.path("input"), |
| 34 | destination: s.path("output"), |
| 35 | selection: Selection::All, |
| 36 | list_id: None, |
| 37 | gzip_output_name: None, |
| 38 | }, |
| 39 | limits: Limits::default(), |
| 40 | } |
| 41 | } |
| 42 | fn fixture(s: &Scratch, names: &[&str]) { |
| 43 | let members = names |
| 44 | .iter() |
| 45 | .enumerate() |
| 46 | .map(|(id, name)| archive::Member { |
| 47 | entry: Entry { |
| 48 | entry_id: id as u32, |
| 49 | name: (*name).into(), |
| 50 | kind: EntryKind::File, |
| 51 | size: PAYLOAD.len() as u64, |
| 52 | compressed_size: PAYLOAD.len() as u64, |
| 53 | encrypted: false, |
| 54 | }, |
| 55 | data: PAYLOAD.to_vec(), |
| 56 | }) |
| 57 | .collect::<Vec<_>>(); |
| 58 | let zip = |
| 59 | archive::create_zip(&members, Method::Store, Limits::default(), &mut || Ok(())).unwrap(); |
| 60 | fs::write(s.0.join("input"), zip).unwrap(); |
| 61 | directory(s, "output"); |
| 62 | #[cfg(unix)] |
| 63 | { |
| 64 | use std::os::unix::fs::PermissionsExt; |
| 65 | fs::set_permissions(s.0.join("output"), fs::Permissions::from_mode(0o700)).unwrap(); |
| 66 | } |
| 67 | for name in names { |
| 68 | let path = s.0.join("output").join(name); |
| 69 | directory( |
| 70 | s, |
| 71 | path.parent() |
| 72 | .unwrap() |
| 73 | .strip_prefix(&s.0) |
| 74 | .unwrap() |
| 75 | .to_str() |
| 76 | .unwrap(), |
| 77 | ); |
| 78 | fs::write(path, b"original").unwrap(); |
| 79 | } |
| 80 | } |
| 81 | fn reply(id: u64, choice: ConflictChoice, apply: bool) -> Message { |
| 82 | Message::ConflictReply { |
| 83 | challenge_id: id, |
| 84 | choice, |
| 85 | apply_to_remaining: apply, |
| 86 | } |
| 87 | } |
| 88 | fn run( |
| 89 | s: &Scratch, |
| 90 | start: Start, |
| 91 | fault: Option<&str>, |
| 92 | initial: Vec<Message>, |
| 93 | mut respond: impl FnMut(&Message) -> Option<Vec<Message>>, |
| 94 | ) -> Vec<Message> { |
| 95 | let mut command = s.worker(); |
| 96 | command |
| 97 | .stdin(Stdio::piped()) |
| 98 | .stdout(Stdio::piped()) |
| 99 | .stderr(Stdio::piped()); |
| 100 | #[cfg(target_os = "linux")] |
| 101 | if let Some(fault) = fault { |
| 102 | let shim = s.0.join("io_fault.so"); |
| 103 | assert!( |
| 104 | Command::new("cc") |
| 105 | .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) |
| 106 | .arg( |
| 107 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 108 | .join("../../tests/rust-archive-worker/io_fault.c") |
| 109 | ) |
| 110 | .arg("-ldl") |
| 111 | .arg("-o") |
| 112 | .arg(&shim) |
| 113 | .status() |
| 114 | .unwrap() |
| 115 | .success() |
| 116 | ); |
| 117 | command |
| 118 | .env("LD_PRELOAD", shim) |
| 119 | .env("ZIPELO_TEST_FAULT", fault) |
| 120 | .env("ZIPELO_TEST_MARKER", s.0.join("paused")) |
| 121 | .env("ZIPELO_TEST_RELEASE", s.0.join("release")); |
| 122 | } |
| 123 | #[cfg(not(target_os = "linux"))] |
| 124 | assert!(fault.is_none()); |
| 125 | let mut child = Running(command.spawn().unwrap()); |
| 126 | let mut writer: Option<Writer<ChildStdin>> = |
| 127 | Some(Writer::new(child.0.stdin.take().unwrap(), 17)); |
| 128 | writer |
| 129 | .as_mut() |
| 130 | .unwrap() |
| 131 | .send(&Message::StartInteractive(start)) |
| 132 | .unwrap(); |
| 133 | for message in initial { |
| 134 | writer.as_mut().unwrap().send(&message).unwrap(); |
| 135 | } |
| 136 | let stdout = child.0.stdout.take().unwrap(); |
| 137 | let mut stderr = child.0.stderr.take().unwrap(); |
| 138 | let errors = std::thread::spawn(move || { |
| 139 | let mut bytes = Vec::new(); |
| 140 | stderr.read_to_end(&mut bytes).unwrap(); |
| 141 | bytes |
| 142 | }); |
| 143 | let (send, receive) = mpsc::channel(); |
| 144 | let reader = std::thread::spawn(move || { |
| 145 | let mut reader = Reader::new(stdout); |
| 146 | loop { |
| 147 | let frame = reader.read(); |
| 148 | let done = !matches!(frame, Ok(Some(_))); |
| 149 | if send.send(frame).is_err() || done { |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | }); |
| 154 | let mut events = Vec::new(); |
| 155 | loop { |
| 156 | let Some((job, _, event)) = receive |
| 157 | .recv_timeout(Duration::from_secs(15)) |
| 158 | .expect("worker response timeout") |
| 159 | .unwrap() |
| 160 | else { |
| 161 | break; |
| 162 | }; |
| 163 | assert_eq!(job, 17); |
| 164 | if let Some(commands) = respond(&event) { |
| 165 | if commands.is_empty() { |
| 166 | writer = None; |
| 167 | } else { |
| 168 | for command in commands { |
| 169 | writer.as_mut().unwrap().send(&command).unwrap(); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | let done = matches!(event, Message::Terminal(_)); |
| 174 | events.push(event); |
| 175 | if done { |
| 176 | writer = None; |
| 177 | } |
| 178 | } |
| 179 | assert!(child.0.wait().unwrap().success(), "{events:?}"); |
| 180 | reader.join().unwrap(); |
| 181 | assert!(errors.join().unwrap().is_empty()); |
| 182 | assert!(matches!(events.last(), Some(Message::Terminal(_)))); |
| 183 | events |
| 184 | } |
| 185 | fn choose(s: &Scratch, choice: ConflictChoice, apply: bool) -> Vec<Message> { |
| 186 | run(s, request(s), None, vec![], |event| match event { |
| 187 | Message::ConflictRequired { challenge_id, .. } => { |
| 188 | Some(vec![reply(*challenge_id, choice, apply)]) |
| 189 | } |
| 190 | _ => None, |
| 191 | }) |
| 192 | } |
| 193 | fn count(events: &[Message], predicate: impl Fn(&Message) -> bool) -> usize { |
| 194 | events.iter().filter(|event| predicate(event)).count() |
| 195 | } |
| 196 | fn questions(events: &[Message]) -> usize { |
| 197 | count(events, |e| matches!(e, Message::ConflictRequired { .. })) |
| 198 | } |
| 199 | fn created(events: &[Message]) -> usize { |
| 200 | count(events, |e| { |
| 201 | matches!( |
| 202 | e, |
| 203 | Message::Output { |
| 204 | disposition: Disposition::Created, |
| 205 | .. |
| 206 | } |
| 207 | ) |
| 208 | }) |
| 209 | } |
| 210 | fn hash(path: &std::path::Path) -> Vec<u8> { |
| 211 | Sha256::digest(fs::read(path).unwrap()).to_vec() |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn three_choices_and_apply_remaining_report_every_entry() { |
| 216 | for choice in [ |
| 217 | ConflictChoice::Overwrite, |
| 218 | ConflictChoice::Skip, |
| 219 | ConflictChoice::Rename, |
| 220 | ] { |
| 221 | for apply in [false, true] { |
| 222 | let s = Scratch::new(); |
| 223 | fixture(&s, &["a.txt", "b.txt"]); |
| 224 | let before = hash(&s.0.join("output/a.txt")); |
| 225 | let events = choose(&s, choice, apply); |
| 226 | let t = terminal(&events); |
| 227 | assert_eq!(t.outcome, Outcome::Completed, "{events:?}"); |
| 228 | assert_eq!(questions(&events), if apply { 1 } else { 2 }); |
| 229 | assert_eq!( |
| 230 | count(&events, |e| matches!(e, Message::ConflictResolved { .. })), |
| 231 | 2 |
| 232 | ); |
| 233 | for name in ["a", "b"] { |
| 234 | let original = s.0.join(format!("output/{name}.txt")); |
| 235 | if choice == ConflictChoice::Overwrite { |
| 236 | assert_eq!(fs::read(original).unwrap(), PAYLOAD); |
| 237 | } else { |
| 238 | assert_eq!(hash(&original), before); |
| 239 | } |
| 240 | if choice == ConflictChoice::Rename { |
| 241 | assert_eq!( |
| 242 | fs::read(s.0.join(format!("output/{name} (1).txt"))).unwrap(), |
| 243 | PAYLOAD |
| 244 | ); |
| 245 | } |
| 246 | } |
| 247 | let skipped = choice == ConflictChoice::Skip; |
| 248 | assert_eq!(t.created_count, if skipped { 0 } else { 2 }); |
| 249 | assert_eq!(created(&events), t.created_count as usize); |
| 250 | assert_eq!( |
| 251 | t.publication, |
| 252 | if skipped { |
| 253 | Publication::None |
| 254 | } else { |
| 255 | Publication::Complete |
| 256 | } |
| 257 | ); |
| 258 | assert_eq!(t.cleanup, Cleanup::Complete); |
| 259 | if skipped { |
| 260 | assert_eq!(t.durability, Durability::NotApplicable); |
| 261 | } |
| 262 | assert_eq!( |
| 263 | fs::read_dir(s.0.join("output")).unwrap().count(), |
| 264 | if choice == ConflictChoice::Rename { |
| 265 | 4 |
| 266 | } else { |
| 267 | 2 |
| 268 | } |
| 269 | ); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | #[test] |
| 274 | fn rename_avoids_existing_names_and_later_archive_paths() { |
| 275 | let s = Scratch::new(); |
| 276 | fixture(&s, &["폴더/자료.txt", "폴더/자료 (1).txt"]); |
| 277 | fs::write(s.0.join("output/폴더/자료 (2).txt"), b"keep").unwrap(); |
| 278 | let events = choose(&s, ConflictChoice::Rename, true); |
| 279 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 280 | assert_eq!( |
| 281 | fs::read(s.0.join("output/폴더/자료 (3).txt")).unwrap(), |
| 282 | PAYLOAD |
| 283 | ); |
| 284 | assert_eq!( |
| 285 | fs::read(s.0.join("output/폴더/자료 (1) (1).txt")).unwrap(), |
| 286 | PAYLOAD |
| 287 | ); |
| 288 | assert_eq!( |
| 289 | fs::read(s.0.join("output/폴더/자료 (2).txt")).unwrap(), |
| 290 | b"keep" |
| 291 | ); |
| 292 | } |
| 293 | #[test] |
| 294 | fn mixed_choices_and_non_conflicting_files() { |
| 295 | let s = Scratch::new(); |
| 296 | fixture(&s, &["a", "b", "c", "new"]); |
| 297 | fs::remove_file(s.0.join("output/new")).unwrap(); |
| 298 | let mut index = 0; |
| 299 | let events = run(&s, request(&s), None, vec![], |event| match event { |
| 300 | Message::ConflictRequired { |
| 301 | challenge_id, |
| 302 | incoming_size, |
| 303 | existing_size, |
| 304 | .. |
| 305 | } => { |
| 306 | assert_eq!(*incoming_size, PAYLOAD.len() as u64); |
| 307 | assert_eq!(*existing_size, 8); |
| 308 | let choice = [ |
| 309 | ConflictChoice::Overwrite, |
| 310 | ConflictChoice::Skip, |
| 311 | ConflictChoice::Rename, |
| 312 | ][index]; |
| 313 | index += 1; |
| 314 | Some(vec![reply(*challenge_id, choice, false)]) |
| 315 | } |
| 316 | _ => None, |
| 317 | }); |
| 318 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 319 | assert_eq!(terminal(&events).created_count, 3); |
| 320 | assert_eq!(fs::read(s.0.join("output/a")).unwrap(), PAYLOAD); |
| 321 | assert_eq!(fs::read(s.0.join("output/b")).unwrap(), b"original"); |
| 322 | assert_eq!(fs::read(s.0.join("output/c (1)")).unwrap(), PAYLOAD); |
| 323 | assert_eq!(fs::read(s.0.join("output/new")).unwrap(), PAYLOAD); |
| 324 | } |
| 325 | #[test] |
| 326 | fn cancel_waiting_for_choice_preserves_originals() { |
| 327 | let s = Scratch::new(); |
| 328 | fixture(&s, &["a"]); |
| 329 | let before = hash(&s.0.join("output/a")); |
| 330 | let events = run(&s, request(&s), None, vec![], |e| { |
| 331 | matches!(e, Message::ConflictRequired { .. }).then(|| vec![Message::Cancel]) |
| 332 | }); |
| 333 | assert_eq!(terminal(&events).outcome, Outcome::Cancelled, "{events:?}"); |
| 334 | assert_eq!(terminal(&events).publication, Publication::None); |
| 335 | assert_eq!(created(&events), 0); |
| 336 | assert_eq!(hash(&s.0.join("output/a")), before); |
| 337 | assert!(events.iter().any(|e| matches!( |
| 338 | e, |
| 339 | Message::CancelAck { |
| 340 | status: CancelStatus::Requested, |
| 341 | .. |
| 342 | } |
| 343 | ))); |
| 344 | } |
| 345 | #[test] |
| 346 | fn cancellation_keeps_earlier_replacement() { |
| 347 | let s = Scratch::new(); |
| 348 | fixture(&s, &["a", "b"]); |
| 349 | let events = run(&s, request(&s), None, vec![], |e| match e { |
| 350 | Message::ConflictRequired { |
| 351 | challenge_id, |
| 352 | entry_id: 0, |
| 353 | .. |
| 354 | } => Some(vec![reply(*challenge_id, ConflictChoice::Overwrite, false)]), |
| 355 | Message::ConflictRequired { .. } => Some(vec![Message::Cancel]), |
| 356 | _ => None, |
| 357 | }); |
| 358 | assert_eq!(terminal(&events).outcome, Outcome::Cancelled, "{events:?}"); |
| 359 | assert_eq!(terminal(&events).publication, Publication::Partial); |
| 360 | assert_eq!(terminal(&events).created_count, 1); |
| 361 | assert_eq!(fs::read(s.0.join("output/a")).unwrap(), PAYLOAD); |
| 362 | assert_eq!(fs::read(s.0.join("output/b")).unwrap(), b"original"); |
| 363 | } |
| 364 | #[test] |
| 365 | fn eof_stale_duplicate_and_wrong_kind_replies_fail_protocol() { |
| 366 | for mode in 0..4 { |
| 367 | let s = Scratch::new(); |
| 368 | fixture(&s, &["a", "b"]); |
| 369 | let events = run(&s, request(&s), None, vec![], |e| match e { |
| 370 | Message::ConflictRequired { challenge_id, .. } => Some(match mode { |
| 371 | 0 => vec![], |
| 372 | 1 => vec![reply(challenge_id + 1, ConflictChoice::Skip, false)], |
| 373 | 2 => vec![ |
| 374 | reply(*challenge_id, ConflictChoice::Skip, false), |
| 375 | reply(*challenge_id, ConflictChoice::Skip, false), |
| 376 | ], |
| 377 | _ => vec![Message::Password { |
| 378 | challenge_id: *challenge_id, |
| 379 | secret: Secret(vec![]), |
| 380 | }], |
| 381 | }), |
| 382 | _ => None, |
| 383 | }); |
| 384 | assert_eq!( |
| 385 | terminal(&events).error, |
| 386 | ErrorCode::Protocol, |
| 387 | "{mode}: {events:?}" |
| 388 | ); |
| 389 | assert_eq!(created(&events), 0); |
| 390 | } |
| 391 | } |
| 392 | #[test] |
| 393 | fn premature_reply_is_rejected() { |
| 394 | let s = Scratch::new(); |
| 395 | fixture(&s, &["a"]); |
| 396 | let events = run( |
| 397 | &s, |
| 398 | request(&s), |
| 399 | None, |
| 400 | vec![reply(999, ConflictChoice::Skip, true)], |
| 401 | |_| None, |
| 402 | ); |
| 403 | assert_eq!(terminal(&events).error, ErrorCode::Protocol, "{events:?}"); |
| 404 | assert_eq!(created(&events), 0); |
| 405 | } |
| 406 | #[test] |
| 407 | fn password_and_conflict_share_distinct_challenges() { |
| 408 | let s = Scratch::new(); |
| 409 | fs::write(s.0.join("input"), AES_ZIP).unwrap(); |
| 410 | directory(&s, "output"); |
| 411 | fs::write(s.0.join("output/payload.bin"), b"original").unwrap(); |
| 412 | let mut password_id = None; |
| 413 | let events = run(&s, request(&s), None, vec![], |e| match e { |
| 414 | Message::PasswordRequired { challenge_id, .. } => { |
| 415 | password_id = Some(*challenge_id); |
| 416 | Some(password_reply(*challenge_id, TEST_PASSWORD)) |
| 417 | } |
| 418 | Message::ConflictRequired { challenge_id, .. } => { |
| 419 | assert_ne!(Some(*challenge_id), password_id); |
| 420 | Some(vec![reply(*challenge_id, ConflictChoice::Skip, false)]) |
| 421 | } |
| 422 | _ => None, |
| 423 | }); |
| 424 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 425 | assert_eq!(questions(&events), 1); |
| 426 | assert_eq!(created(&events), 0); |
| 427 | } |
| 428 | #[test] |
| 429 | fn legacy_start_still_aborts_and_policy_does_not_cross_jobs() { |
| 430 | let s = Scratch::new(); |
| 431 | fixture(&s, &["a", "b"]); |
| 432 | assert_eq!(terminal(&execute(request(&s))).error, ErrorCode::Conflict); |
| 433 | assert_eq!(questions(&choose(&s, ConflictChoice::Skip, true)), 1); |
| 434 | assert_eq!(questions(&choose(&s, ConflictChoice::Skip, false)), 2); |
| 435 | } |
| 436 | #[test] |
| 437 | fn corruption_is_rejected_before_any_question_or_output() { |
| 438 | let s = Scratch::new(); |
| 439 | fixture(&s, &["a"]); |
| 440 | let mut bytes = fs::read(s.0.join("input")).unwrap(); |
| 441 | let at = bytes |
| 442 | .windows(PAYLOAD.len()) |
| 443 | .position(|b| b == PAYLOAD) |
| 444 | .unwrap(); |
| 445 | bytes[at] ^= 1; |
| 446 | fs::write(s.0.join("input"), bytes).unwrap(); |
| 447 | let events = run(&s, request(&s), None, vec![], |_| None); |
| 448 | assert_eq!(terminal(&events).error, ErrorCode::Integrity, "{events:?}"); |
| 449 | assert_eq!(questions(&events), 0); |
| 450 | assert_eq!(created(&events), 0); |
| 451 | } |
| 452 | #[test] |
| 453 | fn raw_gzip_conflicts_use_explicit_name() { |
| 454 | let s = Scratch::new(); |
| 455 | fixture(&s, &["chosen.txt"]); |
| 456 | fs::write(s.0.join("input"), include_bytes!("fixtures/sample.gz")).unwrap(); |
| 457 | let mut start = request(&s); |
| 458 | if let Action::Extract { |
| 459 | gzip_output_name, .. |
| 460 | } = &mut start.action |
| 461 | { |
| 462 | *gzip_output_name = Some("chosen.txt".into()); |
| 463 | } |
| 464 | let events = run(&s, start, None, vec![], |e| match e { |
| 465 | Message::ConflictRequired { |
| 466 | challenge_id, |
| 467 | relative_name, |
| 468 | .. |
| 469 | } => { |
| 470 | assert_eq!(relative_name, "chosen.txt"); |
| 471 | Some(vec![reply(*challenge_id, ConflictChoice::Overwrite, false)]) |
| 472 | } |
| 473 | _ => None, |
| 474 | }); |
| 475 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 476 | assert_eq!(fs::read(s.0.join("output/chosen.txt")).unwrap(), PAYLOAD); |
| 477 | } |
| 478 | #[test] |
| 479 | fn file_directory_collisions_are_rejected_before_questions() { |
| 480 | for path in ["a", "parent"] { |
| 481 | let s = Scratch::new(); |
| 482 | fixture(&s, &[if path == "a" { "a" } else { "parent/a" }]); |
| 483 | let target = s.0.join("output").join(path); |
| 484 | if path == "a" { |
| 485 | fs::remove_file(&target).unwrap(); |
| 486 | fs::create_dir(&target).unwrap(); |
| 487 | } else { |
| 488 | fs::remove_dir_all(&target).unwrap(); |
| 489 | fs::write(&target, b"keep").unwrap(); |
| 490 | } |
| 491 | let events = run(&s, request(&s), None, vec![], |_| None); |
| 492 | assert_eq!(terminal(&events).error, ErrorCode::UnsafePath, "{events:?}"); |
| 493 | assert_eq!(questions(&events), 0); |
| 494 | assert_eq!(created(&events), 0); |
| 495 | } |
| 496 | } |
| 497 | #[test] |
| 498 | fn newly_created_empty_directory_is_counted_when_files_are_skipped() { |
| 499 | let s = Scratch::new(); |
| 500 | directory(&s, "empty"); |
| 501 | fs::write(s.0.join("source"), PAYLOAD).unwrap(); |
| 502 | let events = execute(Start { |
| 503 | action: Action::CreateZip { |
| 504 | sources: vec![ |
| 505 | Source { |
| 506 | path: s.path("source"), |
| 507 | archive_name: "a".into(), |
| 508 | }, |
| 509 | Source { |
| 510 | path: s.path("empty"), |
| 511 | archive_name: "empty".into(), |
| 512 | }, |
| 513 | ], |
| 514 | output_file: s.path("input"), |
| 515 | method: Method::Store, |
| 516 | encryption: Encryption::None, |
| 517 | }, |
| 518 | limits: Limits::default(), |
| 519 | }); |
| 520 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 521 | directory(&s, "output"); |
| 522 | fs::write(s.0.join("output/a"), b"original").unwrap(); |
| 523 | let events = choose(&s, ConflictChoice::Skip, true); |
| 524 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 525 | assert_eq!(terminal(&events).publication, Publication::Complete); |
| 526 | assert_eq!(terminal(&events).created_count, 1); |
| 527 | assert_eq!(created(&events), 1); |
| 528 | assert!(s.0.join("output/empty").is_dir()); |
| 529 | } |
| 530 | |
| 531 | #[cfg(target_os = "linux")] |
| 532 | #[test] |
| 533 | fn links_and_special_entries_are_not_interactive() { |
| 534 | use std::os::unix::{fs::symlink, net::UnixListener}; |
| 535 | for mode in 0..3 { |
| 536 | let s = Scratch::new(); |
| 537 | fixture(&s, &["a"]); |
| 538 | fs::remove_file(s.0.join("output/a")).unwrap(); |
| 539 | fs::write(s.0.join("outside"), b"keep").unwrap(); |
| 540 | let listener; |
| 541 | match mode { |
| 542 | 0 => symlink(s.0.join("outside"), s.0.join("output/a")).unwrap(), |
| 543 | 1 => symlink(s.0.join("missing"), s.0.join("output/a")).unwrap(), |
| 544 | _ => { |
| 545 | use std::os::fd::AsRawFd; |
| 546 | let directory = fs::File::open(s.0.join("output")).unwrap(); |
| 547 | listener = UnixListener::bind(format!("/proc/self/fd/{}/a", directory.as_raw_fd())) |
| 548 | .unwrap(); |
| 549 | let _ = &listener; |
| 550 | } |
| 551 | } |
| 552 | let events = run(&s, request(&s), None, vec![], |_| None); |
| 553 | assert_eq!(terminal(&events).error, ErrorCode::UnsafePath, "{events:?}"); |
| 554 | assert_eq!(questions(&events), 0); |
| 555 | assert_eq!(fs::read(s.0.join("outside")).unwrap(), b"keep"); |
| 556 | } |
| 557 | } |
| 558 | #[cfg(target_os = "linux")] |
| 559 | #[test] |
| 560 | fn pre_replacement_failures_preserve_original_hash_and_remove_temps() { |
| 561 | for mode in [ |
| 562 | "create-full", |
| 563 | "full", |
| 564 | "sync-full", |
| 565 | "write-io", |
| 566 | "replace-fail", |
| 567 | ] { |
| 568 | let s = Scratch::new(); |
| 569 | fixture(&s, &["a"]); |
| 570 | let before = hash(&s.0.join("output/a")); |
| 571 | let events = run(&s, request(&s), Some(mode), vec![], |e| match e { |
| 572 | Message::ConflictRequired { challenge_id, .. } => { |
| 573 | Some(vec![reply(*challenge_id, ConflictChoice::Overwrite, false)]) |
| 574 | } |
| 575 | _ => None, |
| 576 | }); |
| 577 | assert_eq!( |
| 578 | terminal(&events).outcome, |
| 579 | Outcome::Failed, |
| 580 | "{mode}: {events:?}" |
| 581 | ); |
| 582 | assert_eq!(created(&events), 0); |
| 583 | assert_eq!(hash(&s.0.join("output/a")), before); |
| 584 | assert_eq!(terminal(&events).cleanup, Cleanup::Complete); |
| 585 | assert_eq!(terminal(&events).retained_partial_count, 0); |
| 586 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 1); |
| 587 | } |
| 588 | } |
| 589 | #[cfg(target_os = "linux")] |
| 590 | #[test] |
| 591 | fn changed_target_is_not_replaced() { |
| 592 | let s = Scratch::new(); |
| 593 | fixture(&s, &["a"]); |
| 594 | let events = run(&s, request(&s), None, vec![], |e| match e { |
| 595 | Message::ConflictRequired { challenge_id, .. } => { |
| 596 | fs::write(s.0.join("output/a"), b"changed by user").unwrap(); |
| 597 | Some(vec![reply(*challenge_id, ConflictChoice::Overwrite, false)]) |
| 598 | } |
| 599 | _ => None, |
| 600 | }); |
| 601 | assert_eq!(terminal(&events).error, ErrorCode::Conflict, "{events:?}"); |
| 602 | assert_eq!(created(&events), 0); |
| 603 | assert_eq!(fs::read(s.0.join("output/a")).unwrap(), b"changed by user"); |
| 604 | } |
| 605 | #[cfg(target_os = "linux")] |
| 606 | #[test] |
| 607 | fn cancel_during_temporary_write_preserves_original_hash() { |
| 608 | let s = Scratch::new(); |
| 609 | fixture(&s, &["a"]); |
| 610 | let before = hash(&s.0.join("output/a")); |
| 611 | let mut cancelled = false; |
| 612 | let events = run(&s, request(&s), Some("pause"), vec![], |e| match e { |
| 613 | Message::ConflictRequired { challenge_id, .. } => { |
| 614 | Some(vec![reply(*challenge_id, ConflictChoice::Overwrite, false)]) |
| 615 | } |
| 616 | Message::Progress { |
| 617 | stage: Stage::Write, |
| 618 | .. |
| 619 | } if !cancelled => { |
| 620 | let deadline = Instant::now() + Duration::from_secs(5); |
| 621 | while !s.0.join("paused").exists() { |
| 622 | assert!(Instant::now() < deadline); |
| 623 | std::thread::sleep(Duration::from_millis(5)); |
| 624 | } |
| 625 | cancelled = true; |
| 626 | Some(vec![Message::Cancel]) |
| 627 | } |
| 628 | Message::CancelAck { |
| 629 | status: CancelStatus::Requested, |
| 630 | .. |
| 631 | } => { |
| 632 | fs::write(s.0.join("release"), []).unwrap(); |
| 633 | None |
| 634 | } |
| 635 | _ => None, |
| 636 | }); |
| 637 | assert!(cancelled); |
| 638 | assert_eq!(terminal(&events).outcome, Outcome::Cancelled, "{events:?}"); |
| 639 | assert_eq!(hash(&s.0.join("output/a")), before); |
| 640 | assert_eq!(created(&events), 0); |
| 641 | assert_eq!(terminal(&events).cleanup, Cleanup::Complete); |
| 642 | } |
| 643 | #[cfg(target_os = "linux")] |
| 644 | #[test] |
| 645 | fn post_replacement_sync_error_reports_actual_publication() { |
| 646 | let s = Scratch::new(); |
| 647 | fixture(&s, &["a"]); |
| 648 | let events = run( |
| 649 | &s, |
| 650 | request(&s), |
| 651 | Some("dir-sync-full"), |
| 652 | vec![], |
| 653 | |e| match e { |
| 654 | Message::ConflictRequired { challenge_id, .. } => { |
| 655 | Some(vec![reply(*challenge_id, ConflictChoice::Overwrite, false)]) |
| 656 | } |
| 657 | _ => None, |
| 658 | }, |
| 659 | ); |
| 660 | assert_eq!(terminal(&events).error, ErrorCode::DiskFull, "{events:?}"); |
| 661 | assert_eq!(terminal(&events).publication, Publication::Partial); |
| 662 | assert_eq!(terminal(&events).created_count, 1); |
| 663 | assert_eq!(terminal(&events).durability, Durability::Failed); |
| 664 | assert_eq!(terminal(&events).cleanup, Cleanup::Complete); |
| 665 | assert_eq!(fs::read(s.0.join("output/a")).unwrap(), PAYLOAD); |
| 666 | assert_eq!(created(&events), 1); |
| 667 | } |
| 668 | |
| 669 | #[test] |
| 670 | fn rename_handles_maximum_utf8_filename_length() { |
| 671 | let s = Scratch::new(); |
| 672 | let name = format!("{}ab.txt", "가".repeat(83)); |
| 673 | fixture(&s, &[&name]); |
| 674 | let events = choose(&s, ConflictChoice::Rename, false); |
| 675 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 676 | let renamed = events |
| 677 | .iter() |
| 678 | .find_map(|event| match event { |
| 679 | Message::ConflictResolved { |
| 680 | relative_name, |
| 681 | choice: ConflictChoice::Rename, |
| 682 | .. |
| 683 | } => Some(relative_name), |
| 684 | _ => None, |
| 685 | }) |
| 686 | .unwrap(); |
| 687 | assert!(renamed.len() <= 255); |
| 688 | assert!(renamed.ends_with(" (1).txt")); |
| 689 | assert_eq!(fs::read(s.0.join("output").join(renamed)).unwrap(), PAYLOAD); |
| 690 | assert_eq!( |
| 691 | fs::read(s.0.join("output").join(name)).unwrap(), |
| 692 | b"original" |
| 693 | ); |
| 694 | } |
| 695 | #[cfg(target_os = "linux")] |
| 696 | #[test] |
| 697 | fn overwrite_does_not_modify_other_hard_links() { |
| 698 | let s = Scratch::new(); |
| 699 | fixture(&s, &["a"]); |
| 700 | fs::hard_link(s.0.join("output/a"), s.0.join("outside")).unwrap(); |
| 701 | let events = choose(&s, ConflictChoice::Overwrite, false); |
| 702 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 703 | assert_eq!(fs::read(s.0.join("output/a")).unwrap(), PAYLOAD); |
| 704 | assert_eq!(fs::read(s.0.join("outside")).unwrap(), b"original"); |
| 705 | } |
| 706 | |
| 707 | #[cfg(target_os = "linux")] |
| 708 | #[test] |
| 709 | fn skip_all_ignores_destination_space_for_zip_tar_and_gzip() { |
| 710 | for format in ["zip", "tar", "gzip"] { |
| 711 | for apply in [false, true] { |
| 712 | let s = Scratch::new(); |
| 713 | let names = match format { |
| 714 | "zip" => vec!["a", "b"], |
| 715 | "tar" => vec!["folder/hello.txt"], |
| 716 | _ => vec!["chosen.txt"], |
| 717 | }; |
| 718 | fixture(&s, &names); |
| 719 | let mut start = request(&s); |
| 720 | if format == "tar" { |
| 721 | fs::write(s.0.join("input"), include_bytes!("fixtures/sample.tar")).unwrap(); |
| 722 | } else if format == "gzip" { |
| 723 | fs::write(s.0.join("input"), include_bytes!("fixtures/sample.gz")).unwrap(); |
| 724 | if let Action::Extract { |
| 725 | gzip_output_name, .. |
| 726 | } = &mut start.action |
| 727 | { |
| 728 | *gzip_output_name = Some("chosen.txt".into()); |
| 729 | } |
| 730 | } |
| 731 | let before = names |
| 732 | .iter() |
| 733 | .map(|name| hash(&s.0.join("output").join(name))) |
| 734 | .collect::<Vec<_>>(); |
| 735 | let events = run( |
| 736 | &s, |
| 737 | start, |
| 738 | Some("space-destination"), |
| 739 | vec![], |
| 740 | |event| match event { |
| 741 | Message::ConflictRequired { challenge_id, .. } => { |
| 742 | Some(vec![reply(*challenge_id, ConflictChoice::Skip, apply)]) |
| 743 | } |
| 744 | _ => None, |
| 745 | }, |
| 746 | ); |
| 747 | assert_eq!( |
| 748 | terminal(&events).outcome, |
| 749 | Outcome::Completed, |
| 750 | "{format}/{apply}: {events:?}" |
| 751 | ); |
| 752 | assert_eq!(terminal(&events).error, ErrorCode::None); |
| 753 | assert_eq!(terminal(&events).publication, Publication::None); |
| 754 | assert_eq!(terminal(&events).created_count, 0); |
| 755 | assert_eq!(terminal(&events).durability, Durability::NotApplicable); |
| 756 | assert_eq!(terminal(&events).cleanup, Cleanup::Complete); |
| 757 | assert_eq!(created(&events), 0); |
| 758 | let conflicts = if format == "zip" { 2 } else { 1 }; |
| 759 | assert_eq!(questions(&events), if apply { 1 } else { conflicts }); |
| 760 | assert_eq!( |
| 761 | count(&events, |event| matches!( |
| 762 | event, |
| 763 | Message::ConflictResolved { |
| 764 | choice: ConflictChoice::Skip, |
| 765 | .. |
| 766 | } |
| 767 | )), |
| 768 | conflicts |
| 769 | ); |
| 770 | for (name, before) in names.iter().zip(before) { |
| 771 | assert_eq!(hash(&s.0.join("output").join(name)), before); |
| 772 | } |
| 773 | if format == "gzip" { |
| 774 | assert_eq!( |
| 775 | fs::read(s.0.join("output/chosen.txt")).unwrap(), |
| 776 | b"original" |
| 777 | ); |
| 778 | } |
| 779 | assert!( |
| 780 | !s.0.join("paused").exists(), |
| 781 | "skipped files must not charge destination space" |
| 782 | ); |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | #[cfg(target_os = "linux")] |
| 787 | #[test] |
| 788 | fn destination_space_is_checked_after_overwrite_or_rename_choice() { |
| 789 | for choice in [ConflictChoice::Overwrite, ConflictChoice::Rename] { |
| 790 | let s = Scratch::new(); |
| 791 | fixture(&s, &["a", "b"]); |
| 792 | let before = hash(&s.0.join("output/a")); |
| 793 | let events = run( |
| 794 | &s, |
| 795 | request(&s), |
| 796 | Some("space-destination"), |
| 797 | vec![], |
| 798 | |event| match event { |
| 799 | Message::ConflictRequired { challenge_id, .. } => { |
| 800 | Some(vec![reply(*challenge_id, choice, true)]) |
| 801 | } |
| 802 | _ => None, |
| 803 | }, |
| 804 | ); |
| 805 | assert_eq!(questions(&events), 1, "{events:?}"); |
| 806 | assert_eq!( |
| 807 | count(&events, |event| matches!( |
| 808 | event, |
| 809 | Message::ConflictResolved { .. } |
| 810 | )), |
| 811 | 1 |
| 812 | ); |
| 813 | assert_eq!(terminal(&events).error, ErrorCode::DiskFull, "{events:?}"); |
| 814 | assert_eq!(terminal(&events).publication, Publication::None); |
| 815 | assert_eq!(terminal(&events).created_count, 0); |
| 816 | assert_eq!(terminal(&events).cleanup, Cleanup::Complete); |
| 817 | assert_eq!(hash(&s.0.join("output/a")), before); |
| 818 | assert_eq!(hash(&s.0.join("output/b")), before); |
| 819 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 2); |
| 820 | assert!(s.0.join("paused").exists()); |
| 821 | } |
| 822 | } |
| 823 | #[cfg(target_os = "linux")] |
| 824 | #[test] |
| 825 | fn skipped_files_do_not_charge_space_for_remaining_publication() { |
| 826 | for choice in [ |
| 827 | Some(ConflictChoice::Overwrite), |
| 828 | Some(ConflictChoice::Rename), |
| 829 | None, |
| 830 | ] { |
| 831 | let s = Scratch::new(); |
| 832 | fixture(&s, &["a", "b", "c"]); |
| 833 | assert!(PAYLOAD.len() <= 64 && PAYLOAD.len() * 3 > 64); |
| 834 | if choice.is_none() { |
| 835 | fs::remove_file(s.0.join("output/c")).unwrap(); |
| 836 | } |
| 837 | let events = run( |
| 838 | &s, |
| 839 | request(&s), |
| 840 | Some("space-one-output"), |
| 841 | vec![], |
| 842 | |event| match event { |
| 843 | Message::ConflictRequired { |
| 844 | challenge_id, |
| 845 | entry_id, |
| 846 | .. |
| 847 | } => { |
| 848 | let choice = if *entry_id < 2 { |
| 849 | ConflictChoice::Skip |
| 850 | } else { |
| 851 | choice.unwrap() |
| 852 | }; |
| 853 | Some(vec![reply(*challenge_id, choice, false)]) |
| 854 | } |
| 855 | _ => None, |
| 856 | }, |
| 857 | ); |
| 858 | assert_eq!( |
| 859 | terminal(&events).outcome, |
| 860 | Outcome::Completed, |
| 861 | "{choice:?}: {events:?}" |
| 862 | ); |
| 863 | assert_eq!(terminal(&events).publication, Publication::Complete); |
| 864 | assert_eq!(terminal(&events).created_count, 1); |
| 865 | assert_eq!(created(&events), 1); |
| 866 | assert_eq!(questions(&events), if choice.is_some() { 3 } else { 2 }); |
| 867 | assert_eq!(fs::read(s.0.join("output/a")).unwrap(), b"original"); |
| 868 | assert_eq!(fs::read(s.0.join("output/b")).unwrap(), b"original"); |
| 869 | let output = if choice == Some(ConflictChoice::Rename) { |
| 870 | "c (1)" |
| 871 | } else { |
| 872 | "c" |
| 873 | }; |
| 874 | assert_eq!(fs::read(s.0.join("output").join(output)).unwrap(), PAYLOAD); |
| 875 | if choice == Some(ConflictChoice::Rename) { |
| 876 | assert_eq!(fs::read(s.0.join("output/c")).unwrap(), b"original"); |
| 877 | } |
| 878 | assert!(s.0.join("paused").exists()); |
| 879 | } |
| 880 | } |
| 881 | #[cfg(target_os = "linux")] |
| 882 | #[test] |
| 883 | fn insufficient_destination_space_does_not_prevent_cancellation() { |
| 884 | let s = Scratch::new(); |
| 885 | fixture(&s, &["a"]); |
| 886 | let events = run( |
| 887 | &s, |
| 888 | request(&s), |
| 889 | Some("space-destination"), |
| 890 | vec![], |
| 891 | |event| matches!(event, Message::ConflictRequired { .. }).then(|| vec![Message::Cancel]), |
| 892 | ); |
| 893 | assert_eq!(questions(&events), 1, "{events:?}"); |
| 894 | assert_eq!(terminal(&events).outcome, Outcome::Cancelled, "{events:?}"); |
| 895 | assert_eq!(terminal(&events).publication, Publication::None); |
| 896 | assert_eq!(fs::read(s.0.join("output/a")).unwrap(), b"original"); |
| 897 | } |
| 898 | #[cfg(target_os = "linux")] |
| 899 | #[test] |
| 900 | fn interactive_staging_space_and_integrity_checks_remain_before_questions() { |
| 901 | for corrupt in [false, true] { |
| 902 | let s = Scratch::new(); |
| 903 | fixture(&s, &["a"]); |
| 904 | if corrupt { |
| 905 | let mut bytes = fs::read(s.0.join("input")).unwrap(); |
| 906 | let at = bytes |
| 907 | .windows(PAYLOAD.len()) |
| 908 | .position(|bytes| bytes == PAYLOAD) |
| 909 | .unwrap(); |
| 910 | bytes[at] ^= 1; |
| 911 | fs::write(s.0.join("input"), bytes).unwrap(); |
| 912 | } |
| 913 | let fault = if corrupt { |
| 914 | "space-destination" |
| 915 | } else { |
| 916 | "space-stage" |
| 917 | }; |
| 918 | let events = run(&s, request(&s), Some(fault), vec![], |_| None); |
| 919 | assert_eq!( |
| 920 | terminal(&events).error, |
| 921 | if corrupt { |
| 922 | ErrorCode::Integrity |
| 923 | } else { |
| 924 | ErrorCode::DiskFull |
| 925 | }, |
| 926 | "{events:?}" |
| 927 | ); |
| 928 | assert_eq!(questions(&events), 0); |
| 929 | assert_eq!(created(&events), 0); |
| 930 | assert_eq!(terminal(&events).publication, Publication::None); |
| 931 | assert_eq!(fs::read(s.0.join("output/a")).unwrap(), b"original"); |
| 932 | } |
| 933 | } |