team-ljyzipelo

공개
74줄2.7 KB
1use super::*;
2
3// 해제 작업의 원본 사본과 단계 임시 파일은 시스템 임시 폴더가 아니라 출력 폴더에 둠.
4// 시스템 임시 폴더를 쓸 수 없게 만들어도(/tmp 할당량 초과와 같은 상황) 해제가 성공해야 함.
5fn extract_without_system_temp(bytes: &[u8]) -> (Scratch, Vec<Message>) {
6 let s = Scratch::new();
7 let start = encrypted_start(&s, bytes);
8 let mut child = s
9 .worker()
10 .env("TMPDIR", s.0.join("missing-temp"))
11 .stdin(Stdio::piped())
12 .stdout(Stdio::piped())
13 .stderr(Stdio::piped())
14 .spawn()
15 .unwrap();
16 let mut writer = Some(Writer::new(child.stdin.take().unwrap(), 17));
17 writer
18 .as_mut()
19 .unwrap()
20 .send(&Message::StartInteractive(start))
21 .unwrap();
22 let mut reader = Reader::new(child.stdout.take().unwrap());
23 let mut events = Vec::new();
24 while let Some((job, _, event)) = reader.read().unwrap() {
25 assert_eq!(job, 17);
26 if let Message::PasswordRequired { challenge_id, .. } = &event {
27 for reply in password_reply(*challenge_id, TEST_PASSWORD) {
28 writer.as_mut().unwrap().send(&reply).unwrap();
29 }
30 }
31 if matches!(event, Message::Terminal(_)) {
32 writer = None;
33 }
34 events.push(event);
35 }
36 drop(writer);
37 let output = child.wait_with_output().unwrap();
38 assert!(output.status.success(), "{:?}", output.stderr);
39 assert!(output.stderr.is_empty());
40 assert!(!s.0.join("missing-temp").exists());
41 (s, events)
42}
43
44fn assert_only_outputs(s: &Scratch, expected: &[&str]) {
45 let mut names = fs::read_dir(s.0.join("output"))
46 .unwrap()
47 .map(|entry| entry.unwrap().file_name().into_string().unwrap())
48 .collect::<Vec<_>>();
49 names.sort();
50 assert_eq!(names, expected, "임시 파일이 출력 폴더에 남으면 안 됨");
51}
52
53#[test]
54fn extract_spools_in_destination_when_system_temp_is_unusable() {
55 let (s, events) = extract_without_system_temp(include_bytes!("fixtures/deflate.zip"));
56 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
57 assert_eq!(terminal(&events).publication, Publication::Complete);
58 assert_eq!(
59 fs::read(s.0.join("output/folder/hello.txt")).unwrap(),
60 PAYLOAD
61 );
62 assert_only_outputs(&s, &["empty", "folder"]);
63}
64
65#[test]
66fn encrypted_extract_spools_in_destination_when_system_temp_is_unusable() {
67 let (s, events) = extract_without_system_temp(AES_ZIP);
68 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
69 assert_eq!(
70 fs::read(s.0.join("output/payload.bin")).unwrap(),
71 ENCRYPTED_PAYLOAD
72 );
73 assert_only_outputs(&s, &["payload.bin"]);
74}