8226825: Replace wildcard address with loopback or local host in tests - part 19

Replace use of wildcard by the loopback address, or possibly the local host address, wherever possible, to improve test stability.

Reviewed-by: chegar, bpb
This commit is contained in:
Julia Boes 2019-06-27 16:12:39 +01:00 committed by Daniel Fuchs
parent 7a1bd61849
commit c45f932cc5
6 changed files with 150 additions and 102 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, 2018, 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
@ -28,6 +28,7 @@
* @summary Test Socket.setSoLinger * @summary Test Socket.setSoLinger
* @run main SetSoLinger * @run main SetSoLinger
* @run main/othervm -Djava.net.preferIPv4Stack=true SetSoLinger * @run main/othervm -Djava.net.preferIPv4Stack=true SetSoLinger
* @run main/othervm -Djava.net.preferIPv6Addresses=true SetSoLinger
*/ */
import java.net.*; import java.net.*;
@ -41,7 +42,10 @@ public class SetSoLinger {
int value; int value;
InetAddress addr = InetAddress.getLocalHost(); InetAddress addr = InetAddress.getLocalHost();
ServerSocket ss = new ServerSocket(0); ServerSocket ss = new ServerSocket();
InetSocketAddress socketAddress = new InetSocketAddress(addr, 0);
ss.bind(socketAddress);
int port = ss.getLocalPort(); int port = ss.getLocalPort();
Socket s = new Socket(addr, port); Socket s = new Socket(addr, port);

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,18 +24,21 @@
/* /*
* @test * @test
* @bug 6358532 * @bug 6358532
* @library /test/lib
* @modules jdk.httpserver * @modules jdk.httpserver
* @run main/othervm AsyncDisconnect * @run main/othervm AsyncDisconnect
* @run main/othervm -Djava.net.preferIPv6Addresses=true AsyncDisconnect
* @summary HttpURLConnection.disconnect doesn't really do the job * @summary HttpURLConnection.disconnect doesn't really do the job
*/ */
import java.net.*; import java.net.*;
import java.util.*;
import java.io.*; import java.io.*;
import com.sun.net.httpserver.*; import com.sun.net.httpserver.*;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import jdk.test.lib.net.URIBuilder;
public class AsyncDisconnect implements Runnable public class AsyncDisconnect implements Runnable
{ {
com.sun.net.httpserver.HttpServer httpServer; com.sun.net.httpserver.HttpServer httpServer;
@ -43,27 +46,30 @@ public class AsyncDisconnect implements Runnable
ExecutorService executorService; ExecutorService executorService;
HttpURLConnection uc; HttpURLConnection uc;
public static void main(String[] args) { public static void main(String[] args) throws Exception {
new AsyncDisconnect(); new AsyncDisconnect();
} }
public AsyncDisconnect() { public AsyncDisconnect() throws Exception {
try { startHttpServer();
startHttpServer(); doClient();
doClient();
} catch (IOException ioe) {
System.err.println(ioe);
}
} }
void doClient() { void doClient() throws Exception {
Thread t = new Thread(this);
try { try {
InetSocketAddress address = httpServer.getAddress(); InetSocketAddress address = httpServer.getAddress();
URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/"); URL url = URIBuilder.newBuilder()
uc = (HttpURLConnection)url.openConnection(); .scheme("http")
.host(address.getAddress())
.port(address.getPort())
.path("/test/")
.toURL();
uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
// create a thread that will disconnect the connection // create a thread that will disconnect the connection
(new Thread(this)).start(); t.start();
uc.getInputStream(); uc.getInputStream();
@ -73,11 +79,11 @@ public class AsyncDisconnect implements Runnable
} catch (SocketException se) { } catch (SocketException se) {
// this is what we expect to happen and is OK. // this is what we expect to happen and is OK.
//System.out.println(se); //System.out.println(se);
} catch (IOException e) {
e.printStackTrace();
} finally { } finally {
httpServer.stop(1); httpServer.stop(1);
t.join();
executorService.shutdown(); executorService.shutdown();
} }
} }
@ -93,7 +99,9 @@ public class AsyncDisconnect implements Runnable
* Http Server * Http Server
*/ */
public void startHttpServer() throws IOException { public void startHttpServer() throws IOException {
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress address = new InetSocketAddress(loopback, 0);
httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);
httpHandler = new MyHandler(); httpHandler = new MyHandler();
HttpContext ctx = httpServer.createContext("/test/", httpHandler); HttpContext ctx = httpServer.createContext("/test/", httpHandler);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 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,7 +25,10 @@
* @test * @test
* @bug 6641309 * @bug 6641309
* @modules jdk.httpserver * @modules jdk.httpserver
* @summary Wrong Cookie separator used in HttpURLConnection * @library /test/lib
* @run main/othervm B6641309
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6641309
* @summary Wrong Cookie separator used in HttpURLConnection B6641309
*/ */
import java.net.*; import java.net.*;
@ -35,65 +38,65 @@ import com.sun.net.httpserver.*;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import jdk.test.lib.net.URIBuilder;
public class B6641309 public class B6641309
{ {
com.sun.net.httpserver.HttpServer httpServer; com.sun.net.httpserver.HttpServer httpServer;
ExecutorService executorService; ExecutorService executorService;
public static void main(String[] args) public static void main(String[] args) throws Exception {
{
new B6641309(); new B6641309();
} }
public B6641309() public B6641309() throws Exception {
{ startHttpServer();
try { doClient();
startHttpServer();
doClient();
} catch (IOException ioe) {
System.err.println(ioe);
}
} }
void doClient() { void doClient() throws Exception {
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
try { ProxySelector.setDefault(ProxySelector.of(null));
InetSocketAddress address = httpServer.getAddress();
// GET Request InetSocketAddress address = httpServer.getAddress();
URL url = new URL("http://localhost:" + address.getPort() + "/test/");
CookieHandler ch = CookieHandler.getDefault();
Map<String,List<String>> header = new HashMap<String,List<String>>();
List<String> values = new LinkedList<String>();
values.add("Test1Cookie=TEST1; path=/test/");
values.add("Test2Cookie=TEST2; path=/test/");
header.put("Set-Cookie", values);
// preload the CookieHandler with a cookie for our URL // GET Request
// so that it will be sent during the first request URL url = URIBuilder.newBuilder()
ch.put(url.toURI(), header); .scheme("http")
HttpURLConnection uc = (HttpURLConnection)url.openConnection(); .host(address.getAddress())
int resp = uc.getResponseCode(); .port(address.getPort())
if (resp != 200) .path("/test/")
throw new RuntimeException("Failed: Response code from GET is not 200"); .toURL();
System.out.println("Response code from GET = 200 OK"); CookieHandler ch = CookieHandler.getDefault();
Map<String,List<String>> header = new HashMap<String,List<String>>();
List<String> values = new LinkedList<String>();
values.add("Test1Cookie=TEST1; path=/test/");
values.add("Test2Cookie=TEST2; path=/test/");
header.put("Set-Cookie", values);
} catch (IOException e) { // preload the CookieHandler with a cookie for our URL
e.printStackTrace(); // so that it will be sent during the first request
} catch (URISyntaxException e) { ch.put(url.toURI(), header);
e.printStackTrace(); HttpURLConnection uc = (HttpURLConnection)url.openConnection();
} finally { int resp = uc.getResponseCode();
httpServer.stop(1); if (resp != 200) {
executorService.shutdown(); throw new RuntimeException("Failed: Response code from GET is not 200: "
+ resp);
} }
System.out.println("Response code from GET = 200 OK");
httpServer.stop(1);
executorService.shutdown();
} }
/** /**
* Http Server * Http Server
*/ */
public void startHttpServer() throws IOException { public void startHttpServer() throws IOException {
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress address = new InetSocketAddress(loopback, 0);
httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);
// create HttpServer context // create HttpServer context
HttpContext ctx = httpServer.createContext("/test/", new MyHandler()); HttpContext ctx = httpServer.createContext("/test/", new MyHandler());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 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,7 +25,10 @@
* @test * @test
* @bug 6660405 * @bug 6660405
* @modules jdk.httpserver * @modules jdk.httpserver
* @summary HttpURLConnection returns the wrong InputStream * @library /test/lib
* @run main/othervm B6660405
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6660405
* @summary HttpURLConnection returns the wrong InputStream B6660405
*/ */
import java.net.*; import java.net.*;
@ -34,6 +37,8 @@ import java.io.*;
import com.sun.net.httpserver.*; import com.sun.net.httpserver.*;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import jdk.test.lib.net.URIBuilder;
public class B6660405 public class B6660405
{ {
@ -72,7 +77,8 @@ public class B6660405
} }
@Override @Override
public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders)
throws IOException
{ {
if (uri.getPath().equals("/redirect/index.html")) { if (uri.getPath().equals("/redirect/index.html")) {
return new MyCacheResponse(); return new MyCacheResponse();
@ -88,53 +94,61 @@ public class B6660405
} }
public static void main(String[] args) public static void main(String[] args) throws Exception
{ {
new B6660405(); new B6660405();
} }
public B6660405() public B6660405() throws Exception {
{ startHttpServer();
try { doClient();
startHttpServer();
doClient();
} catch (IOException ioe) {
System.err.println(ioe);
}
} }
void doClient() { void doClient() throws Exception {
ResponseCache.setDefault(new MyResponseCache()); ResponseCache.setDefault(new MyResponseCache());
try { InetSocketAddress address = httpServer.getAddress();
InetSocketAddress address = httpServer.getAddress();
// GET Request // GET Request
URL url = new URL("http://localhost:" + address.getPort() + "/test/index.html"); URL url = URIBuilder.newBuilder()
HttpURLConnection uc = (HttpURLConnection)url.openConnection(); .scheme("http")
int code = uc.getResponseCode(); .host(address.getAddress())
System.err.println("response code = " + code); .port(address.getPort())
int l = uc.getContentLength(); .path("/test/index.html")
System.err.println("content-length = " + l); .toURL();
InputStream in = uc.getInputStream();
int i = 0; HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
// Read till end of stream int code = uc.getResponseCode();
do { System.err.println("response code = " + code);
i = in.read(); int l = uc.getContentLength();
} while (i != -1); System.err.println("content-length = " + l);
in.close(); if (l != 1024) {
} catch (IOException e) { throw new AssertionError("Bad content length: " + l);
throw new RuntimeException("Got the wrong InputStream after checking headers");
} finally {
httpServer.stop(1);
executorService.shutdown();
} }
InputStream in = uc.getInputStream();
int i = 0;
// Read till end of stream
do {
l--;
i = in.read();
} while (i != -1);
in.close();
if (l != -1) {
throw new AssertionError("Only " + (1024 - (l + 1))
+ " bytes read from stream.");
}
httpServer.stop(1);
executorService.shutdown();
} }
/** /**
* Http Server * Http Server
*/ */
public void startHttpServer() throws IOException { public void startHttpServer() throws IOException {
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress address = new InetSocketAddress(loopback,0);
httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);
// create HttpServer context // create HttpServer context
HttpContext ctx = httpServer.createContext("/test/", new MyHandler()); HttpContext ctx = httpServer.createContext("/test/", new MyHandler());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 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
@ -23,7 +23,9 @@
/** /**
* @test * @test
* @bug 6890349 * @bug 6890349
* @library /test/lib
* @run main/othervm B6890349 * @run main/othervm B6890349
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6890349
* @summary Light weight HTTP server * @summary Light weight HTTP server
*/ */
@ -34,7 +36,11 @@ public class B6890349 extends Thread {
public static final void main(String[] args) throws Exception { public static final void main(String[] args) throws Exception {
try { try {
ServerSocket server = new ServerSocket (0); ServerSocket server = new ServerSocket();
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress address = new InetSocketAddress(loopback, 0);
server.bind(address);
int port = server.getLocalPort(); int port = server.getLocalPort();
System.out.println ("listening on " + port); System.out.println ("listening on " + port);
B6890349 t = new B6890349 (server); B6890349 t = new B6890349 (server);
@ -44,11 +50,11 @@ public class B6890349 extends Thread {
port, port,
"/foo\nbar"); "/foo\nbar");
System.out.println("URL: " + u); System.out.println("URL: " + u);
HttpURLConnection urlc = (HttpURLConnection)u.openConnection (); HttpURLConnection urlc = (HttpURLConnection)u.openConnection(Proxy.NO_PROXY);
InputStream is = urlc.getInputStream(); InputStream is = urlc.getInputStream();
throw new RuntimeException ("Test failed"); throw new RuntimeException ("Test failed");
} catch (IOException e) { } catch (IOException e) {
System.out.println ("OK"); System.out.println ("Caught expected exception: " + e);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, 2001, 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,12 +24,16 @@
/* /*
* @test * @test
* @bug 4092605 * @bug 4092605
* @library /test/lib
* @run main/othervm Modified
* @run main/othervm -Djava.net.preferIPv6Addresses=true Modified
* @summary Test HttpURLConnection setIfModifiedSince * @summary Test HttpURLConnection setIfModifiedSince
* *
*/ */
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import jdk.test.lib.net.URIBuilder;
public class Modified implements Runnable { public class Modified implements Runnable {
@ -78,13 +82,22 @@ public class Modified implements Runnable {
Modified() throws Exception { Modified() throws Exception {
ss = new ServerSocket(0); InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress address = new InetSocketAddress(loopback, 0);
ss = new ServerSocket();
ss.bind(address);
int port = ss.getLocalPort();
Thread thr = new Thread(this); Thread thr = new Thread(this);
thr.start(); thr.start();
URL testURL = new URL("http://localhost:" + ss.getLocalPort() + URL testURL = URIBuilder.newBuilder()
"/index.html"); .scheme("http")
URLConnection URLConn = testURL.openConnection(); .host(loopback)
.port(port)
.path("/index.html")
.toURL();
URLConnection URLConn = testURL.openConnection(Proxy.NO_PROXY);
HttpURLConnection httpConn; HttpURLConnection httpConn;
if (URLConn instanceof HttpURLConnection) { if (URLConn instanceof HttpURLConnection) {