Chromium Code Reviews| Index: net/cookies/evicted_domain_cookie_counter_unittest.cc |
| diff --git a/net/cookies/evicted_domain_cookie_counter_unittest.cc b/net/cookies/evicted_domain_cookie_counter_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..30226c0e2b80792f41cf982063e3c00b9209d04f |
| --- /dev/null |
| +++ b/net/cookies/evicted_domain_cookie_counter_unittest.cc |
| @@ -0,0 +1,413 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/time.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "net/cookies/canonical_cookie.h" |
| +#include "net/cookies/cookie_monster.h" |
| +#include "net/cookies/evicted_domain_cookie_counter.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| + |
| +using base::Time; |
| +using base::TimeDelta; |
| + |
| +namespace { |
| + |
| +const char* google_url1 = "http://www.google.com"; |
| +const char* google_url2 = "http://mail.google.com"; |
| +const char* other_url1 = "http://www.example.com"; |
| +const char* other_url2 = "http://www.example.co.uk"; |
| + |
| +class EvictedDomainCookieCounterTest : public testing::Test { |
| + protected: |
| + class MockDelegate : public EvictedDomainCookieCounter::Delegate { |
| + public: |
| + explicit MockDelegate(EvictedDomainCookieCounterTest* tester); |
| + |
| + // EvictedDomainCookieCounter::Delegate implementation. |
| + virtual void Report(const EvictedDomainCookieCounter::EvictedCookie& ec, |
| + const Time& reinstatement_time) OVERRIDE; |
| + virtual Time CurrentTime() OVERRIDE; |
| + |
| + private: |
| + EvictedDomainCookieCounterTest* tester_; |
| + }; |
| + |
| + EvictedDomainCookieCounterTest(); |
| + virtual ~EvictedDomainCookieCounterTest(); |
| + |
| + // testing::Test implementation. |
| + virtual void SetUp() OVERRIDE; |
| + virtual void TearDown() OVERRIDE; |
| + |
| + // Initialization that allows parameters to be specified. |
| + void InitCounter(size_t max_size, size_t purge_count); |
| + |
| + // Wrapper to allocate new cookie and store it in |cookies_|. |
| + // If |max_age| == 0, then the cookie does not expire. |
| + void CreateNewCookie( |
| + const char* url, const std::string& cookie_line, int64 max_age); |
| + |
| + // Clears |cookies_| and creates common cookies for multiple tests. |
| + void InitStockCookies(); |
| + |
| + // Sets simulation time to |rel_time|. |
| + void GotoTime(int64 rel_time); |
| + |
| + // Simulates time-passage by |delta_second|. |
| + void StepTime(int64 delta_second); |
| + |
| + // Simulates cookie addition or update. |
| + void Add(CanonicalCookie* cc); |
| + |
| + // Simulates cookie removal. |
| + void Remove(CanonicalCookie* cc); |
| + |
| + // Simulates cookie eviction. |
| + void Evict(CanonicalCookie* cc); |
| + |
| + // For semi-realism, time considered are relative to |mock_time_base_|. |
| + Time mock_time_base_; |
| + Time mock_time_; |
| + |
| + // To store allocated cookies for reuse |
| + ScopedVector<CanonicalCookie> cookies_; |
| + |
| + scoped_refptr<EvictedDomainCookieCounter> edcc_; |
| + |
| + // Statistics as comma-separated string of duration (in seconds) between |
| + // eviction and reinstatement for each cookie, in the order of eviction. |
| + std::string google_stat_; |
| + std::string other_stat_; |
| +}; |
| + |
| +EvictedDomainCookieCounterTest::MockDelegate::MockDelegate( |
| + EvictedDomainCookieCounterTest* tester) |
| + : tester_(tester) {} |
| + |
| +void EvictedDomainCookieCounterTest::MockDelegate::Report( |
| + const EvictedDomainCookieCounter::EvictedCookie& ec, |
| + const Time& reinstatement_time) { |
| + std::string& dest = ec.is_google_ ? |
| + tester_->google_stat_ : tester_->other_stat_; |
| + if (!dest.empty()) |
| + dest.append(","); |
| + TimeDelta delta(reinstatement_time - ec.eviction_time_); |
| + dest.append(base::Int64ToString(delta.InSeconds())); |
| +} |
| + |
| +Time EvictedDomainCookieCounterTest::MockDelegate::CurrentTime() { |
| + return tester_->mock_time_; |
| +} |
| + |
| +EvictedDomainCookieCounterTest::EvictedDomainCookieCounterTest() {} |
| + |
| +EvictedDomainCookieCounterTest::~EvictedDomainCookieCounterTest() {} |
| + |
| +void EvictedDomainCookieCounterTest::SetUp() { |
| + mock_time_base_ = Time::Now() - TimeDelta::FromHours(1); |
| + mock_time_ = mock_time_base_; |
| +} |
| + |
| +void EvictedDomainCookieCounterTest::TearDown() { |
| +} |
| + |
| +void EvictedDomainCookieCounterTest::InitCounter(size_t max_size, |
| + size_t purge_count) { |
| + scoped_ptr<MockDelegate> delegate(new MockDelegate(this)); |
| + edcc_ = new EvictedDomainCookieCounter(NULL, delegate.Pass(), |
| + max_size, purge_count); |
| +} |
| + |
| +void EvictedDomainCookieCounterTest::CreateNewCookie( |
| + const char* url, const std::string& cookie_line, int64 max_age) { |
| + std::string line(cookie_line); |
| + if (max_age) |
| + line.append(";max-age=" + base::Int64ToString(max_age)); |
| + CanonicalCookie* cc = CanonicalCookie::Create( |
| + GURL(url), line, mock_time_, CookieOptions()); |
| + DCHECK(cc); |
| + cookies_.push_back(cc); |
| +} |
| + |
| +void EvictedDomainCookieCounterTest::InitStockCookies() { |
| + cookies_.clear(); |
| + CreateNewCookie(google_url1, "a1=1", 3000); // cookies_[0]. |
| + CreateNewCookie(google_url2, "a2=1", 2000); // cookies_[1]. |
| + CreateNewCookie(other_url1, "a1=1", 1000); // cookies_[2]. |
| + CreateNewCookie(other_url1, "a2=1", 1001); // cookies_[3]. |
| + CreateNewCookie(google_url1, "a1=1;Path=/sub", 999); // cookies_[4]. |
| + CreateNewCookie(other_url2, "a2=1", 0); // cookies_[5]. |
| +} |
| + |
| +void EvictedDomainCookieCounterTest::GotoTime(int64 rel_time) { |
| + mock_time_ = mock_time_base_ + TimeDelta::FromSeconds(rel_time); |
| +} |
| + |
| +void EvictedDomainCookieCounterTest::StepTime(int64 delta_second) { |
| + mock_time_ += TimeDelta::FromSeconds(delta_second); |
| +} |
| + |
| +void EvictedDomainCookieCounterTest::Add(CanonicalCookie* cc) { |
| + edcc_->OnCookieChanged(*cc, false, |
| + CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT); |
| +} |
| + |
| +void EvictedDomainCookieCounterTest::Remove(CanonicalCookie* cc) { |
| + edcc_->OnCookieChanged(*cc, true, |
| + CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT); |
| +} |
| + |
| +void EvictedDomainCookieCounterTest::Evict(CanonicalCookie* cc) { |
| + edcc_->OnCookieChanged(*cc, true, |
| + CookieMonster::Delegate::CHANGE_COOKIE_EVICTED); |
| +} |
| + |
| +} // namespace |
| + |
| +// Test EvictedDomainCookieCounter::UnsafeIsGoogleCookie(). |
| +TEST_F(EvictedDomainCookieCounterTest, TestUnsafeIsGoogleCookie) { |
| + struct { |
| + char* url; |
| + bool expected; |
| + } test_cases[] = { |
| + {"http://www.example.com", false}, |
| + {"http://www.example.com/google.com", false}, |
| + {"http://www.google.com", true}, |
| + {"http://www.google.com:8080", true}, |
| + {"http://google.com", true}, |
| + {"http://google.example.com", false}, |
| + {"http://www.google.com/", true}, |
| + {"http://www.google.com/some/sub/directory", true}, |
| + {"https://www.GOOgle.co.uk", true}, |
| + {"http://google.google.com", true}, |
| + {"http://www.thegoogle.com", false}, |
| + {"http://www.googlers.com", false}, |
| + {"http://74.125.226.0", false}, |
| + }; |
| + for (int i = 0; i < arraysize(test_cases); ++i) { |
| + scoped_ptr<CanonicalCookie> cc(CanonicalCookie::Create( |
| + GURL(test_cases[i].url), "a=1", mock_time_, CookieOptions())); |
| + EXPECT_EQ(test_cases[i].expected, |
| + EvictedDomainCookieCounter::UnsafeIsGoogleCookie(*cc)); |
| + } |
| +} |
| + |
| +// EvictedDomainCookieCounter takes (and owns) a CookieMonster::Delegate for |
| +// chaining. To ensure that the chaining indeed occurs, we implement a |
| +// dummy CookieMonster::Delegate to increment an integer. |
| +TEST_F(EvictedDomainCookieCounterTest, TestChain) { |
| + int result = 0; |
| + |
| + class ChangedDelegateDummy : public CookieMonster::Delegate { |
| + public: |
| + explicit ChangedDelegateDummy(int* result) : result_(result) {} |
| + |
| + virtual void OnCookieChanged(const CanonicalCookie& cookie, |
| + bool removed, |
| + ChangeCause cause) OVERRIDE { |
| + ++(*result_); |
| + } |
| + |
| + private: |
| + int* result_; |
| + }; |
| + |
| + scoped_ptr<MockDelegate> delegate(new MockDelegate(this)); |
| + edcc_ = new EvictedDomainCookieCounter(new ChangedDelegateDummy(&result), |
| + delegate.Pass(), 10, 5); |
| + InitStockCookies(); |
| + // Perform 6 cookie transactions. |
| + for (int i = 0; i < 6; ++i) { |
| + Add(cookies_[i]); |
| + StepTime(1); |
| + Evict(cookies_[i]); |
| + StepTime(1); |
| + Remove(cookies_[i]); |
| + } |
| + EXPECT_EQ(18, result); // 6 cookies x 3 operations each. |
| +} |
| + |
| +// Basic flow: add cookies, evict, then reinstate. |
| +TEST_F(EvictedDomainCookieCounterTest, TestBasicFlow) { |
| + InitCounter(10, 4); |
| + InitStockCookies(); |
| + // Add all cookies at (relative time) t = 0. |
| + for (int i = 0; i < 6; ++i) |
| + Add(cookies_[i]); |
| + EXPECT_EQ(0, edcc_->GetStorageSize()); // No activities on add. |
| + EXPECT_EQ(";", google_stat_ + ";" + other_stat_); |
| + // Evict cookies at t = [1,3,6,10,15,21]. |
| + for (int i = 0; i < 6; ++i) { |
| + StepTime(i + 1); |
| + Evict(cookies_[i]); |
| + } |
| + EXPECT_EQ(6, edcc_->GetStorageSize()); // All evictions are stored. |
| + EXPECT_EQ(";", google_stat_ + ";" + other_stat_); |
| + // Reinstate cookies at t = [22,23,24,25,26,27]. |
| + for (int i = 0; i < 6; ++i) { |
| + StepTime(1); |
| + Add(cookies_[i]); |
| + } |
| + EXPECT_EQ(0, edcc_->GetStorageSize()); // Everything is removed. |
| + // Expected reinstatement delays: [21,20,18,15,11,6]. |
| + EXPECT_EQ("21,20,11;18,15,6", google_stat_ + ";" + other_stat_); |
| +} |
| + |
| +// Removed cookies are ignored by EvictedDomainCookieCounter. |
| +TEST_F(EvictedDomainCookieCounterTest, TestRemove) { |
| + InitCounter(10, 4); |
| + InitStockCookies(); |
| + // Add all cookies at (relative time) t = 0. |
| + for (int i = 0; i < 6; ++i) |
| + Add(cookies_[i]); |
| + // Remove cookies at t = [1,3,6,10,15,21]. |
| + for (int i = 0; i < 6; ++i) { |
| + StepTime(i + 1); |
| + Remove(cookies_[i]); |
| + } |
| + EXPECT_EQ(0, edcc_->GetStorageSize()); |
| + // Add cookies again at t = [22,23,24,25,26,27]. |
| + for (int i = 0; i < 5; ++i) { |
| + StepTime(1); |
| + Add(cookies_[i]); |
| + } |
| + EXPECT_EQ(0, edcc_->GetStorageSize()); |
| + // No cookies were evicted, so no reinstatement take place. |
| + EXPECT_EQ(";", google_stat_ + ";" + other_stat_); |
| +} |
| + |
| +// Expired cookies should not be counted by EvictedDomainCookieCounter. |
| +TEST_F(EvictedDomainCookieCounterTest, TestExpired) { |
| + InitCounter(10, 4); |
| + InitStockCookies(); |
| + // Add all cookies at (relative time) t = 0. |
| + for (int i = 0; i < 6; ++i) |
| + Add(cookies_[i]); |
| + // Evict cookies at t = [1,3,6,10,15,21]. |
| + for (int i = 0; i < 6; ++i) { |
| + StepTime(i + 1); |
| + Evict(cookies_[i]); |
| + } |
| + EXPECT_EQ(6, edcc_->GetStorageSize()); |
| + GotoTime(1000); // t = 1000, so cookies_[2,4] expire. |
| + |
| + // Try to reinstate an expired cookie at t = 1000, should have no effect. |
| + 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).
|
| + CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED_OVERWRITE); |
| + EXPECT_EQ(6, edcc_->GetStorageSize()); |
| + |
| + // Reinstate cookies at t = [1000,1000,(1000),1000,(1000),1000]. |
| + InitStockCookies(); // Refresh cookies, so new cookies expire in the future. |
| + for (int i = 0; i < 6; ++i) |
| + Add(cookies_[i]); |
| + EXPECT_EQ(0, edcc_->GetStorageSize()); |
| + // Reinstatement delays: [999,997,(994),990,(985),979]. |
| + EXPECT_EQ("999,997;990,979", google_stat_ + ";" + other_stat_); |
| +} |
| + |
| +// Garbage collection should remove the oldest evicted cookies. |
| +TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollection) { |
| + InitCounter(4, 2); // Reduced capacity. |
| + InitStockCookies(); |
| + // Add all cookies at (relative time) t = 0. |
| + for (int i = 0; i < 6; ++i) |
| + Add(cookies_[i]); |
| + // Evict cookies at t = [1,3,6,10]. |
| + for (int i = 0; i < 4; ++i) { |
| + StepTime(i + 1); |
| + Evict(cookies_[i]); |
| + } |
| + EXPECT_EQ(4, edcc_->GetStorageSize()); // Reached capacity. |
| + StepTime(5); |
| + Evict(cookies_[4]); // Evict at t = 15, garbage collection takes place. |
| + EXPECT_EQ(2, edcc_->GetStorageSize()); |
| + StepTime(6); |
| + Evict(cookies_[5]); // Evict at t = 21. |
| + EXPECT_EQ(3, edcc_->GetStorageSize()); |
| + EXPECT_EQ(";", google_stat_ + ";" + other_stat_); |
| + // Reinstate cookies at t = [(100),(100),(100),100,100,100]. |
| + GotoTime(100); |
| + for (int i = 0; i < 6; ++i) |
| + Add(cookies_[i]); |
| + // Expected reinstatement delays: [(99),(97),(94),90,85,79] |
| + EXPECT_EQ("85;90,79", google_stat_ + ";" + other_stat_); |
| +} |
| + |
| +// Garbage collection should remove the specified number of evicted cookies |
| +// even when there are ties amongst oldest evicted cookies. |
| +TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionTie) { |
| + InitCounter(9, 3); |
| + // Add 10 cookies at time [0,1,3,6,...,45] |
| + for (int i = 0; i < 10; ++i) { |
| + StepTime(i); |
| + CreateNewCookie(google_url1, "a" + base::IntToString(i) + "=1", 3000); |
| + Add(cookies_[i]); |
| + } |
| + // Evict 6 cookies at t = [100,...,100]. |
| + GotoTime(100); |
| + for (int i = 0; i < 6; ++i) |
| + Evict(cookies_[i]); |
| + EXPECT_EQ(6, edcc_->GetStorageSize()); |
| + // Evict 3 cookies at t = [210,220,230]. |
| + GotoTime(200); |
| + for (int i = 6; i < 9; ++i) { |
| + StepTime(10); |
| + Evict(cookies_[i]); |
| + } |
| + EXPECT_EQ(9, edcc_->GetStorageSize()); // Reached capacity. |
| + // Evict 1 cookie at t = 300, and garbage collection takes place. |
| + GotoTime(300); |
| + Evict(cookies_[9]); |
| + // Some arbitrary 4 out of 6 cookies evicted at t = 100 are gone from storage. |
| + EXPECT_EQ(6, edcc_->GetStorageSize()); // 10 - 4. |
| + // Reinstate cookies at t = [400,...,400]. |
| + GotoTime(400); |
| + for (int i = 0; i < 10; ++i) |
| + Add(cookies_[i]); |
| + EXPECT_EQ(0, edcc_->GetStorageSize()); |
| + // Expected reinstatement delays: |
| + // [300,300,300,300,300,300 <= keeping 2 only,190,180,170,100]. |
| + EXPECT_EQ("300,300,190,180,170,100;", google_stat_ + ";" + other_stat_); |
| +} |
| + |
| +// Garbage collection prioritize removal of expired cookies. |
| +TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionWithExpiry) { |
| + InitCounter(5, 1); |
| + InitStockCookies(); |
| + // Add all cookies at (relative time) t = 0. |
| + for (int i = 0; i < 6; ++i) |
| + Add(cookies_[i]); |
| + // Evict cookies at t = [1,3,6,10,15]. |
| + for (int i = 0; i < 5; ++i) { |
| + StepTime(i + 1); |
| + Evict(cookies_[i]); |
| + } |
| + EXPECT_EQ(5, edcc_->GetStorageSize()); // Reached capacity. |
| + GotoTime(1200); // t = 1200, so cookies_[2,3,4] expire. |
| + // Evict cookies_[5] (not expired) at t = 1200. |
| + Evict(cookies_[5]); |
| + // Garbage collection would have taken place, removing 3 expired cookies, |
| + // so that there's no need to remove more. |
| + EXPECT_EQ(3, edcc_->GetStorageSize()); |
| + // Reinstate cookies at t = [1500,1500,(1500),(1500),(1500),1500]. |
| + GotoTime(1500); |
| + InitStockCookies(); // Refresh cookies, so new cookies expire in the future. |
| + for (int i = 0; i < 6; ++i) |
| + Add(cookies_[i]); |
| + EXPECT_EQ(0, edcc_->GetStorageSize()); |
| + // Reinstatement delays: [1499,1497,(1494),(1490),(1485),300]. |
| + EXPECT_EQ("1499,1497;300", google_stat_ + ";" + other_stat_); |
| +} |
| + |
| +} // namespace net |