team-ljyzipelo

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