| 1 | use super::*; |
| 2 | use worker_protocol::protocol::EntryKind; |
| 3 | |
| 4 | fn begin() -> (Model, FakeFactory) { |
| 5 | let mut model = Model::default(); |
| 6 | let mut factory = FakeFactory::manual(); |
| 7 | assert!(model.begin( |
| 8 | Start::RecoverZip { |
| 9 | archive: path("damaged.zip"), |
| 10 | output_file: path("recovered.zip"), |
| 11 | }, |
| 12 | &mut factory |
| 13 | )); |
| 14 | factory |
| 15 | .state |
| 16 | .borrow_mut() |
| 17 | .frame(Event::Accepted { encryption: false }); |
| 18 | (model, factory) |
| 19 | } |
| 20 | fn entry() -> RecoveryEntry { |
| 21 | RecoveryEntry { |
| 22 | candidate_id: 0, |
| 23 | source_offset: 0, |
| 24 | name: Some("한글.txt".into()), |
| 25 | kind: Some(EntryKind::File), |
| 26 | status: RecoveryStatus::Verified, |
| 27 | reason: RecoveryReason::HeaderRebuilt, |
| 28 | declared_size: Some(4), |
| 29 | verified_size: Some(4), |
| 30 | sha256: Some([1; 32]), |
| 31 | } |
| 32 | } |
| 33 | fn summary(partial: bool) -> RecoveryEnd { |
| 34 | RecoveryEnd { |
| 35 | mode: if partial { |
| 36 | RecoveryMode::LocalChain |
| 37 | } else { |
| 38 | RecoveryMode::CentralDirectory |
| 39 | }, |
| 40 | source_bytes: 128, |
| 41 | source_sha256: [2; 32], |
| 42 | known_entries: (!partial).then_some(1), |
| 43 | verified_entries: 1, |
| 44 | lost_entries: 0, |
| 45 | unverified_entries: 0, |
| 46 | unknown_loss: partial, |
| 47 | } |
| 48 | } |
| 49 | fn result(partial: bool) -> Terminal { |
| 50 | Terminal { |
| 51 | outcome: if partial { |
| 52 | Outcome::Failed |
| 53 | } else { |
| 54 | Outcome::Completed |
| 55 | }, |
| 56 | error: partial.then_some(ErrorCode::RecoveryIncomplete), |
| 57 | publication: Publication::Complete, |
| 58 | created_count: 1, |
| 59 | retained_partial_count: 0, |
| 60 | durability: Durability::Synced, |
| 61 | cleanup: Cleanup::Complete, |
| 62 | } |
| 63 | } |
| 64 | fn reports(factory: &FakeFactory, partial: bool) { |
| 65 | let mut s = factory.state.borrow_mut(); |
| 66 | s.frame(Event::RecoveryEntry(entry())); |
| 67 | s.frame(Event::RecoveryEnd(summary(partial))); |
| 68 | s.frame(Event::Output { |
| 69 | entry_id: None, |
| 70 | relative_name: "recovered.zip".into(), |
| 71 | disposition: Disposition::Created, |
| 72 | }); |
| 73 | s.frame(Event::Terminal(result(partial))); |
| 74 | } |
| 75 | fn eof(factory: &FakeFactory) { |
| 76 | factory.state.borrow_mut().queue.extend([ |
| 77 | HostEvent::StdoutEof, |
| 78 | HostEvent::StderrEof, |
| 79 | HostEvent::Exited { success: true }, |
| 80 | ]); |
| 81 | } |
| 82 | #[test] |
| 83 | fn recovery_requires_terminal_both_eofs_and_reap_even_for_saved_partial() { |
| 84 | for partial in [false, true] { |
| 85 | let (mut model, factory) = begin(); |
| 86 | reports(&factory, partial); |
| 87 | for _ in 0..20 { |
| 88 | model.poll(); |
| 89 | } |
| 90 | assert!(model.busy()); |
| 91 | assert!(!model.confirmed); |
| 92 | eof(&factory); |
| 93 | pump(&mut model); |
| 94 | assert!(model.confirmed, "{:?}", model.error); |
| 95 | assert_eq!(model.recovery_end.as_ref().unwrap().unknown_loss, partial); |
| 96 | assert!(model.entries.is_empty()); |
| 97 | assert!(model.list_id.is_none()); |
| 98 | assert!(!model.can_retry_expanded()); |
| 99 | } |
| 100 | } |
| 101 | #[test] |
| 102 | fn recovery_rejects_wrong_ids_offsets_names_and_unverified_hashes() { |
| 103 | for change in 0..5 { |
| 104 | let (mut model, factory) = begin(); |
| 105 | let mut e = entry(); |
| 106 | match change { |
| 107 | 0 => e.candidate_id = 1, |
| 108 | 1 => e.name = Some("../outside".into()), |
| 109 | 2 => e.status = RecoveryStatus::Lost, |
| 110 | 3 => e.verified_size = Some(5), |
| 111 | _ => e.name = Some("x".repeat(4097)), |
| 112 | } |
| 113 | factory.state.borrow_mut().frame(Event::RecoveryEntry(e)); |
| 114 | for _ in 0..5 { |
| 115 | model.poll(); |
| 116 | } |
| 117 | assert!(model.error.is_some(), "{change}"); |
| 118 | assert!(model.recovery_entries.is_empty()); |
| 119 | } |
| 120 | } |
| 121 | #[test] |
| 122 | fn recovery_rejects_duplicate_end_wrong_counts_and_false_success() { |
| 123 | for change in 0..4 { |
| 124 | let (mut model, factory) = begin(); |
| 125 | let mut s = factory.state.borrow_mut(); |
| 126 | s.frame(Event::RecoveryEntry(entry())); |
| 127 | let mut end = summary(change == 3); |
| 128 | if change == 0 { |
| 129 | end.verified_entries = 2; |
| 130 | } |
| 131 | if change == 1 { |
| 132 | end.source_bytes = 0; |
| 133 | } |
| 134 | s.frame(Event::RecoveryEnd(end)); |
| 135 | if change == 2 { |
| 136 | s.frame(Event::RecoveryEnd(summary(false))); |
| 137 | } |
| 138 | s.frame(Event::Output { |
| 139 | entry_id: None, |
| 140 | relative_name: "recovered.zip".into(), |
| 141 | disposition: Disposition::Created, |
| 142 | }); |
| 143 | s.frame(Event::Terminal(result(false))); |
| 144 | drop(s); |
| 145 | eof(&factory); |
| 146 | pump(&mut model); |
| 147 | assert!(!model.confirmed, "{change}"); |
| 148 | assert!(model.error.is_some()); |
| 149 | } |
| 150 | } |
| 151 | #[test] |
| 152 | fn recovery_summary_is_not_a_saved_artifact() { |
| 153 | let (mut model, factory) = begin(); |
| 154 | let mut s = factory.state.borrow_mut(); |
| 155 | s.frame(Event::RecoveryEntry(entry())); |
| 156 | s.frame(Event::RecoveryEnd(summary(false))); |
| 157 | s.frame(Event::Terminal(result(false))); |
| 158 | drop(s); |
| 159 | eof(&factory); |
| 160 | pump(&mut model); |
| 161 | assert!(!model.confirmed); |
| 162 | assert!(model.outputs.is_empty()); |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn recovery_retains_temporary_paths_with_and_without_a_publication_report() { |
| 167 | for final_disposition in [ |
| 168 | None, |
| 169 | Some(Disposition::Created), |
| 170 | Some(Disposition::CommitUncertain), |
| 171 | ] { |
| 172 | let (mut model, factory) = begin(); |
| 173 | let mut state = factory.state.borrow_mut(); |
| 174 | state.frame(Event::RecoveryEntry(entry())); |
| 175 | state.frame(Event::RecoveryEnd(summary(false))); |
| 176 | if let Some(disposition) = final_disposition { |
| 177 | state.frame(Event::Output { |
| 178 | entry_id: None, |
| 179 | relative_name: "recovered.zip".into(), |
| 180 | disposition, |
| 181 | }); |
| 182 | } |
| 183 | state.frame(Event::Output { |
| 184 | entry_id: None, |
| 185 | relative_name: ".zipelo-42-1.tmp".into(), |
| 186 | disposition: Disposition::PartialRetained, |
| 187 | }); |
| 188 | state.frame(Event::Terminal(Terminal { |
| 189 | outcome: Outcome::Failed, |
| 190 | error: Some(ErrorCode::Io), |
| 191 | publication: match final_disposition { |
| 192 | None => Publication::None, |
| 193 | Some(Disposition::Created) => Publication::Partial, |
| 194 | _ => Publication::Uncertain, |
| 195 | }, |
| 196 | created_count: u64::from(final_disposition == Some(Disposition::Created)), |
| 197 | retained_partial_count: 1, |
| 198 | durability: Durability::Unconfirmed, |
| 199 | cleanup: Cleanup::Failed, |
| 200 | })); |
| 201 | drop(state); |
| 202 | eof(&factory); |
| 203 | pump(&mut model); |
| 204 | assert!(model.confirmed, "{:?}", model.error); |
| 205 | assert!( |
| 206 | model |
| 207 | .outputs |
| 208 | .contains(&(".zipelo-42-1.tmp".into(), Disposition::PartialRetained)) |
| 209 | ); |
| 210 | assert!(!model.can_retry_expanded()); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn recovery_output_validation_still_rejects_wrong_fields_and_duplicates() { |
| 216 | for case in 0..6 { |
| 217 | let (mut model, factory) = begin(); |
| 218 | let mut state = factory.state.borrow_mut(); |
| 219 | state.frame(Event::RecoveryEntry(entry())); |
| 220 | state.frame(Event::RecoveryEnd(summary(false))); |
| 221 | let (entry_id, name, disposition) = match case { |
| 222 | 0 => (Some(0), ".zipelo-42-1.tmp", Disposition::PartialRetained), |
| 223 | 1 => (None, "../outside.tmp", Disposition::PartialRetained), |
| 224 | 2 => (None, "other.zip", Disposition::Created), |
| 225 | 3 => (None, "recovered.zip", Disposition::PartialRetained), |
| 226 | 4 => (None, "recovered.zip", Disposition::Created), |
| 227 | _ => (None, ".zipelo-42-1.tmp", Disposition::PartialRetained), |
| 228 | }; |
| 229 | let report = Event::Output { |
| 230 | entry_id, |
| 231 | relative_name: name.into(), |
| 232 | disposition, |
| 233 | }; |
| 234 | state.frame(report.clone()); |
| 235 | if case >= 4 { |
| 236 | state.frame(report); |
| 237 | } |
| 238 | drop(state); |
| 239 | for _ in 0..20 { |
| 240 | model.poll(); |
| 241 | } |
| 242 | assert!(model.error.is_some(), "{case}"); |
| 243 | } |
| 244 | } |