1 // Copyright (c) 2012 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 "net/cookies/cookie_monster_store_test.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/time/time.h"
13 #include "net/cookies/cookie_constants.h"
14 #include "net/cookies/cookie_util.h"
15 #include "net/cookies/parsed_cookie.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback
,
21 std::vector
<CanonicalCookie
*> cookies
)
22 : loaded_callback_(loaded_callback
), cookies_(cookies
) {
25 LoadedCallbackTask::~LoadedCallbackTask() {
28 MockPersistentCookieStore::MockPersistentCookieStore()
29 : load_return_value_(true), loaded_(false) {
32 void MockPersistentCookieStore::SetLoadExpectation(
34 const std::vector
<CanonicalCookie
*>& result
) {
35 load_return_value_
= return_value
;
36 load_result_
= result
;
39 void MockPersistentCookieStore::Load(const LoadedCallback
& loaded_callback
) {
40 std::vector
<CanonicalCookie
*> out_cookies
;
41 if (load_return_value_
) {
42 out_cookies
= load_result_
;
45 base::ThreadTaskRunnerHandle::Get()->PostTask(
47 base::Bind(&LoadedCallbackTask::Run
,
48 new LoadedCallbackTask(loaded_callback
, out_cookies
)));
51 void MockPersistentCookieStore::LoadCookiesForKey(
52 const std::string
& key
,
53 const LoadedCallback
& loaded_callback
) {
55 Load(loaded_callback
);
57 base::ThreadTaskRunnerHandle::Get()->PostTask(
59 base::Bind(&LoadedCallbackTask::Run
,
60 new LoadedCallbackTask(loaded_callback
,
61 std::vector
<CanonicalCookie
*>())));
65 void MockPersistentCookieStore::AddCookie(const CanonicalCookie
& cookie
) {
66 commands_
.push_back(CookieStoreCommand(CookieStoreCommand::ADD
, cookie
));
69 void MockPersistentCookieStore::UpdateCookieAccessTime(
70 const CanonicalCookie
& cookie
) {
72 CookieStoreCommand(CookieStoreCommand::UPDATE_ACCESS_TIME
, cookie
));
75 void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie
& cookie
) {
76 commands_
.push_back(CookieStoreCommand(CookieStoreCommand::REMOVE
, cookie
));
79 void MockPersistentCookieStore::Flush(const base::Closure
& callback
) {
80 if (!callback
.is_null())
81 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, callback
);
84 void MockPersistentCookieStore::SetForceKeepSessionState() {
87 MockPersistentCookieStore::~MockPersistentCookieStore() {
90 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {
93 void MockCookieMonsterDelegate::OnCookieChanged(
94 const CanonicalCookie
& cookie
,
96 CookieMonster::Delegate::ChangeCause cause
) {
97 CookieNotification
notification(cookie
, removed
);
98 changes_
.push_back(notification
);
101 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {
104 CanonicalCookie
BuildCanonicalCookie(const std::string
& key
,
105 const std::string
& cookie_line
,
106 const base::Time
& creation_time
) {
107 // Parse the cookie line.
108 ParsedCookie
pc(cookie_line
);
109 EXPECT_TRUE(pc
.IsValid());
111 // This helper is simplistic in interpreting a parsed cookie, in order to
112 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration()
113 // functions. Would be nice to export them, and re-use here.
114 EXPECT_FALSE(pc
.HasMaxAge());
115 EXPECT_TRUE(pc
.HasPath());
116 base::Time cookie_expires
= pc
.HasExpires()
117 ? cookie_util::ParseCookieTime(pc
.Expires())
119 std::string cookie_path
= pc
.Path();
121 return CanonicalCookie(GURL(), pc
.Name(), pc
.Value(), key
, cookie_path
,
122 creation_time
, cookie_expires
, creation_time
,
123 pc
.IsSecure(), pc
.IsHttpOnly(), pc
.IsFirstPartyOnly(),
127 void AddCookieToList(const std::string
& key
,
128 const std::string
& cookie_line
,
129 const base::Time
& creation_time
,
130 std::vector
<CanonicalCookie
*>* out_list
) {
131 scoped_ptr
<CanonicalCookie
> cookie(new CanonicalCookie(
132 BuildCanonicalCookie(key
, cookie_line
, creation_time
)));
134 out_list
->push_back(cookie
.release());
137 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore()
141 void MockSimplePersistentCookieStore::Load(
142 const LoadedCallback
& loaded_callback
) {
143 std::vector
<CanonicalCookie
*> out_cookies
;
145 for (CanonicalCookieMap::const_iterator it
= cookies_
.begin();
146 it
!= cookies_
.end(); it
++)
147 out_cookies
.push_back(new CanonicalCookie(it
->second
));
149 base::ThreadTaskRunnerHandle::Get()->PostTask(
151 base::Bind(&LoadedCallbackTask::Run
,
152 new LoadedCallbackTask(loaded_callback
, out_cookies
)));
156 void MockSimplePersistentCookieStore::LoadCookiesForKey(
157 const std::string
& key
,
158 const LoadedCallback
& loaded_callback
) {
160 Load(loaded_callback
);
162 base::ThreadTaskRunnerHandle::Get()->PostTask(
164 base::Bind(&LoadedCallbackTask::Run
,
165 new LoadedCallbackTask(loaded_callback
,
166 std::vector
<CanonicalCookie
*>())));
170 void MockSimplePersistentCookieStore::AddCookie(const CanonicalCookie
& cookie
) {
171 int64 creation_time
= cookie
.CreationDate().ToInternalValue();
172 EXPECT_TRUE(cookies_
.find(creation_time
) == cookies_
.end());
173 cookies_
[creation_time
] = cookie
;
176 void MockSimplePersistentCookieStore::UpdateCookieAccessTime(
177 const CanonicalCookie
& cookie
) {
178 int64 creation_time
= cookie
.CreationDate().ToInternalValue();
179 ASSERT_TRUE(cookies_
.find(creation_time
) != cookies_
.end());
180 cookies_
[creation_time
].SetLastAccessDate(base::Time::Now());
183 void MockSimplePersistentCookieStore::DeleteCookie(
184 const CanonicalCookie
& cookie
) {
185 int64 creation_time
= cookie
.CreationDate().ToInternalValue();
186 CanonicalCookieMap::iterator it
= cookies_
.find(creation_time
);
187 ASSERT_TRUE(it
!= cookies_
.end());
191 void MockSimplePersistentCookieStore::Flush(const base::Closure
& callback
) {
192 if (!callback
.is_null())
193 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, callback
);
196 void MockSimplePersistentCookieStore::SetForceKeepSessionState() {
199 CookieMonster
* CreateMonsterFromStoreForGC(int num_cookies
,
202 base::Time
current(base::Time::Now());
203 base::Time
past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000));
204 scoped_refptr
<MockSimplePersistentCookieStore
> store(
205 new MockSimplePersistentCookieStore
);
206 // Must expire to be persistent
207 for (int i
= 0; i
< num_cookies
; i
++) {
208 base::Time creation_time
=
209 past_creation
+ base::TimeDelta::FromMicroseconds(i
);
210 base::Time expiration_time
= current
+ base::TimeDelta::FromDays(30);
211 base::Time last_access_time
=
212 (i
< num_old_cookies
) ? current
- base::TimeDelta::FromDays(days_old
)
215 CanonicalCookie
cc(GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i
),
216 "/path", creation_time
, expiration_time
,
217 last_access_time
, false, false, false,
218 COOKIE_PRIORITY_DEFAULT
);
219 store
->AddCookie(cc
);
222 return new CookieMonster(store
.get(), NULL
);
225 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {