Skip to content

Commit

Permalink
Merge pull request #29 from robgonnella/add-network-tests
Browse files Browse the repository at this point in the history
[r-lanlib]: adds network tests
  • Loading branch information
robgonnella authored Oct 24, 2024
2 parents 49152ae + ca51879 commit bfa16af
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions r-lanlib/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,52 @@ fn get_interface_ipv4_and_cidr(interface: &PNetNetworkInterface) -> Option<(Stri
let cidr = String::from(format!("{base}/{prefix}"));
Some((ip, cidr))
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn returns_none_for_bogus_interface_name() {
let res = get_interface("noop");
assert!(res.is_none());
}

#[cfg(target_os = "macos")]
#[test]
fn returns_interface_by_name() {
let res = get_interface("en0");
assert!(res.is_some());
}

#[cfg(target_os = "linux")]
#[test]
fn returns_interface_by_name() {
let res = get_interface("eth0");
assert!(res.is_some());
}

#[test]
fn returns_default_interface() {
let res = get_default_interface();
assert!(res.is_some());
}

#[test]
fn returns_an_available_port_on_system() {
let res = get_available_port();
assert!(res.is_ok());
}

#[test]
fn get_ip_and_cidr_from_interface() {
let iface = pnet::datalink::interfaces()
.into_iter()
.find(|e| e.is_up() && !e.is_loopback() && e.ips.iter().find(|i| i.is_ipv4()).is_some())
.unwrap();
let (ip, cidr) = get_interface_ipv4_and_cidr(&iface).unwrap();
assert!(!ip.is_empty());
assert!(!cidr.is_empty());
}
}

0 comments on commit bfa16af

Please sign in to comment.