sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
#[allow(unused_imports)]
use crate::platform::MIN_USERS;
#[allow(unused_imports)]
use crate::*;

// A compilation failure here means the current OS does not implement these items correctly.
#[cfg(feature = "system")]
#[test]
fn check_macro_types() {
    fn check_is_supported(_: bool) {}

    check_is_supported(System::new().is_ok());
}

// A compilation failure here means the current OS does not implement these items correctly.
#[cfg(feature = "system")]
#[test]
fn check_macro_types2() {
    fn check_supported_signals(_: &'static [Signal]) {}
    fn check_minimum_cpu_update_interval(_: std::time::Duration) {}

    check_supported_signals(SUPPORTED_SIGNALS);
    check_minimum_cpu_update_interval(MINIMUM_CPU_UPDATE_INTERVAL);
}

#[cfg(feature = "user")]
#[test]
fn check_uid_gid() {
    let mut users = check_unsupported!(Users::new());
    assert!(users.is_empty());
    users.refresh();
    assert!(users.len() >= MIN_USERS);

    #[cfg(not(target_os = "windows"))]
    {
        let user = users
            .iter()
            .find(|u| u.name() == "root")
            .expect("no root user");
        assert_eq!(**user.id(), 0);
        let gid = user.group_id().expect("root user has no group");
        assert_eq!(*gid, 0);
        if let Some(user) = users.iter().find(|u| u.group_id().is_some_and(|g| *g > 0)) {
            assert!(**user.id() > 0);
            assert!(user.group_id().is_some_and(|g| *g > 0));
        }
        assert!(users.iter().filter(|u| **u.id() > 0).count() > 0);
    }

    #[cfg(feature = "system")]
    {
        // Verify that `get_user_by_id` resolves the IDs reported by processes.
        if let Ok(s) = System::new_with_specifics(
            RefreshKind::nothing()
                .with_processes(ProcessRefreshKind::nothing().with_user(UpdateKind::Always)),
        ) {
            assert!(
                s.processes()
                    .values()
                    .filter_map(|p| p.user_id())
                    .any(|uid| users.get_user_by_id(uid).is_some())
            );
        }
    }
}

#[cfg(all(feature = "system", feature = "user"))]
#[test]
fn check_all_process_uids_resolvable() {
    // Some Linux user IDs have no entry in the local user database, so they cannot be resolved reliably.
    if cfg!(target_os = "linux") {
        return;
    }
    let s = check_unsupported!(System::new_with_specifics(
        RefreshKind::nothing()
            .with_processes(ProcessRefreshKind::nothing().with_user(UpdateKind::Always)),
    ));
    let users = check_unsupported!(Users::new_with_refreshed_list());

    // Every user ID reported by a process must exist in the global user list.
    for process in s.processes().values() {
        if let Some(uid) = process.user_id() {
            assert!(users.get_user_by_id(uid).is_some(), "No UID {uid:?} found");
        }
    }
}

#[cfg(feature = "system")]
#[test]
fn ensure_is_supported_is_set_correctly() {
    if MIN_USERS > 0 {
        assert!(System::new().is_ok());
    } else {
        assert!(System::new().is_err());
    }
}