# HG changeset patch # User dfuchs # Date 1557408232 -3600 # Node ID 7748aa47b4e28cf4561f1ed970e1bcc27b8342b2 # Parent ebf733a324d4aca38355ce5b6e441981ae7b2c51 8223573: Replace wildcard address with loopback or local host in tests - part 4 Summary: Makes a few intermittently failing tests more resilient to port reuse issues by ensuring they bind to the loopback address instead of the wildcard. Reviewed-by: alanb, chegar diff -r ebf733a324d4 -r 7748aa47b4e2 test/jdk/java/net/Socket/Streams.java --- a/test/jdk/java/net/Socket/Streams.java Thu May 09 14:28:30 2019 +0200 +++ b/test/jdk/java/net/Socket/Streams.java Thu May 09 14:23:52 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -29,6 +29,8 @@ import java.io.IOException; import java.lang.reflect.Constructor; +import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.Phaser; @@ -42,7 +44,9 @@ public static void main(String[] args) throws Exception { - try (ServerSocket ss = new ServerSocket(0)) { + try (ServerSocket ss = new ServerSocket()) { + InetAddress loopback = InetAddress.getLoopbackAddress(); + ss.bind(new InetSocketAddress(loopback, 0)); runTest(OutputStreamGetter.class, ss); runTest(InputStreamGetter.class, ss); } @@ -55,9 +59,12 @@ throws Exception { final int port = ss.getLocalPort(); + final InetAddress address = ss.getInetAddress(); Socket[] sockets = new Socket[NUM_THREADS]; for (int i=0; i -1) { + hostaddr = "[" + hostaddr + "]"; + } + return hostaddr + ":" + getLocalPort(); + } + + void addClient(Socket client) { FtpCommandHandler h = new FtpCommandHandler(client, this); h.setHandlers(fsh, auth); diff -r ebf733a324d4 -r 7748aa47b4e2 test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java --- a/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java Thu May 09 14:28:30 2019 +0200 +++ b/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java Thu May 09 14:23:52 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,7 @@ * @test * @bug 4333920 * @modules jdk.httpserver + * @library /test/lib * @run main ChunkedEncodingTest * @summary ChunkedEncodingTest unit test */ @@ -36,6 +37,7 @@ import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpExchange; import static java.lang.System.out; +import jdk.test.lib.net.URIBuilder; public class ChunkedEncodingTest{ private static MessageDigest serverDigest, clientDigest; @@ -61,7 +63,13 @@ int port = server.getAddress().getPort(); out.println ("Server listening on port: " + port); - client("http://localhost:" + port + "/chunked/"); + String url = URIBuilder.newBuilder() + .scheme("http") + .host(server.getAddress().getAddress()) + .port(port) + .path("/chunked/") + .build().toString(); + client(url); if (!MessageDigest.isEqual(clientMac, serverMac)) { throw new RuntimeException( @@ -83,7 +91,8 @@ * Http Server */ static HttpServer startHttpServer() throws IOException { - HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0); HttpHandler httpHandler = new SimpleHandler(); httpServer.createContext("/chunked/", httpHandler); httpServer.start(); diff -r ebf733a324d4 -r 7748aa47b4e2 test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingWithProgressMonitorTest.java --- a/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingWithProgressMonitorTest.java Thu May 09 14:28:30 2019 +0200 +++ b/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingWithProgressMonitorTest.java Thu May 09 14:23:52 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,6 +27,7 @@ * @summary ChunkedEncoding unit test; MeteredStream/ProgressData problem * @modules java.base/sun.net * jdk.httpserver + * @library /test/lib * @run main ChunkedEncodingWithProgressMonitorTest */ diff -r ebf733a324d4 -r 7748aa47b4e2 test/jdk/sun/net/www/http/ChunkedInputStream/TestAvailable.java --- a/test/jdk/sun/net/www/http/ChunkedInputStream/TestAvailable.java Thu May 09 14:28:30 2019 +0200 +++ b/test/jdk/sun/net/www/http/ChunkedInputStream/TestAvailable.java Thu May 09 14:23:52 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,7 @@ * @test * @bug 6446990 * @modules jdk.httpserver + * @library /test/lib * @run main/othervm TestAvailable * @summary HttpURLConnection#available() reads more and more data into memory */ @@ -35,6 +36,7 @@ import com.sun.net.httpserver.*; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; +import jdk.test.lib.net.URIBuilder; public class TestAvailable { @@ -60,7 +62,13 @@ try { InetSocketAddress address = httpServer.getAddress(); - URL url = new URL("http://localhost:" + address.getPort() + "/testAvailable/"); + URL url = URIBuilder.newBuilder() + .scheme("http") + .host(address.getAddress()) + .port(address.getPort()) + .path("/testAvailable/") + .toURLUnchecked(); + HttpURLConnection uc = (HttpURLConnection)url.openConnection(); uc.setDoOutput(true); @@ -102,7 +110,9 @@ * Http Server */ public void startHttpServer() throws IOException { - httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + InetSocketAddress sockaddr = new InetSocketAddress(loopback, 0); + httpServer = com.sun.net.httpserver.HttpServer.create(sockaddr, 0); // create HttpServer context HttpContext ctx = httpServer.createContext("/testAvailable/", new MyHandler()); diff -r ebf733a324d4 -r 7748aa47b4e2 test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java --- a/test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java Thu May 09 14:28:30 2019 +0200 +++ b/test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java Thu May 09 14:23:52 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -84,8 +84,8 @@ byte[] b; int requests; - MultiThreadTest(int port, int requests) throws Exception { - uri = "http://localhost:" + port + "/foo.html"; + MultiThreadTest(String authority, int requests) throws Exception { + uri = "http://" + authority + "/foo.html"; b = new byte [256]; this.requests = requests; @@ -134,14 +134,16 @@ } /* start the server */ - ServerSocket ss = new ServerSocket(0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + ServerSocket ss = new ServerSocket(); + ss.bind(new InetSocketAddress(loopback, 0)); Server svr = new Server(ss); svr.start(); Object lock = MultiThreadTest.getLock(); synchronized (lock) { for (int i=0; i -1) { + hostaddr = "[" + hostaddr + "]"; + } + return hostaddr + ":" + ss.getLocalPort(); + } + public Queue workers() { return workers; } diff -r ebf733a324d4 -r 7748aa47b4e2 test/jdk/sun/net/www/http/HttpURLConnection/DigestAuth.java --- a/test/jdk/sun/net/www/http/HttpURLConnection/DigestAuth.java Thu May 09 14:28:30 2019 +0200 +++ b/test/jdk/sun/net/www/http/HttpURLConnection/DigestAuth.java Thu May 09 14:23:52 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -29,6 +29,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.Authenticator; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.PasswordAuthentication; import java.net.URL; @@ -54,7 +55,6 @@ */ public class DigestAuth { - static final String LOCALHOST = "localhost"; static final String EXPECT_FAILURE = null; static final String EXPECT_DIGEST = "Digest"; static final String REALM = "testrealm@host.com"; @@ -119,8 +119,7 @@ AuthenticatorImpl auth = new AuthenticatorImpl(); Authenticator.setDefault(auth); - String url = String.format("http://%s:%d/test/", - LOCALHOST, server.getPort()); + String url = String.format("http://%s/test/", server.getAuthority()); boolean success = true; switch (testcase) { @@ -322,6 +321,16 @@ this.server = server; } + public String getAuthority() { + InetAddress address = server.getAddress().getAddress(); + String hostaddr = address.isAnyLocalAddress() + ? "localhost" : address.getHostAddress(); + if (hostaddr.indexOf(':') > -1) { + hostaddr = "[" + hostaddr + "]"; + } + return hostaddr + ":" + getPort(); + } + void setWWWAuthHeader(String wwwAuthHeader) { this.wwwAuthHeader = wwwAuthHeader; } @@ -331,8 +340,9 @@ } static LocalHttpServer startServer() throws IOException { + InetAddress loopback = InetAddress.getLoopbackAddress(); HttpServer httpServer = HttpServer.create( - new InetSocketAddress(0), 0); + new InetSocketAddress(loopback, 0), 0); LocalHttpServer localHttpServer = new LocalHttpServer(httpServer); localHttpServer.start(); @@ -342,7 +352,7 @@ void start() { server.createContext("/test", this); server.start(); - System.out.println("HttpServer: started on port " + getPort()); + System.out.println("HttpServer: started on port " + getAuthority()); } void stop() { diff -r ebf733a324d4 -r 7748aa47b4e2 test/jdk/sun/net/www/protocol/http/6550798/test.java --- a/test/jdk/sun/net/www/protocol/http/6550798/test.java Thu May 09 14:28:30 2019 +0200 +++ b/test/jdk/sun/net/www/protocol/http/6550798/test.java Thu May 09 14:23:52 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -43,14 +43,15 @@ public static void main(String[] args) throws Exception { TestCache.reset(); - HttpServer s = HttpServer.create (new InetSocketAddress(0), 10); - s.createContext ("/", new HttpHandler () { - public void handle (HttpExchange e) { + InetAddress loopback = InetAddress.getLoopbackAddress(); + HttpServer s = HttpServer.create(new InetSocketAddress(loopback, 0), 10); + s.createContext("/", new HttpHandler() { + public void handle(HttpExchange e) { try { byte[] buf = new byte [LEN]; OutputStream o = e.getResponseBody(); e.sendResponseHeaders(200, LEN); - o.write (buf); + o.write(buf); e.close(); } catch (IOException ex) { ex.printStackTrace(); @@ -91,10 +92,10 @@ } if (TestCache.fail) { - System.out.println ("TEST FAILED"); - throw new RuntimeException (); + System.out.println("TEST FAILED"); + throw new RuntimeException(); } else { - System.out.println ("TEST OK"); + System.out.println("TEST OK"); } } } diff -r ebf733a324d4 -r 7748aa47b4e2 test/jdk/sun/net/www/protocol/http/CloseOptionHeader.java --- a/test/jdk/sun/net/www/protocol/http/CloseOptionHeader.java Thu May 09 14:28:30 2019 +0200 +++ b/test/jdk/sun/net/www/protocol/http/CloseOptionHeader.java Thu May 09 14:23:52 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,7 @@ * @test * @bug 6189206 * @modules java.base/sun.net.www + * @library /test/lib * @run main/othervm -Dhttp.keepAlive=false CloseOptionHeader * @summary HTTP client should set "Connection: close" header in request when keepalive is disabled */ @@ -33,7 +34,7 @@ import java.util.*; import java.io.*; import sun.net.www.MessageHeader; - +import jdk.test.lib.net.URIBuilder; public class CloseOptionHeader implements Runnable { static ServerSocket ss; @@ -79,14 +80,20 @@ Thread tester = new Thread(new CloseOptionHeader()); /* start the server */ - ss = new ServerSocket(0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + ss = new ServerSocket(); + ss.bind(new InetSocketAddress(loopback, 0)); tester.start(); /* connect to the server just started * server then check the request to see whether * there is a close connection option header in it */ - URL url = new URL("http://localhost:" + ss.getLocalPort()); + URL url = URIBuilder.newBuilder() + .scheme("http") + .host(ss.getInetAddress()) + .port(ss.getLocalPort()) + .toURL(); HttpURLConnection huc = (HttpURLConnection)url.openConnection(); huc.connect(); huc.getResponseCode();