Skip to content

Commit 8d5cb02

Browse files
more test cases and fix in pub rel, comp and rec
1 parent 25a59de commit 8d5cb02

File tree

8 files changed

+156
-50
lines changed

8 files changed

+156
-50
lines changed

fuzz/fuzz_targets/fuzz_target_1.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ async fn test(mut data: &[u8]) {
77
let _ = mqrstt::packets::Packet::async_read(&mut data).await;
88
}
99

10+
#[cfg(target_os = "linux")]
1011
fuzz_target!(|data: &[u8]| {
1112
test(data);
1213
});

mqrstt/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ tokio = { version = "1", features = [
5252
smol = { version = "2", optional = true }
5353

5454
[dev-dependencies]
55+
pretty_assertions = "1.4.1"
56+
5557
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
5658

5759
smol = { version = "2.0.0" }

mqrstt/src/packets/macros/properties_macros.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -399,17 +399,17 @@ macro_rules! properties_read_match_branch_body {
399399
$properties.authentication_data = Some(prop_body);
400400
}};
401401
($stream:ident, $properties:ident, $read_property_bytes:ident, PropertyType::RequestResponseInformation) => {{
402-
if $properties.authentication_data.is_some() {
402+
if $properties.request_response_information.is_some() {
403403
return Err($crate::packets::error::ReadError::DeserializeError(DeserializeError::DuplicateProperty(
404404
PropertyType::RequestResponseInformation,
405405
)));
406406
}
407407
let (prop_body, read_bytes) = u8::async_read($stream).await?;
408408
$read_property_bytes += read_bytes;
409-
$properties.request_problem_information = Some(prop_body);
409+
$properties.request_response_information = Some(prop_body);
410410
}};
411411
($stream:ident, $properties:ident, $read_property_bytes:ident, PropertyType::RequestProblemInformation) => {{
412-
if $properties.authentication_data.is_some() {
412+
if $properties.request_problem_information.is_some() {
413413
return Err($crate::packets::error::ReadError::DeserializeError(DeserializeError::DuplicateProperty(
414414
PropertyType::RequestProblemInformation,
415415
)));

mqrstt/src/packets/mod.rs

+54-42
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ mod tests {
453453
use crate::tests::test_packets::*;
454454

455455
#[rstest::rstest]
456+
#[case::connect_case(connect_case())]
456457
#[case::ping_req_case(ping_req_case().1)]
457458
#[case::ping_resp_case(ping_resp_case().1)]
458459
#[case::connack_case(connack_case().1)]
@@ -472,6 +473,10 @@ mod tests {
472473
#[case::suback(suback_case())]
473474
#[case::unsubscribe(unsubscribe_case())]
474475
#[case::unsuback(unsuback_case())]
476+
#[case::pubcomp_case(pubcomp_case())]
477+
#[case::pubrec_case(pubrec_case())]
478+
#[case::pubrec_case(pubrel_case2())]
479+
#[case::auth_case(auth_case())]
475480
fn test_write_read_write_read_cases(#[case] packet: Packet) {
476481
use crate::packets::WireLength;
477482

@@ -481,9 +486,11 @@ mod tests {
481486

482487
let wire_len = packet.wire_len();
483488
assert_eq!(wire_len, buffer.len());
484-
dbg!(wire_len);
485-
let a: Vec<_> = buffer.iter().map(|f| *f as u16).collect();
486-
println!("{:?}", a);
489+
490+
// dbg!(wire_len);
491+
492+
// let a: Vec<_> = buffer.iter().map(|f| *f as u16).collect();
493+
// println!("{:?}", a);
487494

488495
let res1 = Packet::read(&mut buffer).unwrap();
489496

@@ -496,6 +503,50 @@ mod tests {
496503
assert_eq!(res1, res2);
497504
}
498505

506+
#[rstest::rstest]
507+
#[case::connect_case(connect_case())]
508+
#[case::ping_req_case(ping_req_case().1)]
509+
#[case::ping_resp_case(ping_resp_case().1)]
510+
#[case::connack_case(connack_case().1)]
511+
#[case::create_subscribe_packet(create_subscribe_packet(1))]
512+
#[case::create_subscribe_packet(create_subscribe_packet(65335))]
513+
#[case::create_puback_packet(create_puback_packet(1))]
514+
#[case::create_puback_packet(create_puback_packet(65335))]
515+
#[case::create_disconnect_packet(create_disconnect_packet())]
516+
#[case::create_connack_packet(create_connack_packet(true))]
517+
#[case::create_connack_packet(create_connack_packet(false))]
518+
#[case::publish_packet_1(publish_packet_1())]
519+
#[case::publish_packet_2(publish_packet_2())]
520+
#[case::publish_packet_3(publish_packet_3())]
521+
#[case::publish_packet_4(publish_packet_4())]
522+
#[case::create_empty_publish_packet(create_empty_publish_packet())]
523+
#[case::subscribe(subscribe_case())]
524+
#[case::suback(suback_case())]
525+
#[case::unsubscribe(unsubscribe_case())]
526+
#[case::unsuback(unsuback_case())]
527+
#[case::pubcomp_case(pubcomp_case())]
528+
#[case::pubrec_case(pubrec_case())]
529+
#[case::pubrec_case(pubrel_case2())]
530+
#[case::auth_case(auth_case())]
531+
#[tokio::test]
532+
async fn test_async_write_read_write_read_cases(#[case] packet: Packet) {
533+
use crate::packets::WireLength;
534+
535+
let mut buffer = Vec::with_capacity(1000);
536+
let res = packet.async_write(&mut buffer).await.unwrap();
537+
538+
let wire_len = packet.wire_len();
539+
540+
assert_eq!(res, buffer.len());
541+
assert_eq!(wire_len, buffer.len());
542+
543+
let mut buf = buffer.as_slice();
544+
545+
let res1 = Packet::async_read(&mut buf).await.unwrap();
546+
547+
pretty_assertions::assert_eq!(packet, res1);
548+
}
549+
499550
#[rstest::rstest]
500551
#[case::disconnect(disconnect_case())]
501552
#[case::ping_req(ping_req_case())]
@@ -548,45 +599,6 @@ mod tests {
548599
assert_eq!(out, input)
549600
}
550601

551-
#[rstest::rstest]
552-
#[case::ping_req_case(ping_req_case().1)]
553-
#[case::ping_resp_case(ping_resp_case().1)]
554-
#[case::connack_case(connack_case().1)]
555-
#[case::create_subscribe_packet(create_subscribe_packet(1))]
556-
#[case::create_subscribe_packet(create_subscribe_packet(65335))]
557-
#[case::create_puback_packet(create_puback_packet(1))]
558-
#[case::create_puback_packet(create_puback_packet(65335))]
559-
#[case::create_disconnect_packet(create_disconnect_packet())]
560-
#[case::create_connack_packet(create_connack_packet(true))]
561-
#[case::create_connack_packet(create_connack_packet(false))]
562-
#[case::publish_packet_1(publish_packet_1())]
563-
#[case::publish_packet_2(publish_packet_2())]
564-
#[case::publish_packet_3(publish_packet_3())]
565-
#[case::publish_packet_4(publish_packet_4())]
566-
#[case::create_empty_publish_packet(create_empty_publish_packet())]
567-
#[case::subscribe(subscribe_case())]
568-
#[case::suback(suback_case())]
569-
#[case::unsubscribe(unsubscribe_case())]
570-
#[case::unsuback(unsuback_case())]
571-
#[tokio::test]
572-
async fn test_async_write_read_write_read_cases(#[case] packet: Packet) {
573-
use crate::packets::WireLength;
574-
575-
let mut buffer = Vec::with_capacity(1000);
576-
let res = packet.async_write(&mut buffer).await.unwrap();
577-
578-
let wire_len = packet.wire_len();
579-
580-
assert_eq!(res, buffer.len());
581-
assert_eq!(wire_len, buffer.len());
582-
583-
let mut buf = buffer.as_slice();
584-
585-
let res1 = Packet::async_read(&mut buf).await.unwrap();
586-
587-
assert_eq!(packet, res1);
588-
}
589-
590602
// #[rstest::rstest]
591603
// #[case(&[59, 1, 0, 59])]
592604
// #[case(&[16, 14, 0, 4, 77, 81, 84, 84, 5, 247, 247, 252, 1, 17, 247, 247, 247])]

mqrstt/src/packets/pubcomp/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub use properties::PubCompProperties;
77
use super::{
88
error::{DeserializeError, ReadError},
99
mqtt_trait::{MqttAsyncRead, MqttRead, MqttWrite, PacketAsyncRead, PacketRead, PacketWrite, WireLength},
10+
VariableInteger,
1011
};
1112
use bytes::BufMut;
1213
use tokio::io::AsyncReadExt;
@@ -148,7 +149,8 @@ impl WireLength for PubComp {
148149
} else if self.properties.reason_string.is_none() && self.properties.user_properties.is_empty() {
149150
3
150151
} else {
151-
2 + 1 + self.properties.wire_len()
152+
let prop_wire_len = self.properties.wire_len();
153+
2 + 1 + prop_wire_len.variable_integer_len() + prop_wire_len
152154
}
153155
}
154156
}

mqrstt/src/packets/pubrec/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tokio::io::AsyncReadExt;
1111
use super::{
1212
error::DeserializeError,
1313
mqtt_trait::{MqttAsyncRead, MqttRead, MqttWrite, PacketRead, PacketWrite, WireLength},
14-
PacketAsyncRead,
14+
PacketAsyncRead, VariableInteger,
1515
};
1616

1717
/// The [`PubRec`] (Publish Received) packet is part of the acknowledgment flow for a [`crate::packets::Publish`] with QoS 2.
@@ -141,7 +141,8 @@ impl WireLength for PubRec {
141141
} else if self.properties.reason_string.is_none() && self.properties.user_properties.is_empty() {
142142
3
143143
} else {
144-
2 + 1 + self.properties.wire_len()
144+
let prop_wire_len = self.properties.wire_len();
145+
2 + 1 + prop_wire_len.variable_integer_len() + prop_wire_len
145146
}
146147
}
147148
}

mqrstt/src/packets/pubrel/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use tokio::io::AsyncReadExt;
1010
use super::{
1111
error::{DeserializeError, ReadError},
1212
mqtt_trait::{MqttAsyncRead, MqttRead, MqttWrite, PacketAsyncRead, PacketRead, PacketWrite, WireLength},
13+
VariableInteger,
1314
};
1415

1516
/// The [`PubRel`] (Publish Release) packet acknowledges the reception of a [`crate::packets::PubRec`] Packet.
@@ -139,7 +140,8 @@ impl WireLength for PubRel {
139140
} else if self.properties.reason_string.is_none() && self.properties.user_properties.is_empty() {
140141
3
141142
} else {
142-
2 + 1 + self.properties.wire_len()
143+
let prop_wire_len = self.properties.wire_len();
144+
2 + 1 + prop_wire_len.variable_integer_len() + prop_wire_len
143145
}
144146
}
145147
}

mqrstt/src/tests/test_packets.rs

+87-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,31 @@ pub fn pubrel_smallest_case() -> (&'static [u8], Packet) {
119119
(packet, Packet::PubRel(expected))
120120
}
121121

122+
pub fn connect_case() -> Packet {
123+
let connect = Connect {
124+
protocol_version: ProtocolVersion::V5,
125+
clean_start: true,
126+
last_will: Some(LastWill::new(QoS::ExactlyOnce, true, "will/topic", b"will payload".to_vec())),
127+
username: Some("ThisIsTheUsername".into()),
128+
password: Some("ThisIsThePassword".into()),
129+
keep_alive: 60,
130+
connect_properties: ConnectProperties {
131+
session_expiry_interval: Some(5),
132+
receive_maximum: Some(10),
133+
maximum_packet_size: Some(100),
134+
topic_alias_maximum: Some(10),
135+
user_properties: vec![("test".into(), "test".into()), ("test2".into(), "test2".into())],
136+
authentication_method: Some("AuthenticationMethod".into()),
137+
authentication_data: Some(b"AuthenticationData".to_vec()),
138+
request_response_information: Some(0),
139+
request_problem_information: Some(1),
140+
},
141+
client_id: "ThisIsTheClientID".into(),
142+
};
143+
144+
Packet::Connect(connect)
145+
}
146+
122147
pub fn publish_packet_1() -> Packet {
123148
Packet::Publish(Publish {
124149
dup: false,
@@ -324,12 +349,73 @@ pub fn unsubscribe_case() -> Packet {
324349
let expected = Unsubscribe {
325350
packet_identifier: 3,
326351
topics: vec!["test/topic".into()],
327-
properties: UnsubscribeProperties { user_properties: vec![("written += 1;".into(), "value".into())] },
352+
properties: UnsubscribeProperties {
353+
user_properties: vec![("written += 1;".into(), "value".into())],
354+
},
328355
};
329356

330357
Packet::Unsubscribe(expected)
331358
}
332359

360+
pub fn pubrec_case() -> Packet {
361+
let expected = PubRec {
362+
packet_identifier: 3,
363+
reason_code: PubRecReasonCode::Success,
364+
properties: PubRecProperties {
365+
reason_string: Some("test".into()),
366+
user_properties: vec![("test5asdf".into(), "test3".into()), ("test4".into(), "test2".into())],
367+
},
368+
};
369+
370+
Packet::PubRec(expected)
371+
}
372+
373+
pub fn pubcomp_case() -> Packet {
374+
let expected = PubComp {
375+
packet_identifier: 3,
376+
reason_code: PubCompReasonCode::PacketIdentifierNotFound,
377+
properties: PubCompProperties {
378+
reason_string: Some("test".into()),
379+
user_properties: vec![
380+
("test5asdf".into(), "test3".into()),
381+
("test⌚5asdf".into(), "test3".into()),
382+
("test5asdf".into(), "test3".into()),
383+
("test5asdf".into(), "test3".into()),
384+
("test4".into(), "test2".into()),
385+
],
386+
},
387+
};
388+
389+
Packet::PubComp(expected)
390+
}
391+
392+
pub fn pubrel_case2() -> Packet {
393+
let expected = PubRel {
394+
packet_identifier: 3,
395+
reason_code: PubRelReasonCode::Success,
396+
properties: PubRelProperties {
397+
reason_string: Some("test".into()),
398+
user_properties: vec![("test5asdf".into(), "test3".repeat(10000).into()), ("test4".into(), "test2".into())],
399+
},
400+
};
401+
402+
Packet::PubRel(expected)
403+
}
404+
405+
pub fn auth_case() -> Packet {
406+
let expected = Auth {
407+
reason_code: AuthReasonCode::ContinueAuthentication,
408+
properties: AuthProperties {
409+
authentication_method: Some("SomeRandomDataHere".into()),
410+
authentication_data: Some(b"VeryRandomStuff".to_vec()),
411+
reason_string: Some("⌚this_is_for_sure_a_test_⌚".into()),
412+
user_properties: vec![("SureHopeThisWorks".into(), "😰".into())],
413+
},
414+
};
415+
416+
Packet::Auth(expected)
417+
}
418+
333419
#[rstest]
334420
#[case(create_subscribe_packet(1))]
335421
#[case(create_subscribe_packet(65335))]

0 commit comments

Comments
 (0)