oh shit gotta go
Some checks failed
Rust/spacepackets/pipeline/head There was a failure building this commit

This commit is contained in:
Robin Müller 2023-12-05 10:08:19 +01:00
parent 13b9ca356c
commit 08b1ddc41d
Signed by: muellerr
GPG Key ID: A649FB78196E3849
3 changed files with 63 additions and 18 deletions

View File

@ -169,18 +169,13 @@ mod tests {
use crate::cfdp::pdu::{FileDirectiveType, PduHeader}; use crate::cfdp::pdu::{FileDirectiveType, PduHeader};
use crate::cfdp::{ConditionCode, CrcFlag, LargeFileFlag, PduType, TransmissionMode}; use crate::cfdp::{ConditionCode, CrcFlag, LargeFileFlag, PduType, TransmissionMode};
#[test] fn verify_state(&eof_pdu: &EofPdu, file_flag: LargeFileFlag) {
fn test_basic() {
let pdu_conf = common_pdu_conf(CrcFlag::NoCrc, LargeFileFlag::Normal);
let pdu_header = PduHeader::new_no_file_data(pdu_conf, 0);
let eof_pdu = EofPdu::new_no_error(pdu_header, 0x01020304, 12);
assert_eq!(eof_pdu.len_written(), pdu_header.header_len() + 2 + 4 + 4);
assert_eq!(eof_pdu.file_checksum(), 0x01020304); assert_eq!(eof_pdu.file_checksum(), 0x01020304);
assert_eq!(eof_pdu.file_size(), 12); assert_eq!(eof_pdu.file_size(), 12);
assert_eq!(eof_pdu.condition_code(), ConditionCode::NoError); assert_eq!(eof_pdu.condition_code(), ConditionCode::NoError);
assert_eq!(eof_pdu.crc_flag(), CrcFlag::NoCrc); assert_eq!(eof_pdu.crc_flag(), CrcFlag::NoCrc);
assert_eq!(eof_pdu.file_flag(), LargeFileFlag::Normal); assert_eq!(eof_pdu.file_flag(), file_flag);
assert_eq!(eof_pdu.pdu_type(), PduType::FileDirective); assert_eq!(eof_pdu.pdu_type(), PduType::FileDirective);
assert_eq!( assert_eq!(
eof_pdu.file_directive_type(), eof_pdu.file_directive_type(),
@ -193,6 +188,15 @@ mod tests {
assert_eq!(eof_pdu.transaction_seq_num(), TEST_SEQ_NUM.into()); assert_eq!(eof_pdu.transaction_seq_num(), TEST_SEQ_NUM.into());
} }
#[test]
fn test_basic() {
let pdu_conf = common_pdu_conf(CrcFlag::NoCrc, LargeFileFlag::Normal);
let pdu_header = PduHeader::new_no_file_data(pdu_conf, 0);
let eof_pdu = EofPdu::new_no_error(pdu_header, 0x01020304, 12);
assert_eq!(eof_pdu.len_written(), pdu_header.header_len() + 2 + 4 + 4);
verify_state(&eof_pdu, LargeFileFlag::Normal);
}
#[test] #[test]
fn test_serialization() { fn test_serialization() {
let pdu_conf = common_pdu_conf(CrcFlag::NoCrc, LargeFileFlag::Normal); let pdu_conf = common_pdu_conf(CrcFlag::NoCrc, LargeFileFlag::Normal);
@ -270,4 +274,13 @@ mod tests {
panic!("expected crc error"); panic!("expected crc error");
} }
} }
#[test]
fn test_with_large_file_flag() {
let pdu_conf = common_pdu_conf(CrcFlag::NoCrc, LargeFileFlag::Large);
let pdu_header = PduHeader::new_no_file_data(pdu_conf, 0);
let eof_pdu = EofPdu::new_no_error(pdu_header, 0x01020304, 12);
verify_state(&eof_pdu, LargeFileFlag::Large);
assert_eq!(eof_pdu.len_written(), pdu_header.header_len() + 2 + 8 + 4);
}
} }

View File

@ -32,7 +32,7 @@ pub enum FileDirectiveType {
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PduError { pub enum PduError {
ByteConversionError(ByteConversionError), ByteConversion(ByteConversionError),
/// Found version ID invalid, not equal to [CFDP_VERSION_2]. /// Found version ID invalid, not equal to [CFDP_VERSION_2].
CfdpVersionMissmatch(u8), CfdpVersionMissmatch(u8),
/// Invalid length for the entity ID detected. Only the values 1, 2, 4 and 8 are supported. /// Invalid length for the entity ID detected. Only the values 1, 2, 4 and 8 are supported.
@ -107,7 +107,7 @@ impl Display for PduError {
"missmatch of PDU source length {src_id_len} and destination length {dest_id_len}" "missmatch of PDU source length {src_id_len} and destination length {dest_id_len}"
) )
} }
PduError::ByteConversionError(e) => { PduError::ByteConversion(e) => {
write!(f, "{}", e) write!(f, "{}", e)
} }
PduError::FileSizeTooLarge(value) => { PduError::FileSizeTooLarge(value) => {
@ -145,7 +145,7 @@ impl Display for PduError {
impl Error for PduError { impl Error for PduError {
fn source(&self) -> Option<&(dyn Error + 'static)> { fn source(&self) -> Option<&(dyn Error + 'static)> {
match self { match self {
PduError::ByteConversionError(e) => Some(e), PduError::ByteConversion(e) => Some(e),
PduError::TlvLvError(e) => Some(e), PduError::TlvLvError(e) => Some(e),
_ => None, _ => None,
} }
@ -154,7 +154,7 @@ impl Error for PduError {
impl From<ByteConversionError> for PduError { impl From<ByteConversionError> for PduError {
fn from(value: ByteConversionError) -> Self { fn from(value: ByteConversionError) -> Self {
Self::ByteConversionError(value) Self::ByteConversion(value)
} }
} }
@ -526,7 +526,7 @@ impl PduHeader {
/// function. /// function.
pub fn from_bytes(buf: &[u8]) -> Result<(Self, usize), PduError> { pub fn from_bytes(buf: &[u8]) -> Result<(Self, usize), PduError> {
if buf.len() < FIXED_HEADER_LEN { if buf.len() < FIXED_HEADER_LEN {
return Err(PduError::ByteConversionError( return Err(PduError::ByteConversion(
ByteConversionError::FromSliceTooSmall { ByteConversionError::FromSliceTooSmall {
found: buf.len(), found: buf.len(),
expected: FIXED_HEADER_LEN, expected: FIXED_HEADER_LEN,
@ -689,6 +689,8 @@ pub(crate) fn add_pdu_crc(buf: &mut [u8], mut current_idx: usize) -> usize {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use alloc::string::ToString;
use crate::cfdp::pdu::{CommonPduConfig, PduError, PduHeader, FIXED_HEADER_LEN}; use crate::cfdp::pdu::{CommonPduConfig, PduError, PduHeader, FIXED_HEADER_LEN};
use crate::cfdp::{ use crate::cfdp::{
CrcFlag, Direction, LargeFileFlag, PduType, SegmentMetadataFlag, SegmentationControl, CrcFlag, Direction, LargeFileFlag, PduType, SegmentMetadataFlag, SegmentationControl,
@ -946,6 +948,7 @@ mod tests {
let error = res.unwrap_err(); let error = res.unwrap_err();
if let PduError::CfdpVersionMissmatch(raw_version) = error { if let PduError::CfdpVersionMissmatch(raw_version) = error {
assert_eq!(raw_version, CFDP_VERSION_2 + 1); assert_eq!(raw_version, CFDP_VERSION_2 + 1);
assert_eq!(error.to_string(), "cfdp version missmatch, found 2, expected 1");
} else { } else {
panic!("invalid exception: {}", error); panic!("invalid exception: {}", error);
} }
@ -957,7 +960,7 @@ mod tests {
let res = PduHeader::from_bytes(&buf); let res = PduHeader::from_bytes(&buf);
assert!(res.is_err()); assert!(res.is_err());
let error = res.unwrap_err(); let error = res.unwrap_err();
if let PduError::ByteConversionError(ByteConversionError::FromSliceTooSmall { if let PduError::ByteConversion(ByteConversionError::FromSliceTooSmall {
found, found,
expected, expected,
}) = error }) = error
@ -983,13 +986,17 @@ mod tests {
let header = PduHeader::from_bytes(&buf[0..6]); let header = PduHeader::from_bytes(&buf[0..6]);
assert!(header.is_err()); assert!(header.is_err());
let error = header.unwrap_err(); let error = header.unwrap_err();
if let PduError::ByteConversionError(ByteConversionError::FromSliceTooSmall { if let PduError::ByteConversion(ByteConversionError::FromSliceTooSmall {
found, found,
expected, expected,
}) = error }) = error
{ {
assert_eq!(found, 6); assert_eq!(found, 6);
assert_eq!(expected, 7); assert_eq!(expected, 7);
assert_eq!(
error.to_string(),
"source slice with size 6 too small, expected at least 7 bytes"
);
} }
} }
@ -1017,6 +1024,10 @@ mod tests {
let error = pdu_conf_res.unwrap_err(); let error = pdu_conf_res.unwrap_err();
if let PduError::InvalidEntityLen(len) = error { if let PduError::InvalidEntityLen(len) = error {
assert_eq!(len, 3); assert_eq!(len, 3);
assert_eq!(
error.to_string(),
"invalid PDU entity ID length 3, only [1, 2, 4, 8] are allowed"
);
} else { } else {
panic!("Invalid exception: {}", error) panic!("Invalid exception: {}", error)
} }
@ -1087,4 +1098,20 @@ mod tests {
panic!("invalid exception {:?}", error) panic!("invalid exception {:?}", error)
} }
} }
#[test]
fn test_pdu_error_clonable_and_comparable() {
let pdu_error = PduError::InvalidEntityLen(0);
let pdu_error_2 = pdu_error;
assert_eq!(pdu_error, pdu_error_2);
}
#[test]
fn test_pdu_config_clonable_and_comparable() {
let common_pdu_cfg_0 =
CommonPduConfig::new_with_byte_fields(UbfU8::new(1), UbfU8::new(2), UbfU8::new(3))
.expect("common config creation failed");
let common_pdu_cfg_1 = common_pdu_cfg_0;
assert_eq!(common_pdu_cfg_0, common_pdu_cfg_1);
}
} }

View File

@ -398,7 +398,7 @@ impl<'seg_reqs> NakPduReader<'seg_reqs> {
let end_of_scope; let end_of_scope;
if pdu_header.common_pdu_conf().file_flag == LargeFileFlag::Large { if pdu_header.common_pdu_conf().file_flag == LargeFileFlag::Large {
if current_idx + 16 > buf.len() { if current_idx + 16 > buf.len() {
return Err(PduError::ByteConversionError( return Err(PduError::ByteConversion(
ByteConversionError::FromSliceTooSmall { ByteConversionError::FromSliceTooSmall {
found: buf.len(), found: buf.len(),
expected: current_idx + 16, expected: current_idx + 16,
@ -734,12 +734,17 @@ mod tests {
let pdu_conf = common_pdu_conf(CrcFlag::NoCrc, LargeFileFlag::Normal); let pdu_conf = common_pdu_conf(CrcFlag::NoCrc, LargeFileFlag::Normal);
let pdu_header = PduHeader::new_no_file_data(pdu_conf, 0); let pdu_header = PduHeader::new_no_file_data(pdu_conf, 0);
let u32_list = SegmentRequests::U32Pairs(&[(0, 50), (50, 100)]); let u32_list = SegmentRequests::U32Pairs(&[(0, 50), (50, 100)]);
if let Err(PduError::InvalidStartOrEndOfScopeValue) = NakPduCreator::new_generic( //let error = NakPduCreator::new_generic(pdu_header, 100, 300, Some(u32_list));
let error = NakPduCreator::new_generic(
pdu_header, pdu_header,
u32::MAX as u64 + 1, u32::MAX as u64 + 1,
u32::MAX as u64 + 2, u32::MAX as u64 + 2,
Some(u32_list), Some(u32_list),
) { );
assert!(error.is_err());
if let Err(PduError::InvalidStartOrEndOfScopeValue) = error {
assert_eq!(error.to_string(), )
} else { } else {
panic!("API call did not fail"); panic!("API call did not fail");
} }
@ -758,7 +763,7 @@ mod tests {
assert!(error.is_err()); assert!(error.is_err());
let e = error.unwrap_err(); let e = error.unwrap_err();
match e { match e {
PduError::ByteConversionError(conv_error) => match conv_error { PduError::ByteConversion(conv_error) => match conv_error {
ByteConversionError::ToSliceTooSmall { found, expected } => { ByteConversionError::ToSliceTooSmall { found, expected } => {
assert_eq!(expected, nak_pdu.len_written()); assert_eq!(expected, nak_pdu.len_written());
assert_eq!(found, 5); assert_eq!(found, 5);