basic event testing framework

This commit is contained in:
Robin Müller 2022-10-15 21:14:27 +02:00
parent 0c9571f290
commit 183aa01b7f
No known key found for this signature in database
GPG Key ID: FC76078F520434A5
4 changed files with 219 additions and 136 deletions

View File

@ -1,7 +1,7 @@
//! Event support module //! Event support module
use spacepackets::{ByteConversionError, SizeMissmatch};
use spacepackets::ecss::EcssEnumeration; use spacepackets::ecss::EcssEnumeration;
use spacepackets::{ByteConversionError, SizeMissmatch};
pub type GroupId = u16; pub type GroupId = u16;
pub type UniqueId = u16; pub type UniqueId = u16;
@ -103,10 +103,11 @@ impl EcssEnumeration for Event {
if buf.len() < self.byte_width() { if buf.len() < self.byte_width() {
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
found: buf.len(), found: buf.len(),
expected: self.byte_width() expected: self.byte_width(),
})) }));
} }
Ok(buf.copy_from_slice(self.raw().to_be_bytes().as_slice())) buf.copy_from_slice(self.raw().to_be_bytes().as_slice());
Ok(())
} }
} }

View File

@ -255,12 +255,44 @@ mod allocvec {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::events::{Event, Severity};
use crate::pus::tests::CommonTmInfo;
use std::collections::VecDeque;
const EXAMPLE_APID: u16 = 0xee; const EXAMPLE_APID: u16 = 0xee;
const EXAMPLE_GROUP_ID: u16 = 2;
const EXAMPLE_EVENT_ID: u16 = 1;
#[derive(Debug, Eq, PartialEq)]
struct TmInfo {
pub common: CommonTmInfo,
}
#[derive(Default)]
struct TestSender {
pub service_queue: VecDeque<TmInfo>,
}
impl EcssTmSender<()> for TestSender {
fn send_tm(&mut self, tm: PusTm) -> Result<(), EcssTmError<()>> {
self.service_queue.push_back(TmInfo {
common: CommonTmInfo::new_from_tm(&tm),
});
Ok(())
}
}
#[test] #[test]
fn basic_event_generation() { fn basic_event_generation() {
let _reporter = EventReporter::new(EXAMPLE_APID, 16); let mut sender = TestSender::default();
//reporter. let reporter = EventReporter::new(EXAMPLE_APID, 16);
assert!(reporter.is_some());
let mut reporter = reporter.unwrap();
let time_stamp_empty: [u8; 7] = [0; 7];
let event = Event::new(Severity::INFO, EXAMPLE_GROUP_ID, EXAMPLE_EVENT_ID)
.expect("Error creating example event");
reporter
.event_info(&mut sender, &time_stamp_empty, event, None)
.expect("Error reporting info event");
} }
} }

View File

@ -55,55 +55,29 @@ pub(crate) fn source_buffer_large_enough<E>(cap: usize, len: usize) -> Result<()
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use crate::pus::verification::RequestId;
use crate::pus::{EcssTmError, EcssTmSender};
use alloc::vec::Vec;
use spacepackets::ecss::PusPacket;
use spacepackets::tm::{PusTm, PusTmSecondaryHeaderT}; use spacepackets::tm::{PusTm, PusTmSecondaryHeaderT};
use spacepackets::CcsdsPacket; use spacepackets::CcsdsPacket;
use std::collections::VecDeque;
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub(crate) struct TmInfo { pub(crate) struct CommonTmInfo {
pub subservice: u8, pub subservice: u8,
pub apid: u16, pub apid: u16,
pub msg_counter: u16, pub msg_counter: u16,
pub dest_id: u16, pub dest_id: u16,
pub time_stamp: [u8; 7], pub time_stamp: [u8; 7],
pub req_id: RequestId,
pub additional_data: Option<Vec<u8>>,
} }
#[derive(Default)] impl CommonTmInfo {
pub(crate) struct TestSender { pub fn new_from_tm(tm: &PusTm) -> Self {
pub service_queue: VecDeque<TmInfo>,
}
impl EcssTmSender<()> for TestSender {
fn send_tm(&mut self, tm: PusTm) -> Result<(), EcssTmError<()>> {
assert_eq!(PusPacket::service(&tm), 1);
assert!(tm.source_data().is_some());
let mut time_stamp = [0; 7]; let mut time_stamp = [0; 7];
time_stamp.clone_from_slice(&tm.time_stamp()[0..7]); time_stamp.clone_from_slice(&tm.time_stamp()[0..7]);
let src_data = tm.source_data().unwrap(); Self {
assert!(src_data.len() >= 4); subservice: tm.subservice(),
let req_id = RequestId::from_bytes(&src_data[0..RequestId::SIZE_AS_BYTES]).unwrap();
let mut vec = None;
if src_data.len() > 4 {
let mut new_vec = Vec::new();
new_vec.extend_from_slice(&src_data[RequestId::SIZE_AS_BYTES..]);
vec = Some(new_vec);
}
self.service_queue.push_back(TmInfo {
subservice: PusPacket::subservice(&tm),
apid: tm.apid(), apid: tm.apid(),
msg_counter: tm.msg_counter(), msg_counter: tm.msg_counter(),
dest_id: tm.dest_id(), dest_id: tm.dest_id(),
time_stamp, time_stamp,
req_id, }
additional_data: vec,
});
Ok(())
} }
} }
} }

View File

@ -1066,7 +1066,7 @@ mod stdmod {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::pus::tests::{TestSender, TmInfo}; use crate::pus::tests::CommonTmInfo;
use crate::pus::verification::{ use crate::pus::verification::{
EcssTmError, EcssTmSender, FailParams, FailParamsWithStep, RequestId, StateNone, EcssTmError, EcssTmSender, FailParams, FailParamsWithStep, RequestId, StateNone,
VerificationReporter, VerificationReporterCfg, VerificationReporterWithSender, VerificationReporter, VerificationReporterCfg, VerificationReporterWithSender,
@ -1074,14 +1074,52 @@ mod tests {
}; };
use alloc::boxed::Box; use alloc::boxed::Box;
use alloc::format; use alloc::format;
use spacepackets::ecss::{EcssEnumU16, EcssEnumU32, EcssEnumU8, EcssEnumeration}; use spacepackets::ecss::{EcssEnumU16, EcssEnumU32, EcssEnumU8, EcssEnumeration, PusPacket};
use spacepackets::tc::{PusTc, PusTcSecondaryHeader}; use spacepackets::tc::{PusTc, PusTcSecondaryHeader};
use spacepackets::tm::PusTm; use spacepackets::tm::PusTm;
use spacepackets::{ByteConversionError, SpHeader}; use spacepackets::{ByteConversionError, SpHeader};
use std::collections::VecDeque;
use std::vec::Vec;
const TEST_APID: u16 = 0x02; const TEST_APID: u16 = 0x02;
const EMPTY_STAMP: [u8; 7] = [0; 7]; const EMPTY_STAMP: [u8; 7] = [0; 7];
#[derive(Debug, Eq, PartialEq)]
struct TmInfo {
pub common: CommonTmInfo,
pub req_id: RequestId,
pub additional_data: Option<Vec<u8>>,
}
#[derive(Default)]
struct TestSender {
pub service_queue: VecDeque<TmInfo>,
}
impl EcssTmSender<()> for TestSender {
fn send_tm(&mut self, tm: PusTm) -> Result<(), EcssTmError<()>> {
assert_eq!(PusPacket::service(&tm), 1);
assert!(tm.source_data().is_some());
let mut time_stamp = [0; 7];
time_stamp.clone_from_slice(&tm.time_stamp()[0..7]);
let src_data = tm.source_data().unwrap();
assert!(src_data.len() >= 4);
let req_id = RequestId::from_bytes(&src_data[0..RequestId::SIZE_AS_BYTES]).unwrap();
let mut vec = None;
if src_data.len() > 4 {
let mut new_vec = Vec::new();
new_vec.extend_from_slice(&src_data[RequestId::SIZE_AS_BYTES..]);
vec = Some(new_vec);
}
self.service_queue.push_back(TmInfo {
common: CommonTmInfo::new_from_tm(&tm),
req_id,
additional_data: vec,
});
Ok(())
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
struct DummyError {} struct DummyError {}
#[derive(Default)] #[derive(Default)]
@ -1155,11 +1193,13 @@ mod tests {
fn acceptance_check(sender: &mut TestSender, req_id: &RequestId) { fn acceptance_check(sender: &mut TestSender, req_id: &RequestId) {
let cmp_info = TmInfo { let cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 1, subservice: 1,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 0, msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: None, additional_data: None,
req_id: req_id.clone(), req_id: req_id.clone(),
}; };
@ -1214,11 +1254,13 @@ mod tests {
fn acceptance_fail_check(sender: &mut TestSender, req_id: RequestId, stamp_buf: [u8; 7]) { fn acceptance_fail_check(sender: &mut TestSender, req_id: RequestId, stamp_buf: [u8; 7]) {
let cmp_info = TmInfo { let cmp_info = TmInfo {
time_stamp: stamp_buf, common: CommonTmInfo {
subservice: 2, subservice: 2,
dest_id: 5,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 0, msg_counter: 0,
dest_id: 5,
time_stamp: stamp_buf,
},
additional_data: Some([0, 2].to_vec()), additional_data: Some([0, 2].to_vec()),
req_id, req_id,
}; };
@ -1300,11 +1342,13 @@ mod tests {
b.vr.acceptance_failure(tok, &mut sender, fail_params) b.vr.acceptance_failure(tok, &mut sender, fail_params)
.expect("Sending acceptance success failed"); .expect("Sending acceptance success failed");
let cmp_info = TmInfo { let cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 2, subservice: 2,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 0, msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: Some([10, 0, 0, 0, 12].to_vec()), additional_data: Some([10, 0, 0, 0, 12].to_vec()),
req_id: tok.req_id, req_id: tok.req_id,
}; };
@ -1316,11 +1360,13 @@ mod tests {
fn start_fail_check(sender: &mut TestSender, req_id: RequestId, fail_data_raw: [u8; 4]) { fn start_fail_check(sender: &mut TestSender, req_id: RequestId, fail_data_raw: [u8; 4]) {
assert_eq!(sender.service_queue.len(), 2); assert_eq!(sender.service_queue.len(), 2);
let mut cmp_info = TmInfo { let mut cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 1, subservice: 1,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 0, msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: None, additional_data: None,
req_id, req_id,
}; };
@ -1328,11 +1374,13 @@ mod tests {
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
cmp_info = TmInfo { cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 4, subservice: 4,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 1, msg_counter: 1,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: Some([&[22], fail_data_raw.as_slice()].concat().to_vec()), additional_data: Some([&[22], fail_data_raw.as_slice()].concat().to_vec()),
req_id, req_id,
}; };
@ -1384,44 +1432,52 @@ mod tests {
fn step_success_check(sender: &mut TestSender, req_id: RequestId) { fn step_success_check(sender: &mut TestSender, req_id: RequestId) {
let mut cmp_info = TmInfo { let mut cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 1, subservice: 1,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 0, msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: None, additional_data: None,
req_id, req_id,
}; };
let mut info = sender.service_queue.pop_front().unwrap(); let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
cmp_info = TmInfo { cmp_info = TmInfo {
time_stamp: [0, 1, 0, 1, 0, 1, 0], common: CommonTmInfo {
subservice: 3, subservice: 3,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 1, msg_counter: 1,
dest_id: 0,
time_stamp: [0, 1, 0, 1, 0, 1, 0],
},
additional_data: None, additional_data: None,
req_id, req_id,
}; };
info = sender.service_queue.pop_front().unwrap(); info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
cmp_info = TmInfo { cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 5, subservice: 5,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 2, msg_counter: 2,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: Some([0].to_vec()), additional_data: Some([0].to_vec()),
req_id, req_id,
}; };
info = sender.service_queue.pop_front().unwrap(); info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
cmp_info = TmInfo { cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 5, subservice: 5,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 3, msg_counter: 3,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: Some([1].to_vec()), additional_data: Some([1].to_vec()),
req_id, req_id,
}; };
@ -1493,11 +1549,13 @@ mod tests {
fn check_step_failure(sender: &mut TestSender, req_id: RequestId, fail_data_raw: [u8; 4]) { fn check_step_failure(sender: &mut TestSender, req_id: RequestId, fail_data_raw: [u8; 4]) {
assert_eq!(sender.service_queue.len(), 4); assert_eq!(sender.service_queue.len(), 4);
let mut cmp_info = TmInfo { let mut cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 1, subservice: 1,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 0, msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: None, additional_data: None,
req_id, req_id,
}; };
@ -1505,11 +1563,13 @@ mod tests {
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
cmp_info = TmInfo { cmp_info = TmInfo {
time_stamp: [0, 1, 0, 1, 0, 1, 0], common: CommonTmInfo {
subservice: 3, subservice: 3,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 1, msg_counter: 1,
dest_id: 0,
time_stamp: [0, 1, 0, 1, 0, 1, 0],
},
additional_data: None, additional_data: None,
req_id, req_id,
}; };
@ -1517,11 +1577,13 @@ mod tests {
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
cmp_info = TmInfo { cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 5, subservice: 5,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 2, msg_counter: 2,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: Some([0].to_vec()), additional_data: Some([0].to_vec()),
req_id, req_id,
}; };
@ -1529,11 +1591,13 @@ mod tests {
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
cmp_info = TmInfo { cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 6, subservice: 6,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 3, msg_counter: 3,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: Some( additional_data: Some(
[ [
[1].as_slice(), [1].as_slice(),
@ -1630,11 +1694,13 @@ mod tests {
assert_eq!(sender.service_queue.len(), 3); assert_eq!(sender.service_queue.len(), 3);
let mut cmp_info = TmInfo { let mut cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 1, subservice: 1,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 0, msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: None, additional_data: None,
req_id, req_id,
}; };
@ -1642,11 +1708,13 @@ mod tests {
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
cmp_info = TmInfo { cmp_info = TmInfo {
time_stamp: [0, 1, 0, 1, 0, 1, 0], common: CommonTmInfo {
subservice: 3, subservice: 3,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 1, msg_counter: 1,
dest_id: 0,
time_stamp: [0, 1, 0, 1, 0, 1, 0],
},
additional_data: None, additional_data: None,
req_id, req_id,
}; };
@ -1654,11 +1722,13 @@ mod tests {
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
cmp_info = TmInfo { cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 8, subservice: 8,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 2, msg_counter: 2,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: Some([0, 0, 0x10, 0x20].to_vec()), additional_data: Some([0, 0, 0x10, 0x20].to_vec()),
req_id, req_id,
}; };
@ -1714,11 +1784,13 @@ mod tests {
fn completion_success_check(sender: &mut TestSender, req_id: RequestId) { fn completion_success_check(sender: &mut TestSender, req_id: RequestId) {
assert_eq!(sender.service_queue.len(), 3); assert_eq!(sender.service_queue.len(), 3);
let cmp_info = TmInfo { let cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 1, subservice: 1,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 0, msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: None, additional_data: None,
req_id, req_id,
}; };
@ -1726,22 +1798,26 @@ mod tests {
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
let cmp_info = TmInfo { let cmp_info = TmInfo {
time_stamp: [0, 1, 0, 1, 0, 1, 0], common: CommonTmInfo {
subservice: 3, subservice: 3,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 1, msg_counter: 1,
dest_id: 0,
time_stamp: [0, 1, 0, 1, 0, 1, 0],
},
additional_data: None, additional_data: None,
req_id, req_id,
}; };
info = sender.service_queue.pop_front().unwrap(); info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info); assert_eq!(info, cmp_info);
let cmp_info = TmInfo { let cmp_info = TmInfo {
time_stamp: EMPTY_STAMP, common: CommonTmInfo {
subservice: 7, subservice: 7,
dest_id: 0,
apid: TEST_APID, apid: TEST_APID,
msg_counter: 2, msg_counter: 2,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
additional_data: None, additional_data: None,
req_id, req_id,
}; };