team-ljyzipelo

공개
235줄7.9 KB
1use super::*;
2
3/// 실제 7-Zip 26.00이 `-p`로 만든 분할 표본(split7z)의 공개 시험 암호.
4const SPLIT_PASSWORD: &[u8] = b"zipelo-split-test";
5/// seven_zip_aes_fixtures.py가 만든 합성 표본의 공개 시험 암호.
6const SYNTHETIC_PASSWORD: &str = "zipelo-암호";
7
8/// 실제 7-Zip 분할 표본의 조각을 이어 붙이면 같은 단일 7z가 됨.
9fn joined_reference(name: &str) -> Vec<u8> {
10 let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
11 .join("../../tests/rust-archive-worker/fixtures/split7z");
12 let mut bytes = Vec::new();
13 for index in 1..=999 {
14 match fs::read(root.join(format!("{name}.7z.{index:03}"))) {
15 Ok(part) => bytes.extend(part),
16 Err(error) if error.kind() == std::io::ErrorKind::NotFound => break,
17 Err(error) => panic!("{error:?}"),
18 }
19 }
20 assert!(bytes.starts_with(b"7z\xbc\xaf\x27\x1c"));
21 bytes
22}
23fn reference_files() -> Vec<(&'static str, Vec<u8>)> {
24 vec![
25 ("folder/hello.txt", SEVEN_PAYLOAD.repeat(100)),
26 (
27 "second.bin",
28 (0..4096usize)
29 .map(|index| ((index * 37 + (index >> 3)) % 256) as u8)
30 .collect(),
31 ),
32 ]
33}
34fn synthetic_files() -> Vec<(&'static str, Vec<u8>)> {
35 vec![
36 ("folder/empty", Vec::new()),
37 (
38 "folder/hello.txt",
39 b"Zipelo 7z AES interoperability\n".repeat(60),
40 ),
41 (
42 "한글.bin",
43 (0..3000usize)
44 .map(|index| ((index * 31 + (index >> 4)) % 256) as u8)
45 .collect(),
46 ),
47 ]
48}
49fn list_start(s: &Scratch, bytes: &[u8]) -> Start {
50 fs::write(s.0.join("input"), bytes).unwrap();
51 Start {
52 action: Action::List {
53 archive: s.path("input"),
54 },
55 limits: Limits::default(),
56 }
57}
58fn prompts(events: &[Message]) -> Vec<u32> {
59 events
60 .iter()
61 .filter_map(|event| match event {
62 Message::PasswordRequired {
63 attempt, purpose, ..
64 } => {
65 assert_eq!(*purpose, PasswordPurpose::Decrypt);
66 Some(*attempt)
67 }
68 _ => None,
69 })
70 .collect()
71}
72fn entries(events: &[Message]) -> Vec<Entry> {
73 events
74 .iter()
75 .filter_map(|event| match event {
76 Message::Entry(entry) => Some(entry.clone()),
77 _ => None,
78 })
79 .collect()
80}
81fn assert_files(s: &Scratch, expected: &[(&str, Vec<u8>)]) {
82 for (name, data) in expected {
83 assert_eq!(
84 &fs::read(s.0.join("output").join(name)).unwrap(),
85 data,
86 "{name}"
87 );
88 }
89}
90
91#[test]
92fn real_encrypted_seven_zip_lists_without_password_and_extracts_with_one_prompt() {
93 let bytes = joined_reference("encrypted-body");
94 let s = Scratch::new();
95 let listing = execute(list_start(&s, &bytes));
96 assert_eq!(
97 terminal(&listing).outcome,
98 Outcome::Completed,
99 "{listing:?}"
100 );
101 assert!(prompts(&listing).is_empty());
102 let listed = entries(&listing);
103 assert!(
104 listed
105 .iter()
106 .filter(|e| e.kind == EntryKind::File)
107 .all(|e| e.encrypted)
108 );
109 let start = encrypted_start(&s, &bytes);
110 let events = interactive(start, |id, _, _| password_reply(id, SPLIT_PASSWORD));
111 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
112 assert_eq!(prompts(&events), [1]);
113 assert_files(&s, &reference_files());
114}
115
116#[test]
117fn encrypted_seven_zip_headers_ask_once_for_listing_and_reuse_for_extraction() {
118 for (bytes, password, expected) in [
119 (
120 joined_reference("encrypted-header"),
121 SPLIT_PASSWORD.to_vec(),
122 reference_files(),
123 ),
124 (
125 seven_fixture("aes-lzma-header.7z"),
126 SYNTHETIC_PASSWORD.as_bytes().to_vec(),
127 synthetic_files(),
128 ),
129 ] {
130 let s = Scratch::new();
131 let listing = interactive(list_start(&s, &bytes), |id, _, _| {
132 password_reply(id, &password)
133 });
134 assert_eq!(
135 terminal(&listing).outcome,
136 Outcome::Completed,
137 "{listing:?}"
138 );
139 assert_eq!(prompts(&listing), [1]);
140 let listed = entries(&listing);
141 assert!(!listed.is_empty());
142 assert!(
143 listed
144 .iter()
145 .filter(|e| e.kind == EntryKind::File && e.size != 0)
146 .all(|e| e.encrypted)
147 );
148 // 암호 없이 목록을 요청하면 이름을 보이지 않고 틀린 암호로 끝남.
149 let refused = interactive(list_start(&s, &bytes), |_, _, _| Vec::new());
150 assert_eq!(terminal(&refused).outcome, Outcome::Failed);
151 assert!(entries(&refused).is_empty());
152 let start = encrypted_start(&s, &bytes);
153 let events = interactive(start, |id, _, _| password_reply(id, &password));
154 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
155 assert_eq!(prompts(&events), [1]);
156 assert_files(&s, &expected);
157 }
158}
159
160#[test]
161fn synthetic_copy_aes_folder_extracts_with_utf16_password() {
162 let bytes = seven_fixture("aes-copy.7z");
163 let s = Scratch::new();
164 let listing = execute(list_start(&s, &bytes));
165 assert_eq!(
166 terminal(&listing).outcome,
167 Outcome::Completed,
168 "{listing:?}"
169 );
170 let listed = entries(&listing);
171 assert_eq!(listed.len(), 4);
172 assert!(
173 listed
174 .iter()
175 .any(|e| e.kind == EntryKind::Directory && !e.encrypted)
176 );
177 assert!(listed.iter().any(|e| e.name == "한글.bin" && e.encrypted));
178 let start = encrypted_start(&s, &bytes);
179 let events = interactive(start, |id, _, _| {
180 password_reply(id, SYNTHETIC_PASSWORD.as_bytes())
181 });
182 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
183 assert_files(&s, &synthetic_files());
184}
185
186#[test]
187fn wrong_seven_zip_passwords_retry_then_fail_without_output() {
188 for (bytes, password) in [
189 (joined_reference("encrypted-body"), SPLIT_PASSWORD.to_vec()),
190 (
191 joined_reference("encrypted-header"),
192 SPLIT_PASSWORD.to_vec(),
193 ),
194 (
195 seven_fixture("aes-copy.7z"),
196 SYNTHETIC_PASSWORD.as_bytes().to_vec(),
197 ),
198 (
199 seven_fixture("aes-lzma-header.7z"),
200 SYNTHETIC_PASSWORD.as_bytes().to_vec(),
201 ),
202 ] {
203 let s = Scratch::new();
204 let events = interactive(encrypted_start(&s, &bytes), |id, _, _| {
205 password_reply(id, b"wrong-password")
206 });
207 assert_eq!(prompts(&events), [1, 2, 3]);
208 no_encrypted_output(&s, &events, ErrorCode::WrongPassword);
209 // 같은 작업 안에서 틀린 뒤 맞는 암호를 넣으면 이어서 풂.
210 fs::remove_dir(s.0.join("output")).unwrap();
211 let events = interactive(encrypted_start(&s, &bytes), |id, attempt, _| {
212 password_reply(id, if attempt == 1 { b"wrong" } else { &password })
213 });
214 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
215 assert_eq!(prompts(&events), [1, 2]);
216 }
217}
218
219#[test]
220fn damaged_or_costly_seven_zip_aes_is_not_reported_as_wrong_password() {
221 let s = Scratch::new();
222 let bytes = seven_fixture("aes-bad-packed.7z");
223 let events = interactive(encrypted_start(&s, &bytes), |id, _, _| {
224 password_reply(id, SYNTHETIC_PASSWORD.as_bytes())
225 });
226 no_encrypted_output(&s, &events, ErrorCode::Integrity);
227 // 반복 지수가 상한(24)을 넘으면 키를 유도하기 전에 암호도 묻지 않고 거부함.
228 let s = Scratch::new();
229 let events = interactive(
230 list_start(&s, &seven_fixture("aes-cycles.7z")),
231 |id, _, _| password_reply(id, SYNTHETIC_PASSWORD.as_bytes()),
232 );
233 assert_eq!(terminal(&events).error, ErrorCode::UnsupportedFeature);
234 assert!(prompts(&events).is_empty());
235}