use crate::{read_value, write_value, ProcError, ProcResult};
use lazy_static::lazy_static;
lazy_static! {
static ref RANDOM_ROOT: std::path::PathBuf = std::path::PathBuf::from("/proc/sys/kernel/random");
}
pub fn entropy_avail() -> ProcResult<u16> {
read_value(RANDOM_ROOT.join("entropy_avail"))
}
pub fn poolsize() -> ProcResult<u16> {
read_value(RANDOM_ROOT.join("poolsize"))
}
pub fn read_wakeup_threshold() -> ProcResult<u32> {
match read_value(RANDOM_ROOT.join("read_wakeup_threshold")) {
Ok(val) => Ok(val),
Err(err) => match err {
ProcError::NotFound(_) => read_value(RANDOM_ROOT.join("write_wakeup_threshold")),
err => Err(err),
},
}
}
pub fn write_wakeup_threshold(new_value: u32) -> ProcResult<()> {
write_value(RANDOM_ROOT.join("write_wakeup_threshold"), new_value)
}
pub fn uuid() -> ProcResult<String> {
read_value(RANDOM_ROOT.join("uuid"))
}
pub fn boot_id() -> ProcResult<String> {
read_value(RANDOM_ROOT.join("boot_id"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_entropy_avail() {
let entropy = entropy_avail().unwrap();
assert!(entropy <= 4096);
}
#[test]
fn test_poolsize() {
let _poolsize = poolsize().unwrap();
}
#[test]
fn test_read_wakeup_threshold() {
let threshold = read_wakeup_threshold().unwrap();
println!("{}", threshold);
}
#[test]
fn test_write_wakeup_threshold() {
let old_threshold = read_wakeup_threshold().unwrap();
match write_wakeup_threshold(1024) {
Ok(_) => (),
Err(err) => match err {
ProcError::PermissionDenied(_) => {
return;
}
err => panic!("test_write_wakeup_threshold error: {:?}", err),
},
}
let _ = write_wakeup_threshold(old_threshold);
}
#[test]
fn test_uuid_fns() {
let uuid = uuid().unwrap();
let boot_id = boot_id().unwrap();
println!("UUID: {}", uuid);
println!("boot UUID: {}", boot_id);
}
}