提交 0c3dd731 编写于 作者: K khazra

7150552: network test hangs [macosx]

Summary: Remove usage of test/sun/net/www/httptest
Reviewed-by: chegar
上级 99320688
......@@ -205,10 +205,6 @@ java/net/MulticastSocket/Test.java macosx-all
#7143960
java/net/DatagramSocket/SendDatagramToBadAddress.java macosx-all
# 7150552
sun/net/www/protocol/http/B6299712.java macosx-all
java/net/CookieHandler/CookieManagerTest.java macosx-all
############################################################################
# jdk_io
......
......@@ -24,21 +24,25 @@
/*
* @test
* @summary Unit test for java.net.CookieManager
* @bug 6244040
* @library ../../../sun/net/www/httptest/
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
* @bug 6244040 7150552
* @run main/othervm -ea CookieManagerTest
* @author Edward Wang
*/
import java.net.*;
import java.util.*;
import java.io.*;
import sun.net.www.MessageHeader;
import com.sun.net.httpserver.*;
import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URL;
public class CookieManagerTest {
static CookieHttpTransaction httpTrans;
static TestHttpServer server;
static CookieTransactionHandler httpTrans;
static HttpServer server;
public static void main(String[] args) throws Exception {
startHttpServer();
......@@ -49,41 +53,48 @@ public class CookieManagerTest {
}
}
public static void startHttpServer() {
try {
httpTrans = new CookieHttpTransaction();
server = new TestHttpServer(httpTrans, 1, 1, 0);
} catch (IOException e) {
e.printStackTrace();
}
public static void startHttpServer() throws IOException {
httpTrans = new CookieTransactionHandler();
server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext("/", httpTrans);
server.start();
}
public static void makeHttpCall() {
public static void makeHttpCall() throws IOException {
try {
System.out.println("http server listen on: " + server.getLocalPort());
System.out.println("http server listenining on: "
+ server.getAddress().getPort());
// install CookieManager to use
CookieHandler.setDefault(new CookieManager());
for (int i = 0; i < CookieHttpTransaction.testCount; i++) {
System.out.println("====== CookieManager test " + (i+1) + " ======");
((CookieManager)CookieHandler.getDefault()).setCookiePolicy(CookieHttpTransaction.testPolicies[i]);
((CookieManager)CookieHandler.getDefault()).getCookieStore().removeAll();
URL url = new URL("http" , InetAddress.getLocalHost().getHostAddress(),
server.getLocalPort(), CookieHttpTransaction.testCases[i][0].serverPath);
for (int i = 0; i < CookieTransactionHandler.testCount; i++) {
System.out.println("====== CookieManager test " + (i+1)
+ " ======");
((CookieManager)CookieHandler.getDefault())
.setCookiePolicy(CookieTransactionHandler.testPolicies[i]);
((CookieManager)CookieHandler.getDefault())
.getCookieStore().removeAll();
URL url = new URL("http" ,
InetAddress.getLocalHost().getHostAddress(),
server.getAddress().getPort(),
CookieTransactionHandler.testCases[i][0]
.serverPath);
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
uc.getResponseCode();
uc.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
server.terminate();
server.stop(0);
}
}
}
class CookieHttpTransaction implements HttpCallback {
class CookieTransactionHandler implements HttpHandler {
private int testcaseDone = 0;
private int testDone = 0;
public static boolean badRequest = false;
// the main test control logic will also loop exactly this number
// to send http request
......@@ -91,6 +102,47 @@ class CookieHttpTransaction implements HttpCallback {
private String localHostAddr = "127.0.0.1";
@Override
public void handle(HttpExchange exchange) throws IOException {
if (testDone < testCases[testcaseDone].length) {
// still have other tests to run,
// check the Cookie header and then redirect it
if (testDone > 0) checkRequest(exchange.getRequestHeaders());
exchange.getResponseHeaders().add("Location",
testCases[testcaseDone][testDone].serverPath);
exchange.getResponseHeaders()
.add(testCases[testcaseDone][testDone].headerToken,
testCases[testcaseDone][testDone].cookieToSend);
exchange.sendResponseHeaders(302, -1);
testDone++;
} else {
// the last test of this test case
if (testDone > 0) checkRequest(exchange.getRequestHeaders());
testcaseDone++;
testDone = 0;
exchange.sendResponseHeaders(200, -1);
}
exchange.close();
}
private void checkRequest(Headers hdrs) {
assert testDone > 0;
String cookieHeader = hdrs.getFirst("Cookie");
if (cookieHeader != null &&
cookieHeader
.equalsIgnoreCase(testCases[testcaseDone][testDone-1]
.cookieToRecv))
{
System.out.printf("%15s %s\n", "PASSED:", cookieHeader);
} else {
System.out.printf("%15s %s\n", "FAILED:", cookieHeader);
System.out.printf("%15s %s\n\n", "should be:",
testCases[testcaseDone][testDone-1].cookieToRecv);
badRequest = true;
}
}
// test cases
public static class CookieTestCase {
public String headerToken;
......@@ -106,13 +158,17 @@ class CookieHttpTransaction implements HttpCallback {
}
};
//
// these two must match each other, i.e. testCases.length == testPolicies.length
//
public static CookieTestCase[][] testCases = null; // the test cases to run; each test case may contain multiple roundtrips
public static CookiePolicy[] testPolicies = null; // indicates what CookiePolicy to use with each test cases
/*
* these two must match each other,
* i.e. testCases.length == testPolicies.length
*/
// the test cases to run; each test case may contain multiple roundtrips
public static CookieTestCase[][] testCases = null;
// indicates what CookiePolicy to use with each test cases
public static CookiePolicy[] testPolicies = null;
CookieHttpTransaction() {
CookieTransactionHandler() {
testCases = new CookieTestCase[testCount][];
testPolicies = new CookiePolicy[testCount];
......@@ -126,7 +182,9 @@ class CookieHttpTransaction implements HttpCallback {
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
testCases[count++] = new CookieTestCase[]{
new CookieTestCase("Set-Cookie",
"CUSTOMER=WILE:BOB; path=/; expires=Sat, 09-Nov-2030 23:12:40 GMT;" + "domain=." + localHostAddr,
"CUSTOMER=WILE:BOB; " +
"path=/; expires=Sat, 09-Nov-2030 23:12:40 GMT;" + "domain=." +
localHostAddr,
"CUSTOMER=WILE:BOB",
"/"
),
......@@ -172,12 +230,17 @@ class CookieHttpTransaction implements HttpCallback {
),
new CookieTestCase("Set-Cookie2",
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\";" + "domain=." + localHostAddr,
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." +
localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";"
+ "$Domain=\"." + localHostAddr + "\"",
"/acme/pickitem"
),
new CookieTestCase("Set-Cookie2",
"Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr +
"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"."
+ localHostAddr + "\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";" +
"$Domain=\"." + localHostAddr + "\"",
"/acme/shipping"
)
};
......@@ -191,8 +254,11 @@ class CookieHttpTransaction implements HttpCallback {
"/acme/ammo"
),
new CookieTestCase("Set-Cookie2",
"Part_Number=\"Riding_Rocket_0023\"; Version=\"1\"; Path=\"/acme/ammo\";" + "domain=." + localHostAddr,
"$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\";$Path=\"/acme/ammo\";$Domain=\"." + localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
"Part_Number=\"Riding_Rocket_0023\"; Version=\"1\"; Path=\"/acme/ammo\";" + "domain=."
+ localHostAddr,
"$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\";$Path=\"/acme/ammo\";$Domain=\"."
+ localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";"
+ "$Domain=\"." + localHostAddr + "\"",
"/acme/ammo"
),
new CookieTestCase("",
......@@ -228,60 +294,19 @@ class CookieHttpTransaction implements HttpCallback {
),
new CookieTestCase("Set-Cookie2",
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"",
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +
"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
"/acme/pickitem"
),
new CookieTestCase("Set-Cookie2",
"Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"",
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +
"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +
"; Shipping=\"FedEx\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
"/acme/shipping"
)
};
assert count == testCount;
}
private int testcaseDone = 0;
private int testDone = 0;
/*
* Our http server which is conducted by testCases array
*/
public void request(HttpTransaction trans) {
try {
if (testDone < testCases[testcaseDone].length) {
// still have other tests to run,
// check the Cookie header and then redirect it
if (testDone > 0) checkResquest(trans);
trans.addResponseHeader("Location", testCases[testcaseDone][testDone].serverPath);
trans.addResponseHeader(testCases[testcaseDone][testDone].headerToken,
testCases[testcaseDone][testDone].cookieToSend);
testDone++;
trans.sendResponse(302, "Moved Temporarily");
} else {
// the last test of this test case
if (testDone > 0) checkResquest(trans);
testcaseDone++;
testDone = 0;
trans.sendResponse(200, "OK");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void checkResquest(HttpTransaction trans) {
String cookieHeader = null;
assert testDone > 0;
cookieHeader = trans.getRequestHeader("Cookie");
if (cookieHeader != null &&
cookieHeader.equalsIgnoreCase(testCases[testcaseDone][testDone-1].cookieToRecv))
{
System.out.printf("%15s %s\n", "PASSED:", cookieHeader);
} else {
System.out.printf("%15s %s\n", "FAILED:", cookieHeader);
System.out.printf("%15s %s\n\n", "should be:", testCases[testcaseDone][testDone-1].cookieToRecv);
badRequest = true;
}
}
}
......@@ -23,33 +23,33 @@
/*
* @test
* @bug 6299712
* @library ../../httptest/
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
* @bug 6299712 7150552
* @run main/othervm B6299712
* @summary NullPointerException in sun.net.www.protocol.http.HttpURLConnection.followRedirect
*/
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.net.*;
import java.io.*;
import java.util.*;
/*
* Test Description:
* - main thread run as a http client
* - another thread runs a http server, which redirect the first call to "/redirect"
* and return '200 OK' for the successive call
* - a global ResponseCache instance is installed, which return DeployCacheResponse
* for url ends with "/redirect", i.e. the url redirected to by our simple http server,
* and null for other url.
* - main thread is run as a http client
* - another thread runs an http server, which redirects calls to "/" to
* "/redirect" and returns '200 OK' for the successive call
* - a global ResponseCache instance is installed, which returns DeployCacheResponse
* for urls that end with "/redirect", i.e. the url redirected to by our simple http server,
* and null for other urls.
* - the whole result is that the first call will be served by our simple
* http server and is redirected to "/redirect". The successive call will be done
* automatically by HttpURLConnection, which will be served by DeployCacheResponse.
* The NPE will be thrown on the second round if the bug is there.
*/
public class B6299712 {
static SimpleHttpTransaction httpTrans;
static TestHttpServer server;
static HttpServer server;
public static void main(String[] args) throws Exception {
ResponseCache.setDefault(new DeployCacheHandler());
......@@ -58,123 +58,119 @@ public class B6299712 {
makeHttpCall();
}
public static void startHttpServer() {
try {
httpTrans = new SimpleHttpTransaction();
server = new TestHttpServer(httpTrans, 1, 10, 0);
} catch (IOException e) {
e.printStackTrace();
}
public static void startHttpServer() throws IOException {
server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext("/", new DefaultHandler());
server.createContext("/redirect", new RedirectHandler());
server.start();
}
public static void makeHttpCall() {
public static void makeHttpCall() throws IOException {
try {
System.out.println("http server listen on: " + server.getLocalPort());
URL url = new URL("http" , InetAddress.getLocalHost().getHostAddress(),
server.getLocalPort(), "/");
System.out.println("http server listen on: "
+ server.getAddress().getPort());
URL url = new URL("http",
InetAddress.getLocalHost().getHostAddress(),
server.getAddress().getPort(), "/");
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
System.out.println(uc.getResponseCode());
} catch (IOException e) {
e.printStackTrace();
if (uc.getResponseCode() != 200)
throw new RuntimeException("Expected Response Code was 200,"
+ "received: " + uc.getResponseCode());
uc.disconnect();
} finally {
server.terminate();
server.stop(0);
}
}
}
class SimpleHttpTransaction implements HttpCallback {
/*
* Our http server which simply redirect first call
*/
public void request(HttpTransaction trans) {
try {
String path = trans.getRequestURI().getPath();
if (path.equals("/")) {
// the first call, redirect it
String location = "/redirect";
trans.addResponseHeader("Location", location);
trans.sendResponse(302, "Moved Temporarily");
} else {
// the second call
trans.sendResponse(200, "OK");
}
} catch (Exception e) {
e.printStackTrace();
static class RedirectHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(200, -1);
exchange.close();
}
}
}
class DeployCacheHandler extends java.net.ResponseCache {
private boolean inCacheHandler = false;
private boolean _downloading = false;
static class DefaultHandler implements HttpHandler {
public synchronized CacheResponse get(final URI uri, String rqstMethod,
Map requestHeaders) throws IOException {
System.out.println("get!!!: " + uri);
try {
@Override
public void handle(HttpExchange exchange) throws IOException {
exchange.getResponseHeaders().add("Location", "/redirect");
exchange.sendResponseHeaders(302, -1);
exchange.close();
}
}
static class DeployCacheHandler extends java.net.ResponseCache {
public synchronized CacheResponse get(final URI uri, String rqstMethod,
Map<String, List<String>> requestHeaders) throws IOException
{
System.out.println("get!!!: " + uri);
if (!uri.toString().endsWith("redirect")) {
return null;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Serving request from cache");
return new DeployCacheResponse(new EmptyInputStream(),
new HashMap<String, List<String>>());
}
return new DeployCacheResponse(new EmptyInputStream(), new HashMap());
}
public synchronized CacheRequest put(URI uri, URLConnection conn)
throws IOException {
URL url = uri.toURL();
return new DeployCacheRequest(url, conn);
public synchronized CacheRequest put(URI uri, URLConnection conn)
throws IOException
{
URL url = uri.toURL();
return new DeployCacheRequest(url, conn);
}
}
}
class DeployCacheRequest extends java.net.CacheRequest {
static class DeployCacheRequest extends java.net.CacheRequest {
private URL _url;
private URLConnection _conn;
private boolean _downloading = false;
private URL _url;
private URLConnection _conn;
DeployCacheRequest(URL url, URLConnection conn) {
_url = url;
_conn = conn;
}
DeployCacheRequest(URL url, URLConnection conn) {
_url = url;
_conn = conn;
}
public void abort() {
public void abort() {
}
}
public OutputStream getBody() throws IOException {
public OutputStream getBody() throws IOException {
return null;
return null;
}
}
}
class DeployCacheResponse extends java.net.CacheResponse {
protected InputStream is;
protected Map headers;
static class DeployCacheResponse extends java.net.CacheResponse {
protected InputStream is;
protected Map<String, List<String>> headers;
DeployCacheResponse(InputStream is, Map headers) {
this.is = is;
this.headers = headers;
}
DeployCacheResponse(InputStream is, Map<String, List<String>> headers) {
this.is = is;
this.headers = headers;
}
public InputStream getBody() throws IOException {
return is;
}
public InputStream getBody() throws IOException {
return is;
}
public Map getHeaders() throws IOException {
return headers;
public Map<String, List<String>> getHeaders() throws IOException {
List<String> val = new ArrayList<>();
val.add("HTTP/1.1 200 OK");
headers.put(null, val);
return headers;
}
}
}
class EmptyInputStream extends InputStream {
public EmptyInputStream() {
}
static class EmptyInputStream extends InputStream {
public int read()
throws IOException {
return -1;
public int read() throws IOException {
return -1;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册