summaryrefslogtreecommitdiffstats
path: root/mqtt-tester
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2023-01-13 09:49:23 +0100
committerMatthias Beyer <mail@beyermatthias.de>2023-01-19 11:11:52 +0100
commit0eb580fd4cc74d839ad583ecdc2010a83704abc8 (patch)
tree6990da92602c9b050782ea648013505599f3a9da /mqtt-tester
parent459f96cc2315fc0cff4bde1b985cff263407bdc5 (diff)
Reimpl check as PacketInvariant: ConnectPacketProtocolName
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'mqtt-tester')
-rw-r--r--mqtt-tester/src/client_report.rs51
-rw-r--r--mqtt-tester/src/invariant/connect_packet_protocol_name.rs38
-rw-r--r--mqtt-tester/src/invariant/mod.rs1
3 files changed, 44 insertions, 46 deletions
diff --git a/mqtt-tester/src/client_report.rs b/mqtt-tester/src/client_report.rs
index 0bc1c85..c99c734 100644
--- a/mqtt-tester/src/client_report.rs
+++ b/mqtt-tester/src/client_report.rs
@@ -11,10 +11,9 @@ use futures::FutureExt;
use mqtt_format::v3::packet::{MConnect, MPacket};
-use mqtt_format::v3::strings::MString;
-
use crate::behaviour_test::BehaviourTest;
use crate::executable::ClientExecutable;
+use crate::invariant::connect_packet_protocol_name::ConnectPacketProtocolName;
use crate::invariant::no_username_means_no_password::NoUsernameMeansNoPassword;
use crate::packet_invariant::PacketInvariant;
use crate::report::{Report, ReportResult};
@@ -28,7 +27,6 @@ pub async fn create_client_report(
let executable = ClientExecutable::new(client_exe_path);
let reports = vec![
- check_connect_packet_protocol_name(&executable).boxed_local(),
check_connect_packet_reserved_flag_zero(&executable).boxed_local(),
check_connect_flag_username_set_username_present(&executable).boxed_local(),
check_connect_flag_password_set_password_present(&executable).boxed_local(),
@@ -47,7 +45,10 @@ pub async fn create_client_report(
Box::new(crate::behaviour::FirstPacketFromClientIsConnect),
];
- let invariants: Vec<Arc<dyn PacketInvariant>> = vec![Arc::new(NoUsernameMeansNoPassword)];
+ let invariants: Vec<Arc<dyn PacketInvariant>> = vec![
+ Arc::new(NoUsernameMeansNoPassword),
+ Arc::new(ConnectPacketProtocolName),
+ ];
let mut collected_reports = Vec::with_capacity(flows.len());
for flow in flows {
@@ -198,48 +199,6 @@ macro_rules! wait_for_output {
}};
}
-async fn check_connect_packet_protocol_name(
- executable: &ClientExecutable,
-) -> miette::Result<Report> {
- let (client, _input, mut output) = executable
- .call(&[])
- .map(crate::command::Command::new)?
- .spawn()?;
-
- output
- .wait_and_check(
- &(|bytes: &[u8]| -> bool {
- let packet =
- match nom::combinator::all_consuming(mqtt_format::v3::packet::mpacket)(bytes) {
- Ok((_, packet)) => packet,
- Err(_e) => return false,
- };
-
- match packet {
- MPacket::Connect(connect) => connect.protocol_name == MString { value: "MQTT" },
- _ => false,
- }
- }),
- )
- .await?;
-
- let output = client.wait_with_output();
- let (result, output) = wait_for_output! {
- output,
- timeout_ms: 100,
- out_success => { ReportResult::Success },
- out_failure => { ReportResult::Inconclusive }
- };
-
- Ok(mk_report! {
- name: "Protocol name should be 'MQTT'",
- desc: "If the protocol name is incorrect the Server MAY disconnect the Client, or it MAY continue processing the CONNECT packet in accordance with some other specification. In the latter case, the Server MUST NOT continue to process the CONNECT packet in line with this specification",
- normative: "[MQTT-3.1.2-1]",
- result,
- output
- })
-}
-
async fn check_connect_packet_reserved_flag_zero(
executable: &ClientExecutable,
) -> miette::Result<Report> {
diff --git a/mqtt-tester/src/invariant/connect_packet_protocol_name.rs b/mqtt-tester/src/invariant/connect_packet_protocol_name.rs
new file mode 100644
index 0000000..c9848ec
--- /dev/null
+++ b/mqtt-tester/src/invariant/connect_packet_protocol_name.rs
@@ -0,0 +1,38 @@
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+//
+
+use mqtt_format::v3::{
+ packet::{MConnect, MPacket},
+ strings::MString,
+};
+
+use crate::{
+ packet_invariant::PacketInvariant,
+ report::{Report, ReportResult},
+};
+
+pub struct ConnectPacketProtocolName;
+
+impl PacketInvariant for ConnectPacketProtocolName {
+ fn test_invariant(&self, packet: &MPacket<'_>) -> Option<miette::Result<Report>> {
+ let result = if let MPacket::Connect(MConnect { protocol_name, .. }) = packet {
+ if *protocol_name == (MString { value: "MQTT" }) {
+ ReportResult::Success
+ } else {
+ ReportResult::Inconclusive
+ }
+ } else {
+ return None;
+ };
+
+ Some(Ok(crate::mk_report! {
+ name: "The CONNECT packet must have protocol_name = 'MQTT'",
+ desc: "The Protocol Name is a UTF-8 encoded string that represents the protocol name “MQTT”, capitalized as shown",
+ normative: "[MQTT-3.1.2-1]",
+ result
+ }))
+ }
+}
diff --git a/mqtt-tester/src/invariant/mod.rs b/mqtt-tester/src/invariant/mod.rs
index 612d6d6..0e004ec 100644
--- a/mqtt-tester/src/invariant/mod.rs
+++ b/mqtt-tester/src/invariant/mod.rs
@@ -4,4 +4,5 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
+pub mod connect_packet_protocol_name;
pub mod no_username_means_no_password;