Coverage Update #47
@ -165,7 +165,9 @@ impl<'data> Lv<'data> {
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::cfdp::lv::Lv;
|
||||
use super::*;
|
||||
use alloc::string::ToString;
|
||||
|
||||
use crate::cfdp::TlvLvError;
|
||||
use crate::ByteConversionError;
|
||||
use std::string::String;
|
||||
@ -176,7 +178,7 @@ pub mod tests {
|
||||
let lv_res = Lv::new(&lv_data);
|
||||
assert!(lv_res.is_ok());
|
||||
let lv = lv_res.unwrap();
|
||||
assert!(lv.value().len() > 0);
|
||||
assert!(!lv.value().is_empty());
|
||||
let val = lv.value();
|
||||
assert_eq!(val[0], 1);
|
||||
assert_eq!(val[1], 2);
|
||||
@ -259,6 +261,10 @@ pub mod tests {
|
||||
let error = lv.unwrap_err();
|
||||
if let TlvLvError::DataTooLarge(size) = error {
|
||||
assert_eq!(size, u8::MAX as usize + 1);
|
||||
assert_eq!(
|
||||
error.to_string(),
|
||||
"data with size 256 larger than allowed 255 bytes"
|
||||
);
|
||||
} else {
|
||||
panic!("invalid exception {:?}", error)
|
||||
}
|
||||
|
@ -171,7 +171,10 @@ pub enum TlvLvError {
|
||||
DataTooLarge(usize),
|
||||
ByteConversion(ByteConversionError),
|
||||
/// 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.
|
||||
/// Depending on the concrete TLV type, the value length may also be logically invalid.
|
||||
InvalidValueLength(usize),
|
||||
@ -200,7 +203,7 @@ impl Display for TlvLvError {
|
||||
)
|
||||
}
|
||||
TlvLvError::ByteConversion(e) => {
|
||||
write!(f, "{}", e)
|
||||
write!(f, "tlv or lv byte conversion: {}", e)
|
||||
}
|
||||
TlvLvError::InvalidTlvTypeField((found, expected)) => {
|
||||
write!(
|
||||
|
@ -202,11 +202,19 @@ impl<'fs_responses> FinishedPdu<'fs_responses> {
|
||||
return Err(PduError::FormatError);
|
||||
}
|
||||
} 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) => {
|
||||
return Err(TlvLvError::InvalidTlvTypeField((raw, None)).into());
|
||||
return Err(TlvLvError::InvalidTlvTypeField {
|
||||
found: raw,
|
||||
expected: None,
|
||||
}
|
||||
.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -176,13 +176,15 @@ impl<'data> Tlv<'data> {
|
||||
}
|
||||
|
||||
pub(crate) fn verify_tlv_type(raw_type: u8, expected_tlv_type: TlvType) -> Result<(), TlvLvError> {
|
||||
let tlv_type = TlvType::try_from(raw_type)
|
||||
.map_err(|_| TlvLvError::InvalidTlvTypeField((raw_type, Some(expected_tlv_type as u8))))?;
|
||||
let tlv_type = TlvType::try_from(raw_type).map_err(|_| TlvLvError::InvalidTlvTypeField {
|
||||
found: raw_type,
|
||||
expected: Some(expected_tlv_type.into()),
|
||||
})?;
|
||||
if tlv_type != expected_tlv_type {
|
||||
return Err(TlvLvError::InvalidTlvTypeField((
|
||||
tlv_type as u8,
|
||||
Some(expected_tlv_type as u8),
|
||||
)));
|
||||
return Err(TlvLvError::InvalidTlvTypeField {
|
||||
found: tlv_type as u8,
|
||||
expected: Some(expected_tlv_type as u8),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -259,17 +261,17 @@ impl<'data> TryFrom<Tlv<'data>> for EntityIdTlv {
|
||||
match value.tlv_type_field {
|
||||
TlvTypeField::Standard(tlv_type) => {
|
||||
if tlv_type != TlvType::EntityId {
|
||||
return Err(TlvLvError::InvalidTlvTypeField((
|
||||
tlv_type as u8,
|
||||
Some(TlvType::EntityId as u8),
|
||||
)));
|
||||
return Err(TlvLvError::InvalidTlvTypeField {
|
||||
found: tlv_type as u8,
|
||||
expected: Some(TlvType::EntityId as u8),
|
||||
});
|
||||
}
|
||||
}
|
||||
TlvTypeField::Custom(val) => {
|
||||
return Err(TlvLvError::InvalidTlvTypeField((
|
||||
val,
|
||||
Some(TlvType::EntityId as u8),
|
||||
)));
|
||||
return Err(TlvLvError::InvalidTlvTypeField {
|
||||
found: val,
|
||||
expected: Some(TlvType::EntityId as u8),
|
||||
});
|
||||
}
|
||||
}
|
||||
let len_value = value.value().len();
|
||||
@ -485,7 +487,8 @@ impl<'first_name, 'second_name> FilestoreRequestTlv<'first_name, 'second_name> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::println;
|
||||
|
||||
use alloc::string::ToString;
|
||||
|
||||
use super::*;
|
||||
use crate::cfdp::lv::Lv;
|
||||
@ -623,6 +626,10 @@ mod tests {
|
||||
let error = tlv_res.unwrap_err();
|
||||
if let TlvLvError::DataTooLarge(size) = error {
|
||||
assert_eq!(size, u8::MAX as usize + 1);
|
||||
assert_eq!(
|
||||
error.to_string(),
|
||||
"data with size 256 larger than allowed 255 bytes"
|
||||
);
|
||||
} else {
|
||||
panic!("unexpected error {:?}", error);
|
||||
}
|
||||
@ -826,4 +833,19 @@ mod tests {
|
||||
let req_conv_back = req_conv_back.unwrap();
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
@ -62,17 +62,17 @@ impl<'data> MsgToUserTlv<'data> {
|
||||
match msg_to_user.tlv_type_field() {
|
||||
TlvTypeField::Standard(tlv_type) => {
|
||||
if tlv_type != TlvType::MsgToUser {
|
||||
return Err(TlvLvError::InvalidTlvTypeField((
|
||||
tlv_type as u8,
|
||||
Some(TlvType::MsgToUser as u8),
|
||||
)));
|
||||
return Err(TlvLvError::InvalidTlvTypeField {
|
||||
found: tlv_type as u8,
|
||||
expected: Some(TlvType::MsgToUser as u8),
|
||||
});
|
||||
}
|
||||
}
|
||||
TlvTypeField::Custom(raw) => {
|
||||
return Err(TlvLvError::InvalidTlvTypeField((
|
||||
raw,
|
||||
Some(TlvType::MsgToUser as u8),
|
||||
)));
|
||||
return Err(TlvLvError::InvalidTlvTypeField{
|
||||
found: raw,
|
||||
expected: Some(TlvType::MsgToUser as u8),
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(msg_to_user)
|
||||
|
Loading…
Reference in New Issue
Block a user