This source file includes following definitions.
- Report
- CurrentTime
- SetUp
- TearDown
- InitCounter
- CreateNewCookie
- InitStockCookies
- GotoTime
- StepTime
- Add
- Remove
- TEST_F
- OnCookieChanged
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#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/time.h"
#include "chrome/browser/net/evicted_domain_cookie_counter.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_monster.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace chrome_browser_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);
virtual void Report(
const EvictedDomainCookieCounter::EvictedCookie& evicted_cookie,
const Time& reinstatement_time) OVERRIDE;
virtual Time CurrentTime() const OVERRIDE;
private:
EvictedDomainCookieCounterTest* tester_;
};
EvictedDomainCookieCounterTest();
virtual ~EvictedDomainCookieCounterTest();
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
void InitCounter(size_t max_size, size_t purge_count);
void CreateNewCookie(
const char* url, const std::string& cookie_line, int64 max_age);
void InitStockCookies();
void GotoTime(int64 rel_time);
void StepTime(int64 delta_second);
void Add(net::CanonicalCookie* cookie);
void Remove(net::CanonicalCookie* cookie);
void Evict(net::CanonicalCookie* cookie);
Time mock_time_base_;
Time mock_time_;
ScopedVector<net::CanonicalCookie> cookies_;
scoped_refptr<EvictedDomainCookieCounter> cookie_counter_;
std::string google_stat_;
std::string other_stat_;
};
EvictedDomainCookieCounterTest::MockDelegate::MockDelegate(
EvictedDomainCookieCounterTest* tester)
: tester_(tester) {}
void EvictedDomainCookieCounterTest::MockDelegate::Report(
const EvictedDomainCookieCounter::EvictedCookie& evicted_cookie,
const Time& reinstatement_time) {
std::string& dest = evicted_cookie.is_google ?
tester_->google_stat_ : tester_->other_stat_;
if (!dest.empty())
dest.append(",");
TimeDelta delta(reinstatement_time - evicted_cookie.eviction_time);
dest.append(base::Int64ToString(delta.InSeconds()));
}
Time EvictedDomainCookieCounterTest::MockDelegate::CurrentTime() const {
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> cookie_counter_delegate(new MockDelegate(this));
cookie_counter_ = new EvictedDomainCookieCounter(
NULL,
cookie_counter_delegate.PassAs<EvictedDomainCookieCounter::Delegate>(),
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));
net::CanonicalCookie* cookie = net::CanonicalCookie::Create(
GURL(url), line, mock_time_, net::CookieOptions());
DCHECK(cookie);
cookies_.push_back(cookie);
}
void EvictedDomainCookieCounterTest::InitStockCookies() {
cookies_.clear();
CreateNewCookie(google_url1, "a1=1", 3000);
CreateNewCookie(google_url2, "a2=1", 2000);
CreateNewCookie(other_url1, "a1=1", 1000);
CreateNewCookie(other_url1, "a2=1", 1001);
CreateNewCookie(google_url1, "a1=1;Path=/sub", 999);
CreateNewCookie(other_url2, "a2=1", 0);
}
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(net::CanonicalCookie* cookie) {
cookie_counter_->OnCookieChanged(
*cookie, false, net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT);
}
void EvictedDomainCookieCounterTest::Remove(net::CanonicalCookie* cookie) {
cookie_counter_->OnCookieChanged(
*cookie, true, net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT);
}
void EvictedDomainCookieCounterTest::Evict(net::CanonicalCookie* cookie) {
cookie_counter_->OnCookieChanged(
*cookie, true, net::CookieMonster::Delegate::CHANGE_COOKIE_EVICTED);
}
TEST_F(EvictedDomainCookieCounterTest, TestChain) {
int result = 0;
class ChangedDelegateDummy : public net::CookieMonster::Delegate {
public:
explicit ChangedDelegateDummy(int* result) : result_(result) {}
virtual void OnCookieChanged(const net::CanonicalCookie& cookie,
bool removed,
ChangeCause cause) OVERRIDE {
++(*result_);
}
private:
virtual ~ChangedDelegateDummy() {}
int* result_;
};
scoped_ptr<MockDelegate> cookie_counter_delegate(new MockDelegate(this));
cookie_counter_ = new EvictedDomainCookieCounter(
new ChangedDelegateDummy(&result),
cookie_counter_delegate.PassAs<EvictedDomainCookieCounter::Delegate>(),
10,
5);
InitStockCookies();
for (int i = 0; i < 6; ++i) {
Add(cookies_[i]);
StepTime(1);
Evict(cookies_[i]);
StepTime(1);
Remove(cookies_[i]);
}
EXPECT_EQ(18, result);
}
TEST_F(EvictedDomainCookieCounterTest, TestBasicFlow) {
InitCounter(10, 4);
InitStockCookies();
for (int i = 0; i < 6; ++i)
Add(cookies_[i]);
EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
for (int i = 0; i < 6; ++i) {
StepTime(i + 1);
Evict(cookies_[i]);
}
EXPECT_EQ(6u, cookie_counter_->GetStorageSize());
EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
for (int i = 0; i < 6; ++i) {
StepTime(1);
Add(cookies_[i]);
}
EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
EXPECT_EQ("21,20,11;18,15,6", google_stat_ + ";" + other_stat_);
}
TEST_F(EvictedDomainCookieCounterTest, TestRemove) {
InitCounter(10, 4);
InitStockCookies();
for (int i = 0; i < 6; ++i)
Add(cookies_[i]);
for (int i = 0; i < 6; ++i) {
StepTime(i + 1);
Remove(cookies_[i]);
}
EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
for (int i = 0; i < 5; ++i) {
StepTime(1);
Add(cookies_[i]);
}
EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
}
TEST_F(EvictedDomainCookieCounterTest, TestExpired) {
InitCounter(10, 4);
InitStockCookies();
for (int i = 0; i < 6; ++i)
Add(cookies_[i]);
for (int i = 0; i < 6; ++i) {
StepTime(i + 1);
Evict(cookies_[i]);
}
EXPECT_EQ(6u, cookie_counter_->GetStorageSize());
GotoTime(1000);
InitStockCookies();
for (int i = 0; i < 6; ++i)
Add(cookies_[i]);
EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
EXPECT_EQ("999,997;990,979", google_stat_ + ";" + other_stat_);
}
TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollection) {
InitCounter(4, 2);
InitStockCookies();
for (int i = 0; i < 6; ++i)
Add(cookies_[i]);
for (int i = 0; i < 4; ++i) {
StepTime(i + 1);
Evict(cookies_[i]);
}
EXPECT_EQ(4u, cookie_counter_->GetStorageSize());
StepTime(5);
Evict(cookies_[4]);
EXPECT_EQ(2u, cookie_counter_->GetStorageSize());
StepTime(6);
Evict(cookies_[5]);
EXPECT_EQ(3u, cookie_counter_->GetStorageSize());
EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
GotoTime(100);
for (int i = 0; i < 6; ++i)
Add(cookies_[i]);
EXPECT_EQ("85;90,79", google_stat_ + ";" + other_stat_);
}
TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionTie) {
InitCounter(9, 3);
for (int i = 0; i < 10; ++i) {
StepTime(i);
CreateNewCookie(google_url1, "a" + base::IntToString(i) + "=1", 3000);
Add(cookies_[i]);
}
GotoTime(100);
for (int i = 0; i < 6; ++i)
Evict(cookies_[i]);
EXPECT_EQ(6u, cookie_counter_->GetStorageSize());
GotoTime(200);
for (int i = 6; i < 9; ++i) {
StepTime(10);
Evict(cookies_[i]);
}
EXPECT_EQ(9u, cookie_counter_->GetStorageSize());
GotoTime(300);
Evict(cookies_[9]);
EXPECT_EQ(6u, cookie_counter_->GetStorageSize());
GotoTime(400);
for (int i = 0; i < 10; ++i)
Add(cookies_[i]);
EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
EXPECT_EQ("300,300,190,180,170,100;", google_stat_ + ";" + other_stat_);
}
TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionWithExpiry) {
InitCounter(5, 1);
InitStockCookies();
for (int i = 0; i < 6; ++i)
Add(cookies_[i]);
for (int i = 0; i < 5; ++i) {
StepTime(i + 1);
Evict(cookies_[i]);
}
EXPECT_EQ(5u, cookie_counter_->GetStorageSize());
GotoTime(1200);
Evict(cookies_[5]);
EXPECT_EQ(3u, cookie_counter_->GetStorageSize());
GotoTime(1500);
InitStockCookies();
for (int i = 0; i < 6; ++i)
Add(cookies_[i]);
EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
EXPECT_EQ("1499,1497;300", google_stat_ + ";" + other_stat_);
}
}
}