5556N/A/*
5556N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5556N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5556N/A *
5556N/A * This code is free software; you can redistribute it and/or modify it
5556N/A * under the terms of the GNU General Public License version 2 only, as
5556N/A * published by the Free Software Foundation.
5556N/A *
5556N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5556N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5556N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5556N/A * version 2 for more details (a copy is included in the LICENSE file that
5556N/A * accompanied this code).
5556N/A *
5556N/A * You should have received a copy of the GNU General Public License version
5556N/A * 2 along with this work; if not, write to the Free Software Foundation,
5556N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5556N/A *
5556N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5556N/A * or visit www.oracle.com if you need additional information or have any
5556N/A * questions.
5556N/A */
5556N/A
5556N/A/*
5556N/A * @test
5556N/A * @bug 8000525
5556N/A */
5556N/A
5556N/Aimport java.net.*;
5556N/Aimport java.util.*;
5556N/Aimport java.io.*;
5556N/Aimport java.text.*;
5556N/A
5556N/Apublic class ExpiredCookieTest {
5556N/A // lifted from HttpCookie.java
5556N/A private final static String[] COOKIE_DATE_FORMATS = {
5556N/A "EEE',' dd-MMM-yy HH:mm:ss 'GMT'",
5556N/A "EEE',' dd MMM yy HH:mm:ss 'GMT'",
5556N/A "EEE MMM dd yy HH:mm:ss 'GMT'Z",
5556N/A "EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'",
5556N/A "EEE',' dd MMM yyyy HH:mm:ss 'GMT'",
5556N/A "EEE MMM dd yyyy HH:mm:ss 'GMT'Z"
5556N/A };
5556N/A static final TimeZone GMT = TimeZone.getTimeZone("GMT");
5556N/A
5556N/A public static void main(String[] args) throws Exception {
5556N/A Calendar cal = Calendar.getInstance(GMT);
5556N/A
5556N/A for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
5556N/A SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
5556N/A Locale.US);
5556N/A cal.set(1970, 0, 1, 0, 0, 0);
5556N/A df.setTimeZone(GMT);
5556N/A df.setLenient(false);
5556N/A df.set2DigitYearStart(cal.getTime());
5556N/A CookieManager cm = new CookieManager(
5556N/A null, CookiePolicy.ACCEPT_ALL);
5556N/A CookieHandler.setDefault(cm);
5556N/A Map<String,List<String>> header = new HashMap<>();
5556N/A List<String> values = new ArrayList<>();
5556N/A
5556N/A cal.set(1970, 6, 9, 10, 10, 1);
5556N/A StringBuilder datestring =
5556N/A new StringBuilder(df.format(cal.getTime()));
5556N/A values.add(
5556N/A "TEST1=TEST1; Path=/; Expires=" + datestring.toString());
5556N/A
5556N/A cal.set(1969, 6, 9, 10, 10, 2);
5556N/A datestring = new StringBuilder(df.format(cal.getTime()));
5556N/A values.add(
5556N/A "TEST2=TEST2; Path=/; Expires=" + datestring.toString());
5556N/A
5556N/A cal.set(2070, 6, 9, 10, 10, 3);
5556N/A datestring = new StringBuilder(df.format(cal.getTime()));
5556N/A values.add(
5556N/A "TEST3=TEST3; Path=/; Expires=" + datestring.toString());
5556N/A
5556N/A cal.set(2069, 6, 9, 10, 10, 4);
5556N/A datestring = new StringBuilder(df.format(cal.getTime()));
5556N/A values.add(
5556N/A "TEST4=TEST4; Path=/; Expires=" + datestring.toString());
5556N/A
5556N/A header.put("Set-Cookie", values);
5556N/A cm.put(new URI("http://127.0.0.1/"), header);
5556N/A
5556N/A CookieStore cookieJar = cm.getCookieStore();
5556N/A List <HttpCookie> cookies = cookieJar.getCookies();
5556N/A if (COOKIE_DATE_FORMATS[i].contains("yyyy")) {
5556N/A if (cookies.size() != 2)
5556N/A throw new RuntimeException(
5556N/A "Incorrectly parsing a bad date");
5556N/A } else if (cookies.size() != 1) {
5556N/A throw new RuntimeException(
5556N/A "Incorrectly parsing a bad date");
5556N/A }
5556N/A }
5556N/A }
5556N/A}