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