test: move the last inline test modules to tests/, document new signals
- All remaining #[cfg(test)] modules left src/ (manager auth/heartbeat, protocol registry and HTTP parser, traffic hook/prefix/profile, TUI Prometheus); 41 tests moved into 10 integration suites with the same names and assertions, so `grep -rn '#[cfg(test)]' src/` is now empty. - runbook.md: degradation signals (overloaded connections, ClickHouse drops, XDP drops, generic mode, EMFILE backoff) and how to verify XDP was detached. - troubleshooting.md: accept backoff, orphaned XDP, password/JWT startup requirements, ClickHouse event loss. - TODO: review round recorded as fixed/deferred.
This commit is contained in:
parent
e456cb583f
commit
da87d2a11f
21 changed files with 600 additions and 521 deletions
|
|
@ -100,127 +100,3 @@ impl TrafficHook {
|
|||
tracing::warn!(%ip, reason, ttl_secs = self.ban_ttl_secs, "auto-ban issued by traffic intelligence");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
|
||||
fn ip(octets: [u8; 4]) -> IpAddr {
|
||||
IpAddr::V4(Ipv4Addr::from(octets))
|
||||
}
|
||||
|
||||
fn hook(rep: &Arc<IpReputation>, bl: &Arc<Blacklist>) -> TrafficHook {
|
||||
TrafficHook::new(
|
||||
Arc::clone(rep),
|
||||
Arc::clone(bl),
|
||||
DetectAutobanConfig {
|
||||
enabled: true,
|
||||
reputation_threshold: -50,
|
||||
},
|
||||
60,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cps_window_counts_and_resets() {
|
||||
let rep = Arc::new(IpReputation::new());
|
||||
let bl = Arc::new(Blacklist::new());
|
||||
let hook = hook(&rep, &bl);
|
||||
hook.on_accept();
|
||||
hook.on_accept();
|
||||
hook.on_accept();
|
||||
assert_eq!(hook.take_cps(), 3);
|
||||
assert_eq!(hook.take_cps(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reputation_fall_triggers_autoban() {
|
||||
let rep = Arc::new(IpReputation::new());
|
||||
let bl = Arc::new(Blacklist::new());
|
||||
let hook = hook(&rep, &bl);
|
||||
let target = ip([10, 1, 0, 1]);
|
||||
for _ in 0..6 {
|
||||
rep.record_bad(target);
|
||||
}
|
||||
assert_eq!(rep.score(target), -60);
|
||||
assert!(!bl.is_blocked(target));
|
||||
hook.on_connection_end(target);
|
||||
assert!(bl.is_blocked(target));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_autoban_above_threshold() {
|
||||
let rep = Arc::new(IpReputation::new());
|
||||
let bl = Arc::new(Blacklist::new());
|
||||
let hook = hook(&rep, &bl);
|
||||
let target = ip([10, 1, 0, 2]);
|
||||
for _ in 0..4 {
|
||||
rep.record_bad(target);
|
||||
}
|
||||
assert_eq!(rep.score(target), -40);
|
||||
hook.on_connection_end(target);
|
||||
assert!(!bl.is_blocked(target));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disabled_autoban_never_bans() {
|
||||
let rep = Arc::new(IpReputation::new());
|
||||
let bl = Arc::new(Blacklist::new());
|
||||
let hook = TrafficHook::new(
|
||||
Arc::clone(&rep),
|
||||
Arc::clone(&bl),
|
||||
DetectAutobanConfig {
|
||||
enabled: false,
|
||||
reputation_threshold: -50,
|
||||
},
|
||||
60,
|
||||
);
|
||||
let target = ip([10, 1, 0, 3]);
|
||||
for _ in 0..20 {
|
||||
rep.record_bad(target);
|
||||
}
|
||||
hook.on_connection_end(target);
|
||||
assert!(hook.ban_offenders() == 0);
|
||||
assert!(!bl.is_blocked(target));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ban_offenders_bans_only_below_threshold() {
|
||||
let rep = Arc::new(IpReputation::new());
|
||||
let bl = Arc::new(Blacklist::new());
|
||||
let hook = hook(&rep, &bl);
|
||||
let bad = ip([10, 2, 0, 1]);
|
||||
let ok = ip([10, 2, 0, 2]);
|
||||
for _ in 0..8 {
|
||||
rep.record_bad(bad);
|
||||
}
|
||||
rep.record_good(ok);
|
||||
assert_eq!(hook.ban_offenders(), 1);
|
||||
assert!(bl.is_blocked(bad));
|
||||
assert!(!bl.is_blocked(ok));
|
||||
assert_eq!(hook.ban_offenders(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_autoban_ttl_expires() {
|
||||
let rep = Arc::new(IpReputation::new());
|
||||
let bl = Arc::new(Blacklist::new());
|
||||
let hook = TrafficHook::new(
|
||||
Arc::clone(&rep),
|
||||
Arc::clone(&bl),
|
||||
DetectAutobanConfig {
|
||||
enabled: true,
|
||||
reputation_threshold: -50,
|
||||
},
|
||||
// Zero TTL as a time mock: the ban must be dead on arrival.
|
||||
0,
|
||||
);
|
||||
let target = ip([10, 3, 0, 1]);
|
||||
for _ in 0..20 {
|
||||
rep.record_bad(target);
|
||||
}
|
||||
hook.on_connection_end(target);
|
||||
assert!(!bl.is_blocked(target), "zero TTL must not produce an active ban");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -115,60 +115,3 @@ pub async fn send_webhook(url: String, alert: Alert) {
|
|||
tracing::warn!("webhook delivery failed: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_alert_display_with_ip() {
|
||||
let alert = Alert::new(
|
||||
AlertLevel::Critical,
|
||||
"possible attack".into(),
|
||||
Some(Ipv4Addr::new(192, 168, 1, 1)),
|
||||
100500.0,
|
||||
);
|
||||
let s = alert.to_string();
|
||||
assert!(s.contains("CRITICAL"));
|
||||
assert!(s.contains("192.168.1.1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_alert_display_without_ip() {
|
||||
let alert = Alert::new(AlertLevel::Info, "traffic spike".into(), None, 5000.0);
|
||||
let s = alert.to_string();
|
||||
assert!(s.contains("INFO"));
|
||||
assert!(s.contains("traffic spike"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_dispatcher_two_transitions_two_alerts() {
|
||||
let dispatcher = AlertDispatcher::new();
|
||||
assert_eq!(dispatcher.on_status(AttackStatus::Normal), None);
|
||||
assert_eq!(dispatcher.on_status(AttackStatus::Suspicious), None);
|
||||
let started = dispatcher.on_status(AttackStatus::UnderAttack);
|
||||
assert_eq!(dispatcher.on_status(AttackStatus::UnderAttack), None);
|
||||
assert_eq!(dispatcher.on_status(AttackStatus::UnderAttack), None);
|
||||
let ended = dispatcher.on_status(AttackStatus::Normal);
|
||||
assert_eq!(dispatcher.on_status(AttackStatus::Normal), None);
|
||||
assert!(started.is_some());
|
||||
assert!(ended.is_some());
|
||||
assert!(
|
||||
started.expect("started alert").message.contains("attack started")
|
||||
&& ended.expect("ended alert").message.contains("attack ended")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_dispatcher_suspicious_does_not_flap_alerts() {
|
||||
let dispatcher = AlertDispatcher::new();
|
||||
for status in [
|
||||
AttackStatus::Normal,
|
||||
AttackStatus::Suspicious,
|
||||
AttackStatus::Normal,
|
||||
AttackStatus::Suspicious,
|
||||
] {
|
||||
assert_eq!(dispatcher.on_status(status), None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,43 +43,3 @@ impl TrafficProfiler {
|
|||
(pps / base).min(10.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_profiler_record_and_baseline() {
|
||||
let mut p = TrafficProfiler::new();
|
||||
p.record(1000.0);
|
||||
p.record(1100.0);
|
||||
let base = p.baseline();
|
||||
assert!(base > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anomaly_score_normal() {
|
||||
let mut p = TrafficProfiler::new();
|
||||
for _ in 0..10 {
|
||||
p.record(100.0);
|
||||
}
|
||||
let score = p.anomaly_score(100.0);
|
||||
assert!(score < 2.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anomaly_score_capped() {
|
||||
let mut p = TrafficProfiler::new();
|
||||
for _ in 0..10 {
|
||||
p.record(1.0);
|
||||
}
|
||||
let score = p.anomaly_score(1_000_000.0);
|
||||
assert!((score - 10.0).abs() < 0.001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anomaly_score_zero_baseline() {
|
||||
let p = TrafficProfiler::new();
|
||||
assert_eq!(p.anomaly_score(100.0), 0.0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue