| 1 | use super::*; |
| 2 | use std::os::unix::fs::DirBuilderExt; |
| 3 | |
| 4 | const SOURCE: &[u8] = b"unchanged source"; |
| 5 | const SENTINEL: &[u8] = b"unrelated user file"; |
| 6 | |
| 7 | fn prepare(s: &Scratch, operation: &str) -> (Start, PathBuf) { |
| 8 | fs::write(s.0.join("source"), SOURCE).unwrap(); |
| 9 | fs::DirBuilder::new() |
| 10 | .mode(0o700) |
| 11 | .create(s.0.join("tmp")) |
| 12 | .unwrap(); |
| 13 | let (action, output) = match operation { |
| 14 | "extract" | "extract-nested" => { |
| 15 | let created = execute(Start { |
| 16 | action: Action::CreateZip { |
| 17 | sources: vec![Source { |
| 18 | path: s.path("source"), |
| 19 | archive_name: if operation == "extract-nested" { |
| 20 | "folder/payload" |
| 21 | } else { |
| 22 | "payload" |
| 23 | } |
| 24 | .into(), |
| 25 | }], |
| 26 | output_file: s.path("archive"), |
| 27 | method: Method::Store, |
| 28 | encryption: Encryption::None, |
| 29 | }, |
| 30 | limits: Limits::default(), |
| 31 | }); |
| 32 | assert_eq!(terminal(&created).outcome, Outcome::Completed); |
| 33 | fs::DirBuilder::new() |
| 34 | .mode(0o700) |
| 35 | .create(s.0.join("output")) |
| 36 | .unwrap(); |
| 37 | if operation == "extract-nested" { |
| 38 | fs::DirBuilder::new() |
| 39 | .mode(0o700) |
| 40 | .create(s.0.join("output/folder")) |
| 41 | .unwrap(); |
| 42 | } |
| 43 | ( |
| 44 | Action::Extract { |
| 45 | archive: s.path("archive"), |
| 46 | destination: s.path("output"), |
| 47 | selection: Selection::All, |
| 48 | list_id: None, |
| 49 | gzip_output_name: None, |
| 50 | }, |
| 51 | s.0.join(if operation == "extract-nested" { |
| 52 | "output/folder/payload" |
| 53 | } else { |
| 54 | "output/payload" |
| 55 | }), |
| 56 | ) |
| 57 | } |
| 58 | "seven-zip" => ( |
| 59 | Action::CreateSevenZip { |
| 60 | sources: vec![Source { |
| 61 | path: s.path("source"), |
| 62 | archive_name: "payload".into(), |
| 63 | }], |
| 64 | output_file: s.path("result.7z"), |
| 65 | method: SevenZipMethod::Lzma2, |
| 66 | solid: false, |
| 67 | encryption: Encryption::None, |
| 68 | volume_size: None, |
| 69 | }, |
| 70 | s.0.join("result.7z"), |
| 71 | ), |
| 72 | "recovery" => { |
| 73 | fs::write( |
| 74 | s.0.join("archive"), |
| 75 | include_bytes!("../zip-recovery-fixtures/broken-local-header.zip"), |
| 76 | ) |
| 77 | .unwrap(); |
| 78 | ( |
| 79 | Action::RecoverZip { |
| 80 | archive: s.path("archive"), |
| 81 | output_file: s.path("recovered.zip"), |
| 82 | }, |
| 83 | s.0.join("recovered.zip"), |
| 84 | ) |
| 85 | } |
| 86 | _ => unreachable!(), |
| 87 | }; |
| 88 | ( |
| 89 | Start { |
| 90 | action, |
| 91 | limits: Limits::default(), |
| 92 | }, |
| 93 | output, |
| 94 | ) |
| 95 | } |
| 96 | |
| 97 | fn run(s: &Scratch, start: Start, fault: &str) -> Vec<Message> { |
| 98 | let library = s.0.join("fault.so"); |
| 99 | assert!( |
| 100 | Command::new("cc") |
| 101 | .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) |
| 102 | .arg( |
| 103 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 104 | .join("../../tests/rust-archive-worker/io_fault.c"), |
| 105 | ) |
| 106 | .args(["-ldl", "-o"]) |
| 107 | .arg(&library) |
| 108 | .status() |
| 109 | .unwrap() |
| 110 | .success() |
| 111 | ); |
| 112 | let mut child = s |
| 113 | .worker() |
| 114 | .env("TMPDIR", s.0.join("tmp")) |
| 115 | .env("LD_PRELOAD", library) |
| 116 | .env("ZIPELO_TEST_FAULT", fault) |
| 117 | .env("ZIPELO_TEST_MARKER", s.0.join("marker")) |
| 118 | .stdin(Stdio::piped()) |
| 119 | .stdout(Stdio::piped()) |
| 120 | .stderr(Stdio::piped()) |
| 121 | .spawn() |
| 122 | .unwrap(); |
| 123 | Writer::new(child.stdin.take().unwrap(), 17) |
| 124 | .send(&Message::Start(start)) |
| 125 | .unwrap(); |
| 126 | let output = child.wait_with_output().unwrap(); |
| 127 | assert!(output.status.success()); |
| 128 | assert!(output.stderr.is_empty(), "{:?}", output.stderr); |
| 129 | let mut reader = Reader::new(output.stdout.as_slice()); |
| 130 | let mut events = Vec::new(); |
| 131 | while let Some((_, _, event)) = reader.read().unwrap() { |
| 132 | events.push(event); |
| 133 | } |
| 134 | assert_eq!( |
| 135 | events |
| 136 | .iter() |
| 137 | .filter(|e| matches!(e, Message::Terminal(_))) |
| 138 | .count(), |
| 139 | 1 |
| 140 | ); |
| 141 | assert_eq!(fs::read_dir(s.0.join("tmp")).unwrap().count(), 0); |
| 142 | events |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn storage_failure_matrix_preserves_outputs_and_reports_remaining_files() { |
| 147 | for operation in ["extract", "extract-nested", "seven-zip", "recovery"] { |
| 148 | for fault in [ |
| 149 | "none", |
| 150 | "temp-full", |
| 151 | "create-full", |
| 152 | "full", |
| 153 | "sync-full", |
| 154 | "sync-io", |
| 155 | "dir-sync-full", |
| 156 | "dir-sync-io", |
| 157 | "cleanup-fail", |
| 158 | "full-cleanup-fail", |
| 159 | "sync-cleanup-fail", |
| 160 | ] { |
| 161 | let s = Scratch::new(); |
| 162 | let (request, output) = prepare(&s, operation); |
| 163 | let parent = output.parent().unwrap(); |
| 164 | let root = if operation.starts_with("extract") { |
| 165 | s.0.join("output") |
| 166 | } else { |
| 167 | s.0.clone() |
| 168 | }; |
| 169 | let unrelated = parent.join(".zipelo-unrelated.tmp"); |
| 170 | fs::write(&unrelated, SENTINEL).unwrap(); |
| 171 | let archive = fs::read(s.0.join("archive")).ok(); |
| 172 | let events = run(&s, request, fault); |
| 173 | let end = terminal(&events); |
| 174 | let context = format!("{operation}/{fault}: {events:?}"); |
| 175 | let published = matches!( |
| 176 | fault, |
| 177 | "none" | "cleanup-fail" | "dir-sync-full" | "dir-sync-io" |
| 178 | ); |
| 179 | assert_eq!(output.exists(), published, "{context}"); |
| 180 | assert_eq!(end.created_count, u64::from(published), "{context}"); |
| 181 | assert_eq!( |
| 182 | end.outcome, |
| 183 | if fault == "none" { |
| 184 | Outcome::Completed |
| 185 | } else { |
| 186 | Outcome::Failed |
| 187 | }, |
| 188 | "{context}" |
| 189 | ); |
| 190 | let expected_error = if fault == "none" { |
| 191 | ErrorCode::None |
| 192 | } else if matches!( |
| 193 | fault, |
| 194 | "sync-io" | "dir-sync-io" | "cleanup-fail" | "sync-cleanup-fail" |
| 195 | ) { |
| 196 | ErrorCode::Io |
| 197 | } else { |
| 198 | ErrorCode::DiskFull |
| 199 | }; |
| 200 | assert_eq!(end.error, expected_error, "{context}"); |
| 201 | assert_eq!( |
| 202 | end.publication, |
| 203 | if fault == "none" { |
| 204 | Publication::Complete |
| 205 | } else if published { |
| 206 | Publication::Partial |
| 207 | } else { |
| 208 | Publication::None |
| 209 | }, |
| 210 | "{context}" |
| 211 | ); |
| 212 | assert_eq!( |
| 213 | end.durability, |
| 214 | if fault.starts_with("dir-sync") { |
| 215 | Durability::Failed |
| 216 | } else if published { |
| 217 | Durability::Synced |
| 218 | } else { |
| 219 | Durability::NotApplicable |
| 220 | }, |
| 221 | "{context}" |
| 222 | ); |
| 223 | let retained: Vec<_> = events |
| 224 | .iter() |
| 225 | .filter_map(|e| match e { |
| 226 | Message::Output { |
| 227 | relative_name, |
| 228 | disposition: Disposition::PartialRetained, |
| 229 | .. |
| 230 | } => Some(relative_name), |
| 231 | _ => None, |
| 232 | }) |
| 233 | .collect(); |
| 234 | let cleanup_failed = fault.ends_with("cleanup-fail"); |
| 235 | assert_eq!( |
| 236 | end.cleanup, |
| 237 | if cleanup_failed { |
| 238 | Cleanup::Failed |
| 239 | } else { |
| 240 | Cleanup::Complete |
| 241 | }, |
| 242 | "{context}" |
| 243 | ); |
| 244 | assert_eq!( |
| 245 | end.retained_partial_count, |
| 246 | u64::from(cleanup_failed), |
| 247 | "{context}" |
| 248 | ); |
| 249 | assert_eq!(retained.len(), usize::from(cleanup_failed), "{context}"); |
| 250 | for name in retained { |
| 251 | let path = root.join(name); |
| 252 | assert!(path.is_file(), "{context}: {path:?}"); |
| 253 | if published { |
| 254 | assert_eq!(fs::read(path).unwrap(), fs::read(&output).unwrap()); |
| 255 | } |
| 256 | } |
| 257 | assert_eq!(fs::read(&unrelated).unwrap(), SENTINEL, "{context}"); |
| 258 | assert_eq!(fs::read(s.0.join("source")).unwrap(), SOURCE, "{context}"); |
| 259 | assert_eq!(fs::read(s.0.join("archive")).ok(), archive, "{context}"); |
| 260 | assert_eq!( |
| 261 | fs::read_dir(parent) |
| 262 | .unwrap() |
| 263 | .filter(|e| { |
| 264 | let name = e.as_ref().unwrap().file_name(); |
| 265 | let name = name.to_string_lossy(); |
| 266 | name.starts_with(".zipelo-") && name != ".zipelo-unrelated.tmp" |
| 267 | }) |
| 268 | .count(), |
| 269 | usize::from(cleanup_failed), |
| 270 | "{context}" |
| 271 | ); |
| 272 | if published && operation.starts_with("extract") { |
| 273 | assert_eq!(fs::read(output).unwrap(), SOURCE, "{context}"); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn cleanup_does_not_delete_a_replacement_at_the_temporary_name() { |
| 281 | for operation in ["extract", "extract-nested", "seven-zip", "recovery"] { |
| 282 | for fault in ["swap-sync", "swap-publish"] { |
| 283 | let s = Scratch::new(); |
| 284 | let (request, output) = prepare(&s, operation); |
| 285 | let events = run(&s, request, fault); |
| 286 | let end = terminal(&events); |
| 287 | assert_eq!(end.outcome, Outcome::Failed, "{operation}: {events:?}"); |
| 288 | assert_eq!( |
| 289 | end.error, |
| 290 | if fault == "swap-publish" { |
| 291 | ErrorCode::Conflict |
| 292 | } else { |
| 293 | ErrorCode::Io |
| 294 | } |
| 295 | ); |
| 296 | assert!(!output.exists()); |
| 297 | let replacements: Vec<_> = fs::read_dir(output.parent().unwrap()) |
| 298 | .unwrap() |
| 299 | .map(|e| e.unwrap().path()) |
| 300 | .filter(|p| p.file_name().unwrap().to_string_lossy().ends_with(".tmp")) |
| 301 | .collect(); |
| 302 | assert_eq!(replacements.len(), 1, "{operation}: {events:?}"); |
| 303 | assert_eq!( |
| 304 | fs::read(&replacements[0]).unwrap(), |
| 305 | b"unrelated replacement" |
| 306 | ); |
| 307 | assert_eq!(end.cleanup, Cleanup::Unconfirmed); |
| 308 | assert_eq!(end.retained_partial_count, 0); |
| 309 | assert!(!events.iter().any(|e| matches!( |
| 310 | e, |
| 311 | Message::Output { |
| 312 | disposition: Disposition::PartialRetained, |
| 313 | .. |
| 314 | } |
| 315 | ))); |
| 316 | assert_eq!(fs::read(s.0.join("source")).unwrap(), SOURCE); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | #[test] |
| 322 | fn late_temporary_name_swap_is_never_reported_as_created_output() { |
| 323 | for operation in ["extract", "extract-nested", "seven-zip", "recovery"] { |
| 324 | let s = Scratch::new(); |
| 325 | let (request, output) = prepare(&s, operation); |
| 326 | let original = fs::read(s.0.join("archive")).ok(); |
| 327 | let events = run(&s, request, "swap-on-link"); |
| 328 | let end = terminal(&events); |
| 329 | assert_eq!(end.outcome, Outcome::Failed, "{operation}: {events:?}"); |
| 330 | assert_eq!(end.created_count, 0, "{operation}: {events:?}"); |
| 331 | assert_eq!(end.error, ErrorCode::Conflict); |
| 332 | assert_eq!(end.publication, Publication::Uncertain); |
| 333 | assert_eq!(end.durability, Durability::Unconfirmed); |
| 334 | assert_eq!(end.cleanup, Cleanup::Unconfirmed); |
| 335 | assert_eq!(end.retained_partial_count, 0); |
| 336 | assert!(!events.iter().any(|e| matches!( |
| 337 | e, |
| 338 | Message::Output { |
| 339 | disposition: Disposition::Created, |
| 340 | .. |
| 341 | } |
| 342 | ))); |
| 343 | assert_eq!( |
| 344 | events |
| 345 | .iter() |
| 346 | .filter(|e| matches!( |
| 347 | e, |
| 348 | Message::Output { |
| 349 | disposition: Disposition::CommitUncertain, |
| 350 | .. |
| 351 | } |
| 352 | )) |
| 353 | .count(), |
| 354 | 1 |
| 355 | ); |
| 356 | assert_eq!(fs::read(&output).unwrap(), b"unrelated replacement"); |
| 357 | assert_eq!(fs::read(s.0.join("source")).unwrap(), SOURCE); |
| 358 | assert_eq!(fs::read(s.0.join("archive")).ok(), original); |
| 359 | let replacements: Vec<_> = fs::read_dir(output.parent().unwrap()) |
| 360 | .unwrap() |
| 361 | .map(|e| e.unwrap().path()) |
| 362 | .filter(|p| p.file_name().unwrap().to_string_lossy().ends_with(".tmp")) |
| 363 | .collect(); |
| 364 | assert_eq!(replacements.len(), 1); |
| 365 | assert_eq!( |
| 366 | fs::read(&replacements[0]).unwrap(), |
| 367 | b"unrelated replacement" |
| 368 | ); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | #[test] |
| 373 | fn moved_temporary_file_is_not_reported_cleaned() { |
| 374 | for operation in ["extract", "extract-nested", "seven-zip", "recovery"] { |
| 375 | let s = Scratch::new(); |
| 376 | let (request, output) = prepare(&s, operation); |
| 377 | let events = run(&s, request, "move-on-sync"); |
| 378 | let end = terminal(&events); |
| 379 | assert!(!output.exists()); |
| 380 | assert_eq!(end.outcome, Outcome::Failed, "{operation}: {events:?}"); |
| 381 | assert_eq!(end.error, ErrorCode::Conflict); |
| 382 | assert_eq!(end.publication, Publication::None); |
| 383 | assert_eq!(end.created_count, 0); |
| 384 | let owned: Vec<_> = fs::read_dir(output.parent().unwrap()) |
| 385 | .unwrap() |
| 386 | .map(|e| e.unwrap().path()) |
| 387 | .filter(|p| p.file_name().unwrap().to_string_lossy().ends_with(".owned")) |
| 388 | .collect(); |
| 389 | assert_eq!(owned.len(), 1); |
| 390 | assert!(!fs::read(&owned[0]).unwrap().is_empty()); |
| 391 | if operation.starts_with("extract") { |
| 392 | assert_eq!(fs::read(&owned[0]).unwrap(), SOURCE); |
| 393 | } |
| 394 | assert_eq!(end.cleanup, Cleanup::Unconfirmed, "{events:?}"); |
| 395 | assert_eq!(end.retained_partial_count, 0); |
| 396 | assert_eq!(fs::read(s.0.join("source")).unwrap(), SOURCE); |
| 397 | } |
| 398 | } |