use ember_tune_rs::sal::safety::{HardwareStateGuard, PowerLimitWatts}; use crate::common::fakesys::FakeSysBuilder; use std::fs; mod common; #[test] fn test_hardware_state_guard_panic_restoration() { let fake = FakeSysBuilder::new(); let pl1_path = fake.base_path().join("sys/class/powercap/intel-rapl:0/constraint_0_power_limit_uw"); fake.add_rapl("intel-rapl:0", "1000", "15000000"); // 15W original let target_files = vec![pl1_path.clone()]; // Simulate a scope where the guard is active { let mut _guard = HardwareStateGuard::acquire(&target_files, &[]).expect("Failed to acquire guard"); // Modify the file fs::write(&pl1_path, "25000000").expect("Failed to write new value"); assert_eq!(fs::read_to_string(&pl1_path).unwrap().trim(), "25000000"); // Guard is dropped here (simulating end of scope or panic) } // Verify restoration let restored = fs::read_to_string(&pl1_path).expect("Failed to read restored file"); assert_eq!(restored.trim(), "15000000"); } #[test] fn test_tdp_limit_bounds_checking() { // 1. Valid value assert!(PowerLimitWatts::try_new(15.0).is_ok()); // 2. Too low (Dangerous 0W or below 3W) let low_res = PowerLimitWatts::try_new(1.0); assert!(low_res.is_err()); assert!(low_res.unwrap_err().to_string().contains("outside safe bounds")); // 3. Too high (> 100W) let high_res = PowerLimitWatts::try_new(150.0); assert!(high_res.is_err()); assert!(high_res.unwrap_err().to_string().contains("outside safe bounds")); } #[test] fn test_0w_tdp_regression_prevention() { // The prime directive is to never set 0W. let zero_res = PowerLimitWatts::try_new(0.0); assert!(zero_res.is_err()); }