Files
ianitor/crates/edge/tests/share_throttle.rs

82 lines
2.7 KiB
Rust

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! The wrong-password throttle (QC fix round 1, M1): the id it keys on is unproven (anyone can
//! forge a token with any id), so it must not be possible to reset a real link's counter by
//! flooding it with made-up ids, and a full table refuses instead of forgetting.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::net::IpAddr;
use ianitor_core::share::ShareId;
use ianitor_edge::share::Throttle;
fn junk(n: u32) -> ShareId {
let mut id = [0u8; 16];
id[..4].copy_from_slice(&n.to_be_bytes());
id[15] = 1;
ShareId(id)
}
#[test]
fn junk_ids_from_the_same_network_cannot_flush_a_link_counter() {
let t = Throttle::default();
let victim = ShareId([7; 16]);
let ip: IpAddr = "192.0.2.10".parse().unwrap();
for _ in 0..5 {
t.failed(victim, Some(ip));
}
assert!(!t.allowed(victim, Some(ip)), "locked after 5 failures");
for n in 0u32..10_000 {
t.failed(junk(n), Some(ip));
}
assert!(
!t.allowed(victim, Some(ip)),
"10,000 junk failures reset the victim's counter"
);
}
#[test]
fn a_network_is_limited_across_all_links() {
let t = Throttle::default();
let ip: IpAddr = "192.0.2.10".parse().unwrap();
let neighbour: IpAddr = "192.0.2.77".parse().unwrap();
let elsewhere: IpAddr = "198.51.100.1".parse().unwrap();
let mut n = 0;
while t.allowed(junk(n), Some(ip)) {
t.failed(junk(n), Some(ip));
n += 1;
assert!(n < 1000, "no limit per network");
}
assert!(n <= 20, "{n} wrong passwords a minute from one network");
// The same /24 is limited for a link it never tried; other networks are not.
assert!(!t.allowed(ShareId([9; 16]), Some(neighbour)));
assert!(t.allowed(ShareId([9; 16]), Some(elsewhere)));
}
#[test]
fn a_full_table_refuses_new_visitors_instead_of_forgetting_old_ones() {
let t = Throttle::default();
let victim = ShareId([7; 16]);
let ip: IpAddr = "192.0.2.10".parse().unwrap();
for _ in 0..5 {
t.failed(victim, Some(ip));
}
// Fill the table from many networks, one failure each.
for n in 0u32..20_000 {
let other = IpAddr::from([10, (n >> 16) as u8, (n >> 8) as u8, n as u8]);
t.failed(junk(n), Some(other));
}
assert!(
!t.allowed(victim, Some(ip)),
"the victim's counter survived"
);
let newcomer = IpAddr::from([203, 0, 113, 5]);
assert!(
!t.allowed(ShareId([3; 16]), Some(newcomer)),
"full table: refuse, don't forget"
);
}