diff --git a/src/tc/filters/flower/mpls.rs b/src/tc/filters/flower/mpls.rs index c14d0bba..e1bfa233 100644 --- a/src/tc/filters/flower/mpls.rs +++ b/src/tc/filters/flower/mpls.rs @@ -149,7 +149,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for LseFilter { NlasIterator::new(payload) .map(|nla| LseOptions::parse(&nla?)) .collect::, _>>() - .map(|opts| LseFilter::try_from(opts))? + .map(LseFilter::try_from)? } } @@ -287,3 +287,148 @@ impl From> for Options { Self::Lses(value) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_back_lse_options_depth_zero() { + let example = LseOptions::Depth(0); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseOptions::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_options_depth_example() { + let example = LseOptions::Depth(0xab); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseOptions::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_options_label_zero() { + let example = LseOptions::Label(mpls::Label::new(0).unwrap()); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseOptions::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_options_label_example() { + let example = LseOptions::Label(mpls::Label::new(0x12345).unwrap()); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseOptions::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_options_tc_zero() { + let example = + LseOptions::TrafficClass(mpls::TrafficClass::new(0).unwrap()); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseOptions::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_options_tc_example() { + let example = + LseOptions::TrafficClass(mpls::TrafficClass::new(0x3).unwrap()); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseOptions::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_options_bos_unset() { + let example = LseOptions::BottomOfStack(mpls::BottomOfStack::Unset); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseOptions::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_options_bos_set() { + let example = LseOptions::BottomOfStack(mpls::BottomOfStack::Set); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseOptions::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_options_ttl_zero() { + let example = LseOptions::Ttl(0); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseOptions::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_options_ttl_example() { + let example = LseOptions::Ttl(0x34); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseOptions::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_filter_default() { + let example = LseFilter::default(); + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseFilter::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } + + #[test] + fn parse_back_lse_filter_example() { + let example = LseFilter { + depth: 1, + label: Some(mpls::Label::new(0x12345).unwrap()), + traffic_class: Some(mpls::TrafficClass::new(0x3).unwrap()), + bottom_of_stack: Some(mpls::BottomOfStack::Set), + ttl: Some(0x34), + }; + let mut buffer = vec![0; example.buffer_len()]; + example.emit(&mut buffer); + let parsed = + LseFilter::parse(&NlaBuffer::new_checked(&buffer).unwrap()) + .unwrap(); + assert_eq!(example, parsed); + } +}