pub const CAP_DAC_OVERRIDE: u64 = 1 << 1;
pub const CAP_DAC_READ_SEARCH: u64 = 1 << 2;
pub const CAP_FOWNER: u64 = 1 << 3;
pub const CAP_CHOWN: u64 = 1 << 0;
pub const CAP_LINUX_IMMUTABLE: u64 = 1 << 9;
pub const CAP_FSETID: u64 = 1 << 4;
pub const CAP_SYS_ADMIN: u64 = 1 << 21;
#[must_use]
pub fn has_cap(bitmask: u64, cap: u64) -> bool {
bitmask & cap != 0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn has_cap_detects_set_bit() {
assert!(has_cap(CAP_DAC_OVERRIDE, CAP_DAC_OVERRIDE));
}
#[test]
fn has_cap_returns_false_for_unset_bit() {
assert!(!has_cap(0, CAP_DAC_OVERRIDE));
}
#[test]
fn has_cap_detects_combined_bitmask() {
let bitmask = CAP_DAC_OVERRIDE | CAP_FOWNER;
assert!(has_cap(bitmask, CAP_FOWNER));
assert!(!has_cap(bitmask, CAP_DAC_READ_SEARCH));
}
#[test]
fn cap_dac_override_and_read_search_are_independent() {
assert!(!has_cap(CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH));
assert!(!has_cap(CAP_DAC_READ_SEARCH, CAP_DAC_OVERRIDE));
}
#[test]
fn cap_fsetid_is_bit_4() {
assert_eq!(CAP_FSETID, 1 << 4);
}
#[test]
fn cap_sys_admin_is_bit_21() {
assert_eq!(CAP_SYS_ADMIN, 1 << 21);
}
#[test]
fn cap_fsetid_independent_of_fowner() {
assert!(!has_cap(CAP_FOWNER, CAP_FSETID));
}
#[test]
fn cap_sys_admin_independent_of_dac_override() {
assert!(!has_cap(CAP_DAC_OVERRIDE, CAP_SYS_ADMIN));
}
}