2023-06-12 03:57:38 +02:00
|
|
|
use crate::cfdp::pdu::{
|
2023-06-12 13:00:58 +02:00
|
|
|
add_pdu_crc, generic_length_checks_pdu_deserialization, read_fss_field, write_fss_field,
|
|
|
|
PduError, PduHeader,
|
2023-06-12 03:57:38 +02:00
|
|
|
};
|
2023-05-30 00:16:16 +02:00
|
|
|
use crate::cfdp::{CrcFlag, LargeFileFlag, PduType, SegmentMetadataFlag};
|
2023-08-18 10:09:32 +02:00
|
|
|
use crate::ByteConversionError;
|
2023-05-30 00:16:16 +02:00
|
|
|
use num_enum::{IntoPrimitive, TryFromPrimitive};
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2023-11-24 17:09:23 +01:00
|
|
|
use super::{CfdpPdu, FileDirectiveType, WritablePduPacket};
|
2023-11-11 18:17:41 +01:00
|
|
|
|
2023-05-30 00:16:16 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
|
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum RecordContinuationState {
|
|
|
|
NoStartNoEnd = 0b00,
|
|
|
|
StartWithoutEnd = 0b01,
|
|
|
|
EndWithoutStart = 0b10,
|
|
|
|
StartAndEnd = 0b11,
|
|
|
|
}
|
|
|
|
|
2023-05-30 11:09:41 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
2023-05-30 00:16:16 +02:00
|
|
|
pub struct SegmentMetadata<'seg_meta> {
|
|
|
|
record_continuation_state: RecordContinuationState,
|
|
|
|
metadata: Option<&'seg_meta [u8]>,
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:36:02 +02:00
|
|
|
impl<'seg_meta> SegmentMetadata<'seg_meta> {
|
2023-05-30 17:39:33 +02:00
|
|
|
pub fn new(
|
|
|
|
record_continuation_state: RecordContinuationState,
|
|
|
|
metadata: Option<&'seg_meta [u8]>,
|
|
|
|
) -> Option<Self> {
|
|
|
|
if let Some(metadata) = metadata {
|
|
|
|
if metadata.len() > 2_usize.pow(6) - 1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(Self {
|
|
|
|
record_continuation_state,
|
|
|
|
metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-02 14:58:03 +01:00
|
|
|
pub fn record_continuation_state(&self) -> RecordContinuationState {
|
|
|
|
self.record_continuation_state
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metadata(&self) -> Option<&'seg_meta [u8]> {
|
|
|
|
self.metadata
|
|
|
|
}
|
|
|
|
|
2023-05-30 00:16:16 +02:00
|
|
|
pub fn written_len(&self) -> usize {
|
2023-05-30 17:39:33 +02:00
|
|
|
// Map empty metadata to 0 and slice to its length.
|
|
|
|
1 + self.metadata.map_or(0, |meta| meta.len())
|
2023-05-30 00:16:16 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:36:02 +02:00
|
|
|
pub(crate) fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
2023-05-30 00:16:16 +02:00
|
|
|
if buf.len() < self.written_len() {
|
2023-08-18 10:09:32 +02:00
|
|
|
return Err(ByteConversionError::ToSliceTooSmall {
|
2023-05-30 00:16:16 +02:00
|
|
|
found: buf.len(),
|
|
|
|
expected: self.written_len(),
|
2023-08-18 10:09:32 +02:00
|
|
|
});
|
2023-05-30 00:16:16 +02:00
|
|
|
}
|
2023-05-30 17:39:33 +02:00
|
|
|
buf[0] = ((self.record_continuation_state as u8) << 6)
|
|
|
|
| self.metadata.map_or(0, |meta| meta.len() as u8);
|
2023-05-30 19:35:38 +02:00
|
|
|
if let Some(metadata) = self.metadata {
|
|
|
|
buf[1..1 + metadata.len()].copy_from_slice(metadata)
|
2023-05-30 00:16:16 +02:00
|
|
|
}
|
|
|
|
Ok(self.written_len())
|
|
|
|
}
|
2023-05-30 15:36:02 +02:00
|
|
|
|
|
|
|
pub(crate) fn from_bytes(buf: &'seg_meta [u8]) -> Result<Self, ByteConversionError> {
|
|
|
|
if buf.is_empty() {
|
2023-08-18 10:09:32 +02:00
|
|
|
return Err(ByteConversionError::FromSliceTooSmall {
|
2023-05-30 15:36:02 +02:00
|
|
|
found: buf.len(),
|
|
|
|
expected: 2,
|
2023-08-18 10:09:32 +02:00
|
|
|
});
|
2023-05-30 15:36:02 +02:00
|
|
|
}
|
|
|
|
let mut metadata = None;
|
|
|
|
let seg_metadata_len = (buf[0] & 0b111111) as usize;
|
|
|
|
if seg_metadata_len > 0 {
|
|
|
|
metadata = Some(&buf[1..1 + seg_metadata_len]);
|
|
|
|
}
|
|
|
|
Ok(Self {
|
|
|
|
// Can't fail, only 2 bits
|
|
|
|
record_continuation_state: RecordContinuationState::try_from((buf[0] >> 6) & 0b11)
|
|
|
|
.unwrap(),
|
|
|
|
metadata,
|
|
|
|
})
|
|
|
|
}
|
2023-05-30 00:16:16 +02:00
|
|
|
}
|
|
|
|
|
2023-06-12 04:04:52 +02:00
|
|
|
/// File Data PDU abstraction.
|
|
|
|
///
|
|
|
|
/// For more information, refer to CFDP chapter 5.3.
|
2023-05-30 11:09:41 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
2023-05-30 00:16:16 +02:00
|
|
|
pub struct FileDataPdu<'seg_meta, 'file_data> {
|
|
|
|
pdu_header: PduHeader,
|
2023-05-30 11:09:41 +02:00
|
|
|
#[cfg_attr(feature = "serde", serde(borrow))]
|
2023-05-30 00:16:16 +02:00
|
|
|
segment_metadata: Option<SegmentMetadata<'seg_meta>>,
|
|
|
|
offset: u64,
|
|
|
|
file_data: &'file_data [u8],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'seg_meta, 'file_data> FileDataPdu<'seg_meta, 'file_data> {
|
|
|
|
pub fn new_with_seg_metadata(
|
|
|
|
pdu_header: PduHeader,
|
|
|
|
segment_metadata: SegmentMetadata<'seg_meta>,
|
|
|
|
offset: u64,
|
|
|
|
file_data: &'file_data [u8],
|
|
|
|
) -> Self {
|
|
|
|
Self::new_generic(pdu_header, Some(segment_metadata), offset, file_data)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_no_seg_metadata(
|
|
|
|
pdu_header: PduHeader,
|
|
|
|
offset: u64,
|
|
|
|
file_data: &'file_data [u8],
|
|
|
|
) -> Self {
|
|
|
|
Self::new_generic(pdu_header, None, offset, file_data)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_generic(
|
|
|
|
mut pdu_header: PduHeader,
|
|
|
|
segment_metadata: Option<SegmentMetadata<'seg_meta>>,
|
|
|
|
offset: u64,
|
|
|
|
file_data: &'file_data [u8],
|
|
|
|
) -> Self {
|
|
|
|
pdu_header.pdu_type = PduType::FileData;
|
|
|
|
if segment_metadata.is_some() {
|
|
|
|
pdu_header.seg_metadata_flag = SegmentMetadataFlag::Present;
|
|
|
|
}
|
2023-05-30 15:36:02 +02:00
|
|
|
let mut pdu = Self {
|
2023-05-30 00:16:16 +02:00
|
|
|
pdu_header,
|
|
|
|
segment_metadata,
|
|
|
|
offset,
|
|
|
|
file_data,
|
2023-05-30 15:36:02 +02:00
|
|
|
};
|
|
|
|
pdu.pdu_header.pdu_datafield_len = pdu.calc_pdu_datafield_len() as u16;
|
|
|
|
pdu
|
2023-05-30 00:16:16 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:36:02 +02:00
|
|
|
fn calc_pdu_datafield_len(&self) -> usize {
|
|
|
|
let mut len = core::mem::size_of::<u32>();
|
2023-05-30 00:16:16 +02:00
|
|
|
if self.pdu_header.pdu_conf.file_flag == LargeFileFlag::Large {
|
2023-06-06 08:59:18 +02:00
|
|
|
len += 4;
|
2023-05-30 00:16:16 +02:00
|
|
|
}
|
2023-05-30 15:36:02 +02:00
|
|
|
if self.segment_metadata.is_some() {
|
|
|
|
len += self.segment_metadata.as_ref().unwrap().written_len()
|
|
|
|
}
|
2023-05-30 00:16:16 +02:00
|
|
|
len += self.file_data.len();
|
2023-11-26 21:24:24 +01:00
|
|
|
if self.crc_flag() == CrcFlag::WithCrc {
|
2023-05-30 00:16:16 +02:00
|
|
|
len += 2;
|
|
|
|
}
|
|
|
|
len
|
|
|
|
}
|
|
|
|
|
2023-05-30 11:09:41 +02:00
|
|
|
pub fn offset(&self) -> u64 {
|
|
|
|
self.offset
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn file_data(&self) -> &'file_data [u8] {
|
|
|
|
self.file_data
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:36:02 +02:00
|
|
|
pub fn segment_metadata(&self) -> Option<&SegmentMetadata> {
|
2023-05-30 11:09:41 +02:00
|
|
|
self.segment_metadata.as_ref()
|
|
|
|
}
|
|
|
|
|
2023-12-02 14:58:03 +01:00
|
|
|
pub fn from_bytes<'buf: 'seg_meta + 'file_data>(buf: &'buf [u8]) -> Result<Self, PduError> {
|
2023-05-30 15:36:02 +02:00
|
|
|
let (pdu_header, mut current_idx) = PduHeader::from_bytes(buf)?;
|
|
|
|
let full_len_without_crc = pdu_header.verify_length_and_checksum(buf)?;
|
2023-06-12 03:57:38 +02:00
|
|
|
let min_expected_len = current_idx + core::mem::size_of::<u32>();
|
|
|
|
generic_length_checks_pdu_deserialization(buf, min_expected_len, full_len_without_crc)?;
|
2023-05-30 15:36:02 +02:00
|
|
|
let mut segment_metadata = None;
|
|
|
|
if pdu_header.seg_metadata_flag == SegmentMetadataFlag::Present {
|
|
|
|
segment_metadata = Some(SegmentMetadata::from_bytes(&buf[current_idx..])?);
|
|
|
|
current_idx += segment_metadata.as_ref().unwrap().written_len();
|
|
|
|
}
|
|
|
|
let (fss, offset) = read_fss_field(pdu_header.pdu_conf.file_flag, &buf[current_idx..]);
|
|
|
|
current_idx += fss;
|
|
|
|
if current_idx > full_len_without_crc {
|
2023-08-18 10:09:32 +02:00
|
|
|
return Err(ByteConversionError::FromSliceTooSmall {
|
2023-05-30 15:36:02 +02:00
|
|
|
found: current_idx,
|
|
|
|
expected: full_len_without_crc,
|
2023-08-18 10:09:32 +02:00
|
|
|
}
|
2023-05-30 15:36:02 +02:00
|
|
|
.into());
|
|
|
|
}
|
|
|
|
Ok(Self {
|
|
|
|
pdu_header,
|
|
|
|
segment_metadata,
|
|
|
|
offset,
|
|
|
|
file_data: &buf[current_idx..full_len_without_crc],
|
|
|
|
})
|
|
|
|
}
|
2023-05-30 00:16:16 +02:00
|
|
|
}
|
2023-11-24 17:09:23 +01:00
|
|
|
impl CfdpPdu for FileDataPdu<'_, '_> {
|
|
|
|
fn pdu_header(&self) -> &PduHeader {
|
|
|
|
&self.pdu_header
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_directive_type(&self) -> Option<FileDirectiveType> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 08:33:03 +02:00
|
|
|
|
2023-11-11 18:17:41 +01:00
|
|
|
impl WritablePduPacket for FileDataPdu<'_, '_> {
|
|
|
|
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, PduError> {
|
2023-11-24 16:15:46 +01:00
|
|
|
if buf.len() < self.len_written() {
|
2023-11-11 18:17:41 +01:00
|
|
|
return Err(ByteConversionError::ToSliceTooSmall {
|
|
|
|
found: buf.len(),
|
2023-11-24 16:15:46 +01:00
|
|
|
expected: self.len_written(),
|
2023-11-11 18:17:41 +01:00
|
|
|
}
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
let mut current_idx = self.pdu_header.write_to_bytes(buf)?;
|
|
|
|
if self.segment_metadata.is_some() {
|
|
|
|
current_idx += self
|
|
|
|
.segment_metadata
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.write_to_bytes(&mut buf[current_idx..])?;
|
|
|
|
}
|
|
|
|
current_idx += write_fss_field(
|
|
|
|
self.pdu_header.common_pdu_conf().file_flag,
|
|
|
|
self.offset,
|
|
|
|
&mut buf[current_idx..],
|
|
|
|
)?;
|
|
|
|
buf[current_idx..current_idx + self.file_data.len()].copy_from_slice(self.file_data);
|
|
|
|
current_idx += self.file_data.len();
|
2023-11-26 21:24:24 +01:00
|
|
|
if self.crc_flag() == CrcFlag::WithCrc {
|
2023-11-11 18:17:41 +01:00
|
|
|
current_idx = add_pdu_crc(buf, current_idx);
|
|
|
|
}
|
|
|
|
Ok(current_idx)
|
|
|
|
}
|
2023-11-24 16:15:46 +01:00
|
|
|
|
|
|
|
fn len_written(&self) -> usize {
|
|
|
|
self.pdu_header.header_len() + self.calc_pdu_datafield_len()
|
|
|
|
}
|
2023-11-11 18:17:41 +01:00
|
|
|
}
|
|
|
|
|
2023-05-30 08:33:03 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-11-11 18:17:41 +01:00
|
|
|
use super::*;
|
2023-11-26 15:04:44 +01:00
|
|
|
use crate::cfdp::pdu::tests::{TEST_DEST_ID, TEST_SEQ_NUM, TEST_SRC_ID};
|
2023-05-30 11:09:41 +02:00
|
|
|
use crate::cfdp::pdu::{CommonPduConfig, PduHeader};
|
2023-11-26 15:04:44 +01:00
|
|
|
use crate::cfdp::{Direction, SegmentMetadataFlag, SegmentationControl, TransmissionMode};
|
2023-05-30 11:09:41 +02:00
|
|
|
|
2023-05-30 08:33:03 +02:00
|
|
|
#[test]
|
|
|
|
fn test_basic() {
|
2023-11-26 15:04:44 +01:00
|
|
|
let common_conf =
|
|
|
|
CommonPduConfig::new_with_byte_fields(TEST_SRC_ID, TEST_DEST_ID, TEST_SEQ_NUM).unwrap();
|
2023-05-30 11:09:41 +02:00
|
|
|
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);
|
|
|
|
assert_eq!(fd_pdu.file_data(), file_data);
|
|
|
|
assert_eq!(fd_pdu.offset(), 10);
|
2023-05-30 15:36:02 +02:00
|
|
|
assert!(fd_pdu.segment_metadata().is_none());
|
|
|
|
assert_eq!(
|
2023-11-24 16:15:46 +01:00
|
|
|
fd_pdu.len_written(),
|
2023-05-30 15:36:02 +02:00
|
|
|
fd_pdu.pdu_header.header_len() + core::mem::size_of::<u32>() + 4
|
|
|
|
);
|
2023-11-26 15:04:44 +01:00
|
|
|
|
|
|
|
assert_eq!(fd_pdu.crc_flag(), CrcFlag::NoCrc);
|
|
|
|
assert_eq!(fd_pdu.file_flag(), LargeFileFlag::Normal);
|
|
|
|
assert_eq!(fd_pdu.pdu_type(), PduType::FileData);
|
|
|
|
assert_eq!(fd_pdu.file_directive_type(), None);
|
|
|
|
assert_eq!(fd_pdu.transmission_mode(), TransmissionMode::Acknowledged);
|
|
|
|
assert_eq!(fd_pdu.direction(), Direction::TowardsReceiver);
|
|
|
|
assert_eq!(fd_pdu.source_id(), TEST_SRC_ID.into());
|
|
|
|
assert_eq!(fd_pdu.dest_id(), TEST_DEST_ID.into());
|
|
|
|
assert_eq!(fd_pdu.transaction_seq_num(), TEST_SEQ_NUM.into());
|
2023-05-30 15:36:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialization() {
|
2023-11-26 15:04:44 +01:00
|
|
|
let common_conf =
|
|
|
|
CommonPduConfig::new_with_byte_fields(TEST_SRC_ID, TEST_DEST_ID, TEST_SEQ_NUM).unwrap();
|
2023-05-30 15:36:02 +02:00
|
|
|
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; 32] = [0; 32];
|
|
|
|
let res = fd_pdu.write_to_bytes(&mut buf);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let written = res.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
written,
|
|
|
|
fd_pdu.pdu_header.header_len() + core::mem::size_of::<u32>() + 4
|
|
|
|
);
|
|
|
|
let mut current_idx = fd_pdu.pdu_header.header_len();
|
|
|
|
let file_size = u32::from_be_bytes(
|
|
|
|
buf[fd_pdu.pdu_header.header_len()..fd_pdu.pdu_header.header_len() + 4]
|
|
|
|
.try_into()
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
current_idx += 4;
|
|
|
|
assert_eq!(file_size, 10);
|
|
|
|
assert_eq!(buf[current_idx], 1);
|
|
|
|
current_idx += 1;
|
|
|
|
assert_eq!(buf[current_idx], 2);
|
|
|
|
current_idx += 1;
|
|
|
|
assert_eq!(buf[current_idx], 3);
|
|
|
|
current_idx += 1;
|
|
|
|
assert_eq!(buf[current_idx], 4);
|
|
|
|
}
|
|
|
|
|
2023-11-24 16:34:06 +01:00
|
|
|
#[test]
|
|
|
|
fn test_write_to_vec() {
|
2023-11-26 15:04:44 +01:00
|
|
|
let common_conf =
|
|
|
|
CommonPduConfig::new_with_byte_fields(TEST_SRC_ID, TEST_DEST_ID, TEST_SEQ_NUM).unwrap();
|
2023-11-24 16:34:06 +01:00
|
|
|
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();
|
|
|
|
let pdu_vec = fd_pdu.to_vec().unwrap();
|
|
|
|
assert_eq!(buf[0..written], pdu_vec);
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:36:02 +02:00
|
|
|
#[test]
|
|
|
|
fn test_deserialization() {
|
2023-11-26 15:04:44 +01:00
|
|
|
let common_conf =
|
|
|
|
CommonPduConfig::new_with_byte_fields(TEST_SRC_ID, TEST_DEST_ID, TEST_SEQ_NUM).unwrap();
|
2023-05-30 15:36:02 +02:00
|
|
|
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; 32] = [0; 32];
|
|
|
|
fd_pdu.write_to_bytes(&mut buf).unwrap();
|
|
|
|
let fd_pdu_read_back = FileDataPdu::from_bytes(&buf);
|
|
|
|
assert!(fd_pdu_read_back.is_ok());
|
|
|
|
let fd_pdu_read_back = fd_pdu_read_back.unwrap();
|
|
|
|
assert_eq!(fd_pdu_read_back, fd_pdu);
|
2023-05-30 08:33:03 +02:00
|
|
|
}
|
2023-05-30 17:39:33 +02:00
|
|
|
|
2023-11-26 21:24:24 +01:00
|
|
|
#[test]
|
|
|
|
fn test_with_crc() {
|
2023-11-26 23:52:34 +01:00
|
|
|
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");
|
|
|
|
}
|
2023-11-26 21:24:24 +01:00
|
|
|
}
|
|
|
|
|
2023-05-30 17:39:33 +02:00
|
|
|
#[test]
|
2023-06-06 08:59:18 +02:00
|
|
|
fn test_with_seg_metadata_serialization() {
|
2023-05-30 17:39:33 +02:00
|
|
|
let common_conf =
|
2023-11-26 15:04:44 +01:00
|
|
|
CommonPduConfig::new_with_byte_fields(TEST_SRC_ID, TEST_DEST_ID, TEST_SEQ_NUM).unwrap();
|
2023-05-30 17:39:33 +02:00
|
|
|
let pdu_header = PduHeader::new_for_file_data(
|
|
|
|
common_conf,
|
|
|
|
0,
|
|
|
|
SegmentMetadataFlag::Present,
|
|
|
|
SegmentationControl::WithRecordBoundaryPreservation,
|
|
|
|
);
|
|
|
|
let file_data: [u8; 4] = [1, 2, 3, 4];
|
|
|
|
let seg_metadata: [u8; 4] = [4, 3, 2, 1];
|
|
|
|
let segment_meta =
|
|
|
|
SegmentMetadata::new(RecordContinuationState::StartAndEnd, Some(&seg_metadata))
|
|
|
|
.unwrap();
|
|
|
|
let fd_pdu = FileDataPdu::new_with_seg_metadata(pdu_header, segment_meta, 10, &file_data);
|
|
|
|
assert!(fd_pdu.segment_metadata().is_some());
|
|
|
|
assert_eq!(*fd_pdu.segment_metadata().unwrap(), segment_meta);
|
2023-05-30 19:35:38 +02:00
|
|
|
assert_eq!(
|
2023-11-24 16:15:46 +01:00
|
|
|
fd_pdu.len_written(),
|
2023-05-30 19:35:38 +02:00
|
|
|
fd_pdu.pdu_header.header_len()
|
|
|
|
+ 1
|
|
|
|
+ seg_metadata.len()
|
|
|
|
+ core::mem::size_of::<u32>()
|
|
|
|
+ 4
|
|
|
|
);
|
|
|
|
let mut buf: [u8; 32] = [0; 32];
|
|
|
|
fd_pdu
|
|
|
|
.write_to_bytes(&mut buf)
|
|
|
|
.expect("writing FD PDU failed");
|
|
|
|
let mut current_idx = fd_pdu.pdu_header.header_len();
|
|
|
|
assert_eq!(
|
|
|
|
RecordContinuationState::try_from((buf[current_idx] >> 6) & 0b11).unwrap(),
|
|
|
|
RecordContinuationState::StartAndEnd
|
|
|
|
);
|
|
|
|
assert_eq!((buf[current_idx] & 0b111111) as usize, seg_metadata.len());
|
|
|
|
current_idx += 1;
|
|
|
|
assert_eq!(buf[current_idx], 4);
|
|
|
|
current_idx += 1;
|
|
|
|
assert_eq!(buf[current_idx], 3);
|
|
|
|
current_idx += 1;
|
|
|
|
assert_eq!(buf[current_idx], 2);
|
|
|
|
current_idx += 1;
|
|
|
|
assert_eq!(buf[current_idx], 1);
|
|
|
|
current_idx += 1;
|
|
|
|
// Still verify that the rest is written correctly.
|
2023-05-30 19:35:59 +02:00
|
|
|
assert_eq!(
|
|
|
|
u32::from_be_bytes(buf[current_idx..current_idx + 4].try_into().unwrap()),
|
|
|
|
10
|
|
|
|
);
|
|
|
|
current_idx += 4;
|
2023-05-30 19:35:38 +02:00
|
|
|
assert_eq!(buf[current_idx], 1);
|
|
|
|
current_idx += 1;
|
|
|
|
assert_eq!(buf[current_idx], 2);
|
|
|
|
current_idx += 1;
|
|
|
|
assert_eq!(buf[current_idx], 3);
|
|
|
|
current_idx += 1;
|
|
|
|
assert_eq!(buf[current_idx], 4);
|
2023-05-30 19:35:59 +02:00
|
|
|
current_idx += 1;
|
2023-11-24 16:15:46 +01:00
|
|
|
assert_eq!(current_idx, fd_pdu.len_written());
|
2023-05-30 17:39:33 +02:00
|
|
|
}
|
2023-06-06 08:59:18 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_seg_metadata_deserialization() {
|
|
|
|
let common_conf =
|
2023-11-26 15:04:44 +01:00
|
|
|
CommonPduConfig::new_with_byte_fields(TEST_SRC_ID, TEST_DEST_ID, TEST_SEQ_NUM).unwrap();
|
2023-06-06 08:59:18 +02:00
|
|
|
let pdu_header = PduHeader::new_for_file_data(
|
|
|
|
common_conf,
|
|
|
|
0,
|
|
|
|
SegmentMetadataFlag::Present,
|
|
|
|
SegmentationControl::WithRecordBoundaryPreservation,
|
|
|
|
);
|
|
|
|
let file_data: [u8; 4] = [1, 2, 3, 4];
|
|
|
|
let seg_metadata: [u8; 4] = [4, 3, 2, 1];
|
|
|
|
let segment_meta =
|
|
|
|
SegmentMetadata::new(RecordContinuationState::StartAndEnd, Some(&seg_metadata))
|
|
|
|
.unwrap();
|
|
|
|
let fd_pdu = FileDataPdu::new_with_seg_metadata(pdu_header, segment_meta, 10, &file_data);
|
|
|
|
let mut buf: [u8; 32] = [0; 32];
|
|
|
|
fd_pdu
|
|
|
|
.write_to_bytes(&mut buf)
|
|
|
|
.expect("writing FD PDU failed");
|
|
|
|
let fd_pdu_read_back = FileDataPdu::from_bytes(&buf);
|
|
|
|
assert!(fd_pdu_read_back.is_ok());
|
|
|
|
let fd_pdu_read_back = fd_pdu_read_back.unwrap();
|
|
|
|
assert_eq!(fd_pdu_read_back, fd_pdu);
|
|
|
|
}
|
2023-05-30 11:09:41 +02:00
|
|
|
}
|