jdk/test/java/net/CookieHandler/TestHttpCookie.java
author jccollet
Fri, 20 Nov 2009 14:50:55 +0100
changeset 4324 5fd48b8b450b
parent 1932 d3506bce7d27
child 5152 a95085d9fe62
permissions -rw-r--r--
6901170: HttpCookie parsing of version and max-age mis-handled Summary: Accept single quotes in cookies and better exception handling in CookieManager Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1247
b4c26443dee5 6754988: Update copyright year
xdono
parents: 1234
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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.HttpCookie
4324
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 1932
diff changeset
    27
 * @bug 6244040 6277796 6277801 6277808 6294071 6692802 6790677 6901170
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Edward Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.HttpCookie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class TestHttpCookie {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static int testCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private String cHeader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private List<HttpCookie> cookies = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    // test case here expressed as a string, which represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    // the header string to be parsed into HttpCookie instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // A TestHttpCookie instance will be created to hold such a HttpCookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // object, and TestHttpCookie class has utility methods to check equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // between HttpCookie's real property and expected property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static TestHttpCookie test(String cookieHeader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        return new TestHttpCookie(cookieHeader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    TestHttpCookie(String cHeader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        assert cHeader != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.cHeader = cHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            List<HttpCookie> cookies = HttpCookie.parse(cHeader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            this.cookies = cookies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        } catch (IllegalArgumentException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            cookies = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // check name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    TestHttpCookie n(int index, String n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        HttpCookie cookie = cookies.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (cookie == null || !n.equalsIgnoreCase(cookie.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            raiseError("name", cookie.getName(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    TestHttpCookie n(String n) { return n(0, n); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // check value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    TestHttpCookie v(int index, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        HttpCookie cookie = cookies.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (cookie == null || !v.equals(cookie.getValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            raiseError("value", cookie.getValue(), v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    TestHttpCookie v(String v) { return v(0, v); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // check version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    TestHttpCookie ver(int index, int ver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        HttpCookie cookie = cookies.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (cookie == null || (ver != cookie.getVersion())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            raiseError("version", Integer.toString(cookie.getVersion()), Integer.toString(ver));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    TestHttpCookie ver(int ver) { return ver(0, ver); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // check path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    TestHttpCookie p(int index, String p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        HttpCookie cookie = cookies.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (cookie == null || !p.equals(cookie.getPath())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            raiseError("path", cookie.getPath(), p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    TestHttpCookie p(String p) { return p(0, p); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // check null-ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    TestHttpCookie nil() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (cookies != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            raiseError("Check null-ability fail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // check comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    TestHttpCookie c(int index, String c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        HttpCookie cookie = cookies.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (cookie == null || !c.equals(cookie.getComment())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            raiseError("comment", cookie.getComment(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    TestHttpCookie c(String c) { return c(0, c); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // check comment url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    TestHttpCookie cu(int index, String cu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        HttpCookie cookie = cookies.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (cookie == null || !cu.equals(cookie.getCommentURL())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            raiseError("comment url", cookie.getCommentURL(), cu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    TestHttpCookie cu(String cu) { return cu(0, cu); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // check discard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    TestHttpCookie dsc(int index, boolean dsc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        HttpCookie cookie = cookies.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (cookie == null || (dsc != cookie.getDiscard())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            raiseError("discard", Boolean.toString(cookie.getDiscard()), Boolean.toString(dsc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    TestHttpCookie dsc(boolean dsc) { return dsc(0, dsc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // check domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    TestHttpCookie d(int index, String d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        HttpCookie cookie = cookies.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (cookie == null || !d.equalsIgnoreCase(cookie.getDomain())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            raiseError("domain", cookie.getDomain(), d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    TestHttpCookie d(String d) { return d(0, d); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // check max-age
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    TestHttpCookie a(int index, long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        HttpCookie cookie = cookies.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (cookie == null || (a != cookie.getMaxAge())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            raiseError("max-age", Long.toString(cookie.getMaxAge()), Long.toString(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    TestHttpCookie a(long a) { return a(0, a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // check port list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    TestHttpCookie port(int index, String p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        HttpCookie cookie = cookies.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (cookie == null || !p.equals(cookie.getPortlist())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            raiseError("portlist", cookie.getPortlist(), p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    TestHttpCookie port(String p) { return port(0, p); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
1234
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   181
    // check http only
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   182
    TestHttpCookie httpOnly(int index, boolean b) {
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   183
        HttpCookie cookie = cookies.get(index);
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   184
        if (cookie == null || b != cookie.isHttpOnly()) {
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   185
            raiseError("HttpOnly", String.valueOf(cookie.isHttpOnly()), String.valueOf(b));
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   186
        }
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   187
        return this;
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   188
    }
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   189
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   190
    TestHttpCookie httpOnly(boolean b) {
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   191
        return httpOnly(0, b);
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   192
    }
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   193
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    // check equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static void eq(HttpCookie ck1, HttpCookie ck2, boolean same) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (ck1.equals(ck2) != same) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            raiseError("Comparison inconsistent: " + ck1 + " " + ck2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    + " should " + (same ? "equal" : "not equal"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int h1 = ck1.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        int h2 = ck2.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if ((h1 == h2) != same) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            raiseError("Comparison inconsistent: hashCode for " + ck1 + " " + ck2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    + " should " + (same ? "equal" : "not equal"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // check domainMatches()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static void dm(String domain, String host, boolean matches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (HttpCookie.domainMatches(domain, host) != matches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            raiseError("Host " + host + (matches?" should ":" should not ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        "domain-match with domain " + domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    void raiseError(String attr, String realValue, String expectedValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        sb.append("Cookie ").append(attr).append(" is ").append(realValue).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                append(", should be ").append(expectedValue).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                append(" (").append(cHeader).append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        throw new RuntimeException(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    static void raiseError(String prompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        throw new RuntimeException(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    static void runTests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        rfc2965();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        netscape();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        misc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    static void rfc2965() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        header("Test using rfc 2965 syntax");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        test("set-cookie2: Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // whitespace between attr and = sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        test("set-cookie2: Customer = \"WILE_E_COYOTE\"; Version = \"1\"; Path = \"/acme\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // $NAME is reserved; result should be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        test("set-cookie2: $Customer = \"WILE_E_COYOTE\"; Version = \"1\"; Path = \"/acme\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        .nil();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // a 'full' cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        test("set-cookie2: Customer=\"WILE_E_COYOTE\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                ";Version=\"1\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                ";Path=\"/acme\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                ";Comment=\"this is a coyote\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                ";CommentURL=\"http://www.coyote.org\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                ";Discard" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                ";Domain=\".coyote.org\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                ";Max-Age=\"3600\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                ";Port=\"80\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                ";Secure")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        .c("this is a coyote").cu("http://www.coyote.org").dsc(true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        .d(".coyote.org").a(3600).port("80");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // a 'full' cookie, without leading set-cookie2 token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        test("Customer=\"WILE_E_COYOTE\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                ";Version=\"1\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                ";Path=\"/acme\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                ";Comment=\"this is a coyote\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                ";CommentURL=\"http://www.coyote.org\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                ";Discard" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                ";Domain=\".coyote.org\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                ";Max-Age=\"3600\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                ";Port=\"80\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                ";Secure")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        .c("this is a coyote").cu("http://www.coyote.org").dsc(true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        .d(".coyote.org").a(3600).port("80");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // empty set-cookie string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        test("").nil();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // NullPointerException expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            test(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } catch (NullPointerException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // no-op
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // bug 6277796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        test("Set-Cookie2:Customer=\"dtftest\"; Discard; Secure; Domain=\".sun.com\"; Max-Age=\"100\"; Version=\"1\";  path=\"/www\"; Port=\"80\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        .n("Customer").v("dtftest").ver(1).d(".sun.com").p("/www").port("80").dsc(true).a(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // bug 6277801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        test("Set-Cookie2:Customer=\"dtftest\"; Discard; Secure; Domain=\".sun.com\"; Max-Age=\"100\"; Version=\"1\";  path=\"/www\"; Port=\"80\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                ";Domain=\".java.sun.com\"; Max-Age=\"200\"; path=\"/javadoc\"; Port=\"8080\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        .n("Customer").v("dtftest").ver(1).d(".sun.com").p("/www").port("80").dsc(true).a(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // bug 6294071
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        test("Set-Cookie2:Customer=\"dtftest\";Discard; Secure; Domain=\"sun.com\"; Max-Age=\"100\";Version=\"1\";  Path=\"/www\"; Port=\"80,8080\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        .n("Customer").v("dtftest").ver(1).d("sun.com").p("/www").port("80,8080").dsc(true).a(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        test("Set-Cookie2:Customer=\"developer\";Domain=\"sun.com\";Max-Age=\"100\";Path=\"/www\";Port=\"80,8080\";CommentURL=\"http://www.sun.com/java1,000,000.html\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        .n("Customer").v("developer").d("sun.com").p("/www").port("80,8080").a(100).cu("http://www.sun.com/java1,000,000.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // a header string contains 2 cookies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        test("Set-Cookie2:C1=\"V1\";Domain=\".sun1.com\";path=\"/www1\";Max-Age=\"100\",C2=\"V2\";Domain=\".sun2.com\";path=\"/www2\";Max-Age=\"200\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        .n(0, "C1").v(0, "V1").p(0, "/www1").a(0, 100).d(0, ".sun1.com")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        .n(1, "C2").v(1, "V2").p(1, "/www2").a(1, 200).d(1, ".sun2.com");
1932
d3506bce7d27 6790677: java.net.HttpCookie.parse(String) should ignore unrecognized attributes, RFC2965
jccollet
parents: 1247
diff changeset
   310
d3506bce7d27 6790677: java.net.HttpCookie.parse(String) should ignore unrecognized attributes, RFC2965
jccollet
parents: 1247
diff changeset
   311
        // Bug 6790677: Should ignore bogus attributes
d3506bce7d27 6790677: java.net.HttpCookie.parse(String) should ignore unrecognized attributes, RFC2965
jccollet
parents: 1247
diff changeset
   312
        test("Set-Cookie2:C1=\"V1\";foobar").n(0, "C1").v(0, "V1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    static void netscape() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        header("Test using netscape cookie syntax");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // a Netscape cookie, without set-cookie leading token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        test("CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // a 'google' cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        test("Set-Cookie: PREF=ID=1eda537de48ac25d:CR=1:TM=1112868587:LM=1112868587:S=t3FPA-mT9lTR3bxU;" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
             "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        .n("PREF").v("ID=1eda537de48ac25d:CR=1:TM=1112868587:LM=1112868587:S=t3FPA-mT9lTR3bxU")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        .p("/").d(".google.com").ver(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // bug 6277796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; Secure")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // bug 6277801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; path=\"/acme\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
4324
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 1932
diff changeset
   338
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 1932
diff changeset
   339
        // bug 6901170
5fd48b8b450b 6901170: HttpCookie parsing of version and max-age mis-handled
jccollet
parents: 1932
diff changeset
   340
        test("set-cookie: CUSTOMER=WILE_E_COYOTE; version='1'").ver(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    static void misc() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        header("Test equals()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // test equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        HttpCookie c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        c1.setDomain(".coyote.org");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        c1.setPath("/acme");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        HttpCookie c2 = (HttpCookie)c1.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        eq(c1, c2, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // test equals() when domain and path are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        eq(c1, c2, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // path is case-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        c1.setPath("/acme");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        c2.setPath("/ACME");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        eq(c1, c2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        header("Test domainMatches()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        dm(".foo.com",  "y.x.foo.com",      false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        dm(".foo.com",  "x.foo.com",        true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        dm(".com",      "whatever.com",     false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        dm(".com.",     "whatever.com",     false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        dm(".ajax.com", "ajax.com",         true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        dm(".local",    "example.local",    true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // bug 6277808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            c1 = new HttpCookie("", "whatever");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } catch (IllegalArgumentException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // expected exception; no-op
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
1234
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   380
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   381
        // CR 6692802: HttpOnly flag
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   382
        test("set-cookie: CUSTOMER=WILE_E_COYOTE;HttpOnly").httpOnly(true);
e3dc213d4879 6692802: HttpCookie needs to support HttpOnly attribute
jccollet
parents: 2
diff changeset
   383
        test("set-cookie: CUSTOMER=WILE_E_COYOTE").httpOnly(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    static void header(String prompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        System.out.println("== " + prompt + " ==");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        runTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        System.out.println("Succeeded in running " + testCount + " tests.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}