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