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 "net/extras/sqlite/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" |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 // Create a new pool for the few tests that create multiple stores. In other | 137 // Create a new pool for the few tests that create multiple stores. In other |
138 // cases this is wasted but harmless. | 138 // cases this is wasted but harmless. |
139 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); | 139 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); |
140 } | 140 } |
141 | 141 |
142 void Create(bool crypt_cookies, bool restore_old_session_cookies) { | 142 void Create(bool crypt_cookies, bool restore_old_session_cookies) { |
143 if (crypt_cookies) | 143 if (crypt_cookies) |
144 cookie_crypto_delegate_.reset(new CookieCryptor()); | 144 cookie_crypto_delegate_.reset(new CookieCryptor()); |
145 | 145 |
146 store_ = new SQLitePersistentCookieStore( | 146 store_ = new SQLitePersistentCookieStore( |
147 temp_dir_.path().Append(kCookieFilename), client_task_runner(), | 147 temp_dir_.GetPath().Append(kCookieFilename), client_task_runner(), |
148 background_task_runner(), restore_old_session_cookies, | 148 background_task_runner(), restore_old_session_cookies, |
149 cookie_crypto_delegate_.get()); | 149 cookie_crypto_delegate_.get()); |
150 } | 150 } |
151 | 151 |
152 void CreateAndLoad(bool crypt_cookies, | 152 void CreateAndLoad(bool crypt_cookies, |
153 bool restore_old_session_cookies, | 153 bool restore_old_session_cookies, |
154 CanonicalCookieVector* cookies) { | 154 CanonicalCookieVector* cookies) { |
155 Create(crypt_cookies, restore_old_session_cookies); | 155 Create(crypt_cookies, restore_old_session_cookies); |
156 Load(cookies); | 156 Load(cookies); |
157 } | 157 } |
(...skipping 27 matching lines...) Expand all Loading... |
185 const std::string& path, | 185 const std::string& path, |
186 const base::Time& creation, | 186 const base::Time& creation, |
187 const base::Time& expiration) { | 187 const base::Time& expiration) { |
188 store_->AddCookie(*CanonicalCookie::Create( | 188 store_->AddCookie(*CanonicalCookie::Create( |
189 url, name, value, domain, path, creation, expiration, false, false, | 189 url, name, value, domain, path, creation, expiration, false, false, |
190 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_DEFAULT)); | 190 CookieSameSite::DEFAULT_MODE, false, COOKIE_PRIORITY_DEFAULT)); |
191 } | 191 } |
192 | 192 |
193 std::string ReadRawDBContents() { | 193 std::string ReadRawDBContents() { |
194 std::string contents; | 194 std::string contents; |
195 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), | 195 if (!base::ReadFileToString(temp_dir_.GetPath().Append(kCookieFilename), |
196 &contents)) | 196 &contents)) |
197 return std::string(); | 197 return std::string(); |
198 return contents; | 198 return contents; |
199 } | 199 } |
200 | 200 |
201 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 201 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
202 | 202 |
203 void TearDown() override { | 203 void TearDown() override { |
204 DestroyStore(); | 204 DestroyStore(); |
205 } | 205 } |
(...skipping 21 matching lines...) Expand all Loading... |
227 ASSERT_EQ(1U, cookies.size()); | 227 ASSERT_EQ(1U, cookies.size()); |
228 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); | 228 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); |
229 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 229 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
230 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 230 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
231 DestroyStore(); | 231 DestroyStore(); |
232 base::STLDeleteElements(&cookies); | 232 base::STLDeleteElements(&cookies); |
233 | 233 |
234 // Now corrupt the meta table. | 234 // Now corrupt the meta table. |
235 { | 235 { |
236 sql::Connection db; | 236 sql::Connection db; |
237 ASSERT_TRUE(db.Open(temp_dir_.path().Append(kCookieFilename))); | 237 ASSERT_TRUE(db.Open(temp_dir_.GetPath().Append(kCookieFilename))); |
238 sql::MetaTable meta_table_; | 238 sql::MetaTable meta_table_; |
239 meta_table_.Init(&db, 1, 1); | 239 meta_table_.Init(&db, 1, 1); |
240 ASSERT_TRUE(db.Execute("DELETE FROM meta")); | 240 ASSERT_TRUE(db.Execute("DELETE FROM meta")); |
241 db.Close(); | 241 db.Close(); |
242 } | 242 } |
243 | 243 |
244 // Upon loading, the database should be reset to a good, blank state. | 244 // Upon loading, the database should be reset to a good, blank state. |
245 CreateAndLoad(false, false, &cookies); | 245 CreateAndLoad(false, false, &cookies); |
246 ASSERT_EQ(0U, cookies.size()); | 246 ASSERT_EQ(0U, cookies.size()); |
247 | 247 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 AddCookieWithExpiration(GURL("http://b4.com"), "A", "B", std::string(), "/", | 311 AddCookieWithExpiration(GURL("http://b4.com"), "A", "B", std::string(), "/", |
312 t, base::Time()); | 312 t, base::Time()); |
313 t += base::TimeDelta::FromInternalValue(10); | 313 t += base::TimeDelta::FromInternalValue(10); |
314 AddCookieWithExpiration(GURL("http://b5.com"), "A", "B", std::string(), "/", | 314 AddCookieWithExpiration(GURL("http://b5.com"), "A", "B", std::string(), "/", |
315 t, base::Time()); | 315 t, base::Time()); |
316 DestroyStore(); | 316 DestroyStore(); |
317 | 317 |
318 // Load the store a second time. Before the store finishes loading, add a | 318 // Load the store a second time. Before the store finishes loading, add a |
319 // transient cookie and flush it to disk. | 319 // transient cookie and flush it to disk. |
320 store_ = new SQLitePersistentCookieStore( | 320 store_ = new SQLitePersistentCookieStore( |
321 temp_dir_.path().Append(kCookieFilename), client_task_runner(), | 321 temp_dir_.GetPath().Append(kCookieFilename), client_task_runner(), |
322 background_task_runner(), false, nullptr); | 322 background_task_runner(), false, nullptr); |
323 | 323 |
324 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | 324 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
325 // until both Load and Flush have been posted to its task queue. | 325 // until both Load and Flush have been posted to its task queue. |
326 background_task_runner()->PostTask( | 326 background_task_runner()->PostTask( |
327 FROM_HERE, base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | 327 FROM_HERE, base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
328 base::Unretained(this))); | 328 base::Unretained(this))); |
329 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 329 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
330 base::Unretained(this))); | 330 base::Unretained(this))); |
331 t += base::TimeDelta::FromInternalValue(10); | 331 t += base::TimeDelta::FromInternalValue(10); |
(...skipping 14 matching lines...) Expand all Loading... |
346 db_thread_event_.Signal(); | 346 db_thread_event_.Signal(); |
347 event.Wait(); | 347 event.Wait(); |
348 loaded_event_.Wait(); | 348 loaded_event_.Wait(); |
349 base::STLDeleteElements(&cookies_); | 349 base::STLDeleteElements(&cookies_); |
350 DestroyStore(); | 350 DestroyStore(); |
351 | 351 |
352 // Load the store a third time, this time restoring session cookies. The | 352 // Load the store a third time, this time restoring session cookies. The |
353 // store should contain exactly 4 cookies: the 3 persistent, and "c.com", | 353 // store should contain exactly 4 cookies: the 3 persistent, and "c.com", |
354 // which was added during the second cookie store load. | 354 // which was added during the second cookie store load. |
355 store_ = new SQLitePersistentCookieStore( | 355 store_ = new SQLitePersistentCookieStore( |
356 temp_dir_.path().Append(kCookieFilename), client_task_runner(), | 356 temp_dir_.GetPath().Append(kCookieFilename), client_task_runner(), |
357 background_task_runner(), true, nullptr); | 357 background_task_runner(), true, nullptr); |
358 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 358 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
359 base::Unretained(this))); | 359 base::Unretained(this))); |
360 loaded_event_.Wait(); | 360 loaded_event_.Wait(); |
361 ASSERT_EQ(4u, cookies_.size()); | 361 ASSERT_EQ(4u, cookies_.size()); |
362 base::STLDeleteElements(&cookies_); | 362 base::STLDeleteElements(&cookies_); |
363 } | 363 } |
364 | 364 |
365 // Test that priority load of cookies for a specfic domain key could be | 365 // Test that priority load of cookies for a specfic domain key could be |
366 // completed before the entire store is loaded | 366 // completed before the entire store is loaded |
367 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | 367 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
368 InitializeStore(false, false); | 368 InitializeStore(false, false); |
369 base::Time t = base::Time::Now(); | 369 base::Time t = base::Time::Now(); |
370 AddCookie(GURL("http://foo.bar"), "A", "B", std::string(), "/", t); | 370 AddCookie(GURL("http://foo.bar"), "A", "B", std::string(), "/", t); |
371 t += base::TimeDelta::FromInternalValue(10); | 371 t += base::TimeDelta::FromInternalValue(10); |
372 AddCookie(GURL("http://www.aaa.com"), "A", "B", std::string(), "/", t); | 372 AddCookie(GURL("http://www.aaa.com"), "A", "B", std::string(), "/", t); |
373 t += base::TimeDelta::FromInternalValue(10); | 373 t += base::TimeDelta::FromInternalValue(10); |
374 AddCookie(GURL("http://travel.aaa.com"), "A", "B", std::string(), "/", t); | 374 AddCookie(GURL("http://travel.aaa.com"), "A", "B", std::string(), "/", t); |
375 t += base::TimeDelta::FromInternalValue(10); | 375 t += base::TimeDelta::FromInternalValue(10); |
376 AddCookie(GURL("http://www.bbb.com"), "A", "B", std::string(), "/", t); | 376 AddCookie(GURL("http://www.bbb.com"), "A", "B", std::string(), "/", t); |
377 DestroyStore(); | 377 DestroyStore(); |
378 | 378 |
379 store_ = new SQLitePersistentCookieStore( | 379 store_ = new SQLitePersistentCookieStore( |
380 temp_dir_.path().Append(kCookieFilename), client_task_runner(), | 380 temp_dir_.GetPath().Append(kCookieFilename), client_task_runner(), |
381 background_task_runner(), false, nullptr); | 381 background_task_runner(), false, nullptr); |
382 | 382 |
383 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | 383 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
384 // until both Load and LoadCookiesForKey have been posted to its task queue. | 384 // until both Load and LoadCookiesForKey have been posted to its task queue. |
385 background_task_runner()->PostTask( | 385 background_task_runner()->PostTask( |
386 FROM_HERE, base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | 386 FROM_HERE, base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
387 base::Unretained(this))); | 387 base::Unretained(this))); |
388 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 388 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
389 base::Unretained(this))); | 389 base::Unretained(this))); |
390 store_->LoadCookiesForKey( | 390 store_->LoadCookiesForKey( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 ASSERT_EQ(cookies_loaded.find("foo.bar") != cookies_loaded.end(), true); | 425 ASSERT_EQ(cookies_loaded.find("foo.bar") != cookies_loaded.end(), true); |
426 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); | 426 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); |
427 base::STLDeleteElements(&cookies_); | 427 base::STLDeleteElements(&cookies_); |
428 } | 428 } |
429 | 429 |
430 // Test that we can force the database to be written by calling Flush(). | 430 // Test that we can force the database to be written by calling Flush(). |
431 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { | 431 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { |
432 InitializeStore(false, false); | 432 InitializeStore(false, false); |
433 // File timestamps don't work well on all platforms, so we'll determine | 433 // File timestamps don't work well on all platforms, so we'll determine |
434 // whether the DB file has been modified by checking its size. | 434 // whether the DB file has been modified by checking its size. |
435 base::FilePath path = temp_dir_.path().Append(kCookieFilename); | 435 base::FilePath path = temp_dir_.GetPath().Append(kCookieFilename); |
436 base::File::Info info; | 436 base::File::Info info; |
437 ASSERT_TRUE(base::GetFileInfo(path, &info)); | 437 ASSERT_TRUE(base::GetFileInfo(path, &info)); |
438 int64_t base_size = info.size; | 438 int64_t base_size = info.size; |
439 | 439 |
440 // Write some large cookies, so the DB will have to expand by several KB. | 440 // Write some large cookies, so the DB will have to expand by several KB. |
441 for (char c = 'a'; c < 'z'; ++c) { | 441 for (char c = 'a'; c < 'z'; ++c) { |
442 // Each cookie needs a unique timestamp for creation_utc (see DB schema). | 442 // Each cookie needs a unique timestamp for creation_utc (see DB schema). |
443 base::Time t = base::Time::Now() + base::TimeDelta::FromMicroseconds(c); | 443 base::Time t = base::Time::Now() + base::TimeDelta::FromMicroseconds(c); |
444 std::string name(1, c); | 444 std::string name(1, c); |
445 std::string value(1000, c); | 445 std::string value(1000, c); |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
719 } | 719 } |
720 EXPECT_EQ("encrypted_value123XYZ", cookie_name->Value()); | 720 EXPECT_EQ("encrypted_value123XYZ", cookie_name->Value()); |
721 EXPECT_EQ("something456ABC", cookie_other->Value()); | 721 EXPECT_EQ("something456ABC", cookie_other->Value()); |
722 DestroyStore(); | 722 DestroyStore(); |
723 base::STLDeleteElements(&cookies_); | 723 base::STLDeleteElements(&cookies_); |
724 | 724 |
725 // Examine the real record to make sure plaintext version doesn't exist. | 725 // Examine the real record to make sure plaintext version doesn't exist. |
726 sql::Connection db; | 726 sql::Connection db; |
727 sql::Statement smt; | 727 sql::Statement smt; |
728 int resultcount = 0; | 728 int resultcount = 0; |
729 ASSERT_TRUE(db.Open(temp_dir_.path().Append(kCookieFilename))); | 729 ASSERT_TRUE(db.Open(temp_dir_.GetPath().Append(kCookieFilename))); |
730 smt.Assign(db.GetCachedStatement(SQL_FROM_HERE, | 730 smt.Assign(db.GetCachedStatement(SQL_FROM_HERE, |
731 "SELECT * " | 731 "SELECT * " |
732 "FROM cookies " | 732 "FROM cookies " |
733 "WHERE host_key = 'foo.bar'")); | 733 "WHERE host_key = 'foo.bar'")); |
734 while (smt.Step()) { | 734 while (smt.Step()) { |
735 resultcount++; | 735 resultcount++; |
736 for (int i = 0; i < smt.ColumnCount(); i++) { | 736 for (int i = 0; i < smt.ColumnCount(); i++) { |
737 EXPECT_EQ(smt.ColumnString(i).find("value"), std::string::npos); | 737 EXPECT_EQ(smt.ColumnString(i).find("value"), std::string::npos); |
738 EXPECT_EQ(smt.ColumnString(i).find("something"), std::string::npos); | 738 EXPECT_EQ(smt.ColumnString(i).find("something"), std::string::npos); |
739 } | 739 } |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 EXPECT_TRUE(was_called_with_no_cookies); | 825 EXPECT_TRUE(was_called_with_no_cookies); |
826 | 826 |
827 // Same with trying to load a specific cookie. | 827 // Same with trying to load a specific cookie. |
828 was_called_with_no_cookies = false; | 828 was_called_with_no_cookies = false; |
829 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, | 829 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, |
830 &was_called_with_no_cookies)); | 830 &was_called_with_no_cookies)); |
831 EXPECT_TRUE(was_called_with_no_cookies); | 831 EXPECT_TRUE(was_called_with_no_cookies); |
832 } | 832 } |
833 | 833 |
834 } // namespace net | 834 } // namespace net |
OLD | NEW |