blob: 2bc0be8480cf26909159599b731c17fc5f7784de [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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 <algorithm>
6
7#include "base/bind.h"
Ben Murdoch9ab55632013-07-18 11:57:30 +01008#include "base/message_loop/message_loop.h"
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +01009#include "base/strings/string_util.h"
10#include "base/strings/stringprintf.h"
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +010011#include "base/test/perf_time_logger.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000012#include "net/cookies/canonical_cookie.h"
13#include "net/cookies/cookie_monster.h"
14#include "net/cookies/cookie_monster_store_test.h"
15#include "net/cookies/parsed_cookie.h"
16#include "testing/gtest/include/gtest/gtest.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010017#include "url/gurl.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000019namespace net {
20
Torne (Richard Coles)58218062012-11-14 11:43:16 +000021namespace {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000022
23const int kNumCookies = 20000;
24const char kCookieLine[] = "A = \"b=;\\\"\" ;secure;;;";
25const char kGoogleURL[] = "http://www.google.izzle";
26
27int CountInString(const std::string& str, char c) {
28 return std::count(str.begin(), str.end(), c);
29}
30
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031class CookieMonsterTest : public testing::Test {
32 public:
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010033 CookieMonsterTest() : message_loop_(new base::MessageLoopForIO()) {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +000034
35 private:
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010036 scoped_ptr<base::MessageLoop> message_loop_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000037};
Torne (Richard Coles)58218062012-11-14 11:43:16 +000038
39class BaseCallback {
40 public:
41 BaseCallback() : has_run_(false) {}
42
43 protected:
44 void WaitForCallback() {
45 // Note that the performance tests currently all operate on a loaded cookie
46 // store (or, more precisely, one that has no backing persistent store).
47 // Therefore, callbacks will actually always complete synchronously. If the
48 // tests get more advanced we need to add other means of signaling
49 // completion.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010050 base::MessageLoop::current()->RunUntilIdle();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000051 EXPECT_TRUE(has_run_);
52 has_run_ = false;
53 }
54
55 void Run() {
56 has_run_ = true;
57 }
58
59 bool has_run_;
60};
61
Torne (Richard Coles)58218062012-11-14 11:43:16 +000062class SetCookieCallback : public BaseCallback {
63 public:
64 void SetCookie(
65 CookieMonster* cm, const GURL& gurl, const std::string& cookie) {
66 cm->SetCookieWithOptionsAsync(gurl, cookie, options_, base::Bind(
67 &SetCookieCallback::Run, base::Unretained(this)));
68 WaitForCallback();
69 }
70 private:
71 void Run(bool success) {
72 EXPECT_TRUE(success);
73 BaseCallback::Run();
74 }
75 net::CookieOptions options_;
76};
77
78class GetCookiesCallback : public BaseCallback {
79 public:
80 const std::string& GetCookies(CookieMonster* cm, const GURL& gurl) {
81 cm->GetCookiesWithOptionsAsync(gurl, options_, base::Bind(
82 &GetCookiesCallback::Run, base::Unretained(this)));
83 WaitForCallback();
84 return cookies_;
85 }
86
87 private:
88 void Run(const std::string& cookies) {
89 cookies_ = cookies;
90 BaseCallback::Run();
91 }
92 std::string cookies_;
93 net::CookieOptions options_;
94};
95
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000096} // namespace
97
98TEST(ParsedCookieTest, TestParseCookies) {
99 std::string cookie(kCookieLine);
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100100 base::PerfTimeLogger timer("Parsed_cookie_parse_cookies");
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000101 for (int i = 0; i < kNumCookies; ++i) {
102 ParsedCookie pc(cookie);
103 EXPECT_TRUE(pc.IsValid());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000104 }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000105 timer.Done();
106}
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000107
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000108TEST(ParsedCookieTest, TestParseBigCookies) {
109 std::string cookie(3800, 'z');
110 cookie += kCookieLine;
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100111 base::PerfTimeLogger timer("Parsed_cookie_parse_big_cookies");
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000112 for (int i = 0; i < kNumCookies; ++i) {
113 ParsedCookie pc(cookie);
114 EXPECT_TRUE(pc.IsValid());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000115 }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000116 timer.Done();
117}
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000118
119TEST_F(CookieMonsterTest, TestAddCookiesOnSingleHost) {
120 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
121 std::vector<std::string> cookies;
122 for (int i = 0; i < kNumCookies; i++) {
123 cookies.push_back(base::StringPrintf("a%03d=b", i));
124 }
125
126 SetCookieCallback setCookieCallback;
127
128 // Add a bunch of cookies on a single host
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100129 base::PerfTimeLogger timer("Cookie_monster_add_single_host");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000130
131 for (std::vector<std::string>::const_iterator it = cookies.begin();
132 it != cookies.end(); ++it) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100133 setCookieCallback.SetCookie(cm.get(), GURL(kGoogleURL), *it);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000134 }
135 timer.Done();
136
137 GetCookiesCallback getCookiesCallback;
138
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100139 base::PerfTimeLogger timer2("Cookie_monster_query_single_host");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000140 for (std::vector<std::string>::const_iterator it = cookies.begin();
141 it != cookies.end(); ++it) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100142 getCookiesCallback.GetCookies(cm.get(), GURL(kGoogleURL));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000143 }
144 timer2.Done();
145
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100146 base::PerfTimeLogger timer3("Cookie_monster_deleteall_single_host");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000147 cm->DeleteAllAsync(CookieMonster::DeleteCallback());
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100148 base::MessageLoop::current()->RunUntilIdle();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000149 timer3.Done();
150}
151
152TEST_F(CookieMonsterTest, TestAddCookieOnManyHosts) {
153 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
154 std::string cookie(kCookieLine);
155 std::vector<GURL> gurls; // just wanna have ffffuunnn
156 for (int i = 0; i < kNumCookies; ++i) {
157 gurls.push_back(GURL(base::StringPrintf("https://a%04d.izzle", i)));
158 }
159
160 SetCookieCallback setCookieCallback;
161
162 // Add a cookie on a bunch of host
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100163 base::PerfTimeLogger timer("Cookie_monster_add_many_hosts");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000164 for (std::vector<GURL>::const_iterator it = gurls.begin();
165 it != gurls.end(); ++it) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100166 setCookieCallback.SetCookie(cm.get(), *it, cookie);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000167 }
168 timer.Done();
169
170 GetCookiesCallback getCookiesCallback;
171
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100172 base::PerfTimeLogger timer2("Cookie_monster_query_many_hosts");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000173 for (std::vector<GURL>::const_iterator it = gurls.begin();
174 it != gurls.end(); ++it) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100175 getCookiesCallback.GetCookies(cm.get(), *it);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000176 }
177 timer2.Done();
178
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100179 base::PerfTimeLogger timer3("Cookie_monster_deleteall_many_hosts");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000180 cm->DeleteAllAsync(CookieMonster::DeleteCallback());
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100181 base::MessageLoop::current()->RunUntilIdle();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000182 timer3.Done();
183}
184
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000185TEST_F(CookieMonsterTest, TestDomainTree) {
186 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
187 GetCookiesCallback getCookiesCallback;
188 SetCookieCallback setCookieCallback;
189 const char* domain_cookie_format_tree = "a=b; domain=%s";
190 const std::string domain_base("top.com");
191
192 std::vector<std::string> domain_list;
193
194 // Create a balanced binary tree of domains on which the cookie is set.
195 domain_list.push_back(domain_base);
196 for (int i1 = 0; i1 < 2; i1++) {
197 std::string domain_base_1((i1 ? "a." : "b.") + domain_base);
198 EXPECT_EQ("top.com", cm->GetKey(domain_base_1));
199 domain_list.push_back(domain_base_1);
200 for (int i2 = 0; i2 < 2; i2++) {
201 std::string domain_base_2((i2 ? "a." : "b.") + domain_base_1);
202 EXPECT_EQ("top.com", cm->GetKey(domain_base_2));
203 domain_list.push_back(domain_base_2);
204 for (int i3 = 0; i3 < 2; i3++) {
205 std::string domain_base_3((i3 ? "a." : "b.") + domain_base_2);
206 EXPECT_EQ("top.com", cm->GetKey(domain_base_3));
207 domain_list.push_back(domain_base_3);
208 for (int i4 = 0; i4 < 2; i4++) {
209 std::string domain_base_4((i4 ? "a." : "b.") + domain_base_3);
210 EXPECT_EQ("top.com", cm->GetKey(domain_base_4));
211 domain_list.push_back(domain_base_4);
212 }
213 }
214 }
215 }
216
217
218 EXPECT_EQ(31u, domain_list.size());
219 for (std::vector<std::string>::const_iterator it = domain_list.begin();
220 it != domain_list.end(); it++) {
221 GURL gurl("https://" + *it + "/");
222 const std::string cookie = base::StringPrintf(domain_cookie_format_tree,
223 it->c_str());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100224 setCookieCallback.SetCookie(cm.get(), gurl, cookie);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000225 }
226 EXPECT_EQ(31u, cm->GetAllCookies().size());
227
228 GURL probe_gurl("https://b.a.b.a.top.com/");
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100229 std::string cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl);
230 EXPECT_EQ(5, CountInString(cookie_line, '='))
231 << "Cookie line: " << cookie_line;
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100232 base::PerfTimeLogger timer("Cookie_monster_query_domain_tree");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000233 for (int i = 0; i < kNumCookies; i++) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100234 getCookiesCallback.GetCookies(cm.get(), probe_gurl);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000235 }
236 timer.Done();
237}
238
239TEST_F(CookieMonsterTest, TestDomainLine) {
240 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
241 SetCookieCallback setCookieCallback;
242 GetCookiesCallback getCookiesCallback;
243 std::vector<std::string> domain_list;
244 GURL probe_gurl("https://b.a.b.a.top.com/");
245 std::string cookie_line;
246
247 // Create a line of 32 domain cookies such that all cookies stored
248 // by effective TLD+1 will apply to probe GURL.
249 // (TLD + 1 is the level above .com/org/net/etc, e.g. "top.com"
250 // or "google.com". "Effective" is added to include sites like
251 // bbc.co.uk, where the effetive TLD+1 is more than one level
252 // below the top level.)
253 domain_list.push_back("a.top.com");
254 domain_list.push_back("b.a.top.com");
255 domain_list.push_back("a.b.a.top.com");
256 domain_list.push_back("b.a.b.a.top.com");
257 EXPECT_EQ(4u, domain_list.size());
258
259 const char* domain_cookie_format_line = "a%03d=b; domain=%s";
260 for (int i = 0; i < 8; i++) {
261 for (std::vector<std::string>::const_iterator it = domain_list.begin();
262 it != domain_list.end(); it++) {
263 GURL gurl("https://" + *it + "/");
264 const std::string cookie = base::StringPrintf(domain_cookie_format_line,
265 i, it->c_str());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100266 setCookieCallback.SetCookie(cm.get(), gurl, cookie);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000267 }
268 }
269
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100270 cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000271 EXPECT_EQ(32, CountInString(cookie_line, '='));
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100272 base::PerfTimeLogger timer2("Cookie_monster_query_domain_line");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000273 for (int i = 0; i < kNumCookies; i++) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100274 getCookiesCallback.GetCookies(cm.get(), probe_gurl);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000275 }
276 timer2.Done();
277}
278
279TEST_F(CookieMonsterTest, TestImport) {
280 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore);
281 std::vector<CanonicalCookie*> initial_cookies;
282 GetCookiesCallback getCookiesCallback;
283
284 // We want to setup a fairly large backing store, with 300 domains of 50
285 // cookies each. Creation times must be unique.
286 int64 time_tick(base::Time::Now().ToInternalValue());
287
288 for (int domain_num = 0; domain_num < 300; domain_num++) {
289 std::string domain_name(base::StringPrintf(".Domain_%d.com", domain_num));
290 std::string gurl("www" + domain_name);
291 for (int cookie_num = 0; cookie_num < 50; cookie_num++) {
292 std::string cookie_line(base::StringPrintf("Cookie_%d=1; Path=/",
293 cookie_num));
294 AddCookieToList(gurl, cookie_line,
295 base::Time::FromInternalValue(time_tick++),
296 &initial_cookies);
297 }
298 }
299
300 store->SetLoadExpectation(true, initial_cookies);
301
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100302 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000303
304 // Import will happen on first access.
305 GURL gurl("www.google.com");
306 CookieOptions options;
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100307 base::PerfTimeLogger timer("Cookie_monster_import_from_store");
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100308 getCookiesCallback.GetCookies(cm.get(), gurl);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000309 timer.Done();
310
311 // Just confirm keys were set as expected.
312 EXPECT_EQ("domain_1.com", cm->GetKey("www.Domain_1.com"));
313}
314
315TEST_F(CookieMonsterTest, TestGetKey) {
316 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100317 base::PerfTimeLogger timer("Cookie_monster_get_key");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000318 for (int i = 0; i < kNumCookies; i++)
319 cm->GetKey("www.google.com");
320 timer.Done();
321}
322
323// This test is probing for whether garbage collection happens when it
324// shouldn't. This will not in general be visible functionally, since
325// if GC runs twice in a row without any change to the store, the second
326// GC run will not do anything the first one didn't. That's why this is
327// a performance test. The test should be considered to pass if all the
328// times reported are approximately the same--this indicates that no GC
329// happened repeatedly for any case.
330TEST_F(CookieMonsterTest, TestGCTimes) {
331 SetCookieCallback setCookieCallback;
332
333 const struct TestCase {
334 const char* name;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000335 size_t num_cookies;
336 size_t num_old_cookies;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000337 } test_cases[] = {
338 {
339 // A whole lot of recent cookies; gc shouldn't happen.
340 "all_recent",
341 CookieMonster::kMaxCookies * 2,
342 0,
343 }, {
344 // Some old cookies, but still overflowing max.
345 "mostly_recent",
346 CookieMonster::kMaxCookies * 2,
347 CookieMonster::kMaxCookies / 2,
348 }, {
349 // Old cookies enough to bring us right down to our purge line.
350 "balanced",
351 CookieMonster::kMaxCookies * 2,
352 CookieMonster::kMaxCookies + CookieMonster::kPurgeCookies + 1,
353 }, {
354 "mostly_old",
355 // Old cookies enough to bring below our purge line (which we
356 // shouldn't do).
357 CookieMonster::kMaxCookies * 2,
358 CookieMonster::kMaxCookies * 3 / 4,
359 }, {
360 "less_than_gc_thresh",
361 // Few enough cookies that gc shouldn't happen at all.
362 CookieMonster::kMaxCookies - 5,
363 0,
364 },
365 };
366 for (int ci = 0; ci < static_cast<int>(ARRAYSIZE_UNSAFE(test_cases)); ++ci) {
367 const TestCase& test_case(test_cases[ci]);
368 scoped_refptr<CookieMonster> cm(
369 CreateMonsterFromStoreForGC(
370 test_case.num_cookies, test_case.num_old_cookies,
371 CookieMonster::kSafeFromGlobalPurgeDays * 2));
372
373 GURL gurl("http://google.com");
374 std::string cookie_line("z=3");
375 // Trigger the Garbage collection we're allowed.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100376 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000377
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100378 base::PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000379 for (int i = 0; i < kNumCookies; i++)
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100380 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000381 timer.Done();
382 }
383}
384
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000385} // namespace net