| 1 | use super::*; |
| 2 | |
| 3 | const ARCHIVE: &[u8] = b"/archive"; |
| 4 | const DESTINATION: &[u8] = b"/destination"; |
| 5 | |
| 6 | fn request(count: usize, kind: u16, ceiling: u32) -> Message { |
| 7 | let start = Start { |
| 8 | action: Action::Extract { |
| 9 | archive: NativePath::Unix(ARCHIVE.to_vec()), |
| 10 | destination: NativePath::Unix(DESTINATION.to_vec()), |
| 11 | selection: Selection::EntryIds((0..count as u32).collect()), |
| 12 | list_id: None, |
| 13 | gzip_output_name: None, |
| 14 | }, |
| 15 | limits: Limits::default(), |
| 16 | }; |
| 17 | match kind { |
| 18 | 1 => Message::Start(start), |
| 19 | 4 => Message::StartInteractive(start), |
| 20 | 6 => Message::StartGuarded(GuardedStart { |
| 21 | ceiling: Limits { |
| 22 | entries: ceiling, |
| 23 | ..start.limits |
| 24 | }, |
| 25 | start, |
| 26 | interactive: true, |
| 27 | }), |
| 28 | _ => unreachable!(), |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | fn encoded(message: &Message) -> Vec<u8> { |
| 33 | let mut frame = Vec::new(); |
| 34 | Writer::new(&mut frame, 17).send(message).unwrap(); |
| 35 | frame |
| 36 | } |
| 37 | |
| 38 | fn resize_body(frame: &mut [u8]) { |
| 39 | let length = (frame.len() - 32) as u32; |
| 40 | frame[8..12].copy_from_slice(&length.to_le_bytes()); |
| 41 | } |
| 42 | |
| 43 | fn ids_offset() -> usize { |
| 44 | 32 + 1 + 1 + 4 + ARCHIVE.len() + 1 + 4 + DESTINATION.len() + 1 |
| 45 | } |
| 46 | |
| 47 | fn unchecked_frame(count: usize, kind: u16, ceiling: u32) -> Vec<u8> { |
| 48 | let mut frame = encoded(&request(0, kind, ceiling)); |
| 49 | let at = ids_offset(); |
| 50 | let mut ids = (count as u32).to_le_bytes().to_vec(); |
| 51 | for id in 0..count as u32 { |
| 52 | ids.extend(id.to_le_bytes()); |
| 53 | } |
| 54 | frame.splice(at..at + 4, ids); |
| 55 | resize_body(&mut frame); |
| 56 | frame |
| 57 | } |
| 58 | |
| 59 | fn boundary(count: usize, kind: u16, ceiling: u32, accepted: bool) { |
| 60 | let mut frame = Vec::new(); |
| 61 | let result = Writer::new(&mut frame, 17).send(&request(count, kind, ceiling)); |
| 62 | assert_eq!(result.is_ok(), accepted, "encode {kind} {ceiling} {count}"); |
| 63 | if !accepted { |
| 64 | assert!(frame.is_empty()); |
| 65 | } |
| 66 | let raw = unchecked_frame(count, kind, ceiling); |
| 67 | let result = Reader::new(raw.as_slice()).read(); |
| 68 | assert_eq!(result.is_ok(), accepted, "decode {kind} {ceiling} {count}"); |
| 69 | if accepted { |
| 70 | assert_eq!(frame, raw); |
| 71 | assert!(frame.len() - 32 <= MAX_BODY); |
| 72 | let (_, _, decoded) = result.unwrap().unwrap(); |
| 73 | let start = match decoded { |
| 74 | Message::Start(start) | Message::StartInteractive(start) => start, |
| 75 | Message::StartGuarded(start) => start.start, |
| 76 | other => panic!("{other:?}"), |
| 77 | }; |
| 78 | let Action::Extract { |
| 79 | selection: Selection::EntryIds(ids), |
| 80 | .. |
| 81 | } = start.action |
| 82 | else { |
| 83 | panic!("missing selection"); |
| 84 | }; |
| 85 | assert_eq!(ids.len(), count); |
| 86 | assert!( |
| 87 | ids.iter() |
| 88 | .enumerate() |
| 89 | .all(|(index, id)| *id as usize == index) |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn expanded_selection_round_trips_exact_policy_boundaries() { |
| 96 | for count in [32_768, 32_769, 200_000, 200_001] { |
| 97 | boundary(count, 6, 200_000, count <= 200_000); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn legacy_and_unexpanded_requests_keep_the_original_selection_bound() { |
| 103 | for kind in [1, 4, 6] { |
| 104 | for count in [32_768, 32_769, 200_000, 200_001] { |
| 105 | boundary(count, kind, 10_000, count <= 32_768); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn guarded_selection_honors_lower_ceilings_and_rejects_truncated_counts() { |
| 112 | boundary(40_000, 6, 40_000, true); |
| 113 | boundary(40_001, 6, 40_000, false); |
| 114 | let mut short = unchecked_frame(0, 6, 200_000); |
| 115 | short[ids_offset()..ids_offset() + 4].copy_from_slice(&u32::MAX.to_le_bytes()); |
| 116 | assert!(Reader::new(short.as_slice()).read().is_err()); |
| 117 | let mut frame = unchecked_frame(200_000, 6, 200_000); |
| 118 | frame.truncate(frame.len() - 1); |
| 119 | assert!(Reader::new(frame.as_slice()).read().is_err()); |
| 120 | resize_body(&mut frame); |
| 121 | assert!(Reader::new(frame.as_slice()).read().is_err()); |
| 122 | let mut invalid_ceiling = unchecked_frame(32_769, 6, 200_000); |
| 123 | let at = invalid_ceiling.len() - 5; |
| 124 | invalid_ceiling[at..at + 4].copy_from_slice(&200_001u32.to_le_bytes()); |
| 125 | assert!(Reader::new(invalid_ceiling.as_slice()).read().is_err()); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn expanded_selection_does_not_expand_paths_sources_or_frame_bounds() { |
| 130 | for path in [ |
| 131 | NativePath::Unix(vec![b'a'; 32_769]), |
| 132 | NativePath::Windows(vec![b'a' as u16; 32_769]), |
| 133 | ] { |
| 134 | let Message::StartGuarded(mut guarded) = request(0, 6, 200_000) else { |
| 135 | unreachable!(); |
| 136 | }; |
| 137 | if let Action::Extract { archive, .. } = &mut guarded.start.action { |
| 138 | *archive = path; |
| 139 | } |
| 140 | assert!( |
| 141 | Writer::new(Vec::new(), 17) |
| 142 | .send(&Message::StartGuarded(guarded)) |
| 143 | .is_err() |
| 144 | ); |
| 145 | } |
| 146 | let mut long_path = unchecked_frame(0, 6, 200_000); |
| 147 | let at = 32 + 1 + 1; |
| 148 | let mut value = 32_769u32.to_le_bytes().to_vec(); |
| 149 | value.extend(vec![b'a'; 32_769]); |
| 150 | long_path.splice(at..at + 4 + ARCHIVE.len(), value); |
| 151 | resize_body(&mut long_path); |
| 152 | assert!(Reader::new(long_path.as_slice()).read().is_err()); |
| 153 | |
| 154 | let source = Source { |
| 155 | path: NativePath::Unix(b"/s".to_vec()), |
| 156 | archive_name: "s".into(), |
| 157 | }; |
| 158 | let create = |sources| { |
| 159 | Message::StartGuarded(GuardedStart { |
| 160 | start: Start { |
| 161 | action: Action::CreateZip { |
| 162 | sources, |
| 163 | output_file: NativePath::Unix(b"/new.zip".to_vec()), |
| 164 | method: Method::Store, |
| 165 | encryption: Encryption::None, |
| 166 | }, |
| 167 | limits: Limits::default(), |
| 168 | }, |
| 169 | ceiling: Limits { |
| 170 | entries: 200_000, |
| 171 | ..Limits::default() |
| 172 | }, |
| 173 | interactive: false, |
| 174 | }) |
| 175 | }; |
| 176 | assert!( |
| 177 | Writer::new(Vec::new(), 17) |
| 178 | .send(&create(vec![source; 32_769])) |
| 179 | .is_err() |
| 180 | ); |
| 181 | let mut sources = encoded(&create(Vec::new())); |
| 182 | let mut value = 32_769u32.to_le_bytes().to_vec(); |
| 183 | for _ in 0..32_769 { |
| 184 | value.extend([1, 2, 0, 0, 0, b'/', b's', 1, 0, 0, 0, b's']); |
| 185 | } |
| 186 | sources.splice(33..37, value); |
| 187 | resize_body(&mut sources); |
| 188 | assert!(Reader::new(sources.as_slice()).read().is_err()); |
| 189 | |
| 190 | let mut oversized = unchecked_frame(0, 6, 200_000); |
| 191 | oversized[8..12].copy_from_slice(&((MAX_BODY + 1) as u32).to_le_bytes()); |
| 192 | assert!(Reader::new(oversized.as_slice()).read().is_err()); |
| 193 | } |