From d51c1b8622fc54e0850b59f5e7dbb8d0381f7be8 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Wed, 11 Jan 2023 15:12:38 +0000 Subject: [PATCH] Add more unit tests to lqos_bus, checking that encode/decode of request and replies is valid. --- src/rust/lqos_bus/src/bus/mod.rs | 35 +++++++++++++++++++++++++++ src/rust/lqos_bus/src/bus/request.rs | 2 +- src/rust/lqos_bus/src/bus/response.rs | 2 +- src/rust/lqos_bus/src/ip_stats.rs | 6 ++--- src/rust/lqos_bus/src/tc_handle.rs | 2 +- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/rust/lqos_bus/src/bus/mod.rs b/src/rust/lqos_bus/src/bus/mod.rs index 891d3496..4b2c8996 100644 --- a/src/rust/lqos_bus/src/bus/mod.rs +++ b/src/rust/lqos_bus/src/bus/mod.rs @@ -38,4 +38,39 @@ pub fn decode_response(bytes: &[u8]) -> Result { /// The cookie value to use to determine that the session is valid. pub fn cookie_value() -> u32 { 1234 +} + +#[cfg(test)] +mod test { + use super::*; + use crate::{BusRequest, BusResponse}; + + #[test] + fn test_session_roundtrip() { + let session = BusSession { + auth_cookie: cookie_value(), + requests: vec![ + BusRequest::Ping, + ] + }; + + let bytes = encode_request(&session).unwrap(); + let new_session = decode_request(&bytes).unwrap(); + assert_eq!(new_session.auth_cookie, session.auth_cookie); + assert_eq!(new_session.requests.len(), session.requests.len()); + assert_eq!(new_session.requests[0], session.requests[0]); + } + + #[test] + fn test_reply_roundtrip() { + let reply = BusReply { + auth_cookie: cookie_value(), + responses: vec![BusResponse::Ack] + }; + let bytes = encode_response(&reply).unwrap(); + let new_reply = decode_response(&bytes).unwrap(); + assert_eq!(reply.auth_cookie, new_reply.auth_cookie); + assert_eq!(reply.responses.len(), new_reply.responses.len()); + assert_eq!(reply.responses[0], new_reply.responses[0]); + } } \ No newline at end of file diff --git a/src/rust/lqos_bus/src/bus/request.rs b/src/rust/lqos_bus/src/bus/request.rs index a370aa6f..58f80241 100644 --- a/src/rust/lqos_bus/src/bus/request.rs +++ b/src/rust/lqos_bus/src/bus/request.rs @@ -4,7 +4,7 @@ use crate::TcHandle; /// One or more `BusRequest` objects must be included in a `BusSession` /// request. Each `BusRequest` represents a single request for action /// or data. -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub enum BusRequest { /// A generic "is it alive?" test. Returns an `Ack`. Ping, diff --git a/src/rust/lqos_bus/src/bus/response.rs b/src/rust/lqos_bus/src/bus/response.rs index fc93f722..2267d9d8 100644 --- a/src/rust/lqos_bus/src/bus/response.rs +++ b/src/rust/lqos_bus/src/bus/response.rs @@ -5,7 +5,7 @@ use crate::{IpStats, IpMapping, XdpPpingResult}; /// A `BusResponse` object represents a single /// reply generated from a `BusRequest`, and batched /// inside a `BusReply`. -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub enum BusResponse { /// Yes, we're alive Ack, diff --git a/src/rust/lqos_bus/src/ip_stats.rs b/src/rust/lqos_bus/src/ip_stats.rs index 3b1501f1..82b0992a 100644 --- a/src/rust/lqos_bus/src/ip_stats.rs +++ b/src/rust/lqos_bus/src/ip_stats.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; /// Transmission representation of IP statistics associated /// with a host. -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct IpStats { /// The host's IP address, as detected by the XDP program. pub ip_address: String, @@ -24,7 +24,7 @@ pub struct IpStats { } /// Represents an IP Mapping in the XDP IP to TC/CPU mapping system. -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct IpMapping { /// The mapped IP address. May be IPv4, or IPv6. pub ip_address: String, @@ -42,7 +42,7 @@ pub struct IpMapping { /// Provided for backwards compatibility with `xdp_pping`, with the intent /// to retire it eventually. -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct XdpPpingResult { /// The TC handle in text format. e.g. "1:12" pub tc: String, diff --git a/src/rust/lqos_bus/src/tc_handle.rs b/src/rust/lqos_bus/src/tc_handle.rs index 375b610a..286d47aa 100644 --- a/src/rust/lqos_bus/src/tc_handle.rs +++ b/src/rust/lqos_bus/src/tc_handle.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use std::ffi::CString; /// Provides consistent handling of TC handle types. -#[derive(Copy, Clone, Serialize, Deserialize, Debug, Default)] +#[derive(Copy, Clone, Serialize, Deserialize, Debug, Default, PartialEq, Eq)] pub struct TcHandle(u32); #[allow(non_camel_case_types)]