1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 4 // option. This file may not be copied, modified, or distributed 5 // except according to those terms. 6 7 // Directly relating to QUIC frames. 8 9 use neqo_common::{qtrace, Decoder}; 10 11 use crate::cid::MAX_CONNECTION_ID_LEN; 12 use crate::packet::PacketType; 13 use crate::stream_id::{StreamId, StreamType}; 14 use crate::{AppError, ConnectionError, Error, Res, TransportError}; 15 16 use std::convert::TryFrom; 17 use std::ops::RangeInclusive; 18 19 #[allow(clippy::module_name_repetitions)] 20 pub type FrameType = u64; 21 22 const FRAME_TYPE_PADDING: FrameType = 0x0; 23 pub const FRAME_TYPE_PING: FrameType = 0x1; 24 pub const FRAME_TYPE_ACK: FrameType = 0x2; 25 const FRAME_TYPE_ACK_ECN: FrameType = 0x3; 26 pub const FRAME_TYPE_RESET_STREAM: FrameType = 0x4; 27 pub const FRAME_TYPE_STOP_SENDING: FrameType = 0x5; 28 pub const FRAME_TYPE_CRYPTO: FrameType = 0x6; 29 pub const FRAME_TYPE_NEW_TOKEN: FrameType = 0x7; 30 const FRAME_TYPE_STREAM: FrameType = 0x8; 31 const FRAME_TYPE_STREAM_MAX: FrameType = 0xf; 32 pub const FRAME_TYPE_MAX_DATA: FrameType = 0x10; 33 pub const FRAME_TYPE_MAX_STREAM_DATA: FrameType = 0x11; 34 pub const FRAME_TYPE_MAX_STREAMS_BIDI: FrameType = 0x12; 35 pub const FRAME_TYPE_MAX_STREAMS_UNIDI: FrameType = 0x13; 36 pub const FRAME_TYPE_DATA_BLOCKED: FrameType = 0x14; 37 pub const FRAME_TYPE_STREAM_DATA_BLOCKED: FrameType = 0x15; 38 pub const FRAME_TYPE_STREAMS_BLOCKED_BIDI: FrameType = 0x16; 39 pub const FRAME_TYPE_STREAMS_BLOCKED_UNIDI: FrameType = 0x17; 40 pub const FRAME_TYPE_NEW_CONNECTION_ID: FrameType = 0x18; 41 pub const FRAME_TYPE_RETIRE_CONNECTION_ID: FrameType = 0x19; 42 pub const FRAME_TYPE_PATH_CHALLENGE: FrameType = 0x1a; 43 pub const FRAME_TYPE_PATH_RESPONSE: FrameType = 0x1b; 44 pub const FRAME_TYPE_CONNECTION_CLOSE_TRANSPORT: FrameType = 0x1c; 45 pub const FRAME_TYPE_CONNECTION_CLOSE_APPLICATION: FrameType = 0x1d; 46 pub const FRAME_TYPE_HANDSHAKE_DONE: FrameType = 0x1e; 47 // draft-ietf-quic-ack-delay 48 pub const FRAME_TYPE_ACK_FREQUENCY: FrameType = 0xaf; 49 // draft-ietf-quic-datagram 50 pub const FRAME_TYPE_DATAGRAM: FrameType = 0x30; 51 pub const FRAME_TYPE_DATAGRAM_WITH_LEN: FrameType = 0x31; 52 const DATAGRAM_FRAME_BIT_LEN: u64 = 0x01; 53 54 const STREAM_FRAME_BIT_FIN: u64 = 0x01; 55 const STREAM_FRAME_BIT_LEN: u64 = 0x02; 56 const STREAM_FRAME_BIT_OFF: u64 = 0x04; 57 58 #[derive(PartialEq, Eq, Debug, PartialOrd, Ord, Clone, Copy)] 59 pub enum CloseError { 60 Transport(TransportError), 61 Application(AppError), 62 } 63 64 impl CloseError { frame_type_bit(self) -> u6465 fn frame_type_bit(self) -> u64 { 66 match self { 67 Self::Transport(_) => 0, 68 Self::Application(_) => 1, 69 } 70 } 71 from_type_bit(bit: u64, code: u64) -> Self72 fn from_type_bit(bit: u64, code: u64) -> Self { 73 if (bit & 0x01) == 0 { 74 Self::Transport(code) 75 } else { 76 Self::Application(code) 77 } 78 } 79 code(&self) -> u6480 pub fn code(&self) -> u64 { 81 match self { 82 Self::Transport(c) | Self::Application(c) => *c, 83 } 84 } 85 } 86 87 impl From<ConnectionError> for CloseError { from(err: ConnectionError) -> Self88 fn from(err: ConnectionError) -> Self { 89 match err { 90 ConnectionError::Transport(c) => Self::Transport(c.code()), 91 ConnectionError::Application(c) => Self::Application(c), 92 } 93 } 94 } 95 96 #[derive(PartialEq, Debug, Default, Clone)] 97 pub struct AckRange { 98 pub(crate) gap: u64, 99 pub(crate) range: u64, 100 } 101 102 #[derive(PartialEq, Debug, Clone)] 103 pub enum Frame<'a> { 104 Padding, 105 Ping, 106 Ack { 107 largest_acknowledged: u64, 108 ack_delay: u64, 109 first_ack_range: u64, 110 ack_ranges: Vec<AckRange>, 111 }, 112 ResetStream { 113 stream_id: StreamId, 114 application_error_code: AppError, 115 final_size: u64, 116 }, 117 StopSending { 118 stream_id: StreamId, 119 application_error_code: AppError, 120 }, 121 Crypto { 122 offset: u64, 123 data: &'a [u8], 124 }, 125 NewToken { 126 token: &'a [u8], 127 }, 128 Stream { 129 stream_id: StreamId, 130 offset: u64, 131 data: &'a [u8], 132 fin: bool, 133 fill: bool, 134 }, 135 MaxData { 136 maximum_data: u64, 137 }, 138 MaxStreamData { 139 stream_id: StreamId, 140 maximum_stream_data: u64, 141 }, 142 MaxStreams { 143 stream_type: StreamType, 144 maximum_streams: u64, 145 }, 146 DataBlocked { 147 data_limit: u64, 148 }, 149 StreamDataBlocked { 150 stream_id: StreamId, 151 stream_data_limit: u64, 152 }, 153 StreamsBlocked { 154 stream_type: StreamType, 155 stream_limit: u64, 156 }, 157 NewConnectionId { 158 sequence_number: u64, 159 retire_prior: u64, 160 connection_id: &'a [u8], 161 stateless_reset_token: &'a [u8; 16], 162 }, 163 RetireConnectionId { 164 sequence_number: u64, 165 }, 166 PathChallenge { 167 data: [u8; 8], 168 }, 169 PathResponse { 170 data: [u8; 8], 171 }, 172 ConnectionClose { 173 error_code: CloseError, 174 frame_type: u64, 175 // Not a reference as we use this to hold the value. 176 // This is not used in optimized builds anyway. 177 reason_phrase: Vec<u8>, 178 }, 179 HandshakeDone, 180 AckFrequency { 181 /// The current ACK frequency sequence number. 182 seqno: u64, 183 /// The number of contiguous packets that can be received without 184 /// acknowledging immediately. 185 tolerance: u64, 186 /// The time to delay after receiving the first packet that is 187 /// not immediately acknowledged. 188 delay: u64, 189 /// Ignore reordering when deciding to immediately acknowledge. 190 ignore_order: bool, 191 }, 192 Datagram { 193 data: &'a [u8], 194 fill: bool, 195 }, 196 } 197 198 impl<'a> Frame<'a> { get_stream_type_bit(stream_type: StreamType) -> u64199 fn get_stream_type_bit(stream_type: StreamType) -> u64 { 200 match stream_type { 201 StreamType::BiDi => 0, 202 StreamType::UniDi => 1, 203 } 204 } 205 stream_type_from_bit(bit: u64) -> StreamType206 fn stream_type_from_bit(bit: u64) -> StreamType { 207 if (bit & 0x01) == 0 { 208 StreamType::BiDi 209 } else { 210 StreamType::UniDi 211 } 212 } 213 get_type(&self) -> FrameType214 pub fn get_type(&self) -> FrameType { 215 match self { 216 Self::Padding => FRAME_TYPE_PADDING, 217 Self::Ping => FRAME_TYPE_PING, 218 Self::Ack { .. } => FRAME_TYPE_ACK, // We don't do ACK ECN. 219 Self::ResetStream { .. } => FRAME_TYPE_RESET_STREAM, 220 Self::StopSending { .. } => FRAME_TYPE_STOP_SENDING, 221 Self::Crypto { .. } => FRAME_TYPE_CRYPTO, 222 Self::NewToken { .. } => FRAME_TYPE_NEW_TOKEN, 223 Self::Stream { 224 fin, offset, fill, .. 225 } => Self::stream_type(*fin, *offset > 0, *fill), 226 Self::MaxData { .. } => FRAME_TYPE_MAX_DATA, 227 Self::MaxStreamData { .. } => FRAME_TYPE_MAX_STREAM_DATA, 228 Self::MaxStreams { stream_type, .. } => { 229 FRAME_TYPE_MAX_STREAMS_BIDI + Self::get_stream_type_bit(*stream_type) 230 } 231 Self::DataBlocked { .. } => FRAME_TYPE_DATA_BLOCKED, 232 Self::StreamDataBlocked { .. } => FRAME_TYPE_STREAM_DATA_BLOCKED, 233 Self::StreamsBlocked { stream_type, .. } => { 234 FRAME_TYPE_STREAMS_BLOCKED_BIDI + Self::get_stream_type_bit(*stream_type) 235 } 236 Self::NewConnectionId { .. } => FRAME_TYPE_NEW_CONNECTION_ID, 237 Self::RetireConnectionId { .. } => FRAME_TYPE_RETIRE_CONNECTION_ID, 238 Self::PathChallenge { .. } => FRAME_TYPE_PATH_CHALLENGE, 239 Self::PathResponse { .. } => FRAME_TYPE_PATH_RESPONSE, 240 Self::ConnectionClose { error_code, .. } => { 241 FRAME_TYPE_CONNECTION_CLOSE_TRANSPORT + error_code.frame_type_bit() 242 } 243 Self::HandshakeDone => FRAME_TYPE_HANDSHAKE_DONE, 244 Self::AckFrequency { .. } => FRAME_TYPE_ACK_FREQUENCY, 245 Self::Datagram { fill, .. } => { 246 if *fill { 247 FRAME_TYPE_DATAGRAM 248 } else { 249 FRAME_TYPE_DATAGRAM_WITH_LEN 250 } 251 } 252 } 253 } 254 is_stream(&self) -> bool255 pub fn is_stream(&self) -> bool { 256 matches!( 257 self, 258 Self::ResetStream { .. } 259 | Self::StopSending { .. } 260 | Self::Stream { .. } 261 | Self::MaxData { .. } 262 | Self::MaxStreamData { .. } 263 | Self::MaxStreams { .. } 264 | Self::DataBlocked { .. } 265 | Self::StreamDataBlocked { .. } 266 | Self::StreamsBlocked { .. } 267 ) 268 } 269 stream_type(fin: bool, nonzero_offset: bool, fill: bool) -> u64270 pub fn stream_type(fin: bool, nonzero_offset: bool, fill: bool) -> u64 { 271 let mut t = FRAME_TYPE_STREAM; 272 if fin { 273 t |= STREAM_FRAME_BIT_FIN; 274 } 275 if nonzero_offset { 276 t |= STREAM_FRAME_BIT_OFF; 277 } 278 if !fill { 279 t |= STREAM_FRAME_BIT_LEN; 280 } 281 t 282 } 283 284 /// If the frame causes a recipient to generate an ACK within its 285 /// advertised maximum acknowledgement delay. ack_eliciting(&self) -> bool286 pub fn ack_eliciting(&self) -> bool { 287 !matches!( 288 self, 289 Self::Ack { .. } | Self::Padding | Self::ConnectionClose { .. } 290 ) 291 } 292 293 /// If the frame can be sent in a path probe 294 /// without initiating migration to that path. path_probing(&self) -> bool295 pub fn path_probing(&self) -> bool { 296 matches!( 297 self, 298 Self::Padding 299 | Self::NewConnectionId { .. } 300 | Self::PathChallenge { .. } 301 | Self::PathResponse { .. } 302 ) 303 } 304 305 /// Converts AckRanges as encoded in a ACK frame (see -transport 306 /// 19.3.1) into ranges of acked packets (end, start), inclusive of 307 /// start and end values. decode_ack_frame( largest_acked: u64, first_ack_range: u64, ack_ranges: &[AckRange], ) -> Res<Vec<RangeInclusive<u64>>>308 pub fn decode_ack_frame( 309 largest_acked: u64, 310 first_ack_range: u64, 311 ack_ranges: &[AckRange], 312 ) -> Res<Vec<RangeInclusive<u64>>> { 313 let mut acked_ranges = Vec::with_capacity(ack_ranges.len() + 1); 314 315 if largest_acked < first_ack_range { 316 return Err(Error::FrameEncodingError); 317 } 318 acked_ranges.push((largest_acked - first_ack_range)..=largest_acked); 319 if !ack_ranges.is_empty() && largest_acked < first_ack_range + 1 { 320 return Err(Error::FrameEncodingError); 321 } 322 let mut cur = if ack_ranges.is_empty() { 323 0 324 } else { 325 largest_acked - first_ack_range - 1 326 }; 327 for r in ack_ranges { 328 if cur < r.gap + 1 { 329 return Err(Error::FrameEncodingError); 330 } 331 cur = cur - r.gap - 1; 332 333 if cur < r.range { 334 return Err(Error::FrameEncodingError); 335 } 336 acked_ranges.push((cur - r.range)..=cur); 337 338 if cur > r.range + 1 { 339 cur -= r.range + 1; 340 } else { 341 cur -= r.range; 342 } 343 } 344 345 Ok(acked_ranges) 346 } 347 dump(&self) -> Option<String>348 pub fn dump(&self) -> Option<String> { 349 match self { 350 Self::Crypto { offset, data } => Some(format!( 351 "Crypto {{ offset: {}, len: {} }}", 352 offset, 353 data.len() 354 )), 355 Self::Stream { 356 stream_id, 357 offset, 358 fill, 359 data, 360 fin, 361 } => Some(format!( 362 "Stream {{ stream_id: {}, offset: {}, len: {}{}, fin: {} }}", 363 stream_id.as_u64(), 364 offset, 365 if *fill { ">>" } else { "" }, 366 data.len(), 367 fin, 368 )), 369 Self::Padding => None, 370 Self::Datagram { data, .. } => Some(format!("Datagram {{ len: {} }}", data.len())), 371 _ => Some(format!("{:?}", self)), 372 } 373 } 374 is_allowed(&self, pt: PacketType) -> bool375 pub fn is_allowed(&self, pt: PacketType) -> bool { 376 match self { 377 Self::Padding | Self::Ping => true, 378 Self::Crypto { .. } 379 | Self::Ack { .. } 380 | Self::ConnectionClose { 381 error_code: CloseError::Transport(_), 382 .. 383 } => pt != PacketType::ZeroRtt, 384 Self::NewToken { .. } | Self::ConnectionClose { .. } => pt == PacketType::Short, 385 _ => pt == PacketType::ZeroRtt || pt == PacketType::Short, 386 } 387 } 388 decode(dec: &mut Decoder<'a>) -> Res<Self>389 pub fn decode(dec: &mut Decoder<'a>) -> Res<Self> { 390 fn d<T>(v: Option<T>) -> Res<T> { 391 v.ok_or(Error::NoMoreData) 392 } 393 fn dv(dec: &mut Decoder) -> Res<u64> { 394 d(dec.decode_varint()) 395 } 396 397 // TODO(ekr@rtfm.com): check for minimal encoding 398 let t = d(dec.decode_varint())?; 399 match t { 400 FRAME_TYPE_PADDING => Ok(Self::Padding), 401 FRAME_TYPE_PING => Ok(Self::Ping), 402 FRAME_TYPE_RESET_STREAM => Ok(Self::ResetStream { 403 stream_id: StreamId::from(dv(dec)?), 404 application_error_code: d(dec.decode_varint())?, 405 final_size: match dec.decode_varint() { 406 Some(v) => v, 407 _ => return Err(Error::NoMoreData), 408 }, 409 }), 410 FRAME_TYPE_ACK | FRAME_TYPE_ACK_ECN => { 411 let la = dv(dec)?; 412 let ad = dv(dec)?; 413 let nr = dv(dec)?; 414 let fa = dv(dec)?; 415 let mut arr: Vec<AckRange> = Vec::with_capacity(nr as usize); 416 for _ in 0..nr { 417 let ar = AckRange { 418 gap: dv(dec)?, 419 range: dv(dec)?, 420 }; 421 arr.push(ar); 422 } 423 424 // Now check for the values for ACK_ECN. 425 if t == FRAME_TYPE_ACK_ECN { 426 dv(dec)?; 427 dv(dec)?; 428 dv(dec)?; 429 } 430 431 Ok(Self::Ack { 432 largest_acknowledged: la, 433 ack_delay: ad, 434 first_ack_range: fa, 435 ack_ranges: arr, 436 }) 437 } 438 FRAME_TYPE_STOP_SENDING => Ok(Self::StopSending { 439 stream_id: StreamId::from(dv(dec)?), 440 application_error_code: d(dec.decode_varint())?, 441 }), 442 FRAME_TYPE_CRYPTO => { 443 let offset = dv(dec)?; 444 let data = d(dec.decode_vvec())?; 445 if offset + u64::try_from(data.len()).unwrap() > ((1 << 62) - 1) { 446 return Err(Error::FrameEncodingError); 447 } 448 Ok(Self::Crypto { offset, data }) 449 } 450 FRAME_TYPE_NEW_TOKEN => { 451 let token = d(dec.decode_vvec())?; 452 if token.is_empty() { 453 return Err(Error::FrameEncodingError); 454 } 455 Ok(Self::NewToken { token }) 456 } 457 FRAME_TYPE_STREAM..=FRAME_TYPE_STREAM_MAX => { 458 let s = dv(dec)?; 459 let o = if t & STREAM_FRAME_BIT_OFF == 0 { 460 0 461 } else { 462 dv(dec)? 463 }; 464 let fill = (t & STREAM_FRAME_BIT_LEN) == 0; 465 let data = if fill { 466 qtrace!("STREAM frame, extends to the end of the packet"); 467 dec.decode_remainder() 468 } else { 469 qtrace!("STREAM frame, with length"); 470 d(dec.decode_vvec())? 471 }; 472 if o + u64::try_from(data.len()).unwrap() > ((1 << 62) - 1) { 473 return Err(Error::FrameEncodingError); 474 } 475 Ok(Self::Stream { 476 fin: (t & STREAM_FRAME_BIT_FIN) != 0, 477 stream_id: StreamId::from(s), 478 offset: o, 479 data, 480 fill, 481 }) 482 } 483 FRAME_TYPE_MAX_DATA => Ok(Self::MaxData { 484 maximum_data: dv(dec)?, 485 }), 486 FRAME_TYPE_MAX_STREAM_DATA => Ok(Self::MaxStreamData { 487 stream_id: StreamId::from(dv(dec)?), 488 maximum_stream_data: dv(dec)?, 489 }), 490 FRAME_TYPE_MAX_STREAMS_BIDI | FRAME_TYPE_MAX_STREAMS_UNIDI => { 491 let m = dv(dec)?; 492 if m > (1 << 60) { 493 return Err(Error::StreamLimitError); 494 } 495 Ok(Self::MaxStreams { 496 stream_type: Self::stream_type_from_bit(t), 497 maximum_streams: m, 498 }) 499 } 500 FRAME_TYPE_DATA_BLOCKED => Ok(Self::DataBlocked { 501 data_limit: dv(dec)?, 502 }), 503 FRAME_TYPE_STREAM_DATA_BLOCKED => Ok(Self::StreamDataBlocked { 504 stream_id: dv(dec)?.into(), 505 stream_data_limit: dv(dec)?, 506 }), 507 FRAME_TYPE_STREAMS_BLOCKED_BIDI | FRAME_TYPE_STREAMS_BLOCKED_UNIDI => { 508 Ok(Self::StreamsBlocked { 509 stream_type: Self::stream_type_from_bit(t), 510 stream_limit: dv(dec)?, 511 }) 512 } 513 FRAME_TYPE_NEW_CONNECTION_ID => { 514 let sequence_number = dv(dec)?; 515 let retire_prior = dv(dec)?; 516 let connection_id = d(dec.decode_vec(1))?; 517 if connection_id.len() > MAX_CONNECTION_ID_LEN { 518 return Err(Error::DecodingFrame); 519 } 520 let srt = d(dec.decode(16))?; 521 let stateless_reset_token = <&[_; 16]>::try_from(srt).unwrap(); 522 523 Ok(Self::NewConnectionId { 524 sequence_number, 525 retire_prior, 526 connection_id, 527 stateless_reset_token, 528 }) 529 } 530 FRAME_TYPE_RETIRE_CONNECTION_ID => Ok(Self::RetireConnectionId { 531 sequence_number: dv(dec)?, 532 }), 533 FRAME_TYPE_PATH_CHALLENGE => { 534 let data = d(dec.decode(8))?; 535 let mut datav: [u8; 8] = [0; 8]; 536 datav.copy_from_slice(data); 537 Ok(Self::PathChallenge { data: datav }) 538 } 539 FRAME_TYPE_PATH_RESPONSE => { 540 let data = d(dec.decode(8))?; 541 let mut datav: [u8; 8] = [0; 8]; 542 datav.copy_from_slice(data); 543 Ok(Self::PathResponse { data: datav }) 544 } 545 FRAME_TYPE_CONNECTION_CLOSE_TRANSPORT | FRAME_TYPE_CONNECTION_CLOSE_APPLICATION => { 546 let error_code = CloseError::from_type_bit(t, d(dec.decode_varint())?); 547 let frame_type = if t == FRAME_TYPE_CONNECTION_CLOSE_TRANSPORT { 548 dv(dec)? 549 } else { 550 0 551 }; 552 // We can tolerate this copy for now. 553 let reason_phrase = d(dec.decode_vvec())?.to_vec(); 554 Ok(Self::ConnectionClose { 555 error_code, 556 frame_type, 557 reason_phrase, 558 }) 559 } 560 FRAME_TYPE_HANDSHAKE_DONE => Ok(Self::HandshakeDone), 561 FRAME_TYPE_ACK_FREQUENCY => { 562 let seqno = dv(dec)?; 563 let tolerance = dv(dec)?; 564 if tolerance == 0 { 565 return Err(Error::FrameEncodingError); 566 } 567 let delay = dv(dec)?; 568 let ignore_order = match d(dec.decode_uint(1))? { 569 0 => false, 570 1 => true, 571 _ => return Err(Error::FrameEncodingError), 572 }; 573 Ok(Self::AckFrequency { 574 seqno, 575 tolerance, 576 delay, 577 ignore_order, 578 }) 579 } 580 FRAME_TYPE_DATAGRAM | FRAME_TYPE_DATAGRAM_WITH_LEN => { 581 let fill = (t & DATAGRAM_FRAME_BIT_LEN) == 0; 582 let data = if fill { 583 qtrace!("DATAGRAM frame, extends to the end of the packet"); 584 dec.decode_remainder() 585 } else { 586 qtrace!("DATAGRAM frame, with length"); 587 d(dec.decode_vvec())? 588 }; 589 Ok(Self::Datagram { data, fill }) 590 } 591 _ => Err(Error::UnknownFrameType), 592 } 593 } 594 } 595 596 #[cfg(test)] 597 mod tests { 598 use super::*; 599 use neqo_common::{Decoder, Encoder}; 600 just_dec(f: &Frame, s: &str)601 fn just_dec(f: &Frame, s: &str) { 602 let encoded = Encoder::from_hex(s); 603 let decoded = Frame::decode(&mut encoded.as_decoder()).unwrap(); 604 assert_eq!(*f, decoded); 605 } 606 607 #[test] padding()608 fn padding() { 609 let f = Frame::Padding; 610 just_dec(&f, "00"); 611 } 612 613 #[test] ping()614 fn ping() { 615 let f = Frame::Ping; 616 just_dec(&f, "01"); 617 } 618 619 #[test] ack()620 fn ack() { 621 let ar = vec![AckRange { gap: 1, range: 2 }, AckRange { gap: 3, range: 4 }]; 622 623 let f = Frame::Ack { 624 largest_acknowledged: 0x1234, 625 ack_delay: 0x1235, 626 first_ack_range: 0x1236, 627 ack_ranges: ar, 628 }; 629 630 just_dec(&f, "025234523502523601020304"); 631 632 // Try to parse ACK_ECN without ECN values 633 let enc = Encoder::from_hex("035234523502523601020304"); 634 let mut dec = enc.as_decoder(); 635 assert_eq!(Frame::decode(&mut dec).unwrap_err(), Error::NoMoreData); 636 637 // Try to parse ACK_ECN without ECN values 638 let enc = Encoder::from_hex("035234523502523601020304010203"); 639 let mut dec = enc.as_decoder(); 640 assert_eq!(Frame::decode(&mut dec).unwrap(), f); 641 } 642 643 #[test] reset_stream()644 fn reset_stream() { 645 let f = Frame::ResetStream { 646 stream_id: StreamId::from(0x1234), 647 application_error_code: 0x77, 648 final_size: 0x3456, 649 }; 650 651 just_dec(&f, "04523440777456"); 652 } 653 654 #[test] stop_sending()655 fn stop_sending() { 656 let f = Frame::StopSending { 657 stream_id: StreamId::from(63), 658 application_error_code: 0x77, 659 }; 660 661 just_dec(&f, "053F4077") 662 } 663 664 #[test] crypto()665 fn crypto() { 666 let f = Frame::Crypto { 667 offset: 1, 668 data: &[1, 2, 3], 669 }; 670 671 just_dec(&f, "060103010203"); 672 } 673 674 #[test] new_token()675 fn new_token() { 676 let f = Frame::NewToken { 677 token: &[0x12, 0x34, 0x56], 678 }; 679 680 just_dec(&f, "0703123456"); 681 } 682 683 #[test] empty_new_token()684 fn empty_new_token() { 685 let mut dec = Decoder::from(&[0x07, 0x00][..]); 686 assert_eq!( 687 Frame::decode(&mut dec).unwrap_err(), 688 Error::FrameEncodingError 689 ); 690 } 691 692 #[test] stream()693 fn stream() { 694 // First, just set the length bit. 695 let f = Frame::Stream { 696 fin: false, 697 stream_id: StreamId::from(5), 698 offset: 0, 699 data: &[1, 2, 3], 700 fill: false, 701 }; 702 703 just_dec(&f, "0a0503010203"); 704 705 // Now with offset != 0 and FIN 706 let f = Frame::Stream { 707 fin: true, 708 stream_id: StreamId::from(5), 709 offset: 1, 710 data: &[1, 2, 3], 711 fill: false, 712 }; 713 just_dec(&f, "0f050103010203"); 714 715 // Now to fill the packet. 716 let f = Frame::Stream { 717 fin: true, 718 stream_id: StreamId::from(5), 719 offset: 0, 720 data: &[1, 2, 3], 721 fill: true, 722 }; 723 just_dec(&f, "0905010203"); 724 } 725 726 #[test] max_data()727 fn max_data() { 728 let f = Frame::MaxData { 729 maximum_data: 0x1234, 730 }; 731 732 just_dec(&f, "105234"); 733 } 734 735 #[test] max_stream_data()736 fn max_stream_data() { 737 let f = Frame::MaxStreamData { 738 stream_id: StreamId::from(5), 739 maximum_stream_data: 0x1234, 740 }; 741 742 just_dec(&f, "11055234"); 743 } 744 745 #[test] max_streams()746 fn max_streams() { 747 let mut f = Frame::MaxStreams { 748 stream_type: StreamType::BiDi, 749 maximum_streams: 0x1234, 750 }; 751 752 just_dec(&f, "125234"); 753 754 f = Frame::MaxStreams { 755 stream_type: StreamType::UniDi, 756 maximum_streams: 0x1234, 757 }; 758 759 just_dec(&f, "135234"); 760 } 761 762 #[test] data_blocked()763 fn data_blocked() { 764 let f = Frame::DataBlocked { data_limit: 0x1234 }; 765 766 just_dec(&f, "145234"); 767 } 768 769 #[test] stream_data_blocked()770 fn stream_data_blocked() { 771 let f = Frame::StreamDataBlocked { 772 stream_id: StreamId::from(5), 773 stream_data_limit: 0x1234, 774 }; 775 776 just_dec(&f, "15055234"); 777 } 778 779 #[test] streams_blocked()780 fn streams_blocked() { 781 let mut f = Frame::StreamsBlocked { 782 stream_type: StreamType::BiDi, 783 stream_limit: 0x1234, 784 }; 785 786 just_dec(&f, "165234"); 787 788 f = Frame::StreamsBlocked { 789 stream_type: StreamType::UniDi, 790 stream_limit: 0x1234, 791 }; 792 793 just_dec(&f, "175234"); 794 } 795 796 #[test] new_connection_id()797 fn new_connection_id() { 798 let f = Frame::NewConnectionId { 799 sequence_number: 0x1234, 800 retire_prior: 0, 801 connection_id: &[0x01, 0x02], 802 stateless_reset_token: &[9; 16], 803 }; 804 805 just_dec(&f, "1852340002010209090909090909090909090909090909"); 806 } 807 808 #[test] too_large_new_connection_id()809 fn too_large_new_connection_id() { 810 let mut enc = Encoder::from_hex("18523400"); // up to the CID 811 enc.encode_vvec(&[0x0c; MAX_CONNECTION_ID_LEN + 10]); 812 enc.encode(&[0x11; 16][..]); 813 assert_eq!( 814 Frame::decode(&mut enc.as_decoder()).unwrap_err(), 815 Error::DecodingFrame 816 ); 817 } 818 819 #[test] retire_connection_id()820 fn retire_connection_id() { 821 let f = Frame::RetireConnectionId { 822 sequence_number: 0x1234, 823 }; 824 825 just_dec(&f, "195234"); 826 } 827 828 #[test] path_challenge()829 fn path_challenge() { 830 let f = Frame::PathChallenge { data: [9; 8] }; 831 832 just_dec(&f, "1a0909090909090909"); 833 } 834 835 #[test] path_response()836 fn path_response() { 837 let f = Frame::PathResponse { data: [9; 8] }; 838 839 just_dec(&f, "1b0909090909090909"); 840 } 841 842 #[test] connection_close_transport()843 fn connection_close_transport() { 844 let f = Frame::ConnectionClose { 845 error_code: CloseError::Transport(0x5678), 846 frame_type: 0x1234, 847 reason_phrase: vec![0x01, 0x02, 0x03], 848 }; 849 850 just_dec(&f, "1c80005678523403010203"); 851 } 852 853 #[test] connection_close_application()854 fn connection_close_application() { 855 let f = Frame::ConnectionClose { 856 error_code: CloseError::Application(0x5678), 857 frame_type: 0, 858 reason_phrase: vec![0x01, 0x02, 0x03], 859 }; 860 861 just_dec(&f, "1d8000567803010203"); 862 } 863 864 #[test] test_compare()865 fn test_compare() { 866 let f1 = Frame::Padding; 867 let f2 = Frame::Padding; 868 let f3 = Frame::Crypto { 869 offset: 0, 870 data: &[1, 2, 3], 871 }; 872 let f4 = Frame::Crypto { 873 offset: 0, 874 data: &[1, 2, 3], 875 }; 876 let f5 = Frame::Crypto { 877 offset: 1, 878 data: &[1, 2, 3], 879 }; 880 let f6 = Frame::Crypto { 881 offset: 0, 882 data: &[1, 2, 4], 883 }; 884 885 assert_eq!(f1, f2); 886 assert_ne!(f1, f3); 887 assert_eq!(f3, f4); 888 assert_ne!(f3, f5); 889 assert_ne!(f3, f6); 890 } 891 892 #[test] decode_ack_frame()893 fn decode_ack_frame() { 894 let res = Frame::decode_ack_frame(7, 2, &[AckRange { gap: 0, range: 3 }]); 895 assert!(res.is_ok()); 896 assert_eq!(res.unwrap(), vec![5..=7, 0..=3]); 897 } 898 899 #[test] ack_frequency()900 fn ack_frequency() { 901 let f = Frame::AckFrequency { 902 seqno: 10, 903 tolerance: 5, 904 delay: 2000, 905 ignore_order: true, 906 }; 907 just_dec(&f, "40af0a0547d001"); 908 } 909 910 #[test] ack_frequency_ignore_error_error()911 fn ack_frequency_ignore_error_error() { 912 let enc = Encoder::from_hex("40af0a0547d003"); // ignore_order of 3 913 assert_eq!( 914 Frame::decode(&mut enc.as_decoder()).unwrap_err(), 915 Error::FrameEncodingError 916 ); 917 } 918 919 /// Hopefully this test is eventually redundant. 920 #[test] ack_frequency_zero_packets()921 fn ack_frequency_zero_packets() { 922 let enc = Encoder::from_hex("40af0a000101"); // packets of 0 923 assert_eq!( 924 Frame::decode(&mut enc.as_decoder()).unwrap_err(), 925 Error::FrameEncodingError 926 ); 927 } 928 929 #[test] datagram()930 fn datagram() { 931 // Without the length bit. 932 let f = Frame::Datagram { 933 data: &[1, 2, 3], 934 fill: true, 935 }; 936 937 just_dec(&f, "4030010203"); 938 939 // With the length bit. 940 let f = Frame::Datagram { 941 data: &[1, 2, 3], 942 fill: false, 943 }; 944 just_dec(&f, "403103010203"); 945 } 946 } 947