team-ljyzipelo

공개
405줄12 KB
1use super::*;
2
3fn fixture(kind: &str, case: &str) -> Scratch {
4 let scratch = Scratch::new();
5 let output = Command::new("python3")
6 .arg(
7 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
8 .join("../../tests/rust-archive-worker/compressed_tar_cases.py"),
9 )
10 .arg(&scratch.0)
11 .arg(kind)
12 .arg(case)
13 .output()
14 .unwrap();
15 assert!(
16 output.status.success(),
17 "{kind}/{case}: {}",
18 String::from_utf8_lossy(&output.stderr)
19 );
20 scratch
21}
22
23fn run_case(s: &Scratch, limits: Limits) -> Vec<Message> {
24 extract(
25 s,
26 &fs::read(s.0.join("input")).unwrap(),
27 None,
28 Selection::All,
29 None,
30 limits,
31 )
32}
33
34fn no_output(s: &Scratch, events: &[Message], error: ErrorCode) {
35 let result = terminal(events);
36 assert_eq!(result.outcome, Outcome::Failed, "{events:?}");
37 assert_eq!(result.error, error, "{events:?}");
38 assert_eq!(result.created_count, 0);
39 assert_eq!(result.publication, Publication::None);
40 assert!(
41 !events
42 .iter()
43 .any(|event| matches!(event, Message::Output { .. }))
44 );
45 assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0);
46 assert!(!s.0.join("escape").exists());
47}
48
49fn normal(kind: &str, case: &str) {
50 let s = fixture(kind, case);
51 let events = run_case(&s, Limits::default());
52 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
53 assert_eq!(
54 fs::read(s.0.join("output/folder/payload.bin")).unwrap(),
55 fs::read(s.0.join("expected")).unwrap()
56 );
57 assert_eq!(fs::read(s.0.join("output/한글.txt")).unwrap(), b"end");
58 assert!(s.0.join("output/empty").is_dir());
59}
60
61#[test]
62fn bzip2_reference_tar_and_concatenated_streams() {
63 for case in ["normal", "multi"] {
64 normal("bz2", case);
65 }
66}
67
68#[test]
69fn xz_reference_tar_checks_filters_and_concatenated_streams() {
70 for case in [
71 "normal",
72 "multi",
73 "multi-block",
74 "multi-block-filters",
75 "stored-chunks",
76 "padding",
77 "crc32",
78 "sha256",
79 "no-check",
80 "filters",
81 ] {
82 normal("xz", case);
83 }
84}
85
86#[test]
87fn compressed_empty_tar_is_valid() {
88 for kind in ["bz2", "xz"] {
89 let s = fixture(kind, "empty");
90 let events = run_case(&s, Limits::default());
91 assert_eq!(
92 terminal(&events).outcome,
93 Outcome::Completed,
94 "{kind}: {events:?}"
95 );
96 assert_eq!(terminal(&events).created_count, 0);
97 assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0);
98 }
99}
100
101#[test]
102fn bzip2_truncation_corruption_and_trailing_data_never_publish() {
103 for case in [
104 "truncated",
105 "corrupt",
106 "trailing",
107 "multi-bad",
108 "bad-tar",
109 "raw",
110 "empty-stream",
111 ] {
112 let s = fixture("bz2", case);
113 let events = run_case(&s, Limits::default());
114 no_output(&s, &events, ErrorCode::Integrity);
115 }
116}
117
118#[test]
119fn xz_truncation_corruption_and_trailing_data_never_publish() {
120 for case in [
121 "truncated",
122 "corrupt",
123 "trailing",
124 "multi-bad",
125 "bad-tar",
126 "raw",
127 "empty-stream",
128 "bad-padding",
129 ] {
130 let s = fixture("xz", case);
131 let events = run_case(&s, Limits::default());
132 no_output(&s, &events, ErrorCode::Integrity);
133 }
134}
135
136#[test]
137fn xz_index_sizes_cannot_cancel_between_streams() {
138 let s = fixture("xz", "index-stream-sizes");
139 let events = run_case(&s, Limits::default());
140 no_output(&s, &events, ErrorCode::Integrity);
141}
142
143#[test]
144fn xz_index_sizes_cannot_cancel_between_blocks() {
145 let s = fixture("xz", "index-block-sizes");
146 let events = run_case(&s, Limits::default());
147 no_output(&s, &events, ErrorCode::Integrity);
148}
149
150#[test]
151fn xz_index_unpadded_sizes_must_match_each_block() {
152 let s = fixture("xz", "index-unpadded-sizes");
153 let events = run_case(&s, Limits::default());
154 no_output(&s, &events, ErrorCode::Integrity);
155}
156
157#[test]
158fn compressed_tar_bombs_stop_before_publication() {
159 for kind in ["bz2", "xz"] {
160 let s = fixture(kind, "bomb");
161 let events = run_case(
162 &s,
163 Limits {
164 output: 256 * 1024,
165 ..Limits::default()
166 },
167 );
168 no_output(&s, &events, ErrorCode::Limit);
169 }
170}
171
172#[test]
173fn xz_huge_dictionary_is_rejected_before_allocation() {
174 let s = fixture("xz", "dictionary");
175 let events = run_case(&s, Limits::default());
176 no_output(&s, &events, ErrorCode::Limit);
177}
178
179#[test]
180fn compressed_tar_paths_and_links_keep_existing_guards() {
181 for kind in ["bz2", "xz"] {
182 for (case, error) in [
183 ("unsafe", ErrorCode::UnsafePath),
184 ("link", ErrorCode::UnsafePath),
185 ] {
186 let s = fixture(kind, case);
187 let events = run_case(&s, Limits::default());
188 no_output(&s, &events, error);
189 }
190 }
191}
192
193#[test]
194fn compressed_tar_list_identity_and_selection() {
195 for kind in ["bz2", "xz"] {
196 let s = fixture(kind, "normal");
197 let events = execute(Start {
198 action: Action::List {
199 archive: s.path("input"),
200 },
201 limits: Limits::default(),
202 });
203 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
204 let token = events
205 .iter()
206 .find_map(|event| match event {
207 Message::ListEnd { list_id, .. } => Some(list_id.clone()),
208 _ => None,
209 })
210 .unwrap();
211 assert_eq!(token.format, Format::Tar);
212 let bytes = fs::read(s.0.join("input")).unwrap();
213 let events = extract(
214 &s,
215 &bytes,
216 None,
217 Selection::EntryIds(vec![2]),
218 Some(token),
219 Limits::default(),
220 );
221 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
222 assert_eq!(fs::read(s.0.join("output/한글.txt")).unwrap(), b"end");
223 assert!(!s.0.join("output/folder").exists());
224 }
225}
226
227#[test]
228fn compressed_tar_entry_quota_is_preserved() {
229 for kind in ["bz2", "xz"] {
230 let s = fixture(kind, "normal");
231 let events = run_case(
232 &s,
233 Limits {
234 entries: 1,
235 ..Limits::default()
236 },
237 );
238 no_output(&s, &events, ErrorCode::Limit);
239 }
240}
241
242#[cfg(target_os = "linux")]
243mod linux {
244 use super::*;
245 use std::{
246 io::Read,
247 time::{Duration, Instant},
248 };
249
250 struct Running(std::process::Child);
251 impl Drop for Running {
252 fn drop(&mut self) {
253 let _ = self.0.kill();
254 let _ = self.0.wait();
255 }
256 }
257
258 fn inject(s: &Scratch, mode: &str) -> (Running, Writer<std::process::ChildStdin>) {
259 let library = s.0.join("compressed_fault.so");
260 assert!(
261 Command::new("cc")
262 .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"])
263 .arg(
264 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
265 .join("../../tests/rust-archive-worker/compressed_fault.c")
266 )
267 .arg("-ldl")
268 .arg("-o")
269 .arg(&library)
270 .status()
271 .unwrap()
272 .success()
273 );
274 let bytes = fs::read(s.0.join("input")).unwrap();
275 let preparation = extract(
276 s,
277 &bytes,
278 None,
279 Selection::EntryIds(vec![999]),
280 None,
281 Limits::default(),
282 );
283 assert_ne!(terminal(&preparation).outcome, Outcome::Completed);
284 let mut command = s.worker();
285 command
286 .env("LD_PRELOAD", library)
287 .env("ZIPELO_COMPRESSED_FAULT", mode)
288 .env("ZIPELO_COMPRESSED_MARKER", s.0.join("paused"))
289 .env("ZIPELO_COMPRESSED_RELEASE", s.0.join("release"))
290 .stdin(Stdio::piped())
291 .stdout(Stdio::piped())
292 .stderr(Stdio::piped());
293 let mut child = command.spawn().unwrap();
294 let mut writer = Writer::new(child.stdin.take().unwrap(), 17);
295 writer
296 .send(&Message::Start(Start {
297 action: Action::Extract {
298 archive: s.path("input"),
299 destination: s.path("output"),
300 selection: Selection::All,
301 list_id: None,
302 gzip_output_name: None,
303 },
304 limits: Limits::default(),
305 }))
306 .unwrap();
307 (Running(child), writer)
308 }
309
310 fn finish(child: &mut Running) -> Vec<Message> {
311 drop(child.0.stdin.take());
312 let mut out = child.0.stdout.take().unwrap();
313 let mut err = child.0.stderr.take().unwrap();
314 let mut bytes = Vec::new();
315 out.read_to_end(&mut bytes).unwrap();
316 let mut errors = Vec::new();
317 err.read_to_end(&mut errors).unwrap();
318 assert!(child.0.wait().unwrap().success());
319 assert!(errors.is_empty(), "{errors:?}");
320 let mut reader = Reader::new(bytes.as_slice());
321 let mut events = Vec::new();
322 while let Some((_, _, event)) = reader.read().unwrap() {
323 events.push(event);
324 }
325 events
326 }
327
328 #[test]
329 fn compressed_tar_codec_space_failure_never_publishes() {
330 for kind in ["bz2", "xz"] {
331 for (mode, error) in [("full", ErrorCode::DiskFull), ("io", ErrorCode::Io)] {
332 let s = fixture(kind, "normal");
333 let (mut child, writer) = inject(&s, mode);
334 drop(writer);
335 let events = finish(&mut child);
336 assert!(s.0.join("paused").exists());
337 no_output(&s, &events, error);
338 }
339 }
340 }
341
342 #[test]
343 fn compressed_tar_cancels_during_decode_and_cleans_staging() {
344 for kind in ["bz2", "xz"] {
345 let s = fixture(kind, "normal");
346 let (mut child, mut writer) = inject(&s, "pause");
347 let deadline = Instant::now() + Duration::from_secs(10);
348 while !s.0.join("paused").exists() {
349 assert!(Instant::now() < deadline, "decoder did not pause");
350 std::thread::sleep(Duration::from_millis(5));
351 }
352 writer.send(&Message::Cancel).unwrap();
353 drop(writer);
354 std::thread::sleep(Duration::from_millis(25));
355 fs::write(s.0.join("release"), b"").unwrap();
356 let events = finish(&mut child);
357 assert_eq!(terminal(&events).outcome, Outcome::Cancelled, "{events:?}");
358 assert_eq!(terminal(&events).created_count, 0);
359 assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0);
360 assert!(
361 !events
362 .iter()
363 .any(|event| matches!(event, Message::Output { .. }))
364 );
365 }
366 }
367}
368
369#[test]
370fn xz_index_and_footer_claims_are_checked_before_publication() {
371 for (case, error) in [
372 ("index-size", ErrorCode::Integrity),
373 ("footer-offset", ErrorCode::Integrity),
374 ("index-huge", ErrorCode::Limit),
375 ] {
376 let s = fixture("xz", case);
377 let events = run_case(&s, Limits::default());
378 no_output(&s, &events, error);
379 }
380}
381
382#[test]
383fn compressed_tar_output_quota_boundary() {
384 for kind in ["bz2", "xz"] {
385 for below in [false, true] {
386 let s = fixture(kind, "normal");
387 let size: u64 = fs::read_to_string(s.0.join("plain-size"))
388 .unwrap()
389 .parse()
390 .unwrap();
391 let events = run_case(
392 &s,
393 Limits {
394 output: size - u64::from(below),
395 ..Limits::default()
396 },
397 );
398 if below {
399 no_output(&s, &events, ErrorCode::Limit);
400 } else {
401 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
402 }
403 }
404 }
405}