1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// SPDX-License-Identifier: MIT
#![cfg(feature = "tokio_socket")]
use futures::stream::TryStreamExt;
use netlink_packet_route::link::{
InfoData, InfoKind, InfoMacVlan, LinkAttribute, LinkInfo, LinkMessage,
};
use tokio::runtime::Runtime;
use crate::{new_connection, Error, LinkHandle};
const IFACE_NAME: &str = "wg142"; // rand?
/*#[test]
fn create_get_delete_wg() {
let rt = Runtime::new().unwrap();
let handle = rt.block_on(_create_wg());
//assert!(handle.is_ok()); //this assert is pointeless, the unwrap below will achive the same thing with a more informative error.
let mut handle = handle.unwrap();
let msg = rt.block_on(_get_iface(&mut handle, IFACE_NAME.to_owned()));
assert!(msg.is_ok());
let msg = msg.unwrap();
assert!(has_nla(
&msg,
&LinkAttribute::LinkInfo(vec![LinkInfo::Kind(InfoKind::Wireguard)])
));
rt.block_on(_del_iface(&mut handle, msg.header.index))
.unwrap();
}
#[test]
fn create_get_delete_macvlan() {
const MACVLAN_IFACE_NAME: &str = "mvlan1";
const LOWER_DEVICE_IDX: u32 = 2;
const MACVLAN_MODE: u32 = 4; // bridge
let mac_address = &vec![2u8, 0, 0, 0, 0, 1];
let rt = Runtime::new().unwrap();
let handle = rt.block_on(_create_macvlan(
&MACVLAN_IFACE_NAME.to_owned(),
LOWER_DEVICE_IDX, /* assuming there's always a network interface in
* the system ... */
MACVLAN_MODE,
mac_address.to_vec(),
));
assert!(handle.is_ok());
let mut handle = handle.unwrap();
let msg =
rt.block_on(_get_iface(&mut handle, MACVLAN_IFACE_NAME.to_owned()));
assert!(msg.is_ok());
assert!(has_nla(
&msg.as_ref().unwrap(),
&LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::MacVlan),
LinkInfo::Data(InfoData::MacVlan(vec![
InfoMacVlan::Mode(MACVLAN_MODE),
InfoMacVlan::Flags(0), // defaulted by the kernel
InfoMacVlan::MacAddrCount(0), // defaulted by the kernel
InfoMacVlan::BcQueueLen(1000), // defaulted by the kernel
InfoMacVlan::BcQueueLenUsed(1000) // defaulted by the kernel
]))
])
));
assert!(has_nla(
&msg.as_ref().unwrap(),
&LinkAttribute::IfName(MACVLAN_IFACE_NAME.to_string())
));
assert!(has_nla(
&msg.as_ref().unwrap(),
&LinkAttribute::Link(LOWER_DEVICE_IDX)
));
assert!(has_nla(
&msg.as_ref().unwrap(),
&LinkAttribute::Address(mac_address.to_vec())
));
rt.block_on(_del_iface(&mut handle, msg.unwrap().header.index))
.unwrap();
}
fn has_nla(msg: &LinkMessage, nla: &LinkAttribute) -> bool {
msg.attributes.iter().any(|x| x == nla)
}
async fn _create_wg() -> Result<LinkHandle, Error> {
let (conn, handle, _) = new_connection().unwrap();
tokio::spawn(conn);
let link_handle = handle.link();
let mut req = link_handle.add();
let mutator = req.message_mut();
let info =
LinkAttribute::LinkInfo(vec![LinkInfo::Kind(InfoKind::Wireguard)]);
mutator.attributes.push(info);
mutator
.attributes
.push(LinkAttribute::IfName(IFACE_NAME.to_owned()));
req.execute().await?;
Ok(link_handle)
}
async fn _get_iface(
handle: &mut LinkHandle,
iface_name: String,
) -> Result<LinkMessage, Error> {
let mut links = handle.get().match_name(iface_name).execute();
let msg = links.try_next().await?;
msg.ok_or(Error::RequestFailed)
}
async fn _del_iface(handle: &mut LinkHandle, index: u32) -> Result<(), Error> {
handle.del(index).execute().await
}
async fn _create_macvlan(
name: &String,
lower_device_index: u32,
mode: u32,
mac: Vec<u8>,
) -> Result<LinkHandle, Error> {
let (conn, handle, _) = new_connection().unwrap();
tokio::spawn(conn);
let link_handle = handle.link();
let req = link_handle
.add()
.macvlan(name.to_string(), lower_device_index, mode)
.name(name.to_owned())
.address(mac);
req.execute().await?;
Ok(link_handle)
}
*/
|