Skip to content

Commit

Permalink
Mpls tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-noland committed May 13, 2024
1 parent 7cb0d28 commit e1170f2
Showing 1 changed file with 146 additions and 1 deletion.
147 changes: 146 additions & 1 deletion src/tc/filters/flower/mpls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for LseFilter {
NlasIterator::new(payload)
.map(|nla| LseOptions::parse(&nla?))
.collect::<Result<Vec<_>, _>>()
.map(|opts| LseFilter::try_from(opts))?
.map(LseFilter::try_from)?
}
}

Expand Down Expand Up @@ -287,3 +287,148 @@ impl From<Vec<LseFilter>> 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);
}
}

0 comments on commit e1170f2

Please sign in to comment.