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 IOException { 47 Calendar cal = Calendar.getInstance(GMT); 48 49 try { 50 for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) { 51 SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i], 52 Locale.US); 53 cal.set(1970, 0, 1, 0, 0, 0); 54 df.setTimeZone(GMT); 55 df.setLenient(false); 56 df.set2DigitYearStart(cal.getTime()); 57 CookieManager cm = new CookieManager( 58 null, CookiePolicy.ACCEPT_ALL); 59 CookieHandler.setDefault(cm); 60 Map<String,List<String>> header = new HashMap<>(); 61 List<String> values = new ArrayList<>(); 62 63 cal.set(1970, 6, 9, 10, 10, 1); 64 StringBuilder datestring = 65 new StringBuilder(df.format(cal.getTime())); 66 values.add( 67 "TEST1=TEST1; Path=/; Expires=" + datestring.toString()); 68 69 cal.set(1969, 6, 9, 10, 10, 2); 70 datestring = new StringBuilder(df.format(cal.getTime())); 71 values.add( 72 "TEST2=TEST2; Path=/; Expires=" + datestring.toString()); 73 74 cal.set(2070, 6, 9, 10, 10, 3); 75 datestring = new StringBuilder(df.format(cal.getTime())); 76 values.add( 77 "TEST3=TEST3; Path=/; Expires=" + datestring.toString()); 78 79 cal.set(2069, 6, 9, 10, 10, 4); 80 datestring = new StringBuilder(df.format(cal.getTime())); 81 values.add( 82 "TEST4=TEST4; Path=/; Expires=" + datestring.toString()); 83 84 header.put("Set-Cookie", values); 85 cm.put(new URI("http://127.0.0.1/"), header); 86 87 CookieStore cookieJar = cm.getCookieStore(); 88 List <HttpCookie> cookies = cookieJar.getCookies(); 89 if (COOKIE_DATE_FORMATS[i].contains("yyyy")) { 90 if (cookies.size() != 2) 91 throw new RuntimeException( 92 "Incorrectly parsing a bad date"); 93 } else if (cookies.size() != 1) { 94 throw new RuntimeException( 95 "Incorrectly parsing a bad date"); 96 } 97 } 98 } catch(Exception e) { 99 System.out.println("Unable to get cookie using CookieHandler"); 100 e.printStackTrace(); 101 } 102 } 103 }