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/message_loop/message_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/time/time.h"
11 #include "net/cookies/cookie_constants.h"
12 #include "net/cookies/cookie_util.h"
13 #include "net/cookies/parsed_cookie.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback
,
19 std::vector
<CanonicalCookie
*> cookies
)
20 : loaded_callback_(loaded_callback
), cookies_(cookies
) {
23 LoadedCallbackTask::~LoadedCallbackTask() {
26 MockPersistentCookieStore::MockPersistentCookieStore()
27 : load_return_value_(true), loaded_(false) {
30 void MockPersistentCookieStore::SetLoadExpectation(
32 const std::vector
<CanonicalCookie
*>& result
) {
33 load_return_value_
= return_value
;
34 load_result_
= result
;
37 void MockPersistentCookieStore::Load(const LoadedCallback
& loaded_callback
) {
38 std::vector
<CanonicalCookie
*> out_cookies
;
39 if (load_return_value_
) {
40 out_cookies
= load_result_
;
43 base::MessageLoop::current()->PostTask(
45 base::Bind(&LoadedCallbackTask::Run
,
46 new LoadedCallbackTask(loaded_callback
, out_cookies
)));
49 void MockPersistentCookieStore::LoadCookiesForKey(
50 const std::string
& key
,
51 const LoadedCallback
& loaded_callback
) {
53 Load(loaded_callback
);
55 base::MessageLoop::current()->PostTask(
57 base::Bind(&LoadedCallbackTask::Run
,
58 new LoadedCallbackTask(loaded_callback
,
59 std::vector
<CanonicalCookie
*>())));
63 void MockPersistentCookieStore::AddCookie(const CanonicalCookie
& cookie
) {
64 commands_
.push_back(CookieStoreCommand(CookieStoreCommand::ADD
, cookie
));
67 void MockPersistentCookieStore::UpdateCookieAccessTime(
68 const CanonicalCookie
& cookie
) {
70 CookieStoreCommand(CookieStoreCommand::UPDATE_ACCESS_TIME
, cookie
));
73 void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie
& cookie
) {
74 commands_
.push_back(CookieStoreCommand(CookieStoreCommand::REMOVE
, cookie
));
77 void MockPersistentCookieStore::Flush(const base::Closure
& callback
) {
78 if (!callback
.is_null())
79 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
82 void MockPersistentCookieStore::SetForceKeepSessionState() {
85 MockPersistentCookieStore::~MockPersistentCookieStore() {
88 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {
91 void MockCookieMonsterDelegate::OnCookieChanged(
92 const CanonicalCookie
& cookie
,
94 CookieMonster::Delegate::ChangeCause cause
) {
95 CookieNotification
notification(cookie
, removed
);
96 changes_
.push_back(notification
);
99 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {
102 CanonicalCookie
BuildCanonicalCookie(const std::string
& key
,
103 const std::string
& cookie_line
,
104 const base::Time
& creation_time
) {
105 // Parse the cookie line.
106 ParsedCookie
pc(cookie_line
);
107 EXPECT_TRUE(pc
.IsValid());
109 // This helper is simplistic in interpreting a parsed cookie, in order to
110 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration()
111 // functions. Would be nice to export them, and re-use here.
112 EXPECT_FALSE(pc
.HasMaxAge());
113 EXPECT_TRUE(pc
.HasPath());
114 base::Time cookie_expires
= pc
.HasExpires()
115 ? cookie_util::ParseCookieTime(pc
.Expires())
117 std::string cookie_path
= pc
.Path();
119 return CanonicalCookie(GURL(), pc
.Name(), pc
.Value(), key
, cookie_path
,
120 creation_time
, cookie_expires
, creation_time
,
121 pc
.IsSecure(), pc
.IsHttpOnly(), pc
.IsFirstPartyOnly(),
125 void AddCookieToList(const std::string
& key
,
126 const std::string
& cookie_line
,
127 const base::Time
& creation_time
,
128 std::vector
<CanonicalCookie
*>* out_list
) {
129 scoped_ptr
<CanonicalCookie
> cookie(new CanonicalCookie(
130 BuildCanonicalCookie(key
, cookie_line
, creation_time
)));
132 out_list
->push_back(cookie
.release());
135 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore()
139 void MockSimplePersistentCookieStore::Load(
140 const LoadedCallback
& loaded_callback
) {
141 std::vector
<CanonicalCookie
*> out_cookies
;
143 for (CanonicalCookieMap::const_iterator it
= cookies_
.begin();
144 it
!= cookies_
.end(); it
++)
145 out_cookies
.push_back(new CanonicalCookie(it
->second
));
147 base::MessageLoop::current()->PostTask(
149 base::Bind(&LoadedCallbackTask::Run
,
150 new LoadedCallbackTask(loaded_callback
, out_cookies
)));
154 void MockSimplePersistentCookieStore::LoadCookiesForKey(
155 const std::string
& key
,
156 const LoadedCallback
& loaded_callback
) {
158 Load(loaded_callback
);
160 base::MessageLoop::current()->PostTask(
162 base::Bind(&LoadedCallbackTask::Run
,
163 new LoadedCallbackTask(loaded_callback
,
164 std::vector
<CanonicalCookie
*>())));
168 void MockSimplePersistentCookieStore::AddCookie(const CanonicalCookie
& cookie
) {
169 int64 creation_time
= cookie
.CreationDate().ToInternalValue();
170 EXPECT_TRUE(cookies_
.find(creation_time
) == cookies_
.end());
171 cookies_
[creation_time
] = cookie
;
174 void MockSimplePersistentCookieStore::UpdateCookieAccessTime(
175 const CanonicalCookie
& cookie
) {
176 int64 creation_time
= cookie
.CreationDate().ToInternalValue();
177 ASSERT_TRUE(cookies_
.find(creation_time
) != cookies_
.end());
178 cookies_
[creation_time
].SetLastAccessDate(base::Time::Now());
181 void MockSimplePersistentCookieStore::DeleteCookie(
182 const CanonicalCookie
& cookie
) {
183 int64 creation_time
= cookie
.CreationDate().ToInternalValue();
184 CanonicalCookieMap::iterator it
= cookies_
.find(creation_time
);
185 ASSERT_TRUE(it
!= cookies_
.end());
189 void MockSimplePersistentCookieStore::Flush(const base::Closure
& callback
) {
190 if (!callback
.is_null())
191 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
194 void MockSimplePersistentCookieStore::SetForceKeepSessionState() {
197 CookieMonster
* CreateMonsterFromStoreForGC(int num_cookies
,
200 base::Time
current(base::Time::Now());
201 base::Time
past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000));
202 scoped_refptr
<MockSimplePersistentCookieStore
> store(
203 new MockSimplePersistentCookieStore
);
204 // Must expire to be persistent
205 for (int i
= 0; i
< num_cookies
; i
++) {
206 base::Time creation_time
=
207 past_creation
+ base::TimeDelta::FromMicroseconds(i
);
208 base::Time expiration_time
= current
+ base::TimeDelta::FromDays(30);
209 base::Time last_access_time
=
210 (i
< num_old_cookies
) ? current
- base::TimeDelta::FromDays(days_old
)
213 CanonicalCookie
cc(GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i
),
214 "/path", creation_time
, expiration_time
,
215 last_access_time
, false, false, false,
216 COOKIE_PRIORITY_DEFAULT
);
217 store
->AddCookie(cc
);
220 return new CookieMonster(store
.get(), NULL
);
223 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {