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 "content/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "content/browser/net/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
18 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
19 #include "base/test/sequenced_worker_pool_owner.h" | 19 #include "base/test/sequenced_worker_pool_owner.h" |
20 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
22 #include "content/public/browser/cookie_crypto_delegate.h" | |
23 #include "content/public/browser/cookie_store_factory.h" | |
24 #include "crypto/encryptor.h" | |
25 #include "crypto/symmetric_key.h" | |
26 #include "net/cookies/canonical_cookie.h" | 22 #include "net/cookies/canonical_cookie.h" |
27 #include "net/cookies/cookie_constants.h" | 23 #include "net/cookies/cookie_constants.h" |
28 #include "sql/connection.h" | 24 #include "sql/connection.h" |
29 #include "sql/meta_table.h" | 25 #include "sql/meta_table.h" |
30 #include "sql/statement.h" | |
31 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
32 #include "url/gurl.h" | 27 #include "url/gurl.h" |
33 | 28 |
34 namespace content { | 29 namespace content { |
35 | 30 |
36 namespace { | 31 namespace { |
37 | 32 |
38 const base::FilePath::CharType kCookieFilename[] = FILE_PATH_LITERAL("Cookies"); | 33 const base::FilePath::CharType kCookieFilename[] = FILE_PATH_LITERAL("Cookies"); |
39 | 34 |
40 class CookieCryptor : public content::CookieCryptoDelegate { | |
41 public: | |
42 CookieCryptor(); | |
43 virtual bool EncryptString(const std::string& plaintext, | |
44 std::string* ciphertext) OVERRIDE; | |
45 virtual bool DecryptString(const std::string& ciphertext, | |
46 std::string* plaintext) OVERRIDE; | |
47 | |
48 private: | |
49 scoped_ptr<crypto::SymmetricKey> key_; | |
50 crypto::Encryptor encryptor_; | |
51 }; | |
52 | |
53 CookieCryptor::CookieCryptor() : key_( | |
54 crypto::SymmetricKey::DeriveKeyFromPassword( | |
55 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256)) { | |
56 std::string iv("the iv: 16 bytes"); | |
57 encryptor_.Init(key_.get(), crypto::Encryptor::CBC, iv); | |
58 } | |
59 | |
60 bool CookieCryptor::EncryptString(const std::string& plaintext, | |
61 std::string* ciphertext) { | |
62 return encryptor_.Encrypt(plaintext, ciphertext); | |
63 } | |
64 | |
65 bool CookieCryptor::DecryptString(const std::string& ciphertext, | |
66 std::string* plaintext) { | |
67 return encryptor_.Decrypt(ciphertext, plaintext); | |
68 } | |
69 | |
70 } // namespace | 35 } // namespace |
71 | 36 |
72 typedef std::vector<net::CanonicalCookie*> CanonicalCookieVector; | 37 typedef std::vector<net::CanonicalCookie*> CanonicalCookieVector; |
73 | 38 |
74 class SQLitePersistentCookieStoreTest : public testing::Test { | 39 class SQLitePersistentCookieStoreTest : public testing::Test { |
75 public: | 40 public: |
76 SQLitePersistentCookieStoreTest() | 41 SQLitePersistentCookieStoreTest() |
77 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), | 42 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), |
78 loaded_event_(false, false), | 43 loaded_event_(false, false), |
79 key_loaded_event_(false, false), | 44 key_loaded_event_(false, false), |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 void DestroyStore() { | 83 void DestroyStore() { |
119 store_ = NULL; | 84 store_ = NULL; |
120 // Make sure we wait until the destructor has run by shutting down the pool | 85 // Make sure we wait until the destructor has run by shutting down the pool |
121 // resetting the owner (whose destructor blocks on the pool completion). | 86 // resetting the owner (whose destructor blocks on the pool completion). |
122 pool_owner_->pool()->Shutdown(); | 87 pool_owner_->pool()->Shutdown(); |
123 // Create a new pool for the few tests that create multiple stores. In other | 88 // Create a new pool for the few tests that create multiple stores. In other |
124 // cases this is wasted but harmless. | 89 // cases this is wasted but harmless. |
125 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); | 90 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); |
126 } | 91 } |
127 | 92 |
128 void CreateAndLoad(bool crypt_cookies, | 93 void CreateAndLoad(bool restore_old_session_cookies, |
129 bool restore_old_session_cookies, | |
130 CanonicalCookieVector* cookies) { | 94 CanonicalCookieVector* cookies) { |
131 store_ = new SQLitePersistentCookieStore( | 95 store_ = new SQLitePersistentCookieStore( |
132 temp_dir_.path().Append(kCookieFilename), | 96 temp_dir_.path().Append(kCookieFilename), |
133 client_task_runner(), | 97 client_task_runner(), |
134 background_task_runner(), | 98 background_task_runner(), |
135 restore_old_session_cookies, | 99 restore_old_session_cookies, |
136 NULL, | 100 NULL); |
137 crypt_cookies ? | |
138 scoped_ptr<content::CookieCryptoDelegate>(new CookieCryptor) : | |
139 scoped_ptr<content::CookieCryptoDelegate>()); | |
140 Load(cookies); | 101 Load(cookies); |
141 } | 102 } |
142 | 103 |
143 void InitializeStore(bool crypt, bool restore_old_session_cookies) { | 104 void InitializeStore(bool restore_old_session_cookies) { |
144 CanonicalCookieVector cookies; | 105 CanonicalCookieVector cookies; |
145 CreateAndLoad(crypt, restore_old_session_cookies, &cookies); | 106 CreateAndLoad(restore_old_session_cookies, &cookies); |
146 EXPECT_EQ(0U, cookies.size()); | 107 EXPECT_EQ(0U, cookies.size()); |
147 } | 108 } |
148 | 109 |
149 // We have to create this method to wrap WaitableEvent::Wait, since we cannot | 110 // We have to create this method to wrap WaitableEvent::Wait, since we cannot |
150 // bind a non-void returning method as a Closure. | 111 // bind a non-void returning method as a Closure. |
151 void WaitOnDBEvent() { | 112 void WaitOnDBEvent() { |
152 db_thread_event_.Wait(); | 113 db_thread_event_.Wait(); |
153 } | 114 } |
154 | 115 |
155 // Adds a persistent cookie to store_. | 116 // Adds a persistent cookie to store_. |
156 void AddCookie(const std::string& name, | 117 void AddCookie(const std::string& name, |
157 const std::string& value, | 118 const std::string& value, |
158 const std::string& domain, | 119 const std::string& domain, |
159 const std::string& path, | 120 const std::string& path, |
160 const base::Time& creation) { | 121 const base::Time& creation) { |
161 store_->AddCookie( | 122 store_->AddCookie( |
162 net::CanonicalCookie(GURL(), name, value, domain, path, creation, | 123 net::CanonicalCookie(GURL(), name, value, domain, path, creation, |
163 creation, creation, false, false, | 124 creation, creation, false, false, |
164 net::COOKIE_PRIORITY_DEFAULT)); | 125 net::COOKIE_PRIORITY_DEFAULT)); |
165 } | 126 } |
166 | 127 |
167 std::string ReadRawDBContents() { | |
168 std::string contents; | |
169 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), | |
170 &contents)) | |
171 return std::string(); | |
172 return contents; | |
173 } | |
174 | |
175 virtual void SetUp() OVERRIDE { | 128 virtual void SetUp() OVERRIDE { |
176 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 129 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
177 } | 130 } |
178 | 131 |
179 virtual void TearDown() OVERRIDE { | 132 virtual void TearDown() OVERRIDE { |
180 DestroyStore(); | 133 DestroyStore(); |
181 pool_owner_->pool()->Shutdown(); | 134 pool_owner_->pool()->Shutdown(); |
182 } | 135 } |
183 | 136 |
184 protected: | 137 protected: |
185 base::MessageLoop main_loop_; | 138 base::MessageLoop main_loop_; |
186 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; | 139 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; |
187 base::WaitableEvent loaded_event_; | 140 base::WaitableEvent loaded_event_; |
188 base::WaitableEvent key_loaded_event_; | 141 base::WaitableEvent key_loaded_event_; |
189 base::WaitableEvent db_thread_event_; | 142 base::WaitableEvent db_thread_event_; |
190 CanonicalCookieVector cookies_; | 143 CanonicalCookieVector cookies_; |
191 base::ScopedTempDir temp_dir_; | 144 base::ScopedTempDir temp_dir_; |
192 scoped_refptr<SQLitePersistentCookieStore> store_; | 145 scoped_refptr<SQLitePersistentCookieStore> store_; |
193 }; | 146 }; |
194 | 147 |
195 TEST_F(SQLitePersistentCookieStoreTest, TestInvalidMetaTableRecovery) { | 148 TEST_F(SQLitePersistentCookieStoreTest, TestInvalidMetaTableRecovery) { |
196 InitializeStore(false, false); | 149 InitializeStore(false); |
197 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); | 150 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); |
198 DestroyStore(); | 151 DestroyStore(); |
199 | 152 |
200 // Load up the store and verify that it has good data in it. | 153 // Load up the store and verify that it has good data in it. |
201 CanonicalCookieVector cookies; | 154 CanonicalCookieVector cookies; |
202 CreateAndLoad(false, false, &cookies); | 155 CreateAndLoad(false, &cookies); |
203 ASSERT_EQ(1U, cookies.size()); | 156 ASSERT_EQ(1U, cookies.size()); |
204 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); | 157 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); |
205 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 158 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
206 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 159 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
207 DestroyStore(); | 160 DestroyStore(); |
208 STLDeleteElements(&cookies); | 161 STLDeleteElements(&cookies); |
209 | 162 |
210 // Now corrupt the meta table. | 163 // Now corrupt the meta table. |
211 { | 164 { |
212 sql::Connection db; | 165 sql::Connection db; |
213 ASSERT_TRUE(db.Open(temp_dir_.path().Append(kCookieFilename))); | 166 ASSERT_TRUE(db.Open(temp_dir_.path().Append(kCookieFilename))); |
214 sql::MetaTable meta_table_; | 167 sql::MetaTable meta_table_; |
215 meta_table_.Init(&db, 1, 1); | 168 meta_table_.Init(&db, 1, 1); |
216 ASSERT_TRUE(db.Execute("DELETE FROM meta")); | 169 ASSERT_TRUE(db.Execute("DELETE FROM meta")); |
217 db.Close(); | 170 db.Close(); |
218 } | 171 } |
219 | 172 |
220 // Upon loading, the database should be reset to a good, blank state. | 173 // Upon loading, the database should be reset to a good, blank state. |
221 CreateAndLoad(false, false, &cookies); | 174 CreateAndLoad(false, &cookies); |
222 ASSERT_EQ(0U, cookies.size()); | 175 ASSERT_EQ(0U, cookies.size()); |
223 | 176 |
224 // Verify that, after, recovery, the database persists properly. | 177 // Verify that, after, recovery, the database persists properly. |
225 AddCookie("X", "Y", "foo.bar", "/", base::Time::Now()); | 178 AddCookie("X", "Y", "foo.bar", "/", base::Time::Now()); |
226 DestroyStore(); | 179 DestroyStore(); |
227 CreateAndLoad(false, false, &cookies); | 180 CreateAndLoad(false, &cookies); |
228 ASSERT_EQ(1U, cookies.size()); | 181 ASSERT_EQ(1U, cookies.size()); |
229 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); | 182 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); |
230 ASSERT_STREQ("X", cookies[0]->Name().c_str()); | 183 ASSERT_STREQ("X", cookies[0]->Name().c_str()); |
231 ASSERT_STREQ("Y", cookies[0]->Value().c_str()); | 184 ASSERT_STREQ("Y", cookies[0]->Value().c_str()); |
232 STLDeleteElements(&cookies); | 185 STLDeleteElements(&cookies); |
233 } | 186 } |
234 | 187 |
235 // Test if data is stored as expected in the SQLite database. | 188 // Test if data is stored as expected in the SQLite database. |
236 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { | 189 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { |
237 InitializeStore(false, false); | 190 InitializeStore(false); |
238 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); | 191 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); |
239 // Replace the store effectively destroying the current one and forcing it | 192 // Replace the store effectively destroying the current one and forcing it |
240 // to write its data to disk. Then we can see if after loading it again it | 193 // to write its data to disk. Then we can see if after loading it again it |
241 // is still there. | 194 // is still there. |
242 DestroyStore(); | 195 DestroyStore(); |
243 // Reload and test for persistence | 196 // Reload and test for persistence |
244 CanonicalCookieVector cookies; | 197 CanonicalCookieVector cookies; |
245 CreateAndLoad(false, false, &cookies); | 198 CreateAndLoad(false, &cookies); |
246 ASSERT_EQ(1U, cookies.size()); | 199 ASSERT_EQ(1U, cookies.size()); |
247 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); | 200 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); |
248 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 201 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
249 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 202 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
250 | 203 |
251 // Now delete the cookie and check persistence again. | 204 // Now delete the cookie and check persistence again. |
252 store_->DeleteCookie(*cookies[0]); | 205 store_->DeleteCookie(*cookies[0]); |
253 DestroyStore(); | 206 DestroyStore(); |
254 STLDeleteElements(&cookies); | 207 STLDeleteElements(&cookies); |
255 | 208 |
256 // Reload and check if the cookie has been removed. | 209 // Reload and check if the cookie has been removed. |
257 CreateAndLoad(false, false, &cookies); | 210 CreateAndLoad(false, &cookies); |
258 ASSERT_EQ(0U, cookies.size()); | 211 ASSERT_EQ(0U, cookies.size()); |
259 } | 212 } |
260 | 213 |
261 // Test that priority load of cookies for a specfic domain key could be | 214 // Test that priority load of cookies for a specfic domain key could be |
262 // completed before the entire store is loaded | 215 // completed before the entire store is loaded |
263 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | 216 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
264 InitializeStore(false, false); | 217 InitializeStore(false); |
265 base::Time t = base::Time::Now(); | 218 base::Time t = base::Time::Now(); |
266 AddCookie("A", "B", "foo.bar", "/", t); | 219 AddCookie("A", "B", "foo.bar", "/", t); |
267 t += base::TimeDelta::FromInternalValue(10); | 220 t += base::TimeDelta::FromInternalValue(10); |
268 AddCookie("A", "B", "www.aaa.com", "/", t); | 221 AddCookie("A", "B", "www.aaa.com", "/", t); |
269 t += base::TimeDelta::FromInternalValue(10); | 222 t += base::TimeDelta::FromInternalValue(10); |
270 AddCookie("A", "B", "travel.aaa.com", "/", t); | 223 AddCookie("A", "B", "travel.aaa.com", "/", t); |
271 t += base::TimeDelta::FromInternalValue(10); | 224 t += base::TimeDelta::FromInternalValue(10); |
272 AddCookie("A", "B", "www.bbb.com", "/", t); | 225 AddCookie("A", "B", "www.bbb.com", "/", t); |
273 DestroyStore(); | 226 DestroyStore(); |
274 | 227 |
275 store_ = new SQLitePersistentCookieStore( | 228 store_ = new SQLitePersistentCookieStore( |
276 temp_dir_.path().Append(kCookieFilename), | 229 temp_dir_.path().Append(kCookieFilename), |
277 client_task_runner(), | 230 client_task_runner(), |
278 background_task_runner(), | 231 background_task_runner(), |
279 false, NULL, | 232 false, NULL); |
280 scoped_ptr<content::CookieCryptoDelegate>()); | |
281 | |
282 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | 233 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
283 // until both Load and LoadCookiesForKey have been posted to its task queue. | 234 // until both Load and LoadCookiesForKey have been posted to its task queue. |
284 background_task_runner()->PostTask( | 235 background_task_runner()->PostTask( |
285 FROM_HERE, | 236 FROM_HERE, |
286 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | 237 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
287 base::Unretained(this))); | 238 base::Unretained(this))); |
288 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 239 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
289 base::Unretained(this))); | 240 base::Unretained(this))); |
290 store_->LoadCookiesForKey("aaa.com", | 241 store_->LoadCookiesForKey("aaa.com", |
291 base::Bind(&SQLitePersistentCookieStoreTest::OnKeyLoaded, | 242 base::Bind(&SQLitePersistentCookieStoreTest::OnKeyLoaded, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 } | 277 } |
327 ASSERT_EQ(4U, cookies_loaded.size()); | 278 ASSERT_EQ(4U, cookies_loaded.size()); |
328 ASSERT_EQ(cookies_loaded.find("foo.bar") != cookies_loaded.end(), | 279 ASSERT_EQ(cookies_loaded.find("foo.bar") != cookies_loaded.end(), |
329 true); | 280 true); |
330 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); | 281 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); |
331 STLDeleteElements(&cookies_); | 282 STLDeleteElements(&cookies_); |
332 } | 283 } |
333 | 284 |
334 // Test that we can force the database to be written by calling Flush(). | 285 // Test that we can force the database to be written by calling Flush(). |
335 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { | 286 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { |
336 InitializeStore(false, false); | 287 InitializeStore(false); |
337 // File timestamps don't work well on all platforms, so we'll determine | 288 // File timestamps don't work well on all platforms, so we'll determine |
338 // whether the DB file has been modified by checking its size. | 289 // whether the DB file has been modified by checking its size. |
339 base::FilePath path = temp_dir_.path().Append(kCookieFilename); | 290 base::FilePath path = temp_dir_.path().Append(kCookieFilename); |
340 base::PlatformFileInfo info; | 291 base::PlatformFileInfo info; |
341 ASSERT_TRUE(base::GetFileInfo(path, &info)); | 292 ASSERT_TRUE(base::GetFileInfo(path, &info)); |
342 int64 base_size = info.size; | 293 int64 base_size = info.size; |
343 | 294 |
344 // Write some large cookies, so the DB will have to expand by several KB. | 295 // Write some large cookies, so the DB will have to expand by several KB. |
345 for (char c = 'a'; c < 'z'; ++c) { | 296 for (char c = 'a'; c < 'z'; ++c) { |
346 // Each cookie needs a unique timestamp for creation_utc (see DB schema). | 297 // Each cookie needs a unique timestamp for creation_utc (see DB schema). |
347 base::Time t = base::Time::Now() + base::TimeDelta::FromMicroseconds(c); | 298 base::Time t = base::Time::Now() + base::TimeDelta::FromMicroseconds(c); |
348 std::string name(1, c); | 299 std::string name(1, c); |
349 std::string value(1000, c); | 300 std::string value(1000, c); |
350 AddCookie(name, value, "foo.bar", "/", t); | 301 AddCookie(name, value, "foo.bar", "/", t); |
351 } | 302 } |
352 | 303 |
353 Flush(); | 304 Flush(); |
354 | 305 |
355 // We forced a write, so now the file will be bigger. | 306 // We forced a write, so now the file will be bigger. |
356 ASSERT_TRUE(base::GetFileInfo(path, &info)); | 307 ASSERT_TRUE(base::GetFileInfo(path, &info)); |
357 ASSERT_GT(info.size, base_size); | 308 ASSERT_GT(info.size, base_size); |
358 } | 309 } |
359 | 310 |
360 // Test loading old session cookies from the disk. | 311 // Test loading old session cookies from the disk. |
361 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 312 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
362 InitializeStore(false, true); | 313 InitializeStore(true); |
363 | 314 |
364 // Add a session cookie. | 315 // Add a session cookie. |
365 store_->AddCookie( | 316 store_->AddCookie( |
366 net::CanonicalCookie( | 317 net::CanonicalCookie( |
367 GURL(), "C", "D", "sessioncookie.com", "/", base::Time::Now(), | 318 GURL(), "C", "D", "sessioncookie.com", "/", base::Time::Now(), |
368 base::Time(), base::Time::Now(), false, false, | 319 base::Time(), base::Time::Now(), false, false, |
369 net::COOKIE_PRIORITY_DEFAULT)); | 320 net::COOKIE_PRIORITY_DEFAULT)); |
370 | 321 |
371 // Force the store to write its data to the disk. | 322 // Force the store to write its data to the disk. |
372 DestroyStore(); | 323 DestroyStore(); |
373 | 324 |
374 // Create a store that loads session cookies and test that the session cookie | 325 // Create a store that loads session cookies and test that the session cookie |
375 // was loaded. | 326 // was loaded. |
376 CanonicalCookieVector cookies; | 327 CanonicalCookieVector cookies; |
377 CreateAndLoad(false, true, &cookies); | 328 CreateAndLoad(true, &cookies); |
378 | 329 |
379 ASSERT_EQ(1U, cookies.size()); | 330 ASSERT_EQ(1U, cookies.size()); |
380 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 331 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
381 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 332 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
382 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 333 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
383 ASSERT_EQ(net::COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); | 334 ASSERT_EQ(net::COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); |
384 | 335 |
385 STLDeleteElements(&cookies); | 336 STLDeleteElements(&cookies); |
386 } | 337 } |
387 | 338 |
388 // Test loading old session cookies from the disk. | 339 // Test loading old session cookies from the disk. |
389 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 340 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
390 InitializeStore(false, true); | 341 InitializeStore(true); |
391 | 342 |
392 // Add a session cookie. | 343 // Add a session cookie. |
393 store_->AddCookie( | 344 store_->AddCookie( |
394 net::CanonicalCookie( | 345 net::CanonicalCookie( |
395 GURL(), "C", "D", "sessioncookie.com", "/", base::Time::Now(), | 346 GURL(), "C", "D", "sessioncookie.com", "/", base::Time::Now(), |
396 base::Time(), base::Time::Now(), false, false, | 347 base::Time(), base::Time::Now(), false, false, |
397 net::COOKIE_PRIORITY_DEFAULT)); | 348 net::COOKIE_PRIORITY_DEFAULT)); |
398 | 349 |
399 // Force the store to write its data to the disk. | 350 // Force the store to write its data to the disk. |
400 DestroyStore(); | 351 DestroyStore(); |
401 | 352 |
402 // Create a store that doesn't load old session cookies and test that the | 353 // Create a store that doesn't load old session cookies and test that the |
403 // session cookie was not loaded. | 354 // session cookie was not loaded. |
404 CanonicalCookieVector cookies; | 355 CanonicalCookieVector cookies; |
405 CreateAndLoad(false, false, &cookies); | 356 CreateAndLoad(false, &cookies); |
406 ASSERT_EQ(0U, cookies.size()); | 357 ASSERT_EQ(0U, cookies.size()); |
407 | 358 |
408 // The store should also delete the session cookie. Wait until that has been | 359 // The store should also delete the session cookie. Wait until that has been |
409 // done. | 360 // done. |
410 DestroyStore(); | 361 DestroyStore(); |
411 | 362 |
412 // Create a store that loads old session cookies and test that the session | 363 // Create a store that loads old session cookies and test that the session |
413 // cookie is gone. | 364 // cookie is gone. |
414 CreateAndLoad(false, true, &cookies); | 365 CreateAndLoad(true, &cookies); |
415 ASSERT_EQ(0U, cookies.size()); | 366 ASSERT_EQ(0U, cookies.size()); |
416 } | 367 } |
417 | 368 |
418 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { | 369 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { |
419 InitializeStore(false, true); | 370 InitializeStore(true); |
420 static const char kSessionName[] = "session"; | 371 static const char kSessionName[] = "session"; |
421 static const char kPersistentName[] = "persistent"; | 372 static const char kPersistentName[] = "persistent"; |
422 | 373 |
423 // Add a session cookie. | 374 // Add a session cookie. |
424 store_->AddCookie( | 375 store_->AddCookie( |
425 net::CanonicalCookie( | 376 net::CanonicalCookie( |
426 GURL(), kSessionName, "val", "sessioncookie.com", "/", | 377 GURL(), kSessionName, "val", "sessioncookie.com", "/", |
427 base::Time::Now(), base::Time(), base::Time::Now(), false, false, | 378 base::Time::Now(), base::Time(), base::Time::Now(), false, false, |
428 net::COOKIE_PRIORITY_DEFAULT)); | 379 net::COOKIE_PRIORITY_DEFAULT)); |
429 // Add a persistent cookie. | 380 // Add a persistent cookie. |
430 store_->AddCookie( | 381 store_->AddCookie( |
431 net::CanonicalCookie( | 382 net::CanonicalCookie( |
432 GURL(), kPersistentName, "val", "sessioncookie.com", "/", | 383 GURL(), kPersistentName, "val", "sessioncookie.com", "/", |
433 base::Time::Now() - base::TimeDelta::FromDays(1), | 384 base::Time::Now() - base::TimeDelta::FromDays(1), |
434 base::Time::Now() + base::TimeDelta::FromDays(1), | 385 base::Time::Now() + base::TimeDelta::FromDays(1), |
435 base::Time::Now(), false, false, | 386 base::Time::Now(), false, false, |
436 net::COOKIE_PRIORITY_DEFAULT)); | 387 net::COOKIE_PRIORITY_DEFAULT)); |
437 | 388 |
438 // Force the store to write its data to the disk. | 389 // Force the store to write its data to the disk. |
439 DestroyStore(); | 390 DestroyStore(); |
440 | 391 |
441 // Create a store that loads session cookie and test that the IsPersistent | 392 // Create a store that loads session cookie and test that the IsPersistent |
442 // attribute is restored. | 393 // attribute is restored. |
443 CanonicalCookieVector cookies; | 394 CanonicalCookieVector cookies; |
444 CreateAndLoad(false, true, &cookies); | 395 CreateAndLoad(true, &cookies); |
445 ASSERT_EQ(2U, cookies.size()); | 396 ASSERT_EQ(2U, cookies.size()); |
446 | 397 |
447 std::map<std::string, net::CanonicalCookie*> cookie_map; | 398 std::map<std::string, net::CanonicalCookie*> cookie_map; |
448 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 399 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
449 it != cookies.end(); | 400 it != cookies.end(); |
450 ++it) { | 401 ++it) { |
451 cookie_map[(*it)->Name()] = *it; | 402 cookie_map[(*it)->Name()] = *it; |
452 } | 403 } |
453 | 404 |
454 std::map<std::string, net::CanonicalCookie*>::const_iterator it = | 405 std::map<std::string, net::CanonicalCookie*>::const_iterator it = |
455 cookie_map.find(kSessionName); | 406 cookie_map.find(kSessionName); |
456 ASSERT_TRUE(it != cookie_map.end()); | 407 ASSERT_TRUE(it != cookie_map.end()); |
457 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); | 408 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); |
458 | 409 |
459 it = cookie_map.find(kPersistentName); | 410 it = cookie_map.find(kPersistentName); |
460 ASSERT_TRUE(it != cookie_map.end()); | 411 ASSERT_TRUE(it != cookie_map.end()); |
461 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); | 412 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); |
462 | 413 |
463 STLDeleteElements(&cookies); | 414 STLDeleteElements(&cookies); |
464 } | 415 } |
465 | 416 |
466 TEST_F(SQLitePersistentCookieStoreTest, PriorityIsPersistent) { | 417 TEST_F(SQLitePersistentCookieStoreTest, PriorityIsPersistent) { |
467 static const char kLowName[] = "low"; | 418 static const char kLowName[] = "low"; |
468 static const char kMediumName[] = "medium"; | 419 static const char kMediumName[] = "medium"; |
469 static const char kHighName[] = "high"; | 420 static const char kHighName[] = "high"; |
470 static const char kCookieDomain[] = "sessioncookie.com"; | 421 static const char kCookieDomain[] = "sessioncookie.com"; |
471 static const char kCookieValue[] = "value"; | 422 static const char kCookieValue[] = "value"; |
472 static const char kCookiePath[] = "/"; | 423 static const char kCookiePath[] = "/"; |
473 | 424 |
474 InitializeStore(false, true); | 425 InitializeStore(true); |
475 | 426 |
476 // Add a low-priority persistent cookie. | 427 // Add a low-priority persistent cookie. |
477 store_->AddCookie( | 428 store_->AddCookie( |
478 net::CanonicalCookie( | 429 net::CanonicalCookie( |
479 GURL(), kLowName, kCookieValue, kCookieDomain, kCookiePath, | 430 GURL(), kLowName, kCookieValue, kCookieDomain, kCookiePath, |
480 base::Time::Now() - base::TimeDelta::FromMinutes(1), | 431 base::Time::Now() - base::TimeDelta::FromMinutes(1), |
481 base::Time::Now() + base::TimeDelta::FromDays(1), | 432 base::Time::Now() + base::TimeDelta::FromDays(1), |
482 base::Time::Now(), false, false, | 433 base::Time::Now(), false, false, |
483 net::COOKIE_PRIORITY_LOW)); | 434 net::COOKIE_PRIORITY_LOW)); |
484 | 435 |
(...skipping 14 matching lines...) Expand all Loading... |
499 base::Time::Now() + base::TimeDelta::FromDays(1), | 450 base::Time::Now() + base::TimeDelta::FromDays(1), |
500 base::Time::Now(), false, false, | 451 base::Time::Now(), false, false, |
501 net::COOKIE_PRIORITY_HIGH)); | 452 net::COOKIE_PRIORITY_HIGH)); |
502 | 453 |
503 // Force the store to write its data to the disk. | 454 // Force the store to write its data to the disk. |
504 DestroyStore(); | 455 DestroyStore(); |
505 | 456 |
506 // Create a store that loads session cookie and test that the priority | 457 // Create a store that loads session cookie and test that the priority |
507 // attribute values are restored. | 458 // attribute values are restored. |
508 CanonicalCookieVector cookies; | 459 CanonicalCookieVector cookies; |
509 CreateAndLoad(false, true, &cookies); | 460 CreateAndLoad(true, &cookies); |
510 ASSERT_EQ(3U, cookies.size()); | 461 ASSERT_EQ(3U, cookies.size()); |
511 | 462 |
512 // Put the cookies into a map, by name, so we can easily find them. | 463 // Put the cookies into a map, by name, so we can easily find them. |
513 std::map<std::string, net::CanonicalCookie*> cookie_map; | 464 std::map<std::string, net::CanonicalCookie*> cookie_map; |
514 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 465 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
515 it != cookies.end(); | 466 it != cookies.end(); |
516 ++it) { | 467 ++it) { |
517 cookie_map[(*it)->Name()] = *it; | 468 cookie_map[(*it)->Name()] = *it; |
518 } | 469 } |
519 | 470 |
520 // Validate that each cookie has the correct priority. | 471 // Validate that each cookie has the correct priority. |
521 std::map<std::string, net::CanonicalCookie*>::const_iterator it = | 472 std::map<std::string, net::CanonicalCookie*>::const_iterator it = |
522 cookie_map.find(kLowName); | 473 cookie_map.find(kLowName); |
523 ASSERT_TRUE(it != cookie_map.end()); | 474 ASSERT_TRUE(it != cookie_map.end()); |
524 EXPECT_EQ(net::COOKIE_PRIORITY_LOW, cookie_map[kLowName]->Priority()); | 475 EXPECT_EQ(net::COOKIE_PRIORITY_LOW, cookie_map[kLowName]->Priority()); |
525 | 476 |
526 it = cookie_map.find(kMediumName); | 477 it = cookie_map.find(kMediumName); |
527 ASSERT_TRUE(it != cookie_map.end()); | 478 ASSERT_TRUE(it != cookie_map.end()); |
528 EXPECT_EQ(net::COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); | 479 EXPECT_EQ(net::COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); |
529 | 480 |
530 it = cookie_map.find(kHighName); | 481 it = cookie_map.find(kHighName); |
531 ASSERT_TRUE(it != cookie_map.end()); | 482 ASSERT_TRUE(it != cookie_map.end()); |
532 EXPECT_EQ(net::COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); | 483 EXPECT_EQ(net::COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); |
533 | 484 |
534 STLDeleteElements(&cookies); | 485 STLDeleteElements(&cookies); |
535 } | 486 } |
536 | 487 |
537 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { | |
538 CanonicalCookieVector cookies; | |
539 | |
540 // Create unencrypted cookie store and write something to it. | |
541 InitializeStore(false, false); | |
542 AddCookie("name", "value123XYZ", "foo.bar", "/", base::Time::Now()); | |
543 DestroyStore(); | |
544 | |
545 // Verify that "value" is visible in the file. This is necessary in order to | |
546 // have confidence in a later test that "encrypted_value" is not visible. | |
547 std::string contents = ReadRawDBContents(); | |
548 EXPECT_NE(0U, contents.length()); | |
549 EXPECT_NE(contents.find("value123XYZ"), std::string::npos); | |
550 | |
551 // Create encrypted cookie store and ensure old cookie still reads. | |
552 STLDeleteElements(&cookies_); | |
553 EXPECT_EQ(0U, cookies_.size()); | |
554 CreateAndLoad(true, false, &cookies); | |
555 EXPECT_EQ(1U, cookies_.size()); | |
556 EXPECT_EQ("name", cookies_[0]->Name()); | |
557 EXPECT_EQ("value123XYZ", cookies_[0]->Value()); | |
558 | |
559 // Make sure we can update existing cookie and add new cookie as encrypted. | |
560 store_->DeleteCookie(*(cookies_[0])); | |
561 AddCookie("name", "encrypted_value123XYZ", "foo.bar", "/", base::Time::Now()); | |
562 AddCookie("other", "something456ABC", "foo.bar", "/", | |
563 base::Time::Now() + base::TimeDelta::FromInternalValue(10)); | |
564 DestroyStore(); | |
565 STLDeleteElements(&cookies_); | |
566 CreateAndLoad(true, false, &cookies); | |
567 EXPECT_EQ(2U, cookies_.size()); | |
568 net::CanonicalCookie* cookie_name = NULL; | |
569 net::CanonicalCookie* cookie_other = NULL; | |
570 if (cookies_[0]->Name() == "name") { | |
571 cookie_name = cookies_[0]; | |
572 cookie_other = cookies_[1]; | |
573 } else { | |
574 cookie_name = cookies_[1]; | |
575 cookie_other = cookies_[0]; | |
576 } | |
577 EXPECT_EQ("encrypted_value123XYZ", cookie_name->Value()); | |
578 EXPECT_EQ("something456ABC", cookie_other->Value()); | |
579 DestroyStore(); | |
580 | |
581 // Examine the real record to make sure plaintext version doesn't exist. | |
582 sql::Connection db; | |
583 sql::Statement smt; | |
584 int resultcount = 0; | |
585 ASSERT_TRUE(db.Open(temp_dir_.path().Append(kCookieFilename))); | |
586 smt.Assign(db.GetCachedStatement(SQL_FROM_HERE, | |
587 "SELECT * " | |
588 "FROM cookies " | |
589 "WHERE host_key = 'foo.bar'")); | |
590 while (smt.Step()) { | |
591 resultcount++; | |
592 for (int i=0; i < smt.ColumnCount(); i++) { | |
593 EXPECT_EQ(smt.ColumnString(i).find("value"), std::string::npos); | |
594 EXPECT_EQ(smt.ColumnString(i).find("something"), std::string::npos); | |
595 } | |
596 } | |
597 EXPECT_EQ(2, resultcount); | |
598 | |
599 // Verify that "encrypted_value" is NOT visible in the file. | |
600 contents = ReadRawDBContents(); | |
601 EXPECT_NE(0U, contents.length()); | |
602 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); | |
603 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); | |
604 } | |
605 | |
606 } // namespace content | 488 } // namespace content |
OLD | NEW |