From b58e8318b61671463c8bf019d45e3894a0352619 Mon Sep 17 00:00:00 2001 From: obabec Date: Wed, 16 Mar 2022 11:38:30 +0100 Subject: [PATCH] Move tests and latest rust (#8) * Move tests and latest rust --- .github/workflows/integration_tests.yaml | 7 +- .github/workflows/unit_tests.yaml | 5 +- mqtt/Cargo.toml | 6 +- mqtt/src/client/client_v5.rs | 18 ++--- mqtt/src/lib.rs | 1 - mqtt/src/network/mod.rs | 43 +++++++++++- mqtt/src/network/network_trait.rs | 66 ------------------- mqtt/src/packet/v5/property.rs | 4 +- mqtt/src/tests/integration/mod.rs | 24 ------- mqtt/src/tests/mod.rs | 5 -- mqtt/src/tokio_net/tokio_network.rs | 12 ++-- .../integration_test_single.rs | 40 +++++------ rust-toolchain.toml | 2 +- 13 files changed, 89 insertions(+), 144 deletions(-) delete mode 100644 mqtt/src/network/network_trait.rs delete mode 100644 mqtt/src/tests/integration/mod.rs rename mqtt/{src/tests/integration => tests}/integration_test_single.rs (91%) diff --git a/.github/workflows/integration_tests.yaml b/.github/workflows/integration_tests.yaml index f336014..6308484 100644 --- a/.github/workflows/integration_tests.yaml +++ b/.github/workflows/integration_tests.yaml @@ -10,7 +10,8 @@ jobs: - name: Git checkout uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 with: toolchain: stable @@ -22,5 +23,5 @@ jobs: sudo apt-get install mosquitto mosquitto -c .ci/mosquitto.conf -d - - name: Run integration tests - run: RUST_LOG=trace cargo test integration --features "testing" \ No newline at end of file + - name: Run integration-tests tests + run: RUST_LOG=trace cargo test integration \ No newline at end of file diff --git a/.github/workflows/unit_tests.yaml b/.github/workflows/unit_tests.yaml index babda5a..03f8c04 100644 --- a/.github/workflows/unit_tests.yaml +++ b/.github/workflows/unit_tests.yaml @@ -9,9 +9,10 @@ jobs: steps: - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 with: toolchain: stable - name: Run Unit tests - run: RUST_LOG=trace cargo test unit --features "testing" \ No newline at end of file + run: RUST_LOG=trace cargo test unit \ No newline at end of file diff --git a/mqtt/Cargo.toml b/mqtt/Cargo.toml index 7d4912c..18be351 100644 --- a/mqtt/Cargo.toml +++ b/mqtt/Cargo.toml @@ -11,7 +11,6 @@ resolver = "2" heapless = "0.7.10" rand_core = "0.6.0" defmt = { version = "0.3", optional = true } -futures = { version = "0.3.21", optional = true, default-features = false} log = { version = "0.4.14", optional = true } tokio = { version = "1", features = ["full"], optional = true, default-features = false } @@ -23,7 +22,6 @@ futures = { version = "0.3.21" } log = { version = "0.4.14"} [features] -default = ["testing"] -testing = ["tokio", "std", "log", "futures"] -std = [] +default = ["std"] +std = ["tokio", "log"] no_std = ["defmt"] \ No newline at end of file diff --git a/mqtt/src/client/client_v5.rs b/mqtt/src/client/client_v5.rs index 30f2b0b..34052ab 100644 --- a/mqtt/src/client/client_v5.rs +++ b/mqtt/src/client/client_v5.rs @@ -23,7 +23,7 @@ */ use crate::client::client_config::ClientConfig; -use crate::network::network_trait::NetworkConnection; +use crate::network::NetworkConnection; use crate::packet::v5::connack_packet::ConnackPacket; use crate::packet::v5::connect_packet::ConnectPacket; use crate::packet::v5::disconnect_packet::DisconnectPacket; @@ -42,7 +42,7 @@ use crate::utils::types::BufferError; use heapless::Vec; use rand_core::RngCore; -use crate::network::network_trait::NetworkError::Connection; +use crate::network::NetworkError::Connection; pub struct MqttClientV5<'a, T, const MAX_PROPERTIES: usize> { connection: Option, @@ -101,7 +101,7 @@ where } let mut conn = self.connection.as_mut().unwrap(); trace!("Sending connect"); - conn.send(self.buffer, len.unwrap()).await?; + conn.send(&self.buffer[0..len.unwrap()]).await?; //connack let reason: Result = { @@ -151,7 +151,7 @@ where return Err(ReasonCode::BuffError); } - if let Err(e) = conn.send(self.buffer, len.unwrap()).await { + if let Err(e) = conn.send(&self.buffer[0..len.unwrap()]).await { warn!("Could not send DISCONNECT packet"); } @@ -188,7 +188,7 @@ where return Err(ReasonCode::BuffError); } trace!("Sending message"); - conn.send(self.buffer, len.unwrap()).await?; + conn.send(&self.buffer[0..len.unwrap()]).await?; // QoS1 if >::into(self.config.qos) @@ -249,7 +249,7 @@ where return Err(ReasonCode::BuffError); } - conn.send(self.buffer, len.unwrap()).await?; + conn.send(&self.buffer[0..len.unwrap()]).await?; let reason: Result, BufferError> = { conn.receive(self.recv_buffer).await?; @@ -299,7 +299,7 @@ where return Err(ReasonCode::BuffError); } - conn.send(self.buffer, len.unwrap()).await?; + conn.send(&self.buffer[0..len.unwrap()]).await?; let reason: Result = { conn.receive(self.recv_buffer).await?; @@ -361,7 +361,7 @@ where error!("[DECODE ERR]: {}", err); return Err(ReasonCode::BuffError); } - conn.send(self.buffer, len.unwrap()).await?; + conn.send(&self.buffer[0..len.unwrap()]).await?; } } @@ -383,7 +383,7 @@ where return Err(ReasonCode::BuffError); } - conn.send(self.buffer, len.unwrap()).await?; + conn.send(&self.buffer[0..len.unwrap()]).await?; conn.receive(self.recv_buffer).await?; let mut packet = PingrespPacket::new(); diff --git a/mqtt/src/lib.rs b/mqtt/src/lib.rs index 6c86ad1..d85d830 100644 --- a/mqtt/src/lib.rs +++ b/mqtt/src/lib.rs @@ -22,7 +22,6 @@ * SOFTWARE. */ -#![feature(in_band_lifetimes)] #![macro_use] #![cfg_attr(not(feature = "std"), no_std)] #![allow(dead_code)] diff --git a/mqtt/src/network/mod.rs b/mqtt/src/network/mod.rs index d1f654d..c6d552d 100644 --- a/mqtt/src/network/mod.rs +++ b/mqtt/src/network/mod.rs @@ -22,4 +22,45 @@ * SOFTWARE. */ -pub mod network_trait; +use core::future::Future; + +use crate::packet::v5::reason_codes::ReasonCode; + +#[derive(Debug)] +pub enum NetworkError { + Connection, + Unknown, + QoSAck, + IDNotMatchedOnAck, + NoMatchingSubs, +} + +pub trait NetworkConnectionFactory: Sized { + type Connection: NetworkConnection; + + type ConnectionFuture<'m>: Future> + where + Self: 'm; + + fn connect<'m>(&'m mut self, ip: [u8; 4], port: u16) -> Self::ConnectionFuture<'m>; +} + +pub trait NetworkConnection { + type SendFuture<'m>: Future> + where + Self: 'm; + + type ReceiveFuture<'m>: Future> + where + Self: 'm; + + type CloseFuture<'m>: Future> + where + Self: 'm; + + fn send<'m>(&'m mut self, buffer: &'m [u8]) -> Self::SendFuture<'m>; + + fn receive<'m>(&'m mut self, buffer: &'m mut [u8]) -> Self::ReceiveFuture<'m>; + + fn close<'m>(self) -> Self::CloseFuture<'m>; +} diff --git a/mqtt/src/network/network_trait.rs b/mqtt/src/network/network_trait.rs deleted file mode 100644 index 0dae540..0000000 --- a/mqtt/src/network/network_trait.rs +++ /dev/null @@ -1,66 +0,0 @@ -/* - * MIT License - * - * Copyright (c) [2022] [Ondrej Babec ] - * - * 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 core::future::Future; - -use crate::packet::v5::reason_codes::ReasonCode; - -#[derive(Debug)] -pub enum NetworkError { - Connection, - Unknown, - QoSAck, - IDNotMatchedOnAck, - NoMatchingSubs, -} - -pub trait NetworkConnectionFactory: Sized { - type Connection: NetworkConnection; - - type ConnectionFuture<'m>: Future> - where - Self: 'm; - - fn connect<'m>(&'m mut self, ip: [u8; 4], port: u16) -> Self::ConnectionFuture<'m>; -} - -pub trait NetworkConnection { - type WriteFuture<'m>: Future> - where - Self: 'm; - - type ReadFuture<'m>: Future> - where - Self: 'm; - - type CloseFuture<'m>: Future> - where - Self: 'm; - - fn send(&'m mut self, buffer: &'m mut [u8], len: usize) -> Self::WriteFuture<'m>; - - fn receive(&'m mut self, buffer: &'m mut [u8]) -> Self::ReadFuture<'m>; - - fn close(self) -> Self::CloseFuture<'m>; -} diff --git a/mqtt/src/packet/v5/property.rs b/mqtt/src/packet/v5/property.rs index 8595b89..3b7e62b 100644 --- a/mqtt/src/packet/v5/property.rs +++ b/mqtt/src/packet/v5/property.rs @@ -320,7 +320,7 @@ impl<'a> Property<'a> { } } -impl Into for &Property<'a> { +impl<'a> Into for &Property<'a> { fn into(self) -> u8 { return match &*self { Property::PayloadFormat(_u) => 0x01, @@ -355,7 +355,7 @@ impl Into for &Property<'a> { } } -impl From for Property<'a> { +impl<'a> From for Property<'a> { fn from(_orig: u8) -> Self { return match _orig { _ => Property::Reserved(), diff --git a/mqtt/src/tests/integration/mod.rs b/mqtt/src/tests/integration/mod.rs deleted file mode 100644 index 10c7c40..0000000 --- a/mqtt/src/tests/integration/mod.rs +++ /dev/null @@ -1,24 +0,0 @@ -/* - * MIT License - * - * Copyright (c) [2022] [Ondrej Babec ] - * - * 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; diff --git a/mqtt/src/tests/mod.rs b/mqtt/src/tests/mod.rs index 635cbf5..17b1913 100644 --- a/mqtt/src/tests/mod.rs +++ b/mqtt/src/tests/mod.rs @@ -26,8 +26,3 @@ #[allow(unused_must_use)] pub mod unit; -#[allow(dead_code)] -#[allow(unused_must_use)] -#[allow(unused_imports)] -#[cfg(feature = "testing")] -pub mod integration; diff --git a/mqtt/src/tokio_net/tokio_network.rs b/mqtt/src/tokio_net/tokio_network.rs index 94f04ab..9ff829f 100644 --- a/mqtt/src/tokio_net/tokio_network.rs +++ b/mqtt/src/tokio_net/tokio_network.rs @@ -28,7 +28,7 @@ use alloc::string::String; use core::future::Future; use core::time::Duration; -use crate::network::network_trait::{NetworkConnection, NetworkConnectionFactory}; +use crate::network::{NetworkConnection, NetworkConnectionFactory}; use crate::packet::v5::reason_codes::ReasonCode; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; @@ -52,12 +52,12 @@ impl TokioNetwork { } impl NetworkConnection for TokioNetwork { - type WriteFuture<'m> + type SendFuture<'m> where Self: 'm, = impl Future> + 'm; - type ReadFuture<'m> + type ReceiveFuture<'m> where Self: 'm, = impl Future> + 'm; @@ -72,16 +72,16 @@ impl NetworkConnection for TokioNetwork { Self: 'm, = impl Future;*/ - fn send<'m>(&'m mut self, buffer: &'m mut [u8], len: usize) -> Self::WriteFuture<'m> { + fn send<'m>(&'m mut self, buffer: &'m [u8]) -> Self::SendFuture<'m> { async move { self.stream - .write_all(&buffer[0..len]) + .write_all(buffer) .await .map_err(|_| ReasonCode::NetworkError) } } - fn receive<'m>(&'m mut self, buffer: &'m mut [u8]) -> Self::ReadFuture<'m> { + fn receive<'m>(&'m mut self, buffer: &'m mut [u8]) -> Self::ReceiveFuture<'m> { async move { self.stream .read(buffer) diff --git a/mqtt/src/tests/integration/integration_test_single.rs b/mqtt/tests/integration_test_single.rs similarity index 91% rename from mqtt/src/tests/integration/integration_test_single.rs rename to mqtt/tests/integration_test_single.rs index 916b453..6810572 100644 --- a/mqtt/src/tests/integration/integration_test_single.rs +++ b/mqtt/tests/integration_test_single.rs @@ -25,20 +25,20 @@ extern crate alloc; use alloc::string::String; use core::time::Duration; use std::future::Future; -use log::LevelFilter; +use log::{info, LevelFilter}; use tokio::time::sleep; use tokio::task; use tokio_test::{assert_err, assert_ok}; use heapless::Vec; -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; +use rust_mqtt::client::client_config::ClientConfig; +use rust_mqtt::client::client_v5::MqttClientV5; +use rust_mqtt::network::{NetworkConnection, NetworkConnectionFactory}; +use rust_mqtt::packet::v5::property::Property; +use rust_mqtt::packet::v5::publish_packet::QualityOfService; +use rust_mqtt::packet::v5::reason_codes::ReasonCode; +use rust_mqtt::packet::v5::reason_codes::ReasonCode::NotAuthorized; +use rust_mqtt::tokio_net::tokio_network::{TokioNetwork, TokioNetworkFactory}; +use rust_mqtt::utils::types::BufferError; use std::sync::Once; use futures::future::{join, join3}; @@ -251,9 +251,9 @@ async fn receive_with_wrong_cred(qos: QualityOfService) -> Result<(), ReasonCode } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] -async fn simple_publish_recv() { +async fn integration_simple_publish_recv() { setup(); - info!("Running simple integration test"); + info!("Running simple tests test"); let recv = task::spawn(async move { receive(IP, QualityOfService::QoS0, "test/recv/simple").await }); @@ -267,9 +267,9 @@ async fn simple_publish_recv() { } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] -async fn simple_publish_recv_multiple() { +async fn integration_simple_publish_recv_multiple() { setup(); - info!("Running simple integration test"); + info!("Running simple tests test"); let mut topic_names = Vec::<&str, 2>::new(); topic_names.push("test/topic1"); topic_names.push("test/topic2"); @@ -289,9 +289,9 @@ async fn simple_publish_recv_multiple() { } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] -async fn simple_publish_recv_multiple_qos() { +async fn integration_simple_publish_recv_multiple_qos() { setup(); - info!("Running simple integration test"); + info!("Running simple tests test"); let mut topic_names = Vec::<&str, 2>::new(); topic_names.push("test/topic3"); topic_names.push("test/topic4"); @@ -311,9 +311,9 @@ async fn simple_publish_recv_multiple_qos() { } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] -async fn simple_publish_recv_qos() { +async fn integration_simple_publish_recv_qos() { setup(); - info!("Running simple integration test with Quality of Service 1"); + info!("Running simple tests test with Quality of Service 1"); let recv = task::spawn(async move { receive(IP, QualityOfService::QoS1, "test/recv/qos").await }); @@ -324,9 +324,9 @@ async fn simple_publish_recv_qos() { } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] -async fn simple_publish_recv_wrong_cred() { +async fn integration_simple_publish_recv_wrong_cred() { setup(); - info!("Running simple integration test wrong credentials"); + info!("Running simple tests test wrong credentials"); let recv = task::spawn(async move { receive_with_wrong_cred(QualityOfService::QoS1).await }); diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 8559eac..6f4e0aa 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "nightly-2021-12-16" +channel = "nightly-2022-03-10" components = [ "rust-src", "rustfmt" ] targets = [ "thumbv7em-none-eabi", "thumbv7m-none-eabi", "thumbv6m-none-eabi", "thumbv7em-none-eabihf", "thumbv8m.main-none-eabihf", "wasm32-unknown-unknown" ] \ No newline at end of file