| 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.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/perftimer.h" | 9 #include "base/perftimer.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 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, 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 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, 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 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 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, *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 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, *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 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) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 214 } | 214 } |
| 215 } | 215 } |
| 216 | 216 |
| 217 | 217 |
| 218 EXPECT_EQ(31u, domain_list.size()); | 218 EXPECT_EQ(31u, domain_list.size()); |
| 219 for (std::vector<std::string>::const_iterator it = domain_list.begin(); | 219 for (std::vector<std::string>::const_iterator it = domain_list.begin(); |
| 220 it != domain_list.end(); it++) { | 220 it != domain_list.end(); it++) { |
| 221 GURL gurl("https://" + *it + "/"); | 221 GURL gurl("https://" + *it + "/"); |
| 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, 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, probe_gurl); | 229 std::string cookie_line = getCookiesCallback.GetCookies(cm.get(), probe_gurl); |
| 230 EXPECT_EQ(5, CountInString(cookie_line, '=')) << "Cookie line: " << | 230 EXPECT_EQ(5, CountInString(cookie_line, '=')) |
| 231 cookie_line; | 231 << "Cookie line: " << cookie_line; |
| 232 PerfTimeLogger timer("Cookie_monster_query_domain_tree"); | 232 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, 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; |
| 243 std::vector<std::string> domain_list; | 243 std::vector<std::string> domain_list; |
| 244 GURL probe_gurl("https://b.a.b.a.top.com/"); | 244 GURL probe_gurl("https://b.a.b.a.top.com/"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 256 domain_list.push_back("b.a.b.a.top.com"); | 256 domain_list.push_back("b.a.b.a.top.com"); |
| 257 EXPECT_EQ(4u, domain_list.size()); | 257 EXPECT_EQ(4u, domain_list.size()); |
| 258 | 258 |
| 259 const char* domain_cookie_format_line = "a%03d=b; domain=%s"; | 259 const char* domain_cookie_format_line = "a%03d=b; domain=%s"; |
| 260 for (int i = 0; i < 8; i++) { | 260 for (int i = 0; i < 8; i++) { |
| 261 for (std::vector<std::string>::const_iterator it = domain_list.begin(); | 261 for (std::vector<std::string>::const_iterator it = domain_list.begin(); |
| 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, gurl, cookie); | 266 setCookieCallback.SetCookie(cm.get(), gurl, cookie); |
| 267 } | 267 } |
| 268 } | 268 } |
| 269 | 269 |
| 270 cookie_line = getCookiesCallback.GetCookies(cm, 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 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, 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; |
| 283 | 283 |
| 284 // We want to setup a fairly large backing store, with 300 domains of 50 | 284 // We want to setup a fairly large backing store, with 300 domains of 50 |
| 285 // cookies each. Creation times must be unique. | 285 // cookies each. Creation times must be unique. |
| 286 int64 time_tick(base::Time::Now().ToInternalValue()); | 286 int64 time_tick(base::Time::Now().ToInternalValue()); |
| 287 | 287 |
| 288 for (int domain_num = 0; domain_num < 300; domain_num++) { | 288 for (int domain_num = 0; domain_num < 300; domain_num++) { |
| 289 std::string domain_name(base::StringPrintf(".Domain_%d.com", domain_num)); | 289 std::string domain_name(base::StringPrintf(".Domain_%d.com", domain_num)); |
| 290 std::string gurl("www" + domain_name); | 290 std::string gurl("www" + domain_name); |
| 291 for (int cookie_num = 0; cookie_num < 50; cookie_num++) { | 291 for (int cookie_num = 0; cookie_num < 50; cookie_num++) { |
| 292 std::string cookie_line(base::StringPrintf("Cookie_%d=1; Path=/", | 292 std::string cookie_line(base::StringPrintf("Cookie_%d=1; Path=/", |
| 293 cookie_num)); | 293 cookie_num)); |
| 294 AddCookieToList(gurl, cookie_line, | 294 AddCookieToList(gurl, cookie_line, |
| 295 base::Time::FromInternalValue(time_tick++), | 295 base::Time::FromInternalValue(time_tick++), |
| 296 &initial_cookies); | 296 &initial_cookies); |
| 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, 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 PerfTimeLogger timer("Cookie_monster_import_from_store"); |
| 308 getCookiesCallback.GetCookies(cm, 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 PerfTimeLogger timer("Cookie_monster_get_key"); |
| 318 for (int i = 0; i < kNumCookies; i++) | 318 for (int i = 0; i < kNumCookies; i++) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 for (int ci = 0; ci < static_cast<int>(ARRAYSIZE_UNSAFE(test_cases)); ++ci) { | 366 for (int ci = 0; ci < static_cast<int>(ARRAYSIZE_UNSAFE(test_cases)); ++ci) { |
| 367 const TestCase& test_case(test_cases[ci]); | 367 const TestCase& test_case(test_cases[ci]); |
| 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, 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 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, 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 |