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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 void DestroyStore() { | 120 void DestroyStore() { |
121 store_ = nullptr; | 121 store_ = nullptr; |
122 // Make sure we wait until the destructor has run by shutting down the pool | 122 // Make sure we wait until the destructor has run by shutting down the pool |
123 // resetting the owner (whose destructor blocks on the pool completion). | 123 // resetting the owner (whose destructor blocks on the pool completion). |
124 pool_owner_->pool()->Shutdown(); | 124 pool_owner_->pool()->Shutdown(); |
125 // Create a new pool for the few tests that create multiple stores. In other | 125 // Create a new pool for the few tests that create multiple stores. In other |
126 // cases this is wasted but harmless. | 126 // cases this is wasted but harmless. |
127 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); | 127 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); |
128 } | 128 } |
129 | 129 |
130 void CreateAndLoad(bool crypt_cookies, | 130 void Create(bool crypt_cookies, bool restore_old_session_cookies) { |
131 bool restore_old_session_cookies, | |
132 CanonicalCookieVector* cookies) { | |
133 if (crypt_cookies) | 131 if (crypt_cookies) |
134 cookie_crypto_delegate_.reset(new CookieCryptor()); | 132 cookie_crypto_delegate_.reset(new CookieCryptor()); |
135 | 133 |
136 store_ = new SQLitePersistentCookieStore( | 134 store_ = new SQLitePersistentCookieStore( |
137 temp_dir_.path().Append(kCookieFilename), client_task_runner(), | 135 temp_dir_.path().Append(kCookieFilename), client_task_runner(), |
138 background_task_runner(), restore_old_session_cookies, | 136 background_task_runner(), restore_old_session_cookies, |
139 cookie_crypto_delegate_.get()); | 137 cookie_crypto_delegate_.get()); |
| 138 } |
| 139 |
| 140 void CreateAndLoad(bool crypt_cookies, |
| 141 bool restore_old_session_cookies, |
| 142 CanonicalCookieVector* cookies) { |
| 143 Create(crypt_cookies, restore_old_session_cookies); |
140 Load(cookies); | 144 Load(cookies); |
141 } | 145 } |
142 | 146 |
143 void InitializeStore(bool crypt, bool restore_old_session_cookies) { | 147 void InitializeStore(bool crypt, bool restore_old_session_cookies) { |
144 CanonicalCookieVector cookies; | 148 CanonicalCookieVector cookies; |
145 CreateAndLoad(crypt, restore_old_session_cookies, &cookies); | 149 CreateAndLoad(crypt, restore_old_session_cookies, &cookies); |
146 EXPECT_EQ(0U, cookies.size()); | 150 EXPECT_EQ(0U, cookies.size()); |
147 } | 151 } |
148 | 152 |
149 // We have to create this method to wrap WaitableEvent::Wait, since we cannot | 153 // We have to create this method to wrap WaitableEvent::Wait, since we cannot |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 } | 657 } |
654 EXPECT_EQ(2, resultcount); | 658 EXPECT_EQ(2, resultcount); |
655 | 659 |
656 // Verify that "encrypted_value" is NOT visible in the file. | 660 // Verify that "encrypted_value" is NOT visible in the file. |
657 contents = ReadRawDBContents(); | 661 contents = ReadRawDBContents(); |
658 EXPECT_NE(0U, contents.length()); | 662 EXPECT_NE(0U, contents.length()); |
659 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); | 663 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); |
660 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); | 664 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); |
661 } | 665 } |
662 | 666 |
| 667 namespace { |
| 668 void WasCalledWithNoCookies(bool* was_called_with_no_cookies, |
| 669 const std::vector<CanonicalCookie*>& cookies) { |
| 670 *was_called_with_no_cookies = cookies.empty(); |
| 671 } |
| 672 } |
| 673 |
| 674 TEST_F(SQLitePersistentCookieStoreTest, EmptyLoadAfterClose) { |
| 675 // Create unencrypted cookie store and write something to it. |
| 676 InitializeStore(false, false); |
| 677 AddCookie("name", "value123XYZ", "foo.bar", "/", base::Time::Now()); |
| 678 DestroyStore(); |
| 679 |
| 680 // Create the cookie store, but immediately close it. |
| 681 Create(false, false); |
| 682 store_->Close(base::Closure()); |
| 683 |
| 684 // Expect any attempt to call Load() to synchronously respond with an empty |
| 685 // vector of cookies after we've Close()d the database. |
| 686 bool was_called_with_no_cookies = false; |
| 687 store_->Load(base::Bind(WasCalledWithNoCookies, &was_called_with_no_cookies)); |
| 688 EXPECT_TRUE(was_called_with_no_cookies); |
| 689 |
| 690 // Same with trying to load a specific cookie. |
| 691 was_called_with_no_cookies = false; |
| 692 store_->LoadCookiesForKey("foo.bar", base::Bind(WasCalledWithNoCookies, |
| 693 &was_called_with_no_cookies)); |
| 694 EXPECT_TRUE(was_called_with_no_cookies); |
| 695 } |
| 696 |
663 } // namespace net | 697 } // namespace net |
OLD | NEW |