diff --git a/src/cfdp/pdu/metadata.rs b/src/cfdp/pdu/metadata.rs index 90303d3..646d03b 100644 --- a/src/cfdp/pdu/metadata.rs +++ b/src/cfdp/pdu/metadata.rs @@ -340,7 +340,10 @@ impl CfdpPdu for MetadataPduReader<'_> { #[cfg(test)] pub mod tests { + use alloc::string::ToString; + use crate::cfdp::lv::Lv; + use crate::cfdp::pdu::eof::EofPdu; use crate::cfdp::pdu::metadata::{ build_metadata_opts_from_slice, build_metadata_opts_from_vec, MetadataGenericParams, MetadataPduCreator, MetadataPduReader, @@ -348,7 +351,7 @@ pub mod tests { use crate::cfdp::pdu::tests::{ common_pdu_conf, verify_raw_header, TEST_DEST_ID, TEST_SEQ_NUM, TEST_SRC_ID, }; - use crate::cfdp::pdu::{CfdpPdu, WritablePduPacket}; + use crate::cfdp::pdu::{CfdpPdu, PduError, WritablePduPacket}; use crate::cfdp::pdu::{FileDirectiveType, PduHeader}; use crate::cfdp::tlv::{Tlv, TlvType}; use crate::cfdp::{ @@ -600,6 +603,45 @@ pub mod tests { assert_eq!(accumulated_len, pdu_read_back.options().len()); } + #[test] + fn test_invalid_directive_code() { + let (_, _, metadata_pdu) = generic_metadata_pdu(CrcFlag::NoCrc, LargeFileFlag::Large, &[]); + let mut metadata_vec = metadata_pdu.to_vec().unwrap(); + metadata_vec[7] = 0xff; + let metadata_error = MetadataPduReader::from_bytes(&metadata_vec); + assert!(metadata_error.is_err()); + let error = metadata_error.unwrap_err(); + if let PduError::InvalidDirectiveType { found, expected } = error { + assert_eq!(found, 0xff); + assert_eq!(expected, Some(FileDirectiveType::MetadataPdu)); + assert_eq!( + error.to_string(), + "invalid directive type value 255, expected Some(MetadataPdu)" + ); + } else { + panic!("Expected InvalidDirectiveType error, got {:?}", error); + } + } + + #[test] + fn test_wrong_directive_code() { + let (_, _, metadata_pdu) = generic_metadata_pdu(CrcFlag::NoCrc, LargeFileFlag::Large, &[]); + let mut metadata_vec = metadata_pdu.to_vec().unwrap(); + metadata_vec[7] = FileDirectiveType::EofPdu as u8; + let metadata_error = MetadataPduReader::from_bytes(&metadata_vec); + assert!(metadata_error.is_err()); + let error = metadata_error.unwrap_err(); + if let PduError::WrongDirectiveType { found, expected } = error { + assert_eq!(found, FileDirectiveType::EofPdu); + assert_eq!(expected, FileDirectiveType::MetadataPdu); + assert_eq!( + error.to_string(), + "found directive type EofPdu, expected MetadataPdu" + ); + } else { + panic!("Expected InvalidDirectiveType error, got {:?}", error); + } + } #[test] fn test_corrects_pdu_header() { let pdu_header = PduHeader::new_for_file_data( diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index 5520536..2fb8516 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -55,7 +55,6 @@ pub enum PduError { found: u8, expected: Option, }, - InvalidSegmentRequestFormat, InvalidStartOrEndOfScopeValue, /// Invalid condition code. Contains the raw detected value. InvalidConditionCode(u8), @@ -80,9 +79,6 @@ impl Display for PduError { "invalid PDU entity ID length {raw_id}, only [1, 2, 4, 8] are allowed" ) } - PduError::InvalidSegmentRequestFormat => { - write!(f, "invalid segment request format for NAK PDU") - } PduError::InvalidStartOrEndOfScopeValue => { write!(f, "invalid start or end of scope for NAK PDU") } @@ -855,6 +851,7 @@ mod tests { // 4 byte fixed header plus three bytes src, dest ID and transaction ID assert_eq!(res.unwrap(), 7); verify_raw_header(&pdu_header, &buf); + assert_eq!(pdu_header.pdu_datafield_len(), 5); } #[test] @@ -948,7 +945,10 @@ 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"); + assert_eq!( + error.to_string(), + "cfdp version missmatch, found 2, expected 1" + ); } else { panic!("invalid exception: {}", error); } @@ -1048,6 +1048,10 @@ mod tests { { assert_eq!(src_id_len, 1); assert_eq!(dest_id_len, 2); + assert_eq!( + error.to_string(), + "missmatch of PDU source length 1 and destination length 2" + ); } } @@ -1110,7 +1114,7 @@ mod tests { 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"); + .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 3b34903..3a7a38a 100644 --- a/src/cfdp/pdu/nak.rs +++ b/src/cfdp/pdu/nak.rs @@ -501,6 +501,8 @@ impl<'a, 'b> PartialEq> for NakPduReader<'b> { #[cfg(test)] mod tests { + use alloc::string::ToString; + use crate::cfdp::{ pdu::tests::{common_pdu_conf, verify_raw_header, TEST_DEST_ID, TEST_SEQ_NUM, TEST_SRC_ID}, PduType, TransmissionMode, @@ -742,11 +744,14 @@ mod tests { Some(u32_list), ); assert!(error.is_err()); - - if let Err(PduError::InvalidStartOrEndOfScopeValue) = error { - assert_eq!(error.to_string(), ) + let error = error.unwrap_err(); + if let PduError::InvalidStartOrEndOfScopeValue = error { + assert_eq!( + error.to_string(), + "invalid start or end of scope for NAK PDU" + ); } else { - panic!("API call did not fail"); + panic!("unexpected error {error}"); } }