Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / net / evicted_domain_cookie_counter_unittest.cc
blob5eb654552051ca21e41f055cb66beb5d16e2b6cc
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.
5 #include <string>
6 #include <vector>
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"
19 namespace chrome_browser_net {
21 using base::Time;
22 using base::TimeDelta;
24 namespace {
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 {
32 protected:
33 class MockDelegate : public EvictedDomainCookieCounter::Delegate {
34 public:
35 explicit MockDelegate(EvictedDomainCookieCounterTest* tester);
37 // EvictedDomainCookieCounter::Delegate implementation.
38 void Report(const EvictedDomainCookieCounter::EvictedCookie& evicted_cookie,
39 const Time& reinstatement_time) override;
40 Time CurrentTime() const override;
42 private:
43 EvictedDomainCookieCounterTest* tester_;
46 EvictedDomainCookieCounterTest();
47 ~EvictedDomainCookieCounterTest() override;
49 // testing::Test implementation.
50 void SetUp() override;
51 void TearDown() override;
53 // Initialization that allows parameters to be specified.
54 void InitCounter(size_t max_size, size_t purge_count);
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);
61 // Clears |cookies_| and creates common cookies for multiple tests.
62 void InitStockCookies();
64 // Sets simulation time to |rel_time|.
65 void GotoTime(int64 rel_time);
67 // Simulates time-passage by |delta_second|.
68 void StepTime(int64 delta_second);
70 // Simulates cookie addition or update.
71 void Add(net::CanonicalCookie* cookie);
73 // Simulates cookie removal.
74 void Remove(net::CanonicalCookie* cookie);
76 // Simulates cookie eviction.
77 void Evict(net::CanonicalCookie* cookie);
79 // For semi-realism, time considered are relative to |mock_time_base_|.
80 Time mock_time_base_;
81 Time mock_time_;
83 // To store allocated cookies for reuse.
84 ScopedVector<net::CanonicalCookie> cookies_;
86 scoped_refptr<EvictedDomainCookieCounter> cookie_counter_;
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_;
94 EvictedDomainCookieCounterTest::MockDelegate::MockDelegate(
95 EvictedDomainCookieCounterTest* tester)
96 : tester_(tester) {}
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()));
109 Time EvictedDomainCookieCounterTest::MockDelegate::CurrentTime() const {
110 return tester_->mock_time_;
113 EvictedDomainCookieCounterTest::EvictedDomainCookieCounterTest() {}
115 EvictedDomainCookieCounterTest::~EvictedDomainCookieCounterTest() {}
117 void EvictedDomainCookieCounterTest::SetUp() {
118 mock_time_base_ = Time::Now() - TimeDelta::FromHours(1);
119 mock_time_ = mock_time_base_;
122 void EvictedDomainCookieCounterTest::TearDown() {
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);
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);
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].
153 void EvictedDomainCookieCounterTest::GotoTime(int64 rel_time) {
154 mock_time_ = mock_time_base_ + TimeDelta::FromSeconds(rel_time);
157 void EvictedDomainCookieCounterTest::StepTime(int64 delta_second) {
158 mock_time_ += TimeDelta::FromSeconds(delta_second);
161 void EvictedDomainCookieCounterTest::Add(net::CanonicalCookie* cookie) {
162 cookie_counter_->OnCookieChanged(
163 *cookie, false, net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT);
166 void EvictedDomainCookieCounterTest::Remove(net::CanonicalCookie* cookie) {
167 cookie_counter_->OnCookieChanged(
168 *cookie, true, net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT);
171 void EvictedDomainCookieCounterTest::Evict(net::CanonicalCookie* cookie) {
172 cookie_counter_->OnCookieChanged(
173 *cookie, true, net::CookieMonster::Delegate::CHANGE_COOKIE_EVICTED);
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;
182 class ChangedDelegateDummy : public net::CookieMonster::Delegate {
183 public:
184 explicit ChangedDelegateDummy(int* result) : result_(result) {}
186 void OnCookieChanged(const net::CanonicalCookie& cookie,
187 bool removed,
188 ChangeCause cause) override {
189 ++(*result_);
192 private:
193 ~ChangedDelegateDummy() override {}
195 int* result_;
198 scoped_ptr<MockDelegate> cookie_counter_delegate(new MockDelegate(this));
199 cookie_counter_ = new EvictedDomainCookieCounter(
200 new ChangedDelegateDummy(&result), cookie_counter_delegate.Pass(), 10, 5);
201 InitStockCookies();
202 // Perform 6 cookie transactions.
203 for (int i = 0; i < 6; ++i) {
204 Add(cookies_[i]);
205 StepTime(1);
206 Evict(cookies_[i]);
207 StepTime(1);
208 Remove(cookies_[i]);
210 EXPECT_EQ(18, result); // 6 cookies x 3 operations each.
213 // Basic flow: add cookies, evict, then reinstate.
214 TEST_F(EvictedDomainCookieCounterTest, TestBasicFlow) {
215 InitCounter(10, 4);
216 InitStockCookies();
217 // Add all cookies at (relative time) t = 0.
218 for (int i = 0; i < 6; ++i)
219 Add(cookies_[i]);
220 EXPECT_EQ(0u, cookie_counter_->GetStorageSize()); // No activities on add.
221 EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
222 // Evict cookies at t = [1,3,6,10,15,21].
223 for (int i = 0; i < 6; ++i) {
224 StepTime(i + 1);
225 Evict(cookies_[i]);
227 EXPECT_EQ(6u, cookie_counter_->GetStorageSize()); // Storing all evictions.
228 EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
229 // Reinstate cookies at t = [22,23,24,25,26,27].
230 for (int i = 0; i < 6; ++i) {
231 StepTime(1);
232 Add(cookies_[i]);
234 EXPECT_EQ(0u, cookie_counter_->GetStorageSize()); // Everything is removed.
235 // Expected reinstatement delays: [21,20,18,15,11,6].
236 EXPECT_EQ("21,20,11;18,15,6", google_stat_ + ";" + other_stat_);
239 // Removed cookies are ignored by EvictedDomainCookieCounter.
240 TEST_F(EvictedDomainCookieCounterTest, TestRemove) {
241 InitCounter(10, 4);
242 InitStockCookies();
243 // Add all cookies at (relative time) t = 0.
244 for (int i = 0; i < 6; ++i)
245 Add(cookies_[i]);
246 // Remove cookies at t = [1,3,6,10,15,21].
247 for (int i = 0; i < 6; ++i) {
248 StepTime(i + 1);
249 Remove(cookies_[i]);
251 EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
252 // Add cookies again at t = [22,23,24,25,26,27].
253 for (int i = 0; i < 5; ++i) {
254 StepTime(1);
255 Add(cookies_[i]);
257 EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
258 // No cookies were evicted, so no reinstatement take place.
259 EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
262 // Expired cookies should not be counted by EvictedDomainCookieCounter.
263 TEST_F(EvictedDomainCookieCounterTest, TestExpired) {
264 InitCounter(10, 4);
265 InitStockCookies();
266 // Add all cookies at (relative time) t = 0.
267 for (int i = 0; i < 6; ++i)
268 Add(cookies_[i]);
269 // Evict cookies at t = [1,3,6,10,15,21].
270 for (int i = 0; i < 6; ++i) {
271 StepTime(i + 1);
272 Evict(cookies_[i]);
274 EXPECT_EQ(6u, cookie_counter_->GetStorageSize());
275 GotoTime(1000); // t = 1000, so cookies_[2,4] expire.
277 // Reinstate cookies at t = [1000,1000,(1000),1000,(1000),1000].
278 InitStockCookies(); // Refresh cookies, so new cookies expire in the future.
279 for (int i = 0; i < 6; ++i)
280 Add(cookies_[i]);
281 EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
282 // Reinstatement delays: [999,997,(994),990,(985),979].
283 EXPECT_EQ("999,997;990,979", google_stat_ + ";" + other_stat_);
286 // Garbage collection should remove the oldest evicted cookies.
287 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollection) {
288 InitCounter(4, 2); // Reduced capacity.
289 InitStockCookies();
290 // Add all cookies at (relative time) t = 0.
291 for (int i = 0; i < 6; ++i)
292 Add(cookies_[i]);
293 // Evict cookies at t = [1,3,6,10].
294 for (int i = 0; i < 4; ++i) {
295 StepTime(i + 1);
296 Evict(cookies_[i]);
298 EXPECT_EQ(4u, cookie_counter_->GetStorageSize()); // Reached capacity.
299 StepTime(5);
300 Evict(cookies_[4]); // Evict at t = 15, garbage collection takes place.
301 EXPECT_EQ(2u, cookie_counter_->GetStorageSize());
302 StepTime(6);
303 Evict(cookies_[5]); // Evict at t = 21.
304 EXPECT_EQ(3u, cookie_counter_->GetStorageSize());
305 EXPECT_EQ(";", google_stat_ + ";" + other_stat_);
306 // Reinstate cookies at t = [(100),(100),(100),100,100,100].
307 GotoTime(100);
308 for (int i = 0; i < 6; ++i)
309 Add(cookies_[i]);
310 // Expected reinstatement delays: [(99),(97),(94),90,85,79]
311 EXPECT_EQ("85;90,79", google_stat_ + ";" + other_stat_);
314 // Garbage collection should remove the specified number of evicted cookies
315 // even when there are ties amongst oldest evicted cookies.
316 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionTie) {
317 InitCounter(9, 3);
318 // Add 10 cookies at time [0,1,3,6,...,45]
319 for (int i = 0; i < 10; ++i) {
320 StepTime(i);
321 CreateNewCookie(google_url1, "a" + base::IntToString(i) + "=1", 3000);
322 Add(cookies_[i]);
324 // Evict 6 cookies at t = [100,...,100].
325 GotoTime(100);
326 for (int i = 0; i < 6; ++i)
327 Evict(cookies_[i]);
328 EXPECT_EQ(6u, cookie_counter_->GetStorageSize());
329 // Evict 3 cookies at t = [210,220,230].
330 GotoTime(200);
331 for (int i = 6; i < 9; ++i) {
332 StepTime(10);
333 Evict(cookies_[i]);
335 EXPECT_EQ(9u, cookie_counter_->GetStorageSize()); // Reached capacity.
336 // Evict 1 cookie at t = 300, and garbage collection takes place.
337 GotoTime(300);
338 Evict(cookies_[9]);
339 // Some arbitrary 4 out of 6 cookies evicted at t = 100 are gone from storage.
340 EXPECT_EQ(6u, cookie_counter_->GetStorageSize()); // 10 - 4.
341 // Reinstate cookies at t = [400,...,400].
342 GotoTime(400);
343 for (int i = 0; i < 10; ++i)
344 Add(cookies_[i]);
345 EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
346 // Expected reinstatement delays:
347 // [300,300,300,300,300,300 <= keeping 2 only,190,180,170,100].
348 EXPECT_EQ("300,300,190,180,170,100;", google_stat_ + ";" + other_stat_);
351 // Garbage collection prioritize removal of expired cookies.
352 TEST_F(EvictedDomainCookieCounterTest, TestGarbageCollectionWithExpiry) {
353 InitCounter(5, 1);
354 InitStockCookies();
355 // Add all cookies at (relative time) t = 0.
356 for (int i = 0; i < 6; ++i)
357 Add(cookies_[i]);
358 // Evict cookies at t = [1,3,6,10,15].
359 for (int i = 0; i < 5; ++i) {
360 StepTime(i + 1);
361 Evict(cookies_[i]);
363 EXPECT_EQ(5u, cookie_counter_->GetStorageSize()); // Reached capacity.
364 GotoTime(1200); // t = 1200, so cookies_[2,3,4] expire.
365 // Evict cookies_[5] (not expired) at t = 1200.
366 Evict(cookies_[5]);
367 // Garbage collection would have taken place, removing 3 expired cookies,
368 // so that there's no need to remove more.
369 EXPECT_EQ(3u, cookie_counter_->GetStorageSize());
370 // Reinstate cookies at t = [1500,1500,(1500),(1500),(1500),1500].
371 GotoTime(1500);
372 InitStockCookies(); // Refresh cookies, so new cookies expire in the future.
373 for (int i = 0; i < 6; ++i)
374 Add(cookies_[i]);
375 EXPECT_EQ(0u, cookie_counter_->GetStorageSize());
376 // Reinstatement delays: [1499,1497,(1494),(1490),(1485),300].
377 EXPECT_EQ("1499,1497;300", google_stat_ + ";" + other_stat_);
380 } // namespace
382 } // namespace chrome_browser_net