| 1 | use std::{cell::RefCell, collections::VecDeque, rc::Rc}; |
| 2 | use zipelo_ui_memory_app::archive_ui::*; |
| 3 | |
| 4 | #[derive(Default)] |
| 5 | pub struct State { |
| 6 | pub requests: Vec<(u64, &'static str)>, |
| 7 | pub starts: Vec<Start>, |
| 8 | pub password_seen: bool, |
| 9 | pub expanded: bool, |
| 10 | pub resources: Vec<(u64, ResourceScope, bool)>, |
| 11 | pub conflict_replies: Vec<(u64, ConflictChoice, bool)>, |
| 12 | /// 마지막으로 받은 암호 원문. 화면에서 입력한 암호가 그대로 전달됐는지 확인할 때 씀. |
| 13 | pub password: Vec<u8>, |
| 14 | pub job: u64, |
| 15 | sequence: u64, |
| 16 | pub queue: VecDeque<HostEvent>, |
| 17 | pub manual: bool, |
| 18 | pub list_format: Option<Format>, |
| 19 | pub fail_start: bool, |
| 20 | pub dropped_connections: usize, |
| 21 | pause: bool, |
| 22 | } |
| 23 | #[derive(Default, Clone)] |
| 24 | pub struct FakeFactory { |
| 25 | pub state: Rc<RefCell<State>>, |
| 26 | } |
| 27 | impl FakeFactory { |
| 28 | pub fn manual() -> Self { |
| 29 | let f = Self::default(); |
| 30 | f.state.borrow_mut().manual = true; |
| 31 | f |
| 32 | } |
| 33 | } |
| 34 | impl State { |
| 35 | pub fn frame(&mut self, event: Event) { |
| 36 | let sequence = self.sequence; |
| 37 | self.sequence += 1; |
| 38 | self.queue.push_back(HostEvent::Frame { |
| 39 | job_id: self.job, |
| 40 | sequence, |
| 41 | event, |
| 42 | }); |
| 43 | } |
| 44 | pub fn finish(&mut self, outcome: Outcome, publication: Publication) { |
| 45 | self.frame(Event::Terminal(Terminal { |
| 46 | outcome, |
| 47 | error: None, |
| 48 | publication, |
| 49 | created_count: if publication == Publication::Complete { |
| 50 | 1 |
| 51 | } else { |
| 52 | 0 |
| 53 | }, |
| 54 | retained_partial_count: 0, |
| 55 | durability: Durability::Synced, |
| 56 | cleanup: Cleanup::Complete, |
| 57 | })); |
| 58 | self.queue.push_back(HostEvent::StdoutEof); |
| 59 | self.queue.push_back(HostEvent::StderrEof); |
| 60 | self.queue.push_back(HostEvent::Exited { success: true }); |
| 61 | } |
| 62 | fn complete_action(&mut self) { |
| 63 | match self.starts.last().cloned().unwrap() { |
| 64 | Start::List { archive } => { |
| 65 | self.frame(Event::Entry(Entry { |
| 66 | id: 0, |
| 67 | name: "읽어주세요.txt".into(), |
| 68 | kind: Kind::File, |
| 69 | bytes: Some(31), |
| 70 | packed: Some(54), |
| 71 | encrypted: false, |
| 72 | })); |
| 73 | let format = self.list_format.unwrap_or_else(|| { |
| 74 | if archive.extension().is_some_and(|x| x == "gz") { |
| 75 | Format::Gzip |
| 76 | } else { |
| 77 | Format::Zip |
| 78 | } |
| 79 | }); |
| 80 | self.frame(Event::ListEnd { |
| 81 | entry_count: 1, |
| 82 | list_id: ListId { |
| 83 | catalog_version: 1, |
| 84 | format, |
| 85 | source_object: SourceObject::Unix { |
| 86 | device: 1, |
| 87 | inode: 1, |
| 88 | }, |
| 89 | archive_bytes: 54, |
| 90 | archive_sha256: [7; 32], |
| 91 | }, |
| 92 | }); |
| 93 | self.finish(Outcome::Completed, Publication::None); |
| 94 | } |
| 95 | Start::Preview { entry_id, .. } => { |
| 96 | use sha2::{Digest, Sha256}; |
| 97 | let data = "미리보기 내용".as_bytes().to_vec(); |
| 98 | self.frame(Event::PreviewBegin(PreviewInfo { |
| 99 | entry_id, |
| 100 | format: PreviewFormat::Utf8Text, |
| 101 | total_bytes: data.len() as u64, |
| 102 | width: 0, |
| 103 | height: 0, |
| 104 | })); |
| 105 | self.frame(Event::PreviewChunk { |
| 106 | entry_id, |
| 107 | offset: 0, |
| 108 | data: data.clone(), |
| 109 | }); |
| 110 | self.frame(Event::PreviewEnd { |
| 111 | entry_id, |
| 112 | total_bytes: data.len() as u64, |
| 113 | sha256: Sha256::digest(&data).into(), |
| 114 | }); |
| 115 | self.frame(Event::Terminal(Terminal { |
| 116 | outcome: Outcome::Completed, |
| 117 | error: None, |
| 118 | publication: Publication::None, |
| 119 | created_count: 0, |
| 120 | retained_partial_count: 0, |
| 121 | durability: Durability::NotApplicable, |
| 122 | cleanup: Cleanup::Complete, |
| 123 | })); |
| 124 | self.queue.push_back(HostEvent::StdoutEof); |
| 125 | self.queue.push_back(HostEvent::StderrEof); |
| 126 | self.queue.push_back(HostEvent::Exited { success: true }); |
| 127 | } |
| 128 | _ => { |
| 129 | self.frame(Event::Progress { |
| 130 | stage: Stage::Write, |
| 131 | processed: 31, |
| 132 | total: Some(31), |
| 133 | completed_entries: 1, |
| 134 | }); |
| 135 | self.frame(Event::Output { |
| 136 | entry_id: Some(0), |
| 137 | relative_name: "읽어주세요.txt".into(), |
| 138 | disposition: Disposition::Created, |
| 139 | }); |
| 140 | self.finish(Outcome::Completed, Publication::Complete); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | struct FakeConnection { |
| 146 | state: Rc<RefCell<State>>, |
| 147 | } |
| 148 | impl Drop for FakeConnection { |
| 149 | fn drop(&mut self) { |
| 150 | self.state.borrow_mut().dropped_connections += 1; |
| 151 | } |
| 152 | } |
| 153 | impl Factory for FakeFactory { |
| 154 | fn connect(&mut self, job: u64) -> Result<Box<dyn Connection>, String> { |
| 155 | let mut s = self.state.borrow_mut(); |
| 156 | s.job = job; |
| 157 | s.sequence = 0; |
| 158 | s.queue.clear(); |
| 159 | s.pause = false; |
| 160 | Ok(Box::new(FakeConnection { |
| 161 | state: self.state.clone(), |
| 162 | })) |
| 163 | } |
| 164 | } |
| 165 | impl Connection for FakeConnection { |
| 166 | fn send(&mut self, request: Request) -> Result<(), String> { |
| 167 | let mut s = self.state.borrow_mut(); |
| 168 | assert_eq!(s.job, request.job_id); |
| 169 | if matches!(&request.command, Command::StartExpanded(_)) { |
| 170 | s.expanded = true; |
| 171 | } |
| 172 | match request.command { |
| 173 | Command::Start(start) | Command::StartExpanded(start) => { |
| 174 | if let Start::Extract { |
| 175 | list_id, |
| 176 | gzip_output_name, |
| 177 | .. |
| 178 | } = &start |
| 179 | { |
| 180 | let valid = match list_id.format { |
| 181 | Format::Gzip => gzip_output_name.as_deref().is_some_and(valid_leaf), |
| 182 | _ => gzip_output_name.is_none(), |
| 183 | }; |
| 184 | if !valid { |
| 185 | return Err("형식에 맞지 않는 GZIP 출력명".into()); |
| 186 | } |
| 187 | } |
| 188 | s.requests.push((request.sequence, "start")); |
| 189 | s.starts.push(start.clone()); |
| 190 | if s.fail_start { |
| 191 | return Err("Start 전송 실패".into()); |
| 192 | } |
| 193 | if !s.manual { |
| 194 | s.frame(Event::Accepted { encryption: false }); |
| 195 | s.frame(Event::Progress { |
| 196 | stage: Stage::Scan, |
| 197 | processed: 0, |
| 198 | total: None, |
| 199 | completed_entries: 0, |
| 200 | }); |
| 201 | if matches!(&start,Start::List {archive} if archive.file_name().is_some_and(|n|n.to_string_lossy().contains("locked"))) |
| 202 | { |
| 203 | s.frame(Event::PasswordRequired { |
| 204 | challenge_id: 17, |
| 205 | attempt: 1, |
| 206 | encrypt: false, |
| 207 | }); |
| 208 | } else { |
| 209 | s.complete_action(); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | Command::Password { |
| 214 | challenge_id, |
| 215 | secret, |
| 216 | } => { |
| 217 | assert_eq!(challenge_id, 17); |
| 218 | assert!(!secret.as_bytes().is_empty()); |
| 219 | s.password_seen = true; |
| 220 | s.password = secret.as_bytes().to_vec(); |
| 221 | s.requests.push((request.sequence, "password")); |
| 222 | if !s.manual { |
| 223 | s.complete_action(); |
| 224 | } |
| 225 | } |
| 226 | Command::ResourceReply { |
| 227 | challenge_id, |
| 228 | scope, |
| 229 | allow, |
| 230 | } => { |
| 231 | s.resources.push((challenge_id, scope, allow)); |
| 232 | } |
| 233 | Command::ConflictReply { |
| 234 | challenge_id, |
| 235 | choice, |
| 236 | apply_to_remaining, |
| 237 | } => { |
| 238 | s.requests.push((request.sequence, "conflict")); |
| 239 | s.conflict_replies |
| 240 | .push((challenge_id, choice, apply_to_remaining)); |
| 241 | } |
| 242 | Command::Cancel => { |
| 243 | s.requests.push((request.sequence, "cancel")); |
| 244 | if !s.manual { |
| 245 | let consumed = s |
| 246 | .queue |
| 247 | .iter() |
| 248 | .filter(|e| matches!(e, HostEvent::Frame { .. })) |
| 249 | .count() as u64; |
| 250 | s.sequence -= consumed; |
| 251 | s.queue.clear(); |
| 252 | s.frame(Event::CancelAck { |
| 253 | request_sequence: request.sequence, |
| 254 | status: CancelStatus::Requested, |
| 255 | }); |
| 256 | s.finish(Outcome::Cancelled, Publication::None); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | Ok(()) |
| 261 | } |
| 262 | fn poll(&mut self) -> Option<HostEvent> { |
| 263 | let mut s = self.state.borrow_mut(); |
| 264 | if s.pause { |
| 265 | s.pause = false; |
| 266 | return None; |
| 267 | } |
| 268 | s.pause = true; |
| 269 | s.queue.pop_front() |
| 270 | } |
| 271 | } |