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