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