diff --git a/core/crates/account/src/account/mod.rs b/core/crates/account/src/account/mod.rs index f682142929..a3f50f1724 100644 --- a/core/crates/account/src/account/mod.rs +++ b/core/crates/account/src/account/mod.rs @@ -745,11 +745,9 @@ mod tests { MachineId::derive("4c4f5e2a0b6d4f3c9e8a7b6c5d4e3f20\n").expect("a machine ID") } - // Nothing listens on the port once the listener is dropped, so every request is refused. + // Port 1 is below every system's ephemeral range, so no test's listener takes it and every request is refused. fn unreachable() -> Endpoints { - let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port"); - let base = format!("http://{}", listener.local_addr().expect("its address")); - Endpoints::new(&base, &format!("{base}/v2")).expect("the endpoints") + Endpoints::new("http://127.0.0.1:1", "http://127.0.0.1:1/v2").expect("the endpoints") } // Stands in for tablepro-cloud: `answer` gives the status and body for a method and path. diff --git a/core/crates/account/src/account/share_calls.rs b/core/crates/account/src/account/share_calls.rs index 975b459767..559b138209 100644 --- a/core/crates/account/src/account/share_calls.rs +++ b/core/crates/account/src/account/share_calls.rs @@ -905,10 +905,8 @@ mod tests { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn a_network_failure_never_words_the_share_token() { let dir = tempfile::tempdir().expect("a temporary directory"); - let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port"); - let base = format!("http://{}", listener.local_addr().expect("its address")); - drop(listener); - let endpoints = Endpoints::new(&base, &format!("{base}/v2")).expect("the endpoints"); + // Port 1 is below every system's ephemeral range, so no test's listener takes it and the request is refused. + let endpoints = Endpoints::new("http://127.0.0.1:1", "http://127.0.0.1:1/v2").expect("the endpoints"); let account = signed_in(dir.path(), endpoints).await; let failed = account.fetch_shared(TOKEN, None).await.expect_err("nothing listens"); diff --git a/core/crates/ai/tests/providers.rs b/core/crates/ai/tests/providers.rs index 461d1b6ad9..15ac03369f 100644 --- a/core/crates/ai/tests/providers.rs +++ b/core/crates/ai/tests/providers.rs @@ -478,10 +478,8 @@ async fn the_models_are_listed_sorted() { #[tokio::test(flavor = "multi_thread")] async fn a_closed_port_says_it_could_not_reach_the_host() { - let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); - let address = listener.local_addr().unwrap(); - drop(listener); - let endpoint = Endpoint::new(AiProvider::OpenAiCompatible, &format!("http://{address}/v1"), "m", None).unwrap(); + // Port 1 is below every system's ephemeral range, so no test's listener takes it and the request is refused. + let endpoint = Endpoint::new(AiProvider::OpenAiCompatible, "http://127.0.0.1:1/v1", "m", None).unwrap(); let models = list_models(&client(), &endpoint).await; diff --git a/core/crates/cli/tests/cli_over_mcp.rs b/core/crates/cli/tests/cli_over_mcp.rs index 28327dac1e..4886adabfd 100644 --- a/core/crates/cli/tests/cli_over_mcp.rs +++ b/core/crates/cli/tests/cli_over_mcp.rs @@ -546,11 +546,9 @@ async fn a_changed_token_is_refused_and_a_closed_port_is_unreachable() { token: Some("tp_other".to_owned()), ..Machine::of(&harness) }; - let free = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port"); - let closed_port = free.local_addr().expect("an address").port(); - drop(free); + // Port 1 is below every system's ephemeral range, so no test's listener takes it and the connect is refused. let silent = Machine { - port: closed_port, + port: 1, ..Machine::of(&harness) }; diff --git a/core/crates/drivers/clickhouse/src/editor_run.rs b/core/crates/drivers/clickhouse/src/editor_run.rs index 61a6caba3f..74ff6b8268 100644 --- a/core/crates/drivers/clickhouse/src/editor_run.rs +++ b/core/crates/drivers/clickhouse/src/editor_run.rs @@ -487,12 +487,9 @@ mod tests { ) } - /// A port nothing listens on, so a request is refused at once. - async fn closed_route() -> clickhouse::Client { - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("a listener"); - let address = listener.local_addr().expect("an address"); - drop(listener); - clickhouse::Client::default().with_url(format!("http://{address}")) + // Port 1 is below every system's ephemeral range, so no test's listener takes it and a request is refused at once. + fn closed_route() -> clickhouse::Client { + clickhouse::Client::default().with_url("http://127.0.0.1:1") } struct Discard; @@ -526,7 +523,7 @@ mod tests { let lingering = Arc::new(LingeringQuery::default()); lingering.left(Uuid::new_v4()); let run = EditorRun { - session: closed_route().await, + session: closed_route(), control, statement_timeout: None, bounds_on_server: true, @@ -558,7 +555,7 @@ mod tests { for bounds_on_server in [false, true] { let run = EditorRun { session: clickhouse::Client::default().with_url(url.clone()), - control: closed_route().await, + control: closed_route(), statement_timeout: None, bounds_on_server, grace: Duration::from_millis(200), diff --git a/core/crates/drivers/mongodb/tests/integration/replica_set.rs b/core/crates/drivers/mongodb/tests/integration/replica_set.rs index e1929d30bc..fc71ddd8cf 100644 --- a/core/crates/drivers/mongodb/tests/integration/replica_set.rs +++ b/core/crates/drivers/mongodb/tests/integration/replica_set.rs @@ -13,6 +13,9 @@ use crate::support::{CALL, DATABASE, connect_to, direct, editor, first_page, pas /// The name the set's one member gives itself. const MEMBER: &str = "localhost.test.build.10gen.cc:27017"; +// Port 1 is below every system's ephemeral range, so no test's listener takes it and the seed is refused. +const CLOSED_PORT: u16 = 1; + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker, public DNS and host port 27017"] async fn a_replica_set_is_found_from_its_seeds_and_read_through_its_primary() { @@ -22,7 +25,7 @@ async fn a_replica_set_is_found_from_its_seeds_and_read_through_its_primary() { let liveness = test_liveness(); // Only the second seed reaches the set, so the set is found from the host list or not at all. let target = NetworkTarget { - transport: direct("127.0.0.1", closed_port(), &liveness), + transport: direct("127.0.0.1", CLOSED_PORT, &liveness), credentials: password(&fixture.password_credentials()), database: Some(DATABASE.to_owned()), tls: TlsConfig::disabled(), @@ -59,9 +62,3 @@ async fn a_replica_set_is_found_from_its_seeds_and_read_through_its_primary() { session.close().await; connection.close().await.expect("a clean close"); } - -/// A port on this host that nothing listens on. -fn closed_port() -> u16 { - let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port"); - listener.local_addr().expect("the port bound").port() -} diff --git a/core/crates/ffi/tests/account.rs b/core/crates/ffi/tests/account.rs index 16691cc66b..7b8fa06709 100644 --- a/core/crates/ffi/tests/account.rs +++ b/core/crates/ffi/tests/account.rs @@ -135,10 +135,9 @@ async fn open(base: &str) -> Opened { } } -// Nothing listens once the listener is dropped, so every request is refused. +// Port 1 is below every system's ephemeral range, so no test's listener takes it and every request is refused. fn unreachable() -> String { - let listener = TcpListener::bind("127.0.0.1:0").unwrap(); - format!("http://{}", listener.local_addr().unwrap()) + "http://127.0.0.1:1".to_owned() } // Stands in for tablepro-cloud: `answer` gives the status and body for a method, path and request body. diff --git a/core/crates/net/src/dial.rs b/core/crates/net/src/dial.rs index fde8778c14..b4173548d9 100644 --- a/core/crates/net/src/dial.rs +++ b/core/crates/net/src/dial.rs @@ -151,6 +151,11 @@ mod tests { Instant::now() + liveness.connect_timeout } + // Port 1 is below every system's ephemeral range, so no test's listener takes it and a connect is refused. + fn refused() -> SocketAddr { + SocketAddr::from(([127, 0, 0, 1], 1)) + } + fn full_backlog_listener() -> (Socket, SocketAddr, Vec) { let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap(); socket.bind(&SocketAddr::from(([127, 0, 0, 1], 0)).into()).unwrap(); @@ -163,14 +168,13 @@ mod tests { } #[tokio::test] - async fn dropped_port_then_live_listener_connects_second() { - let dropped = StdListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap(); + async fn refused_port_then_live_listener_connects_second() { let live = StdListener::bind("127.0.0.1:0").unwrap(); let live_address = live.local_addr().unwrap(); let endpoint = NetworkEndpoint::new("127.0.0.1", live_address.port()).unwrap(); let liveness = quick_liveness(); - let stream = dial_addresses(vec![dropped, live_address], &endpoint, &liveness, deadline(&liveness)) + let stream = dial_addresses(vec![refused(), live_address], &endpoint, &liveness, deadline(&liveness)) .await .unwrap(); assert_eq!(stream.peer_addr().unwrap(), live_address); @@ -243,13 +247,11 @@ mod tests { #[tokio::test] async fn every_address_refused_is_refused() { - let first = StdListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap(); - let second = StdListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap(); - let endpoint = NetworkEndpoint::new("127.0.0.1", first.port()).unwrap(); + let endpoint = NetworkEndpoint::new("127.0.0.1", refused().port()).unwrap(); let liveness = racing_liveness(); assert_eq!( - dial_addresses(vec![first, second], &endpoint, &liveness, deadline(&liveness)) + dial_addresses(vec![refused(), refused()], &endpoint, &liveness, deadline(&liveness)) .await .err(), Some(TransportError::Refused { diff --git a/core/crates/net/src/tls/probe.rs b/core/crates/net/src/tls/probe.rs index ad0e0e3b7f..1513abba4f 100644 --- a/core/crates/net/src/tls/probe.rs +++ b/core/crates/net/src/tls/probe.rs @@ -140,10 +140,9 @@ mod tests { #[tokio::test] async fn nothing_listening_does_not_serve_tls() { - let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); - let address = listener.local_addr().unwrap(); - drop(listener); + // Port 1 is below every system's ephemeral range, so no test's listener takes it and the connect is refused. + let refused = SocketAddr::from(([127, 0, 0, 1], 1)); - assert!(!serves_tls(&transport(address), NAME, WITHIN).await); + assert!(!serves_tls(&transport(refused), NAME, WITHIN).await); } } diff --git a/windows/tests/TablePro.Interop.Tests/AccountTests.cs b/windows/tests/TablePro.Interop.Tests/AccountTests.cs index 70a808b0f2..0ea7d8c682 100644 --- a/windows/tests/TablePro.Interop.Tests/AccountTests.cs +++ b/windows/tests/TablePro.Interop.Tests/AccountTests.cs @@ -246,15 +246,8 @@ private static async Task FirstChangeAsync(TableProAccount accoun private static string Problem(int status, string code) => $$"""{"type":"about:blank","title":"Refused","status":{{status}},"code":"{{code}}"}"""; - // Nothing listens once the listener is stopped, so every request is refused. - private static string Unreachable() - { - var listener = new TcpListener(IPAddress.Loopback, 0); - listener.Start(); - var port = ((IPEndPoint)listener.LocalEndpoint).Port; - listener.Stop(); - return $"http://127.0.0.1:{port}"; - } + // Port 1 is below every system's ephemeral range, so no test's listener takes it and every request is refused. + private static string Unreachable() => "http://127.0.0.1:1"; private Task OpenAsync(string cloud, IAccountBrowser browser) => _core.OpenAccountAsync(Configuration(cloud), browser);