Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/HttpCookie/ExpiredCookieTest.java
38812 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*/2728import java.net.*;29import java.util.*;30import java.io.*;31import java.text.*;3233public class ExpiredCookieTest {34// lifted from HttpCookie.java35private final static String[] COOKIE_DATE_FORMATS = {36"EEE',' dd-MMM-yy HH:mm:ss 'GMT'",37"EEE',' dd MMM yy HH:mm:ss 'GMT'",38"EEE MMM dd yy HH:mm:ss 'GMT'Z",39"EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'",40"EEE',' dd MMM yyyy HH:mm:ss 'GMT'",41"EEE MMM dd yyyy HH:mm:ss 'GMT'Z"42};43static final TimeZone GMT = TimeZone.getTimeZone("GMT");4445public static void main(String[] args) throws Exception {46Calendar cal = Calendar.getInstance(GMT);4748for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {49SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],50Locale.US);51cal.set(1970, 0, 1, 0, 0, 0);52df.setTimeZone(GMT);53df.setLenient(false);54df.set2DigitYearStart(cal.getTime());55CookieManager cm = new CookieManager(56null, CookiePolicy.ACCEPT_ALL);57CookieHandler.setDefault(cm);58Map<String,List<String>> header = new HashMap<>();59List<String> values = new ArrayList<>();6061cal.set(1970, 6, 9, 10, 10, 1);62StringBuilder datestring =63new StringBuilder(df.format(cal.getTime()));64values.add(65"TEST1=TEST1; Path=/; Expires=" + datestring.toString());6667cal.set(1969, 6, 9, 10, 10, 2);68datestring = new StringBuilder(df.format(cal.getTime()));69values.add(70"TEST2=TEST2; Path=/; Expires=" + datestring.toString());7172cal.set(2070, 6, 9, 10, 10, 3);73datestring = new StringBuilder(df.format(cal.getTime()));74values.add(75"TEST3=TEST3; Path=/; Expires=" + datestring.toString());7677cal.set(2069, 6, 9, 10, 10, 4);78datestring = new StringBuilder(df.format(cal.getTime()));79values.add(80"TEST4=TEST4; Path=/; Expires=" + datestring.toString());8182header.put("Set-Cookie", values);83cm.put(new URI("http://127.0.0.1/"), header);8485CookieStore cookieJar = cm.getCookieStore();86List <HttpCookie> cookies = cookieJar.getCookies();87if (COOKIE_DATE_FORMATS[i].contains("yyyy")) {88if (cookies.size() != 2)89throw new RuntimeException(90"Incorrectly parsing a bad date");91} else if (cookies.size() != 1) {92throw new RuntimeException(93"Incorrectly parsing a bad date");94}95}96}97}9899100