From b8956c1924a66588ea410bb7cc15416c376f473f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 13 Jan 2023 08:55:04 +0100 Subject: Reimpl test as BehaviourTest: PublishQosZeroWithIdentFails Signed-off-by: Matthias Beyer --- mqtt-tester/src/behaviour/mod.rs | 2 + .../behaviour/publish_qos_zero_with_ident_fails.rs | 70 ++++++++++++++++++++++ mqtt-tester/src/client_report.rs | 45 +------------- 3 files changed, 73 insertions(+), 44 deletions(-) create mode 100644 mqtt-tester/src/behaviour/publish_qos_zero_with_ident_fails.rs diff --git a/mqtt-tester/src/behaviour/mod.rs b/mqtt-tester/src/behaviour/mod.rs index e831a05..39a7d03 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_zero_with_ident_fails; pub mod receiving_server_packet; pub mod utf8_with_nullchar_is_rejected; pub mod wait_for_connect; @@ -14,6 +15,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_zero_with_ident_fails::PublishQosZeroWithIdentFails; pub use self::receiving_server_packet::ReceivingServerPacket; pub use self::utf8_with_nullchar_is_rejected::Utf8WithNullcharIsRejected; pub use self::wait_for_connect::WaitForConnect; diff --git a/mqtt-tester/src/behaviour/publish_qos_zero_with_ident_fails.rs b/mqtt-tester/src/behaviour/publish_qos_zero_with_ident_fails.rs new file mode 100644 index 0000000..fe5544d --- /dev/null +++ b/mqtt-tester/src/behaviour/publish_qos_zero_with_ident_fails.rs @@ -0,0 +1,70 @@ +// +// 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 PublishQosZeroWithIdentFails; + +#[async_trait::async_trait] +impl BehaviourTest for PublishQosZeroWithIdentFails { + 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::AtMostOnce, // QoS 0 + retain: false, + topic_name: MString { value: "a" }, + id: Some(MPacketIdentifier(1)), + payload: &[0x00], + }) + .await?; + Ok(()) + } + + fn report_name(&self) -> &str { + "A PUBLISH packet with QoS zero must not contain a packet identifier" + } + + fn report_desc(&self) -> &str { + "A PUBLISH Packet MUST NOT contain a Packet Identifier if its QoS value is set to 0." + } + + fn report_normative(&self) -> &str { + "[MQTT-2.3.1-5]" + } + + 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 b83b519..d6de3da 100644 --- a/mqtt-tester/src/client_report.rs +++ b/mqtt-tester/src/client_report.rs @@ -32,7 +32,6 @@ pub async fn create_client_report( let executable = ClientExecutable::new(client_exe_path); let reports = vec![ - check_publish_qos_zero_with_ident_fails(&executable).boxed_local(), 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(), @@ -49,6 +48,7 @@ pub async fn create_client_report( Box::new(crate::behaviour::InvalidFirstPacketIsRejected), Box::new(crate::behaviour::Utf8WithNullcharIsRejected), Box::new(crate::behaviour::ConnackFlagsAreSetAsReserved), + Box::new(crate::behaviour::PublishQosZeroWithIdentFails), ]; let invariants: Vec> = vec![Arc::new(NoUsernameMeansNoPassword)]; @@ -155,49 +155,6 @@ macro_rules! wait_for_output { }}; } -async fn check_publish_qos_zero_with_ident_fails( - executable: &ClientExecutable, -) -> miette::Result { - let (client, mut input, _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::AtMostOnce, // QoS 0 - retain: false, - topic_name: MString { value: "a" }, - id: Some(MPacketIdentifier(1)), - payload: &[0x00], - }) - .await?; - - let output = client.wait_with_output(); - let (result, output) = wait_for_output! { - output, - timeout_ms: 100, - out_success => { ReportResult::Failure }, - out_failure => { ReportResult::Success } - }; - - Ok(mk_report! { - name: "A PUBLISH packet with QoS zero must not contain a packet identifier", - desc: "A PUBLISH Packet MUST NOT contain a Packet Identifier if its QoS value is set to 0.", - normative: "[MQTT-2.3.1-5]", - result, - output - }) -} - async fn check_publish_qos_2_is_acked(executable: &ClientExecutable) -> miette::Result { let (client, mut input, mut output) = executable .call(&[]) -- cgit v1.2.3