ICE-NET/workspace/crates/icenet-guard/tests/rate.rs
loki5512344 2e61688eb3
Some checks failed
rust-ci / checks (push) Has been cancelled
Initial import: ICE-NET research docs and Rust workspace (v0.4)
- Rust workspace: 7 library crates, icenetd CLI, icenet-city (Slint GUI)
- Sim: fix false Delivered after a route cut, derive traffic class from
  payload size, per-link airtime slots (link capacity, queueing)
- Docs: single docs/STATUS.md for all figures, remove v0.2/egui leftovers,
  drop unimplemented seed claims, uncheck store-and-forward
- License: LGPL-3.0-or-later (LICENSE, LICENSES/GPL-3.0.txt)
2026-11-13 12:28:48 +01:00

52 lines
1.5 KiB
Rust

use icenet_core::{NodeId, ServiceId};
use icenet_guard::{Context, Guard, Limits, Verdict};
fn limits(rate: u64, burst: u64) -> Limits {
Limits {
rate_per_second: rate,
burst,
replay_window: 64,
..Limits::default()
}
}
fn context(peer: u64, sequence: u64, now_ms: u64) -> Context {
Context {
authenticated: true,
peer: NodeId(peer),
service: ServiceId(7),
payload_bytes: 1,
sequence,
now_ms,
queue_depth: 0,
}
}
#[test]
fn burst_is_allowed_then_requests_are_limited_until_refill() {
let mut guard = Guard::new([ServiceId(7)], limits(2, 2));
assert_eq!(guard.check(context(1, 1, 0)), Verdict::Permit);
assert_eq!(guard.check(context(1, 2, 0)), Verdict::Permit);
assert_eq!(guard.check(context(1, 3, 0)), Verdict::Deny("rate_limited"));
assert_eq!(guard.check(context(1, 4, 500)), Verdict::Permit);
assert_eq!(
guard.check(context(1, 5, 500)),
Verdict::Deny("rate_limited")
);
}
#[test]
fn rate_buckets_are_per_peer() {
let mut guard = Guard::new([ServiceId(7)], limits(1, 1));
assert_eq!(guard.check(context(1, 1, 0)), Verdict::Permit);
assert_eq!(guard.check(context(1, 2, 0)), Verdict::Deny("rate_limited"));
assert_eq!(guard.check(context(2, 1, 0)), Verdict::Permit);
}
#[test]
fn zero_rate_disables_limiting() {
let mut guard = Guard::new([ServiceId(7)], limits(0, 0));
for sequence in 0..50 {
assert_eq!(guard.check(context(1, sequence, 0)), Verdict::Permit);
}
}