#![cfg(all(target_os = "linux", not(skip_patchbay)))]
use netwatch::interfaces::State;
use patchbay::{IpSupport, Lab};
use testresult::TestResult;
#[ctor::ctor(unsafe)]
fn userns_ctor() {
patchbay::init_userns().expect("failed to init userns");
}
async fn state_for_routed_device(ip_support: IpSupport) -> TestResult<State> {
let lab = Lab::new().await?;
let router = lab
.add_router("router")
.ip_support(ip_support)
.build()
.await?;
let device = lab.add_device("device").uplink(router.id()).build().await?;
let state = device.spawn(|_| State::new())?.await?;
Ok(state)
}
#[tokio::test]
async fn default_route_v4_only() -> TestResult {
let state = state_for_routed_device(IpSupport::V4Only).await?;
assert!(state.have_v4, "should have v4");
assert!(!state.have_v6, "should not have v6");
assert_eq!(state.default_route_interface.as_deref(), Some("eth0"));
Ok(())
}
#[tokio::test]
async fn default_route_v6_only() -> TestResult {
let state = state_for_routed_device(IpSupport::V6Only).await?;
assert!(!state.have_v4, "should not have v4");
assert!(state.have_v6, "should have v6");
assert_eq!(state.default_route_interface.as_deref(), Some("eth0"));
Ok(())
}
#[tokio::test]
async fn default_route_dual_stack() -> TestResult {
let state = state_for_routed_device(IpSupport::DualStack).await?;
assert!(state.have_v4, "should have v4");
assert!(state.have_v6, "should have v6");
assert_eq!(state.default_route_interface.as_deref(), Some("eth0"));
Ok(())
}
#[tokio::test]
async fn default_route_after_replug_v4_to_v6() -> TestResult {
let lab = Lab::new().await?;
let v4_router = lab
.add_router("v4")
.ip_support(IpSupport::V4Only)
.build()
.await?;
let v6_router = lab
.add_router("v6")
.ip_support(IpSupport::V6Only)
.build()
.await?;
let device = lab
.add_device("device")
.uplink(v4_router.id())
.build()
.await?;
let state = device.spawn(|_| State::new())?.await?;
assert!(state.have_v4, "should have v4");
assert!(!state.have_v6, "should not have v6");
assert_eq!(state.default_route_interface.as_deref(), Some("eth0"));
device.iface("eth0").unwrap().replug(v6_router.id()).await?;
let state = device.spawn(|_| State::new())?.await?;
assert!(!state.have_v4, "should not have v4");
assert!(state.have_v6, "should have v6");
assert_eq!(state.default_route_interface.as_deref(), Some("eth0"));
Ok(())
}