team-ljyzipelo

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