netstat_esr/
lib.rs

1//! Cross-platform library to retrieve network sockets information.
2//! Tries to be optimal by using low-level OS APIs instead of command line utilities.
3//! Provides unified interface and returns data structures which may have additional fields depending on platform.
4
5#![allow(non_camel_case_types)]
6
7#[macro_use]
8extern crate bitflags;
9extern crate libc;
10
11mod integrations;
12mod types;
13mod utils;
14
15pub use integrations::*;
16pub use types::*;
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21
22    #[test]
23    fn result_is_ok_for_any_flags() {
24        let af_flags_combs = (0..AddressFamilyFlags::all().bits() + 1)
25            .filter_map(|x| AddressFamilyFlags::from_bits(x))
26            .collect::<Vec<AddressFamilyFlags>>();
27        let proto_flags_combs = (0..ProtocolFlags::all().bits() + 1)
28            .filter_map(|x| ProtocolFlags::from_bits(x))
29            .collect::<Vec<ProtocolFlags>>();
30        for af_flags in af_flags_combs.iter() {
31            for proto_flags in proto_flags_combs.iter() {
32                assert!(get_sockets_info(*af_flags, *proto_flags).is_ok());
33            }
34        }
35    }
36
37    #[test]
38    fn result_is_empty_for_empty_flags() {
39        let sockets_info =
40            get_sockets_info(AddressFamilyFlags::empty(), ProtocolFlags::empty()).unwrap();
41        assert!(sockets_info.len() == 0);
42    }
43
44    #[test]
45    fn every_socket_is_either_tcp_or_udp() {
46        let af_flags = AddressFamilyFlags::all();
47        let proto_flags = ProtocolFlags::all();
48        let sockets_info = iterate_sockets_info(af_flags, proto_flags).unwrap();
49
50        for socket_info in sockets_info {
51            assert!(socket_info.is_tcp() == !socket_info.is_udp());
52        }
53    }
54}