use thermogram::*;
use tempfile::tempdir;
#[test]
fn test_detect_hash_tampering() {
let mut thermo = Thermogram::new("test", PlasticityRule::stdp_like());
let delta = Delta::create("key1", b"value1".to_vec(), "source");
thermo.apply_delta(delta.clone()).unwrap();
let mut tampered = thermo.clone();
tampered.dirty_chain.deltas[0].hash = "tampered_hash".to_string();
assert!(tampered.dirty_chain.verify().is_err());
}
#[test]
fn test_reject_invalid_chain_link() {
let mut thermo = Thermogram::new("test", PlasticityRule::stdp_like());
let delta1 = Delta::create("key1", b"value1".to_vec(), "source");
thermo.apply_delta(delta1).unwrap();
let delta2 = Delta::update(
"key1",
b"value2".to_vec(),
"source",
0.8,
Some("wrong_hash".to_string()),
);
assert!(thermo.apply_delta(delta2).is_err());
}
#[test]
fn test_corrupted_file_load() {
let dir = tempdir().unwrap();
let path = dir.path().join("corrupted.thermo");
std::fs::write(&path, b"{ invalid json !!!").unwrap();
let result = Thermogram::load(&path);
assert!(result.is_err());
}
#[test]
fn test_partial_file_corruption() {
let dir = tempdir().unwrap();
let path = dir.path().join("partial.thermo");
let mut thermo = Thermogram::new("test", PlasticityRule::stdp_like());
let delta = Delta::create("key1", b"value1".to_vec(), "source");
thermo.apply_delta(delta).unwrap();
thermo.save(&path).unwrap();
let data = std::fs::read(&path).unwrap();
let truncated = &data[..data.len().saturating_sub(100)];
std::fs::write(&path, truncated).unwrap();
let result = Thermogram::load(&path);
assert!(result.is_err());
}
#[test]
fn test_neuromod_bounds_violation() {
let mut state = NeuromodState::baseline();
state.reward(10.0);
assert!(state.dopamine >= 0.0 && state.dopamine <= 1.0);
}
#[test]
fn test_negative_neuromod_values() {
let mut state = NeuromodState::baseline();
state.stress(10.0);
assert!(state.serotonin >= 0.0 && state.serotonin <= 1.0);
assert!(state.norepinephrine >= 0.0 && state.norepinephrine <= 1.0);
}
#[test]
fn test_empty_key_rejection() {
let delta = Delta::create("", b"value".to_vec(), "source");
assert!(!delta.key.is_empty() || delta.key.is_empty());
}
#[test]
fn test_huge_value_delta() {
let mut thermo = Thermogram::new("test", PlasticityRule::stdp_like());
let huge_value = vec![0u8; 10_000_000];
let delta = Delta::create("huge", huge_value, "source");
let result = thermo.apply_delta(delta);
assert!(result.is_ok() || result.is_err()); }
#[test]
fn test_consolidation_with_all_weak_entries() {
let mut thermo = Thermogram::new("test", PlasticityRule::stdp_like());
for i in 0..10 {
let delta = Delta::update(
format!("weak_{}", i),
b"value".to_vec(),
"source",
0.01, thermo.dirty_chain.head_hash.clone(),
);
thermo.apply_delta(delta).unwrap();
}
let result = thermo.consolidate().unwrap();
assert_eq!(result.entries_pruned, 10);
assert_eq!(thermo.clean_state.len(), 0);
}
#[test]
fn test_hash_collision_resistance() {
let delta1 = Delta::create("key1", b"value1".to_vec(), "source");
let delta2 = Delta::create("key2", b"value2".to_vec(), "source");
assert_ne!(delta1.hash, delta2.hash);
}
#[test]
fn test_replay_attack_prevention() {
let mut thermo = Thermogram::new("test", PlasticityRule::stdp_like());
let delta = Delta::create("key1", b"value1".to_vec(), "source");
thermo.apply_delta(delta.clone()).unwrap();
let result = thermo.apply_delta(delta);
assert!(result.is_err());
}
#[test]
fn test_concurrent_write_detection() {
let mut thermo = Thermogram::new("test", PlasticityRule::stdp_like());
let delta1 = Delta::create("key1", b"value1".to_vec(), "source");
let head1 = delta1.hash.clone();
thermo.apply_delta(delta1).unwrap();
let delta2 = Delta::update("key1", b"value2".to_vec(), "source", 0.8, Some(head1));
assert!(thermo.apply_delta(delta2).is_ok());
}
#[test]
fn test_snn_nan_protection() {
let config = EmbeddedSNNConfig::default();
let mut snn = EmbeddedSNN::new(config);
let input = vec![f32::NAN; 2048];
let neuromod = NeuromodState::baseline();
let result = snn.process(&input, &neuromod);
match result {
Ok(deltas) => {
for delta in deltas {
assert!(!delta.value.is_empty());
}
}
Err(_) => {
}
}
}
#[test]
fn test_snn_infinity_protection() {
let config = EmbeddedSNNConfig::default();
let mut snn = EmbeddedSNN::new(config);
let input = vec![f32::INFINITY; 2048];
let neuromod = NeuromodState::baseline();
let result = snn.process(&input, &neuromod);
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_memory_bomb_protection() {
let mut thermo = Thermogram::new("test", PlasticityRule::stdp_like());
for i in 0..1000 {
let delta = Delta::create(
format!("key_{}", i),
b"value".to_vec(),
"source",
);
if let Err(_) = thermo.apply_delta(delta) {
break;
}
}
assert!(thermo.read("key_0").is_ok());
}