mqtt5_protocol/packet/
pubrel.rs

1use super::ack_common::{define_ack_packet, is_valid_pubrel_reason_code};
2use crate::packet::PacketType;
3
4define_ack_packet! {
5    /// MQTT PUBREL packet (`QoS` 2 publish release, part 2)
6    pub struct PubRelPacket;
7    packet_type = PacketType::PubRel;
8    validator = is_valid_pubrel_reason_code;
9    error_prefix = "PUBREL";
10    flags = 0x02;
11    validate_flags = true;
12}
13
14#[cfg(test)]
15mod tests {
16    use super::*;
17    use crate::packet::{FixedHeader, MqttPacket};
18    use crate::protocol::v5::properties::PropertyId;
19    use crate::types::ReasonCode;
20    use bytes::{BufMut, BytesMut};
21
22    #[test]
23    fn test_pubrel_basic() {
24        let packet = PubRelPacket::new(123);
25
26        assert_eq!(packet.packet_id, 123);
27        assert_eq!(packet.reason_code, ReasonCode::Success);
28        assert!(packet.properties.is_empty());
29        assert_eq!(packet.flags(), 0x02);
30    }
31
32    #[test]
33    fn test_pubrel_with_reason() {
34        let packet = PubRelPacket::new_with_reason(456, ReasonCode::PacketIdentifierNotFound)
35            .with_reason_string("Packet ID not found".to_string());
36
37        assert_eq!(packet.packet_id, 456);
38        assert_eq!(packet.reason_code, ReasonCode::PacketIdentifierNotFound);
39        assert!(packet.properties.contains(PropertyId::ReasonString));
40    }
41
42    #[test]
43    fn test_pubrel_encode_decode() {
44        let packet = PubRelPacket::new(789);
45
46        let mut buf = BytesMut::new();
47        packet.encode(&mut buf).unwrap();
48
49        let fixed_header = FixedHeader::decode(&mut buf).unwrap();
50        assert_eq!(fixed_header.packet_type, PacketType::PubRel);
51        assert_eq!(fixed_header.flags, 0x02);
52
53        let decoded = PubRelPacket::decode_body(&mut buf, &fixed_header).unwrap();
54        assert_eq!(decoded.packet_id, 789);
55        assert_eq!(decoded.reason_code, ReasonCode::Success);
56    }
57
58    #[test]
59    fn test_pubrel_invalid_flags() {
60        let mut buf = BytesMut::new();
61        buf.put_u16(123);
62
63        let fixed_header = FixedHeader::new(PacketType::PubRel, 0x00, 2);
64        let result = PubRelPacket::decode_body(&mut buf, &fixed_header);
65        assert!(result.is_err());
66    }
67
68    #[test]
69    fn test_pubrel_v311_style() {
70        let mut buf = BytesMut::new();
71        buf.put_u16(1234);
72
73        let fixed_header = FixedHeader::new(PacketType::PubRel, 0x02, 2);
74        let decoded = PubRelPacket::decode_body(&mut buf, &fixed_header).unwrap();
75
76        assert_eq!(decoded.packet_id, 1234);
77        assert_eq!(decoded.reason_code, ReasonCode::Success);
78    }
79
80    #[test]
81    fn test_pubrel_invalid_reason_code() {
82        let mut buf = BytesMut::new();
83        buf.put_u16(123);
84        buf.put_u8(0xFF);
85
86        let fixed_header = FixedHeader::new(PacketType::PubRel, 0x02, 3);
87        let result = PubRelPacket::decode_body(&mut buf, &fixed_header);
88        assert!(result.is_err());
89    }
90
91    #[test]
92    fn test_pubrel_missing_packet_id() {
93        let mut buf = BytesMut::new();
94        buf.put_u8(0);
95
96        let fixed_header = FixedHeader::new(PacketType::PubRel, 0x02, 1);
97        let result = PubRelPacket::decode_body(&mut buf, &fixed_header);
98        assert!(result.is_err());
99    }
100}