use std::net::{IpAddr, Ipv4Addr};
pub fn get_local_ip() -> Option<IpAddr> {
local_ip_address::local_ip()
.ok()
.or_else(|| local_ip_address::local_ipv6().ok())
}
pub fn get_local_ip_with_default() -> String {
get_local_ip()
.unwrap_or_else(|| IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))) .to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::Ipv4Addr;
#[test]
fn test_get_local_ip_returns_some_ip() {
let ip = get_local_ip();
assert!(ip.is_some(), "Should be able to get local IP address");
if let Some(ip_addr) = ip {
println!("Local IP address: {ip_addr}");
match ip_addr {
IpAddr::V4(ipv4) => {
assert!(!ipv4.is_unspecified(), "IPv4 should not be unspecified (0.0.0.0)");
println!("Got IPv4 address: {ipv4}");
}
IpAddr::V6(ipv6) => {
assert!(!ipv6.is_unspecified(), "IPv6 should not be unspecified (::)");
println!("Got IPv6 address: {ipv6}");
}
}
}
}
#[test]
fn test_get_local_ip_with_default_never_empty() {
let ip_string = get_local_ip_with_default();
assert!(!ip_string.is_empty(), "IP string should never be empty");
let parsed_ip: Result<IpAddr, _> = ip_string.parse();
assert!(parsed_ip.is_ok(), "Returned string should be a valid IP address: {ip_string}");
println!("Local IP with default: {ip_string}");
}
#[test]
fn test_get_local_ip_with_default_fallback() {
let default_ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
let ip_string = get_local_ip_with_default();
if get_local_ip().is_none() {
assert_eq!(ip_string, default_ip.to_string());
}
let parsed: Result<IpAddr, _> = ip_string.parse();
assert!(parsed.is_ok(), "Should always return a valid IP string");
}
#[test]
fn test_ip_address_types() {
if let Some(ip) = get_local_ip() {
match ip {
IpAddr::V4(ipv4) => {
println!("IPv4 address: {ipv4}");
assert!(!ipv4.is_multicast(), "Local IP should not be multicast");
assert!(!ipv4.is_broadcast(), "Local IP should not be broadcast");
let is_private = ipv4.is_private();
let is_loopback = ipv4.is_loopback();
println!("IPv4 is private: {is_private}, is loopback: {is_loopback}");
}
IpAddr::V6(ipv6) => {
println!("IPv6 address: {ipv6}");
assert!(!ipv6.is_multicast(), "Local IP should not be multicast");
let is_loopback = ipv6.is_loopback();
println!("IPv6 is loopback: {is_loopback}");
}
}
}
}
#[test]
fn test_ip_string_format() {
let ip_string = get_local_ip_with_default();
assert!(!ip_string.contains(' '), "IP string should not contain spaces");
assert!(!ip_string.is_empty(), "IP string should not be empty");
let parsed_ip: IpAddr = ip_string.parse().expect("Should parse as valid IP");
let back_to_string = parsed_ip.to_string();
println!("Original: {ip_string}, Parsed back: {back_to_string}");
}
#[test]
fn test_default_fallback_value() {
let default_ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
assert_eq!(default_ip.to_string(), "127.0.0.1");
if let IpAddr::V4(ipv4) = default_ip {
assert!(ipv4.is_loopback(), "Default IP should be loopback");
assert!(!ipv4.is_unspecified(), "Default IP should not be unspecified");
assert!(!ipv4.is_multicast(), "Default IP should not be multicast");
}
}
#[test]
fn test_consistency_between_functions() {
let ip_option = get_local_ip();
let ip_string = get_local_ip_with_default();
match ip_option {
Some(ip) => {
assert_eq!(ip.to_string(), ip_string, "Both functions should return the same IP when available");
}
None => {
assert_eq!(ip_string, "127.0.0.1", "Should return default value when no IP is available");
}
}
}
#[test]
fn test_multiple_calls_consistency() {
let ip1 = get_local_ip();
let ip2 = get_local_ip();
let ip_str1 = get_local_ip_with_default();
let ip_str2 = get_local_ip_with_default();
assert_eq!(ip1, ip2, "Multiple calls to get_local_ip should return same result");
assert_eq!(ip_str1, ip_str2, "Multiple calls to get_local_ip_with_default should return same result");
}
#[cfg(feature = "integration")]
#[test]
fn test_network_connectivity() {
if let Some(ip) = get_local_ip() {
match ip {
IpAddr::V4(ipv4) => {
assert!(!ipv4.is_unspecified(), "Should not be 0.0.0.0");
if !ipv4.is_loopback() {
println!("Got routable IPv4: {ipv4}");
}
}
IpAddr::V6(ipv6) => {
assert!(!ipv6.is_unspecified(), "Should not be ::");
if !ipv6.is_loopback() {
println!("Got routable IPv6: {ipv6}");
}
}
}
}
}
}