Path: blob/master/test/jdk/java/net/HttpCookie/ExpiredCookieTest.java
41149 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 800052526* @library /test/lib27*/2829import java.net.*;30import java.util.*;31import java.io.*;32import java.text.*;33import jdk.test.lib.net.URIBuilder;3435public class ExpiredCookieTest {36// lifted from HttpCookie.java37private final static String[] COOKIE_DATE_FORMATS = {38"EEE',' dd-MMM-yy HH:mm:ss 'GMT'",39"EEE',' dd MMM yy HH:mm:ss 'GMT'",40"EEE MMM dd yy HH:mm:ss 'GMT'Z",41"EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'",42"EEE',' dd MMM yyyy HH:mm:ss 'GMT'",43"EEE MMM dd yyyy HH:mm:ss 'GMT'Z"44};45static final TimeZone GMT = TimeZone.getTimeZone("GMT");4647public static void main(String[] args) throws Exception {48Calendar cal = Calendar.getInstance(GMT);4950for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {51SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],52Locale.US);53cal.set(1970, 0, 1, 0, 0, 0);54df.setTimeZone(GMT);55df.setLenient(false);56df.set2DigitYearStart(cal.getTime());57CookieManager cm = new CookieManager(58null, CookiePolicy.ACCEPT_ALL);59CookieHandler.setDefault(cm);60Map<String,List<String>> header = new HashMap<>();61List<String> values = new ArrayList<>();6263cal.set(1970, 6, 9, 10, 10, 1);64StringBuilder datestring =65new StringBuilder(df.format(cal.getTime()));66values.add(67"TEST1=TEST1; Path=/; Expires=" + datestring.toString());6869cal.set(1969, 6, 9, 10, 10, 2);70datestring = new StringBuilder(df.format(cal.getTime()));71values.add(72"TEST2=TEST2; Path=/; Expires=" + datestring.toString());7374cal.set(2070, 6, 9, 10, 10, 3);75datestring = new StringBuilder(df.format(cal.getTime()));76values.add(77"TEST3=TEST3; Path=/; Expires=" + datestring.toString());7879cal.set(2069, 6, 9, 10, 10, 4);80datestring = new StringBuilder(df.format(cal.getTime()));81values.add(82"TEST4=TEST4; Path=/; Expires=" + datestring.toString());8384header.put("Set-Cookie", values);85URI uri = URIBuilder.newBuilder()86.scheme("http")87.loopback()88.path("/")89.buildUnchecked();90System.out.println("URI: " + uri);91cm.put(uri, header);9293CookieStore cookieJar = cm.getCookieStore();94List <HttpCookie> cookies = cookieJar.getCookies();95if (COOKIE_DATE_FORMATS[i].contains("yyyy")) {96if (cookies.size() != 2)97throw new RuntimeException(98"Incorrectly parsing a bad date");99} else if (cookies.size() != 1) {100throw new RuntimeException(101"Incorrectly parsing a bad date");102}103}104}105}106107108