| 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 26 matching lines...) Expand all Loading... |
| 109 return pool_owner_->pool()->GetSequencedTaskRunner( | 108 return pool_owner_->pool()->GetSequencedTaskRunner( |
| 110 pool_owner_->pool()->GetNamedSequenceToken("background")); | 109 pool_owner_->pool()->GetNamedSequenceToken("background")); |
| 111 } | 110 } |
| 112 | 111 |
| 113 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { | 112 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { |
| 114 return pool_owner_->pool()->GetSequencedTaskRunner( | 113 return pool_owner_->pool()->GetSequencedTaskRunner( |
| 115 pool_owner_->pool()->GetNamedSequenceToken("client")); | 114 pool_owner_->pool()->GetNamedSequenceToken("client")); |
| 116 } | 115 } |
| 117 | 116 |
| 118 void DestroyStore() { | 117 void DestroyStore() { |
| 119 store_ = NULL; | 118 store_ = nullptr; |
| 120 // 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 |
| 121 // resetting the owner (whose destructor blocks on the pool completion). | 120 // resetting the owner (whose destructor blocks on the pool completion). |
| 122 pool_owner_->pool()->Shutdown(); | 121 pool_owner_->pool()->Shutdown(); |
| 123 // 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 |
| 124 // cases this is wasted but harmless. | 123 // cases this is wasted but harmless. |
| 125 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); | 124 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); |
| 126 } | 125 } |
| 127 | 126 |
| 128 void CreateAndLoad(bool crypt_cookies, | 127 void CreateAndLoad(bool crypt_cookies, |
| 129 bool restore_old_session_cookies, | 128 bool restore_old_session_cookies, |
| 130 CanonicalCookieVector* cookies) { | 129 CanonicalCookieVector* cookies) { |
| 131 if (crypt_cookies) | 130 if (crypt_cookies) |
| 132 cookie_crypto_delegate_.reset(new CookieCryptor()); | 131 cookie_crypto_delegate_.reset(new CookieCryptor()); |
| 133 | 132 |
| 134 store_ = new SQLitePersistentCookieStore( | 133 store_ = new SQLitePersistentCookieStore( |
| 135 temp_dir_.path().Append(kCookieFilename), | 134 temp_dir_.path().Append(kCookieFilename), |
| 136 client_task_runner(), | 135 client_task_runner(), |
| 137 background_task_runner(), | 136 background_task_runner(), |
| 138 restore_old_session_cookies, | 137 restore_old_session_cookies, |
| 139 NULL, | |
| 140 cookie_crypto_delegate_.get()); | 138 cookie_crypto_delegate_.get()); |
| 141 Load(cookies); | 139 Load(cookies); |
| 142 } | 140 } |
| 143 | 141 |
| 144 void InitializeStore(bool crypt, bool restore_old_session_cookies) { | 142 void InitializeStore(bool crypt, bool restore_old_session_cookies) { |
| 145 CanonicalCookieVector cookies; | 143 CanonicalCookieVector cookies; |
| 146 CreateAndLoad(crypt, restore_old_session_cookies, &cookies); | 144 CreateAndLoad(crypt, restore_old_session_cookies, &cookies); |
| 147 EXPECT_EQ(0U, cookies.size()); | 145 EXPECT_EQ(0U, cookies.size()); |
| 148 } | 146 } |
| 149 | 147 |
| 150 // 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 |
| 151 // bind a non-void returning method as a Closure. | 149 // bind a non-void returning method as a Closure. |
| 152 void WaitOnDBEvent() { | 150 void WaitOnDBEvent() { |
| 153 db_thread_event_.Wait(); | 151 db_thread_event_.Wait(); |
| 154 } | 152 } |
| 155 | 153 |
| 156 // Adds a persistent cookie to store_. | 154 // Adds a persistent cookie to store_. |
| 157 void AddCookie(const std::string& name, | 155 void AddCookie(const std::string& name, |
| 158 const std::string& value, | 156 const std::string& value, |
| 159 const std::string& domain, | 157 const std::string& domain, |
| 160 const std::string& path, | 158 const std::string& path, |
| 161 const base::Time& creation) { | 159 const base::Time& creation) { |
| 162 store_->AddCookie(net::CanonicalCookie( | 160 store_->AddCookie(CanonicalCookie( |
| 163 GURL(), name, value, domain, path, creation, creation, creation, false, | 161 GURL(), name, value, domain, path, creation, creation, creation, false, |
| 164 false, false, net::COOKIE_PRIORITY_DEFAULT)); | 162 false, false, COOKIE_PRIORITY_DEFAULT)); |
| 165 } | 163 } |
| 166 | 164 |
| 167 void AddCookieWithExpiration(const std::string& name, | 165 void AddCookieWithExpiration(const std::string& name, |
| 168 const std::string& value, | 166 const std::string& value, |
| 169 const std::string& domain, | 167 const std::string& domain, |
| 170 const std::string& path, | 168 const std::string& path, |
| 171 const base::Time& creation, | 169 const base::Time& creation, |
| 172 const base::Time& expiration) { | 170 const base::Time& expiration) { |
| 173 store_->AddCookie(net::CanonicalCookie( | 171 store_->AddCookie(CanonicalCookie( |
| 174 GURL(), name, value, domain, path, creation, expiration, creation, | 172 GURL(), name, value, domain, path, creation, expiration, creation, |
| 175 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); | 173 false, false, false, COOKIE_PRIORITY_DEFAULT)); |
| 176 } | 174 } |
| 177 | 175 |
| 178 std::string ReadRawDBContents() { | 176 std::string ReadRawDBContents() { |
| 179 std::string contents; | 177 std::string contents; |
| 180 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), | 178 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), |
| 181 &contents)) | 179 &contents)) |
| 182 return std::string(); | 180 return std::string(); |
| 183 return contents; | 181 return contents; |
| 184 } | 182 } |
| 185 | 183 |
| 186 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 184 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
| 187 | 185 |
| 188 void TearDown() override { | 186 void TearDown() override { |
| 189 DestroyStore(); | 187 DestroyStore(); |
| 190 pool_owner_->pool()->Shutdown(); | 188 pool_owner_->pool()->Shutdown(); |
| 191 } | 189 } |
| 192 | 190 |
| 193 protected: | 191 protected: |
| 194 base::MessageLoop main_loop_; | |
| 195 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; | 192 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; |
| 196 base::WaitableEvent loaded_event_; | 193 base::WaitableEvent loaded_event_; |
| 197 base::WaitableEvent key_loaded_event_; | 194 base::WaitableEvent key_loaded_event_; |
| 198 base::WaitableEvent db_thread_event_; | 195 base::WaitableEvent db_thread_event_; |
| 199 CanonicalCookieVector cookies_; | 196 CanonicalCookieVector cookies_; |
| 200 base::ScopedTempDir temp_dir_; | 197 base::ScopedTempDir temp_dir_; |
| 201 scoped_refptr<SQLitePersistentCookieStore> store_; | 198 scoped_refptr<SQLitePersistentCookieStore> store_; |
| 202 scoped_ptr<net::CookieCryptoDelegate> cookie_crypto_delegate_; | 199 scoped_ptr<CookieCryptoDelegate> cookie_crypto_delegate_; |
| 203 }; | 200 }; |
| 204 | 201 |
| 205 TEST_F(SQLitePersistentCookieStoreTest, TestInvalidMetaTableRecovery) { | 202 TEST_F(SQLitePersistentCookieStoreTest, TestInvalidMetaTableRecovery) { |
| 206 InitializeStore(false, false); | 203 InitializeStore(false, false); |
| 207 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); | 204 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); |
| 208 DestroyStore(); | 205 DestroyStore(); |
| 209 | 206 |
| 210 // Load up the store and verify that it has good data in it. | 207 // Load up the store and verify that it has good data in it. |
| 211 CanonicalCookieVector cookies; | 208 CanonicalCookieVector cookies; |
| 212 CreateAndLoad(false, false, &cookies); | 209 CreateAndLoad(false, false, &cookies); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 t += base::TimeDelta::FromInternalValue(10); | 290 t += base::TimeDelta::FromInternalValue(10); |
| 294 AddCookieWithExpiration("A", "B", "b5.com", "/", t, base::Time()); | 291 AddCookieWithExpiration("A", "B", "b5.com", "/", t, base::Time()); |
| 295 DestroyStore(); | 292 DestroyStore(); |
| 296 | 293 |
| 297 // Load the store a second time. Before the store finishes loading, add a | 294 // Load the store a second time. Before the store finishes loading, add a |
| 298 // transient cookie and flush it to disk. | 295 // transient cookie and flush it to disk. |
| 299 store_ = new SQLitePersistentCookieStore( | 296 store_ = new SQLitePersistentCookieStore( |
| 300 temp_dir_.path().Append(kCookieFilename), | 297 temp_dir_.path().Append(kCookieFilename), |
| 301 client_task_runner(), | 298 client_task_runner(), |
| 302 background_task_runner(), | 299 background_task_runner(), |
| 303 false, NULL, NULL); | 300 false, nullptr); |
| 304 | 301 |
| 305 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | 302 // 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. | 303 // until both Load and Flush have been posted to its task queue. |
| 307 background_task_runner()->PostTask( | 304 background_task_runner()->PostTask( |
| 308 FROM_HERE, | 305 FROM_HERE, |
| 309 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | 306 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
| 310 base::Unretained(this))); | 307 base::Unretained(this))); |
| 311 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 308 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 312 base::Unretained(this))); | 309 base::Unretained(this))); |
| 313 t += base::TimeDelta::FromInternalValue(10); | 310 t += base::TimeDelta::FromInternalValue(10); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 329 STLDeleteElements(&cookies_); | 326 STLDeleteElements(&cookies_); |
| 330 DestroyStore(); | 327 DestroyStore(); |
| 331 | 328 |
| 332 // Load the store a third time, this time restoring session cookies. The | 329 // 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", | 330 // store should contain exactly 4 cookies: the 3 persistent, and "c.com", |
| 334 // which was added during the second cookie store load. | 331 // which was added during the second cookie store load. |
| 335 store_ = new SQLitePersistentCookieStore( | 332 store_ = new SQLitePersistentCookieStore( |
| 336 temp_dir_.path().Append(kCookieFilename), | 333 temp_dir_.path().Append(kCookieFilename), |
| 337 client_task_runner(), | 334 client_task_runner(), |
| 338 background_task_runner(), | 335 background_task_runner(), |
| 339 true, NULL, NULL); | 336 true, nullptr); |
| 340 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 337 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 341 base::Unretained(this))); | 338 base::Unretained(this))); |
| 342 loaded_event_.Wait(); | 339 loaded_event_.Wait(); |
| 343 ASSERT_EQ(4u, cookies_.size()); | 340 ASSERT_EQ(4u, cookies_.size()); |
| 344 STLDeleteElements(&cookies_); | 341 STLDeleteElements(&cookies_); |
| 345 } | 342 } |
| 346 | 343 |
| 347 // Test that priority load of cookies for a specfic domain key could be | 344 // Test that priority load of cookies for a specfic domain key could be |
| 348 // completed before the entire store is loaded | 345 // completed before the entire store is loaded |
| 349 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | 346 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
| 350 InitializeStore(false, false); | 347 InitializeStore(false, false); |
| 351 base::Time t = base::Time::Now(); | 348 base::Time t = base::Time::Now(); |
| 352 AddCookie("A", "B", "foo.bar", "/", t); | 349 AddCookie("A", "B", "foo.bar", "/", t); |
| 353 t += base::TimeDelta::FromInternalValue(10); | 350 t += base::TimeDelta::FromInternalValue(10); |
| 354 AddCookie("A", "B", "www.aaa.com", "/", t); | 351 AddCookie("A", "B", "www.aaa.com", "/", t); |
| 355 t += base::TimeDelta::FromInternalValue(10); | 352 t += base::TimeDelta::FromInternalValue(10); |
| 356 AddCookie("A", "B", "travel.aaa.com", "/", t); | 353 AddCookie("A", "B", "travel.aaa.com", "/", t); |
| 357 t += base::TimeDelta::FromInternalValue(10); | 354 t += base::TimeDelta::FromInternalValue(10); |
| 358 AddCookie("A", "B", "www.bbb.com", "/", t); | 355 AddCookie("A", "B", "www.bbb.com", "/", t); |
| 359 DestroyStore(); | 356 DestroyStore(); |
| 360 | 357 |
| 361 store_ = new SQLitePersistentCookieStore( | 358 store_ = new SQLitePersistentCookieStore( |
| 362 temp_dir_.path().Append(kCookieFilename), | 359 temp_dir_.path().Append(kCookieFilename), |
| 363 client_task_runner(), | 360 client_task_runner(), |
| 364 background_task_runner(), | 361 background_task_runner(), |
| 365 false, NULL, NULL); | 362 false, nullptr); |
| 366 | 363 |
| 367 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | 364 // 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. | 365 // until both Load and LoadCookiesForKey have been posted to its task queue. |
| 369 background_task_runner()->PostTask( | 366 background_task_runner()->PostTask( |
| 370 FROM_HERE, | 367 FROM_HERE, |
| 371 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | 368 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
| 372 base::Unretained(this))); | 369 base::Unretained(this))); |
| 373 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 370 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 374 base::Unretained(this))); | 371 base::Unretained(this))); |
| 375 store_->LoadCookiesForKey("aaa.com", | 372 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. | 437 // We forced a write, so now the file will be bigger. |
| 441 ASSERT_TRUE(base::GetFileInfo(path, &info)); | 438 ASSERT_TRUE(base::GetFileInfo(path, &info)); |
| 442 ASSERT_GT(info.size, base_size); | 439 ASSERT_GT(info.size, base_size); |
| 443 } | 440 } |
| 444 | 441 |
| 445 // Test loading old session cookies from the disk. | 442 // Test loading old session cookies from the disk. |
| 446 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 443 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
| 447 InitializeStore(false, true); | 444 InitializeStore(false, true); |
| 448 | 445 |
| 449 // Add a session cookie. | 446 // Add a session cookie. |
| 450 store_->AddCookie(net::CanonicalCookie(GURL(), "C", "D", "sessioncookie.com", | 447 store_->AddCookie(CanonicalCookie(GURL(), "C", "D", "sessioncookie.com", |
| 451 "/", base::Time::Now(), base::Time(), | 448 "/", base::Time::Now(), base::Time(), |
| 452 base::Time::Now(), false, false, false, | 449 base::Time::Now(), false, false, false, |
| 453 net::COOKIE_PRIORITY_DEFAULT)); | 450 COOKIE_PRIORITY_DEFAULT)); |
| 454 | 451 |
| 455 // Force the store to write its data to the disk. | 452 // Force the store to write its data to the disk. |
| 456 DestroyStore(); | 453 DestroyStore(); |
| 457 | 454 |
| 458 // Create a store that loads session cookies and test that the session cookie | 455 // Create a store that loads session cookies and test that the session cookie |
| 459 // was loaded. | 456 // was loaded. |
| 460 CanonicalCookieVector cookies; | 457 CanonicalCookieVector cookies; |
| 461 CreateAndLoad(false, true, &cookies); | 458 CreateAndLoad(false, true, &cookies); |
| 462 | 459 |
| 463 ASSERT_EQ(1U, cookies.size()); | 460 ASSERT_EQ(1U, cookies.size()); |
| 464 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 461 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
| 465 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 462 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
| 466 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 463 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
| 467 ASSERT_EQ(net::COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); | 464 ASSERT_EQ(COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); |
| 468 | 465 |
| 469 STLDeleteElements(&cookies); | 466 STLDeleteElements(&cookies); |
| 470 } | 467 } |
| 471 | 468 |
| 472 // Test loading old session cookies from the disk. | 469 // Test loading old session cookies from the disk. |
| 473 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 470 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
| 474 InitializeStore(false, true); | 471 InitializeStore(false, true); |
| 475 | 472 |
| 476 // Add a session cookie. | 473 // Add a session cookie. |
| 477 store_->AddCookie(net::CanonicalCookie(GURL(), "C", "D", "sessioncookie.com", | 474 store_->AddCookie(CanonicalCookie(GURL(), "C", "D", "sessioncookie.com", |
| 478 "/", base::Time::Now(), base::Time(), | 475 "/", base::Time::Now(), base::Time(), |
| 479 base::Time::Now(), false, false, false, | 476 base::Time::Now(), false, false, false, |
| 480 net::COOKIE_PRIORITY_DEFAULT)); | 477 COOKIE_PRIORITY_DEFAULT)); |
| 481 | 478 |
| 482 // Force the store to write its data to the disk. | 479 // Force the store to write its data to the disk. |
| 483 DestroyStore(); | 480 DestroyStore(); |
| 484 | 481 |
| 485 // Create a store that doesn't load old session cookies and test that the | 482 // Create a store that doesn't load old session cookies and test that the |
| 486 // session cookie was not loaded. | 483 // session cookie was not loaded. |
| 487 CanonicalCookieVector cookies; | 484 CanonicalCookieVector cookies; |
| 488 CreateAndLoad(false, false, &cookies); | 485 CreateAndLoad(false, false, &cookies); |
| 489 ASSERT_EQ(0U, cookies.size()); | 486 ASSERT_EQ(0U, cookies.size()); |
| 490 | 487 |
| 491 // The store should also delete the session cookie. Wait until that has been | 488 // The store should also delete the session cookie. Wait until that has been |
| 492 // done. | 489 // done. |
| 493 DestroyStore(); | 490 DestroyStore(); |
| 494 | 491 |
| 495 // Create a store that loads old session cookies and test that the session | 492 // Create a store that loads old session cookies and test that the session |
| 496 // cookie is gone. | 493 // cookie is gone. |
| 497 CreateAndLoad(false, true, &cookies); | 494 CreateAndLoad(false, true, &cookies); |
| 498 ASSERT_EQ(0U, cookies.size()); | 495 ASSERT_EQ(0U, cookies.size()); |
| 499 } | 496 } |
| 500 | 497 |
| 501 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { | 498 TEST_F(SQLitePersistentCookieStoreTest, PersistIsPersistent) { |
| 502 InitializeStore(false, true); | 499 InitializeStore(false, true); |
| 503 static const char kSessionName[] = "session"; | 500 static const char kSessionName[] = "session"; |
| 504 static const char kPersistentName[] = "persistent"; | 501 static const char kPersistentName[] = "persistent"; |
| 505 | 502 |
| 506 // Add a session cookie. | 503 // Add a session cookie. |
| 507 store_->AddCookie(net::CanonicalCookie( | 504 store_->AddCookie(CanonicalCookie( |
| 508 GURL(), kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), | 505 GURL(), kSessionName, "val", "sessioncookie.com", "/", base::Time::Now(), |
| 509 base::Time(), base::Time::Now(), false, false, false, | 506 base::Time(), base::Time::Now(), false, false, false, |
| 510 net::COOKIE_PRIORITY_DEFAULT)); | 507 COOKIE_PRIORITY_DEFAULT)); |
| 511 // Add a persistent cookie. | 508 // Add a persistent cookie. |
| 512 store_->AddCookie(net::CanonicalCookie( | 509 store_->AddCookie(CanonicalCookie( |
| 513 GURL(), kPersistentName, "val", "sessioncookie.com", "/", | 510 GURL(), kPersistentName, "val", "sessioncookie.com", "/", |
| 514 base::Time::Now() - base::TimeDelta::FromDays(1), | 511 base::Time::Now() - base::TimeDelta::FromDays(1), |
| 515 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 512 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
| 516 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); | 513 false, false, false, COOKIE_PRIORITY_DEFAULT)); |
| 517 | 514 |
| 518 // Force the store to write its data to the disk. | 515 // Force the store to write its data to the disk. |
| 519 DestroyStore(); | 516 DestroyStore(); |
| 520 | 517 |
| 521 // Create a store that loads session cookie and test that the IsPersistent | 518 // Create a store that loads session cookie and test that the IsPersistent |
| 522 // attribute is restored. | 519 // attribute is restored. |
| 523 CanonicalCookieVector cookies; | 520 CanonicalCookieVector cookies; |
| 524 CreateAndLoad(false, true, &cookies); | 521 CreateAndLoad(false, true, &cookies); |
| 525 ASSERT_EQ(2U, cookies.size()); | 522 ASSERT_EQ(2U, cookies.size()); |
| 526 | 523 |
| 527 std::map<std::string, net::CanonicalCookie*> cookie_map; | 524 std::map<std::string, CanonicalCookie*> cookie_map; |
| 528 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 525 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
| 529 it != cookies.end(); | 526 it != cookies.end(); |
| 530 ++it) { | 527 ++it) { |
| 531 cookie_map[(*it)->Name()] = *it; | 528 cookie_map[(*it)->Name()] = *it; |
| 532 } | 529 } |
| 533 | 530 |
| 534 std::map<std::string, net::CanonicalCookie*>::const_iterator it = | 531 std::map<std::string, CanonicalCookie*>::const_iterator it = |
| 535 cookie_map.find(kSessionName); | 532 cookie_map.find(kSessionName); |
| 536 ASSERT_TRUE(it != cookie_map.end()); | 533 ASSERT_TRUE(it != cookie_map.end()); |
| 537 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); | 534 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); |
| 538 | 535 |
| 539 it = cookie_map.find(kPersistentName); | 536 it = cookie_map.find(kPersistentName); |
| 540 ASSERT_TRUE(it != cookie_map.end()); | 537 ASSERT_TRUE(it != cookie_map.end()); |
| 541 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); | 538 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); |
| 542 | 539 |
| 543 STLDeleteElements(&cookies); | 540 STLDeleteElements(&cookies); |
| 544 } | 541 } |
| 545 | 542 |
| 546 TEST_F(SQLitePersistentCookieStoreTest, PriorityIsPersistent) { | 543 TEST_F(SQLitePersistentCookieStoreTest, PriorityIsPersistent) { |
| 547 static const char kLowName[] = "low"; | 544 static const char kLowName[] = "low"; |
| 548 static const char kMediumName[] = "medium"; | 545 static const char kMediumName[] = "medium"; |
| 549 static const char kHighName[] = "high"; | 546 static const char kHighName[] = "high"; |
| 550 static const char kCookieDomain[] = "sessioncookie.com"; | 547 static const char kCookieDomain[] = "sessioncookie.com"; |
| 551 static const char kCookieValue[] = "value"; | 548 static const char kCookieValue[] = "value"; |
| 552 static const char kCookiePath[] = "/"; | 549 static const char kCookiePath[] = "/"; |
| 553 | 550 |
| 554 InitializeStore(false, true); | 551 InitializeStore(false, true); |
| 555 | 552 |
| 556 // Add a low-priority persistent cookie. | 553 // Add a low-priority persistent cookie. |
| 557 store_->AddCookie(net::CanonicalCookie( | 554 store_->AddCookie(CanonicalCookie( |
| 558 GURL(), kLowName, kCookieValue, kCookieDomain, kCookiePath, | 555 GURL(), kLowName, kCookieValue, kCookieDomain, kCookiePath, |
| 559 base::Time::Now() - base::TimeDelta::FromMinutes(1), | 556 base::Time::Now() - base::TimeDelta::FromMinutes(1), |
| 560 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 557 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
| 561 false, false, false, net::COOKIE_PRIORITY_LOW)); | 558 false, false, false, COOKIE_PRIORITY_LOW)); |
| 562 | 559 |
| 563 // Add a medium-priority persistent cookie. | 560 // Add a medium-priority persistent cookie. |
| 564 store_->AddCookie(net::CanonicalCookie( | 561 store_->AddCookie(CanonicalCookie( |
| 565 GURL(), kMediumName, kCookieValue, kCookieDomain, kCookiePath, | 562 GURL(), kMediumName, kCookieValue, kCookieDomain, kCookiePath, |
| 566 base::Time::Now() - base::TimeDelta::FromMinutes(2), | 563 base::Time::Now() - base::TimeDelta::FromMinutes(2), |
| 567 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 564 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
| 568 false, false, false, net::COOKIE_PRIORITY_MEDIUM)); | 565 false, false, false, COOKIE_PRIORITY_MEDIUM)); |
| 569 | 566 |
| 570 // Add a high-priority peristent cookie. | 567 // Add a high-priority peristent cookie. |
| 571 store_->AddCookie(net::CanonicalCookie( | 568 store_->AddCookie(CanonicalCookie( |
| 572 GURL(), kHighName, kCookieValue, kCookieDomain, kCookiePath, | 569 GURL(), kHighName, kCookieValue, kCookieDomain, kCookiePath, |
| 573 base::Time::Now() - base::TimeDelta::FromMinutes(3), | 570 base::Time::Now() - base::TimeDelta::FromMinutes(3), |
| 574 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), | 571 base::Time::Now() + base::TimeDelta::FromDays(1), base::Time::Now(), |
| 575 false, false, false, net::COOKIE_PRIORITY_HIGH)); | 572 false, false, false, COOKIE_PRIORITY_HIGH)); |
| 576 | 573 |
| 577 // Force the store to write its data to the disk. | 574 // Force the store to write its data to the disk. |
| 578 DestroyStore(); | 575 DestroyStore(); |
| 579 | 576 |
| 580 // Create a store that loads session cookie and test that the priority | 577 // Create a store that loads session cookie and test that the priority |
| 581 // attribute values are restored. | 578 // attribute values are restored. |
| 582 CanonicalCookieVector cookies; | 579 CanonicalCookieVector cookies; |
| 583 CreateAndLoad(false, true, &cookies); | 580 CreateAndLoad(false, true, &cookies); |
| 584 ASSERT_EQ(3U, cookies.size()); | 581 ASSERT_EQ(3U, cookies.size()); |
| 585 | 582 |
| 586 // Put the cookies into a map, by name, so we can easily find them. | 583 // Put the cookies into a map, by name, so we can easily find them. |
| 587 std::map<std::string, net::CanonicalCookie*> cookie_map; | 584 std::map<std::string, CanonicalCookie*> cookie_map; |
| 588 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 585 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
| 589 it != cookies.end(); | 586 it != cookies.end(); |
| 590 ++it) { | 587 ++it) { |
| 591 cookie_map[(*it)->Name()] = *it; | 588 cookie_map[(*it)->Name()] = *it; |
| 592 } | 589 } |
| 593 | 590 |
| 594 // Validate that each cookie has the correct priority. | 591 // Validate that each cookie has the correct priority. |
| 595 std::map<std::string, net::CanonicalCookie*>::const_iterator it = | 592 std::map<std::string, CanonicalCookie*>::const_iterator it = |
| 596 cookie_map.find(kLowName); | 593 cookie_map.find(kLowName); |
| 597 ASSERT_TRUE(it != cookie_map.end()); | 594 ASSERT_TRUE(it != cookie_map.end()); |
| 598 EXPECT_EQ(net::COOKIE_PRIORITY_LOW, cookie_map[kLowName]->Priority()); | 595 EXPECT_EQ(COOKIE_PRIORITY_LOW, cookie_map[kLowName]->Priority()); |
| 599 | 596 |
| 600 it = cookie_map.find(kMediumName); | 597 it = cookie_map.find(kMediumName); |
| 601 ASSERT_TRUE(it != cookie_map.end()); | 598 ASSERT_TRUE(it != cookie_map.end()); |
| 602 EXPECT_EQ(net::COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); | 599 EXPECT_EQ(COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); |
| 603 | 600 |
| 604 it = cookie_map.find(kHighName); | 601 it = cookie_map.find(kHighName); |
| 605 ASSERT_TRUE(it != cookie_map.end()); | 602 ASSERT_TRUE(it != cookie_map.end()); |
| 606 EXPECT_EQ(net::COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); | 603 EXPECT_EQ(COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); |
| 607 | 604 |
| 608 STLDeleteElements(&cookies); | 605 STLDeleteElements(&cookies); |
| 609 } | 606 } |
| 610 | 607 |
| 611 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { | 608 TEST_F(SQLitePersistentCookieStoreTest, UpdateToEncryption) { |
| 612 CanonicalCookieVector cookies; | 609 CanonicalCookieVector cookies; |
| 613 | 610 |
| 614 // Create unencrypted cookie store and write something to it. | 611 // Create unencrypted cookie store and write something to it. |
| 615 InitializeStore(false, false); | 612 InitializeStore(false, false); |
| 616 AddCookie("name", "value123XYZ", "foo.bar", "/", base::Time::Now()); | 613 AddCookie("name", "value123XYZ", "foo.bar", "/", base::Time::Now()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 632 | 629 |
| 633 // Make sure we can update existing cookie and add new cookie as encrypted. | 630 // Make sure we can update existing cookie and add new cookie as encrypted. |
| 634 store_->DeleteCookie(*(cookies_[0])); | 631 store_->DeleteCookie(*(cookies_[0])); |
| 635 AddCookie("name", "encrypted_value123XYZ", "foo.bar", "/", base::Time::Now()); | 632 AddCookie("name", "encrypted_value123XYZ", "foo.bar", "/", base::Time::Now()); |
| 636 AddCookie("other", "something456ABC", "foo.bar", "/", | 633 AddCookie("other", "something456ABC", "foo.bar", "/", |
| 637 base::Time::Now() + base::TimeDelta::FromInternalValue(10)); | 634 base::Time::Now() + base::TimeDelta::FromInternalValue(10)); |
| 638 DestroyStore(); | 635 DestroyStore(); |
| 639 STLDeleteElements(&cookies_); | 636 STLDeleteElements(&cookies_); |
| 640 CreateAndLoad(true, false, &cookies); | 637 CreateAndLoad(true, false, &cookies); |
| 641 EXPECT_EQ(2U, cookies_.size()); | 638 EXPECT_EQ(2U, cookies_.size()); |
| 642 net::CanonicalCookie* cookie_name = NULL; | 639 CanonicalCookie* cookie_name = nullptr; |
| 643 net::CanonicalCookie* cookie_other = NULL; | 640 CanonicalCookie* cookie_other = nullptr; |
| 644 if (cookies_[0]->Name() == "name") { | 641 if (cookies_[0]->Name() == "name") { |
| 645 cookie_name = cookies_[0]; | 642 cookie_name = cookies_[0]; |
| 646 cookie_other = cookies_[1]; | 643 cookie_other = cookies_[1]; |
| 647 } else { | 644 } else { |
| 648 cookie_name = cookies_[1]; | 645 cookie_name = cookies_[1]; |
| 649 cookie_other = cookies_[0]; | 646 cookie_other = cookies_[0]; |
| 650 } | 647 } |
| 651 EXPECT_EQ("encrypted_value123XYZ", cookie_name->Value()); | 648 EXPECT_EQ("encrypted_value123XYZ", cookie_name->Value()); |
| 652 EXPECT_EQ("something456ABC", cookie_other->Value()); | 649 EXPECT_EQ("something456ABC", cookie_other->Value()); |
| 653 DestroyStore(); | 650 DestroyStore(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 671 } | 668 } |
| 672 EXPECT_EQ(2, resultcount); | 669 EXPECT_EQ(2, resultcount); |
| 673 | 670 |
| 674 // Verify that "encrypted_value" is NOT visible in the file. | 671 // Verify that "encrypted_value" is NOT visible in the file. |
| 675 contents = ReadRawDBContents(); | 672 contents = ReadRawDBContents(); |
| 676 EXPECT_NE(0U, contents.length()); | 673 EXPECT_NE(0U, contents.length()); |
| 677 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); | 674 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); |
| 678 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); | 675 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); |
| 679 } | 676 } |
| 680 | 677 |
| 681 } // namespace content | 678 } // namespace net |
| OLD | NEW |