| 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 #include <memory> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/test/perf_time_logger.h" | 14 #include "base/test/perf_time_logger.h" |
| 15 #include "net/cookies/canonical_cookie.h" | 15 #include "net/cookies/canonical_cookie.h" |
| 16 #include "net/cookies/cookie_monster.h" | 16 #include "net/cookies/cookie_monster.h" |
| 17 #include "net/cookies/cookie_monster_store_test.h" | 17 #include "net/cookies/cookie_monster_store_test.h" |
| 18 #include "net/cookies/parsed_cookie.h" | 18 #include "net/cookies/parsed_cookie.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 | 21 |
| 22 namespace net { | 22 namespace net { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const int kNumCookies = 20000; | 26 const int kNumCookies = 20000; |
| 27 const char kCookieLine[] = "A = \"b=;\\\"\" ;secure;;;"; | 27 const char kCookieLine[] = "A = \"b=;\\\"\" ;secure;;;"; |
| 28 const char kGoogleURL[] = "http://www.google.izzle"; | 28 const char kGoogleURL[] = "http://www.foo.com"; |
| 29 | 29 |
| 30 int CountInString(const std::string& str, char c) { | 30 int CountInString(const std::string& str, char c) { |
| 31 return std::count(str.begin(), str.end(), c); | 31 return std::count(str.begin(), str.end(), c); |
| 32 } | 32 } |
| 33 | 33 |
| 34 class CookieMonsterTest : public testing::Test { | 34 class CookieMonsterTest : public testing::Test { |
| 35 public: | 35 public: |
| 36 CookieMonsterTest() : message_loop_(new base::MessageLoopForIO()) {} | 36 CookieMonsterTest() : message_loop_(new base::MessageLoopForIO()) {} |
| 37 | 37 |
| 38 private: | 38 private: |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 base::Time::FromInternalValue(time_tick++), | 298 base::Time::FromInternalValue(time_tick++), |
| 299 &initial_cookies); | 299 &initial_cookies); |
| 300 } | 300 } |
| 301 } | 301 } |
| 302 | 302 |
| 303 store->SetLoadExpectation(true, std::move(initial_cookies)); | 303 store->SetLoadExpectation(true, std::move(initial_cookies)); |
| 304 | 304 |
| 305 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); | 305 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); |
| 306 | 306 |
| 307 // Import will happen on first access. | 307 // Import will happen on first access. |
| 308 GURL gurl("www.google.com"); | 308 GURL gurl("www.foo.com"); |
| 309 CookieOptions options; | 309 CookieOptions options; |
| 310 base::PerfTimeLogger timer("Cookie_monster_import_from_store"); | 310 base::PerfTimeLogger timer("Cookie_monster_import_from_store"); |
| 311 getCookiesCallback.GetCookies(cm.get(), gurl); | 311 getCookiesCallback.GetCookies(cm.get(), gurl); |
| 312 timer.Done(); | 312 timer.Done(); |
| 313 | 313 |
| 314 // Just confirm keys were set as expected. | 314 // Just confirm keys were set as expected. |
| 315 EXPECT_EQ("domain_1.com", cm->GetKey("www.Domain_1.com")); | 315 EXPECT_EQ("domain_1.com", cm->GetKey("www.Domain_1.com")); |
| 316 } | 316 } |
| 317 | 317 |
| 318 TEST_F(CookieMonsterTest, TestGetKey) { | 318 TEST_F(CookieMonsterTest, TestGetKey) { |
| 319 std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); | 319 std::unique_ptr<CookieMonster> cm(new CookieMonster(nullptr, nullptr)); |
| 320 base::PerfTimeLogger timer("Cookie_monster_get_key"); | 320 base::PerfTimeLogger timer("Cookie_monster_get_key"); |
| 321 for (int i = 0; i < kNumCookies; i++) | 321 for (int i = 0; i < kNumCookies; i++) |
| 322 cm->GetKey("www.google.com"); | 322 cm->GetKey("www.foo.com"); |
| 323 timer.Done(); | 323 timer.Done(); |
| 324 } | 324 } |
| 325 | 325 |
| 326 // This test is probing for whether garbage collection happens when it | 326 // This test is probing for whether garbage collection happens when it |
| 327 // shouldn't. This will not in general be visible functionally, since | 327 // shouldn't. This will not in general be visible functionally, since |
| 328 // if GC runs twice in a row without any change to the store, the second | 328 // if GC runs twice in a row without any change to the store, the second |
| 329 // GC run will not do anything the first one didn't. That's why this is | 329 // GC run will not do anything the first one didn't. That's why this is |
| 330 // a performance test. The test should be considered to pass if all the | 330 // a performance test. The test should be considered to pass if all the |
| 331 // times reported are approximately the same--this indicates that no GC | 331 // times reported are approximately the same--this indicates that no GC |
| 332 // happened repeatedly for any case. | 332 // happened repeatedly for any case. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 CookieMonster::kMaxCookies - 5, | 369 CookieMonster::kMaxCookies - 5, |
| 370 0, | 370 0, |
| 371 }, | 371 }, |
| 372 }; | 372 }; |
| 373 for (int ci = 0; ci < static_cast<int>(arraysize(test_cases)); ++ci) { | 373 for (int ci = 0; ci < static_cast<int>(arraysize(test_cases)); ++ci) { |
| 374 const TestCase& test_case(test_cases[ci]); | 374 const TestCase& test_case(test_cases[ci]); |
| 375 std::unique_ptr<CookieMonster> cm = CreateMonsterFromStoreForGC( | 375 std::unique_ptr<CookieMonster> cm = CreateMonsterFromStoreForGC( |
| 376 test_case.num_cookies, test_case.num_old_cookies, 0, 0, | 376 test_case.num_cookies, test_case.num_old_cookies, 0, 0, |
| 377 CookieMonster::kSafeFromGlobalPurgeDays * 2); | 377 CookieMonster::kSafeFromGlobalPurgeDays * 2); |
| 378 | 378 |
| 379 GURL gurl("http://google.com"); | 379 GURL gurl("http://foo.com"); |
| 380 std::string cookie_line("z=3"); | 380 std::string cookie_line("z=3"); |
| 381 // Trigger the Garbage collection we're allowed. | 381 // Trigger the Garbage collection we're allowed. |
| 382 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); | 382 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); |
| 383 | 383 |
| 384 base::PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); | 384 base::PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); |
| 385 for (int i = 0; i < kNumCookies; i++) | 385 for (int i = 0; i < kNumCookies; i++) |
| 386 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); | 386 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); |
| 387 timer.Done(); | 387 timer.Done(); |
| 388 } | 388 } |
| 389 } | 389 } |
| 390 | 390 |
| 391 } // namespace net | 391 } // namespace net |
| OLD | NEW |