From d7be5224f148cebb73d01dc8d4fb7d4928d249d5 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 17 Feb 2024 01:07:01 +0100 Subject: [PATCH] thats one hell of a test suite.. --- satrs/src/pus/action.rs | 104 ++++++++++++++++++++++++++++++---- satrs/src/pus/mod.rs | 44 ++++++++++++-- satrs/src/pus/test.rs | 9 ++- satrs/src/pus/verification.rs | 33 +++++++---- 4 files changed, 160 insertions(+), 30 deletions(-) diff --git a/satrs/src/pus/action.rs b/satrs/src/pus/action.rs index ee2eafc..4b7c018 100644 --- a/satrs/src/pus/action.rs +++ b/satrs/src/pus/action.rs @@ -167,12 +167,27 @@ pub mod std_mod { #[cfg(test)] mod tests { - use spacepackets::ecss::tc::PusTcReader; + use core::cell::RefCell; - use crate::pus::{verification::VerificationReportingProvider, GenericRoutingError}; + use alloc::{collections::VecDeque, vec::Vec}; + use spacepackets::{ + ecss::{tc::PusTcReader, PusPacket}, + CcsdsPacket, + }; + + use crate::pus::{ + tests::PusServiceHandlerWithVecCommon, + verification::{tests::TestVerificationReporter, VerificationReportingProvider}, + EcssTcInVecConverter, GenericRoutingError, PusPacketHandlingError, PusRoutingErrorHandler, + PusServiceHelper, + }; use super::*; - pub struct TestRouter {} + + #[derive(Default)] + pub struct TestRouter { + pub routing_requests: RefCell>, + } impl PusActionRequestRouter for TestRouter { type Error = GenericRoutingError; @@ -181,23 +196,92 @@ mod tests { &self, target_id: TargetId, hk_request: ActionRequest, - token: VerificationToken, + _token: VerificationToken, ) -> Result<(), Self::Error> { + self.routing_requests + .borrow_mut() + .push_back((target_id, hk_request)); + Ok(()) + } + } + + impl PusRoutingErrorHandler for TestRouter { + type Error = GenericRoutingError; + + fn handle_error( + &self, + target_id: TargetId, + token: VerificationToken, + tc: &PusTcReader, + error: Self::Error, + time_stamp: &[u8], + verif_reporter: &impl VerificationReportingProvider, + ) { todo!() } } - pub struct TestConverter {} + #[derive(Default)] + pub struct TestConverter { + pub conversion_request: VecDeque>, + } + impl PusActionToRequestConverter for TestConverter { - type Error = GenericRoutingError; + type Error = PusPacketHandlingError; fn convert( &mut self, - token: VerificationToken, + _token: VerificationToken, tc: &PusTcReader, - time_stamp: &[u8], - verif_reporter: &impl VerificationReportingProvider, + _time_stamp: &[u8], + _verif_reporter: &impl VerificationReportingProvider, ) -> Result<(TargetId, ActionRequest), Self::Error> { - todo!() + self.conversion_request.push_back(tc.raw_data().to_vec()); + let target_id = tc.apid(); + if tc.subservice() == 1 { + return Ok(( + target_id.into(), + ActionRequest::UnsignedIdAndVecData { + action_id: u32::from_be_bytes(tc.user_data()[0..4].try_into().unwrap()), + data: tc.user_data()[4..].to_vec(), + }, + )); + } + Err(PusPacketHandlingError::InvalidAppData( + "unexpected app data".into(), + )) } } + + struct Pus8HandlerWithVecTester { + common: PusServiceHandlerWithVecCommon, + handler: PusService8ActionHandler< + EcssTcInVecConverter, + TestVerificationReporter, + TestConverter, + TestRouter, + TestRouter, + >, + } + + impl Pus8HandlerWithVecTester { + pub fn new() -> Self { + let (common, srv_handler) = + PusServiceHandlerWithVecCommon::new_with_test_verif_sender(); + Self { + common, + handler: PusService8ActionHandler::new( + srv_handler, + TestConverter::default(), + TestRouter::default(), + TestRouter::default(), + ), + } + } + } + + #[test] + fn basic_test() { + //let service_helper = PusServiceHelper::new(); + let action_handler = Pus8HandlerWithVecTester::new(); + } } diff --git a/satrs/src/pus/mod.rs b/satrs/src/pus/mod.rs index a65926a..aa83876 100644 --- a/satrs/src/pus/mod.rs +++ b/satrs/src/pus/mod.rs @@ -947,6 +947,7 @@ pub mod tests { use crate::pus::verification::RequestId; use crate::tmtc::tm_helper::SharedTmPool; + use super::verification::tests::TestVerificationReporter; use super::verification::{ TcStateAccepted, VerificationReporterCfg, VerificationReporterWithSender, VerificationReportingProvider, VerificationToken, @@ -1105,15 +1106,15 @@ pub mod tests { } } - pub struct PusServiceHandlerWithVecCommon { + pub struct PusServiceHandlerWithVecCommon { current_tm: Option>, tc_sender: mpsc::Sender, tm_receiver: mpsc::Receiver>, - verification_handler: VerificationReporterWithSender, + verification_handler: VerificationReporter, } - impl PusServiceHandlerWithVecCommon { - pub fn new() -> ( + impl PusServiceHandlerWithVecCommon { + pub fn new_with_standard_verif_reporter() -> ( Self, PusServiceHelper, ) { @@ -1124,6 +1125,7 @@ pub mod tests { 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(); @@ -1143,7 +1145,41 @@ pub mod tests { ), ) } + } + impl PusServiceHandlerWithVecCommon { + pub fn new_with_test_verif_sender() -> ( + Self, + PusServiceHelper, + ) { + let (test_srv_tc_tx, test_srv_tc_rx) = mpsc::channel(); + let (tm_tx, tm_rx) = mpsc::channel(); + + 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 verification_handler = TestVerificationReporter::default(); + ( + Self { + current_tm: None, + tc_sender: test_srv_tc_tx, + tm_receiver: tm_rx, + verification_handler: verification_handler.clone(), + }, + PusServiceHelper::new( + Box::new(test_srv_tc_receiver), + Box::new(test_srv_tm_sender), + TEST_APID, + verification_handler, + in_store_converter, + ), + ) + } + } + + impl + PusServiceHandlerWithVecCommon + { pub fn send_tc(&mut self, tc: &PusTcCreator) -> VerificationToken { let token = self.verification_handler.add_tc(tc); let token = self diff --git a/satrs/src/pus/test.rs b/satrs/src/pus/test.rs index 40b23b3..c8fa1e3 100644 --- a/satrs/src/pus/test.rs +++ b/satrs/src/pus/test.rs @@ -104,9 +104,7 @@ mod tests { PusServiceHandlerWithSharedStoreCommon, PusServiceHandlerWithVecCommon, PusTestHarness, SimplePusPacketHandler, TEST_APID, }; - use crate::pus::verification::{ - RequestId, VerificationReporterWithSender, VerificationReportingProvider, - }; + use crate::pus::verification::{RequestId, VerificationReporterWithSender}; use crate::pus::verification::{TcStateAccepted, VerificationToken}; use crate::pus::{ EcssTcInSharedStoreConverter, EcssTcInVecConverter, PusPacketHandlerResult, @@ -160,13 +158,14 @@ mod tests { } struct Pus17HandlerWithVecTester { - common: PusServiceHandlerWithVecCommon, + common: PusServiceHandlerWithVecCommon, handler: PusService17TestHandler, } impl Pus17HandlerWithVecTester { pub fn new() -> Self { - let (common, srv_handler) = PusServiceHandlerWithVecCommon::new(); + let (common, srv_handler) = + PusServiceHandlerWithVecCommon::new_with_standard_verif_reporter(); Self { common, handler: PusService17TestHandler::new(srv_handler), diff --git a/satrs/src/pus/verification.rs b/satrs/src/pus/verification.rs index ae61c64..199d1a5 100644 --- a/satrs/src/pus/verification.rs +++ b/satrs/src/pus/verification.rs @@ -1407,6 +1407,7 @@ pub mod tests { use crate::ChannelId; use alloc::boxed::Box; use alloc::format; + use alloc::sync::Arc; use hashbrown::HashMap; use spacepackets::ecss::tc::{PusTcCreator, PusTcSecondaryHeader}; use spacepackets::ecss::tm::PusTmReader; @@ -1415,7 +1416,7 @@ pub mod tests { use spacepackets::{ByteConversionError, CcsdsPacket, SpHeader}; use std::cell::RefCell; use std::collections::VecDeque; - use std::sync::mpsc; + use std::sync::{mpsc, Mutex}; use std::time::Duration; use std::vec; use std::vec::Vec; @@ -1436,13 +1437,15 @@ pub mod tests { pub fail_enum: Option>, } + #[derive(Default, Clone)] pub struct TestVerificationReporter { - pub verification_map: RefCell>, + pub verification_map: Arc>>>, } impl VerificationReportingProvider for TestVerificationReporter { fn add_tc_with_req_id(&mut self, req_id: RequestId) -> VerificationToken { - self.verification_map.borrow_mut().insert( + let verif_map = self.verification_map.lock().unwrap(); + verif_map.borrow_mut().insert( req_id, VerificationStatus { accepted: None, @@ -1468,7 +1471,8 @@ pub mod tests { VerificationToken, super::VerificationOrSendErrorWithToken, > { - match self.verification_map.borrow_mut().get_mut(&token.req_id) { + let verif_map = self.verification_map.lock().unwrap(); + match verif_map.borrow_mut().get_mut(&token.req_id) { Some(entry) => entry.accepted = Some(true), None => panic!( "unexpected acceptance success for request ID {}", @@ -1486,7 +1490,8 @@ pub mod tests { token: VerificationToken, params: FailParams, ) -> Result<(), super::VerificationOrSendErrorWithToken> { - match self.verification_map.borrow_mut().get_mut(&token.req_id) { + let verif_map = self.verification_map.lock().unwrap(); + match verif_map.borrow_mut().get_mut(&token.req_id) { Some(entry) => { entry.accepted = Some(false); entry.failure_data = params.failure_data.map(|v| v.to_vec()); @@ -1509,7 +1514,8 @@ pub mod tests { VerificationToken, super::VerificationOrSendErrorWithToken, > { - match self.verification_map.borrow_mut().get_mut(&token.req_id) { + let verif_map = self.verification_map.lock().unwrap(); + match verif_map.borrow_mut().get_mut(&token.req_id) { Some(entry) => entry.started = Some(true), None => panic!("unexpected start success for request ID {}", token.req_id()), }; @@ -1524,7 +1530,8 @@ pub mod tests { token: VerificationToken, params: FailParams, ) -> Result<(), super::VerificationOrSendErrorWithToken> { - match self.verification_map.borrow_mut().get_mut(&token.req_id) { + let verif_map = self.verification_map.lock().unwrap(); + match verif_map.borrow_mut().get_mut(&token.req_id) { Some(entry) => { entry.started = Some(false); entry.failure_data = params.failure_data.map(|v| v.to_vec()); @@ -1542,7 +1549,8 @@ pub mod tests { _time_stamp: Option<&[u8]>, _step: impl spacepackets::ecss::EcssEnumeration, ) -> Result<(), EcssTmtcError> { - match self.verification_map.borrow_mut().get_mut(&token.req_id) { + let verif_map = self.verification_map.lock().unwrap(); + match verif_map.borrow_mut().get_mut(&token.req_id) { Some(entry) => { // TODO: Enable this after the spacepackets update. // entry.step = Some(step.value()), @@ -1558,7 +1566,8 @@ pub mod tests { token: VerificationToken, _params: FailParamsWithStep, ) -> Result<(), super::VerificationOrSendErrorWithToken> { - match self.verification_map.borrow_mut().get_mut(&token.req_id) { + let verif_map = self.verification_map.lock().unwrap(); + match verif_map.borrow_mut().get_mut(&token.req_id) { Some(entry) => { // TODO: Enable this after the spacepackets update. // entry.step = Some(step.value()), @@ -1574,7 +1583,8 @@ pub mod tests { token: VerificationToken, _time_stamp: Option<&[u8]>, ) -> Result<(), super::VerificationOrSendErrorWithToken> { - match self.verification_map.borrow_mut().get_mut(&token.req_id) { + let verif_map = self.verification_map.lock().unwrap(); + match verif_map.borrow_mut().get_mut(&token.req_id) { Some(entry) => entry.completed = Some(true), None => panic!( "unexpected acceptance success for request ID {}", @@ -1589,7 +1599,8 @@ pub mod tests { token: VerificationToken, params: FailParams, ) -> Result<(), super::VerificationOrSendErrorWithToken> { - match self.verification_map.borrow_mut().get_mut(&token.req_id) { + let verif_map = self.verification_map.lock().unwrap(); + match verif_map.borrow_mut().get_mut(&token.req_id) { Some(entry) => { entry.completed = Some(false); entry.failure_data = params.failure_data.map(|v| v.to_vec());