| 1 | const FIXTURE_PASSWORD: &str = "fixture-password"; |
| 2 | fn encrypted_fixture(scratch: &Scratch, name: &str) -> PathBuf { |
| 3 | let archive = scratch.path(name); |
| 4 | fs::copy( |
| 5 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 6 | .join("../../tests/rust-archive-worker/fixtures") |
| 7 | .join(name), |
| 8 | &archive, |
| 9 | ) |
| 10 | .unwrap(); |
| 11 | archive |
| 12 | } |
| 13 | impl Screen { |
| 14 | fn password_request(&mut self, expected_attempt: u32, encrypt: bool) -> u64 { |
| 15 | let deadline = Instant::now() + Duration::from_secs(10); |
| 16 | loop { |
| 17 | self.render(vec![]); |
| 18 | assert!(self.app.model.busy(), "{}", self.app.model.status()); |
| 19 | if let Some((id, attempt, purpose)) = self.app.model.challenge { |
| 20 | assert_eq!(attempt, expected_attempt); |
| 21 | assert_eq!(purpose, encrypt); |
| 22 | self.render(vec![]); |
| 23 | return id; |
| 24 | } |
| 25 | assert!(Instant::now() < deadline); |
| 26 | std::thread::sleep(Duration::from_millis(2)); |
| 27 | } |
| 28 | } |
| 29 | fn password_answer(&mut self, text: &str) { |
| 30 | self.render(vec![]); |
| 31 | let input = self |
| 32 | .context |
| 33 | .read_response(egui::Id::unique("archive-password-input")) |
| 34 | .expect("암호 입력란이 없음") |
| 35 | .rect |
| 36 | .center(); |
| 37 | self.click_at(input); |
| 38 | let rendered = self.render(vec![ |
| 39 | egui::Event::Key { |
| 40 | key: egui::Key::A, |
| 41 | physical_key: None, |
| 42 | pressed: true, |
| 43 | repeat: false, |
| 44 | modifiers: egui::Modifiers::COMMAND, |
| 45 | }, |
| 46 | egui::Event::Text(text.into()), |
| 47 | ]); |
| 48 | if !text.is_empty() { |
| 49 | assert!(!rendered.iter().any(|(value, _)| value.contains(text))); |
| 50 | } |
| 51 | if self |
| 52 | .app |
| 53 | .model |
| 54 | .challenge |
| 55 | .is_some_and(|(_, _, encrypt)| encrypt) |
| 56 | { |
| 57 | self.set_text("archive-password-confirm", text); |
| 58 | } |
| 59 | self.click("확인"); |
| 60 | } |
| 61 | fn start_password_extract(&mut self, output: &Path) { |
| 62 | self.app.choose_destination(output.to_path_buf()); |
| 63 | self.dialogs |
| 64 | .0 |
| 65 | .borrow_mut() |
| 66 | .answers |
| 67 | .push_back(Some(vec![output.to_path_buf()])); |
| 68 | self.click("압축해제"); |
| 69 | assert!(self.app.model.busy()); |
| 70 | } |
| 71 | fn finish_password_job(&mut self, expected: Outcome) { |
| 72 | let deadline = Instant::now() + Duration::from_secs(10); |
| 73 | while self.app.model.busy() { |
| 74 | self.render(vec![]); |
| 75 | assert!(Instant::now() < deadline); |
| 76 | std::thread::sleep(Duration::from_millis(2)); |
| 77 | } |
| 78 | assert!(self.app.model.confirmed, "{}", self.app.model.status()); |
| 79 | assert_eq!(self.app.model.terminal.as_ref().unwrap().outcome, expected); |
| 80 | assert!(self.app.model.challenge.is_none()); |
| 81 | } |
| 82 | /// ZIP 만들기 창에서 암호로 보호하기를 켜고 두 칸에 암호를 넣은 뒤 압축하기를 누름. |
| 83 | /// 작업이 시작됐는지 돌려줌. |
| 84 | fn start_encrypted_zip(&mut self, source: &Path, archive: &Path, password: &str) -> bool { |
| 85 | self.app.add_sources(vec![source.to_path_buf()]); |
| 86 | self.render(vec![]); |
| 87 | self.set_text("zip-name", archive.file_name().unwrap().to_str().unwrap()); |
| 88 | self.click("암호로 보호하기"); |
| 89 | self.set_text("zip-password-input", password); |
| 90 | self.set_text("zip-password-confirm", password); |
| 91 | if !password.is_empty() { |
| 92 | // 입력한 암호는 화면에 그대로 보이지 않음. |
| 93 | assert!(!self.render(vec![]).iter().any(|(value, _)| value.contains(password))); |
| 94 | } |
| 95 | self.click("압축하기"); |
| 96 | self.app.model.busy() |
| 97 | } |
| 98 | /// 별도 암호 입력 창 없이 작업이 끝나는지 확인함. |
| 99 | fn finish_without_password_prompt(&mut self, expected: Outcome) { |
| 100 | let deadline = Instant::now() + Duration::from_secs(10); |
| 101 | while self.app.model.busy() { |
| 102 | let shown = self.render(vec![]); |
| 103 | assert!(!shown.iter().any(|(value, _)| value == "ZIP 암호 설정")); |
| 104 | assert!(Instant::now() < deadline); |
| 105 | std::thread::sleep(Duration::from_millis(2)); |
| 106 | } |
| 107 | assert!(self.app.model.confirmed, "{}", self.app.model.status()); |
| 108 | assert_eq!(self.app.model.terminal.as_ref().unwrap().outcome, expected); |
| 109 | assert!(self.app.model.challenge.is_none()); |
| 110 | } |
| 111 | } |
| 112 | #[test] |
| 113 | fn encrypted_zip_list_and_extract_with_actual_password_dialog() { |
| 114 | for name in [ |
| 115 | "zipcrypto-store.zip", |
| 116 | "zipcrypto-deflate.zip", |
| 117 | "aes1-store.zip", |
| 118 | "aes1-deflate.zip", |
| 119 | "aes2-store.zip", |
| 120 | "aes2-deflate.zip", |
| 121 | ] { |
| 122 | let scratch = Scratch::new(); |
| 123 | let archive = encrypted_fixture(&scratch, name); |
| 124 | let original = fs::read(&archive).unwrap(); |
| 125 | let output = make_directory(&scratch.path("output")); |
| 126 | let (factory, observed) = observed_factory(); |
| 127 | let mut screen = Screen::new(factory); |
| 128 | screen.open(&archive); |
| 129 | assert!(screen.app.model.entries.iter().all(|entry| entry.encrypted)); |
| 130 | assert!(screen.app.model.challenge.is_none()); |
| 131 | screen.start_password_extract(&output); |
| 132 | screen.password_request(1, false); |
| 133 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 134 | screen.password_answer(FIXTURE_PASSWORD); |
| 135 | screen.finish_password_job(Outcome::Completed); |
| 136 | assert_eq!( |
| 137 | fs::read(output.join("payload.bin")).unwrap(), |
| 138 | b"Zipelo encrypted interoperability\n".repeat(8) |
| 139 | ); |
| 140 | assert_eq!(fs::read(archive).unwrap(), original); |
| 141 | assert_reaped(&observed); |
| 142 | } |
| 143 | } |
| 144 | #[test] |
| 145 | fn wrong_password_retry_clears_input_and_succeeds_without_partial_output() { |
| 146 | let scratch = Scratch::new(); |
| 147 | let archive = encrypted_fixture(&scratch, "aes2-deflate.zip"); |
| 148 | let output = make_directory(&scratch.path("output")); |
| 149 | let (factory, observed) = observed_factory(); |
| 150 | let mut screen = Screen::new(factory); |
| 151 | screen.open(&archive); |
| 152 | screen.start_password_extract(&output); |
| 153 | let first = screen.password_request(1, false); |
| 154 | screen.password_answer("wrong-password"); |
| 155 | let second = screen.password_request(2, false); |
| 156 | assert_ne!(first, second); |
| 157 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 158 | let labels = screen.render(vec![]); |
| 159 | assert!( |
| 160 | labels |
| 161 | .iter() |
| 162 | .any(|(value, _)| value == "암호가 맞지 않습니다. 다시 입력하세요") |
| 163 | ); |
| 164 | screen.click("확인"); |
| 165 | assert_eq!(screen.app.model.challenge, Some((second, 2, false))); |
| 166 | assert!(screen.app.model.input_error.is_none()); |
| 167 | screen.render(vec![]); |
| 168 | screen.password_answer(FIXTURE_PASSWORD); |
| 169 | screen.finish_password_job(Outcome::Completed); |
| 170 | assert_reaped(&observed); |
| 171 | } |
| 172 | #[test] |
| 173 | fn three_wrong_passwords_fail_without_writes_and_can_start_a_new_job() { |
| 174 | let scratch = Scratch::new(); |
| 175 | let archive = encrypted_fixture(&scratch, "zipcrypto-deflate.zip"); |
| 176 | let output = make_directory(&scratch.path("output")); |
| 177 | let (factory, observed) = observed_factory(); |
| 178 | let mut screen = Screen::new(factory); |
| 179 | screen.open(&archive); |
| 180 | screen.start_password_extract(&output); |
| 181 | for attempt in 1..=3 { |
| 182 | screen.password_request(attempt, false); |
| 183 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 184 | screen.password_answer("wrong-password"); |
| 185 | } |
| 186 | screen.finish_password_job(Outcome::Failed); |
| 187 | assert_eq!( |
| 188 | screen.app.model.terminal.as_ref().unwrap().error, |
| 189 | Some(ErrorCode::WrongPassword) |
| 190 | ); |
| 191 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 192 | screen.open(&archive); |
| 193 | assert_reaped(&observed); |
| 194 | } |
| 195 | #[test] |
| 196 | fn cancel_password_extraction_waits_for_cleanup_without_writes() { |
| 197 | let scratch = Scratch::new(); |
| 198 | let archive = encrypted_fixture(&scratch, "aes1-store.zip"); |
| 199 | let output = make_directory(&scratch.path("output")); |
| 200 | fs::write(output.join("existing.txt"), b"preserve").unwrap(); |
| 201 | let (factory, observed) = observed_factory(); |
| 202 | let mut screen = Screen::new(factory); |
| 203 | screen.open(&archive); |
| 204 | screen.start_password_extract(&output); |
| 205 | screen.password_request(1, false); |
| 206 | screen.escape(); |
| 207 | screen.app.model.request_close(); |
| 208 | screen.app.model.confirm_close(); |
| 209 | assert!(matches!( |
| 210 | screen.app.model.close, |
| 211 | Close::Waiting | Close::Review |
| 212 | )); |
| 213 | screen.finish_password_job(Outcome::Cancelled); |
| 214 | // 정리가 끝나면 결과 확인 안내 없이 바로 닫힘. |
| 215 | assert_eq!(screen.app.model.close, Close::Allowed); |
| 216 | assert_eq!(fs::read(output.join("existing.txt")).unwrap(), b"preserve"); |
| 217 | assert_eq!(fs::read_dir(output).unwrap().count(), 1); |
| 218 | assert_reaped(&observed); |
| 219 | } |
| 220 | #[test] |
| 221 | fn cold_start_creates_aes_zip_then_reopens_and_extracts_with_password() { |
| 222 | let scratch = Scratch::new(); |
| 223 | let source = scratch.path("private.txt"); |
| 224 | let payload = b"Public synthetic password flow payload\n"; |
| 225 | fs::write(&source, payload).unwrap(); |
| 226 | let archive = scratch.path("encrypted.zip"); |
| 227 | let output = make_directory(&scratch.path("output")); |
| 228 | let (factory, observed) = observed_factory(); |
| 229 | let mut screen = Screen::new(factory); |
| 230 | assert!(!screen.app.model.encryption); |
| 231 | // 암호는 ZIP 만들기 창에서 받고, 작업자가 요청하면 별도 창 없이 넘김. |
| 232 | assert!(screen.start_encrypted_zip(&source, &archive, FIXTURE_PASSWORD)); |
| 233 | screen.finish_without_password_prompt(Outcome::Completed); |
| 234 | let bytes = fs::read(&archive).unwrap(); |
| 235 | assert_eq!(&bytes[..4], b"PK\x03\x04"); |
| 236 | assert_eq!(u16::from_le_bytes(bytes[6..8].try_into().unwrap()) & 1, 1); |
| 237 | assert_eq!(u16::from_le_bytes(bytes[8..10].try_into().unwrap()), 99); |
| 238 | screen.open(&archive); |
| 239 | assert!(screen.app.model.entries.iter().all(|entry| entry.encrypted)); |
| 240 | screen.start_password_extract(&output); |
| 241 | screen.password_request(1, false); |
| 242 | screen.password_answer(FIXTURE_PASSWORD); |
| 243 | screen.finish_password_job(Outcome::Completed); |
| 244 | assert_eq!(fs::read(output.join("private.txt")).unwrap(), payload); |
| 245 | assert_eq!(fs::read(source).unwrap(), payload); |
| 246 | assert_reaped(&observed); |
| 247 | } |
| 248 | #[test] |
| 249 | fn cancel_encrypted_creation_window_starts_no_worker_or_archive() { |
| 250 | let scratch = Scratch::new(); |
| 251 | let source = scratch.path("private.txt"); |
| 252 | fs::write(&source, b"preserve").unwrap(); |
| 253 | let archive = scratch.path("cancelled.zip"); |
| 254 | let (factory, observed) = observed_factory(); |
| 255 | let mut screen = Screen::new(factory); |
| 256 | screen.app.add_sources(vec![source.clone()]); |
| 257 | screen.render(vec![]); |
| 258 | screen.click("암호로 보호하기"); |
| 259 | screen.set_text("zip-password-input", FIXTURE_PASSWORD); |
| 260 | screen.set_text("zip-password-confirm", FIXTURE_PASSWORD); |
| 261 | screen.click("취소"); |
| 262 | let shown = screen.render(vec![]); |
| 263 | assert!(!shown.iter().any(|(value, _)| value == "ZIP 만들기")); |
| 264 | assert!(!screen.app.model.busy()); |
| 265 | assert!(observed.borrow().jobs.is_empty()); |
| 266 | assert!(!archive.exists()); |
| 267 | assert_eq!(fs::read(source).unwrap(), b"preserve"); |
| 268 | assert_eq!(fs::read_dir(&scratch.0).unwrap().count(), 1); |
| 269 | assert_reaped(&observed); |
| 270 | } |
| 271 | |
| 272 | #[test] |
| 273 | fn oversized_utf8_password_stays_in_dialog_then_accepts_valid_input() { |
| 274 | let scratch = Scratch::new(); |
| 275 | let archive = encrypted_fixture(&scratch, "aes2-store.zip"); |
| 276 | let output = make_directory(&scratch.path("output")); |
| 277 | let (factory, observed) = observed_factory(); |
| 278 | let mut screen = Screen::new(factory); |
| 279 | screen.open(&archive); |
| 280 | screen.start_password_extract(&output); |
| 281 | let id = screen.password_request(1, false); |
| 282 | screen.password_answer(&"가".repeat(342)); |
| 283 | assert_eq!(screen.app.model.challenge, Some((id, 1, false))); |
| 284 | assert!( |
| 285 | screen |
| 286 | .render(vec![]) |
| 287 | .iter() |
| 288 | .any(|(text, _)| text.contains("1~1024바이트")) |
| 289 | ); |
| 290 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 291 | screen.password_answer(FIXTURE_PASSWORD); |
| 292 | screen.finish_password_job(Outcome::Completed); |
| 293 | assert_reaped(&observed); |
| 294 | } |
| 295 | |
| 296 | fn invalid_zip_password_never_starts_a_worker(invalid: &str, close: bool) { |
| 297 | let scratch = Scratch::new(); |
| 298 | let source = scratch.path("private.txt"); |
| 299 | fs::write(&source, b"preserve").unwrap(); |
| 300 | let archive = scratch.path("cancelled.zip"); |
| 301 | let (factory, observed) = observed_factory(); |
| 302 | let mut screen = Screen::new(factory); |
| 303 | // 빈 암호나 1024바이트를 넘는 암호로는 압축하기를 누를 수 없음. |
| 304 | assert!(!screen.start_encrypted_zip(&source, &archive, invalid)); |
| 305 | assert!(observed.borrow().jobs.is_empty()); |
| 306 | if !invalid.is_empty() { |
| 307 | assert!( |
| 308 | screen |
| 309 | .render(vec![]) |
| 310 | .iter() |
| 311 | .any(|(text, _)| text.contains("1~1024바이트")) |
| 312 | ); |
| 313 | } |
| 314 | if close { |
| 315 | // 작업이 없으므로 확인만 거치면 닫을 수 있음. |
| 316 | screen.app.model.request_close(); |
| 317 | assert_eq!(screen.app.model.close, Close::Review); |
| 318 | screen.app.model.acknowledge_close(); |
| 319 | assert_eq!(screen.app.model.close, Close::Allowed); |
| 320 | } else { |
| 321 | screen.click("취소"); |
| 322 | assert!( |
| 323 | !screen |
| 324 | .render(vec![]) |
| 325 | .iter() |
| 326 | .any(|(value, _)| value == "ZIP 만들기") |
| 327 | ); |
| 328 | } |
| 329 | assert!(!archive.exists()); |
| 330 | assert_eq!(fs::read(&source).unwrap(), b"preserve"); |
| 331 | assert_eq!(fs::read_dir(&scratch.0).unwrap().count(), 1); |
| 332 | assert_reaped(&observed); |
| 333 | if !close { |
| 334 | screen.open(&encrypted_fixture(&scratch, "aes2-store.zip")); |
| 335 | assert_reaped(&observed); |
| 336 | } |
| 337 | } |
| 338 | #[test] |
| 339 | fn empty_zip_password_never_starts_a_worker() { |
| 340 | invalid_zip_password_never_starts_a_worker("", false); |
| 341 | } |
| 342 | #[test] |
| 343 | fn oversized_zip_password_never_starts_a_worker() { |
| 344 | invalid_zip_password_never_starts_a_worker(&"가".repeat(342), false); |
| 345 | } |
| 346 | #[test] |
| 347 | fn invalid_zip_password_then_close_allows_closing() { |
| 348 | invalid_zip_password_never_starts_a_worker("", true); |
| 349 | } |
| 350 | |
| 351 | /// 실제 7-Zip이 `-p -mhe=on`으로 만든 분할 표본을 복사함. 공개 시험 암호는 zipelo-split-test. |
| 352 | fn encrypted_seven_zip_volumes(scratch: &Scratch, name: &str) -> PathBuf { |
| 353 | let root = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 354 | .join("../../tests/rust-archive-worker/fixtures/split7z"); |
| 355 | for index in 1.. { |
| 356 | let source = root.join(format!("{name}.7z.{index:03}")); |
| 357 | if !source.is_file() { |
| 358 | assert!(index > 2); |
| 359 | break; |
| 360 | } |
| 361 | fs::copy(source, scratch.path(&format!("sample.7z.{index:03}"))).unwrap(); |
| 362 | } |
| 363 | scratch.path("sample.7z.001") |
| 364 | } |
| 365 | #[test] |
| 366 | fn encrypted_seven_zip_header_asks_password_while_listing_then_again_for_extract() { |
| 367 | let scratch = Scratch::new(); |
| 368 | let archive = encrypted_seven_zip_volumes(&scratch, "encrypted-header"); |
| 369 | let output = make_directory(&scratch.path("output")); |
| 370 | let (factory, observed) = observed_factory(); |
| 371 | let mut screen = Screen::new(factory); |
| 372 | screen.app.open_path(archive.clone()); |
| 373 | // 헤더가 암호화되어 있으면 목록을 읽는 작업에서 같은 암호 창이 열림. |
| 374 | screen.password_request(1, false); |
| 375 | assert!( |
| 376 | screen |
| 377 | .render(vec![]) |
| 378 | .iter() |
| 379 | .any(|(value, _)| value == "암호가 필요합니다") |
| 380 | ); |
| 381 | screen.password_answer("wrong-password"); |
| 382 | screen.password_request(2, false); |
| 383 | screen.password_answer("zipelo-split-test"); |
| 384 | screen.settle(); |
| 385 | assert_eq!(screen.app.model.entries.len(), 3); |
| 386 | assert!( |
| 387 | screen |
| 388 | .app |
| 389 | .model |
| 390 | .entries |
| 391 | .iter() |
| 392 | .filter(|entry| entry.bytes.is_some_and(|bytes| bytes != 0)) |
| 393 | .all(|entry| entry.encrypted) |
| 394 | ); |
| 395 | // 해제는 새 작업자이므로 암호를 다시 물음. |
| 396 | screen.start_password_extract(&output); |
| 397 | screen.password_request(1, false); |
| 398 | screen.password_answer("zipelo-split-test"); |
| 399 | screen.finish_password_job(Outcome::Completed); |
| 400 | assert_eq!( |
| 401 | fs::read(output.join("folder/hello.txt")).unwrap(), |
| 402 | b"Zipelo 7z interoperability\n".repeat(100) |
| 403 | ); |
| 404 | assert_reaped(&observed); |
| 405 | } |
| 406 | #[test] |
| 407 | fn encrypted_seven_zip_header_cancel_while_listing_shows_no_entries() { |
| 408 | let scratch = Scratch::new(); |
| 409 | let archive = encrypted_seven_zip_volumes(&scratch, "encrypted-header"); |
| 410 | let (factory, observed) = observed_factory(); |
| 411 | let mut screen = Screen::new(factory); |
| 412 | screen.app.open_path(archive); |
| 413 | screen.password_request(1, false); |
| 414 | screen.escape(); |
| 415 | screen.finish_password_job(Outcome::Cancelled); |
| 416 | assert!(screen.app.model.entries.is_empty()); |
| 417 | assert_reaped(&observed); |
| 418 | } |