use serde::Serialize;
use crate::state::Probe;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub enum SeLinuxMode {
Enforcing,
Permissive,
Disabled,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SeLinuxState {
pub mode: SeLinuxMode,
pub subject_ctx: String,
pub target_ctx: String,
pub access_allowed: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AppArmorState {
pub profile_label: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct MacState {
pub selinux: Probe<SeLinuxState>,
pub apparmor: Probe<AppArmorState>,
}
impl Default for MacState {
fn default() -> Self {
Self {
selinux: Probe::Unknown,
apparmor: Probe::Unknown,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mac_state_default_has_unknown_probes() {
let state = MacState::default();
assert_eq!(state.selinux, Probe::Unknown);
assert_eq!(state.apparmor, Probe::Unknown);
}
#[test]
fn selinux_state_roundtrips_fields() {
let s = SeLinuxState {
mode: SeLinuxMode::Enforcing,
subject_ctx: "unconfined_u:unconfined_r:unconfined_t:s0".into(),
target_ctx: "system_u:object_r:httpd_sys_content_t:s0".into(),
access_allowed: true,
};
assert_eq!(s.mode, SeLinuxMode::Enforcing);
assert!(s.access_allowed);
}
#[test]
fn apparmor_state_roundtrips_label() {
let a = AppArmorState {
profile_label: "nginx (enforce)".into(),
};
assert_eq!(a.profile_label, "nginx (enforce)");
}
}