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/run_loop.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "base/synchronization/waitable_event.h" | |
| 13 #include "base/test/sequenced_worker_pool_owner.h" | |
| 14 #include "base/threading/sequenced_worker_pool.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "content/browser/net/quota_policy_cookie_store.h" | |
| 17 #include "content/public/test/mock_special_storage_policy.h" | |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | |
| 19 #include "net/base/test_data_directory.h" | |
| 20 #include "net/cookies/cookie_util.h" | |
| 21 #include "net/ssl/ssl_client_cert_type.h" | |
| 22 #include "net/test/cert_test_util.h" | |
| 23 #include "sql/statement.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 | |
| 26 namespace { | |
| 27 const base::FilePath::CharType kTestCookiesFilename[] = | |
| 28 FILE_PATH_LITERAL("Cookies"); | |
| 29 } | |
| 30 | |
| 31 namespace content { | |
| 32 namespace { | |
| 33 | |
| 34 typedef std::vector<net::CanonicalCookie*> CanonicalCookieVector; | |
| 35 | |
| 36 class QuotaPolicyCookieStoreTest : public testing::Test { | |
| 37 public: | |
| 38 QuotaPolicyCookieStoreTest() | |
| 39 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), | |
| 40 loaded_event_(false, false), | |
| 41 destroy_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 void ReleaseStore() { | |
| 58 EXPECT_TRUE(background_task_runner()->RunsTasksOnCurrentThread()); | |
| 59 store_ = nullptr; | |
| 60 destroy_event_.Signal(); | |
| 61 } | |
| 62 | |
| 63 void DestroyStoreOnBackgroundThread() { | |
| 64 background_task_runner()->PostTask( | |
| 65 FROM_HERE, base::Bind(&QuotaPolicyCookieStoreTest::ReleaseStore, | |
| 66 base::Unretained(this))); | |
| 67 destroy_event_.Wait(); | |
|
Ryan Sleevi
2015/05/15 22:18:04
DANGER BUG: This is an inherently dangerous patter
rohitrao (ping after 24h)
2015/05/15 22:53:27
Acknowledged, will fix in a follow.
For some reas
| |
| 68 DestroyStore(); | |
| 69 } | |
| 70 | |
| 71 protected: | |
| 72 scoped_refptr<base::SequencedTaskRunner> background_task_runner() { | |
| 73 return pool_owner_->pool()->GetSequencedTaskRunner( | |
| 74 pool_owner_->pool()->GetNamedSequenceToken("background")); | |
| 75 } | |
| 76 | |
| 77 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { | |
| 78 return pool_owner_->pool()->GetSequencedTaskRunner( | |
| 79 pool_owner_->pool()->GetNamedSequenceToken("client")); | |
| 80 } | |
| 81 | |
| 82 void CreateAndLoad(storage::SpecialStoragePolicy* storage_policy, | |
| 83 CanonicalCookieVector* cookies) { | |
| 84 scoped_refptr<net::SQLitePersistentCookieStore> sqlite_store( | |
| 85 new net::SQLitePersistentCookieStore( | |
| 86 temp_dir_.path().Append(kTestCookiesFilename), | |
| 87 client_task_runner(), | |
| 88 background_task_runner(), | |
| 89 true, nullptr)); | |
| 90 store_ = new QuotaPolicyCookieStore(sqlite_store.get(), storage_policy); | |
| 91 Load(cookies); | |
| 92 } | |
| 93 | |
| 94 // Adds a persistent cookie to store_. | |
| 95 void AddCookie(const std::string& name, | |
| 96 const std::string& value, | |
| 97 const std::string& domain, | |
| 98 const std::string& path, | |
| 99 const base::Time& creation) { | |
| 100 store_->AddCookie( | |
| 101 net::CanonicalCookie( | |
| 102 GURL(), name, value, domain, path, creation, creation, creation, | |
| 103 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); | |
| 104 } | |
| 105 | |
| 106 void DestroyStore() { | |
| 107 store_ = nullptr; | |
| 108 // Ensure that |store_|'s destructor has run by shutting down the pool and | |
| 109 // then forcing the pool to be destructed. This will ensure that all the | |
| 110 // tasks that block pool shutdown (e.g. |store_|'s cleanup) have run before | |
| 111 // yielding control. | |
| 112 pool_owner_->pool()->FlushForTesting(); | |
| 113 pool_owner_->pool()->Shutdown(); | |
| 114 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); | |
| 115 } | |
| 116 | |
| 117 void SetUp() override { | |
| 118 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 119 } | |
| 120 | |
| 121 void TearDown() override { | |
| 122 DestroyStore(); | |
| 123 pool_owner_->pool()->Shutdown(); | |
| 124 } | |
| 125 | |
| 126 TestBrowserThreadBundle bundle_; | |
| 127 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; | |
| 128 base::WaitableEvent loaded_event_; | |
| 129 base::WaitableEvent destroy_event_; | |
| 130 base::ScopedTempDir temp_dir_; | |
| 131 scoped_refptr<QuotaPolicyCookieStore> store_; | |
| 132 CanonicalCookieVector cookies_; | |
| 133 }; | |
| 134 | |
| 135 // Test if data is stored as expected in the QuotaPolicy database. | |
| 136 TEST_F(QuotaPolicyCookieStoreTest, TestPersistence) { | |
| 137 CanonicalCookieVector cookies; | |
| 138 CreateAndLoad(nullptr, &cookies); | |
| 139 ASSERT_EQ(0U, cookies.size()); | |
| 140 | |
| 141 base::Time t = base::Time::Now(); | |
| 142 AddCookie("A", "B", "foo.com", "/", t); | |
| 143 t += base::TimeDelta::FromInternalValue(10); | |
| 144 AddCookie("A", "B", "persistent.com", "/", t); | |
| 145 | |
| 146 // Replace the store effectively destroying the current one and forcing it | |
| 147 // to write its data to disk. Then we can see if after loading it again it | |
| 148 // is still there. | |
| 149 DestroyStore(); | |
| 150 | |
| 151 // Reload and test for persistence. | |
| 152 STLDeleteElements(&cookies); | |
| 153 CreateAndLoad(nullptr, &cookies); | |
| 154 EXPECT_EQ(2U, cookies.size()); | |
| 155 bool found_foo_cookie = false; | |
| 156 bool found_persistent_cookie = false; | |
| 157 for (const auto& cookie : cookies) { | |
| 158 if (cookie->Domain() == "foo.com") | |
| 159 found_foo_cookie = true; | |
| 160 else if (cookie->Domain() == "persistent.com") | |
| 161 found_persistent_cookie = true; | |
| 162 } | |
| 163 EXPECT_TRUE(found_foo_cookie); | |
| 164 EXPECT_TRUE(found_persistent_cookie); | |
| 165 | |
| 166 // Now delete the cookies and check persistence again. | |
| 167 store_->DeleteCookie(*cookies[0]); | |
| 168 store_->DeleteCookie(*cookies[1]); | |
| 169 DestroyStore(); | |
| 170 | |
| 171 // Reload and check if the cookies have been removed. | |
| 172 STLDeleteElements(&cookies); | |
| 173 CreateAndLoad(nullptr, &cookies); | |
| 174 EXPECT_EQ(0U, cookies.size()); | |
| 175 STLDeleteElements(&cookies); | |
| 176 } | |
| 177 | |
| 178 // Test if data is stored as expected in the QuotaPolicy database. | |
| 179 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) { | |
| 180 CanonicalCookieVector cookies; | |
| 181 CreateAndLoad(nullptr, &cookies); | |
| 182 ASSERT_EQ(0U, cookies.size()); | |
| 183 | |
| 184 base::Time t = base::Time::Now(); | |
| 185 AddCookie("A", "B", "foo.com", "/", t); | |
| 186 t += base::TimeDelta::FromInternalValue(10); | |
| 187 AddCookie("A", "B", "persistent.com", "/", t); | |
| 188 t += base::TimeDelta::FromInternalValue(10); | |
| 189 AddCookie("A", "B", "nonpersistent.com", "/", t); | |
| 190 | |
| 191 // Replace the store effectively destroying the current one and forcing it | |
| 192 // to write its data to disk. Then we can see if after loading it again it | |
| 193 // is still there. | |
| 194 DestroyStore(); | |
| 195 // Specify storage policy that makes "nonpersistent.com" session only. | |
| 196 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | |
| 197 new content::MockSpecialStoragePolicy(); | |
| 198 storage_policy->AddSessionOnly( | |
| 199 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | |
| 200 | |
| 201 // Reload and test for persistence. | |
| 202 STLDeleteElements(&cookies); | |
| 203 CreateAndLoad(storage_policy.get(), &cookies); | |
| 204 EXPECT_EQ(3U, cookies.size()); | |
| 205 | |
| 206 t += base::TimeDelta::FromInternalValue(10); | |
| 207 AddCookie("A", "B", "nonpersistent.com", "/second", t); | |
| 208 | |
| 209 // Now close the store, and "nonpersistent.com" should be deleted according to | |
| 210 // policy. | |
| 211 DestroyStore(); | |
| 212 STLDeleteElements(&cookies); | |
| 213 CreateAndLoad(nullptr, &cookies); | |
| 214 | |
| 215 EXPECT_EQ(2U, cookies.size()); | |
| 216 for (const auto& cookie : cookies) { | |
| 217 EXPECT_NE("nonpersistent.com", cookie->Domain()); | |
| 218 } | |
| 219 STLDeleteElements(&cookies); | |
| 220 } | |
| 221 | |
| 222 TEST_F(QuotaPolicyCookieStoreTest, ForceKeepSessionState) { | |
| 223 CanonicalCookieVector cookies; | |
| 224 CreateAndLoad(nullptr, &cookies); | |
| 225 ASSERT_EQ(0U, cookies.size()); | |
| 226 | |
| 227 base::Time t = base::Time::Now(); | |
| 228 AddCookie("A", "B", "foo.com", "/", t); | |
| 229 | |
| 230 // Recreate |store_| with a storage policy that makes "nonpersistent.com" | |
| 231 // session only, but then instruct the store to forcibly keep all cookies. | |
| 232 DestroyStore(); | |
| 233 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | |
| 234 new content::MockSpecialStoragePolicy(); | |
| 235 storage_policy->AddSessionOnly( | |
| 236 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | |
| 237 | |
| 238 // Reload and test for persistence | |
| 239 STLDeleteElements(&cookies); | |
| 240 CreateAndLoad(storage_policy.get(), &cookies); | |
| 241 EXPECT_EQ(1U, cookies.size()); | |
| 242 | |
| 243 t += base::TimeDelta::FromInternalValue(10); | |
| 244 AddCookie("A", "B", "persistent.com", "/", t); | |
| 245 t += base::TimeDelta::FromInternalValue(10); | |
| 246 AddCookie("A", "B", "nonpersistent.com", "/", t); | |
| 247 | |
| 248 // Now close the store, but the "nonpersistent.com" cookie should not be | |
| 249 // deleted. | |
| 250 store_->SetForceKeepSessionState(); | |
| 251 DestroyStore(); | |
| 252 STLDeleteElements(&cookies); | |
| 253 CreateAndLoad(nullptr, &cookies); | |
| 254 | |
| 255 EXPECT_EQ(3U, cookies.size()); | |
| 256 STLDeleteElements(&cookies); | |
| 257 } | |
| 258 | |
| 259 // Tests that the special storage policy is properly applied even when the store | |
| 260 // is destroyed on a background thread. | |
| 261 TEST_F(QuotaPolicyCookieStoreTest, TestDestroyOnBackgroundThread) { | |
| 262 // Specify storage policy that makes "nonpersistent.com" session only. | |
| 263 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | |
| 264 new content::MockSpecialStoragePolicy(); | |
| 265 storage_policy->AddSessionOnly( | |
| 266 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | |
| 267 | |
| 268 CanonicalCookieVector cookies; | |
| 269 CreateAndLoad(storage_policy.get(), &cookies); | |
| 270 ASSERT_EQ(0U, cookies.size()); | |
| 271 | |
| 272 base::Time t = base::Time::Now(); | |
| 273 AddCookie("A", "B", "nonpersistent.com", "/", t); | |
| 274 | |
| 275 // Replace the store effectively destroying the current one and forcing it | |
| 276 // to write its data to disk. Then we can see if after loading it again it | |
| 277 // is still there. | |
|
Ryan Sleevi
2015/05/15 22:18:03
grumble grumble pronouns in comments
// Replace t
rohitrao (ping after 24h)
2015/05/15 22:53:27
Fixed everywhere.
| |
| 278 DestroyStoreOnBackgroundThread(); | |
| 279 | |
| 280 // Reload and test for persistence. | |
| 281 STLDeleteElements(&cookies); | |
| 282 CreateAndLoad(storage_policy.get(), &cookies); | |
| 283 EXPECT_EQ(0U, cookies.size()); | |
| 284 | |
| 285 STLDeleteElements(&cookies); | |
| 286 } | |
| 287 | |
| 288 } // namespace | |
| 289 } // namespace content | |
| OLD | NEW |