OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "net/base/cookie_monster_store_test.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/stringprintf.h" | |
10 #include "base/time.h" | |
11 #include "googleurl/src/gurl.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace net { | |
15 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback, | |
16 std::vector<CookieMonster::CanonicalCookie*> cookies) | |
17 : loaded_callback_(loaded_callback), | |
18 cookies_(cookies) { | |
19 } | |
20 | |
21 LoadedCallbackTask::~LoadedCallbackTask() {} | |
22 | |
23 MockPersistentCookieStore::MockPersistentCookieStore() | |
24 : load_return_value_(true), | |
25 loaded_(false) { | |
26 } | |
27 | |
28 MockPersistentCookieStore::~MockPersistentCookieStore() {} | |
29 | |
30 void MockPersistentCookieStore::SetLoadExpectation( | |
31 bool return_value, | |
32 const std::vector<CookieMonster::CanonicalCookie*>& result) { | |
33 load_return_value_ = return_value; | |
34 load_result_ = result; | |
35 } | |
36 | |
37 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | |
38 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | |
39 if (load_return_value_) { | |
40 out_cookies = load_result_; | |
41 loaded_ = true; | |
42 } | |
43 MessageLoop::current()->PostTask(FROM_HERE, | |
44 base::Bind(&LoadedCallbackTask::Run, | |
45 new LoadedCallbackTask(loaded_callback, out_cookies))); | |
46 } | |
47 | |
48 void MockPersistentCookieStore::LoadCookiesForKey(const std::string& key, | |
49 const LoadedCallback& loaded_callback) { | |
50 if (!loaded_) { | |
51 Load(loaded_callback); | |
52 } else { | |
53 MessageLoop::current()->PostTask(FROM_HERE, | |
54 base::Bind(&LoadedCallbackTask::Run, | |
55 new LoadedCallbackTask(loaded_callback, | |
56 std::vector<CookieMonster::CanonicalCookie*>()))); | |
57 } | |
58 } | |
59 | |
60 void MockPersistentCookieStore::AddCookie( | |
61 const CookieMonster::CanonicalCookie& cookie) { | |
62 commands_.push_back( | |
63 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); | |
64 } | |
65 | |
66 void MockPersistentCookieStore::UpdateCookieAccessTime( | |
67 const CookieMonster::CanonicalCookie& cookie) { | |
68 commands_.push_back(CookieStoreCommand( | |
69 CookieStoreCommand::UPDATE_ACCESS_TIME, cookie)); | |
70 } | |
71 | |
72 void MockPersistentCookieStore::DeleteCookie( | |
73 const CookieMonster::CanonicalCookie& cookie) { | |
74 commands_.push_back( | |
75 CookieStoreCommand(CookieStoreCommand::REMOVE, cookie)); | |
76 } | |
77 | |
78 void MockPersistentCookieStore::Flush(const base::Closure& callback) { | |
79 if (!callback.is_null()) | |
80 MessageLoop::current()->PostTask(FROM_HERE, callback); | |
81 } | |
82 | |
83 // No files are created so nothing to clear either | |
84 void | |
85 MockPersistentCookieStore::SetClearLocalStateOnExit(bool clear_local_state) { | |
86 } | |
87 | |
88 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {} | |
89 | |
90 void MockCookieMonsterDelegate::OnCookieChanged( | |
91 const CookieMonster::CanonicalCookie& cookie, | |
92 bool removed, | |
93 CookieMonster::Delegate::ChangeCause cause) { | |
94 CookieNotification notification(cookie, removed); | |
95 changes_.push_back(notification); | |
96 } | |
97 | |
98 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {} | |
99 | |
100 void AddCookieToList( | |
101 const std::string& key, | |
102 const std::string& cookie_line, | |
103 const base::Time& creation_time, | |
104 std::vector<CookieMonster::CanonicalCookie*>* out_list) { | |
105 scoped_ptr<CookieMonster::CanonicalCookie> cookie( | |
106 new CookieMonster::CanonicalCookie( | |
107 BuildCanonicalCookie(key, cookie_line, creation_time))); | |
108 | |
109 out_list->push_back(cookie.release()); | |
110 } | |
111 | |
112 CookieMonster::CanonicalCookie BuildCanonicalCookie( | |
113 const std::string& key, | |
114 const std::string& cookie_line, | |
115 const base::Time& creation_time) { | |
116 | |
117 // Parse the cookie line. | |
118 CookieMonster::ParsedCookie pc(cookie_line); | |
119 EXPECT_TRUE(pc.IsValid()); | |
120 | |
121 // This helper is simplistic in interpreting a parsed cookie, in order to | |
122 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration() | |
123 // functions. Would be nice to export them, and re-use here. | |
124 EXPECT_FALSE(pc.HasMaxAge()); | |
125 EXPECT_TRUE(pc.HasPath()); | |
126 base::Time cookie_expires = pc.HasExpires() ? | |
127 CookieMonster::ParseCookieTime(pc.Expires()) : base::Time(); | |
128 std::string cookie_path = pc.Path(); | |
129 | |
130 return CookieMonster::CanonicalCookie( | |
131 GURL(), pc.Name(), pc.Value(), key, cookie_path, | |
132 pc.MACKey(), pc.MACAlgorithm(), | |
133 creation_time, creation_time, cookie_expires, | |
134 pc.IsSecure(), pc.IsHttpOnly(), | |
135 !cookie_expires.is_null(), !cookie_expires.is_null()); | |
136 } | |
137 | |
138 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() | |
139 : loaded_(false) {} | |
140 | |
141 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} | |
142 | |
143 void MockSimplePersistentCookieStore::Load( | |
144 const LoadedCallback& loaded_callback) { | |
145 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | |
146 | |
147 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); | |
148 it != cookies_.end(); it++) | |
149 out_cookies.push_back( | |
150 new CookieMonster::CanonicalCookie(it->second)); | |
151 | |
152 MessageLoop::current()->PostTask(FROM_HERE, | |
153 base::Bind(&LoadedCallbackTask::Run, | |
154 new LoadedCallbackTask(loaded_callback, out_cookies))); | |
155 loaded_ = true; | |
156 } | |
157 | |
158 void MockSimplePersistentCookieStore::LoadCookiesForKey(const std::string& key, | |
159 const LoadedCallback& loaded_callback) { | |
160 if (!loaded_) { | |
161 Load(loaded_callback); | |
162 } else { | |
163 MessageLoop::current()->PostTask(FROM_HERE, | |
164 base::Bind(&LoadedCallbackTask::Run, | |
165 new LoadedCallbackTask(loaded_callback, | |
166 std::vector<CookieMonster::CanonicalCookie*>()))); | |
167 } | |
168 } | |
169 | |
170 void MockSimplePersistentCookieStore::AddCookie( | |
171 const CookieMonster::CanonicalCookie& cookie) { | |
172 int64 creation_time = cookie.CreationDate().ToInternalValue(); | |
173 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); | |
174 cookies_[creation_time] = cookie; | |
175 } | |
176 | |
177 void MockSimplePersistentCookieStore::UpdateCookieAccessTime( | |
178 const CookieMonster::CanonicalCookie& cookie) { | |
179 int64 creation_time = cookie.CreationDate().ToInternalValue(); | |
180 ASSERT_TRUE(cookies_.find(creation_time) != cookies_.end()); | |
181 cookies_[creation_time].SetLastAccessDate(base::Time::Now()); | |
182 } | |
183 | |
184 void MockSimplePersistentCookieStore::DeleteCookie( | |
185 const CookieMonster::CanonicalCookie& cookie) { | |
186 int64 creation_time = cookie.CreationDate().ToInternalValue(); | |
187 CanonicalCookieMap::iterator it = cookies_.find(creation_time); | |
188 ASSERT_TRUE(it != cookies_.end()); | |
189 cookies_.erase(it); | |
190 } | |
191 | |
192 void MockSimplePersistentCookieStore::Flush(const base::Closure& callback) { | |
193 if (!callback.is_null()) | |
194 MessageLoop::current()->PostTask(FROM_HERE, callback); | |
195 } | |
196 | |
197 void MockSimplePersistentCookieStore::SetClearLocalStateOnExit( | |
198 bool clear_local_state) { | |
199 } | |
200 | |
201 CookieMonster* CreateMonsterFromStoreForGC( | |
202 int num_cookies, | |
203 int num_old_cookies, | |
204 int days_old) { | |
205 base::Time current(base::Time::Now()); | |
206 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000)); | |
207 scoped_refptr<MockSimplePersistentCookieStore> store( | |
208 new MockSimplePersistentCookieStore); | |
209 // Must expire to be persistent | |
210 for (int i = 0; i < num_cookies; i++) { | |
211 base::Time creation_time = | |
212 past_creation + base::TimeDelta::FromMicroseconds(i); | |
213 base::Time expiration_time = current + base::TimeDelta::FromDays(30); | |
214 base::Time last_access_time = | |
215 (i < num_old_cookies) ? current - base::TimeDelta::FromDays(days_old) : | |
216 current; | |
217 | |
218 std::string mac_key; | |
219 std::string mac_algorithm; | |
220 | |
221 CookieMonster::CanonicalCookie cc( | |
222 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", | |
223 mac_key, mac_algorithm, creation_time, expiration_time, | |
224 last_access_time, false, false, true, true); | |
225 store->AddCookie(cc); | |
226 } | |
227 | |
228 return new CookieMonster(store, NULL); | |
229 } | |
230 | |
231 } // namespace net | |
OLD | NEW |