Merge pull request #8784 from eclipse/jetty-10.0.x-localhost-resolves-many-addresses

Fixed tests that fail when localhost resolves to more than one address.
This commit is contained in:
Simone Bordet 2022-10-28 16:18:34 +02:00 committed by GitHub
commit f94599bf11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 16 deletions

View File

@ -54,7 +54,7 @@ public class Socks4ProxyTest
public void prepare() throws Exception public void prepare() throws Exception
{ {
proxy = ServerSocketChannel.open(); proxy = ServerSocketChannel.open();
proxy.bind(new InetSocketAddress("localhost", 0)); proxy.bind(new InetSocketAddress("127.0.0.1", 0));
ClientConnector connector = new ClientConnector(); ClientConnector connector = new ClientConnector();
QueuedThreadPool clientThreads = new QueuedThreadPool(); QueuedThreadPool clientThreads = new QueuedThreadPool();
@ -77,7 +77,7 @@ public class Socks4ProxyTest
public void testSocks4Proxy() throws Exception public void testSocks4Proxy() throws Exception
{ {
int proxyPort = proxy.socket().getLocalPort(); int proxyPort = proxy.socket().getLocalPort();
client.getProxyConfiguration().addProxy(new Socks4Proxy("localhost", proxyPort)); client.getProxyConfiguration().addProxy(new Socks4Proxy("127.0.0.1", proxyPort));
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
@ -139,7 +139,7 @@ public class Socks4ProxyTest
public void testSocks4ProxyWithSplitResponse() throws Exception public void testSocks4ProxyWithSplitResponse() throws Exception
{ {
int proxyPort = proxy.socket().getLocalPort(); int proxyPort = proxy.socket().getLocalPort();
client.getProxyConfiguration().addProxy(new Socks4Proxy("localhost", proxyPort)); client.getProxyConfiguration().addProxy(new Socks4Proxy("127.0.0.1", proxyPort));
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
@ -196,7 +196,6 @@ public class Socks4ProxyTest
@Test @Test
public void testSocks4ProxyWithTLSServer() throws Exception public void testSocks4ProxyWithTLSServer() throws Exception
{ {
String proxyHost = "localhost";
int proxyPort = proxy.socket().getLocalPort(); int proxyPort = proxy.socket().getLocalPort();
String serverHost = "127.0.0.13"; // Server host different from proxy host. String serverHost = "127.0.0.13"; // Server host different from proxy host.
@ -215,7 +214,7 @@ public class Socks4ProxyTest
// The hostname must be that of the server, not of the proxy. // The hostname must be that of the server, not of the proxy.
ssl.setHostnameVerifier((hostname, session) -> serverHost.equals(hostname)); ssl.setHostnameVerifier((hostname, session) -> serverHost.equals(hostname));
}); });
client.getProxyConfiguration().addProxy(new Socks4Proxy(proxyHost, proxyPort)); client.getProxyConfiguration().addProxy(new Socks4Proxy("127.0.0.1", proxyPort));
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
client.newRequest(serverHost, serverPort) client.newRequest(serverHost, serverPort)
@ -281,12 +280,15 @@ public class Socks4ProxyTest
@Test @Test
public void testRequestTimeoutWhenSocksProxyDoesNotRespond() throws Exception public void testRequestTimeoutWhenSocksProxyDoesNotRespond() throws Exception
{ {
String proxyHost = "localhost";
int proxyPort = proxy.socket().getLocalPort(); int proxyPort = proxy.socket().getLocalPort();
client.getProxyConfiguration().addProxy(new Socks4Proxy(proxyHost, proxyPort)); client.getProxyConfiguration().addProxy(new Socks4Proxy("127.0.0.1", proxyPort));
long timeout = 1000; long timeout = 1000;
Request request = client.newRequest("localhost", proxyPort + 1)
// Use an address to avoid resolution of "localhost" to multiple addresses.
String serverHost = "127.0.0.13";
int serverPort = proxyPort + 1; // Any port will do
Request request = client.newRequest(serverHost, serverPort)
.timeout(timeout, TimeUnit.MILLISECONDS); .timeout(timeout, TimeUnit.MILLISECONDS);
FutureResponseListener listener = new FutureResponseListener(request); FutureResponseListener listener = new FutureResponseListener(request);
request.send(listener); request.send(listener);
@ -303,13 +305,15 @@ public class Socks4ProxyTest
@Test @Test
public void testIdleTimeoutWhenSocksProxyDoesNotRespond() throws Exception public void testIdleTimeoutWhenSocksProxyDoesNotRespond() throws Exception
{ {
String proxyHost = "localhost";
int proxyPort = proxy.socket().getLocalPort(); int proxyPort = proxy.socket().getLocalPort();
client.getProxyConfiguration().addProxy(new Socks4Proxy(proxyHost, proxyPort)); client.getProxyConfiguration().addProxy(new Socks4Proxy("127.0.0.1", proxyPort));
long idleTimeout = 1000; long idleTimeout = 1000;
client.setIdleTimeout(idleTimeout); client.setIdleTimeout(idleTimeout);
Request request = client.newRequest("localhost", proxyPort + 1); // Use an address to avoid resolution of "localhost" to multiple addresses.
String serverHost = "127.0.0.13";
int serverPort = proxyPort + 1; // Any port will do
Request request = client.newRequest(serverHost, serverPort);
FutureResponseListener listener = new FutureResponseListener(request); FutureResponseListener listener = new FutureResponseListener(request);
request.send(listener); request.send(listener);
@ -325,11 +329,13 @@ public class Socks4ProxyTest
@Test @Test
public void testSocksProxyClosesConnectionImmediately() throws Exception public void testSocksProxyClosesConnectionImmediately() throws Exception
{ {
String proxyHost = "localhost";
int proxyPort = proxy.socket().getLocalPort(); int proxyPort = proxy.socket().getLocalPort();
client.getProxyConfiguration().addProxy(new Socks4Proxy(proxyHost, proxyPort)); client.getProxyConfiguration().addProxy(new Socks4Proxy("127.0.0.1", proxyPort));
Request request = client.newRequest("localhost", proxyPort + 1); // Use an address to avoid resolution of "localhost" to multiple addresses.
String serverHost = "127.0.0.13";
int serverPort = proxyPort + 1; // Any port will do
Request request = client.newRequest(serverHost, serverPort);
FutureResponseListener listener = new FutureResponseListener(request); FutureResponseListener listener = new FutureResponseListener(request);
request.send(listener); request.send(listener);

View File

@ -130,7 +130,8 @@ public class ForwardProxyTLSServerTest
protected HttpProxy newHttpProxy() protected HttpProxy newHttpProxy()
{ {
return new HttpProxy(new Origin.Address("localhost", proxyConnector.getLocalPort()), proxySslContextFactory != null); // Use an address to avoid resolution of "localhost" to multiple addresses.
return new HttpProxy(new Origin.Address("127.0.0.1", proxyConnector.getLocalPort()), proxySslContextFactory != null);
} }
private HttpClient newHttpClient() private HttpClient newHttpClient()
@ -615,7 +616,7 @@ public class ForwardProxyTLSServerTest
if (includeAddress) if (includeAddress)
httpProxy.getIncludedAddresses().add("localhost:" + serverConnector.getLocalPort()); httpProxy.getIncludedAddresses().add("localhost:" + serverConnector.getLocalPort());
httpClient.getProxyConfiguration().addProxy(httpProxy); httpClient.getProxyConfiguration().addProxy(httpProxy);
URI uri = URI.create((proxySslContextFactory == null ? "http" : "https") + "://localhost:" + proxyConnector.getLocalPort()); URI uri = URI.create((proxySslContextFactory == null ? "http" : "https") + "://127.0.0.1:" + proxyConnector.getLocalPort());
httpClient.getAuthenticationStore().addAuthentication(new BasicAuthentication(uri, realm, "proxyUser", "proxyPassword")); httpClient.getAuthenticationStore().addAuthentication(new BasicAuthentication(uri, realm, "proxyUser", "proxyPassword"));
httpClient.start(); httpClient.start();