Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/memory/scoped_vector.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/time.h" | |
| 13 #include "googleurl/src/gurl.h" | |
| 14 #include "net/cookies/canonical_cookie.h" | |
| 15 #include "net/cookies/cookie_monster.h" | |
| 16 #include "net/cookies/evicted_domain_cookie_counter.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 namespace net { | |
| 20 | |
| 21 using base::Time; | |
| 22 using base::TimeDelta; | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const char* google_url1 = "http://www.google.com"; | |
| 27 const char* google_url2 = "http://mail.google.com"; | |
| 28 const char* other_url1 = "http://www.example.com"; | |
| 29 const char* other_url2 = "http://www.example.co.uk"; | |
| 30 | |
| 31 class EvictedDomainCookieCounterTest : public testing::Test { | |
| 32 protected: | |
| 33 class MockDelegate : public EvictedDomainCookieCounter::Delegate { | |
| 34 public: | |
| 35 explicit MockDelegate(EvictedDomainCookieCounterTest* tester); | |
| 36 | |
| 37 // EvictedDomainCookieCounter::Delegate implementation. | |
| 38 virtual void Report(const EvictedDomainCookieCounter::EvictedCookie& ec, | |
| 39 const Time& reinstatement_time) OVERRIDE; | |
| 40 virtual Time CurrentTime() OVERRIDE; | |
| 41 | |
| 42 private: | |
| 43 EvictedDomainCookieCounterTest* tester_; | |
| 44 }; | |
| 45 | |
| 46 EvictedDomainCookieCounterTest(); | |
| 47 virtual ~EvictedDomainCookieCounterTest(); | |
| 48 | |
| 49 // testing::Test implementation. | |
| 50 virtual void SetUp() OVERRIDE; | |
| 51 virtual void TearDown() OVERRIDE; | |
| 52 | |
| 53 // Initialization that allows parameters to be specified. | |
| 54 void InitCounter(size_t max_size, size_t purge_count); | |
| 55 | |
| 56 // Wrapper to allocate new cookie and store it in |cookies_|. | |
| 57 // If |max_age| == 0, then the cookie does not expire. | |
| 58 void CreateNewCookie( | |
| 59 const char* url, const std::string& cookie_line, int64 max_age); | |
| 60 | |
| 61 // Clears |cookies_| and creates common cookies for multiple tests. | |
| 62 void InitStockCookies(); | |
| 63 | |
| 64 // Sets simulation time to |rel_time|. | |
| 65 void GotoTime(int64 rel_time); | |
| 66 | |
| 67 // Simulates time-passage by |delta_second|. | |
| 68 void StepTime(int64 delta_second); | |
| 69 | |
| 70 // Simulates cookie addition or update. | |
| 71 void Add(CanonicalCookie* cc); | |
| 72 | |
| 73 // Simulates cookie removal. | |
| 74 void Remove(CanonicalCookie* cc); | |
| 75 | |
| 76 // Simulates cookie eviction. | |
| 77 void Evict(CanonicalCookie* cc); | |
| 78 | |
| 79 // For semi-realism, time considered are relative to |mock_time_base_|. | |
| 80 Time mock_time_base_; | |
| 81 Time mock_time_; | |
| 82 | |
| 83 // To store allocated cookies for reuse | |
| 84 ScopedVector<CanonicalCookie> cookies_; | |
| 85 | |
| 86 scoped_refptr<EvictedDomainCookieCounter> edcc_; | |
| 87 | |
| 88 // Statistics as comma-separated string of duration (in seconds) between | |
| 89 // eviction and reinstatement for each cookie, in the order of eviction. | |
| 90 std::string google_stat_; | |
| 91 std::string other_stat_; | |
| 92 }; | |
| 93 | |
| 94 EvictedDomainCookieCounterTest::MockDelegate::MockDelegate( | |
| 95 EvictedDomainCookieCounterTest* tester) | |
| 96 : tester_(tester) {} | |
| 97 | |
| 98 void EvictedDomainCookieCounterTest::MockDelegate::Report( | |
| 99 const EvictedDomainCookieCounter::EvictedCookie& ec, | |
| 100 const Time& reinstatement_time) { | |
| 101 std::string& dest = ec.is_google_ ? | |
| 102 tester_->google_stat_ : tester_->other_stat_; | |
| 103 if (!dest.empty()) | |
| 104 dest.append(","); | |
| 105 TimeDelta delta(reinstatement_time - ec.eviction_time_); | |
| 106 dest.append(base::Int64ToString(delta.InSeconds())); | |
| 107 } | |
| 108 | |
| 109 Time EvictedDomainCookieCounterTest::MockDelegate::CurrentTime() { | |
| 110 return tester_->mock_time_; | |
| 111 } | |
| 112 | |
| 113 EvictedDomainCookieCounterTest::EvictedDomainCookieCounterTest() {} | |
| 114 | |
| 115 EvictedDomainCookieCounterTest::~EvictedDomainCookieCounterTest() {} | |
| 116 | |
| 117 void EvictedDomainCookieCounterTest::SetUp() { | |
| 118 mock_time_base_ = Time::Now() - TimeDelta::FromHours(1); | |
| 119 mock_time_ = mock_time_base_; | |
| 120 } | |
| 121 | |
| 122 void EvictedDomainCookieCounterTest::TearDown() { | |
| 123 } | |
| 124 | |
| 125 void EvictedDomainCookieCounterTest::InitCounter(size_t max_size, | |
| 126 size_t purge_count) { | |
| 127 scoped_ptr<MockDelegate> delegate(new MockDelegate(this)); | |
| 128 edcc_ = new EvictedDomainCookieCounter(NULL, delegate.Pass(), | |
| 129 max_size, purge_count); | |
| 130 } | |
| 131 | |
| 132 void EvictedDomainCookieCounterTest::CreateNewCookie( | |
| 133 const char* url, const std::string& cookie_line, int64 max_age) { | |
| 134 std::string line(cookie_line); | |
| 135 if (max_age) | |
| 136 line.append(";max-age=" + base::Int64ToString(max_age)); | |
| 137 CanonicalCookie* cc = CanonicalCookie::Create( | |
| 138 GURL(url), line, mock_time_, CookieOptions()); | |
| 139 DCHECK(cc); | |
| 140 cookies_.push_back(cc); | |
| 141 } | |
| 142 | |
| 143 void EvictedDomainCookieCounterTest::InitStockCookies() { | |
| 144 cookies_.clear(); | |
| 145 CreateNewCookie(google_url1, "a1=1", 3000); // cookies_[0]. | |
| 146 CreateNewCookie(google_url2, "a2=1", 2000); // cookies_[1]. | |
| 147 CreateNewCookie(other_url1, "a1=1", 1000); // cookies_[2]. | |
| 148 CreateNewCookie(other_url1, "a2=1", 1001); // cookies_[3]. | |
| 149 CreateNewCookie(google_url1, "a1=1;Path=/sub", 999); // cookies_[4]. | |
| 150 CreateNewCookie(other_url2, "a2=1", 0); // cookies_[5]. | |
| 151 } | |
| 152 | |
| 153 void EvictedDomainCookieCounterTest::GotoTime(int64 rel_time) { | |
| 154 mock_time_ = mock_time_base_ + TimeDelta::FromSeconds(rel_time); | |
| 155 } | |
| 156 | |
| 157 void EvictedDomainCookieCounterTest::StepTime(int64 delta_second) { | |
| 158 mock_time_ += TimeDelta::FromSeconds(delta_second); | |
| 159 } | |
| 160 | |
| 161 void EvictedDomainCookieCounterTest::Add(CanonicalCookie* cc) { | |
| 162 edcc_->OnCookieChanged(*cc, false, | |
| 163 CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT); | |
| 164 } | |
| 165 | |
| 166 void EvictedDomainCookieCounterTest::Remove(CanonicalCookie* cc) { | |
| 167 edcc_->OnCookieChanged(*cc, true, | |
| 168 CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT); | |
| 169 } | |
| 170 | |
| 171 void EvictedDomainCookieCounterTest::Evict(CanonicalCookie* cc) { | |
| 172 edcc_->OnCookieChanged(*cc, true, | |
| 173 CookieMonster::Delegate::CHANGE_COOKIE_EVICTED); | |
| 174 } | |
| 175 | |
| 176 } // namespace | |
| 177 | |
| 178 // Test EvictedDomainCookieCounter::UnsafeIsGoogleCookie(). | |
| 179 TEST_F(EvictedDomainCookieCounterTest, TestUnsafeIsGoogleCookie) { | |
| 180 struct { | |
| 181 char* url; | |
| 182 bool expected; | |
| 183 } test_cases[] = { | |
| 184 {"http://www.example.com", false}, | |
| 185 {"http://www.example.com/google.com", false}, | |
| 186 {"http://www.google.com", true}, | |
| 187 {"http://www.google.com:8080", true}, | |
| 188 {"http://google.com", true}, | |
| 189 {"http://google.example.com", false}, | |
| 190 {"http://www.google.com/", true}, | |
| 191 {"http://www.google.com/some/sub/directory", true}, | |
| 192 {"https://www.GOOgle.co.uk", true}, | |
| 193 {"http://google.google.com", true}, | |
| 194 {"http://www.thegoogle.com", false}, | |
| 195 {"http://www.googlers.com", false}, | |
| 196 {"http://74.125.226.0", false}, | |
| 197 }; | |
| 198 for (int i = 0; i < arraysize(test_cases); ++i) { | |
| 199 scoped_ptr<CanonicalCookie> cc(CanonicalCookie::Create( | |
| 200 GURL(test_cases[i].url), "a=1", mock_time_, CookieOptions())); | |
| 201 EXPECT_EQ(test_cases[i].expected, | |
| 202 EvictedDomainCookieCounter::UnsafeIsGoogleCookie(*cc)); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 // EvictedDomainCookieCounter takes (and owns) a CookieMonster::Delegate for | |
| 207 // chaining. To ensure that the chaining indeed occurs, we implement a | |
| 208 // dummy CookieMonster::Delegate to increment an integer. | |
| 209 TEST_F(EvictedDomainCookieCounterTest, TestChain) { | |
| 210 int result = 0; | |
| 211 | |
| 212 class ChangedDelegateDummy : public CookieMonster::Delegate { | |
| 213 public: | |
| 214 explicit ChangedDelegateDummy(int* result) : result_(result) {} | |
| 215 | |
| 216 virtual void OnCookieChanged(const CanonicalCookie& cookie, | |
| 217 bool removed, | |
| 218 ChangeCause cause) OVERRIDE { | |
| 219 ++(*result_); | |
| 220 } | |
| 221 | |
| 222 private: | |
| 223 int* result_; | |
| 224 }; | |
| 225 | |
| 226 scoped_ptr<MockDelegate> delegate(new MockDelegate(this)); | |
| 227 edcc_ = new EvictedDomainCookieCounter(new ChangedDelegateDummy(&result), | |
| 228 delegate.Pass(), 10, 5); | |
| 229 InitStockCookies(); | |
| 230 // Perform 6 cookie transactions. | |
| 231 for (int i = 0; i < 6; ++i) { | |
| 232 Add(cookies_[i]); | |
| 233 StepTime(1); | |
| 234 Evict(cookies_[i]); | |
| 235 StepTime(1); | |
| 236 Remove(cookies_[i]); | |
| 237 } | |
| 238 EXPECT_EQ(18, result); // 6 cookies x 3 operations each. | |
| 239 } | |
| 240 | |
| 241 // Basic flow: add cookies, evict, then reinstate. | |
| 242 TEST_F(EvictedDomainCookieCounterTest, TestBasicFlow) { | |
| 243 InitCounter(10, 4); | |
| 244 InitStockCookies(); | |
| 245 // Add all cookies at (relative time) t = 0. | |
| 246 for (int i = 0; i < 6; ++i) | |
| 247 Add(cookies_[i]); | |
| 248 EXPECT_EQ(0, edcc_->GetStorageSize()); // No activities on add. | |
| 249 EXPECT_EQ(";", google_stat_ + ";" + other_stat_); | |
| 250 // Evict cookies at t = [1,3,6,10,15,21]. | |
| 251 for (int i = 0; i < 6; ++i) { | |
| 252 StepTime(i + 1); | |
| 253 Evict(cookies_[i]); | |
| 254 } | |
| 255 EXPECT_EQ(6, edcc_->GetStorageSize()); // All evictions are stored. | |
| 256 EXPECT_EQ(";", google_stat_ + ";" + other_stat_); | |
| 257 // Reinstate cookies at t = [22,23,24,25,26,27]. | |
| 258 for (int i = 0; i < 6; ++i) { | |
| 259 StepTime(1); | |
| 260 Add(cookies_[i]); | |
| 261 } | |
| 262 EXPECT_EQ(0, edcc_->GetStorageSize()); // Everything is removed. | |
| 263 // Expected reinstatement delays: [21,20,18,15,11,6]. | |
| 264 EXPECT_EQ("21,20,11;18,15,6", google_stat_ + ";" + other_stat_); | |
| 265 } | |
| 266 | |
| 267 // Removed cookies are ignored by EvictedDomainCookieCounter. | |
| 268 TEST_F(EvictedDomainCookieCounterTest, TestRemove) { | |
| 269 InitCounter(10, 4); | |
| 270 InitStockCookies(); | |
| 271 // Add all cookies at (relative time) t = 0. | |
| 272 for (int i = 0; i < 6; ++i) | |
| 273 Add(cookies_[i]); | |
| 274 // Remove cookies at t = [1,3,6,10,15,21]. | |
| 275 for (int i = 0; i < 6; ++i) { | |
| 276 StepTime(i + 1); | |
| 277 Remove(cookies_[i]); | |
| 278 } | |
| 279 EXPECT_EQ(0, edcc_->GetStorageSize()); | |
| 280 // Add cookies again at t = [22,23,24,25,26,27]. | |
| 281 for (int i = 0; i < 5; ++i) { | |
| 282 StepTime(1); | |
| 283 Add(cookies_[i]); | |
| 284 } | |
| 285 EXPECT_EQ(0, edcc_->GetStorageSize()); | |
| 286 // No cookies were evicted, so no reinstatement take place. | |
| 287 EXPECT_EQ(";", google_stat_ + ";" + other_stat_); | |
| 288 } | |
| 289 | |
| 290 // Expired cookies should not be counted by EvictedDomainCookieCounter. | |
| 291 TEST_F(EvictedDomainCookieCounterTest, TestExpired) { | |
| 292 InitCounter(10, 4); | |
| 293 InitStockCookies(); | |
| 294 // Add all cookies at (relative time) t = 0. | |
| 295 for (int i = 0; i < 6; ++i) | |
| 296 Add(cookies_[i]); | |
| 297 // Evict cookies at t = [1,3,6,10,15,21]. | |
| 298 for (int i = 0; i < 6; ++i) { | |
| 299 StepTime(i + 1); | |
| 300 Evict(cookies_[i]); | |
| 301 } | |
| 302 EXPECT_EQ(6, edcc_->GetStorageSize()); | |
| 303 GotoTime(1000); // t = 1000, so cookies_[2,4] expire. | |
| 304 | |
| 305 // Try to reinstate an expired cookie at t = 1000, should have no effect. | |
| 306 edcc_->OnCookieChanged(*cookies_[4], false, | |
|
erikwright (departed)
2013/03/25 17:29:41
This seems like an invalid test.
CHANGE_COOKIE_EX
huangs
2013/03/25 21:02:12
Done (removed this case).
| |
| 307 CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED_OVERWRITE); | |
| 308 EXPECT_EQ(6, edcc_->GetStorageSize()); | |
| 309 | |
| 310 // Reinstate cookies at t = [1000,1000,(1000),1000,(1000),1000]. | |
| 311 InitStockCookies(); // Refresh cookies, so new cookies expire in the future. | |
| 312 for (int i = 0; i < 6; ++i) | |
| 313 Add(cookies_[i]); | |
| 314 EXPECT_EQ(0, edcc_->GetStorageSize()); | |
| 315 // Reinstatement delays: [999,997,(994),990,(985),979]. | |
| 316 EXPECT_EQ("999,997;990,979", google_stat_ + ";" + other_stat_); | |
| 317 } | |
| 318 | |
| 319 // Garbage collection should remove the oldest evicted cookies. | |
| 320 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollection) { | |
| 321 InitCounter(4, 2); // Reduced capacity. | |
| 322 InitStockCookies(); | |
| 323 // Add all cookies at (relative time) t = 0. | |
| 324 for (int i = 0; i < 6; ++i) | |
| 325 Add(cookies_[i]); | |
| 326 // Evict cookies at t = [1,3,6,10]. | |
| 327 for (int i = 0; i < 4; ++i) { | |
| 328 StepTime(i + 1); | |
| 329 Evict(cookies_[i]); | |
| 330 } | |
| 331 EXPECT_EQ(4, edcc_->GetStorageSize()); // Reached capacity. | |
| 332 StepTime(5); | |
| 333 Evict(cookies_[4]); // Evict at t = 15, garbage collection takes place. | |
| 334 EXPECT_EQ(2, edcc_->GetStorageSize()); | |
| 335 StepTime(6); | |
| 336 Evict(cookies_[5]); // Evict at t = 21. | |
| 337 EXPECT_EQ(3, edcc_->GetStorageSize()); | |
| 338 EXPECT_EQ(";", google_stat_ + ";" + other_stat_); | |
| 339 // Reinstate cookies at t = [(100),(100),(100),100,100,100]. | |
| 340 GotoTime(100); | |
| 341 for (int i = 0; i < 6; ++i) | |
| 342 Add(cookies_[i]); | |
| 343 // Expected reinstatement delays: [(99),(97),(94),90,85,79] | |
| 344 EXPECT_EQ("85;90,79", google_stat_ + ";" + other_stat_); | |
| 345 } | |
| 346 | |
| 347 // Garbage collection should remove the specified number of evicted cookies | |
| 348 // even when there are ties amongst oldest evicted cookies. | |
| 349 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionTie) { | |
| 350 InitCounter(9, 3); | |
| 351 // Add 10 cookies at time [0,1,3,6,...,45] | |
| 352 for (int i = 0; i < 10; ++i) { | |
| 353 StepTime(i); | |
| 354 CreateNewCookie(google_url1, "a" + base::IntToString(i) + "=1", 3000); | |
| 355 Add(cookies_[i]); | |
| 356 } | |
| 357 // Evict 6 cookies at t = [100,...,100]. | |
| 358 GotoTime(100); | |
| 359 for (int i = 0; i < 6; ++i) | |
| 360 Evict(cookies_[i]); | |
| 361 EXPECT_EQ(6, edcc_->GetStorageSize()); | |
| 362 // Evict 3 cookies at t = [210,220,230]. | |
| 363 GotoTime(200); | |
| 364 for (int i = 6; i < 9; ++i) { | |
| 365 StepTime(10); | |
| 366 Evict(cookies_[i]); | |
| 367 } | |
| 368 EXPECT_EQ(9, edcc_->GetStorageSize()); // Reached capacity. | |
| 369 // Evict 1 cookie at t = 300, and garbage collection takes place. | |
| 370 GotoTime(300); | |
| 371 Evict(cookies_[9]); | |
| 372 // Some arbitrary 4 out of 6 cookies evicted at t = 100 are gone from storage. | |
| 373 EXPECT_EQ(6, edcc_->GetStorageSize()); // 10 - 4. | |
| 374 // Reinstate cookies at t = [400,...,400]. | |
| 375 GotoTime(400); | |
| 376 for (int i = 0; i < 10; ++i) | |
| 377 Add(cookies_[i]); | |
| 378 EXPECT_EQ(0, edcc_->GetStorageSize()); | |
| 379 // Expected reinstatement delays: | |
| 380 // [300,300,300,300,300,300 <= keeping 2 only,190,180,170,100]. | |
| 381 EXPECT_EQ("300,300,190,180,170,100;", google_stat_ + ";" + other_stat_); | |
| 382 } | |
| 383 | |
| 384 // Garbage collection prioritize removal of expired cookies. | |
| 385 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionWithExpiry) { | |
| 386 InitCounter(5, 1); | |
| 387 InitStockCookies(); | |
| 388 // Add all cookies at (relative time) t = 0. | |
| 389 for (int i = 0; i < 6; ++i) | |
| 390 Add(cookies_[i]); | |
| 391 // Evict cookies at t = [1,3,6,10,15]. | |
| 392 for (int i = 0; i < 5; ++i) { | |
| 393 StepTime(i + 1); | |
| 394 Evict(cookies_[i]); | |
| 395 } | |
| 396 EXPECT_EQ(5, edcc_->GetStorageSize()); // Reached capacity. | |
| 397 GotoTime(1200); // t = 1200, so cookies_[2,3,4] expire. | |
| 398 // Evict cookies_[5] (not expired) at t = 1200. | |
| 399 Evict(cookies_[5]); | |
| 400 // Garbage collection would have taken place, removing 3 expired cookies, | |
| 401 // so that there's no need to remove more. | |
| 402 EXPECT_EQ(3, edcc_->GetStorageSize()); | |
| 403 // Reinstate cookies at t = [1500,1500,(1500),(1500),(1500),1500]. | |
| 404 GotoTime(1500); | |
| 405 InitStockCookies(); // Refresh cookies, so new cookies expire in the future. | |
| 406 for (int i = 0; i < 6; ++i) | |
| 407 Add(cookies_[i]); | |
| 408 EXPECT_EQ(0, edcc_->GetStorageSize()); | |
| 409 // Reinstatement delays: [1499,1497,(1494),(1490),(1485),300]. | |
| 410 EXPECT_EQ("1499,1497;300", google_stat_ + ";" + other_stat_); | |
| 411 } | |
| 412 | |
| 413 } // namespace net | |
| OLD | NEW |