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