diff --git a/mqrstt/README.md b/README.md similarity index 96% rename from mqrstt/README.md rename to README.md index a331db9..8829f61 100644 --- a/mqrstt/README.md +++ b/README.md @@ -94,7 +94,6 @@ fn main() { assert_eq!(result.unwrap(), NetworkStatus::OutgoingDisconnect); }); } - ``` ### Tokio example: diff --git a/mqrstt/src/tests/resources/certs/README b/examples/tcp/src/certs/README similarity index 100% rename from mqrstt/src/tests/resources/certs/README rename to examples/tcp/src/certs/README diff --git a/mqrstt/src/tests/resources/broker.emqx.io-ca.crt b/examples/tcp/src/certs/broker.emqx.io-ca.crt similarity index 100% rename from mqrstt/src/tests/resources/broker.emqx.io-ca.crt rename to examples/tcp/src/certs/broker.emqx.io-ca.crt diff --git a/mqrstt/src/tests/resources/certs/cacert - Copy.crt b/examples/tcp/src/certs/cacert - Copy.crt similarity index 100% rename from mqrstt/src/tests/resources/certs/cacert - Copy.crt rename to examples/tcp/src/certs/cacert - Copy.crt diff --git a/mqrstt/src/tests/resources/certs/cacert.pem b/examples/tcp/src/certs/cacert.pem similarity index 100% rename from mqrstt/src/tests/resources/certs/cacert.pem rename to examples/tcp/src/certs/cacert.pem diff --git a/mqrstt/src/tests/resources/certs/cert.pem b/examples/tcp/src/certs/cert.crt similarity index 100% rename from mqrstt/src/tests/resources/certs/cert.pem rename to examples/tcp/src/certs/cert.crt diff --git a/mqrstt/src/tests/resources/certs/client-cert.pem b/examples/tcp/src/certs/client-cert.crt similarity index 100% rename from mqrstt/src/tests/resources/certs/client-cert.pem rename to examples/tcp/src/certs/client-cert.crt diff --git a/mqrstt/src/tests/resources/certs/client-key.pem b/examples/tcp/src/certs/client-key.pem similarity index 100% rename from mqrstt/src/tests/resources/certs/client-key.pem rename to examples/tcp/src/certs/client-key.pem diff --git a/mqrstt/src/tests/resources/certs/key.pem b/examples/tcp/src/certs/key.pem similarity index 100% rename from mqrstt/src/tests/resources/certs/key.pem rename to examples/tcp/src/certs/key.pem diff --git a/examples/tcp/src/tokio.rs b/examples/tcp/src/tokio.rs index e3db001..6fc0f96 100644 --- a/examples/tcp/src/tokio.rs +++ b/examples/tcp/src/tokio.rs @@ -5,11 +5,9 @@ pub struct Handler { } impl AsyncEventHandler for Handler { - fn handle(&mut self, incoming_packet: mqrstt::packets::Packet) -> impl std::future::Future + Send + Sync { - async move { - if let mqrstt::packets::Packet::Publish(publish) = incoming_packet { - self.byte_count += publish.payload.len() as u64; - } + async fn handle(&mut self, incoming_packet: mqrstt::packets::Packet) { + if let mqrstt::packets::Packet::Publish(publish) = incoming_packet { + self.byte_count += publish.payload.len() as u64; } } } diff --git a/mqrstt/src/packets/connect/mod.rs b/mqrstt/src/packets/connect/mod.rs index cb5a611..8dfbd2b 100644 --- a/mqrstt/src/packets/connect/mod.rs +++ b/mqrstt/src/packets/connect/mod.rs @@ -360,9 +360,7 @@ mod tests { ]; buf.extend_from_slice(packet); - let c = Connect::read(0, 0, buf.into()).unwrap(); - - dbg!(c); + Connect::read(0, 0, buf.into()).unwrap(); } #[test] @@ -427,8 +425,6 @@ mod tests { c.write(&mut write_buf).unwrap(); assert_eq!(packet.to_vec(), write_buf.to_vec()); - - dbg!(c); } #[tokio::test] @@ -522,8 +518,6 @@ mod tests { let c = Connect::read(0, 0, buf.into()).unwrap(); - dbg!(c.clone()); - let mut write_buf = bytes::BytesMut::new(); c.write(&mut write_buf).unwrap(); diff --git a/mqrstt/src/packets/mod.rs b/mqrstt/src/packets/mod.rs index 5921c21..9aba8b8 100644 --- a/mqrstt/src/packets/mod.rs +++ b/mqrstt/src/packets/mod.rs @@ -442,171 +442,3 @@ impl std::fmt::Display for PacketType { std::fmt::Debug::fmt(&self, f) } } - -#[cfg(test)] -mod tests { - - use bytes::BytesMut; - - use crate::packets::Packet; - - use crate::tests::test_packets::*; - - #[rstest::rstest] - #[case::connect_case(connect_case())] - #[case::ping_req_case(ping_req_case().1)] - #[case::ping_resp_case(ping_resp_case().1)] - #[case::connack_case(connack_case().1)] - #[case::create_subscribe_packet(create_subscribe_packet(1))] - #[case::create_subscribe_packet(create_subscribe_packet(65335))] - #[case::create_puback_packet(create_puback_packet(1))] - #[case::create_puback_packet(create_puback_packet(65335))] - #[case::create_disconnect_packet(create_disconnect_packet())] - #[case::create_connack_packet(create_connack_packet(true))] - #[case::create_connack_packet(create_connack_packet(false))] - #[case::publish_packet_1(publish_packet_1())] - #[case::publish_packet_2(publish_packet_2())] - #[case::publish_packet_3(publish_packet_3())] - #[case::publish_packet_4(publish_packet_4())] - #[case::create_empty_publish_packet(create_empty_publish_packet())] - #[case::subscribe(subscribe_case())] - #[case::suback(suback_case())] - #[case::unsubscribe(unsubscribe_case())] - #[case::unsuback(unsuback_case())] - #[case::pubcomp_case(pubcomp_case())] - #[case::pubrec_case(pubrec_case())] - #[case::pubrec_case(pubrel_case2())] - #[case::auth_case(auth_case())] - fn test_write_read_write_read_cases(#[case] packet: Packet) { - use crate::packets::WireLength; - - let mut buffer = BytesMut::new(); - - packet.write(&mut buffer).unwrap(); - - let wire_len = packet.wire_len(); - assert_eq!(wire_len, buffer.len()); - - // dbg!(wire_len); - - // let a: Vec<_> = buffer.iter().map(|f| *f as u16).collect(); - // println!("{:?}", a); - - let res1 = Packet::read(&mut buffer).unwrap(); - - assert_eq!(packet, res1); - - let mut buffer = BytesMut::new(); - res1.write(&mut buffer).unwrap(); - let res2 = Packet::read(&mut buffer).unwrap(); - - assert_eq!(res1, res2); - } - - #[rstest::rstest] - #[case::connect_case(connect_case())] - #[case::ping_req_case(ping_req_case().1)] - #[case::ping_resp_case(ping_resp_case().1)] - #[case::connack_case(connack_case().1)] - #[case::create_subscribe_packet(create_subscribe_packet(1))] - #[case::create_subscribe_packet(create_subscribe_packet(65335))] - #[case::create_puback_packet(create_puback_packet(1))] - #[case::create_puback_packet(create_puback_packet(65335))] - #[case::create_disconnect_packet(create_disconnect_packet())] - #[case::create_connack_packet(create_connack_packet(true))] - #[case::create_connack_packet(create_connack_packet(false))] - #[case::publish_packet_1(publish_packet_1())] - #[case::publish_packet_2(publish_packet_2())] - #[case::publish_packet_3(publish_packet_3())] - #[case::publish_packet_4(publish_packet_4())] - #[case::create_empty_publish_packet(create_empty_publish_packet())] - #[case::subscribe(subscribe_case())] - #[case::suback(suback_case())] - #[case::unsubscribe(unsubscribe_case())] - #[case::unsuback(unsuback_case())] - #[case::pubcomp_case(pubcomp_case())] - #[case::pubrec_case(pubrec_case())] - #[case::pubrec_case(pubrel_case2())] - #[case::auth_case(auth_case())] - #[tokio::test] - async fn test_async_write_read_write_read_cases(#[case] packet: Packet) { - use crate::packets::WireLength; - - let mut buffer = Vec::with_capacity(1000); - let res = packet.async_write(&mut buffer).await.unwrap(); - - let wire_len = packet.wire_len(); - - assert_eq!(res, buffer.len()); - assert_eq!(wire_len, buffer.len()); - - let mut buf = buffer.as_slice(); - - let res1 = Packet::async_read(&mut buf).await.unwrap(); - - pretty_assertions::assert_eq!(packet, res1); - } - - #[rstest::rstest] - #[case::disconnect(disconnect_case())] - #[case::ping_req(ping_req_case())] - #[case::ping_resp(ping_resp_case())] - #[case::publish(publish_case())] - #[case::pubrel(pubrel_case())] - #[case::pubrel_smallest(pubrel_smallest_case())] - fn test_read_write_cases(#[case] (bytes, expected_packet): (&[u8], Packet)) { - let mut buffer = BytesMut::from_iter(bytes); - - let res = Packet::read(&mut buffer); - - assert!(res.is_ok()); - - let packet = res.unwrap(); - - assert_eq!(packet, expected_packet); - - buffer.clear(); - - packet.write(&mut buffer).unwrap(); - - assert_eq!(buffer.to_vec(), bytes.to_vec()) - } - - #[rstest::rstest] - #[case::disconnect(disconnect_case())] - #[case::ping_req(ping_req_case())] - #[case::ping_resp(ping_resp_case())] - #[case::publish(publish_case())] - #[case::pubrel(pubrel_case())] - #[case::pubrel_smallest(pubrel_smallest_case())] - #[tokio::test] - async fn test_async_read_write(#[case] (mut bytes, expected_packet): (&[u8], Packet)) { - let input = bytes.to_vec(); - - let res = Packet::async_read(&mut bytes).await; - - dbg!(&res); - assert!(res.is_ok()); - - let packet = res.unwrap(); - - assert_eq!(packet, expected_packet); - - let mut out = Vec::with_capacity(1000); - - packet.async_write(&mut out).await.unwrap(); - - assert_eq!(out, input) - } - - // #[rstest::rstest] - // #[case(&[59, 1, 0, 59])] - // #[case(&[16, 14, 0, 4, 77, 81, 84, 84, 5, 247, 247, 252, 1, 17, 247, 247, 247])] - // fn test_read_error(#[case] bytes: &[u8]) { - // let mut buffer = BytesMut::from_iter(bytes); - - // let res = Packet::read_from_buffer(&mut buffer); - - // assert!(res.is_err()); - // } -} diff --git a/mqrstt/src/packets/subscribe/mod.rs b/mqrstt/src/packets/subscribe/mod.rs index deb39ab..58aaf93 100644 --- a/mqrstt/src/packets/subscribe/mod.rs +++ b/mqrstt/src/packets/subscribe/mod.rs @@ -434,15 +434,6 @@ mod tests { 0x35, 0xd6, 0x02, 0x0b, 0x01, 0x00, 0x16, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x15, ]; - // let sub_data = &[0x35, 0xd6, 0x02, 0x0b, 0x01, 0x00, 0x16, 0x73, - // 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, - // 0x2f, 0x74, 0x65, 0x73, 0x74, 0x15]; - - // let sub_data = &[ - // 53, 214, 2, 11, 1, 0, 22, 115, 117, 98, 115, - // 99, 114, 105, 98, 101, 47, 101, 120, 97, 109, - // 112, 108, 101, 47, 116, 101, 115, 116, 21, - // ]; let mut bufmut = BytesMut::new(); bufmut.extend(&sub_data[..]); @@ -451,8 +442,6 @@ mod tests { let s = Subscribe::read(0, 30, buf.clone()).unwrap(); - let s = dbg!(s); - let mut result = BytesMut::new(); s.write(&mut result).unwrap(); diff --git a/mqrstt/src/state_handler.rs b/mqrstt/src/state_handler.rs index ecf7fd6..36e897a 100644 --- a/mqrstt/src/state_handler.rs +++ b/mqrstt/src/state_handler.rs @@ -251,9 +251,11 @@ mod handler_tests { Packet, PubComp, PubCompProperties, PubCompReasonCode, PubRec, PubRecProperties, PubRecReasonCode, PubRel, PubRelProperties, PubRelReasonCode, QoS, SubAck, SubAckProperties, SubAckReasonCode, UnsubAck, UnsubAckProperties, UnsubAckReasonCode, }, - tests::test_packets::{create_connack_packet, create_puback_packet, create_publish_packet, create_subscribe_packet, create_unsubscribe_packet}, ConnectOptions, StateHandler, }; + + use crate::tests::test_packets::*; + fn handler(clean_start: bool) -> (StateHandler, Receiver) { let (apkids, apkids_r) = AvailablePacketIds::new(100); diff --git a/mqrstt/src/tests/mod.rs b/mqrstt/src/tests/mod.rs index bc1a143..ba8a4e3 100644 --- a/mqrstt/src/tests/mod.rs +++ b/mqrstt/src/tests/mod.rs @@ -1,4 +1,2 @@ -pub mod resources; mod test_bytes; -pub mod test_packets; -pub mod tls; +pub(crate) mod test_packets; diff --git a/mqrstt/src/tests/resources/mod.rs b/mqrstt/src/tests/resources/mod.rs deleted file mode 100644 index 30a88a7..0000000 --- a/mqrstt/src/tests/resources/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[cfg(test)] -mod test_bytes_packet; - -pub const EMQX_CERT: &[u8] = include_bytes!("broker.emqx.io-ca.crt"); diff --git a/mqrstt/src/tests/resources/test_bytes_packet.rs b/mqrstt/src/tests/resources/test_bytes_packet.rs deleted file mode 100644 index 02d4ed7..0000000 --- a/mqrstt/src/tests/resources/test_bytes_packet.rs +++ /dev/null @@ -1,93 +0,0 @@ -// use bytes::Bytes; - -// use crate::packets::{ -// connack::{ConnAck, ConnAckFlags, ConnAckProperties}, -// disconnect::{Disconnect, DisconnectProperties}, -// packets::Packet, -// publish::{Publish, PublishProperties}, -// reason_codes::{ConnAckReasonCode, DisconnectReasonCode}, -// QoS, -// }; - -// pub fn connack_bytes_packet() -> ([u8; 21], Packet) { -// const CONNACK_BYTES: [u8; 21] = [ -// 0x20, 0x13, 0x01, 0x00, 0x10, 0x27, 0x00, 0x10, 0x00, 0x00, 0x25, 0x01, 0x2a, 0x01, 0x29, -// 0x01, 0x22, 0xff, 0xff, 0x28, 0x01, -// ]; - -// let expected = ConnAck { -// connack_flags: ConnAckFlags::SESSION_PRESENT, -// reason_code: ConnAckReasonCode::Success, -// connack_properties: ConnAckProperties { -// session_expiry_interval: None, -// receive_maximum: None, -// maximum_qos: None, -// retain_available: Some(true), -// maximum_packet_size: Some(1048576), -// assigned_client_id: None, -// topic_alias_maximum: Some(65535), -// reason_string: None, -// user_properties: vec![], -// wildcards_available: Some(true), -// subscription_ids_available: Some(true), -// shared_subscription_available: Some(true), -// server_keep_alive: None, -// response_info: None, -// server_reference: None, -// authentication_method: None, -// authentication_data: None, -// }, -// }; -// (CONNACK_BYTES, Packet::ConnAck(expected)) -// } - -// pub fn disconnect_bytes_packet() -> ([u8; 4], Packet) { -// const DISCONNECT_BYTES: [u8; 4] = [0xe0, 0x02, 0x8e, 0x00]; - -// let expected = Disconnect { -// reason_code: DisconnectReasonCode::SessionTakenOver, -// properties: DisconnectProperties { -// session_expiry_interval: None, -// reason_string: None, -// user_properties: vec![], -// server_reference: None, -// }, -// }; -// (DISCONNECT_BYTES, Packet::Disconnect(expected)) -// } - -// pub fn pingreq_bytes_packet() -> ([u8; 2], Packet) { -// ([0xc0, 0x00], Packet::PingReq) -// } - -// pub fn pingresp_bytes_packet() -> ([u8; 2], Packet) { -// ([0xd0, 0x00], Packet::PingResp) -// } - -// pub fn publish_bytes_packet_qos_2() -> ([u8; 38], Packet) { -// const PUBLISH_BYTES: [u8; 38] = [ -// 0x35, 0x24, 0x00, 0x14, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x31, 0x32, 0x33, 0x2f, 0x74, 0x65, -// 0x73, 0x74, 0x2f, 0x62, 0x6c, 0x61, 0x62, 0x6c, 0x61, 0x35, 0xd3, 0x0b, 0x01, 0x01, 0x09, -// 0x00, 0x04, 0x31, 0x32, 0x31, 0x32, 0x0b, 0x01, -// ]; - -// let expected = Publish { -// dup: false, -// qos: QoS::ExactlyOnce, -// retain: true, -// topic: "test/123/test/blabla".to_string(), -// packet_identifier: Some(13779), -// publish_properties: PublishProperties { -// payload_format_indicator: Some(1), -// message_expiry_interval: None, -// topic_alias: None, -// response_topic: None, -// correlation_data: Some(Bytes::from_static(b"1212")), -// subscription_identifier: vec![1], -// user_properties: vec![], -// content_type: None, -// }, -// payload: Bytes::from_static(b""), -// }; -// (PUBLISH_BYTES, Packet::Publish(expected)) -// } diff --git a/mqrstt/src/tests/test_packets/mod.rs b/mqrstt/src/tests/test_packets/mod.rs new file mode 100644 index 0000000..c7a828b --- /dev/null +++ b/mqrstt/src/tests/test_packets/mod.rs @@ -0,0 +1,185 @@ +mod test_cases; + +pub use test_cases::*; + +#[cfg(test)] +mod tests { + + use bytes::BytesMut; + + use crate::packets::Packet; + + use rstest::*; + + use crate::packets::mqtt_trait::WireLength; + use crate::tests::test_packets::*; + + #[rstest::rstest] + #[case::connect_case(connect_case())] + #[case::ping_req_case(ping_req_case().1)] + #[case::ping_resp_case(ping_resp_case().1)] + #[case::connack_case(connack_case().1)] + #[case::create_subscribe_packet(create_subscribe_packet(1))] + #[case::create_subscribe_packet(create_subscribe_packet(65335))] + #[case::create_puback_packet(create_puback_packet(1))] + #[case::create_puback_packet(create_puback_packet(65335))] + #[case::create_disconnect_packet(create_disconnect_packet())] + #[case::create_connack_packet(create_connack_packet(true))] + #[case::create_connack_packet(create_connack_packet(false))] + #[case::publish_packet_1(publish_packet_1())] + #[case::publish_packet_2(publish_packet_2())] + #[case::publish_packet_3(publish_packet_3())] + #[case::publish_packet_4(publish_packet_4())] + #[case::create_empty_publish_packet(create_empty_publish_packet())] + #[case::subscribe(subscribe_case())] + #[case::suback(suback_case())] + #[case::unsubscribe(unsubscribe_case())] + #[case::unsuback(unsuback_case())] + #[case::pubcomp_case(pubcomp_case())] + #[case::pubrec_case(pubrec_case())] + #[case::pubrec_case(pubrel_case2())] + #[case::auth_case(auth_case())] + fn test_write_read_write_read_cases(#[case] packet: Packet) { + let mut buffer = BytesMut::new(); + + packet.write(&mut buffer).unwrap(); + + let wire_len = packet.wire_len(); + assert_eq!(wire_len, buffer.len()); + + let res1 = Packet::read(&mut buffer).unwrap(); + + assert_eq!(packet, res1); + + let mut buffer = BytesMut::new(); + res1.write(&mut buffer).unwrap(); + let res2 = Packet::read(&mut buffer).unwrap(); + + assert_eq!(res1, res2); + } + + #[rstest::rstest] + #[case::connect_case(connect_case())] + #[case::ping_req_case(ping_req_case().1)] + #[case::ping_resp_case(ping_resp_case().1)] + #[case::connack_case(connack_case().1)] + #[case::create_subscribe_packet(create_subscribe_packet(1))] + #[case::create_subscribe_packet(create_subscribe_packet(65335))] + #[case::create_puback_packet(create_puback_packet(1))] + #[case::create_puback_packet(create_puback_packet(65335))] + #[case::create_disconnect_packet(create_disconnect_packet())] + #[case::create_connack_packet(create_connack_packet(true))] + #[case::create_connack_packet(create_connack_packet(false))] + #[case::publish_packet_1(publish_packet_1())] + #[case::publish_packet_2(publish_packet_2())] + #[case::publish_packet_3(publish_packet_3())] + #[case::publish_packet_4(publish_packet_4())] + #[case::create_empty_publish_packet(create_empty_publish_packet())] + #[case::subscribe(subscribe_case())] + #[case::suback(suback_case())] + #[case::unsubscribe(unsubscribe_case())] + #[case::unsuback(unsuback_case())] + #[case::pubcomp_case(pubcomp_case())] + #[case::pubrec_case(pubrec_case())] + #[case::pubrec_case(pubrel_case2())] + #[case::auth_case(auth_case())] + #[tokio::test] + async fn test_async_write_read_write_read_cases(#[case] packet: Packet) { + let mut buffer = Vec::with_capacity(1000); + let res = packet.async_write(&mut buffer).await.unwrap(); + + let wire_len = packet.wire_len(); + + assert_eq!(res, buffer.len()); + assert_eq!(wire_len, buffer.len()); + + let mut buf = buffer.as_slice(); + + let res1 = Packet::async_read(&mut buf).await.unwrap(); + + pretty_assertions::assert_eq!(packet, res1); + } + + #[rstest::rstest] + #[case::disconnect(disconnect_case())] + #[case::ping_req(ping_req_case())] + #[case::ping_resp(ping_resp_case())] + #[case::publish(publish_case())] + #[case::pubrel(pubrel_case())] + #[case::pubrel_smallest(pubrel_smallest_case())] + fn test_read_write_cases(#[case] (bytes, expected_packet): (&[u8], Packet)) { + let mut buffer = BytesMut::from_iter(bytes); + + let res = Packet::read(&mut buffer); + + assert!(res.is_ok()); + + let packet = res.unwrap(); + + assert_eq!(packet, expected_packet); + + buffer.clear(); + + packet.write(&mut buffer).unwrap(); + + assert_eq!(buffer.to_vec(), bytes.to_vec()) + } + + #[rstest::rstest] + #[case::disconnect(disconnect_case())] + #[case::ping_req(ping_req_case())] + #[case::ping_resp(ping_resp_case())] + #[case::publish(publish_case())] + #[case::pubrel(pubrel_case())] + #[case::pubrel_smallest(pubrel_smallest_case())] + #[tokio::test] + async fn test_async_read_write(#[case] (mut bytes, expected_packet): (&[u8], Packet)) { + let input = bytes.to_vec(); + + let res = Packet::async_read(&mut bytes).await; + + assert!(res.is_ok()); + + let packet = res.unwrap(); + + assert_eq!(packet, expected_packet); + + let mut out = Vec::with_capacity(1000); + + packet.async_write(&mut out).await.unwrap(); + + assert_eq!(out, input) + } + + #[rstest] + #[case(create_subscribe_packet(1))] + #[case(create_subscribe_packet(65335))] + #[case(create_puback_packet(1))] + #[case(create_puback_packet(65335))] + #[case(create_disconnect_packet())] + #[case(publish_packet_1())] + #[case(publish_packet_2())] + #[case(publish_packet_3())] + #[case(publish_packet_4())] + /// Test if the input == output after read packet form input and write packet to output + fn test_equal_write_read(#[case] packet: Packet) { + let mut buffer = bytes::BytesMut::new(); + + packet.write(&mut buffer).unwrap(); + + let read_packet = Packet::read(&mut buffer).unwrap(); + + assert_eq!(packet, read_packet); + } + + // #[rstest::rstest] + // #[case(&[59, 1, 0, 59])] + // #[case(&[16, 14, 0, 4, 77, 81, 84, 84, 5, 247, 247, 252, 1, 17, 247, 247, 247])] + // fn test_read_error(#[case] bytes: &[u8]) { + // let mut buffer = BytesMut::from_iter(bytes); + + // let res = Packet::read_from_buffer(&mut buffer); + + // assert!(res.is_err()); + // } +} diff --git a/mqrstt/src/tests/test_packets.rs b/mqrstt/src/tests/test_packets/test_cases.rs similarity index 95% rename from mqrstt/src/tests/test_packets.rs rename to mqrstt/src/tests/test_packets/test_cases.rs index f23a80a..c4be53f 100644 --- a/mqrstt/src/tests/test_packets.rs +++ b/mqrstt/src/tests/test_packets/test_cases.rs @@ -1,5 +1,3 @@ -use rstest::*; - use crate::packets::*; pub fn connack_case() -> (&'static [u8], Packet) { @@ -415,24 +413,3 @@ pub fn auth_case() -> Packet { Packet::Auth(expected) } - -#[rstest] -#[case(create_subscribe_packet(1))] -#[case(create_subscribe_packet(65335))] -#[case(create_puback_packet(1))] -#[case(create_puback_packet(65335))] -#[case(create_disconnect_packet())] -#[case(publish_packet_1())] -#[case(publish_packet_2())] -#[case(publish_packet_3())] -#[case(publish_packet_4())] -/// Test if the input == output after read packet form input and write packet to output -fn test_equal_write_read(#[case] packet: Packet) { - let mut buffer = bytes::BytesMut::new(); - - packet.write(&mut buffer).unwrap(); - - let read_packet = Packet::read(&mut buffer).unwrap(); - - assert_eq!(packet, read_packet); -} diff --git a/mqrstt/src/tests/tls.rs b/mqrstt/src/tests/tls.rs deleted file mode 100644 index 955a14b..0000000 --- a/mqrstt/src/tests/tls.rs +++ /dev/null @@ -1,58 +0,0 @@ -#[cfg(test)] -pub mod tests { - use std::{ - io::{BufReader, Cursor}, - sync::Arc, - }; - - use rustls::{Certificate, ClientConfig, Error, OwnedTrustAnchor, RootCertStore}; - - #[derive(Debug, Clone)] - pub enum PrivateKey { - RSA(Vec), - ECC(Vec), - } - - pub fn simple_rust_tls(ca: Vec, alpn: Option>>, client_auth: Option<(Vec, PrivateKey)>) -> Result, Error> { - let mut root_cert_store = RootCertStore::empty(); - - let ca_certs = rustls_pemfile::certs(&mut BufReader::new(Cursor::new(ca))).unwrap(); - - let trust_anchors = ca_certs.iter().map_while(|cert| { - if let Ok(ta) = webpki::TrustAnchor::try_from_cert_der(&cert[..]) { - Some(OwnedTrustAnchor::from_subject_spki_name_constraints(ta.subject, ta.spki, ta.name_constraints)) - } else { - None - } - }); - root_cert_store.add_trust_anchors(trust_anchors); - - assert!(!root_cert_store.is_empty()); - - let config = ClientConfig::builder().with_safe_defaults().with_root_certificates(root_cert_store); - - let mut config = match client_auth { - Some((client_cert_info, client_private_info)) => { - let read_private_keys = match client_private_info { - PrivateKey::RSA(rsa) => rustls_pemfile::rsa_private_keys(&mut BufReader::new(Cursor::new(rsa))), - PrivateKey::ECC(ecc) => rustls_pemfile::pkcs8_private_keys(&mut BufReader::new(Cursor::new(ecc))), - } - .unwrap(); - - let key = read_private_keys.into_iter().next().unwrap(); - - let client_certs = rustls_pemfile::certs(&mut BufReader::new(Cursor::new(client_cert_info))).unwrap(); - let client_cert_chain = client_certs.into_iter().map(Certificate).collect(); - - config.with_client_auth_cert(client_cert_chain, rustls::PrivateKey(key))? - } - None => config.with_no_client_auth(), - }; - - if let Some(alpn) = alpn { - config.alpn_protocols.extend(alpn) - } - - Ok(Arc::new(config)) - } -}