team-ljyzipelo

공개
327줄12 KB
1use super::*;
2use sha2::{Digest, Sha256};
3fn request(s: &Scratch, method: Method, encryption: Encryption) -> Start {
4 Start {
5 action: Action::CreateZip {
6 sources: vec![
7 Source {
8 path: s.path("tree"),
9 archive_name: "documents".into(),
10 },
11 Source {
12 path: s.path("loose"),
13 archive_name: "loose.bin".into(),
14 },
15 ],
16 output_file: s.path("created.zip"),
17 method,
18 encryption,
19 },
20 limits: Limits::default(),
21 }
22}
23fn seed(s: &Scratch) -> Vec<u8> {
24 fs::create_dir_all(s.0.join("tree/nested")).unwrap();
25 fs::create_dir(s.0.join("tree/empty")).unwrap();
26 let bytes = (0..262157).map(|i| (i % 251) as u8).collect::<Vec<_>>();
27 fs::write(s.0.join("tree/nested/한글.bin"), &bytes).unwrap();
28 fs::write(s.0.join("tree/zero"), []).unwrap();
29 fs::write(s.0.join("loose"), b"loose content").unwrap();
30 bytes
31}
32#[test]
33fn mixed_folders_and_files_round_trip_plain_and_aes_with_sha256() {
34 for method in [Method::Store, Method::Deflate] {
35 for encryption in [Encryption::None, Encryption::Aes256] {
36 let s = Scratch::new();
37 let original = seed(&s);
38 let events = interactive(request(&s, method, encryption), |id, _, _| {
39 vec![Message::Password {
40 challenge_id: id,
41 secret: Secret(b"folder-password".to_vec()),
42 }]
43 });
44 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
45 let listed = execute(Start {
46 action: Action::List {
47 archive: s.path("created.zip"),
48 },
49 limits: Limits::default(),
50 });
51 assert_eq!(terminal(&listed).outcome, Outcome::Completed, "{listed:?}");
52 let entries = listed
53 .iter()
54 .filter_map(|e| {
55 if let Message::Entry(e) = e {
56 Some(e)
57 } else {
58 None
59 }
60 })
61 .collect::<Vec<_>>();
62 assert_eq!(entries.len(), 6);
63 assert_eq!(
64 entries
65 .iter()
66 .filter(|e| e.kind == EntryKind::Directory)
67 .count(),
68 3
69 );
70 for entry in entries {
71 assert_eq!(
72 entry.encrypted,
73 encryption == Encryption::Aes256 && entry.kind == EntryKind::File
74 );
75 }
76 if encryption == Encryption::None {
77 assert!(Command::new("python3").arg("-c").arg("import sys,zipfile; z=zipfile.ZipFile(sys.argv[1]); assert z.testzip() is None; assert z.getinfo('documents/empty/').is_dir(); assert z.read('loose.bin') == b'loose content'").arg(s.0.join("created.zip")).status().unwrap().success());
78 }
79 fs::create_dir(s.0.join("output")).unwrap();
80 #[cfg(unix)]
81 {
82 use std::os::unix::fs::PermissionsExt;
83 fs::set_permissions(s.0.join("output"), fs::Permissions::from_mode(0o700)).unwrap();
84 }
85 let extracted = interactive(
86 Start {
87 action: Action::Extract {
88 archive: s.path("created.zip"),
89 destination: s.path("output"),
90 selection: Selection::All,
91 list_id: None,
92 gzip_output_name: None,
93 },
94 limits: Limits::default(),
95 },
96 |id, _, _| {
97 vec![Message::Password {
98 challenge_id: id,
99 secret: Secret(b"folder-password".to_vec()),
100 }]
101 },
102 );
103 assert_eq!(
104 terminal(&extracted).outcome,
105 Outcome::Completed,
106 "{extracted:?}"
107 );
108 assert_eq!(
109 Sha256::digest(fs::read(s.0.join("output/documents/nested/한글.bin")).unwrap()),
110 Sha256::digest(original)
111 );
112 assert_eq!(
113 Sha256::digest(fs::read(s.0.join("output/loose.bin")).unwrap()),
114 Sha256::digest(b"loose content")
115 );
116 assert_eq!(
117 fs::read(s.0.join("output/documents/zero")).unwrap(),
118 Vec::<u8>::new()
119 );
120 assert!(s.0.join("output/documents/empty").is_dir());
121 }
122 }
123}
124#[test]
125fn descendant_name_conflict_is_rejected_before_password() {
126 let s = Scratch::new();
127 seed(&s);
128 let mut start = request(&s, Method::Store, Encryption::Aes256);
129 if let Action::CreateZip { sources, .. } = &mut start.action {
130 sources[1].archive_name = "documents/zero".into();
131 }
132 let events = execute(start);
133 assert_eq!(terminal(&events).error, ErrorCode::Conflict, "{events:?}");
134 assert!(
135 !events
136 .iter()
137 .any(|e| matches!(e, Message::PasswordRequired { .. }))
138 );
139 assert!(!s.0.join("created.zip").exists());
140}
141#[test]
142fn empty_root_folder_is_preserved() {
143 let s = Scratch::new();
144 fs::create_dir(s.0.join("tree")).unwrap();
145 let mut start = request(&s, Method::Deflate, Encryption::None);
146 if let Action::CreateZip { sources, .. } = &mut start.action {
147 sources.truncate(1);
148 }
149 let events = execute(start);
150 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
151 let bytes = fs::read(s.0.join("created.zip")).unwrap();
152 let catalog = worker_catalog(&bytes, Limits::default(), true, None).unwrap();
153 assert_eq!(catalog.members.len(), 1);
154 assert_eq!(catalog.members[0].entry.kind, EntryKind::Directory);
155 assert_eq!(catalog.members[0].entry.name, "documents");
156}
157#[cfg(target_os = "linux")]
158#[test]
159fn symlinks_and_special_files_are_not_followed() {
160 let s = Scratch::new();
161 seed(&s);
162 for target in ["loose", "tree", "missing"] {
163 std::os::unix::fs::symlink(s.0.join(target), s.0.join("tree/link")).unwrap();
164 let events = execute(request(&s, Method::Store, Encryption::Aes256));
165 assert_eq!(terminal(&events).error, ErrorCode::UnsafePath, "{events:?}");
166 assert!(
167 !events
168 .iter()
169 .any(|e| matches!(e, Message::PasswordRequired { .. }))
170 );
171 assert!(!s.0.join("created.zip").exists());
172 fs::remove_file(s.0.join("tree/link")).unwrap();
173 }
174 assert!(
175 Command::new("mkfifo")
176 .arg(s.0.join("tree/pipe"))
177 .status()
178 .unwrap()
179 .success()
180 );
181 let events = execute(request(&s, Method::Store, Encryption::None));
182 assert_eq!(terminal(&events).error, ErrorCode::UnsafePath, "{events:?}");
183}
184#[test]
185fn source_replacement_while_waiting_for_password_leaves_no_zip() {
186 let s = Scratch::new();
187 seed(&s);
188 let events = interactive(
189 request(&s, Method::Store, Encryption::Aes256),
190 |id, _, _| {
191 fs::rename(s.0.join("tree/nested/한글.bin"), s.0.join("previous")).unwrap();
192 fs::write(s.0.join("tree/nested/한글.bin"), b"replacement").unwrap();
193 vec![Message::Password {
194 challenge_id: id,
195 secret: Secret(b"folder-password".to_vec()),
196 }]
197 },
198 );
199 assert_eq!(
200 terminal(&events).error,
201 ErrorCode::SourceChanged,
202 "{events:?}"
203 );
204 assert!(!s.0.join("created.zip").exists());
205}
206
207#[cfg(target_os = "linux")]
208#[test]
209fn invalid_descendant_names_and_case_collisions_fail_before_password() {
210 let s = Scratch::new();
211 seed(&s);
212 for (leaf, expected) in [
213 ("CON.txt", ErrorCode::UnsafePath),
214 ("ZERO", ErrorCode::Conflict),
215 ] {
216 fs::write(s.0.join("tree").join(leaf), b"not exported").unwrap();
217 let events = execute(request(&s, Method::Store, Encryption::Aes256));
218 assert_eq!(terminal(&events).error, expected, "{events:?}");
219 assert!(
220 !events
221 .iter()
222 .any(|e| matches!(e, Message::PasswordRequired { .. }))
223 );
224 assert!(!s.0.join("created.zip").exists());
225 fs::remove_file(s.0.join("tree").join(leaf)).unwrap();
226 }
227}
228
229#[cfg(target_os = "linux")]
230fn readable_sources_under_parent(mode: u32) {
231 use std::os::unix::fs::PermissionsExt;
232 struct RestoreParent(PathBuf);
233 impl Drop for RestoreParent {
234 fn drop(&mut self) {
235 fs::set_permissions(&self.0, fs::Permissions::from_mode(0o700)).unwrap();
236 }
237 }
238 let s = Scratch::new();
239 let parent = s.0.join("restricted");
240 fs::create_dir_all(parent.join("tree/empty")).unwrap();
241 fs::write(parent.join("loose"), b"readable file").unwrap();
242 fs::write(parent.join("tree/nested"), b"nested content").unwrap();
243 let _restore = RestoreParent(parent.clone());
244 fs::set_permissions(&parent, fs::Permissions::from_mode(mode)).unwrap();
245 if mode == 0o111 {
246 assert_eq!(
247 fs::read_dir(&parent).unwrap_err().kind(),
248 std::io::ErrorKind::PermissionDenied
249 );
250 } else {
251 assert!(fs::read_dir(&parent).is_ok());
252 }
253 assert_eq!(fs::read(parent.join("loose")).unwrap(), b"readable file");
254 for (name, encryption) in [("plain", Encryption::None), ("aes", Encryption::Aes256)] {
255 let archive_name = format!("{name}.zip");
256 let events = interactive(
257 Start {
258 action: Action::CreateZip {
259 sources: vec![
260 Source {
261 path: NativePath::from_path(&parent.join("loose")),
262 archive_name: "loose.txt".into(),
263 },
264 Source {
265 path: NativePath::from_path(&parent.join("tree")),
266 archive_name: "tree".into(),
267 },
268 ],
269 output_file: s.path(&archive_name),
270 method: Method::Deflate,
271 encryption,
272 },
273 limits: Limits::default(),
274 },
275 |id, _, _| {
276 vec![Message::Password {
277 challenge_id: id,
278 secret: Secret(b"parent-password".to_vec()),
279 }]
280 },
281 );
282 assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
283 let output = format!("output-{name}");
284 fs::create_dir(s.0.join(&output)).unwrap();
285 fs::set_permissions(s.0.join(&output), fs::Permissions::from_mode(0o700)).unwrap();
286 let extracted = interactive(
287 Start {
288 action: Action::Extract {
289 archive: s.path(&archive_name),
290 destination: s.path(&output),
291 selection: Selection::All,
292 list_id: None,
293 gzip_output_name: None,
294 },
295 limits: Limits::default(),
296 },
297 |id, _, _| {
298 vec![Message::Password {
299 challenge_id: id,
300 secret: Secret(b"parent-password".to_vec()),
301 }]
302 },
303 );
304 assert_eq!(
305 terminal(&extracted).outcome,
306 Outcome::Completed,
307 "{extracted:?}"
308 );
309 for (source, destination) in [("loose", "loose.txt"), ("tree/nested", "tree/nested")] {
310 assert_eq!(
311 Sha256::digest(fs::read(parent.join(source)).unwrap()),
312 Sha256::digest(fs::read(s.0.join(&output).join(destination)).unwrap())
313 );
314 }
315 assert!(s.0.join(&output).join("tree/empty").is_dir());
316 }
317}
318#[cfg(target_os = "linux")]
319#[test]
320fn readable_sources_under_search_only_parent_0111() {
321 readable_sources_under_parent(0o111);
322}
323#[cfg(target_os = "linux")]
324#[test]
325fn readable_sources_under_readable_parent_0711() {
326 readable_sources_under_parent(0o711);
327}