diff --git a/src/cfdp/pdu/eof.rs b/src/cfdp/pdu/eof.rs index 627c5a7..61aa4d0 100644 --- a/src/cfdp/pdu/eof.rs +++ b/src/cfdp/pdu/eof.rs @@ -169,18 +169,13 @@ mod tests { use crate::cfdp::pdu::{FileDirectiveType, PduHeader}; use crate::cfdp::{ConditionCode, CrcFlag, LargeFileFlag, PduType, TransmissionMode}; - #[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); + fn verify_state(&eof_pdu: &EofPdu, file_flag: LargeFileFlag) { assert_eq!(eof_pdu.file_checksum(), 0x01020304); assert_eq!(eof_pdu.file_size(), 12); assert_eq!(eof_pdu.condition_code(), ConditionCode::NoError); 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.file_directive_type(), @@ -193,6 +188,15 @@ mod tests { 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] fn test_serialization() { let pdu_conf = common_pdu_conf(CrcFlag::NoCrc, LargeFileFlag::Normal); @@ -270,4 +274,13 @@ mod tests { 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); + } } diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index 3f7d344..5520536 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -32,7 +32,7 @@ pub enum FileDirectiveType { #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum PduError { - ByteConversionError(ByteConversionError), + ByteConversion(ByteConversionError), /// Found version ID invalid, not equal to [CFDP_VERSION_2]. CfdpVersionMissmatch(u8), /// 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}" ) } - PduError::ByteConversionError(e) => { + PduError::ByteConversion(e) => { write!(f, "{}", e) } PduError::FileSizeTooLarge(value) => { @@ -145,7 +145,7 @@ impl Display for PduError { impl Error for PduError { fn source(&self) -> Option<&(dyn Error + 'static)> { match self { - PduError::ByteConversionError(e) => Some(e), + PduError::ByteConversion(e) => Some(e), PduError::TlvLvError(e) => Some(e), _ => None, } @@ -154,7 +154,7 @@ impl Error for PduError { impl From for PduError { fn from(value: ByteConversionError) -> Self { - Self::ByteConversionError(value) + Self::ByteConversion(value) } } @@ -526,7 +526,7 @@ impl PduHeader { /// function. pub fn from_bytes(buf: &[u8]) -> Result<(Self, usize), PduError> { if buf.len() < FIXED_HEADER_LEN { - return Err(PduError::ByteConversionError( + return Err(PduError::ByteConversion( ByteConversionError::FromSliceTooSmall { found: buf.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)] mod tests { + use alloc::string::ToString; + use crate::cfdp::pdu::{CommonPduConfig, PduError, PduHeader, FIXED_HEADER_LEN}; use crate::cfdp::{ CrcFlag, Direction, LargeFileFlag, PduType, SegmentMetadataFlag, SegmentationControl, @@ -946,6 +948,7 @@ mod tests { let error = res.unwrap_err(); if let PduError::CfdpVersionMissmatch(raw_version) = error { assert_eq!(raw_version, CFDP_VERSION_2 + 1); + assert_eq!(error.to_string(), "cfdp version missmatch, found 2, expected 1"); } else { panic!("invalid exception: {}", error); } @@ -957,7 +960,7 @@ mod tests { let res = PduHeader::from_bytes(&buf); assert!(res.is_err()); let error = res.unwrap_err(); - if let PduError::ByteConversionError(ByteConversionError::FromSliceTooSmall { + if let PduError::ByteConversion(ByteConversionError::FromSliceTooSmall { found, expected, }) = error @@ -983,13 +986,17 @@ mod tests { let header = PduHeader::from_bytes(&buf[0..6]); assert!(header.is_err()); let error = header.unwrap_err(); - if let PduError::ByteConversionError(ByteConversionError::FromSliceTooSmall { + if let PduError::ByteConversion(ByteConversionError::FromSliceTooSmall { found, expected, }) = error { assert_eq!(found, 6); 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(); if let PduError::InvalidEntityLen(len) = error { assert_eq!(len, 3); + assert_eq!( + error.to_string(), + "invalid PDU entity ID length 3, only [1, 2, 4, 8] are allowed" + ); } else { panic!("Invalid exception: {}", error) } @@ -1087,4 +1098,20 @@ mod tests { 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); + } } diff --git a/src/cfdp/pdu/nak.rs b/src/cfdp/pdu/nak.rs index 22d18f2..3b34903 100644 --- a/src/cfdp/pdu/nak.rs +++ b/src/cfdp/pdu/nak.rs @@ -398,7 +398,7 @@ impl<'seg_reqs> NakPduReader<'seg_reqs> { let end_of_scope; if pdu_header.common_pdu_conf().file_flag == LargeFileFlag::Large { if current_idx + 16 > buf.len() { - return Err(PduError::ByteConversionError( + return Err(PduError::ByteConversion( ByteConversionError::FromSliceTooSmall { found: buf.len(), expected: current_idx + 16, @@ -734,12 +734,17 @@ mod tests { let pdu_conf = common_pdu_conf(CrcFlag::NoCrc, LargeFileFlag::Normal); let pdu_header = PduHeader::new_no_file_data(pdu_conf, 0); 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, u32::MAX as u64 + 1, u32::MAX as u64 + 2, Some(u32_list), - ) { + ); + assert!(error.is_err()); + + if let Err(PduError::InvalidStartOrEndOfScopeValue) = error { + assert_eq!(error.to_string(), ) } else { panic!("API call did not fail"); } @@ -758,7 +763,7 @@ mod tests { assert!(error.is_err()); let e = error.unwrap_err(); match e { - PduError::ByteConversionError(conv_error) => match conv_error { + PduError::ByteConversion(conv_error) => match conv_error { ByteConversionError::ToSliceTooSmall { found, expected } => { assert_eq!(expected, nak_pdu.len_written()); assert_eq!(found, 5);