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 "chrome/browser/net/evicted_domain_cookie_counter.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "net/cookies/canonical_cookie.h" |
| 16 #include "net/cookies/cookie_monster.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace chrome_browser_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(net::CanonicalCookie* cc); |
| 72 |
| 73 // Simulates cookie removal. |
| 74 void Remove(net::CanonicalCookie* cc); |
| 75 |
| 76 // Simulates cookie eviction. |
| 77 void Evict(net::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<net::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 net::CanonicalCookie* cc = net::CanonicalCookie::Create( |
| 138 GURL(url), line, mock_time_, net::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(net::CanonicalCookie* cc) { |
| 162 edcc_->OnCookieChanged(*cc, false, |
| 163 net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT); |
| 164 } |
| 165 |
| 166 void EvictedDomainCookieCounterTest::Remove(net::CanonicalCookie* cc) { |
| 167 edcc_->OnCookieChanged(*cc, true, |
| 168 net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT); |
| 169 } |
| 170 |
| 171 void EvictedDomainCookieCounterTest::Evict(net::CanonicalCookie* cc) { |
| 172 edcc_->OnCookieChanged(*cc, true, |
| 173 net::CookieMonster::Delegate::CHANGE_COOKIE_EVICTED); |
| 174 } |
| 175 |
| 176 } // namespace |
| 177 |
| 178 // EvictedDomainCookieCounter takes (and owns) a CookieMonster::Delegate for |
| 179 // chaining. To ensure that the chaining indeed occurs, we implement a |
| 180 // dummy CookieMonster::Delegate to increment an integer. |
| 181 TEST_F(EvictedDomainCookieCounterTest, TestChain) { |
| 182 int result = 0; |
| 183 |
| 184 class ChangedDelegateDummy : public net::CookieMonster::Delegate { |
| 185 public: |
| 186 explicit ChangedDelegateDummy(int* result) : result_(result) {} |
| 187 |
| 188 virtual void OnCookieChanged(const net::CanonicalCookie& cookie, |
| 189 bool removed, |
| 190 ChangeCause cause) OVERRIDE { |
| 191 ++(*result_); |
| 192 } |
| 193 |
| 194 private: |
| 195 int* result_; |
| 196 }; |
| 197 |
| 198 scoped_ptr<MockDelegate> delegate(new MockDelegate(this)); |
| 199 edcc_ = new EvictedDomainCookieCounter(new ChangedDelegateDummy(&result), |
| 200 delegate.Pass(), 10, 5); |
| 201 InitStockCookies(); |
| 202 // Perform 6 cookie transactions. |
| 203 for (int i = 0; i < 6; ++i) { |
| 204 Add(cookies_[i]); |
| 205 StepTime(1); |
| 206 Evict(cookies_[i]); |
| 207 StepTime(1); |
| 208 Remove(cookies_[i]); |
| 209 } |
| 210 EXPECT_EQ(18, result); // 6 cookies x 3 operations each. |
| 211 } |
| 212 |
| 213 // Basic flow: add cookies, evict, then reinstate. |
| 214 TEST_F(EvictedDomainCookieCounterTest, TestBasicFlow) { |
| 215 InitCounter(10, 4); |
| 216 InitStockCookies(); |
| 217 // Add all cookies at (relative time) t = 0. |
| 218 for (int i = 0; i < 6; ++i) |
| 219 Add(cookies_[i]); |
| 220 EXPECT_EQ(0, edcc_->GetStorageSize()); // No activities on add. |
| 221 EXPECT_EQ(";", google_stat_ + ";" + other_stat_); |
| 222 // Evict cookies at t = [1,3,6,10,15,21]. |
| 223 for (int i = 0; i < 6; ++i) { |
| 224 StepTime(i + 1); |
| 225 Evict(cookies_[i]); |
| 226 } |
| 227 EXPECT_EQ(6, edcc_->GetStorageSize()); // All evictions are stored. |
| 228 EXPECT_EQ(";", google_stat_ + ";" + other_stat_); |
| 229 // Reinstate cookies at t = [22,23,24,25,26,27]. |
| 230 for (int i = 0; i < 6; ++i) { |
| 231 StepTime(1); |
| 232 Add(cookies_[i]); |
| 233 } |
| 234 EXPECT_EQ(0, edcc_->GetStorageSize()); // Everything is removed. |
| 235 // Expected reinstatement delays: [21,20,18,15,11,6]. |
| 236 EXPECT_EQ("21,20,11;18,15,6", google_stat_ + ";" + other_stat_); |
| 237 } |
| 238 |
| 239 // Removed cookies are ignored by EvictedDomainCookieCounter. |
| 240 TEST_F(EvictedDomainCookieCounterTest, TestRemove) { |
| 241 InitCounter(10, 4); |
| 242 InitStockCookies(); |
| 243 // Add all cookies at (relative time) t = 0. |
| 244 for (int i = 0; i < 6; ++i) |
| 245 Add(cookies_[i]); |
| 246 // Remove cookies at t = [1,3,6,10,15,21]. |
| 247 for (int i = 0; i < 6; ++i) { |
| 248 StepTime(i + 1); |
| 249 Remove(cookies_[i]); |
| 250 } |
| 251 EXPECT_EQ(0, edcc_->GetStorageSize()); |
| 252 // Add cookies again at t = [22,23,24,25,26,27]. |
| 253 for (int i = 0; i < 5; ++i) { |
| 254 StepTime(1); |
| 255 Add(cookies_[i]); |
| 256 } |
| 257 EXPECT_EQ(0, edcc_->GetStorageSize()); |
| 258 // No cookies were evicted, so no reinstatement take place. |
| 259 EXPECT_EQ(";", google_stat_ + ";" + other_stat_); |
| 260 } |
| 261 |
| 262 // Expired cookies should not be counted by EvictedDomainCookieCounter. |
| 263 TEST_F(EvictedDomainCookieCounterTest, TestExpired) { |
| 264 InitCounter(10, 4); |
| 265 InitStockCookies(); |
| 266 // Add all cookies at (relative time) t = 0. |
| 267 for (int i = 0; i < 6; ++i) |
| 268 Add(cookies_[i]); |
| 269 // Evict cookies at t = [1,3,6,10,15,21]. |
| 270 for (int i = 0; i < 6; ++i) { |
| 271 StepTime(i + 1); |
| 272 Evict(cookies_[i]); |
| 273 } |
| 274 EXPECT_EQ(6, edcc_->GetStorageSize()); |
| 275 GotoTime(1000); // t = 1000, so cookies_[2,4] expire. |
| 276 |
| 277 // Reinstate cookies at t = [1000,1000,(1000),1000,(1000),1000]. |
| 278 InitStockCookies(); // Refresh cookies, so new cookies expire in the future. |
| 279 for (int i = 0; i < 6; ++i) |
| 280 Add(cookies_[i]); |
| 281 EXPECT_EQ(0, edcc_->GetStorageSize()); |
| 282 // Reinstatement delays: [999,997,(994),990,(985),979]. |
| 283 EXPECT_EQ("999,997;990,979", google_stat_ + ";" + other_stat_); |
| 284 } |
| 285 |
| 286 // Garbage collection should remove the oldest evicted cookies. |
| 287 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollection) { |
| 288 InitCounter(4, 2); // Reduced capacity. |
| 289 InitStockCookies(); |
| 290 // Add all cookies at (relative time) t = 0. |
| 291 for (int i = 0; i < 6; ++i) |
| 292 Add(cookies_[i]); |
| 293 // Evict cookies at t = [1,3,6,10]. |
| 294 for (int i = 0; i < 4; ++i) { |
| 295 StepTime(i + 1); |
| 296 Evict(cookies_[i]); |
| 297 } |
| 298 EXPECT_EQ(4, edcc_->GetStorageSize()); // Reached capacity. |
| 299 StepTime(5); |
| 300 Evict(cookies_[4]); // Evict at t = 15, garbage collection takes place. |
| 301 EXPECT_EQ(2, edcc_->GetStorageSize()); |
| 302 StepTime(6); |
| 303 Evict(cookies_[5]); // Evict at t = 21. |
| 304 EXPECT_EQ(3, edcc_->GetStorageSize()); |
| 305 EXPECT_EQ(";", google_stat_ + ";" + other_stat_); |
| 306 // Reinstate cookies at t = [(100),(100),(100),100,100,100]. |
| 307 GotoTime(100); |
| 308 for (int i = 0; i < 6; ++i) |
| 309 Add(cookies_[i]); |
| 310 // Expected reinstatement delays: [(99),(97),(94),90,85,79] |
| 311 EXPECT_EQ("85;90,79", google_stat_ + ";" + other_stat_); |
| 312 } |
| 313 |
| 314 // Garbage collection should remove the specified number of evicted cookies |
| 315 // even when there are ties amongst oldest evicted cookies. |
| 316 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionTie) { |
| 317 InitCounter(9, 3); |
| 318 // Add 10 cookies at time [0,1,3,6,...,45] |
| 319 for (int i = 0; i < 10; ++i) { |
| 320 StepTime(i); |
| 321 CreateNewCookie(google_url1, "a" + base::IntToString(i) + "=1", 3000); |
| 322 Add(cookies_[i]); |
| 323 } |
| 324 // Evict 6 cookies at t = [100,...,100]. |
| 325 GotoTime(100); |
| 326 for (int i = 0; i < 6; ++i) |
| 327 Evict(cookies_[i]); |
| 328 EXPECT_EQ(6, edcc_->GetStorageSize()); |
| 329 // Evict 3 cookies at t = [210,220,230]. |
| 330 GotoTime(200); |
| 331 for (int i = 6; i < 9; ++i) { |
| 332 StepTime(10); |
| 333 Evict(cookies_[i]); |
| 334 } |
| 335 EXPECT_EQ(9, edcc_->GetStorageSize()); // Reached capacity. |
| 336 // Evict 1 cookie at t = 300, and garbage collection takes place. |
| 337 GotoTime(300); |
| 338 Evict(cookies_[9]); |
| 339 // Some arbitrary 4 out of 6 cookies evicted at t = 100 are gone from storage. |
| 340 EXPECT_EQ(6, edcc_->GetStorageSize()); // 10 - 4. |
| 341 // Reinstate cookies at t = [400,...,400]. |
| 342 GotoTime(400); |
| 343 for (int i = 0; i < 10; ++i) |
| 344 Add(cookies_[i]); |
| 345 EXPECT_EQ(0, edcc_->GetStorageSize()); |
| 346 // Expected reinstatement delays: |
| 347 // [300,300,300,300,300,300 <= keeping 2 only,190,180,170,100]. |
| 348 EXPECT_EQ("300,300,190,180,170,100;", google_stat_ + ";" + other_stat_); |
| 349 } |
| 350 |
| 351 // Garbage collection prioritize removal of expired cookies. |
| 352 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionWithExpiry) { |
| 353 InitCounter(5, 1); |
| 354 InitStockCookies(); |
| 355 // Add all cookies at (relative time) t = 0. |
| 356 for (int i = 0; i < 6; ++i) |
| 357 Add(cookies_[i]); |
| 358 // Evict cookies at t = [1,3,6,10,15]. |
| 359 for (int i = 0; i < 5; ++i) { |
| 360 StepTime(i + 1); |
| 361 Evict(cookies_[i]); |
| 362 } |
| 363 EXPECT_EQ(5, edcc_->GetStorageSize()); // Reached capacity. |
| 364 GotoTime(1200); // t = 1200, so cookies_[2,3,4] expire. |
| 365 // Evict cookies_[5] (not expired) at t = 1200. |
| 366 Evict(cookies_[5]); |
| 367 // Garbage collection would have taken place, removing 3 expired cookies, |
| 368 // so that there's no need to remove more. |
| 369 EXPECT_EQ(3, edcc_->GetStorageSize()); |
| 370 // Reinstate cookies at t = [1500,1500,(1500),(1500),(1500),1500]. |
| 371 GotoTime(1500); |
| 372 InitStockCookies(); // Refresh cookies, so new cookies expire in the future. |
| 373 for (int i = 0; i < 6; ++i) |
| 374 Add(cookies_[i]); |
| 375 EXPECT_EQ(0, edcc_->GetStorageSize()); |
| 376 // Reinstatement delays: [1499,1497,(1494),(1490),(1485),300]. |
| 377 EXPECT_EQ("1499,1497;300", google_stat_ + ";" + other_stat_); |
| 378 } |
| 379 |
| 380 } // namespace chrome_browser_net |
OLD | NEW |