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