#![cfg(target_os = "linux")]
use pree::socket::platform::get_sockets_info;
use pree::{Protocol, SocketState};
use std::collections::HashMap;
use std::fs;
use std::net::{TcpListener, UdpSocket};
use std::process::Command;
use std::thread;
use std::time::Duration;
#[test]
fn test_linux_proc_socket_enumeration() {
let sockets = get_sockets_info().expect("Failed to get sockets via /proc");
assert!(
!sockets.is_empty(),
"Should find sockets using /proc filesystem"
);
let tcp_sockets: Vec<_> = sockets
.iter()
.filter(|s| s.protocol == Protocol::Tcp)
.collect();
let udp_sockets: Vec<_> = sockets
.iter()
.filter(|s| s.protocol == Protocol::Udp)
.collect();
assert!(!tcp_sockets.is_empty(), "Should find TCP sockets");
assert!(!udp_sockets.is_empty(), "Should find UDP sockets");
let sockets_with_process: Vec<_> = sockets.iter().filter(|s| s.process_id.is_some()).collect();
assert!(
!sockets_with_process.is_empty(),
"/proc parsing should provide process information"
);
}
#[test]
fn test_linux_process_info_accuracy() {
let sockets = get_sockets_info().expect("Failed to get socket info");
let mut process_map: HashMap<u32, String> = HashMap::new();
for socket in &sockets {
if let (Some(pid), Some(name)) = (socket.process_id, &socket.process_name) {
process_map.insert(pid, name.clone());
}
}
assert!(
!process_map.is_empty(),
"Should find processes associated with sockets"
);
for (pid, name) in &process_map {
assert!(*pid > 0, "Process ID should be positive");
assert!(!name.is_empty(), "Process name should not be empty");
let proc_path = format!("/proc/{}", pid);
if std::path::Path::new(&proc_path).exists() {
let comm_path = format!("/proc/{}/comm", pid);
if let Ok(proc_name) = fs::read_to_string(&comm_path) {
let proc_name = proc_name.trim();
assert!(
name.contains(proc_name)
|| proc_name.contains(name)
|| name.starts_with(proc_name)
|| proc_name.starts_with(name),
"Process name mismatch: socket='{}', proc='{}'",
name,
proc_name
);
}
}
}
println!("Validated {} process entries", process_map.len());
}
#[test]
fn test_linux_proc_net_parsing() {
let sockets = get_sockets_info().expect("Failed to get socket info");
if let Ok(tcp_content) = fs::read_to_string("/proc/net/tcp") {
let proc_tcp_lines: Vec<_> = tcp_content.lines().skip(1).collect(); let our_tcp_sockets: Vec<_> = sockets
.iter()
.filter(|s| s.protocol == Protocol::Tcp)
.collect();
println!(
"Found {} TCP sockets in our implementation",
our_tcp_sockets.len()
);
println!("Found {} TCP lines in /proc/net/tcp", proc_tcp_lines.len());
let ratio = our_tcp_sockets.len() as f64 / proc_tcp_lines.len() as f64;
assert!(
ratio > 0.5 && ratio <= 1.2,
"TCP socket count should be reasonably close to /proc/net/tcp"
);
}
if let Ok(udp_content) = fs::read_to_string("/proc/net/udp") {
let proc_udp_lines: Vec<_> = udp_content.lines().skip(1).collect(); let our_udp_sockets: Vec<_> = sockets
.iter()
.filter(|s| s.protocol == Protocol::Udp)
.collect();
println!(
"Found {} UDP sockets in our implementation",
our_udp_sockets.len()
);
println!("Found {} UDP lines in /proc/net/udp", proc_udp_lines.len());
let ratio = our_udp_sockets.len() as f64 / proc_udp_lines.len() as f64;
assert!(
ratio > 0.5 && ratio <= 1.2,
"UDP socket count should be reasonably close to /proc/net/udp"
);
}
}
#[test]
fn test_linux_tcp_socket_states() {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to create listening socket");
let listening_port = listener.local_addr().unwrap().port();
thread::sleep(Duration::from_millis(100));
let sockets = get_sockets_info().expect("Failed to get socket info");
let listening_socket = sockets.iter().find(|s| {
s.local_addr.port() == listening_port
&& s.protocol == Protocol::Tcp
&& matches!(s.state, SocketState::Listen)
});
if listening_socket.is_some() {
println!(
"Successfully found listening socket on port {}",
listening_port
);
} else {
eprintln!("Warning: Could not find our test listening socket");
}
let state_counts = sockets.iter().filter(|s| s.protocol == Protocol::Tcp).fold(
HashMap::new(),
|mut acc, socket| {
*acc.entry(&socket.state).or_insert(0) += 1;
acc
},
);
println!("TCP socket states found:");
for (state, count) in &state_counts {
println!(" {:?}: {}", state, count);
}
assert!(state_counts.len() > 0, "Should find various TCP states");
drop(listener);
}
#[test]
fn test_linux_udp_socket_detection() {
let udp_socket = UdpSocket::bind("127.0.0.1:0").expect("Failed to create UDP socket");
let udp_port = udp_socket.local_addr().unwrap().port();
thread::sleep(Duration::from_millis(100));
let sockets = get_sockets_info().expect("Failed to get socket info");
let udp_sockets: Vec<_> = sockets
.iter()
.filter(|s| s.protocol == Protocol::Udp)
.collect();
assert!(!udp_sockets.is_empty(), "Should find UDP sockets");
let our_socket = udp_sockets.iter().find(|s| s.local_addr.port() == udp_port);
if our_socket.is_some() {
println!("Successfully found our UDP socket on port {}", udp_port);
} else {
eprintln!("Warning: Could not find our test UDP socket");
}
drop(udp_socket);
}
#[test]
fn test_linux_performance_vs_netstat() {
use std::time::Instant;
let start = Instant::now();
let our_sockets = get_sockets_info().expect("Failed to get sockets");
let our_duration = start.elapsed();
println!(
"Our implementation found {} sockets in {:?}",
our_sockets.len(),
our_duration
);
let start = Instant::now();
let ss_result = Command::new("ss").args(["-tuln"]).output();
if let Ok(output) = ss_result {
let ss_duration = start.elapsed();
let ss_lines = String::from_utf8_lossy(&output.stdout).lines().count();
println!("ss found ~{} lines in {:?}", ss_lines, ss_duration);
println!(
"Performance comparison - ours: {:?} vs ss: {:?}",
our_duration, ss_duration
);
} else {
println!("ss command not available, trying netstat");
let start = Instant::now();
let netstat_result = Command::new("netstat").args(["-tuln"]).output();
if let Ok(output) = netstat_result {
let netstat_duration = start.elapsed();
let netstat_lines = String::from_utf8_lossy(&output.stdout).lines().count();
println!(
"netstat found ~{} lines in {:?}",
netstat_lines, netstat_duration
);
}
}
let mut durations = Vec::new();
for _ in 0..5 {
let start = Instant::now();
let _ = get_sockets_info().expect("Failed to get socket info");
durations.push(start.elapsed());
}
let avg_duration = durations.iter().sum::<Duration>() / durations.len() as u32;
println!("Average duration over 5 runs: {:?}", avg_duration);
assert!(
avg_duration < Duration::from_secs(2),
"Socket enumeration should complete within 2 seconds"
);
}
#[test]
fn test_linux_inode_mapping() {
let sockets = get_sockets_info().expect("Failed to get socket info");
let mut inode_count = 0;
for socket in &sockets {
if socket.process_id.is_some() {
inode_count += 1;
}
}
println!("Found {} sockets with process information", inode_count);
assert!(inode_count > 0, "Should find sockets with process mapping");
}
#[test]
fn test_linux_proc_filesystem_features() {
assert!(
std::path::Path::new("/proc/net/tcp").exists(),
"/proc/net/tcp should exist"
);
assert!(
std::path::Path::new("/proc/net/udp").exists(),
"/proc/net/udp should exist"
);
if std::path::Path::new("/proc/net/tcp6").exists() {
println!("IPv6 support detected (/proc/net/tcp6 exists)");
let sockets = get_sockets_info().expect("Failed to get socket info");
let ipv6_sockets: Vec<_> = sockets.iter().filter(|s| s.local_addr.is_ipv6()).collect();
println!("Found {} IPv6 sockets", ipv6_sockets.len());
}
assert!(
std::path::Path::new("/proc/self").exists(),
"/proc/self should exist"
);
assert!(
std::path::Path::new("/proc/self/fd").exists(),
"/proc/self/fd should exist"
);
}
#[test]
fn test_linux_socket_extended_info() {
let sockets = get_sockets_info().expect("Failed to get socket info");
let inet_sockets: Vec<_> = sockets.iter().filter(|s| s.local_addr.is_ipv4()).collect();
let inet6_sockets: Vec<_> = sockets.iter().filter(|s| s.local_addr.is_ipv6()).collect();
println!("Found {} IPv4 sockets", inet_sockets.len());
println!("Found {} IPv6 sockets", inet6_sockets.len());
assert!(!inet_sockets.is_empty(), "Should find IPv4 sockets");
let tcp_count = sockets
.iter()
.filter(|s| s.protocol == Protocol::Tcp)
.count();
let udp_count = sockets
.iter()
.filter(|s| s.protocol == Protocol::Udp)
.count();
println!(
"Protocol distribution - TCP: {}, UDP: {}",
tcp_count, udp_count
);
assert!(tcp_count > 0, "Should find TCP sockets");
assert!(udp_count > 0, "Should find UDP sockets");
}
#[test]
fn test_linux_edge_cases() {
let mut handles = Vec::new();
for _ in 0..20 {
let handle = thread::spawn(|| {
let _tcp = TcpListener::bind("127.0.0.1:0").ok();
let _udp = UdpSocket::bind("127.0.0.1:0").ok();
thread::sleep(Duration::from_millis(50));
});
handles.push(handle);
}
let sockets = get_sockets_info().expect("Should handle concurrent socket operations");
assert!(!sockets.is_empty(), "Should still find sockets under load");
for handle in handles {
let _ = handle.join();
}
let sockets_before = get_sockets_info().expect("Failed to get sockets");
let sockets_after = get_sockets_info().expect("Failed to get sockets");
let diff = (sockets_before.len() as i32 - sockets_after.len() as i32).abs();
assert!(diff < 50, "Socket count should be relatively stable");
}
#[test]
fn test_linux_resource_usage() {
use std::time::Instant;
let iterations = 100;
let start = Instant::now();
for i in 0..iterations {
let sockets = get_sockets_info().expect("Failed to get socket info");
let tcp_count = sockets
.iter()
.filter(|s| s.protocol == Protocol::Tcp)
.count();
if i % 20 == 0 {
println!("Iteration {}: found {} TCP sockets", i, tcp_count);
}
if i % 10 == 0 {
thread::sleep(Duration::from_millis(1));
}
}
let total_duration = start.elapsed();
println!(
"Completed {} iterations in {:?}",
iterations, total_duration
);
println!("Average per iteration: {:?}", total_duration / iterations);
assert!(
total_duration < Duration::from_secs(20),
"Should complete stress test within 20 seconds"
);
}
#[test]
fn test_linux_networking_features() {
let sockets = get_sockets_info().expect("Failed to get socket info");
let system_ports = [22, 53, 80, 443, 631]; let mut found_system_sockets = 0;
for &port in &system_ports {
let port_sockets: Vec<_> = sockets
.iter()
.filter(|s| s.local_addr.port() == port)
.collect();
if !port_sockets.is_empty() {
println!(
"Found {} sockets on system port {}",
port_sockets.len(),
port
);
found_system_sockets += 1;
}
}
println!(
"Found system sockets on {}/{} common ports",
found_system_sockets,
system_ports.len()
);
let localhost_sockets: Vec<_> = sockets
.iter()
.filter(|s| s.local_addr.ip().is_loopback())
.collect();
let external_sockets: Vec<_> = sockets
.iter()
.filter(|s| !s.local_addr.ip().is_loopback() && !s.local_addr.ip().is_unspecified())
.collect();
println!("Localhost sockets: {}", localhost_sockets.len());
println!("External sockets: {}", external_sockets.len());
assert!(
!localhost_sockets.is_empty(),
"Should find localhost sockets"
);
}