8223465: Replace wildcard address with loopback or local host in tests - part 3

Reviewed-by: dfuchs
This commit is contained in:
Aleksei Efimov 2019-05-10 15:34:17 +01:00
parent 2559700d1f
commit 6ecf7ffbb1
5 changed files with 63 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2017, 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
@ -29,9 +29,10 @@
* exception on Windows 2000. * exception on Windows 2000.
*/ */
import java.net.BindException; import java.net.BindException;
import java.net.InetAddress;
import java.net.DatagramSocket;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
public class PortUnreachable { public class PortUnreachable {
@ -67,7 +68,7 @@ public class PortUnreachable {
serverPort); serverPort);
while (serverSocket == null) { while (serverSocket == null) {
try { try {
serverSocket = new DatagramSocket(serverPort); serverSocket = new DatagramSocket(serverPort, InetAddress.getLocalHost());
} catch (BindException bEx) { } catch (BindException bEx) {
if (retryCount++ < 5) { if (retryCount++ < 5) {
Thread.sleep(500); Thread.sleep(500);
@ -84,8 +85,7 @@ public class PortUnreachable {
} }
PortUnreachable() throws Exception { PortUnreachable() throws Exception {
clientSock = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 0));
clientSock = new DatagramSocket();
clientPort = clientSock.getLocalPort(); clientPort = clientSock.getLocalPort();
} }
@ -93,7 +93,7 @@ public class PortUnreachable {
void execute () throws Exception{ void execute () throws Exception{
// pick a port for the server // pick a port for the server
DatagramSocket sock2 = new DatagramSocket(); DatagramSocket sock2 = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 0));
serverPort = sock2.getLocalPort(); serverPort = sock2.getLocalPort();
// send a burst of packets to the unbound port - we should get back // send a burst of packets to the unbound port - we should get back

View File

@ -25,7 +25,9 @@
* @test * @test
* @bug 6370908 8220663 * @bug 6370908 8220663
* @library /test/lib * @library /test/lib
* @summary Add support for HTTP_CONNECT proxy in Socket class * @summary Add support for HTTP_CONNECT proxy in Socket class.
* This test uses the wildcard address and is susceptible to fail intermittently.
* @key intermittent
* @modules java.base/sun.net.www * @modules java.base/sun.net.www
* @run main HttpProxy * @run main HttpProxy
* @run main/othervm -Djava.net.preferIPv4Stack=true HttpProxy * @run main/othervm -Djava.net.preferIPv4Stack=true HttpProxy
@ -63,7 +65,7 @@ public class HttpProxy {
// Start internal proxy // Start internal proxy
proxy = new ConnectProxyTunnelServer(); proxy = new ConnectProxyTunnelServer();
proxy.start(); proxy.start();
host = "localhost"; host = InetAddress.getLoopbackAddress().getHostAddress();
port = proxy.getLocalPort(); port = proxy.getLocalPort();
out.println("Running with internal proxy: " + host + ":" + port); out.println("Running with internal proxy: " + host + ":" + port);
} else if (args.length == 2) { } else if (args.length == 2) {
@ -93,6 +95,7 @@ public class HttpProxy {
InetSocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort); InetSocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort);
Proxy httpProxy = new Proxy(Proxy.Type.HTTP, proxyAddress); Proxy httpProxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
// Wildcard address is needed here
try (ServerSocket ss = new ServerSocket(0)) { try (ServerSocket ss = new ServerSocket(0)) {
List<InetSocketAddress> externalAddresses = new ArrayList<>(); List<InetSocketAddress> externalAddresses = new ArrayList<>();
externalAddresses.add( externalAddresses.add(
@ -195,7 +198,7 @@ public class HttpProxy {
private volatile boolean closed; private volatile boolean closed;
public ConnectProxyTunnelServer() throws IOException { public ConnectProxyTunnelServer() throws IOException {
ss = new ServerSocket(0); ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
} }
@Override @Override

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 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
@ -26,13 +26,18 @@
* throws a SocketException if the socket is asynchronously closed. * throws a SocketException if the socket is asynchronously closed.
*/ */
import java.io.IOException; import java.io.IOException;
import java.net.*; import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
public class ServerSocket_accept extends AsyncCloseTest implements Runnable { public class ServerSocket_accept extends AsyncCloseTest implements Runnable {
private final ServerSocket ss; private final ServerSocket ss;
private final int timeout; private final int timeout;
private final CountDownLatch latch; private final CountDownLatch latch;
private final AtomicBoolean readyToClose = new AtomicBoolean(false);
public ServerSocket_accept() throws IOException { public ServerSocket_accept() throws IOException {
this(0); this(0);
@ -55,8 +60,14 @@ public class ServerSocket_accept extends AsyncCloseTest implements Runnable {
public void run() { public void run() {
try { try {
latch.countDown(); latch.countDown();
Socket s = ss.accept(); Socket s;
failed("ServerSocket.accept() returned unexpectly!!" + " - " + s); // if readyToClose is still false it means some other
// process on the system attempted to connect: just
// ignore it, and go back to accept again.
do {
s = ss.accept();
} while (!readyToClose.get());
failed("ServerSocket.accept() returned unexpectedly!!" + " - " + s);
} catch (SocketException se) { } catch (SocketException se) {
closed(); closed();
} catch (Exception e) { } catch (Exception e) {
@ -70,6 +81,7 @@ public class ServerSocket_accept extends AsyncCloseTest implements Runnable {
thr.start(); thr.start();
latch.await(); latch.await();
Thread.sleep(5000); //sleep, so ServerSocket.accept() can block Thread.sleep(5000); //sleep, so ServerSocket.accept() can block
readyToClose.set(true);
ss.close(); ss.close();
thr.join(); thr.join();

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
@ -30,7 +30,14 @@
*/ */
import java.io.IOException; import java.io.IOException;
import java.net.*; import java.net.Authenticator;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocksV4Test { public class SocksV4Test {
@ -55,8 +62,9 @@ public class SocksV4Test {
// We actually use V5 for this test because that is the default // We actually use V5 for this test because that is the default
// protocol version used by the client and it doesn't really handle // protocol version used by the client and it doesn't really handle
// down grading very well. // down grading very well.
try (SocksServer srvr = new SocksServer(0, false); InetAddress lba = InetAddress.getLoopbackAddress();
ServerSocket ss = new ServerSocket(0)) { try (SocksServer srvr = new SocksServer(lba, 0, false);
ServerSocket ss = new ServerSocket(0, 0, lba)) {
srvr.addUser(USER, PASSWORD); srvr.addUser(USER, PASSWORD);
int serverPort = ss.getLocalPort(); int serverPort = ss.getLocalPort();
@ -64,9 +72,9 @@ public class SocksV4Test {
int proxyPort = srvr.getPort(); int proxyPort = srvr.getPort();
System.out.printf("Server port %d, Proxy port %d\n", serverPort, proxyPort); System.out.printf("Server port %d, Proxy port %d\n", serverPort, proxyPort);
Proxy sp = new Proxy(Proxy.Type.SOCKS, Proxy sp = new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress("localhost", proxyPort)); new InetSocketAddress(lba, proxyPort));
// Let's create an unresolved address // Let's create an unresolved address
InetSocketAddress ad = new InetSocketAddress("127.0.0.1", serverPort); InetSocketAddress ad = new InetSocketAddress(lba.getHostAddress(), serverPort);
try (Socket s = new Socket(sp)) { try (Socket s = new Socket(sp)) {
s.connect(ad, 10000); s.connect(ad, 10000);
int pp = s.getLocalPort(); int pp = s.getLocalPort();
@ -85,11 +93,12 @@ public class SocksV4Test {
// sanity before running the test // sanity before running the test
assertUnresolvableHost(HOSTNAME); assertUnresolvableHost(HOSTNAME);
InetAddress lba = InetAddress.getLoopbackAddress();
// Create a SOCKS V4 proxy // Create a SOCKS V4 proxy
try (SocksServer srvr = new SocksServer(0, true)) { try (SocksServer srvr = new SocksServer(lba, 0, true)) {
srvr.start(); srvr.start();
Proxy sp = new Proxy(Proxy.Type.SOCKS, Proxy sp = new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress("localhost", srvr.getPort())); new InetSocketAddress(lba, srvr.getPort()));
// Let's create an unresolved address // Let's create an unresolved address
InetSocketAddress ad = new InetSocketAddress(HOSTNAME, 1234); InetSocketAddress ad = new InetSocketAddress(HOSTNAME, 1234);
try (Socket s = new Socket(sp)) { try (Socket s = new Socket(sp)) {

View File

@ -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. * 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 6488669 6595324 6993490 * @bug 6488669 6595324 6993490
* @library /test/lib
* @modules jdk.httpserver * @modules jdk.httpserver
* @run main/othervm ChunkedErrorStream * @run main/othervm ChunkedErrorStream
* @summary Chunked ErrorStream tests * @summary Chunked ErrorStream tests
@ -32,6 +33,7 @@
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import com.sun.net.httpserver.*; import com.sun.net.httpserver.*;
import jdk.test.lib.net.URIBuilder;
/** /**
* Part 1: 6488669 * Part 1: 6488669
@ -94,16 +96,21 @@ public class ChunkedErrorStream
for (int times=0; times<3; times++) { for (int times=0; times<3; times++) {
HttpURLConnection uc = null; HttpURLConnection uc = null;
try { try {
InetSocketAddress address = httpServer.getAddress(); String path = "/test/";
String URLStr = "http://localhost:" + address.getPort() + "/test/";
if (times == 0) { if (times == 0) {
URLStr += "first"; path += "first";
} else { } else {
URLStr += "second"; path += "second";
} }
System.out.println("Trying " + URLStr); URL url = URIBuilder.newBuilder()
URL url = new URL(URLStr); .scheme("http")
.host(httpServer.getAddress().getAddress())
.port(httpServer.getAddress().getPort())
.path(path)
.toURLUnchecked();
System.out.println("Trying " + url);
uc = (HttpURLConnection)url.openConnection(); uc = (HttpURLConnection)url.openConnection();
uc.getInputStream(); uc.getInputStream();
@ -142,7 +149,9 @@ public class ChunkedErrorStream
* Http Server * Http Server
*/ */
void startHttpServer() throws IOException { void startHttpServer() throws IOException {
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); InetAddress lba = InetAddress.getLoopbackAddress();
InetSocketAddress addr = new InetSocketAddress(lba, 0);
httpServer = com.sun.net.httpserver.HttpServer.create(addr, 0);
// create HttpServer context // create HttpServer context
httpServer.createContext("/test/first", new FirstHandler()); httpServer.createContext("/test/first", new FirstHandler());