Skip to content

Commit

Permalink
Add a few tests for ping
Browse files Browse the repository at this point in the history
  • Loading branch information
swsnr committed Feb 7, 2025
1 parent 947f102 commit 7924ef5
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/net/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,80 @@ impl Display for PingDestination {
}
}
}

#[cfg(test)]
mod tests {
use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr},
str::FromStr,
time::Duration,
};

use gtk::gio::IOErrorEnum;

use super::PingDestination;

#[test]
fn ping_loopback_ipv4() {
glib::MainContext::new().block_on(async move {
let duration = super::ping_address(Ipv4Addr::LOCALHOST.into(), 4)
.await
.unwrap();
// A reasonable sanity test
assert!(duration < Duration::from_secs(5));
});
}

#[test]
fn ping_loopback_ipv6() {
glib::MainContext::new().block_on(async move {
let duration = super::ping_address(Ipv6Addr::LOCALHOST.into(), 4)
.await
.unwrap();
// A reasonable sanity test
assert!(duration < Duration::from_secs(5));
});
}

#[test]
fn ping_with_timeout_unroutable() {
glib::MainContext::new().block_on(async move {
let error = super::ping_address_with_timeout(
Ipv4Addr::from_str("192.0.2.42").unwrap().into(),
4,
Duration::from_secs(1),
)
.await
.unwrap_err();
assert!(error.matches(IOErrorEnum::TimedOut));
assert_eq!(error.message(), "Timeout after 1000ms");
});
}

#[test]
fn ping_destination_resolve() {
glib::MainContext::new().block_on(async move {
assert_eq!(
PingDestination::Addr(Ipv4Addr::LOCALHOST.into())
.resolve()
.await
.unwrap(),
vec![IpAddr::V4(Ipv4Addr::LOCALHOST)]
);
assert_eq!(
PingDestination::Addr(Ipv6Addr::LOCALHOST.into())
.resolve()
.await
.unwrap(),
vec![IpAddr::V6(Ipv6Addr::LOCALHOST)]
);
let addresses = PingDestination::Dns("localhost".into())
.resolve()
.await
.unwrap();
assert!(addresses.len() >= 2);
assert!(addresses.contains(&IpAddr::V4(Ipv4Addr::LOCALHOST)));
assert!(addresses.contains(&IpAddr::V6(Ipv6Addr::LOCALHOST)));
});
}
}

0 comments on commit 7924ef5

Please sign in to comment.