| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
use base64::{Engine, engine::general_purpose::STANDARD as B64}; |
| 32 |
|
| 33 |
use super::{Command, Control, Medium, Parser, query_response}; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
pub const MAX_RETAINED_PER_INPUT_BYTE: usize = 4; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
pub const MAX_RETAINED_BYTES: usize = crate::MAX_IN_FLIGHT_BYTES * 2 + 64 * 1024; |
| 57 |
|
| 58 |
|
| 59 |
pub const RETAINED_BASE_BYTES: usize = 4096; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#[allow(clippy::naive_bytecount)] |
| 73 |
pub fn check_query_response(control: &Control) -> bool { |
| 74 |
let reply = query_response(control); |
| 75 |
assert!( |
| 76 |
reply.starts_with(b"\x1b_G"), |
| 77 |
"query answer is not an APC: {reply:?}" |
| 78 |
); |
| 79 |
assert!( |
| 80 |
reply.ends_with(b"\x1b\\"), |
| 81 |
"query answer has no string terminator: {reply:?}" |
| 82 |
); |
| 83 |
|
| 84 |
|
| 85 |
assert_eq!( |
| 86 |
reply.iter().filter(|&&b| b == 0x1b).count(), |
| 87 |
2, |
| 88 |
"query answer contains an embedded escape: {reply:?}" |
| 89 |
); |
| 90 |
let text = std::str::from_utf8(&reply).expect("query answer is ASCII"); |
| 91 |
let id = control.id.unwrap_or(0); |
| 92 |
assert!( |
| 93 |
text.contains(&format!("i={id};")), |
| 94 |
"query answer is addressed to nobody: {text:?}" |
| 95 |
); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
let claims_ok = text.contains(";OK"); |
| 100 |
let satisfiable = |
| 101 |
control.format.is_some() && matches!(control.medium, None | Some(Medium::Direct)); |
| 102 |
assert_eq!( |
| 103 |
claims_ok, satisfiable, |
| 104 |
"query answered {text:?} for format {:?} medium {:?}", |
| 105 |
control.format, control.medium |
| 106 |
); |
| 107 |
claims_ok |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
fn chunk_len_for(encoded_len: usize) -> usize { |
| 131 |
let quanta = encoded_len.div_ceil(4); |
| 132 |
(4 * quanta.div_ceil(3)).max(4) |
| 133 |
} |
| 134 |
|
| 135 |
fn check_chunk_equivalence(action: char, payload: &[u8]) -> usize { |
| 136 |
if payload.is_empty() { |
| 137 |
return 0; |
| 138 |
} |
| 139 |
let encoded = B64.encode(payload); |
| 140 |
let chunk_len = chunk_len_for(encoded.len()); |
| 141 |
|
| 142 |
let mut p = Parser::new(); |
| 143 |
let mut first = true; |
| 144 |
let mut rest = encoded.as_str(); |
| 145 |
let mut got = None; |
| 146 |
let mut chunks = 0; |
| 147 |
while !rest.is_empty() { |
| 148 |
chunks += 1; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
let take = chunk_len.clamp(1, rest.len()); |
| 154 |
let (head, tail) = rest.split_at(take); |
| 155 |
rest = tail; |
| 156 |
let more = if rest.is_empty() { "0" } else { "1" }; |
| 157 |
let body = if first { |
| 158 |
format!("Ga={action},i=4242,m={more};{head}") |
| 159 |
} else { |
| 160 |
format!("Gi=4242,m={more};{head}") |
| 161 |
}; |
| 162 |
first = false; |
| 163 |
if let Some(cmd) = p.feed(body.as_bytes()) { |
| 164 |
got = Some(cmd); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
let chunked = match got { |
| 169 |
Some(Command::Transmit { payload, .. } | Command::FrameAppend { payload, .. }) => payload, |
| 170 |
Some(other) => panic!("chunked {action} came back as {other:?}"), |
| 171 |
None => panic!( |
| 172 |
"chunked {action} of {} bytes never completed", |
| 173 |
payload.len() |
| 174 |
), |
| 175 |
}; |
| 176 |
assert_eq!( |
| 177 |
chunked.len(), |
| 178 |
payload.len(), |
| 179 |
"chunked payload is {} bytes, single-shot was {}", |
| 180 |
chunked.len(), |
| 181 |
payload.len() |
| 182 |
); |
| 183 |
assert!( |
| 184 |
chunked == payload, |
| 185 |
"chunked payload differs from the single-shot one" |
| 186 |
); |
| 187 |
assert_eq!( |
| 188 |
p.pending_transmissions(), |
| 189 |
0, |
| 190 |
"a completed transmission was left in flight" |
| 191 |
); |
| 192 |
chunks |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
fn b64_len_of(body: &[u8]) -> usize { |
| 212 |
body.iter() |
| 213 |
.position(|&b| b == b';') |
| 214 |
.map_or(0, |i| body.len() - i - 1) |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
fn retained_ceiling(input_len: usize) -> usize { |
| 222 |
RETAINED_BASE_BYTES + input_len.saturating_mul(MAX_RETAINED_PER_INPUT_BYTE) |
| 223 |
} |
| 224 |
|
| 225 |
pub fn check_body(body: &[u8]) -> bool { |
| 226 |
let mut p = Parser::new(); |
| 227 |
let Some(cmd) = p.feed(body) else { |
| 228 |
return false; |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
assert_eq!( |
| 233 |
p.pending_transmissions(), |
| 234 |
0, |
| 235 |
"a single body both completed and stayed in flight" |
| 236 |
); |
| 237 |
|
| 238 |
match &cmd { |
| 239 |
Command::Query { control } => { |
| 240 |
check_query_response(control); |
| 241 |
} |
| 242 |
Command::Transmit { control, payload } | Command::FrameAppend { control, payload } => { |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
let b64_len = b64_len_of(body); |
| 247 |
assert!( |
| 248 |
payload.len() <= b64_len.div_ceil(4) * 3, |
| 249 |
"decoded {} bytes out of {b64_len} base64 characters", |
| 250 |
payload.len() |
| 251 |
); |
| 252 |
let action = if matches!(cmd, Command::FrameAppend { .. }) { |
| 253 |
'f' |
| 254 |
} else { |
| 255 |
control.action |
| 256 |
}; |
| 257 |
check_chunk_equivalence(action, payload); |
| 258 |
} |
| 259 |
Command::Place { .. } | Command::Delete { .. } | Command::FrameCompose { .. } => {} |
| 260 |
} |
| 261 |
true |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
pub fn check_bodies(input: &[u8]) -> usize { |
| 277 |
let mut session = Parser::new(); |
| 278 |
let mut fed = 0; |
| 279 |
for piece in input.split(|&b| b == 0x1b) { |
| 280 |
let piece = piece.strip_prefix(b"_").unwrap_or(piece); |
| 281 |
let piece = piece.strip_suffix(b"\\").unwrap_or(piece); |
| 282 |
if piece.is_empty() { |
| 283 |
continue; |
| 284 |
} |
| 285 |
fed += 1; |
| 286 |
|
| 287 |
|
| 288 |
if let Some(Command::Query { control }) = session.feed(piece) { |
| 289 |
check_query_response(&control); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
check_body(piece); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
assert!( |
| 299 |
session.pending_transmissions() <= crate::MAX_IN_FLIGHT_TRANSMISSIONS, |
| 300 |
"{} transmissions in flight after {fed} bodies, over the cap", |
| 301 |
session.pending_transmissions() |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
let retained = session.pending_bytes(); |
| 306 |
let ceiling = retained_ceiling(input.len()); |
| 307 |
assert!( |
| 308 |
retained <= ceiling, |
| 309 |
"parser holds {retained} bytes after {} bytes of input, over the {ceiling} ceiling", |
| 310 |
input.len() |
| 311 |
); |
| 312 |
assert!( |
| 313 |
retained <= MAX_RETAINED_BYTES, |
| 314 |
"parser holds {retained} bytes, over the {MAX_RETAINED_BYTES} absolute ceiling" |
| 315 |
); |
| 316 |
assert!( |
| 317 |
session.pending_transmissions() <= fed.min(crate::MAX_IN_FLIGHT_TRANSMISSIONS), |
| 318 |
"{} transmissions in flight after {fed} bodies", |
| 319 |
session.pending_transmissions() |
| 320 |
); |
| 321 |
fed |
| 322 |
} |
| 323 |
|
| 324 |
#[cfg(test)] |
| 325 |
mod tests { |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
use super::{ |
| 336 |
MAX_RETAINED_BYTES, RETAINED_BASE_BYTES, b64_len_of, check_body, check_chunk_equivalence, |
| 337 |
check_query_response, chunk_len_for, retained_ceiling, |
| 338 |
}; |
| 339 |
use crate::{Control, Format, Medium}; |
| 340 |
use base64::{Engine, engine::general_purpose::STANDARD as B64}; |
| 341 |
|
| 342 |
#[test] |
| 343 |
fn the_absolute_retention_ceiling_is_two_budgets_and_a_slack() { |
| 344 |
|
| 345 |
|
| 346 |
assert_eq!(MAX_RETAINED_BYTES, 134_283_264); |
| 347 |
assert_eq!(RETAINED_BASE_BYTES, 4096); |
| 348 |
} |
| 349 |
|
| 350 |
#[test] |
| 351 |
fn the_per_input_ceiling_is_the_base_plus_four_per_byte() { |
| 352 |
assert_eq!(retained_ceiling(1000), 8096); |
| 353 |
assert_eq!(retained_ceiling(0), RETAINED_BASE_BYTES); |
| 354 |
} |
| 355 |
|
| 356 |
#[test] |
| 357 |
fn the_chunk_width_is_a_third_of_the_payload_in_whole_quanta() { |
| 358 |
|
| 359 |
|
| 360 |
assert_eq!(chunk_len_for(128), 44); |
| 361 |
} |
| 362 |
|
| 363 |
#[test] |
| 364 |
fn the_chunk_width_never_falls_below_one_quantum() { |
| 365 |
|
| 366 |
|
| 367 |
assert_eq!(chunk_len_for(0), 4); |
| 368 |
assert_eq!(chunk_len_for(4), 4); |
| 369 |
assert!(chunk_len_for(3) >= 4); |
| 370 |
} |
| 371 |
|
| 372 |
#[test] |
| 373 |
fn the_base64_length_is_what_follows_the_first_semicolon() { |
| 374 |
assert_eq!(b64_len_of(b"a=T,f=32;QUJD"), 4); |
| 375 |
assert_eq!( |
| 376 |
b64_len_of(b"a=T,f=32"), |
| 377 |
0, |
| 378 |
"a body with no payload marker carries no base64" |
| 379 |
); |
| 380 |
assert_eq!( |
| 381 |
b64_len_of(b"a=T;QUJD;RUZH"), |
| 382 |
9, |
| 383 |
"the FIRST semicolon is the separator; later ones are payload" |
| 384 |
); |
| 385 |
} |
| 386 |
|
| 387 |
#[test] |
| 388 |
fn chunk_equivalence_reports_how_many_chunks_it_took() { |
| 389 |
|
| 390 |
assert_eq!(check_chunk_equivalence('T', &[0x5A; 96]), 3); |
| 391 |
assert_eq!( |
| 392 |
check_chunk_equivalence('T', &[]), |
| 393 |
0, |
| 394 |
"an empty payload is not chunked at all" |
| 395 |
); |
| 396 |
} |
| 397 |
|
| 398 |
#[test] |
| 399 |
fn check_body_reports_whether_a_command_came_out() { |
| 400 |
let body = format!("Ga=T,f=32,s=2,v=2;{}", B64.encode([0x11u8; 16])); |
| 401 |
assert!(check_body(body.as_bytes()), "a valid transmit yields one"); |
| 402 |
assert!( |
| 403 |
!check_body(b"Gnot-a-command"), |
| 404 |
"a body that parses to nothing yields none" |
| 405 |
); |
| 406 |
} |
| 407 |
|
| 408 |
#[test] |
| 409 |
fn the_query_answer_says_ok_only_for_a_medium_the_host_can_serve() { |
| 410 |
let direct = Control { |
| 411 |
action: 'q', |
| 412 |
format: Some(Format::Rgba), |
| 413 |
medium: Some(Medium::Direct), |
| 414 |
id: Some(4242), |
| 415 |
..Control::default() |
| 416 |
}; |
| 417 |
assert!(check_query_response(&direct), "inline base64 is servable"); |
| 418 |
|
| 419 |
let from_file = Control { |
| 420 |
medium: Some(Medium::File), |
| 421 |
..direct.clone() |
| 422 |
}; |
| 423 |
assert!( |
| 424 |
!check_query_response(&from_file), |
| 425 |
"a file transfer must not be answered OK" |
| 426 |
); |
| 427 |
|
| 428 |
let no_format = Control { |
| 429 |
format: None, |
| 430 |
..direct |
| 431 |
}; |
| 432 |
assert!( |
| 433 |
!check_query_response(&no_format), |
| 434 |
"a query naming no format is not something to say OK to" |
| 435 |
); |
| 436 |
} |
| 437 |
} |
| 438 |
|