Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / cookies / cookie_util_unittest.cc
blobe76d8c92fe214e294a5a8200a8f31df987b73903
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.
5 #include <string>
6 #include <utility>
8 #include "base/basictypes.h"
9 #include "base/strings/string_split.h"
10 #include "net/cookies/cookie_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace net {
15 namespace {
17 struct RequestCookieParsingTest {
18 std::string str;
19 base::StringPairs parsed;
22 cookie_util::ParsedRequestCookies MakeParsedRequestCookies(
23 const base::StringPairs& data) {
24 cookie_util::ParsedRequestCookies parsed;
25 for (size_t i = 0; i < data.size(); i++) {
26 parsed.push_back(std::make_pair(base::StringPiece(data[i].first),
27 base::StringPiece(data[i].second)));
29 return parsed;
32 void CheckParse(const std::string& str,
33 const base::StringPairs& parsed_expected) {
34 cookie_util::ParsedRequestCookies parsed;
35 cookie_util::ParseRequestCookieLine(str, &parsed);
36 EXPECT_EQ(MakeParsedRequestCookies(parsed_expected), parsed);
39 void CheckSerialize(const base::StringPairs& parsed,
40 const std::string& str_expected) {
41 cookie_util::ParsedRequestCookies prc = MakeParsedRequestCookies(parsed);
42 EXPECT_EQ(str_expected, cookie_util::SerializeRequestCookieLine(prc));
45 TEST(CookieUtilTest, TestDomainIsHostOnly) {
46 const struct {
47 const char* str;
48 const bool is_host_only;
49 } tests[] = {
50 { "", true },
51 { "www.google.com", true },
52 { ".google.com", false }
55 for (size_t i = 0; i < arraysize(tests); ++i) {
56 EXPECT_EQ(tests[i].is_host_only,
57 cookie_util::DomainIsHostOnly(tests[i].str));
61 TEST(CookieUtilTest, TestCookieDateParsing) {
62 const struct {
63 const char* str;
64 const bool valid;
65 const time_t epoch;
66 } tests[] = {
67 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 },
68 { "Thu, 19-Apr-2007 16:00:00 GMT", true, 1176998400 },
69 { "Wed, 25 Apr 2007 21:02:13 GMT", true, 1177534933 },
70 { "Thu, 19/Apr\\2007 16:00:00 GMT", true, 1176998400 },
71 { "Fri, 1 Jan 2010 01:01:50 GMT", true, 1262307710 },
72 { "Wednesday, 1-Jan-2003 00:00:00 GMT", true, 1041379200 },
73 { ", 1-Jan-2003 00:00:00 GMT", true, 1041379200 },
74 { " 1-Jan-2003 00:00:00 GMT", true, 1041379200 },
75 { "1-Jan-2003 00:00:00 GMT", true, 1041379200 },
76 { "Wed,18-Apr-07 22:50:12 GMT", true, 1176936612 },
77 { "WillyWonka , 18-Apr-07 22:50:12 GMT", true, 1176936612 },
78 { "WillyWonka , 18-Apr-07 22:50:12", true, 1176936612 },
79 { "WillyWonka , 18-apr-07 22:50:12", true, 1176936612 },
80 { "Mon, 18-Apr-1977 22:50:13 GMT", true, 230251813 },
81 { "Mon, 18-Apr-77 22:50:13 GMT", true, 230251813 },
82 // If the cookie came in with the expiration quoted (which in terms of
83 // the RFC you shouldn't do), we will get string quoted. Bug 1261605.
84 { "\"Sat, 15-Apr-17\\\"21:01:22\\\"GMT\"", true, 1492290082 },
85 // Test with full month names and partial names.
86 { "Partyday, 18- April-07 22:50:12", true, 1176936612 },
87 { "Partyday, 18 - Apri-07 22:50:12", true, 1176936612 },
88 { "Wednes, 1-Januar-2003 00:00:00 GMT", true, 1041379200 },
89 // Test that we always take GMT even with other time zones or bogus
90 // values. The RFC says everything should be GMT, and in the worst case
91 // we are 24 hours off because of zone issues.
92 { "Sat, 15-Apr-17 21:01:22", true, 1492290082 },
93 { "Sat, 15-Apr-17 21:01:22 GMT-2", true, 1492290082 },
94 { "Sat, 15-Apr-17 21:01:22 GMT BLAH", true, 1492290082 },
95 { "Sat, 15-Apr-17 21:01:22 GMT-0400", true, 1492290082 },
96 { "Sat, 15-Apr-17 21:01:22 GMT-0400 (EDT)",true, 1492290082 },
97 { "Sat, 15-Apr-17 21:01:22 DST", true, 1492290082 },
98 { "Sat, 15-Apr-17 21:01:22 -0400", true, 1492290082 },
99 { "Sat, 15-Apr-17 21:01:22 (hello there)", true, 1492290082 },
100 // Test that if we encounter multiple : fields, that we take the first
101 // that correctly parses.
102 { "Sat, 15-Apr-17 21:01:22 11:22:33", true, 1492290082 },
103 { "Sat, 15-Apr-17 ::00 21:01:22", true, 1492290082 },
104 { "Sat, 15-Apr-17 boink:z 21:01:22", true, 1492290082 },
105 // We take the first, which in this case is invalid.
106 { "Sat, 15-Apr-17 91:22:33 21:01:22", false, 0 },
107 // amazon.com formats their cookie expiration like this.
108 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 },
109 // Test that hh:mm:ss can occur anywhere.
110 { "22:50:12 Thu Apr 18 2007 GMT", true, 1176936612 },
111 { "Thu 22:50:12 Apr 18 2007 GMT", true, 1176936612 },
112 { "Thu Apr 22:50:12 18 2007 GMT", true, 1176936612 },
113 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 },
114 { "Thu Apr 18 2007 22:50:12 GMT", true, 1176936612 },
115 { "Thu Apr 18 2007 GMT 22:50:12", true, 1176936612 },
116 // Test that the day and year can be anywhere if they are unambigious.
117 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 },
118 { "15-Sat, Apr-17 21:01:22 GMT", true, 1492290082 },
119 { "15-Sat, Apr 21:01:22 GMT 17", true, 1492290082 },
120 { "15-Sat, Apr 21:01:22 GMT 2017", true, 1492290082 },
121 { "15 Apr 21:01:22 2017", true, 1492290082 },
122 { "15 17 Apr 21:01:22", true, 1492290082 },
123 { "Apr 15 17 21:01:22", true, 1492290082 },
124 { "Apr 15 21:01:22 17", true, 1492290082 },
125 { "2017 April 15 21:01:22", true, 1492290082 },
126 { "15 April 2017 21:01:22", true, 1492290082 },
127 // Some invalid dates
128 { "98 April 17 21:01:22", false, 0 },
129 { "Thu, 012-Aug-2008 20:49:07 GMT", false, 0 },
130 { "Thu, 12-Aug-31841 20:49:07 GMT", false, 0 },
131 { "Thu, 12-Aug-9999999999 20:49:07 GMT", false, 0 },
132 { "Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0 },
133 { "Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0 },
134 { "IAintNoDateFool", false, 0 },
137 base::Time parsed_time;
138 for (size_t i = 0; i < arraysize(tests); ++i) {
139 parsed_time = cookie_util::ParseCookieTime(tests[i].str);
140 if (!tests[i].valid) {
141 EXPECT_TRUE(parsed_time.is_null()) << tests[i].str;
142 continue;
144 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str;
145 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str;
149 TEST(CookieUtilTest, TestRequestCookieParsing) {
150 std::vector<RequestCookieParsingTest> tests;
152 // Simple case.
153 tests.push_back(RequestCookieParsingTest());
154 tests.back().str = "key=value";
155 tests.back().parsed.push_back(std::make_pair(std::string("key"),
156 std::string("value")));
157 // Multiple key/value pairs.
158 tests.push_back(RequestCookieParsingTest());
159 tests.back().str = "key1=value1; key2=value2";
160 tests.back().parsed.push_back(std::make_pair(std::string("key1"),
161 std::string("value1")));
162 tests.back().parsed.push_back(std::make_pair(std::string("key2"),
163 std::string("value2")));
164 // Empty value.
165 tests.push_back(RequestCookieParsingTest());
166 tests.back().str = "key=; otherkey=1234";
167 tests.back().parsed.push_back(std::make_pair(std::string("key"),
168 std::string()));
169 tests.back().parsed.push_back(std::make_pair(std::string("otherkey"),
170 std::string("1234")));
171 // Special characters (including equals signs) in value.
172 tests.push_back(RequestCookieParsingTest());
173 tests.back().str = "key=; a2=s=(./&t=:&u=a#$; a3=+~";
174 tests.back().parsed.push_back(std::make_pair(std::string("key"),
175 std::string()));
176 tests.back().parsed.push_back(std::make_pair(std::string("a2"),
177 std::string("s=(./&t=:&u=a#$")));
178 tests.back().parsed.push_back(std::make_pair(std::string("a3"),
179 std::string("+~")));
180 // Quoted value.
181 tests.push_back(RequestCookieParsingTest());
182 tests.back().str = "key=\"abcdef\"; otherkey=1234";
183 tests.back().parsed.push_back(std::make_pair(std::string("key"),
184 std::string("\"abcdef\"")));
185 tests.back().parsed.push_back(std::make_pair(std::string("otherkey"),
186 std::string("1234")));
188 for (size_t i = 0; i < tests.size(); i++) {
189 SCOPED_TRACE(testing::Message() << "Test " << i);
190 CheckParse(tests[i].str, tests[i].parsed);
191 CheckSerialize(tests[i].parsed, tests[i].str);
195 TEST(CookieUtilTest, TestGetEffectiveDomain) {
196 // Note: registry_controlled_domains::GetDomainAndRegistry is tested in its
197 // own unittests.
198 EXPECT_EQ("example.com",
199 cookie_util::GetEffectiveDomain("http", "www.example.com"));
200 EXPECT_EQ("example.com",
201 cookie_util::GetEffectiveDomain("https", "www.example.com"));
202 EXPECT_EQ("example.com",
203 cookie_util::GetEffectiveDomain("ws", "www.example.com"));
204 EXPECT_EQ("example.com",
205 cookie_util::GetEffectiveDomain("wss", "www.example.com"));
206 EXPECT_EQ("www.example.com",
207 cookie_util::GetEffectiveDomain("ftp", "www.example.com"));
210 } // namespace
212 } // namespace net