Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: chrome/browser/net/evicted_domain_cookie_counter_unittest.cc

Issue 12805010: Implementing EvictedDomainCookieCounter to keep user metrics on cookie eviction and reinstatement. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Style and comment fixes; changing partial_sort() comparator to fix Linux build. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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(
39 const EvictedDomainCookieCounter::EvictedCookie& evicted_cookie,
40 const Time& reinstatement_time) OVERRIDE;
41 virtual Time CurrentTime() const OVERRIDE;
42
43 private:
44 EvictedDomainCookieCounterTest* tester_;
45 };
46
47 EvictedDomainCookieCounterTest();
48 virtual ~EvictedDomainCookieCounterTest();
49
50 // testing::Test implementation.
51 virtual void SetUp() OVERRIDE;
52 virtual void TearDown() OVERRIDE;
53
54 // Initialization that allows parameters to be specified.
55 void InitCounter(size_t max_size, size_t purge_count);
56
57 // Wrapper to allocateevicted_cookie new cookie and store it in |cookies_|.
mmenke 2013/04/03 14:23:51 This comment needs to be fixed.
huangs 2013/04/03 15:42:15 Made mistake in manual s/ec/evicted_cookie/. Done
58 // If |max_age| == 0, then the cookie does not expire.
59 void CreateNewCookie(
60 const char* url, const std::string& cookie_line, int64 max_age);
61
62 // Clears |cookies_| and creates common cookies for multiple tests.
63 void InitStockCookies();
64
65 // Sets simulation time to |rel_time|.
66 void GotoTime(int64 rel_time);
67
68 // Simulates time-passage by |delta_sevicted_cookieond|.
mmenke 2013/04/03 14:23:51 Also needs to be fixed.
huangs 2013/04/03 15:42:15 Done.
69 void StepTime(int64 delta_second);
70
71 // Simulates cookie addition or update.
72 void Add(net::CanonicalCookie* cookie);
73
74 // Simulates cookie removal.
75 void Remove(net::CanonicalCookie* cookie);
76
77 // Simulates cookie eviction.
78 void Evict(net::CanonicalCookie* cookie);
79
80 // For semi-realism, time considered are relative to |mock_time_base_|.
81 Time mock_time_base_;
82 Time mock_time_;
83
84 // To store allocated cookies for reuse.
85 ScopedVector<net::CanonicalCookie> cookies_;
86
87 scoped_refptr<EvictedDomainCookieCounter> cookie_counter_;
88
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_;
93 };
94
95 EvictedDomainCookieCounterTest::MockDelegate::MockDelegate(
96 EvictedDomainCookieCounterTest* tester)
97 : tester_(tester) {}
98
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_;
104 if (!dest.empty())
105 dest.append(",");
106 TimeDelta delta(reinstatement_time - evicted_cookie.eviction_time);
107 dest.append(base::Int64ToString(delta.InSeconds()));
108 }
109
110 Time EvictedDomainCookieCounterTest::MockDelegate::CurrentTime() const {
111 return tester_->mock_time_;
112 }
113
114 EvictedDomainCookieCounterTest::EvictedDomainCookieCounterTest() {}
115
116 EvictedDomainCookieCounterTest::~EvictedDomainCookieCounterTest() {}
117
118 void EvictedDomainCookieCounterTest::SetUp() {
119 mock_time_base_ = Time::Now() - TimeDelta::FromHours(1);
120 mock_time_ = mock_time_base_;
121 }
122
123 void EvictedDomainCookieCounterTest::TearDown() {
124 }
125
126 void EvictedDomainCookieCounterTest::InitCounter(size_t max_size,
127 size_t purge_count) {
128 scoped_ptr<MockDelegate> delegate(new MockDelegate(this));
129 cookie_counter_ = new EvictedDomainCookieCounter(NULL, delegate.Pass(),
130 max_size, purge_count);
131 }
132
133 void EvictedDomainCookieCounterTest::CreateNewCookie(
134 const char* url, const std::string& cookie_line, int64 max_age) {
135 std::string line(cookie_line);
136 if (max_age)
137 line.append(";max-age=" + base::Int64ToString(max_age));
138 net::CanonicalCookie* cookie = net::CanonicalCookie::Create(
139 GURL(url), line, mock_time_, net::CookieOptions());
140 DCHECK(cookie);
141 cookies_.push_back(cookie);
142 }
143
144 void EvictedDomainCookieCounterTest::InitStockCookies() {
145 cookies_.clear();
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].
152 }
153
154 void EvictedDomainCookieCounterTest::GotoTime(int64 rel_time) {
155 mock_time_ = mock_time_base_ + TimeDelta::FromSeconds(rel_time);
156 }
157
158 void EvictedDomainCookieCounterTest::StepTime(int64 delta_second) {
159 mock_time_ += TimeDelta::FromSeconds(delta_second);
160 }
161
162 void EvictedDomainCookieCounterTest::Add(net::CanonicalCookie* cookie) {
163 cookie_counter_->OnCookieChanged(
164 *cookie, false, net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT);
165 }
166
167 void EvictedDomainCookieCounterTest::Remove(net::CanonicalCookie* cookie) {
168 cookie_counter_->OnCookieChanged(
169 *cookie, true, net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT);
170 }
171
172 void EvictedDomainCookieCounterTest::Evict(net::CanonicalCookie* cookie) {
173 cookie_counter_->OnCookieChanged(
174 *cookie, true, net::CookieMonster::Delegate::CHANGE_COOKIE_EVICTED);
175 }
176
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) {
181 int result = 0;
182
183 class ChangedDelegateDummy : public net::CookieMonster::Delegate {
184 public:
185 explicit ChangedDelegateDummy(int* result) : result_(result) {}
186
187 virtual void OnCookieChanged(const net::CanonicalCookie& cookie,
188 bool removed,
189 ChangeCause cause) OVERRIDE {
190 ++(*result_);
191 }
192
193 private:
194 int* result_;
195 };
196
197 scoped_ptr<MockDelegate> delegate(new MockDelegate(this));
198 cookie_counter_ = new EvictedDomainCookieCounter(
199 new ChangedDelegateDummy(&result), delegate.Pass(), 10, 5);
200 InitStockCookies();
201 // Perform 6 cookie transactions.
202 for (int i = 0; i < 6; ++i) {
203 Add(cookies_[i]);
204 StepTime(1);
205 Evict(cookies_[i]);
206 StepTime(1);
207 Remove(cookies_[i]);
208 }
209 EXPECT_EQ(18, result); // 6 cookies x 3 operations each.
210 }
211
212 // Basic flow: add cookies, evict, then reinstate.
213 TEST_F(EvictedDomainCookieCounterTest, TestBasicFlow) {
214 InitCounter(10, 4);
215 InitStockCookies();
216 // Add all cookies at (relative time) t = 0.
217 for (int i = 0; i < 6; ++i)
218 Add(cookies_[i]);
219 EXPECT_EQ(0, cookie_counter_->GetStorageSize()); // No activities on add.
220 EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
221 // Evict cookies at t = [1,3,6,10,15,21].
222 for (int i = 0; i < 6; ++i) {
223 StepTime(i + 1);
224 Evict(cookies_[i]);
225 }
226 EXPECT_EQ(6, cookie_counter_->GetStorageSize()); // All evictions are stored.
227 EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
228 // Reinstate cookies at t = [22,23,24,25,26,27].
229 for (int i = 0; i < 6; ++i) {
230 StepTime(1);
231 Add(cookies_[i]);
232 }
233 EXPECT_EQ(0, cookie_counter_->GetStorageSize()); // Everything is removed.
234 // Expected reinstatement delays: [21,20,18,15,11,6].
235 EXPECT_EQ("21,20,11;18,15,6", google_stat_ + ";" + other_stat_);
236 }
237
238 // Removed cookies are ignored by EvictedDomainCookieCounter.
239 TEST_F(EvictedDomainCookieCounterTest, TestRemove) {
240 InitCounter(10, 4);
241 InitStockCookies();
242 // Add all cookies at (relative time) t = 0.
243 for (int i = 0; i < 6; ++i)
244 Add(cookies_[i]);
245 // Remove cookies at t = [1,3,6,10,15,21].
246 for (int i = 0; i < 6; ++i) {
247 StepTime(i + 1);
248 Remove(cookies_[i]);
249 }
250 EXPECT_EQ(0, cookie_counter_->GetStorageSize());
251 // Add cookies again at t = [22,23,24,25,26,27].
252 for (int i = 0; i < 5; ++i) {
253 StepTime(1);
254 Add(cookies_[i]);
255 }
256 EXPECT_EQ(0, cookie_counter_->GetStorageSize());
257 // No cookies were evicted, so no reinstatement take place.
258 EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
259 }
260
261 // Expired cookies should not be counted by EvictedDomainCookieCounter.
262 TEST_F(EvictedDomainCookieCounterTest, TestExpired) {
263 InitCounter(10, 4);
264 InitStockCookies();
265 // Add all cookies at (relative time) t = 0.
266 for (int i = 0; i < 6; ++i)
267 Add(cookies_[i]);
268 // Evict cookies at t = [1,3,6,10,15,21].
269 for (int i = 0; i < 6; ++i) {
270 StepTime(i + 1);
271 Evict(cookies_[i]);
272 }
273 EXPECT_EQ(6, cookie_counter_->GetStorageSize());
274 GotoTime(1000); // t = 1000, so cookies_[2,4] expire.
275
276 // Reinstate cookies at t = [1000,1000,(1000),1000,(1000),1000].
277 InitStockCookies(); // Refresh cookies, so new cookies expire in the future.
278 for (int i = 0; i < 6; ++i)
279 Add(cookies_[i]);
280 EXPECT_EQ(0, cookie_counter_->GetStorageSize());
281 // Reinstatement delays: [999,997,(994),990,(985),979].
282 EXPECT_EQ("999,997;990,979", google_stat_ + ";" + other_stat_);
283 }
284
285 // Garbage collection should remove the oldest evicted cookies.
286 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollection) {
287 InitCounter(4, 2); // Reduced capacity.
288 InitStockCookies();
289 // Add all cookies at (relative time) t = 0.
290 for (int i = 0; i < 6; ++i)
291 Add(cookies_[i]);
292 // Evict cookies at t = [1,3,6,10].
293 for (int i = 0; i < 4; ++i) {
294 StepTime(i + 1);
295 Evict(cookies_[i]);
296 }
297 EXPECT_EQ(4, cookie_counter_->GetStorageSize()); // Reached capacity.
298 StepTime(5);
299 Evict(cookies_[4]); // Evict at t = 15, garbage collection takes place.
300 EXPECT_EQ(2, cookie_counter_->GetStorageSize());
301 StepTime(6);
302 Evict(cookies_[5]); // Evict at t = 21.
303 EXPECT_EQ(3, cookie_counter_->GetStorageSize());
304 EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
305 // Reinstate cookies at t = [(100),(100),(100),100,100,100].
306 GotoTime(100);
307 for (int i = 0; i < 6; ++i)
308 Add(cookies_[i]);
309 // Expected reinstatement delays: [(99),(97),(94),90,85,79]
310 EXPECT_EQ("85;90,79", google_stat_ + ";" + other_stat_);
311 }
312
313 // Garbage collection should remove the specified number of evicted cookies
314 // even when there are ties amongst oldest evicted cookies.
315 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionTie) {
316 InitCounter(9, 3);
317 // Add 10 cookies at time [0,1,3,6,...,45]
318 for (int i = 0; i < 10; ++i) {
319 StepTime(i);
320 CreateNewCookie(google_url1, "a" + base::IntToString(i) + "=1", 3000);
321 Add(cookies_[i]);
322 }
323 // Evict 6 cookies at t = [100,...,100].
324 GotoTime(100);
325 for (int i = 0; i < 6; ++i)
326 Evict(cookies_[i]);
327 EXPECT_EQ(6, cookie_counter_->GetStorageSize());
328 // Evict 3 cookies at t = [210,220,230].
329 GotoTime(200);
330 for (int i = 6; i < 9; ++i) {
331 StepTime(10);
332 Evict(cookies_[i]);
333 }
334 EXPECT_EQ(9, cookie_counter_->GetStorageSize()); // Reached capacity.
335 // Evict 1 cookie at t = 300, and garbage collection takes place.
336 GotoTime(300);
337 Evict(cookies_[9]);
338 // Some arbitrary 4 out of 6 cookies evicted at t = 100 are gone from storage.
339 EXPECT_EQ(6, cookie_counter_->GetStorageSize()); // 10 - 4.
340 // Reinstate cookies at t = [400,...,400].
341 GotoTime(400);
342 for (int i = 0; i < 10; ++i)
343 Add(cookies_[i]);
344 EXPECT_EQ(0, cookie_counter_->GetStorageSize());
345 // Expected reinstatement delays:
346 // [300,300,300,300,300,300 <= keeping 2 only,190,180,170,100].
347 EXPECT_EQ("300,300,190,180,170,100;", google_stat_ + ";" + other_stat_);
348 }
349
350 // Garbage collection prioritize removal of expired cookies.
351 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionWithExpiry) {
352 InitCounter(5, 1);
353 InitStockCookies();
354 // Add all cookies at (relative time) t = 0.
355 for (int i = 0; i < 6; ++i)
356 Add(cookies_[i]);
357 // Evict cookies at t = [1,3,6,10,15].
358 for (int i = 0; i < 5; ++i) {
359 StepTime(i + 1);
360 Evict(cookies_[i]);
361 }
362 EXPECT_EQ(5, cookie_counter_->GetStorageSize()); // Reached capacity.
363 GotoTime(1200); // t = 1200, so cookies_[2,3,4] expire.
364 // Evict cookies_[5] (not expired) at t = 1200.
365 Evict(cookies_[5]);
366 // Garbage collection would have taken place, removing 3 expired cookies,
367 // so that there's no need to remove more.
368 EXPECT_EQ(3, cookie_counter_->GetStorageSize());
369 // Reinstate cookies at t = [1500,1500,(1500),(1500),(1500),1500].
370 GotoTime(1500);
371 InitStockCookies(); // Refresh cookies, so new cookies expire in the future.
372 for (int i = 0; i < 6; ++i)
373 Add(cookies_[i]);
374 EXPECT_EQ(0, cookie_counter_->GetStorageSize());
375 // Reinstatement delays: [1499,1497,(1494),(1490),(1485),300].
376 EXPECT_EQ("1499,1497;300", google_stat_ + ";" + other_stat_);
377 }
378
379 } // namespace
380
381 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698