Files
RustProxy/src/geoip/tests.rs
T

61 lines
1.8 KiB
Rust

mod tests {
use super::*;
#[test]
fn compact_ipv6_ranges_preserve_numeric_order() {
assert_eq!(std::mem::size_of::<V6Range>(), 34);
assert_eq!(std::mem::size_of::<V4Range>(), 12);
let ranges = [
V6Range {
start: 0u128.to_be_bytes(),
end: 127u128.to_be_bytes(),
country: *b"PL",
},
V6Range {
start: 256u128.to_be_bytes(),
end: 65535u128.to_be_bytes(),
country: *b"DE",
},
V6Range {
start: (1u128 << 120).to_be_bytes(),
end: u128::MAX.to_be_bytes(),
country: *b"US",
},
];
for (address, expected) in [
(0, Some(*b"PL")),
(127, Some(*b"PL")),
(128, None),
(255, None),
(256, Some(*b"DE")),
(65535, Some(*b"DE")),
(65536, None),
(1u128 << 120, Some(*b"US")),
(u128::MAX, Some(*b"US")),
] {
assert_eq!(find_v6(&ranges, address.to_be_bytes()), expected);
}
}
#[test]
fn range_lookup_handles_gaps() {
let directory = tempfile::tempdir().unwrap();
std::fs::write(
directory.path().join("user-country-ipv4.csv"),
"192.0.2.0,192.0.2.255,PL\n",
)
.unwrap();
std::fs::write(
directory.path().join("user-country-ipv6.csv"),
"2001:db8::,2001:db8::ffff,PL\n",
)
.unwrap();
let db = Database::load_format(directory.path(), DatabaseFormat::CsvPair).unwrap();
assert_eq!(
db.country("192.0.2.7".parse().unwrap()).as_deref(),
Some("PL")
);
assert!(db.country("198.51.100.1".parse().unwrap()).is_none());
}
}