Add more unit tests to lqos_bus, checking that encode/decode of request and replies is valid.

This commit is contained in:
Herbert Wolverson committed 2023-01-11 15:12:38 +00:00
1 parent 5cf20a9afe
commit d51c1b8622
5 files changed
+41 -6

No files matched your search

+35
View File
@@ -38,4 +38,39 @@ pub fn decode_response(bytes: &[u8]) -> Result<BusReply> {
/// 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]);
}
}
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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,
+3 -3
View File
@@ -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,
+1 -1
View File
@@ -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)]