diff --git a/h3-shim/src/tests.rs b/h3-shim/src/tests.rs index 2614e6b5..7bb8f659 100644 --- a/h3-shim/src/tests.rs +++ b/h3-shim/src/tests.rs @@ -40,9 +40,9 @@ async fn h3_test() { }; let client = async move { - client_example::run(client_opt) + _ = client_example::run(client_opt) .await - .expect("client failed"); + .inspect_err(|e| println!("Error occur: {}", e.to_string())); }; let server = async move { diff --git a/qbase/src/frame/ack.rs b/qbase/src/frame/ack.rs index 5449b71e..d854335b 100644 --- a/qbase/src/frame/ack.rs +++ b/qbase/src/frame/ack.rs @@ -45,7 +45,7 @@ impl super::BeFrame for AckFrame { } fn max_encoding_size(&self) -> usize { - 1 + 8 + 8 + 8 + self.ranges.len() * 16 + if self.ecn.is_some() { 24 } else { 0 } + 1 + 8 + 8 + 8 + 8 + self.ranges.len() * 16 + if self.ecn.is_some() { 24 } else { 0 } } fn encoding_size(&self) -> usize { @@ -191,10 +191,35 @@ mod tests { use super::{ack_frame_with_flag, be_ecn_counts, AckFrame, EcnCounts, ACK_FRAME_TYPE}; use crate::{ - frame::io::WriteFrame, + frame::{io::WriteFrame, BeFrame, FrameType}, varint::{be_varint, VarInt}, }; + #[test] + fn test_ack_frame() { + // test frame type, encoding size, and max encoding size + let mut frame = AckFrame { + largest: VarInt::from_u32(0x1234), + delay: VarInt::from_u32(0x1234), + first_range: VarInt::from_u32(0x1234), + ranges: vec![(VarInt::from_u32(3), VarInt::from_u32(20))], + ecn: None, + }; + assert_eq!(frame.frame_type(), FrameType::Ack(0)); + assert_eq!(frame.encoding_size(), 1 + 2 * 3 + 1 + 2); + assert_eq!(frame.max_encoding_size(), 1 + 4 * 8 + 2 * 8); + + // test set_ecn and take_ecn + let ecn = EcnCounts { + ect0: VarInt::from_u32(0x1234), + ect1: VarInt::from_u32(0x1234), + ce: VarInt::from_u32(0x1234), + }; + frame.set_ecn(ecn); + assert!(frame.ecn.is_some()); + assert_eq!(frame.take_ecn(), Some(ecn)); + } + #[test] fn test_read_ecn_count() { let input = vec![0x52, 0x34, 0x52, 0x34, 0x52, 0x34]; diff --git a/qbase/src/frame/connection_close.rs b/qbase/src/frame/connection_close.rs index c085a4a9..d8842b10 100644 --- a/qbase/src/frame/connection_close.rs +++ b/qbase/src/frame/connection_close.rs @@ -125,7 +125,25 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { - use crate::{error::ErrorKind, frame::io::WriteFrame}; + use crate::{ + error::ErrorKind, + frame::{io::WriteFrame, BeFrame, FrameType}, + }; + + #[test] + fn test_connection_close_frame() { + let frame = super::ConnectionCloseFrame { + error_kind: ErrorKind::Application, + frame_type: None, + reason: "wrong".into(), + }; + assert_eq!( + frame.frame_type(), + FrameType::ConnectionClose(super::APP_LAYER) + ); + assert_eq!(frame.max_encoding_size(), 1 + 8 + 2 + 5); + assert_eq!(frame.encoding_size(), 1 + 1 + 1 + 5); + } #[test] fn test_read_connection_close_frame() { diff --git a/qbase/src/frame/crypto.rs b/qbase/src/frame/crypto.rs index ecc17f65..97037840 100644 --- a/qbase/src/frame/crypto.rs +++ b/qbase/src/frame/crypto.rs @@ -38,9 +38,7 @@ impl super::BeFrame for CryptoFrame { } fn encoding_size(&self) -> usize { - 1 + self.offset.encoding_size() - + self.length.encoding_size() - + self.length.into_inner() as usize + 1 + self.offset.encoding_size() + self.length.encoding_size() } } @@ -113,7 +111,22 @@ where #[cfg(test)] mod tests { use super::{CryptoFrame, CRYPTO_FRAME_TYPE}; - use crate::{frame::io::WriteDataFrame, varint::VarInt}; + use crate::{ + frame::{io::WriteDataFrame, BeFrame}, + varint::VarInt, + }; + + #[test] + fn test_crypto_frame() { + let frame = CryptoFrame { + offset: VarInt::from_u32(0), + length: VarInt::from_u32(500), + }; + assert_eq!(frame.frame_type(), super::super::FrameType::Crypto); + assert_eq!(frame.max_encoding_size(), 1 + 8 + 8); + assert_eq!(frame.encoding_size(), 1 + 1 + 2); + assert_eq!(frame.range(), 0..500); + } #[test] fn test_read_crypto_frame() { diff --git a/qbase/src/frame/data_blocked.rs b/qbase/src/frame/data_blocked.rs index 774d9d01..4e234783 100644 --- a/qbase/src/frame/data_blocked.rs +++ b/qbase/src/frame/data_blocked.rs @@ -49,7 +49,17 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { use super::{DataBlockedFrame, DATA_BLOCKED_FRAME_TYPE}; - use crate::frame::io::WriteFrame; + use crate::frame::{io::WriteFrame, BeFrame}; + + #[test] + fn test_data_blocked_frame() { + let frame = DataBlockedFrame { + limit: crate::varint::VarInt::from_u32(0x1234), + }; + assert_eq!(frame.frame_type(), super::super::FrameType::Crypto); + assert_eq!(frame.max_encoding_size(), 1 + 8); + assert_eq!(frame.encoding_size(), 1 + 2); + } #[test] fn test_read_data_blocked_frame() { diff --git a/qbase/src/frame/datagram.rs b/qbase/src/frame/datagram.rs index 0acd83a6..4b0686bb 100644 --- a/qbase/src/frame/datagram.rs +++ b/qbase/src/frame/datagram.rs @@ -77,6 +77,16 @@ mod tests { use super::*; use crate::frame::io::WriteDataFrame; + #[test] + fn test_datagram_frame() { + let frame = DatagramFrame { + length: Some(VarInt::from_u32(3)), + }; + assert_eq!(frame.frame_type(), FrameType::Datagram(1)); + assert_eq!(frame.max_encoding_size(), 1 + 8); + assert_eq!(frame.encoding_size(), 1 + 1); + } + #[test] fn test_datagram_frame_with_flag() { let input = [0x05, 0x00, 0x00, 0x00, 0x00, 0x00]; diff --git a/qbase/src/frame/error.rs b/qbase/src/frame/error.rs index 4dd8f2bb..ed83dd1e 100644 --- a/qbase/src/frame/error.rs +++ b/qbase/src/frame/error.rs @@ -77,3 +77,83 @@ impl nom::error::ParseError<&[u8]> for Error { } // TODO: conver DecodingError to quic error + +#[cfg(test)] +mod tests { + use nom::error::ParseError; + + use super::*; + use crate::packet::r#type::{ + long::{Type::V1, Ver1}, + Type, + }; + + #[test] + fn test_error_conversion_to_transport_error() { + let cases = vec![ + (Error::NoFrames, TransportErrorKind::ProtocolViolation), + ( + Error::IncompleteType("test".to_string()), + TransportErrorKind::FrameEncoding, + ), + ( + Error::InvalidType(VarInt::from_u32(0x1f)), + TransportErrorKind::FrameEncoding, + ), + ( + Error::WrongType(FrameType::Ping, Type::Long(V1(Ver1::INITIAL))), + TransportErrorKind::FrameEncoding, + ), + ( + Error::IncompleteFrame(FrameType::Ping, "incomplete".to_string()), + TransportErrorKind::FrameEncoding, + ), + ( + Error::ParseError(FrameType::Ping, "parse error".to_string()), + TransportErrorKind::FrameEncoding, + ), + ]; + + for (error, expected_kind) in cases { + let transport_error: TransportError = error.into(); + assert_eq!(transport_error.kind(), expected_kind); + } + } + + #[test] + fn test_nom_error_conversion() { + let error = Error::NoFrames; + let nom_error = nom::Err::Error(error.clone()); + let converted: Error = nom_error.into(); + assert_eq!(converted, error); + + let nom_failure = nom::Err::Failure(error.clone()); + let converted: Error = nom_failure.into(); + assert_eq!(converted, error); + } + + #[test] + fn test_parse_error_impl() { + let error = Error::ParseError(FrameType::Ping, "test error".to_string()); + let appended = Error::append(&[], NomErrorKind::ManyTill, error.clone()); + assert_eq!(appended, error); + } + + #[test] + #[should_panic(expected = "QUIC frame parser must always consume")] + fn test_parse_error_unreachable() { + Error::from_error_kind(&[], NomErrorKind::ManyTill); + } + + #[test] + fn test_error_display() { + let error = Error::NoFrames; + assert_eq!(error.to_string(), "A packet containing no frames"); + + let error = Error::IncompleteType("test".to_string()); + assert_eq!(error.to_string(), "Incomplete frame type: test"); + + let error = Error::InvalidType(VarInt::from_u32(0x1f)); + assert_eq!(error.to_string(), "Invalid frame type from 31"); + } +} diff --git a/qbase/src/frame/handshake_done.rs b/qbase/src/frame/handshake_done.rs index e995fa77..aa590f09 100644 --- a/qbase/src/frame/handshake_done.rs +++ b/qbase/src/frame/handshake_done.rs @@ -34,7 +34,14 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { - use crate::frame::{io::WriteFrame, HandshakeDoneFrame}; + use crate::frame::{io::WriteFrame, BeFrame, FrameType, HandshakeDoneFrame}; + + #[test] + fn test_handshake_done_frame() { + assert_eq!(HandshakeDoneFrame.frame_type(), FrameType::HandshakeDone); + assert_eq!(HandshakeDoneFrame.max_encoding_size(), 1); + assert_eq!(HandshakeDoneFrame.encoding_size(), 1); + } #[test] fn test_read_handshake_done_frame() { diff --git a/qbase/src/frame/max_data.rs b/qbase/src/frame/max_data.rs index b190836c..f0a6f360 100644 --- a/qbase/src/frame/max_data.rs +++ b/qbase/src/frame/max_data.rs @@ -49,7 +49,20 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { use super::{MaxDataFrame, MAX_DATA_FRAME_TYPE}; - use crate::{frame::io::WriteFrame, varint::VarInt}; + use crate::{ + frame::{io::WriteFrame, BeFrame, FrameType}, + varint::VarInt, + }; + + #[test] + fn test_max_data_frame() { + let frame = MaxDataFrame { + max_data: VarInt::from_u32(0x1234), + }; + assert_eq!(frame.frame_type(), FrameType::MaxData); + assert_eq!(frame.max_encoding_size(), 1 + 8); + assert_eq!(frame.encoding_size(), 1 + 2); + } #[test] fn test_read_max_data_frame() { diff --git a/qbase/src/frame/max_stream_data.rs b/qbase/src/frame/max_stream_data.rs index e7864595..ffb7e2c2 100644 --- a/qbase/src/frame/max_stream_data.rs +++ b/qbase/src/frame/max_stream_data.rs @@ -71,7 +71,21 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { use super::{MaxStreamDataFrame, MAX_STREAM_DATA_FRAME_TYPE}; - use crate::{frame::io::WriteFrame, varint::VarInt}; + use crate::{ + frame::{io::WriteFrame, BeFrame, FrameType}, + varint::VarInt, + }; + + #[test] + fn test_max_stream_data_frame() { + let frame = MaxStreamDataFrame { + stream_id: VarInt::from_u32(0x1234).into(), + max_stream_data: VarInt::from_u32(0x5678), + }; + assert_eq!(frame.frame_type(), FrameType::MaxStreamData); + assert_eq!(frame.max_encoding_size(), 1 + 8 + 8); + assert_eq!(frame.encoding_size(), 1 + 2 + 4); + } #[test] fn test_read_max_stream_data_frame() { diff --git a/qbase/src/frame/max_streams.rs b/qbase/src/frame/max_streams.rs index 872a9681..3ed19f33 100644 --- a/qbase/src/frame/max_streams.rs +++ b/qbase/src/frame/max_streams.rs @@ -97,10 +97,23 @@ impl super::io::WriteFrame for T { mod tests { use super::{MaxStreamsFrame, MAX_STREAMS_FRAME_TYPE}; use crate::{ - frame::io::WriteFrame, + frame::{io::WriteFrame, BeFrame, FrameType}, varint::{be_varint, VarInt}, }; + #[test] + fn test_max_streams_frame() { + let frame = MaxStreamsFrame::Bi(VarInt::from_u32(0x1234)); + assert_eq!(frame.frame_type(), FrameType::MaxStreams(0)); + assert_eq!(frame.max_encoding_size(), 1 + 8); + assert_eq!(frame.encoding_size(), 1 + 2); + + let frame = MaxStreamsFrame::Uni(VarInt::from_u32(0x1236)); + assert_eq!(frame.frame_type(), FrameType::MaxStreams(1)); + assert_eq!(frame.max_encoding_size(), 1 + 8); + assert_eq!(frame.encoding_size(), 1 + 2); + } + #[test] fn test_read_max_streams_frame() { use nom::combinator::flat_map; diff --git a/qbase/src/frame/new_connection_id.rs b/qbase/src/frame/new_connection_id.rs index 26cf958a..d11ba73e 100644 --- a/qbase/src/frame/new_connection_id.rs +++ b/qbase/src/frame/new_connection_id.rs @@ -103,3 +103,73 @@ impl super::io::WriteFrame for T { self.put_slice(&frame.reset_token); } } + +#[cfg(test)] +mod tests { + use bytes::{BufMut, BytesMut}; + + use super::*; + use crate::frame::{io::WriteFrame, BeFrame, FrameType}; + + #[test] + fn test_new_connection_id_frame() { + let new_cid_frame = NewConnectionIdFrame::new( + ConnectionId::from_slice(&[1, 2, 3, 4][..]), + VarInt::from_u32(1), + VarInt::from_u32(0), + ); + assert_eq!(new_cid_frame.sequence, VarInt::from_u32(1)); + assert_eq!(new_cid_frame.retire_prior_to, VarInt::from_u32(0)); + assert_eq!( + new_cid_frame.id, + ConnectionId::from_slice(&[1, 2, 3, 4][..]) + ); + + assert_eq!(new_cid_frame.frame_type(), FrameType::NewConnectionId); + assert_eq!( + new_cid_frame.max_encoding_size(), + 1 + 8 + 8 + 21 + RESET_TOKEN_SIZE + ); + assert_eq!(new_cid_frame.encoding_size(), 1 + 1 + 1 + 1 + 4 + 16); + } + + #[test] + fn test_frame_parsing() { + let mut buf = BytesMut::new(); + let original_cid = ConnectionId::from_slice(&[1, 2, 3, 4][..]); + let original_frame = + NewConnectionIdFrame::new(original_cid, VarInt::from_u32(1), VarInt::from_u32(0)); + + // Write frame to buffer + buf.put_frame(&original_frame); + + // Skip frame type byte + let (_, parsed_frame) = be_new_connection_id_frame(&buf[1..]).unwrap(); + + assert_eq!(parsed_frame.sequence, original_frame.sequence); + assert_eq!(parsed_frame.retire_prior_to, original_frame.retire_prior_to); + assert_eq!(parsed_frame.id, original_frame.id); + assert_eq!(parsed_frame.reset_token, original_frame.reset_token); + } + + #[test] + fn test_invalid_retire_prior_to() { + let mut buf = BytesMut::new(); + buf.put_u8(NEW_CONNECTION_ID_FRAME_TYPE); + buf.put_varint(&VarInt::from_u32(1)); // sequence + buf.put_varint(&VarInt::from_u32(2)); // retire_prior_to > sequence + + assert!(be_new_connection_id_frame(&buf[1..]).is_err()); + } + + #[test] + fn test_zero_length_connection_id() { + let mut buf = BytesMut::new(); + buf.put_u8(NEW_CONNECTION_ID_FRAME_TYPE); + buf.put_varint(&VarInt::from_u32(1)); + buf.put_varint(&VarInt::from_u32(0)); + buf.put_u8(0); // zero length CID + + assert!(be_new_connection_id_frame(&buf[1..]).is_err()); + } +} diff --git a/qbase/src/frame/new_token.rs b/qbase/src/frame/new_token.rs index 874d887f..ee6365fa 100644 --- a/qbase/src/frame/new_token.rs +++ b/qbase/src/frame/new_token.rs @@ -59,7 +59,17 @@ impl super::io::WriteFrame for T { } #[cfg(test)] mod tests { - use crate::frame::io::WriteFrame; + use crate::frame::{io::WriteFrame, BeFrame, FrameType}; + + #[test] + fn test_new_token_frame() { + let frame = super::NewTokenFrame { + token: vec![0x01, 0x02], + }; + assert_eq!(frame.frame_type(), FrameType::NewToken); + assert_eq!(frame.max_encoding_size(), 1 + 1 + 2); + assert_eq!(frame.encoding_size(), 1 + 1 + 2); + } #[test] fn test_read_new_token_frame() { diff --git a/qbase/src/frame/padding.rs b/qbase/src/frame/padding.rs index d45ef904..80883995 100644 --- a/qbase/src/frame/padding.rs +++ b/qbase/src/frame/padding.rs @@ -35,7 +35,14 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { use super::{PaddingFrame, PADDING_FRAME_TYPE}; - use crate::frame::io::WriteFrame; + use crate::frame::{io::WriteFrame, BeFrame, FrameType}; + + #[test] + fn test_padding_frame() { + assert_eq!(PaddingFrame.frame_type(), FrameType::Padding); + assert_eq!(PaddingFrame.max_encoding_size(), 1); + assert_eq!(PaddingFrame.encoding_size(), 1); + } #[test] fn test_read_padding_frame() { diff --git a/qbase/src/frame/path_challenge.rs b/qbase/src/frame/path_challenge.rs index 84c8ec0d..6aabfe0e 100644 --- a/qbase/src/frame/path_challenge.rs +++ b/qbase/src/frame/path_challenge.rs @@ -66,6 +66,17 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { + #[test] + fn test_path_challenge_frame() { + use crate::frame::{BeFrame, FrameType}; + let frame = super::PathChallengeFrame { + data: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], + }; + assert_eq!(frame.frame_type(), FrameType::PathChallenge); + assert_eq!(frame.max_encoding_size(), 1 + 8); + assert_eq!(frame.encoding_size(), 1 + 8); + } + #[test] fn test_read_path_challenge_frame() { use nom::combinator::flat_map; diff --git a/qbase/src/frame/path_response.rs b/qbase/src/frame/path_response.rs index 532d2bad..de5441aa 100644 --- a/qbase/src/frame/path_response.rs +++ b/qbase/src/frame/path_response.rs @@ -66,6 +66,17 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { + #[test] + fn test_path_response_frame() { + use crate::frame::{BeFrame, FrameType}; + let frame = super::PathResponseFrame { + data: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], + }; + assert_eq!(frame.frame_type(), FrameType::PathResponse); + assert_eq!(frame.max_encoding_size(), 1 + 8); + assert_eq!(frame.encoding_size(), 1 + 8); + } + #[test] fn test_read_path_response_frame() { use nom::combinator::flat_map; diff --git a/qbase/src/frame/ping.rs b/qbase/src/frame/ping.rs index c19da949..efee7082 100644 --- a/qbase/src/frame/ping.rs +++ b/qbase/src/frame/ping.rs @@ -34,7 +34,14 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { use super::{PingFrame, PING_FRAME_TYPE}; - use crate::frame::io::WriteFrame; + use crate::frame::{io::WriteFrame, BeFrame, FrameType}; + + #[test] + fn test_ping_frame() { + assert_eq!(PingFrame.frame_type(), FrameType::Ping); + assert_eq!(PingFrame.max_encoding_size(), 1); + assert_eq!(PingFrame.encoding_size(), 1); + } #[test] fn test_read_ping_frame() { diff --git a/qbase/src/frame/reset_stream.rs b/qbase/src/frame/reset_stream.rs index 3c08466d..4a844548 100644 --- a/qbase/src/frame/reset_stream.rs +++ b/qbase/src/frame/reset_stream.rs @@ -66,7 +66,7 @@ impl super::io::WriteFrame for T { } } -#[derive(Clone, Copy, Debug, Error)] +#[derive(Clone, Copy, Debug, Error, PartialEq, Eq)] #[error("the stream was reset with app error code: {app_error_code}, final size: {final_size}")] pub struct ResetStreamError { app_error_code: VarInt, @@ -107,12 +107,30 @@ impl From<&ResetStreamFrame> for ResetStreamError { mod tests { use nom::combinator::flat_map; - use super::{ResetStreamFrame, RESET_STREAM_FRAME_TYPE}; + use super::{ResetStreamError, ResetStreamFrame, RESET_STREAM_FRAME_TYPE}; use crate::{ - frame::io::WriteFrame, + frame::{io::WriteFrame, BeFrame, FrameType}, varint::{be_varint, VarInt}, }; + #[test] + fn test_reset_stream_frame() { + let frame = ResetStreamFrame { + stream_id: VarInt::from_u32(0x1234).into(), + app_error_code: VarInt::from_u32(0x5678), + final_size: VarInt::from_u32(0x9abc), + }; + assert_eq!(frame.frame_type(), FrameType::ResetStream); + assert_eq!(frame.max_encoding_size(), 1 + 8 + 8 + 8); + assert_eq!(frame.encoding_size(), 1 + 2 + 4 + 4); + + let reset_stream_error: ResetStreamError = (&frame).into(); + assert_eq!( + reset_stream_error, + ResetStreamError::new(VarInt::from_u32(0x5678), VarInt::from_u32(0x9abc)) + ); + } + #[test] fn test_read_reset_stream_frame() { let buf = vec![ diff --git a/qbase/src/frame/retire_connection_id.rs b/qbase/src/frame/retire_connection_id.rs index b5a9f607..a06316f7 100644 --- a/qbase/src/frame/retire_connection_id.rs +++ b/qbase/src/frame/retire_connection_id.rs @@ -49,7 +49,20 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { use super::{be_retire_connection_id_frame, RetireConnectionIdFrame}; - use crate::{frame::io::WriteFrame, varint::VarInt}; + use crate::{ + frame::{io::WriteFrame, BeFrame, FrameType}, + varint::VarInt, + }; + + #[test] + fn test_retire_connection_id_frame() { + let frame = RetireConnectionIdFrame { + sequence: VarInt::from_u32(0x1234), + }; + assert_eq!(frame.frame_type(), FrameType::RetireConnectionId); + assert_eq!(frame.max_encoding_size(), 1 + 8); + assert_eq!(frame.encoding_size(), 1 + 2); + } #[test] fn test_read_retire_connection_id_frame() { diff --git a/qbase/src/frame/stop_sending.rs b/qbase/src/frame/stop_sending.rs index 22eab241..362fd6fe 100644 --- a/qbase/src/frame/stop_sending.rs +++ b/qbase/src/frame/stop_sending.rs @@ -70,7 +70,21 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { use super::{StopSendingFrame, STOP_SENDING_FRAME_TYPE}; - use crate::{frame::io::WriteFrame, varint::VarInt}; + use crate::{ + frame::{io::WriteFrame, BeFrame, FrameType}, + varint::VarInt, + }; + + #[test] + fn test_stop_sending_frame() { + let frame = StopSendingFrame { + stream_id: VarInt::from_u32(0x1234).into(), + app_err_code: VarInt::from_u32(0x5678), + }; + assert_eq!(frame.frame_type(), FrameType::StopSending); + assert_eq!(frame.max_encoding_size(), 1 + 8 + 8); + assert_eq!(frame.encoding_size(), 1 + 2 + 4); + } #[test] fn test_parse_stop_sending_frame() { diff --git a/qbase/src/frame/stream.rs b/qbase/src/frame/stream.rs index bd568902..5eca78f8 100644 --- a/qbase/src/frame/stream.rs +++ b/qbase/src/frame/stream.rs @@ -65,7 +65,6 @@ impl BeFrame for StreamFrame { } else { 0 } - + self.length } } @@ -238,10 +237,23 @@ mod tests { use super::{stream_frame_with_flag, StreamFrame, STREAM_FRAME_TYPE}; use crate::{ - frame::io::WriteDataFrame, + frame::{io::WriteDataFrame, BeFrame, FrameType}, varint::{be_varint, VarInt}, }; + #[test] + fn test_stream_frame() { + let stream_frame = StreamFrame { + id: VarInt::from_u32(0x1234).into(), + offset: VarInt::from_u32(0x1234), + length: 11, + flag: 0b110, + }; + assert_eq!(stream_frame.frame_type(), FrameType::Stream(0b110)); + assert_eq!(stream_frame.max_encoding_size(), 1 + 8 + 8 + 8); + assert_eq!(stream_frame.encoding_size(), 1 + 2 + 2 + 1); + } + #[test] fn test_read_stream_frame() { let raw = Bytes::from_static(&[ diff --git a/qbase/src/frame/stream_data_blocked.rs b/qbase/src/frame/stream_data_blocked.rs index 764a93c4..fe3993c1 100644 --- a/qbase/src/frame/stream_data_blocked.rs +++ b/qbase/src/frame/stream_data_blocked.rs @@ -62,7 +62,21 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { use super::{StreamDataBlockedFrame, STREAM_DATA_BLOCKED_FRAME_TYPE}; - use crate::{frame::io::WriteFrame, varint::VarInt}; + use crate::{ + frame::{io::WriteFrame, BeFrame, FrameType}, + varint::VarInt, + }; + + #[test] + fn test_stream_data_blocked_frame() { + let frame = StreamDataBlockedFrame { + stream_id: VarInt::from_u32(0x1234).into(), + maximum_stream_data: VarInt::from_u32(0x5678), + }; + assert_eq!(frame.frame_type(), FrameType::StreamDataBlocked); + assert_eq!(frame.max_encoding_size(), 1 + 8 + 8); + assert_eq!(frame.encoding_size(), 1 + 2 + 4); + } #[test] fn test_read_stream_data_blocked() { diff --git a/qbase/src/frame/streams_blocked.rs b/qbase/src/frame/streams_blocked.rs index a17f72fd..f2657993 100644 --- a/qbase/src/frame/streams_blocked.rs +++ b/qbase/src/frame/streams_blocked.rs @@ -89,7 +89,23 @@ impl super::io::WriteFrame for T { #[cfg(test)] mod tests { use super::{StreamsBlockedFrame, STREAMS_BLOCKED_FRAME_TYPE}; - use crate::{frame::io::WriteFrame, varint::VarInt}; + use crate::{ + frame::{io::WriteFrame, BeFrame, FrameType}, + varint::VarInt, + }; + + #[test] + fn test_stream_data_blocked_frame() { + let frame = StreamsBlockedFrame::Bi(VarInt::from_u32(0x1234)); + assert_eq!(frame.frame_type(), FrameType::StreamsBlocked(0)); + assert_eq!(frame.max_encoding_size(), 1 + 8); + assert_eq!(frame.encoding_size(), 1 + 2); + + let frame = StreamsBlockedFrame::Uni(VarInt::from_u32(0x1234)); + assert_eq!(frame.frame_type(), FrameType::StreamsBlocked(1)); + assert_eq!(frame.max_encoding_size(), 1 + 8); + assert_eq!(frame.encoding_size(), 1 + 2); + } #[test] fn test_read_streams_blocked_frame() {