From ca7ce8c58e861d76b30eff2b53c2e256cfdb24fd Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Mon, 16 Nov 2026 13:08:34 +0100 Subject: [PATCH] test(edge): end-to-end connection path over loopback Drive a real TCP client through engine::listener and Gateway to a real upstream on 127.0.0.1, covering plain HTTP proxying, the PoW gate (coalesced nonce + request, wrong nonce dropped), the per-IP rate limit and the blacklist. Ports are ephemeral, every wait is bounded, and the accept loop is always stopped and awaited. --- tests/edge_e2e.rs | 153 +++++++++++++++++++++++ tests/edge_e2e/support.rs | 250 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 403 insertions(+) create mode 100644 tests/edge_e2e.rs create mode 100644 tests/edge_e2e/support.rs diff --git a/tests/edge_e2e.rs b/tests/edge_e2e.rs new file mode 100644 index 0000000..e7d59f1 --- /dev/null +++ b/tests/edge_e2e.rs @@ -0,0 +1,153 @@ +//! End-to-end tests of the real edge connection path over loopback. +//! +//! A TCP client is driven through the production accept loop (`engine::listener`) +//! and pipeline (`engine::tunnel::Gateway`) to a real upstream socket on +//! `127.0.0.1`: framing, the PoW gate, the admission filters and the relay are +//! exercised as one system, without root and without any external network. +#![cfg(feature = "protocol-http")] + +#[path = "edge_e2e/support.rs"] +mod support; + +use rampart::engine::challenge::solve; +use sha2::{Digest, Sha256}; +use std::net::Ipv4Addr; +use std::sync::atomic::Ordering; +use std::time::Duration; +use support::{ + IO_BUDGET, POW_DIFFICULTY, REQUEST, RESPONSE, exchange, read_to_close, rejected_reply, start_edge, start_upstream, +}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::TcpStream; + +/// Reads one newline-terminated line (the PoW challenge). +async fn read_line(stream: &mut TcpStream) -> anyhow::Result { + let mut line = Vec::new(); + for _ in 0..128 { + let mut byte = [0u8; 1]; + if stream.read(&mut byte).await? == 0 || byte[0] == b'\n' { + break; + } + line.push(byte[0]); + } + Ok(String::from_utf8_lossy(&line).trim().to_string()) +} + +fn hash_satisfies(challenge: &str, nonce: &str, difficulty: u8) -> bool { + let digest = Sha256::digest(format!("{challenge}{nonce}").as_bytes()); + hex::encode(digest) + .as_bytes() + .iter() + .take(difficulty as usize) + .all(|byte| byte.is_ascii_digit() && *byte <= b'3') +} + +/// Finds a nonce the gate must reject; the caller re-checks the result. +fn invalid_nonce(challenge: &str, difficulty: u8) -> String { + // At difficulty 2 about fifteen of sixteen candidates fail, so this stops + // within a handful of iterations. + for candidate in 0u64..1_000_000 { + let nonce = candidate.to_string(); + if !hash_satisfies(challenge, &nonce, difficulty) { + return nonce; + } + } + "0".to_string() +} + +#[tokio::test] +async fn plain_http_request_is_proxied_to_the_upstream() -> anyhow::Result<()> { + let (upstream, accepted, seen) = start_upstream().await; + let edge = start_edge(upstream, |_| {}).await?; + + let response = exchange(edge.addr, REQUEST).await?; + assert_eq!(response.as_slice(), RESPONSE); + assert_eq!(accepted.load(Ordering::SeqCst), 1); + assert_eq!(*seen.lock().expect("requests lock"), vec![REQUEST.to_vec()]); + + edge.stop().await +} + +#[tokio::test] +async fn pow_gate_accepts_a_solved_challenge_and_drops_a_wrong_nonce() -> anyhow::Result<()> { + let (upstream, accepted, seen) = start_upstream().await; + let edge = start_edge(upstream, |config| { + config.pow.enabled = true; + config.pow.difficulty = POW_DIFFICULTY; + }) + .await?; + + let mut client = TcpStream::connect(edge.addr).await?; + let challenge = tokio::time::timeout(IO_BUDGET, read_line(&mut client)).await??; + let nonce = solve(&challenge, POW_DIFFICULTY) + .ok_or_else(|| anyhow::anyhow!("no solution found for challenge {challenge}"))?; + // Coalesced write: the PoW reader must consume exactly the nonce line and + // leave the HTTP head buffered for the handshake that runs next. + let mut payload = format!("{nonce}\r\n").into_bytes(); + payload.extend_from_slice(REQUEST); + client.write_all(&payload).await?; + let response = tokio::time::timeout(IO_BUDGET, read_to_close(&mut client)).await??; + assert_eq!(response.as_slice(), RESPONSE); + assert_eq!(accepted.load(Ordering::SeqCst), 1); + + let mut hostile = TcpStream::connect(edge.addr).await?; + let challenge = tokio::time::timeout(IO_BUDGET, read_line(&mut hostile)).await??; + let wrong = invalid_nonce(&challenge, POW_DIFFICULTY); + assert!(!hash_satisfies(&challenge, &wrong, POW_DIFFICULTY)); + // Send the request after the wrong nonce: if the gate accepted it, the head + // would be buffered and proxied, and the upstream response would come back. + let mut payload = format!("{wrong}\r\n").into_bytes(); + payload.extend_from_slice(REQUEST); + let _ = hostile.write_all(&payload).await; + let reply = tokio::time::timeout(IO_BUDGET, read_to_close(&mut hostile)).await??; + assert!(reply.is_empty(), "a wrong nonce must be dropped, got {reply:?}"); + assert_eq!( + accepted.load(Ordering::SeqCst), + 1, + "a failed PoW must not reach the upstream" + ); + assert_eq!(seen.lock().expect("requests lock").len(), 1); + + edge.stop().await +} + +#[tokio::test] +async fn rate_limit_drops_the_second_connection_before_the_upstream() -> anyhow::Result<()> { + let (upstream, accepted, _seen) = start_upstream().await; + let edge = start_edge(upstream, |config| { + // One token, practically no refill: the second connection is over budget. + config.limits.rate_limit_pps = 0.001; + config.limits.rate_limit_burst = 1.0; + }) + .await?; + + let response = exchange(edge.addr, REQUEST).await?; + assert_eq!(response.as_slice(), RESPONSE); + assert_eq!(accepted.load(Ordering::SeqCst), 1); + + let reply = rejected_reply(edge.addr, REQUEST).await?; + assert!( + reply.is_empty(), + "rate-limited connection must not be proxied, got {reply:?}" + ); + // EOF above means the reject path already ran; it precedes the upstream + // stage, so a second accept cannot appear afterwards. + assert_eq!(accepted.load(Ordering::SeqCst), 1); + + edge.stop().await +} + +#[tokio::test] +async fn blacklisted_client_never_reaches_the_upstream() -> anyhow::Result<()> { + let (upstream, accepted, seen) = start_upstream().await; + let edge = start_edge(upstream, |_| {}).await?; + edge.blacklist + .add(Ipv4Addr::LOCALHOST.into(), Duration::from_secs(60), "edge_e2e"); + + let reply = rejected_reply(edge.addr, REQUEST).await?; + assert!(reply.is_empty(), "a banned client must be dropped, got {reply:?}"); + assert_eq!(accepted.load(Ordering::SeqCst), 0); + assert!(seen.lock().expect("requests lock").is_empty()); + + edge.stop().await +} diff --git a/tests/edge_e2e/support.rs b/tests/edge_e2e/support.rs new file mode 100644 index 0000000..a907a59 --- /dev/null +++ b/tests/edge_e2e/support.rs @@ -0,0 +1,250 @@ +//! Shared loopback harness for `tests/edge_e2e.rs`; cargo does not build it as +//! its own test binary because it lives in a subdirectory. + +use rampart::config::Config; +use rampart::engine::challenge::DifficultyAdjuster; +use rampart::engine::listener; +use rampart::engine::tunnel::Gateway; +use rampart::filter::blacklist::Blacklist; +use rampart::filter::rate_limit::RateLimiter; +use rampart::protocol::ProtocolRegistry; +use rampart::protocol::http::HttpProtocolHandler; +use rampart::traffic::hook::TrafficHook; +use rampart::traffic::reputation::IpReputation; +use std::collections::HashSet; +use std::io::ErrorKind; +use std::net::{Ipv4Addr, SocketAddr}; +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::TcpStream; +use tokio::sync::watch; +use tokio::task::JoinHandle; + +pub const REQUEST: &[u8] = b"GET / HTTP/1.1\r\nHost: edge\r\n\r\n"; +pub const RESPONSE: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nOK"; +pub const POW_DIFFICULTY: u8 = 2; +/// Upper bound of every I/O wait, so a regression fails instead of hanging. +pub const IO_BUDGET: Duration = Duration::from_secs(4); +const READY_BUDGET: Duration = Duration::from_secs(2); +const MAX_REPLY_BYTES: usize = 64 * 1024; + +// --- upstream ------------------------------------------------------------- + +/// Real upstream: returns its address, an accept counter and the heads it saw. +pub async fn start_upstream() -> (SocketAddr, Arc, Arc>>>) { + let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0)) + .await + .expect("bind upstream"); + let addr = listener.local_addr().expect("upstream address"); + let accepted = Arc::new(AtomicUsize::new(0)); + let requests = Arc::new(Mutex::new(Vec::new())); + let (count, seen) = (Arc::clone(&accepted), Arc::clone(&requests)); + // Detached: the per-test runtime drops this server when the test ends. + tokio::spawn(async move { + while let Ok((mut stream, _)) = listener.accept().await { + count.fetch_add(1, Ordering::SeqCst); + let seen = Arc::clone(&seen); + tokio::spawn(async move { + let head = read_head(&mut stream).await; + seen.lock().expect("requests lock").push(head); + let _ = stream.write_all(RESPONSE).await; + let _ = stream.shutdown().await; + }); + } + }); + (addr, accepted, requests) +} + +/// Reads until the request head ends (`\r\n\r\n`), the peer closes or a read fails. +async fn read_head(stream: &mut TcpStream) -> Vec { + let mut head = Vec::new(); + let mut buf = [0u8; 512]; + while head.len() < 4096 { + match stream.read(&mut buf).await { + Ok(0) | Err(_) => break, + Ok(n) => { + head.extend_from_slice(&buf[..n]); + if head.windows(4).any(|w| w == b"\r\n\r\n".as_slice()) { + break; + } + }, + } + } + head +} + +// --- edge ----------------------------------------------------------------- + +/// Production-shaped config from serde defaults, plus the per-test overrides. +fn test_config(port: u16, upstream: SocketAddr) -> Config { + let mut config = Config::default(); + config.bind.address = Ipv4Addr::LOCALHOST.to_string(); + config.bind.port = port; + config.backend.upstreams = vec![upstream.to_string()]; + config.workers.count = 1; + config.limits.handshake_timeout_secs = 2; + config.limits.relay_idle_timeout_secs = 5; + config.limits.max_connections_per_ip = 8; + config.protocol.http.max_header_bytes = 4096; + config.protocol.http.timeout_secs = 2; + config +} + +/// Running accept loop plus the shared state a test may pre-seed. +pub struct Edge { + pub addr: SocketAddr, + pub blacklist: Arc, + shutdown: watch::Sender, + task: JoinHandle>, +} + +impl Edge { + /// Stops the accept loop and waits for it, so no task outlives the test. + pub async fn stop(mut self) -> anyhow::Result<()> { + let _ = self.shutdown.send(true); + match tokio::time::timeout(IO_BUDGET, &mut self.task).await { + Ok(result) => result?, + Err(_) => anyhow::bail!("edge accept loop did not stop within {IO_BUDGET:?}"), + } + } +} + +impl Drop for Edge { + fn drop(&mut self) { + // Panic path: still wake the accept loop instead of leaking it. + let _ = self.shutdown.send(true); + } +} + +/// Starts `listener::run`, retrying once if the throwaway port was stolen. +/// +/// The port comes from binding `:0` and dropping the probe, so another process +/// can claim it before the edge binds; `run` then fails immediately and a fresh +/// port is used. The retry is bounded, never a loop. +pub async fn start_edge(upstream: SocketAddr, tune: impl Fn(&mut Config)) -> anyhow::Result { + let mut last_error = anyhow::anyhow!("edge listener did not bind"); + for _ in 0..2 { + let port = free_port()?; + let mut config = test_config(port, upstream); + tune(&mut config); + let config = Arc::new(config); + let (gateway, blacklist, hook) = build_gateway(&config); + let (shutdown, shutdown_rx) = watch::channel(false); + let task = tokio::spawn(listener::run(config, gateway, hook, shutdown_rx)); + let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); + if wait_until_bound(addr, &task).await { + return Ok(Edge { + addr, + blacklist, + shutdown, + task, + }); + } + let _ = shutdown.send(true); + if let Ok(Err(error)) = task.await { + last_error = error; + } + } + Err(last_error.context("edge listener failed to bind a loopback port after two attempts")) +} + +/// Binds `127.0.0.1:0`, reads the assigned port and releases it. +fn free_port() -> anyhow::Result { + let probe = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))?; + let port = probe.local_addr()?.port(); + drop(probe); + Ok(port) +} + +/// Waits until the edge owns `addr`. A second bind without `SO_REUSEPORT` fails +/// with `EADDRINUSE` exactly then, so no throwaway connection spends a +/// rate-limit token and pollutes the assertions. +async fn wait_until_bound(addr: SocketAddr, task: &JoinHandle>) -> bool { + let deadline = Instant::now() + READY_BUDGET; + while Instant::now() < deadline { + if task.is_finished() { + return false; + } + if std::net::TcpListener::bind(addr).is_err() { + return true; + } + tokio::time::sleep(Duration::from_millis(2)).await; + } + false +} + +/// Assembles the edge state as `app::runtime` does, minus XDP, Redis and the +/// ClickHouse writer (all optional, all `None` here). +fn build_gateway(config: &Arc) -> (Arc, Arc, Arc) { + let blacklist = Arc::new(Blacklist::new()); + let reputation = Arc::new(IpReputation::new()); + let mut registry = ProtocolRegistry::new(); + registry.register(Box::new(HttpProtocolHandler::new( + &config.protocol.http, + &config.backend.upstreams, + ))); + let gateway = Gateway::new( + Arc::clone(config), + Arc::new(RateLimiter::new( + config.limits.rate_limit_pps, + config.limits.rate_limit_burst, + )), + Arc::clone(&blacklist), + Arc::new(Mutex::new(DifficultyAdjuster::with_floor(config.pow.difficulty))), + Arc::new(HashSet::new()), + Arc::clone(&reputation), + None, + None, + Arc::new(AtomicU64::new(0)), + Arc::new(registry), + None, + ); + // Auto-ban stays at its default (disabled), so a drop cannot leak a ban into + // a later assertion. + let hook = Arc::new(TrafficHook::new( + reputation, + Arc::clone(&blacklist), + config.detect.autoban.clone(), + config.ban.ban_duration_secs, + )); + (Arc::new(gateway), blacklist, hook) +} + +// --- client --------------------------------------------------------------- + +/// Reads until the peer closes; a reset also means "closed, no extra data". +pub async fn read_to_close(stream: &mut TcpStream) -> anyhow::Result> { + let mut out = Vec::new(); + let mut buf = [0u8; 512]; + loop { + match stream.read(&mut buf).await { + Ok(0) => return Ok(out), + Ok(n) => { + out.extend_from_slice(&buf[..n]); + if out.len() > MAX_REPLY_BYTES { + anyhow::bail!("peer kept sending past {MAX_REPLY_BYTES} bytes"); + } + }, + Err(error) if error.kind() == ErrorKind::ConnectionReset => return Ok(out), + Err(error) => return Err(error.into()), + } + } +} + +/// Sends `payload` on a fresh connection and returns everything until the close. +pub async fn exchange(addr: SocketAddr, payload: &[u8]) -> anyhow::Result> { + let mut client = TcpStream::connect(addr).await?; + client.write_all(payload).await?; + tokio::time::timeout(IO_BUDGET, read_to_close(&mut client)).await? +} + +/// Best-effort request on a fresh connection: a rejected peer may already be +/// gone, so write errors are ignored. The reply is what the caller asserts on — +/// a gate that let the request through would return the upstream response. +pub async fn rejected_reply(addr: SocketAddr, payload: &[u8]) -> anyhow::Result> { + let mut client = TcpStream::connect(addr).await?; + let _ = client.write_all(payload).await; + tokio::time::timeout(IO_BUDGET, read_to_close(&mut client)).await? +}