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