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:
parent
2fded1c480
commit
96bb069a2d
@ -27,6 +27,7 @@
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.net.SimpleSSLContext
|
||||
* @run main/othervm Test1
|
||||
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test1
|
||||
* @run main/othervm -Djdk.net.usePlainSocketImpl Test1
|
||||
* @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test1
|
||||
* @run main/othervm -Dsun.net.httpserver.nodelay=true Test1
|
||||
@ -40,6 +41,7 @@ import java.io.*;
|
||||
import java.net.*;
|
||||
import javax.net.ssl.*;
|
||||
import jdk.test.lib.net.SimpleSSLContext;
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
/* basic http/s connectivity test
|
||||
* Tests:
|
||||
@ -64,7 +66,8 @@ public class Test1 extends Test {
|
||||
try {
|
||||
String root = System.getProperty ("test.src")+ "/docs";
|
||||
System.out.print ("Test1: ");
|
||||
InetSocketAddress addr = new InetSocketAddress (0);
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
InetSocketAddress addr = new InetSocketAddress (loopback, 0);
|
||||
s1 = HttpServer.create (addr, 0);
|
||||
if (s1 instanceof 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 {
|
||||
URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);
|
||||
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
|
||||
URL url = URIBuilder.newBuilder()
|
||||
.scheme(protocol)
|
||||
.loopback()
|
||||
.port(port)
|
||||
.path("/test1/"+f)
|
||||
.toURL();
|
||||
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
|
||||
if (urlc instanceof HttpsURLConnection) {
|
||||
HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
|
||||
urlcs.setHostnameVerifier (new HostnameVerifier () {
|
||||
|
@ -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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -26,6 +26,8 @@
|
||||
* @bug 6373555
|
||||
* @library /test/lib
|
||||
* @summary HTTP Server failing to answer client requests
|
||||
* @run main B6373555
|
||||
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6373555
|
||||
*/
|
||||
|
||||
import java.net.*;
|
||||
@ -105,7 +107,7 @@ public class B6373555 {
|
||||
.path("/test")
|
||||
.toURLUnchecked();
|
||||
System.out.println("URL: " + url);
|
||||
HttpURLConnection con = (HttpURLConnection)url.openConnection();
|
||||
HttpURLConnection con = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
|
||||
con.setDoOutput(true);
|
||||
con.setDoInput(true);
|
||||
con.setRequestMethod("POST");
|
||||
@ -148,7 +150,8 @@ public class B6373555 {
|
||||
|
||||
private static HttpServer createHttpServer(ExecutorService execs)
|
||||
throws Exception {
|
||||
InetSocketAddress inetAddress = new InetSocketAddress(0);
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
InetSocketAddress inetAddress = new InetSocketAddress(loopback, 0);
|
||||
HttpServer testServer = HttpServer.create(inetAddress, 15);
|
||||
testServer.setExecutor(execs);
|
||||
HttpContext context = testServer.createContext("/test");
|
||||
|
@ -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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -26,11 +26,14 @@
|
||||
* @library /test/lib
|
||||
* @bug 6401598
|
||||
* @summary new HttpServer cannot serve binary stream data
|
||||
* @run main B6401598
|
||||
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6401598
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.Proxy;
|
||||
import java.net.URL;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
@ -84,7 +87,8 @@ public class B6401598 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
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());
|
||||
exec = Executors.newFixedThreadPool(3);
|
||||
server.setExecutor(exec);
|
||||
@ -123,11 +127,9 @@ public class B6401598 {
|
||||
dis.close();
|
||||
}
|
||||
System.out.println ("Stopping");
|
||||
server.stop (1);
|
||||
exec.shutdown();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
throw new AssertionError("Unexpected exception: " + e, e);
|
||||
} finally {
|
||||
server.stop (1);
|
||||
exec.shutdown();
|
||||
}
|
||||
@ -137,7 +139,8 @@ public class B6401598 {
|
||||
|
||||
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.setReadTimeout(timeout);
|
||||
|
@ -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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -24,13 +24,17 @@
|
||||
/**
|
||||
* @test
|
||||
* @bug 6431193
|
||||
* @library /test/lib
|
||||
* @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.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.*;
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
import com.sun.net.httpserver.*;
|
||||
|
||||
@ -44,8 +48,8 @@ public class B6431193 {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
class MyHandler implements HttpHandler {
|
||||
public void handle(HttpExchange t) throws IOException {
|
||||
@ -64,7 +68,8 @@ public class B6431193 {
|
||||
|
||||
HttpServer server;
|
||||
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.setExecutor(null);
|
||||
@ -72,8 +77,13 @@ public class B6431193 {
|
||||
server.start();
|
||||
int port = server.getAddress().getPort();
|
||||
String s = "http://localhost:"+port+"/apps/foo";
|
||||
URL url = new URL (s);
|
||||
InputStream is = url.openStream();
|
||||
URL url = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.loopback()
|
||||
.port(port)
|
||||
.path("/apps/foo")
|
||||
.toURL();
|
||||
InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream();
|
||||
read (is);
|
||||
server.stop (1);
|
||||
if (error) {
|
||||
@ -81,9 +91,8 @@ public class B6431193 {
|
||||
}
|
||||
|
||||
}
|
||||
catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
catch (Exception e) {
|
||||
throw new AssertionError("Unexpected exception: " + e, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -25,6 +25,8 @@
|
||||
* @test
|
||||
* @bug 6433018
|
||||
* @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.*;
|
||||
@ -63,13 +65,14 @@ public class B6433018 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
CountDownLatch finished = new CountDownLatch(2);
|
||||
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);
|
||||
HttpContext ctx = server.createContext("/test", handler);
|
||||
|
||||
server.start();
|
||||
int port = server.getAddress().getPort();
|
||||
try (Socket s = new Socket("localhost", port);
|
||||
try (Socket s = new Socket(loopback, port);
|
||||
OutputStream os = s.getOutputStream()) {
|
||||
os.write(cmd.getBytes());
|
||||
finished.await(30, TimeUnit.SECONDS);
|
||||
|
@ -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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -24,7 +24,9 @@
|
||||
/**
|
||||
* @test
|
||||
* @bug 8211420
|
||||
* @library /test/lib
|
||||
* @run main/othervm B8211420
|
||||
* @run main/othervm -Djava.net.preferIPv6Addresses=true B8211420
|
||||
* @summary
|
||||
*/
|
||||
|
||||
@ -36,45 +38,53 @@ import java.util.logging.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
public class B8211420 {
|
||||
|
||||
public static void main (String[] args) throws Exception {
|
||||
Logger logger = Logger.getLogger ("com.sun.net.httpserver");
|
||||
public static void main(String[] args) throws Exception {
|
||||
Logger logger = Logger.getLogger("com.sun.net.httpserver");
|
||||
ConsoleHandler c = new ConsoleHandler();
|
||||
c.setLevel (Level.WARNING);
|
||||
logger.addHandler (c);
|
||||
logger.setLevel (Level.WARNING);
|
||||
c.setLevel(Level.WARNING);
|
||||
logger.addHandler(c);
|
||||
logger.setLevel(Level.WARNING);
|
||||
Handler handler = new Handler();
|
||||
InetSocketAddress addr = new InetSocketAddress (0);
|
||||
HttpServer server = HttpServer.create (addr, 0);
|
||||
HttpContext ctx = server.createContext ("/test", handler);
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
|
||||
HttpServer server = HttpServer.create(addr, 0);
|
||||
HttpContext ctx = server.createContext("/test", handler);
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
server.setExecutor (executor);
|
||||
server.start ();
|
||||
server.setExecutor(executor);
|
||||
server.start();
|
||||
|
||||
URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
|
||||
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
|
||||
URL url = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.host(server.getAddress().getAddress())
|
||||
.port(server.getAddress().getPort())
|
||||
.path("/test/foo.html")
|
||||
.toURL();
|
||||
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
|
||||
try {
|
||||
InputStream is = urlc.getInputStream();
|
||||
while (is.read()!= -1) ;
|
||||
is.close ();
|
||||
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)
|
||||
throw new RuntimeException("Content-length was present");
|
||||
|
||||
urlc = (HttpURLConnection)url.openConnection();
|
||||
urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
|
||||
is = urlc.getInputStream();
|
||||
while (is.read()!= -1) ;
|
||||
is.close ();
|
||||
is.close();
|
||||
if (urlc.getResponseCode() != 304) // expected for 2nd test
|
||||
throw new RuntimeException("wrong response code");
|
||||
String clen = urlc.getHeaderField("Content-length");
|
||||
System.out.println ("Content-length = " + clen + " should be 99");
|
||||
System.out.println ("len = " + clen.length());
|
||||
System.out.println("Content-length = " + clen + " should be 99");
|
||||
System.out.println("len = " + clen.length());
|
||||
if (clen == null || !clen.equals("99"))
|
||||
throw new RuntimeException("Content-length not present or has wrong value");
|
||||
System.out.println ("OK");
|
||||
System.out.println("OK");
|
||||
} finally {
|
||||
server.stop(2);
|
||||
executor.shutdown();
|
||||
@ -91,7 +101,7 @@ public class B8211420 {
|
||||
InputStream is = t.getRequestBody();
|
||||
Headers map = t.getRequestHeaders();
|
||||
Headers rmap = t.getResponseHeaders();
|
||||
while (is.read () != -1) ;
|
||||
while (is.read() != -1) ;
|
||||
is.close();
|
||||
if (invocation++ == 1) {
|
||||
// send a 204 response with no body
|
||||
|
@ -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.
|
||||
*
|
||||
* 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"
|
||||
* @modules jdk.httpserver
|
||||
* @run main/othervm Test2
|
||||
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test2
|
||||
*/
|
||||
|
||||
import com.sun.net.httpserver.*;
|
||||
@ -69,11 +70,12 @@ public class Test2 {
|
||||
|
||||
public static void main (String[] args) throws Exception {
|
||||
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);
|
||||
port = server.getAddress().getPort();
|
||||
HttpContext ctx = server.createContext ("/test", handler);
|
||||
System.out.println ("Server: " + server.getAddress().getPort());
|
||||
System.out.println ("Server: " + server.getAddress());
|
||||
ResponseCache.setDefault(new Cache());
|
||||
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
@ -95,7 +97,7 @@ public class Test2 {
|
||||
.toURLUnchecked();
|
||||
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.setInstanceFollowRedirects(true);
|
||||
System.out.println(urlc.getResponseCode());
|
||||
|
@ -26,6 +26,8 @@
|
||||
* @bug 4145315
|
||||
* @library /test/lib
|
||||
* @summary Test a read from nonexistant URL
|
||||
* @run main GetContent
|
||||
* @run main/othervm -Djava.net.preferIPv6Addresses GetContent
|
||||
*/
|
||||
|
||||
import java.net.*;
|
||||
@ -37,8 +39,7 @@ public class GetContent implements Runnable {
|
||||
ServerSocket ss;
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
Socket s = ss.accept();
|
||||
try (Socket s = ss.accept()) {
|
||||
s.setTcpNoDelay(true);
|
||||
|
||||
PrintStream out = new PrintStream(
|
||||
@ -57,7 +58,6 @@ public class GetContent implements Runnable {
|
||||
// client get error and re-establish connection
|
||||
Thread.sleep(2000);
|
||||
|
||||
s.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
@ -66,8 +66,10 @@ public class GetContent implements Runnable {
|
||||
}
|
||||
|
||||
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);
|
||||
thr.start();
|
||||
|
||||
@ -79,7 +81,8 @@ public class GetContent implements Runnable {
|
||||
.port(ss.getLocalPort())
|
||||
.path("/no-such-name")
|
||||
.toURL();
|
||||
Object obj = url.getContent();
|
||||
Object obj = url.openConnection(Proxy.NO_PROXY)
|
||||
.getContent();
|
||||
InputStream in = (InputStream) obj;
|
||||
byte buff[] = new byte[200];
|
||||
int len = in.read(buff);
|
||||
|
@ -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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -25,9 +25,15 @@
|
||||
* @test
|
||||
* @bug 6672144 8050983
|
||||
* @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.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.URL;
|
||||
import java.io.IOException;
|
||||
@ -35,6 +41,8 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import static java.lang.System.out;
|
||||
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
public class StreamingRetry implements Runnable {
|
||||
static final int ACCEPT_TIMEOUT = 20 * 1000; // 20 seconds
|
||||
volatile ServerSocket ss;
|
||||
@ -55,7 +63,8 @@ public class StreamingRetry implements Runnable {
|
||||
}
|
||||
|
||||
void test(String method) throws Exception {
|
||||
ss = new ServerSocket(0);
|
||||
ss = new ServerSocket();
|
||||
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
|
||||
ss.setSoTimeout(ACCEPT_TIMEOUT);
|
||||
int port = ss.getLocalPort();
|
||||
|
||||
@ -63,8 +72,13 @@ public class StreamingRetry implements Runnable {
|
||||
otherThread.start();
|
||||
|
||||
try {
|
||||
URL url = new URL("http://localhost:" + port + "/");
|
||||
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
|
||||
URL url = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.host(ss.getInetAddress())
|
||||
.port(port)
|
||||
.path("/")
|
||||
.toURL();
|
||||
HttpURLConnection uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
|
||||
uc.setDoOutput(true);
|
||||
if (method != null)
|
||||
uc.setRequestMethod(method);
|
||||
|
@ -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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -26,6 +26,13 @@
|
||||
* @bug 4432213
|
||||
* @modules java.base/sun.net.www
|
||||
* @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
|
||||
*/
|
||||
|
||||
@ -57,13 +64,15 @@ class DigestServer extends Thread {
|
||||
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
|
||||
"Server: Apache/1.3.14 (Unix)\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\nHelloWorld2\r\n"+
|
||||
"B\r\nHelloWorld3\r\n"+
|
||||
"B\r\nHelloWorld4\r\n"+
|
||||
"B\r\nHelloWorld5\r\n"+
|
||||
"0\r\n"+
|
||||
"0\r\n\r\n";
|
||||
String authInfo =
|
||||
"Authentication-Info: ";
|
||||
|
||||
DigestServer (ServerSocket y) {
|
||||
@ -84,7 +93,7 @@ class DigestServer extends Thread {
|
||||
s1 = s.accept ();
|
||||
is = s1.getInputStream ();
|
||||
os = s1.getOutputStream ();
|
||||
is.read ();
|
||||
//is.read ();
|
||||
// need to get the cnonce out of the response
|
||||
MessageHeader header = new MessageHeader (is);
|
||||
String raw = header.findValue ("Authorization");
|
||||
@ -92,7 +101,7 @@ class DigestServer extends Thread {
|
||||
String cnonce = parser.findValue ("cnonce");
|
||||
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());
|
||||
Thread.sleep (2000);
|
||||
s1.close ();
|
||||
@ -193,6 +202,9 @@ class DigestServer extends Thread {
|
||||
|
||||
public class DigestTest {
|
||||
|
||||
static final boolean SUCCEED =
|
||||
Boolean.parseBoolean(System.getProperty("test.succeed", "false"));
|
||||
|
||||
static class MyAuthenticator extends Authenticator {
|
||||
public MyAuthenticator () {
|
||||
super ();
|
||||
@ -200,7 +212,9 @@ public class DigestTest {
|
||||
|
||||
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;
|
||||
ServerSocket sock;
|
||||
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
try {
|
||||
sock = new ServerSocket (0);
|
||||
port = sock.getLocalPort ();
|
||||
sock = new ServerSocket();
|
||||
sock.bind(new InetSocketAddress(loopback, 0));
|
||||
port = sock.getLocalPort();
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println ("Exception: " + e);
|
||||
return;
|
||||
throw e;
|
||||
}
|
||||
|
||||
server = new DigestServer(sock);
|
||||
server.start ();
|
||||
boolean passed = false;
|
||||
ProtocolException exception = null;
|
||||
|
||||
try {
|
||||
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);
|
||||
java.net.URLConnection conURL = url.openConnection();
|
||||
java.net.URLConnection conURL = url.openConnection(Proxy.NO_PROXY);
|
||||
|
||||
InputStream in = conURL.getInputStream();
|
||||
while (in.read () != -1) {}
|
||||
in.close ();
|
||||
if (SUCCEED) passed = true;
|
||||
} catch(ProtocolException e) {
|
||||
passed = true;
|
||||
exception = e;
|
||||
if (!SUCCEED) passed = true;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -28,6 +28,7 @@
|
||||
* @library ../../httptest/
|
||||
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
|
||||
* @run main RelativeRedirect
|
||||
* @run main/othervm -Djava.net.preferIPv6Addresses=true RelativeRedirect
|
||||
* @summary URLConnection cannot handle redirects
|
||||
*/
|
||||
|
||||
@ -58,7 +59,7 @@ public class RelativeRedirect implements HttpCallback {
|
||||
|
||||
void secondReply (HttpTransaction req) throws IOException {
|
||||
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.sendResponse (200, "Ok");
|
||||
} 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 {
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
MyAuthenticator auth = new MyAuthenticator ();
|
||||
Authenticator.setDefault (auth);
|
||||
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());
|
||||
URL url = new URL("http://localhost:"+server.getLocalPort());
|
||||
URL url = new URL("http://" + authority(server.getLocalPort()));
|
||||
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 ();
|
||||
is.close();
|
||||
} catch (Exception e) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user