PDU header base impl done
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good

This commit is contained in:
Robin Müller 2023-05-18 13:32:45 +02:00
parent 02bae5de6c
commit d5a3e7c0d4
Signed by: muellerr
GPG Key ID: A649FB78196E3849
2 changed files with 162 additions and 65 deletions

View File

@ -26,13 +26,13 @@ impl Display for PduError {
PduError::InvalidEntityLen(raw_id) => {
write!(
f,
"Invalid entity ID length {raw_id}, only [1, 2, 4, 8] are allowed"
"Invalid PDU entity ID length {raw_id}, only [1, 2, 4, 8] are allowed"
)
}
PduError::InvalidTransactionSeqNumLen(raw_id) => {
write!(
f,
"Invalid transaction seq num length {raw_id}, only [1, 2, 4, 8] are allowed"
"invalid PDUtransaction seq num length {raw_id}, only [1, 2, 4, 8] are allowed"
)
}
PduError::CfdpVersionMissmatch(raw) => {
@ -44,7 +44,7 @@ impl Display for PduError {
PduError::SourceDestIdLenMissmatch((src_len, dest_len)) => {
write!(
f,
"missmatch of source length {src_len} and destination length {dest_len}"
"missmatch of PDU source length {src_len} and destination length {dest_len}"
)
}
PduError::ByteConversionError(e) => {
@ -188,6 +188,8 @@ impl PduHeader {
}
pub fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, PduError> {
// Internal note: There is currently no way to pass a PDU configuration like this, but
// this check is still kept for defensive programming.
if self.pdu_conf.source_entity_id.len() != self.pdu_conf.dest_entity_id.len() {
return Err(PduError::SourceDestIdLenMissmatch((
self.pdu_conf.source_entity_id.len(),
@ -341,15 +343,15 @@ mod tests {
CrcFlag, Direction, LargeFileFlag, PduType, SegmentMetadataFlag, SegmentationControl,
TransmissionMode, CFDP_VERSION_2,
};
use crate::util::{UnsignedU16, UnsignedU8};
use crate::util::{UbfU8, UnsignedByteField, UnsignedByteFieldU16, UnsignedByteFieldU8};
use crate::ByteConversionError;
use std::format;
#[test]
fn test_basic_state() {
let src_id = UnsignedU8::new(1);
let dest_id = UnsignedU8::new(2);
let transaction_id = UnsignedU8::new(3);
let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id)
.expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
@ -370,9 +372,9 @@ mod tests {
#[test]
fn test_serialization_1() {
let src_id = UnsignedU8::new(1);
let dest_id = UnsignedU8::new(2);
let transaction_id = UnsignedU8::new(3);
let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id)
.expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
@ -409,9 +411,9 @@ mod tests {
#[test]
fn test_deserialization_1() {
let src_id = UnsignedU8::new(1);
let dest_id = UnsignedU8::new(2);
let transaction_id = UnsignedU8::new(3);
let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id)
.expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
@ -427,9 +429,9 @@ mod tests {
#[test]
fn test_serialization_2() {
let src_id = UnsignedU16::new(0x0001);
let dest_id = UnsignedU16::new(0x0203);
let transaction_id = UnsignedU16::new(0x0405);
let src_id = UnsignedByteFieldU16::new(0x0001);
let dest_id = UnsignedByteFieldU16::new(0x0203);
let transaction_id = UnsignedByteFieldU16::new(0x0405);
let mut common_pdu_cfg =
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id)
.expect("common config creation failed");
@ -476,9 +478,9 @@ mod tests {
#[test]
fn test_deserialization_2() {
let src_id = UnsignedU16::new(0x0001);
let dest_id = UnsignedU16::new(0x0203);
let transaction_id = UnsignedU16::new(0x0405);
let src_id = UnsignedByteFieldU16::new(0x0001);
let dest_id = UnsignedByteFieldU16::new(0x0203);
let transaction_id = UnsignedByteFieldU16::new(0x0405);
let mut common_pdu_cfg =
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id)
.expect("common config creation failed");
@ -504,9 +506,9 @@ mod tests {
#[test]
fn test_invalid_raw_version() {
let src_id = UnsignedU8::new(1);
let dest_id = UnsignedU8::new(2);
let transaction_id = UnsignedU8::new(3);
let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id)
.expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
@ -543,9 +545,9 @@ mod tests {
#[test]
fn test_buf_too_small_2() {
let src_id = UnsignedU8::new(1);
let dest_id = UnsignedU8::new(2);
let transaction_id = UnsignedU8::new(3);
let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id)
.expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
@ -562,4 +564,93 @@ mod tests {
assert_eq!(missmatch.expected, 7);
}
}
#[test]
fn test_invalid_seq_len() {
let src_id = UbfU8::new(1);
let dest_id = UbfU8::new(2);
let transaction_seq_id = UbfU8::new(3);
let invalid_byte_field = UnsignedByteField::new(3, 5);
let pdu_conf_res = CommonPduConfig::new_with_defaults(src_id, dest_id, invalid_byte_field);
assert!(pdu_conf_res.is_err());
let error = pdu_conf_res.unwrap_err();
if let PduError::InvalidTransactionSeqNumLen(len) = error {
assert_eq!(len, 3);
} else {
panic!("Invalid exception: {}", error)
}
let pdu_conf_res = CommonPduConfig::new_with_defaults(
invalid_byte_field,
invalid_byte_field,
transaction_seq_id,
);
assert!(pdu_conf_res.is_err());
let error = pdu_conf_res.unwrap_err();
if let PduError::InvalidEntityLen(len) = error {
assert_eq!(len, 3);
} else {
panic!("Invalid exception: {}", error)
}
}
#[test]
fn test_missmatch_src_dest_id() {
let src_id = UnsignedByteField::new(1, 5);
let dest_id = UnsignedByteField::new(2, 5);
let transaction_seq_id = UbfU8::new(3);
let pdu_conf_res = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_id);
assert!(pdu_conf_res.is_err());
let error = pdu_conf_res.unwrap_err();
if let PduError::SourceDestIdLenMissmatch((src_len, dest_len)) = error {
assert_eq!(src_len, 1);
assert_eq!(dest_len, 2);
}
}
#[test]
fn test_invalid_raw_src_id_len() {
let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id)
.expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
let mut buf: [u8; 7] = [0; 7];
let res = pdu_header.write_to_be_bytes(&mut buf);
assert!(res.is_ok());
buf[3] &= !0b0111_0000;
// Equivalent to the length of three
buf[3] |= 0b10 << 4;
let header_res = PduHeader::from_be_bytes(&buf);
assert!(header_res.is_err());
let error = header_res.unwrap_err();
if let PduError::InvalidEntityLen(len) = error {
assert_eq!(len, 3);
} else {
panic!("invalid exception {:?}", error)
}
}
#[test]
fn test_invalid_transaction_seq_id_len() {
let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id)
.expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
let mut buf: [u8; 7] = [0; 7];
let res = pdu_header.write_to_be_bytes(&mut buf);
assert!(res.is_ok());
buf[3] &= !0b0000_0111;
// Equivalent to the length of three
buf[3] |= 0b10;
let header_res = PduHeader::from_be_bytes(&buf);
assert!(header_res.is_err());
let error = header_res.unwrap_err();
if let PduError::InvalidTransactionSeqNumLen(len) = error {
assert_eq!(len, 3);
} else {
panic!("invalid exception {:?}", error)
}
}
}

View File

@ -169,19 +169,19 @@ impl UnsignedEnum for UnsignedByteField {
match self.len() {
0 => Ok(()),
1 => {
let u8 = UnsignedU8::try_from(*self).unwrap();
let u8 = UnsignedByteFieldU8::try_from(*self).unwrap();
u8.write_to_be_bytes(buf)
}
2 => {
let u16 = UnsignedU16::try_from(*self).unwrap();
let u16 = UnsignedByteFieldU16::try_from(*self).unwrap();
u16.write_to_be_bytes(buf)
}
4 => {
let u32 = UnsignedU32::try_from(*self).unwrap();
let u32 = UnsignedByteFieldU32::try_from(*self).unwrap();
u32.write_to_be_bytes(buf)
}
8 => {
let u64 = UnsignedU64::try_from(*self).unwrap();
let u64 = UnsignedByteFieldU64::try_from(*self).unwrap();
u64.write_to_be_bytes(buf)
}
_ => {
@ -222,18 +222,23 @@ impl<TYPE: ToBeBytes> UnsignedEnum for GenericUnsignedByteField<TYPE> {
}
pub type UnsignedByteFieldEmpty = GenericUnsignedByteField<()>;
pub type UnsignedU8 = GenericUnsignedByteField<u8>;
pub type UnsignedU16 = GenericUnsignedByteField<u16>;
pub type UnsignedU32 = GenericUnsignedByteField<u32>;
pub type UnsignedU64 = GenericUnsignedByteField<u64>;
pub type UnsignedByteFieldU8 = GenericUnsignedByteField<u8>;
pub type UnsignedByteFieldU16 = GenericUnsignedByteField<u16>;
pub type UnsignedByteFieldU32 = GenericUnsignedByteField<u32>;
pub type UnsignedByteFieldU64 = GenericUnsignedByteField<u64>;
impl From<UnsignedU8> for UnsignedByteField {
fn from(value: UnsignedU8) -> Self {
pub type UbfU8 = UnsignedByteFieldU8;
pub type UbfU16 = UnsignedByteFieldU16;
pub type UbfU32 = UnsignedByteFieldU32;
pub type UbfU64 = UnsignedByteFieldU64;
impl From<UnsignedByteFieldU8> for UnsignedByteField {
fn from(value: UnsignedByteFieldU8) -> Self {
Self::new(1, value.value as u64)
}
}
impl TryFrom<UnsignedByteField> for UnsignedU8 {
impl TryFrom<UnsignedByteField> for UnsignedByteFieldU8 {
type Error = UnsignedByteFieldError;
fn try_from(value: UnsignedByteField) -> Result<Self, Self::Error> {
@ -244,13 +249,13 @@ impl TryFrom<UnsignedByteField> for UnsignedU8 {
}
}
impl From<UnsignedU16> for UnsignedByteField {
fn from(value: UnsignedU16) -> Self {
impl From<UnsignedByteFieldU16> for UnsignedByteField {
fn from(value: UnsignedByteFieldU16) -> Self {
Self::new(2, value.value as u64)
}
}
impl TryFrom<UnsignedByteField> for UnsignedU16 {
impl TryFrom<UnsignedByteField> for UnsignedByteFieldU16 {
type Error = UnsignedByteFieldError;
fn try_from(value: UnsignedByteField) -> Result<Self, Self::Error> {
@ -261,13 +266,13 @@ impl TryFrom<UnsignedByteField> for UnsignedU16 {
}
}
impl From<UnsignedU32> for UnsignedByteField {
fn from(value: UnsignedU32) -> Self {
impl From<UnsignedByteFieldU32> for UnsignedByteField {
fn from(value: UnsignedByteFieldU32) -> Self {
Self::new(4, value.value as u64)
}
}
impl TryFrom<UnsignedByteField> for UnsignedU32 {
impl TryFrom<UnsignedByteField> for UnsignedByteFieldU32 {
type Error = UnsignedByteFieldError;
fn try_from(value: UnsignedByteField) -> Result<Self, Self::Error> {
@ -278,13 +283,13 @@ impl TryFrom<UnsignedByteField> for UnsignedU32 {
}
}
impl From<UnsignedU64> for UnsignedByteField {
fn from(value: UnsignedU64) -> Self {
impl From<UnsignedByteFieldU64> for UnsignedByteField {
fn from(value: UnsignedByteFieldU64) -> Self {
Self::new(8, value.value)
}
}
impl TryFrom<UnsignedByteField> for UnsignedU64 {
impl TryFrom<UnsignedByteField> for UnsignedByteFieldU64 {
type Error = UnsignedByteFieldError;
fn try_from(value: UnsignedByteField) -> Result<Self, Self::Error> {
@ -298,15 +303,15 @@ impl TryFrom<UnsignedByteField> for UnsignedU64 {
#[cfg(test)]
pub mod tests {
use crate::util::{
UnsignedByteField, UnsignedByteFieldError, UnsignedEnum, UnsignedU16, UnsignedU32,
UnsignedU64, UnsignedU8,
UnsignedByteField, UnsignedByteFieldError, UnsignedByteFieldU16, UnsignedByteFieldU32,
UnsignedByteFieldU64, UnsignedByteFieldU8, UnsignedEnum,
};
use crate::ByteConversionError;
use std::format;
#[test]
fn test_simple_u8() {
let u8 = UnsignedU8::new(5);
let u8 = UnsignedByteFieldU8::new(5);
assert_eq!(u8.len(), 1);
let mut buf: [u8; 8] = [0; 8];
u8.write_to_be_bytes(&mut buf)
@ -319,7 +324,7 @@ pub mod tests {
#[test]
fn test_simple_u16() {
let u16 = UnsignedU16::new(3823);
let u16 = UnsignedByteFieldU16::new(3823);
assert_eq!(u16.len(), 2);
let mut buf: [u8; 8] = [0; 8];
u16.write_to_be_bytes(&mut buf)
@ -333,7 +338,7 @@ pub mod tests {
#[test]
fn test_simple_u32() {
let u32 = UnsignedU32::new(80932);
let u32 = UnsignedByteFieldU32::new(80932);
assert_eq!(u32.len(), 4);
let mut buf: [u8; 8] = [0; 8];
u32.write_to_be_bytes(&mut buf)
@ -347,7 +352,7 @@ pub mod tests {
#[test]
fn test_simple_u64() {
let u64 = UnsignedU64::new(5999999);
let u64 = UnsignedByteFieldU64::new(5999999);
assert_eq!(u64.len(), 8);
let mut buf: [u8; 8] = [0; 8];
u64.write_to_be_bytes(&mut buf)
@ -358,11 +363,12 @@ pub mod tests {
#[test]
fn conversions_u8() {
let u8 = UnsignedU8::new(5);
let u8 = UnsignedByteFieldU8::new(5);
let u8_type_erased = UnsignedByteField::from(u8);
assert_eq!(u8_type_erased.width, 1);
assert_eq!(u8_type_erased.value, 5);
let u8_conv_back = UnsignedU8::try_from(u8_type_erased).expect("conversion failed for u8");
let u8_conv_back =
UnsignedByteFieldU8::try_from(u8_type_erased).expect("conversion failed for u8");
assert_eq!(u8, u8_conv_back);
assert_eq!(u8_conv_back.value, 5);
}
@ -370,7 +376,7 @@ pub mod tests {
#[test]
fn conversion_u8_fails() {
let field = UnsignedByteField::new(2, 60000);
let conv_fails = UnsignedU8::try_from(field);
let conv_fails = UnsignedByteFieldU8::try_from(field);
assert!(conv_fails.is_err());
let err = conv_fails.unwrap_err();
match err {
@ -386,12 +392,12 @@ pub mod tests {
#[test]
fn conversions_u16() {
let u16 = UnsignedU16::new(64444);
let u16 = UnsignedByteFieldU16::new(64444);
let u16_type_erased = UnsignedByteField::from(u16);
assert_eq!(u16_type_erased.width, 2);
assert_eq!(u16_type_erased.value, 64444);
let u16_conv_back =
UnsignedU16::try_from(u16_type_erased).expect("conversion failed for u16");
UnsignedByteFieldU16::try_from(u16_type_erased).expect("conversion failed for u16");
assert_eq!(u16, u16_conv_back);
assert_eq!(u16_conv_back.value, 64444);
}
@ -399,7 +405,7 @@ pub mod tests {
#[test]
fn conversion_u16_fails() {
let field = UnsignedByteField::new(4, 75000);
let conv_fails = UnsignedU16::try_from(field);
let conv_fails = UnsignedByteFieldU16::try_from(field);
assert!(conv_fails.is_err());
let err = conv_fails.unwrap_err();
match err {
@ -415,12 +421,12 @@ pub mod tests {
#[test]
fn conversions_u32() {
let u32 = UnsignedU32::new(75000);
let u32 = UnsignedByteFieldU32::new(75000);
let u32_type_erased = UnsignedByteField::from(u32);
assert_eq!(u32_type_erased.width, 4);
assert_eq!(u32_type_erased.value, 75000);
let u32_conv_back =
UnsignedU32::try_from(u32_type_erased).expect("conversion failed for u32");
UnsignedByteFieldU32::try_from(u32_type_erased).expect("conversion failed for u32");
assert_eq!(u32, u32_conv_back);
assert_eq!(u32_conv_back.value, 75000);
}
@ -428,7 +434,7 @@ pub mod tests {
#[test]
fn conversion_u32_fails() {
let field = UnsignedByteField::new(8, 75000);
let conv_fails = UnsignedU32::try_from(field);
let conv_fails = UnsignedByteFieldU32::try_from(field);
assert!(conv_fails.is_err());
let err = conv_fails.unwrap_err();
match err {
@ -444,12 +450,12 @@ pub mod tests {
#[test]
fn conversions_u64() {
let u64 = UnsignedU64::new(5999999);
let u64 = UnsignedByteFieldU64::new(5999999);
let u64_type_erased = UnsignedByteField::from(u64);
assert_eq!(u64_type_erased.width, 8);
assert_eq!(u64_type_erased.value, 5999999);
let u64_conv_back =
UnsignedU64::try_from(u64_type_erased).expect("conversion failed for u64");
UnsignedByteFieldU64::try_from(u64_type_erased).expect("conversion failed for u64");
assert_eq!(u64, u64_conv_back);
assert_eq!(u64_conv_back.value, 5999999);
}
@ -457,7 +463,7 @@ pub mod tests {
#[test]
fn conversion_u64_fails() {
let field = UnsignedByteField::new(4, 60000);
let conv_fails = UnsignedU64::try_from(field);
let conv_fails = UnsignedByteFieldU64::try_from(field);
assert!(conv_fails.is_err());
let err = conv_fails.unwrap_err();
match err {
@ -557,7 +563,7 @@ pub mod tests {
#[test]
fn type_u16_target_buf_too_small() {
let u16 = UnsignedU16::new(500);
let u16 = UnsignedByteFieldU16::new(500);
let mut buf: [u8; 1] = [0; 1];
let res = u16.write_to_be_bytes(&mut buf);
assert!(res.is_err());
@ -605,7 +611,7 @@ pub mod tests {
#[test]
fn type_u32_target_buf_too_small() {
let u16 = UnsignedU32::new(500);
let u16 = UnsignedByteFieldU32::new(500);
let mut buf: [u8; 3] = [0; 3];
let res = u16.write_to_be_bytes(&mut buf);
assert!(res.is_err());