| 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 "net/extras/sqlite/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/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/location.h" |
| 14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.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_store_factory.h" | |
| 23 #include "crypto/encryptor.h" | 22 #include "crypto/encryptor.h" |
| 24 #include "crypto/symmetric_key.h" | 23 #include "crypto/symmetric_key.h" |
| 25 #include "net/cookies/canonical_cookie.h" | 24 #include "net/cookies/canonical_cookie.h" |
| 26 #include "net/cookies/cookie_constants.h" | 25 #include "net/cookies/cookie_constants.h" |
| 27 #include "net/extras/sqlite/cookie_crypto_delegate.h" | 26 #include "net/extras/sqlite/cookie_crypto_delegate.h" |
| 28 #include "sql/connection.h" | 27 #include "sql/connection.h" |
| 29 #include "sql/meta_table.h" | 28 #include "sql/meta_table.h" |
| 30 #include "sql/statement.h" | 29 #include "sql/statement.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 33 | 32 |
| 34 namespace content { | 33 namespace net { |
| 35 | 34 |
| 36 namespace { | 35 namespace { |
| 37 | 36 |
| 38 const base::FilePath::CharType kCookieFilename[] = FILE_PATH_LITERAL("Cookies"); | 37 const base::FilePath::CharType kCookieFilename[] = FILE_PATH_LITERAL("Cookies"); |
| 39 | 38 |
| 40 class CookieCryptor : public net::CookieCryptoDelegate { | 39 class CookieCryptor : public CookieCryptoDelegate { |
| 41 public: | 40 public: |
| 42 CookieCryptor(); | 41 CookieCryptor(); |
| 43 bool EncryptString(const std::string& plaintext, | 42 bool EncryptString(const std::string& plaintext, |
| 44 std::string* ciphertext) override; | 43 std::string* ciphertext) override; |
| 45 bool DecryptString(const std::string& ciphertext, | 44 bool DecryptString(const std::string& ciphertext, |
| 46 std::string* plaintext) override; | 45 std::string* plaintext) override; |
| 47 | 46 |
| 48 private: | 47 private: |
| 49 scoped_ptr<crypto::SymmetricKey> key_; | 48 scoped_ptr<crypto::SymmetricKey> key_; |
| 50 crypto::Encryptor encryptor_; | 49 crypto::Encryptor encryptor_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 62 return encryptor_.Encrypt(plaintext, ciphertext); | 61 return encryptor_.Encrypt(plaintext, ciphertext); |
| 63 } | 62 } |
| 64 | 63 |
| 65 bool CookieCryptor::DecryptString(const std::string& ciphertext, | 64 bool CookieCryptor::DecryptString(const std::string& ciphertext, |
| 66 std::string* plaintext) { | 65 std::string* plaintext) { |
| 67 return encryptor_.Decrypt(ciphertext, plaintext); | 66 return encryptor_.Decrypt(ciphertext, plaintext); |
| 68 } | 67 } |
| 69 | 68 |
| 70 } // namespace | 69 } // namespace |
| 71 | 70 |
| 72 typedef std::vector<net::CanonicalCookie*> CanonicalCookieVector; | 71 typedef std::vector<CanonicalCookie*> CanonicalCookieVector; |
| 73 | 72 |
| 74 class SQLitePersistentCookieStoreTest : public testing::Test { | 73 class SQLitePersistentCookieStoreTest : public testing::Test { |
| 75 public: | 74 public: |
| 76 SQLitePersistentCookieStoreTest() | 75 SQLitePersistentCookieStoreTest() |
| 77 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), | 76 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), |
| 78 loaded_event_(false, false), | 77 loaded_event_(false, false), |
| 79 key_loaded_event_(false, false), | 78 key_loaded_event_(false, false), |
| 80 db_thread_event_(false, false) { | 79 db_thread_event_(false, false) { |
| 81 } | 80 } |
| 82 | 81 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 99 } | 98 } |
| 100 | 99 |
| 101 void Flush() { | 100 void Flush() { |
| 102 base::WaitableEvent event(false, false); | 101 base::WaitableEvent event(false, false); |
| 103 store_->Flush(base::Bind(&base::WaitableEvent::Signal, | 102 store_->Flush(base::Bind(&base::WaitableEvent::Signal, |
| 104 base::Unretained(&event))); | 103 base::Unretained(&event))); |
| 105 event.Wait(); | 104 event.Wait(); |
| 106 } | 105 } |
| 107 | 106 |
| 108 scoped_refptr<base::SequencedTaskRunner> background_task_runner() { | 107 scoped_refptr<base::SequencedTaskRunner> background_task_runner() { |
| 109 return pool_owner_->pool()->GetSequencedTaskRunner( | 108 if (!background_task_runner_.get()) { |
| 110 pool_owner_->pool()->GetNamedSequenceToken("background")); | 109 background_task_runner_ = pool_owner_->pool()->GetSequencedTaskRunner( |
| 110 pool_owner_->pool()->GetNamedSequenceToken("background")); |
| 111 } |
| 112 return background_task_runner_; |
| 111 } | 113 } |
| 112 | 114 |
| 113 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { | 115 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { |
| 114 return pool_owner_->pool()->GetSequencedTaskRunner( | 116 if (!client_task_runner_.get()) { |
| 115 pool_owner_->pool()->GetNamedSequenceToken("client")); | 117 client_task_runner_ = pool_owner_->pool()->GetSequencedTaskRunner( |
| 118 pool_owner_->pool()->GetNamedSequenceToken("client")); |
| 119 } |
| 120 return client_task_runner_; |
| 116 } | 121 } |
| 117 | 122 |
| 118 void DestroyStore() { | 123 void DestroyStore() { |
| 119 store_ = NULL; | 124 store_ = nullptr; |
| 125 background_task_runner_ = nullptr; |
| 126 client_task_runner_ = nullptr; |
| 120 // Make sure we wait until the destructor has run by shutting down the pool | 127 // 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). | 128 // resetting the owner (whose destructor blocks on the pool completion). |
| 122 pool_owner_->pool()->Shutdown(); | 129 pool_owner_->pool()->Shutdown(); |
| 123 // Create a new pool for the few tests that create multiple stores. In other | 130 // Create a new pool for the few tests that create multiple stores. In other |
| 124 // cases this is wasted but harmless. | 131 // cases this is wasted but harmless. |
| 125 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); | 132 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); |
| 126 } | 133 } |
| 127 | 134 |
| 128 void CreateAndLoad(bool crypt_cookies, | 135 void CreateAndLoad(bool crypt_cookies, |
| 129 bool restore_old_session_cookies, | 136 bool restore_old_session_cookies, |
| 130 CanonicalCookieVector* cookies) { | 137 CanonicalCookieVector* cookies) { |
| 131 if (crypt_cookies) | 138 if (crypt_cookies) |
| 132 cookie_crypto_delegate_.reset(new CookieCryptor()); | 139 cookie_crypto_delegate_.reset(new CookieCryptor()); |
| 133 | 140 |
| 134 store_ = new SQLitePersistentCookieStore( | 141 store_ = new SQLitePersistentCookieStore( |
| 135 temp_dir_.path().Append(kCookieFilename), | 142 temp_dir_.path().Append(kCookieFilename), |
| 136 client_task_runner(), | 143 client_task_runner(), |
| 137 background_task_runner(), | 144 background_task_runner(), |
| 138 restore_old_session_cookies, | 145 restore_old_session_cookies, |
| 139 NULL, | |
| 140 cookie_crypto_delegate_.get()); | 146 cookie_crypto_delegate_.get()); |
| 141 Load(cookies); | 147 Load(cookies); |
| 142 } | 148 } |
| 143 | 149 |
| 144 void InitializeStore(bool crypt, bool restore_old_session_cookies) { | 150 void InitializeStore(bool crypt, bool restore_old_session_cookies) { |
| 145 CanonicalCookieVector cookies; | 151 CanonicalCookieVector cookies; |
| 146 CreateAndLoad(crypt, restore_old_session_cookies, &cookies); | 152 CreateAndLoad(crypt, restore_old_session_cookies, &cookies); |
| 147 EXPECT_EQ(0U, cookies.size()); | 153 EXPECT_EQ(0U, cookies.size()); |
| 148 } | 154 } |
| 149 | 155 |
| 150 // We have to create this method to wrap WaitableEvent::Wait, since we cannot | 156 // We have to create this method to wrap WaitableEvent::Wait, since we cannot |
| 151 // bind a non-void returning method as a Closure. | 157 // bind a non-void returning method as a Closure. |
| 152 void WaitOnDBEvent() { | 158 void WaitOnDBEvent() { |
| 153 db_thread_event_.Wait(); | 159 db_thread_event_.Wait(); |
| 154 } | 160 } |
| 155 | 161 |
| 156 // Adds a persistent cookie to store_. | 162 // Adds a persistent cookie to store_. |
| 157 void AddCookie(const std::string& name, | 163 void AddCookie(const std::string& name, |
| 158 const std::string& value, | 164 const std::string& value, |
| 159 const std::string& domain, | 165 const std::string& domain, |
| 160 const std::string& path, | 166 const std::string& path, |
| 161 const base::Time& creation) { | 167 const base::Time& creation) { |
| 162 store_->AddCookie(net::CanonicalCookie( | 168 store_->AddCookie(CanonicalCookie( |
| 163 GURL(), name, value, domain, path, creation, creation, creation, false, | 169 GURL(), name, value, domain, path, creation, creation, creation, false, |
| 164 false, false, net::COOKIE_PRIORITY_DEFAULT)); | 170 false, false, COOKIE_PRIORITY_DEFAULT)); |
| 165 } | 171 } |
| 166 | 172 |
| 167 void AddCookieWithExpiration(const std::string& name, | 173 void AddCookieWithExpiration(const std::string& name, |
| 168 const std::string& value, | 174 const std::string& value, |
| 169 const std::string& domain, | 175 const std::string& domain, |
| 170 const std::string& path, | 176 const std::string& path, |
| 171 const base::Time& creation, | 177 const base::Time& creation, |
| 172 const base::Time& expiration) { | 178 const base::Time& expiration) { |
| 173 store_->AddCookie(net::CanonicalCookie( | 179 store_->AddCookie(CanonicalCookie( |
| 174 GURL(), name, value, domain, path, creation, expiration, creation, | 180 GURL(), name, value, domain, path, creation, expiration, creation, |
| 175 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); | 181 false, false, false, COOKIE_PRIORITY_DEFAULT)); |
| 176 } | 182 } |
| 177 | 183 |
| 178 std::string ReadRawDBContents() { | 184 std::string ReadRawDBContents() { |
| 179 std::string contents; | 185 std::string contents; |
| 180 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), | 186 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), |
| 181 &contents)) | 187 &contents)) |
| 182 return std::string(); | 188 return std::string(); |
| 183 return contents; | 189 return contents; |
| 184 } | 190 } |
| 185 | 191 |
| 186 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 192 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
| 187 | 193 |
| 188 void TearDown() override { | 194 void TearDown() override { |
| 189 DestroyStore(); | 195 DestroyStore(); |
| 190 pool_owner_->pool()->Shutdown(); | 196 pool_owner_->pool()->Shutdown(); |
| 191 } | 197 } |
| 192 | 198 |
| 193 protected: | 199 protected: |
| 194 base::MessageLoop main_loop_; | |
| 195 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; | 200 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; |
| 201 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 202 scoped_refptr<base::SequencedTaskRunner> client_task_runner_; |
| 196 base::WaitableEvent loaded_event_; | 203 base::WaitableEvent loaded_event_; |
| 197 base::WaitableEvent key_loaded_event_; | 204 base::WaitableEvent key_loaded_event_; |
| 198 base::WaitableEvent db_thread_event_; | 205 base::WaitableEvent db_thread_event_; |
| 199 CanonicalCookieVector cookies_; | 206 CanonicalCookieVector cookies_; |
| 200 base::ScopedTempDir temp_dir_; | 207 base::ScopedTempDir temp_dir_; |
| 201 scoped_refptr<SQLitePersistentCookieStore> store_; | 208 scoped_refptr<SQLitePersistentCookieStore> store_; |
| 202 scoped_ptr<net::CookieCryptoDelegate> cookie_crypto_delegate_; | 209 scoped_ptr<CookieCryptoDelegate> cookie_crypto_delegate_; |
| 203 }; | 210 }; |
| 204 | 211 |
| 205 TEST_F(SQLitePersistentCookieStoreTest, TestInvalidMetaTableRecovery) { | 212 TEST_F(SQLitePersistentCookieStoreTest, TestInvalidMetaTableRecovery) { |
| 206 InitializeStore(false, false); | 213 InitializeStore(false, false); |
| 207 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); | 214 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); |
| 208 DestroyStore(); | 215 DestroyStore(); |
| 209 | 216 |
| 210 // Load up the store and verify that it has good data in it. | 217 // Load up the store and verify that it has good data in it. |
| 211 CanonicalCookieVector cookies; | 218 CanonicalCookieVector cookies; |
| 212 CreateAndLoad(false, false, &cookies); | 219 CreateAndLoad(false, false, &cookies); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 t += base::TimeDelta::FromInternalValue(10); | 300 t += base::TimeDelta::FromInternalValue(10); |
| 294 AddCookieWithExpiration("A", "B", "b5.com", "/", t, base::Time()); | 301 AddCookieWithExpiration("A", "B", "b5.com", "/", t, base::Time()); |
| 295 DestroyStore(); | 302 DestroyStore(); |
| 296 | 303 |
| 297 // Load the store a second time. Before the store finishes loading, add a | 304 // Load the store a second time. Before the store finishes loading, add a |
| 298 // transient cookie and flush it to disk. | 305 // transient cookie and flush it to disk. |
| 299 store_ = new SQLitePersistentCookieStore( | 306 store_ = new SQLitePersistentCookieStore( |
| 300 temp_dir_.path().Append(kCookieFilename), | 307 temp_dir_.path().Append(kCookieFilename), |
| 301 client_task_runner(), | 308 client_task_runner(), |
| 302 background_task_runner(), | 309 background_task_runner(), |
| 303 false, NULL, NULL); | 310 false, nullptr); |
| 304 | 311 |
| 305 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | 312 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
| 306 // until both Load and Flush have been posted to its task queue. | 313 // until both Load and Flush have been posted to its task queue. |
| 307 background_task_runner()->PostTask( | 314 background_task_runner()->PostTask( |
| 308 FROM_HERE, | 315 FROM_HERE, |
| 309 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | 316 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
| 310 base::Unretained(this))); | 317 base::Unretained(this))); |
| 311 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 318 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 312 base::Unretained(this))); | 319 base::Unretained(this))); |
| 313 t += base::TimeDelta::FromInternalValue(10); | 320 t += base::TimeDelta::FromInternalValue(10); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 329 STLDeleteElements(&cookies_); | 336 STLDeleteElements(&cookies_); |
| 330 DestroyStore(); | 337 DestroyStore(); |
| 331 | 338 |
| 332 // Load the store a third time, this time restoring session cookies. The | 339 // Load the store a third time, this time restoring session cookies. The |
| 333 // store should contain exactly 4 cookies: the 3 persistent, and "c.com", | 340 // store should contain exactly 4 cookies: the 3 persistent, and "c.com", |
| 334 // which was added during the second cookie store load. | 341 // which was added during the second cookie store load. |
| 335 store_ = new SQLitePersistentCookieStore( | 342 store_ = new SQLitePersistentCookieStore( |
| 336 temp_dir_.path().Append(kCookieFilename), | 343 temp_dir_.path().Append(kCookieFilename), |
| 337 client_task_runner(), | 344 client_task_runner(), |
| 338 background_task_runner(), | 345 background_task_runner(), |
| 339 true, NULL, NULL); | 346 true, nullptr); |
| 340 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 347 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 341 base::Unretained(this))); | 348 base::Unretained(this))); |
| 342 loaded_event_.Wait(); | 349 loaded_event_.Wait(); |
| 343 ASSERT_EQ(4u, cookies_.size()); | 350 ASSERT_EQ(4u, cookies_.size()); |
| 344 STLDeleteElements(&cookies_); | 351 STLDeleteElements(&cookies_); |
| 345 } | 352 } |
| 346 | 353 |
| 347 // Test that priority load of cookies for a specfic domain key could be | 354 // Test that priority load of cookies for a specfic domain key could be |
| 348 // completed before the entire store is loaded | 355 // completed before the entire store is loaded |
| 349 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | 356 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
| 350 InitializeStore(false, false); | 357 InitializeStore(false, false); |
| 351 base::Time t = base::Time::Now(); | 358 base::Time t = base::Time::Now(); |
| 352 AddCookie("A", "B", "foo.bar", "/", t); | 359 AddCookie("A", "B", "foo.bar", "/", t); |
| 353 t += base::TimeDelta::FromInternalValue(10); | 360 t += base::TimeDelta::FromInternalValue(10); |
| 354 AddCookie("A", "B", "www.aaa.com", "/", t); | 361 AddCookie("A", "B", "www.aaa.com", "/", t); |
| 355 t += base::TimeDelta::FromInternalValue(10); | 362 t += base::TimeDelta::FromInternalValue(10); |
| 356 AddCookie("A", "B", "travel.aaa.com", "/", t); | 363 AddCookie("A", "B", "travel.aaa.com", "/", t); |
| 357 t += base::TimeDelta::FromInternalValue(10); | 364 t += base::TimeDelta::FromInternalValue(10); |
| 358 AddCookie("A", "B", "www.bbb.com", "/", t); | 365 AddCookie("A", "B", "www.bbb.com", "/", t); |
| 359 DestroyStore(); | 366 DestroyStore(); |
| 360 | 367 |
| 361 store_ = new SQLitePersistentCookieStore( | 368 store_ = new SQLitePersistentCookieStore( |
| 362 temp_dir_.path().Append(kCookieFilename), | 369 temp_dir_.path().Append(kCookieFilename), |
| 363 client_task_runner(), | 370 client_task_runner(), |
| 364 background_task_runner(), | 371 background_task_runner(), |
| 365 false, NULL, NULL); | 372 false, nullptr); |
| 366 | 373 |
| 367 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | 374 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
| 368 // until both Load and LoadCookiesForKey have been posted to its task queue. | 375 // until both Load and LoadCookiesForKey have been posted to its task queue. |
| 369 background_task_runner()->PostTask( | 376 background_task_runner()->PostTask( |
| 370 FROM_HERE, | 377 FROM_HERE, |
| 371 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | 378 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
| 372 base::Unretained(this))); | 379 base::Unretained(this))); |
| 373 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 380 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 374 base::Unretained(this))); | 381 base::Unretained(this))); |
| 375 store_->LoadCookiesForKey("aaa.com", | 382 store_->LoadCookiesForKey("aaa.com", |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 // We forced a write, so now the file will be bigger. | 447 // We forced a write, so now the file will be bigger. |
| 441 ASSERT_TRUE(base::GetFileInfo(path, &info)); | 448 ASSERT_TRUE(base::GetFileInfo(path, &info)); |
| 442 ASSERT_GT(info.size, base_size); | 449 ASSERT_GT(info.size, base_size); |
| 443 } | 450 } |
| 444 | 451 |
| 445 // Test loading old session cookies from the disk. | 452 // Test loading old session cookies from the disk. |
| 446 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 453 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
| 447 InitializeStore(false, true); | 454 InitializeStore(false, true); |
| 448 | 455 |
| 449 // Add a session cookie. | 456 // Add a session cookie. |
| 450 store_->AddCookie(net::CanonicalCookie(GURL(), "C", "D", "sessioncookie.com", | 457 store_->AddCookie(CanonicalCookie(GURL(), "C", "D", "sessioncookie.com", |
| 451 "/", base::Time::Now(), base::Time(), | 458 "/", base::Time::Now(), base::Time(), |
| 452 base::Time::Now(), false, false, false, | 459 base::Time::Now(), false, false, false, |
| 453 net::COOKIE_PRIORITY_DEFAULT)); | 460 COOKIE_PRIORITY_DEFAULT)); |
| 454 | 461 |
| 455 // Force the store to write its data to the disk. | 462 // Force the store to write its data to the disk. |
| 456 DestroyStore(); | 463 DestroyStore(); |
| 457 | 464 |
| 458 // Create a store that loads session cookies and test that the session cookie | 465 // Create a store that loads session cookies and test that the session cookie |
| 459 // was loaded. | 466 // was loaded. |
| 460 CanonicalCookieVector cookies; | 467 CanonicalCookieVector cookies; |
| 461 CreateAndLoad(false, true, &cookies); | 468 CreateAndLoad(false, true, &cookies); |
| 462 | 469 |
| 463 ASSERT_EQ(1U, cookies.size()); | 470 ASSERT_EQ(1U, cookies.size()); |
| 464 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 471 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
| 465 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 472 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
| 466 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 473 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
| 467 ASSERT_EQ(net::COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); | 474 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); |
| 468 | 475 |
| 469 STLDeleteElements(&cookies); | 476 STLDeleteElements(&cookies); |
| 470 } | 477 } |
| 471 | 478 |
| 472 // Test loading old session cookies from the disk. | 479 // Test loading old session cookies from the disk. |
| 473 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 480 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
| 474 InitializeStore(false, true); | 481 InitializeStore(false, true); |
| 475 | 482 |
| 476 // Add a session cookie. | 483 // Add a session cookie. |
| 477 store_->AddCookie(net::CanonicalCookie(GURL(), "C", "D", "sessioncookie.com", | 484 store_->AddCookie(CanonicalCookie(GURL(), "C", "D", "sessioncookie.com", |
| 478 "/", base::Time::Now(), base::Time(), | 485 "/", base::Time::Now(), base::Time(), |
| 479 base::Time::Now(), false, false, false, | 486 base::Time::Now(), false, false, false, |
| 480 net::COOKIE_PRIORITY_DEFAULT)); | 487 COOKIE_PRIORITY_DEFAULT)); |
| 481 | 488 |
| 482 // Force the store to write its data to the disk. | 489 // Force the store to write its data to the disk. |
| 483 DestroyStore(); | 490 DestroyStore(); |
| 484 | 491 |
| 485 // Create a store that doesn't load old session cookies and test that the | 492 // Create a store that doesn't load old session cookies and test that the |
| 486 // session cookie was not loaded. | 493 // session cookie was not loaded. |
| 487 CanonicalCookieVector cookies; | 494 CanonicalCookieVector cookies; |
| 488 CreateAndLoad(false, false, &cookies); | 495 CreateAndLoad(false, false, &cookies); |
| 489 ASSERT_EQ(0U, cookies.size()); | 496 ASSERT_EQ(0U, cookies.size()); |
| 490 | 497 |
| 491 // The store should also delete the session cookie. Wait until that has been | 498 // The store should also delete the session cookie. Wait until that has been |
| 492 // done. | 499 // done. |
| 493 DestroyStore(); | 500 DestroyStore(); |
| 494 | 501 |
| 495 // Create a store that loads old session cookies and test that the session | 502 // Create a store that loads old session cookies and test that the session |
| 496 // cookie is gone. | 503 // cookie is gone. |
| 497 CreateAndLoad(false, true, &cookies); | 504 CreateAndLoad(false, true, &cookies); |
| 498 ASSERT_EQ(0U, cookies.size()); | 505 ASSERT_EQ(0U, cookies.size()); |
| 499 } | 506 } |
| 500 | 507 |
| 501 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { | 508 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { |
| 502 InitializeStore(false, true); | 509 InitializeStore(false, true); |
| 503 static const char kSessionName[] = "session"; | 510 static const char kSessionName[] = "session"; |
| 504 static const char kPersistentName[] = "persistent"; | 511 static const char kPersistentName[] = "persistent"; |
| 505 | 512 |
| 506 // Add a session cookie. | 513 // Add a session cookie. |
| 507 store_->AddCookie(net::CanonicalCookie( | 514 store_->AddCookie(CanonicalCookie( |
| 508 GURL(), kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), | 515 GURL(), kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), |
| 509 base::Time(), base::Time::Now(), false, false, false, | 516 base::Time(), base::Time::Now(), false, false, false, |
| 510 net::COOKIE_PRIORITY_DEFAULT)); | 517 COOKIE_PRIORITY_DEFAULT)); |
| 511 // Add a persistent cookie. | 518 // Add a persistent cookie. |
| 512 store_->AddCookie(net::CanonicalCookie( | 519 store_->AddCookie(CanonicalCookie( |
| 513 GURL(), kPersistentName, "val", "sessioncookie.com", "/", | 520 GURL(), kPersistentName, "val", "sessioncookie.com", "/", |
| 514 base::Time::Now() - base::TimeDelta::FromDays(1), | 521 base::Time::Now() - base::TimeDelta::FromDays(1), |
| 515 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 522 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
| 516 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); | 523 false, false, false, COOKIE_PRIORITY_DEFAULT)); |
| 517 | 524 |
| 518 // Force the store to write its data to the disk. | 525 // Force the store to write its data to the disk. |
| 519 DestroyStore(); | 526 DestroyStore(); |
| 520 | 527 |
| 521 // Create a store that loads session cookie and test that the IsPersistent | 528 // Create a store that loads session cookie and test that the IsPersistent |
| 522 // attribute is restored. | 529 // attribute is restored. |
| 523 CanonicalCookieVector cookies; | 530 CanonicalCookieVector cookies; |
| 524 CreateAndLoad(false, true, &cookies); | 531 CreateAndLoad(false, true, &cookies); |
| 525 ASSERT_EQ(2U, cookies.size()); | 532 ASSERT_EQ(2U, cookies.size()); |
| 526 | 533 |
| 527 std::map<std::string, net::CanonicalCookie*> cookie_map; | 534 std::map<std::string, CanonicalCookie*> cookie_map; |
| 528 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 535 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
| 529 it != cookies.end(); | 536 it != cookies.end(); |
| 530 ++it) { | 537 ++it) { |
| 531 cookie_map[(*it)->Name()] = *it; | 538 cookie_map[(*it)->Name()] = *it; |
| 532 } | 539 } |
| 533 | 540 |
| 534 std::map<std::string, net::CanonicalCookie*>::const_iterator it = | 541 std::map<std::string, CanonicalCookie*>::const_iterator it = |
| 535 cookie_map.find(kSessionName); | 542 cookie_map.find(kSessionName); |
| 536 ASSERT_TRUE(it != cookie_map.end()); | 543 ASSERT_TRUE(it != cookie_map.end()); |
| 537 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); | 544 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); |
| 538 | 545 |
| 539 it = cookie_map.find(kPersistentName); | 546 it = cookie_map.find(kPersistentName); |
| 540 ASSERT_TRUE(it != cookie_map.end()); | 547 ASSERT_TRUE(it != cookie_map.end()); |
| 541 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); | 548 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); |
| 542 | 549 |
| 543 STLDeleteElements(&cookies); | 550 STLDeleteElements(&cookies); |
| 544 } | 551 } |
| 545 | 552 |
| 546 TEST_F(SQLitePersistentCookieStoreTest, PriorityIsPersistent) { | 553 TEST_F(SQLitePersistentCookieStoreTest, PriorityIsPersistent) { |
| 547 static const char kLowName[] = "low"; | 554 static const char kLowName[] = "low"; |
| 548 static const char kMediumName[] = "medium"; | 555 static const char kMediumName[] = "medium"; |
| 549 static const char kHighName[] = "high"; | 556 static const char kHighName[] = "high"; |
| 550 static const char kCookieDomain[] = "sessioncookie.com"; | 557 static const char kCookieDomain[] = "sessioncookie.com"; |
| 551 static const char kCookieValue[] = "value"; | 558 static const char kCookieValue[] = "value"; |
| 552 static const char kCookiePath[] = "/"; | 559 static const char kCookiePath[] = "/"; |
| 553 | 560 |
| 554 InitializeStore(false, true); | 561 InitializeStore(false, true); |
| 555 | 562 |
| 556 // Add a low-priority persistent cookie. | 563 // Add a low-priority persistent cookie. |
| 557 store_->AddCookie(net::CanonicalCookie( | 564 store_->AddCookie(CanonicalCookie( |
| 558 GURL(), kLowName, kCookieValue, kCookieDomain, kCookiePath, | 565 GURL(), kLowName, kCookieValue, kCookieDomain, kCookiePath, |
| 559 base::Time::Now() - base::TimeDelta::FromMinutes(1), | 566 base::Time::Now() - base::TimeDelta::FromMinutes(1), |
| 560 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 567 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
| 561 false, false, false, net::COOKIE_PRIORITY_LOW)); | 568 false, false, false, COOKIE_PRIORITY_LOW)); |
| 562 | 569 |
| 563 // Add a medium-priority persistent cookie. | 570 // Add a medium-priority persistent cookie. |
| 564 store_->AddCookie(net::CanonicalCookie( | 571 store_->AddCookie(CanonicalCookie( |
| 565 GURL(), kMediumName, kCookieValue, kCookieDomain, kCookiePath, | 572 GURL(), kMediumName, kCookieValue, kCookieDomain, kCookiePath, |
| 566 base::Time::Now() - base::TimeDelta::FromMinutes(2), | 573 base::Time::Now() - base::TimeDelta::FromMinutes(2), |
| 567 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 574 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
| 568 false, false, false, net::COOKIE_PRIORITY_MEDIUM)); | 575 false, false, false, COOKIE_PRIORITY_MEDIUM)); |
| 569 | 576 |
| 570 // Add a high-priority peristent cookie. | 577 // Add a high-priority peristent cookie. |
| 571 store_->AddCookie(net::CanonicalCookie( | 578 store_->AddCookie(CanonicalCookie( |
| 572 GURL(), kHighName, kCookieValue, kCookieDomain, kCookiePath, | 579 GURL(), kHighName, kCookieValue, kCookieDomain, kCookiePath, |
| 573 base::Time::Now() - base::TimeDelta::FromMinutes(3), | 580 base::Time::Now() - base::TimeDelta::FromMinutes(3), |
| 574 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 581 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
| 575 false, false, false, net::COOKIE_PRIORITY_HIGH)); | 582 false, false, false, COOKIE_PRIORITY_HIGH)); |
| 576 | 583 |
| 577 // Force the store to write its data to the disk. | 584 // Force the store to write its data to the disk. |
| 578 DestroyStore(); | 585 DestroyStore(); |
| 579 | 586 |
| 580 // Create a store that loads session cookie and test that the priority | 587 // Create a store that loads session cookie and test that the priority |
| 581 // attribute values are restored. | 588 // attribute values are restored. |
| 582 CanonicalCookieVector cookies; | 589 CanonicalCookieVector cookies; |
| 583 CreateAndLoad(false, true, &cookies); | 590 CreateAndLoad(false, true, &cookies); |
| 584 ASSERT_EQ(3U, cookies.size()); | 591 ASSERT_EQ(3U, cookies.size()); |
| 585 | 592 |
| 586 // Put the cookies into a map, by name, so we can easily find them. | 593 // Put the cookies into a map, by name, so we can easily find them. |
| 587 std::map<std::string, net::CanonicalCookie*> cookie_map; | 594 std::map<std::string, CanonicalCookie*> cookie_map; |
| 588 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 595 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
| 589 it != cookies.end(); | 596 it != cookies.end(); |
| 590 ++it) { | 597 ++it) { |
| 591 cookie_map[(*it)->Name()] = *it; | 598 cookie_map[(*it)->Name()] = *it; |
| 592 } | 599 } |
| 593 | 600 |
| 594 // Validate that each cookie has the correct priority. | 601 // Validate that each cookie has the correct priority. |
| 595 std::map<std::string, net::CanonicalCookie*>::const_iterator it = | 602 std::map<std::string, CanonicalCookie*>::const_iterator it = |
| 596 cookie_map.find(kLowName); | 603 cookie_map.find(kLowName); |
| 597 ASSERT_TRUE(it != cookie_map.end()); | 604 ASSERT_TRUE(it != cookie_map.end()); |
| 598 EXPECT_EQ(net::COOKIE_PRIORITY_LOW, cookie_map[kLowName]->Priority()); | 605 EXPECT_EQ(COOKIE_PRIORITY_LOW, cookie_map[kLowName]->Priority()); |
| 599 | 606 |
| 600 it = cookie_map.find(kMediumName); | 607 it = cookie_map.find(kMediumName); |
| 601 ASSERT_TRUE(it != cookie_map.end()); | 608 ASSERT_TRUE(it != cookie_map.end()); |
| 602 EXPECT_EQ(net::COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); | 609 EXPECT_EQ(COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); |
| 603 | 610 |
| 604 it = cookie_map.find(kHighName); | 611 it = cookie_map.find(kHighName); |
| 605 ASSERT_TRUE(it != cookie_map.end()); | 612 ASSERT_TRUE(it != cookie_map.end()); |
| 606 EXPECT_EQ(net::COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); | 613 EXPECT_EQ(COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); |
| 607 | 614 |
| 608 STLDeleteElements(&cookies); | 615 STLDeleteElements(&cookies); |
| 609 } | 616 } |
| 610 | 617 |
| 611 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { | 618 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { |
| 612 CanonicalCookieVector cookies; | 619 CanonicalCookieVector cookies; |
| 613 | 620 |
| 614 // Create unencrypted cookie store and write something to it. | 621 // Create unencrypted cookie store and write something to it. |
| 615 InitializeStore(false, false); | 622 InitializeStore(false, false); |
| 616 AddCookie("name", "value123XYZ", "foo.bar", "/", base::Time::Now()); | 623 AddCookie("name", "value123XYZ", "foo.bar", "/", base::Time::Now()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 632 | 639 |
| 633 // Make sure we can update existing cookie and add new cookie as encrypted. | 640 // Make sure we can update existing cookie and add new cookie as encrypted. |
| 634 store_->DeleteCookie(*(cookies_[0])); | 641 store_->DeleteCookie(*(cookies_[0])); |
| 635 AddCookie("name", "encrypted_value123XYZ", "foo.bar", "/", base::Time::Now()); | 642 AddCookie("name", "encrypted_value123XYZ", "foo.bar", "/", base::Time::Now()); |
| 636 AddCookie("other", "something456ABC", "foo.bar", "/", | 643 AddCookie("other", "something456ABC", "foo.bar", "/", |
| 637 base::Time::Now() + base::TimeDelta::FromInternalValue(10)); | 644 base::Time::Now() + base::TimeDelta::FromInternalValue(10)); |
| 638 DestroyStore(); | 645 DestroyStore(); |
| 639 STLDeleteElements(&cookies_); | 646 STLDeleteElements(&cookies_); |
| 640 CreateAndLoad(true, false, &cookies); | 647 CreateAndLoad(true, false, &cookies); |
| 641 EXPECT_EQ(2U, cookies_.size()); | 648 EXPECT_EQ(2U, cookies_.size()); |
| 642 net::CanonicalCookie* cookie_name = NULL; | 649 CanonicalCookie* cookie_name = nullptr; |
| 643 net::CanonicalCookie* cookie_other = NULL; | 650 CanonicalCookie* cookie_other = nullptr; |
| 644 if (cookies_[0]->Name() == "name") { | 651 if (cookies_[0]->Name() == "name") { |
| 645 cookie_name = cookies_[0]; | 652 cookie_name = cookies_[0]; |
| 646 cookie_other = cookies_[1]; | 653 cookie_other = cookies_[1]; |
| 647 } else { | 654 } else { |
| 648 cookie_name = cookies_[1]; | 655 cookie_name = cookies_[1]; |
| 649 cookie_other = cookies_[0]; | 656 cookie_other = cookies_[0]; |
| 650 } | 657 } |
| 651 EXPECT_EQ("encrypted_value123XYZ", cookie_name->Value()); | 658 EXPECT_EQ("encrypted_value123XYZ", cookie_name->Value()); |
| 652 EXPECT_EQ("something456ABC", cookie_other->Value()); | 659 EXPECT_EQ("something456ABC", cookie_other->Value()); |
| 653 DestroyStore(); | 660 DestroyStore(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 671 } | 678 } |
| 672 EXPECT_EQ(2, resultcount); | 679 EXPECT_EQ(2, resultcount); |
| 673 | 680 |
| 674 // Verify that "encrypted_value" is NOT visible in the file. | 681 // Verify that "encrypted_value" is NOT visible in the file. |
| 675 contents = ReadRawDBContents(); | 682 contents = ReadRawDBContents(); |
| 676 EXPECT_NE(0U, contents.length()); | 683 EXPECT_NE(0U, contents.length()); |
| 677 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); | 684 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); |
| 678 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); | 685 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); |
| 679 } | 686 } |
| 680 | 687 |
| 681 } // namespace content | 688 } // namespace net |
| OLD | NEW |