diff --git a/Makefile b/Makefile index ce8dbad..c2bbd8b 100644 --- a/Makefile +++ b/Makefile @@ -141,8 +141,8 @@ bench: coverage: @echo "$(YELLOW)→ Génération du rapport de couverture...$(NC)" - $(CARGO) tarpaulin --out Html --output-dir target/coverage - @echo "$(GREEN)✓ Rapport disponible dans target/coverage/index.html$(NC)" + $(CARGO) tarpaulin --out json --output-dir target/coverage + @echo "$(GREEN)✓ Rapport disponible dans target/coverage/tarpaulin-report.json$(NC)" jjnew: @echo "$(YELLOW)→ Création d'un nouveau commit...$(NC)" diff --git a/src/lib.rs b/src/lib.rs index 270f24f..ff16ff5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,33 @@ pub mod ip_utils; pub use ip_utils::guess_local_ip; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_get_os_string_format() { + let os = get_os_string(); + assert!(!os.is_empty()); + assert!(os.contains("/")); + } + + #[test] + fn test_get_os_string_contains_os_type() { + let os = get_os_string(); + // Les types d'OS retournés par os_info peuvent varier, on vérifie juste le format + let parts: Vec<&str> = os.split('/').collect(); + assert_eq!( + parts.len(), + 2, + "OS string should have format 'Type/Version'" + ); + assert!(!parts[0].is_empty(), "OS type should not be empty"); + assert!(!parts[1].is_empty(), "OS version should not be empty"); + } +} + pub mod process; pub use process::{find_process_using_port, ProcessPortInfo, TransportProtocol}; /// Retourne une chaîne décrivant le système d'exploitation et sa version. diff --git a/src/process.rs b/src/process.rs index dbd41da..e272a9d 100644 --- a/src/process.rs +++ b/src/process.rs @@ -86,3 +86,77 @@ fn build_process_info( port, }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_transport_protocol_tcp() { + let protocol = TransportProtocol::Tcp; + assert!(matches!(protocol, TransportProtocol::Tcp)); + } + + #[test] + fn test_transport_protocol_udp() { + let protocol = TransportProtocol::Udp; + assert!(matches!(protocol, TransportProtocol::Udp)); + } + + #[test] + fn test_process_port_info_debug() { + let info = ProcessPortInfo { + pid: 1234, + process_name: "test".to_string(), + owner: "user".to_string(), + port: 8080, + }; + let debug_str = format!("{:?}", info); + assert!(debug_str.contains("1234")); + assert!(debug_str.contains("test")); + } + + #[test] + fn test_process_port_info_clone() { + let info = ProcessPortInfo { + pid: 5678, + process_name: "test_proc".to_string(), + owner: "test_user".to_string(), + port: 9000, + }; + let cloned = info.clone(); + assert_eq!(info.pid, cloned.pid); + assert_eq!(info.process_name, cloned.process_name); + assert_eq!(info.owner, cloned.owner); + assert_eq!(info.port, cloned.port); + } + + #[test] + fn test_find_process_using_port_invalid() { + let result = find_process_using_port(65535, TransportProtocol::Tcp); + assert!(result.is_none() || result.is_some()); + } + + #[test] + fn test_transport_protocol_copy() { + let protocol = TransportProtocol::Tcp; + let copied = protocol; + assert!(matches!(copied, TransportProtocol::Tcp)); + } + + #[test] + fn test_transport_protocol_traits() { + let tcp = TransportProtocol::Tcp; + let udp = TransportProtocol::Udp; + + assert_eq!(format!("{:?}", tcp), "Tcp"); + assert_eq!(format!("{:?}", udp), "Udp"); + } + + #[test] + fn test_build_process_info_none() { + let mut system = System::new(); + let result = build_process_info(&mut system, 8080, None); + assert!(result.is_none()); + } +}