Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/HttpCookie/ExpiredCookieTest.java
41149 views
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
* @library /test/lib
28
*/
29
30
import java.net.*;
31
import java.util.*;
32
import java.io.*;
33
import java.text.*;
34
import jdk.test.lib.net.URIBuilder;
35
36
public class ExpiredCookieTest {
37
// lifted from HttpCookie.java
38
private final static String[] COOKIE_DATE_FORMATS = {
39
"EEE',' dd-MMM-yy HH:mm:ss 'GMT'",
40
"EEE',' dd MMM yy HH:mm:ss 'GMT'",
41
"EEE MMM dd yy HH:mm:ss 'GMT'Z",
42
"EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'",
43
"EEE',' dd MMM yyyy HH:mm:ss 'GMT'",
44
"EEE MMM dd yyyy HH:mm:ss 'GMT'Z"
45
};
46
static final TimeZone GMT = TimeZone.getTimeZone("GMT");
47
48
public static void main(String[] args) throws Exception {
49
Calendar cal = Calendar.getInstance(GMT);
50
51
for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
52
SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
53
Locale.US);
54
cal.set(1970, 0, 1, 0, 0, 0);
55
df.setTimeZone(GMT);
56
df.setLenient(false);
57
df.set2DigitYearStart(cal.getTime());
58
CookieManager cm = new CookieManager(
59
null, CookiePolicy.ACCEPT_ALL);
60
CookieHandler.setDefault(cm);
61
Map<String,List<String>> header = new HashMap<>();
62
List<String> values = new ArrayList<>();
63
64
cal.set(1970, 6, 9, 10, 10, 1);
65
StringBuilder datestring =
66
new StringBuilder(df.format(cal.getTime()));
67
values.add(
68
"TEST1=TEST1; Path=/; Expires=" + datestring.toString());
69
70
cal.set(1969, 6, 9, 10, 10, 2);
71
datestring = new StringBuilder(df.format(cal.getTime()));
72
values.add(
73
"TEST2=TEST2; Path=/; Expires=" + datestring.toString());
74
75
cal.set(2070, 6, 9, 10, 10, 3);
76
datestring = new StringBuilder(df.format(cal.getTime()));
77
values.add(
78
"TEST3=TEST3; Path=/; Expires=" + datestring.toString());
79
80
cal.set(2069, 6, 9, 10, 10, 4);
81
datestring = new StringBuilder(df.format(cal.getTime()));
82
values.add(
83
"TEST4=TEST4; Path=/; Expires=" + datestring.toString());
84
85
header.put("Set-Cookie", values);
86
URI uri = URIBuilder.newBuilder()
87
.scheme("http")
88
.loopback()
89
.path("/")
90
.buildUnchecked();
91
System.out.println("URI: " + uri);
92
cm.put(uri, header);
93
94
CookieStore cookieJar = cm.getCookieStore();
95
List <HttpCookie> cookies = cookieJar.getCookies();
96
if (COOKIE_DATE_FORMATS[i].contains("yyyy")) {
97
if (cookies.size() != 2)
98
throw new RuntimeException(
99
"Incorrectly parsing a bad date");
100
} else if (cookies.size() != 1) {
101
throw new RuntimeException(
102
"Incorrectly parsing a bad date");
103
}
104
}
105
}
106
}
107
108