| 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" |
| 11 #include "base/test/perftimer.h" | 11 #include "base/test/perf_time_logger.h" |
| 12 #include "net/cookies/canonical_cookie.h" | 12 #include "net/cookies/canonical_cookie.h" |
| 13 #include "net/cookies/cookie_monster.h" | 13 #include "net/cookies/cookie_monster.h" |
| 14 #include "net/cookies/cookie_monster_store_test.h" | 14 #include "net/cookies/cookie_monster_store_test.h" |
| 15 #include "net/cookies/parsed_cookie.h" | 15 #include "net/cookies/parsed_cookie.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 BaseCallback::Run(); | 90 BaseCallback::Run(); |
| 91 } | 91 } |
| 92 std::string cookies_; | 92 std::string cookies_; |
| 93 net::CookieOptions options_; | 93 net::CookieOptions options_; |
| 94 }; | 94 }; |
| 95 | 95 |
| 96 } // namespace | 96 } // namespace |
| 97 | 97 |
| 98 TEST(ParsedCookieTest, TestParseCookies) { | 98 TEST(ParsedCookieTest, TestParseCookies) { |
| 99 std::string cookie(kCookieLine); | 99 std::string cookie(kCookieLine); |
| 100 PerfTimeLogger timer("Parsed_cookie_parse_cookies"); | 100 base::PerfTimeLogger timer("Parsed_cookie_parse_cookies"); |
| 101 for (int i = 0; i < kNumCookies; ++i) { | 101 for (int i = 0; i < kNumCookies; ++i) { |
| 102 ParsedCookie pc(cookie); | 102 ParsedCookie pc(cookie); |
| 103 EXPECT_TRUE(pc.IsValid()); | 103 EXPECT_TRUE(pc.IsValid()); |
| 104 } | 104 } |
| 105 timer.Done(); | 105 timer.Done(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 TEST(ParsedCookieTest, TestParseBigCookies) { | 108 TEST(ParsedCookieTest, TestParseBigCookies) { |
| 109 std::string cookie(3800, 'z'); | 109 std::string cookie(3800, 'z'); |
| 110 cookie += kCookieLine; | 110 cookie += kCookieLine; |
| 111 PerfTimeLogger timer("Parsed_cookie_parse_big_cookies"); | 111 base::PerfTimeLogger timer("Parsed_cookie_parse_big_cookies"); |
| 112 for (int i = 0; i < kNumCookies; ++i) { | 112 for (int i = 0; i < kNumCookies; ++i) { |
| 113 ParsedCookie pc(cookie); | 113 ParsedCookie pc(cookie); |
| 114 EXPECT_TRUE(pc.IsValid()); | 114 EXPECT_TRUE(pc.IsValid()); |
| 115 } | 115 } |
| 116 timer.Done(); | 116 timer.Done(); |
| 117 } | 117 } |
| 118 | 118 |
| 119 TEST_F(CookieMonsterTest, TestAddCookiesOnSingleHost) { | 119 TEST_F(CookieMonsterTest, TestAddCookiesOnSingleHost) { |
| 120 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 120 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 121 std::vector<std::string> cookies; | 121 std::vector<std::string> cookies; |
| 122 for (int i = 0; i < kNumCookies; i++) { | 122 for (int i = 0; i < kNumCookies; i++) { |
| 123 cookies.push_back(base::StringPrintf("a%03d=b", i)); | 123 cookies.push_back(base::StringPrintf("a%03d=b", i)); |
| 124 } | 124 } |
| 125 | 125 |
| 126 SetCookieCallback setCookieCallback; | 126 SetCookieCallback setCookieCallback; |
| 127 | 127 |
| 128 // Add a bunch of cookies on a single host | 128 // Add a bunch of cookies on a single host |
| 129 PerfTimeLogger timer("Cookie_monster_add_single_host"); | 129 base::PerfTimeLogger timer("Cookie_monster_add_single_host"); |
| 130 | 130 |
| 131 for (std::vector<std::string>::const_iterator it = cookies.begin(); | 131 for (std::vector<std::string>::const_iterator it = cookies.begin(); |
| 132 it != cookies.end(); ++it) { | 132 it != cookies.end(); ++it) { |
| 133 setCookieCallback.SetCookie(cm.get(), GURL(kGoogleURL), *it); | 133 setCookieCallback.SetCookie(cm.get(), GURL(kGoogleURL), *it); |
| 134 } | 134 } |
| 135 timer.Done(); | 135 timer.Done(); |
| 136 | 136 |
| 137 GetCookiesCallback getCookiesCallback; | 137 GetCookiesCallback getCookiesCallback; |
| 138 | 138 |
| 139 PerfTimeLogger timer2("Cookie_monster_query_single_host"); | 139 base::PerfTimeLogger timer2("Cookie_monster_query_single_host"); |
| 140 for (std::vector<std::string>::const_iterator it = cookies.begin(); | 140 for (std::vector<std::string>::const_iterator it = cookies.begin(); |
| 141 it != cookies.end(); ++it) { | 141 it != cookies.end(); ++it) { |
| 142 getCookiesCallback.GetCookies(cm.get(), GURL(kGoogleURL)); | 142 getCookiesCallback.GetCookies(cm.get(), GURL(kGoogleURL)); |
| 143 } | 143 } |
| 144 timer2.Done(); | 144 timer2.Done(); |
| 145 | 145 |
| 146 PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); | 146 base::PerfTimeLogger timer3("Cookie_monster_deleteall_single_host"); |
| 147 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); | 147 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); |
| 148 base::MessageLoop::current()->RunUntilIdle(); | 148 base::MessageLoop::current()->RunUntilIdle(); |
| 149 timer3.Done(); | 149 timer3.Done(); |
| 150 } | 150 } |
| 151 | 151 |
| 152 TEST_F(CookieMonsterTest, TestAddCookieOnManyHosts) { | 152 TEST_F(CookieMonsterTest, TestAddCookieOnManyHosts) { |
| 153 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 153 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 154 std::string cookie(kCookieLine); | 154 std::string cookie(kCookieLine); |
| 155 std::vector<GURL> gurls; // just wanna have ffffuunnn | 155 std::vector<GURL> gurls; // just wanna have ffffuunnn |
| 156 for (int i = 0; i < kNumCookies; ++i) { | 156 for (int i = 0; i < kNumCookies; ++i) { |
| 157 gurls.push_back(GURL(base::StringPrintf("https://a%04d.izzle", i))); | 157 gurls.push_back(GURL(base::StringPrintf("https://a%04d.izzle", i))); |
| 158 } | 158 } |
| 159 | 159 |
| 160 SetCookieCallback setCookieCallback; | 160 SetCookieCallback setCookieCallback; |
| 161 | 161 |
| 162 // Add a cookie on a bunch of host | 162 // Add a cookie on a bunch of host |
| 163 PerfTimeLogger timer("Cookie_monster_add_many_hosts"); | 163 base::PerfTimeLogger timer("Cookie_monster_add_many_hosts"); |
| 164 for (std::vector<GURL>::const_iterator it = gurls.begin(); | 164 for (std::vector<GURL>::const_iterator it = gurls.begin(); |
| 165 it != gurls.end(); ++it) { | 165 it != gurls.end(); ++it) { |
| 166 setCookieCallback.SetCookie(cm.get(), *it, cookie); | 166 setCookieCallback.SetCookie(cm.get(), *it, cookie); |
| 167 } | 167 } |
| 168 timer.Done(); | 168 timer.Done(); |
| 169 | 169 |
| 170 GetCookiesCallback getCookiesCallback; | 170 GetCookiesCallback getCookiesCallback; |
| 171 | 171 |
| 172 PerfTimeLogger timer2("Cookie_monster_query_many_hosts"); | 172 base::PerfTimeLogger timer2("Cookie_monster_query_many_hosts"); |
| 173 for (std::vector<GURL>::const_iterator it = gurls.begin(); | 173 for (std::vector<GURL>::const_iterator it = gurls.begin(); |
| 174 it != gurls.end(); ++it) { | 174 it != gurls.end(); ++it) { |
| 175 getCookiesCallback.GetCookies(cm.get(), *it); | 175 getCookiesCallback.GetCookies(cm.get(), *it); |
| 176 } | 176 } |
| 177 timer2.Done(); | 177 timer2.Done(); |
| 178 | 178 |
| 179 PerfTimeLogger timer3("Cookie_monster_deleteall_many_hosts"); | 179 base::PerfTimeLogger timer3("Cookie_monster_deleteall_many_hosts"); |
| 180 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); | 180 cm->DeleteAllAsync(CookieMonster::DeleteCallback()); |
| 181 base::MessageLoop::current()->RunUntilIdle(); | 181 base::MessageLoop::current()->RunUntilIdle(); |
| 182 timer3.Done(); | 182 timer3.Done(); |
| 183 } | 183 } |
| 184 | 184 |
| 185 TEST_F(CookieMonsterTest, TestDomainTree) { | 185 TEST_F(CookieMonsterTest, TestDomainTree) { |
| 186 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 186 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 187 GetCookiesCallback getCookiesCallback; | 187 GetCookiesCallback getCookiesCallback; |
| 188 SetCookieCallback setCookieCallback; | 188 SetCookieCallback setCookieCallback; |
| 189 const char* domain_cookie_format_tree = "a=b; domain=%s"; | 189 const char* domain_cookie_format_tree = "a=b; domain=%s"; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 const std::string cookie = base::StringPrintf(domain_cookie_format_tree, | 222 const std::string cookie = base::StringPrintf(domain_cookie_format_tree, |
| 223 it->c_str()); | 223 it->c_str()); |
| 224 setCookieCallback.SetCookie(cm.get(), gurl, cookie); | 224 setCookieCallback.SetCookie(cm.get(), gurl, cookie); |
| 225 } | 225 } |
| 226 EXPECT_EQ(31u, cm->GetAllCookies().size()); | 226 EXPECT_EQ(31u, cm->GetAllCookies().size()); |
| 227 | 227 |
| 228 GURL probe_gurl("https://b.a.b.a.top.com/"); | 228 GURL probe_gurl("https://b.a.b.a.top.com/"); |
| 229 std::string cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl); | 229 std::string cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl); |
| 230 EXPECT_EQ(5, CountInString(cookie_line, '=')) | 230 EXPECT_EQ(5, CountInString(cookie_line, '=')) |
| 231 << "Cookie line: " << cookie_line; | 231 << "Cookie line: " << cookie_line; |
| 232 PerfTimeLogger timer("Cookie_monster_query_domain_tree"); | 232 base::PerfTimeLogger timer("Cookie_monster_query_domain_tree"); |
| 233 for (int i = 0; i < kNumCookies; i++) { | 233 for (int i = 0; i < kNumCookies; i++) { |
| 234 getCookiesCallback.GetCookies(cm.get(), probe_gurl); | 234 getCookiesCallback.GetCookies(cm.get(), probe_gurl); |
| 235 } | 235 } |
| 236 timer.Done(); | 236 timer.Done(); |
| 237 } | 237 } |
| 238 | 238 |
| 239 TEST_F(CookieMonsterTest, TestDomainLine) { | 239 TEST_F(CookieMonsterTest, TestDomainLine) { |
| 240 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 240 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 241 SetCookieCallback setCookieCallback; | 241 SetCookieCallback setCookieCallback; |
| 242 GetCookiesCallback getCookiesCallback; | 242 GetCookiesCallback getCookiesCallback; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 262 it != domain_list.end(); it++) { | 262 it != domain_list.end(); it++) { |
| 263 GURL gurl("https://" + *it + "/"); | 263 GURL gurl("https://" + *it + "/"); |
| 264 const std::string cookie = base::StringPrintf(domain_cookie_format_line, | 264 const std::string cookie = base::StringPrintf(domain_cookie_format_line, |
| 265 i, it->c_str()); | 265 i, it->c_str()); |
| 266 setCookieCallback.SetCookie(cm.get(), gurl, cookie); | 266 setCookieCallback.SetCookie(cm.get(), gurl, cookie); |
| 267 } | 267 } |
| 268 } | 268 } |
| 269 | 269 |
| 270 cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl); | 270 cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl); |
| 271 EXPECT_EQ(32, CountInString(cookie_line, '=')); | 271 EXPECT_EQ(32, CountInString(cookie_line, '=')); |
| 272 PerfTimeLogger timer2("Cookie_monster_query_domain_line"); | 272 base::PerfTimeLogger timer2("Cookie_monster_query_domain_line"); |
| 273 for (int i = 0; i < kNumCookies; i++) { | 273 for (int i = 0; i < kNumCookies; i++) { |
| 274 getCookiesCallback.GetCookies(cm.get(), probe_gurl); | 274 getCookiesCallback.GetCookies(cm.get(), probe_gurl); |
| 275 } | 275 } |
| 276 timer2.Done(); | 276 timer2.Done(); |
| 277 } | 277 } |
| 278 | 278 |
| 279 TEST_F(CookieMonsterTest, TestImport) { | 279 TEST_F(CookieMonsterTest, TestImport) { |
| 280 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); | 280 scoped_refptr<MockPersistentCookieStore> store(new MockPersistentCookieStore); |
| 281 std::vector<CanonicalCookie*> initial_cookies; | 281 std::vector<CanonicalCookie*> initial_cookies; |
| 282 GetCookiesCallback getCookiesCallback; | 282 GetCookiesCallback getCookiesCallback; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 297 } | 297 } |
| 298 } | 298 } |
| 299 | 299 |
| 300 store->SetLoadExpectation(true, initial_cookies); | 300 store->SetLoadExpectation(true, initial_cookies); |
| 301 | 301 |
| 302 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL)); | 302 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL)); |
| 303 | 303 |
| 304 // Import will happen on first access. | 304 // Import will happen on first access. |
| 305 GURL gurl("www.google.com"); | 305 GURL gurl("www.google.com"); |
| 306 CookieOptions options; | 306 CookieOptions options; |
| 307 PerfTimeLogger timer("Cookie_monster_import_from_store"); | 307 base::PerfTimeLogger timer("Cookie_monster_import_from_store"); |
| 308 getCookiesCallback.GetCookies(cm.get(), gurl); | 308 getCookiesCallback.GetCookies(cm.get(), gurl); |
| 309 timer.Done(); | 309 timer.Done(); |
| 310 | 310 |
| 311 // Just confirm keys were set as expected. | 311 // Just confirm keys were set as expected. |
| 312 EXPECT_EQ("domain_1.com", cm->GetKey("www.Domain_1.com")); | 312 EXPECT_EQ("domain_1.com", cm->GetKey("www.Domain_1.com")); |
| 313 } | 313 } |
| 314 | 314 |
| 315 TEST_F(CookieMonsterTest, TestGetKey) { | 315 TEST_F(CookieMonsterTest, TestGetKey) { |
| 316 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 316 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 317 PerfTimeLogger timer("Cookie_monster_get_key"); | 317 base::PerfTimeLogger timer("Cookie_monster_get_key"); |
| 318 for (int i = 0; i < kNumCookies; i++) | 318 for (int i = 0; i < kNumCookies; i++) |
| 319 cm->GetKey("www.google.com"); | 319 cm->GetKey("www.google.com"); |
| 320 timer.Done(); | 320 timer.Done(); |
| 321 } | 321 } |
| 322 | 322 |
| 323 // This test is probing for whether garbage collection happens when it | 323 // This test is probing for whether garbage collection happens when it |
| 324 // shouldn't. This will not in general be visible functionally, since | 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 | 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 | 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 | 327 // a performance test. The test should be considered to pass if all the |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 scoped_refptr<CookieMonster> cm( | 368 scoped_refptr<CookieMonster> cm( |
| 369 CreateMonsterFromStoreForGC( | 369 CreateMonsterFromStoreForGC( |
| 370 test_case.num_cookies, test_case.num_old_cookies, | 370 test_case.num_cookies, test_case.num_old_cookies, |
| 371 CookieMonster::kSafeFromGlobalPurgeDays * 2)); | 371 CookieMonster::kSafeFromGlobalPurgeDays * 2)); |
| 372 | 372 |
| 373 GURL gurl("http://google.com"); | 373 GURL gurl("http://google.com"); |
| 374 std::string cookie_line("z=3"); | 374 std::string cookie_line("z=3"); |
| 375 // Trigger the Garbage collection we're allowed. | 375 // Trigger the Garbage collection we're allowed. |
| 376 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); | 376 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); |
| 377 | 377 |
| 378 PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); | 378 base::PerfTimeLogger timer((std::string("GC_") + test_case.name).c_str()); |
| 379 for (int i = 0; i < kNumCookies; i++) | 379 for (int i = 0; i < kNumCookies; i++) |
| 380 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); | 380 setCookieCallback.SetCookie(cm.get(), gurl, cookie_line); |
| 381 timer.Done(); | 381 timer.Done(); |
| 382 } | 382 } |
| 383 } | 383 } |
| 384 | 384 |
| 385 } // namespace net | 385 } // namespace net |
| OLD | NEW |