added a few more tests
All checks were successful
Rust/sat-rs/pipeline/pr-main This commit looks good

This commit is contained in:
Robin Müller 2024-03-04 16:07:37 +01:00
parent be4199e3d2
commit 32059e8682
Signed by: muellerr
GPG Key ID: A649FB78196E3849
2 changed files with 103 additions and 23 deletions

View File

@ -224,6 +224,13 @@ pub(crate) mod tests {
&buf[0..size] &buf[0..size]
} }
pub fn generate_ping_tc_as_vec() -> Vec<u8> {
let mut sph = SpHeader::tc_unseg(0x002, 0x34, 0).unwrap();
PusTcCreator::new_simple(&mut sph, 17, 1, None, true)
.to_vec()
.unwrap()
}
type SharedPacketQueue = Arc<Mutex<VecDeque<(u16, Vec<u8>)>>>; type SharedPacketQueue = Arc<Mutex<VecDeque<(u16, Vec<u8>)>>>;
pub struct BasicApidHandlerSharedQueue { pub struct BasicApidHandlerSharedQueue {
pub known_packet_queue: SharedPacketQueue, pub known_packet_queue: SharedPacketQueue,
@ -324,13 +331,8 @@ pub(crate) mod tests {
} }
#[test] #[test]
fn test_distribs_unknown_apid() { fn test_unknown_apid_handling() {
let known_packet_queue = Arc::new(Mutex::default()); let apid_handler = BasicApidHandlerOwnedQueue::default();
let unknown_packet_queue = Arc::new(Mutex::default());
let apid_handler = BasicApidHandlerSharedQueue {
known_packet_queue: known_packet_queue.clone(),
unknown_packet_queue: unknown_packet_queue.clone(),
};
let mut ccsds_distrib = CcsdsDistributor::new(apid_handler); let mut ccsds_distrib = CcsdsDistributor::new(apid_handler);
let mut sph = SpHeader::tc_unseg(0x004, 0x34, 0).unwrap(); let mut sph = SpHeader::tc_unseg(0x004, 0x34, 0).unwrap();
let pus_tc = PusTcCreator::new_simple(&mut sph, 17, 1, None, true); let pus_tc = PusTcCreator::new_simple(&mut sph, 17, 1, None, true);
@ -339,11 +341,52 @@ pub(crate) mod tests {
.write_to_bytes(test_buf.as_mut_slice()) .write_to_bytes(test_buf.as_mut_slice())
.expect("Error writing TC to buffer"); .expect("Error writing TC to buffer");
ccsds_distrib.pass_tc(&test_buf).expect("Passing TC failed"); ccsds_distrib.pass_tc(&test_buf).expect("Passing TC failed");
let recvd = unknown_packet_queue.lock().unwrap().pop_front(); assert!(ccsds_distrib.packet_handler().known_packet_queue.is_empty());
assert!(known_packet_queue.lock().unwrap().is_empty()); let apid_handler = ccsds_distrib.packet_handler_mut();
let recvd = apid_handler.unknown_packet_queue.pop_front();
assert!(recvd.is_some()); assert!(recvd.is_some());
let (apid, packet) = recvd.unwrap(); let (apid, packet) = recvd.unwrap();
assert_eq!(apid, 0x004); assert_eq!(apid, 0x004);
assert_eq!(packet.as_slice(), test_buf); assert_eq!(packet.as_slice(), test_buf);
} }
#[test]
fn test_ccsds_distribution() {
let mut ccsds_distrib = CcsdsDistributor::new(BasicApidHandlerOwnedQueue::default());
let mut sph = SpHeader::tc_unseg(0x002, 0x34, 0).unwrap();
let pus_tc = PusTcCreator::new_simple(&mut sph, 17, 1, None, true);
let tc_vec = pus_tc.to_vec().unwrap();
ccsds_distrib
.pass_ccsds(&sph, &tc_vec)
.expect("passing CCSDS TC failed");
let recvd = ccsds_distrib
.packet_handler_mut()
.known_packet_queue
.pop_front();
assert!(recvd.is_some());
let recvd = recvd.unwrap();
assert_eq!(recvd.0, 0x002);
assert_eq!(recvd.1, tc_vec);
}
#[test]
fn test_distribution_short_packet_fails() {
let mut ccsds_distrib = CcsdsDistributor::new(BasicApidHandlerOwnedQueue::default());
let mut sph = SpHeader::tc_unseg(0x002, 0x34, 0).unwrap();
let pus_tc = PusTcCreator::new_simple(&mut sph, 17, 1, None, true);
let tc_vec = pus_tc.to_vec().unwrap();
let result = ccsds_distrib.pass_tc(&tc_vec[0..6]);
assert!(result.is_err());
let error = result.unwrap_err();
if let CcsdsError::ByteConversionError(ByteConversionError::FromSliceTooSmall {
found,
expected,
}) = error
{
assert_eq!(found, 6);
assert_eq!(expected, 7);
} else {
panic!("Unexpected error variant");
}
}
} }

View File

@ -87,14 +87,16 @@ pub trait PusServiceDistributor {
/// This distributor expects the passed trait object to be [Send]able to allow more ergonomic /// This distributor expects the passed trait object to be [Send]able to allow more ergonomic
/// usage with threads. /// usage with threads.
pub struct PusDistributor<ServiceDistributor: PusServiceDistributor<Error = E>, E> { pub struct PusDistributor<ServiceDistributor: PusServiceDistributor<Error = E>, E> {
service_provider: ServiceDistributor, service_distributor: ServiceDistributor,
} }
impl<ServiceDistributor: PusServiceDistributor<Error = E>, E> impl<ServiceDistributor: PusServiceDistributor<Error = E>, E>
PusDistributor<ServiceDistributor, E> PusDistributor<ServiceDistributor, E>
{ {
pub fn new(service_provider: ServiceDistributor) -> Self { pub fn new(service_provider: ServiceDistributor) -> Self {
PusDistributor { service_provider } PusDistributor {
service_distributor: service_provider,
}
} }
} }
@ -107,8 +109,8 @@ pub enum PusDistribError<E> {
impl<E: Display> Display for PusDistribError<E> { impl<E: Display> Display for PusDistribError<E> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self { match self {
PusDistribError::CustomError(e) => write!(f, "{e}"), PusDistribError::CustomError(e) => write!(f, "pus distribution error: {e}"),
PusDistribError::PusError(e) => write!(f, "{e}"), PusDistribError::PusError(e) => write!(f, "pus distribution error: {e}"),
} }
} }
} }
@ -150,7 +152,7 @@ impl<ServiceDistributor: PusServiceDistributor<Error = E>, E: 'static> ReceivesE
{ {
type Error = PusDistribError<E>; type Error = PusDistribError<E>;
fn pass_pus_tc(&mut self, header: &SpHeader, pus_tc: &PusTcReader) -> Result<(), Self::Error> { fn pass_pus_tc(&mut self, header: &SpHeader, pus_tc: &PusTcReader) -> Result<(), Self::Error> {
self.service_provider self.service_distributor
.distribute_packet(pus_tc.service(), header, pus_tc) .distribute_packet(pus_tc.service(), header, pus_tc)
.map_err(|e| PusDistribError::CustomError(e)) .map_err(|e| PusDistribError::CustomError(e))
} }
@ -160,26 +162,30 @@ impl<ServiceDistributor: PusServiceDistributor<Error = E>, E: 'static>
PusDistributor<ServiceDistributor, E> PusDistributor<ServiceDistributor, E>
{ {
pub fn service_distributor(&self) -> &ServiceDistributor { pub fn service_distributor(&self) -> &ServiceDistributor {
&self.service_provider &self.service_distributor
} }
pub fn service_distributor_mut(&mut self) -> &mut ServiceDistributor { pub fn service_distributor_mut(&mut self) -> &mut ServiceDistributor {
&mut self.service_provider &mut self.service_distributor
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::queue::GenericSendError;
use crate::tmtc::ccsds_distrib::tests::{ use crate::tmtc::ccsds_distrib::tests::{
generate_ping_tc, BasicApidHandlerOwnedQueue, BasicApidHandlerSharedQueue, generate_ping_tc, generate_ping_tc_as_vec, BasicApidHandlerOwnedQueue,
BasicApidHandlerSharedQueue,
}; };
use crate::tmtc::ccsds_distrib::{CcsdsDistributor, CcsdsPacketHandler}; use crate::tmtc::ccsds_distrib::{CcsdsDistributor, CcsdsPacketHandler};
use alloc::format;
use alloc::vec::Vec; use alloc::vec::Vec;
use spacepackets::ecss::PusError; use spacepackets::ecss::PusError;
use spacepackets::CcsdsPacket; use spacepackets::CcsdsPacket;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::collections::VecDeque; use std::collections::VecDeque;
use std::fmt::format;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -190,6 +196,7 @@ mod tests {
pub apid: u16, pub apid: u16,
pub packet: Vec<u8>, pub packet: Vec<u8>,
} }
struct PusHandlerSharedQueue(Arc<Mutex<VecDeque<PacketInfo>>>); struct PusHandlerSharedQueue(Arc<Mutex<VecDeque<PacketInfo>>>);
#[derive(Default)] #[derive(Default)]
@ -295,8 +302,20 @@ mod tests {
} }
#[test] #[test]
#[cfg(feature = "std")] fn test_pus_distribution_as_raw_packet() {
fn test_pus_distribution() { let mut pus_distrib = PusDistributor::new(PusHandlerOwnedQueue::default());
let tc = generate_ping_tc_as_vec();
let result = pus_distrib.pass_tc(&tc);
assert!(result.is_ok());
assert_eq!(pus_distrib.service_distributor_mut().0.len(), 1);
let packet_info = pus_distrib.service_distributor_mut().0.pop_front().unwrap();
assert_eq!(packet_info.service, 17);
assert_eq!(packet_info.apid, 0x002);
assert_eq!(packet_info.packet, tc);
}
#[test]
fn test_pus_distribution_combined_handler() {
let known_packet_queue = Arc::new(Mutex::default()); let known_packet_queue = Arc::new(Mutex::default());
let unknown_packet_queue = Arc::new(Mutex::default()); let unknown_packet_queue = Arc::new(Mutex::default());
let pus_queue = Arc::new(Mutex::default()); let pus_queue = Arc::new(Mutex::default());
@ -306,9 +325,7 @@ mod tests {
unknown_packet_queue: unknown_packet_queue.clone(), unknown_packet_queue: unknown_packet_queue.clone(),
}; };
let pus_distrib = PusDistributor { let pus_distrib = PusDistributor::new(pus_handler);
service_provider: pus_handler,
};
is_send(&pus_distrib); is_send(&pus_distrib);
let apid_handler = ApidHandlerShared { let apid_handler = ApidHandlerShared {
pus_distrib, pus_distrib,
@ -337,7 +354,7 @@ mod tests {
} }
#[test] #[test]
fn test_accessing_distributor() { fn test_accessing_combined_distributor() {
let pus_handler = PusHandlerOwnedQueue::default(); let pus_handler = PusHandlerOwnedQueue::default();
let handler_base = BasicApidHandlerOwnedQueue::default(); let handler_base = BasicApidHandlerOwnedQueue::default();
let pus_distrib = PusDistributor::new(pus_handler); let pus_distrib = PusDistributor::new(pus_handler);
@ -369,4 +386,24 @@ mod tests {
assert_eq!(packet_info.apid, 0x002); assert_eq!(packet_info.apid, 0x002);
assert_eq!(packet_info.packet, tc_slice); assert_eq!(packet_info.packet, tc_slice);
} }
#[test]
fn test_pus_distrib_error_custom_error() {
let error = PusDistribError::CustomError(GenericSendError::RxDisconnected);
let error_string = format!("{}", error);
assert_eq!(
error_string,
"pus distribution error: rx side has disconnected"
);
}
#[test]
fn test_pus_distrib_error_pus_error() {
let error = PusDistribError::<GenericSendError>::PusError(PusError::CrcCalculationMissing);
let error_string = format!("{}", error);
assert_eq!(
error_string,
"pus distribution error: crc16 was not calculated"
);
}
} }