Rust MQTT
This commit is contained in:
228
mqtt/src/tests/integration/integration_test_single.rs
Normal file
228
mqtt/src/tests/integration/integration_test_single.rs
Normal file
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
extern crate alloc;
|
||||
use alloc::string::String;
|
||||
use core::time::Duration;
|
||||
use std::future::Future;
|
||||
use tokio::time::sleep;
|
||||
use tokio::{join, task};
|
||||
use tokio_test::assert_ok;
|
||||
|
||||
use crate::client::client_config::ClientConfig;
|
||||
use crate::client::client_v5::MqttClientV5;
|
||||
use crate::network::network_trait::{NetworkConnection, NetworkConnectionFactory};
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::publish_packet::QualityOfService;
|
||||
use crate::packet::v5::reason_codes::ReasonCode;
|
||||
use crate::packet::v5::reason_codes::ReasonCode::NotAuthorized;
|
||||
use crate::tokio_net::tokio_network::{TokioNetwork, TokioNetworkFactory};
|
||||
use crate::utils::types::BufferError;
|
||||
|
||||
static IP: [u8; 4] = [127, 0, 0, 1];
|
||||
static PORT: u16 = 1883;
|
||||
static USERNAME: &str = "test";
|
||||
static PASSWORD: &str = "testPass";
|
||||
static MSG: &str = "testMessage";
|
||||
|
||||
fn init() {
|
||||
let _ = env_logger::builder()
|
||||
.filter_level(log::LevelFilter::Info)
|
||||
.format_timestamp_nanos()
|
||||
.try_init();
|
||||
}
|
||||
|
||||
async fn publish_core<'b>(
|
||||
client: &mut MqttClientV5<'b, TokioNetwork, 5>,
|
||||
topic: &str,
|
||||
) -> Result<(), ReasonCode> {
|
||||
log::info!(
|
||||
"[Publisher] Connection to broker with username {} and password {}",
|
||||
USERNAME,
|
||||
PASSWORD
|
||||
);
|
||||
let mut result = { client.connect_to_broker().await };
|
||||
assert_ok!(result);
|
||||
log::info!("[Publisher] Waiting {} seconds before sending", 5);
|
||||
sleep(Duration::from_secs(5)).await;
|
||||
|
||||
log::info!("[Publisher] Sending new message {} to topic {}", MSG, topic);
|
||||
result = { client.send_message(topic, MSG).await };
|
||||
assert_ok!(result);
|
||||
|
||||
log::info!("[Publisher] Disconnecting!");
|
||||
result = { client.disconnect().await };
|
||||
assert_ok!(result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn publish(qos: QualityOfService, topic: &str) -> Result<(), ReasonCode> {
|
||||
let mut tokio_factory: TokioNetworkFactory = TokioNetworkFactory::new();
|
||||
let mut tokio_network: TokioNetwork = tokio_factory.connect(IP, PORT).await?;
|
||||
let mut config = ClientConfig::new();
|
||||
config.add_qos(qos);
|
||||
config.add_username(USERNAME);
|
||||
config.add_password(PASSWORD);
|
||||
config.max_packet_size = 100;
|
||||
let mut recv_buffer = [0; 80];
|
||||
let mut write_buffer = [0; 80];
|
||||
|
||||
let mut client = MqttClientV5::<TokioNetwork, 5>::new(
|
||||
&mut tokio_network,
|
||||
&mut write_buffer,
|
||||
80,
|
||||
&mut recv_buffer,
|
||||
80,
|
||||
config,
|
||||
);
|
||||
publish_core(&mut client, topic).await
|
||||
}
|
||||
|
||||
async fn receive_core<'b>(
|
||||
client: &mut MqttClientV5<'b, TokioNetwork, 5>,
|
||||
topic: &str,
|
||||
) -> Result<(), ReasonCode> {
|
||||
log::info!(
|
||||
"[Receiver] Connection to broker with username {} and password {}",
|
||||
USERNAME,
|
||||
PASSWORD
|
||||
);
|
||||
let mut result = { client.connect_to_broker().await };
|
||||
assert_ok!(result);
|
||||
|
||||
log::info!("[Receiver] Subscribing to topic {}", topic);
|
||||
result = { client.subscribe_to_topic(topic).await };
|
||||
assert_ok!(result);
|
||||
log::info!("[Receiver] Waiting for new message!");
|
||||
let msg = { client.receive_message().await };
|
||||
assert_ok!(msg);
|
||||
let act_message = String::from_utf8_lossy(msg?);
|
||||
log::info!("[Receiver] Got new message: {}", act_message);
|
||||
assert_eq!(act_message, MSG);
|
||||
|
||||
log::info!("[Receiver] Disconnecting");
|
||||
result = { client.disconnect().await };
|
||||
assert_ok!(result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive(qos: QualityOfService, topic: &str) -> Result<(), ReasonCode> {
|
||||
let mut tokio_factory: TokioNetworkFactory = TokioNetworkFactory::new();
|
||||
let mut tokio_network: TokioNetwork = tokio_factory.connect(IP, PORT).await?;
|
||||
let mut config = ClientConfig::new();
|
||||
config.add_qos(qos);
|
||||
config.add_username(USERNAME);
|
||||
config.add_password(PASSWORD);
|
||||
config.max_packet_size = 60;
|
||||
config.properties.push(Property::ReceiveMaximum(20));
|
||||
let mut recv_buffer = [0; 100];
|
||||
let mut write_buffer = [0; 100];
|
||||
|
||||
let mut client = MqttClientV5::<TokioNetwork, 5>::new(
|
||||
&mut tokio_network,
|
||||
&mut write_buffer,
|
||||
100,
|
||||
&mut recv_buffer,
|
||||
100,
|
||||
config,
|
||||
);
|
||||
|
||||
receive_core(&mut client, topic).await
|
||||
}
|
||||
|
||||
async fn receive_with_wrong_cred(qos: QualityOfService) -> Result<(), ReasonCode> {
|
||||
let mut tokio_factory: TokioNetworkFactory = TokioNetworkFactory::new();
|
||||
let mut tokio_network: TokioNetwork = tokio_factory.connect(IP, PORT).await?;
|
||||
let mut config = ClientConfig::new();
|
||||
config.add_qos(qos);
|
||||
config.add_username("xyz");
|
||||
config.add_password(PASSWORD);
|
||||
config.max_packet_size = 60;
|
||||
config.properties.push(Property::ReceiveMaximum(20));
|
||||
let mut recv_buffer = [0; 100];
|
||||
let mut write_buffer = [0; 100];
|
||||
|
||||
let mut client = MqttClientV5::<TokioNetwork, 5>::new(
|
||||
&mut tokio_network,
|
||||
&mut write_buffer,
|
||||
100,
|
||||
&mut recv_buffer,
|
||||
100,
|
||||
config,
|
||||
);
|
||||
|
||||
log::info!(
|
||||
"[Receiver] Connection to broker with username {} and password {}",
|
||||
"xyz",
|
||||
PASSWORD
|
||||
);
|
||||
let result = { client.connect_to_broker().await };
|
||||
assert!(result.is_err());
|
||||
assert_eq!(result.unwrap_err(), NotAuthorized);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn simple_publish_recv() {
|
||||
init();
|
||||
log::info!("Running simple integration test");
|
||||
|
||||
let recv =
|
||||
task::spawn(async move { receive(QualityOfService::QoS0, "test/recv/simple").await });
|
||||
|
||||
let publ =
|
||||
task::spawn(async move { publish(QualityOfService::QoS0, "test/recv/simple").await });
|
||||
|
||||
let (r, p) = join!(recv, publ);
|
||||
assert_ok!(r.unwrap());
|
||||
assert_ok!(p.unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn simple_publish_recv_qos() {
|
||||
init();
|
||||
log::info!("Running simple integration test with Quality of Service 1");
|
||||
|
||||
let recv = task::spawn(async move { receive(QualityOfService::QoS1, "test/recv/qos").await });
|
||||
|
||||
let publ = task::spawn(async move { publish(QualityOfService::QoS1, "test/recv/qos").await });
|
||||
let (r, p) = join!(recv, publ);
|
||||
assert_ok!(r.unwrap());
|
||||
assert_ok!(p.unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn simple_publish_recv_wrong_cred() {
|
||||
init();
|
||||
log::info!("Running simple integration test wrong credentials");
|
||||
|
||||
let recv = task::spawn(async move { receive_with_wrong_cred(QualityOfService::QoS1).await });
|
||||
|
||||
let recv_right =
|
||||
task::spawn(async move { receive(QualityOfService::QoS0, "test/recv/wrong").await });
|
||||
|
||||
let publ = task::spawn(async move { publish(QualityOfService::QoS1, "test/recv/wrong").await });
|
||||
let (r, rv, p) = join!(recv, recv_right, publ);
|
||||
assert_ok!(rv.unwrap());
|
||||
assert_ok!(p.unwrap());
|
||||
}
|
||||
24
mqtt/src/tests/integration/mod.rs
Normal file
24
mqtt/src/tests/integration/mod.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
pub mod integration_test_single;
|
||||
33
mqtt/src/tests/mod.rs
Normal file
33
mqtt/src/tests/mod.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(unused_must_use)]
|
||||
pub mod unit;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[allow(unused_must_use)]
|
||||
#[allow(unused_imports)]
|
||||
#[cfg(feature = "tokio")]
|
||||
pub mod integration;
|
||||
25
mqtt/src/tests/unit/encoding/mod.rs
Normal file
25
mqtt/src/tests/unit/encoding/mod.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
pub mod variable_byte_integer_unit;
|
||||
80
mqtt/src/tests/unit/encoding/variable_byte_integer_unit.rs
Normal file
80
mqtt/src/tests/unit/encoding/variable_byte_integer_unit.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::encoding::variable_byte_integer::{
|
||||
VariableByteInteger, VariableByteIntegerDecoder, VariableByteIntegerEncoder,
|
||||
};
|
||||
use crate::utils::types::BufferError;
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
static BUFFER: VariableByteInteger = [0x81, 0x81, 0x81, 0x01];
|
||||
|
||||
let decoded = VariableByteIntegerDecoder::decode(BUFFER);
|
||||
assert!(decoded.is_ok());
|
||||
assert_eq!(decoded.unwrap(), 2113665);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode_small() {
|
||||
static BUFFER: VariableByteInteger = [0x81, 0x81, 0x01, 0x85];
|
||||
|
||||
let decoded = VariableByteIntegerDecoder::decode(BUFFER);
|
||||
assert!(decoded.is_ok());
|
||||
assert_eq!(decoded.unwrap(), 16_513);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let encoded = VariableByteIntegerEncoder::encode(211_366_5);
|
||||
assert!(encoded.is_ok());
|
||||
let res = encoded.unwrap();
|
||||
assert_eq!(res, [0x81, 0x81, 0x81, 0x01]);
|
||||
assert_eq!(VariableByteIntegerEncoder::len(res), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_small() {
|
||||
let encoded = VariableByteIntegerEncoder::encode(16_513);
|
||||
assert!(encoded.is_ok());
|
||||
let res = encoded.unwrap();
|
||||
assert_eq!(res, [0x81, 0x81, 0x01, 0x00]);
|
||||
assert_eq!(VariableByteIntegerEncoder::len(res), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_extra_small() {
|
||||
let encoded = VariableByteIntegerEncoder::encode(5);
|
||||
assert!(encoded.is_ok());
|
||||
let res = encoded.unwrap();
|
||||
assert_eq!(res, [0x05, 0x00, 0x00, 0x00]);
|
||||
assert_eq!(VariableByteIntegerEncoder::len(res), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_max() {
|
||||
let encoded = VariableByteIntegerEncoder::encode(288_435_455);
|
||||
assert!(encoded.is_err());
|
||||
assert_eq!(encoded.unwrap_err(), BufferError::EncodingError);
|
||||
}
|
||||
27
mqtt/src/tests/unit/mod.rs
Normal file
27
mqtt/src/tests/unit/mod.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
pub mod encoding;
|
||||
pub mod packet;
|
||||
pub mod utils;
|
||||
25
mqtt/src/tests/unit/packet/mod.rs
Normal file
25
mqtt/src/tests/unit/packet/mod.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
pub mod v5;
|
||||
86
mqtt/src/tests/unit/packet/v5/connack_packet_unit.rs
Normal file
86
mqtt/src/tests/unit/packet/v5/connack_packet_unit.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::connack_packet::ConnackPacket;
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::reason_codes::ReasonCode;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 100] = [0; 100];
|
||||
let mut connack = ConnackPacket::<2>::new();
|
||||
connack.property_len = 3;
|
||||
let prop = Property::ReceiveMaximum(21);
|
||||
connack.properties.push(prop);
|
||||
connack.connect_reason_code = ReasonCode::ServerMoved.into();
|
||||
connack.ack_flags = 0x45;
|
||||
|
||||
let res = connack.encode(&mut buffer, 100);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(
|
||||
buffer[0..res.unwrap()],
|
||||
[
|
||||
0x20,
|
||||
0x06,
|
||||
0x45,
|
||||
ReasonCode::ServerMoved.into(),
|
||||
0x03,
|
||||
0x21,
|
||||
0x00,
|
||||
0x15
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
let mut buffer: [u8; 8] = [
|
||||
0x20,
|
||||
0x06,
|
||||
0x45,
|
||||
ReasonCode::ServerMoved.into(),
|
||||
0x03,
|
||||
0x21,
|
||||
0x00,
|
||||
0x15,
|
||||
];
|
||||
let mut connack_res = ConnackPacket::<2>::new();
|
||||
let res = connack_res.decode(&mut BuffReader::new(&mut buffer, 8));
|
||||
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(connack_res.property_len, 3);
|
||||
assert_eq!(connack_res.ack_flags, 0x45);
|
||||
assert_eq!(
|
||||
connack_res.connect_reason_code,
|
||||
ReasonCode::ServerMoved.into()
|
||||
);
|
||||
assert_eq!(connack_res.property_len, 3);
|
||||
let prop = connack_res.properties.get(0).unwrap();
|
||||
assert_eq!(<&Property as Into<u8>>::into(prop), 0x21);
|
||||
if let Property::ReceiveMaximum(u) = *prop {
|
||||
assert_eq!(u, 21);
|
||||
}
|
||||
}
|
||||
42
mqtt/src/tests/unit/packet/v5/connect_packet_unit.rs
Normal file
42
mqtt/src/tests/unit/packet/v5/connect_packet_unit.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::connect_packet::ConnectPacket;
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 100] = [0; 100];
|
||||
let mut connect = ConnectPacket::<1, 0>::clean();
|
||||
let res = connect.encode(&mut buffer, 100);
|
||||
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(
|
||||
buffer[0..res.unwrap()],
|
||||
[
|
||||
0x10, 0x10, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x05, 0x02, 0x00, 0x3c, 0x03, 0x21,
|
||||
0x00, 0x14, 0x00, 0x00
|
||||
]
|
||||
)
|
||||
}
|
||||
64
mqtt/src/tests/unit/packet/v5/disconnect_packet_unit.rs
Normal file
64
mqtt/src/tests/unit/packet/v5/disconnect_packet_unit.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::disconnect_packet::DisconnectPacket;
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
use heapless::Vec;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 10] = [0; 10];
|
||||
let mut packet = DisconnectPacket::<1>::new();
|
||||
let prop: Property = Property::SessionExpiryInterval(512);
|
||||
let mut props = Vec::<Property, 1>::new();
|
||||
props.push(prop);
|
||||
packet.property_len = packet.add_properties(&props);
|
||||
let res = packet.encode(&mut buffer, 100);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(
|
||||
buffer[0..res.unwrap()],
|
||||
[0xE0, 0x07, 0x00, 0x05, 0x11, 0x00, 0x00, 0x02, 0x00]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
let buffer: [u8; 10] = [0xE0, 0x07, 0x00, 0x05, 0x11, 0x00, 0x00, 0x04, 0x00, 0x00];
|
||||
let mut packet = DisconnectPacket::<1>::new();
|
||||
let res = packet.decode(&mut BuffReader::new(&buffer, 10));
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(packet.fixed_header, PacketType::Disconnect.into());
|
||||
assert_eq!(packet.remain_len, 7);
|
||||
assert_eq!(packet.disconnect_reason, 0x00);
|
||||
assert_eq!(packet.property_len, 5);
|
||||
let prop = packet.properties.get(0);
|
||||
assert!(prop.is_some());
|
||||
assert_eq!(<&Property as Into<u8>>::into(prop.unwrap()), 0x11);
|
||||
if let Property::SessionExpiryInterval(u) = *prop.unwrap() {
|
||||
assert_eq!(u, 1024);
|
||||
}
|
||||
}
|
||||
38
mqtt/src/tests/unit/packet/v5/mod.rs
Normal file
38
mqtt/src/tests/unit/packet/v5/mod.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
pub mod connack_packet_unit;
|
||||
pub mod connect_packet_unit;
|
||||
pub mod disconnect_packet_unit;
|
||||
pub mod pingreq_packet_unit;
|
||||
pub mod pingresp_packet_unit;
|
||||
pub mod puback_packet_unit;
|
||||
pub mod pubcomp_packet_unit;
|
||||
pub mod publish_packet_unit;
|
||||
pub mod pubrec_packet_unit;
|
||||
pub mod pubrel_packet_unit;
|
||||
pub mod suback_packet_unit;
|
||||
pub mod subscription_packet_unit;
|
||||
pub mod unsuback_packet_unit;
|
||||
pub mod unsubscription_packet_unit;
|
||||
38
mqtt/src/tests/unit/packet/v5/pingreq_packet_unit.rs
Normal file
38
mqtt/src/tests/unit/packet/v5/pingreq_packet_unit.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::pingreq_packet::PingreqPacket;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 3] = [0x00, 0x98, 0x45];
|
||||
let mut packet = PingreqPacket::new();
|
||||
packet.fixed_header = PacketType::Pingreq.into();
|
||||
packet.remain_len = 0;
|
||||
let res = packet.encode(&mut buffer, 3);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(buffer, [0xC0, 0x00, 0x45])
|
||||
}
|
||||
49
mqtt/src/tests/unit/packet/v5/pingresp_packet_unit.rs
Normal file
49
mqtt/src/tests/unit/packet/v5/pingresp_packet_unit.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::pingresp_packet::PingrespPacket;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 3] = [0x00, 0x98, 0x45];
|
||||
let mut packet = PingrespPacket::new();
|
||||
packet.fixed_header = PacketType::Pingresp.into();
|
||||
packet.remain_len = 0;
|
||||
let res = packet.encode(&mut buffer, 3);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(buffer, [0xD0, 0x00, 0x45])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
let buffer: [u8; 3] = [0xD0, 0x00, 0x51];
|
||||
let mut packet = PingrespPacket::new();
|
||||
let res = packet.decode(&mut BuffReader::new(&buffer, 3));
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(packet.fixed_header, PacketType::Pingresp.into());
|
||||
assert_eq!(packet.remain_len, 0);
|
||||
}
|
||||
73
mqtt/src/tests/unit/packet/v5/puback_packet_unit.rs
Normal file
73
mqtt/src/tests/unit/packet/v5/puback_packet_unit.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::puback_packet::PubackPacket;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
use crate::utils::types::EncodedString;
|
||||
use heapless::Vec;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 14] = [0; 14];
|
||||
let mut packet = PubackPacket::<1>::new();
|
||||
packet.packet_identifier = 35420;
|
||||
packet.reason_code = 0x00;
|
||||
let mut str = EncodedString::new();
|
||||
str.string = "Hello";
|
||||
str.len = 5;
|
||||
let mut props = Vec::<Property, 1>::new();
|
||||
props.push(Property::ReasonString(str));
|
||||
packet.property_len = packet.add_properties(&props);
|
||||
let res = packet.encode(&mut buffer, 14);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), 14);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
[0x40, 0x0C, 0x8A, 0x5C, 0x00, 0x08, 0x1F, 0x00, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
let buffer: [u8; 14] = [
|
||||
0x40, 0x0C, 0x8A, 0x5E, 0x15, 0x08, 0x1F, 0x00, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f,
|
||||
];
|
||||
let mut packet = PubackPacket::<1>::new();
|
||||
let res = packet.decode(&mut BuffReader::new(&buffer, 14));
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(packet.fixed_header, PacketType::Puback.into());
|
||||
assert_eq!(packet.remain_len, 12);
|
||||
assert_eq!(packet.packet_identifier, 35422);
|
||||
assert_eq!(packet.reason_code, 0x15);
|
||||
assert_eq!(packet.property_len, 8);
|
||||
let prop = packet.properties.get(0);
|
||||
assert!(prop.is_some());
|
||||
assert_eq!(<&Property as Into<u8>>::into(prop.unwrap()), 0x1F);
|
||||
if let Property::ReasonString(u) = (*prop.unwrap()).clone() {
|
||||
assert_eq!(u.string, "Hello");
|
||||
}
|
||||
}
|
||||
73
mqtt/src/tests/unit/packet/v5/pubcomp_packet_unit.rs
Normal file
73
mqtt/src/tests/unit/packet/v5/pubcomp_packet_unit.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::pubcomp_packet::PubcompPacket;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
use crate::utils::types::EncodedString;
|
||||
use heapless::Vec;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 14] = [0; 14];
|
||||
let mut packet = PubcompPacket::<1>::new();
|
||||
packet.fixed_header = PacketType::Pubcomp.into();
|
||||
packet.packet_identifier = 35420;
|
||||
packet.reason_code = 0x00;
|
||||
let mut str = EncodedString::new();
|
||||
str.string = "Wheel";
|
||||
str.len = 5;
|
||||
let mut props = Vec::<Property, 1>::new();
|
||||
props.push(Property::ReasonString(str));
|
||||
packet.property_len = packet.add_properties(&props);
|
||||
let res = packet.encode(&mut buffer, 14);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), 14);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
[0x70, 0x0C, 0x8A, 0x5C, 0x00, 0x08, 0x1F, 0x00, 0x05, 0x57, 0x68, 0x65, 0x65, 0x6c]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
let buffer: [u8; 14] = [
|
||||
0x70, 0x0C, 0x8A, 0x5C, 0x00, 0x08, 0x1F, 0x00, 0x05, 0x57, 0x68, 0x65, 0x65, 0x6c,
|
||||
];
|
||||
let mut packet = PubcompPacket::<1>::new();
|
||||
let res = packet.decode(&mut BuffReader::new(&buffer, 14));
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(packet.fixed_header, PacketType::Pubcomp.into());
|
||||
assert_eq!(packet.packet_identifier, 35420);
|
||||
assert_eq!(packet.reason_code, 0x00);
|
||||
assert_eq!(packet.property_len, 8);
|
||||
let prop = packet.properties.get(0);
|
||||
assert!(prop.is_some());
|
||||
assert_eq!(<&Property as Into<u8>>::into(prop.unwrap()), 0x1F);
|
||||
if let Property::ReasonString(u) = (*prop.unwrap()).clone() {
|
||||
assert_eq!(u.string, "Wheel");
|
||||
}
|
||||
}
|
||||
97
mqtt/src/tests/unit/packet/v5/publish_packet_unit.rs
Normal file
97
mqtt/src/tests/unit/packet/v5/publish_packet_unit.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::publish_packet::{PublishPacket, QualityOfService};
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
use crate::utils::types::EncodedString;
|
||||
use heapless::Vec;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 29] = [0; 29];
|
||||
let mut packet = PublishPacket::<2>::new();
|
||||
packet.fixed_header = PacketType::Publish.into();
|
||||
packet.add_qos(QualityOfService::QoS1);
|
||||
let mut topic = EncodedString::new();
|
||||
topic.string = "test";
|
||||
topic.len = 4;
|
||||
packet.topic_name = topic;
|
||||
packet.packet_identifier = 23432;
|
||||
let mut props = Vec::<Property, 2>::new();
|
||||
props.push(Property::PayloadFormat(0x01));
|
||||
props.push(Property::MessageExpiryInterval(45678));
|
||||
packet.property_len = packet.add_properties(&props);
|
||||
static MESSAGE: [u8; 11] = [
|
||||
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64,
|
||||
];
|
||||
packet.add_message(&MESSAGE);
|
||||
let res = packet.encode(&mut buffer, 100);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), 29);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
[
|
||||
0x32, 0x1B, 0x00, 0x04, 0x74, 0x65, 0x73, 0x74, 0x5B, 0x88, 0x07, 0x01, 0x01, 0x02,
|
||||
0x00, 0x00, 0xB2, 0x6E, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c,
|
||||
0x64
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
let buffer: [u8; 29] = [
|
||||
0x32, 0x1B, 0x00, 0x04, 0x74, 0x65, 0x73, 0x74, 0x5B, 0x88, 0x07, 0x01, 0x01, 0x02, 0x00,
|
||||
0x00, 0xB2, 0x6E, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64,
|
||||
];
|
||||
let mut packet = PublishPacket::<2>::new();
|
||||
let res = packet.decode(&mut BuffReader::new(&buffer, 29));
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(packet.fixed_header, 0x32);
|
||||
assert_eq!(packet.topic_name.len, 4);
|
||||
assert_eq!(packet.topic_name.string, "test");
|
||||
assert_eq!(packet.packet_identifier, 23432);
|
||||
assert_eq!(packet.property_len, 7);
|
||||
let prop = packet.properties.get(0);
|
||||
assert!(prop.is_some());
|
||||
assert_eq!(<&Property as Into<u8>>::into(prop.unwrap()), 0x01);
|
||||
if let Property::PayloadFormat(u) = (*prop.unwrap()).clone() {
|
||||
assert_eq!(u, 0x01);
|
||||
}
|
||||
let prop2 = packet.properties.get(1);
|
||||
assert!(prop2.is_some());
|
||||
assert_eq!(<&Property as Into<u8>>::into(prop2.unwrap()), 0x02);
|
||||
if let Property::MessageExpiryInterval(u) = (*prop2.unwrap()).clone() {
|
||||
assert_eq!(u, 45678);
|
||||
}
|
||||
if let Some(message) = packet.message {
|
||||
assert_eq!(
|
||||
*message,
|
||||
[0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]
|
||||
);
|
||||
}
|
||||
}
|
||||
86
mqtt/src/tests/unit/packet/v5/pubrec_packet_unit.rs
Normal file
86
mqtt/src/tests/unit/packet/v5/pubrec_packet_unit.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::pubrec_packet::PubrecPacket;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
use crate::utils::types::{EncodedString, StringPair};
|
||||
use heapless::Vec;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 20] = [0; 20];
|
||||
let mut packet = PubrecPacket::<2>::new();
|
||||
packet.packet_identifier = 35420;
|
||||
packet.reason_code = 0x12;
|
||||
let mut name = EncodedString::new();
|
||||
name.string = "name1";
|
||||
name.len = 5;
|
||||
let mut val = EncodedString::new();
|
||||
val.string = "val1";
|
||||
val.len = 4;
|
||||
let mut pair = StringPair::new();
|
||||
pair.name = name;
|
||||
pair.value = val;
|
||||
let mut props = Vec::<Property, 1>::new();
|
||||
props.push(Property::UserProperty(pair));
|
||||
packet.property_len = packet.add_properties(&props);
|
||||
let res = packet.encode(&mut buffer, 20);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), 20);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
[
|
||||
0x50, 0x12, 0x8A, 0x5C, 0x12, 0x0E, 0x26, 0x00, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x31,
|
||||
0x00, 0x04, 0x76, 0x61, 0x6c, 0x31
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
let buffer: [u8; 20] = [
|
||||
0x50, 0x12, 0x8A, 0x5C, 0x12, 0x0E, 0x26, 0x00, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x31, 0x00,
|
||||
0x04, 0x76, 0x61, 0x6c, 0x31,
|
||||
];
|
||||
let mut packet = PubrecPacket::<1>::new();
|
||||
let res = packet.decode(&mut BuffReader::new(&buffer, 20));
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(packet.fixed_header, PacketType::Pubrec.into());
|
||||
assert_eq!(packet.remain_len, 18);
|
||||
assert_eq!(packet.packet_identifier, 35420);
|
||||
assert_eq!(packet.reason_code, 0x12);
|
||||
assert_eq!(packet.property_len, 14);
|
||||
let prop = packet.properties.get(0);
|
||||
assert!(prop.is_some());
|
||||
assert_eq!(<&Property as Into<u8>>::into(prop.unwrap()), 0x26);
|
||||
if let Property::UserProperty(u) = (*prop.unwrap()).clone() {
|
||||
assert_eq!(u.name.len, 5);
|
||||
assert_eq!(u.name.string, "name1");
|
||||
assert_eq!(u.value.len, 4);
|
||||
assert_eq!(u.value.string, "val1");
|
||||
}
|
||||
}
|
||||
87
mqtt/src/tests/unit/packet/v5/pubrel_packet_unit.rs
Normal file
87
mqtt/src/tests/unit/packet/v5/pubrel_packet_unit.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::pubrel_packet::PubrelPacket;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
use crate::utils::types::{EncodedString, StringPair};
|
||||
use heapless::Vec;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 21] = [0; 21];
|
||||
let mut packet = PubrelPacket::<1>::new();
|
||||
packet.fixed_header = PacketType::Pubrel.into();
|
||||
packet.packet_identifier = 12345;
|
||||
packet.reason_code = 0x86;
|
||||
let mut name = EncodedString::new();
|
||||
name.string = "haha";
|
||||
name.len = 4;
|
||||
let mut val = EncodedString::new();
|
||||
val.string = "hehe89";
|
||||
val.len = 6;
|
||||
let mut pair = StringPair::new();
|
||||
pair.name = name;
|
||||
pair.value = val;
|
||||
let mut props = Vec::<Property, 1>::new();
|
||||
props.push(Property::UserProperty(pair));
|
||||
packet.property_len = packet.add_properties(&props);
|
||||
let res = packet.encode(&mut buffer, 21);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), 21);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
[
|
||||
0x60, 0x13, 0x30, 0x39, 0x86, 0x0F, 0x26, 0x00, 0x04, 0x68, 0x61, 0x68, 0x61, 0x00,
|
||||
0x06, 0x68, 0x65, 0x68, 0x65, 0x38, 0x39
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
let buffer: [u8; 21] = [
|
||||
0x60, 0x13, 0x30, 0x39, 0x86, 0x0F, 0x26, 0x00, 0x04, 0x68, 0x61, 0x68, 0x61, 0x00, 0x06,
|
||||
0x68, 0x65, 0x68, 0x65, 0x38, 0x39,
|
||||
];
|
||||
let mut packet = PubrelPacket::<1>::new();
|
||||
let res = packet.decode(&mut BuffReader::new(&buffer, 21));
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(packet.fixed_header, PacketType::Pubrel.into());
|
||||
assert_eq!(packet.remain_len, 19);
|
||||
assert_eq!(packet.packet_identifier, 12345);
|
||||
assert_eq!(packet.reason_code, 0x86);
|
||||
assert_eq!(packet.property_len, 15);
|
||||
let prop = packet.properties.get(0);
|
||||
assert!(prop.is_some());
|
||||
assert_eq!(<&Property as Into<u8>>::into(prop.unwrap()), 0x26);
|
||||
if let Property::UserProperty(u) = (*prop.unwrap()).clone() {
|
||||
assert_eq!(u.name.len, 4);
|
||||
assert_eq!(u.name.string, "haha");
|
||||
assert_eq!(u.value.len, 6);
|
||||
assert_eq!(u.value.string, "hehe89");
|
||||
}
|
||||
}
|
||||
67
mqtt/src/tests/unit/packet/v5/suback_packet_unit.rs
Normal file
67
mqtt/src/tests/unit/packet/v5/suback_packet_unit.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::suback_packet::SubackPacket;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
let buffer: [u8; 23] = [
|
||||
0x90, 0x15, 0xCC, 0x08, 0x0F, 0x1F, 0x00, 0x0C, 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x53,
|
||||
0x74, 0x72, 0x69, 0x6e, 0x67, 0x12, 0x34, 0x56,
|
||||
];
|
||||
let mut packet = SubackPacket::<3, 1>::new();
|
||||
let res = packet.decode(&mut BuffReader::new(&buffer, 23));
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(packet.fixed_header, PacketType::Suback.into());
|
||||
assert_eq!(packet.remain_len, 21);
|
||||
assert_eq!(packet.packet_identifier, 52232);
|
||||
assert_eq!(packet.property_len, 15);
|
||||
let prop = packet.properties.get(0);
|
||||
assert!(prop.is_some());
|
||||
assert_eq!(<&Property as Into<u8>>::into(prop.unwrap()), 0x1F);
|
||||
if let Property::ReasonString(u) = (*prop.unwrap()).clone() {
|
||||
assert_eq!(u.len, 12);
|
||||
assert_eq!(u.string, "reasonString");
|
||||
}
|
||||
assert_eq!(packet.reason_codes.len(), 3);
|
||||
let res1 = packet.reason_codes.get(0);
|
||||
assert!(res1.is_some());
|
||||
if let Some(r) = res1 {
|
||||
assert_eq!(*r, 0x12);
|
||||
}
|
||||
let res2 = packet.reason_codes.get(1);
|
||||
assert!(res2.is_some());
|
||||
if let Some(r) = res2 {
|
||||
assert_eq!(*r, 0x34);
|
||||
}
|
||||
let res3 = packet.reason_codes.get(2);
|
||||
assert!(res3.is_some());
|
||||
if let Some(r) = res3 {
|
||||
assert_eq!(*r, 0x56);
|
||||
}
|
||||
}
|
||||
54
mqtt/src/tests/unit/packet/v5/subscription_packet_unit.rs
Normal file
54
mqtt/src/tests/unit/packet/v5/subscription_packet_unit.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::publish_packet::QualityOfService::{QoS0, QoS1};
|
||||
use crate::packet::v5::subscription_packet::SubscriptionPacket;
|
||||
use heapless::Vec;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 30] = [0; 30];
|
||||
let mut packet = SubscriptionPacket::<2, 1>::new();
|
||||
packet.fixed_header = PacketType::Subscribe.into();
|
||||
packet.packet_identifier = 5432;
|
||||
let mut props = Vec::<Property, 2>::new();
|
||||
props.push(Property::SubscriptionIdentifier(2432));
|
||||
packet.property_len = packet.add_properties(&props);
|
||||
packet.add_new_filter("test/topic", QoS0);
|
||||
packet.add_new_filter("hehe/#", QoS1);
|
||||
let res = packet.encode(&mut buffer, 30);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), 30);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
[
|
||||
0x82, 0x1C, 0x15, 0x38, 0x03, 0x0B, 0x80, 0x13, 0x00, 0x0A, 0x74, 0x65, 0x73, 0x74,
|
||||
0x2f, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x00, 0x00, 0x06, 0x68, 0x65, 0x68, 0x65, 0x2F,
|
||||
0x23, 0x01
|
||||
]
|
||||
);
|
||||
}
|
||||
62
mqtt/src/tests/unit/packet/v5/unsuback_packet_unit.rs
Normal file
62
mqtt/src/tests/unit/packet/v5/unsuback_packet_unit.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::unsuback_packet::UnsubackPacket;
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
|
||||
#[test]
|
||||
fn test_decode() {
|
||||
let buffer: [u8; 22] = [
|
||||
0xB0, 0x14, 0xCC, 0x08, 0x0F, 0x1F, 0x00, 0x0C, 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x53,
|
||||
0x74, 0x72, 0x69, 0x6e, 0x67, 0x77, 0x55,
|
||||
];
|
||||
let mut packet = UnsubackPacket::<2, 1>::new();
|
||||
let res = packet.decode(&mut BuffReader::new(&buffer, 22));
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(packet.fixed_header, PacketType::Unsuback.into());
|
||||
assert_eq!(packet.remain_len, 20);
|
||||
assert_eq!(packet.packet_identifier, 52232);
|
||||
assert_eq!(packet.property_len, 15);
|
||||
let prop = packet.properties.get(0);
|
||||
assert!(prop.is_some());
|
||||
assert_eq!(<&Property as Into<u8>>::into(prop.unwrap()), 0x1F);
|
||||
if let Property::ReasonString(u) = (*prop.unwrap()).clone() {
|
||||
assert_eq!(u.len, 12);
|
||||
assert_eq!(u.string, "reasonString");
|
||||
}
|
||||
assert_eq!(packet.reason_codes.len(), 2);
|
||||
let res1 = packet.reason_codes.get(0);
|
||||
assert!(res1.is_some());
|
||||
if let Some(r) = res1 {
|
||||
assert_eq!(*r, 0x77);
|
||||
}
|
||||
let res2 = packet.reason_codes.get(1);
|
||||
assert!(res2.is_some());
|
||||
if let Some(r) = res2 {
|
||||
assert_eq!(*r, 0x55);
|
||||
}
|
||||
}
|
||||
64
mqtt/src/tests/unit/packet/v5/unsubscription_packet_unit.rs
Normal file
64
mqtt/src/tests/unit/packet/v5/unsubscription_packet_unit.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::mqtt_packet::Packet;
|
||||
use crate::packet::v5::packet_type::PacketType;
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::packet::v5::publish_packet::QualityOfService::{QoS0, QoS1};
|
||||
use crate::packet::v5::unsubscription_packet::UnsubscriptionPacket;
|
||||
use crate::utils::types::{EncodedString, StringPair};
|
||||
use heapless::Vec;
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
let mut buffer: [u8; 40] = [0; 40];
|
||||
let mut packet = UnsubscriptionPacket::<2, 1>::new();
|
||||
packet.fixed_header = PacketType::Unsubscribe.into();
|
||||
packet.packet_identifier = 5432;
|
||||
let mut name = EncodedString::new();
|
||||
name.string = "haha";
|
||||
name.len = 4;
|
||||
let mut val = EncodedString::new();
|
||||
val.string = "hehe89";
|
||||
val.len = 6;
|
||||
let mut pair = StringPair::new();
|
||||
pair.name = name;
|
||||
pair.value = val;
|
||||
let mut props = Vec::<Property, 1>::new();
|
||||
props.push(Property::UserProperty(pair));
|
||||
packet.property_len = packet.add_properties(&props);
|
||||
packet.add_new_filter("test/topic", QoS0);
|
||||
packet.add_new_filter("hehe/#", QoS1);
|
||||
let res = packet.encode(&mut buffer, 40);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), 40);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
[
|
||||
0xA0, 0x26, 0x15, 0x38, 0x0F, 0x26, 0x00, 0x04, 0x68, 0x61, 0x68, 0x61, 0x00, 0x06,
|
||||
0x68, 0x65, 0x68, 0x65, 0x38, 0x39, 0x00, 0x0A, 0x74, 0x65, 0x73, 0x74, 0x2F, 0x74,
|
||||
0x6F, 0x70, 0x69, 0x63, 0x00, 0x06, 0x68, 0x65, 0x68, 0x65, 0x2F, 0x23
|
||||
]
|
||||
);
|
||||
}
|
||||
235
mqtt/src/tests/unit/utils/buffer_reader_unit.rs
Normal file
235
mqtt/src/tests/unit/utils/buffer_reader_unit.rs
Normal file
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::utils::buffer_reader::BuffReader;
|
||||
use crate::utils::types::BufferError;
|
||||
|
||||
#[test]
|
||||
fn buffer_read_variable_byte() {
|
||||
static BUFFER: [u8; 5] = [0x82, 0x82, 0x03, 0x85, 0x84];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 5);
|
||||
let test_number = reader.read_variable_byte_int();
|
||||
assert!(test_number.is_ok());
|
||||
assert_eq!(reader.position, 3);
|
||||
assert_eq!(test_number.unwrap(), 49410);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_read_invalid_size() {
|
||||
static BUFFER: [u8; 2] = [0x82, 0x82];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 2);
|
||||
let test_number = reader.read_variable_byte_int();
|
||||
assert!(test_number.is_err());
|
||||
assert_eq!(
|
||||
test_number.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_smaller_var_int() {
|
||||
static BUFFER: [u8; 2] = [0x82, 0x02];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 2);
|
||||
let test_number = reader.read_variable_byte_int();
|
||||
assert!(test_number.is_ok());
|
||||
assert_eq!(reader.position, 2);
|
||||
assert_eq!(test_number.unwrap(), 258);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_complete_var_int() {
|
||||
static BUFFER: [u8; 4] = [0x81, 0x81, 0x81, 0x01];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 4);
|
||||
let test_number = reader.read_variable_byte_int();
|
||||
assert!(test_number.is_ok());
|
||||
assert_eq!(reader.position, 4);
|
||||
assert_eq!(test_number.unwrap(), 2113665);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_var_empty_buffer() {
|
||||
static BUFFER: [u8; 0] = [];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 0);
|
||||
let test_number = reader.read_variable_byte_int();
|
||||
assert!(test_number.is_err());
|
||||
assert_eq!(
|
||||
test_number.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_u32() {
|
||||
static BUFFER: [u8; 4] = [0x00, 0x02, 0x5E, 0xC1];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 4);
|
||||
let test_number = reader.read_u32();
|
||||
assert!(test_number.is_ok());
|
||||
assert_eq!(test_number.unwrap(), 155329);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_u32_oob() {
|
||||
static BUFFER: [u8; 3] = [0x00, 0x02, 0x5E];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 3);
|
||||
let test_number = reader.read_u32();
|
||||
assert!(test_number.is_err());
|
||||
assert_eq!(
|
||||
test_number.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_u16() {
|
||||
static BUFFER: [u8; 2] = [0x48, 0x5F];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 2);
|
||||
let test_number = reader.read_u16();
|
||||
assert!(test_number.is_ok());
|
||||
assert_eq!(test_number.unwrap(), 18527);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_u16_oob() {
|
||||
static BUFFER: [u8; 1] = [0x5E];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 1);
|
||||
let test_number = reader.read_u16();
|
||||
assert!(test_number.is_err());
|
||||
assert_eq!(
|
||||
test_number.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_u8() {
|
||||
static BUFFER: [u8; 1] = [0xFD];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 1);
|
||||
let test_number = reader.read_u8();
|
||||
assert!(test_number.is_ok());
|
||||
assert_eq!(test_number.unwrap(), 253);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_u8_oob() {
|
||||
static BUFFER: [u8; 0] = [];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 0);
|
||||
let test_number = reader.read_u8();
|
||||
assert!(test_number.is_err());
|
||||
assert_eq!(
|
||||
test_number.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_string() {
|
||||
static BUFFER: [u8; 6] = [0x00, 0x04, 0xF0, 0x9F, 0x92, 0x96];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 6);
|
||||
let test_string = reader.read_string();
|
||||
assert!(test_string.is_ok());
|
||||
let unw = test_string.unwrap();
|
||||
assert_eq!(unw.string, "💖");
|
||||
assert_eq!(unw.len, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_string_utf8_wrong() {
|
||||
static BUFFER: [u8; 5] = [0x00, 0x03, 0xF0, 0x9F, 0x92];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 5);
|
||||
let test_string = reader.read_string();
|
||||
assert!(test_string.is_err());
|
||||
assert_eq!(test_string.unwrap_err(), BufferError::Utf8Error);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_string_oob() {
|
||||
static BUFFER: [u8; 5] = [0x00, 0x04, 0xF0, 0x9F, 0x92];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 5);
|
||||
let test_string = reader.read_string();
|
||||
assert!(test_string.is_err());
|
||||
assert_eq!(
|
||||
test_string.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_binary() {
|
||||
static BUFFER: [u8; 6] = [0x00, 0x04, 0xFF, 0xEE, 0xDD, 0xCC];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 6);
|
||||
let test_bin = reader.read_binary();
|
||||
assert!(test_bin.is_ok());
|
||||
let unw = test_bin.unwrap();
|
||||
assert_eq!(unw.bin, [0xFF, 0xEE, 0xDD, 0xCC]);
|
||||
assert_eq!(unw.len, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_binary_oob() {
|
||||
static BUFFER: [u8; 5] = [0x00, 0x04, 0xFF, 0xEE, 0xDD];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 5);
|
||||
let test_bin = reader.read_binary();
|
||||
assert!(test_bin.is_err());
|
||||
assert_eq!(test_bin.unwrap_err(), BufferError::InsufficientBufferSize);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_string_pair() {
|
||||
static BUFFER: [u8; 11] = [
|
||||
0x00, 0x04, 0xF0, 0x9F, 0x98, 0x8E, 0x00, 0x03, 0xE2, 0x93, 0x87,
|
||||
];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 11);
|
||||
let string_pair = reader.read_string_pair();
|
||||
assert!(string_pair.is_ok());
|
||||
let unw = string_pair.unwrap();
|
||||
assert_eq!(unw.name.string, "😎");
|
||||
assert_eq!(unw.name.len, 4);
|
||||
assert_eq!(unw.value.string, "Ⓡ");
|
||||
assert_eq!(unw.value.len, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_string_pair_wrong_utf8() {
|
||||
static BUFFER: [u8; 11] = [
|
||||
0x00, 0x03, 0xF0, 0x9F, 0x92, 0x00, 0x04, 0xF0, 0x9F, 0x98, 0x8E,
|
||||
];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 11);
|
||||
let string_pair = reader.read_string_pair();
|
||||
assert!(string_pair.is_err());
|
||||
assert_eq!(string_pair.unwrap_err(), BufferError::Utf8Error)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_string_pair_oob() {
|
||||
static BUFFER: [u8; 11] = [
|
||||
0x00, 0x04, 0xF0, 0x9F, 0x98, 0x8E, 0x00, 0x04, 0xE2, 0x93, 0x87,
|
||||
];
|
||||
let mut reader: BuffReader = BuffReader::new(&BUFFER, 11);
|
||||
let string_pair = reader.read_string_pair();
|
||||
assert!(string_pair.is_err());
|
||||
assert_eq!(
|
||||
string_pair.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
381
mqtt/src/tests/unit/utils/buffer_writer_unit.rs
Normal file
381
mqtt/src/tests/unit/utils/buffer_writer_unit.rs
Normal file
@@ -0,0 +1,381 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
use crate::packet::v5::property::Property;
|
||||
use crate::utils::buffer_writer::BuffWriter;
|
||||
use crate::utils::types::{BinaryData, BufferError, EncodedString, StringPair, TopicFilter};
|
||||
use heapless::Vec;
|
||||
|
||||
#[test]
|
||||
fn buffer_write_ref() {
|
||||
static BUFFER: [u8; 5] = [0x82, 0x82, 0x03, 0x85, 0x84];
|
||||
let mut res_buffer: [u8; 5] = [0; 5];
|
||||
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 5);
|
||||
let test_write = writer.insert_ref(5, &BUFFER);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 5);
|
||||
assert_eq!(BUFFER, res_buffer);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_ref_oob() {
|
||||
static BUFFER: [u8; 5] = [0x82, 0x82, 0x03, 0x85, 0x84];
|
||||
let mut res_buffer: [u8; 4] = [0; 4];
|
||||
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 4);
|
||||
let test_number = writer.insert_ref(5, &BUFFER);
|
||||
assert!(test_number.is_err());
|
||||
assert_eq!(
|
||||
test_number.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
assert_eq!(res_buffer, [0; 4])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_u8() {
|
||||
let mut res_buffer: [u8; 1] = [0; 1];
|
||||
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 1);
|
||||
let test_write = writer.write_u8(0xFA);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 1);
|
||||
assert_eq!(res_buffer, [0xFA]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_u8_oob() {
|
||||
let mut res_buffer: [u8; 0] = [];
|
||||
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 0);
|
||||
let test_number = writer.write_u8(0xFA);
|
||||
assert!(test_number.is_err());
|
||||
assert_eq!(
|
||||
test_number.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_u16() {
|
||||
let mut res_buffer: [u8; 2] = [0; 2];
|
||||
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 2);
|
||||
let test_write = writer.write_u16(0xFAED);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 2);
|
||||
assert_eq!(res_buffer, [0xFA, 0xED]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_u16_oob() {
|
||||
let mut res_buffer: [u8; 1] = [0; 1];
|
||||
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 1);
|
||||
let test_number = writer.write_u16(0xFAED);
|
||||
assert!(test_number.is_err());
|
||||
assert_eq!(
|
||||
test_number.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_u32() {
|
||||
let mut res_buffer: [u8; 4] = [0; 4];
|
||||
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 4);
|
||||
let test_write = writer.write_u32(0xFAEDCC09);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 4);
|
||||
assert_eq!(res_buffer, [0xFA, 0xED, 0xCC, 0x09]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_u32_oob() {
|
||||
let mut res_buffer: [u8; 3] = [0; 3];
|
||||
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 3);
|
||||
let test_number = writer.write_u32(0xFAEDCC08);
|
||||
assert!(test_number.is_err());
|
||||
assert_eq!(
|
||||
test_number.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_string() {
|
||||
let mut res_buffer: [u8; 6] = [0; 6];
|
||||
let mut string = EncodedString::new();
|
||||
string.string = "😎";
|
||||
string.len = 4;
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 6);
|
||||
let test_write = writer.write_string_ref(&string);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 6);
|
||||
assert_eq!(res_buffer, [0x00, 0x04, 0xF0, 0x9F, 0x98, 0x8E]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_string_oob() {
|
||||
let mut res_buffer: [u8; 5] = [0; 5];
|
||||
let mut string = EncodedString::new();
|
||||
string.string = "😎";
|
||||
string.len = 4;
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 5);
|
||||
let test_write = writer.write_string_ref(&string);
|
||||
assert!(test_write.is_err());
|
||||
assert_eq!(test_write.unwrap_err(), BufferError::InsufficientBufferSize);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_bin() {
|
||||
let mut res_buffer: [u8; 6] = [0; 6];
|
||||
let mut bin = BinaryData::new();
|
||||
bin.bin = &[0xAB, 0xEF, 0x88, 0x43];
|
||||
bin.len = 4;
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 6);
|
||||
let test_write = writer.write_binary_ref(&bin);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 6);
|
||||
assert_eq!(res_buffer, [0x00, 0x04, 0xAB, 0xEF, 0x88, 0x43]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_bin_oob() {
|
||||
let mut res_buffer: [u8; 6] = [0; 6];
|
||||
let mut bin = BinaryData::new();
|
||||
bin.bin = &[0xAB, 0xEF, 0x88, 0x43];
|
||||
bin.len = 4;
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 5);
|
||||
let test_write = writer.write_binary_ref(&bin);
|
||||
assert!(test_write.is_err());
|
||||
assert_eq!(test_write.unwrap_err(), BufferError::InsufficientBufferSize);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_string_pair() {
|
||||
let mut res_buffer: [u8; 12] = [0; 12];
|
||||
let mut name = EncodedString::new();
|
||||
name.string = "Name";
|
||||
name.len = 4;
|
||||
|
||||
let mut value = EncodedString::new();
|
||||
value.string = "😎";
|
||||
value.len = 4;
|
||||
let mut pair = StringPair::new();
|
||||
pair.name = name;
|
||||
pair.value = value;
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 12);
|
||||
let test_write = writer.write_string_pair_ref(&pair);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 12);
|
||||
assert_eq!(
|
||||
res_buffer,
|
||||
[0x00, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x00, 0x04, 0xF0, 0x9F, 0x98, 0x8E]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_string_pair_oob() {
|
||||
let mut res_buffer: [u8; 12] = [0; 12];
|
||||
let mut name = EncodedString::new();
|
||||
name.string = "Name";
|
||||
name.len = 4;
|
||||
|
||||
let mut value = EncodedString::new();
|
||||
value.string = "😎";
|
||||
value.len = 4;
|
||||
let mut pair = StringPair::new();
|
||||
pair.name = name;
|
||||
pair.value = value;
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 10);
|
||||
let test_write = writer.write_string_pair_ref(&pair);
|
||||
assert!(test_write.is_err());
|
||||
assert_eq!(test_write.unwrap_err(), BufferError::InsufficientBufferSize)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_var_byte() {
|
||||
let mut res_buffer: [u8; 2] = [0; 2];
|
||||
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 2);
|
||||
let test_write = writer.write_variable_byte_int(512);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 2);
|
||||
assert_eq!(res_buffer, [0x80, 0x04]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_var_byte_oob() {
|
||||
let mut res_buffer: [u8; 2] = [0; 2];
|
||||
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 2);
|
||||
let test_number = writer.write_variable_byte_int(453123);
|
||||
assert!(test_number.is_err());
|
||||
assert_eq!(
|
||||
test_number.unwrap_err(),
|
||||
BufferError::InsufficientBufferSize
|
||||
);
|
||||
}
|
||||
|
||||
/*#[test]
|
||||
fn buffer_write_property() {
|
||||
let mut res_buffer: [u8; 7] = [0; 7];
|
||||
let mut topic = EncodedString::new();
|
||||
topic.string = "Name";
|
||||
topic.len = 4;
|
||||
let prop = Property::ResponseTopic(topic);
|
||||
let mut writer: BuffWriter = BuffWriter::new(& mut res_buffer, 7);
|
||||
let test_write = writer.write_property(&prop);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 7);
|
||||
assert_eq!(res_buffer, [0x08, 0x00, 0x04, 0x4e, 0x61, 0x6d, 0x65]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_property_oob() {
|
||||
let mut res_buffer: [u8; 7] = [0; 7];
|
||||
let mut topic = EncodedString::new();
|
||||
topic.string = "Name";
|
||||
topic.len = 4;
|
||||
let prop = Property::ResponseTopic(topic);
|
||||
let mut writer: BuffWriter = BuffWriter::new(& mut res_buffer, 4);
|
||||
let test_write = writer.write_property(&prop);
|
||||
assert!(test_write.is_err());
|
||||
assert_eq!(test_write.unwrap_err(), BufferError::InsufficientBufferSize);
|
||||
}*/
|
||||
|
||||
#[test]
|
||||
fn buffer_write_properties() {
|
||||
let mut res_buffer: [u8; 13] = [0; 13];
|
||||
let mut topic = EncodedString::new();
|
||||
topic.string = "Name";
|
||||
topic.len = 4;
|
||||
let prop = Property::ResponseTopic(topic);
|
||||
let mut corr = BinaryData::new();
|
||||
corr.bin = &[0x12, 0x34, 0x56];
|
||||
corr.len = 3;
|
||||
let prop2 = Property::CorrelationData(corr);
|
||||
let mut properties = Vec::<Property, 2>::new();
|
||||
properties.push(prop);
|
||||
properties.push(prop2);
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 13);
|
||||
let test_write = writer.write_properties(&properties);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 13);
|
||||
assert_eq!(
|
||||
res_buffer,
|
||||
[0x08, 0x00, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x09, 0x00, 0x03, 0x12, 0x34, 0x56]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_properties_oob() {
|
||||
let mut res_buffer: [u8; 10] = [0; 10];
|
||||
let mut topic = EncodedString::new();
|
||||
topic.string = "Name";
|
||||
topic.len = 4;
|
||||
let prop = Property::ResponseTopic(topic);
|
||||
let mut corr = BinaryData::new();
|
||||
corr.bin = &[0x12, 0x34, 0x56];
|
||||
corr.len = 3;
|
||||
let prop2 = Property::CorrelationData(corr);
|
||||
let mut properties = Vec::<Property, 2>::new();
|
||||
properties.push(prop);
|
||||
properties.push(prop2);
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 10);
|
||||
let test_write = writer.write_properties(&properties);
|
||||
assert!(test_write.is_err());
|
||||
assert_eq!(test_write.unwrap_err(), BufferError::InsufficientBufferSize);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_filters() {
|
||||
let mut res_buffer: [u8; 15] = [0; 15];
|
||||
static STR1: &str = "test";
|
||||
static STR2: &str = "topic";
|
||||
let mut topic = EncodedString::new();
|
||||
topic.string = STR1;
|
||||
topic.len = 4;
|
||||
|
||||
let mut topic2 = EncodedString::new();
|
||||
topic2.string = STR2;
|
||||
topic2.len = 5;
|
||||
|
||||
let mut filter1 = TopicFilter::new();
|
||||
filter1.filter = topic;
|
||||
filter1.sub_options = 0xAE;
|
||||
|
||||
let mut filter2 = TopicFilter::new();
|
||||
filter2.filter = topic2;
|
||||
filter2.sub_options = 0x22;
|
||||
|
||||
let mut filters = Vec::<TopicFilter, 2>::new();
|
||||
filters.push(filter1);
|
||||
filters.push(filter2);
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 15);
|
||||
let test_write = writer.write_topic_filters_ref(true, 2, &filters);
|
||||
assert!(test_write.is_ok());
|
||||
assert_eq!(writer.position, 15);
|
||||
assert_eq!(
|
||||
res_buffer,
|
||||
[
|
||||
0x00, 0x04, 0x74, 0x65, 0x73, 0x74, 0xAE, 0x00, 0x05, 0x74, 0x6f, 0x70, 0x69, 0x63,
|
||||
0x22
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_write_filters_oob() {
|
||||
let mut res_buffer: [u8; 15] = [0; 15];
|
||||
static STR1: &str = "test";
|
||||
static STR2: &str = "topic";
|
||||
let mut topic = EncodedString::new();
|
||||
topic.string = STR1;
|
||||
topic.len = 4;
|
||||
|
||||
let mut topic2 = EncodedString::new();
|
||||
topic2.string = STR2;
|
||||
topic2.len = 5;
|
||||
|
||||
let mut filter1 = TopicFilter::new();
|
||||
filter1.filter = topic;
|
||||
filter1.sub_options = 0xAE;
|
||||
|
||||
let mut filter2 = TopicFilter::new();
|
||||
filter2.filter = topic2;
|
||||
filter2.sub_options = 0x22;
|
||||
|
||||
let mut filters = Vec::<TopicFilter, 2>::new();
|
||||
filters.push(filter1);
|
||||
filters.push(filter2);
|
||||
let mut writer: BuffWriter = BuffWriter::new(&mut res_buffer, 5);
|
||||
let test_write = writer.write_topic_filters_ref(true, 2, &filters);
|
||||
assert!(test_write.is_err());
|
||||
assert_eq!(test_write.unwrap_err(), BufferError::InsufficientBufferSize)
|
||||
}
|
||||
26
mqtt/src/tests/unit/utils/mod.rs
Normal file
26
mqtt/src/tests/unit/utils/mod.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
pub mod buffer_reader_unit;
|
||||
pub mod buffer_writer_unit;
|
||||
Reference in New Issue
Block a user