| 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 #include <memory> | 
| 6 | 7 | 
| 7 #include "base/bind.h" | 8 #include "base/bind.h" | 
| 8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" | 
| 9 #include "base/memory/scoped_ptr.h" |  | 
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" | 
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" | 
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" | 
| 13 #include "base/test/perf_time_logger.h" | 13 #include "base/test/perf_time_logger.h" | 
| 14 #include "net/cookies/canonical_cookie.h" | 14 #include "net/cookies/canonical_cookie.h" | 
| 15 #include "net/cookies/cookie_monster.h" | 15 #include "net/cookies/cookie_monster.h" | 
| 16 #include "net/cookies/cookie_monster_store_test.h" | 16 #include "net/cookies/cookie_monster_store_test.h" | 
| 17 #include "net/cookies/parsed_cookie.h" | 17 #include "net/cookies/parsed_cookie.h" | 
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" | 
| 19 #include "url/gurl.h" | 19 #include "url/gurl.h" | 
| 20 | 20 | 
| 21 namespace net { | 21 namespace net { | 
| 22 | 22 | 
| 23 namespace { | 23 namespace { | 
| 24 | 24 | 
| 25 const int kNumCookies = 20000; | 25 const int kNumCookies = 20000; | 
| 26 const char kCookieLine[] = "A  = \"b=;\\\"\"  ;secure;;;"; | 26 const char kCookieLine[] = "A  = \"b=;\\\"\"  ;secure;;;"; | 
| 27 const char kGoogleURL[] = "http://www.google.izzle"; | 27 const char kGoogleURL[] = "http://www.google.izzle"; | 
| 28 | 28 | 
| 29 int CountInString(const std::string& str, char c) { | 29 int CountInString(const std::string& str, char c) { | 
| 30   return std::count(str.begin(), str.end(), c); | 30   return std::count(str.begin(), str.end(), c); | 
| 31 } | 31 } | 
| 32 | 32 | 
| 33 class CookieMonsterTest : public testing::Test { | 33 class CookieMonsterTest : public testing::Test { | 
| 34  public: | 34  public: | 
| 35   CookieMonsterTest() : message_loop_(new base::MessageLoopForIO()) {} | 35   CookieMonsterTest() : message_loop_(new base::MessageLoopForIO()) {} | 
| 36 | 36 | 
| 37  private: | 37  private: | 
| 38   scoped_ptr<base::MessageLoop> message_loop_; | 38   std::unique_ptr<base::MessageLoop> message_loop_; | 
| 39 }; | 39 }; | 
| 40 | 40 | 
| 41 class BaseCallback { | 41 class BaseCallback { | 
| 42  public: | 42  public: | 
| 43   BaseCallback() : has_run_(false) {} | 43   BaseCallback() : has_run_(false) {} | 
| 44 | 44 | 
| 45  protected: | 45  protected: | 
| 46   void WaitForCallback() { | 46   void WaitForCallback() { | 
| 47     // Note that the performance tests currently all operate on a loaded cookie | 47     // Note that the performance tests currently all operate on a loaded cookie | 
| 48     // store (or, more precisely, one that has no backing persistent store). | 48     // store (or, more precisely, one that has no backing persistent store). | 
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 114   cookie += kCookieLine; | 114   cookie += kCookieLine; | 
| 115   base::PerfTimeLogger timer("Parsed_cookie_parse_big_cookies"); | 115   base::PerfTimeLogger timer("Parsed_cookie_parse_big_cookies"); | 
| 116   for (int i = 0; i < kNumCookies; ++i) { | 116   for (int i = 0; i < kNumCookies; ++i) { | 
| 117     ParsedCookie pc(cookie); | 117     ParsedCookie pc(cookie); | 
| 118     EXPECT_TRUE(pc.IsValid()); | 118     EXPECT_TRUE(pc.IsValid()); | 
| 119   } | 119   } | 
| 120   timer.Done(); | 120   timer.Done(); | 
| 121 } | 121 } | 
| 122 | 122 | 
| 123 TEST_F(CookieMonsterTest, TestAddCookiesOnSingleHost) { | 123 TEST_F(CookieMonsterTest, TestAddCookiesOnSingleHost) { | 
| 124   scoped_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 124   std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 
| 125   std::vector<std::string> cookies; | 125   std::vector<std::string> cookies; | 
| 126   for (int i = 0; i < kNumCookies; i++) { | 126   for (int i = 0; i < kNumCookies; i++) { | 
| 127     cookies.push_back(base::StringPrintf("a%03d=b", i)); | 127     cookies.push_back(base::StringPrintf("a%03d=b", i)); | 
| 128   } | 128   } | 
| 129 | 129 | 
| 130   SetCookieCallback setCookieCallback; | 130   SetCookieCallback setCookieCallback; | 
| 131 | 131 | 
| 132   // Add a bunch of cookies on a single host | 132   // Add a bunch of cookies on a single host | 
| 133   base::PerfTimeLogger timer("Cookie_monster_add_single_host"); | 133   base::PerfTimeLogger timer("Cookie_monster_add_single_host"); | 
| 134 | 134 | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
| 147   } | 147   } | 
| 148   timer2.Done(); | 148   timer2.Done(); | 
| 149 | 149 | 
| 150   base::PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); | 150   base::PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); | 
| 151   cm->DeleteAllAsync(CookieMonster::DeleteCallback()); | 151   cm->DeleteAllAsync(CookieMonster::DeleteCallback()); | 
| 152   base::MessageLoop::current()->RunUntilIdle(); | 152   base::MessageLoop::current()->RunUntilIdle(); | 
| 153   timer3.Done(); | 153   timer3.Done(); | 
| 154 } | 154 } | 
| 155 | 155 | 
| 156 TEST_F(CookieMonsterTest, TestAddCookieOnManyHosts) { | 156 TEST_F(CookieMonsterTest, TestAddCookieOnManyHosts) { | 
| 157   scoped_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 157   std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 
| 158   std::string cookie(kCookieLine); | 158   std::string cookie(kCookieLine); | 
| 159   std::vector<GURL> gurls;  // just wanna have ffffuunnn | 159   std::vector<GURL> gurls;  // just wanna have ffffuunnn | 
| 160   for (int i = 0; i < kNumCookies; ++i) { | 160   for (int i = 0; i < kNumCookies; ++i) { | 
| 161     gurls.push_back(GURL(base::StringPrintf("https://a%04d.izzle", i))); | 161     gurls.push_back(GURL(base::StringPrintf("https://a%04d.izzle", i))); | 
| 162   } | 162   } | 
| 163 | 163 | 
| 164   SetCookieCallback setCookieCallback; | 164   SetCookieCallback setCookieCallback; | 
| 165 | 165 | 
| 166   // Add a cookie on a bunch of host | 166   // Add a cookie on a bunch of host | 
| 167   base::PerfTimeLogger timer("Cookie_monster_add_many_hosts"); | 167   base::PerfTimeLogger timer("Cookie_monster_add_many_hosts"); | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
| 180   } | 180   } | 
| 181   timer2.Done(); | 181   timer2.Done(); | 
| 182 | 182 | 
| 183   base::PerfTimeLogger timer3("Cookie_monster_deleteall_many_hosts"); | 183   base::PerfTimeLogger timer3("Cookie_monster_deleteall_many_hosts"); | 
| 184   cm->DeleteAllAsync(CookieMonster::DeleteCallback()); | 184   cm->DeleteAllAsync(CookieMonster::DeleteCallback()); | 
| 185   base::MessageLoop::current()->RunUntilIdle(); | 185   base::MessageLoop::current()->RunUntilIdle(); | 
| 186   timer3.Done(); | 186   timer3.Done(); | 
| 187 } | 187 } | 
| 188 | 188 | 
| 189 TEST_F(CookieMonsterTest, TestDomainTree) { | 189 TEST_F(CookieMonsterTest, TestDomainTree) { | 
| 190   scoped_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 190   std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 
| 191   GetCookiesCallback getCookiesCallback; | 191   GetCookiesCallback getCookiesCallback; | 
| 192   SetCookieCallback setCookieCallback; | 192   SetCookieCallback setCookieCallback; | 
| 193   const char domain_cookie_format_tree[] = "a=b; domain=%s"; | 193   const char domain_cookie_format_tree[] = "a=b; domain=%s"; | 
| 194   const std::string domain_base("top.com"); | 194   const std::string domain_base("top.com"); | 
| 195 | 195 | 
| 196   std::vector<std::string> domain_list; | 196   std::vector<std::string> domain_list; | 
| 197 | 197 | 
| 198   // Create a balanced binary tree of domains on which the cookie is set. | 198   // Create a balanced binary tree of domains on which the cookie is set. | 
| 199   domain_list.push_back(domain_base); | 199   domain_list.push_back(domain_base); | 
| 200   for (int i1 = 0; i1 < 2; i1++) { | 200   for (int i1 = 0; i1 < 2; i1++) { | 
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 233   EXPECT_EQ(5, CountInString(cookie_line, '=')) | 233   EXPECT_EQ(5, CountInString(cookie_line, '=')) | 
| 234       << "Cookie line: " << cookie_line; | 234       << "Cookie line: " << cookie_line; | 
| 235   base::PerfTimeLogger timer("Cookie_monster_query_domain_tree"); | 235   base::PerfTimeLogger timer("Cookie_monster_query_domain_tree"); | 
| 236   for (int i = 0; i < kNumCookies; i++) { | 236   for (int i = 0; i < kNumCookies; i++) { | 
| 237     getCookiesCallback.GetCookies(cm.get(), probe_gurl); | 237     getCookiesCallback.GetCookies(cm.get(), probe_gurl); | 
| 238   } | 238   } | 
| 239   timer.Done(); | 239   timer.Done(); | 
| 240 } | 240 } | 
| 241 | 241 | 
| 242 TEST_F(CookieMonsterTest, TestDomainLine) { | 242 TEST_F(CookieMonsterTest, TestDomainLine) { | 
| 243   scoped_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 243   std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 
| 244   SetCookieCallback setCookieCallback; | 244   SetCookieCallback setCookieCallback; | 
| 245   GetCookiesCallback getCookiesCallback; | 245   GetCookiesCallback getCookiesCallback; | 
| 246   std::vector<std::string> domain_list; | 246   std::vector<std::string> domain_list; | 
| 247   GURL probe_gurl("https://b.a.b.a.top.com/"); | 247   GURL probe_gurl("https://b.a.b.a.top.com/"); | 
| 248   std::string cookie_line; | 248   std::string cookie_line; | 
| 249 | 249 | 
| 250   // Create a line of 32 domain cookies such that all cookies stored | 250   // Create a line of 32 domain cookies such that all cookies stored | 
| 251   // by effective TLD+1 will apply to probe GURL. | 251   // by effective TLD+1 will apply to probe GURL. | 
| 252   // (TLD + 1 is the level above .com/org/net/etc, e.g. "top.com" | 252   // (TLD + 1 is the level above .com/org/net/etc, e.g. "top.com" | 
| 253   // or "google.com".  "Effective" is added to include sites like | 253   // or "google.com".  "Effective" is added to include sites like | 
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 294       std::string cookie_line( | 294       std::string cookie_line( | 
| 295           base::StringPrintf("Cookie_%d=1; Path=/", cookie_num)); | 295           base::StringPrintf("Cookie_%d=1; Path=/", cookie_num)); | 
| 296       AddCookieToList(gurl, cookie_line, | 296       AddCookieToList(gurl, cookie_line, | 
| 297                       base::Time::FromInternalValue(time_tick++), | 297                       base::Time::FromInternalValue(time_tick++), | 
| 298                       &initial_cookies); | 298                       &initial_cookies); | 
| 299     } | 299     } | 
| 300   } | 300   } | 
| 301 | 301 | 
| 302   store->SetLoadExpectation(true, initial_cookies); | 302   store->SetLoadExpectation(true, initial_cookies); | 
| 303 | 303 | 
| 304   scoped_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); | 304   std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); | 
| 305 | 305 | 
| 306   // Import will happen on first access. | 306   // Import will happen on first access. | 
| 307   GURL gurl("www.google.com"); | 307   GURL gurl("www.google.com"); | 
| 308   CookieOptions options; | 308   CookieOptions options; | 
| 309   base::PerfTimeLogger timer("Cookie_monster_import_from_store"); | 309   base::PerfTimeLogger timer("Cookie_monster_import_from_store"); | 
| 310   getCookiesCallback.GetCookies(cm.get(), gurl); | 310   getCookiesCallback.GetCookies(cm.get(), gurl); | 
| 311   timer.Done(); | 311   timer.Done(); | 
| 312 | 312 | 
| 313   // Just confirm keys were set as expected. | 313   // Just confirm keys were set as expected. | 
| 314   EXPECT_EQ("domain_1.com", cm->GetKey("www.Domain_1.com")); | 314   EXPECT_EQ("domain_1.com", cm->GetKey("www.Domain_1.com")); | 
| 315 } | 315 } | 
| 316 | 316 | 
| 317 TEST_F(CookieMonsterTest, TestGetKey) { | 317 TEST_F(CookieMonsterTest, TestGetKey) { | 
| 318   scoped_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 318   std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 
| 319   base::PerfTimeLogger timer("Cookie_monster_get_key"); | 319   base::PerfTimeLogger timer("Cookie_monster_get_key"); | 
| 320   for (int i = 0; i < kNumCookies; i++) | 320   for (int i = 0; i < kNumCookies; i++) | 
| 321     cm->GetKey("www.google.com"); | 321     cm->GetKey("www.google.com"); | 
| 322   timer.Done(); | 322   timer.Done(); | 
| 323 } | 323 } | 
| 324 | 324 | 
| 325 // This test is probing for whether garbage collection happens when it | 325 // This test is probing for whether garbage collection happens when it | 
| 326 // shouldn't.  This will not in general be visible functionally, since | 326 // shouldn't.  This will not in general be visible functionally, since | 
| 327 // if GC runs twice in a row without any change to the store, the second | 327 // if GC runs twice in a row without any change to the store, the second | 
| 328 // GC run will not do anything the first one didn't.  That's why this is | 328 // GC run will not do anything the first one didn't.  That's why this is | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 364       }, | 364       }, | 
| 365       { | 365       { | 
| 366        "less_than_gc_thresh", | 366        "less_than_gc_thresh", | 
| 367        // Few enough cookies that gc shouldn't happen at all. | 367        // Few enough cookies that gc shouldn't happen at all. | 
| 368        CookieMonster::kMaxCookies - 5, | 368        CookieMonster::kMaxCookies - 5, | 
| 369        0, | 369        0, | 
| 370       }, | 370       }, | 
| 371   }; | 371   }; | 
| 372   for (int ci = 0; ci < static_cast<int>(arraysize(test_cases)); ++ci) { | 372   for (int ci = 0; ci < static_cast<int>(arraysize(test_cases)); ++ci) { | 
| 373     const TestCase& test_case(test_cases[ci]); | 373     const TestCase& test_case(test_cases[ci]); | 
| 374     scoped_ptr<CookieMonster> cm = CreateMonsterFromStoreForGC( | 374     std::unique_ptr<CookieMonster> cm = CreateMonsterFromStoreForGC( | 
| 375         test_case.num_cookies, test_case.num_old_cookies, 0, 0, | 375         test_case.num_cookies, test_case.num_old_cookies, 0, 0, | 
| 376         CookieMonster::kSafeFromGlobalPurgeDays * 2); | 376         CookieMonster::kSafeFromGlobalPurgeDays * 2); | 
| 377 | 377 | 
| 378     GURL gurl("http://google.com"); | 378     GURL gurl("http://google.com"); | 
| 379     std::string cookie_line("z=3"); | 379     std::string cookie_line("z=3"); | 
| 380     // Trigger the Garbage collection we're allowed. | 380     // Trigger the Garbage collection we're allowed. | 
| 381     setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); | 381     setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); | 
| 382 | 382 | 
| 383     base::PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); | 383     base::PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); | 
| 384     for (int i = 0; i < kNumCookies; i++) | 384     for (int i = 0; i < kNumCookies; i++) | 
| 385       setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); | 385       setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); | 
| 386     timer.Done(); | 386     timer.Done(); | 
| 387   } | 387   } | 
| 388 } | 388 } | 
| 389 | 389 | 
| 390 }  // namespace net | 390 }  // namespace net | 
| OLD | NEW | 
|---|