nice test harness
All checks were successful
Rust/sat-rs/pipeline/pr-main This commit looks good

This commit is contained in:
Robin Müller 2024-02-01 13:29:28 +01:00
parent 4f9cf38d34
commit cd26cecafb
Signed by: muellerr
GPG Key ID: A649FB78196E3849
2 changed files with 212 additions and 99 deletions

View File

@ -911,7 +911,7 @@ pub(crate) fn source_buffer_large_enough(cap: usize, len: usize) -> Result<(), E
}
#[cfg(test)]
pub(crate) mod tests {
pub mod tests {
use std::sync::{mpsc, RwLock};
use alloc::boxed::Box;
@ -929,11 +929,12 @@ pub(crate) mod tests {
TcStateAccepted, VerificationReporterCfg, VerificationReporterWithSender, VerificationToken,
};
use super::{
EcssTcAndToken, EcssTcInStoreConverter, MpscTcReceiver, MpscTmInStoreSender,
PusServiceHandler,
EcssTcAndToken, EcssTcInStoreConverter, EcssTcInVecConverter, MpscTcReceiver,
MpscTmAsVecSender, MpscTmInStoreSender, PusPacketHandlerResult, PusPacketHandlingError,
PusServiceHandler, TcInMemory,
};
pub(crate) const TEST_APID: u16 = 0x101;
pub const TEST_APID: u16 = 0x101;
#[derive(Debug, Eq, PartialEq, Clone)]
pub(crate) struct CommonTmInfo {
@ -944,6 +945,18 @@ pub(crate) mod tests {
pub time_stamp: [u8; 7],
}
pub trait PusTestHarness {
fn send_tc(&mut self, tc: &PusTcCreator) -> VerificationToken<TcStateAccepted>;
fn read_next_tm(&mut self) -> PusTmReader<'_>;
fn check_next_verification_tm<STATE>(
&self,
subservice: u8,
token: VerificationToken<STATE>,
);
fn handle_one_tc(&mut self) -> Result<PusPacketHandlerResult, PusPacketHandlingError>;
}
impl CommonTmInfo {
pub fn new_from_tm(tm: &PusTmCreator) -> Self {
let mut time_stamp = [0; 7];
@ -958,7 +971,8 @@ pub(crate) mod tests {
}
}
pub(crate) struct PusServiceHandlerWithStoreCommon {
/// Common fields for a PUS service test harness.
pub struct PusServiceHandlerWithStoreCommon {
pus_buf: [u8; 2048],
tm_buf: [u8; 2048],
tc_pool: SharedPool,
@ -969,6 +983,10 @@ pub(crate) mod tests {
}
impl PusServiceHandlerWithStoreCommon {
/// This function generates the structure in addition to the PUS service handler
/// [PusServiceHandler] which might be required for a specific PUS service handler.
///
/// The PUS service handler is instantiated with a [EcssTcInStoreConverter].
pub fn new() -> (Self, PusServiceHandler<EcssTcInStoreConverter>) {
let pool_cfg = PoolCfg::new(vec![(16, 16), (8, 32), (4, 64)]);
let tc_pool = LocalPool::new(pool_cfg.clone());
@ -988,7 +1006,7 @@ pub(crate) mod tests {
let test_srv_tc_receiver = MpscTcReceiver::new(0, "TEST_RECEIVER", test_srv_tc_rx);
let in_store_converter = EcssTcInStoreConverter::new(shared_tc_pool.clone(), 2048);
(
PusServiceHandlerWithStoreCommon {
Self {
pus_buf: [0; 2048],
tm_buf: [0; 2048],
tc_pool: shared_tc_pool,
@ -1006,7 +1024,7 @@ pub(crate) mod tests {
),
)
}
pub(crate) fn send_tc(&mut self, tc: &PusTcCreator) -> VerificationToken<TcStateAccepted> {
pub fn send_tc(&mut self, tc: &PusTcCreator) -> VerificationToken<TcStateAccepted> {
let token = self.verification_handler.add_tc(tc);
let token = self
.verification_handler
@ -1023,7 +1041,7 @@ pub(crate) mod tests {
token
}
pub(crate) fn read_next_tm(&mut self) -> PusTmReader<'_> {
pub fn read_next_tm(&mut self) -> PusTmReader<'_> {
let next_msg = self.tm_receiver.try_recv();
assert!(next_msg.is_ok());
let tm_addr = next_msg.unwrap();
@ -1033,7 +1051,7 @@ pub(crate) mod tests {
PusTmReader::new(&self.tm_buf, 7).unwrap().0
}
pub(crate) fn check_next_verification_tm<STATE>(
pub fn check_next_verification_tm<STATE>(
&self,
subservice: u8,
token: VerificationToken<STATE>,
@ -1052,4 +1070,83 @@ pub(crate) mod tests {
assert_eq!(req_id, token.req_id());
}
}
pub struct PusServiceHandlerWithVecCommon {
current_tm: Option<alloc::vec::Vec<u8>>,
tc_sender: mpsc::Sender<EcssTcAndToken>,
tm_receiver: mpsc::Receiver<alloc::vec::Vec<u8>>,
verification_handler: VerificationReporterWithSender,
}
impl PusServiceHandlerWithVecCommon {
pub fn new() -> (Self, PusServiceHandler<EcssTcInVecConverter>) {
let (test_srv_tc_tx, test_srv_tc_rx) = mpsc::channel();
let (tm_tx, tm_rx) = mpsc::channel();
let verif_sender = MpscTmAsVecSender::new(0, "verififcatio-sender", tm_tx.clone());
let verif_cfg = VerificationReporterCfg::new(TEST_APID, 1, 2, 8).unwrap();
let verification_handler =
VerificationReporterWithSender::new(&verif_cfg, Box::new(verif_sender));
let test_srv_tm_sender = MpscTmAsVecSender::new(0, "test-sender", tm_tx);
let test_srv_tc_receiver = MpscTcReceiver::new(0, "test-receiver", test_srv_tc_rx);
let in_store_converter = EcssTcInVecConverter::default();
(
Self {
current_tm: None,
tc_sender: test_srv_tc_tx,
tm_receiver: tm_rx,
verification_handler: verification_handler.clone(),
},
PusServiceHandler::new(
Box::new(test_srv_tc_receiver),
Box::new(test_srv_tm_sender),
TEST_APID,
verification_handler,
in_store_converter,
),
)
}
pub fn send_tc(&mut self, tc: &PusTcCreator) -> VerificationToken<TcStateAccepted> {
let token = self.verification_handler.add_tc(tc);
let token = self
.verification_handler
.acceptance_success(token, Some(&[0; 7]))
.unwrap();
// Send accepted TC to test service handler.
self.tc_sender
.send(EcssTcAndToken::new(
TcInMemory::Vec(tc.to_vec().expect("pus tc conversion to vec failed")),
token,
))
.expect("sending tc failed");
token
}
pub fn read_next_tm(&mut self) -> PusTmReader<'_> {
let next_msg = self.tm_receiver.try_recv();
assert!(next_msg.is_ok());
self.current_tm = Some(next_msg.unwrap());
PusTmReader::new(self.current_tm.as_ref().unwrap(), 7)
.unwrap()
.0
}
pub fn check_next_verification_tm<STATE>(
&self,
subservice: u8,
token: VerificationToken<STATE>,
) {
let next_msg = self.tm_receiver.try_recv();
assert!(next_msg.is_ok());
let next_msg = next_msg.unwrap();
let tm = PusTmReader::new(next_msg.as_slice(), 7).unwrap().0;
assert_eq!(PusPacket::service(&tm), 1);
assert_eq!(PusPacket::subservice(&tm), subservice);
assert_eq!(tm.apid(), TEST_APID);
let req_id =
RequestId::from_bytes(tm.user_data()).expect("generating request ID failed");
assert_eq!(req_id, token.req_id());
}
}
}

View File

@ -113,37 +113,22 @@ impl<TcInMemConverter: EcssTcInMemConverter> PusService17TestHandler<TcInMemConv
#[cfg(test)]
mod tests {
use crate::pus::tests::{PusServiceHandlerWithStoreCommon, TEST_APID};
use crate::pus::verification::{
RequestId, TcStateAccepted, VerificationReporterCfg, VerificationReporterWithSender,
VerificationToken,
use crate::pus::tests::{
PusServiceHandlerWithStoreCommon, PusServiceHandlerWithVecCommon, PusTestHarness, TEST_APID,
};
use crate::pus::verification::{TcStateAccepted, VerificationToken};
use crate::pus::{
EcssTcAndToken, EcssTcInStoreConverter, EcssTcInVecConverter, MpscTcReceiver,
MpscTmAsVecSender, TcInMemory,
EcssTcInStoreConverter, EcssTcInVecConverter, PusPacketHandlerResult,
PusPacketHandlingError,
};
use delegate::delegate;
use spacepackets::ecss::tc::{PusTcCreator, PusTcSecondaryHeader};
use spacepackets::ecss::tm::PusTmReader;
use spacepackets::ecss::{PusPacket, WritablePusPacket};
use spacepackets::{CcsdsPacket, SequenceFlags, SpHeader};
use std::boxed::Box;
use std::sync::mpsc;
use spacepackets::ecss::PusPacket;
use spacepackets::{SequenceFlags, SpHeader};
use super::PusService17TestHandler;
fn verify_verification_tm<STATE>(
subservice: u8,
tm: &PusTmReader<'_>,
token: VerificationToken<STATE>,
) {
assert_eq!(tm.service(), 1);
assert_eq!(tm.subservice(), subservice);
assert_eq!(tm.apid(), TEST_APID);
let req_id = RequestId::from_bytes(tm.user_data()).expect("generating request ID failed");
assert_eq!(req_id, token.req_id());
}
struct Pus17HandlerWithStoreTester {
common: PusServiceHandlerWithStoreCommon,
handler: PusService17TestHandler<EcssTcInStoreConverter>,
@ -158,7 +143,9 @@ mod tests {
handler: pus_17_handler,
}
}
}
impl PusTestHarness for Pus17HandlerWithStoreTester {
delegate! {
to self.common {
fn send_tc(&mut self, tc: &PusTcCreator) -> VerificationToken<TcStateAccepted>;
@ -169,100 +156,129 @@ mod tests {
token: VerificationToken<STATE>,
);
}
to self.handler {
fn handle_one_tc(&mut self) -> Result<PusPacketHandlerResult, PusPacketHandlingError>;
}
}
}
#[test]
fn test_basic_ping_processing_using_store() {
let mut tester = Pus17HandlerWithStoreTester::new();
struct Pus17HandlerWithVecTester {
common: PusServiceHandlerWithVecCommon,
handler: PusService17TestHandler<EcssTcInVecConverter>,
}
impl Pus17HandlerWithVecTester {
pub fn new() -> Self {
let (common, srv_handler) = PusServiceHandlerWithVecCommon::new();
let pus_17_handler = PusService17TestHandler::new_from_service_handler(srv_handler);
Self {
common,
handler: pus_17_handler,
}
}
}
impl PusTestHarness for Pus17HandlerWithVecTester {
delegate! {
to self.common {
fn send_tc(&mut self, tc: &PusTcCreator) -> VerificationToken<TcStateAccepted>;
fn read_next_tm(&mut self) -> PusTmReader<'_>;
fn check_next_verification_tm<STATE>(
&self,
subservice: u8,
token: VerificationToken<STATE>,
);
}
to self.handler {
fn handle_one_tc(&mut self) -> Result<PusPacketHandlerResult, PusPacketHandlingError>;
}
}
}
fn ping_test(test_harness: &mut impl PusTestHarness) {
// Create a ping TC, verify acceptance.
let mut sp_header = SpHeader::tc(TEST_APID, SequenceFlags::Unsegmented, 0, 0).unwrap();
let sec_header = PusTcSecondaryHeader::new_simple(17, 1);
let ping_tc = PusTcCreator::new_no_app_data(&mut sp_header, sec_header, true);
let token = tester.send_tc(&ping_tc);
let result = tester.handler.handle_one_tc();
let token = test_harness.send_tc(&ping_tc);
let result = test_harness.handle_one_tc();
assert!(result.is_ok());
// We should see 4 replies in the TM queue now: Acceptance TM, Start TM, ping reply and
// Completion TM
// Acceptance TM
tester.check_next_verification_tm(1, token);
test_harness.check_next_verification_tm(1, token);
// Start TM
tester.check_next_verification_tm(3, token);
test_harness.check_next_verification_tm(3, token);
// Ping reply
let tm = tester.read_next_tm();
let tm = test_harness.read_next_tm();
assert_eq!(tm.service(), 17);
assert_eq!(tm.subservice(), 2);
assert!(tm.user_data().is_empty());
// TM completion
tester.check_next_verification_tm(7, token);
test_harness.check_next_verification_tm(7, token);
}
#[test]
fn test_sending_unsupported_service() {
let mut test_harness = Pus17HandlerWithStoreTester::new();
let mut sp_header = SpHeader::tc(TEST_APID, SequenceFlags::Unsegmented, 0, 0).unwrap();
let sec_header = PusTcSecondaryHeader::new_simple(3, 1);
let ping_tc = PusTcCreator::new_no_app_data(&mut sp_header, sec_header, true);
test_harness.send_tc(&ping_tc);
let result = test_harness.handle_one_tc();
assert!(result.is_err());
let error = result.unwrap_err();
if let PusPacketHandlingError::WrongService(num) = error {
assert_eq!(num, 3);
} else {
panic!("unexpected error type {error}")
}
}
#[test]
fn test_sending_custom_subservice() {
let mut test_harness = Pus17HandlerWithStoreTester::new();
let mut sp_header = SpHeader::tc(TEST_APID, SequenceFlags::Unsegmented, 0, 0).unwrap();
let sec_header = PusTcSecondaryHeader::new_simple(17, 200);
let ping_tc = PusTcCreator::new_no_app_data(&mut sp_header, sec_header, true);
test_harness.send_tc(&ping_tc);
let result = test_harness.handle_one_tc();
assert!(result.is_ok());
let result = result.unwrap();
if let PusPacketHandlerResult::CustomSubservice(subservice, _) = result {
assert_eq!(subservice, 200);
} else {
panic!("unexpected result type {result:?}")
}
}
#[test]
fn test_basic_ping_processing_using_store() {
let mut test_harness = Pus17HandlerWithStoreTester::new();
ping_test(&mut test_harness);
}
#[test]
fn test_basic_ping_processing_using_vec() {
let (test_srv_tc_tx, test_srv_tc_rx) = mpsc::channel();
let (tm_tx, tm_rx) = mpsc::channel();
let mut test_harness = Pus17HandlerWithVecTester::new();
ping_test(&mut test_harness);
}
let verif_sender = MpscTmAsVecSender::new(0, "verif_sender", tm_tx.clone());
let verif_cfg = VerificationReporterCfg::new(TEST_APID, 1, 2, 8).unwrap();
let mut verification_handler =
VerificationReporterWithSender::new(&verif_cfg, Box::new(verif_sender));
let test_srv_tm_sender = MpscTmAsVecSender::new(0, "TEST_SENDER", tm_tx);
let test_srv_tc_receiver = MpscTcReceiver::new(0, "TEST_RECEIVER", test_srv_tc_rx);
let in_store_converter = EcssTcInVecConverter::default();
let mut pus_17_handler = PusService17TestHandler::new(
Box::new(test_srv_tc_receiver),
Box::new(test_srv_tm_sender),
TEST_APID,
verification_handler.clone(),
in_store_converter,
);
// Create a ping TC, verify acceptance.
let mut sp_header = SpHeader::tc(TEST_APID, SequenceFlags::Unsegmented, 0, 0).unwrap();
let sec_header = PusTcSecondaryHeader::new_simple(17, 1);
let ping_tc = PusTcCreator::new_no_app_data(&mut sp_header, sec_header, true);
let init_token = verification_handler.add_tc(&ping_tc);
let token = verification_handler
.acceptance_success(init_token, None)
.expect("acceptance failed");
let ping_tc_as_vec = ping_tc.to_vec().unwrap();
let tc_in_memory = TcInMemory::from(ping_tc_as_vec);
test_srv_tc_tx
.send(EcssTcAndToken::new(tc_in_memory, token))
.unwrap();
let result = pus_17_handler.handle_one_tc();
if let Err(e) = result.as_ref() {
panic!("handling one tc failed with: {:?}", e);
#[test]
fn test_empty_tc_queue() {
let mut test_harness = Pus17HandlerWithStoreTester::new();
let result = test_harness.handle_one_tc();
assert!(result.is_ok());
let result = result.unwrap();
if let PusPacketHandlerResult::Empty = result {
} else {
panic!("unexpected result type {result:?}")
}
// We should see 4 replies in the TM queue now: Acceptance TM, Start TM, ping reply and
// Completion TM
let mut next_msg = tm_rx.try_recv();
assert!(next_msg.is_ok());
let mut tm_raw = next_msg.unwrap();
verify_verification_tm(1, &PusTmReader::new(&tm_raw, 0).unwrap().0, token);
next_msg = tm_rx.try_recv();
assert!(next_msg.is_ok());
tm_raw = next_msg.unwrap();
verify_verification_tm(3, &PusTmReader::new(&tm_raw, 7).unwrap().0, token);
next_msg = tm_rx.try_recv();
assert!(next_msg.is_ok());
tm_raw = next_msg.unwrap();
// Is generated with CDS short timestamp.
let (tm, _) = PusTmReader::new(&tm_raw, 7).unwrap();
assert_eq!(tm.service(), 17);
assert_eq!(tm.subservice(), 2);
assert!(tm.user_data().is_empty());
next_msg = tm_rx.try_recv();
assert!(next_msg.is_ok());
tm_raw = next_msg.unwrap();
verify_verification_tm(7, &PusTmReader::new(&tm_raw, 7).unwrap().0, token);
}
}