Skip to content

Commit

Permalink
fix: Use variable length byte to encode the subscription ID (#74)
Browse files Browse the repository at this point in the history
The length of the package was already properly calculated. Only the
value itself wasn't properly encoded in the packet.

Closes #73
  • Loading branch information
ctron authored Nov 4, 2021
1 parent d12dc64 commit fb14f0e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
47 changes: 46 additions & 1 deletion src/v5/codec/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ pub(super) fn reduce_limit(limit: u32, reduction: usize) -> u32 {
#[cfg(test)]
mod tests {
use ntex::util::Bytes;
use std::num::NonZeroU16;
use std::num::{NonZeroU16, NonZeroU32};

use super::*;
use crate::types::{QoS, MAX_PACKET_SIZE};
Expand Down Expand Up @@ -411,6 +411,22 @@ mod tests {
}),
b"\x30\x0c\x00\x05topic\x00data",
);

assert_encode_packet(
&Packet::Publish(Publish {
dup: false,
retain: false,
qos: QoS::AtMostOnce,
topic: ByteString::from_static("topic"),
packet_id: None,
payload: Bytes::from_static(b"data"),
properties: PublishProperties {
subscription_ids: Some(vec![NonZeroU32::new(1).unwrap()]),
..Default::default()
},
}),
b"\x30\x0e\x00\x05topic\x02\x0b\x01data",
);
}

#[test]
Expand Down Expand Up @@ -444,6 +460,35 @@ mod tests {
b"\x82\x13\x12\x34\x00\x00\x04test\x01\x00\x06filter\x02",
);

assert_encode_packet(
&Packet::Subscribe(Subscribe {
packet_id: packet_id(0x1234),
id: Some(NonZeroU32::new(1).unwrap()),
user_properties: Vec::new(),
topic_filters: vec![
(
ByteString::from_static("test"),
SubscriptionOptions {
qos: QoS::AtLeastOnce,
no_local: false,
retain_as_published: false,
retain_handling: RetainHandling::AtSubscribe,
},
),
(
ByteString::from_static("filter"),
SubscriptionOptions {
qos: QoS::ExactlyOnce,
no_local: false,
retain_as_published: false,
retain_handling: RetainHandling::AtSubscribe,
},
),
],
}),
b"\x82\x14\x12\x34\x01\x0b\x01\x00\x04test\x01\x00\x06filter\x02",
);

assert_encode_packet(
&Packet::SubscribeAck(SubscribeAck {
packet_id: packet_id(0x1234),
Expand Down
4 changes: 2 additions & 2 deletions src/v5/codec/packet/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{convert::TryFrom, fmt, num::NonZeroU16, num::NonZeroU32};

use crate::error::{DecodeError, EncodeError};
use crate::types::QoS;
use crate::utils::{self, Decode, Encode, Property};
use crate::utils::{self, write_variable_length, Decode, Encode, Property};
use crate::v5::codec::{encode::*, property_type as pt, UserProperties};

/// PUBLISH message
Expand Down Expand Up @@ -183,7 +183,7 @@ impl EncodeLtd for PublishProperties {
if let Some(sub_ids) = self.subscription_ids.as_ref() {
for sub_id in sub_ids.iter() {
buf.put_u8(pt::SUB_ID);
sub_id.encode(buf)?;
write_variable_length(sub_id.get(), buf);
}
}
self.user_properties.encode(buf)
Expand Down
9 changes: 7 additions & 2 deletions src/v5/codec/packet/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::num::{NonZeroU16, NonZeroU32};
use super::ack_props;
use crate::error::{DecodeError, EncodeError};
use crate::types::QoS;
use crate::utils::{self, Decode, Encode};
use crate::utils::{self, write_variable_length, Decode, Encode};
use crate::v5::codec::{encode::*, property_type as pt, UserProperties, UserProperty};

/// Represents SUBSCRIBE packet
Expand Down Expand Up @@ -191,7 +191,12 @@ impl EncodeLtd for Subscribe {
let prop_len = self.id.map_or(0, |v| var_int_len(v.get() as usize))
+ self.user_properties.encoded_size() as u32; // safe: size was already checked against maximum
utils::write_variable_length(prop_len, buf);
encode_property(&self.id, pt::SUB_ID, buf)?;

if let Some(id) = self.id {
buf.put_u8(pt::SUB_ID);
write_variable_length(id.get(), buf);
}

for (filter, opts) in self.topic_filters.iter() {
filter.encode(buf)?;
opts.encode(buf)?;
Expand Down

0 comments on commit fb14f0e

Please sign in to comment.