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