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