OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/cookies/cookie_monster_store_test.h" | 5 #include "net/cookies/cookie_monster_store_test.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "net/cookies/cookie_constants.h" | 11 #include "net/cookies/cookie_constants.h" |
12 #include "net/cookies/cookie_util.h" | 12 #include "net/cookies/cookie_util.h" |
13 #include "net/cookies/parsed_cookie.h" | 13 #include "net/cookies/parsed_cookie.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback, | 18 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback, |
19 std::vector<CanonicalCookie*> cookies) | 19 std::vector<CanonicalCookie*> cookies) |
20 : loaded_callback_(loaded_callback), | 20 : loaded_callback_(loaded_callback), cookies_(cookies) { |
21 cookies_(cookies) { | |
22 } | 21 } |
23 | 22 |
24 LoadedCallbackTask::~LoadedCallbackTask() {} | 23 LoadedCallbackTask::~LoadedCallbackTask() { |
| 24 } |
25 | 25 |
26 MockPersistentCookieStore::MockPersistentCookieStore() | 26 MockPersistentCookieStore::MockPersistentCookieStore() |
27 : load_return_value_(true), | 27 : load_return_value_(true), loaded_(false) { |
28 loaded_(false) { | |
29 } | 28 } |
30 | 29 |
31 void MockPersistentCookieStore::SetLoadExpectation( | 30 void MockPersistentCookieStore::SetLoadExpectation( |
32 bool return_value, | 31 bool return_value, |
33 const std::vector<CanonicalCookie*>& result) { | 32 const std::vector<CanonicalCookie*>& result) { |
34 load_return_value_ = return_value; | 33 load_return_value_ = return_value; |
35 load_result_ = result; | 34 load_result_ = result; |
36 } | 35 } |
37 | 36 |
38 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 37 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
(...skipping 16 matching lines...) Expand all Loading... |
55 } else { | 54 } else { |
56 base::MessageLoop::current()->PostTask( | 55 base::MessageLoop::current()->PostTask( |
57 FROM_HERE, | 56 FROM_HERE, |
58 base::Bind(&LoadedCallbackTask::Run, | 57 base::Bind(&LoadedCallbackTask::Run, |
59 new LoadedCallbackTask(loaded_callback, | 58 new LoadedCallbackTask(loaded_callback, |
60 std::vector<CanonicalCookie*>()))); | 59 std::vector<CanonicalCookie*>()))); |
61 } | 60 } |
62 } | 61 } |
63 | 62 |
64 void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) { | 63 void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) { |
65 commands_.push_back( | 64 commands_.push_back(CookieStoreCommand(CookieStoreCommand::ADD, cookie)); |
66 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); | |
67 } | 65 } |
68 | 66 |
69 void MockPersistentCookieStore::UpdateCookieAccessTime( | 67 void MockPersistentCookieStore::UpdateCookieAccessTime( |
70 const CanonicalCookie& cookie) { | 68 const CanonicalCookie& cookie) { |
71 commands_.push_back(CookieStoreCommand( | 69 commands_.push_back( |
72 CookieStoreCommand::UPDATE_ACCESS_TIME, cookie)); | 70 CookieStoreCommand(CookieStoreCommand::UPDATE_ACCESS_TIME, cookie)); |
73 } | 71 } |
74 | 72 |
75 void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie& cookie) { | 73 void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie& cookie) { |
76 commands_.push_back( | 74 commands_.push_back(CookieStoreCommand(CookieStoreCommand::REMOVE, cookie)); |
77 CookieStoreCommand(CookieStoreCommand::REMOVE, cookie)); | |
78 } | 75 } |
79 | 76 |
80 void MockPersistentCookieStore::Flush(const base::Closure& callback) { | 77 void MockPersistentCookieStore::Flush(const base::Closure& callback) { |
81 if (!callback.is_null()) | 78 if (!callback.is_null()) |
82 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | 79 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
83 } | 80 } |
84 | 81 |
85 void MockPersistentCookieStore::SetForceKeepSessionState() { | 82 void MockPersistentCookieStore::SetForceKeepSessionState() { |
86 } | 83 } |
87 | 84 |
88 MockPersistentCookieStore::~MockPersistentCookieStore() {} | 85 MockPersistentCookieStore::~MockPersistentCookieStore() { |
| 86 } |
89 | 87 |
90 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {} | 88 MockCookieMonsterDelegate::MockCookieMonsterDelegate() { |
| 89 } |
91 | 90 |
92 void MockCookieMonsterDelegate::OnCookieChanged( | 91 void MockCookieMonsterDelegate::OnCookieChanged( |
93 const CanonicalCookie& cookie, | 92 const CanonicalCookie& cookie, |
94 bool removed, | 93 bool removed, |
95 CookieMonster::Delegate::ChangeCause cause) { | 94 CookieMonster::Delegate::ChangeCause cause) { |
96 CookieNotification notification(cookie, removed); | 95 CookieNotification notification(cookie, removed); |
97 changes_.push_back(notification); | 96 changes_.push_back(notification); |
98 } | 97 } |
99 | 98 |
100 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {} | 99 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() { |
| 100 } |
101 | 101 |
102 CanonicalCookie BuildCanonicalCookie(const std::string& key, | 102 CanonicalCookie BuildCanonicalCookie(const std::string& key, |
103 const std::string& cookie_line, | 103 const std::string& cookie_line, |
104 const base::Time& creation_time) { | 104 const base::Time& creation_time) { |
105 | |
106 // Parse the cookie line. | 105 // Parse the cookie line. |
107 ParsedCookie pc(cookie_line); | 106 ParsedCookie pc(cookie_line); |
108 EXPECT_TRUE(pc.IsValid()); | 107 EXPECT_TRUE(pc.IsValid()); |
109 | 108 |
110 // This helper is simplistic in interpreting a parsed cookie, in order to | 109 // This helper is simplistic in interpreting a parsed cookie, in order to |
111 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration() | 110 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration() |
112 // functions. Would be nice to export them, and re-use here. | 111 // functions. Would be nice to export them, and re-use here. |
113 EXPECT_FALSE(pc.HasMaxAge()); | 112 EXPECT_FALSE(pc.HasMaxAge()); |
114 EXPECT_TRUE(pc.HasPath()); | 113 EXPECT_TRUE(pc.HasPath()); |
115 base::Time cookie_expires = pc.HasExpires() ? | 114 base::Time cookie_expires = pc.HasExpires() |
116 cookie_util::ParseCookieTime(pc.Expires()) : base::Time(); | 115 ? cookie_util::ParseCookieTime(pc.Expires()) |
| 116 : base::Time(); |
117 std::string cookie_path = pc.Path(); | 117 std::string cookie_path = pc.Path(); |
118 | 118 |
119 return CanonicalCookie( | 119 return CanonicalCookie(GURL(), |
120 GURL(), pc.Name(), pc.Value(), key, cookie_path, | 120 pc.Name(), |
121 creation_time, cookie_expires, creation_time, | 121 pc.Value(), |
122 pc.IsSecure(), pc.IsHttpOnly(), pc.Priority()); | 122 key, |
| 123 cookie_path, |
| 124 creation_time, |
| 125 cookie_expires, |
| 126 creation_time, |
| 127 pc.IsSecure(), |
| 128 pc.IsHttpOnly(), |
| 129 pc.Priority()); |
123 } | 130 } |
124 | 131 |
125 void AddCookieToList( | 132 void AddCookieToList(const std::string& key, |
126 const std::string& key, | 133 const std::string& cookie_line, |
127 const std::string& cookie_line, | 134 const base::Time& creation_time, |
128 const base::Time& creation_time, | 135 std::vector<CanonicalCookie*>* out_list) { |
129 std::vector<CanonicalCookie*>* out_list) { | 136 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie( |
130 scoped_ptr<CanonicalCookie> cookie( | 137 BuildCanonicalCookie(key, cookie_line, creation_time))); |
131 new CanonicalCookie( | |
132 BuildCanonicalCookie(key, cookie_line, creation_time))); | |
133 | 138 |
134 out_list->push_back(cookie.release()); | 139 out_list->push_back(cookie.release()); |
135 } | 140 } |
136 | 141 |
137 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() | 142 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() |
138 : loaded_(false) { | 143 : loaded_(false) { |
139 } | 144 } |
140 | 145 |
141 void MockSimplePersistentCookieStore::Load( | 146 void MockSimplePersistentCookieStore::Load( |
142 const LoadedCallback& loaded_callback) { | 147 const LoadedCallback& loaded_callback) { |
143 std::vector<CanonicalCookie*> out_cookies; | 148 std::vector<CanonicalCookie*> out_cookies; |
144 | 149 |
145 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); | 150 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); |
146 it != cookies_.end(); it++) | 151 it != cookies_.end(); |
| 152 it++) |
147 out_cookies.push_back(new CanonicalCookie(it->second)); | 153 out_cookies.push_back(new CanonicalCookie(it->second)); |
148 | 154 |
149 base::MessageLoop::current()->PostTask( | 155 base::MessageLoop::current()->PostTask( |
150 FROM_HERE, | 156 FROM_HERE, |
151 base::Bind(&LoadedCallbackTask::Run, | 157 base::Bind(&LoadedCallbackTask::Run, |
152 new LoadedCallbackTask(loaded_callback, out_cookies))); | 158 new LoadedCallbackTask(loaded_callback, out_cookies))); |
153 loaded_ = true; | 159 loaded_ = true; |
154 } | 160 } |
155 | 161 |
156 void MockSimplePersistentCookieStore::LoadCookiesForKey(const std::string& key, | 162 void MockSimplePersistentCookieStore::LoadCookiesForKey( |
| 163 const std::string& key, |
157 const LoadedCallback& loaded_callback) { | 164 const LoadedCallback& loaded_callback) { |
158 if (!loaded_) { | 165 if (!loaded_) { |
159 Load(loaded_callback); | 166 Load(loaded_callback); |
160 } else { | 167 } else { |
161 base::MessageLoop::current()->PostTask( | 168 base::MessageLoop::current()->PostTask( |
162 FROM_HERE, | 169 FROM_HERE, |
163 base::Bind(&LoadedCallbackTask::Run, | 170 base::Bind(&LoadedCallbackTask::Run, |
164 new LoadedCallbackTask(loaded_callback, | 171 new LoadedCallbackTask(loaded_callback, |
165 std::vector<CanonicalCookie*>()))); | 172 std::vector<CanonicalCookie*>()))); |
166 } | 173 } |
(...skipping 21 matching lines...) Expand all Loading... |
188 } | 195 } |
189 | 196 |
190 void MockSimplePersistentCookieStore::Flush(const base::Closure& callback) { | 197 void MockSimplePersistentCookieStore::Flush(const base::Closure& callback) { |
191 if (!callback.is_null()) | 198 if (!callback.is_null()) |
192 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | 199 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
193 } | 200 } |
194 | 201 |
195 void MockSimplePersistentCookieStore::SetForceKeepSessionState() { | 202 void MockSimplePersistentCookieStore::SetForceKeepSessionState() { |
196 } | 203 } |
197 | 204 |
198 CookieMonster* CreateMonsterFromStoreForGC( | 205 CookieMonster* CreateMonsterFromStoreForGC(int num_cookies, |
199 int num_cookies, | 206 int num_old_cookies, |
200 int num_old_cookies, | 207 int days_old) { |
201 int days_old) { | |
202 base::Time current(base::Time::Now()); | 208 base::Time current(base::Time::Now()); |
203 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000)); | 209 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000)); |
204 scoped_refptr<MockSimplePersistentCookieStore> store( | 210 scoped_refptr<MockSimplePersistentCookieStore> store( |
205 new MockSimplePersistentCookieStore); | 211 new MockSimplePersistentCookieStore); |
206 // Must expire to be persistent | 212 // Must expire to be persistent |
207 for (int i = 0; i < num_cookies; i++) { | 213 for (int i = 0; i < num_cookies; i++) { |
208 base::Time creation_time = | 214 base::Time creation_time = |
209 past_creation + base::TimeDelta::FromMicroseconds(i); | 215 past_creation + base::TimeDelta::FromMicroseconds(i); |
210 base::Time expiration_time = current + base::TimeDelta::FromDays(30); | 216 base::Time expiration_time = current + base::TimeDelta::FromDays(30); |
211 base::Time last_access_time = | 217 base::Time last_access_time = |
212 (i < num_old_cookies) ? current - base::TimeDelta::FromDays(days_old) : | 218 (i < num_old_cookies) ? current - base::TimeDelta::FromDays(days_old) |
213 current; | 219 : current; |
214 | 220 |
215 CanonicalCookie cc( | 221 CanonicalCookie cc(GURL(), |
216 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", | 222 "a", |
217 creation_time, expiration_time, last_access_time, false, false, | 223 "1", |
218 COOKIE_PRIORITY_DEFAULT); | 224 base::StringPrintf("h%05d.izzle", i), |
| 225 "/path", |
| 226 creation_time, |
| 227 expiration_time, |
| 228 last_access_time, |
| 229 false, |
| 230 false, |
| 231 COOKIE_PRIORITY_DEFAULT); |
219 store->AddCookie(cc); | 232 store->AddCookie(cc); |
220 } | 233 } |
221 | 234 |
222 return new CookieMonster(store.get(), NULL); | 235 return new CookieMonster(store.get(), NULL); |
223 } | 236 } |
224 | 237 |
225 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} | 238 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() { |
| 239 } |
226 | 240 |
227 } // namespace net | 241 } // namespace net |
OLD | NEW |