1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "base/basictypes.h"
10 #include "net/cookies/cookie_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 struct RequestCookieParsingTest
{
17 std::vector
<std::pair
<std::string
, std::string
> > parsed
;
20 net::cookie_util::ParsedRequestCookies
MakeParsedRequestCookies(
21 const std::vector
<std::pair
<std::string
, std::string
> >& data
) {
22 net::cookie_util::ParsedRequestCookies parsed
;
23 for (size_t i
= 0; i
< data
.size(); i
++) {
24 parsed
.push_back(std::make_pair(base::StringPiece(data
[i
].first
),
25 base::StringPiece(data
[i
].second
)));
31 const std::string
& str
,
32 const std::vector
<std::pair
<std::string
, std::string
> >& parsed_expected
) {
33 net::cookie_util::ParsedRequestCookies parsed
;
34 net::cookie_util::ParseRequestCookieLine(str
, &parsed
);
35 EXPECT_EQ(MakeParsedRequestCookies(parsed_expected
), parsed
);
39 const std::vector
<std::pair
<std::string
, std::string
> >& parsed
,
40 const std::string
& str_expected
) {
41 net::cookie_util::ParsedRequestCookies prc
=
42 MakeParsedRequestCookies(parsed
);
43 EXPECT_EQ(str_expected
, net::cookie_util::SerializeRequestCookieLine(prc
));
48 TEST(CookieUtilTest
, TestDomainIsHostOnly
) {
51 const bool is_host_only
;
54 { "www.google.com", true },
55 { ".google.com", false }
58 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
59 EXPECT_EQ(tests
[i
].is_host_only
,
60 net::cookie_util::DomainIsHostOnly(tests
[i
].str
));
64 TEST(CookieUtilTest
, TestCookieDateParsing
) {
70 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 },
71 { "Thu, 19-Apr-2007 16:00:00 GMT", true, 1176998400 },
72 { "Wed, 25 Apr 2007 21:02:13 GMT", true, 1177534933 },
73 { "Thu, 19/Apr\\2007 16:00:00 GMT", true, 1176998400 },
74 { "Fri, 1 Jan 2010 01:01:50 GMT", true, 1262307710 },
75 { "Wednesday, 1-Jan-2003 00:00:00 GMT", true, 1041379200 },
76 { ", 1-Jan-2003 00:00:00 GMT", true, 1041379200 },
77 { " 1-Jan-2003 00:00:00 GMT", true, 1041379200 },
78 { "1-Jan-2003 00:00:00 GMT", true, 1041379200 },
79 { "Wed,18-Apr-07 22:50:12 GMT", true, 1176936612 },
80 { "WillyWonka , 18-Apr-07 22:50:12 GMT", true, 1176936612 },
81 { "WillyWonka , 18-Apr-07 22:50:12", true, 1176936612 },
82 { "WillyWonka , 18-apr-07 22:50:12", true, 1176936612 },
83 { "Mon, 18-Apr-1977 22:50:13 GMT", true, 230251813 },
84 { "Mon, 18-Apr-77 22:50:13 GMT", true, 230251813 },
85 // If the cookie came in with the expiration quoted (which in terms of
86 // the RFC you shouldn't do), we will get string quoted. Bug 1261605.
87 { "\"Sat, 15-Apr-17\\\"21:01:22\\\"GMT\"", true, 1492290082 },
88 // Test with full month names and partial names.
89 { "Partyday, 18- April-07 22:50:12", true, 1176936612 },
90 { "Partyday, 18 - Apri-07 22:50:12", true, 1176936612 },
91 { "Wednes, 1-Januar-2003 00:00:00 GMT", true, 1041379200 },
92 // Test that we always take GMT even with other time zones or bogus
93 // values. The RFC says everything should be GMT, and in the worst case
94 // we are 24 hours off because of zone issues.
95 { "Sat, 15-Apr-17 21:01:22", true, 1492290082 },
96 { "Sat, 15-Apr-17 21:01:22 GMT-2", true, 1492290082 },
97 { "Sat, 15-Apr-17 21:01:22 GMT BLAH", true, 1492290082 },
98 { "Sat, 15-Apr-17 21:01:22 GMT-0400", true, 1492290082 },
99 { "Sat, 15-Apr-17 21:01:22 GMT-0400 (EDT)",true, 1492290082 },
100 { "Sat, 15-Apr-17 21:01:22 DST", true, 1492290082 },
101 { "Sat, 15-Apr-17 21:01:22 -0400", true, 1492290082 },
102 { "Sat, 15-Apr-17 21:01:22 (hello there)", true, 1492290082 },
103 // Test that if we encounter multiple : fields, that we take the first
104 // that correctly parses.
105 { "Sat, 15-Apr-17 21:01:22 11:22:33", true, 1492290082 },
106 { "Sat, 15-Apr-17 ::00 21:01:22", true, 1492290082 },
107 { "Sat, 15-Apr-17 boink:z 21:01:22", true, 1492290082 },
108 // We take the first, which in this case is invalid.
109 { "Sat, 15-Apr-17 91:22:33 21:01:22", false, 0 },
110 // amazon.com formats their cookie expiration like this.
111 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 },
112 // Test that hh:mm:ss can occur anywhere.
113 { "22:50:12 Thu Apr 18 2007 GMT", true, 1176936612 },
114 { "Thu 22:50:12 Apr 18 2007 GMT", true, 1176936612 },
115 { "Thu Apr 22:50:12 18 2007 GMT", true, 1176936612 },
116 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 },
117 { "Thu Apr 18 2007 22:50:12 GMT", true, 1176936612 },
118 { "Thu Apr 18 2007 GMT 22:50:12", true, 1176936612 },
119 // Test that the day and year can be anywhere if they are unambigious.
120 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 },
121 { "15-Sat, Apr-17 21:01:22 GMT", true, 1492290082 },
122 { "15-Sat, Apr 21:01:22 GMT 17", true, 1492290082 },
123 { "15-Sat, Apr 21:01:22 GMT 2017", true, 1492290082 },
124 { "15 Apr 21:01:22 2017", true, 1492290082 },
125 { "15 17 Apr 21:01:22", true, 1492290082 },
126 { "Apr 15 17 21:01:22", true, 1492290082 },
127 { "Apr 15 21:01:22 17", true, 1492290082 },
128 { "2017 April 15 21:01:22", true, 1492290082 },
129 { "15 April 2017 21:01:22", true, 1492290082 },
130 // Some invalid dates
131 { "98 April 17 21:01:22", false, 0 },
132 { "Thu, 012-Aug-2008 20:49:07 GMT", false, 0 },
133 { "Thu, 12-Aug-31841 20:49:07 GMT", false, 0 },
134 { "Thu, 12-Aug-9999999999 20:49:07 GMT", false, 0 },
135 { "Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0 },
136 { "Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0 },
137 { "IAintNoDateFool", false, 0 },
140 base::Time parsed_time
;
141 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
142 parsed_time
= net::cookie_util::ParseCookieTime(tests
[i
].str
);
143 if (!tests
[i
].valid
) {
144 EXPECT_FALSE(!parsed_time
.is_null()) << tests
[i
].str
;
147 EXPECT_TRUE(!parsed_time
.is_null()) << tests
[i
].str
;
148 EXPECT_EQ(tests
[i
].epoch
, parsed_time
.ToTimeT()) << tests
[i
].str
;
152 TEST(CookieUtilTest
, TestRequestCookieParsing
) {
153 std::vector
<RequestCookieParsingTest
> tests
;
156 tests
.push_back(RequestCookieParsingTest());
157 tests
.back().str
= "key=value";
158 tests
.back().parsed
.push_back(std::make_pair(std::string("key"),
159 std::string("value")));
160 // Multiple key/value pairs.
161 tests
.push_back(RequestCookieParsingTest());
162 tests
.back().str
= "key1=value1; key2=value2";
163 tests
.back().parsed
.push_back(std::make_pair(std::string("key1"),
164 std::string("value1")));
165 tests
.back().parsed
.push_back(std::make_pair(std::string("key2"),
166 std::string("value2")));
168 tests
.push_back(RequestCookieParsingTest());
169 tests
.back().str
= "key=; otherkey=1234";
170 tests
.back().parsed
.push_back(std::make_pair(std::string("key"),
172 tests
.back().parsed
.push_back(std::make_pair(std::string("otherkey"),
173 std::string("1234")));
174 // Special characters (including equals signs) in value.
175 tests
.push_back(RequestCookieParsingTest());
176 tests
.back().str
= "key=; a2=s=(./&t=:&u=a#$; a3=+~";
177 tests
.back().parsed
.push_back(std::make_pair(std::string("key"),
179 tests
.back().parsed
.push_back(std::make_pair(std::string("a2"),
180 std::string("s=(./&t=:&u=a#$")));
181 tests
.back().parsed
.push_back(std::make_pair(std::string("a3"),
184 tests
.push_back(RequestCookieParsingTest());
185 tests
.back().str
= "key=\"abcdef\"; otherkey=1234";
186 tests
.back().parsed
.push_back(std::make_pair(std::string("key"),
187 std::string("\"abcdef\"")));
188 tests
.back().parsed
.push_back(std::make_pair(std::string("otherkey"),
189 std::string("1234")));
191 for (size_t i
= 0; i
< tests
.size(); i
++) {
192 SCOPED_TRACE(testing::Message() << "Test " << i
);
193 CheckParse(tests
[i
].str
, tests
[i
].parsed
);
194 CheckSerialize(tests
[i
].parsed
, tests
[i
].str
);