From 066397eea716e8e54a1c6705cffe13db3ff738b2 Mon Sep 17 00:00:00 2001 From: Ryan Schmitt Date: Wed, 17 Dec 2025 23:37:48 -0500 Subject: [PATCH] Bind test servers to loopback address Binding to 0.0.0.0 is not guaranteed to result in a socket that is reachable via the loopback address, even when requesting an ephemeral port assignment by binding to port 0. It is possible for another process on the system to bind to the loopback address at that same port, and then client requests will go to that process instead of the test server, resulting in occasional cryptic test failures. By instead binding to the loopback address specifically, rather than the wildcard address, we can prevent binding to a port that is already in use. This significantly improves the reliability of the test suite. --- .../testing/classic/ClassicTestServer.java | 2 +- .../hc/core5/testing/nio/HttpTestServer.java | 3 ++- .../hc/core5/benchmark/BenchmarkToolTest.java | 3 ++- .../apache/hc/core5/testing/nio/AlpnTest.java | 11 +++++----- .../nio/AsyncServerBootstrapFilterTest.java | 3 ++- .../nio/ClassicToAsyncHttp1TransportTest.java | 3 ++- .../nio/ClassicToAsyncTransportTest.java | 7 ++++--- .../hc/core5/testing/nio/H2AlpnTest.java | 3 ++- .../hc/core5/testing/nio/H2ConnPoolTest.java | 3 ++- .../nio/H2CoreTransportMultiplexingTest.java | 11 +++++----- .../nio/H2ServerBootstrapFiltersTest.java | 3 ++- .../testing/nio/Http1AuthenticationTest.java | 7 ++++--- .../testing/nio/Http1CoreTransportTest.java | 3 ++- .../testing/nio/HttpCoreTransportTest.java | 13 ++++++------ .../core5/testing/nio/TLSIntegrationTest.java | 6 +++--- .../hc/core5/testing/nio/TLSUpgradeTest.java | 3 ++- .../nio/TestDefaultListeningIOReactor.java | 7 ++++--- .../testing/reactive/ReactiveClientTest.java | 3 ++- .../hc/core5/ssl/TestSSLContextBuilder.java | 21 ++++++++++--------- 19 files changed, 66 insertions(+), 49 deletions(-) diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java index 01f624292c..46fb945ec1 100644 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java +++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java @@ -191,7 +191,7 @@ public void start() throws IOException { final HttpServer server = new HttpServer( 0, httpService, - null, + InetAddress.getLoopbackAddress(), socketConfig, null, new LoggingBHttpServerConnectionFactory( diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/HttpTestServer.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/HttpTestServer.java index ec127406c9..7218aaab70 100644 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/HttpTestServer.java +++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/HttpTestServer.java @@ -28,6 +28,7 @@ package org.apache.hc.core5.testing.nio; import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; @@ -112,7 +113,7 @@ public void configure(final Decorator exchangeHandle public InetSocketAddress startExecution(final IOEventHandlerFactory handlerFactory) throws Exception { execute(handlerFactory); - final Future future = listen(new InetSocketAddress(0)); + final Future future = listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); final ListenerEndpoint listener = future.get(); return (InetSocketAddress) listener.getAddress(); } diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/benchmark/BenchmarkToolTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/benchmark/BenchmarkToolTest.java index f21d948f68..719b3ef5b7 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/benchmark/BenchmarkToolTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/benchmark/BenchmarkToolTest.java @@ -27,6 +27,7 @@ package org.apache.hc.core5.benchmark; import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.concurrent.Future; import java.util.stream.Stream; @@ -105,7 +106,7 @@ public void handle( .setVersionPolicy(versionPolicy) .create(); server.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTP); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTP); final ListenerEndpoint listener = future.get(); address = (InetSocketAddress) listener.getAddress(); } diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/AlpnTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/AlpnTest.java index 82d13466d9..9b26e85099 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/AlpnTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/AlpnTest.java @@ -29,6 +29,7 @@ import static org.hamcrest.MatcherAssert.assertThat; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.concurrent.Future; import java.util.function.Function; @@ -128,7 +129,7 @@ void setup() throws Exception { @Test void testForceHttp1() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTPS); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); @@ -153,7 +154,7 @@ void testForceHttp1GlobalPolicy() throws Exception { serverResource.configure(bootstrap -> bootstrap.setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_1)); final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTPS); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); @@ -176,7 +177,7 @@ void testForceHttp1GlobalPolicy() throws Exception { @Test void testForceHttp2() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTPS); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); @@ -201,7 +202,7 @@ void testForceHttp2GlobalPolicy() throws Exception { serverResource.configure(bootstrap -> bootstrap.setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)); final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTPS); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); @@ -224,7 +225,7 @@ void testForceHttp2GlobalPolicy() throws Exception { @Test void testNegotiateProtocol() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTPS); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/AsyncServerBootstrapFilterTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/AsyncServerBootstrapFilterTest.java index 6707b23972..e9aa29961a 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/AsyncServerBootstrapFilterTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/AsyncServerBootstrapFilterTest.java @@ -30,6 +30,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.concurrent.Future; @@ -125,7 +126,7 @@ public void pushPromise( @Test void testFilters() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTP); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTP); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncHttp1TransportTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncHttp1TransportTest.java index 3842b1e72a..5e312e44c6 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncHttp1TransportTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncHttp1TransportTest.java @@ -27,6 +27,7 @@ package org.apache.hc.core5.testing.nio; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.Random; import java.util.concurrent.Future; @@ -62,7 +63,7 @@ void test_request_handling_no_keep_alive(final int contentSize) throws Exception final HttpAsyncServer server = serverResource.start(); registerHandler("/echo", () -> new EchoHandler(1024)); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncTransportTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncTransportTest.java index e9c82c0c8a..022cd62cb3 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncTransportTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncTransportTest.java @@ -32,6 +32,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.charset.Charset; @@ -151,7 +152,7 @@ void test_request_execution() throws Exception { final HttpAsyncServer server = serverResource.start(); registerHandler("/echo", () -> new EchoHandler(1024)); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); @@ -228,7 +229,7 @@ void test_request_handling(final String method) throws Exception { }); final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); @@ -324,7 +325,7 @@ void test_request_handling_full_streaming() throws Exception { }); final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2AlpnTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2AlpnTest.java index 39c39b5519..db4726ecc4 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2AlpnTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2AlpnTest.java @@ -32,6 +32,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -114,7 +115,7 @@ public H2AlpnTest(final boolean strictALPN, final boolean h2Allowed) throws Exce @Test void testALPN() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTPS); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final H2MultiplexingRequester requester = clientResource.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ConnPoolTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ConnPoolTest.java index e72e4b1f38..7c3a4c5147 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ConnPoolTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ConnPoolTest.java @@ -27,6 +27,7 @@ package org.apache.hc.core5.testing.nio; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicLong; @@ -112,7 +113,7 @@ void testManyGetSession() throws Exception { final int n = 200; final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTP); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTP); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpHost target = new HttpHost(URIScheme.HTTP.id, "localhost", address.getPort()); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2CoreTransportMultiplexingTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2CoreTransportMultiplexingTest.java index 408ff8d7e7..6d742dce42 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2CoreTransportMultiplexingTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2CoreTransportMultiplexingTest.java @@ -29,6 +29,7 @@ import static org.hamcrest.MatcherAssert.assertThat; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.LinkedList; import java.util.Queue; @@ -109,7 +110,7 @@ public H2CoreTransportMultiplexingTest(final URIScheme scheme) { @Test void testSequentialRequests() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final H2MultiplexingRequester requester = clientResource.start(); @@ -152,7 +153,7 @@ void testSequentialRequests() throws Exception { @Test void testLargeRequest() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final H2MultiplexingRequester requester = clientResource.start(); @@ -173,7 +174,7 @@ void testLargeRequest() throws Exception { @Test void testMultiplexedRequests() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final H2MultiplexingRequester requester = clientResource.start(); @@ -208,7 +209,7 @@ void testMultiplexedRequests() throws Exception { @Test void testValidityCheck() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final H2MultiplexingRequester requester = clientResource.start(); @@ -256,7 +257,7 @@ void testValidityCheck() throws Exception { @Test void testMultiplexedRequestCancellation() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final H2MultiplexingRequester requester = clientResource.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ServerBootstrapFiltersTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ServerBootstrapFiltersTest.java index 61d2a85227..5c6d69970b 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ServerBootstrapFiltersTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ServerBootstrapFiltersTest.java @@ -30,6 +30,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.concurrent.Future; @@ -129,7 +130,7 @@ public void pushPromise( void testSequentialRequests() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTP); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTP); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java index 3a77f018e5..9b0fe38546 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java @@ -29,6 +29,7 @@ import static org.hamcrest.MatcherAssert.assertThat; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import java.util.Random; @@ -137,7 +138,7 @@ protected AsyncEntityProducer generateResponseContent(final HttpResponse unautho @Test void testGetRequestAuthentication() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTP); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTP); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); @@ -171,7 +172,7 @@ void testGetRequestAuthentication() throws Exception { @Test void testPostRequestAuthentication() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTP); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTP); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); @@ -209,7 +210,7 @@ void testPostRequestAuthentication() throws Exception { @Test void testPostRequestAuthenticationNoExpectContinue() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTP); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTP); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1CoreTransportTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1CoreTransportTest.java index e309d3bdd9..ebf0e16368 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1CoreTransportTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1CoreTransportTest.java @@ -30,6 +30,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.concurrent.Future; @@ -147,7 +148,7 @@ HttpAsyncRequester clientStart() { @Test void testSequentialRequestsNonPersistentConnection() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/HttpCoreTransportTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/HttpCoreTransportTest.java index a9049b2e92..a5ccd65baf 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/HttpCoreTransportTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/HttpCoreTransportTest.java @@ -30,6 +30,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.LinkedList; import java.util.Queue; @@ -76,7 +77,7 @@ public HttpCoreTransportTest(final URIScheme scheme) { @Test void testSequentialRequests() throws Exception { final HttpAsyncServer server = serverStart(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientStart(); @@ -119,7 +120,7 @@ void testSequentialRequests() throws Exception { @Test void testLargeRequest() throws Exception { final HttpAsyncServer server = serverStart(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientStart(); @@ -140,7 +141,7 @@ void testLargeRequest() throws Exception { @Test void testSequentialRequestsNonPersistentConnection() throws Exception { final HttpAsyncServer server = serverStart(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientStart(); @@ -183,7 +184,7 @@ void testSequentialRequestsNonPersistentConnection() throws Exception { @Test void testSequentialRequestsSameEndpoint() throws Exception { final HttpAsyncServer server = serverStart(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientStart(); @@ -234,7 +235,7 @@ void testSequentialRequestsSameEndpoint() throws Exception { @Test void testPipelinedRequests() throws Exception { final HttpAsyncServer server = serverStart(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientStart(); @@ -277,7 +278,7 @@ void testPipelinedRequests() throws Exception { @Test void testNonPersistentHeads() throws Exception { final HttpAsyncServer server = serverStart(); - final Future future = server.listen(new InetSocketAddress(0), scheme); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), scheme); final ListenerEndpoint listener = future.get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientStart(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java index 9d2f58a607..58be25efac 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java @@ -173,7 +173,7 @@ HttpAsyncRequester createClient(final TlsStrategy tlsStrategy) { } Future executeTlsHandshake() throws Exception { - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTPS); final ListenerEndpoint listener = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); @@ -258,7 +258,7 @@ void testTLSClientAuthFailure() throws Exception { client = createClient(clientTlsStrategy); client.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTPS); final ListenerEndpoint listener = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); @@ -368,7 +368,7 @@ void testHostNameVerification() throws Exception { client = createClient(new BasicClientTlsStrategy(SSLTestContexts.createClientSSLContext())); client.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTPS); final ListenerEndpoint listener = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSUpgradeTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSUpgradeTest.java index 025f04b1fd..8809683b50 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSUpgradeTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSUpgradeTest.java @@ -29,6 +29,7 @@ import static org.hamcrest.MatcherAssert.assertThat; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.concurrent.Future; @@ -101,7 +102,7 @@ public TLSUpgradeTest() { @Test void testTLSUpgrade() throws Exception { final HttpAsyncServer server = serverResource.start(); - final Future future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS); + final Future future = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTPS); final ListenerEndpoint listener = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); final HttpAsyncRequester requester = clientResource.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TestDefaultListeningIOReactor.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TestDefaultListeningIOReactor.java index d183d391bb..3ccd7218a7 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TestDefaultListeningIOReactor.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TestDefaultListeningIOReactor.java @@ -27,6 +27,7 @@ package org.apache.hc.core5.testing.nio; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.Set; import java.util.concurrent.ExecutionException; @@ -73,10 +74,10 @@ void testEndpointUpAndDown() throws Exception { Assertions.assertNotNull(endpoints); Assertions.assertEquals(0, endpoints.size()); - final Future future1 = ioReactor.listen(new InetSocketAddress(0)); + final Future future1 = ioReactor.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); final ListenerEndpoint endpoint1 = future1.get(); - final Future future2 = ioReactor.listen(new InetSocketAddress(0)); + final Future future2 = ioReactor.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); final ListenerEndpoint endpoint2 = future2.get(); final int port = ((InetSocketAddress) endpoint2.getAddress()).getPort(); @@ -103,7 +104,7 @@ void testEndpointUpAndDown() throws Exception { void testEndpointAlreadyBound() throws Exception { ioReactor.start(); - final Future future1 = ioReactor.listen(new InetSocketAddress(0)); + final Future future1 = ioReactor.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); final ListenerEndpoint endpoint1 = future1.get(); final int port = ((InetSocketAddress) endpoint1.getAddress()).getPort(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/reactive/ReactiveClientTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/reactive/ReactiveClientTest.java index d83d1db6b2..dc946779bb 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/reactive/ReactiveClientTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/reactive/ReactiveClientTest.java @@ -31,6 +31,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketTimeoutException; import java.net.URI; @@ -279,7 +280,7 @@ void testResponseCancellation() throws Exception { private InetSocketAddress startServer() throws IOException, InterruptedException, ExecutionException { final HttpAsyncServer server = serverResource.start(); - final ListenerEndpoint listener = server.listen(new InetSocketAddress(0), URIScheme.HTTP).get(); + final ListenerEndpoint listener = server.listen(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), URIScheme.HTTP).get(); final InetSocketAddress address = (InetSocketAddress) listener.getAddress(); return address; } diff --git a/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java b/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java index 6da19a1d18..81ac48c282 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; @@ -318,7 +319,7 @@ void testSSLHandshakeServerTrusted() throws Exception { .build(); Assertions.assertNotNull(clientSslContext); final ServerSocket serverSocket = serverSslContext.getServerSocketFactory().createServerSocket(); - serverSocket.bind(new InetSocketAddress(0)); + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.executorService = Executors.newSingleThreadExecutor(); final Future future = this.executorService.submit(() -> { @@ -359,7 +360,7 @@ void testSSLHandshakeServerNotTrusted() throws Exception { .build(); Assertions.assertNotNull(clientSslContext); final ServerSocket serverSocket = serverSslContext.getServerSocketFactory().createServerSocket(); - serverSocket.bind(new InetSocketAddress(0)); + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.executorService = Executors.newSingleThreadExecutor(); this.executorService.submit(() -> { @@ -399,7 +400,7 @@ void testSSLHandshakeServerCustomTrustStrategy() throws Exception { Assertions.assertNotNull(clientSslContext); final ServerSocket serverSocket = serverSslContext.getServerSocketFactory().createServerSocket(); - serverSocket.bind(new InetSocketAddress(0)); + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.executorService = Executors.newSingleThreadExecutor(); final Future future = this.executorService.submit(() -> { @@ -459,7 +460,7 @@ void testSSLHandshakeClientUnauthenticated() throws Exception { Assertions.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket(); serverSocket.setWantClientAuth(true); - serverSocket.bind(new InetSocketAddress(0)); + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.executorService = Executors.newSingleThreadExecutor(); final Future future = this.executorService.submit(() -> { @@ -508,7 +509,7 @@ void testSSLHandshakeClientUnauthenticatedError() throws Exception { Assertions.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket(); serverSocket.setNeedClientAuth(true); - serverSocket.bind(new InetSocketAddress(0)); + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.executorService = Executors.newSingleThreadExecutor(); this.executorService.submit(() -> { @@ -548,7 +549,7 @@ void testSSLHandshakeClientAuthenticated() throws Exception { Assertions.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket(); serverSocket.setNeedClientAuth(true); - serverSocket.bind(new InetSocketAddress(0)); + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.executorService = Executors.newSingleThreadExecutor(); final Future future = this.executorService.submit(() -> { @@ -597,7 +598,7 @@ void testSSLHandshakeClientAuthenticatedPrivateKeyStrategy() throws Exception { Assertions.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket(); serverSocket.setNeedClientAuth(true); - serverSocket.bind(new InetSocketAddress(0)); + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.executorService = Executors.newSingleThreadExecutor(); final Future future = this.executorService.submit(() -> { @@ -645,7 +646,7 @@ void testSSLHandshakeProtocolMismatch1() throws Exception { final Set supportedServerProtocols = new LinkedHashSet<>(Arrays.asList(serverSocket.getSupportedProtocols())); Assertions.assertTrue(supportedServerProtocols.contains("TLSv1")); serverSocket.setEnabledProtocols(new String[] {"TLSv1"}); - serverSocket.bind(new InetSocketAddress(0)); + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.executorService = Executors.newSingleThreadExecutor(); this.executorService.submit(() -> { @@ -688,7 +689,7 @@ void testSSLHandshakeProtocolMismatch2() throws Exception { final Set supportedServerProtocols = new LinkedHashSet<>(Arrays.asList(serverSocket.getSupportedProtocols())); Assertions.assertTrue(supportedServerProtocols.contains("SSLv3")); serverSocket.setEnabledProtocols(new String[] {"SSLv3"}); - serverSocket.bind(new InetSocketAddress(0)); + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.executorService = Executors.newSingleThreadExecutor(); this.executorService.submit(() -> { @@ -729,7 +730,7 @@ void testJSSEEndpointIdentification() throws Exception { .build(); Assertions.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket(); - serverSocket.bind(new InetSocketAddress(0)); + serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.executorService = Executors.newSingleThreadExecutor(); this.executorService.submit(() -> {