use crate::models::{CapabilityDescriptor, CapilityHexValue};
pub fn is_valid_hex(value: CapilityHexValue, descriptor: &CapabilityDescriptor) -> bool {
let mut combined_value: CapilityHexValue = 0;
let mut sum_value: CapilityHexValue = 0;
for &unit_value in descriptor.values() {
combined_value |= unit_value;
sum_value += unit_value;
}
if combined_value > sum_value {
return false;
}
if (value & !combined_value) != 0 {
return false;
}
if value > combined_value {
return false;
}
true
}
pub fn get_max_hex_value_descriptor(descriptor: &CapabilityDescriptor) -> CapilityHexValue {
let mut max_value: CapilityHexValue = 0;
for &unit_value in descriptor.values() {
max_value |= unit_value;
}
max_value
}
pub fn get_sum_hex_value_descriptor(descriptor: &CapabilityDescriptor) -> CapilityHexValue {
let mut sum_value: CapilityHexValue = 0;
for &unit_value in descriptor.values() {
sum_value += unit_value;
}
sum_value
}
#[cfg(test)]
mod tests {
use super::*;
use crate::models::CapabilityDescriptor;
fn create_test_descriptor() -> CapabilityDescriptor {
let mut descriptor = CapabilityDescriptor::new();
descriptor.insert("Read".to_string(), 0x1);
descriptor.insert("Write".to_string(), 0x2);
descriptor.insert("Execute".to_string(), 0x4);
descriptor.insert("Admin".to_string(), 0x8);
descriptor
}
#[test]
fn test_is_valid_hex_valid_permissions() {
let descriptor = create_test_descriptor();
assert!(is_valid_hex(0x0, &descriptor)); assert!(is_valid_hex(0x1, &descriptor)); assert!(is_valid_hex(0x3, &descriptor)); assert!(is_valid_hex(0x7, &descriptor)); assert!(is_valid_hex(0xF, &descriptor)); }
#[test]
fn test_is_valid_hex_invalid_permissions() {
let descriptor = create_test_descriptor();
assert!(!is_valid_hex(0x10, &descriptor)); assert!(!is_valid_hex(0x20, &descriptor)); assert!(!is_valid_hex(0xFF, &descriptor)); assert!(!is_valid_hex(0x100, &descriptor)); }
#[test]
fn test_is_valid_hex_empty_descriptor() {
let descriptor = CapabilityDescriptor::new();
assert!(is_valid_hex(0x0, &descriptor)); assert!(!is_valid_hex(0x1, &descriptor)); assert!(!is_valid_hex(0x2, &descriptor)); }
#[test]
fn test_is_valid_hex_single_permission() {
let mut descriptor = CapabilityDescriptor::new();
descriptor.insert("OnlyPermission".to_string(), 0x4);
assert!(is_valid_hex(0x0, &descriptor)); assert!(is_valid_hex(0x4, &descriptor)); assert!(!is_valid_hex(0x1, &descriptor)); assert!(!is_valid_hex(0x2, &descriptor)); assert!(!is_valid_hex(0x8, &descriptor)); }
#[test]
fn test_get_max_hex_value_descriptor() {
let descriptor = create_test_descriptor();
assert_eq!(get_max_hex_value_descriptor(&descriptor), 0xF);
let empty_descriptor = CapabilityDescriptor::new();
assert_eq!(get_max_hex_value_descriptor(&empty_descriptor), 0x0);
let mut single_descriptor = CapabilityDescriptor::new();
single_descriptor.insert("Single".to_string(), 0x8);
assert_eq!(get_max_hex_value_descriptor(&single_descriptor), 0x8);
}
#[test]
fn test_get_sum_hex_value_descriptor() {
let descriptor = create_test_descriptor();
assert_eq!(get_sum_hex_value_descriptor(&descriptor), 0xF);
let empty_descriptor = CapabilityDescriptor::new();
assert_eq!(get_sum_hex_value_descriptor(&empty_descriptor), 0x0);
let mut overlapping_descriptor = CapabilityDescriptor::new();
overlapping_descriptor.insert("Permission1".to_string(), 0x3); overlapping_descriptor.insert("Permission2".to_string(), 0x1);
assert_eq!(get_max_hex_value_descriptor(&overlapping_descriptor), 0x3);
assert_eq!(get_sum_hex_value_descriptor(&overlapping_descriptor), 0x4);
}
#[test]
fn test_descriptor_integrity_validation() {
let descriptor = create_test_descriptor();
let max_value = get_max_hex_value_descriptor(&descriptor);
let sum_value = get_sum_hex_value_descriptor(&descriptor);
assert_eq!(max_value, sum_value); assert!(is_valid_hex(max_value, &descriptor));
}
#[test]
fn test_large_permission_system() {
let mut large_descriptor = CapabilityDescriptor::new();
for i in 0..10 {
large_descriptor.insert(format!("Permission{}", i), 1 << i);
}
let max_value = get_max_hex_value_descriptor(&large_descriptor);
assert_eq!(max_value, 0x3FF);
assert!(is_valid_hex(0x0, &large_descriptor));
assert!(is_valid_hex(0x1, &large_descriptor));
assert!(is_valid_hex(0x3FF, &large_descriptor)); assert!(is_valid_hex(0x155, &large_descriptor));
assert!(!is_valid_hex(0x400, &large_descriptor)); assert!(!is_valid_hex(0x800, &large_descriptor)); }
#[test]
fn test_boundary_conditions() {
let mut max_descriptor = CapabilityDescriptor::new();
max_descriptor.insert("MaxPermission".to_string(), 0x40000000);
assert!(is_valid_hex(0x0, &max_descriptor));
assert!(is_valid_hex(0x40000000, &max_descriptor));
assert!(!is_valid_hex(0x80000000u32 as i32, &max_descriptor));
let mut negative_descriptor = CapabilityDescriptor::new();
negative_descriptor.insert("NegativeTest".to_string(), -1);
assert!(!is_valid_hex(1, &negative_descriptor)); }
#[test]
fn test_zero_permission_value() {
let descriptor = create_test_descriptor();
assert!(is_valid_hex(0x0, &descriptor));
let empty_descriptor = CapabilityDescriptor::new();
assert!(is_valid_hex(0x0, &empty_descriptor));
}
#[test]
fn test_specific_bit_patterns() {
let descriptor = create_test_descriptor();
assert!(is_valid_hex(0x5, &descriptor)); assert!(is_valid_hex(0xA, &descriptor)); assert!(is_valid_hex(0xC, &descriptor));
assert!(!is_valid_hex(0x11, &descriptor)); assert!(!is_valid_hex(0x33, &descriptor)); }
}