1 // Copyright (C) 2019, Cloudflare, Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright notice, 9 // this list of conditions and the following disclaimer. 10 // 11 // * Redistributions in binary form must reproduce the above copyright 12 // notice, this list of conditions and the following disclaimer in the 13 // documentation and/or other materials provided with the distribution. 14 // 15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 17 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 19 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27 use super::Error; 28 use super::Result; 29 30 use super::frame; 31 32 pub const HTTP3_CONTROL_STREAM_TYPE_ID: u64 = 0x0; 33 pub const HTTP3_PUSH_STREAM_TYPE_ID: u64 = 0x1; 34 pub const QPACK_ENCODER_STREAM_TYPE_ID: u64 = 0x2; 35 pub const QPACK_DECODER_STREAM_TYPE_ID: u64 = 0x3; 36 37 const MAX_STATE_BUF_SIZE: usize = (1 << 24) - 1; 38 39 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 40 pub enum Type { 41 Control, 42 Request, 43 Push, 44 QpackEncoder, 45 QpackDecoder, 46 Unknown, 47 } 48 49 impl Type { 50 #[cfg(feature = "qlog")] to_qlog(self) -> qlog::events::h3::H3StreamType51 pub fn to_qlog(self) -> qlog::events::h3::H3StreamType { 52 match self { 53 Type::Control => qlog::events::h3::H3StreamType::Control, 54 Type::Request => qlog::events::h3::H3StreamType::Request, 55 Type::Push => qlog::events::h3::H3StreamType::Push, 56 Type::QpackEncoder => qlog::events::h3::H3StreamType::QpackEncode, 57 Type::QpackDecoder => qlog::events::h3::H3StreamType::QpackDecode, 58 Type::Unknown => qlog::events::h3::H3StreamType::Unknown, 59 } 60 } 61 } 62 63 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 64 pub enum State { 65 /// Reading the stream's type. 66 StreamType, 67 68 /// Reading the stream's current frame's type. 69 FrameType, 70 71 /// Reading the stream's current frame's payload length. 72 FramePayloadLen, 73 74 /// Reading the stream's current frame's payload. 75 FramePayload, 76 77 /// Reading DATA payload. 78 Data, 79 80 /// Reading the push ID. 81 PushId, 82 83 /// Reading a QPACK instruction. 84 QpackInstruction, 85 86 /// Reading and discarding data. 87 Drain, 88 89 /// All data has been read. 90 Finished, 91 } 92 93 impl Type { deserialize(v: u64) -> Result<Type>94 pub fn deserialize(v: u64) -> Result<Type> { 95 match v { 96 HTTP3_CONTROL_STREAM_TYPE_ID => Ok(Type::Control), 97 HTTP3_PUSH_STREAM_TYPE_ID => Ok(Type::Push), 98 QPACK_ENCODER_STREAM_TYPE_ID => Ok(Type::QpackEncoder), 99 QPACK_DECODER_STREAM_TYPE_ID => Ok(Type::QpackDecoder), 100 101 _ => Ok(Type::Unknown), 102 } 103 } 104 } 105 106 /// An HTTP/3 stream. 107 /// 108 /// This maintains the HTTP/3 state for streams of any type (control, request, 109 /// QPACK, ...). 110 /// 111 /// A number of bytes, depending on the current stream's state, is read from the 112 /// transport stream into the HTTP/3 stream's "state buffer". This intermediate 113 /// buffering is required due to the fact that data read from the transport 114 /// might not be complete (e.g. a varint might be split across multiple QUIC 115 /// packets). 116 /// 117 /// When enough data to complete the current state has been buffered, it is 118 /// consumed from the state buffer and the stream is transitioned to the next 119 /// state (see `State` for a list of possible states). 120 #[derive(Debug)] 121 pub struct Stream { 122 /// The corresponding transport stream's ID. 123 id: u64, 124 125 /// The stream's type (if known). 126 ty: Option<Type>, 127 128 /// The current stream state. 129 state: State, 130 131 /// The buffer holding partial data for the current state. 132 state_buf: Vec<u8>, 133 134 /// The expected amount of bytes required to complete the state. 135 state_len: usize, 136 137 /// The write offset in the state buffer, that is, how many bytes have 138 /// already been read from the transport for the current state. When 139 /// it reaches `stream_len` the state can be completed. 140 state_off: usize, 141 142 /// The type of the frame currently being parsed. 143 frame_type: Option<u64>, 144 145 /// Whether the stream was created locally, or by the peer. 146 is_local: bool, 147 148 /// Whether the stream has been remotely initialized. 149 remote_initialized: bool, 150 151 /// Whether the stream has been locally initialized. 152 local_initialized: bool, 153 154 /// Whether a `Data` event has been triggered for this stream. 155 data_event_triggered: bool, 156 157 /// The last `PRIORITY_UPDATE` frame encoded field value, if any. 158 last_priority_update: Option<Vec<u8>>, 159 } 160 161 impl Stream { 162 /// Creates a new HTTP/3 stream. 163 /// 164 /// The `is_local` parameter indicates whether the stream was created by the 165 /// local endpoint, or by the peer. new(id: u64, is_local: bool) -> Stream166 pub fn new(id: u64, is_local: bool) -> Stream { 167 let (ty, state) = if crate::stream::is_bidi(id) { 168 // All bidirectional streams are "request" streams, so we don't 169 // need to read the stream type. 170 (Some(Type::Request), State::FrameType) 171 } else { 172 // The stream's type is yet to be determined. 173 (None, State::StreamType) 174 }; 175 176 Stream { 177 id, 178 ty, 179 180 state, 181 182 // Pre-allocate a buffer to avoid multiple tiny early allocations. 183 state_buf: vec![0; 16], 184 185 // Expect one byte for the initial state, to parse the initial 186 // varint length. 187 state_len: 1, 188 state_off: 0, 189 190 frame_type: None, 191 192 is_local, 193 remote_initialized: false, 194 local_initialized: false, 195 196 data_event_triggered: false, 197 198 last_priority_update: None, 199 } 200 } 201 ty(&self) -> Option<Type>202 pub fn ty(&self) -> Option<Type> { 203 self.ty 204 } 205 state(&self) -> State206 pub fn state(&self) -> State { 207 self.state 208 } 209 210 /// Sets the stream's type and transitions to the next state. set_ty(&mut self, ty: Type) -> Result<()>211 pub fn set_ty(&mut self, ty: Type) -> Result<()> { 212 assert_eq!(self.state, State::StreamType); 213 214 self.ty = Some(ty); 215 216 let state = match ty { 217 Type::Control | Type::Request => State::FrameType, 218 219 Type::Push => State::PushId, 220 221 Type::QpackEncoder | Type::QpackDecoder => { 222 self.remote_initialized = true; 223 224 State::QpackInstruction 225 }, 226 227 Type::Unknown => State::Drain, 228 }; 229 230 self.state_transition(state, 1, true)?; 231 232 Ok(()) 233 } 234 235 /// Sets the push ID and transitions to the next state. set_push_id(&mut self, _id: u64) -> Result<()>236 pub fn set_push_id(&mut self, _id: u64) -> Result<()> { 237 assert_eq!(self.state, State::PushId); 238 239 // TODO: implement push ID. 240 241 self.state_transition(State::FrameType, 1, true)?; 242 243 Ok(()) 244 } 245 246 /// Sets the frame type and transitions to the next state. set_frame_type(&mut self, ty: u64) -> Result<()>247 pub fn set_frame_type(&mut self, ty: u64) -> Result<()> { 248 assert_eq!(self.state, State::FrameType); 249 250 // Only expect frames on Control, Request and Push streams. 251 match self.ty { 252 Some(Type::Control) => { 253 // Control stream starts uninitialized and only SETTINGS is 254 // accepted in that state. Other frames cause an error. Once 255 // initialized, no more SETTINGS are permitted. 256 match (ty, self.remote_initialized) { 257 // Initialize control stream. 258 (frame::SETTINGS_FRAME_TYPE_ID, false) => 259 self.remote_initialized = true, 260 261 // Non-SETTINGS frames not allowed on control stream 262 // before initialization. 263 (_, false) => return Err(Error::MissingSettings), 264 265 // Additional SETTINGS frame. 266 (frame::SETTINGS_FRAME_TYPE_ID, true) => 267 return Err(Error::FrameUnexpected), 268 269 // Frames that can't be received on control stream 270 // after initialization. 271 (frame::DATA_FRAME_TYPE_ID, true) => 272 return Err(Error::FrameUnexpected), 273 274 (frame::HEADERS_FRAME_TYPE_ID, true) => 275 return Err(Error::FrameUnexpected), 276 277 (frame::PUSH_PROMISE_FRAME_TYPE_ID, true) => 278 return Err(Error::FrameUnexpected), 279 280 // All other frames are ignored after initialization. 281 (_, true) => (), 282 } 283 }, 284 285 Some(Type::Request) => { 286 // Request stream starts uninitialized and only HEADERS 287 // is accepted. Other frames cause an error. 288 if !self.is_local { 289 match (ty, self.remote_initialized) { 290 (frame::HEADERS_FRAME_TYPE_ID, false) => 291 self.remote_initialized = true, 292 293 (frame::DATA_FRAME_TYPE_ID, false) => 294 return Err(Error::FrameUnexpected), 295 296 (frame::CANCEL_PUSH_FRAME_TYPE_ID, _) => 297 return Err(Error::FrameUnexpected), 298 299 (frame::SETTINGS_FRAME_TYPE_ID, _) => 300 return Err(Error::FrameUnexpected), 301 302 (frame::GOAWAY_FRAME_TYPE_ID, _) => 303 return Err(Error::FrameUnexpected), 304 305 (frame::MAX_PUSH_FRAME_TYPE_ID, _) => 306 return Err(Error::FrameUnexpected), 307 308 // All other frames can be ignored regardless of stream 309 // state. 310 _ => (), 311 } 312 } 313 }, 314 315 Some(Type::Push) => { 316 match ty { 317 // Frames that can never be received on request streams. 318 frame::CANCEL_PUSH_FRAME_TYPE_ID => 319 return Err(Error::FrameUnexpected), 320 321 frame::SETTINGS_FRAME_TYPE_ID => 322 return Err(Error::FrameUnexpected), 323 324 frame::PUSH_PROMISE_FRAME_TYPE_ID => 325 return Err(Error::FrameUnexpected), 326 327 frame::GOAWAY_FRAME_TYPE_ID => 328 return Err(Error::FrameUnexpected), 329 330 frame::MAX_PUSH_FRAME_TYPE_ID => 331 return Err(Error::FrameUnexpected), 332 333 _ => (), 334 } 335 }, 336 337 _ => return Err(Error::FrameUnexpected), 338 } 339 340 self.frame_type = Some(ty); 341 342 self.state_transition(State::FramePayloadLen, 1, true)?; 343 344 Ok(()) 345 } 346 347 // Returns the stream's current frame type, if any frame_type(&self) -> Option<u64>348 pub fn frame_type(&self) -> Option<u64> { 349 self.frame_type 350 } 351 352 /// Sets the frame's payload length and transitions to the next state. set_frame_payload_len(&mut self, len: u64) -> Result<()>353 pub fn set_frame_payload_len(&mut self, len: u64) -> Result<()> { 354 assert_eq!(self.state, State::FramePayloadLen); 355 356 // Only expect frames on Control, Request and Push streams. 357 if matches!(self.ty, Some(Type::Control | Type::Request | Type::Push)) { 358 let (state, resize) = match self.frame_type { 359 Some(frame::DATA_FRAME_TYPE_ID) => (State::Data, false), 360 361 // These frame types can never have 0 payload length because 362 // they always have fields that must be populated. 363 Some( 364 frame::GOAWAY_FRAME_TYPE_ID | 365 frame::PUSH_PROMISE_FRAME_TYPE_ID | 366 frame::CANCEL_PUSH_FRAME_TYPE_ID | 367 frame::MAX_PUSH_FRAME_TYPE_ID, 368 ) => { 369 if len == 0 { 370 return Err(Error::FrameError); 371 } 372 373 (State::FramePayload, true) 374 }, 375 376 _ => (State::FramePayload, true), 377 }; 378 379 self.state_transition(state, len as usize, resize)?; 380 381 return Ok(()); 382 } 383 384 Err(Error::InternalError) 385 } 386 387 /// Tries to fill the state buffer by reading data from the corresponding 388 /// transport stream. 389 /// 390 /// When not enough data can be read to complete the state, this returns 391 /// `Error::Done`. try_fill_buffer( &mut self, conn: &mut crate::Connection, ) -> Result<()>392 pub fn try_fill_buffer( 393 &mut self, conn: &mut crate::Connection, 394 ) -> Result<()> { 395 // If no bytes are required to be read, return early. 396 if self.state_buffer_complete() { 397 return Ok(()); 398 } 399 400 let buf = &mut self.state_buf[self.state_off..self.state_len]; 401 402 let read = match conn.stream_recv(self.id, buf) { 403 Ok((len, _)) => len, 404 405 Err(e) => { 406 // The stream is not readable anymore, so re-arm the Data event. 407 if e == crate::Error::Done { 408 self.reset_data_event(); 409 } 410 411 return Err(e.into()); 412 }, 413 }; 414 415 trace!( 416 "{} read {} bytes on stream {}", 417 conn.trace_id(), 418 read, 419 self.id, 420 ); 421 422 self.state_off += read; 423 424 if !self.state_buffer_complete() { 425 self.reset_data_event(); 426 427 return Err(Error::Done); 428 } 429 430 Ok(()) 431 } 432 433 /// Initialize the local part of the stream. initialize_local(&mut self)434 pub fn initialize_local(&mut self) { 435 self.local_initialized = true 436 } 437 438 /// Whether the stream has been locally initialized. local_initialized(&self) -> bool439 pub fn local_initialized(&self) -> bool { 440 self.local_initialized 441 } 442 443 /// Tries to fill the state buffer by reading data from the given cursor. 444 /// 445 /// This is intended to replace `try_fill_buffer()` in tests, in order to 446 /// avoid having to setup a transport connection. 447 #[cfg(test)] try_fill_buffer_for_tests( &mut self, stream: &mut std::io::Cursor<Vec<u8>>, ) -> Result<()>448 fn try_fill_buffer_for_tests( 449 &mut self, stream: &mut std::io::Cursor<Vec<u8>>, 450 ) -> Result<()> { 451 // If no bytes are required to be read, return early 452 if self.state_buffer_complete() { 453 return Ok(()); 454 } 455 456 let buf = &mut self.state_buf[self.state_off..self.state_len]; 457 458 let read = std::io::Read::read(stream, buf).unwrap(); 459 460 self.state_off += read; 461 462 if !self.state_buffer_complete() { 463 return Err(Error::Done); 464 } 465 466 Ok(()) 467 } 468 469 /// Tries to parse a varint (including length) from the state buffer. try_consume_varint(&mut self) -> Result<u64>470 pub fn try_consume_varint(&mut self) -> Result<u64> { 471 if self.state_off == 1 { 472 self.state_len = octets::varint_parse_len(self.state_buf[0]); 473 self.state_buf.resize(self.state_len, 0); 474 } 475 476 // Return early if we don't have enough data in the state buffer to 477 // parse the whole varint. 478 if !self.state_buffer_complete() { 479 return Err(Error::Done); 480 } 481 482 let varint = octets::Octets::with_slice(&self.state_buf).get_varint()?; 483 484 Ok(varint) 485 } 486 487 /// Tries to parse a frame from the state buffer. 488 /// 489 /// If successful, returns the `frame::Frame` and the payload length. try_consume_frame(&mut self) -> Result<(frame::Frame, u64)>490 pub fn try_consume_frame(&mut self) -> Result<(frame::Frame, u64)> { 491 // Processing a frame other than DATA, so re-arm the Data event. 492 self.reset_data_event(); 493 494 let payload_len = self.state_len as u64; 495 496 // TODO: properly propagate frame parsing errors. 497 let frame = frame::Frame::from_bytes( 498 self.frame_type.unwrap(), 499 payload_len, 500 &self.state_buf, 501 )?; 502 503 self.state_transition(State::FrameType, 1, true)?; 504 505 Ok((frame, payload_len)) 506 } 507 508 /// Tries to read DATA payload from the transport stream. try_consume_data( &mut self, conn: &mut crate::Connection, out: &mut [u8], ) -> Result<(usize, bool)>509 pub fn try_consume_data( 510 &mut self, conn: &mut crate::Connection, out: &mut [u8], 511 ) -> Result<(usize, bool)> { 512 let left = std::cmp::min(out.len(), self.state_len - self.state_off); 513 514 let (len, fin) = match conn.stream_recv(self.id, &mut out[..left]) { 515 Ok(v) => v, 516 517 Err(e) => { 518 // The stream is not readable anymore, so re-arm the Data event. 519 if e == crate::Error::Done { 520 self.reset_data_event(); 521 } 522 523 return Err(e.into()); 524 }, 525 }; 526 527 self.state_off += len; 528 529 // The stream is not readable anymore, so re-arm the Data event. 530 if !conn.stream_readable(self.id) { 531 self.reset_data_event(); 532 } 533 534 if self.state_buffer_complete() { 535 self.state_transition(State::FrameType, 1, true)?; 536 } 537 538 Ok((len, fin)) 539 } 540 541 /// Marks the stream as finished. finished(&mut self)542 pub fn finished(&mut self) { 543 let _ = self.state_transition(State::Finished, 0, false); 544 } 545 546 /// Tries to read DATA payload from the given cursor. 547 /// 548 /// This is intended to replace `try_consume_data()` in tests, in order to 549 /// avoid having to setup a transport connection. 550 #[cfg(test)] try_consume_data_for_tests( &mut self, stream: &mut std::io::Cursor<Vec<u8>>, out: &mut [u8], ) -> Result<usize>551 fn try_consume_data_for_tests( 552 &mut self, stream: &mut std::io::Cursor<Vec<u8>>, out: &mut [u8], 553 ) -> Result<usize> { 554 let left = std::cmp::min(out.len(), self.state_len - self.state_off); 555 556 let len = std::io::Read::read(stream, &mut out[..left]).unwrap(); 557 558 self.state_off += len; 559 560 if self.state_buffer_complete() { 561 self.state_transition(State::FrameType, 1, true)?; 562 } 563 564 Ok(len) 565 } 566 567 /// Tries to update the data triggered state for the stream. 568 /// 569 /// This returns `true` if a Data event was not already triggered before 570 /// the last reset, and updates the state. Returns `false` otherwise. try_trigger_data_event(&mut self) -> bool571 pub fn try_trigger_data_event(&mut self) -> bool { 572 if self.data_event_triggered { 573 return false; 574 } 575 576 self.data_event_triggered = true; 577 578 true 579 } 580 581 /// Resets the data triggered state. reset_data_event(&mut self)582 fn reset_data_event(&mut self) { 583 self.data_event_triggered = false; 584 } 585 586 /// Set the last priority update for the stream. set_last_priority_update(&mut self, priority_update: Option<Vec<u8>>)587 pub fn set_last_priority_update(&mut self, priority_update: Option<Vec<u8>>) { 588 self.last_priority_update = priority_update; 589 } 590 591 /// Take the last priority update and leave `None` in its place. take_last_priority_update(&mut self) -> Option<Vec<u8>>592 pub fn take_last_priority_update(&mut self) -> Option<Vec<u8>> { 593 self.last_priority_update.take() 594 } 595 596 /// Returns `true` if there is a priority update. has_last_priority_update(&self) -> bool597 pub fn has_last_priority_update(&self) -> bool { 598 self.last_priority_update.is_some() 599 } 600 601 /// Returns true if the state buffer has enough data to complete the state. state_buffer_complete(&self) -> bool602 fn state_buffer_complete(&self) -> bool { 603 self.state_off == self.state_len 604 } 605 606 /// Transitions the stream to a new state, and optionally resets the state 607 /// buffer. state_transition( &mut self, new_state: State, expected_len: usize, resize: bool, ) -> Result<()>608 fn state_transition( 609 &mut self, new_state: State, expected_len: usize, resize: bool, 610 ) -> Result<()> { 611 // Some states don't need the state buffer, so don't resize it if not 612 // necessary. 613 if resize { 614 // A peer can influence the size of the state buffer (e.g. with the 615 // payload size of a GREASE frame), so we need to limit the maximum 616 // size to avoid DoS. 617 if expected_len > MAX_STATE_BUF_SIZE { 618 return Err(Error::ExcessiveLoad); 619 } 620 621 self.state_buf.resize(expected_len, 0); 622 } 623 624 self.state = new_state; 625 self.state_off = 0; 626 self.state_len = expected_len; 627 628 Ok(()) 629 } 630 } 631 632 #[cfg(test)] 633 mod tests { 634 use crate::h3::frame::*; 635 636 use super::*; 637 open_uni(b: &mut octets::OctetsMut, ty: u64) -> Result<Stream>638 fn open_uni(b: &mut octets::OctetsMut, ty: u64) -> Result<Stream> { 639 let stream = Stream::new(2, false); 640 assert_eq!(stream.state, State::StreamType); 641 642 b.put_varint(ty)?; 643 644 Ok(stream) 645 } 646 parse_uni( stream: &mut Stream, ty: u64, cursor: &mut std::io::Cursor<Vec<u8>>, ) -> Result<()>647 fn parse_uni( 648 stream: &mut Stream, ty: u64, cursor: &mut std::io::Cursor<Vec<u8>>, 649 ) -> Result<()> { 650 stream.try_fill_buffer_for_tests(cursor)?; 651 652 let stream_ty = stream.try_consume_varint()?; 653 assert_eq!(stream_ty, ty); 654 stream.set_ty(Type::deserialize(stream_ty).unwrap())?; 655 656 Ok(()) 657 } 658 parse_skip_frame( stream: &mut Stream, cursor: &mut std::io::Cursor<Vec<u8>>, ) -> Result<()>659 fn parse_skip_frame( 660 stream: &mut Stream, cursor: &mut std::io::Cursor<Vec<u8>>, 661 ) -> Result<()> { 662 // Parse the frame type. 663 stream.try_fill_buffer_for_tests(cursor)?; 664 665 let frame_ty = stream.try_consume_varint()?; 666 667 stream.set_frame_type(frame_ty)?; 668 assert_eq!(stream.state, State::FramePayloadLen); 669 670 // Parse the frame payload length. 671 stream.try_fill_buffer_for_tests(cursor)?; 672 673 let frame_payload_len = stream.try_consume_varint()?; 674 stream.set_frame_payload_len(frame_payload_len)?; 675 assert_eq!(stream.state, State::FramePayload); 676 677 // Parse the frame payload. 678 stream.try_fill_buffer_for_tests(cursor)?; 679 680 stream.try_consume_frame()?; 681 assert_eq!(stream.state, State::FrameType); 682 683 Ok(()) 684 } 685 686 #[test] 687 /// Process incoming SETTINGS frame on control stream. control_good()688 fn control_good() { 689 let mut d = vec![42; 40]; 690 let mut b = octets::OctetsMut::with_slice(&mut d); 691 692 let raw_settings = vec![ 693 (SETTINGS_MAX_FIELD_SECTION_SIZE, 0), 694 (SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0), 695 (SETTINGS_QPACK_BLOCKED_STREAMS, 0), 696 ]; 697 698 let frame = Frame::Settings { 699 max_field_section_size: Some(0), 700 qpack_max_table_capacity: Some(0), 701 qpack_blocked_streams: Some(0), 702 connect_protocol_enabled: None, 703 h3_datagram: None, 704 grease: None, 705 raw: Some(raw_settings), 706 }; 707 708 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(); 709 frame.to_bytes(&mut b).unwrap(); 710 711 let mut cursor = std::io::Cursor::new(d); 712 713 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor) 714 .unwrap(); 715 assert_eq!(stream.state, State::FrameType); 716 717 // Parse the SETTINGS frame type. 718 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 719 720 let frame_ty = stream.try_consume_varint().unwrap(); 721 assert_eq!(frame_ty, frame::SETTINGS_FRAME_TYPE_ID); 722 723 stream.set_frame_type(frame_ty).unwrap(); 724 assert_eq!(stream.state, State::FramePayloadLen); 725 726 // Parse the SETTINGS frame payload length. 727 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 728 729 let frame_payload_len = stream.try_consume_varint().unwrap(); 730 assert_eq!(frame_payload_len, 6); 731 stream.set_frame_payload_len(frame_payload_len).unwrap(); 732 assert_eq!(stream.state, State::FramePayload); 733 734 // Parse the SETTINGS frame payload. 735 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 736 737 assert_eq!(stream.try_consume_frame(), Ok((frame, 6))); 738 assert_eq!(stream.state, State::FrameType); 739 } 740 741 #[test] 742 /// Process incoming empty SETTINGS frame on control stream. control_empty_settings()743 fn control_empty_settings() { 744 let mut d = vec![42; 40]; 745 let mut b = octets::OctetsMut::with_slice(&mut d); 746 747 let frame = Frame::Settings { 748 max_field_section_size: None, 749 qpack_max_table_capacity: None, 750 qpack_blocked_streams: None, 751 connect_protocol_enabled: None, 752 h3_datagram: None, 753 grease: None, 754 raw: Some(vec![]), 755 }; 756 757 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(); 758 frame.to_bytes(&mut b).unwrap(); 759 760 let mut cursor = std::io::Cursor::new(d); 761 762 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor) 763 .unwrap(); 764 assert_eq!(stream.state, State::FrameType); 765 766 // Parse the SETTINGS frame type. 767 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 768 769 let frame_ty = stream.try_consume_varint().unwrap(); 770 assert_eq!(frame_ty, frame::SETTINGS_FRAME_TYPE_ID); 771 772 stream.set_frame_type(frame_ty).unwrap(); 773 assert_eq!(stream.state, State::FramePayloadLen); 774 775 // Parse the SETTINGS frame payload length. 776 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 777 778 let frame_payload_len = stream.try_consume_varint().unwrap(); 779 assert_eq!(frame_payload_len, 0); 780 stream.set_frame_payload_len(frame_payload_len).unwrap(); 781 assert_eq!(stream.state, State::FramePayload); 782 783 // Parse the SETTINGS frame payload. 784 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 785 786 assert_eq!(stream.try_consume_frame(), Ok((frame, 0))); 787 assert_eq!(stream.state, State::FrameType); 788 } 789 790 #[test] 791 /// Process duplicate SETTINGS frame on control stream. control_bad_multiple_settings()792 fn control_bad_multiple_settings() { 793 let mut d = vec![42; 40]; 794 let mut b = octets::OctetsMut::with_slice(&mut d); 795 796 let raw_settings = vec![ 797 (SETTINGS_MAX_FIELD_SECTION_SIZE, 0), 798 (SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0), 799 (SETTINGS_QPACK_BLOCKED_STREAMS, 0), 800 ]; 801 802 let frame = frame::Frame::Settings { 803 max_field_section_size: Some(0), 804 qpack_max_table_capacity: Some(0), 805 qpack_blocked_streams: Some(0), 806 connect_protocol_enabled: None, 807 h3_datagram: None, 808 grease: None, 809 raw: Some(raw_settings), 810 }; 811 812 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(); 813 frame.to_bytes(&mut b).unwrap(); 814 frame.to_bytes(&mut b).unwrap(); 815 816 let mut cursor = std::io::Cursor::new(d); 817 818 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor) 819 .unwrap(); 820 assert_eq!(stream.state, State::FrameType); 821 822 // Parse the SETTINGS frame type. 823 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 824 825 let frame_ty = stream.try_consume_varint().unwrap(); 826 assert_eq!(frame_ty, frame::SETTINGS_FRAME_TYPE_ID); 827 828 stream.set_frame_type(frame_ty).unwrap(); 829 assert_eq!(stream.state, State::FramePayloadLen); 830 831 // Parse the SETTINGS frame payload length. 832 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 833 834 let frame_payload_len = stream.try_consume_varint().unwrap(); 835 assert_eq!(frame_payload_len, 6); 836 stream.set_frame_payload_len(frame_payload_len).unwrap(); 837 assert_eq!(stream.state, State::FramePayload); 838 839 // Parse the SETTINGS frame payload. 840 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 841 842 assert_eq!(stream.try_consume_frame(), Ok((frame, 6))); 843 assert_eq!(stream.state, State::FrameType); 844 845 // Parse the second SETTINGS frame type. 846 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 847 848 let frame_ty = stream.try_consume_varint().unwrap(); 849 assert_eq!(stream.set_frame_type(frame_ty), Err(Error::FrameUnexpected)); 850 } 851 852 #[test] 853 /// Process other frame before SETTINGS frame on control stream. control_bad_late_settings()854 fn control_bad_late_settings() { 855 let mut d = vec![42; 40]; 856 let mut b = octets::OctetsMut::with_slice(&mut d); 857 858 let goaway = frame::Frame::GoAway { id: 0 }; 859 860 let raw_settings = vec![ 861 (SETTINGS_MAX_FIELD_SECTION_SIZE, 0), 862 (SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0), 863 (SETTINGS_QPACK_BLOCKED_STREAMS, 0), 864 ]; 865 866 let settings = frame::Frame::Settings { 867 max_field_section_size: Some(0), 868 qpack_max_table_capacity: Some(0), 869 qpack_blocked_streams: Some(0), 870 connect_protocol_enabled: None, 871 h3_datagram: None, 872 grease: None, 873 raw: Some(raw_settings), 874 }; 875 876 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(); 877 goaway.to_bytes(&mut b).unwrap(); 878 settings.to_bytes(&mut b).unwrap(); 879 880 let mut cursor = std::io::Cursor::new(d); 881 882 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor) 883 .unwrap(); 884 assert_eq!(stream.state, State::FrameType); 885 886 // Parse GOAWAY. 887 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 888 889 let frame_ty = stream.try_consume_varint().unwrap(); 890 assert_eq!(stream.set_frame_type(frame_ty), Err(Error::MissingSettings)); 891 } 892 893 #[test] 894 /// Process not-allowed frame on control stream. control_bad_frame()895 fn control_bad_frame() { 896 let mut d = vec![42; 40]; 897 let mut b = octets::OctetsMut::with_slice(&mut d); 898 899 let header_block = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; 900 let hdrs = frame::Frame::Headers { header_block }; 901 902 let raw_settings = vec![ 903 (SETTINGS_MAX_FIELD_SECTION_SIZE, 0), 904 (SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0), 905 (SETTINGS_QPACK_BLOCKED_STREAMS, 0), 906 (33, 33), 907 ]; 908 909 let settings = frame::Frame::Settings { 910 max_field_section_size: Some(0), 911 qpack_max_table_capacity: Some(0), 912 qpack_blocked_streams: Some(0), 913 connect_protocol_enabled: None, 914 h3_datagram: None, 915 grease: None, 916 raw: Some(raw_settings), 917 }; 918 919 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(); 920 settings.to_bytes(&mut b).unwrap(); 921 hdrs.to_bytes(&mut b).unwrap(); 922 923 let mut cursor = std::io::Cursor::new(d); 924 925 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor) 926 .unwrap(); 927 assert_eq!(stream.state, State::FrameType); 928 929 // Parse first SETTINGS frame. 930 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 931 932 let frame_ty = stream.try_consume_varint().unwrap(); 933 stream.set_frame_type(frame_ty).unwrap(); 934 935 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 936 937 let frame_payload_len = stream.try_consume_varint().unwrap(); 938 stream.set_frame_payload_len(frame_payload_len).unwrap(); 939 940 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 941 942 assert!(stream.try_consume_frame().is_ok()); 943 944 // Parse HEADERS. 945 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 946 947 let frame_ty = stream.try_consume_varint().unwrap(); 948 assert_eq!(stream.set_frame_type(frame_ty), Err(Error::FrameUnexpected)); 949 } 950 951 #[test] request_no_data()952 fn request_no_data() { 953 let mut stream = Stream::new(0, false); 954 955 assert_eq!(stream.ty, Some(Type::Request)); 956 assert_eq!(stream.state, State::FrameType); 957 958 assert_eq!(stream.try_consume_varint(), Err(Error::Done)); 959 } 960 961 #[test] request_good()962 fn request_good() { 963 let mut stream = Stream::new(0, false); 964 965 let mut d = vec![42; 128]; 966 let mut b = octets::OctetsMut::with_slice(&mut d); 967 968 let header_block = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; 969 let payload = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; 970 let hdrs = frame::Frame::Headers { header_block }; 971 let data = frame::Frame::Data { 972 payload: payload.clone(), 973 }; 974 975 hdrs.to_bytes(&mut b).unwrap(); 976 data.to_bytes(&mut b).unwrap(); 977 978 let mut cursor = std::io::Cursor::new(d); 979 980 // Parse the HEADERS frame type. 981 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 982 983 let frame_ty = stream.try_consume_varint().unwrap(); 984 assert_eq!(frame_ty, frame::HEADERS_FRAME_TYPE_ID); 985 986 stream.set_frame_type(frame_ty).unwrap(); 987 assert_eq!(stream.state, State::FramePayloadLen); 988 989 // Parse the HEADERS frame payload length. 990 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 991 992 let frame_payload_len = stream.try_consume_varint().unwrap(); 993 assert_eq!(frame_payload_len, 12); 994 995 stream.set_frame_payload_len(frame_payload_len).unwrap(); 996 assert_eq!(stream.state, State::FramePayload); 997 998 // Parse the HEADERS frame. 999 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1000 1001 assert_eq!(stream.try_consume_frame(), Ok((hdrs, 12))); 1002 assert_eq!(stream.state, State::FrameType); 1003 1004 // Parse the DATA frame type. 1005 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1006 1007 let frame_ty = stream.try_consume_varint().unwrap(); 1008 assert_eq!(frame_ty, frame::DATA_FRAME_TYPE_ID); 1009 1010 stream.set_frame_type(frame_ty).unwrap(); 1011 assert_eq!(stream.state, State::FramePayloadLen); 1012 1013 // Parse the DATA frame payload length. 1014 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1015 1016 let frame_payload_len = stream.try_consume_varint().unwrap(); 1017 assert_eq!(frame_payload_len, 12); 1018 1019 stream.set_frame_payload_len(frame_payload_len).unwrap(); 1020 assert_eq!(stream.state, State::Data); 1021 1022 // Parse the DATA payload. 1023 let mut recv_buf = vec![0; payload.len()]; 1024 assert_eq!( 1025 stream.try_consume_data_for_tests(&mut cursor, &mut recv_buf), 1026 Ok(payload.len()) 1027 ); 1028 assert_eq!(payload, recv_buf); 1029 1030 assert_eq!(stream.state, State::FrameType); 1031 } 1032 1033 #[test] push_good()1034 fn push_good() { 1035 let mut d = vec![42; 128]; 1036 let mut b = octets::OctetsMut::with_slice(&mut d); 1037 1038 let header_block = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; 1039 let payload = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; 1040 let hdrs = frame::Frame::Headers { header_block }; 1041 let data = frame::Frame::Data { 1042 payload: payload.clone(), 1043 }; 1044 1045 let mut stream = open_uni(&mut b, HTTP3_PUSH_STREAM_TYPE_ID).unwrap(); 1046 b.put_varint(1).unwrap(); 1047 hdrs.to_bytes(&mut b).unwrap(); 1048 data.to_bytes(&mut b).unwrap(); 1049 1050 let mut cursor = std::io::Cursor::new(d); 1051 1052 parse_uni(&mut stream, HTTP3_PUSH_STREAM_TYPE_ID, &mut cursor).unwrap(); 1053 assert_eq!(stream.state, State::PushId); 1054 1055 // Parse push ID. 1056 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1057 1058 let push_id = stream.try_consume_varint().unwrap(); 1059 assert_eq!(push_id, 1); 1060 1061 stream.set_push_id(push_id).unwrap(); 1062 assert_eq!(stream.state, State::FrameType); 1063 1064 // Parse the HEADERS frame type. 1065 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1066 1067 let frame_ty = stream.try_consume_varint().unwrap(); 1068 assert_eq!(frame_ty, frame::HEADERS_FRAME_TYPE_ID); 1069 1070 stream.set_frame_type(frame_ty).unwrap(); 1071 assert_eq!(stream.state, State::FramePayloadLen); 1072 1073 // Parse the HEADERS frame payload length. 1074 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1075 1076 let frame_payload_len = stream.try_consume_varint().unwrap(); 1077 assert_eq!(frame_payload_len, 12); 1078 1079 stream.set_frame_payload_len(frame_payload_len).unwrap(); 1080 assert_eq!(stream.state, State::FramePayload); 1081 1082 // Parse the HEADERS frame. 1083 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1084 1085 assert_eq!(stream.try_consume_frame(), Ok((hdrs, 12))); 1086 assert_eq!(stream.state, State::FrameType); 1087 1088 // Parse the DATA frame type. 1089 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1090 1091 let frame_ty = stream.try_consume_varint().unwrap(); 1092 assert_eq!(frame_ty, frame::DATA_FRAME_TYPE_ID); 1093 1094 stream.set_frame_type(frame_ty).unwrap(); 1095 assert_eq!(stream.state, State::FramePayloadLen); 1096 1097 // Parse the DATA frame payload length. 1098 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1099 1100 let frame_payload_len = stream.try_consume_varint().unwrap(); 1101 assert_eq!(frame_payload_len, 12); 1102 1103 stream.set_frame_payload_len(frame_payload_len).unwrap(); 1104 assert_eq!(stream.state, State::Data); 1105 1106 // Parse the DATA payload. 1107 let mut recv_buf = vec![0; payload.len()]; 1108 assert_eq!( 1109 stream.try_consume_data_for_tests(&mut cursor, &mut recv_buf), 1110 Ok(payload.len()) 1111 ); 1112 assert_eq!(payload, recv_buf); 1113 1114 assert_eq!(stream.state, State::FrameType); 1115 } 1116 1117 #[test] grease()1118 fn grease() { 1119 let mut d = vec![42; 20]; 1120 let mut b = octets::OctetsMut::with_slice(&mut d); 1121 1122 let mut stream = open_uni(&mut b, 33).unwrap(); 1123 1124 let mut cursor = std::io::Cursor::new(d); 1125 1126 // Parse stream type. 1127 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1128 1129 let stream_ty = stream.try_consume_varint().unwrap(); 1130 assert_eq!(stream_ty, 33); 1131 stream 1132 .set_ty(Type::deserialize(stream_ty).unwrap()) 1133 .unwrap(); 1134 assert_eq!(stream.state, State::Drain); 1135 } 1136 1137 #[test] data_before_headers()1138 fn data_before_headers() { 1139 let mut stream = Stream::new(0, false); 1140 1141 let mut d = vec![42; 128]; 1142 let mut b = octets::OctetsMut::with_slice(&mut d); 1143 1144 let data = frame::Frame::Data { 1145 payload: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 1146 }; 1147 1148 data.to_bytes(&mut b).unwrap(); 1149 1150 let mut cursor = std::io::Cursor::new(d); 1151 1152 // Parse the DATA frame type. 1153 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1154 1155 let frame_ty = stream.try_consume_varint().unwrap(); 1156 assert_eq!(frame_ty, frame::DATA_FRAME_TYPE_ID); 1157 1158 assert_eq!(stream.set_frame_type(frame_ty), Err(Error::FrameUnexpected)); 1159 } 1160 1161 #[test] zero_length_goaway()1162 fn zero_length_goaway() { 1163 let mut d = vec![42; 128]; 1164 let mut b = octets::OctetsMut::with_slice(&mut d); 1165 1166 let frame = Frame::Settings { 1167 max_field_section_size: None, 1168 qpack_max_table_capacity: None, 1169 qpack_blocked_streams: None, 1170 connect_protocol_enabled: None, 1171 h3_datagram: None, 1172 grease: None, 1173 raw: Some(vec![]), 1174 }; 1175 1176 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(); 1177 frame.to_bytes(&mut b).unwrap(); 1178 1179 // Write a 0-length payload frame. 1180 b.put_varint(frame::GOAWAY_FRAME_TYPE_ID).unwrap(); 1181 b.put_varint(0).unwrap(); 1182 1183 let mut cursor = std::io::Cursor::new(d); 1184 1185 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor) 1186 .unwrap(); 1187 1188 // Skip SETTINGS frame type. 1189 parse_skip_frame(&mut stream, &mut cursor).unwrap(); 1190 1191 // Parse frame type. 1192 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1193 let frame_ty = stream.try_consume_varint().unwrap(); 1194 assert_eq!(frame_ty, frame::GOAWAY_FRAME_TYPE_ID); 1195 1196 stream.set_frame_type(frame_ty).unwrap(); 1197 assert_eq!(stream.state, State::FramePayloadLen); 1198 1199 // Parse frame payload length. 1200 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1201 let frame_payload_len = stream.try_consume_varint().unwrap(); 1202 assert_eq!( 1203 Err(Error::FrameError), 1204 stream.set_frame_payload_len(frame_payload_len) 1205 ); 1206 } 1207 1208 #[test] zero_length_push_promise()1209 fn zero_length_push_promise() { 1210 let mut d = vec![42; 128]; 1211 let mut b = octets::OctetsMut::with_slice(&mut d); 1212 1213 let mut stream = Stream::new(0, false); 1214 1215 assert_eq!(stream.ty, Some(Type::Request)); 1216 assert_eq!(stream.state, State::FrameType); 1217 1218 // Write a 0-length payload frame. 1219 b.put_varint(frame::PUSH_PROMISE_FRAME_TYPE_ID).unwrap(); 1220 b.put_varint(0).unwrap(); 1221 1222 let mut cursor = std::io::Cursor::new(d); 1223 1224 // Parse frame type. 1225 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1226 let frame_ty = stream.try_consume_varint().unwrap(); 1227 assert_eq!(frame_ty, frame::PUSH_PROMISE_FRAME_TYPE_ID); 1228 1229 stream.set_frame_type(frame_ty).unwrap(); 1230 assert_eq!(stream.state, State::FramePayloadLen); 1231 1232 // Parse frame payload length. 1233 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1234 let frame_payload_len = stream.try_consume_varint().unwrap(); 1235 assert_eq!( 1236 Err(Error::FrameError), 1237 stream.set_frame_payload_len(frame_payload_len) 1238 ); 1239 } 1240 1241 #[test] zero_length_cancel_push()1242 fn zero_length_cancel_push() { 1243 let mut d = vec![42; 128]; 1244 let mut b = octets::OctetsMut::with_slice(&mut d); 1245 1246 let frame = Frame::Settings { 1247 max_field_section_size: None, 1248 qpack_max_table_capacity: None, 1249 qpack_blocked_streams: None, 1250 connect_protocol_enabled: None, 1251 h3_datagram: None, 1252 grease: None, 1253 raw: Some(vec![]), 1254 }; 1255 1256 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(); 1257 frame.to_bytes(&mut b).unwrap(); 1258 1259 // Write a 0-length payload frame. 1260 b.put_varint(frame::CANCEL_PUSH_FRAME_TYPE_ID).unwrap(); 1261 b.put_varint(0).unwrap(); 1262 1263 let mut cursor = std::io::Cursor::new(d); 1264 1265 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor) 1266 .unwrap(); 1267 1268 // Skip SETTINGS frame type. 1269 parse_skip_frame(&mut stream, &mut cursor).unwrap(); 1270 1271 // Parse frame type. 1272 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1273 let frame_ty = stream.try_consume_varint().unwrap(); 1274 assert_eq!(frame_ty, frame::CANCEL_PUSH_FRAME_TYPE_ID); 1275 1276 stream.set_frame_type(frame_ty).unwrap(); 1277 assert_eq!(stream.state, State::FramePayloadLen); 1278 1279 // Parse frame payload length. 1280 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1281 let frame_payload_len = stream.try_consume_varint().unwrap(); 1282 assert_eq!( 1283 Err(Error::FrameError), 1284 stream.set_frame_payload_len(frame_payload_len) 1285 ); 1286 } 1287 1288 #[test] zero_length_max_push_id()1289 fn zero_length_max_push_id() { 1290 let mut d = vec![42; 128]; 1291 let mut b = octets::OctetsMut::with_slice(&mut d); 1292 1293 let frame = Frame::Settings { 1294 max_field_section_size: None, 1295 qpack_max_table_capacity: None, 1296 qpack_blocked_streams: None, 1297 connect_protocol_enabled: None, 1298 h3_datagram: None, 1299 grease: None, 1300 raw: Some(vec![]), 1301 }; 1302 1303 let mut stream = open_uni(&mut b, HTTP3_CONTROL_STREAM_TYPE_ID).unwrap(); 1304 frame.to_bytes(&mut b).unwrap(); 1305 1306 // Write a 0-length payload frame. 1307 b.put_varint(frame::MAX_PUSH_FRAME_TYPE_ID).unwrap(); 1308 b.put_varint(0).unwrap(); 1309 1310 let mut cursor = std::io::Cursor::new(d); 1311 1312 parse_uni(&mut stream, HTTP3_CONTROL_STREAM_TYPE_ID, &mut cursor) 1313 .unwrap(); 1314 1315 // Skip SETTINGS frame type. 1316 parse_skip_frame(&mut stream, &mut cursor).unwrap(); 1317 1318 // Parse frame type. 1319 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1320 let frame_ty = stream.try_consume_varint().unwrap(); 1321 assert_eq!(frame_ty, frame::MAX_PUSH_FRAME_TYPE_ID); 1322 1323 stream.set_frame_type(frame_ty).unwrap(); 1324 assert_eq!(stream.state, State::FramePayloadLen); 1325 1326 // Parse frame payload length. 1327 stream.try_fill_buffer_for_tests(&mut cursor).unwrap(); 1328 let frame_payload_len = stream.try_consume_varint().unwrap(); 1329 assert_eq!( 1330 Err(Error::FrameError), 1331 stream.set_frame_payload_len(frame_payload_len) 1332 ); 1333 } 1334 } 1335