//! Plain-HTTP listener limits: the same accept loop as TLS, so the same //! connection cap, header timeout and graceful drain apply. use std::time::Duration; use skyg_linux_tracker::config::Config; use skyg_linux_tracker::server::{self, Limits}; use skyg_linux_tracker::sources::fake::fake_state; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::{TcpListener, TcpStream}; use tokio::task::JoinHandle; use tokio_util::sync::CancellationToken; async fn plain(limits: Limits) -> (u16, CancellationToken, JoinHandle>) { let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let port = listener.local_addr().unwrap().port(); let mut config = Config::default(); config.auth.enabled = false; let state = fake_state(config); let shutdown = state.shutdown.clone(); let join = tokio::spawn(server::serve_listener( listener, None, server::build_router(state), shutdown.clone(), limits, )); (port, shutdown, join) } /// Connect and complete one `/healthz` exchange, keeping the connection open. async fn served(port: u16) -> TcpStream { let mut c = TcpStream::connect(("127.0.0.1", port)).await.unwrap(); c.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n") .await .unwrap(); let mut buf = [0u8; 1024]; let n = c.read(&mut buf).await.unwrap(); assert!(String::from_utf8_lossy(&buf[..n]).contains("200 OK")); c } #[tokio::test] async fn slowloris_header_is_closed_after_timeout() { let (port, shutdown, _j) = plain(Limits { max_conns: 16, header_timeout: Duration::from_millis(500), }) .await; let mut c = TcpStream::connect(("127.0.0.1", port)).await.unwrap(); // A partial request line, then silence. c.write_all(b"GET /healthz HTTP/1.1\r\nHost: loc") .await .unwrap(); let start = std::time::Instant::now(); let mut buf = [0u8; 256]; let r = tokio::time::timeout(Duration::from_secs(5), async { loop { match c.read(&mut buf).await { Ok(0) | Err(_) => break, Ok(_) => {} } } }) .await; assert!(r.is_ok(), "slowloris connection was not closed"); assert!(start.elapsed() < Duration::from_secs(4)); shutdown.cancel(); } #[tokio::test] async fn connection_cap_is_enforced() { let (port, shutdown, _j) = plain(Limits { max_conns: 2, header_timeout: Duration::from_secs(30), }) .await; let c1 = served(port).await; let c2 = served(port).await; let third = tokio::time::timeout(Duration::from_millis(800), served(port)).await; assert!(third.is_err(), "third connection was served beyond the cap"); drop(c1); let c3 = tokio::time::timeout(Duration::from_secs(5), served(port)).await; assert!(c3.is_ok(), "slot was not released after a close"); drop(c2); shutdown.cancel(); } #[tokio::test] async fn graceful_shutdown_drains_idle_connections() { let (port, shutdown, join) = plain(Limits::default()).await; let mut c = served(port).await; // The idle keep-alive connection is still open; shutdown must complete. shutdown.cancel(); let r = tokio::time::timeout(Duration::from_secs(5), join) .await .expect("serve did not return after shutdown"); r.unwrap().unwrap(); let mut buf = [0u8; 64]; let n = tokio::time::timeout(Duration::from_secs(2), c.read(&mut buf)) .await .expect("idle connection left open"); assert!(matches!(n, Ok(0) | Err(_))); }