jdk/test/java/net/CookieHandler/CookieManagerTest.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 18148 d641a1b31205
child 38780 fa8bf2d62d36
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18148
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2924
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2924
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2924
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Unit test for java.net.CookieManager
18148
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    27
 * @bug 6244040 7150552 7051862
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/othervm -ea CookieManagerTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Edward Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    32
import com.sun.net.httpserver.*;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    33
import java.io.IOException;
18148
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    34
import java.net.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class CookieManagerTest {
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    37
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    38
    static CookieTransactionHandler httpTrans;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    39
    static HttpServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        startHttpServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        makeHttpCall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (httpTrans.badRequest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            throw new RuntimeException("Test failed : bad cookie header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
18148
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    48
        checkCookiePolicy();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
18148
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    51
   public static void startHttpServer() throws IOException {
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    52
        httpTrans = new CookieTransactionHandler();
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    53
        server = HttpServer.create(new InetSocketAddress(0), 0);
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    54
        server.createContext("/", httpTrans);
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    55
        server.start();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
18148
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    58
    /*
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    59
     * Checks if CookiePolicy.ACCEPT_ORIGINAL_SERVER#shouldAccept()
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    60
     * returns false for null arguments
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    61
     */
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    62
    private static void checkCookiePolicy() throws Exception {
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    63
        CookiePolicy cp = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    64
        boolean retVal;
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    65
        retVal = cp.shouldAccept(null, null);
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    66
        checkValue(retVal);
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    67
        retVal = cp.shouldAccept(null, new HttpCookie("CookieName", "CookieVal"));
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    68
        checkValue(retVal);
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    69
        retVal = cp.shouldAccept((new URL("http", "localhost", 2345, "/")).toURI(),
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    70
                                  null);
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    71
        checkValue(retVal);
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    72
    }
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    73
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    74
    private static void checkValue(boolean val) {
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    75
        if (val)
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    76
            throw new RuntimeException("Return value is not false!");
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    77
    }
d641a1b31205 7051862: CookiePolicy spec conflicts with CookiePolicy.ACCEPT_ORIGINAL_SERVER
khazra
parents: 17489
diff changeset
    78
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    79
    public static void makeHttpCall() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    81
            System.out.println("http server listenining on: "
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    82
                    + server.getAddress().getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            // install CookieManager to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            CookieHandler.setDefault(new CookieManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    87
            for (int i = 0; i < CookieTransactionHandler.testCount; i++) {
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    88
                System.out.println("====== CookieManager test " + (i+1)
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    89
                                    + " ======");
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    90
                ((CookieManager)CookieHandler.getDefault())
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    91
                    .setCookiePolicy(CookieTransactionHandler.testPolicies[i]);
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    92
                ((CookieManager)CookieHandler.getDefault())
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    93
                    .getCookieStore().removeAll();
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    94
                URL url = new URL("http" ,
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    95
                                  InetAddress.getLocalHost().getHostAddress(),
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    96
                                  server.getAddress().getPort(),
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    97
                                  CookieTransactionHandler.testCases[i][0]
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
    98
                                                          .serverPath);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                HttpURLConnection uc = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                uc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                uc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } finally {
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   104
            server.stop(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   109
class CookieTransactionHandler implements HttpHandler {
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   110
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   111
    private int testcaseDone = 0;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   112
    private int testDone = 0;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   113
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static boolean badRequest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // the main test control logic will also loop exactly this number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // to send http request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static final int testCount = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private String localHostAddr = "127.0.0.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   121
    @Override
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   122
    public void handle(HttpExchange exchange) throws IOException {
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   123
        if (testDone < testCases[testcaseDone].length) {
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   124
            // still have other tests to run,
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   125
            // check the Cookie header and then redirect it
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   126
            if (testDone > 0) checkRequest(exchange.getRequestHeaders());
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   127
            exchange.getResponseHeaders().add("Location",
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   128
                    testCases[testcaseDone][testDone].serverPath);
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   129
            exchange.getResponseHeaders()
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   130
                    .add(testCases[testcaseDone][testDone].headerToken,
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   131
                         testCases[testcaseDone][testDone].cookieToSend);
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   132
            exchange.sendResponseHeaders(302, -1);
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   133
            testDone++;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   134
        } else {
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   135
            // the last test of this test case
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   136
            if (testDone > 0) checkRequest(exchange.getRequestHeaders());
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   137
            testcaseDone++;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   138
            testDone = 0;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   139
            exchange.sendResponseHeaders(200, -1);
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   140
        }
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   141
        exchange.close();
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   142
    }
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   143
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   144
    private void checkRequest(Headers hdrs) {
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   145
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   146
        assert testDone > 0;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   147
        String cookieHeader = hdrs.getFirst("Cookie");
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   148
        if (cookieHeader != null &&
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   149
            cookieHeader
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   150
                .equalsIgnoreCase(testCases[testcaseDone][testDone-1]
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   151
                                  .cookieToRecv))
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   152
        {
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   153
            System.out.printf("%15s %s\n", "PASSED:", cookieHeader);
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   154
        } else {
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   155
            System.out.printf("%15s %s\n", "FAILED:", cookieHeader);
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   156
            System.out.printf("%15s %s\n\n", "should be:",
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   157
                    testCases[testcaseDone][testDone-1].cookieToRecv);
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   158
            badRequest = true;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   159
        }
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   160
    }
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   161
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // test cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static class CookieTestCase {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public String headerToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        public String cookieToSend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        public String cookieToRecv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        public String serverPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        public CookieTestCase(String h, String cts, String ctr, String sp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            headerToken = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            cookieToSend = cts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            cookieToRecv = ctr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            serverPath = sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   177
    /*
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   178
     * these two must match each other,
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   179
     * i.e. testCases.length == testPolicies.length
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   180
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   182
    // the test cases to run; each test case may contain multiple roundtrips
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   183
    public static CookieTestCase[][] testCases = null;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   184
    // indicates what CookiePolicy to use with each test cases
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   185
    public static CookiePolicy[] testPolicies = null;
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   186
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   187
    CookieTransactionHandler() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        testCases = new CookieTestCase[testCount][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        testPolicies = new CookiePolicy[testCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            localHostAddr = InetAddress.getLocalHost().getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } catch (Exception ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // an http session with Netscape cookies exchanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                new CookieTestCase("Set-Cookie",
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   201
                "CUSTOMER=WILE:BOB; " +
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   202
                "path=/; expires=Sat, 09-Nov-2030 23:12:40 GMT;" + "domain=." +
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   203
                localHostAddr,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                "CUSTOMER=WILE:BOB",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                "PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   209
                "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                "SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   214
                "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                "SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   219
                "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                "/foo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // check whether or not path rule is applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                "PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                "PART_NUMBER=ROCKET_LAUNCHER_0001",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                "PART_NUMBER=RIDING_ROCKET_0023; path=/ammo;" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   234
                "PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                "/ammo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // an http session with rfc2965 cookies exchanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                "Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   244
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                "/acme/login"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\";" + "domain=." + localHostAddr,
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   249
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." +
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   250
                    localHostAddr  + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";"
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   251
                    + "$Domain=\"." + localHostAddr +  "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                "/acme/pickitem"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   256
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr  +
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   257
                    "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"."
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   258
                    + localHostAddr  + "\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";" +
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   259
                    "$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                "/acme/shipping"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // check whether or not the path rule is applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   269
                "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                "/acme/ammo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                new CookieTestCase("Set-Cookie2",
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   273
                "Part_Number=\"Riding_Rocket_0023\"; Version=\"1\"; Path=\"/acme/ammo\";" + "domain=."
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   274
                    + localHostAddr,
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   275
                "$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\";$Path=\"/acme/ammo\";$Domain=\"."
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   276
                    + localHostAddr  + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";"
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   277
                    + "$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                "/acme/ammo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                new CookieTestCase("",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                "",
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   282
                "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                "/acme/parts"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // new cookie should overwrite old cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   292
                "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                "/acme"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                "Part_Number=\"Rocket_Launcher_2000\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   297
                "$Version=\"1\"; Part_Number=\"Rocket_Launcher_2000\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                "/acme"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // cookies without domain attributes
2924
a67f12d92bdd 6349566: java.net.CookieManager doesn't set default domain
jccollet
parents: 715
diff changeset
   303
        // RFC 2965 states that domain should default to host
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        testPolicies[count] = CookiePolicy.ACCEPT_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                "Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"",
2924
a67f12d92bdd 6349566: java.net.CookieManager doesn't set default domain
jccollet
parents: 715
diff changeset
   308
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                "/acme/login"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"",
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   313
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   314
                    "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                "/acme/pickitem"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"",
17489
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   319
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   320
                    "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +
a74a13b9aa93 7150552: network test hangs [macosx]
khazra
parents: 14787
diff changeset
   321
                    "; Shipping=\"FedEx\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                "/acme/shipping"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        assert count == testCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
}