sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::fmt;

/// The operational state of an interface, based on `IfOperStatus` from [RFC 2863](https://datatracker.ietf.org/doc/html/rfc2863#section-3.1.12).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InterfaceOperationalState {
    #[doc(hidden)]
    #[non_exhaustive]
    /// The interface state is unsupported or holds an unrecognized value.
    Other,
    /// The interface can pass packets.
    Up = 1,
    /// The interface cannot pass packets.
    Down = 2,
    /// The interface cannot pass packets.
    Testing = 3,
    /// The interface state is unknown.
    Unknown = 4,
    /// The interface cannot pass packets.
    Dormant = 5,
    /// The interface cannot pass packets.
    NotPresent = 6,
    /// The interface cannot pass packets.
    LowerLayerDown = 7,
}

impl fmt::Display for InterfaceOperationalState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // see /sys/class/net/<iface>/operstate section at https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-net
        f.write_str(match self {
            InterfaceOperationalState::Other => "other",
            InterfaceOperationalState::Up => "up",
            InterfaceOperationalState::Down => "down",
            InterfaceOperationalState::Testing => "testing",
            InterfaceOperationalState::Unknown => "unknown",
            InterfaceOperationalState::Dormant => "dormant",
            InterfaceOperationalState::NotPresent => "notpresent",
            InterfaceOperationalState::LowerLayerDown => "lowerlayerdown",
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    use std::str::FromStr;

    // Verifies that `MacAddr` implements the `Display` and `Debug` traits.
    #[test]
    fn check_display_impl_mac_address() {
        println!(
            "{} {:?}",
            MacAddr([0x1, 0x2, 0x3, 0x4, 0x5, 0x6]),
            MacAddr([0xa, 0xb, 0xc, 0xd, 0xe, 0xf])
        );
    }

    #[test]
    fn check_mac_address_is_unspecified_true() {
        assert!(MacAddr::UNSPECIFIED.is_unspecified());
        assert!(MacAddr([0; 6]).is_unspecified());
    }

    #[test]
    fn check_mac_address_is_unspecified_false() {
        assert!(!MacAddr([1, 2, 3, 4, 5, 6]).is_unspecified());
    }

    #[test]
    fn check_mac_address_conversions() {
        let mac = MacAddr([0xa, 0xb, 0xc, 0xd, 0xe, 0xf]);

        let mac_s = mac.to_string();
        assert_eq!("0a:0b:0c:0d:0e:0f", mac_s);
        assert_eq!(Ok(mac), MacAddr::from_str(&mac_s));

        assert_eq!(
            MacAddr::from_str("0a:0b:0c:0d:0e:0f:01"),
            Err(MacAddrFromStrError::InvalidAddrFormat)
        );
        assert_eq!(
            MacAddr::from_str("0a:0b:0c:0d:0e"),
            Err(MacAddrFromStrError::InvalidAddrFormat)
        );
    }

    // Verifies that `IpNetwork` implements the `Display` and `Debug` traits.
    #[test]
    fn check_display_impl_ip_network_ipv4() {
        println!(
            "{} {:?}",
            IpNetwork {
                addr: IpAddr::from(Ipv4Addr::new(1, 2, 3, 4)),
                prefix: 3
            },
            IpNetwork {
                addr: IpAddr::from(Ipv4Addr::new(255, 255, 255, 0)),
                prefix: 21
            }
        );
    }

    #[test]
    fn check_display_impl_ip_network_ipv6() {
        println!(
            "{} {:?}",
            IpNetwork {
                addr: IpAddr::from(Ipv6Addr::new(0xffff, 0xaabb, 00, 0, 0, 0x000c, 11, 21)),
                prefix: 127
            },
            IpNetwork {
                addr: IpAddr::from(Ipv6Addr::new(0xffcc, 0, 0, 0xffcc, 0, 0xffff, 0, 0xccaa)),
                prefix: 120
            }
        )
    }

    #[test]
    fn check_ip_networks() {
        let networks = check_unsupported!(Networks::new_with_refreshed_list());
        if networks.iter().any(|(_, n)| !n.ip_networks().is_empty()) {
            return;
        }
        panic!("Networks should have at least one IP network ");
    }

    #[test]
    fn check_ip_network_conversions() {
        let addr = IpNetwork {
            addr: IpAddr::from(Ipv6Addr::new(0xff, 0xa, 0x8, 0x12, 0x7, 0xc, 0xa, 0xb)),
            prefix: 12,
        };

        let addr_s = addr.to_string();
        assert_eq!("ff:a:8:12:7:c:a:b/12", addr_s);
        assert_eq!(Ok(addr), IpNetwork::from_str(&addr_s));

        let addr = IpNetwork {
            addr: IpAddr::from(Ipv4Addr::new(255, 255, 255, 0)),
            prefix: 21,
        };

        let addr_s = addr.to_string();
        assert_eq!("255.255.255.0/21", addr_s);
        assert_eq!(Ok(addr), IpNetwork::from_str(&addr_s));

        assert_eq!(
            IpNetwork::from_str("ff:a:8:12:7:c:a:b"),
            Err(IpNetworkFromStrError::InvalidAddrFormat)
        );
        assert_eq!(
            IpNetwork::from_str("ff:a:8:12:7:c:a:b/12/12"),
            Err(IpNetworkFromStrError::InvalidAddrFormat)
        );
        match IpNetwork::from_str("0a:0b:0c:0d:0e/12") {
            Err(IpNetworkFromStrError::AddrParseError(_)) => {}
            x => panic!("expected `IpNetworkFromStrError::AddrParseError`, found {x:?}"),
        }
    }
}