8223145: Replace wildcard address with loopback or local host in tests - part 1

Replaces binding to wildacard with alternative less susceptible to intermittent failure in some intermittently failing tests.

Reviewed-by: chegar, msheppar
This commit is contained in:
Daniel Fuchs 2019-05-02 11:55:16 +01:00
parent bbd9000753
commit 7d4520c109
24 changed files with 209 additions and 71 deletions

View File

@ -69,7 +69,8 @@ public class B6361557 {
public static void main (String[] args) throws Exception { public static void main (String[] args) throws Exception {
Handler handler = new Handler(); Handler handler = new Handler();
InetSocketAddress addr = new InetSocketAddress (0); InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress addr = new InetSocketAddress (loopback, 0);
HttpServer server = HttpServer.create (addr, 0); HttpServer server = HttpServer.create (addr, 0);
HttpContext ctx = server.createContext ("/test", handler); HttpContext ctx = server.createContext ("/test", handler);
@ -78,7 +79,7 @@ public class B6361557 {
server.start (); server.start ();
InetSocketAddress destaddr = new InetSocketAddress ( InetSocketAddress destaddr = new InetSocketAddress (
InetAddress.getLoopbackAddress(), server.getAddress().getPort() loopback, server.getAddress().getPort()
); );
System.out.println ("destaddr " + destaddr); System.out.println ("destaddr " + destaddr);
@ -86,7 +87,10 @@ public class B6361557 {
int requests = 0; int requests = 0;
int responses = 0; int responses = 0;
while (true) { while (true) {
int selres = selector.select (1); // we need to read responses from time to time: slightly
// increase the timeout with the amount of pending responses
// to give a chance to the server to reply.
int selres = selector.select (requests - responses + 1);
Set<SelectionKey> selkeys = selector.selectedKeys(); Set<SelectionKey> selkeys = selector.selectedKeys();
for (SelectionKey key : selkeys) { for (SelectionKey key : selkeys) {
if (key.isReadable()) { if (key.isReadable()) {
@ -95,14 +99,18 @@ public class B6361557 {
try { try {
int x = chan.read(buf); int x = chan.read(buf);
if (x == -1 || responseComplete(buf)) { if (x == -1 || responseComplete(buf)) {
System.out.print("_");
key.attach(null); key.attach(null);
chan.close(); chan.close();
responses++; responses++;
} }
} catch (IOException e) {} } catch (IOException e) {
System.out.println(e);
}
} }
} }
if (requests < NUM) { if (requests < NUM) {
System.out.print(".");
SocketChannel schan = SocketChannel.open(destaddr); SocketChannel schan = SocketChannel.open(destaddr);
requestBuf.rewind(); requestBuf.rewind();
int c = 0; int c = 0;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2002, 2012, 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -121,13 +121,14 @@ public class B4722333 implements HttpCallback {
MyAuthenticator auth = new MyAuthenticator (); MyAuthenticator auth = new MyAuthenticator ();
Authenticator.setDefault (auth); Authenticator.setDefault (auth);
try { try {
server = new TestHttpServer (new B4722333(), 1, 10, 0); InetAddress loopback = InetAddress.getLoopbackAddress();
server = new TestHttpServer (new B4722333(), 1, 10, loopback, 0);
System.out.println ("Server started: listening on port: " + server.getLocalPort()); System.out.println ("Server started: listening on port: " + server.getLocalPort());
client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html"); client ("http://" + server.getAuthority() + "/d1/d2/d3/foo.html");
client ("http://localhost:"+server.getLocalPort()+"/ASD/d3/x.html"); client ("http://" + server.getAuthority() + "/ASD/d3/x.html");
client ("http://localhost:"+server.getLocalPort()+"/biz/d3/x.html"); client ("http://" + server.getAuthority() + "/biz/d3/x.html");
client ("http://localhost:"+server.getLocalPort()+"/bar/d3/x.html"); client ("http://" + server.getAuthority() + "/bar/d3/x.html");
client ("http://localhost:"+server.getLocalPort()+"/fuzz/d3/x.html"); client ("http://" + server.getAuthority() + "/fuzz/d3/x.html");
} catch (Exception e) { } catch (Exception e) {
if (server != null) { if (server != null) {
server.terminate(); server.terminate();

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,11 +24,13 @@
/** /**
* @test * @test
* @bug 7128648 * @bug 7128648
* @library /test/lib
* @modules jdk.httpserver * @modules jdk.httpserver
* @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map * @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map
*/ */
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -41,6 +43,7 @@ import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.Headers;
import static java.net.Proxy.NO_PROXY; import static java.net.Proxy.NO_PROXY;
import jdk.test.lib.net.URIBuilder;
public class UnmodifiableMaps { public class UnmodifiableMaps {
@ -48,7 +51,12 @@ public class UnmodifiableMaps {
HttpServer server = startHttpServer(); HttpServer server = startHttpServer();
try { try {
InetSocketAddress address = server.getAddress(); InetSocketAddress address = server.getAddress();
URI uri = new URI("http://localhost:" + address.getPort() + "/foo"); URI uri = URIBuilder.newBuilder()
.scheme("http")
.host(address.getAddress())
.port(address.getPort())
.path("/foo")
.build();
doClient(uri); doClient(uri);
} finally { } finally {
server.stop(0); server.stop(0);
@ -78,7 +86,8 @@ public class UnmodifiableMaps {
// HTTP Server // HTTP Server
HttpServer startHttpServer() throws IOException { 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);
httpServer.createContext("/foo", new SimpleHandler()); httpServer.createContext("/foo", new SimpleHandler());
httpServer.start(); httpServer.start();
return httpServer; return httpServer;
@ -146,4 +155,3 @@ public class UnmodifiableMaps {
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");} if (failed > 0) throw new AssertionError("Some tests failed");}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,6 +24,7 @@
/* @test /* @test
* @summary Unit test for java.net.ResponseCache * @summary Unit test for java.net.ResponseCache
* @bug 4837267 * @bug 4837267
* @library /test/lib
* @author Yingxian Wang * @author Yingxian Wang
*/ */
@ -31,6 +32,7 @@ import java.net.*;
import java.util.*; import java.util.*;
import java.io.*; import java.io.*;
import javax.net.ssl.*; import javax.net.ssl.*;
import jdk.test.lib.net.URIBuilder;
/** /**
* Request should get serviced by the cache handler. Response get * Request should get serviced by the cache handler. Response get
@ -90,14 +92,17 @@ public class ResponseCacheTest implements Runnable {
try { fis.close(); } catch (IOException unused) {} try { fis.close(); } catch (IOException unused) {}
} }
} }
static class NameVerifier implements HostnameVerifier { static class NameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) { public boolean verify(String hostname, SSLSession session) {
return true; return true;
} }
} }
ResponseCacheTest() throws Exception { ResponseCacheTest() throws Exception {
/* start the server */ /* start the server */
ss = new ServerSocket(0); InetAddress loopback = InetAddress.getLoopbackAddress();
ss = new ServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));
(new Thread(this)).start(); (new Thread(this)).start();
/* establish http connection to server */ /* establish http connection to server */
url1 = new URL("http://localhost/file1.cache"); url1 = new URL("http://localhost/file1.cache");
@ -126,8 +131,12 @@ static class NameVerifier implements HostnameVerifier {
http.disconnect(); http.disconnect();
// testing ResponseCacheHandler.put() // testing ResponseCacheHandler.put()
url2 = new URL("http://localhost:" + url2 = URIBuilder.newBuilder()
Integer.toString(ss.getLocalPort())+"/file2.1"); .scheme("http")
.host(ss.getInetAddress())
.port(ss.getLocalPort())
.path("/file2.1")
.toURL();
http = (HttpURLConnection)url2.openConnection(); http = (HttpURLConnection)url2.openConnection();
System.out.println("responsecode2 is :"+http.getResponseCode()); System.out.println("responsecode2 is :"+http.getResponseCode());
Map<String,List<String>> headers2 = http.getHeaderFields(); Map<String,List<String>> headers2 = http.getHeaderFields();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -45,7 +45,8 @@ public class GetLocalAddress implements Runnable {
int linger = 65546; int linger = 65546;
int value = 0; int value = 0;
addr = InetAddress.getLocalHost(); addr = InetAddress.getLocalHost();
ss = new ServerSocket(0); ss = new ServerSocket();
ss.bind(new InetSocketAddress(addr, 0));
port = ss.getLocalPort(); port = ss.getLocalPort();
Thread t = new Thread(new GetLocalAddress()); Thread t = new Thread(new GetLocalAddress());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,8 @@
* *
*/ */
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.ServerSocket; import java.net.ServerSocket;
@ -37,8 +39,10 @@ public class SetReceiveBufferSize {
} }
public SetReceiveBufferSize() throws Exception { public SetReceiveBufferSize() throws Exception {
ServerSocket ss = new ServerSocket(0); ServerSocket ss = new ServerSocket();
Socket s = new Socket("localhost", ss.getLocalPort()); InetAddress loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0));
Socket s = new Socket(loopback, ss.getLocalPort());
Socket accepted = ss.accept(); Socket accepted = ss.accept();
try { try {
s.setReceiveBufferSize(0); s.setReceiveBufferSize(0);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -44,7 +44,8 @@ public class SoTimeout implements Runnable {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
addr = InetAddress.getLocalHost(); addr = InetAddress.getLocalHost();
serverSocket = new ServerSocket(0); serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(addr, 0));
port = serverSocket.getLocalPort(); port = serverSocket.getLocalPort();
byte[] b = new byte[12]; byte[] b = new byte[12];

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -39,8 +39,9 @@ public class TestAfterClose
public static void main(String[] args) { public static void main(String[] args) {
try { try {
ServerSocket ss = new ServerSocket(0, 0, null); InetAddress loopback = InetAddress.getLoopbackAddress();
Socket socket = new Socket("localhost", ss.getLocalPort()); ServerSocket ss = new ServerSocket(0, 0, loopback);
Socket socket = new Socket(loopback, ss.getLocalPort());
ss.accept(); ss.accept();
ss.close(); ss.close();
test(socket); test(socket);

View File

@ -54,10 +54,12 @@ public class UrgentDataTest {
try { try {
UrgentDataTest test = new UrgentDataTest (); UrgentDataTest test = new UrgentDataTest ();
if (args.length == 0) { if (args.length == 0) {
test.listener = new ServerSocket (0); InetAddress loopback = InetAddress.getLoopbackAddress();
test.listener = new ServerSocket ();
test.listener.bind(new InetSocketAddress(loopback, 0));
test.isClient = true; test.isClient = true;
test.isServer = true; test.isServer = true;
test.clHost = InetAddress.getLoopbackAddress().getHostAddress(); test.clHost = loopback.getHostAddress();
test.clPort = test.listener.getLocalPort(); test.clPort = test.listener.getLocalPort();
test.run(); test.run();
} else if (args[0].equals ("-server")) { } else if (args[0].equals ("-server")) {

View File

@ -99,7 +99,7 @@ public class OptionsTest {
static void doSocketTests() throws Exception { static void doSocketTests() throws Exception {
try ( try (
ServerSocket srv = new ServerSocket(0); ServerSocket srv = new ServerSocket(0, 50, InetAddress.getLoopbackAddress());
Socket c = new Socket(InetAddress.getLoopbackAddress(), srv.getLocalPort()); Socket c = new Socket(InetAddress.getLoopbackAddress(), srv.getLocalPort());
Socket s = srv.accept(); Socket s = srv.accept();
) { ) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,11 +24,13 @@
/** /**
* @test * @test
* @bug 4145315 * @bug 4145315
* @library /test/lib
* @summary Test a read from nonexistant URL * @summary Test a read from nonexistant URL
*/ */
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import jdk.test.lib.net.URIBuilder;
public class GetContent implements Runnable { public class GetContent implements Runnable {
@ -71,10 +73,12 @@ public class GetContent implements Runnable {
boolean error = true; boolean error = true;
try { try {
String name = "http://localhost:" + ss.getLocalPort() + java.net.URL url = URIBuilder.newBuilder()
"/no-such-name"; .scheme("http")
java.net.URL url = null; .host(ss.getInetAddress())
url = new java.net.URL(name); .port(ss.getLocalPort())
.path("/no-such-name")
.toURL();
Object obj = url.getContent(); Object obj = url.getContent();
InputStream in = (InputStream) obj; InputStream in = (InputStream) obj;
byte buff[] = new byte[200]; byte buff[] = new byte[200];

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -64,9 +64,10 @@ public class B5052093 implements HttpCallback {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
server = new TestHttpServer(new B5052093(), 1, 10, 0); InetAddress loopback = InetAddress.getLoopbackAddress();
server = new TestHttpServer(new B5052093(), 1, 10, loopback, 0);
try { try {
URL url = new URL("http://localhost:"+server.getLocalPort()+"/foo"); URL url = new URL("http://" + server.getAuthority() + "/foo");
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
int i = conn.getContentLength(); int i = conn.getContentLength();
long l = conn.getContentLengthLong(); long l = conn.getContentLengthLong();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -36,6 +36,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetPermission; import java.net.NetPermission;
import java.net.ProxySelector; import java.net.ProxySelector;
import java.net.ServerSocket; import java.net.ServerSocket;
@ -104,7 +106,7 @@ public class LookupTest {
String hostsFileName = CWD + "/LookupTestHosts"; String hostsFileName = CWD + "/LookupTestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName); System.setProperty("jdk.net.hosts.file", hostsFileName);
addMappingToHostsFile("allowedAndFound.com", addMappingToHostsFile("allowedAndFound.com",
"127.0.0.1", InetAddress.getLoopbackAddress().getHostAddress(),
hostsFileName, hostsFileName,
false); false);
addMappingToHostsFile("notAllowedButFound.com", addMappingToHostsFile("notAllowedButFound.com",
@ -131,7 +133,9 @@ public class LookupTest {
private volatile boolean done; private volatile boolean done;
public Server() throws IOException { public Server() throws IOException {
serverSocket = new ServerSocket(0); InetAddress loopback = InetAddress.getLoopbackAddress();
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(loopback, 0));
port = serverSocket.getLocalPort(); port = serverSocket.getLocalPort();
} }

View File

@ -37,6 +37,7 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
@ -52,7 +53,8 @@ public class TestFtpClientNameListWithNull {
FtpClient client = FtpClient.create()) { FtpClient client = FtpClient.create()) {
(new Thread(server)).start(); (new Thread(server)).start();
int port = server.getPort(); int port = server.getPort();
client.connect(new InetSocketAddress("localhost", port)); InetAddress loopback = InetAddress.getLoopbackAddress();
client.connect(new InetSocketAddress(loopback, port));
client.nameList(null); client.nameList(null);
} finally { } finally {
if (commandHasArgs) { if (commandHasArgs) {
@ -66,7 +68,9 @@ public class TestFtpClientNameListWithNull {
private final ServerSocket serverSocket; private final ServerSocket serverSocket;
FtpServer() throws IOException { FtpServer() throws IOException {
serverSocket = new ServerSocket(0); InetAddress loopback = InetAddress.getLoopbackAddress();
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(loopback, 0));
} }
public void handleClient(Socket client) throws IOException { public void handleClient(Socket client) throws IOException {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2002, 2013, 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -126,7 +126,9 @@ public class ProxyTest {
} }
public HttpProxyServer() throws IOException { public HttpProxyServer() throws IOException {
server = new ServerSocket(0); InetAddress loopback = InetAddress.getLoopbackAddress();
server = new ServerSocket();
server.bind(new InetSocketAddress(loopback, 0));
} }
public int getPort() { public int getPort() {
@ -183,7 +185,8 @@ public class ProxyTest {
server.start(); server.start();
int port = server.getPort(); int port = server.getPort();
Proxy ftpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port)); InetAddress loopback = InetAddress.getLoopbackAddress();
Proxy ftpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(loopback, port));
URL url = new URL(testURL); URL url = new URL(testURL);
InputStream ins = (url.openConnection(ftpProxy)).getInputStream(); InputStream ins = (url.openConnection(ftpProxy)).getInputStream();
in = new BufferedReader(new InputStreamReader(ins)); in = new BufferedReader(new InputStreamReader(ins));

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -29,6 +29,7 @@ import java.io.InputStreamReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.Authenticator; import java.net.Authenticator;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.PasswordAuthentication; import java.net.PasswordAuthentication;
import java.net.URL; import java.net.URL;
@ -62,8 +63,8 @@ public class NTLMAuthWithSM {
// set authenticator // set authenticator
Authenticator.setDefault(new AuthenticatorImpl()); Authenticator.setDefault(new AuthenticatorImpl());
String url = String.format("http://localhost:%d/test/", String url = String.format("http://%s/test/",
server.getPort()); server.getAuthority());
// load a document which is protected with NTML authentication // load a document which is protected with NTML authentication
System.out.println("load() called: " + url); System.out.println("load() called: " + url);
@ -107,8 +108,9 @@ public class NTLMAuthWithSM {
} }
static LocalHttpServer startServer() throws IOException { static LocalHttpServer startServer() throws IOException {
InetAddress loopback = InetAddress.getLoopbackAddress();
HttpServer httpServer = HttpServer.create( HttpServer httpServer = HttpServer.create(
new InetSocketAddress(0), 0); new InetSocketAddress(loopback, 0), 0);
LocalHttpServer localHttpServer = new LocalHttpServer(httpServer); LocalHttpServer localHttpServer = new LocalHttpServer(httpServer);
localHttpServer.start(); localHttpServer.start();
@ -126,6 +128,14 @@ public class NTLMAuthWithSM {
System.out.println("HttpServer: stopped"); System.out.println("HttpServer: stopped");
} }
String getAuthority() {
InetAddress address = server.getAddress().getAddress();
String hostaddr = address.isAnyLocalAddress()
? "localhost" : address.getHostAddress();
if (hostaddr.indexOf(':') > -1) hostaddr = "[" + hostaddr + "]";
return hostaddr + ":" + getPort();
}
int getPort() { int getPort() {
return server.getAddress().getPort(); return server.getAddress().getPort();
} }

View File

@ -83,9 +83,10 @@ public class PostOnDelete {
} }
public String getAuthority() { public String getAuthority() {
String address = server.getAddress().getHostString(); InetAddress address = server.getAddress().getAddress();
address = (address.indexOf(':') >= 0) ? ("[" + address + "]") : address; String hostaddr = address.isAnyLocalAddress() ? "localhost" : address.getHostAddress();
return address + ":" + getPort(); hostaddr = (hostaddr.indexOf(':') >= 0) ? ("[" + hostaddr + "]") : hostaddr;
return hostaddr + ":" + getPort();
} }
public int getPort() { public int getPort() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,11 +25,13 @@
* @test * @test
* @bug 4392195 * @bug 4392195
* @summary Infinite loop in sun.net.www.http.KeepAliveStream [due to skip()] * @summary Infinite loop in sun.net.www.http.KeepAliveStream [due to skip()]
* @library /test/lib
* @run main/othervm/timeout=30 KeepAliveStreamClose * @run main/othervm/timeout=30 KeepAliveStreamClose
*/ */
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import jdk.test.lib.net.URIBuilder;
public class KeepAliveStreamClose { public class KeepAliveStreamClose {
static class XServer extends Thread { static class XServer extends Thread {
@ -78,11 +80,16 @@ public class KeepAliveStreamClose {
public static void main (String[] args) { public static void main (String[] args) {
try { try {
ServerSocket serversocket = new ServerSocket (0); InetAddress loopback = InetAddress.getLoopbackAddress();
ServerSocket serversocket = new ServerSocket (0, 50, loopback);
int port = serversocket.getLocalPort (); int port = serversocket.getLocalPort ();
XServer server = new XServer (serversocket); XServer server = new XServer (serversocket);
server.start (); server.start ();
URL url = new URL ("http://localhost:"+port); URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(port)
.toURL();
URLConnection urlc = url.openConnection (); URLConnection urlc = url.openConnection ();
InputStream is = urlc.getInputStream (); InputStream is = urlc.getInputStream ();
int i=0, c; int i=0, c;

View File

@ -68,6 +68,22 @@ public class TestHttpServer {
this (cb, 1, 10, 0); this (cb, 1, 10, 0);
} }
/**
* Create a <code>TestHttpServer<code> instance with the specified callback object
* for handling requests. One thread is created to handle requests,
* and up to ten TCP connections will be handled simultaneously.
* @param cb the callback object which is invoked to handle each
* incoming request
* @param address the address to bind the server to. <code>Null</code>
* means bind to the wildcard address.
* @param port the port number to bind the server to. <code>Zero</code>
* means choose any free port.
*/
public TestHttpServer (HttpCallback cb, InetAddress address, int port) throws IOException {
this (cb, 1, 10, address, 0);
}
/** /**
* Create a <code>TestHttpServer<code> instance with the specified number of * Create a <code>TestHttpServer<code> instance with the specified number of
* threads and maximum number of connections per thread. This functions * threads and maximum number of connections per thread. This functions
@ -102,9 +118,33 @@ public class TestHttpServer {
*/ */
public TestHttpServer (HttpCallback cb, int threads, int cperthread, int port) public TestHttpServer (HttpCallback cb, int threads, int cperthread, int port)
throws IOException {
this(cb, threads, cperthread, null, port);
}
/**
* Create a <code>TestHttpServer<code> instance with the specified number
* of threads and maximum number of connections per thread and running on
* the specified port. The specified number of threads are created to
* handle incoming requests, and each thread is allowed
* to handle a number of simultaneous TCP connections.
* @param cb the callback object which is invoked to handle
* each incoming request
* @param threads the number of threads to create to handle
* requests in parallel
* @param cperthread the number of simultaneous TCP connections
* to handle per thread
* @param address the address to bind the server to. <code>Null</code>
* means bind to the wildcard address.
* @param port the port number to bind the server to. <code>Zero</code>
* means choose any free port.
*/
public TestHttpServer (HttpCallback cb, int threads, int cperthread,
InetAddress address, int port)
throws IOException { throws IOException {
schan = ServerSocketChannel.open (); schan = ServerSocketChannel.open ();
InetSocketAddress addr = new InetSocketAddress (port); InetSocketAddress addr = new InetSocketAddress (address, port);
schan.socket().bind (addr); schan.socket().bind (addr);
this.threads = threads; this.threads = threads;
this.cb = cb; this.cb = cb;
@ -147,6 +187,14 @@ public class TestHttpServer {
return schan.socket().getLocalPort (); return schan.socket().getLocalPort ();
} }
public String getAuthority() {
InetAddress address = schan.socket().getInetAddress();
String hostaddr = address.getHostAddress();
if (address.isAnyLocalAddress()) hostaddr = "localhost";
if (hostaddr.indexOf(':') > -1) hostaddr = "[" + hostaddr + "]";
return hostaddr + ":" + getLocalPort();
}
static class Server extends Thread { static class Server extends Thread {
ServerSocketChannel schan; ServerSocketChannel schan;
@ -746,4 +794,3 @@ public class TestHttpServer {
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -82,7 +82,8 @@ public class B8012625 implements HttpHandler {
ExecutorService ex; ExecutorService ex;
public B8012625 () throws Exception { public B8012625 () throws Exception {
server = HttpServer.create(new InetSocketAddress(0), 10); InetAddress loopback = InetAddress.getLoopbackAddress();
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
HttpContext ctx = server.createContext("/", this); HttpContext ctx = server.createContext("/", this);
ex = Executors.newFixedThreadPool(5); ex = Executors.newFixedThreadPool(5);
server.setExecutor(ex); server.setExecutor(ex);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -80,8 +80,11 @@ class XServer extends Thread {
public class Finalizer { public class Finalizer {
public static void main (String args[]) { public static void main (String args[]) {
ServerSocket serversocket = null;
try { try {
ServerSocket serversocket = new ServerSocket (0); InetAddress loopback = InetAddress.getLoopbackAddress();
serversocket = new ServerSocket();
serversocket.bind(new InetSocketAddress(loopback, 0));
int port = serversocket.getLocalPort (); int port = serversocket.getLocalPort ();
XServer server = new XServer (serversocket); XServer server = new XServer (serversocket);
server.start (); server.start ();
@ -107,6 +110,10 @@ public class Finalizer {
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("finalize method failure."+e); throw new RuntimeException("finalize method failure."+e);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
} finally {
if (serversocket != null) {
try {serversocket.close();} catch (IOException io) {}
}
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -99,8 +99,9 @@ public class ResponseCacheStream implements HttpCallback {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
MyResponseCache cache = new MyResponseCache(); MyResponseCache cache = new MyResponseCache();
try { try {
InetAddress loopback = InetAddress.getLoopbackAddress();
ResponseCache.setDefault(cache); ResponseCache.setDefault(cache);
server = new TestHttpServer (new ResponseCacheStream()); server = new TestHttpServer (new ResponseCacheStream(), loopback, 0);
System.out.println ("Server: listening on port: " + server.getLocalPort()); System.out.println ("Server: listening on port: " + server.getLocalPort());
URL url = URIBuilder.newBuilder() URL url = URIBuilder.newBuilder()
.scheme("http") .scheme("http")

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,6 +24,7 @@
/* @test /* @test
* @bug 4696506 4942650 * @bug 4696506 4942650
* @summary Unit test for java.net.CookieHandler * @summary Unit test for java.net.CookieHandler
* @library /test/lib
* @run main/othervm CookieHandlerTest * @run main/othervm CookieHandlerTest
* *
* SunJSSE does not support dynamic system properties, no way to re-use * SunJSSE does not support dynamic system properties, no way to re-use
@ -35,6 +36,7 @@ import java.net.*;
import java.util.*; import java.util.*;
import java.io.*; import java.io.*;
import javax.net.ssl.*; import javax.net.ssl.*;
import jdk.test.lib.net.URIBuilder;
public class CookieHandlerTest { public class CookieHandlerTest {
static Map<String,String> cookies; static Map<String,String> cookies;
@ -78,10 +80,12 @@ public class CookieHandlerTest {
* to avoid infinite hangs. * to avoid infinite hangs.
*/ */
void doServerSide() throws Exception { void doServerSide() throws Exception {
InetAddress loopback = InetAddress.getLoopbackAddress();
SSLServerSocketFactory sslssf = SSLServerSocketFactory sslssf =
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslServerSocket = SSLServerSocket sslServerSocket =
(SSLServerSocket) sslssf.createServerSocket(serverPort); (SSLServerSocket) sslssf.createServerSocket();
sslServerSocket.bind(new InetSocketAddress(loopback, serverPort));
serverPort = sslServerSocket.getLocalPort(); serverPort = sslServerSocket.getLocalPort();
/* /*
@ -151,8 +155,11 @@ public class CookieHandlerTest {
} }
HttpsURLConnection http = null; HttpsURLConnection http = null;
/* establish http connection to server */ /* establish http connection to server */
String uri = "https://localhost:" + +serverPort ; URL url = URIBuilder.newBuilder()
URL url = new URL(uri); .scheme("https")
.loopback()
.port(serverPort)
.toURL();
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier()); HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
http = (HttpsURLConnection)url.openConnection(); http = (HttpsURLConnection)url.openConnection();

View File

@ -61,6 +61,12 @@ public class URIBuilder {
return this; return this;
} }
public URIBuilder host(InetAddress address) {
String hostaddr = address.isAnyLocalAddress()
? "localhost" : address.getHostAddress();
return host(hostaddr);
}
public URIBuilder loopback() { public URIBuilder loopback() {
return host(InetAddress.getLoopbackAddress().getHostAddress()); return host(InetAddress.getLoopbackAddress().getHostAddress());
} }