Skip to content

Commit 54f7223

Browse files
test: improve test coverage for frames
1 parent d0b07ff commit 54f7223

23 files changed

+436
-30
lines changed

h3-shim/src/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ async fn h3_test() {
2222

2323
let client_opt = client_example::Opt {
2424
ca: PathBuf::from("examples/ca.cert"),
25-
key_log_file: false,
26-
bind: vec!["[::]:0".parse().unwrap()],
25+
key_log_file: true,
26+
bind: vec!["127.0.0.1:0".parse().unwrap(), "[::1]:0".parse().unwrap()],
2727
uri: "https://localhost:4433/Cargo.toml".to_string(),
2828
};
2929

@@ -40,7 +40,7 @@ async fn h3_test() {
4040
};
4141

4242
let client = async move {
43-
client_example::run(client_opt)
43+
_ = client_example::run(client_opt)
4444
.await
4545
.expect("client failed");
4646
};
@@ -54,5 +54,5 @@ async fn h3_test() {
5454
Err(_finish) => { /* ok */ }
5555
}
5656
};
57-
tokio::join!(client, server);
57+
tokio::join!(server, client);
5858
}

qbase/src/frame/ack.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl super::BeFrame for AckFrame {
4545
}
4646

4747
fn max_encoding_size(&self) -> usize {
48-
1 + 8 + 8 + 8 + self.ranges.len() * 16 + if self.ecn.is_some() { 24 } else { 0 }
48+
1 + 8 + 8 + 8 + 8 + self.ranges.len() * 16 + if self.ecn.is_some() { 24 } else { 0 }
4949
}
5050

5151
fn encoding_size(&self) -> usize {
@@ -191,10 +191,35 @@ mod tests {
191191

192192
use super::{ack_frame_with_flag, be_ecn_counts, AckFrame, EcnCounts, ACK_FRAME_TYPE};
193193
use crate::{
194-
frame::io::WriteFrame,
194+
frame::{io::WriteFrame, BeFrame, FrameType},
195195
varint::{be_varint, VarInt},
196196
};
197197

198+
#[test]
199+
fn test_ack_frame() {
200+
// test frame type, encoding size, and max encoding size
201+
let mut frame = AckFrame {
202+
largest: VarInt::from_u32(0x1234),
203+
delay: VarInt::from_u32(0x1234),
204+
first_range: VarInt::from_u32(0x1234),
205+
ranges: vec![(VarInt::from_u32(3), VarInt::from_u32(20))],
206+
ecn: None,
207+
};
208+
assert_eq!(frame.frame_type(), FrameType::Ack(0));
209+
assert_eq!(frame.encoding_size(), 1 + 2 * 3 + 1 + 2);
210+
assert_eq!(frame.max_encoding_size(), 1 + 4 * 8 + 2 * 8);
211+
212+
// test set_ecn and take_ecn
213+
let ecn = EcnCounts {
214+
ect0: VarInt::from_u32(0x1234),
215+
ect1: VarInt::from_u32(0x1234),
216+
ce: VarInt::from_u32(0x1234),
217+
};
218+
frame.set_ecn(ecn);
219+
assert!(frame.ecn.is_some());
220+
assert_eq!(frame.take_ecn(), Some(ecn));
221+
}
222+
198223
#[test]
199224
fn test_read_ecn_count() {
200225
let input = vec![0x52, 0x34, 0x52, 0x34, 0x52, 0x34];

qbase/src/frame/connection_close.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,25 @@ impl<T: bytes::BufMut> super::io::WriteFrame<ConnectionCloseFrame> for T {
125125

126126
#[cfg(test)]
127127
mod tests {
128-
use crate::{error::ErrorKind, frame::io::WriteFrame};
128+
use crate::{
129+
error::ErrorKind,
130+
frame::{io::WriteFrame, BeFrame, FrameType},
131+
};
132+
133+
#[test]
134+
fn test_connection_close_frame() {
135+
let frame = super::ConnectionCloseFrame {
136+
error_kind: ErrorKind::Application,
137+
frame_type: None,
138+
reason: "wrong".into(),
139+
};
140+
assert_eq!(
141+
frame.frame_type(),
142+
FrameType::ConnectionClose(super::APP_LAYER)
143+
);
144+
assert_eq!(frame.max_encoding_size(), 1 + 8 + 2 + 5);
145+
assert_eq!(frame.encoding_size(), 1 + 1 + 1 + 5);
146+
}
129147

130148
#[test]
131149
fn test_read_connection_close_frame() {

qbase/src/frame/crypto.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ impl super::BeFrame for CryptoFrame {
3838
}
3939

4040
fn encoding_size(&self) -> usize {
41-
1 + self.offset.encoding_size()
42-
+ self.length.encoding_size()
43-
+ self.length.into_inner() as usize
41+
1 + self.offset.encoding_size() + self.length.encoding_size()
4442
}
4543
}
4644

@@ -113,7 +111,22 @@ where
113111
#[cfg(test)]
114112
mod tests {
115113
use super::{CryptoFrame, CRYPTO_FRAME_TYPE};
116-
use crate::{frame::io::WriteDataFrame, varint::VarInt};
114+
use crate::{
115+
frame::{io::WriteDataFrame, BeFrame},
116+
varint::VarInt,
117+
};
118+
119+
#[test]
120+
fn test_crypto_frame() {
121+
let frame = CryptoFrame {
122+
offset: VarInt::from_u32(0),
123+
length: VarInt::from_u32(500),
124+
};
125+
assert_eq!(frame.frame_type(), super::super::FrameType::Crypto);
126+
assert_eq!(frame.max_encoding_size(), 1 + 8 + 8);
127+
assert_eq!(frame.encoding_size(), 1 + 1 + 2);
128+
assert_eq!(frame.range(), 0..500);
129+
}
117130

118131
#[test]
119132
fn test_read_crypto_frame() {

qbase/src/frame/data_blocked.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,17 @@ impl<T: bytes::BufMut> super::io::WriteFrame<DataBlockedFrame> for T {
4949
#[cfg(test)]
5050
mod tests {
5151
use super::{DataBlockedFrame, DATA_BLOCKED_FRAME_TYPE};
52-
use crate::frame::io::WriteFrame;
52+
use crate::frame::{io::WriteFrame, BeFrame};
53+
54+
#[test]
55+
fn test_data_blocked_frame() {
56+
let frame = DataBlockedFrame {
57+
limit: crate::varint::VarInt::from_u32(0x1234),
58+
};
59+
assert_eq!(frame.frame_type(), super::super::FrameType::Crypto);
60+
assert_eq!(frame.max_encoding_size(), 1 + 8);
61+
assert_eq!(frame.encoding_size(), 1 + 2);
62+
}
5363

5464
#[test]
5565
fn test_read_data_blocked_frame() {

qbase/src/frame/datagram.rs

+10
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ mod tests {
7777
use super::*;
7878
use crate::frame::io::WriteDataFrame;
7979

80+
#[test]
81+
fn test_datagram_frame() {
82+
let frame = DatagramFrame {
83+
length: Some(VarInt::from_u32(3)),
84+
};
85+
assert_eq!(frame.frame_type(), FrameType::Datagram(1));
86+
assert_eq!(frame.max_encoding_size(), 1 + 8);
87+
assert_eq!(frame.encoding_size(), 1 + 1);
88+
}
89+
8090
#[test]
8191
fn test_datagram_frame_with_flag() {
8292
let input = [0x05, 0x00, 0x00, 0x00, 0x00, 0x00];

qbase/src/frame/error.rs

+80
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,83 @@ impl nom::error::ParseError<&[u8]> for Error {
7777
}
7878

7979
// TODO: conver DecodingError to quic error
80+
81+
#[cfg(test)]
82+
mod tests {
83+
use nom::error::ParseError;
84+
85+
use super::*;
86+
use crate::packet::r#type::{
87+
long::{Type::V1, Ver1},
88+
Type,
89+
};
90+
91+
#[test]
92+
fn test_error_conversion_to_transport_error() {
93+
let cases = vec![
94+
(Error::NoFrames, TransportErrorKind::ProtocolViolation),
95+
(
96+
Error::IncompleteType("test".to_string()),
97+
TransportErrorKind::FrameEncoding,
98+
),
99+
(
100+
Error::InvalidType(VarInt::from_u32(0x1f)),
101+
TransportErrorKind::FrameEncoding,
102+
),
103+
(
104+
Error::WrongType(FrameType::Ping, Type::Long(V1(Ver1::INITIAL))),
105+
TransportErrorKind::FrameEncoding,
106+
),
107+
(
108+
Error::IncompleteFrame(FrameType::Ping, "incomplete".to_string()),
109+
TransportErrorKind::FrameEncoding,
110+
),
111+
(
112+
Error::ParseError(FrameType::Ping, "parse error".to_string()),
113+
TransportErrorKind::FrameEncoding,
114+
),
115+
];
116+
117+
for (error, expected_kind) in cases {
118+
let transport_error: TransportError = error.into();
119+
assert_eq!(transport_error.kind(), expected_kind);
120+
}
121+
}
122+
123+
#[test]
124+
fn test_nom_error_conversion() {
125+
let error = Error::NoFrames;
126+
let nom_error = nom::Err::Error(error.clone());
127+
let converted: Error = nom_error.into();
128+
assert_eq!(converted, error);
129+
130+
let nom_failure = nom::Err::Failure(error.clone());
131+
let converted: Error = nom_failure.into();
132+
assert_eq!(converted, error);
133+
}
134+
135+
#[test]
136+
fn test_parse_error_impl() {
137+
let error = Error::ParseError(FrameType::Ping, "test error".to_string());
138+
let appended = Error::append(&[], NomErrorKind::ManyTill, error.clone());
139+
assert_eq!(appended, error);
140+
}
141+
142+
#[test]
143+
#[should_panic(expected = "QUIC frame parser must always consume")]
144+
fn test_parse_error_unreachable() {
145+
Error::from_error_kind(&[], NomErrorKind::ManyTill);
146+
}
147+
148+
#[test]
149+
fn test_error_display() {
150+
let error = Error::NoFrames;
151+
assert_eq!(error.to_string(), "A packet containing no frames");
152+
153+
let error = Error::IncompleteType("test".to_string());
154+
assert_eq!(error.to_string(), "Incomplete frame type: test");
155+
156+
let error = Error::InvalidType(VarInt::from_u32(0x1f));
157+
assert_eq!(error.to_string(), "Invalid frame type from 31");
158+
}
159+
}

qbase/src/frame/handshake_done.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ impl<T: bytes::BufMut> super::io::WriteFrame<HandshakeDoneFrame> for T {
3434

3535
#[cfg(test)]
3636
mod tests {
37-
use crate::frame::{io::WriteFrame, HandshakeDoneFrame};
37+
use crate::frame::{io::WriteFrame, BeFrame, FrameType, HandshakeDoneFrame};
38+
39+
#[test]
40+
fn test_handshake_done_frame() {
41+
assert_eq!(HandshakeDoneFrame.frame_type(), FrameType::HandshakeDone);
42+
assert_eq!(HandshakeDoneFrame.max_encoding_size(), 1);
43+
assert_eq!(HandshakeDoneFrame.encoding_size(), 1);
44+
}
3845

3946
#[test]
4047
fn test_read_handshake_done_frame() {

qbase/src/frame/max_data.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,20 @@ impl<T: bytes::BufMut> super::io::WriteFrame<MaxDataFrame> for T {
4949
#[cfg(test)]
5050
mod tests {
5151
use super::{MaxDataFrame, MAX_DATA_FRAME_TYPE};
52-
use crate::{frame::io::WriteFrame, varint::VarInt};
52+
use crate::{
53+
frame::{io::WriteFrame, BeFrame, FrameType},
54+
varint::VarInt,
55+
};
56+
57+
#[test]
58+
fn test_max_data_frame() {
59+
let frame = MaxDataFrame {
60+
max_data: VarInt::from_u32(0x1234),
61+
};
62+
assert_eq!(frame.frame_type(), FrameType::MaxData);
63+
assert_eq!(frame.max_encoding_size(), 1 + 8);
64+
assert_eq!(frame.encoding_size(), 1 + 2);
65+
}
5366

5467
#[test]
5568
fn test_read_max_data_frame() {

qbase/src/frame/max_stream_data.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,21 @@ impl<T: bytes::BufMut> super::io::WriteFrame<MaxStreamDataFrame> for T {
7171
#[cfg(test)]
7272
mod tests {
7373
use super::{MaxStreamDataFrame, MAX_STREAM_DATA_FRAME_TYPE};
74-
use crate::{frame::io::WriteFrame, varint::VarInt};
74+
use crate::{
75+
frame::{io::WriteFrame, BeFrame, FrameType},
76+
varint::VarInt,
77+
};
78+
79+
#[test]
80+
fn test_max_stream_data_frame() {
81+
let frame =
82+
MaxStreamDataFrame::new(VarInt::from_u32(0x1234).into(), VarInt::from_u32(0x5678));
83+
assert_eq!(frame.stream_id, VarInt::from_u32(0x1234).into());
84+
assert_eq!(frame.max_stream_data, VarInt::from_u32(0x5678));
85+
assert_eq!(frame.frame_type(), FrameType::MaxStreamData);
86+
assert_eq!(frame.max_encoding_size(), 1 + 8 + 8);
87+
assert_eq!(frame.encoding_size(), 1 + 2 + 4);
88+
}
7589

7690
#[test]
7791
fn test_read_max_stream_data_frame() {

qbase/src/frame/max_streams.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,23 @@ impl<T: bytes::BufMut> super::io::WriteFrame<MaxStreamsFrame> for T {
9797
mod tests {
9898
use super::{MaxStreamsFrame, MAX_STREAMS_FRAME_TYPE};
9999
use crate::{
100-
frame::io::WriteFrame,
100+
frame::{io::WriteFrame, BeFrame, FrameType},
101101
varint::{be_varint, VarInt},
102102
};
103103

104+
#[test]
105+
fn test_max_streams_frame() {
106+
let frame = MaxStreamsFrame::Bi(VarInt::from_u32(0x1234));
107+
assert_eq!(frame.frame_type(), FrameType::MaxStreams(0));
108+
assert_eq!(frame.max_encoding_size(), 1 + 8);
109+
assert_eq!(frame.encoding_size(), 1 + 2);
110+
111+
let frame = MaxStreamsFrame::Uni(VarInt::from_u32(0x1236));
112+
assert_eq!(frame.frame_type(), FrameType::MaxStreams(1));
113+
assert_eq!(frame.max_encoding_size(), 1 + 8);
114+
assert_eq!(frame.encoding_size(), 1 + 2);
115+
}
116+
104117
#[test]
105118
fn test_read_max_streams_frame() {
106119
use nom::combinator::flat_map;

0 commit comments

Comments
 (0)