|
| 1 | +//! Tests for benign client disconnect handling. |
| 2 | +//! |
| 3 | +//! These tests verify that common client disconnects (e.g., health checks, aborted |
| 4 | +//! connections) are logged at DEBUG level instead of ERROR. |
| 5 | +//! |
| 6 | +//! Note: HTTPS disconnect handling is implemented in handle_https_peer and uses the same |
| 7 | +//! benign disconnect detection logic. However, testing HTTPS requires TLS configuration |
| 8 | +//! which is not included in the default test setup. |
| 9 | +
|
| 10 | +use anyhow::Context as _; |
| 11 | +use rstest::rstest; |
| 12 | +use testsuite::cli::{dgw_tokio_cmd, wait_for_tcp_port}; |
| 13 | +use testsuite::dgw_config::{DgwConfig, DgwConfigHandle, VerbosityProfile}; |
| 14 | +use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; |
| 15 | +use tokio::process::Child; |
| 16 | + |
| 17 | +/// Starts a gateway instance and returns the process and a handle to collect stderr. |
| 18 | +/// |
| 19 | +/// The gateway is configured with DEBUG logging enabled to capture disconnect logs. |
| 20 | +/// Stderr is collected in a background task and returned when the handle is awaited. |
| 21 | +async fn start_gateway_with_logs( |
| 22 | + config_handle: &DgwConfigHandle, |
| 23 | +) -> anyhow::Result<(Child, tokio::task::JoinHandle<Vec<String>>)> { |
| 24 | + let mut process = dgw_tokio_cmd() |
| 25 | + .env("DGATEWAY_CONFIG_PATH", config_handle.config_dir()) |
| 26 | + .env("RUST_LOG", "devolutions_gateway=debug") |
| 27 | + .kill_on_drop(true) |
| 28 | + .stdout(std::process::Stdio::piped()) |
| 29 | + .stderr(std::process::Stdio::piped()) |
| 30 | + .spawn() |
| 31 | + .context("failed to start Devolutions Gateway")?; |
| 32 | + |
| 33 | + let stderr = process.stderr.take().context("failed to take stderr")?; |
| 34 | + let stderr_handle = tokio::spawn(async move { |
| 35 | + let mut stderr_reader = BufReader::new(stderr); |
| 36 | + let mut lines = Vec::new(); |
| 37 | + let mut line_buf = String::new(); |
| 38 | + loop { |
| 39 | + match stderr_reader.read_line(&mut line_buf).await { |
| 40 | + Ok(0) => break, |
| 41 | + Ok(_) => { |
| 42 | + lines.push(line_buf.clone()); |
| 43 | + line_buf.clear(); |
| 44 | + } |
| 45 | + Err(_) => break, |
| 46 | + } |
| 47 | + } |
| 48 | + lines |
| 49 | + }); |
| 50 | + |
| 51 | + // Wait for HTTP port to be ready. |
| 52 | + wait_for_tcp_port(config_handle.http_port()).await?; |
| 53 | + |
| 54 | + Ok((process, stderr_handle)) |
| 55 | +} |
| 56 | + |
| 57 | +/// Test that benign HTTP disconnects log DEBUG, not ERROR. |
| 58 | +/// |
| 59 | +/// Tests various scenarios where clients disconnect without error: |
| 60 | +/// - Connecting and immediately closing (e.g., health checks, port scanners) |
| 61 | +/// - Sending partial request then closing (e.g., aborted browser requests) |
| 62 | +#[rstest] |
| 63 | +#[case::connect_and_close(None)] |
| 64 | +#[case::abort_mid_request(Some("GET /jet/health HTTP/1.1\r\nHost: localhost\r\n".as_bytes()))] |
| 65 | +#[tokio::test] |
| 66 | +async fn benign_http_disconnect(#[case] payload: Option<&[u8]>) -> anyhow::Result<()> { |
| 67 | + // 1) Start the gateway with DEBUG logging. |
| 68 | + let config_handle = DgwConfig::builder() |
| 69 | + .disable_token_validation(true) |
| 70 | + .verbosity_profile(VerbosityProfile::DEBUG) |
| 71 | + .build() |
| 72 | + .init() |
| 73 | + .context("init config")?; |
| 74 | + |
| 75 | + let (mut process, stderr_handle) = start_gateway_with_logs(&config_handle).await?; |
| 76 | + |
| 77 | + // 2) Connect to HTTP port. |
| 78 | + let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", config_handle.http_port())) |
| 79 | + .await |
| 80 | + .context("failed to connect to HTTP port")?; |
| 81 | + |
| 82 | + // 3) Send payload if provided. |
| 83 | + if let Some(data) = payload { |
| 84 | + stream.write_all(data).await.context("failed to send payload")?; |
| 85 | + } |
| 86 | + |
| 87 | + // 4) Close the connection. |
| 88 | + drop(stream); |
| 89 | + |
| 90 | + // Wait a bit for the log to be written. |
| 91 | + tokio::time::sleep(tokio::time::Duration::from_millis(500)).await; |
| 92 | + |
| 93 | + // 5) Stop the gateway and collect logs. |
| 94 | + let _ = process.start_kill(); |
| 95 | + let stderr_lines = tokio::time::timeout(tokio::time::Duration::from_secs(5), stderr_handle) |
| 96 | + .await |
| 97 | + .context("timeout waiting for stderr")? |
| 98 | + .context("wait for stderr collection")?; |
| 99 | + let _ = process.wait().await; |
| 100 | + |
| 101 | + // 6) Verify no ERROR logs about "HTTP server" or "handle_http_peer failed". |
| 102 | + let has_error = stderr_lines.iter().any(|line| { |
| 103 | + line.contains("ERROR") && (line.contains("HTTP server") || line.contains("handle_http_peer failed")) |
| 104 | + }); |
| 105 | + |
| 106 | + assert!( |
| 107 | + !has_error, |
| 108 | + "Expected no ERROR logs for benign HTTP disconnect, but found one" |
| 109 | + ); |
| 110 | + |
| 111 | + Ok(()) |
| 112 | +} |
0 commit comments