Chromium Code Reviews| 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 namespace { | |
| 34 | |
| 35 typedef std::vector<net::CanonicalCookie*> CanonicalCookieVector; | |
| 36 | |
| 37 class QuotaPolicyCookieStoreTest : public testing::Test { | |
| 38 public: | |
| 39 QuotaPolicyCookieStoreTest() | |
| 40 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), | |
| 41 loaded_event_(false, false) { | |
| 42 } | |
| 43 | |
| 44 void OnLoaded(const CanonicalCookieVector& cookies) { | |
| 45 cookies_ = cookies; | |
| 46 loaded_event_.Signal(); | |
| 47 } | |
| 48 | |
| 49 void Load(CanonicalCookieVector* cookies) { | |
| 50 EXPECT_FALSE(loaded_event_.IsSignaled()); | |
| 51 store_->Load(base::Bind(&QuotaPolicyCookieStoreTest::OnLoaded, | |
| 52 base::Unretained(this))); | |
| 53 loaded_event_.Wait(); | |
| 54 *cookies = cookies_; | |
| 55 } | |
| 56 | |
| 57 protected: | |
| 58 scoped_refptr<base::SequencedTaskRunner> background_task_runner() { | |
| 59 return pool_owner_->pool()->GetSequencedTaskRunner( | |
| 60 pool_owner_->pool()->GetNamedSequenceToken("background")); | |
| 61 } | |
| 62 | |
| 63 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { | |
| 64 return pool_owner_->pool()->GetSequencedTaskRunner( | |
| 65 pool_owner_->pool()->GetNamedSequenceToken("client")); | |
| 66 } | |
| 67 | |
| 68 void InitializeStore() { | |
| 69 store_ = new QuotaPolicyCookieStore( | |
| 70 temp_dir_.path().Append(kTestCookiesFilename), | |
| 71 client_task_runner(), | |
| 72 background_task_runner(), | |
| 73 true, NULL, NULL); | |
|
Ryan Sleevi
2015/05/06 00:44:18
nullptr, nullptr
rohitrao (ping after 24h)
2015/05/11 19:25:30
Done.
| |
| 74 CanonicalCookieVector cookies; | |
| 75 Load(&cookies); | |
| 76 ASSERT_EQ(0u, cookies.size()); | |
| 77 } | |
| 78 | |
| 79 // Adds a persistent cookie to store_. | |
| 80 void AddCookie(const std::string& name, | |
| 81 const std::string& value, | |
| 82 const std::string& domain, | |
| 83 const std::string& path, | |
| 84 const base::Time& creation) { | |
| 85 store_->AddCookie( | |
| 86 net::CanonicalCookie( | |
| 87 GURL(), name, value, domain, path, creation, creation, creation, | |
| 88 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); | |
| 89 } | |
| 90 | |
| 91 void DestroyStore() { | |
| 92 store_ = NULL; | |
|
Ryan Sleevi
2015/05/06 00:44:18
nullptr
rohitrao (ping after 24h)
2015/05/11 19:25:30
Done.
| |
| 93 // Make sure we wait until the destructor has run by shutting down the pool | |
| 94 // resetting the owner (whose destructor blocks on the pool completion). | |
|
Ryan Sleevi
2015/05/06 00:44:18
https://groups.google.com/a/chromium.org/forum/#!t
rohitrao (ping after 24h)
2015/05/11 19:25:30
Your rewording seems correct, inasmuch as I don't
| |
| 95 pool_owner_->pool()->FlushForTesting(); | |
| 96 pool_owner_->pool()->Shutdown(); | |
| 97 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); | |
| 98 } | |
| 99 | |
| 100 void SetUp() override { | |
| 101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 102 } | |
| 103 | |
| 104 void TearDown() override { | |
| 105 DestroyStore(); | |
| 106 pool_owner_->pool()->Shutdown(); | |
| 107 } | |
| 108 | |
| 109 base::MessageLoop main_loop_; | |
|
Ryan Sleevi
2015/05/06 00:44:18
Being in //content, should this be using a TestBro
rohitrao (ping after 24h)
2015/05/11 19:25:30
Makes sense, done.
| |
| 110 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; | |
| 111 base::WaitableEvent loaded_event_; | |
| 112 base::ScopedTempDir temp_dir_; | |
| 113 scoped_refptr<QuotaPolicyCookieStore> store_; | |
| 114 CanonicalCookieVector cookies_; | |
| 115 }; | |
| 116 | |
| 117 // Test if data is stored as expected in the QuotaPolicy database. | |
| 118 TEST_F(QuotaPolicyCookieStoreTest, TestPersistence) { | |
| 119 InitializeStore(); | |
| 120 base::Time t = base::Time::Now(); | |
| 121 AddCookie("A", "B", "foo.com", "/", t); | |
| 122 t += base::TimeDelta::FromInternalValue(10); | |
| 123 AddCookie("A", "B", "persistent.com", "/", t); | |
| 124 | |
| 125 // Replace the store effectively destroying the current one and forcing it | |
| 126 // to write its data to disk. Then we can see if after loading it again it | |
| 127 // is still there. | |
| 128 DestroyStore(); | |
| 129 store_ = new QuotaPolicyCookieStore( | |
| 130 temp_dir_.path().Append(kTestCookiesFilename), | |
| 131 client_task_runner(), | |
| 132 background_task_runner(), | |
| 133 true, NULL, NULL); | |
|
Ryan Sleevi
2015/05/06 00:44:18
nullptr blah blah
rohitrao (ping after 24h)
2015/05/11 19:25:30
Fixed everywhere.
| |
| 134 | |
| 135 // Reload and test for persistence | |
| 136 CanonicalCookieVector cookies; | |
| 137 Load(&cookies); | |
| 138 EXPECT_EQ(2U, cookies.size()); | |
| 139 EXPECT_EQ("foo.com", cookies[0]->Domain()); | |
| 140 EXPECT_EQ("persistent.com", cookies[1]->Domain()); | |
| 141 | |
| 142 // Now delete the cookies and check persistence again. | |
| 143 store_->DeleteCookie(*cookies[0]); | |
| 144 store_->DeleteCookie(*cookies[1]); | |
| 145 DestroyStore(); | |
| 146 | |
| 147 STLDeleteElements(&cookies); | |
| 148 cookies.clear(); | |
| 149 store_ = new QuotaPolicyCookieStore( | |
| 150 temp_dir_.path().Append(kTestCookiesFilename), | |
| 151 client_task_runner(), | |
| 152 background_task_runner(), | |
| 153 true, NULL, NULL); | |
| 154 | |
| 155 // Reload and check if the cookies have been removed. | |
| 156 Load(&cookies); | |
| 157 EXPECT_EQ(0U, cookies.size()); | |
| 158 STLDeleteElements(&cookies); | |
| 159 } | |
| 160 | |
| 161 // Test if data is stored as expected in the QuotaPolicy database. | |
| 162 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) { | |
| 163 InitializeStore(); | |
| 164 base::Time t = base::Time::Now(); | |
| 165 AddCookie("A", "B", "foo.com", "/", t); | |
| 166 t += base::TimeDelta::FromInternalValue(10); | |
| 167 AddCookie("A", "B", "persistent.com", "/", t); | |
| 168 t += base::TimeDelta::FromInternalValue(10); | |
| 169 AddCookie("A", "B", "nonpersistent.com", "/", t); | |
| 170 | |
| 171 // Replace the store effectively destroying the current one and forcing it | |
| 172 // to write its data to disk. Then we can see if after loading it again it | |
| 173 // is still there. | |
| 174 DestroyStore(); | |
| 175 // Specify storage policy that makes "nonpersistent.com" session only. | |
| 176 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | |
| 177 new content::MockSpecialStoragePolicy(); | |
| 178 storage_policy->AddSessionOnly( | |
| 179 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | |
| 180 store_ = new QuotaPolicyCookieStore( | |
| 181 temp_dir_.path().Append(kTestCookiesFilename), | |
| 182 client_task_runner(), | |
| 183 background_task_runner(), | |
| 184 true, storage_policy.get(), NULL); | |
| 185 | |
| 186 // Reload and test for persistence | |
| 187 CanonicalCookieVector cookies; | |
| 188 Load(&cookies); | |
| 189 EXPECT_EQ(3U, cookies.size()); | |
| 190 STLDeleteElements(&cookies); | |
| 191 | |
| 192 t += base::TimeDelta::FromInternalValue(10); | |
| 193 AddCookie("A", "B", "nonpersistent.com", "/second", t); | |
| 194 | |
| 195 // Now close the store, and "nonpersistent.com" should be deleted according to | |
| 196 // policy. | |
| 197 DestroyStore(); | |
| 198 store_ = new QuotaPolicyCookieStore( | |
| 199 temp_dir_.path().Append(kTestCookiesFilename), | |
| 200 client_task_runner(), | |
| 201 background_task_runner(), | |
| 202 true, NULL, NULL); | |
| 203 | |
| 204 Load(&cookies); | |
| 205 EXPECT_EQ(2U, cookies.size()); | |
| 206 for (auto it = cookies.begin(); it != cookies.end(); ++it) { | |
| 207 EXPECT_NE("nonpersistent.com", (*it)->Domain()); | |
| 208 } | |
|
Ryan Sleevi
2015/05/06 00:44:18
for (const auto& cookie : cookies) {
EXPECT_NE("
rohitrao (ping after 24h)
2015/05/11 19:25:30
Done.
| |
| 209 STLDeleteElements(&cookies); | |
| 210 } | |
| 211 | |
| 212 TEST_F(QuotaPolicyCookieStoreTest, ForceKeepSessionState) { | |
| 213 InitializeStore(); | |
| 214 base::Time t = base::Time::Now(); | |
| 215 AddCookie("A", "B", "foo.com", "/", t); | |
| 216 | |
| 217 // Recreate |store_| with a storage policy that makes "nonpersistent.com" | |
| 218 // session only, but then instruct the store to forcibly keep all cookies. | |
| 219 DestroyStore(); | |
| 220 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | |
| 221 new content::MockSpecialStoragePolicy(); | |
| 222 storage_policy->AddSessionOnly( | |
| 223 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | |
| 224 store_ = new QuotaPolicyCookieStore( | |
| 225 temp_dir_.path().Append(kTestCookiesFilename), | |
| 226 client_task_runner(), | |
| 227 background_task_runner(), | |
| 228 true, storage_policy.get(), NULL); | |
| 229 | |
| 230 // Reload and test for persistence | |
| 231 CanonicalCookieVector cookies; | |
| 232 Load(&cookies); | |
| 233 EXPECT_EQ(1U, cookies.size()); | |
| 234 STLDeleteElements(&cookies); | |
| 235 | |
| 236 t += base::TimeDelta::FromInternalValue(10); | |
| 237 AddCookie("A", "B", "persistent.com", "/", t); | |
| 238 t += base::TimeDelta::FromInternalValue(10); | |
| 239 AddCookie("A", "B", "nonpersistent.com", "/", t); | |
| 240 | |
| 241 // Now close the store, but the "nonpersistent.com" cookie should not be | |
| 242 // deleted. | |
| 243 store_->SetForceKeepSessionState(); | |
| 244 DestroyStore(); | |
| 245 store_ = new QuotaPolicyCookieStore( | |
| 246 temp_dir_.path().Append(kTestCookiesFilename), | |
| 247 client_task_runner(), | |
| 248 background_task_runner(), | |
| 249 true, NULL, NULL); | |
| 250 | |
| 251 Load(&cookies); | |
| 252 EXPECT_EQ(3U, cookies.size()); | |
| 253 STLDeleteElements(&cookies); | |
| 254 } | |
| 255 | |
| 256 } // namespace | |
| 257 } // namespace content | |
| OLD | NEW |