mqttrs 0.4.1

Mqtt protocol encoding and decoding
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
416
417
418
419
420
421
422
423
424
425
426
use crate::*;
use bytes::BytesMut;
use subscribe::LimitedString;

macro_rules! header {
    ($t:ident, $d:expr, $q:ident, $r:expr) => {
        decoder::Header {
            typ: PacketType::$t,
            dup: $d,
            qos: QoS::$q,
            retain: $r,
        }
    };
}

fn bm(d: &[u8]) -> BytesMut {
    BytesMut::from(d)
}

/// Test all possible header first byte, using remaining_len=0.
#[test]
fn header_firstbyte() {
    let valid = vec![
        (0b0001_0000, header!(Connect, false, AtMostOnce, false)),
        (0b0010_0000, header!(Connack, false, AtMostOnce, false)),
        (0b0011_0000, header!(Publish, false, AtMostOnce, false)),
        (0b0011_0001, header!(Publish, false, AtMostOnce, true)),
        (0b0011_0010, header!(Publish, false, AtLeastOnce, false)),
        (0b0011_0011, header!(Publish, false, AtLeastOnce, true)),
        (0b0011_0100, header!(Publish, false, ExactlyOnce, false)),
        (0b0011_0101, header!(Publish, false, ExactlyOnce, true)),
        (0b0011_1000, header!(Publish, true, AtMostOnce, false)),
        (0b0011_1001, header!(Publish, true, AtMostOnce, true)),
        (0b0011_1010, header!(Publish, true, AtLeastOnce, false)),
        (0b0011_1011, header!(Publish, true, AtLeastOnce, true)),
        (0b0011_1100, header!(Publish, true, ExactlyOnce, false)),
        (0b0011_1101, header!(Publish, true, ExactlyOnce, true)),
        (0b0100_0000, header!(Puback, false, AtMostOnce, false)),
        (0b0101_0000, header!(Pubrec, false, AtMostOnce, false)),
        (0b0110_0010, header!(Pubrel, false, AtLeastOnce, false)),
        (0b0111_0000, header!(Pubcomp, false, AtMostOnce, false)),
        (0b1000_0010, header!(Subscribe, false, AtLeastOnce, false)),
        (0b1001_0000, header!(Suback, false, AtMostOnce, false)),
        (0b1010_0010, header!(Unsubscribe, false, AtLeastOnce, false)),
        (0b1011_0000, header!(Unsuback, false, AtMostOnce, false)),
        (0b1100_0000, header!(Pingreq, false, AtMostOnce, false)),
        (0b1101_0000, header!(Pingresp, false, AtMostOnce, false)),
        (0b1110_0000, header!(Disconnect, false, AtMostOnce, false)),
    ];
    for n in 0..=255 {
        let res = match valid.iter().find(|(byte, _)| *byte == n) {
            Some((_, header)) => Ok(Some((*header, 0))),
            None if ((n & 0b110) == 0b110) && (n >> 4 == 3) => Err(Error::InvalidQos(3)),
            None => Err(Error::InvalidHeader),
        };
        let mut buf: &[u8] = &[n, 0];
        let mut offset = 0;
        assert_eq!(
            res,
            decoder::read_header(&mut buf, &mut offset),
            "{:08b}",
            n
        );
        if res.is_ok() {
            assert_eq!(offset, 2);
        } else {
            assert_eq!(offset, 0);
        }
    }
}

/// Test decoding of length and actual buffer len.
#[rustfmt::skip]
#[test]
fn header_len() {
    let h = header!(Connect, false, AtMostOnce, false);
    for (res, mut bytes, buflen) in vec![
        (Ok(Some((h, 0))),          vec![1 << 4, 0],   2),
        (Ok(None),                  vec![1 << 4, 127], 128),
        (Ok(Some((h, 127))),        vec![1 << 4, 127], 129),
        (Ok(None),                  vec![1 << 4, 0x80], 2),
        (Ok(Some((h, 0))),          vec![1 << 4, 0x80, 0], 3), //Weird encoding for "0" buf matches spec
        (Ok(Some((h, 128))),        vec![1 << 4, 0x80, 1], 131),
        (Ok(None),                  vec![1 << 4, 0x80+16, 78], 10002),
        (Ok(Some((h, 10000))),      vec![1 << 4, 0x80+16, 78], 10003),
        (Err(Error::InvalidHeader), vec![1 << 4, 0x80, 0x80, 0x80, 0x80], 10),
    ] {
        let offset_expectation = bytes.len();
        bytes.resize(buflen, 0);
        let mut slice_buf = bytes.as_slice();
        let mut offset = 0;
        assert_eq!(res, decoder::read_header(&mut slice_buf, &mut offset));
        match res {
            Ok(Some(_)) => assert_eq!(offset, offset_expectation),
            _ => assert_eq!(offset, 0)
        }
    }
}

#[test]
fn non_utf8_string() {
    let mut data: &[u8] = &[
        0b00110000, 10, // type=Publish, remaining_len=10
        0x00, 0x03, 'a' as u8, '/' as u8, 0xc0 as u8, // Topic with Invalid utf8
        'h' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8, // payload
    ];
    assert!(match decode_slice(&mut data) {
        Err(Error::InvalidString(_)) => true,
        _ => false,
    });
}

/// Validity of remaining_len is tested exhaustively elsewhere, this is for inner lengths, which
/// are rarer.
#[test]
fn inner_length_too_long() {
    let mut data = bm(&[
        0b00010000, 20, // Connect packet, remaining_len=20
        0x00, 0x04, 'M' as u8, 'Q' as u8, 'T' as u8, 'T' as u8, 0x04, 0b01000000, // +password
        0x00, 0x0a, // keepalive 10 sec
        0x00, 0x04, 't' as u8, 'e' as u8, 's' as u8, 't' as u8, // client_id
        0x00, 0x03, 'm' as u8, 'q' as u8, // password with invalid length
    ]);
    assert_eq!(Err(Error::InvalidLength), decode_slice(&mut data));

    let mut slice: &[u8] = &[
        0b00010000, 20, // Connect packet, remaining_len=20
        0x00, 0x04, 'M' as u8, 'Q' as u8, 'T' as u8, 'T' as u8, 0x04, 0b01000000, // +password
        0x00, 0x0a, // keepalive 10 sec
        0x00, 0x04, 't' as u8, 'e' as u8, 's' as u8, 't' as u8, // client_id
        0x00, 0x03, 'm' as u8, 'q' as u8, // password with invalid length
    ];

    assert_eq!(Err(Error::InvalidLength), decode_slice(&mut slice));
    // assert_eq!(slice, []);
}

#[test]
fn test_half_connect() {
    let mut data: &[u8] = &[
        0b00010000, 39, 0x00, 0x04, 'M' as u8, 'Q' as u8, 'T' as u8, 'T' as u8, 0x04,
        0b11001110, // +username, +password, -will retain, will qos=1, +last_will, +clean_session
        0x00,
        0x0a, // 10 sec
              // 0x00, 0x04, 't' as u8, 'e' as u8, 's' as u8, 't' as u8, // client_id
              // 0x00, 0x02, '/' as u8, 'a' as u8, // will topic = '/a'
              // 0x00, 0x07, 'o' as u8, 'f' as u8, 'f' as u8, 'l' as u8, 'i' as u8, 'n' as u8,
              // 'e' as u8, // will msg = 'offline'
              // 0x00, 0x04, 'r' as u8, 'u' as u8, 's' as u8, 't' as u8, // username = 'rust'
              // 0x00, 0x02, 'm' as u8, 'q' as u8, // password = 'mq'
    ];
    assert_eq!(Ok(None), decode_slice(&mut data));
    assert_eq!(12, data.len());
}

#[test]
fn test_connect_wrong_version() {
    let mut data: &[u8] = &[
        0b00010000, 39, 0x00, 0x04, 'M' as u8, 'Q' as u8, 'T' as u8, 'T' as u8, 0x01,
        0b11001110, // +username, +password, -will retain, will qos=1, +last_will, +clean_session
        0x00, 0x0a, // 10 sec
        0x00, 0x04, 't' as u8, 'e' as u8, 's' as u8, 't' as u8, // client_id
        0x00, 0x02, '/' as u8, 'a' as u8, // will topic = '/a'
        0x00, 0x07, 'o' as u8, 'f' as u8, 'f' as u8, 'l' as u8, 'i' as u8, 'n' as u8,
        'e' as u8, // will msg = 'offline'
        0x00, 0x04, 'r' as u8, 'u' as u8, 's' as u8, 't' as u8, // username = 'rust'
        0x00, 0x02, 'm' as u8, 'q' as u8, // password = 'mq'
    ];
    assert!(
        decode_slice(&mut data).is_err(),
        "Unknown version should return error"
    );
}

#[test]
fn test_connect() {
    let mut data: &[u8] = &[
        0b00010000, 39, 0x00, 0x04, 'M' as u8, 'Q' as u8, 'T' as u8, 'T' as u8, 0x04,
        0b11001110, // +username, +password, -will retain, will qos=1, +last_will, +clean_session
        0x00, 0x0a, // 10 sec
        0x00, 0x04, 't' as u8, 'e' as u8, 's' as u8, 't' as u8, // client_id
        0x00, 0x02, '/' as u8, 'a' as u8, // will topic = '/a'
        0x00, 0x07, 'o' as u8, 'f' as u8, 'f' as u8, 'l' as u8, 'i' as u8, 'n' as u8,
        'e' as u8, // will msg = 'offline'
        0x00, 0x04, 'r' as u8, 'u' as u8, 's' as u8, 't' as u8, // username = 'rust'
        0x00, 0x02, 'm' as u8, 'q' as u8, // password = 'mq'
    ];
    let pkt = Connect {
        protocol: Protocol::MQTT311,
        keep_alive: 10,
        client_id: "test",
        clean_session: true,
        last_will: Some(LastWill {
            topic: "/a",
            message: b"offline",
            qos: QoS::AtLeastOnce,
            retain: false,
        }),
        username: Some("rust"),
        password: Some(b"mq"),
    };

    let packet_buf = &mut [0u8; 64];
    assert_eq!(clone_packet(&mut data, &mut packet_buf[..]).unwrap(), 41);
    assert_eq!(Ok(Some(pkt.into())), decode_slice(packet_buf));
    // assert_eq!(data.len(), 0);
}

#[test]
fn test_connack() {
    let mut data: &[u8] = &[0b00100000, 2, 0b00000000, 0b00000001];
    let d = decode_slice(&mut data).unwrap();
    match d {
        Some(Packet::Connack(c)) => {
            let o = Connack {
                session_present: false,
                code: ConnectReturnCode::RefusedProtocolVersion,
            };
            assert_eq!(c.session_present, o.session_present);
            assert_eq!(c.code, o.code);
        }
        _ => panic!(),
    }
}

#[test]
fn test_ping_req() {
    let mut data: &[u8] = &[0b11000000, 0b00000000];
    assert_eq!(Ok(Some(Packet::Pingreq)), decode_slice(&mut data));
}

#[test]
fn test_ping_resp() {
    let mut data: &[u8] = &[0b11010000, 0b00000000];
    assert_eq!(Ok(Some(Packet::Pingresp)), decode_slice(&mut data));
}

#[test]
fn test_disconnect() {
    let mut data: &[u8] = &[0b11100000, 0b00000000];
    assert_eq!(Ok(Some(Packet::Disconnect)), decode_slice(&mut data));
}

#[test]
#[ignore]
fn test_offset_start() {
    let mut data: &[u8] = &[
        1, 2, 3, 0b00110000, 10, 0x00, 0x03, 'a' as u8, '/' as u8, 'b' as u8, 'h' as u8, 'e' as u8,
        'l' as u8, 'l' as u8, 'o' as u8, //
        0b00111000, 10, 0x00, 0x03, 'a' as u8, '/' as u8, 'b' as u8, 'h' as u8, 'e' as u8,
        'l' as u8, 'l' as u8, 'o' as u8, //
        0b00111101, 12, 0x00, 0x03, 'a' as u8, '/' as u8, 'b' as u8, 0, 10, 'h' as u8, 'e' as u8,
        'l' as u8, 'l' as u8, 'o' as u8,
    ];

    let packet_buf = &mut [0u8; 64];
    assert_eq!(clone_packet(&mut data, &mut packet_buf[..]).unwrap(), 12);
    assert_eq!(data.len(), 29);

    match decode_slice(packet_buf) {
        Ok(Some(Packet::Publish(p))) => {
            assert_eq!(p.dup, false);
            assert_eq!(p.retain, false);
            assert_eq!(p.qospid, QosPid::AtMostOnce);
            assert_eq!(p.topic_name, "a/b");
            assert_eq!(core::str::from_utf8(p.payload).unwrap(), "hello");
        }
        other => panic!("Failed decode: {:?}", other),
    }
}

#[test]
#[ignore]
fn test_publish() {
    let mut data: &[u8] = &[
        0b00110000, 10, 0x00, 0x03, 'a' as u8, '/' as u8, 'b' as u8, 'h' as u8, 'e' as u8,
        'l' as u8, 'l' as u8, 'o' as u8, //
        0b00111000, 10, 0x00, 0x03, 'a' as u8, '/' as u8, 'b' as u8, 'h' as u8, 'e' as u8,
        'l' as u8, 'l' as u8, 'o' as u8, //
        0b00111101, 12, 0x00, 0x03, 'a' as u8, '/' as u8, 'b' as u8, 0, 10, 'h' as u8, 'e' as u8,
        'l' as u8, 'l' as u8, 'o' as u8,
    ];

    let mut offset = 0;
    assert_eq!(
        decoder::read_header(&data, &mut offset).unwrap(),
        Some((decoder::Header::new(0b00110000).unwrap(), 10))
    );
    assert_eq!(data.len(), 38);

    let packet_buf = &mut [0u8; 64];
    assert_eq!(clone_packet(&mut data, &mut packet_buf[..]).unwrap(), 12);
    // assert_eq!(data.len(), 26);

    match decode_slice(packet_buf) {
        Ok(Some(Packet::Publish(p))) => {
            assert_eq!(p.dup, false);
            assert_eq!(p.retain, false);
            assert_eq!(p.qospid, QosPid::AtMostOnce);
            assert_eq!(p.topic_name, "a/b");
            assert_eq!(core::str::from_utf8(p.payload).unwrap(), "hello");
        }
        other => panic!("Failed decode: {:?}", other),
    }

    let packet_buf2 = &mut [0u8; 64];
    assert_eq!(clone_packet(&mut data, &mut packet_buf2[..]).unwrap(), 12);
    // assert_eq!(data.len(), 14);
    match decode_slice(packet_buf2) {
        Ok(Some(Packet::Publish(p))) => {
            assert_eq!(p.dup, true);
            assert_eq!(p.retain, false);
            assert_eq!(p.qospid, QosPid::AtMostOnce);
            assert_eq!(p.topic_name, "a/b");
            assert_eq!(core::str::from_utf8(p.payload).unwrap(), "hello");
        }
        other => panic!("Failed decode: {:?}", other),
    }

    let packet_buf3 = &mut [0u8; 64];
    assert_eq!(clone_packet(&mut data, &mut packet_buf3[..]).unwrap(), 14);
    // assert_eq!(data.len(), 0);

    match decode_slice(packet_buf3) {
        Ok(Some(Packet::Publish(p))) => {
            assert_eq!(p.dup, true);
            assert_eq!(p.retain, true);
            assert_eq!(p.qospid, QosPid::from_u8u16(2, 10));
            assert_eq!(p.topic_name, "a/b");
            assert_eq!(core::str::from_utf8(p.payload).unwrap(), "hello");
        }
        other => panic!("Failed decode: {:?}", other),
    }
}

#[test]
fn test_pub_ack() {
    let mut data: &[u8] = &[0b01000000, 0b00000010, 0, 10];
    match decode_slice(&mut data) {
        Ok(Some(Packet::Puback(a))) => assert_eq!(a.get(), 10),
        other => panic!("Failed decode: {:?}", other),
    };
}

#[test]
fn test_pub_rec() {
    let mut data: &[u8] = &[0b01010000, 0b00000010, 0, 10];
    match decode_slice(&mut data) {
        Ok(Some(Packet::Pubrec(a))) => assert_eq!(a.get(), 10),
        other => panic!("Failed decode: {:?}", other),
    };
}

#[test]
fn test_pub_rel() {
    let mut data: &[u8] = &[0b01100010, 0b00000010, 0, 10];
    match decode_slice(&mut data) {
        Ok(Some(Packet::Pubrel(a))) => assert_eq!(a.get(), 10),
        other => panic!("Failed decode: {:?}", other),
    };
}

#[test]
fn test_pub_comp() {
    let mut data: &[u8] = &[0b01110000, 0b00000010, 0, 10];
    match decode_slice(&mut data) {
        Ok(Some(Packet::Pubcomp(a))) => assert_eq!(a.get(), 10),
        other => panic!("Failed decode: {:?}", other),
    };
}

#[test]
fn test_subscribe() {
    let mut data: &[u8] = &[
        0b10000010, 8, 0, 10, 0, 3, 'a' as u8, '/' as u8, 'b' as u8, 0,
    ];
    match decode_slice(&mut data) {
        Ok(Some(Packet::Subscribe(s))) => {
            assert_eq!(s.pid.get(), 10);
            let t = SubscribeTopic {
                topic_path: LimitedString::from("a/b"),
                qos: QoS::AtMostOnce,
            };
            assert_eq!(s.topics.get(0), Some(&t));
        }
        other => panic!("Failed decode: {:?}", other),
    }
}

#[test]
fn test_suback() {
    let mut data: &[u8] = &[0b10010000, 3, 0, 10, 0b00000010];
    match decode_slice(&mut data) {
        Ok(Some(Packet::Suback(s))) => {
            assert_eq!(s.pid.get(), 10);
            assert_eq!(
                s.return_codes.get(0),
                Some(&SubscribeReturnCodes::Success(QoS::ExactlyOnce))
            );
        }
        other => panic!("Failed decode: {:?}", other),
    }
}

#[test]
fn test_unsubscribe() {
    let mut data: &[u8] = &[0b10100010, 5, 0, 10, 0, 1, 'a' as u8];
    match decode_slice(&mut data) {
        Ok(Some(Packet::Unsubscribe(a))) => {
            assert_eq!(a.pid.get(), 10);
            assert_eq!(a.topics.get(0), Some(&LimitedString::from("a")));
        }
        other => panic!("Failed decode: {:?}", other),
    }
}

#[test]
fn test_unsub_ack() {
    let mut data: &[u8] = &[0b10110000, 2, 0, 10];
    match decode_slice(&mut data) {
        Ok(Some(Packet::Unsuback(p))) => {
            assert_eq!(p.get(), 10);
        }
        other => panic!("Failed decode: {:?}", other),
    }
}