Merge branch 'worktree-agent-ae36f7883b9775990'
# Conflicts: # tests/manager_api.rs
This commit is contained in:
commit
febc49f3eb
12 changed files with 403 additions and 78 deletions
|
|
@ -114,6 +114,9 @@ async fn blacklist_rejects_invalid_targets() {
|
|||
"1.2.3.4/33",
|
||||
"::1/129",
|
||||
"1.2.3.4/+8",
|
||||
"0.0.0.0/0",
|
||||
"::/0",
|
||||
"1.2.3.4/32",
|
||||
] {
|
||||
let body = format!(r#"{{"target":"{target}","type":"ip","reason":"manual_ban"}}"#);
|
||||
let response = status(
|
||||
|
|
|
|||
115
tests/xdp_abi_layout.rs
Normal file
115
tests/xdp_abi_layout.rs
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
//! ABI layout of every Rust type that mirrors a BPF map key/value, ringbuf record or
|
||||
//! `.rodata` image.
|
||||
//!
|
||||
//! The expected numbers are parsed by hand from the C definitions in `xdp/core/`
|
||||
//! (natural alignment rules of clang/BPF: every field aligned to its own size, struct
|
||||
//! size rounded up to its largest member). A failure here means one side of the ABI
|
||||
//! moved without the other. Value-level checks live in `xdp_globals.rs`,
|
||||
//! `xdp_ban_keys.rs` and `xdp_whitelist_keys.rs`.
|
||||
|
||||
use std::mem::{align_of, offset_of, size_of};
|
||||
|
||||
use rampart::traffic::prefix::{PrefixKey, PrefixStatsVal};
|
||||
|
||||
#[test]
|
||||
fn prefix_key_mirrors_struct_prefix_key() {
|
||||
// prefix_stats.h: struct prefix_key { __u8 family; __u8 pad[7]; __u8 addr[16]; }
|
||||
assert_eq!(size_of::<PrefixKey>(), 24);
|
||||
assert_eq!(align_of::<PrefixKey>(), 1);
|
||||
assert_eq!(offset_of!(PrefixKey, family), 0);
|
||||
assert_eq!(offset_of!(PrefixKey, pad), 1);
|
||||
assert_eq!(offset_of!(PrefixKey, addr), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefix_key_byte_codec_matches_the_c_offsets() {
|
||||
let key = PrefixKey {
|
||||
family: 6,
|
||||
pad: [0; 7],
|
||||
addr: [7; 16],
|
||||
};
|
||||
let bytes = key.to_bytes();
|
||||
assert_eq!(bytes[0], 6, "family @0");
|
||||
assert_eq!(bytes[8..24], [7; 16], "addr @8");
|
||||
assert_eq!(PrefixKey::from_bytes(bytes), key);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefix_stats_val_mirrors_struct_prefix_stats_val() {
|
||||
// prefix_stats.h: struct prefix_stats_val { __u64 syn_count, pkt_count, last_seen_ns, source_bits; }
|
||||
assert_eq!(size_of::<PrefixStatsVal>(), 32);
|
||||
assert_eq!(align_of::<PrefixStatsVal>(), 8);
|
||||
assert_eq!(offset_of!(PrefixStatsVal, syn_count), 0);
|
||||
assert_eq!(offset_of!(PrefixStatsVal, pkt_count), 8);
|
||||
assert_eq!(offset_of!(PrefixStatsVal, last_seen_ns), 16);
|
||||
assert_eq!(offset_of!(PrefixStatsVal, source_bits), 24);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefix_stats_val_byte_codec_matches_the_c_offsets() {
|
||||
let val = PrefixStatsVal {
|
||||
syn_count: 1,
|
||||
pkt_count: 2,
|
||||
last_seen_ns: 3,
|
||||
source_bits: 4,
|
||||
};
|
||||
let bytes = val.to_bytes();
|
||||
for (i, expected) in [1u64, 2, 3, 4].into_iter().enumerate() {
|
||||
let at = i * 8;
|
||||
assert_eq!(
|
||||
u64::from_le_bytes(bytes[at..at + 8].try_into().expect("8 bytes")),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "xdp")]
|
||||
mod xdp {
|
||||
use super::{align_of, offset_of, size_of};
|
||||
use rampart::xdp::{RODATA_SIZE, XDP_EVENT_LEN, XdpGlobals, blacklist_key, blacklist6_key};
|
||||
|
||||
#[test]
|
||||
fn xdp_globals_mirrors_struct_rampart_config() {
|
||||
// config.h: struct rampart_config, sizeof == 96 (its own _Static_assert).
|
||||
assert_eq!(size_of::<XdpGlobals>(), 96);
|
||||
assert_eq!(align_of::<XdpGlobals>(), 8);
|
||||
assert_eq!(RODATA_SIZE, 96);
|
||||
let expected = [
|
||||
(offset_of!(XdpGlobals, start_port), 0),
|
||||
(offset_of!(XdpGlobals, end_port), 2),
|
||||
(offset_of!(XdpGlobals, syn_hit_count), 4),
|
||||
(offset_of!(XdpGlobals, syn_window_ns), 8),
|
||||
(offset_of!(XdpGlobals, syn_ban_duration_ns), 16),
|
||||
(offset_of!(XdpGlobals, conntrack_idle_ns), 24),
|
||||
(offset_of!(XdpGlobals, player_idle_ns), 32),
|
||||
(offset_of!(XdpGlobals, ban_duration_ns), 40),
|
||||
(offset_of!(XdpGlobals, max_out_of_order), 48),
|
||||
(offset_of!(XdpGlobals, udp_policy), 49),
|
||||
(offset_of!(XdpGlobals, udp_hit_count), 52),
|
||||
(offset_of!(XdpGlobals, udp_window_ns), 56),
|
||||
(offset_of!(XdpGlobals, feature_syn_throttle), 64),
|
||||
(offset_of!(XdpGlobals, feature_events), 65),
|
||||
(offset_of!(XdpGlobals, syn_challenge_enabled), 66),
|
||||
(offset_of!(XdpGlobals, challenge_secret), 72),
|
||||
(offset_of!(XdpGlobals, challenge_timeout_ms), 80),
|
||||
(offset_of!(XdpGlobals, challenge_verified_ttl_ns), 88),
|
||||
];
|
||||
for (rust, c) in expected {
|
||||
assert_eq!(rust, c);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lpm_keys_have_the_size_of_struct_lpm_key_and_key6() {
|
||||
// common.h: struct lpm_key { __u32 prefixlen; __u32 ip; } -> 8
|
||||
assert_eq!(size_of_val(&blacklist_key(24, &[0; 4])), 8);
|
||||
// common.h: struct lpm_key6 { __u32 prefixlen; __u8 addr[16]; } -> 20, align 4
|
||||
assert_eq!(size_of_val(&blacklist6_key(64, &[0; 16])), 20);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xdp_event_len_matches_struct_xdp_event() {
|
||||
// common.h: struct xdp_event { __u32 type, src_ip, metadata; (4 pad) __u64 timestamp; } -> 24
|
||||
assert_eq!(XDP_EVENT_LEN, 24);
|
||||
}
|
||||
}
|
||||
95
tests/xdp_cidr_validation.rs
Normal file
95
tests/xdp_cidr_validation.rs
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
//! Prefix/CIDR validation for every ban and whitelist entry point.
|
||||
//!
|
||||
//! A `/0` would blackhole (or whitelist) a whole address family, anything above the
|
||||
//! address width is nonsense, and the mask math must never shift by the full width.
|
||||
#![cfg(feature = "xdp")]
|
||||
|
||||
use std::net::IpAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
use rampart::filter::blacklist::Blacklist;
|
||||
use rampart::xdp::{canonical_cidr, normalize_cidr};
|
||||
|
||||
fn ip(s: &str) -> IpAddr {
|
||||
s.parse().expect("test address")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_prefix_is_rejected_for_both_families() {
|
||||
assert!(normalize_cidr(ip("10.0.0.1"), 0).is_err());
|
||||
assert!(normalize_cidr(ip("2001:db8::1"), 0).is_err());
|
||||
assert!(canonical_cidr(ip("::ffff:10.0.0.1"), 0).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefix_above_the_address_width_is_rejected() {
|
||||
assert!(normalize_cidr(ip("10.0.0.1"), 33).is_err());
|
||||
assert!(normalize_cidr(ip("10.0.0.1"), 255).is_err());
|
||||
assert!(normalize_cidr(ip("2001:db8::1"), 129).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_width_prefixes_do_not_overflow_the_mask() {
|
||||
assert_eq!(normalize_cidr(ip("10.1.2.3"), 32).expect("/32"), ip("10.1.2.3"));
|
||||
assert_eq!(normalize_cidr(ip("2001:db8::1"), 128).expect("/128"), ip("2001:db8::1"));
|
||||
assert_eq!(normalize_cidr(ip("10.1.2.3"), 1).expect("/1"), ip("0.0.0.0"));
|
||||
assert_eq!(normalize_cidr(ip("ffff::1"), 1).expect("/1"), ip("8000::"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ipv6_masks_partial_bytes() {
|
||||
assert_eq!(
|
||||
normalize_cidr(ip("2001:db8:ffff::1"), 36).expect("/36"),
|
||||
ip("2001:db8:f000::")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mapped_ipv6_folds_into_ipv4_with_the_length_shifted() {
|
||||
let (net, len) = canonical_cidr(ip("::ffff:10.1.2.3"), 120).expect("mapped /120");
|
||||
assert_eq!((net, len), (ip("10.1.2.0"), 24));
|
||||
let (net, len) = canonical_cidr(ip("::ffff:10.1.2.3"), 128).expect("mapped /128");
|
||||
assert_eq!((net, len), (ip("10.1.2.3"), 32));
|
||||
// The empty IPv4 prefix is /0 in disguise.
|
||||
assert!(canonical_cidr(ip("::ffff:10.1.2.3"), 96).is_err());
|
||||
// Wider than IPv4 space: stays IPv6.
|
||||
let (net, len) = canonical_cidr(ip("::ffff:10.1.2.3"), 64).expect("mapped /64");
|
||||
assert_eq!((net, len), (ip("::"), 64));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn userspace_blacklist_rejects_zero_and_oversized_prefixes() {
|
||||
let blacklist = Blacklist::new();
|
||||
for (addr, len) in [
|
||||
("10.0.0.1", 0),
|
||||
("10.0.0.1", 33),
|
||||
("2001:db8::1", 0),
|
||||
("2001:db8::1", 129),
|
||||
("::ffff:10.0.0.1", 0),
|
||||
("::ffff:10.0.0.1", 96),
|
||||
] {
|
||||
blacklist.add_cidr(ip(addr), len, Duration::from_secs(60), "test");
|
||||
}
|
||||
assert!(blacklist.is_empty(), "no invalid CIDR may be installed");
|
||||
assert!(!blacklist.is_blocked(ip("10.0.0.1")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn userspace_blacklist_accepts_full_width_prefixes() {
|
||||
let blacklist = Blacklist::new();
|
||||
blacklist.add_cidr(ip("10.0.0.7"), 32, Duration::from_secs(60), "test");
|
||||
blacklist.add_cidr(ip("2001:db8::7"), 128, Duration::from_secs(60), "test");
|
||||
assert!(blacklist.is_blocked(ip("10.0.0.7")));
|
||||
assert!(!blacklist.is_blocked(ip("10.0.0.8")));
|
||||
assert!(blacklist.is_blocked(ip("2001:db8::7")));
|
||||
assert!(!blacklist.is_blocked(ip("2001:db8::8")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn userspace_blacklist_folds_mapped_prefixes_like_the_xdp_loader() {
|
||||
let blacklist = Blacklist::new();
|
||||
blacklist.add_cidr(ip("::ffff:10.9.8.7"), 120, Duration::from_secs(60), "test");
|
||||
assert!(blacklist.is_blocked(ip("10.9.8.200")), "/120 mapped == v4 /24");
|
||||
assert!(!blacklist.is_blocked(ip("10.9.9.1")));
|
||||
assert!(blacklist.is_blocked(ip("::ffff:10.9.8.1")));
|
||||
}
|
||||
34
tests/xdp_key_byte_order.rs
Normal file
34
tests/xdp_key_byte_order.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
//! IPv6 LPM keys carry the address in network order right after the prefix length.
|
||||
#![cfg(feature = "xdp")]
|
||||
|
||||
use std::net::{IpAddr, Ipv6Addr};
|
||||
|
||||
use rampart::xdp::{blacklist6_key, normalize_cidr, whitelist6_key};
|
||||
|
||||
#[test]
|
||||
fn ipv6_keys_hold_the_network_order_bytes_of_the_address() {
|
||||
let addr: Ipv6Addr = "2001:db8:1234:5678:9abc:def0:1122:3344".parse().expect("ipv6");
|
||||
let wire = addr.octets();
|
||||
assert_eq!(
|
||||
wire[0..4],
|
||||
[0x20, 0x01, 0x0d, 0xb8],
|
||||
"sanity: octets() is network order"
|
||||
);
|
||||
|
||||
for key in [blacklist6_key(128, &wire), whitelist6_key(128, &wire)] {
|
||||
assert_eq!(key[0..4], 128u32.to_le_bytes(), "prefixlen is a host-order (LE) u32");
|
||||
assert_eq!(key[4..20], wire, "address bytes are not byte-swapped");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn masked_cidr_bytes_feed_the_key_in_network_order() {
|
||||
let addr: Ipv6Addr = "2001:db8:aaaa:bbbb:cccc:dddd:eeee:ffff".parse().expect("ipv6");
|
||||
let net = normalize_cidr(IpAddr::V6(addr), 48).expect("/48");
|
||||
let IpAddr::V6(net) = net else {
|
||||
unreachable!("a v6 prefix stays v6");
|
||||
};
|
||||
let key = blacklist6_key(48, &net.octets());
|
||||
assert_eq!(key[4..10], [0x20, 0x01, 0x0d, 0xb8, 0xaa, 0xaa]);
|
||||
assert_eq!(key[10..20], [0u8; 10], "host bits cleared");
|
||||
}
|
||||
32
tests/xdp_percpu_sum.rs
Normal file
32
tests/xdp_percpu_sum.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
//! Per-CPU counter aggregation: libbpf returns one buffer per possible CPU.
|
||||
|
||||
use rampart::xdp::sum_percpu_u64;
|
||||
|
||||
fn slot(v: u64) -> Vec<u8> {
|
||||
v.to_le_bytes().to_vec()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sums_every_slot_regardless_of_cpu_count() {
|
||||
for cpus in [1u64, 2, 7, 64, 512] {
|
||||
let slots: Vec<Vec<u8>> = (1..=cpus).map(slot).collect();
|
||||
assert_eq!(sum_percpu_u64(&slots), cpus * (cpus + 1) / 2, "{cpus} CPUs");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offline_but_possible_cpus_contribute_zero() {
|
||||
let slots = vec![slot(5), slot(0), slot(0), slot(7)];
|
||||
assert_eq!(sum_percpu_u64(&slots), 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_and_short_buffers_are_harmless() {
|
||||
assert_eq!(sum_percpu_u64(&[]), 0);
|
||||
assert_eq!(sum_percpu_u64(&[vec![1, 2, 3]]), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_counters_wrap_instead_of_panicking() {
|
||||
assert_eq!(sum_percpu_u64(&[slot(u64::MAX), slot(2)]), 1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue