8224865: Replace wildcard address with loopback or local host in tests - part 13

Fixes intermittent failures observed in some network tests

Reviewed-by: chegar
This commit is contained in:
Daniel Fuchs 2019-06-10 11:17:57 +01:00
parent 2fded1c480
commit 96bb069a2d
11 changed files with 170 additions and 77 deletions

View File

@ -27,6 +27,7 @@
* @library /test/lib * @library /test/lib
* @build jdk.test.lib.net.SimpleSSLContext * @build jdk.test.lib.net.SimpleSSLContext
* @run main/othervm Test1 * @run main/othervm Test1
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test1
* @run main/othervm -Djdk.net.usePlainSocketImpl Test1 * @run main/othervm -Djdk.net.usePlainSocketImpl Test1
* @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test1 * @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test1
* @run main/othervm -Dsun.net.httpserver.nodelay=true Test1 * @run main/othervm -Dsun.net.httpserver.nodelay=true Test1
@ -40,6 +41,7 @@ import java.io.*;
import java.net.*; import java.net.*;
import javax.net.ssl.*; import javax.net.ssl.*;
import jdk.test.lib.net.SimpleSSLContext; import jdk.test.lib.net.SimpleSSLContext;
import jdk.test.lib.net.URIBuilder;
/* basic http/s connectivity test /* basic http/s connectivity test
* Tests: * Tests:
@ -64,7 +66,8 @@ public class Test1 extends Test {
try { try {
String root = System.getProperty ("test.src")+ "/docs"; String root = System.getProperty ("test.src")+ "/docs";
System.out.print ("Test1: "); System.out.print ("Test1: ");
InetSocketAddress addr = new InetSocketAddress (0); InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress addr = new InetSocketAddress (loopback, 0);
s1 = HttpServer.create (addr, 0); s1 = HttpServer.create (addr, 0);
if (s1 instanceof HttpsServer) { if (s1 instanceof HttpsServer) {
throw new RuntimeException ("should not be httpsserver"); throw new RuntimeException ("should not be httpsserver");
@ -104,8 +107,13 @@ public class Test1 extends Test {
} }
static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception { static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f); URL url = URIBuilder.newBuilder()
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); .scheme(protocol)
.loopback()
.port(port)
.path("/test1/"+f)
.toURL();
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
if (urlc instanceof HttpsURLConnection) { if (urlc instanceof HttpsURLConnection) {
HttpsURLConnection urlcs = (HttpsURLConnection) urlc; HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
urlcs.setHostnameVerifier (new HostnameVerifier () { urlcs.setHostnameVerifier (new HostnameVerifier () {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, 2011, 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
@ -26,6 +26,8 @@
* @bug 6373555 * @bug 6373555
* @library /test/lib * @library /test/lib
* @summary HTTP Server failing to answer client requests * @summary HTTP Server failing to answer client requests
* @run main B6373555
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6373555
*/ */
import java.net.*; import java.net.*;
@ -105,7 +107,7 @@ public class B6373555 {
.path("/test") .path("/test")
.toURLUnchecked(); .toURLUnchecked();
System.out.println("URL: " + url); System.out.println("URL: " + url);
HttpURLConnection con = (HttpURLConnection)url.openConnection(); HttpURLConnection con = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
con.setDoOutput(true); con.setDoOutput(true);
con.setDoInput(true); con.setDoInput(true);
con.setRequestMethod("POST"); con.setRequestMethod("POST");
@ -148,7 +150,8 @@ public class B6373555 {
private static HttpServer createHttpServer(ExecutorService execs) private static HttpServer createHttpServer(ExecutorService execs)
throws Exception { throws Exception {
InetSocketAddress inetAddress = new InetSocketAddress(0); InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress inetAddress = new InetSocketAddress(loopback, 0);
HttpServer testServer = HttpServer.create(inetAddress, 15); HttpServer testServer = HttpServer.create(inetAddress, 15);
testServer.setExecutor(execs); testServer.setExecutor(execs);
HttpContext context = testServer.createContext("/test"); HttpContext context = testServer.createContext("/test");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, 2010, 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
@ -26,11 +26,14 @@
* @library /test/lib * @library /test/lib
* @bug 6401598 * @bug 6401598
* @summary new HttpServer cannot serve binary stream data * @summary new HttpServer cannot serve binary stream data
* @run main B6401598
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6401598
*/ */
import java.io.*; import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -84,7 +87,8 @@ public class B6401598 {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
server = HttpServer.create(new InetSocketAddress(0), 400); InetAddress loopback = InetAddress.getLoopbackAddress();
server = HttpServer.create(new InetSocketAddress(loopback, 0), 400);
server.createContext("/server/", new MyHandler()); server.createContext("/server/", new MyHandler());
exec = Executors.newFixedThreadPool(3); exec = Executors.newFixedThreadPool(3);
server.setExecutor(exec); server.setExecutor(exec);
@ -123,11 +127,9 @@ public class B6401598 {
dis.close(); dis.close();
} }
System.out.println ("Stopping"); System.out.println ("Stopping");
server.stop (1); } catch (Exception e) {
exec.shutdown(); throw new AssertionError("Unexpected exception: " + e, e);
} catch (IOException e) { } finally {
// TODO Auto-generated catch block
e.printStackTrace();
server.stop (1); server.stop (1);
exec.shutdown(); exec.shutdown();
} }
@ -137,7 +139,8 @@ public class B6401598 {
static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException { static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); HttpURLConnection httpURLConnection =
(HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
httpURLConnection.setConnectTimeout(40000); httpURLConnection.setConnectTimeout(40000);
httpURLConnection.setReadTimeout(timeout); httpURLConnection.setReadTimeout(timeout);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, 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,13 +24,17 @@
/** /**
* @test * @test
* @bug 6431193 * @bug 6431193
* @library /test/lib
* @summary The new HTTP server exits immediately * @summary The new HTTP server exits immediately
* @run main B6431193
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6431193
*/ */
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.*; import java.net.*;
import jdk.test.lib.net.URIBuilder;
import com.sun.net.httpserver.*; import com.sun.net.httpserver.*;
@ -44,8 +48,8 @@ public class B6431193 {
} }
/** /**
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
class MyHandler implements HttpHandler { class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException { public void handle(HttpExchange t) throws IOException {
@ -64,7 +68,8 @@ public class B6431193 {
HttpServer server; HttpServer server;
try { try {
server = HttpServer.create(new InetSocketAddress(0), 10); InetAddress loopback = InetAddress.getLoopbackAddress();
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
server.createContext("/apps", new MyHandler()); server.createContext("/apps", new MyHandler());
server.setExecutor(null); server.setExecutor(null);
@ -72,8 +77,13 @@ public class B6431193 {
server.start(); server.start();
int port = server.getAddress().getPort(); int port = server.getAddress().getPort();
String s = "http://localhost:"+port+"/apps/foo"; String s = "http://localhost:"+port+"/apps/foo";
URL url = new URL (s); URL url = URIBuilder.newBuilder()
InputStream is = url.openStream(); .scheme("http")
.loopback()
.port(port)
.path("/apps/foo")
.toURL();
InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream();
read (is); read (is);
server.stop (1); server.stop (1);
if (error) { if (error) {
@ -81,9 +91,8 @@ public class B6431193 {
} }
} }
catch (IOException e) { catch (Exception e) {
// TODO Auto-generated catch block throw new AssertionError("Unexpected exception: " + e, e);
e.printStackTrace();
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, 2013, 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
@ -25,6 +25,8 @@
* @test * @test
* @bug 6433018 * @bug 6433018
* @summary HTTP server sometimes sends bad request for browsers javascript * @summary HTTP server sometimes sends bad request for browsers javascript
* @run main B6433018
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6433018
*/ */
import com.sun.net.httpserver.*; import com.sun.net.httpserver.*;
@ -63,13 +65,14 @@ public class B6433018 {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
CountDownLatch finished = new CountDownLatch(2); CountDownLatch finished = new CountDownLatch(2);
Handler handler = new Handler(finished); Handler handler = new Handler(finished);
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);
server.start(); server.start();
int port = server.getAddress().getPort(); int port = server.getAddress().getPort();
try (Socket s = new Socket("localhost", port); try (Socket s = new Socket(loopback, port);
OutputStream os = s.getOutputStream()) { OutputStream os = s.getOutputStream()) {
os.write(cmd.getBytes()); os.write(cmd.getBytes());
finished.await(30, TimeUnit.SECONDS); finished.await(30, TimeUnit.SECONDS);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2018, 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,7 +24,9 @@
/** /**
* @test * @test
* @bug 8211420 * @bug 8211420
* @library /test/lib
* @run main/othervm B8211420 * @run main/othervm B8211420
* @run main/othervm -Djava.net.preferIPv6Addresses=true B8211420
* @summary * @summary
*/ */
@ -36,45 +38,53 @@ import java.util.logging.*;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import jdk.test.lib.net.URIBuilder;
public class B8211420 { public class B8211420 {
public static void main (String[] args) throws Exception { public static void main(String[] args) throws Exception {
Logger logger = Logger.getLogger ("com.sun.net.httpserver"); Logger logger = Logger.getLogger("com.sun.net.httpserver");
ConsoleHandler c = new ConsoleHandler(); ConsoleHandler c = new ConsoleHandler();
c.setLevel (Level.WARNING); c.setLevel(Level.WARNING);
logger.addHandler (c); logger.addHandler(c);
logger.setLevel (Level.WARNING); logger.setLevel(Level.WARNING);
Handler handler = new Handler(); Handler handler = new Handler();
InetSocketAddress addr = new InetSocketAddress (0); InetAddress loopback = InetAddress.getLoopbackAddress();
HttpServer server = HttpServer.create (addr, 0); InetSocketAddress addr = new InetSocketAddress(loopback, 0);
HttpContext ctx = server.createContext ("/test", handler); HttpServer server = HttpServer.create(addr, 0);
HttpContext ctx = server.createContext("/test", handler);
ExecutorService executor = Executors.newCachedThreadPool(); ExecutorService executor = Executors.newCachedThreadPool();
server.setExecutor (executor); server.setExecutor(executor);
server.start (); server.start();
URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html"); URL url = URIBuilder.newBuilder()
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); .scheme("http")
.host(server.getAddress().getAddress())
.port(server.getAddress().getPort())
.path("/test/foo.html")
.toURL();
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
try { try {
InputStream is = urlc.getInputStream(); InputStream is = urlc.getInputStream();
while (is.read()!= -1) ; while (is.read()!= -1) ;
is.close (); is.close ();
String prop = urlc.getHeaderField("Content-length"); String prop = urlc.getHeaderField("Content-length");
System.out.println ("Content-length = " + prop + " should be null"); System.out.println("Content-length = " + prop + " should be null");
if (prop != null) if (prop != null)
throw new RuntimeException("Content-length was present"); throw new RuntimeException("Content-length was present");
urlc = (HttpURLConnection)url.openConnection(); urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
is = urlc.getInputStream(); is = urlc.getInputStream();
while (is.read()!= -1) ; while (is.read()!= -1) ;
is.close (); is.close();
if (urlc.getResponseCode() != 304) // expected for 2nd test if (urlc.getResponseCode() != 304) // expected for 2nd test
throw new RuntimeException("wrong response code"); throw new RuntimeException("wrong response code");
String clen = urlc.getHeaderField("Content-length"); String clen = urlc.getHeaderField("Content-length");
System.out.println ("Content-length = " + clen + " should be 99"); System.out.println("Content-length = " + clen + " should be 99");
System.out.println ("len = " + clen.length()); System.out.println("len = " + clen.length());
if (clen == null || !clen.equals("99")) if (clen == null || !clen.equals("99"))
throw new RuntimeException("Content-length not present or has wrong value"); throw new RuntimeException("Content-length not present or has wrong value");
System.out.println ("OK"); System.out.println("OK");
} finally { } finally {
server.stop(2); server.stop(2);
executor.shutdown(); executor.shutdown();
@ -91,7 +101,7 @@ public class B8211420 {
InputStream is = t.getRequestBody(); InputStream is = t.getRequestBody();
Headers map = t.getRequestHeaders(); Headers map = t.getRequestHeaders();
Headers rmap = t.getResponseHeaders(); Headers rmap = t.getResponseHeaders();
while (is.read () != -1) ; while (is.read() != -1) ;
is.close(); is.close();
if (invocation++ == 1) { if (invocation++ == 1) {
// send a 204 response with no body // send a 204 response with no body

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2014, 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 Check for CRL results in IllegalArgumentException "white space not allowed" * @summary Check for CRL results in IllegalArgumentException "white space not allowed"
* @modules jdk.httpserver * @modules jdk.httpserver
* @run main/othervm Test2 * @run main/othervm Test2
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test2
*/ */
import com.sun.net.httpserver.*; import com.sun.net.httpserver.*;
@ -69,11 +70,12 @@ public class Test2 {
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);
port = server.getAddress().getPort(); port = server.getAddress().getPort();
HttpContext ctx = server.createContext ("/test", handler); HttpContext ctx = server.createContext ("/test", handler);
System.out.println ("Server: " + server.getAddress().getPort()); System.out.println ("Server: " + server.getAddress());
ResponseCache.setDefault(new Cache()); ResponseCache.setDefault(new Cache());
ExecutorService executor = Executors.newCachedThreadPool(); ExecutorService executor = Executors.newCachedThreadPool();
@ -95,7 +97,7 @@ public class Test2 {
.toURLUnchecked(); .toURLUnchecked();
System.out.println("Redir URL: " + redir); System.out.println("Redir URL: " + redir);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
urlc.addRequestProperty("X-Foo", "bar"); urlc.addRequestProperty("X-Foo", "bar");
urlc.setInstanceFollowRedirects(true); urlc.setInstanceFollowRedirects(true);
System.out.println(urlc.getResponseCode()); System.out.println(urlc.getResponseCode());

View File

@ -26,6 +26,8 @@
* @bug 4145315 * @bug 4145315
* @library /test/lib * @library /test/lib
* @summary Test a read from nonexistant URL * @summary Test a read from nonexistant URL
* @run main GetContent
* @run main/othervm -Djava.net.preferIPv6Addresses GetContent
*/ */
import java.net.*; import java.net.*;
@ -37,8 +39,7 @@ public class GetContent implements Runnable {
ServerSocket ss; ServerSocket ss;
public void run() { public void run() {
try { try (Socket s = ss.accept()) {
Socket s = ss.accept();
s.setTcpNoDelay(true); s.setTcpNoDelay(true);
PrintStream out = new PrintStream( PrintStream out = new PrintStream(
@ -57,7 +58,6 @@ public class GetContent implements Runnable {
// client get error and re-establish connection // client get error and re-establish connection
Thread.sleep(2000); Thread.sleep(2000);
s.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
@ -66,8 +66,10 @@ public class GetContent implements Runnable {
} }
GetContent() throws Exception { GetContent() throws Exception {
InetAddress loopback = InetAddress.getLoopbackAddress();
ss = new ServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));
ss = new ServerSocket(0);
Thread thr = new Thread(this); Thread thr = new Thread(this);
thr.start(); thr.start();
@ -79,7 +81,8 @@ public class GetContent implements Runnable {
.port(ss.getLocalPort()) .port(ss.getLocalPort())
.path("/no-such-name") .path("/no-such-name")
.toURL(); .toURL();
Object obj = url.getContent(); Object obj = url.openConnection(Proxy.NO_PROXY)
.getContent();
InputStream in = (InputStream) obj; InputStream in = (InputStream) obj;
byte buff[] = new byte[200]; byte buff[] = new byte[200];
int len = in.read(buff); int len = in.read(buff);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2010, 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. * 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,9 +25,15 @@
* @test * @test
* @bug 6672144 8050983 * @bug 6672144 8050983
* @summary Do not retry failed request with a streaming body. * @summary Do not retry failed request with a streaming body.
* @library /test/lib
* @run main StreamingRetry
* @run main/othervm -Djava.net.preferIPv6Addresses=true StreamingRetry
*/ */
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.URL; import java.net.URL;
import java.io.IOException; import java.io.IOException;
@ -35,6 +41,8 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import static java.lang.System.out; import static java.lang.System.out;
import jdk.test.lib.net.URIBuilder;
public class StreamingRetry implements Runnable { public class StreamingRetry implements Runnable {
static final int ACCEPT_TIMEOUT = 20 * 1000; // 20 seconds static final int ACCEPT_TIMEOUT = 20 * 1000; // 20 seconds
volatile ServerSocket ss; volatile ServerSocket ss;
@ -55,7 +63,8 @@ public class StreamingRetry implements Runnable {
} }
void test(String method) throws Exception { void test(String method) throws Exception {
ss = new ServerSocket(0); ss = new ServerSocket();
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
ss.setSoTimeout(ACCEPT_TIMEOUT); ss.setSoTimeout(ACCEPT_TIMEOUT);
int port = ss.getLocalPort(); int port = ss.getLocalPort();
@ -63,8 +72,13 @@ public class StreamingRetry implements Runnable {
otherThread.start(); otherThread.start();
try { try {
URL url = new URL("http://localhost:" + port + "/"); URL url = URIBuilder.newBuilder()
HttpURLConnection uc = (HttpURLConnection) url.openConnection(); .scheme("http")
.host(ss.getInetAddress())
.port(port)
.path("/")
.toURL();
HttpURLConnection uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
uc.setDoOutput(true); uc.setDoOutput(true);
if (method != null) if (method != null)
uc.setRequestMethod(method); uc.setRequestMethod(method);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2010, 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,6 +26,13 @@
* @bug 4432213 * @bug 4432213
* @modules java.base/sun.net.www * @modules java.base/sun.net.www
* @run main/othervm -Dhttp.auth.digest.validateServer=true DigestTest * @run main/othervm -Dhttp.auth.digest.validateServer=true DigestTest
* @run main/othervm -Djava.net.preferIPv6Addresses=true
* -Dhttp.auth.digest.validateServer=true DigestTest
* @run main/othervm -Dhttp.auth.digest.validateServer=true
-Dtest.succeed=true DigestTest
* @run main/othervm -Djava.net.preferIPv6Addresses=true
* -Dhttp.auth.digest.validateServer=true
-Dtest.succeed=true DigestTest
* @summary Need to support Digest Authentication for Proxies * @summary Need to support Digest Authentication for Proxies
*/ */
@ -57,13 +64,15 @@ class DigestServer extends Thread {
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
"Server: Apache/1.3.14 (Unix)\r\n" + "Server: Apache/1.3.14 (Unix)\r\n" +
"Content-Type: text/html; charset=iso-8859-1\r\n" + "Content-Type: text/html; charset=iso-8859-1\r\n" +
"Transfer-encoding: chunked\r\n\r\n"+ "Transfer-encoding: chunked\r\n";
String body =
"B\r\nHelloWorld1\r\n"+ "B\r\nHelloWorld1\r\n"+
"B\r\nHelloWorld2\r\n"+ "B\r\nHelloWorld2\r\n"+
"B\r\nHelloWorld3\r\n"+ "B\r\nHelloWorld3\r\n"+
"B\r\nHelloWorld4\r\n"+ "B\r\nHelloWorld4\r\n"+
"B\r\nHelloWorld5\r\n"+ "B\r\nHelloWorld5\r\n"+
"0\r\n"+ "0\r\n\r\n";
String authInfo =
"Authentication-Info: "; "Authentication-Info: ";
DigestServer (ServerSocket y) { DigestServer (ServerSocket y) {
@ -84,7 +93,7 @@ class DigestServer extends Thread {
s1 = s.accept (); s1 = s.accept ();
is = s1.getInputStream (); is = s1.getInputStream ();
os = s1.getOutputStream (); os = s1.getOutputStream ();
is.read (); //is.read ();
// need to get the cnonce out of the response // need to get the cnonce out of the response
MessageHeader header = new MessageHeader (is); MessageHeader header = new MessageHeader (is);
String raw = header.findValue ("Authorization"); String raw = header.findValue ("Authorization");
@ -92,7 +101,7 @@ class DigestServer extends Thread {
String cnonce = parser.findValue ("cnonce"); String cnonce = parser.findValue ("cnonce");
String cnstring = parser.findValue ("nc"); String cnstring = parser.findValue ("nc");
String reply = reply2 + getAuthorization (uri, "GET", cnonce, cnstring) +"\r\n"; String reply = reply2 + authInfo + getAuthorization (uri, "GET", cnonce, cnstring) +"\r\n" + body;
os.write (reply.getBytes()); os.write (reply.getBytes());
Thread.sleep (2000); Thread.sleep (2000);
s1.close (); s1.close ();
@ -193,6 +202,9 @@ class DigestServer extends Thread {
public class DigestTest { public class DigestTest {
static final boolean SUCCEED =
Boolean.parseBoolean(System.getProperty("test.succeed", "false"));
static class MyAuthenticator extends Authenticator { static class MyAuthenticator extends Authenticator {
public MyAuthenticator () { public MyAuthenticator () {
super (); super ();
@ -200,7 +212,9 @@ public class DigestTest {
public PasswordAuthentication getPasswordAuthentication () public PasswordAuthentication getPasswordAuthentication ()
{ {
return (new PasswordAuthentication ("user", "Wrongpassword".toCharArray())); char[] passwd = SUCCEED ? DigestServer.passwd.clone()
: "Wrongpassword".toCharArray();
return new PasswordAuthentication("user", passwd);
} }
} }
@ -210,34 +224,47 @@ public class DigestTest {
DigestServer server; DigestServer server;
ServerSocket sock; ServerSocket sock;
InetAddress loopback = InetAddress.getLoopbackAddress();
try { try {
sock = new ServerSocket (0); sock = new ServerSocket();
port = sock.getLocalPort (); sock.bind(new InetSocketAddress(loopback, 0));
port = sock.getLocalPort();
} }
catch (Exception e) { catch (Exception e) {
System.out.println ("Exception: " + e); System.out.println ("Exception: " + e);
return; throw e;
} }
server = new DigestServer(sock); server = new DigestServer(sock);
server.start (); server.start ();
boolean passed = false; boolean passed = false;
ProtocolException exception = null;
try { try {
Authenticator.setDefault (new MyAuthenticator ()); Authenticator.setDefault (new MyAuthenticator ());
String s = "http://localhost:" + port + DigestServer.uri; String address = loopback.getHostAddress();
if (address.indexOf(':') > -1) address = "[" + address + "]";
String s = "http://" + address + ":" + port + DigestServer.uri;
URL url = new URL(s); URL url = new URL(s);
java.net.URLConnection conURL = url.openConnection(); java.net.URLConnection conURL = url.openConnection(Proxy.NO_PROXY);
InputStream in = conURL.getInputStream(); InputStream in = conURL.getInputStream();
while (in.read () != -1) {} while (in.read () != -1) {}
in.close (); in.close ();
if (SUCCEED) passed = true;
} catch(ProtocolException e) { } catch(ProtocolException e) {
passed = true; exception = e;
if (!SUCCEED) passed = true;
} }
if (!passed) { if (!passed) {
throw new RuntimeException ("Expected a ProtocolException from wrong password"); if (!SUCCEED) {
throw new RuntimeException("Expected a ProtocolException from wrong password");
} else {
assert exception != null;
throw new RuntimeException("Unexpected ProtocolException from correct password: "
+ exception, exception);
}
} }
} }
} }

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
@ -28,6 +28,7 @@
* @library ../../httptest/ * @library ../../httptest/
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
* @run main RelativeRedirect * @run main RelativeRedirect
* @run main/othervm -Djava.net.preferIPv6Addresses=true RelativeRedirect
* @summary URLConnection cannot handle redirects * @summary URLConnection cannot handle redirects
*/ */
@ -58,7 +59,7 @@ public class RelativeRedirect implements HttpCallback {
void secondReply (HttpTransaction req) throws IOException { void secondReply (HttpTransaction req) throws IOException {
if (req.getRequestURI().toString().equals("/redirect/file.html") && if (req.getRequestURI().toString().equals("/redirect/file.html") &&
req.getRequestHeader("Host").equals("localhost:"+server.getLocalPort())) { req.getRequestHeader("Host").equals(authority(server.getLocalPort()))) {
req.setResponseEntityBody ("Hello ."); req.setResponseEntityBody ("Hello .");
req.sendResponse (200, "Ok"); req.sendResponse (200, "Ok");
} else { } else {
@ -86,15 +87,25 @@ public class RelativeRedirect implements HttpCallback {
} }
} }
static String authority(int port) {
InetAddress loopback = InetAddress.getLoopbackAddress();
String hostaddr = loopback.getHostAddress();
if (hostaddr.indexOf(':') > -1) {
hostaddr = "[" + hostaddr + "]";
}
return hostaddr + ":" + port;
}
public static void main (String[] args) throws Exception { public static void main (String[] args) throws Exception {
InetAddress loopback = InetAddress.getLoopbackAddress();
MyAuthenticator auth = new MyAuthenticator (); MyAuthenticator auth = new MyAuthenticator ();
Authenticator.setDefault (auth); Authenticator.setDefault (auth);
try { try {
server = new TestHttpServer (new RelativeRedirect(), 1, 10, 0); server = new TestHttpServer (new RelativeRedirect(), 1, 10, loopback, 0);
System.out.println ("Server: listening on port: " + server.getLocalPort()); System.out.println ("Server: listening on port: " + server.getLocalPort());
URL url = new URL("http://localhost:"+server.getLocalPort()); URL url = new URL("http://" + authority(server.getLocalPort()));
System.out.println ("client opening connection to: " + url); System.out.println ("client opening connection to: " + url);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); HttpURLConnection urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);
InputStream is = urlc.getInputStream (); InputStream is = urlc.getInputStream ();
is.close(); is.close();
} catch (Exception e) { } catch (Exception e) {