use netlink_packet_core::{Emitable, Parseable};
use crate::{
link::{
InfoData, InfoKind, InfoNetkit, LinkAttribute, LinkHeader, LinkInfo,
LinkLayerType, LinkMessage, LinkMessageBuffer, NetkitMode,
NetkitPolicy, NetkitScrub,
},
AddressFamily,
};
#[test]
fn test_create_netkit() {
let raw: Vec<u8> = vec![
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x03, 0x00, 0x6e, 0x6b, 0x74, 0x65, 0x73, 0x74, 0x30, 0x00, 0x2c, 0x00, 0x12, 0x00, 0x0b, 0x00, 0x01, 0x00, 0x6e, 0x65, 0x74, 0x6b, 0x69, 0x74, 0x00, 0x00, 0x1c, 0x00, 0x02, 0x00, 0x05, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, ];
let expected = LinkMessage {
header: LinkHeader {
interface_family: AddressFamily::Unspec,
index: 0,
link_layer_type: LinkLayerType::Netrom,
..Default::default()
},
attributes: vec![
LinkAttribute::IfName("nktest0".to_string()),
LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::Netkit),
LinkInfo::Data(InfoData::Netkit(vec![
InfoNetkit::Primary(true),
InfoNetkit::Policy(NetkitPolicy::Pass),
InfoNetkit::Mode(NetkitMode::L3),
])),
]),
],
};
assert_eq!(
expected,
LinkMessage::parse(&LinkMessageBuffer::new(&raw)).unwrap()
);
let mut buf = vec![0; expected.buffer_len()];
expected.emit(&mut buf);
assert_eq!(buf, raw);
}
#[test]
fn test_netkit_mode_display() {
assert_eq!("l2", NetkitMode::L2.to_string());
assert_eq!("l3", NetkitMode::L3.to_string());
assert_eq!("255", NetkitMode::Other(255).to_string());
}
#[test]
fn test_netkit_mode_from_str() {
use std::str::FromStr;
assert_eq!(NetkitMode::L2, "l2".parse().unwrap());
assert_eq!(NetkitMode::L3, "l3".parse().unwrap());
assert!(NetkitMode::from_str("42").is_err());
assert!(NetkitMode::from_str("bogus").is_err());
}
#[test]
fn test_netkit_policy_display() {
assert_eq!("forward", NetkitPolicy::Pass.to_string());
assert_eq!("blackhole", NetkitPolicy::Drop.to_string());
assert_eq!("redirect", NetkitPolicy::Redirect.to_string());
assert_eq!("255", NetkitPolicy::Other(255).to_string());
}
#[test]
fn test_netkit_policy_from_str() {
use std::str::FromStr;
assert_eq!(NetkitPolicy::Pass, "forward".parse().unwrap());
assert_eq!(NetkitPolicy::Drop, "blackhole".parse().unwrap());
assert_eq!(NetkitPolicy::Redirect, "redirect".parse().unwrap());
assert!(NetkitPolicy::from_str("42").is_err());
assert!(NetkitPolicy::from_str("bogus").is_err());
}
#[test]
fn test_netkit_scrub_display() {
assert_eq!("none", NetkitScrub::None.to_string());
assert_eq!("default", NetkitScrub::Default.to_string());
}
#[test]
fn test_netkit_scrub_from_str() {
use std::str::FromStr;
assert_eq!(NetkitScrub::None, "none".parse().unwrap());
assert_eq!(NetkitScrub::Default, "default".parse().unwrap());
assert!(NetkitScrub::from_str("bogus").is_err());
}