mqrstt 0.4.2

Pure rust MQTTv5 client implementation Smol and Tokio
Documentation
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use crate::packets::*;

pub fn connack_case() -> (&'static [u8], Packet) {
    let packet: &'static [u8] = &[
        0x20, 0x13, 0x01, 0x00, 0x10, 0x27, 0x00, 0x10, 0x00, 0x00, 0x25, 0x01, 0x2a, 0x01, 0x29, 0x01, 0x22, 0xff, 0xff, 0x28, 0x01,
    ];

    let expected = ConnAck {
        connack_flags: ConnAckFlags { session_present: true },
        reason_code: ConnAckReasonCode::Success,
        connack_properties: ConnAckProperties {
            session_expiry_interval: None,
            receive_maximum: None,
            maximum_qos: None,
            retain_available: Some(true),
            maximum_packet_size: Some(1048576),
            assigned_client_identifier: None,
            topic_alias_maximum: Some(65535),
            reason_string: None,
            user_properties: vec![],
            wildcards_available: Some(true),
            subscription_ids_available: Some(true),
            shared_subscription_available: Some(true),
            server_keep_alive: None,
            response_info: None,
            server_reference: None,
            authentication_method: None,
            authentication_data: None,
        },
    };

    (packet, Packet::ConnAck(expected))
}

pub fn disconnect_case() -> (&'static [u8], Packet) {
    let packet: &'static [u8] = &[0xe0, 0x02, 0x8e, 0x00];

    let expected = Disconnect {
        reason_code: DisconnectReasonCode::SessionTakenOver,
        properties: DisconnectProperties {
            session_expiry_interval: None,
            reason_string: None,
            user_properties: vec![],
            server_reference: None,
        },
    };

    (packet, Packet::Disconnect(expected))
}

pub fn ping_req_case() -> (&'static [u8], Packet) {
    let packet: &'static [u8] = &[0xc0, 0x00];

    (packet, Packet::PingReq)
}

pub fn ping_resp_case() -> (&'static [u8], Packet) {
    let packet: &'static [u8] = &[0xd0, 0x00];

    (packet, Packet::PingResp)
}
pub fn publish_case() -> (&'static [u8], Packet) {
    let packet: &'static [u8] = &[
        0x35, 0x24, 0x00, 0x14, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x31, 0x32, 0x33, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x62, 0x6c, 0x61, 0x62, 0x6c, 0x61, 0x35, 0xd3, 0x0b, 0x01, 0x01, 0x09, 0x00, 0x04,
        0x31, 0x32, 0x31, 0x32, 0x0b, 0x01,
    ];

    let expected = Publish {
        dup: false,
        qos: QoS::ExactlyOnce,
        retain: true,
        topic: "test/123/test/blabla".into(),
        packet_identifier: Some(13779),
        publish_properties: PublishProperties {
            payload_format_indicator: Some(1),
            message_expiry_interval: None,
            topic_alias: None,
            response_topic: None,
            correlation_data: Some(b"1212".to_vec()),
            subscription_identifiers: vec![1],
            user_properties: vec![],
            content_type: None,
        },
        payload: b"".to_vec(),
    };

    (packet, Packet::Publish(expected))
}

pub fn pubrel_case() -> (&'static [u8], Packet) {
    let packet: &'static [u8] = &[0x62, 0x02, 0x35, 0xd3];

    let expected = PubRel {
        packet_identifier: 13779,
        reason_code: PubRelReasonCode::Success,
        properties: PubRelProperties {
            reason_string: None,
            user_properties: vec![],
        },
    };

    (packet, Packet::PubRel(expected))
}

pub fn pubrel_smallest_case() -> (&'static [u8], Packet) {
    let packet: &'static [u8] = &[0x62, 0x02, 0x35, 0xd3];

    let expected = PubRel {
        packet_identifier: 13779,
        reason_code: PubRelReasonCode::Success,
        properties: PubRelProperties {
            reason_string: None,
            user_properties: vec![],
        },
    };

    (packet, Packet::PubRel(expected))
}

pub fn connect_case() -> Packet {
    let connect = Connect {
        protocol_version: ProtocolVersion::V5,
        clean_start: true,
        last_will: Some(LastWill::new(QoS::ExactlyOnce, true, "will/topic", b"will payload".to_vec())),
        username: Some("ThisIsTheUsername".into()),
        password: Some("ThisIsThePassword".into()),
        keep_alive: 60,
        connect_properties: ConnectProperties {
            session_expiry_interval: Some(5),
            receive_maximum: Some(10),
            maximum_packet_size: Some(100),
            topic_alias_maximum: Some(10),
            user_properties: vec![("test".into(), "test".into()), ("test2".into(), "test2".into())],
            authentication_method: Some("AuthenticationMethod".into()),
            authentication_data: Some(b"AuthenticationData".to_vec()),
            request_response_information: Some(0),
            request_problem_information: Some(1),
        },
        client_id: "ThisIsTheClientID".into(),
    };

    Packet::Connect(connect)
}

pub fn publish_packet_1() -> Packet {
    Packet::Publish(Publish {
        dup: false,
        qos: QoS::ExactlyOnce,
        retain: true,
        topic: "test/123/test/blabla".into(),
        packet_identifier: Some(13779),
        publish_properties: PublishProperties {
            payload_format_indicator: Some(1),
            message_expiry_interval: None,
            topic_alias: None,
            response_topic: None,
            correlation_data: Some(b"1212".to_vec()),
            subscription_identifiers: vec![1],
            user_properties: vec![],
            content_type: None,
        },
        payload: b"".to_vec(),
    })
}
pub fn publish_packet_2() -> Packet {
    Packet::Publish(Publish {
        dup: true,
        qos: QoS::ExactlyOnce,
        retain: false,
        topic: "test/#".into(),
        packet_identifier: Some(4566),
        publish_properties: PublishProperties {
            payload_format_indicator: None,
            message_expiry_interval: Some(3600),
            topic_alias: Some(1),
            response_topic: None,
            correlation_data: Some(b"1212".to_vec()),
            subscription_identifiers: vec![1],
            user_properties: vec![],
            content_type: None,
        },
        payload: b"".to_vec(),
    })
}
pub fn publish_packet_3() -> Packet {
    Packet::Publish(Publish {
        dup: true,
        qos: QoS::AtLeastOnce,
        retain: false,
        topic: "test/#".into(),
        packet_identifier: Some(4566),
        publish_properties: PublishProperties {
            payload_format_indicator: None,
            message_expiry_interval: Some(3600),
            topic_alias: None,
            response_topic: Some("Please respond here thank you".into()),
            correlation_data: Some(b"5420874".to_vec()),
            subscription_identifiers: vec![],
            user_properties: vec![("blabla".into(), "another blabla".into())],
            content_type: None,
        },
        payload: b"".to_vec(),
    })
}
pub fn publish_packet_4() -> Packet {
    Packet::Publish(Publish {
        dup: true,
        qos: QoS::AtLeastOnce,
        retain: false,
        topic: "test/#".into(),
        packet_identifier: Some(4566),
        publish_properties: PublishProperties {
            payload_format_indicator: None,
            message_expiry_interval: Some(3600),
            topic_alias: Some(1),
            response_topic: None,
            correlation_data: Some(b"1212".to_vec()),
            subscription_identifiers: vec![1],
            user_properties: vec![],
            content_type: Some("Garbage".into()),
        },
        payload: b"".to_vec(),
        // payload: Bytes::from_iter(b"abcdefg".repeat(500)),
    })
}

pub fn create_subscribe_packet(packet_identifier: u16) -> Packet {
    let subscription: SubscribeTopics = "test/topic".into();
    let sub = Subscribe::new(packet_identifier, subscription.0);
    Packet::Subscribe(sub)
}

pub fn create_unsubscribe_packet(packet_identifier: u16) -> Packet {
    let sub = Unsubscribe::new(packet_identifier, vec!["test/topic".into()]);
    Packet::Unsubscribe(sub)
}

pub fn create_publish_packet(qos: QoS, dup: bool, retain: bool, packet_identifier: Option<u16>) -> Packet {
    Packet::Publish(Publish {
        dup,
        qos,
        retain,
        topic: "test/#".into(),
        packet_identifier,
        publish_properties: PublishProperties {
            payload_format_indicator: None,
            message_expiry_interval: Some(3600),
            topic_alias: Some(1),
            response_topic: None,
            correlation_data: Some(b"1212".to_vec()),
            subscription_identifiers: vec![1],
            user_properties: vec![],
            content_type: Some("Garbage".into()),
        },
        payload: b"testabcbba==asdasdasdasdasd".repeat(500).to_vec(),
    })
}

pub fn create_empty_publish_packet() -> Packet {
    Packet::Publish(Publish {
        dup: false,
        qos: QoS::AtMostOnce,
        retain: false,
        topic: "test/#".into(),
        packet_identifier: None,
        publish_properties: PublishProperties {
            payload_format_indicator: None,
            message_expiry_interval: Some(3600),
            topic_alias: Some(1),
            response_topic: None,
            correlation_data: Some(b"1212".to_vec()),
            subscription_identifiers: vec![1],
            user_properties: vec![],
            content_type: Some("Garbage".into()),
        },
        payload: vec![],
    })
}

pub fn create_puback_packet(packet_identifier: u16) -> Packet {
    Packet::PubAck(PubAck {
        packet_identifier,
        reason_code: PubAckReasonCode::Success,
        properties: PubAckProperties::default(),
    })
}

pub fn create_connack_packet(session_present: bool) -> Packet {
    let mut connack = ConnAck::default();
    connack.connack_flags.session_present = session_present;

    Packet::ConnAck(connack)
}

pub fn create_disconnect_packet() -> Packet {
    Packet::Disconnect(Disconnect {
        reason_code: DisconnectReasonCode::NormalDisconnection,
        properties: DisconnectProperties::default(),
    })
}

pub fn suback_case() -> Packet {
    let expected = SubAck {
        packet_identifier: 3,
        reason_codes: vec![SubAckReasonCode::GrantedQoS0, SubAckReasonCode::GrantedQoS1, SubAckReasonCode::GrantedQoS2],
        properties: SubAckProperties {
            user_properties: vec![(String::from("test").into(), String::from("test").into())],
            subscription_identifier: Some(2000),
        },
    };

    Packet::SubAck(expected)
}

pub fn subscribe_case() -> Packet {
    let expected = Subscribe {
        packet_identifier: 3,
        topics: vec![("test/topic".into(), SubscriptionOptions::default())],
        properties: SubscribeProperties {
            user_properties: vec![(String::from("test").into(), String::from("test").into())],
            subscription_identifier: Some(2000),
        },
    };

    Packet::Subscribe(expected)
}

// return a crazy big packet
pub fn unsuback_case() -> Packet {
    let expected = UnsubAck {
        packet_identifier: 3,
        reason_codes: vec![
            UnsubAckReasonCode::NoSubscriptionExisted,
            UnsubAckReasonCode::UnspecifiedError,
            UnsubAckReasonCode::ImplementationSpecificError,
        ],
        properties: UnsubAckProperties {
            user_properties: vec![],
            reason_string: None,
        },
    };

    Packet::UnsubAck(expected)
}

pub fn unsubscribe_case() -> Packet {
    let expected = Unsubscribe {
        packet_identifier: 3,
        topics: vec!["test/topic".into()],
        properties: UnsubscribeProperties {
            user_properties: vec![("written += 1;".into(), "value".into())],
        },
    };

    Packet::Unsubscribe(expected)
}

pub fn pubrec_case() -> Packet {
    let expected = PubRec {
        packet_identifier: 3,
        reason_code: PubRecReasonCode::Success,
        properties: PubRecProperties {
            reason_string: Some("test".into()),
            user_properties: vec![("test5asdf".into(), "test3".into()), ("test4".into(), "test2".into())],
        },
    };

    Packet::PubRec(expected)
}

pub fn pubcomp_case() -> Packet {
    let expected = PubComp {
        packet_identifier: 3,
        reason_code: PubCompReasonCode::PacketIdentifierNotFound,
        properties: PubCompProperties {
            reason_string: Some("test".into()),
            user_properties: vec![
                ("test5asdf".into(), "test3".into()),
                ("test⌚5asdf".into(), "test3".into()),
                ("test5asdf".into(), "test3".into()),
                ("test5asdf".into(), "test3".into()),
                ("test4".into(), "test2".into()),
            ],
        },
    };

    Packet::PubComp(expected)
}

pub fn pubrel_case2() -> Packet {
    let expected = PubRel {
        packet_identifier: 3,
        reason_code: PubRelReasonCode::Success,
        properties: PubRelProperties {
            reason_string: Some("test".into()),
            user_properties: vec![("test5asdf".into(), "test3".repeat(10000).into()), ("test4".into(), "test2".into())],
        },
    };

    Packet::PubRel(expected)
}

pub fn auth_case() -> Packet {
    let expected = Auth {
        reason_code: AuthReasonCode::ContinueAuthentication,
        properties: AuthProperties {
            authentication_method: Some("SomeRandomDataHere".into()),
            authentication_data: Some(b"VeryRandomStuff".to_vec()),
            reason_string: Some("⌚this_is_for_sure_a_test_⌚".into()),
            user_properties: vec![("SureHopeThisWorks".into(), "😰".into())],
        },
    };

    Packet::Auth(expected)
}