| 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 |
#![deny(unsafe_code)] |
| 32 |
|
| 33 |
pub mod oracle; |
| 34 |
|
| 35 |
use std::collections::HashMap; |
| 36 |
|
| 37 |
use base64::{Engine, engine::general_purpose::STANDARD as B64}; |
| 38 |
|
| 39 |
|
| 40 |
#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 41 |
pub enum Format { |
| 42 |
Rgb, |
| 43 |
Rgba, |
| 44 |
Png, |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 49 |
pub enum Medium { |
| 50 |
Direct, |
| 51 |
File, |
| 52 |
TempFile, |
| 53 |
Shm, |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
#[derive(Clone, Debug, Default)] |
| 58 |
pub struct Control { |
| 59 |
pub action: char, |
| 60 |
pub format: Option<Format>, |
| 61 |
pub medium: Option<Medium>, |
| 62 |
|
| 63 |
pub id: Option<u32>, |
| 64 |
|
| 65 |
pub number: Option<u32>, |
| 66 |
|
| 67 |
pub placement: Option<u32>, |
| 68 |
|
| 69 |
pub width_px: Option<u32>, |
| 70 |
|
| 71 |
pub height_px: Option<u32>, |
| 72 |
|
| 73 |
pub cell_cols: Option<u32>, |
| 74 |
|
| 75 |
pub cell_rows: Option<u32>, |
| 76 |
|
| 77 |
pub no_cursor_move: bool, |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
pub unicode_placeholder: bool, |
| 82 |
|
| 83 |
pub quiet: u8, |
| 84 |
|
| 85 |
pub more_chunks: bool, |
| 86 |
|
| 87 |
|
| 88 |
pub last_chunk: bool, |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
#[derive(Clone, Debug)] |
| 94 |
pub enum Command { |
| 95 |
|
| 96 |
Transmit { control: Control, payload: Vec<u8> }, |
| 97 |
|
| 98 |
|
| 99 |
Place { control: Control }, |
| 100 |
|
| 101 |
|
| 102 |
Delete { control: Control }, |
| 103 |
|
| 104 |
|
| 105 |
FrameAppend { control: Control, payload: Vec<u8> }, |
| 106 |
|
| 107 |
|
| 108 |
FrameCompose { control: Control }, |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
Query { control: Control }, |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
#[must_use] |
| 130 |
pub fn query_response(control: &Control) -> Vec<u8> { |
| 131 |
let id = control.id.unwrap_or(0); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
let supported = |
| 137 |
control.format.is_some() && matches!(control.medium, None | Some(Medium::Direct)); |
| 138 |
if supported { |
| 139 |
format!("\x1b_Gi={id};OK\x1b\\").into_bytes() |
| 140 |
} else { |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
format!("\x1b_Gi={id};ENOTSUPP\x1b\\").into_bytes() |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
pub const MAX_IN_FLIGHT_BYTES: usize = 64 * 1024 * 1024; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
pub const MAX_IN_FLIGHT_TRANSMISSIONS: usize = 16; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#[derive(Debug, Default)] |
| 191 |
pub struct Parser { |
| 192 |
partial: HashMap<PartialKey, Partial>, |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
clock: u64, |
| 198 |
} |
| 199 |
|
| 200 |
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] |
| 201 |
enum PartialKey { |
| 202 |
ById(u32), |
| 203 |
ByNumber(u32), |
| 204 |
|
| 205 |
Anon, |
| 206 |
} |
| 207 |
|
| 208 |
#[derive(Debug)] |
| 209 |
struct Partial { |
| 210 |
control: Control, |
| 211 |
payload: Vec<u8>, |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
overflowed: bool, |
| 220 |
|
| 221 |
|
| 222 |
last_advanced: u64, |
| 223 |
} |
| 224 |
|
| 225 |
impl Parser { |
| 226 |
pub fn new() -> Self { |
| 227 |
Self::default() |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
#[must_use] |
| 237 |
pub fn pending_transmissions(&self) -> usize { |
| 238 |
self.partial.len() |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
#[must_use] |
| 253 |
pub fn pending_bytes(&self) -> usize { |
| 254 |
self.partial.capacity() * std::mem::size_of::<(PartialKey, Partial)>() |
| 255 |
+ self |
| 256 |
.partial |
| 257 |
.values() |
| 258 |
.map(|p| p.payload.capacity()) |
| 259 |
.sum::<usize>() |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
pub fn feed(&mut self, body: &[u8]) -> Option<Command> { |
| 266 |
|
| 267 |
|
| 268 |
let body = body.strip_prefix(b"G").unwrap_or(body); |
| 269 |
|
| 270 |
let (control_bytes, payload_b64) = split_once_byte(body, b';'); |
| 271 |
let control = parse_control(control_bytes)?; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
let needs_payload = matches!(control.action, 'T' | 't' | 'f'); |
| 278 |
if needs_payload && payload_b64.is_none() { |
| 279 |
return None; |
| 280 |
} |
| 281 |
|
| 282 |
match control.action { |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
'T' | 't' | 'f' => { |
| 288 |
let b64 = payload_b64.unwrap_or(&[]); |
| 289 |
let (control, payload) = self.accept_chunked(control, b64)?; |
| 290 |
Some(match control.action { |
| 291 |
'f' => Command::FrameAppend { control, payload }, |
| 292 |
_ => Command::Transmit { control, payload }, |
| 293 |
}) |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
'p' | 'c' | 'd' | 'q' => { |
| 304 |
if let Some(b) = payload_b64 { |
| 305 |
if !b.is_empty() && B64.decode(b).is_err() { |
| 306 |
return None; |
| 307 |
} |
| 308 |
} |
| 309 |
match control.action { |
| 310 |
'p' => Some(Command::Place { control }), |
| 311 |
'c' => Some(Command::FrameCompose { control }), |
| 312 |
'd' => Some(Command::Delete { control }), |
| 313 |
'q' => Some(Command::Query { control }), |
| 314 |
_ => unreachable!(), |
| 315 |
} |
| 316 |
} |
| 317 |
_ => { |
| 318 |
tracing::trace!("kitty-graphics: unhandled action {}", control.action); |
| 319 |
None |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
fn accept_chunked( |
| 332 |
&mut self, |
| 333 |
control: Control, |
| 334 |
payload_b64: &[u8], |
| 335 |
) -> Option<(Control, Vec<u8>)> { |
| 336 |
let key = if let Some(id) = control.id { |
| 337 |
PartialKey::ById(id) |
| 338 |
} else if let Some(n) = control.number { |
| 339 |
PartialKey::ByNumber(n) |
| 340 |
} else { |
| 341 |
PartialKey::Anon |
| 342 |
}; |
| 343 |
|
| 344 |
if !control.more_chunks && !self.partial.contains_key(&key) { |
| 345 |
|
| 346 |
|
| 347 |
if over_budget(decoded_upper_bound(payload_b64)) { |
| 348 |
return None; |
| 349 |
} |
| 350 |
let payload = if payload_b64.is_empty() { |
| 351 |
Vec::new() |
| 352 |
} else { |
| 353 |
B64.decode(payload_b64).ok()? |
| 354 |
}; |
| 355 |
return Some((control, payload)); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
if !self.partial.contains_key(&key) { |
| 361 |
self.evict_until_room_for_one(); |
| 362 |
} |
| 363 |
|
| 364 |
self.clock += 1; |
| 365 |
let clock = self.clock; |
| 366 |
|
| 367 |
|
| 368 |
let held_elsewhere = self.in_flight_bytes_excluding(key); |
| 369 |
|
| 370 |
let entry = self.partial.entry(key).or_insert_with(|| Partial { |
| 371 |
control: control.clone(), |
| 372 |
payload: Vec::new(), |
| 373 |
overflowed: false, |
| 374 |
last_advanced: clock, |
| 375 |
}); |
| 376 |
entry.last_advanced = clock; |
| 377 |
|
| 378 |
if !payload_b64.is_empty() { |
| 379 |
let start = entry.payload.len(); |
| 380 |
|
| 381 |
let extra = decoded_upper_bound(payload_b64); |
| 382 |
|
| 383 |
|
| 384 |
let after = held_elsewhere.saturating_add(start).saturating_add(extra); |
| 385 |
if entry.overflowed || over_budget(after) { |
| 386 |
entry.overflowed = true; |
| 387 |
} else { |
| 388 |
entry.payload.resize(start + extra, 0); |
| 389 |
match B64.decode_slice(payload_b64, &mut entry.payload[start..]) { |
| 390 |
Ok(written) => entry.payload.truncate(start + written), |
| 391 |
Err(_) => { |
| 392 |
entry.payload.truncate(start); |
| 393 |
return None; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
merge_control(&mut entry.control, &control); |
| 400 |
|
| 401 |
if control.more_chunks { |
| 402 |
None |
| 403 |
} else { |
| 404 |
let Partial { |
| 405 |
control, |
| 406 |
payload, |
| 407 |
overflowed, |
| 408 |
.. |
| 409 |
} = self.partial.remove(&key)?; |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
if overflowed { |
| 415 |
return None; |
| 416 |
} |
| 417 |
Some((control, payload)) |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
fn in_flight_bytes_excluding(&self, key: PartialKey) -> usize { |
| 428 |
self.partial |
| 429 |
.iter() |
| 430 |
.filter(|(k, _)| **k != key) |
| 431 |
.map(|(_, p)| p.payload.len()) |
| 432 |
.sum() |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
fn evict_until_room_for_one(&mut self) { |
| 437 |
while self.partial.len() >= MAX_IN_FLIGHT_TRANSMISSIONS { |
| 438 |
let Some(oldest) = self |
| 439 |
.partial |
| 440 |
.iter() |
| 441 |
.min_by_key(|(_, p)| p.last_advanced) |
| 442 |
.map(|(k, _)| *k) |
| 443 |
else { |
| 444 |
break; |
| 445 |
}; |
| 446 |
self.partial.remove(&oldest); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
self.partial.shrink_to_fit(); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
fn decoded_upper_bound(payload_b64: &[u8]) -> usize { |
| 460 |
payload_b64.len().div_ceil(4) * 3 |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
fn over_budget(bytes: usize) -> bool { |
| 471 |
bytes > MAX_IN_FLIGHT_BYTES |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
fn split_once_byte(bytes: &[u8], sep: u8) -> (&[u8], Option<&[u8]>) { |
| 478 |
match bytes.iter().position(|&b| b == sep) { |
| 479 |
Some(i) => (&bytes[..i], Some(&bytes[i + 1..])), |
| 480 |
None => (bytes, None), |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
fn parse_control(bytes: &[u8]) -> Option<Control> { |
| 485 |
let mut c = Control { |
| 486 |
action: 'T', |
| 487 |
..Control::default() |
| 488 |
}; |
| 489 |
let text = std::str::from_utf8(bytes).ok()?; |
| 490 |
for kv in text.split(',') { |
| 491 |
let (k, v) = kv.split_once('=')?; |
| 492 |
match k { |
| 493 |
"a" => c.action = v.chars().next()?, |
| 494 |
"f" => { |
| 495 |
c.format = match v { |
| 496 |
"24" => Some(Format::Rgb), |
| 497 |
"32" => Some(Format::Rgba), |
| 498 |
"100" => Some(Format::Png), |
| 499 |
_ => None, |
| 500 |
} |
| 501 |
} |
| 502 |
"t" => { |
| 503 |
c.medium = match v { |
| 504 |
"d" => Some(Medium::Direct), |
| 505 |
"f" => Some(Medium::File), |
| 506 |
"t" => Some(Medium::TempFile), |
| 507 |
"s" => Some(Medium::Shm), |
| 508 |
_ => None, |
| 509 |
} |
| 510 |
} |
| 511 |
"i" => c.id = v.parse().ok(), |
| 512 |
"I" => c.number = v.parse().ok(), |
| 513 |
"p" => c.placement = v.parse().ok(), |
| 514 |
"s" => c.width_px = v.parse().ok(), |
| 515 |
"v" => c.height_px = v.parse().ok(), |
| 516 |
"c" => c.cell_cols = v.parse().ok(), |
| 517 |
"r" => c.cell_rows = v.parse().ok(), |
| 518 |
"C" => c.no_cursor_move = v == "1", |
| 519 |
"U" => c.unicode_placeholder = v == "1", |
| 520 |
"q" => c.quiet = v.parse().unwrap_or(0), |
| 521 |
"m" => match v { |
| 522 |
"0" => c.last_chunk = true, |
| 523 |
"1" => c.more_chunks = true, |
| 524 |
_ => {} |
| 525 |
}, |
| 526 |
_ => {} |
| 527 |
} |
| 528 |
} |
| 529 |
if !c.more_chunks { |
| 530 |
c.last_chunk = true; |
| 531 |
} |
| 532 |
Some(c) |
| 533 |
} |
| 534 |
|
| 535 |
fn merge_control(into: &mut Control, from: &Control) { |
| 536 |
if into.format.is_none() { |
| 537 |
into.format = from.format; |
| 538 |
} |
| 539 |
if into.width_px.is_none() { |
| 540 |
into.width_px = from.width_px; |
| 541 |
} |
| 542 |
if into.height_px.is_none() { |
| 543 |
into.height_px = from.height_px; |
| 544 |
} |
| 545 |
if into.cell_cols.is_none() { |
| 546 |
into.cell_cols = from.cell_cols; |
| 547 |
} |
| 548 |
if into.cell_rows.is_none() { |
| 549 |
into.cell_rows = from.cell_rows; |
| 550 |
} |
| 551 |
if from.no_cursor_move { |
| 552 |
into.no_cursor_move = true; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
#[cfg(test)] |
| 557 |
mod tests { |
| 558 |
use super::*; |
| 559 |
use base64::Engine; |
| 560 |
|
| 561 |
fn b64(bytes: &[u8]) -> String { |
| 562 |
B64.encode(bytes) |
| 563 |
} |
| 564 |
|
| 565 |
fn transmit(cmd: Command) -> (Control, Vec<u8>) { |
| 566 |
match cmd { |
| 567 |
Command::Transmit { control, payload } => (control, payload), |
| 568 |
other => panic!("expected Transmit, got {other:?}"), |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
#[test] |
| 580 |
fn the_in_flight_budget_is_sixty_four_mebibytes() { |
| 581 |
|
| 582 |
|
| 583 |
assert_eq!(MAX_IN_FLIGHT_BYTES, 67_108_864); |
| 584 |
} |
| 585 |
|
| 586 |
#[test] |
| 587 |
fn the_budget_boundary_admits_exactly_the_budget() { |
| 588 |
|
| 589 |
|
| 590 |
assert!(!over_budget(MAX_IN_FLIGHT_BYTES)); |
| 591 |
assert!(over_budget(MAX_IN_FLIGHT_BYTES + 1)); |
| 592 |
assert!(!over_budget(MAX_IN_FLIGHT_BYTES - 1)); |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
fn parser_holding(payload: &[u8]) -> Parser { |
| 597 |
let mut p = Parser::new(); |
| 598 |
let body = format!("Ga=T,f=32,s=64,v=64,i=7,m=1;{}", b64(payload)); |
| 599 |
assert!( |
| 600 |
p.feed(body.as_bytes()).is_none(), |
| 601 |
"an opening chunk emits nothing" |
| 602 |
); |
| 603 |
p |
| 604 |
} |
| 605 |
|
| 606 |
#[test] |
| 607 |
fn pending_bytes_counts_the_map_spine_and_every_payload() { |
| 608 |
let p = parser_holding(&[0xAB; 3000]); |
| 609 |
|
| 610 |
|
| 611 |
let expected = p.partial.capacity() * std::mem::size_of::<(PartialKey, Partial)>() |
| 612 |
+ p.partial |
| 613 |
.values() |
| 614 |
.map(|x| x.payload.capacity()) |
| 615 |
.sum::<usize>(); |
| 616 |
assert_eq!(p.pending_bytes(), expected); |
| 617 |
assert!( |
| 618 |
p.pending_bytes() >= 3000, |
| 619 |
"a transmission holding 3000 bytes cannot report fewer" |
| 620 |
); |
| 621 |
} |
| 622 |
|
| 623 |
#[test] |
| 624 |
fn in_flight_bytes_excluding_leaves_out_the_named_transmission_only() { |
| 625 |
let mut p = Parser::new(); |
| 626 |
for (id, len) in [(7u32, 3000usize), (9, 6000)] { |
| 627 |
let body = format!("Ga=T,f=32,s=64,v=64,i={id},m=1;{}", b64(&vec![0xCD; len])); |
| 628 |
assert!(p.feed(body.as_bytes()).is_none()); |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
let seven = p.in_flight_bytes_excluding(PartialKey::ById(7)); |
| 633 |
let nine = p.in_flight_bytes_excluding(PartialKey::ById(9)); |
| 634 |
assert_eq!(seven, 6000, "excluding 7 must leave 9's bytes"); |
| 635 |
assert_eq!(nine, 3000, "excluding 9 must leave 7's bytes"); |
| 636 |
assert_eq!( |
| 637 |
p.in_flight_bytes_excluding(PartialKey::Anon), |
| 638 |
9000, |
| 639 |
"excluding a key that is not there leaves both" |
| 640 |
); |
| 641 |
} |
| 642 |
|
| 643 |
#[test] |
| 644 |
fn the_map_never_holds_more_than_the_in_flight_cap() { |
| 645 |
|
| 646 |
|
| 647 |
let mut p = Parser::new(); |
| 648 |
let over = MAX_IN_FLIGHT_TRANSMISSIONS + 24; |
| 649 |
for id in 0..over { |
| 650 |
let body = format!("Ga=T,f=32,s=8,v=8,i={id},m=1;{}", b64(&[0x11; 12])); |
| 651 |
assert!(p.feed(body.as_bytes()).is_none()); |
| 652 |
assert!( |
| 653 |
p.pending_transmissions() <= MAX_IN_FLIGHT_TRANSMISSIONS, |
| 654 |
"{} transmissions in flight after opening {}", |
| 655 |
p.pending_transmissions(), |
| 656 |
id + 1 |
| 657 |
); |
| 658 |
} |
| 659 |
assert_eq!( |
| 660 |
p.pending_transmissions(), |
| 661 |
MAX_IN_FLIGHT_TRANSMISSIONS, |
| 662 |
"{over} openings must leave exactly the cap behind" |
| 663 |
); |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
#[test] |
| 669 |
fn every_medium_spelling_parses_to_its_own_variant() { |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
for (spelling, want) in [ |
| 674 |
("d", Medium::Direct), |
| 675 |
("f", Medium::File), |
| 676 |
("t", Medium::TempFile), |
| 677 |
("s", Medium::Shm), |
| 678 |
] { |
| 679 |
let c = parse_control(format!("a=q,t={spelling}").as_bytes()) |
| 680 |
.unwrap_or_else(|| panic!("t={spelling} did not parse")); |
| 681 |
assert_eq!(c.medium, Some(want), "t={spelling}"); |
| 682 |
} |
| 683 |
assert_eq!( |
| 684 |
parse_control(b"a=q,t=z").unwrap().medium, |
| 685 |
None, |
| 686 |
"an unknown medium is None, not a default" |
| 687 |
); |
| 688 |
} |
| 689 |
|
| 690 |
#[test] |
| 691 |
fn a_body_with_no_m_field_is_its_own_last_chunk() { |
| 692 |
|
| 693 |
|
| 694 |
let c = parse_control(b"a=T,f=32,s=1,v=1").unwrap(); |
| 695 |
assert!(c.last_chunk, "a body with no m= is complete by itself"); |
| 696 |
assert!(!c.more_chunks); |
| 697 |
} |
| 698 |
|
| 699 |
#[test] |
| 700 |
fn m_zero_sets_last_chunk_even_when_m_one_came_first() { |
| 701 |
|
| 702 |
|
| 703 |
let c = parse_control(b"a=T,m=1,m=0").unwrap(); |
| 704 |
assert!(c.last_chunk, "m=0 says last chunk in its own right"); |
| 705 |
assert!(c.more_chunks); |
| 706 |
} |
| 707 |
|
| 708 |
#[test] |
| 709 |
fn the_clock_ticks_once_per_accepted_chunk() { |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
let mut p = Parser::new(); |
| 715 |
for id in 0..5u32 { |
| 716 |
let body = format!("Ga=T,f=32,s=8,v=8,i={id},m=1;{}", b64(&[0x22; 12])); |
| 717 |
assert!(p.feed(body.as_bytes()).is_none()); |
| 718 |
} |
| 719 |
assert_eq!(p.clock, 5, "one tick per chunk accepted, and no more"); |
| 720 |
} |
| 721 |
|
| 722 |
#[test] |
| 723 |
fn eviction_drops_the_least_recently_advanced_transmission() { |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
let mut p = Parser::new(); |
| 728 |
for id in 0..MAX_IN_FLIGHT_TRANSMISSIONS as u32 { |
| 729 |
let body = format!("Ga=T,f=32,s=8,v=8,i={id},m=1;{}", b64(&[0x33; 12])); |
| 730 |
assert!(p.feed(body.as_bytes()).is_none()); |
| 731 |
} |
| 732 |
|
| 733 |
assert!( |
| 734 |
p.feed(format!("Gi=0,m=1;{}", b64(&[0x44; 12])).as_bytes()) |
| 735 |
.is_none() |
| 736 |
); |
| 737 |
|
| 738 |
assert!( |
| 739 |
p.feed(format!("Ga=T,f=32,s=8,v=8,i=99,m=1;{}", b64(&[0x55; 12])).as_bytes()) |
| 740 |
.is_none() |
| 741 |
); |
| 742 |
assert!( |
| 743 |
p.partial.contains_key(&PartialKey::ById(0)), |
| 744 |
"the transmission that was still being fed must survive" |
| 745 |
); |
| 746 |
assert!( |
| 747 |
!p.partial.contains_key(&PartialKey::ById(1)), |
| 748 |
"the least recently advanced one is what goes" |
| 749 |
); |
| 750 |
assert_eq!(p.pending_transmissions(), MAX_IN_FLIGHT_TRANSMISSIONS); |
| 751 |
} |
| 752 |
|
| 753 |
#[test] |
| 754 |
fn a_later_chunk_can_carry_fields_the_opener_left_out() { |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
let payload = [0x6Eu8; 96]; |
| 759 |
let encoded = b64(&payload); |
| 760 |
let (head, tail) = encoded.split_at(16); |
| 761 |
let mut p = Parser::new(); |
| 762 |
assert!(p.feed(format!("Ga=T,i=12,m=1;{head}").as_bytes()).is_none()); |
| 763 |
let cmd = p |
| 764 |
.feed(format!("Gi=12,f=32,s=4,v=8,C=1,m=0;{tail}").as_bytes()) |
| 765 |
.expect("the closing chunk completes the transmission"); |
| 766 |
let (control, got) = transmit(cmd); |
| 767 |
assert_eq!(got, payload); |
| 768 |
assert_eq!( |
| 769 |
control.format, |
| 770 |
Some(Format::Rgba), |
| 771 |
"f= arrived on the closing chunk and must be kept" |
| 772 |
); |
| 773 |
assert_eq!(control.width_px, Some(4)); |
| 774 |
assert_eq!(control.height_px, Some(8)); |
| 775 |
assert!(control.no_cursor_move); |
| 776 |
} |
| 777 |
|
| 778 |
#[test] |
| 779 |
fn a_continuation_chunk_inherits_the_opening_control() { |
| 780 |
|
| 781 |
|
| 782 |
let payload = [0x7Fu8; 96]; |
| 783 |
let encoded = b64(&payload); |
| 784 |
let (head, tail) = encoded.split_at(16); |
| 785 |
let mut p = Parser::new(); |
| 786 |
assert!( |
| 787 |
p.feed(format!("Ga=T,f=32,s=4,v=8,C=1,i=11,m=1;{head}").as_bytes()) |
| 788 |
.is_none() |
| 789 |
); |
| 790 |
let cmd = p |
| 791 |
.feed(format!("Gi=11,m=0;{tail}").as_bytes()) |
| 792 |
.expect("the closing chunk completes the transmission"); |
| 793 |
let (control, got) = transmit(cmd); |
| 794 |
assert_eq!(got, payload); |
| 795 |
assert_eq!( |
| 796 |
control.format, |
| 797 |
Some(Format::Rgba), |
| 798 |
"f= came from the opener" |
| 799 |
); |
| 800 |
assert_eq!(control.width_px, Some(4), "s= came from the opener"); |
| 801 |
assert_eq!(control.height_px, Some(8), "v= came from the opener"); |
| 802 |
assert!(control.no_cursor_move, "C= came from the opener"); |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
#[test] |
| 808 |
fn header_only_transmit_is_rejected() { |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
let mut p = Parser::new(); |
| 813 |
assert!(p.feed(b"Gf=32,s=1,v=1").is_none()); |
| 814 |
assert!(p.feed(b"Ga=T,f=32,s=1,v=1").is_none()); |
| 815 |
assert!(p.feed(b"Ga=t,f=32,s=1,v=1").is_none()); |
| 816 |
assert!(p.feed(b"Ga=f,f=32,s=1,v=1").is_none()); |
| 817 |
} |
| 818 |
|
| 819 |
#[test] |
| 820 |
fn empty_payload_after_semicolon_is_accepted() { |
| 821 |
|
| 822 |
|
| 823 |
let mut p = Parser::new(); |
| 824 |
let (control, payload) = transmit( |
| 825 |
p.feed(b"Gf=32,s=1,v=1;") |
| 826 |
.expect("empty-payload transmit with `;` must dispatch"), |
| 827 |
); |
| 828 |
assert_eq!(control.action, 'T'); |
| 829 |
assert_eq!(control.format, Some(Format::Rgba)); |
| 830 |
assert!(payload.is_empty()); |
| 831 |
} |
| 832 |
|
| 833 |
#[test] |
| 834 |
fn parses_full_control_fields() { |
| 835 |
let mut p = Parser::new(); |
| 836 |
let body = format!( |
| 837 |
"Ga=T,f=100,t=d,i=42,I=7,p=3,s=100,v=50,c=8,r=4,C=1,q=1;{}", |
| 838 |
b64(b"data") |
| 839 |
); |
| 840 |
let (c, payload) = transmit(p.feed(body.as_bytes()).unwrap()); |
| 841 |
assert_eq!(c.action, 'T'); |
| 842 |
assert_eq!(c.format, Some(Format::Png)); |
| 843 |
assert_eq!(c.medium, Some(Medium::Direct)); |
| 844 |
assert_eq!(c.id, Some(42)); |
| 845 |
assert_eq!(c.number, Some(7)); |
| 846 |
assert_eq!(c.placement, Some(3)); |
| 847 |
assert_eq!(c.width_px, Some(100)); |
| 848 |
assert_eq!(c.height_px, Some(50)); |
| 849 |
assert_eq!(c.cell_cols, Some(8)); |
| 850 |
assert_eq!(c.cell_rows, Some(4)); |
| 851 |
assert!(c.no_cursor_move); |
| 852 |
assert_eq!(c.quiet, 1); |
| 853 |
assert_eq!(payload, b"data"); |
| 854 |
} |
| 855 |
|
| 856 |
#[test] |
| 857 |
fn tolerates_missing_g_prefix() { |
| 858 |
|
| 859 |
|
| 860 |
let mut p = Parser::new(); |
| 861 |
let body = format!("a=T,f=32,s=1,v=1;{}", b64(b"xyz")); |
| 862 |
let (control, payload) = transmit(p.feed(body.as_bytes()).unwrap()); |
| 863 |
assert_eq!(control.action, 'T'); |
| 864 |
assert_eq!(payload, b"xyz"); |
| 865 |
} |
| 866 |
|
| 867 |
fn query_of(body: &[u8]) -> Control { |
| 868 |
let mut p = Parser::new(); |
| 869 |
match p.feed(body) { |
| 870 |
Some(Command::Query { control }) => control, |
| 871 |
other => panic!("expected a query, got {other:?}"), |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
#[test] |
| 876 |
fn a_probe_query_parses_as_a_query() { |
| 877 |
|
| 878 |
|
| 879 |
let control = query_of(b"Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA"); |
| 880 |
assert_eq!(control.action, 'q'); |
| 881 |
assert_eq!(control.id, Some(31)); |
| 882 |
} |
| 883 |
|
| 884 |
#[test] |
| 885 |
fn a_query_is_answered_ok_and_addressed_to_its_id() { |
| 886 |
let control = query_of(b"Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA"); |
| 887 |
assert_eq!(query_response(&control), b"\x1b_Gi=31;OK\x1b\\".to_vec()); |
| 888 |
} |
| 889 |
|
| 890 |
#[test] |
| 891 |
fn a_query_with_no_id_is_answered_against_zero() { |
| 892 |
let control = query_of(b"Ga=q,f=32,s=1,v=1;AAAA"); |
| 893 |
assert_eq!(query_response(&control), b"\x1b_Gi=0;OK\x1b\\".to_vec()); |
| 894 |
} |
| 895 |
|
| 896 |
#[test] |
| 897 |
fn every_format_shop_decodes_answers_ok() { |
| 898 |
for body in [ |
| 899 |
b"Ga=q,i=1,f=24,s=1,v=1;AAAA".as_slice(), |
| 900 |
b"Ga=q,i=1,f=32,s=1,v=1;AAAA".as_slice(), |
| 901 |
b"Ga=q,i=1,f=100;AAAA".as_slice(), |
| 902 |
] { |
| 903 |
let reply = query_response(&query_of(body)); |
| 904 |
assert_eq!(reply, b"\x1b_Gi=1;OK\x1b\\".to_vec(), "for {body:?}"); |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
#[test] |
| 909 |
fn a_medium_shop_cannot_read_is_declined_rather_than_ignored() { |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
let control = query_of(b"Ga=q,i=7,f=100,t=f;L3RtcC94"); |
| 914 |
assert_eq!( |
| 915 |
query_response(&control), |
| 916 |
b"\x1b_Gi=7;ENOTSUPP\x1b\\".to_vec() |
| 917 |
); |
| 918 |
} |
| 919 |
|
| 920 |
#[test] |
| 921 |
fn an_absent_medium_means_direct() { |
| 922 |
let control = query_of(b"Ga=q,i=2,f=24,s=1,v=1;AAAA"); |
| 923 |
assert_eq!(query_response(&control), b"\x1b_Gi=2;OK\x1b\\".to_vec()); |
| 924 |
} |
| 925 |
|
| 926 |
#[test] |
| 927 |
fn a_query_with_no_format_is_declined() { |
| 928 |
let control = query_of(b"Ga=q,i=3;AAAA"); |
| 929 |
assert_eq!( |
| 930 |
query_response(&control), |
| 931 |
b"\x1b_Gi=3;ENOTSUPP\x1b\\".to_vec() |
| 932 |
); |
| 933 |
} |
| 934 |
|
| 935 |
#[test] |
| 936 |
fn a_query_with_an_undecodable_payload_is_rejected() { |
| 937 |
let mut p = Parser::new(); |
| 938 |
assert!(p.feed(b"Ga=q,i=1,f=24;!!!!").is_none()); |
| 939 |
} |
| 940 |
|
| 941 |
#[test] |
| 942 |
fn a_query_stores_nothing() { |
| 943 |
|
| 944 |
|
| 945 |
let mut p = Parser::new(); |
| 946 |
assert!(p.feed(b"Gi=31,a=q,f=24,s=1,v=1;AAAA").is_some()); |
| 947 |
assert!(p.feed(b"Gi=31,a=q,f=24,s=1,v=1;AAAA").is_some()); |
| 948 |
} |
| 949 |
|
| 950 |
#[test] |
| 951 |
fn unknown_action_returns_none() { |
| 952 |
let mut p = Parser::new(); |
| 953 |
assert!(p.feed(b"Ga=z,f=32;YWJj").is_none()); |
| 954 |
} |
| 955 |
|
| 956 |
#[test] |
| 957 |
fn malformed_control_returns_none() { |
| 958 |
|
| 959 |
let mut p = Parser::new(); |
| 960 |
assert!(p.feed(b"Ga=T,broken;YWJj").is_none()); |
| 961 |
} |
| 962 |
|
| 963 |
#[test] |
| 964 |
fn malformed_base64_returns_none() { |
| 965 |
let mut p = Parser::new(); |
| 966 |
assert!(p.feed(b"Ga=T,f=32;this-is-not-base64!!!").is_none()); |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
#[test] |
| 972 |
fn format_24_is_rgb() { |
| 973 |
let mut p = Parser::new(); |
| 974 |
let body = format!("Ga=T,f=24,s=1,v=1;{}", b64(&[1, 2, 3])); |
| 975 |
let (c, _) = transmit(p.feed(body.as_bytes()).unwrap()); |
| 976 |
assert_eq!(c.format, Some(Format::Rgb)); |
| 977 |
} |
| 978 |
|
| 979 |
#[test] |
| 980 |
fn format_32_is_rgba() { |
| 981 |
let mut p = Parser::new(); |
| 982 |
let body = format!("Ga=T,f=32,s=1,v=1;{}", b64(&[1, 2, 3, 4])); |
| 983 |
let (c, _) = transmit(p.feed(body.as_bytes()).unwrap()); |
| 984 |
assert_eq!(c.format, Some(Format::Rgba)); |
| 985 |
} |
| 986 |
|
| 987 |
#[test] |
| 988 |
fn format_100_is_png() { |
| 989 |
let mut p = Parser::new(); |
| 990 |
let body = format!("Ga=T,f=100;{}", b64(b"\x89PNG")); |
| 991 |
let (c, _) = transmit(p.feed(body.as_bytes()).unwrap()); |
| 992 |
assert_eq!(c.format, Some(Format::Png)); |
| 993 |
} |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
#[test] |
| 998 |
fn single_chunk_no_m_dispatches_immediately() { |
| 999 |
let mut p = Parser::new(); |
| 1000 |
let body = format!("Ga=T,f=32,s=1,v=1;{}", b64(b"solo")); |
| 1001 |
let (_, payload) = transmit(p.feed(body.as_bytes()).unwrap()); |
| 1002 |
assert_eq!(payload, b"solo"); |
| 1003 |
} |
| 1004 |
|
| 1005 |
#[test] |
| 1006 |
fn two_chunks_reassemble_by_id() { |
| 1007 |
let mut p = Parser::new(); |
| 1008 |
let a = format!("Ga=T,f=32,s=2,v=1,i=7,m=1;{}", b64(b"HEAD")); |
| 1009 |
let b = format!("Gi=7,m=0;{}", b64(b"TAIL")); |
| 1010 |
assert!( |
| 1011 |
p.feed(a.as_bytes()).is_none(), |
| 1012 |
"first chunk should not dispatch" |
| 1013 |
); |
| 1014 |
let (c, payload) = transmit(p.feed(b.as_bytes()).unwrap()); |
| 1015 |
assert_eq!(c.id, Some(7)); |
| 1016 |
|
| 1017 |
assert_eq!(c.format, Some(Format::Rgba)); |
| 1018 |
assert_eq!(c.width_px, Some(2)); |
| 1019 |
assert_eq!(payload, b"HEADTAIL"); |
| 1020 |
} |
| 1021 |
|
| 1022 |
#[test] |
| 1023 |
fn three_chunks_reassemble() { |
| 1024 |
let mut p = Parser::new(); |
| 1025 |
let a = format!("Ga=T,f=32,i=1,m=1;{}", b64(b"AAA")); |
| 1026 |
let b = format!("Gi=1,m=1;{}", b64(b"BBB")); |
| 1027 |
let c = format!("Gi=1,m=0;{}", b64(b"CCC")); |
| 1028 |
assert!(p.feed(a.as_bytes()).is_none()); |
| 1029 |
assert!(p.feed(b.as_bytes()).is_none()); |
| 1030 |
let (_, payload) = transmit(p.feed(c.as_bytes()).unwrap()); |
| 1031 |
assert_eq!(payload, b"AAABBBCCC"); |
| 1032 |
} |
| 1033 |
|
| 1034 |
#[test] |
| 1035 |
fn concurrent_ids_do_not_cross_contaminate() { |
| 1036 |
let mut p = Parser::new(); |
| 1037 |
|
| 1038 |
let a1 = format!("Ga=T,f=32,i=1,m=1;{}", b64(b"HELLO")); |
| 1039 |
let b1 = format!("Ga=T,f=32,i=2,m=1;{}", b64(b"WORLD")); |
| 1040 |
let a2 = format!("Gi=1,m=0;{}", b64(b"!")); |
| 1041 |
let b2 = format!("Gi=2,m=0;{}", b64(b"?")); |
| 1042 |
|
| 1043 |
assert!(p.feed(a1.as_bytes()).is_none()); |
| 1044 |
assert!(p.feed(b1.as_bytes()).is_none()); |
| 1045 |
let (ca, pa) = transmit(p.feed(a2.as_bytes()).unwrap()); |
| 1046 |
let (cb, pb) = transmit(p.feed(b2.as_bytes()).unwrap()); |
| 1047 |
assert_eq!(ca.id, Some(1)); |
| 1048 |
assert_eq!(cb.id, Some(2)); |
| 1049 |
assert_eq!(pa, b"HELLO!"); |
| 1050 |
assert_eq!(pb, b"WORLD?"); |
| 1051 |
} |
| 1052 |
|
| 1053 |
#[test] |
| 1054 |
fn chunked_by_number_field() { |
| 1055 |
|
| 1056 |
let mut p = Parser::new(); |
| 1057 |
let a = format!("Ga=T,f=32,I=99,m=1;{}", b64(b"XX")); |
| 1058 |
let b = format!("GI=99,m=0;{}", b64(b"YY")); |
| 1059 |
assert!(p.feed(a.as_bytes()).is_none()); |
| 1060 |
let (_, payload) = transmit(p.feed(b.as_bytes()).unwrap()); |
| 1061 |
assert_eq!(payload, b"XXYY"); |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
#[test] |
| 1067 |
fn delete_returns_delete_command() { |
| 1068 |
let mut p = Parser::new(); |
| 1069 |
let cmd = p.feed(b"Ga=d,d=A").expect("must dispatch"); |
| 1070 |
assert!(matches!(cmd, Command::Delete { .. })); |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
#[test] |
| 1076 |
fn place_at_cursor_returns_place_command() { |
| 1077 |
let mut p = Parser::new(); |
| 1078 |
let cmd = p |
| 1079 |
.feed(b"Ga=p,i=1,c=8,r=4") |
| 1080 |
.expect("place must dispatch header-only"); |
| 1081 |
let Command::Place { control } = cmd else { |
| 1082 |
panic!("expected Place, got {cmd:?}"); |
| 1083 |
}; |
| 1084 |
assert_eq!(control.action, 'p'); |
| 1085 |
assert_eq!(control.id, Some(1)); |
| 1086 |
assert_eq!(control.cell_cols, Some(8)); |
| 1087 |
assert_eq!(control.cell_rows, Some(4)); |
| 1088 |
assert!(!control.unicode_placeholder); |
| 1089 |
} |
| 1090 |
|
| 1091 |
#[test] |
| 1092 |
fn place_unicode_placeholder_flag_is_parsed() { |
| 1093 |
let mut p = Parser::new(); |
| 1094 |
let cmd = p |
| 1095 |
.feed(b"Ga=p,U=1,i=2,q=2") |
| 1096 |
.expect("virtual placement must dispatch"); |
| 1097 |
let Command::Place { control } = cmd else { |
| 1098 |
panic!("expected Place, got {cmd:?}"); |
| 1099 |
}; |
| 1100 |
assert!(control.unicode_placeholder); |
| 1101 |
assert_eq!(control.quiet, 2); |
| 1102 |
assert_eq!(control.id, Some(2)); |
| 1103 |
} |
| 1104 |
|
| 1105 |
#[test] |
| 1106 |
fn frame_append_dispatches_with_payload() { |
| 1107 |
let mut p = Parser::new(); |
| 1108 |
let body = format!("Ga=f,f=32,s=1,v=1,i=5;{}", b64(b"FRAME")); |
| 1109 |
let cmd = p.feed(body.as_bytes()).expect("frame append must dispatch"); |
| 1110 |
let Command::FrameAppend { control, payload } = cmd else { |
| 1111 |
panic!("expected FrameAppend, got {cmd:?}"); |
| 1112 |
}; |
| 1113 |
assert_eq!(control.action, 'f'); |
| 1114 |
assert_eq!(control.id, Some(5)); |
| 1115 |
assert_eq!(payload, b"FRAME"); |
| 1116 |
} |
| 1117 |
|
| 1118 |
#[test] |
| 1119 |
fn frame_append_reassembles_chunks() { |
| 1120 |
let mut p = Parser::new(); |
| 1121 |
let a = format!("Ga=f,f=32,i=9,m=1;{}", b64(b"HEAD")); |
| 1122 |
let b = format!("Gi=9,m=0;{}", b64(b"TAIL")); |
| 1123 |
assert!(p.feed(a.as_bytes()).is_none()); |
| 1124 |
let cmd = p.feed(b.as_bytes()).expect("must dispatch on last chunk"); |
| 1125 |
let Command::FrameAppend { control, payload } = cmd else { |
| 1126 |
panic!("expected FrameAppend, got {cmd:?}"); |
| 1127 |
}; |
| 1128 |
assert_eq!(control.id, Some(9)); |
| 1129 |
assert_eq!(payload, b"HEADTAIL"); |
| 1130 |
} |
| 1131 |
|
| 1132 |
#[test] |
| 1133 |
fn frame_compose_dispatches_header_only() { |
| 1134 |
let mut p = Parser::new(); |
| 1135 |
let cmd = p |
| 1136 |
.feed(b"Ga=c,i=3,r=1,c=2") |
| 1137 |
.expect("frame compose must dispatch header-only"); |
| 1138 |
let Command::FrameCompose { control } = cmd else { |
| 1139 |
panic!("expected FrameCompose, got {cmd:?}"); |
| 1140 |
}; |
| 1141 |
assert_eq!(control.action, 'c'); |
| 1142 |
assert_eq!(control.id, Some(3)); |
| 1143 |
} |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
#[test] |
| 1150 |
fn distinct_ids_do_not_accumulate_without_bound() { |
| 1151 |
let mut p = Parser::new(); |
| 1152 |
for i in 0..50_000u32 { |
| 1153 |
let body = format!("Ga=T,f=32,i={i},m=1;{}", b64(b"ABC")); |
| 1154 |
assert!(p.feed(body.as_bytes()).is_none()); |
| 1155 |
} |
| 1156 |
assert_eq!(p.pending_transmissions(), MAX_IN_FLIGHT_TRANSMISSIONS); |
| 1157 |
assert!( |
| 1158 |
p.pending_bytes() < 8 * 1024, |
| 1159 |
"parser kept {} bytes for 50k ids", |
| 1160 |
p.pending_bytes() |
| 1161 |
); |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
#[test] |
| 1167 |
fn eviction_keeps_the_transmission_being_advanced() { |
| 1168 |
let mut p = Parser::new(); |
| 1169 |
let head = format!("Ga=T,f=32,i=7,m=1;{}", b64(b"HEAD")); |
| 1170 |
assert!(p.feed(head.as_bytes()).is_none()); |
| 1171 |
|
| 1172 |
for round in 0..4 { |
| 1173 |
for i in 100..100 + MAX_IN_FLIGHT_TRANSMISSIONS as u32 - 1 { |
| 1174 |
let noise = format!("Ga=T,f=32,i={},m=1;{}", i + round * 1000, b64(b"NN")); |
| 1175 |
p.feed(noise.as_bytes()); |
| 1176 |
} |
| 1177 |
|
| 1178 |
let more = format!("Gi=7,m=1;{}", b64(b"-")); |
| 1179 |
p.feed(more.as_bytes()); |
| 1180 |
} |
| 1181 |
|
| 1182 |
let tail = format!("Gi=7,m=0;{}", b64(b"TAIL")); |
| 1183 |
let (control, payload) = transmit(p.feed(tail.as_bytes()).expect("id 7 survived")); |
| 1184 |
assert_eq!(control.id, Some(7)); |
| 1185 |
assert_eq!(payload, b"HEAD----TAIL"); |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
#[test] |
| 1192 |
fn a_transmission_past_the_budget_is_dropped_whole() { |
| 1193 |
let mut p = Parser::new(); |
| 1194 |
|
| 1195 |
let huge = b64(&vec![0u8; 4096]); |
| 1196 |
let first = format!("Ga=T,f=32,i=1,m=1;{huge}"); |
| 1197 |
assert!(p.feed(first.as_bytes()).is_none()); |
| 1198 |
|
| 1199 |
let oversized = "A".repeat(MAX_IN_FLIGHT_BYTES / 3 * 4 + 8); |
| 1200 |
let second = format!("Gi=1,m=1;{oversized}"); |
| 1201 |
assert!(p.feed(second.as_bytes()).is_none()); |
| 1202 |
|
| 1203 |
let tail = format!("Gi=1,m=0;{}", b64(b"TAIL")); |
| 1204 |
assert!( |
| 1205 |
p.feed(tail.as_bytes()).is_none(), |
| 1206 |
"an over-budget transmission must not complete" |
| 1207 |
); |
| 1208 |
assert_eq!(p.pending_transmissions(), 0, "and must not linger"); |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
#[test] |
| 1213 |
fn a_single_shot_past_the_budget_is_refused() { |
| 1214 |
let mut p = Parser::new(); |
| 1215 |
let oversized = "A".repeat(MAX_IN_FLIGHT_BYTES / 3 * 4 + 8); |
| 1216 |
let body = format!("Ga=T,f=32,i=1;{oversized}"); |
| 1217 |
assert!(p.feed(body.as_bytes()).is_none()); |
| 1218 |
assert_eq!(p.pending_transmissions(), 0); |
| 1219 |
} |
| 1220 |
|
| 1221 |
|
| 1222 |
#[test] |
| 1223 |
fn the_byte_budget_is_shared_across_transmissions() { |
| 1224 |
let mut p = Parser::new(); |
| 1225 |
|
| 1226 |
let third = "A".repeat(MAX_IN_FLIGHT_BYTES / 3 / 3 * 4); |
| 1227 |
for i in 0..3u32 { |
| 1228 |
let body = format!("Ga=T,f=32,i={i},m=1;{third}"); |
| 1229 |
assert!(p.feed(body.as_bytes()).is_none()); |
| 1230 |
} |
| 1231 |
assert!( |
| 1232 |
p.pending_bytes() <= MAX_IN_FLIGHT_BYTES * 2, |
| 1233 |
"in-flight bytes reached {}", |
| 1234 |
p.pending_bytes() |
| 1235 |
); |
| 1236 |
} |
| 1237 |
} |
| 1238 |
|