use icenet_core::{NodeId, ServiceId}; use icenet_guard::{Context, Guard, Limits, Verdict}; fn limits(window: u64) -> Limits { Limits { replay_window: window, rate_per_second: 0, ..Limits::default() } } fn context(peer: u64, sequence: u64) -> Context { Context { authenticated: true, peer: NodeId(peer), service: ServiceId(7), payload_bytes: 1, sequence, now_ms: 0, queue_depth: 0, } } #[test] fn duplicate_sequence_is_denied() { let mut guard = Guard::new([ServiceId(7)], limits(4)); assert_eq!(guard.check(context(1, 1)), Verdict::Permit); assert_eq!(guard.check(context(1, 1)), Verdict::Deny("replay")); } #[test] fn sequence_below_the_window_is_denied() { let mut guard = Guard::new([ServiceId(7)], limits(4)); assert_eq!(guard.check(context(1, 10)), Verdict::Permit); assert_eq!(guard.check(context(1, 6)), Verdict::Deny("replay")); assert_eq!(guard.check(context(1, 7)), Verdict::Permit); } #[test] fn higher_sequence_advances_and_prunes_the_window() { let mut guard = Guard::new([ServiceId(7)], limits(4)); assert_eq!(guard.check(context(1, 4)), Verdict::Permit); assert_eq!(guard.check(context(1, 5)), Verdict::Permit); assert_eq!(guard.check(context(1, 1)), Verdict::Deny("replay")); assert_eq!(guard.check(context(1, 2)), Verdict::Permit); } #[test] fn replay_state_is_per_peer() { let mut guard = Guard::new([ServiceId(7)], limits(4)); assert_eq!(guard.check(context(1, 5)), Verdict::Permit); assert_eq!(guard.check(context(2, 5)), Verdict::Permit); assert_eq!(guard.check(context(2, 5)), Verdict::Deny("replay")); }