#![allow(clippy::unwrap_in_result)]
use std::net::IpAddr;
use std::time::Duration;
use crate::peer_set::initialize::recent_by_ip::RecentByIp;
#[test]
fn old_connection_attempts_are_pruned() {
const TEST_TIME_LIMIT: Duration = Duration::from_secs(5);
let _init_guard = zebra_test::init();
let ip = "127.0.0.1".parse().expect("should parse");
for max_connections_per_ip in [None, Some(3)] {
let mut recent_connections = RecentByIp::new(Some(TEST_TIME_LIMIT), max_connections_per_ip);
let limit = recent_connections.max_connections_per_ip;
for _ in 0..limit {
assert!(
!recent_connections.is_past_limit_or_add(ip),
"should admit connections up to the limit"
);
}
assert!(
recent_connections.is_past_limit_or_add(ip),
"should be past max_connections_per_ip limit"
);
let recorded_at = recent_connections.by_time[0].1;
for (_, time) in &mut recent_connections.by_time {
*time = recorded_at;
}
recent_connections.prune_by_time(recorded_at + TEST_TIME_LIMIT / 3);
assert_eq!(
recent_connections.by_ip.get(&ip),
Some(&limit),
"should still be at the limit after a third of the reconnection delay",
);
assert_eq!(recent_connections.by_time.len(), limit);
recent_connections.prune_by_time(recorded_at + TEST_TIME_LIMIT - Duration::from_nanos(1));
assert_eq!(
recent_connections.by_ip.get(&ip),
Some(&limit),
"should retain all attempts just before the expiry cutoff",
);
assert_eq!(recent_connections.by_time.len(), limit);
recent_connections.prune_by_time(recorded_at + TEST_TIME_LIMIT);
assert!(
recent_connections.by_ip.is_empty(),
"should prune counts at the cutoff",
);
assert!(
recent_connections.by_time.is_empty(),
"should prune attempts at the cutoff",
);
for _ in 0..limit {
assert!(
!recent_connections.is_past_limit_or_add(ip),
"should readmit connections after expiry"
);
}
assert!(
recent_connections.is_past_limit_or_add(ip),
"readmitted connections should enforce the same limit"
);
}
}
#[test]
fn ipv6_addresses_in_same_64_share_the_limit() {
const TEST_TIME_LIMIT: Duration = Duration::from_secs(5);
let _init_guard = zebra_test::init();
let mut recent_connections = RecentByIp::new(Some(TEST_TIME_LIMIT), Some(1));
let first: IpAddr = "2001:db8::a".parse().expect("valid IPv6 address");
let second: IpAddr = "2001:db8::b".parse().expect("valid IPv6 address");
assert!(
!recent_connections.is_past_limit_or_add(first),
"the first address in a /64 should be accepted"
);
assert!(
recent_connections.is_past_limit_or_add(second),
"a second address in the same /64 should be past the limit"
);
let other_subnet: IpAddr = "2001:db8:1::a".parse().expect("valid IPv6 address");
assert!(
!recent_connections.is_past_limit_or_add(other_subnet),
"an address in a different /64 should be accepted"
);
}
#[test]
fn ipv4_addresses_in_same_24_are_limited_separately() {
const TEST_TIME_LIMIT: Duration = Duration::from_secs(5);
let _init_guard = zebra_test::init();
let mut recent_connections = RecentByIp::new(Some(TEST_TIME_LIMIT), Some(1));
let first: IpAddr = "192.0.2.10".parse().expect("valid IPv4 address");
let second: IpAddr = "192.0.2.20".parse().expect("valid IPv4 address");
assert!(
!recent_connections.is_past_limit_or_add(first),
"the first address in a /24 should be accepted"
);
assert!(
!recent_connections.is_past_limit_or_add(second),
"a different address in the same /24 should also be accepted"
);
assert!(
recent_connections.is_past_limit_or_add(first),
"a repeat of the first address should be past the limit"
);
}
#[test]
fn ipv4_mapped_ipv6_shares_the_ipv4_limit() {
const TEST_TIME_LIMIT: Duration = Duration::from_secs(5);
let _init_guard = zebra_test::init();
let mut recent_connections = RecentByIp::new(Some(TEST_TIME_LIMIT), Some(1));
let ipv4: IpAddr = "192.0.2.10".parse().expect("valid IPv4 address");
let mapped: IpAddr = "::ffff:192.0.2.10"
.parse()
.expect("valid IPv4-mapped address");
assert!(
!recent_connections.is_past_limit_or_add(ipv4),
"the plain IPv4 address should be accepted"
);
assert!(
recent_connections.is_past_limit_or_add(mapped),
"the IPv4-mapped spelling of the same address should be past the limit"
);
}
#[test]
fn huge_time_limit_does_not_panic() {
let _init_guard = zebra_test::init();
let ip = "127.0.0.1".parse().expect("should parse");
let mut recent_connections = RecentByIp::new(Some(Duration::MAX), None);
assert!(!recent_connections.is_past_limit_or_add(ip));
assert_eq!(recent_connections.by_time.len(), 1);
assert_eq!(recent_connections.by_ip.get(&ip), Some(&1));
}
#[test]
fn huge_time_limit_still_enforces_per_ip_limit() {
let _init_guard = zebra_test::init();
let ip = "127.0.0.1".parse().expect("should parse");
for max_connections_per_ip in [None, Some(3)] {
let mut recent_connections = RecentByIp::new(Some(Duration::MAX), max_connections_per_ip);
let limit = recent_connections.max_connections_per_ip;
for _ in 0..limit {
assert!(!recent_connections.is_past_limit_or_add(ip));
}
assert!(recent_connections.is_past_limit_or_add(ip));
assert_eq!(recent_connections.by_time.len(), limit);
assert_eq!(recent_connections.by_ip.get(&ip), Some(&limit));
}
}