diff --git a/src/cfdp/pdu/ack.rs b/src/cfdp/pdu/ack.rs index 979015b..38f27d2 100644 --- a/src/cfdp/pdu/ack.rs +++ b/src/cfdp/pdu/ack.rs @@ -63,7 +63,7 @@ impl AckPdu { .unwrap() } - pub fn new_for_finished_pdu( + pub fn new_for_ack_pdu( pdu_header: PduHeader, condition_code: ConditionCode, transaction_status: TransactionStatus, @@ -192,8 +192,42 @@ impl WritablePduPacket for AckPdu { #[cfg(test)] mod tests { + use crate::cfdp::{ + pdu::tests::{common_pdu_conf, TEST_DEST_ID, TEST_SEQ_NUM, TEST_SRC_ID}, + LargeFileFlag, PduType, TransmissionMode, + }; + + use super::*; + #[test] fn test_basic() { - todo!(); + let pdu_conf = common_pdu_conf(CrcFlag::NoCrc, LargeFileFlag::Normal); + let pdu_header = PduHeader::new_no_file_data(pdu_conf, 0); + let ack_pdu = AckPdu::new( + pdu_header, + FileDirectiveType::FinishedPdu, + ConditionCode::NoError, + TransactionStatus::Active, + ) + .expect("creating ACK PDU failed"); + assert_eq!( + ack_pdu.directive_code_of_acked_pdu(), + FileDirectiveType::FinishedPdu + ); + assert_eq!(ack_pdu.condition_code(), ConditionCode::NoError); + assert_eq!(ack_pdu.transaction_status(), TransactionStatus::Active); + + assert_eq!(ack_pdu.crc_flag(), CrcFlag::NoCrc); + assert_eq!(ack_pdu.file_flag(), LargeFileFlag::Normal); + assert_eq!(ack_pdu.pdu_type(), PduType::FileDirective); + assert_eq!( + ack_pdu.file_directive_type(), + Some(FileDirectiveType::AckPdu) + ); + assert_eq!(ack_pdu.transmission_mode(), TransmissionMode::Acknowledged); + assert_eq!(ack_pdu.direction(), Direction::TowardsReceiver); + assert_eq!(ack_pdu.source_id(), TEST_SRC_ID.into()); + assert_eq!(ack_pdu.dest_id(), TEST_DEST_ID.into()); + assert_eq!(ack_pdu.transaction_seq_num(), TEST_SEQ_NUM.into()); } } diff --git a/src/cfdp/pdu/eof.rs b/src/cfdp/pdu/eof.rs index cfb4e69..2ee5fa4 100644 --- a/src/cfdp/pdu/eof.rs +++ b/src/cfdp/pdu/eof.rs @@ -253,6 +253,21 @@ mod tests { #[test] fn test_with_crc() { - todo!(); + let pdu_conf = common_pdu_conf(CrcFlag::WithCrc, LargeFileFlag::Normal); + let pdu_header = PduHeader::new_no_file_data(pdu_conf, 0); + let eof_pdu = EofPdu::new_no_error(pdu_header, 0x01020304, 12); + let mut buf: [u8; 64] = [0; 64]; + let written = eof_pdu.write_to_bytes(&mut buf).unwrap(); + assert_eq!(written, eof_pdu.len_written()); + let eof_from_raw = EofPdu::from_bytes(&buf).expect("creating EOF PDU failed"); + assert_eq!(eof_from_raw, eof_pdu); + buf[written - 1] -= 1; + let crc: u16 = ((buf[written - 2] as u16) << 8) as u16 | buf[written - 1] as u16; + let error = EofPdu::from_bytes(&buf).unwrap_err(); + if let PduError::ChecksumError(e) = error { + assert_eq!(e, crc); + } else { + panic!("expected crc error"); + } } } diff --git a/src/cfdp/pdu/file_data.rs b/src/cfdp/pdu/file_data.rs index 852470a..db60eef 100644 --- a/src/cfdp/pdu/file_data.rs +++ b/src/cfdp/pdu/file_data.rs @@ -331,7 +331,25 @@ mod tests { #[test] fn test_with_crc() { - todo!(); + let mut common_conf = + CommonPduConfig::new_with_byte_fields(TEST_SRC_ID, TEST_DEST_ID, TEST_SEQ_NUM).unwrap(); + common_conf.crc_flag = true.into(); + let pdu_header = PduHeader::new_for_file_data_default(common_conf, 0); + let file_data: [u8; 4] = [1, 2, 3, 4]; + let fd_pdu = FileDataPdu::new_no_seg_metadata(pdu_header, 10, &file_data); + let mut buf: [u8; 64] = [0; 64]; + let written = fd_pdu.write_to_bytes(&mut buf).unwrap(); + assert_eq!(written, fd_pdu.len_written()); + let finished_pdu_from_raw = FileDataPdu::from_bytes(&buf).unwrap(); + assert_eq!(finished_pdu_from_raw, fd_pdu); + buf[written - 1] -= 1; + let crc: u16 = ((buf[written - 2] as u16) << 8) | buf[written - 1] as u16; + let error = FileDataPdu::from_bytes(&buf).unwrap_err(); + if let PduError::ChecksumError(e) = error { + assert_eq!(e, crc); + } else { + panic!("expected crc error"); + } } #[test] diff --git a/src/cfdp/pdu/finished.rs b/src/cfdp/pdu/finished.rs index 93cd008..dc410fb 100644 --- a/src/cfdp/pdu/finished.rs +++ b/src/cfdp/pdu/finished.rs @@ -395,6 +395,24 @@ mod tests { #[test] fn test_with_crc() { - todo!(); + let finished_pdu = generic_finished_pdu( + CrcFlag::WithCrc, + LargeFileFlag::Normal, + DeliveryCode::Complete, + FileStatus::Retained, + ); + let mut buf: [u8; 64] = [0; 64]; + let written = finished_pdu.write_to_bytes(&mut buf).unwrap(); + assert_eq!(written, finished_pdu.len_written()); + let finished_pdu_from_raw = FinishedPdu::from_bytes(&buf).unwrap(); + assert_eq!(finished_pdu_from_raw, finished_pdu); + buf[written - 1] -= 1; + let crc: u16 = ((buf[written - 2] as u16) << 8) as u16 | buf[written - 1] as u16; + let error = FinishedPdu::from_bytes(&buf).unwrap_err(); + if let PduError::ChecksumError(e) = error { + assert_eq!(e, crc); + } else { + panic!("expected crc error"); + } } }