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