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