| 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 net::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; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 173 } | 171 } |
| 174 | 172 |
| 175 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 173 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
| 176 | 174 |
| 177 void TearDown() override { | 175 void TearDown() override { |
| 178 DestroyStore(); | 176 DestroyStore(); |
| 179 pool_owner_->pool()->Shutdown(); | 177 pool_owner_->pool()->Shutdown(); |
| 180 } | 178 } |
| 181 | 179 |
| 182 protected: | 180 protected: |
| 183 base::MessageLoop main_loop_; | |
| 184 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; | 181 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; |
| 185 base::WaitableEvent loaded_event_; | 182 base::WaitableEvent loaded_event_; |
| 186 base::WaitableEvent key_loaded_event_; | 183 base::WaitableEvent key_loaded_event_; |
| 187 base::WaitableEvent db_thread_event_; | 184 base::WaitableEvent db_thread_event_; |
| 188 CanonicalCookieVector cookies_; | 185 CanonicalCookieVector cookies_; |
| 189 base::ScopedTempDir temp_dir_; | 186 base::ScopedTempDir temp_dir_; |
| 190 scoped_refptr<SQLitePersistentCookieStore> store_; | 187 scoped_refptr<SQLitePersistentCookieStore> store_; |
| 191 scoped_ptr<net::CookieCryptoDelegate> cookie_crypto_delegate_; | 188 scoped_ptr<net::CookieCryptoDelegate> cookie_crypto_delegate_; |
| 192 }; | 189 }; |
| 193 | 190 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 t += base::TimeDelta::FromInternalValue(10); | 265 t += base::TimeDelta::FromInternalValue(10); |
| 269 AddCookie("A", "B", "travel.aaa.com", "/", t); | 266 AddCookie("A", "B", "travel.aaa.com", "/", t); |
| 270 t += base::TimeDelta::FromInternalValue(10); | 267 t += base::TimeDelta::FromInternalValue(10); |
| 271 AddCookie("A", "B", "www.bbb.com", "/", t); | 268 AddCookie("A", "B", "www.bbb.com", "/", t); |
| 272 DestroyStore(); | 269 DestroyStore(); |
| 273 | 270 |
| 274 store_ = new SQLitePersistentCookieStore( | 271 store_ = new SQLitePersistentCookieStore( |
| 275 temp_dir_.path().Append(kCookieFilename), | 272 temp_dir_.path().Append(kCookieFilename), |
| 276 client_task_runner(), | 273 client_task_runner(), |
| 277 background_task_runner(), | 274 background_task_runner(), |
| 278 false, NULL, NULL); | 275 false, NULL); |
| 279 | 276 |
| 280 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | 277 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
| 281 // until both Load and LoadCookiesForKey have been posted to its task queue. | 278 // until both Load and LoadCookiesForKey have been posted to its task queue. |
| 282 background_task_runner()->PostTask( | 279 background_task_runner()->PostTask( |
| 283 FROM_HERE, | 280 FROM_HERE, |
| 284 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | 281 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
| 285 base::Unretained(this))); | 282 base::Unretained(this))); |
| 286 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 283 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 287 base::Unretained(this))); | 284 base::Unretained(this))); |
| 288 store_->LoadCookiesForKey("aaa.com", | 285 store_->LoadCookiesForKey("aaa.com", |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 584 } | 581 } |
| 585 EXPECT_EQ(2, resultcount); | 582 EXPECT_EQ(2, resultcount); |
| 586 | 583 |
| 587 // Verify that "encrypted_value" is NOT visible in the file. | 584 // Verify that "encrypted_value" is NOT visible in the file. |
| 588 contents = ReadRawDBContents(); | 585 contents = ReadRawDBContents(); |
| 589 EXPECT_NE(0U, contents.length()); | 586 EXPECT_NE(0U, contents.length()); |
| 590 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); | 587 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); |
| 591 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); | 588 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); |
| 592 } | 589 } |
| 593 | 590 |
| 594 } // namespace content | 591 } // namespace net |
| OLD | NEW |