onionwire/tests/ratelimit.rs
Sirius DevOps e63203ff56
All checks were successful
ci / test (push) Successful in 4m11s
feat: incoming rate limit, identity backup, v0.2.0
App-level token bucket on rend accepts (30/60s, burst 10), panic-safe TUI restore, sqlite WAL + integrity_check fail-closed. Encrypted owbak1 identity backup/restore. README and threat model cover profile, XMR sidecar, backup, mixed-version frames.
2026-09-10 14:50:01 -04:00

28 lines
805 B
Rust

//! Incoming rend accepts: 30 / 60s, burst 10.
use std::time::{Duration, Instant};
use onionwire::ratelimit::TokenBucket;
#[test]
fn burst_allows_then_denies() {
let t0 = Instant::now();
let mut b = TokenBucket::new(30, Duration::from_secs(60), 10);
for _ in 0..10 {
assert!(b.try_acquire_at(t0), "burst of 10 must pass");
}
assert!(!b.try_acquire_at(t0), "11th in the burst must drop");
}
#[test]
fn refill_one_token_after_two_seconds() {
let t0 = Instant::now();
let mut b = TokenBucket::new(30, Duration::from_secs(60), 10);
for _ in 0..10 {
assert!(b.try_acquire_at(t0));
}
// 30 tokens / 60s = 0.5/s → 2s yields one token.
let t1 = t0 + Duration::from_secs(2);
assert!(b.try_acquire_at(t1));
assert!(!b.try_acquire_at(t1));
}