| 1 | use super::*; |
| 2 | use std::{io::Read, process::Child, sync::mpsc, time::Duration}; |
| 3 | |
| 4 | fn private_dir(path: &std::path::Path) { |
| 5 | let mut builder = fs::DirBuilder::new(); |
| 6 | #[cfg(unix)] |
| 7 | { |
| 8 | use std::os::unix::fs::DirBuilderExt; |
| 9 | builder.mode(0o700); |
| 10 | } |
| 11 | builder.create(path).unwrap(); |
| 12 | } |
| 13 | struct Case(PathBuf); |
| 14 | impl Case { |
| 15 | fn new() -> Self { |
| 16 | let root = std::env::var_os("TMPDIR") |
| 17 | .map(PathBuf::from) |
| 18 | .unwrap_or_else(|| { |
| 19 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/work/tmp") |
| 20 | }); |
| 21 | fs::create_dir_all(&root).unwrap(); |
| 22 | let path = root.canonicalize().unwrap().join(format!( |
| 23 | "zipelo-7zc-{}-{}", |
| 24 | std::process::id(), |
| 25 | NEXT.fetch_add(1, Ordering::Relaxed) |
| 26 | )); |
| 27 | private_dir(&path); |
| 28 | private_dir(&path.join("tmp")); |
| 29 | Self(path) |
| 30 | } |
| 31 | fn path(&self, name: &str) -> NativePath { |
| 32 | NativePath::from_path(&self.0.join(name)) |
| 33 | } |
| 34 | fn request(&self, names: &[&str]) -> Start { |
| 35 | Start { |
| 36 | action: Action::CreateSevenZip { |
| 37 | sources: names |
| 38 | .iter() |
| 39 | .map(|name| Source { |
| 40 | path: self.path(name), |
| 41 | archive_name: (*name).to_owned(), |
| 42 | }) |
| 43 | .collect(), |
| 44 | output_file: self.path("result.7z"), |
| 45 | method: SevenZipMethod::Lzma2, |
| 46 | solid: false, |
| 47 | encryption: Encryption::None, |
| 48 | volume_size: None, |
| 49 | }, |
| 50 | limits: Limits::default(), |
| 51 | } |
| 52 | } |
| 53 | fn command(&self) -> Command { |
| 54 | let mut cmd = Command::new(env!("CARGO_BIN_EXE_zipelo-archive-worker")); |
| 55 | cmd.env("TMPDIR", self.0.join("tmp")) |
| 56 | .env("TMP", self.0.join("tmp")) |
| 57 | .env("TEMP", self.0.join("tmp")); |
| 58 | cmd |
| 59 | } |
| 60 | fn run(&self, message: Message) -> Vec<Message> { |
| 61 | self.run_with(&mut self.command(), message, |_| Vec::new()) |
| 62 | } |
| 63 | fn run_with( |
| 64 | &self, |
| 65 | command: &mut Command, |
| 66 | message: Message, |
| 67 | mut respond: impl FnMut(&Message) -> Vec<Message>, |
| 68 | ) -> Vec<Message> { |
| 69 | struct Running(Child); |
| 70 | impl Drop for Running { |
| 71 | fn drop(&mut self) { |
| 72 | let _ = self.0.kill(); |
| 73 | let _ = self.0.wait(); |
| 74 | } |
| 75 | } |
| 76 | let mut child = Running( |
| 77 | command |
| 78 | .stdin(Stdio::piped()) |
| 79 | .stdout(Stdio::piped()) |
| 80 | .stderr(Stdio::piped()) |
| 81 | .spawn() |
| 82 | .unwrap(), |
| 83 | ); |
| 84 | let mut writer = Writer::new(child.0.stdin.take().unwrap(), 17); |
| 85 | writer.send(&message).unwrap(); |
| 86 | let stdout = child.0.stdout.take().unwrap(); |
| 87 | let mut stderr = child.0.stderr.take().unwrap(); |
| 88 | let diagnostic = std::thread::spawn(move || { |
| 89 | let mut bytes = Vec::new(); |
| 90 | stderr.read_to_end(&mut bytes).unwrap(); |
| 91 | bytes |
| 92 | }); |
| 93 | let (send, receive) = mpsc::sync_channel(128); |
| 94 | let reader = std::thread::spawn(move || { |
| 95 | let mut reader = Reader::new(stdout); |
| 96 | loop { |
| 97 | let frame = reader.read(); |
| 98 | let end = !matches!(frame, Ok(Some(_))); |
| 99 | if send.send(frame).is_err() || end { |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | }); |
| 104 | let mut events = Vec::new(); |
| 105 | while let Some((job, _, event)) = receive |
| 106 | .recv_timeout(Duration::from_secs(120)) |
| 107 | .expect("worker timeout") |
| 108 | .unwrap() |
| 109 | { |
| 110 | assert_eq!(job, 17); |
| 111 | for reply in respond(&event) { |
| 112 | writer.send(&reply).unwrap(); |
| 113 | } |
| 114 | events.push(event); |
| 115 | } |
| 116 | drop(writer); |
| 117 | reader.join().unwrap(); |
| 118 | assert!(child.0.wait().unwrap().success()); |
| 119 | assert!(diagnostic.join().unwrap().is_empty()); |
| 120 | assert!(matches!(events.last(), Some(Message::Terminal(_)))); |
| 121 | assert_eq!( |
| 122 | events |
| 123 | .iter() |
| 124 | .filter(|e| matches!(e, Message::Terminal(_))) |
| 125 | .count(), |
| 126 | 1 |
| 127 | ); |
| 128 | assert_eq!(fs::read_dir(self.0.join("tmp")).unwrap().count(), 0); |
| 129 | events |
| 130 | } |
| 131 | fn list(&self) -> Vec<Message> { |
| 132 | self.run(Message::Start(Start { |
| 133 | action: Action::List { |
| 134 | archive: self.path("result.7z"), |
| 135 | }, |
| 136 | limits: Limits::default(), |
| 137 | })) |
| 138 | } |
| 139 | fn extract(&self, destination: &str) -> Vec<Message> { |
| 140 | private_dir(&self.0.join(destination)); |
| 141 | self.run(Message::Start(Start { |
| 142 | action: Action::Extract { |
| 143 | archive: self.path("result.7z"), |
| 144 | destination: self.path(destination), |
| 145 | selection: Selection::All, |
| 146 | list_id: None, |
| 147 | gzip_output_name: None, |
| 148 | }, |
| 149 | limits: Limits::default(), |
| 150 | })) |
| 151 | } |
| 152 | } |
| 153 | impl Drop for Case { |
| 154 | fn drop(&mut self) { |
| 155 | fs::remove_dir_all(&self.0).expect("7z 작성 시험 폴더 정리에 실패함"); |
| 156 | } |
| 157 | } |
| 158 | fn success(events: &[Message]) { |
| 159 | let t = terminal(events); |
| 160 | assert_eq!(t.outcome, Outcome::Completed, "{events:?}"); |
| 161 | assert_eq!(t.error, ErrorCode::None); |
| 162 | assert!(events.iter().any(|e| matches!( |
| 163 | e, |
| 164 | Message::Progress { |
| 165 | stage: Stage::Commit, |
| 166 | .. |
| 167 | } |
| 168 | ))); |
| 169 | assert_eq!(t.publication, Publication::Complete); |
| 170 | assert_eq!(t.created_count, 1); |
| 171 | assert_eq!(t.retained_partial_count, 0); |
| 172 | assert_eq!(t.cleanup, Cleanup::Complete); |
| 173 | assert_eq!( |
| 174 | events |
| 175 | .iter() |
| 176 | .filter(|e| matches!(e, Message::Output { .. })) |
| 177 | .count(), |
| 178 | 1 |
| 179 | ); |
| 180 | assert!(events.iter().any(|e| matches!(e, Message::Output { entry_id: None, relative_name, disposition: Disposition::Created } if relative_name == "result.7z"))); |
| 181 | assert!(!events.iter().any(|e| matches!( |
| 182 | e, |
| 183 | Message::PasswordRequired { .. } | Message::ConflictRequired { .. } |
| 184 | ))); |
| 185 | } |
| 186 | fn failure(events: &[Message], error: ErrorCode) { |
| 187 | let t = terminal(events); |
| 188 | assert_eq!(t.error, error, "{events:?}"); |
| 189 | assert_eq!( |
| 190 | t.outcome, |
| 191 | if error == ErrorCode::None { |
| 192 | Outcome::Cancelled |
| 193 | } else { |
| 194 | Outcome::Failed |
| 195 | } |
| 196 | ); |
| 197 | assert_eq!(t.publication, Publication::None); |
| 198 | assert_eq!(t.created_count, 0); |
| 199 | assert_eq!(t.retained_partial_count, 0); |
| 200 | assert_eq!(t.cleanup, Cleanup::Complete); |
| 201 | assert!(!events.iter().any(|e| matches!(e, Message::Output { .. }))); |
| 202 | } |
| 203 | fn tree(root: &std::path::Path) -> std::collections::BTreeMap<String, Option<[u8; 32]>> { |
| 204 | use sha2::{Digest, Sha256}; |
| 205 | fn walk( |
| 206 | root: &std::path::Path, |
| 207 | at: &std::path::Path, |
| 208 | result: &mut std::collections::BTreeMap<String, Option<[u8; 32]>>, |
| 209 | ) { |
| 210 | for entry in fs::read_dir(at).unwrap() { |
| 211 | let path = entry.unwrap().path(); |
| 212 | let name = path |
| 213 | .strip_prefix(root) |
| 214 | .unwrap() |
| 215 | .to_str() |
| 216 | .unwrap() |
| 217 | .replace('\\', "/"); |
| 218 | if path.is_dir() { |
| 219 | result.insert(name, None); |
| 220 | walk(root, &path, result); |
| 221 | } else { |
| 222 | result.insert(name, Some(Sha256::digest(fs::read(path).unwrap()).into())); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | let mut result = std::collections::BTreeMap::new(); |
| 227 | walk(root, root, &mut result); |
| 228 | result |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | fn saving_outside_inside_and_nested_in_source_preserves_the_original_tree() { |
| 233 | for parent in ["", "source", "source/nested"] { |
| 234 | let c = Case::new(); |
| 235 | private_dir(&c.0.join("source")); |
| 236 | private_dir(&c.0.join("source/nested")); |
| 237 | private_dir(&c.0.join("source/nested/empty")); |
| 238 | fs::write(c.0.join("source/한글.txt"), b"original").unwrap(); |
| 239 | fs::write(c.0.join("source/nested/empty.txt"), b"").unwrap(); |
| 240 | let expected = tree(&c.0.join("source")); |
| 241 | let archive = NativePath::from_path(&c.0.join(parent).join("result.7z")); |
| 242 | let mut request = c.request(&["source"]); |
| 243 | if let Action::CreateSevenZip { output_file, .. } = &mut request.action { |
| 244 | *output_file = archive.clone(); |
| 245 | } |
| 246 | success(&c.run(Message::Start(request))); |
| 247 | let listed = c.run(Message::Start(Start { |
| 248 | action: Action::List { |
| 249 | archive: archive.clone(), |
| 250 | }, |
| 251 | limits: Limits::default(), |
| 252 | })); |
| 253 | assert_eq!(terminal(&listed).outcome, Outcome::Completed, "{listed:?}"); |
| 254 | private_dir(&c.0.join("decoded")); |
| 255 | let extracted = c.run(Message::Start(Start { |
| 256 | action: Action::Extract { |
| 257 | archive: archive.clone(), |
| 258 | destination: c.path("decoded"), |
| 259 | selection: Selection::All, |
| 260 | list_id: None, |
| 261 | gzip_output_name: None, |
| 262 | }, |
| 263 | limits: Limits::default(), |
| 264 | })); |
| 265 | assert_eq!( |
| 266 | terminal(&extracted).outcome, |
| 267 | Outcome::Completed, |
| 268 | "{extracted:?}" |
| 269 | ); |
| 270 | assert_eq!(tree(&c.0.join("decoded/source")), expected); |
| 271 | if let Some(tool) = std::env::var_os("ZIPELO_TEST_7ZIP") { |
| 272 | let output = Command::new(tool) |
| 273 | .args(["x", "-y"]) |
| 274 | .arg(archive.to_path().unwrap()) |
| 275 | .arg(format!("-o{}", c.0.join("independent").display())) |
| 276 | .output() |
| 277 | .unwrap(); |
| 278 | assert!(output.status.success(), "{output:?}"); |
| 279 | assert_eq!(tree(&c.0.join("independent/source")), expected); |
| 280 | } |
| 281 | assert!( |
| 282 | !tree(&c.0.join("source")) |
| 283 | .keys() |
| 284 | .any(|name| name.contains(".zipelo-")) |
| 285 | ); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | #[test] |
| 290 | fn mixed_unicode_files_and_empty_bitmap_boundaries_round_trip() { |
| 291 | for count in [7, 8, 9] { |
| 292 | let c = Case::new(); |
| 293 | fs::create_dir_all(c.0.join("원본/폴더/빈 폴더")).unwrap(); |
| 294 | fs::write(c.0.join("원본/가.txt"), "한글 자료\n".repeat(1000)).unwrap(); |
| 295 | fs::write( |
| 296 | c.0.join("원본/폴더/z"), |
| 297 | (0..131_081) |
| 298 | .map(|n| ((n * 73) % 251) as u8) |
| 299 | .collect::<Vec<_>>(), |
| 300 | ) |
| 301 | .unwrap(); |
| 302 | for index in 0..count - 3 { |
| 303 | fs::write(c.0.join(format!("원본/빈-{index}")), b"").unwrap(); |
| 304 | } |
| 305 | fs::write(c.0.join("원본/\u{20000}.txt"), b"UTF-16 surrogate pair").unwrap(); |
| 306 | let mut state = 0x1234_5678u32; |
| 307 | let noise: Vec<u8> = (0..131_081) |
| 308 | .map(|_| { |
| 309 | state ^= state << 13; |
| 310 | state ^= state >> 17; |
| 311 | state ^= state << 5; |
| 312 | state as u8 |
| 313 | }) |
| 314 | .collect(); |
| 315 | fs::write(c.0.join("원본/noise.bin"), noise).unwrap(); |
| 316 | let expected = tree(&c.0.join("원본")); |
| 317 | let events = c.run(Message::Start(c.request(&["원본"]))); |
| 318 | success(&events); |
| 319 | assert!(matches!( |
| 320 | events[0], |
| 321 | Message::Accepted { |
| 322 | action: ActionKind::CreateSevenZip, |
| 323 | encryption: false |
| 324 | } |
| 325 | )); |
| 326 | let list = c.list(); |
| 327 | assert_eq!(terminal(&list).outcome, Outcome::Completed, "{list:?}"); |
| 328 | let extracted = c.extract("decoded"); |
| 329 | assert_eq!( |
| 330 | terminal(&extracted).outcome, |
| 331 | Outcome::Completed, |
| 332 | "{extracted:?}" |
| 333 | ); |
| 334 | assert_eq!(tree(&c.0.join("decoded/원본")), expected); |
| 335 | if let Some(tool) = std::env::var_os("ZIPELO_TEST_7ZIP") { |
| 336 | for operation in ["t", "l"] { |
| 337 | let output = Command::new(&tool) |
| 338 | .args([operation, "-slt"]) |
| 339 | .arg(c.0.join("result.7z")) |
| 340 | .output() |
| 341 | .unwrap(); |
| 342 | assert!( |
| 343 | output.status.success(), |
| 344 | "{}", |
| 345 | String::from_utf8_lossy(&output.stdout) |
| 346 | ); |
| 347 | if operation == "l" { |
| 348 | let text = String::from_utf8_lossy(&output.stdout); |
| 349 | assert!(text.contains("Method = LZMA2:20"), "{text}"); |
| 350 | assert!(text.contains("Solid = -"), "{text}"); |
| 351 | assert!(!text.contains("Encrypted = +")); |
| 352 | } |
| 353 | } |
| 354 | let output = Command::new(tool) |
| 355 | .args(["x", "-y"]) |
| 356 | .arg(c.0.join("result.7z")) |
| 357 | .arg(format!("-o{}", c.0.join("independent").display())) |
| 358 | .output() |
| 359 | .unwrap(); |
| 360 | assert!( |
| 361 | output.status.success(), |
| 362 | "{}", |
| 363 | String::from_utf8_lossy(&output.stdout) |
| 364 | ); |
| 365 | assert_eq!(tree(&c.0.join("independent/원본")), expected); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | #[test] |
| 371 | fn archives_with_only_empty_files_and_directories_are_valid() { |
| 372 | for directory in [false, true] { |
| 373 | let c = Case::new(); |
| 374 | if directory { |
| 375 | fs::create_dir_all(c.0.join("empty/nested")).unwrap(); |
| 376 | } else { |
| 377 | fs::write(c.0.join("empty"), b"").unwrap(); |
| 378 | } |
| 379 | success(&c.run(Message::Start(c.request(&["empty"])))); |
| 380 | let extracted = c.extract("decoded"); |
| 381 | assert_eq!( |
| 382 | terminal(&extracted).outcome, |
| 383 | Outcome::Completed, |
| 384 | "{extracted:?}" |
| 385 | ); |
| 386 | assert_eq!(c.0.join("decoded/empty").is_dir(), directory); |
| 387 | if let Some(tool) = std::env::var_os("ZIPELO_TEST_7ZIP") { |
| 388 | assert!( |
| 389 | Command::new(tool) |
| 390 | .arg("t") |
| 391 | .arg(c.0.join("result.7z")) |
| 392 | .output() |
| 393 | .unwrap() |
| 394 | .status |
| 395 | .success() |
| 396 | ); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | #[test] |
| 402 | fn existing_output_empty_sources_limits_and_links_are_rejected() { |
| 403 | let c = Case::new(); |
| 404 | fs::write(c.0.join("source"), b"abcdef").unwrap(); |
| 405 | fs::write(c.0.join("result.7z"), b"preserve").unwrap(); |
| 406 | failure( |
| 407 | &c.run(Message::Start(c.request(&["source"]))), |
| 408 | ErrorCode::Conflict, |
| 409 | ); |
| 410 | assert_eq!(fs::read(c.0.join("result.7z")).unwrap(), b"preserve"); |
| 411 | fs::remove_file(c.0.join("result.7z")).unwrap(); |
| 412 | failure( |
| 413 | &c.run(Message::Start(c.request(&[]))), |
| 414 | ErrorCode::InvalidRequest, |
| 415 | ); |
| 416 | for bytes in [5, 6, 7] { |
| 417 | let mut start = c.request(&["source"]); |
| 418 | start.limits.input = bytes; |
| 419 | start.limits.output = bytes; |
| 420 | let events = c.run(Message::Start(start)); |
| 421 | if bytes == 5 { |
| 422 | failure(&events, ErrorCode::Limit); |
| 423 | } else { |
| 424 | success(&events); |
| 425 | fs::remove_file(c.0.join("result.7z")).unwrap(); |
| 426 | } |
| 427 | } |
| 428 | #[cfg(unix)] |
| 429 | { |
| 430 | std::os::unix::fs::symlink(c.0.join("source"), c.0.join("link")).unwrap(); |
| 431 | failure( |
| 432 | &c.run(Message::Start(c.request(&["link"]))), |
| 433 | ErrorCode::UnsafePath, |
| 434 | ); |
| 435 | } |
| 436 | assert!(!c.0.join("result.7z").exists()); |
| 437 | } |
| 438 | |
| 439 | #[test] |
| 440 | fn guarded_approval_decline_and_cancel_preserve_creation_rules() { |
| 441 | let c = Case::new(); |
| 442 | fs::write(c.0.join("source"), b"abcdef").unwrap(); |
| 443 | for allow in [false, true] { |
| 444 | let mut start = c.request(&["source"]); |
| 445 | start.limits.output = 5; |
| 446 | let message = Message::StartGuarded(GuardedStart { |
| 447 | ceiling: Limits { |
| 448 | output: 6, |
| 449 | ..start.limits |
| 450 | }, |
| 451 | start, |
| 452 | interactive: false, |
| 453 | }); |
| 454 | let mut questions = 0; |
| 455 | let events = c.run_with(&mut c.command(), message, |event| { |
| 456 | if let Message::ResourceRequired { |
| 457 | challenge_id, |
| 458 | action, |
| 459 | scope, |
| 460 | .. |
| 461 | } = event |
| 462 | { |
| 463 | assert_eq!(*action, ActionKind::CreateSevenZip); |
| 464 | questions += 1; |
| 465 | vec![Message::ResourceReply { |
| 466 | challenge_id: *challenge_id, |
| 467 | scope: *scope, |
| 468 | allow, |
| 469 | }] |
| 470 | } else { |
| 471 | Vec::new() |
| 472 | } |
| 473 | }); |
| 474 | assert_eq!(questions, 1); |
| 475 | if allow { |
| 476 | success(&events); |
| 477 | fs::remove_file(c.0.join("result.7z")).unwrap(); |
| 478 | } else { |
| 479 | failure(&events, ErrorCode::None); |
| 480 | assert!(!c.0.join("result.7z").exists()); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | #[test] |
| 486 | fn cancel_during_snapshot_or_encoding_publishes_nothing() { |
| 487 | for stage in [Stage::Read, Stage::Write] { |
| 488 | let c = Case::new(); |
| 489 | fs::File::create(c.0.join("source")) |
| 490 | .unwrap() |
| 491 | .set_len(128 * 1024 * 1024) |
| 492 | .unwrap(); |
| 493 | let mut sent = false; |
| 494 | let events = c.run_with( |
| 495 | &mut c.command(), |
| 496 | Message::Start(c.request(&["source"])), |
| 497 | |event| { |
| 498 | if !sent |
| 499 | && matches!(event, Message::Progress { stage: actual, processed_bytes, .. } if *actual == stage && (stage == Stage::Read || *processed_bytes > 0)) |
| 500 | { |
| 501 | sent = true; |
| 502 | vec![Message::Cancel] |
| 503 | } else { |
| 504 | Vec::new() |
| 505 | } |
| 506 | }, |
| 507 | ); |
| 508 | assert!(sent); |
| 509 | failure(&events, ErrorCode::None); |
| 510 | assert!(!c.0.join("result.7z").exists()); |
| 511 | assert!(events.iter().any(|e| matches!( |
| 512 | e, |
| 513 | Message::CancelAck { |
| 514 | status: CancelStatus::Requested, |
| 515 | .. |
| 516 | } |
| 517 | ))); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | #[cfg(target_os = "linux")] |
| 522 | fn shim(c: &Case, file: &str) -> PathBuf { |
| 523 | let output = c.0.join("fault.so"); |
| 524 | let status = Command::new("cc") |
| 525 | .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) |
| 526 | .arg( |
| 527 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 528 | .join("../../tests/rust-archive-worker") |
| 529 | .join(file), |
| 530 | ) |
| 531 | .args(["-ldl", "-o"]) |
| 532 | .arg(&output) |
| 533 | .status() |
| 534 | .unwrap(); |
| 535 | assert!(status.success()); |
| 536 | output |
| 537 | } |
| 538 | |
| 539 | #[cfg(target_os = "linux")] |
| 540 | #[test] |
| 541 | fn storage_failures_keep_real_publication_and_cleanup_state() { |
| 542 | for fault in [ |
| 543 | "temp-full", |
| 544 | "space-stage", |
| 545 | "create-full", |
| 546 | "create-denied", |
| 547 | "full", |
| 548 | "write-io", |
| 549 | "sync-full", |
| 550 | "dir-sync-full", |
| 551 | ] { |
| 552 | let c = Case::new(); |
| 553 | fs::write(c.0.join("source"), b"a".repeat(100_000)).unwrap(); |
| 554 | let mut command = c.command(); |
| 555 | command |
| 556 | .env("LD_PRELOAD", shim(&c, "io_fault.c")) |
| 557 | .env("ZIPELO_TEST_FAULT", fault) |
| 558 | .env("ZIPELO_TEST_MARKER", c.0.join("marker")); |
| 559 | let events = c.run_with(&mut command, Message::Start(c.request(&["source"])), |_| { |
| 560 | Vec::new() |
| 561 | }); |
| 562 | let error = if matches!(fault, "create-denied" | "write-io") { |
| 563 | ErrorCode::Io |
| 564 | } else { |
| 565 | ErrorCode::DiskFull |
| 566 | }; |
| 567 | if fault == "dir-sync-full" { |
| 568 | let t = terminal(&events); |
| 569 | assert_eq!(t.error, error, "{events:?}"); |
| 570 | assert_eq!(t.outcome, Outcome::Failed); |
| 571 | assert_eq!(t.created_count, 1); |
| 572 | assert_eq!(t.durability, Durability::Failed); |
| 573 | assert!(c.0.join("result.7z").is_file()); |
| 574 | } else { |
| 575 | failure(&events, error); |
| 576 | assert!(!c.0.join("result.7z").exists()); |
| 577 | } |
| 578 | assert!(!fs::read_dir(&c.0).unwrap().any(|entry| { |
| 579 | entry |
| 580 | .unwrap() |
| 581 | .file_name() |
| 582 | .to_string_lossy() |
| 583 | .starts_with(".zipelo-") |
| 584 | })); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | #[cfg(target_os = "linux")] |
| 589 | #[test] |
| 590 | fn source_changes_new_destination_and_cancel_before_publication_are_preserved() { |
| 591 | for operation in ["source", "destination", "cancel"] { |
| 592 | let c = Case::new(); |
| 593 | fs::write(c.0.join("source"), b"a".repeat(100_000)).unwrap(); |
| 594 | let marker = c.0.join("marker"); |
| 595 | let release = c.0.join("release"); |
| 596 | let mut command = c.command(); |
| 597 | command |
| 598 | .env("LD_PRELOAD", shim(&c, "io_fault.c")) |
| 599 | .env("ZIPELO_TEST_FAULT", "pause") |
| 600 | .env("ZIPELO_TEST_MARKER", &marker) |
| 601 | .env("ZIPELO_TEST_RELEASE", &release); |
| 602 | let mut changed = false; |
| 603 | let events = c.run_with( |
| 604 | &mut command, |
| 605 | Message::Start(c.request(&["source"])), |
| 606 | |event| { |
| 607 | if !changed |
| 608 | && matches!( |
| 609 | event, |
| 610 | Message::Progress { |
| 611 | stage: Stage::Write, |
| 612 | total_bytes: Some(_), |
| 613 | .. |
| 614 | } |
| 615 | ) |
| 616 | { |
| 617 | let until = std::time::Instant::now() + Duration::from_secs(5); |
| 618 | while !marker.exists() { |
| 619 | assert!(std::time::Instant::now() < until); |
| 620 | std::thread::sleep(Duration::from_millis(2)); |
| 621 | } |
| 622 | match operation { |
| 623 | "source" => fs::write(c.0.join("source"), b"changed").unwrap(), |
| 624 | "destination" => fs::write(c.0.join("result.7z"), b"user data").unwrap(), |
| 625 | _ => {} |
| 626 | } |
| 627 | changed = true; |
| 628 | let replies = if operation == "cancel" { |
| 629 | vec![Message::Cancel] |
| 630 | } else { |
| 631 | Vec::new() |
| 632 | }; |
| 633 | fs::write(&release, b"resume").unwrap(); |
| 634 | replies |
| 635 | } else { |
| 636 | Vec::new() |
| 637 | } |
| 638 | }, |
| 639 | ); |
| 640 | assert!(changed); |
| 641 | failure( |
| 642 | &events, |
| 643 | match operation { |
| 644 | "source" => ErrorCode::SourceChanged, |
| 645 | "destination" => ErrorCode::Conflict, |
| 646 | _ => ErrorCode::None, |
| 647 | }, |
| 648 | ); |
| 649 | if operation == "destination" { |
| 650 | assert_eq!(fs::read(c.0.join("result.7z")).unwrap(), b"user data"); |
| 651 | } else { |
| 652 | assert!(!c.0.join("result.7z").exists()); |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | #[cfg(target_os = "linux")] |
| 658 | #[test] |
| 659 | fn internal_destination_still_rejects_real_source_changes_and_cancellation() { |
| 660 | for parent in ["source", "source/nested"] { |
| 661 | for operation in [ |
| 662 | "add", |
| 663 | "foreign-temp", |
| 664 | "remove", |
| 665 | "rename", |
| 666 | "modify", |
| 667 | "replace-dir", |
| 668 | "cancel", |
| 669 | ] { |
| 670 | let c = Case::new(); |
| 671 | private_dir(&c.0.join("source")); |
| 672 | private_dir(&c.0.join("source/nested")); |
| 673 | let directory = c.0.join(parent); |
| 674 | private_dir(&directory.join("empty")); |
| 675 | fs::write(directory.join("file"), b"a".repeat(100_000)).unwrap(); |
| 676 | let mut request = c.request(&["source"]); |
| 677 | if let Action::CreateSevenZip { output_file, .. } = &mut request.action { |
| 678 | *output_file = NativePath::from_path(&directory.join("result.7z")); |
| 679 | } |
| 680 | let marker = c.0.join("marker"); |
| 681 | let release = c.0.join("release"); |
| 682 | let mut command = c.command(); |
| 683 | command |
| 684 | .env("LD_PRELOAD", shim(&c, "io_fault.c")) |
| 685 | .env("ZIPELO_TEST_FAULT", "pause") |
| 686 | .env("ZIPELO_TEST_MARKER", &marker) |
| 687 | .env("ZIPELO_TEST_RELEASE", &release); |
| 688 | let mut changed = false; |
| 689 | let events = c.run_with(&mut command, Message::Start(request), |event| { |
| 690 | if changed |
| 691 | || !matches!( |
| 692 | event, |
| 693 | Message::Progress { |
| 694 | stage: Stage::Write, |
| 695 | total_bytes: Some(_), |
| 696 | .. |
| 697 | } |
| 698 | ) |
| 699 | { |
| 700 | return Vec::new(); |
| 701 | } |
| 702 | let until = std::time::Instant::now() + Duration::from_secs(5); |
| 703 | while !marker.exists() { |
| 704 | assert!(std::time::Instant::now() < until); |
| 705 | std::thread::sleep(Duration::from_millis(2)); |
| 706 | } |
| 707 | match operation { |
| 708 | "add" => fs::write(directory.join("new-file"), b"user addition").unwrap(), |
| 709 | "foreign-temp" => { |
| 710 | fs::write(directory.join(".zipelo-user.tmp"), b"user file").unwrap() |
| 711 | } |
| 712 | "remove" => fs::remove_file(directory.join("file")).unwrap(), |
| 713 | "rename" => { |
| 714 | fs::rename(directory.join("file"), directory.join("renamed")).unwrap() |
| 715 | } |
| 716 | "modify" => fs::write(directory.join("file"), b"user modification").unwrap(), |
| 717 | "replace-dir" => { |
| 718 | fs::rename(directory.join("empty"), c.0.join("old-empty")).unwrap(); |
| 719 | private_dir(&directory.join("empty")); |
| 720 | } |
| 721 | _ => {} |
| 722 | } |
| 723 | changed = true; |
| 724 | fs::write(&release, b"resume").unwrap(); |
| 725 | if operation == "cancel" { |
| 726 | vec![Message::Cancel] |
| 727 | } else { |
| 728 | Vec::new() |
| 729 | } |
| 730 | }); |
| 731 | assert!(changed); |
| 732 | failure( |
| 733 | &events, |
| 734 | if operation == "cancel" { |
| 735 | ErrorCode::None |
| 736 | } else { |
| 737 | ErrorCode::SourceChanged |
| 738 | }, |
| 739 | ); |
| 740 | assert!(!directory.join("result.7z").exists()); |
| 741 | let expected_temp = if operation == "foreign-temp" { 1 } else { 0 }; |
| 742 | assert_eq!( |
| 743 | fs::read_dir(&directory) |
| 744 | .unwrap() |
| 745 | .filter(|entry| { |
| 746 | entry |
| 747 | .as_ref() |
| 748 | .unwrap() |
| 749 | .file_name() |
| 750 | .to_string_lossy() |
| 751 | .starts_with(".zipelo-") |
| 752 | }) |
| 753 | .count(), |
| 754 | expected_temp |
| 755 | ); |
| 756 | if operation == "foreign-temp" { |
| 757 | assert_eq!( |
| 758 | fs::read(directory.join(".zipelo-user.tmp")).unwrap(), |
| 759 | b"user file" |
| 760 | ); |
| 761 | } |
| 762 | if operation == "modify" { |
| 763 | assert_eq!( |
| 764 | fs::read(directory.join("file")).unwrap(), |
| 765 | b"user modification" |
| 766 | ); |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | #[cfg(target_os = "linux")] |
| 773 | #[test] |
| 774 | fn internal_destination_write_failures_clean_only_the_owned_temporary() { |
| 775 | for fault in ["full", "sync-full"] { |
| 776 | let c = Case::new(); |
| 777 | private_dir(&c.0.join("source")); |
| 778 | fs::write(c.0.join("source/file"), b"original").unwrap(); |
| 779 | fs::write(c.0.join("source/.zipelo-user.tmp"), b"keep").unwrap(); |
| 780 | let expected = tree(&c.0.join("source")); |
| 781 | let mut request = c.request(&["source"]); |
| 782 | if let Action::CreateSevenZip { output_file, .. } = &mut request.action { |
| 783 | *output_file = c.path("source/result.7z"); |
| 784 | } |
| 785 | let mut command = c.command(); |
| 786 | command |
| 787 | .env("LD_PRELOAD", shim(&c, "io_fault.c")) |
| 788 | .env("ZIPELO_TEST_FAULT", fault); |
| 789 | failure( |
| 790 | &c.run_with(&mut command, Message::Start(request), |_| Vec::new()), |
| 791 | ErrorCode::DiskFull, |
| 792 | ); |
| 793 | assert_eq!(tree(&c.0.join("source")), expected); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | #[cfg(target_os = "linux")] |
| 798 | #[test] |
| 799 | fn encoder_finish_header_write_seek_and_patch_errors_publish_nothing() { |
| 800 | for fault in [ |
| 801 | "encode-full", |
| 802 | "finish-full", |
| 803 | "header-write", |
| 804 | "header-seek", |
| 805 | "header-patch", |
| 806 | ] { |
| 807 | let c = Case::new(); |
| 808 | fs::write( |
| 809 | c.0.join("source"), |
| 810 | b"x".repeat(if fault == "encode-full" { |
| 811 | 3 * 1024 * 1024 |
| 812 | } else { |
| 813 | 123 |
| 814 | }), |
| 815 | ) |
| 816 | .unwrap(); |
| 817 | let mut command = c.command(); |
| 818 | command |
| 819 | .env("LD_PRELOAD", shim(&c, "seven_zip_create_fault.c")) |
| 820 | .env("ZIPELO_7Z_FAULT", fault); |
| 821 | let events = c.run_with(&mut command, Message::Start(c.request(&["source"])), |_| { |
| 822 | Vec::new() |
| 823 | }); |
| 824 | failure( |
| 825 | &events, |
| 826 | if fault.ends_with("full") { |
| 827 | ErrorCode::DiskFull |
| 828 | } else { |
| 829 | ErrorCode::Io |
| 830 | }, |
| 831 | ); |
| 832 | assert!(!c.0.join("result.7z").exists()); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | #[cfg(target_os = "linux")] |
| 837 | #[test] |
| 838 | fn gigabyte_creation_stays_below_64_mib_peak_rss() { |
| 839 | assert!( |
| 840 | Command::new("python3") |
| 841 | .arg( |
| 842 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 843 | .join("../../tests/rust-archive-worker/seven_zip_create_memory.py") |
| 844 | ) |
| 845 | .arg(env!("CARGO_BIN_EXE_zipelo-archive-worker")) |
| 846 | .status() |
| 847 | .unwrap() |
| 848 | .success() |
| 849 | ); |
| 850 | } |
| 851 | |
| 852 | #[path = "seven_zip_split_create.rs"] |
| 853 | mod split; |