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