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> reporter(new MockDelegate(this)); |
| 128 edcc_ = new EvictedDomainCookieCounter(NULL, reporter.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> reporter(new MockDelegate(this)); |
| 227 edcc_ = new EvictedDomainCookieCounter(new ChangedDelegateDummy(&result), |
| 228 reporter.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 // Reinstate cookies at t = [1000,1000,(1000),1000,(1000),1000]. |
| 306 InitStockCookies(); // Refresh cookies, so new cookies expire in the future. |
| 307 for (int i = 0; i < 6; ++i) |
| 308 Add(cookies_[i]); |
| 309 EXPECT_EQ(0, edcc_->GetStorageSize()); |
| 310 // Reinstatement delays: [999,997,(994),990,(985),979]. |
| 311 EXPECT_EQ("999,997;990,979", google_stat_ + ";" + other_stat_); |
| 312 } |
| 313 |
| 314 // Garbage collection should remove the oldest evicted cookies. |
| 315 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollection) { |
| 316 InitCounter(4, 2); // Reduced capacity. |
| 317 InitStockCookies(); |
| 318 // Add all cookies at (relative time) t = 0. |
| 319 for (int i = 0; i < 6; ++i) |
| 320 Add(cookies_[i]); |
| 321 // Evict cookies at t = [1,3,6,10]. |
| 322 for (int i = 0; i < 4; ++i) { |
| 323 StepTime(i + 1); |
| 324 Evict(cookies_[i]); |
| 325 } |
| 326 EXPECT_EQ(4, edcc_->GetStorageSize()); // Reached capacity. |
| 327 StepTime(5); |
| 328 Evict(cookies_[4]); // Evict at t = 15, garbage collection takes place. |
| 329 EXPECT_EQ(2, edcc_->GetStorageSize()); |
| 330 StepTime(6); |
| 331 Evict(cookies_[5]); // Evict at t = 21. |
| 332 EXPECT_EQ(3, edcc_->GetStorageSize()); |
| 333 EXPECT_EQ(";", google_stat_ + ";" + other_stat_); |
| 334 // Reinstate cookies at t = [(100),(100),(100),100,100,100]. |
| 335 GotoTime(100); |
| 336 for (int i = 0; i < 6; ++i) |
| 337 Add(cookies_[i]); |
| 338 // Expected reinstatement delays: [(99),(97),(94),90,85,79] |
| 339 EXPECT_EQ("85;90,79", google_stat_ + ";" + other_stat_); |
| 340 } |
| 341 |
| 342 // Garbage collection should remove the specified number of evicted cookies |
| 343 // even when there are ties amongst oldest evicted cookies. |
| 344 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionTie) { |
| 345 InitCounter(9, 3); |
| 346 // Add 10 cookies at time [0,1,3,6,...,45] |
| 347 for (int i = 0; i < 10; ++i) { |
| 348 StepTime(i); |
| 349 CreateNewCookie(google_url1, "a" + base::IntToString(i) + "=1", 3000); |
| 350 Add(cookies_[i]); |
| 351 } |
| 352 // Evict 6 cookies at t = [100,...,100]. |
| 353 GotoTime(100); |
| 354 for (int i = 0; i < 6; ++i) |
| 355 Evict(cookies_[i]); |
| 356 EXPECT_EQ(6, edcc_->GetStorageSize()); |
| 357 // Evict 3 cookies at t = [210,220,230]. |
| 358 GotoTime(200); |
| 359 for (int i = 6; i < 9; ++i) { |
| 360 StepTime(10); |
| 361 Evict(cookies_[i]); |
| 362 } |
| 363 EXPECT_EQ(9, edcc_->GetStorageSize()); // Reached capacity. |
| 364 // Evict 1 cookie at t = 300, and garbage collection takes place. |
| 365 GotoTime(300); |
| 366 Evict(cookies_[9]); |
| 367 // Some arbitrary 4 out of 6 cookies evicted at t = 100 are gone from storage. |
| 368 EXPECT_EQ(6, edcc_->GetStorageSize()); // 10 - 4. |
| 369 // Reinstate cookies at t = [400,...,400]. |
| 370 GotoTime(400); |
| 371 for (int i = 0; i < 10; ++i) |
| 372 Add(cookies_[i]); |
| 373 EXPECT_EQ(0, edcc_->GetStorageSize()); |
| 374 // Expected reinstatement delays: |
| 375 // [300,300,300,300,300,300 <= keeping 2 only,190,180,170,100]. |
| 376 EXPECT_EQ("300,300,190,180,170,100;", google_stat_ + ";" + other_stat_); |
| 377 } |
| 378 |
| 379 // Garbage collection prioritize removal of expired cookies. |
| 380 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionWithExpiry) { |
| 381 InitCounter(5, 1); |
| 382 InitStockCookies(); |
| 383 // Add all cookies at (relative time) t = 0. |
| 384 for (int i = 0; i < 6; ++i) |
| 385 Add(cookies_[i]); |
| 386 // Evict cookies at t = [1,3,6,10,15]. |
| 387 for (int i = 0; i < 5; ++i) { |
| 388 StepTime(i + 1); |
| 389 Evict(cookies_[i]); |
| 390 } |
| 391 EXPECT_EQ(5, edcc_->GetStorageSize()); // Reached capacity. |
| 392 GotoTime(1200); // t = 1200, so cookies_[2,3,4] expire. |
| 393 // Evict cookies_[5] (not expired) at t = 1200. |
| 394 Evict(cookies_[5]); |
| 395 // Garbage collection would have taken place, removing 3 expired cookies, |
| 396 // so that there's no need to remove more. |
| 397 EXPECT_EQ(3, edcc_->GetStorageSize()); |
| 398 // Reinstate cookies at t = [1500,1500,(1500),(1500),(1500),1500]. |
| 399 GotoTime(1500); |
| 400 InitStockCookies(); // Refresh cookies, so new cookies expire in the future. |
| 401 for (int i = 0; i < 6; ++i) |
| 402 Add(cookies_[i]); |
| 403 EXPECT_EQ(0, edcc_->GetStorageSize()); |
| 404 // Reinstatement delays: [1499,1497,(1494),(1490),(1485),300]. |
| 405 EXPECT_EQ("1499,1497;300", google_stat_ + ";" + other_stat_); |
| 406 } |
| 407 |
| 408 } // namespace net |
OLD | NEW |