| 1 | use super::*; |
| 2 | |
| 3 | fn sample_start() -> Start { |
| 4 | Start { |
| 5 | action: Action::Extract { |
| 6 | archive: NativePath::Unix(b"/archive".to_vec()), |
| 7 | destination: NativePath::Unix(b"/output".to_vec()), |
| 8 | selection: Selection::All, |
| 9 | list_id: None, |
| 10 | gzip_output_name: None, |
| 11 | }, |
| 12 | limits: Limits { |
| 13 | input: 7, |
| 14 | output: 8, |
| 15 | entries: 9, |
| 16 | }, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn encoded(message: &Message) -> Vec<u8> { |
| 21 | let mut bytes = Vec::new(); |
| 22 | Writer::new(&mut bytes, 17).send(message).unwrap(); |
| 23 | bytes |
| 24 | } |
| 25 | |
| 26 | fn decoded(bytes: &[u8]) -> Message { |
| 27 | let mut reader = Reader::new(bytes); |
| 28 | let (job, sequence, message) = reader.read().unwrap().unwrap(); |
| 29 | assert_eq!((job, sequence), (17, 0)); |
| 30 | assert!(reader.read().unwrap().is_none()); |
| 31 | message |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn interactive_start_preserves_legacy_body_and_frame_version() { |
| 36 | let start = sample_start(); |
| 37 | let legacy = encoded(&Message::Start(start.clone())); |
| 38 | let interactive = encoded(&Message::StartInteractive(start.clone())); |
| 39 | let mut expected = legacy; |
| 40 | expected[6..8].copy_from_slice(&4u16.to_le_bytes()); |
| 41 | assert_eq!(interactive, expected); |
| 42 | assert_eq!(&interactive[..6], b"ZAW1\x01\x00"); |
| 43 | match decoded(&interactive) { |
| 44 | Message::StartInteractive(actual) => assert_eq!(actual, start), |
| 45 | other => panic!("unexpected frame: {other:?}"), |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn interactive_start_rejects_list_and_create_on_both_codec_directions() { |
| 51 | for action in [ |
| 52 | Action::List { |
| 53 | archive: NativePath::Unix(b"/archive".to_vec()), |
| 54 | }, |
| 55 | Action::CreateZip { |
| 56 | sources: Vec::new(), |
| 57 | output_file: NativePath::Unix(b"/output.zip".to_vec()), |
| 58 | method: Method::Store, |
| 59 | encryption: Encryption::None, |
| 60 | }, |
| 61 | ] { |
| 62 | let start = Start { |
| 63 | action, |
| 64 | limits: Limits::default(), |
| 65 | }; |
| 66 | let mut bytes = Vec::new(); |
| 67 | assert!( |
| 68 | Writer::new(&mut bytes, 17) |
| 69 | .send(&Message::StartInteractive(start.clone())) |
| 70 | .is_err() |
| 71 | ); |
| 72 | assert!(bytes.is_empty()); |
| 73 | let mut frame = encoded(&Message::Start(start)); |
| 74 | frame[6..8].copy_from_slice(&4u16.to_le_bytes()); |
| 75 | assert_eq!( |
| 76 | Reader::new(frame.as_slice()).read().unwrap_err().kind(), |
| 77 | std::io::ErrorKind::InvalidData |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn conflict_reply_choices_and_apply_flag_have_stable_bytes() { |
| 84 | for (choice, tag) in [ |
| 85 | (ConflictChoice::Overwrite, 0), |
| 86 | (ConflictChoice::Skip, 1), |
| 87 | (ConflictChoice::Rename, 2), |
| 88 | ] { |
| 89 | for apply_to_remaining in [false, true] { |
| 90 | let frame = encoded(&Message::ConflictReply { |
| 91 | challenge_id: 0x0807060504030201, |
| 92 | choice, |
| 93 | apply_to_remaining, |
| 94 | }); |
| 95 | assert_eq!(&frame[6..12], &[5, 0, 10, 0, 0, 0]); |
| 96 | assert_eq!( |
| 97 | &frame[32..], |
| 98 | &[1, 2, 3, 4, 5, 6, 7, 8, tag, u8::from(apply_to_remaining)] |
| 99 | ); |
| 100 | match decoded(&frame) { |
| 101 | Message::ConflictReply { |
| 102 | challenge_id, |
| 103 | choice: actual_choice, |
| 104 | apply_to_remaining: actual_apply, |
| 105 | } => { |
| 106 | assert_eq!(challenge_id, 0x0807060504030201); |
| 107 | assert_eq!(actual_choice, choice); |
| 108 | assert_eq!(actual_apply, apply_to_remaining); |
| 109 | } |
| 110 | other => panic!("unexpected frame: {other:?}"), |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn conflict_metadata_round_trips_unicode_names_and_full_width_sizes() { |
| 118 | let name = "자료/보고서.txt"; |
| 119 | let required = encoded(&Message::ConflictRequired { |
| 120 | challenge_id: u64::MAX, |
| 121 | entry_id: u32::MAX, |
| 122 | relative_name: name.to_owned(), |
| 123 | incoming_size: u64::MAX - 1, |
| 124 | existing_size: u64::MAX - 2, |
| 125 | }); |
| 126 | assert_eq!(&required[6..8], &108u16.to_le_bytes()); |
| 127 | match decoded(&required) { |
| 128 | Message::ConflictRequired { |
| 129 | challenge_id, |
| 130 | entry_id, |
| 131 | relative_name, |
| 132 | incoming_size, |
| 133 | existing_size, |
| 134 | } => { |
| 135 | assert_eq!(challenge_id, u64::MAX); |
| 136 | assert_eq!(entry_id, u32::MAX); |
| 137 | assert_eq!(relative_name, name); |
| 138 | assert_eq!(incoming_size, u64::MAX - 1); |
| 139 | assert_eq!(existing_size, u64::MAX - 2); |
| 140 | } |
| 141 | other => panic!("unexpected frame: {other:?}"), |
| 142 | } |
| 143 | for choice in [ |
| 144 | ConflictChoice::Overwrite, |
| 145 | ConflictChoice::Skip, |
| 146 | ConflictChoice::Rename, |
| 147 | ] { |
| 148 | let resolved = encoded(&Message::ConflictResolved { |
| 149 | entry_id: 23, |
| 150 | relative_name: "자료/보고서 (1).txt".to_owned(), |
| 151 | choice, |
| 152 | }); |
| 153 | assert_eq!(&resolved[6..8], &109u16.to_le_bytes()); |
| 154 | match decoded(&resolved) { |
| 155 | Message::ConflictResolved { |
| 156 | entry_id, |
| 157 | relative_name, |
| 158 | choice: actual, |
| 159 | } => { |
| 160 | assert_eq!(entry_id, 23); |
| 161 | assert_eq!(relative_name, "자료/보고서 (1).txt"); |
| 162 | assert_eq!(actual, choice); |
| 163 | } |
| 164 | other => panic!("unexpected frame: {other:?}"), |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn conflict_frames_reject_unknown_choices_flags_utf8_and_kinds() { |
| 171 | let reply = encoded(&Message::ConflictReply { |
| 172 | challenge_id: 1, |
| 173 | choice: ConflictChoice::Skip, |
| 174 | apply_to_remaining: false, |
| 175 | }); |
| 176 | for (offset, value) in [(40, 3), (41, 2)] { |
| 177 | let mut invalid = reply.clone(); |
| 178 | invalid[offset] = value; |
| 179 | assert!(Reader::new(invalid.as_slice()).read().is_err()); |
| 180 | } |
| 181 | let mut required = encoded(&Message::ConflictRequired { |
| 182 | challenge_id: 1, |
| 183 | entry_id: 2, |
| 184 | relative_name: "a".to_owned(), |
| 185 | incoming_size: 3, |
| 186 | existing_size: 4, |
| 187 | }); |
| 188 | required[48] = 0xff; |
| 189 | assert!(Reader::new(required.as_slice()).read().is_err()); |
| 190 | let mut resolved = encoded(&Message::ConflictResolved { |
| 191 | entry_id: 1, |
| 192 | relative_name: "a".to_owned(), |
| 193 | choice: ConflictChoice::Rename, |
| 194 | }); |
| 195 | *resolved.last_mut().unwrap() = 3; |
| 196 | assert!(Reader::new(resolved.as_slice()).read().is_err()); |
| 197 | let mut unknown = reply; |
| 198 | unknown[6..8].copy_from_slice(&110u16.to_le_bytes()); |
| 199 | assert!(Reader::new(unknown.as_slice()).read().is_err()); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn conflict_frames_reject_truncation_and_trailing_fields() { |
| 204 | for message in [ |
| 205 | Message::StartInteractive(sample_start()), |
| 206 | Message::ConflictReply { |
| 207 | challenge_id: 1, |
| 208 | choice: ConflictChoice::Overwrite, |
| 209 | apply_to_remaining: true, |
| 210 | }, |
| 211 | Message::ConflictRequired { |
| 212 | challenge_id: 1, |
| 213 | entry_id: 2, |
| 214 | relative_name: "a".to_owned(), |
| 215 | incoming_size: 3, |
| 216 | existing_size: 4, |
| 217 | }, |
| 218 | Message::ConflictResolved { |
| 219 | entry_id: 2, |
| 220 | relative_name: "a".to_owned(), |
| 221 | choice: ConflictChoice::Skip, |
| 222 | }, |
| 223 | ] { |
| 224 | let frame = encoded(&message); |
| 225 | for length in 1..frame.len() { |
| 226 | assert!(Reader::new(&frame[..length]).read().is_err()); |
| 227 | } |
| 228 | let mut trailing = frame; |
| 229 | trailing.push(0); |
| 230 | let length = (trailing.len() - 32) as u32; |
| 231 | trailing[8..12].copy_from_slice(&length.to_le_bytes()); |
| 232 | assert!(Reader::new(trailing.as_slice()).read().is_err()); |
| 233 | } |
| 234 | } |