Skip to content

Commit

Permalink
first test
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed Nov 26, 2023
1 parent fdf6e1d commit c0805db
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
38 changes: 36 additions & 2 deletions src/cfdp/pdu/ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());
}
}
17 changes: 16 additions & 1 deletion src/cfdp/pdu/eof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
20 changes: 19 additions & 1 deletion src/cfdp/pdu/file_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
20 changes: 19 additions & 1 deletion src/cfdp/pdu/finished.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}

0 comments on commit c0805db

Please sign in to comment.