jdk/test/java/net/HttpCookie/ExpiredCookieTest.java
author robm
Thu, 13 Dec 2012 15:28:40 +0000
changeset 14787 54eb3f090934
permissions -rw-r--r--
8000525: Java.net.httpcookie api does not support 2-digit year format Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14787
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
     1
/*
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
     4
 *
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
     8
 *
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    13
 * accompanied this code).
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    14
 *
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    18
 *
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    21
 * questions.
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    22
 */
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    23
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    24
/*
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    25
 * @test
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    26
 * @bug 8000525
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    27
 */
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    28
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    29
import java.net.*;
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    30
import java.util.*;
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    31
import java.io.*;
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    32
import java.text.*;
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    33
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    34
public class ExpiredCookieTest {
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    35
    // lifted from HttpCookie.java
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    36
    private final static String[] COOKIE_DATE_FORMATS = {
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    37
        "EEE',' dd-MMM-yy HH:mm:ss 'GMT'",
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    38
        "EEE',' dd MMM yy HH:mm:ss 'GMT'",
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    39
        "EEE MMM dd yy HH:mm:ss 'GMT'Z",
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    40
        "EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'",
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    41
        "EEE',' dd MMM yyyy HH:mm:ss 'GMT'",
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    42
        "EEE MMM dd yyyy HH:mm:ss 'GMT'Z"
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    43
    };
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    44
    static final TimeZone GMT = TimeZone.getTimeZone("GMT");
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    45
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    47
        Calendar cal = Calendar.getInstance(GMT);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    48
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    49
        for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    50
            SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    51
                                                     Locale.US);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    52
            cal.set(1970, 0, 1, 0, 0, 0);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    53
            df.setTimeZone(GMT);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    54
            df.setLenient(false);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    55
            df.set2DigitYearStart(cal.getTime());
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    56
            CookieManager cm = new CookieManager(
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    57
                null, CookiePolicy.ACCEPT_ALL);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    58
            CookieHandler.setDefault(cm);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    59
            Map<String,List<String>> header = new HashMap<>();
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    60
            List<String> values = new ArrayList<>();
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    61
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    62
            cal.set(1970, 6, 9, 10, 10, 1);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    63
            StringBuilder datestring =
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    64
                new StringBuilder(df.format(cal.getTime()));
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    65
            values.add(
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    66
                "TEST1=TEST1; Path=/; Expires=" + datestring.toString());
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    67
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    68
            cal.set(1969, 6, 9, 10, 10, 2);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    69
            datestring = new StringBuilder(df.format(cal.getTime()));
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    70
            values.add(
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    71
                "TEST2=TEST2; Path=/; Expires=" + datestring.toString());
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    72
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    73
            cal.set(2070, 6, 9, 10, 10, 3);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    74
            datestring = new StringBuilder(df.format(cal.getTime()));
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    75
            values.add(
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    76
                "TEST3=TEST3; Path=/; Expires=" + datestring.toString());
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    77
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    78
            cal.set(2069, 6, 9, 10, 10, 4);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    79
            datestring = new StringBuilder(df.format(cal.getTime()));
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    80
            values.add(
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    81
                "TEST4=TEST4; Path=/; Expires=" + datestring.toString());
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    82
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    83
            header.put("Set-Cookie", values);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    84
            cm.put(new URI("http://127.0.0.1/"), header);
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    85
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    86
            CookieStore cookieJar =  cm.getCookieStore();
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    87
            List <HttpCookie> cookies = cookieJar.getCookies();
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    88
            if (COOKIE_DATE_FORMATS[i].contains("yyyy")) {
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    89
                if (cookies.size() != 2)
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    90
                    throw new RuntimeException(
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    91
                        "Incorrectly parsing a bad date");
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    92
            } else if (cookies.size() != 1) {
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    93
                throw new RuntimeException(
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    94
                    "Incorrectly parsing a bad date");
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    95
            }
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    96
        }
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    97
    }
54eb3f090934 8000525: Java.net.httpcookie api does not support 2-digit year format
robm
parents:
diff changeset
    98
}