amélioration de la webapp
This commit is contained in:
@@ -114,22 +114,25 @@ mod tests {
|
||||
#[test]
|
||||
fn test_guess_local_ip_returns_valid_ip() {
|
||||
let ip = guess_local_ip();
|
||||
|
||||
|
||||
// Vérifie que le résultat est parsable comme une IP
|
||||
assert!(ip.parse::<IpAddr>().is_ok(), "Should return a valid IP address");
|
||||
assert!(
|
||||
ip.parse::<IpAddr>().is_ok(),
|
||||
"Should return a valid IP address"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_guess_local_ip_not_empty() {
|
||||
let ip = guess_local_ip();
|
||||
|
||||
|
||||
assert!(!ip.is_empty(), "IP should not be empty");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_guess_local_ip_is_ipv4() {
|
||||
let ip = guess_local_ip();
|
||||
|
||||
|
||||
if let Ok(parsed_ip) = ip.parse::<IpAddr>() {
|
||||
assert!(parsed_ip.is_ipv4(), "Should return an IPv4 address");
|
||||
}
|
||||
@@ -141,7 +144,7 @@ mod tests {
|
||||
// (difficile à tester sans mocker, mais on vérifie la cohérence)
|
||||
let ip = guess_local_ip();
|
||||
let parsed = ip.parse::<IpAddr>().unwrap();
|
||||
|
||||
|
||||
// L'IP doit être soit locale (127.0.0.1) soit une IP privée valide
|
||||
assert!(
|
||||
parsed.is_loopback() || is_private_ip(&ip),
|
||||
@@ -152,7 +155,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_list_all_ips_no_loopback() {
|
||||
let ips = list_all_ips();
|
||||
|
||||
|
||||
// Vérifie qu'aucune adresse de loopback n'est présente
|
||||
for (_, addresses) in ips.iter() {
|
||||
for addr in addresses {
|
||||
@@ -169,13 +172,13 @@ mod tests {
|
||||
#[test]
|
||||
fn test_list_all_ips_only_ipv4() {
|
||||
let ips = list_all_ips();
|
||||
|
||||
|
||||
// Vérifie que seules des adresses IPv4 sont retournées
|
||||
for (iface_name, addresses) in ips.iter() {
|
||||
if iface_name == "error" {
|
||||
continue; // Skip error entries
|
||||
}
|
||||
|
||||
|
||||
for addr in addresses {
|
||||
if let Ok(parsed_ip) = addr.parse::<IpAddr>() {
|
||||
assert!(
|
||||
@@ -190,13 +193,13 @@ mod tests {
|
||||
#[test]
|
||||
fn test_list_all_ips_valid_format() {
|
||||
let ips = list_all_ips();
|
||||
|
||||
|
||||
// Vérifie que toutes les IPs sont dans un format valide
|
||||
for (iface_name, addresses) in ips.iter() {
|
||||
if iface_name == "error" {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
for addr in addresses {
|
||||
assert!(
|
||||
addr.parse::<IpAddr>().is_ok(),
|
||||
@@ -210,23 +213,26 @@ mod tests {
|
||||
#[test]
|
||||
fn test_list_all_ips_interface_names_not_empty() {
|
||||
let ips = list_all_ips();
|
||||
|
||||
|
||||
// Vérifie que les noms d'interface ne sont pas vides
|
||||
for (iface_name, _) in ips.iter() {
|
||||
assert!(!iface_name.is_empty(), "Interface names should not be empty");
|
||||
assert!(
|
||||
!iface_name.is_empty(),
|
||||
"Interface names should not be empty"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_list_all_ips_no_duplicate_ips_per_interface() {
|
||||
let ips = list_all_ips();
|
||||
|
||||
|
||||
// Vérifie qu'il n'y a pas de doublons par interface
|
||||
for (iface_name, addresses) in ips.iter() {
|
||||
if iface_name == "error" {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
let unique_addresses: std::collections::HashSet<_> = addresses.iter().collect();
|
||||
assert_eq!(
|
||||
addresses.len(),
|
||||
@@ -264,4 +270,4 @@ mod tests {
|
||||
assert!(!is_private_ip("8.8.8.8"));
|
||||
assert!(!is_private_ip("127.0.0.1")); // loopback n'est pas "privé" au sens réseau local
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ pub mod ip_utils;
|
||||
|
||||
pub use ip_utils::guess_local_ip;
|
||||
pub mod process;
|
||||
pub use process::{find_process_using_port, ProcessPortInfo, TransportProtocol};
|
||||
pub use process::{ProcessPortInfo, TransportProtocol, find_process_using_port};
|
||||
|
||||
/// Retourne une chaîne décrivant le système d'exploitation et sa version.
|
||||
///
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use netstat2::{get_sockets_info, AddressFamilyFlags, ProtocolFlags, ProtocolSocketInfo};
|
||||
use netstat2::{AddressFamilyFlags, ProtocolFlags, ProtocolSocketInfo, get_sockets_info};
|
||||
use sysinfo::{Pid, System};
|
||||
|
||||
/// Informations sur un processus utilisant un port réseau.
|
||||
@@ -39,8 +39,7 @@ pub fn find_process_using_port(port: u16, protocol: TransportProtocol) -> Option
|
||||
for socket in sockets {
|
||||
match socket.protocol_socket_info {
|
||||
ProtocolSocketInfo::Tcp(ref tcp_info)
|
||||
if matches!(protocol, TransportProtocol::Tcp)
|
||||
&& tcp_info.local_port == port =>
|
||||
if matches!(protocol, TransportProtocol::Tcp) && tcp_info.local_port == port =>
|
||||
{
|
||||
if let Some(info) =
|
||||
build_process_info(&mut system, port, socket.associated_pids.first())
|
||||
@@ -49,8 +48,7 @@ pub fn find_process_using_port(port: u16, protocol: TransportProtocol) -> Option
|
||||
}
|
||||
}
|
||||
ProtocolSocketInfo::Udp(ref udp_info)
|
||||
if matches!(protocol, TransportProtocol::Udp)
|
||||
&& udp_info.local_port == port =>
|
||||
if matches!(protocol, TransportProtocol::Udp) && udp_info.local_port == port =>
|
||||
{
|
||||
if let Some(info) =
|
||||
build_process_info(&mut system, port, socket.associated_pids.first())
|
||||
@@ -77,8 +75,7 @@ fn build_process_info(
|
||||
let owner = process
|
||||
.user_id()
|
||||
.and_then(|uid| {
|
||||
users::get_user_by_uid(**uid)
|
||||
.map(|user| user.name().to_string_lossy().into_owned())
|
||||
users::get_user_by_uid(**uid).map(|user| user.name().to_string_lossy().into_owned())
|
||||
})
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user