burrow/tun/tests/tokio.rs
Conrad Kramer fc79766a31
All checks were successful
Build Site / Next.js Build (push) Successful in 1m39s
Build Rust / Cargo Test (push) Successful in 3m28s
Build Apple / Build App (iOS Simulator) (push) Successful in 2m14s
Build Apple / Build App (macOS) (push) Successful in 2m14s
Skip tun tokio test without tun access
2026-03-19 04:56:56 -07:00

42 lines
1.2 KiB
Rust

#[cfg(all(feature = "tokio", not(target_os = "windows")))]
use std::{
io::ErrorKind,
net::Ipv4Addr,
};
#[cfg(all(feature = "tokio", not(target_os = "windows")))]
fn open_test_tun() -> Option<tun::TunInterface> {
match tun::TunInterface::new() {
Ok(tun) => Some(tun),
Err(error) if matches!(error.kind(), ErrorKind::NotFound | ErrorKind::PermissionDenied) => {
eprintln!("skipping test: {}", error);
None
}
Err(error) => panic!("failed to create tun interface: {error}"),
}
}
#[tokio::test]
#[cfg(all(feature = "tokio", not(target_os = "windows")))]
async fn test_create() {
let Some(tun) = open_test_tun() else {
return;
};
let _ = tun::tokio::TunInterface::new(tun).unwrap();
}
#[tokio::test]
#[ignore = "requires interactivity"]
#[cfg(all(feature = "tokio", not(target_os = "windows")))]
async fn test_write() {
let Some(tun) = open_test_tun() else {
return;
};
tun.set_ipv4_addr(Ipv4Addr::from([192, 168, 1, 10]))
.unwrap();
let async_tun = tun::tokio::TunInterface::new(tun).unwrap();
let mut buf = [0u8; 1500];
buf[0] = 6 << 4;
let bytes_written = async_tun.send(&buf).await.unwrap();
assert!(bytes_written > 0);
}