| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 // Note that the performance tests currently all operate on a loaded cookie | 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). | 46 // store (or, more precisely, one that has no backing persistent store). |
| 47 // Therefore, callbacks will actually always complete synchronously. If the | 47 // Therefore, callbacks will actually always complete synchronously. If the |
| 48 // tests get more advanced we need to add other means of signaling | 48 // tests get more advanced we need to add other means of signaling |
| 49 // completion. | 49 // completion. |
| 50 base::MessageLoop::current()->RunUntilIdle(); | 50 base::MessageLoop::current()->RunUntilIdle(); |
| 51 EXPECT_TRUE(has_run_); | 51 EXPECT_TRUE(has_run_); |
| 52 has_run_ = false; | 52 has_run_ = false; |
| 53 } | 53 } |
| 54 | 54 |
| 55 void Run() { | 55 void Run() { has_run_ = true; } |
| 56 has_run_ = true; | |
| 57 } | |
| 58 | 56 |
| 59 bool has_run_; | 57 bool has_run_; |
| 60 }; | 58 }; |
| 61 | 59 |
| 62 class SetCookieCallback : public BaseCallback { | 60 class SetCookieCallback : public BaseCallback { |
| 63 public: | 61 public: |
| 64 void SetCookie( | 62 void SetCookie(CookieMonster* cm, |
| 65 CookieMonster* cm, const GURL& gurl, const std::string& cookie) { | 63 const GURL& gurl, |
| 66 cm->SetCookieWithOptionsAsync(gurl, cookie, options_, base::Bind( | 64 const std::string& cookie) { |
| 67 &SetCookieCallback::Run, base::Unretained(this))); | 65 cm->SetCookieWithOptionsAsync( |
| 66 gurl, |
| 67 cookie, |
| 68 options_, |
| 69 base::Bind(&SetCookieCallback::Run, base::Unretained(this))); |
| 68 WaitForCallback(); | 70 WaitForCallback(); |
| 69 } | 71 } |
| 72 |
| 70 private: | 73 private: |
| 71 void Run(bool success) { | 74 void Run(bool success) { |
| 72 EXPECT_TRUE(success); | 75 EXPECT_TRUE(success); |
| 73 BaseCallback::Run(); | 76 BaseCallback::Run(); |
| 74 } | 77 } |
| 75 net::CookieOptions options_; | 78 net::CookieOptions options_; |
| 76 }; | 79 }; |
| 77 | 80 |
| 78 class GetCookiesCallback : public BaseCallback { | 81 class GetCookiesCallback : public BaseCallback { |
| 79 public: | 82 public: |
| 80 const std::string& GetCookies(CookieMonster* cm, const GURL& gurl) { | 83 const std::string& GetCookies(CookieMonster* cm, const GURL& gurl) { |
| 81 cm->GetCookiesWithOptionsAsync(gurl, options_, base::Bind( | 84 cm->GetCookiesWithOptionsAsync( |
| 82 &GetCookiesCallback::Run, base::Unretained(this))); | 85 gurl, |
| 86 options_, |
| 87 base::Bind(&GetCookiesCallback::Run, base::Unretained(this))); |
| 83 WaitForCallback(); | 88 WaitForCallback(); |
| 84 return cookies_; | 89 return cookies_; |
| 85 } | 90 } |
| 86 | 91 |
| 87 private: | 92 private: |
| 88 void Run(const std::string& cookies) { | 93 void Run(const std::string& cookies) { |
| 89 cookies_ = cookies; | 94 cookies_ = cookies; |
| 90 BaseCallback::Run(); | 95 BaseCallback::Run(); |
| 91 } | 96 } |
| 92 std::string cookies_; | 97 std::string cookies_; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 122 for (int i = 0; i < kNumCookies; i++) { | 127 for (int i = 0; i < kNumCookies; i++) { |
| 123 cookies.push_back(base::StringPrintf("a%03d=b", i)); | 128 cookies.push_back(base::StringPrintf("a%03d=b", i)); |
| 124 } | 129 } |
| 125 | 130 |
| 126 SetCookieCallback setCookieCallback; | 131 SetCookieCallback setCookieCallback; |
| 127 | 132 |
| 128 // Add a bunch of cookies on a single host | 133 // Add a bunch of cookies on a single host |
| 129 base::PerfTimeLogger timer("Cookie_monster_add_single_host"); | 134 base::PerfTimeLogger timer("Cookie_monster_add_single_host"); |
| 130 | 135 |
| 131 for (std::vector<std::string>::const_iterator it = cookies.begin(); | 136 for (std::vector<std::string>::const_iterator it = cookies.begin(); |
| 132 it != cookies.end(); ++it) { | 137 it != cookies.end(); |
| 138 ++it) { |
| 133 setCookieCallback.SetCookie(cm.get(), GURL(kGoogleURL), *it); | 139 setCookieCallback.SetCookie(cm.get(), GURL(kGoogleURL), *it); |
| 134 } | 140 } |
| 135 timer.Done(); | 141 timer.Done(); |
| 136 | 142 |
| 137 GetCookiesCallback getCookiesCallback; | 143 GetCookiesCallback getCookiesCallback; |
| 138 | 144 |
| 139 base::PerfTimeLogger timer2("Cookie_monster_query_single_host"); | 145 base::PerfTimeLogger timer2("Cookie_monster_query_single_host"); |
| 140 for (std::vector<std::string>::const_iterator it = cookies.begin(); | 146 for (std::vector<std::string>::const_iterator it = cookies.begin(); |
| 141 it != cookies.end(); ++it) { | 147 it != cookies.end(); |
| 148 ++it) { |
| 142 getCookiesCallback.GetCookies(cm.get(), GURL(kGoogleURL)); | 149 getCookiesCallback.GetCookies(cm.get(), GURL(kGoogleURL)); |
| 143 } | 150 } |
| 144 timer2.Done(); | 151 timer2.Done(); |
| 145 | 152 |
| 146 base::PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); | 153 base::PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); |
| 147 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); | 154 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); |
| 148 base::MessageLoop::current()->RunUntilIdle(); | 155 base::MessageLoop::current()->RunUntilIdle(); |
| 149 timer3.Done(); | 156 timer3.Done(); |
| 150 } | 157 } |
| 151 | 158 |
| 152 TEST_F(CookieMonsterTest, TestAddCookieOnManyHosts) { | 159 TEST_F(CookieMonsterTest, TestAddCookieOnManyHosts) { |
| 153 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 160 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 154 std::string cookie(kCookieLine); | 161 std::string cookie(kCookieLine); |
| 155 std::vector<GURL> gurls; // just wanna have ffffuunnn | 162 std::vector<GURL> gurls; // just wanna have ffffuunnn |
| 156 for (int i = 0; i < kNumCookies; ++i) { | 163 for (int i = 0; i < kNumCookies; ++i) { |
| 157 gurls.push_back(GURL(base::StringPrintf("https://a%04d.izzle", i))); | 164 gurls.push_back(GURL(base::StringPrintf("https://a%04d.izzle", i))); |
| 158 } | 165 } |
| 159 | 166 |
| 160 SetCookieCallback setCookieCallback; | 167 SetCookieCallback setCookieCallback; |
| 161 | 168 |
| 162 // Add a cookie on a bunch of host | 169 // Add a cookie on a bunch of host |
| 163 base::PerfTimeLogger timer("Cookie_monster_add_many_hosts"); | 170 base::PerfTimeLogger timer("Cookie_monster_add_many_hosts"); |
| 164 for (std::vector<GURL>::const_iterator it = gurls.begin(); | 171 for (std::vector<GURL>::const_iterator it = gurls.begin(); it != gurls.end(); |
| 165 it != gurls.end(); ++it) { | 172 ++it) { |
| 166 setCookieCallback.SetCookie(cm.get(), *it, cookie); | 173 setCookieCallback.SetCookie(cm.get(), *it, cookie); |
| 167 } | 174 } |
| 168 timer.Done(); | 175 timer.Done(); |
| 169 | 176 |
| 170 GetCookiesCallback getCookiesCallback; | 177 GetCookiesCallback getCookiesCallback; |
| 171 | 178 |
| 172 base::PerfTimeLogger timer2("Cookie_monster_query_many_hosts"); | 179 base::PerfTimeLogger timer2("Cookie_monster_query_many_hosts"); |
| 173 for (std::vector<GURL>::const_iterator it = gurls.begin(); | 180 for (std::vector<GURL>::const_iterator it = gurls.begin(); it != gurls.end(); |
| 174 it != gurls.end(); ++it) { | 181 ++it) { |
| 175 getCookiesCallback.GetCookies(cm.get(), *it); | 182 getCookiesCallback.GetCookies(cm.get(), *it); |
| 176 } | 183 } |
| 177 timer2.Done(); | 184 timer2.Done(); |
| 178 | 185 |
| 179 base::PerfTimeLogger timer3("Cookie_monster_deleteall_many_hosts"); | 186 base::PerfTimeLogger timer3("Cookie_monster_deleteall_many_hosts"); |
| 180 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); | 187 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); |
| 181 base::MessageLoop::current()->RunUntilIdle(); | 188 base::MessageLoop::current()->RunUntilIdle(); |
| 182 timer3.Done(); | 189 timer3.Done(); |
| 183 } | 190 } |
| 184 | 191 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 207 domain_list.push_back(domain_base_3); | 214 domain_list.push_back(domain_base_3); |
| 208 for (int i4 = 0; i4 < 2; i4++) { | 215 for (int i4 = 0; i4 < 2; i4++) { |
| 209 std::string domain_base_4((i4 ? "a." : "b.") + domain_base_3); | 216 std::string domain_base_4((i4 ? "a." : "b.") + domain_base_3); |
| 210 EXPECT_EQ("top.com", cm->GetKey(domain_base_4)); | 217 EXPECT_EQ("top.com", cm->GetKey(domain_base_4)); |
| 211 domain_list.push_back(domain_base_4); | 218 domain_list.push_back(domain_base_4); |
| 212 } | 219 } |
| 213 } | 220 } |
| 214 } | 221 } |
| 215 } | 222 } |
| 216 | 223 |
| 217 | |
| 218 EXPECT_EQ(31u, domain_list.size()); | 224 EXPECT_EQ(31u, domain_list.size()); |
| 219 for (std::vector<std::string>::const_iterator it = domain_list.begin(); | 225 for (std::vector<std::string>::const_iterator it = domain_list.begin(); |
| 220 it != domain_list.end(); it++) { | 226 it != domain_list.end(); |
| 227 it++) { |
| 221 GURL gurl("https://" + *it + "/"); | 228 GURL gurl("https://" + *it + "/"); |
| 222 const std::string cookie = base::StringPrintf(domain_cookie_format_tree, | 229 const std::string cookie = |
| 223 it->c_str()); | 230 base::StringPrintf(domain_cookie_format_tree, it->c_str()); |
| 224 setCookieCallback.SetCookie(cm.get(), gurl, cookie); | 231 setCookieCallback.SetCookie(cm.get(), gurl, cookie); |
| 225 } | 232 } |
| 226 EXPECT_EQ(31u, cm->GetAllCookies().size()); | 233 EXPECT_EQ(31u, cm->GetAllCookies().size()); |
| 227 | 234 |
| 228 GURL probe_gurl("https://b.a.b.a.top.com/"); | 235 GURL probe_gurl("https://b.a.b.a.top.com/"); |
| 229 std::string cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl); | 236 std::string cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl); |
| 230 EXPECT_EQ(5, CountInString(cookie_line, '=')) | 237 EXPECT_EQ(5, CountInString(cookie_line, '=')) |
| 231 << "Cookie line: " << cookie_line; | 238 << "Cookie line: " << cookie_line; |
| 232 base::PerfTimeLogger timer("Cookie_monster_query_domain_tree"); | 239 base::PerfTimeLogger timer("Cookie_monster_query_domain_tree"); |
| 233 for (int i = 0; i < kNumCookies; i++) { | 240 for (int i = 0; i < kNumCookies; i++) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 252 // below the top level.) | 259 // below the top level.) |
| 253 domain_list.push_back("a.top.com"); | 260 domain_list.push_back("a.top.com"); |
| 254 domain_list.push_back("b.a.top.com"); | 261 domain_list.push_back("b.a.top.com"); |
| 255 domain_list.push_back("a.b.a.top.com"); | 262 domain_list.push_back("a.b.a.top.com"); |
| 256 domain_list.push_back("b.a.b.a.top.com"); | 263 domain_list.push_back("b.a.b.a.top.com"); |
| 257 EXPECT_EQ(4u, domain_list.size()); | 264 EXPECT_EQ(4u, domain_list.size()); |
| 258 | 265 |
| 259 const char* domain_cookie_format_line = "a%03d=b; domain=%s"; | 266 const char* domain_cookie_format_line = "a%03d=b; domain=%s"; |
| 260 for (int i = 0; i < 8; i++) { | 267 for (int i = 0; i < 8; i++) { |
| 261 for (std::vector<std::string>::const_iterator it = domain_list.begin(); | 268 for (std::vector<std::string>::const_iterator it = domain_list.begin(); |
| 262 it != domain_list.end(); it++) { | 269 it != domain_list.end(); |
| 270 it++) { |
| 263 GURL gurl("https://" + *it + "/"); | 271 GURL gurl("https://" + *it + "/"); |
| 264 const std::string cookie = base::StringPrintf(domain_cookie_format_line, | 272 const std::string cookie = |
| 265 i, it->c_str()); | 273 base::StringPrintf(domain_cookie_format_line, i, it->c_str()); |
| 266 setCookieCallback.SetCookie(cm.get(), gurl, cookie); | 274 setCookieCallback.SetCookie(cm.get(), gurl, cookie); |
| 267 } | 275 } |
| 268 } | 276 } |
| 269 | 277 |
| 270 cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl); | 278 cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl); |
| 271 EXPECT_EQ(32, CountInString(cookie_line, '=')); | 279 EXPECT_EQ(32, CountInString(cookie_line, '=')); |
| 272 base::PerfTimeLogger timer2("Cookie_monster_query_domain_line"); | 280 base::PerfTimeLogger timer2("Cookie_monster_query_domain_line"); |
| 273 for (int i = 0; i < kNumCookies; i++) { | 281 for (int i = 0; i < kNumCookies; i++) { |
| 274 getCookiesCallback.GetCookies(cm.get(), probe_gurl); | 282 getCookiesCallback.GetCookies(cm.get(), probe_gurl); |
| 275 } | 283 } |
| 276 timer2.Done(); | 284 timer2.Done(); |
| 277 } | 285 } |
| 278 | 286 |
| 279 TEST_F(CookieMonsterTest, TestImport) { | 287 TEST_F(CookieMonsterTest, TestImport) { |
| 280 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); | 288 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); |
| 281 std::vector<CanonicalCookie*> initial_cookies; | 289 std::vector<CanonicalCookie*> initial_cookies; |
| 282 GetCookiesCallback getCookiesCallback; | 290 GetCookiesCallback getCookiesCallback; |
| 283 | 291 |
| 284 // We want to setup a fairly large backing store, with 300 domains of 50 | 292 // We want to setup a fairly large backing store, with 300 domains of 50 |
| 285 // cookies each. Creation times must be unique. | 293 // cookies each. Creation times must be unique. |
| 286 int64 time_tick(base::Time::Now().ToInternalValue()); | 294 int64 time_tick(base::Time::Now().ToInternalValue()); |
| 287 | 295 |
| 288 for (int domain_num = 0; domain_num < 300; domain_num++) { | 296 for (int domain_num = 0; domain_num < 300; domain_num++) { |
| 289 std::string domain_name(base::StringPrintf(".Domain_%d.com", domain_num)); | 297 std::string domain_name(base::StringPrintf(".Domain_%d.com", domain_num)); |
| 290 std::string gurl("www" + domain_name); | 298 std::string gurl("www" + domain_name); |
| 291 for (int cookie_num = 0; cookie_num < 50; cookie_num++) { | 299 for (int cookie_num = 0; cookie_num < 50; cookie_num++) { |
| 292 std::string cookie_line(base::StringPrintf("Cookie_%d=1; Path=/", | 300 std::string cookie_line( |
| 293 cookie_num)); | 301 base::StringPrintf("Cookie_%d=1; Path=/", cookie_num)); |
| 294 AddCookieToList(gurl, cookie_line, | 302 AddCookieToList(gurl, |
| 303 cookie_line, |
| 295 base::Time::FromInternalValue(time_tick++), | 304 base::Time::FromInternalValue(time_tick++), |
| 296 &initial_cookies); | 305 &initial_cookies); |
| 297 } | 306 } |
| 298 } | 307 } |
| 299 | 308 |
| 300 store->SetLoadExpectation(true, initial_cookies); | 309 store->SetLoadExpectation(true, initial_cookies); |
| 301 | 310 |
| 302 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL)); | 311 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL)); |
| 303 | 312 |
| 304 // Import will happen on first access. | 313 // Import will happen on first access. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 328 // times reported are approximately the same--this indicates that no GC | 337 // times reported are approximately the same--this indicates that no GC |
| 329 // happened repeatedly for any case. | 338 // happened repeatedly for any case. |
| 330 TEST_F(CookieMonsterTest, TestGCTimes) { | 339 TEST_F(CookieMonsterTest, TestGCTimes) { |
| 331 SetCookieCallback setCookieCallback; | 340 SetCookieCallback setCookieCallback; |
| 332 | 341 |
| 333 const struct TestCase { | 342 const struct TestCase { |
| 334 const char* name; | 343 const char* name; |
| 335 size_t num_cookies; | 344 size_t num_cookies; |
| 336 size_t num_old_cookies; | 345 size_t num_old_cookies; |
| 337 } test_cases[] = { | 346 } test_cases[] = { |
| 338 { | 347 {// A whole lot of recent cookies; gc shouldn't happen. |
| 339 // A whole lot of recent cookies; gc shouldn't happen. | 348 "all_recent", CookieMonster::kMaxCookies * 2, 0, |
| 340 "all_recent", | 349 }, |
| 341 CookieMonster::kMaxCookies * 2, | 350 {// Some old cookies, but still overflowing max. |
| 342 0, | 351 "mostly_recent", CookieMonster::kMaxCookies * 2, |
| 343 }, { | 352 CookieMonster::kMaxCookies / 2, |
| 344 // Some old cookies, but still overflowing max. | 353 }, |
| 345 "mostly_recent", | 354 {// Old cookies enough to bring us right down to our purge line. |
| 346 CookieMonster::kMaxCookies * 2, | 355 "balanced", CookieMonster::kMaxCookies * 2, |
| 347 CookieMonster::kMaxCookies / 2, | 356 CookieMonster::kMaxCookies + CookieMonster::kPurgeCookies + 1, |
| 348 }, { | 357 }, |
| 349 // Old cookies enough to bring us right down to our purge line. | 358 { |
| 350 "balanced", | 359 "mostly_old", |
| 351 CookieMonster::kMaxCookies * 2, | 360 // Old cookies enough to bring below our purge line (which we |
| 352 CookieMonster::kMaxCookies + CookieMonster::kPurgeCookies + 1, | 361 // shouldn't do). |
| 353 }, { | 362 CookieMonster::kMaxCookies * 2, CookieMonster::kMaxCookies * 3 / 4, |
| 354 "mostly_old", | 363 }, |
| 355 // Old cookies enough to bring below our purge line (which we | 364 { |
| 356 // shouldn't do). | 365 "less_than_gc_thresh", |
| 357 CookieMonster::kMaxCookies * 2, | 366 // Few enough cookies that gc shouldn't happen at all. |
| 358 CookieMonster::kMaxCookies * 3 / 4, | 367 CookieMonster::kMaxCookies - 5, 0, |
| 359 }, { | 368 }, |
| 360 "less_than_gc_thresh", | 369 }; |
| 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) { | 370 for (int ci = 0; ci < static_cast<int>(ARRAYSIZE_UNSAFE(test_cases)); ++ci) { |
| 367 const TestCase& test_case(test_cases[ci]); | 371 const TestCase& test_case(test_cases[ci]); |
| 368 scoped_refptr<CookieMonster> cm( | 372 scoped_refptr<CookieMonster> cm(CreateMonsterFromStoreForGC( |
| 369 CreateMonsterFromStoreForGC( | 373 test_case.num_cookies, |
| 370 test_case.num_cookies, test_case.num_old_cookies, | 374 test_case.num_old_cookies, |
| 371 CookieMonster::kSafeFromGlobalPurgeDays * 2)); | 375 CookieMonster::kSafeFromGlobalPurgeDays * 2)); |
| 372 | 376 |
| 373 GURL gurl("http://google.com"); | 377 GURL gurl("http://google.com"); |
| 374 std::string cookie_line("z=3"); | 378 std::string cookie_line("z=3"); |
| 375 // Trigger the Garbage collection we're allowed. | 379 // Trigger the Garbage collection we're allowed. |
| 376 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); | 380 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); |
| 377 | 381 |
| 378 base::PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); | 382 base::PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); |
| 379 for (int i = 0; i < kNumCookies; i++) | 383 for (int i = 0; i < kNumCookies; i++) |
| 380 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); | 384 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); |
| 381 timer.Done(); | 385 timer.Done(); |
| 382 } | 386 } |
| 383 } | 387 } |
| 384 | 388 |
| 385 } // namespace net | 389 } // namespace net |
| OLD | NEW |