use crate::rng::SimRng;
#[cfg(feature = "canary-skip-fsync")]
pub fn should_skip_fsync(rng: &mut SimRng) -> bool {
rng.next_bool_with_probability(0.001) }
#[cfg(not(feature = "canary-skip-fsync"))]
pub fn should_skip_fsync(_rng: &mut SimRng) -> bool {
false
}
#[cfg(feature = "canary-wrong-hash")]
pub fn corrupt_hash(hash: &[u8; 32]) -> [u8; 32] {
let mut corrupted = *hash;
corrupted[0] ^= 0xFF; corrupted
}
#[cfg(not(feature = "canary-wrong-hash"))]
pub fn corrupt_hash(hash: &[u8; 32]) -> [u8; 32] {
*hash
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(feature = "canary-skip-fsync")]
fn test_skip_fsync_canary_enabled() {
let mut rng = SimRng::new(12345);
let mut skipped = false;
for _ in 0..10_000 {
if should_skip_fsync(&mut rng) {
skipped = true;
break;
}
}
assert!(skipped, "canary-skip-fsync should trigger");
}
#[test]
#[cfg(not(feature = "canary-skip-fsync"))]
fn test_skip_fsync_canary_disabled() {
let mut rng = SimRng::new(12345);
for _ in 0..1_000 {
assert!(!should_skip_fsync(&mut rng));
}
}
#[test]
#[cfg(feature = "canary-wrong-hash")]
fn test_wrong_hash_canary_enabled() {
let hash = [0u8; 32];
let corrupted = corrupt_hash(&hash);
assert_ne!(hash, corrupted, "hash should be corrupted");
}
#[test]
#[cfg(not(feature = "canary-wrong-hash"))]
fn test_wrong_hash_canary_disabled() {
let hash = [0u8; 32];
let not_corrupted = corrupt_hash(&hash);
assert_eq!(hash, not_corrupted, "hash should not be corrupted");
}
}