From 70af820838e32f76b87a1eb24333b1c3f0b71fa2 Mon Sep 17 00:00:00 2001 From: ari Date: Tue, 20 Oct 2026 16:22:56 -0400 Subject: [PATCH] H: prove the SSE test server identity Require the child's own listening line for the probed port before it is signalled; a 200 from /healthz proves liveness only, since another slinuxm answers it too. Retry a lost bind race instead of signalling a dead child, and add a squatter that answers 200. --- tests/integration.rs | 207 ++++++++++++++++++++++++++++++++----------- 1 file changed, 156 insertions(+), 51 deletions(-) diff --git a/tests/integration.rs b/tests/integration.rs index 7be844e..661394b 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -390,9 +390,9 @@ fn nonloopback_without_auth_exits_nonzero() { #[test] fn sigterm_with_open_sse_client_exits_promptly() { - // The port is probed before the child binds it. If something else takes - // it in between, our child exits and the socket belongs to a stranger, so - // a fresh port is tried instead of signalling the wrong process. + // The port is probed before the child binds it, so a stranger can take it + // first. Identity, not liveness, decides whether to signal: see + // `spawn_listening`. let mut started = false; for _ in 0..3 { if sse_stream_then_sigterm() { @@ -403,9 +403,14 @@ fn sigterm_with_open_sse_client_exits_promptly() { assert!(started, "server did not stay up on any of three ports"); } +const STARTUP_BOUND: Duration = Duration::from_secs(10); + /// Signals the child and waits for a successful exit. Anything still running /// after `bound` is killed and the test fails with `panic_msg`. fn sigterm_and_wait(child: &mut std::process::Child, bound: Duration, panic_msg: &str) { + if let Some(st) = child.try_wait().unwrap() { + panic!("server exited before SIGTERM: {st}"); + } let pid = child.id().to_string(); assert!(std::process::Command::new("kill") .args(["-TERM", &pid]) @@ -426,49 +431,114 @@ fn sigterm_and_wait(child: &mut std::process::Child, bound: Duration, panic_msg: } } -/// One attempt on a fresh port. `false` means the child exited before it -/// served, which only happens when the probed port was taken by someone else. -fn sse_stream_then_sigterm() -> bool { - let port = { - let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); - l.local_addr().unwrap().port() - }; - sse_stream_then_sigterm_on(port) +fn free_port() -> u16 { + let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + l.local_addr().unwrap().port() } -fn sse_stream_then_sigterm_on(port: u16) -> bool { - use std::io::{Read, Write}; - use std::net::TcpStream as StdStream; - +/// A child whose own startup line says it listens on `127.0.0.1:port`, or +/// `None` when the child exited before binding, which happens when another +/// process holds the probed port. +/// +/// A 200 from `/healthz` proves nothing: another `slinuxm` (or any squatter) +/// answers it too. The child's stderr is the only statement about which socket +/// is its own; once it listens there, nothing else can hold the port, so a +/// later SIGTERM cannot reach the wrong process. +fn spawn_listening(port: u16) -> Option { + use std::io::{BufRead, BufReader}; let mut child = std::process::Command::new(env!("CARGO_BIN_EXE_slinuxm")) .args(["serve", "--auth-enabled", "false", "--bind", "127.0.0.1"]) .args(["--port", &port.to_string()]) .env_clear() - .stderr(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) .spawn() .unwrap(); - // Only a server that answers proves the socket is ours; a listener that - // answers nothing is a squatter, and the child that lost the race exits. - let mut ready = false; - for _ in 0..50 { + let stderr = child.stderr.take().unwrap(); + let (tx, rx) = std::sync::mpsc::channel::(); + // The pipe is drained for the child's lifetime: a full buffer would block it. + let reader = std::thread::spawn(move || { + for line in BufReader::new(stderr).lines().map_while(Result::ok) { + if tx.send(line).is_err() { + break; + } + } + }); + let found = |rx: &std::sync::mpsc::Receiver| { + rx.try_iter().any(|line| logs_listening(&line, port)) + }; + let deadline = std::time::Instant::now() + STARTUP_BOUND; + loop { + if found(&rx) { + return Some(child); + } if child.try_wait().unwrap().is_some() { - return false; + // Read what is left before calling it a lost race. + let _ = reader.join(); + return found(&rx).then_some(child); } - if healthz_answers(port) { - ready = true; - break; + if std::time::Instant::now() > deadline { + let _ = child.kill(); + let _ = child.wait(); + panic!("server never logged listening on 127.0.0.1:{port}"); } - std::thread::sleep(Duration::from_millis(100)); - } - if !ready || child.try_wait().unwrap().is_some() { - // Alive but never answered: a real startup failure, not a lost race. - let hung = !ready && child.try_wait().unwrap().is_none(); - let _ = child.kill(); - let _ = child.wait(); - assert!(!hung, "server did not start"); - return false; + std::thread::sleep(Duration::from_millis(25)); } +} +/// The startup record: `listening addr=127.0.0.1: tls=false`. +fn logs_listening(line: &str, port: u16) -> bool { + let line = without_ansi(line); + line.contains("listening") + && line + .split_whitespace() + .find_map(|w| w.strip_prefix("addr=")) + .and_then(|addr| addr.parse::().ok()) + == Some(std::net::SocketAddr::from(([127, 0, 0, 1], port))) +} + +/// The log is written to a pipe, but tracing still colourises it. +fn without_ansi(line: &str) -> String { + let mut out = String::with_capacity(line.len()); + let mut rest = line.chars(); + while let Some(c) = rest.next() { + if c != '\u{1b}' { + out.push(c); + continue; + } + // CSI: `ESC [ params… final`; the final byte ends the sequence. + for c in rest.by_ref() { + if !matches!(c, '[' | '0'..='9' | ';' | ':') { + break; + } + } + } + out +} + +/// A running child on a free port, retrying a port a stranger won. +fn start_listening() -> (std::process::Child, u16) { + for _ in 0..3 { + let port = free_port(); + if let Some(child) = spawn_listening(port) { + return (child, port); + } + } + panic!("server did not start"); +} + +fn sse_stream_then_sigterm() -> bool { + sse_stream_then_sigterm_on(free_port()) +} + +/// One attempt on `port`. `false` means the child exited before it served, +/// which only happens when the probed port was taken by someone else. +fn sse_stream_then_sigterm_on(port: u16) -> bool { + use std::io::{Read, Write}; + use std::net::TcpStream as StdStream; + + let Some(mut child) = spawn_listening(port) else { + return false; + }; let mut conn = StdStream::connect(("127.0.0.1", port)).unwrap(); conn.set_read_timeout(Some(Duration::from_millis(500))) .unwrap(); @@ -487,16 +557,61 @@ fn sse_stream_then_sigterm_on(port: u16) -> bool { true } -/// A port already held by a listener that never answers is not ours: the -/// helper reports "retry" instead of signalling the wrong server. #[test] -fn a_probed_port_taken_by_someone_else_is_retried() { - let squatter = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); - let port = squatter.local_addr().unwrap().port(); - assert!(!sse_stream_then_sigterm_on(port)); - drop(squatter); +fn only_the_childs_own_listening_line_counts() { + let line = "\u{1b}[2m2026-10-20T20:22:10.228194Z\u{1b}[0m \u{1b}[32m INFO\u{1b}[0m \u{1b}[2mskyg_linux_tracker::server\u{1b}[0m\u{1b}[2m:\u{1b}[0m listening \u{1b}[3maddr\u{1b}[0m\u{1b}[2m=\u{1b}[0m127.0.0.1:40001 \u{1b}[3mtls\u{1b}[0m\u{1b}[2m=\u{1b}[0mfalse"; + assert!(logs_listening(line, 40001)); + // A neighbouring port shares the prefix; the address must match whole. + assert!(!logs_listening(line, 4000)); + assert!(!logs_listening(line, 40002)); + assert!(!logs_listening( + "listening addr=127.0.0.2:40001 tls=false", + 40001 + )); + assert!(!logs_listening("HTTP/1.1 200 OK", 40001)); } +/// A port held by a listener that answers `/healthz` with 200 is still not +/// ours: liveness is not identity, so the helper retries instead of +/// signalling a child that lost the bind and exited. +#[test] +fn a_probed_port_held_by_a_lying_squatter_is_retried() { + use std::io::{Read, Write}; + let squatter = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let port = squatter.local_addr().unwrap().port(); + squatter.set_nonblocking(true).unwrap(); + let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let serving = { + let stop = std::sync::Arc::clone(&stop); + std::thread::spawn(move || { + while !stop.load(std::sync::atomic::Ordering::SeqCst) { + match squatter.accept() { + Ok((mut sock, _)) => { + let mut buf = [0u8; 1024]; + let _ = sock.read(&mut buf); + let _ = sock.write_all( + b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok", + ); + } + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + std::thread::sleep(Duration::from_millis(10)); + } + Err(_) => break, + } + } + }) + }; + // The squatter really does hand out 200s, so the helper cannot lean on them. + assert!(healthz_answers(port)); + assert!( + !sse_stream_then_sigterm_on(port), + "a 200-answering squatter must not be taken for our server" + ); + stop.store(true, std::sync::atomic::Ordering::SeqCst); + serving.join().unwrap(); +} + +/// True when `port` answers `/healthz` with a 200. Not proof of identity. fn healthz_answers(port: u16) -> bool { use std::io::{Read, Write}; let Ok(mut conn) = std::net::TcpStream::connect(("127.0.0.1", port)) else { @@ -525,17 +640,7 @@ fn healthz_answers(port: u16) -> bool { fn sigterm_with_inflight_mcp_tail_exits_within_bound() { use std::io::{Read, Write}; use std::net::TcpStream as StdStream; - let port = { - let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); - l.local_addr().unwrap().port() - }; - let mut child = std::process::Command::new(env!("CARGO_BIN_EXE_slinuxm")) - .args(["serve", "--auth-enabled", "false", "--bind", "127.0.0.1"]) - .args(["--port", &port.to_string()]) - .env_clear() - .stderr(std::process::Stdio::null()) - .spawn() - .unwrap(); + let (mut child, port) = start_listening(); let post = |body: &str, sid: Option<&str>, read_resp: bool| -> (StdStream, String) { let mut c = None; for _ in 0..50 {