| 1 | use super::*; |
| 2 | |
| 3 | fn waiting() -> (Model, FakeFactory) { |
| 4 | let mut f = FakeFactory::manual(); |
| 5 | let mut m = Model::default(); |
| 6 | assert!(m.begin( |
| 7 | Start::List { |
| 8 | archive: path("large.zip") |
| 9 | }, |
| 10 | &mut f |
| 11 | )); |
| 12 | f.state |
| 13 | .borrow_mut() |
| 14 | .frame(Event::Accepted { encryption: true }); |
| 15 | for _ in 0..4 { |
| 16 | m.poll(); |
| 17 | } |
| 18 | (m, f) |
| 19 | } |
| 20 | fn question(id: u64) -> ResourceChallenge { |
| 21 | ResourceChallenge { |
| 22 | id, |
| 23 | action: ActionKind::List, |
| 24 | scope: ResourceScope { |
| 25 | ratio: true, |
| 26 | ..Default::default() |
| 27 | }, |
| 28 | usage: ResourceUsage::default(), |
| 29 | soft_limits: Limits::default(), |
| 30 | ceiling: Limits::default(), |
| 31 | } |
| 32 | } |
| 33 | fn receive(m: &mut Model, f: &FakeFactory, event: Event) { |
| 34 | f.state.borrow_mut().frame(event); |
| 35 | for _ in 0..4 { |
| 36 | m.poll(); |
| 37 | } |
| 38 | } |
| 39 | fn fail( |
| 40 | m: &mut Model, |
| 41 | f: &FakeFactory, |
| 42 | publication: Publication, |
| 43 | created: u64, |
| 44 | partial: u64, |
| 45 | cleanup: Cleanup, |
| 46 | ) { |
| 47 | let mut s = f.state.borrow_mut(); |
| 48 | s.frame(Event::Terminal(Terminal { |
| 49 | outcome: Outcome::Failed, |
| 50 | error: Some(ErrorCode::Limit), |
| 51 | publication, |
| 52 | created_count: created, |
| 53 | retained_partial_count: partial, |
| 54 | durability: Durability::NotApplicable, |
| 55 | cleanup, |
| 56 | })); |
| 57 | s.queue.extend([ |
| 58 | HostEvent::StdoutEof, |
| 59 | HostEvent::StderrEof, |
| 60 | HostEvent::Exited { success: true }, |
| 61 | ]); |
| 62 | drop(s); |
| 63 | pump(m); |
| 64 | } |
| 65 | #[test] |
| 66 | fn reply_is_bound_to_job_question_scope_and_once_only() { |
| 67 | let (mut m, f) = waiting(); |
| 68 | receive(&mut m, &f, Event::ResourceRequired(question(1))); |
| 69 | assert!(!m.resource_reply(99, 1, true)); |
| 70 | assert!(!m.resource_reply(m.job_id().unwrap(), 2, true)); |
| 71 | assert!(m.resource_reply(m.job_id().unwrap(), 1, true)); |
| 72 | assert!(!m.resource_reply(m.job_id().unwrap(), 1, true)); |
| 73 | assert_eq!(f.state.borrow().resources, [(1, question(1).scope, true)]); |
| 74 | } |
| 75 | #[test] |
| 76 | fn cancel_during_question_never_grants_and_clears_prompt() { |
| 77 | let (mut m, f) = waiting(); |
| 78 | receive(&mut m, &f, Event::ResourceRequired(question(1))); |
| 79 | let job = m.job_id().unwrap(); |
| 80 | m.cancel(); |
| 81 | assert!(m.resource.is_none()); |
| 82 | assert!(!m.resource_reply(job, 1, true)); |
| 83 | f.state |
| 84 | .borrow_mut() |
| 85 | .finish(Outcome::Cancelled, Publication::None); |
| 86 | pump(&mut m); |
| 87 | assert!(f.state.borrow().resources.is_empty()); |
| 88 | assert!(m.confirmed); |
| 89 | } |
| 90 | #[test] |
| 91 | fn queued_question_after_cancel_does_not_turn_cancel_into_transport_failure() { |
| 92 | let (mut m, f) = waiting(); |
| 93 | m.cancel(); |
| 94 | receive(&mut m, &f, Event::ResourceRequired(question(1))); |
| 95 | assert!(m.resource.is_none()); |
| 96 | assert!(m.error.is_none()); |
| 97 | } |
| 98 | #[test] |
| 99 | fn wrong_action_limits_empty_scope_and_overlapping_questions_are_rejected() { |
| 100 | for mode in 0..6 { |
| 101 | let (mut m, f) = waiting(); |
| 102 | let mut q = question(1); |
| 103 | match mode { |
| 104 | 0 => q.action = ActionKind::Extract, |
| 105 | 1 => q.ceiling.output += 1, |
| 106 | 2 => q.scope = ResourceScope::default(), |
| 107 | 3 => q.id = 0, |
| 108 | 4 => receive( |
| 109 | &mut m, |
| 110 | &f, |
| 111 | Event::PasswordRequired { |
| 112 | challenge_id: 1, |
| 113 | attempt: 1, |
| 114 | encrypt: false, |
| 115 | }, |
| 116 | ), |
| 117 | _ => receive(&mut m, &f, Event::ResourceRequired(q.clone())), |
| 118 | } |
| 119 | receive(&mut m, &f, Event::ResourceRequired(q)); |
| 120 | assert!(m.error.is_some(), "{mode}"); |
| 121 | } |
| 122 | } |
| 123 | #[test] |
| 124 | fn old_challenge_and_repeated_scope_are_rejected_after_reply() { |
| 125 | for reused_id in [true, false] { |
| 126 | let (mut m, f) = waiting(); |
| 127 | receive(&mut m, &f, Event::ResourceRequired(question(1))); |
| 128 | assert!(m.resource_reply(m.job_id().unwrap(), 1, true)); |
| 129 | receive( |
| 130 | &mut m, |
| 131 | &f, |
| 132 | Event::ResourceRequired(question(if reused_id { 1 } else { 2 })), |
| 133 | ); |
| 134 | assert!(m.error.is_some()); |
| 135 | } |
| 136 | } |
| 137 | #[test] |
| 138 | fn only_confirmed_clean_empty_failure_offers_expanded_retry_once() { |
| 139 | for (publication, count, partial, cleanup, retry) in [ |
| 140 | (Publication::None, 0, 0, Cleanup::Complete, true), |
| 141 | (Publication::None, 1, 0, Cleanup::Complete, false), |
| 142 | (Publication::Partial, 1, 0, Cleanup::Complete, false), |
| 143 | (Publication::Uncertain, 0, 0, Cleanup::Complete, false), |
| 144 | (Publication::None, 0, 1, Cleanup::Complete, false), |
| 145 | (Publication::None, 0, 0, Cleanup::Unconfirmed, false), |
| 146 | ] { |
| 147 | let (mut m, mut f) = waiting(); |
| 148 | fail(&mut m, &f, publication, count, partial, cleanup); |
| 149 | assert_eq!(m.can_retry_expanded(), retry); |
| 150 | assert_eq!(m.retry_expanded(&mut f), retry); |
| 151 | if retry { |
| 152 | assert!(f.state.borrow().expanded); |
| 153 | receive(&mut m, &f, Event::Accepted { encryption: true }); |
| 154 | fail(&mut m, &f, Publication::None, 0, 0, Cleanup::Complete); |
| 155 | assert!(!m.can_retry_expanded()); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | #[test] |
| 160 | fn known_extract_size_requests_expansion_before_processing_and_estimates_unknown_separately() { |
| 161 | let mut m = Model::default(); |
| 162 | let mut f = FakeFactory::default(); |
| 163 | list(&mut m, &mut f); |
| 164 | m.entries[0].bytes = Some(Limits::default().output + 1); |
| 165 | f.state.borrow_mut().manual = true; |
| 166 | assert!(m.extract(path("out"), false, None, &mut f)); |
| 167 | assert!(f.state.borrow().expanded); |
| 168 | assert_eq!( |
| 169 | m.estimated_usage().estimated_bytes, |
| 170 | Some(Limits::default().output + 1) |
| 171 | ); |
| 172 | assert_eq!(m.estimated_usage().actual_bytes, None); |
| 173 | } |
| 174 | #[test] |
| 175 | fn entries_over_one_hundred_thousand_are_accepted_only_after_entry_approval() { |
| 176 | for approved in [false, true] { |
| 177 | let (mut m, f) = waiting(); |
| 178 | if approved { |
| 179 | fail(&mut m, &f, Publication::None, 0, 0, Cleanup::Complete); |
| 180 | let mut factory = f.clone(); |
| 181 | assert!(m.retry_expanded(&mut factory)); |
| 182 | receive(&mut m, &f, Event::Accepted { encryption: true }); |
| 183 | let mut q = question(1); |
| 184 | q.scope = ResourceScope { |
| 185 | output: true, |
| 186 | entries: true, |
| 187 | ratio: false, |
| 188 | }; |
| 189 | q.ceiling = Limits { |
| 190 | input: u64::MAX, |
| 191 | output: 64 * 1024 * 1024 * 1024, |
| 192 | entries: 200_000, |
| 193 | }; |
| 194 | receive(&mut m, &f, Event::ResourceRequired(q)); |
| 195 | assert!(m.resource_reply(m.job_id().unwrap(), 1, true)); |
| 196 | } |
| 197 | let cap = if approved { 200_000 } else { 100_000 }; |
| 198 | for id in 0..=cap { |
| 199 | assert!(m.error.is_none(), "{id}"); |
| 200 | receive( |
| 201 | &mut m, |
| 202 | &f, |
| 203 | Event::Entry(Entry { |
| 204 | id, |
| 205 | name: format!("{id}"), |
| 206 | kind: Kind::File, |
| 207 | bytes: Some(0), |
| 208 | packed: Some(0), |
| 209 | encrypted: false, |
| 210 | }), |
| 211 | ); |
| 212 | } |
| 213 | assert!(m.error.is_some()); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn output_history_ceiling_matches_approved_metadata_nodes() { |
| 219 | for permission in 0..4 { |
| 220 | let approved = permission != 0; |
| 221 | let mut m = Model::default(); |
| 222 | let mut f = FakeFactory::manual(); |
| 223 | assert!(m.begin( |
| 224 | Start::CreateZip { |
| 225 | sources: vec![(path("source"), "source".into())], |
| 226 | output_file: path("new.zip"), |
| 227 | method: Method::Store, |
| 228 | aes256: false |
| 229 | }, |
| 230 | &mut f |
| 231 | )); |
| 232 | receive(&mut m, &f, Event::Accepted { encryption: true }); |
| 233 | if approved { |
| 234 | fail(&mut m, &f, Publication::None, 0, 0, Cleanup::Complete); |
| 235 | assert!(m.retry_expanded(&mut f)); |
| 236 | receive(&mut m, &f, Event::Accepted { encryption: true }); |
| 237 | let mut q = question(1); |
| 238 | q.action = ActionKind::CreateZip; |
| 239 | q.scope = ResourceScope { |
| 240 | output: permission == 1, |
| 241 | entries: permission == 2, |
| 242 | ratio: permission == 3, |
| 243 | }; |
| 244 | q.ceiling = Limits::expanded(ActionKind::CreateZip); |
| 245 | receive(&mut m, &f, Event::ResourceRequired(q)); |
| 246 | assert!(m.resource_reply(m.job_id().unwrap(), 1, true)); |
| 247 | } |
| 248 | let cap = if approved { 262_144 } else { 100_000 }; |
| 249 | for n in 0..=cap { |
| 250 | assert!(m.error.is_none(), "{n}"); |
| 251 | receive( |
| 252 | &mut m, |
| 253 | &f, |
| 254 | Event::Output { |
| 255 | entry_id: None, |
| 256 | relative_name: format!("{n}"), |
| 257 | disposition: Disposition::Created, |
| 258 | }, |
| 259 | ); |
| 260 | } |
| 261 | assert_eq!(m.outputs.len(), cap); |
| 262 | assert!(m.error.is_some()); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | #[test] |
| 267 | fn approved_implicit_directories_do_not_fail_valid_extraction() { |
| 268 | let mut m = Model::default(); |
| 269 | let mut f = FakeFactory::default(); |
| 270 | list(&mut m, &mut f); |
| 271 | m.entries[0].bytes = Some(Limits::default().output + 1); |
| 272 | f.state.borrow_mut().manual = true; |
| 273 | assert!(m.extract(path("out"), false, None, &mut f)); |
| 274 | receive(&mut m, &f, Event::Accepted { encryption: true }); |
| 275 | let mut q = question(1); |
| 276 | q.action = ActionKind::Extract; |
| 277 | q.scope = ResourceScope { |
| 278 | output: true, |
| 279 | entries: true, |
| 280 | ratio: false, |
| 281 | }; |
| 282 | q.ceiling = Limits { |
| 283 | input: u64::MAX, |
| 284 | output: 64 * 1024 * 1024 * 1024, |
| 285 | entries: 200_000, |
| 286 | }; |
| 287 | receive(&mut m, &f, Event::ResourceRequired(q)); |
| 288 | assert!(m.resource_reply(m.job_id().unwrap(), 1, true)); |
| 289 | for id in 0..100_001 { |
| 290 | for name in [format!("d{id}"), format!("d{id}/f")] { |
| 291 | receive( |
| 292 | &mut m, |
| 293 | &f, |
| 294 | Event::Output { |
| 295 | entry_id: None, |
| 296 | relative_name: name, |
| 297 | disposition: Disposition::Created, |
| 298 | }, |
| 299 | ); |
| 300 | } |
| 301 | } |
| 302 | receive( |
| 303 | &mut m, |
| 304 | &f, |
| 305 | Event::Terminal(Terminal { |
| 306 | outcome: Outcome::Completed, |
| 307 | error: None, |
| 308 | publication: Publication::Complete, |
| 309 | created_count: 200_002, |
| 310 | retained_partial_count: 0, |
| 311 | durability: Durability::Synced, |
| 312 | cleanup: Cleanup::Complete, |
| 313 | }), |
| 314 | ); |
| 315 | f.state.borrow_mut().queue.extend([ |
| 316 | HostEvent::StdoutEof, |
| 317 | HostEvent::StderrEof, |
| 318 | HostEvent::Exited { success: true }, |
| 319 | ]); |
| 320 | pump(&mut m); |
| 321 | assert!(m.error.is_none(), "{:?}", m.error); |
| 322 | assert!(m.confirmed); |
| 323 | assert_eq!(m.outputs.len(), 200_002); |
| 324 | assert!(!m.can_retry_expanded()); |
| 325 | } |