use std::net::Ipv4Addr;
use netlink_packet_core::{Emitable, Parseable};
use crate::{
route::{
flags::RouteFlags, RouteAttribute, RouteHeader, RouteMessage,
RouteMessageBuffer, RouteMetric, RouteProtocol, RouteScope, RouteType,
},
AddressFamily,
};
#[test]
fn test_route_metrics_mtu_window_rtt_hoplimit() {
let raw = vec![
0x02, 0x10, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x0f, 0x00, 0xfe, 0x00, 0x00, 0x00,
0x08, 0x00, 0x01, 0x00, 0x0a, 0x67, 0x00, 0x00,
0x24, 0x00, 0x08, 0x00, 0x08, 0x00, 0x02, 0x00, 0xdc, 0x05, 0x00, 0x00,
0x08, 0x00, 0x03, 0x00, 0xff, 0xff, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00,
0x20, 0x03, 0x00, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x40, 0x00, 0x00, 0x00,
0x08, 0x00, 0x05, 0x00, 0x0a, 0x00, 0x00, 0xfe,
0x08, 0x00, 0x04, 0x00, 0x11, 0x00, 0x00, 0x00,
];
let expected = RouteMessage {
header: RouteHeader {
address_family: AddressFamily::Inet,
destination_prefix_length: 16,
source_prefix_length: 0,
tos: 0,
table: 254,
protocol: RouteProtocol::Boot,
scope: RouteScope::Universe,
kind: RouteType::Unicast,
flags: RouteFlags::empty(),
},
attributes: vec![
RouteAttribute::Table(254),
RouteAttribute::Destination(Ipv4Addr::new(10, 103, 0, 0).into()),
RouteAttribute::Metrics(vec![
RouteMetric::Mtu(1500),
RouteMetric::Window(65535),
RouteMetric::Rtt(800),
RouteMetric::Hoplimit(64),
]),
RouteAttribute::Gateway(Ipv4Addr::new(10, 0, 0, 254).into()),
RouteAttribute::Oif(17),
],
};
assert_eq!(
expected,
RouteMessage::parse(&RouteMessageBuffer::new(&raw)).unwrap()
);
let mut buf = vec![0; expected.buffer_len()];
expected.emit(&mut buf);
assert_eq!(buf, raw);
}
#[test]
fn test_route_metrics_advmss_ssthresh_cwnd_initcwnd_initrwnd() {
let raw = vec![
0x02, 0x10, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x0f, 0x00, 0xfe, 0x00, 0x00, 0x00,
0x08, 0x00, 0x01, 0x00, 0x0a, 0x6a, 0x00, 0x00,
0x2c, 0x00, 0x08, 0x00, 0x08, 0x00, 0x06, 0x00, 0x64, 0x00, 0x00, 0x00,
0x08, 0x00, 0x07, 0x00, 0x64, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00,
0xb4, 0x05, 0x00, 0x00, 0x08, 0x00, 0x0b, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x08, 0x00, 0x0e, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x08, 0x00, 0x05, 0x00, 0x0a, 0x00, 0x00, 0xfe,
0x08, 0x00, 0x04, 0x00, 0x11, 0x00, 0x00, 0x00,
];
let expected = RouteMessage {
header: RouteHeader {
address_family: AddressFamily::Inet,
destination_prefix_length: 16,
source_prefix_length: 0,
tos: 0,
table: 254,
protocol: RouteProtocol::Boot,
scope: RouteScope::Universe,
kind: RouteType::Unicast,
flags: RouteFlags::empty(),
},
attributes: vec![
RouteAttribute::Table(254),
RouteAttribute::Destination(Ipv4Addr::new(10, 106, 0, 0).into()),
RouteAttribute::Metrics(vec![
RouteMetric::SsThresh(100),
RouteMetric::Cwnd(100),
RouteMetric::Advmss(1460),
RouteMetric::InitCwnd(10),
RouteMetric::InitRwnd(10),
]),
RouteAttribute::Gateway(Ipv4Addr::new(10, 0, 0, 254).into()),
RouteAttribute::Oif(17),
],
};
assert_eq!(
expected,
RouteMessage::parse(&RouteMessageBuffer::new(&raw)).unwrap()
);
let mut buf = vec![0; expected.buffer_len()];
expected.emit(&mut buf);
assert_eq!(buf, raw);
}
#[test]
fn test_route_metrics_reordering_rto_min_quickack_fastopen() {
let raw = vec![
0x02, 0x10, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x0f, 0x00, 0xfe, 0x00, 0x00, 0x00,
0x08, 0x00, 0x01, 0x00, 0x0a, 0x6b, 0x00, 0x00,
0x2c, 0x00, 0x08, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00,
0x08, 0x00, 0x09, 0x00, 0x05, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00,
0xc8, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x00,
0x08, 0x00, 0x11, 0x00, 0x01, 0x00, 0x00, 0x00,
0x08, 0x00, 0x05, 0x00, 0x0a, 0x00, 0x00, 0xfe,
0x08, 0x00, 0x04, 0x00, 0x11, 0x00, 0x00, 0x00,
];
let expected = RouteMessage {
header: RouteHeader {
address_family: AddressFamily::Inet,
destination_prefix_length: 16,
source_prefix_length: 0,
tos: 0,
table: 254,
protocol: RouteProtocol::Boot,
scope: RouteScope::Universe,
kind: RouteType::Unicast,
flags: RouteFlags::empty(),
},
attributes: vec![
RouteAttribute::Table(254),
RouteAttribute::Destination(Ipv4Addr::new(10, 107, 0, 0).into()),
RouteAttribute::Metrics(vec![
RouteMetric::Lock(0x2000),
RouteMetric::Reordering(5),
RouteMetric::RtoMin(200),
RouteMetric::QuickAck(1),
RouteMetric::FastopenNoCookie(1),
]),
RouteAttribute::Gateway(Ipv4Addr::new(10, 0, 0, 254).into()),
RouteAttribute::Oif(17),
],
};
assert_eq!(
expected,
RouteMessage::parse(&RouteMessageBuffer::new(&raw)).unwrap()
);
let mut buf = vec![0; expected.buffer_len()];
expected.emit(&mut buf);
assert_eq!(buf, raw);
}
#[test]
fn test_route_metrics_features_ecn() {
let raw = vec![
0x02, 0x10, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x0f, 0x00, 0xfe, 0x00, 0x00, 0x00,
0x08, 0x00, 0x01, 0x00, 0x0a, 0x6c, 0x00, 0x00,
0x0c, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x00,
0x08, 0x00, 0x05, 0x00, 0x0a, 0x00, 0x00, 0xfe,
0x08, 0x00, 0x04, 0x00, 0x11, 0x00, 0x00, 0x00,
];
let expected = RouteMessage {
header: RouteHeader {
address_family: AddressFamily::Inet,
destination_prefix_length: 16,
source_prefix_length: 0,
tos: 0,
table: 254,
protocol: RouteProtocol::Boot,
scope: RouteScope::Universe,
kind: RouteType::Unicast,
flags: RouteFlags::empty(),
},
attributes: vec![
RouteAttribute::Table(254),
RouteAttribute::Destination(Ipv4Addr::new(10, 108, 0, 0).into()),
RouteAttribute::Metrics(vec![RouteMetric::Features(1)]),
RouteAttribute::Gateway(Ipv4Addr::new(10, 0, 0, 254).into()),
RouteAttribute::Oif(17),
],
};
assert_eq!(
expected,
RouteMessage::parse(&RouteMessageBuffer::new(&raw)).unwrap()
);
let mut buf = vec![0; expected.buffer_len()];
expected.emit(&mut buf);
assert_eq!(buf, raw);
}