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.
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/time.h"
13 #include "chrome/browser/net/evicted_domain_cookie_counter.h"
14 #include "net/cookies/canonical_cookie.h"
15 #include "net/cookies/cookie_monster.h"
16 #include "testing/gtest/include/gtest/gtest.h"
19 namespace chrome_browser_net
{
22 using base::TimeDelta
;
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";
31 class EvictedDomainCookieCounterTest
: public testing::Test
{
33 class MockDelegate
: public EvictedDomainCookieCounter::Delegate
{
35 explicit MockDelegate(EvictedDomainCookieCounterTest
* tester
);
37 // EvictedDomainCookieCounter::Delegate implementation.
39 const EvictedDomainCookieCounter::EvictedCookie
& evicted_cookie
,
40 const Time
& reinstatement_time
) override
;
41 virtual Time
CurrentTime() const override
;
44 EvictedDomainCookieCounterTest
* tester_
;
47 EvictedDomainCookieCounterTest();
48 virtual ~EvictedDomainCookieCounterTest();
50 // testing::Test implementation.
51 virtual void SetUp() override
;
52 virtual void TearDown() override
;
54 // Initialization that allows parameters to be specified.
55 void InitCounter(size_t max_size
, size_t purge_count
);
57 // Wrapper to allocate new cookie and store it in |cookies_|.
58 // If |max_age| == 0, then the cookie does not expire.
60 const char* url
, const std::string
& cookie_line
, int64 max_age
);
62 // Clears |cookies_| and creates common cookies for multiple tests.
63 void InitStockCookies();
65 // Sets simulation time to |rel_time|.
66 void GotoTime(int64 rel_time
);
68 // Simulates time-passage by |delta_second|.
69 void StepTime(int64 delta_second
);
71 // Simulates cookie addition or update.
72 void Add(net::CanonicalCookie
* cookie
);
74 // Simulates cookie removal.
75 void Remove(net::CanonicalCookie
* cookie
);
77 // Simulates cookie eviction.
78 void Evict(net::CanonicalCookie
* cookie
);
80 // For semi-realism, time considered are relative to |mock_time_base_|.
84 // To store allocated cookies for reuse.
85 ScopedVector
<net::CanonicalCookie
> cookies_
;
87 scoped_refptr
<EvictedDomainCookieCounter
> cookie_counter_
;
89 // Statistics as comma-separated string of duration (in seconds) between
90 // eviction and reinstatement for each cookie, in the order of eviction.
91 std::string google_stat_
;
92 std::string other_stat_
;
95 EvictedDomainCookieCounterTest::MockDelegate::MockDelegate(
96 EvictedDomainCookieCounterTest
* tester
)
99 void EvictedDomainCookieCounterTest::MockDelegate::Report(
100 const EvictedDomainCookieCounter::EvictedCookie
& evicted_cookie
,
101 const Time
& reinstatement_time
) {
102 std::string
& dest
= evicted_cookie
.is_google
?
103 tester_
->google_stat_
: tester_
->other_stat_
;
106 TimeDelta
delta(reinstatement_time
- evicted_cookie
.eviction_time
);
107 dest
.append(base::Int64ToString(delta
.InSeconds()));
110 Time
EvictedDomainCookieCounterTest::MockDelegate::CurrentTime() const {
111 return tester_
->mock_time_
;
114 EvictedDomainCookieCounterTest::EvictedDomainCookieCounterTest() {}
116 EvictedDomainCookieCounterTest::~EvictedDomainCookieCounterTest() {}
118 void EvictedDomainCookieCounterTest::SetUp() {
119 mock_time_base_
= Time::Now() - TimeDelta::FromHours(1);
120 mock_time_
= mock_time_base_
;
123 void EvictedDomainCookieCounterTest::TearDown() {
126 void EvictedDomainCookieCounterTest::InitCounter(size_t max_size
,
127 size_t purge_count
) {
128 scoped_ptr
<MockDelegate
> cookie_counter_delegate(new MockDelegate(this));
129 cookie_counter_
= new EvictedDomainCookieCounter(
130 NULL
, cookie_counter_delegate
.Pass(), max_size
, purge_count
);
133 void EvictedDomainCookieCounterTest::CreateNewCookie(
134 const char* url
, const std::string
& cookie_line
, int64 max_age
) {
135 std::string
line(cookie_line
);
137 line
.append(";max-age=" + base::Int64ToString(max_age
));
138 net::CanonicalCookie
* cookie
= net::CanonicalCookie::Create(
139 GURL(url
), line
, mock_time_
, net::CookieOptions());
141 cookies_
.push_back(cookie
);
144 void EvictedDomainCookieCounterTest::InitStockCookies() {
146 CreateNewCookie(google_url1
, "a1=1", 3000); // cookies_[0].
147 CreateNewCookie(google_url2
, "a2=1", 2000); // cookies_[1].
148 CreateNewCookie(other_url1
, "a1=1", 1000); // cookies_[2].
149 CreateNewCookie(other_url1
, "a2=1", 1001); // cookies_[3].
150 CreateNewCookie(google_url1
, "a1=1;Path=/sub", 999); // cookies_[4].
151 CreateNewCookie(other_url2
, "a2=1", 0); // cookies_[5].
154 void EvictedDomainCookieCounterTest::GotoTime(int64 rel_time
) {
155 mock_time_
= mock_time_base_
+ TimeDelta::FromSeconds(rel_time
);
158 void EvictedDomainCookieCounterTest::StepTime(int64 delta_second
) {
159 mock_time_
+= TimeDelta::FromSeconds(delta_second
);
162 void EvictedDomainCookieCounterTest::Add(net::CanonicalCookie
* cookie
) {
163 cookie_counter_
->OnCookieChanged(
164 *cookie
, false, net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT
);
167 void EvictedDomainCookieCounterTest::Remove(net::CanonicalCookie
* cookie
) {
168 cookie_counter_
->OnCookieChanged(
169 *cookie
, true, net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT
);
172 void EvictedDomainCookieCounterTest::Evict(net::CanonicalCookie
* cookie
) {
173 cookie_counter_
->OnCookieChanged(
174 *cookie
, true, net::CookieMonster::Delegate::CHANGE_COOKIE_EVICTED
);
177 // EvictedDomainCookieCounter takes (and owns) a CookieMonster::Delegate for
178 // chaining. To ensure that the chaining indeed occurs, we implement a
179 // dummy CookieMonster::Delegate to increment an integer.
180 TEST_F(EvictedDomainCookieCounterTest
, TestChain
) {
183 class ChangedDelegateDummy
: public net::CookieMonster::Delegate
{
185 explicit ChangedDelegateDummy(int* result
) : result_(result
) {}
187 virtual void OnCookieChanged(const net::CanonicalCookie
& cookie
,
189 ChangeCause cause
) override
{
193 virtual void OnLoaded() override
{}
196 virtual ~ChangedDelegateDummy() {}
201 scoped_ptr
<MockDelegate
> cookie_counter_delegate(new MockDelegate(this));
202 cookie_counter_
= new EvictedDomainCookieCounter(
203 new ChangedDelegateDummy(&result
), cookie_counter_delegate
.Pass(), 10, 5);
205 // Perform 6 cookie transactions.
206 for (int i
= 0; i
< 6; ++i
) {
213 EXPECT_EQ(18, result
); // 6 cookies x 3 operations each.
216 // Basic flow: add cookies, evict, then reinstate.
217 TEST_F(EvictedDomainCookieCounterTest
, TestBasicFlow
) {
220 // Add all cookies at (relative time) t = 0.
221 for (int i
= 0; i
< 6; ++i
)
223 EXPECT_EQ(0u, cookie_counter_
->GetStorageSize()); // No activities on add.
224 EXPECT_EQ(";", google_stat_
+ ";" + other_stat_
);
225 // Evict cookies at t = [1,3,6,10,15,21].
226 for (int i
= 0; i
< 6; ++i
) {
230 EXPECT_EQ(6u, cookie_counter_
->GetStorageSize()); // Storing all evictions.
231 EXPECT_EQ(";", google_stat_
+ ";" + other_stat_
);
232 // Reinstate cookies at t = [22,23,24,25,26,27].
233 for (int i
= 0; i
< 6; ++i
) {
237 EXPECT_EQ(0u, cookie_counter_
->GetStorageSize()); // Everything is removed.
238 // Expected reinstatement delays: [21,20,18,15,11,6].
239 EXPECT_EQ("21,20,11;18,15,6", google_stat_
+ ";" + other_stat_
);
242 // Removed cookies are ignored by EvictedDomainCookieCounter.
243 TEST_F(EvictedDomainCookieCounterTest
, TestRemove
) {
246 // Add all cookies at (relative time) t = 0.
247 for (int i
= 0; i
< 6; ++i
)
249 // Remove cookies at t = [1,3,6,10,15,21].
250 for (int i
= 0; i
< 6; ++i
) {
254 EXPECT_EQ(0u, cookie_counter_
->GetStorageSize());
255 // Add cookies again at t = [22,23,24,25,26,27].
256 for (int i
= 0; i
< 5; ++i
) {
260 EXPECT_EQ(0u, cookie_counter_
->GetStorageSize());
261 // No cookies were evicted, so no reinstatement take place.
262 EXPECT_EQ(";", google_stat_
+ ";" + other_stat_
);
265 // Expired cookies should not be counted by EvictedDomainCookieCounter.
266 TEST_F(EvictedDomainCookieCounterTest
, TestExpired
) {
269 // Add all cookies at (relative time) t = 0.
270 for (int i
= 0; i
< 6; ++i
)
272 // Evict cookies at t = [1,3,6,10,15,21].
273 for (int i
= 0; i
< 6; ++i
) {
277 EXPECT_EQ(6u, cookie_counter_
->GetStorageSize());
278 GotoTime(1000); // t = 1000, so cookies_[2,4] expire.
280 // Reinstate cookies at t = [1000,1000,(1000),1000,(1000),1000].
281 InitStockCookies(); // Refresh cookies, so new cookies expire in the future.
282 for (int i
= 0; i
< 6; ++i
)
284 EXPECT_EQ(0u, cookie_counter_
->GetStorageSize());
285 // Reinstatement delays: [999,997,(994),990,(985),979].
286 EXPECT_EQ("999,997;990,979", google_stat_
+ ";" + other_stat_
);
289 // Garbage collection should remove the oldest evicted cookies.
290 TEST_F(EvictedDomainCookieCounterTest
, TestGarbageCollection
) {
291 InitCounter(4, 2); // Reduced capacity.
293 // Add all cookies at (relative time) t = 0.
294 for (int i
= 0; i
< 6; ++i
)
296 // Evict cookies at t = [1,3,6,10].
297 for (int i
= 0; i
< 4; ++i
) {
301 EXPECT_EQ(4u, cookie_counter_
->GetStorageSize()); // Reached capacity.
303 Evict(cookies_
[4]); // Evict at t = 15, garbage collection takes place.
304 EXPECT_EQ(2u, cookie_counter_
->GetStorageSize());
306 Evict(cookies_
[5]); // Evict at t = 21.
307 EXPECT_EQ(3u, cookie_counter_
->GetStorageSize());
308 EXPECT_EQ(";", google_stat_
+ ";" + other_stat_
);
309 // Reinstate cookies at t = [(100),(100),(100),100,100,100].
311 for (int i
= 0; i
< 6; ++i
)
313 // Expected reinstatement delays: [(99),(97),(94),90,85,79]
314 EXPECT_EQ("85;90,79", google_stat_
+ ";" + other_stat_
);
317 // Garbage collection should remove the specified number of evicted cookies
318 // even when there are ties amongst oldest evicted cookies.
319 TEST_F(EvictedDomainCookieCounterTest
, TestGarbageCollectionTie
) {
321 // Add 10 cookies at time [0,1,3,6,...,45]
322 for (int i
= 0; i
< 10; ++i
) {
324 CreateNewCookie(google_url1
, "a" + base::IntToString(i
) + "=1", 3000);
327 // Evict 6 cookies at t = [100,...,100].
329 for (int i
= 0; i
< 6; ++i
)
331 EXPECT_EQ(6u, cookie_counter_
->GetStorageSize());
332 // Evict 3 cookies at t = [210,220,230].
334 for (int i
= 6; i
< 9; ++i
) {
338 EXPECT_EQ(9u, cookie_counter_
->GetStorageSize()); // Reached capacity.
339 // Evict 1 cookie at t = 300, and garbage collection takes place.
342 // Some arbitrary 4 out of 6 cookies evicted at t = 100 are gone from storage.
343 EXPECT_EQ(6u, cookie_counter_
->GetStorageSize()); // 10 - 4.
344 // Reinstate cookies at t = [400,...,400].
346 for (int i
= 0; i
< 10; ++i
)
348 EXPECT_EQ(0u, cookie_counter_
->GetStorageSize());
349 // Expected reinstatement delays:
350 // [300,300,300,300,300,300 <= keeping 2 only,190,180,170,100].
351 EXPECT_EQ("300,300,190,180,170,100;", google_stat_
+ ";" + other_stat_
);
354 // Garbage collection prioritize removal of expired cookies.
355 TEST_F(EvictedDomainCookieCounterTest
, TestGarbageCollectionWithExpiry
) {
358 // Add all cookies at (relative time) t = 0.
359 for (int i
= 0; i
< 6; ++i
)
361 // Evict cookies at t = [1,3,6,10,15].
362 for (int i
= 0; i
< 5; ++i
) {
366 EXPECT_EQ(5u, cookie_counter_
->GetStorageSize()); // Reached capacity.
367 GotoTime(1200); // t = 1200, so cookies_[2,3,4] expire.
368 // Evict cookies_[5] (not expired) at t = 1200.
370 // Garbage collection would have taken place, removing 3 expired cookies,
371 // so that there's no need to remove more.
372 EXPECT_EQ(3u, cookie_counter_
->GetStorageSize());
373 // Reinstate cookies at t = [1500,1500,(1500),(1500),(1500),1500].
375 InitStockCookies(); // Refresh cookies, so new cookies expire in the future.
376 for (int i
= 0; i
< 6; ++i
)
378 EXPECT_EQ(0u, cookie_counter_
->GetStorageSize());
379 // Reinstatement delays: [1499,1497,(1494),(1490),(1485),300].
380 EXPECT_EQ("1499,1497;300", google_stat_
+ ";" + other_stat_
);
385 } // namespace chrome_browser_net