#[cfg(test)]
mod tests {
use crate::app::initialization::{is_ipv4_address, looks_like_host_specification};
#[test]
fn test_looks_like_host_user_at_host_format() {
assert!(looks_like_host_specification("user@localhost"));
assert!(looks_like_host_specification("admin@server"));
assert!(looks_like_host_specification("root@192.168.1.1"));
assert!(looks_like_host_specification("test@host.example.com"));
}
#[test]
fn test_looks_like_host_port_format() {
assert!(looks_like_host_specification("localhost:22"));
assert!(looks_like_host_specification("server:2222"));
assert!(looks_like_host_specification("192.168.1.1:22"));
assert!(looks_like_host_specification("host.example.com:22"));
}
#[test]
fn test_looks_like_host_ssh_uri() {
assert!(looks_like_host_specification("ssh://localhost"));
assert!(looks_like_host_specification("ssh://user@host"));
assert!(looks_like_host_specification("ssh://server:22"));
assert!(looks_like_host_specification("ssh://host.example.com"));
}
#[test]
fn test_looks_like_host_fqdn() {
assert!(looks_like_host_specification("server.example.com"));
assert!(looks_like_host_specification("host.local"));
assert!(looks_like_host_specification("sub.domain.example.org"));
assert!(looks_like_host_specification("192.168.1.1"));
}
#[test]
fn test_looks_like_host_ipv6() {
assert!(looks_like_host_specification("[::1]"));
assert!(looks_like_host_specification("[::1]:22"));
assert!(looks_like_host_specification("[2001:db8::1]"));
assert!(looks_like_host_specification(
"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:22"
));
}
#[test]
fn test_not_host_simple_commands() {
assert!(!looks_like_host_specification("whoami"));
assert!(!looks_like_host_specification("ls"));
assert!(!looks_like_host_specification("pwd"));
assert!(!looks_like_host_specification("date"));
}
#[test]
fn test_not_host_commands_with_args() {
assert!(!looks_like_host_specification("echo hello"));
assert!(!looks_like_host_specification("ls -la"));
assert!(!looks_like_host_specification("grep pattern file"));
}
#[test]
fn test_is_ipv4_address_valid() {
assert!(is_ipv4_address("127.0.0.1"));
assert!(is_ipv4_address("192.168.1.1"));
assert!(is_ipv4_address("10.0.0.1"));
assert!(is_ipv4_address("172.16.0.1"));
assert!(is_ipv4_address("0.0.0.0"));
assert!(is_ipv4_address("255.255.255.255"));
assert!(is_ipv4_address("1.1.1.1"));
assert!(is_ipv4_address("8.8.8.8"));
}
#[test]
fn test_is_ipv4_address_invalid() {
assert!(!is_ipv4_address("999.999.999.999"));
assert!(!is_ipv4_address("256.1.1.1"));
assert!(!is_ipv4_address("1.256.1.1"));
assert!(!is_ipv4_address("1.1.256.1"));
assert!(!is_ipv4_address("1.1.1.256"));
assert!(!is_ipv4_address("1.2.3"));
assert!(!is_ipv4_address("1.2"));
assert!(!is_ipv4_address("1"));
assert!(!is_ipv4_address("1.2.3.4.5"));
assert!(!is_ipv4_address("a.b.c.d"));
assert!(!is_ipv4_address("192.168.1.x"));
assert!(!is_ipv4_address(""));
assert!(!is_ipv4_address("..."));
assert!(!is_ipv4_address("1..1.1"));
}
#[test]
fn test_looks_like_host_localhost() {
assert!(looks_like_host_specification("localhost"));
assert!(looks_like_host_specification("localhost.localdomain"));
}
#[test]
fn test_looks_like_host_ipv4() {
assert!(looks_like_host_specification("127.0.0.1"));
assert!(looks_like_host_specification("192.168.1.1"));
assert!(looks_like_host_specification("10.0.0.1"));
assert!(looks_like_host_specification("0.0.0.0"));
assert!(looks_like_host_specification("255.255.255.255"));
assert!(!is_ipv4_address("999.999.999.999")); assert!(!is_ipv4_address("1.2.3")); }
#[test]
fn test_not_host_simple_hostname() {
assert!(looks_like_host_specification("localhost")); assert!(!looks_like_host_specification("server"));
assert!(!looks_like_host_specification("hostname"));
}
#[test]
fn test_edge_cases() {
assert!(!looks_like_host_specification(""));
assert!(!looks_like_host_specification("a"));
assert!(!looks_like_host_specification(".."));
assert!(!looks_like_host_specification("."));
assert!(looks_like_host_specification("@"));
assert!(looks_like_host_specification(":")); }
#[test]
fn test_performance_early_returns() {
assert!(looks_like_host_specification("user@host")); assert!(looks_like_host_specification("[::1]")); assert!(looks_like_host_specification("ssh://host")); assert!(looks_like_host_specification("host:22")); }
#[test]
fn test_internationalized_domains() {
assert!(looks_like_host_specification("xn--n3h.com"));
assert!(looks_like_host_specification("서버.한국")); }
#[test]
fn test_special_characters() {
assert!(looks_like_host_specification("my-server.example.com"));
assert!(looks_like_host_specification("web-01:22"));
assert!(looks_like_host_specification("my_server.local"));
assert!(looks_like_host_specification("server123.example.com"));
assert!(looks_like_host_specification("host1:22"));
}
}