use std::{
net::{Ipv4Addr, Ipv6Addr},
str::FromStr,
};
use netlink_packet_core::{Emitable, Parseable};
use crate::{
route::{
flags::RouteFlags, MplsLabel, RouteAddress, RouteAttribute,
RouteCacheInfo, RouteHeader, RouteLwEnCapType, RouteLwTunnelEncap,
RouteMessage, RouteMessageBuffer, RouteMplsIpTunnel,
RouteMplsTtlPropagation, RoutePreference, RouteProtocol, RouteScope,
RouteType,
},
AddressFamily,
};
#[test]
fn test_mpls_route_to_ipv4() {
let raw = vec![
0x02, 0x20, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x0f, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00,
0xc6, 0x33, 0x64, 0x01, 0x08, 0x00, 0x05, 0x00, 0xc0, 0x00, 0x02, 0x01,
0x08, 0x00, 0x04, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x14, 0x00, 0x16, 0x00,
0x08, 0x00, 0x01, 0x00, 0x00, 0x06, 0x41, 0x00, 0x05, 0x00, 0x02, 0x00,
0x19, 0x00, 0x00, 0x00, 0x06, 0x00, 0x15, 0x00, 0x01, 0x00, 0x00, 0x00,
];
let expected = RouteMessage {
header: RouteHeader {
address_family: AddressFamily::Inet,
destination_prefix_length: 32,
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::from_str("198.51.100.1").unwrap().into(),
),
RouteAttribute::Gateway(
Ipv4Addr::from_str("192.0.2.1").unwrap().into(),
),
RouteAttribute::Oif(10),
RouteAttribute::Encap(vec![
RouteLwTunnelEncap::Mpls(RouteMplsIpTunnel::Destination(vec![
MplsLabel {
label: 100,
traffic_class: 0,
bottom_of_stack: true,
ttl: 0,
},
])),
RouteLwTunnelEncap::Mpls(RouteMplsIpTunnel::Ttl(25)),
]),
RouteAttribute::EncapType(RouteLwEnCapType::Mpls),
],
};
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_ipv6_to_mpls_route() {
let raw = vec![
0x0a, 0x40, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x0f, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00,
0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x06, 0x00, 0x00, 0x04, 0x00, 0x00,
0x14, 0x00, 0x05, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x08, 0x00, 0x04, 0x00,
0x07, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x16, 0x00, 0x08, 0x00, 0x01, 0x00,
0x00, 0x0c, 0x81, 0x00, 0x06, 0x00, 0x15, 0x00, 0x01, 0x00, 0x00, 0x00,
0x24, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x05, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let expected = RouteMessage {
header: RouteHeader {
address_family: AddressFamily::Inet6,
destination_prefix_length: 64,
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(
Ipv6Addr::from_str("2001:db8:2::").unwrap().into(),
),
RouteAttribute::Priority(1024),
RouteAttribute::Gateway(
Ipv6Addr::from_str("2001:db8:1::2").unwrap().into(),
),
RouteAttribute::Oif(7),
RouteAttribute::Encap(vec![RouteLwTunnelEncap::Mpls(
RouteMplsIpTunnel::Destination(vec![MplsLabel {
label: 200,
traffic_class: 0,
bottom_of_stack: true,
ttl: 0,
}]),
)]),
RouteAttribute::EncapType(RouteLwEnCapType::Mpls),
RouteAttribute::CacheInfo(RouteCacheInfo {
clntref: 0,
last_use: 0,
expires: 0,
error: 0,
used: 0,
id: 0,
ts: 0,
ts_age: 0,
}),
RouteAttribute::Preference(RoutePreference::Medium),
],
};
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_mpls_route_to_ipv6() {
let raw = vec![
0x1c, 0x14, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x01, 0x00, 0x00, 0x12, 0xc1, 0x00, 0x16, 0x00, 0x12, 0x00,
0x0a, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00,
0x07, 0x00, 0x00, 0x00,
];
let expected = RouteMessage {
header: RouteHeader {
address_family: AddressFamily::Mpls,
destination_prefix_length: 20,
source_prefix_length: 0,
tos: 0,
table: 254,
protocol: RouteProtocol::Boot,
scope: RouteScope::Universe,
kind: RouteType::Unicast,
flags: RouteFlags::empty(),
},
attributes: vec![
RouteAttribute::Destination(RouteAddress::Mpls(MplsLabel {
label: 300,
traffic_class: 0,
bottom_of_stack: true,
ttl: 0,
})),
RouteAttribute::Via(
Ipv6Addr::from_str("2001:db8:1::2").unwrap().into(),
),
RouteAttribute::Oif(7),
],
};
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_mpls_route_relable_new_dst() {
let raw = vec![
0x1c, 0x14, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x01, 0x00, 0x00, 0x06, 0x41, 0x00, 0x08, 0x00, 0x13, 0x00,
0x00, 0x0c, 0x81, 0x00, 0x16, 0x00, 0x12, 0x00, 0x0a, 0x00, 0x20, 0x01,
0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00, 0x00,
];
let expected = RouteMessage {
header: RouteHeader {
address_family: AddressFamily::Mpls,
destination_prefix_length: 20,
source_prefix_length: 0,
tos: 0,
table: 254,
protocol: RouteProtocol::Boot,
scope: RouteScope::Universe,
kind: RouteType::Unicast,
flags: RouteFlags::empty(),
},
attributes: vec![
RouteAttribute::Destination(RouteAddress::Mpls(MplsLabel {
label: 100,
traffic_class: 0,
bottom_of_stack: true,
ttl: 0,
})),
RouteAttribute::NewDestination(vec![MplsLabel {
label: 200,
traffic_class: 0,
bottom_of_stack: true,
ttl: 0,
}]),
RouteAttribute::Via(
Ipv6Addr::from_str("2001:db8:1::2").unwrap().into(),
),
RouteAttribute::Oif(9),
],
};
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_mpls_ttl_propagate() {
let raw = vec![
0x1c, 0x14, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x01, 0x00, 0x00, 0x06, 0x41, 0x00, 0x05, 0x00, 0x1a, 0x00,
0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x13, 0x00, 0x00, 0x0c, 0x81, 0x00,
0x16, 0x00, 0x12, 0x00, 0x0a, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
0x08, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00,
];
let expected = RouteMessage {
header: RouteHeader {
address_family: AddressFamily::Mpls,
destination_prefix_length: 20,
source_prefix_length: 0,
tos: 0,
table: 254,
protocol: RouteProtocol::Boot,
scope: RouteScope::Universe,
kind: RouteType::Unicast,
flags: RouteFlags::empty(),
},
attributes: vec![
RouteAttribute::Destination(RouteAddress::Mpls(MplsLabel {
label: 100,
traffic_class: 0,
bottom_of_stack: true,
ttl: 0,
})),
RouteAttribute::TtlPropagate(RouteMplsTtlPropagation::Enabled),
RouteAttribute::NewDestination(vec![MplsLabel {
label: 200,
traffic_class: 0,
bottom_of_stack: true,
ttl: 0,
}]),
RouteAttribute::Via(
Ipv6Addr::from_str("2001:db8:1::2").unwrap().into(),
),
RouteAttribute::Oif(8),
],
};
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);
}