guard/tests/http_protocol.rs
loki5512344 a8a3057719
feat(xdp): IPv6 UDP rate limit, working loader; English comments and docs
XDP behaviour:
- udp_rate_limit6 (LRU, full 128-bit source key) plus throttle_hit6, so
  udp_policy = "rate-limit" applies to native IPv6 as well.
- scripts/xdp_ipv6_smoke.sh: phase 4 patches .rodata before load and
  verifies policy=2 on a real kernel (3 pass / 3 rate-limited).

Loader (XDP could never load before this):
- .rodata is one struct rampart_config with frozen offsets enforced by
  _Static_assert; XdpGlobals is #[repr(C)] and the image is built with
  offset_of!. Before, a 96-byte image was written into a 76-byte map at
  offsets clang had reordered, so load() failed with EINVAL.
- patch_rodata fails loudly when the map size and image size diverge.
- build.rs tracks xdp/core and xdp/hooks, not just universal_filter.c.
- build_ringbuf looked for "events_map"; the map is "events_ringbuf".
- tests/xdp_object_maps.rs checks map names and .rodata size without root.

Docs and style:
- Code comments are English, JavaDoc/Rustdoc style, without emoji or
  banners (new project rule, TODO §0.8).
- README rewritten bilingual: Russian first, English below, nav buttons,
  compact TOC and copy-paste quick start.
- docs/README.md: documentation index and maintenance rules;
  configuration.md documents the whole [xdp] section and IPv4/IPv6
  coverage; testing.md covers the real-kernel smoke; stale links removed.
2026-11-15 23:50:52 +01:00

133 lines
4 KiB
Rust

//! Integration tests for the HTTP/1.1 protocol handler: request-head
//! accumulation across fragmented writes and policy rejection paths.
#![cfg(feature = "protocol-http")]
use rampart::config::HttpProtocolConfig;
use rampart::protocol::http::HttpProtocolHandler;
use rampart::protocol::{ProtocolHandler, ProtocolRegistry};
use tokio::io::AsyncWriteExt;
use tokio::net::{TcpListener, TcpStream};
const REQUEST: &[u8] = b"GET /index.html HTTP/1.1\r\nHost: example.com\r\n\r\n";
fn handler() -> HttpProtocolHandler {
HttpProtocolHandler::new(&HttpProtocolConfig::default(), &["127.0.0.1:9000".to_string()])
}
async fn socket_pair() -> (TcpStream, TcpStream) {
let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
let addr = listener.local_addr().expect("local addr");
let client = TcpStream::connect(addr).await.expect("connect");
let (server, _) = listener.accept().await.expect("accept");
(client, server)
}
#[tokio::test]
async fn valid_request_is_allowed() {
let (mut client, mut server) = socket_pair().await;
client.write_all(REQUEST).await.expect("write");
assert!(handler().handshake(&mut server).await.is_ok());
}
#[tokio::test]
async fn fragmented_head_is_accumulated_and_parsed() {
let (mut client, mut server) = socket_pair().await;
for chunk in REQUEST.chunks(7) {
client.write_all(chunk).await.expect("write chunk");
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
assert!(handler().handshake(&mut server).await.is_ok());
}
#[tokio::test]
async fn oversized_head_is_rejected() {
let config = HttpProtocolConfig {
max_header_bytes: 32,
..HttpProtocolConfig::default()
};
let (mut client, mut server) = socket_pair().await;
client
.write_all(b"GET / HTTP/1.1\r\nHost: a-very-long-host-name-value\r\n")
.await
.expect("write");
let err = handler_with(&config)
.handshake(&mut server)
.await
.expect_err("oversized head must be rejected");
assert!(err.to_string().contains("exceeds"));
}
#[tokio::test]
async fn unknown_method_is_rejected() {
let (mut client, mut server) = socket_pair().await;
client
.write_all(b"TRACE / HTTP/1.1\r\nHost: h\r\n\r\n")
.await
.expect("write");
let err = handler()
.handshake(&mut server)
.await
.expect_err("unknown method must be rejected");
assert!(err.to_string().contains("method"));
}
#[tokio::test]
async fn missing_host_is_rejected() {
let (mut client, mut server) = socket_pair().await;
client.write_all(b"GET / HTTP/1.1\r\n\r\n").await.expect("write");
let err = handler()
.handshake(&mut server)
.await
.expect_err("missing host must be rejected");
assert!(err.to_string().contains("host"));
}
#[tokio::test]
async fn blocked_path_is_rejected() {
let config = HttpProtocolConfig {
blocked_paths: vec!["/admin".to_string()],
..HttpProtocolConfig::default()
};
let (mut client, mut server) = socket_pair().await;
client
.write_all(b"GET /admin/secrets HTTP/1.1\r\nHost: h\r\n\r\n")
.await
.expect("write");
let err = handler_with(&config)
.handshake(&mut server)
.await
.expect_err("blocked path must be rejected");
assert!(err.to_string().contains("blocklisted"));
}
#[tokio::test]
async fn required_user_agent_missing_is_rejected() {
let config = HttpProtocolConfig {
require_user_agent: true,
..HttpProtocolConfig::default()
};
let (mut client, mut server) = socket_pair().await;
client
.write_all(b"GET / HTTP/1.1\r\nHost: h\r\n\r\n")
.await
.expect("write");
let err = handler_with(&config)
.handshake(&mut server)
.await
.expect_err("missing user-agent must be rejected");
assert!(err.to_string().contains("user-agent"));
}
#[tokio::test]
async fn registry_resolves_http_handler_when_feature_enabled() {
let mut registry = ProtocolRegistry::new();
registry.register(Box::new(handler()));
assert_eq!(registry.names(), vec!["http"]);
let resolved = registry.get("http").expect("http handler must resolve");
assert_eq!(resolved.name(), "http");
assert!(registry.primary().is_ok());
}
fn handler_with(config: &HttpProtocolConfig) -> HttpProtocolHandler {
HttpProtocolHandler::new(config, &["127.0.0.1:9000".to_string()])
}