| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/net/quota_policy_cookie_store.h" | 5 #include "content/browser/net/quota_policy_cookie_store.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 CanonicalCookieVector* cookies) { | 84 CanonicalCookieVector* cookies) { |
| 85 scoped_refptr<net::SQLitePersistentCookieStore> sqlite_store( | 85 scoped_refptr<net::SQLitePersistentCookieStore> sqlite_store( |
| 86 new net::SQLitePersistentCookieStore( | 86 new net::SQLitePersistentCookieStore( |
| 87 temp_dir_.GetPath().Append(kTestCookiesFilename), | 87 temp_dir_.GetPath().Append(kTestCookiesFilename), |
| 88 client_task_runner(), background_task_runner(), true, nullptr)); | 88 client_task_runner(), background_task_runner(), true, nullptr)); |
| 89 store_ = new QuotaPolicyCookieStore(sqlite_store.get(), storage_policy); | 89 store_ = new QuotaPolicyCookieStore(sqlite_store.get(), storage_policy); |
| 90 Load(cookies); | 90 Load(cookies); |
| 91 } | 91 } |
| 92 | 92 |
| 93 // Adds a persistent cookie to store_. | 93 // Adds a persistent cookie to store_. |
| 94 void AddCookie(const GURL& url, | 94 void AddCookie(const std::string& name, |
| 95 const std::string& name, | |
| 96 const std::string& value, | 95 const std::string& value, |
| 97 const std::string& domain, | 96 const std::string& domain, |
| 98 const std::string& path, | 97 const std::string& path, |
| 99 const base::Time& creation) { | 98 const base::Time& creation) { |
| 100 store_->AddCookie(*net::CanonicalCookie::Create( | 99 store_->AddCookie(*net::CanonicalCookie::Create( |
| 101 url, name, value, domain, path, creation, creation, false, false, | 100 name, value, domain, path, creation, creation, base::Time(), false, |
| 102 net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT)); | 101 false, net::CookieSameSite::DEFAULT_MODE, |
| 102 net::COOKIE_PRIORITY_DEFAULT)); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void DestroyStore() { | 105 void DestroyStore() { |
| 106 store_ = nullptr; | 106 store_ = nullptr; |
| 107 // Ensure that |store_|'s destructor has run by shutting down the pool and | 107 // Ensure that |store_|'s destructor has run by shutting down the pool and |
| 108 // then forcing the pool to be destructed. This will ensure that all the | 108 // then forcing the pool to be destructed. This will ensure that all the |
| 109 // tasks that block pool shutdown (e.g. |store_|'s cleanup) have run before | 109 // tasks that block pool shutdown (e.g. |store_|'s cleanup) have run before |
| 110 // yielding control. | 110 // yielding control. |
| 111 pool_owner_->pool()->FlushForTesting(); | 111 pool_owner_->pool()->FlushForTesting(); |
| 112 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); | 112 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 129 CanonicalCookieVector cookies_; | 129 CanonicalCookieVector cookies_; |
| 130 }; | 130 }; |
| 131 | 131 |
| 132 // Test if data is stored as expected in the QuotaPolicy database. | 132 // Test if data is stored as expected in the QuotaPolicy database. |
| 133 TEST_F(QuotaPolicyCookieStoreTest, TestPersistence) { | 133 TEST_F(QuotaPolicyCookieStoreTest, TestPersistence) { |
| 134 CanonicalCookieVector cookies; | 134 CanonicalCookieVector cookies; |
| 135 CreateAndLoad(nullptr, &cookies); | 135 CreateAndLoad(nullptr, &cookies); |
| 136 ASSERT_EQ(0U, cookies.size()); | 136 ASSERT_EQ(0U, cookies.size()); |
| 137 | 137 |
| 138 base::Time t = base::Time::Now(); | 138 base::Time t = base::Time::Now(); |
| 139 AddCookie(GURL("http://foo.com"), "A", "B", std::string(), "/", t); | 139 AddCookie("A", "B", "foo.com", "/", t); |
| 140 t += base::TimeDelta::FromInternalValue(10); | 140 t += base::TimeDelta::FromInternalValue(10); |
| 141 AddCookie(GURL("http://persistent.com"), "A", "B", std::string(), "/", t); | 141 AddCookie("A", "B", "persistent.com", "/", t); |
| 142 | 142 |
| 143 // Replace the store, which forces the current store to flush data to | 143 // Replace the store, which forces the current store to flush data to |
| 144 // disk. Then, after reloading the store, confirm that the data was flushed by | 144 // disk. Then, after reloading the store, confirm that the data was flushed by |
| 145 // making sure it loads successfully. This ensures that all pending commits | 145 // making sure it loads successfully. This ensures that all pending commits |
| 146 // are made to the store before allowing it to be closed. | 146 // are made to the store before allowing it to be closed. |
| 147 DestroyStore(); | 147 DestroyStore(); |
| 148 | 148 |
| 149 // Reload and test for persistence. | 149 // Reload and test for persistence. |
| 150 cookies.clear(); | 150 cookies.clear(); |
| 151 CreateAndLoad(nullptr, &cookies); | 151 CreateAndLoad(nullptr, &cookies); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 173 cookies.clear(); | 173 cookies.clear(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 // Test if data is stored as expected in the QuotaPolicy database. | 176 // Test if data is stored as expected in the QuotaPolicy database. |
| 177 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) { | 177 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) { |
| 178 CanonicalCookieVector cookies; | 178 CanonicalCookieVector cookies; |
| 179 CreateAndLoad(nullptr, &cookies); | 179 CreateAndLoad(nullptr, &cookies); |
| 180 ASSERT_EQ(0U, cookies.size()); | 180 ASSERT_EQ(0U, cookies.size()); |
| 181 | 181 |
| 182 base::Time t = base::Time::Now(); | 182 base::Time t = base::Time::Now(); |
| 183 AddCookie(GURL("http://foo.com"), "A", "B", std::string(), "/", t); | 183 AddCookie("A", "B", "foo.com", "/", t); |
| 184 t += base::TimeDelta::FromInternalValue(10); | 184 t += base::TimeDelta::FromInternalValue(10); |
| 185 AddCookie(GURL("http://persistent.com"), "A", "B", std::string(), "/", t); | 185 AddCookie("A", "B", "persistent.com", "/", t); |
| 186 t += base::TimeDelta::FromInternalValue(10); | 186 t += base::TimeDelta::FromInternalValue(10); |
| 187 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), "/", t); | 187 AddCookie("A", "B", "nonpersistent.com", "/", t); |
| 188 | 188 |
| 189 // Replace the store, which forces the current store to flush data to | 189 // Replace the store, which forces the current store to flush data to |
| 190 // disk. Then, after reloading the store, confirm that the data was flushed by | 190 // disk. Then, after reloading the store, confirm that the data was flushed by |
| 191 // making sure it loads successfully. This ensures that all pending commits | 191 // making sure it loads successfully. This ensures that all pending commits |
| 192 // are made to the store before allowing it to be closed. | 192 // are made to the store before allowing it to be closed. |
| 193 DestroyStore(); | 193 DestroyStore(); |
| 194 // Specify storage policy that makes "nonpersistent.com" session only. | 194 // Specify storage policy that makes "nonpersistent.com" session only. |
| 195 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | 195 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
| 196 new content::MockSpecialStoragePolicy(); | 196 new content::MockSpecialStoragePolicy(); |
| 197 storage_policy->AddSessionOnly( | 197 storage_policy->AddSessionOnly( |
| 198 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | 198 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); |
| 199 | 199 |
| 200 // Reload and test for persistence. | 200 // Reload and test for persistence. |
| 201 cookies.clear(); | 201 cookies.clear(); |
| 202 CreateAndLoad(storage_policy.get(), &cookies); | 202 CreateAndLoad(storage_policy.get(), &cookies); |
| 203 EXPECT_EQ(3U, cookies.size()); | 203 EXPECT_EQ(3U, cookies.size()); |
| 204 | 204 |
| 205 t += base::TimeDelta::FromInternalValue(10); | 205 t += base::TimeDelta::FromInternalValue(10); |
| 206 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), | 206 AddCookie("A", "B", "nonpersistent.com", "/second", t); |
| 207 "/second", t); | |
| 208 | 207 |
| 209 // Now close the store, and "nonpersistent.com" should be deleted according to | 208 // Now close the store, and "nonpersistent.com" should be deleted according to |
| 210 // policy. | 209 // policy. |
| 211 DestroyStore(); | 210 DestroyStore(); |
| 212 cookies.clear(); | 211 cookies.clear(); |
| 213 CreateAndLoad(nullptr, &cookies); | 212 CreateAndLoad(nullptr, &cookies); |
| 214 | 213 |
| 215 EXPECT_EQ(2U, cookies.size()); | 214 EXPECT_EQ(2U, cookies.size()); |
| 216 for (const auto& cookie : cookies) { | 215 for (const auto& cookie : cookies) { |
| 217 EXPECT_NE("nonpersistent.com", cookie->Domain()); | 216 EXPECT_NE("nonpersistent.com", cookie->Domain()); |
| 218 } | 217 } |
| 219 cookies.clear(); | 218 cookies.clear(); |
| 220 } | 219 } |
| 221 | 220 |
| 222 TEST_F(QuotaPolicyCookieStoreTest, ForceKeepSessionState) { | 221 TEST_F(QuotaPolicyCookieStoreTest, ForceKeepSessionState) { |
| 223 CanonicalCookieVector cookies; | 222 CanonicalCookieVector cookies; |
| 224 CreateAndLoad(nullptr, &cookies); | 223 CreateAndLoad(nullptr, &cookies); |
| 225 ASSERT_EQ(0U, cookies.size()); | 224 ASSERT_EQ(0U, cookies.size()); |
| 226 | 225 |
| 227 base::Time t = base::Time::Now(); | 226 base::Time t = base::Time::Now(); |
| 228 AddCookie(GURL("http://foo.com"), "A", "B", std::string(), "/", t); | 227 AddCookie("A", "B", "foo.com", "/", t); |
| 229 | 228 |
| 230 // Recreate |store_| with a storage policy that makes "nonpersistent.com" | 229 // Recreate |store_| with a storage policy that makes "nonpersistent.com" |
| 231 // session only, but then instruct the store to forcibly keep all cookies. | 230 // session only, but then instruct the store to forcibly keep all cookies. |
| 232 DestroyStore(); | 231 DestroyStore(); |
| 233 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | 232 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
| 234 new content::MockSpecialStoragePolicy(); | 233 new content::MockSpecialStoragePolicy(); |
| 235 storage_policy->AddSessionOnly( | 234 storage_policy->AddSessionOnly( |
| 236 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | 235 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); |
| 237 | 236 |
| 238 // Reload and test for persistence | 237 // Reload and test for persistence |
| 239 cookies.clear(); | 238 cookies.clear(); |
| 240 CreateAndLoad(storage_policy.get(), &cookies); | 239 CreateAndLoad(storage_policy.get(), &cookies); |
| 241 EXPECT_EQ(1U, cookies.size()); | 240 EXPECT_EQ(1U, cookies.size()); |
| 242 | 241 |
| 243 t += base::TimeDelta::FromInternalValue(10); | 242 t += base::TimeDelta::FromInternalValue(10); |
| 244 AddCookie(GURL("http://persistent.com"), "A", "B", std::string(), "/", t); | 243 AddCookie("A", "B", "persistent.com", "/", t); |
| 245 t += base::TimeDelta::FromInternalValue(10); | 244 t += base::TimeDelta::FromInternalValue(10); |
| 246 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), "/", t); | 245 AddCookie("A", "B", "nonpersistent.com", "/", t); |
| 247 | 246 |
| 248 // Now close the store, but the "nonpersistent.com" cookie should not be | 247 // Now close the store, but the "nonpersistent.com" cookie should not be |
| 249 // deleted. | 248 // deleted. |
| 250 store_->SetForceKeepSessionState(); | 249 store_->SetForceKeepSessionState(); |
| 251 DestroyStore(); | 250 DestroyStore(); |
| 252 cookies.clear(); | 251 cookies.clear(); |
| 253 CreateAndLoad(nullptr, &cookies); | 252 CreateAndLoad(nullptr, &cookies); |
| 254 | 253 |
| 255 EXPECT_EQ(3U, cookies.size()); | 254 EXPECT_EQ(3U, cookies.size()); |
| 256 cookies.clear(); | 255 cookies.clear(); |
| 257 } | 256 } |
| 258 | 257 |
| 259 // Tests that the special storage policy is properly applied even when the store | 258 // Tests that the special storage policy is properly applied even when the store |
| 260 // is destroyed on a background thread. | 259 // is destroyed on a background thread. |
| 261 TEST_F(QuotaPolicyCookieStoreTest, TestDestroyOnBackgroundThread) { | 260 TEST_F(QuotaPolicyCookieStoreTest, TestDestroyOnBackgroundThread) { |
| 262 // Specify storage policy that makes "nonpersistent.com" session only. | 261 // Specify storage policy that makes "nonpersistent.com" session only. |
| 263 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | 262 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
| 264 new content::MockSpecialStoragePolicy(); | 263 new content::MockSpecialStoragePolicy(); |
| 265 storage_policy->AddSessionOnly( | 264 storage_policy->AddSessionOnly( |
| 266 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | 265 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); |
| 267 | 266 |
| 268 CanonicalCookieVector cookies; | 267 CanonicalCookieVector cookies; |
| 269 CreateAndLoad(storage_policy.get(), &cookies); | 268 CreateAndLoad(storage_policy.get(), &cookies); |
| 270 ASSERT_EQ(0U, cookies.size()); | 269 ASSERT_EQ(0U, cookies.size()); |
| 271 | 270 |
| 272 base::Time t = base::Time::Now(); | 271 base::Time t = base::Time::Now(); |
| 273 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), "/", t); | 272 AddCookie("A", "B", "nonpersistent.com", "/", t); |
| 274 | 273 |
| 275 // Replace the store, which forces the current store to flush data to | 274 // Replace the store, which forces the current store to flush data to |
| 276 // disk. Then, after reloading the store, confirm that the data was flushed by | 275 // disk. Then, after reloading the store, confirm that the data was flushed by |
| 277 // making sure it loads successfully. This ensures that all pending commits | 276 // making sure it loads successfully. This ensures that all pending commits |
| 278 // are made to the store before allowing it to be closed. | 277 // are made to the store before allowing it to be closed. |
| 279 DestroyStoreOnBackgroundThread(); | 278 DestroyStoreOnBackgroundThread(); |
| 280 | 279 |
| 281 // Reload and test for persistence. | 280 // Reload and test for persistence. |
| 282 cookies.clear(); | 281 cookies.clear(); |
| 283 CreateAndLoad(storage_policy.get(), &cookies); | 282 CreateAndLoad(storage_policy.get(), &cookies); |
| 284 EXPECT_EQ(0U, cookies.size()); | 283 EXPECT_EQ(0U, cookies.size()); |
| 285 | 284 |
| 286 cookies.clear(); | 285 cookies.clear(); |
| 287 } | 286 } |
| 288 | 287 |
| 289 } // namespace | 288 } // namespace |
| 290 } // namespace content | 289 } // namespace content |
| OLD | NEW |