use pree::socket::platform::{get_sockets_info, SocketInfo};
use pree::socket::socket::{Socket, SocketConfig};
use pree::{Protocol, SocketState, TcpSocket, UdpSocket};
use std::net::{TcpListener, UdpSocket as StdUdpSocket};
use std::thread;
use std::time::Duration;
#[test]
fn test_socket_discovery_basic() {
let tcp_listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind TCP socket");
let tcp_port = tcp_listener.local_addr().unwrap().port();
let udp_socket = StdUdpSocket::bind("127.0.0.1:0").expect("Failed to bind 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");
assert!(!sockets.is_empty(), "Should find at least some sockets");
let generic_sockets = Socket::list().expect("Failed to list sockets");
assert!(
!generic_sockets.is_empty(),
"Should find at least some sockets"
);
let tcp_found = sockets.iter().any(|s| s.local_addr.port() == tcp_port);
let udp_found = sockets.iter().any(|s| s.local_addr.port() == udp_port);
if !tcp_found {
eprintln!("Warning: TCP test socket not found in socket list");
}
if !udp_found {
eprintln!("Warning: UDP test socket not found in socket list");
}
let tcp_count = TcpSocket::count_active().expect("Failed to count TCP sockets");
let udp_count = UdpSocket::count_active().expect("Failed to count UDP sockets");
assert!(tcp_count > 0, "Should have at least one TCP socket");
assert!(udp_count > 0, "Should have at least one UDP socket");
}
#[test]
fn test_socket_filtering() {
let sockets = get_sockets_info().expect("Failed to get socket info");
let tcp_count = sockets
.iter()
.filter(|s| s.protocol == Protocol::Tcp)
.count();
let udp_count = sockets
.iter()
.filter(|s| s.protocol == Protocol::Udp)
.count();
assert!(tcp_count > 0, "Should find TCP sockets");
assert!(udp_count > 0, "Should find UDP sockets");
let sockets_with_process_count = sockets.iter().filter(|s| s.process_id.is_some()).count();
assert!(
sockets_with_process_count > 0,
"Should find sockets with process info"
);
}
#[test]
fn test_socket_state_consistency() {
let sockets = get_sockets_info().expect("Failed to get socket info");
for socket in sockets {
assert!(
socket.local_addr.port() > 0 || socket.local_addr.ip().is_unspecified(),
"Local address should be valid"
);
assert!(
matches!(socket.protocol, Protocol::Tcp | Protocol::Udp),
"Protocol should be TCP or UDP"
);
match socket.protocol {
Protocol::Tcp => {
assert!(
matches!(
socket.state,
SocketState::Established
| SocketState::Listen
| SocketState::Connecting
| SocketState::Closing
| SocketState::Closed
| SocketState::Unknown(_)
),
"TCP socket should have valid state"
);
}
Protocol::Udp => {
assert!(
matches!(
socket.state,
SocketState::Established | SocketState::Bound | SocketState::Unknown(_)
),
"UDP socket should have valid state"
);
}
_ => {}
}
if let Some(pid) = socket.process_id {
assert!(pid > 0, "Process ID should be positive");
}
}
}
#[test]
fn test_socket_monitoring() {
let initial_sockets = get_sockets_info().expect("Failed to get initial socket info");
let initial_count = initial_sockets.len();
let _temp_listener =
TcpListener::bind("127.0.0.1:0").expect("Failed to create temporary socket");
thread::sleep(Duration::from_millis(100));
let updated_sockets = get_sockets_info().expect("Failed to get updated socket info");
let updated_count = updated_sockets.len();
if updated_count == initial_count {
eprintln!("Warning: Socket count unchanged after creating new socket");
}
assert!(!updated_sockets.is_empty(), "Should still have sockets");
}
#[test]
fn test_error_handling() {
let invalid_sockets = Socket::for_process(0).expect("Should handle invalid PID gracefully");
assert!(
invalid_sockets.is_empty(),
"Should return empty list for invalid PID"
);
let _port_sockets = Socket::listening_on(0).expect("Should handle port 0 gracefully");
let _ = TcpSocket::list().expect("TCP socket listing should not crash");
let _ = UdpSocket::list().expect("UDP socket listing should not crash");
}
#[test]
fn test_cross_platform_consistency() {
let sockets = get_sockets_info().expect("Failed to get socket info");
for socket in sockets {
test_socket_info_completeness(&socket);
}
}
fn test_socket_info_completeness(socket: &SocketInfo) {
assert!(
socket.local_addr.port() > 0 || socket.local_addr.ip().is_unspecified(),
"Local address should be valid"
);
assert!(
matches!(socket.protocol, Protocol::Tcp | Protocol::Udp),
"Protocol should be TCP or UDP"
);
if let SocketState::Unknown(msg) = &socket.state {
assert!(!msg.is_empty(), "Unknown state should have description");
}
if let Some(pid) = socket.process_id {
assert!(pid > 0, "Process ID should be positive");
}
if let Some(name) = &socket.process_name {
assert!(!name.is_empty(), "Process name should not be empty");
}
}
#[test]
fn test_socket_discovery_performance() {
use std::time::Instant;
let start = Instant::now();
let sockets = get_sockets_info().expect("Failed to get socket info");
let duration = start.elapsed();
println!("Socket discovery took: {duration:?}");
println!("Found {} sockets", sockets.len());
assert!(
duration < Duration::from_secs(5),
"Socket discovery should complete within 5 seconds"
);
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>() / u32::try_from(durations.len()).unwrap();
println!("Average socket discovery time: {avg_duration:?}");
assert!(
avg_duration < Duration::from_secs(2),
"Average socket discovery should be under 2 seconds"
);
}