| 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 | fn start_encrypted_zip(&mut self, source: &Path, archive: &Path) { |
| 83 | self.app.add_sources(vec![source.to_path_buf()]); |
| 84 | self.render(vec![]); |
| 85 | self.set_text("zip-name", archive.file_name().unwrap().to_str().unwrap()); |
| 86 | self.click("암호로 보호하기"); |
| 87 | self.click("압축하기"); |
| 88 | assert!(self.app.model.busy(), "{}", self.app.model.status()); |
| 89 | } |
| 90 | } |
| 91 | #[test] |
| 92 | fn encrypted_zip_list_and_extract_with_actual_password_dialog() { |
| 93 | for name in [ |
| 94 | "zipcrypto-store.zip", |
| 95 | "zipcrypto-deflate.zip", |
| 96 | "aes1-store.zip", |
| 97 | "aes1-deflate.zip", |
| 98 | "aes2-store.zip", |
| 99 | "aes2-deflate.zip", |
| 100 | ] { |
| 101 | let scratch = Scratch::new(); |
| 102 | let archive = encrypted_fixture(&scratch, name); |
| 103 | let original = fs::read(&archive).unwrap(); |
| 104 | let output = make_directory(&scratch.path("output")); |
| 105 | let (factory, observed) = observed_factory(); |
| 106 | let mut screen = Screen::new(factory); |
| 107 | screen.open(&archive); |
| 108 | assert!(screen.app.model.entries.iter().all(|entry| entry.encrypted)); |
| 109 | assert!(screen.app.model.challenge.is_none()); |
| 110 | screen.start_password_extract(&output); |
| 111 | screen.password_request(1, false); |
| 112 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 113 | screen.password_answer(FIXTURE_PASSWORD); |
| 114 | screen.finish_password_job(Outcome::Completed); |
| 115 | assert_eq!( |
| 116 | fs::read(output.join("payload.bin")).unwrap(), |
| 117 | b"Zipelo encrypted interoperability\n".repeat(8) |
| 118 | ); |
| 119 | assert_eq!(fs::read(archive).unwrap(), original); |
| 120 | assert_reaped(&observed); |
| 121 | } |
| 122 | } |
| 123 | #[test] |
| 124 | fn wrong_password_retry_clears_input_and_succeeds_without_partial_output() { |
| 125 | let scratch = Scratch::new(); |
| 126 | let archive = encrypted_fixture(&scratch, "aes2-deflate.zip"); |
| 127 | let output = make_directory(&scratch.path("output")); |
| 128 | let (factory, observed) = observed_factory(); |
| 129 | let mut screen = Screen::new(factory); |
| 130 | screen.open(&archive); |
| 131 | screen.start_password_extract(&output); |
| 132 | let first = screen.password_request(1, false); |
| 133 | screen.password_answer("wrong-password"); |
| 134 | let second = screen.password_request(2, false); |
| 135 | assert_ne!(first, second); |
| 136 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 137 | let labels = screen.render(vec![]); |
| 138 | assert!( |
| 139 | labels |
| 140 | .iter() |
| 141 | .any(|(value, _)| value == "암호가 맞지 않습니다. 다시 입력하세요") |
| 142 | ); |
| 143 | screen.click("확인"); |
| 144 | assert_eq!(screen.app.model.challenge, Some((second, 2, false))); |
| 145 | assert!(screen.app.model.input_error.is_none()); |
| 146 | screen.render(vec![]); |
| 147 | screen.password_answer(FIXTURE_PASSWORD); |
| 148 | screen.finish_password_job(Outcome::Completed); |
| 149 | assert_reaped(&observed); |
| 150 | } |
| 151 | #[test] |
| 152 | fn three_wrong_passwords_fail_without_writes_and_can_start_a_new_job() { |
| 153 | let scratch = Scratch::new(); |
| 154 | let archive = encrypted_fixture(&scratch, "zipcrypto-deflate.zip"); |
| 155 | let output = make_directory(&scratch.path("output")); |
| 156 | let (factory, observed) = observed_factory(); |
| 157 | let mut screen = Screen::new(factory); |
| 158 | screen.open(&archive); |
| 159 | screen.start_password_extract(&output); |
| 160 | for attempt in 1..=3 { |
| 161 | screen.password_request(attempt, false); |
| 162 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 163 | screen.password_answer("wrong-password"); |
| 164 | } |
| 165 | screen.finish_password_job(Outcome::Failed); |
| 166 | assert_eq!( |
| 167 | screen.app.model.terminal.as_ref().unwrap().error, |
| 168 | Some(ErrorCode::WrongPassword) |
| 169 | ); |
| 170 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 171 | screen.open(&archive); |
| 172 | assert_reaped(&observed); |
| 173 | } |
| 174 | #[test] |
| 175 | fn cancel_password_extraction_waits_for_cleanup_without_writes() { |
| 176 | let scratch = Scratch::new(); |
| 177 | let archive = encrypted_fixture(&scratch, "aes1-store.zip"); |
| 178 | let output = make_directory(&scratch.path("output")); |
| 179 | fs::write(output.join("existing.txt"), b"preserve").unwrap(); |
| 180 | let (factory, observed) = observed_factory(); |
| 181 | let mut screen = Screen::new(factory); |
| 182 | screen.open(&archive); |
| 183 | screen.start_password_extract(&output); |
| 184 | screen.password_request(1, false); |
| 185 | screen.escape(); |
| 186 | screen.app.model.request_close(); |
| 187 | screen.app.model.confirm_close(); |
| 188 | assert!(matches!( |
| 189 | screen.app.model.close, |
| 190 | Close::Waiting | Close::Review |
| 191 | )); |
| 192 | screen.finish_password_job(Outcome::Cancelled); |
| 193 | assert_eq!(screen.app.model.close, Close::Review); |
| 194 | assert_eq!(fs::read(output.join("existing.txt")).unwrap(), b"preserve"); |
| 195 | assert_eq!(fs::read_dir(output).unwrap().count(), 1); |
| 196 | assert_reaped(&observed); |
| 197 | } |
| 198 | #[test] |
| 199 | fn cold_start_creates_aes_zip_then_reopens_and_extracts_with_password() { |
| 200 | let scratch = Scratch::new(); |
| 201 | let source = scratch.path("private.txt"); |
| 202 | let payload = b"Public synthetic password flow payload\n"; |
| 203 | fs::write(&source, payload).unwrap(); |
| 204 | let archive = scratch.path("encrypted.zip"); |
| 205 | let output = make_directory(&scratch.path("output")); |
| 206 | let (factory, observed) = observed_factory(); |
| 207 | let mut screen = Screen::new(factory); |
| 208 | assert!(!screen.app.model.encryption); |
| 209 | screen.start_encrypted_zip(&source, &archive); |
| 210 | screen.password_request(1, true); |
| 211 | assert!(!archive.exists()); |
| 212 | screen.password_answer(FIXTURE_PASSWORD); |
| 213 | screen.finish_password_job(Outcome::Completed); |
| 214 | let bytes = fs::read(&archive).unwrap(); |
| 215 | assert_eq!(&bytes[..4], b"PK\x03\x04"); |
| 216 | assert_eq!(u16::from_le_bytes(bytes[6..8].try_into().unwrap()) & 1, 1); |
| 217 | assert_eq!(u16::from_le_bytes(bytes[8..10].try_into().unwrap()), 99); |
| 218 | screen.open(&archive); |
| 219 | assert!(screen.app.model.entries.iter().all(|entry| entry.encrypted)); |
| 220 | screen.start_password_extract(&output); |
| 221 | screen.password_request(1, false); |
| 222 | screen.password_answer(FIXTURE_PASSWORD); |
| 223 | screen.finish_password_job(Outcome::Completed); |
| 224 | assert_eq!(fs::read(output.join("private.txt")).unwrap(), payload); |
| 225 | assert_eq!(fs::read(source).unwrap(), payload); |
| 226 | assert_reaped(&observed); |
| 227 | } |
| 228 | #[test] |
| 229 | fn cancel_password_creation_does_not_create_an_archive() { |
| 230 | let scratch = Scratch::new(); |
| 231 | let source = scratch.path("private.txt"); |
| 232 | fs::write(&source, b"preserve").unwrap(); |
| 233 | let archive = scratch.path("cancelled.zip"); |
| 234 | let (factory, observed) = observed_factory(); |
| 235 | let mut screen = Screen::new(factory); |
| 236 | screen.start_encrypted_zip(&source, &archive); |
| 237 | screen.password_request(1, true); |
| 238 | screen.escape(); |
| 239 | screen.finish_password_job(Outcome::Cancelled); |
| 240 | assert!(!archive.exists()); |
| 241 | assert_eq!(fs::read(source).unwrap(), b"preserve"); |
| 242 | assert_eq!(fs::read_dir(&scratch.0).unwrap().count(), 1); |
| 243 | assert_reaped(&observed); |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn oversized_utf8_password_stays_in_dialog_then_accepts_valid_input() { |
| 248 | let scratch = Scratch::new(); |
| 249 | let archive = encrypted_fixture(&scratch, "aes2-store.zip"); |
| 250 | let output = make_directory(&scratch.path("output")); |
| 251 | let (factory, observed) = observed_factory(); |
| 252 | let mut screen = Screen::new(factory); |
| 253 | screen.open(&archive); |
| 254 | screen.start_password_extract(&output); |
| 255 | let id = screen.password_request(1, false); |
| 256 | screen.password_answer(&"가".repeat(342)); |
| 257 | assert_eq!(screen.app.model.challenge, Some((id, 1, false))); |
| 258 | assert!( |
| 259 | screen |
| 260 | .render(vec![]) |
| 261 | .iter() |
| 262 | .any(|(text, _)| text.contains("1~1024바이트")) |
| 263 | ); |
| 264 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 265 | screen.password_answer(FIXTURE_PASSWORD); |
| 266 | screen.finish_password_job(Outcome::Completed); |
| 267 | assert_reaped(&observed); |
| 268 | } |
| 269 | |
| 270 | fn invalid_password_then_cancel(invalid: &str, close: bool) { |
| 271 | let scratch = Scratch::new(); |
| 272 | let source = scratch.path("private.txt"); |
| 273 | fs::write(&source, b"preserve").unwrap(); |
| 274 | let archive = scratch.path("cancelled.zip"); |
| 275 | let (factory, observed) = observed_factory(); |
| 276 | let mut screen = Screen::new(factory); |
| 277 | screen.start_encrypted_zip(&source, &archive); |
| 278 | let challenge = screen.password_request(1, true); |
| 279 | screen.password_answer(invalid); |
| 280 | assert_eq!(screen.app.model.challenge, Some((challenge, 1, true))); |
| 281 | if invalid.is_empty() { |
| 282 | assert!(screen.app.model.input_error.is_none()); |
| 283 | } else { |
| 284 | assert!( |
| 285 | screen |
| 286 | .render(vec![]) |
| 287 | .iter() |
| 288 | .any(|(text, _)| text.contains("1~1024바이트")) |
| 289 | ); |
| 290 | } |
| 291 | if close { |
| 292 | screen.app.model.request_close(); |
| 293 | screen.app.model.confirm_close(); |
| 294 | assert_eq!(screen.app.model.close, Close::Waiting); |
| 295 | assert!(!screen.app.model.confirmed); |
| 296 | } else { |
| 297 | screen.escape(); |
| 298 | } |
| 299 | screen.finish_password_job(Outcome::Cancelled); |
| 300 | assert_eq!(screen.app.model.status(), "작업 취소됨"); |
| 301 | assert_eq!( |
| 302 | screen.app.model.terminal.as_ref().unwrap().publication, |
| 303 | Publication::None |
| 304 | ); |
| 305 | assert_eq!(screen.app.model.terminal.as_ref().unwrap().created_count, 0); |
| 306 | assert_eq!( |
| 307 | screen |
| 308 | .app |
| 309 | .model |
| 310 | .terminal |
| 311 | .as_ref() |
| 312 | .unwrap() |
| 313 | .retained_partial_count, |
| 314 | 0 |
| 315 | ); |
| 316 | assert!(!archive.exists()); |
| 317 | assert_eq!(fs::read(&source).unwrap(), b"preserve"); |
| 318 | assert_eq!(fs::read_dir(&scratch.0).unwrap().count(), 1); |
| 319 | assert_reaped(&observed); |
| 320 | if close { |
| 321 | assert_eq!(screen.app.model.close, Close::Review); |
| 322 | screen.app.model.acknowledge_close(); |
| 323 | assert_eq!(screen.app.model.close, Close::Allowed); |
| 324 | } else { |
| 325 | screen.open(&encrypted_fixture(&scratch, "aes2-store.zip")); |
| 326 | assert_reaped(&observed); |
| 327 | } |
| 328 | } |
| 329 | #[test] |
| 330 | fn empty_password_then_cancel_confirms_worker_exit() { |
| 331 | invalid_password_then_cancel("", false); |
| 332 | } |
| 333 | #[test] |
| 334 | fn oversized_password_then_cancel_confirms_worker_exit() { |
| 335 | invalid_password_then_cancel(&"가".repeat(342), false); |
| 336 | } |
| 337 | #[test] |
| 338 | fn invalid_password_then_close_confirms_worker_exit() { |
| 339 | invalid_password_then_cancel("", true); |
| 340 | } |