OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/files/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/stl_util.h" |
| 13 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/test/sequenced_worker_pool_owner.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" |
| 16 #include "base/time/time.h" |
| 17 #include "content/browser/net/quota_policy_cookie_store.h" |
| 18 #include "content/public/test/mock_special_storage_policy.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "net/base/test_data_directory.h" |
| 21 #include "net/cookies/cookie_util.h" |
| 22 #include "net/ssl/ssl_client_cert_type.h" |
| 23 #include "net/test/cert_test_util.h" |
| 24 #include "sql/statement.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 |
| 27 namespace { |
| 28 const base::FilePath::CharType kTestCookiesFilename[] = |
| 29 FILE_PATH_LITERAL("Cookies"); |
| 30 } |
| 31 |
| 32 namespace content { |
| 33 |
| 34 class QuotaPolicyCookieStoreTest : public testing::Test { |
| 35 public: |
| 36 QuotaPolicyCookieStoreTest() |
| 37 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), |
| 38 loaded_event_(false, false) { |
| 39 } |
| 40 |
| 41 void OnLoaded(const std::vector<net::CanonicalCookie*>& cookies) { |
| 42 cookies_ = cookies; |
| 43 loaded_event_.Signal(); |
| 44 } |
| 45 |
| 46 void Load(std::vector<net::CanonicalCookie*>* cookies) { |
| 47 EXPECT_FALSE(loaded_event_.IsSignaled()); |
| 48 store_->Load(base::Bind(&QuotaPolicyCookieStoreTest::OnLoaded, |
| 49 base::Unretained(this))); |
| 50 loaded_event_.Wait(); |
| 51 *cookies = cookies_; |
| 52 } |
| 53 |
| 54 protected: |
| 55 scoped_refptr<base::SequencedTaskRunner> background_task_runner() { |
| 56 return pool_owner_->pool()->GetSequencedTaskRunner( |
| 57 pool_owner_->pool()->GetNamedSequenceToken("background")); |
| 58 } |
| 59 |
| 60 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { |
| 61 return pool_owner_->pool()->GetSequencedTaskRunner( |
| 62 pool_owner_->pool()->GetNamedSequenceToken("client")); |
| 63 } |
| 64 |
| 65 // Adds a persistent cookie to store_. |
| 66 void AddCookie(const std::string& name, |
| 67 const std::string& value, |
| 68 const std::string& domain, |
| 69 const std::string& path, |
| 70 const base::Time& creation) { |
| 71 store_->AddCookie( |
| 72 net::CanonicalCookie( |
| 73 GURL(), name, value, domain, path, creation, creation, creation, |
| 74 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); |
| 75 } |
| 76 |
| 77 void DestroyStore() { |
| 78 store_ = NULL; |
| 79 // Make sure we wait until the destructor has run by shutting down the pool |
| 80 // resetting the owner (whose destructor blocks on the pool completion). |
| 81 pool_owner_->pool()->FlushForTesting(); |
| 82 pool_owner_->pool()->Shutdown(); |
| 83 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); |
| 84 } |
| 85 |
| 86 void SetUp() override { |
| 87 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 88 store_ = new QuotaPolicyCookieStore( |
| 89 temp_dir_.path().Append(kTestCookiesFilename), |
| 90 client_task_runner(), |
| 91 background_task_runner(), |
| 92 true, NULL, NULL); |
| 93 std::vector<net::CanonicalCookie*> cookies; |
| 94 Load(&cookies); |
| 95 ASSERT_EQ(0u, cookies.size()); |
| 96 // Make sure the store gets written at least once. |
| 97 AddCookie("A", "B", "foo.com", "/", base::Time::Now()); |
| 98 } |
| 99 |
| 100 void TearDown() override { |
| 101 DestroyStore(); |
| 102 pool_owner_->pool()->Shutdown(); |
| 103 } |
| 104 |
| 105 base::MessageLoop main_loop_; |
| 106 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; |
| 107 base::WaitableEvent loaded_event_; |
| 108 base::ScopedTempDir temp_dir_; |
| 109 scoped_refptr<QuotaPolicyCookieStore> store_; |
| 110 std::vector<net::CanonicalCookie*> cookies_; |
| 111 }; |
| 112 |
| 113 // Test if data is stored as expected in the QuotaPolicy database. |
| 114 TEST_F(QuotaPolicyCookieStoreTest, TestPersistence) { |
| 115 AddCookie("A", "B", "persistent.com", "/", base::Time::Now()); |
| 116 |
| 117 // Replace the store effectively destroying the current one and forcing it |
| 118 // to write its data to disk. Then we can see if after loading it again it |
| 119 // is still there. |
| 120 DestroyStore(); |
| 121 store_ = new QuotaPolicyCookieStore( |
| 122 temp_dir_.path().Append(kTestCookiesFilename), |
| 123 client_task_runner(), |
| 124 background_task_runner(), |
| 125 true, NULL, NULL); |
| 126 |
| 127 // Reload and test for persistence |
| 128 std::vector<net::CanonicalCookie*> cookies; |
| 129 Load(&cookies); |
| 130 EXPECT_EQ(2U, cookies.size()); |
| 131 EXPECT_EQ("foo.com", cookies[0]->Domain()); |
| 132 EXPECT_EQ("persistent.com", cookies[1]->Domain()); |
| 133 |
| 134 // Now delete the cookies and check persistence again. |
| 135 store_->DeleteCookie(*cookies[0]); |
| 136 store_->DeleteCookie(*cookies[1]); |
| 137 DestroyStore(); |
| 138 |
| 139 STLDeleteElements(&cookies); |
| 140 cookies.clear(); |
| 141 store_ = new QuotaPolicyCookieStore( |
| 142 temp_dir_.path().Append(kTestCookiesFilename), |
| 143 client_task_runner(), |
| 144 background_task_runner(), |
| 145 true, NULL, NULL); |
| 146 |
| 147 // Reload and check if the cookies have been removed. |
| 148 Load(&cookies); |
| 149 EXPECT_EQ(0U, cookies.size()); |
| 150 STLDeleteElements(&cookies); |
| 151 } |
| 152 |
| 153 // Test if data is stored as expected in the QuotaPolicy database. |
| 154 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) { |
| 155 AddCookie("A", "B", "persistent.com", "/", base::Time::Now()); |
| 156 AddCookie("A", "B", "nonpersistent.com", "/", base::Time::Now()); |
| 157 |
| 158 // Replace the store effectively destroying the current one and forcing it |
| 159 // to write its data to disk. Then we can see if after loading it again it |
| 160 // is still there. |
| 161 DestroyStore(); |
| 162 // Specify storage policy that makes "nonpersistent.com" session only. |
| 163 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
| 164 new content::MockSpecialStoragePolicy(); |
| 165 storage_policy->AddSessionOnly( |
| 166 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); |
| 167 store_ = new QuotaPolicyCookieStore( |
| 168 temp_dir_.path().Append(kTestCookiesFilename), |
| 169 client_task_runner(), |
| 170 background_task_runner(), |
| 171 true, storage_policy.get(), NULL); |
| 172 |
| 173 // Reload and test for persistence |
| 174 std::vector<net::CanonicalCookie*> cookies; |
| 175 Load(&cookies); |
| 176 EXPECT_EQ(3U, cookies.size()); |
| 177 STLDeleteElements(&cookies); |
| 178 |
| 179 AddCookie("A", "B", "nonpersistent.com", "/second", base::Time::Now()); |
| 180 |
| 181 // Now close the store, and "nonpersistent.com" should be deleted according to |
| 182 // policy. |
| 183 DestroyStore(); |
| 184 store_ = new QuotaPolicyCookieStore( |
| 185 temp_dir_.path().Append(kTestCookiesFilename), |
| 186 client_task_runner(), |
| 187 background_task_runner(), |
| 188 true, NULL, NULL); |
| 189 |
| 190 Load(&cookies); |
| 191 EXPECT_EQ(2U, cookies.size()); |
| 192 for (auto it = cookies.begin(); it != cookies.end(); ++it) { |
| 193 EXPECT_NE("nonpersistent.com", (*it)->Domain()); |
| 194 } |
| 195 STLDeleteElements(&cookies); |
| 196 } |
| 197 |
| 198 TEST_F(QuotaPolicyCookieStoreTest, ForceKeepSessionState) { |
| 199 // Recreate |store_| with a storage policy that makes "nonpersistent.com" |
| 200 // session only, but then instruct the store to forcibly keep all cookies. |
| 201 DestroyStore(); |
| 202 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
| 203 new content::MockSpecialStoragePolicy(); |
| 204 storage_policy->AddSessionOnly( |
| 205 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); |
| 206 store_ = new QuotaPolicyCookieStore( |
| 207 temp_dir_.path().Append(kTestCookiesFilename), |
| 208 client_task_runner(), |
| 209 background_task_runner(), |
| 210 true, storage_policy.get(), NULL); |
| 211 |
| 212 // Reload and test for persistence |
| 213 std::vector<net::CanonicalCookie*> cookies; |
| 214 Load(&cookies); |
| 215 EXPECT_EQ(1U, cookies.size()); |
| 216 STLDeleteElements(&cookies); |
| 217 |
| 218 AddCookie("A", "B", "persistent.com", "/", base::Time::Now()); |
| 219 AddCookie("A", "B", "nonpersistent.com", "/", base::Time::Now()); |
| 220 |
| 221 // Now close the store, but the "nonpersistent.com" cookie should not be |
| 222 // deleted. |
| 223 store_->SetForceKeepSessionState(); |
| 224 DestroyStore(); |
| 225 store_ = new QuotaPolicyCookieStore( |
| 226 temp_dir_.path().Append(kTestCookiesFilename), |
| 227 client_task_runner(), |
| 228 background_task_runner(), |
| 229 true, NULL, NULL); |
| 230 |
| 231 Load(&cookies); |
| 232 EXPECT_EQ(3U, cookies.size()); |
| 233 STLDeleteElements(&cookies); |
| 234 } |
| 235 |
| 236 } // namespace content |
OLD | NEW |