summaryrefslogtreecommitdiffstats
path: root/mqtt-tester/src/behaviour/receiving_server_packet.rs
blob: 34ea77755dd2abb178c8cf3cd8266f551bf0becb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
//   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 miette::Context;
use mqtt_format::v3::connect_return::MConnectReturnCode;
use mqtt_format::v3::identifier::MPacketIdentifier;
use mqtt_format::v3::packet::MConnack;
use mqtt_format::v3::packet::MSubscribe;
use mqtt_format::v3::subscription_request::MSubscriptionRequests;

use crate::behaviour_test::BehaviourTest;
use crate::command::Input;
use crate::command::Output;
use crate::executable::ClientExecutableCommand;
use crate::report::ReportResult;

pub struct ReceivingServerPacket;

#[async_trait::async_trait]
impl BehaviourTest for ReceivingServerPacket {
    fn commands(&self) -> Vec<Box<dyn ClientExecutableCommand>> {
        vec![]
    }

    #[tracing::instrument(skip_all)]
    async fn execute(
        &self,
        mut input: Input,
        _output: Output,
    ) -> Result<ReportResult, miette::Error> {
        input
            .send_packet(MConnack {
                session_present: false,
                connect_return_code: MConnectReturnCode::Accepted,
            })
            .await
            .context("Sending packet CONNACK")?;

        input
            .send_packet(MSubscribe {
                id: MPacketIdentifier(1),
                subscriptions: MSubscriptionRequests {
                    count: 1,
                    data: b"a/b",
                },
            })
            .await
            .context("Sending packet SUBSCRIBE")?;
        Ok(ReportResult::Success)
    }

    fn report_name(&self) -> &str {
        "Check if invalid packets are rejected"
    }

    fn report_desc(&self) -> &str {
        "Unexpected packets are a protocol error and the client MUST close the connection."
    }

    fn report_normative(&self) -> &str {
        "[MQTT-4.8.0-1]"
    }

    fn translate_client_exit_code(&self, success: bool) -> ReportResult {
        if success {
            ReportResult::Failure
        } else {
            ReportResult::Success
        }
    }
}