Coverage Update #47

Merged
muellerr merged 35 commits from coverage-update into main 2023-12-06 18:05:57 +01:00
5 changed files with 68 additions and 29 deletions
Showing only changes of commit 7965e71c49 - Show all commits

View File

@ -165,7 +165,9 @@ impl<'data> Lv<'data> {
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use crate::cfdp::lv::Lv; use super::*;
use alloc::string::ToString;
use crate::cfdp::TlvLvError; use crate::cfdp::TlvLvError;
use crate::ByteConversionError; use crate::ByteConversionError;
use std::string::String; use std::string::String;
@ -176,7 +178,7 @@ pub mod tests {
let lv_res = Lv::new(&lv_data); let lv_res = Lv::new(&lv_data);
assert!(lv_res.is_ok()); assert!(lv_res.is_ok());
let lv = lv_res.unwrap(); let lv = lv_res.unwrap();
assert!(lv.value().len() > 0); assert!(!lv.value().is_empty());
let val = lv.value(); let val = lv.value();
assert_eq!(val[0], 1); assert_eq!(val[0], 1);
assert_eq!(val[1], 2); assert_eq!(val[1], 2);
@ -259,6 +261,10 @@ pub mod tests {
let error = lv.unwrap_err(); let error = lv.unwrap_err();
if let TlvLvError::DataTooLarge(size) = error { if let TlvLvError::DataTooLarge(size) = error {
assert_eq!(size, u8::MAX as usize + 1); assert_eq!(size, u8::MAX as usize + 1);
assert_eq!(
error.to_string(),
"data with size 256 larger than allowed 255 bytes"
);
} else { } else {
panic!("invalid exception {:?}", error) panic!("invalid exception {:?}", error)
} }

View File

@ -171,7 +171,10 @@ pub enum TlvLvError {
DataTooLarge(usize), DataTooLarge(usize),
ByteConversion(ByteConversionError), ByteConversion(ByteConversionError),
/// First value: Found value. Second value: Expected value if there is one. /// First value: Found value. Second value: Expected value if there is one.
InvalidTlvTypeField((u8, Option<u8>)), InvalidTlvTypeField {
found: u8,
expected: Option<u8>,
},
/// Logically invalid value length detected. The value length may not exceed 255 bytes. /// Logically invalid value length detected. The value length may not exceed 255 bytes.
/// Depending on the concrete TLV type, the value length may also be logically invalid. /// Depending on the concrete TLV type, the value length may also be logically invalid.
InvalidValueLength(usize), InvalidValueLength(usize),
@ -200,7 +203,7 @@ impl Display for TlvLvError {
) )
} }
TlvLvError::ByteConversion(e) => { TlvLvError::ByteConversion(e) => {
write!(f, "{}", e) write!(f, "tlv or lv byte conversion: {}", e)
} }
TlvLvError::InvalidTlvTypeField((found, expected)) => { TlvLvError::InvalidTlvTypeField((found, expected)) => {
write!( write!(

View File

@ -202,11 +202,19 @@ impl<'fs_responses> FinishedPdu<'fs_responses> {
return Err(PduError::FormatError); return Err(PduError::FormatError);
} }
} else { } else {
return Err(TlvLvError::InvalidTlvTypeField((tlv_type as u8, None)).into()); return Err(TlvLvError::InvalidTlvTypeField {
found: tlv_type.into(),
expected: Some(TlvType::FilestoreResponse.into()),
}
.into());
} }
} }
TlvTypeField::Custom(raw) => { TlvTypeField::Custom(raw) => {
return Err(TlvLvError::InvalidTlvTypeField((raw, None)).into()); return Err(TlvLvError::InvalidTlvTypeField {
found: raw,
expected: None,
}
.into());
} }
} }
} }

View File

@ -176,13 +176,15 @@ impl<'data> Tlv<'data> {
} }
pub(crate) fn verify_tlv_type(raw_type: u8, expected_tlv_type: TlvType) -> Result<(), TlvLvError> { pub(crate) fn verify_tlv_type(raw_type: u8, expected_tlv_type: TlvType) -> Result<(), TlvLvError> {
let tlv_type = TlvType::try_from(raw_type) let tlv_type = TlvType::try_from(raw_type).map_err(|_| TlvLvError::InvalidTlvTypeField {
.map_err(|_| TlvLvError::InvalidTlvTypeField((raw_type, Some(expected_tlv_type as u8))))?; found: raw_type,
expected: Some(expected_tlv_type.into()),
})?;
if tlv_type != expected_tlv_type { if tlv_type != expected_tlv_type {
return Err(TlvLvError::InvalidTlvTypeField(( return Err(TlvLvError::InvalidTlvTypeField {
tlv_type as u8, found: tlv_type as u8,
Some(expected_tlv_type as u8), expected: Some(expected_tlv_type as u8),
))); });
} }
Ok(()) Ok(())
} }
@ -259,17 +261,17 @@ impl<'data> TryFrom<Tlv<'data>> for EntityIdTlv {
match value.tlv_type_field { match value.tlv_type_field {
TlvTypeField::Standard(tlv_type) => { TlvTypeField::Standard(tlv_type) => {
if tlv_type != TlvType::EntityId { if tlv_type != TlvType::EntityId {
return Err(TlvLvError::InvalidTlvTypeField(( return Err(TlvLvError::InvalidTlvTypeField {
tlv_type as u8, found: tlv_type as u8,
Some(TlvType::EntityId as u8), expected: Some(TlvType::EntityId as u8),
))); });
} }
} }
TlvTypeField::Custom(val) => { TlvTypeField::Custom(val) => {
return Err(TlvLvError::InvalidTlvTypeField(( return Err(TlvLvError::InvalidTlvTypeField {
val, found: val,
Some(TlvType::EntityId as u8), expected: Some(TlvType::EntityId as u8),
))); });
} }
} }
let len_value = value.value().len(); let len_value = value.value().len();
@ -485,7 +487,8 @@ impl<'first_name, 'second_name> FilestoreRequestTlv<'first_name, 'second_name> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::println;
use alloc::string::ToString;
use super::*; use super::*;
use crate::cfdp::lv::Lv; use crate::cfdp::lv::Lv;
@ -623,6 +626,10 @@ mod tests {
let error = tlv_res.unwrap_err(); let error = tlv_res.unwrap_err();
if let TlvLvError::DataTooLarge(size) = error { if let TlvLvError::DataTooLarge(size) = error {
assert_eq!(size, u8::MAX as usize + 1); assert_eq!(size, u8::MAX as usize + 1);
assert_eq!(
error.to_string(),
"data with size 256 larger than allowed 255 bytes"
);
} else { } else {
panic!("unexpected error {:?}", error); panic!("unexpected error {:?}", error);
} }
@ -826,4 +833,19 @@ mod tests {
let req_conv_back = req_conv_back.unwrap(); let req_conv_back = req_conv_back.unwrap();
assert_eq!(req_conv_back, req); assert_eq!(req_conv_back, req);
} }
#[test]
fn test_entity_it_tlv_to_tlv() {
let entity_id = UbfU16::new(0x0102);
let entity_id_tlv = EntityIdTlv::new(entity_id.into());
let mut binding = [0; 16];
let tlv = entity_id_tlv.to_tlv(&mut binding).unwrap();
assert_eq!(
tlv.tlv_type_field(),
TlvTypeField::Standard(TlvType::EntityId)
);
assert_eq!(tlv.len_full(), 4);
assert_eq!(tlv.len_value(), 2);
assert_eq!(tlv.value(), &[0x01, 0x02]);
}
} }

View File

@ -62,17 +62,17 @@ impl<'data> MsgToUserTlv<'data> {
match msg_to_user.tlv_type_field() { match msg_to_user.tlv_type_field() {
TlvTypeField::Standard(tlv_type) => { TlvTypeField::Standard(tlv_type) => {
if tlv_type != TlvType::MsgToUser { if tlv_type != TlvType::MsgToUser {
return Err(TlvLvError::InvalidTlvTypeField(( return Err(TlvLvError::InvalidTlvTypeField {
tlv_type as u8, found: tlv_type as u8,
Some(TlvType::MsgToUser as u8), expected: Some(TlvType::MsgToUser as u8),
))); });
} }
} }
TlvTypeField::Custom(raw) => { TlvTypeField::Custom(raw) => {
return Err(TlvLvError::InvalidTlvTypeField(( return Err(TlvLvError::InvalidTlvTypeField{
raw, found: raw,
Some(TlvType::MsgToUser as u8), expected: Some(TlvType::MsgToUser as u8),
))); });
} }
} }
Ok(msg_to_user) Ok(msg_to_user)