From 659d70186a6577d0cae53e276dfeb38c3d09abaa Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 13 Jan 2023 08:55:04 +0100 Subject: Reimpl test as BehaviourTest: PublishQos2IsAcked Signed-off-by: Matthias Beyer --- mqtt-tester/src/behaviour/mod.rs | 2 + .../src/behaviour/publish_qos_2_is_acked.rs | 71 ++++++++++++++++++++++ mqtt-tester/src/client_report.rs | 54 +--------------- 3 files changed, 75 insertions(+), 52 deletions(-) create mode 100644 mqtt-tester/src/behaviour/publish_qos_2_is_acked.rs (limited to 'mqtt-tester') diff --git a/mqtt-tester/src/behaviour/mod.rs b/mqtt-tester/src/behaviour/mod.rs index 39a7d03..422bfd9 100644 --- a/mqtt-tester/src/behaviour/mod.rs +++ b/mqtt-tester/src/behaviour/mod.rs @@ -7,6 +7,7 @@ pub mod connack_flags_are_set_as_reserved; pub mod invalid_first_packet_is_rejected; pub mod invalid_utf8_is_rejected; +pub mod publish_qos_2_is_acked; pub mod publish_qos_zero_with_ident_fails; pub mod receiving_server_packet; pub mod utf8_with_nullchar_is_rejected; @@ -15,6 +16,7 @@ pub mod wait_for_connect; pub use self::connack_flags_are_set_as_reserved::ConnackFlagsAreSetAsReserved; pub use self::invalid_first_packet_is_rejected::InvalidFirstPacketIsRejected; pub use self::invalid_utf8_is_rejected::InvalidUtf8IsRejected; +pub use self::publish_qos_2_is_acked::PublishQos2IsAcked; pub use self::publish_qos_zero_with_ident_fails::PublishQosZeroWithIdentFails; pub use self::receiving_server_packet::ReceivingServerPacket; pub use self::utf8_with_nullchar_is_rejected::Utf8WithNullcharIsRejected; diff --git a/mqtt-tester/src/behaviour/publish_qos_2_is_acked.rs b/mqtt-tester/src/behaviour/publish_qos_2_is_acked.rs new file mode 100644 index 0000000..95dfeae --- /dev/null +++ b/mqtt-tester/src/behaviour/publish_qos_2_is_acked.rs @@ -0,0 +1,71 @@ +// +// 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::{ + connect_return::MConnectReturnCode, + identifier::MPacketIdentifier, + packet::{MConnack, MPublish}, + qos::MQualityOfService, + strings::MString, +}; + +use crate::{ + behaviour_test::BehaviourTest, + command::{Input, Output}, + executable::ClientExecutableCommand, + report::ReportResult, +}; + +pub struct PublishQos2IsAcked; + +#[async_trait::async_trait] +impl BehaviourTest for PublishQos2IsAcked { + fn commands(&self) -> Vec> { + vec![] + } + + async fn execute(&self, mut input: Input, _output: Output) -> Result<(), miette::Error> { + input + .send_packet(MConnack { + session_present: false, + connect_return_code: MConnectReturnCode::Accepted, + }) + .await?; + + input + .send_packet(MPublish { + dup: false, + qos: MQualityOfService::AtLeastOnce, // QoS 2 + retain: false, + topic_name: MString { value: "a" }, + id: Some(MPacketIdentifier(1)), + payload: &[0x00], + }) + .await?; + + Ok(()) + } + + fn report_name(&self) -> &str { + "A PUBLISH packet is replied to with Puback with the same id" + } + + fn report_desc(&self) -> &str { + "A PUBACK, PUBREC or PUBREL Packet MUST contain the same Packet Identifier as the PUBLISH Packet that was originally sent." + } + + fn report_normative(&self) -> &str { + "[MQTT-2.3.1-6]" + } + + fn translate_client_exit_code(&self, success: bool) -> ReportResult { + if success { + ReportResult::Failure + } else { + ReportResult::Success + } + } +} diff --git a/mqtt-tester/src/client_report.rs b/mqtt-tester/src/client_report.rs index d6de3da..e3b143e 100644 --- a/mqtt-tester/src/client_report.rs +++ b/mqtt-tester/src/client_report.rs @@ -9,12 +9,9 @@ use std::sync::Arc; use futures::FutureExt; use miette::IntoDiagnostic; -use mqtt_format::v3::connect_return::MConnectReturnCode; -use mqtt_format::v3::identifier::MPacketIdentifier; -use mqtt_format::v3::packet::{MConnack, MConnect, MPacket, MPuback, MPublish}; +use mqtt_format::v3::packet::{MConnect, MPacket}; -use mqtt_format::v3::qos::MQualityOfService; use mqtt_format::v3::strings::MString; use crate::behaviour_test::BehaviourTest; @@ -32,7 +29,6 @@ pub async fn create_client_report( let executable = ClientExecutable::new(client_exe_path); let reports = vec![ - check_publish_qos_2_is_acked(&executable).boxed_local(), check_first_packet_from_client_is_connect(&executable).boxed_local(), check_connect_packet_protocol_name(&executable).boxed_local(), check_connect_packet_reserved_flag_zero(&executable).boxed_local(), @@ -49,6 +45,7 @@ pub async fn create_client_report( Box::new(crate::behaviour::Utf8WithNullcharIsRejected), Box::new(crate::behaviour::ConnackFlagsAreSetAsReserved), Box::new(crate::behaviour::PublishQosZeroWithIdentFails), + Box::new(crate::behaviour::PublishQos2IsAcked), ]; let invariants: Vec> = vec![Arc::new(NoUsernameMeansNoPassword)]; @@ -155,53 +152,6 @@ macro_rules! wait_for_output { }}; } -async fn check_publish_qos_2_is_acked(executable: &ClientExecutable) -> miette::Result { - let (client, mut input, mut output) = executable - .call(&[]) - .map(crate::command::Command::new)? - .spawn()?; - - input - .send_packet(MConnack { - session_present: false, - connect_return_code: MConnectReturnCode::Accepted, - }) - .await?; - - input - .send_packet(MPublish { - dup: false, - qos: MQualityOfService::AtLeastOnce, // QoS 2 - retain: false, - topic_name: MString { value: "a" }, - id: Some(MPacketIdentifier(1)), - payload: &[0x00], - }) - .await?; - - output - .wait_for_packet(MPuback { - id: MPacketIdentifier(1), - }) - .await?; - - let output = client.wait_with_output(); - let (result, output) = wait_for_output! { - output, - timeout_ms: 100, - out_success => { ReportResult::Success }, - out_failure => { ReportResult::Failure } - }; - - Ok(mk_report! { - name: "A PUBLISH packet is replied to with Puback with the same id", - desc: "A PUBACK, PUBREC or PUBREL Packet MUST contain the same Packet Identifier as the PUBLISH Packet that was originally sent.", - normative: "[MQTT-2.3.1-6]", - result, - output - }) -} - async fn check_first_packet_from_client_is_connect( executable: &ClientExecutable, ) -> miette::Result { -- cgit v1.2.3