7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
authorkhazra
Fri, 07 Jun 2013 10:59:41 -0700
changeset 18148 d641a1b31205
parent 18147 7b596c3ffcd3
child 18149 5997c9aedb0e
7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER Summary: Return false for null arguments in ACCEPT_ORIGINAL_SERVER#shouldAccept() Reviewed-by: chegar
jdk/src/share/classes/java/net/CookiePolicy.java
jdk/test/java/net/CookieHandler/CookieManagerTest.java
--- a/jdk/src/share/classes/java/net/CookiePolicy.java	Fri Jun 07 17:37:23 2013 +0900
+++ b/jdk/src/share/classes/java/net/CookiePolicy.java	Fri Jun 07 10:59:41 2013 -0700
@@ -59,6 +59,8 @@
      */
     public static final CookiePolicy ACCEPT_ORIGINAL_SERVER  = new CookiePolicy(){
         public boolean shouldAccept(URI uri, HttpCookie cookie) {
+            if (uri == null || cookie == null)
+                return false;
             return HttpCookie.domainMatches(cookie.getDomain(), uri.getHost());
         }
     };
--- a/jdk/test/java/net/CookieHandler/CookieManagerTest.java	Fri Jun 07 17:37:23 2013 +0900
+++ b/jdk/test/java/net/CookieHandler/CookieManagerTest.java	Fri Jun 07 10:59:41 2013 -0700
@@ -24,20 +24,14 @@
 /*
  * @test
  * @summary Unit test for java.net.CookieManager
- * @bug 6244040 7150552
+ * @bug 6244040 7150552 7051862
  * @run main/othervm -ea CookieManagerTest
  * @author Edward Wang
  */
 
 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;
+import java.net.*;
 
 public class CookieManagerTest {
 
@@ -51,15 +45,37 @@
         if (httpTrans.badRequest) {
             throw new RuntimeException("Test failed : bad cookie header");
         }
+        checkCookiePolicy();
     }
 
-    public static void startHttpServer() throws IOException {
+   public static void startHttpServer() throws IOException {
         httpTrans = new CookieTransactionHandler();
         server = HttpServer.create(new InetSocketAddress(0), 0);
         server.createContext("/", httpTrans);
         server.start();
     }
 
+    /*
+     * Checks if CookiePolicy.ACCEPT_ORIGINAL_SERVER#shouldAccept()
+     * returns false for null arguments
+     */
+    private static void checkCookiePolicy() throws Exception {
+        CookiePolicy cp = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
+        boolean retVal;
+        retVal = cp.shouldAccept(null, null);
+        checkValue(retVal);
+        retVal = cp.shouldAccept(null, new HttpCookie("CookieName", "CookieVal"));
+        checkValue(retVal);
+        retVal = cp.shouldAccept((new URL("http", "localhost", 2345, "/")).toURI(),
+                                  null);
+        checkValue(retVal);
+    }
+
+    private static void checkValue(boolean val) {
+        if (val)
+            throw new RuntimeException("Return value is not false!");
+    }
+
     public static void makeHttpCall() throws IOException {
         try {
             System.out.println("http server listenining on: "