| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 t += base::TimeDelta::FromInternalValue(10); | 143 t += base::TimeDelta::FromInternalValue(10); |
| 144 AddCookie(GURL("http://persistent.com"), "A", "B", std::string(), "/", t); | 144 AddCookie(GURL("http://persistent.com"), "A", "B", std::string(), "/", t); |
| 145 | 145 |
| 146 // Replace the store, which forces the current store to flush data to | 146 // Replace the store, which forces the current store to flush data to |
| 147 // disk. Then, after reloading the store, confirm that the data was flushed by | 147 // disk. Then, after reloading the store, confirm that the data was flushed by |
| 148 // making sure it loads successfully. This ensures that all pending commits | 148 // making sure it loads successfully. This ensures that all pending commits |
| 149 // are made to the store before allowing it to be closed. | 149 // are made to the store before allowing it to be closed. |
| 150 DestroyStore(); | 150 DestroyStore(); |
| 151 | 151 |
| 152 // Reload and test for persistence. | 152 // Reload and test for persistence. |
| 153 STLDeleteElements(&cookies); | 153 base::STLDeleteElements(&cookies); |
| 154 CreateAndLoad(nullptr, &cookies); | 154 CreateAndLoad(nullptr, &cookies); |
| 155 EXPECT_EQ(2U, cookies.size()); | 155 EXPECT_EQ(2U, cookies.size()); |
| 156 bool found_foo_cookie = false; | 156 bool found_foo_cookie = false; |
| 157 bool found_persistent_cookie = false; | 157 bool found_persistent_cookie = false; |
| 158 for (auto* cookie : cookies) { | 158 for (auto* cookie : cookies) { |
| 159 if (cookie->Domain() == "foo.com") | 159 if (cookie->Domain() == "foo.com") |
| 160 found_foo_cookie = true; | 160 found_foo_cookie = true; |
| 161 else if (cookie->Domain() == "persistent.com") | 161 else if (cookie->Domain() == "persistent.com") |
| 162 found_persistent_cookie = true; | 162 found_persistent_cookie = true; |
| 163 } | 163 } |
| 164 EXPECT_TRUE(found_foo_cookie); | 164 EXPECT_TRUE(found_foo_cookie); |
| 165 EXPECT_TRUE(found_persistent_cookie); | 165 EXPECT_TRUE(found_persistent_cookie); |
| 166 | 166 |
| 167 // Now delete the cookies and check persistence again. | 167 // Now delete the cookies and check persistence again. |
| 168 store_->DeleteCookie(*cookies[0]); | 168 store_->DeleteCookie(*cookies[0]); |
| 169 store_->DeleteCookie(*cookies[1]); | 169 store_->DeleteCookie(*cookies[1]); |
| 170 DestroyStore(); | 170 DestroyStore(); |
| 171 | 171 |
| 172 // Reload and check if the cookies have been removed. | 172 // Reload and check if the cookies have been removed. |
| 173 STLDeleteElements(&cookies); | 173 base::STLDeleteElements(&cookies); |
| 174 CreateAndLoad(nullptr, &cookies); | 174 CreateAndLoad(nullptr, &cookies); |
| 175 EXPECT_EQ(0U, cookies.size()); | 175 EXPECT_EQ(0U, cookies.size()); |
| 176 STLDeleteElements(&cookies); | 176 base::STLDeleteElements(&cookies); |
| 177 } | 177 } |
| 178 | 178 |
| 179 // Test if data is stored as expected in the QuotaPolicy database. | 179 // Test if data is stored as expected in the QuotaPolicy database. |
| 180 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) { | 180 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) { |
| 181 CanonicalCookieVector cookies; | 181 CanonicalCookieVector cookies; |
| 182 CreateAndLoad(nullptr, &cookies); | 182 CreateAndLoad(nullptr, &cookies); |
| 183 ASSERT_EQ(0U, cookies.size()); | 183 ASSERT_EQ(0U, cookies.size()); |
| 184 | 184 |
| 185 base::Time t = base::Time::Now(); | 185 base::Time t = base::Time::Now(); |
| 186 AddCookie(GURL("http://foo.com"), "A", "B", std::string(), "/", t); | 186 AddCookie(GURL("http://foo.com"), "A", "B", std::string(), "/", t); |
| 187 t += base::TimeDelta::FromInternalValue(10); | 187 t += base::TimeDelta::FromInternalValue(10); |
| 188 AddCookie(GURL("http://persistent.com"), "A", "B", std::string(), "/", t); | 188 AddCookie(GURL("http://persistent.com"), "A", "B", std::string(), "/", t); |
| 189 t += base::TimeDelta::FromInternalValue(10); | 189 t += base::TimeDelta::FromInternalValue(10); |
| 190 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), "/", t); | 190 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), "/", t); |
| 191 | 191 |
| 192 // Replace the store, which forces the current store to flush data to | 192 // Replace the store, which forces the current store to flush data to |
| 193 // disk. Then, after reloading the store, confirm that the data was flushed by | 193 // disk. Then, after reloading the store, confirm that the data was flushed by |
| 194 // making sure it loads successfully. This ensures that all pending commits | 194 // making sure it loads successfully. This ensures that all pending commits |
| 195 // are made to the store before allowing it to be closed. | 195 // are made to the store before allowing it to be closed. |
| 196 DestroyStore(); | 196 DestroyStore(); |
| 197 // Specify storage policy that makes "nonpersistent.com" session only. | 197 // Specify storage policy that makes "nonpersistent.com" session only. |
| 198 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | 198 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
| 199 new content::MockSpecialStoragePolicy(); | 199 new content::MockSpecialStoragePolicy(); |
| 200 storage_policy->AddSessionOnly( | 200 storage_policy->AddSessionOnly( |
| 201 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | 201 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); |
| 202 | 202 |
| 203 // Reload and test for persistence. | 203 // Reload and test for persistence. |
| 204 STLDeleteElements(&cookies); | 204 base::STLDeleteElements(&cookies); |
| 205 CreateAndLoad(storage_policy.get(), &cookies); | 205 CreateAndLoad(storage_policy.get(), &cookies); |
| 206 EXPECT_EQ(3U, cookies.size()); | 206 EXPECT_EQ(3U, cookies.size()); |
| 207 | 207 |
| 208 t += base::TimeDelta::FromInternalValue(10); | 208 t += base::TimeDelta::FromInternalValue(10); |
| 209 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), | 209 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), |
| 210 "/second", t); | 210 "/second", t); |
| 211 | 211 |
| 212 // Now close the store, and "nonpersistent.com" should be deleted according to | 212 // Now close the store, and "nonpersistent.com" should be deleted according to |
| 213 // policy. | 213 // policy. |
| 214 DestroyStore(); | 214 DestroyStore(); |
| 215 STLDeleteElements(&cookies); | 215 base::STLDeleteElements(&cookies); |
| 216 CreateAndLoad(nullptr, &cookies); | 216 CreateAndLoad(nullptr, &cookies); |
| 217 | 217 |
| 218 EXPECT_EQ(2U, cookies.size()); | 218 EXPECT_EQ(2U, cookies.size()); |
| 219 for (auto* cookie : cookies) { | 219 for (auto* cookie : cookies) { |
| 220 EXPECT_NE("nonpersistent.com", cookie->Domain()); | 220 EXPECT_NE("nonpersistent.com", cookie->Domain()); |
| 221 } | 221 } |
| 222 STLDeleteElements(&cookies); | 222 base::STLDeleteElements(&cookies); |
| 223 } | 223 } |
| 224 | 224 |
| 225 TEST_F(QuotaPolicyCookieStoreTest, ForceKeepSessionState) { | 225 TEST_F(QuotaPolicyCookieStoreTest, ForceKeepSessionState) { |
| 226 CanonicalCookieVector cookies; | 226 CanonicalCookieVector cookies; |
| 227 CreateAndLoad(nullptr, &cookies); | 227 CreateAndLoad(nullptr, &cookies); |
| 228 ASSERT_EQ(0U, cookies.size()); | 228 ASSERT_EQ(0U, cookies.size()); |
| 229 | 229 |
| 230 base::Time t = base::Time::Now(); | 230 base::Time t = base::Time::Now(); |
| 231 AddCookie(GURL("http://foo.com"), "A", "B", std::string(), "/", t); | 231 AddCookie(GURL("http://foo.com"), "A", "B", std::string(), "/", t); |
| 232 | 232 |
| 233 // Recreate |store_| with a storage policy that makes "nonpersistent.com" | 233 // Recreate |store_| with a storage policy that makes "nonpersistent.com" |
| 234 // session only, but then instruct the store to forcibly keep all cookies. | 234 // session only, but then instruct the store to forcibly keep all cookies. |
| 235 DestroyStore(); | 235 DestroyStore(); |
| 236 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | 236 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
| 237 new content::MockSpecialStoragePolicy(); | 237 new content::MockSpecialStoragePolicy(); |
| 238 storage_policy->AddSessionOnly( | 238 storage_policy->AddSessionOnly( |
| 239 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | 239 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); |
| 240 | 240 |
| 241 // Reload and test for persistence | 241 // Reload and test for persistence |
| 242 STLDeleteElements(&cookies); | 242 base::STLDeleteElements(&cookies); |
| 243 CreateAndLoad(storage_policy.get(), &cookies); | 243 CreateAndLoad(storage_policy.get(), &cookies); |
| 244 EXPECT_EQ(1U, cookies.size()); | 244 EXPECT_EQ(1U, cookies.size()); |
| 245 | 245 |
| 246 t += base::TimeDelta::FromInternalValue(10); | 246 t += base::TimeDelta::FromInternalValue(10); |
| 247 AddCookie(GURL("http://persistent.com"), "A", "B", std::string(), "/", t); | 247 AddCookie(GURL("http://persistent.com"), "A", "B", std::string(), "/", t); |
| 248 t += base::TimeDelta::FromInternalValue(10); | 248 t += base::TimeDelta::FromInternalValue(10); |
| 249 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), "/", t); | 249 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), "/", t); |
| 250 | 250 |
| 251 // Now close the store, but the "nonpersistent.com" cookie should not be | 251 // Now close the store, but the "nonpersistent.com" cookie should not be |
| 252 // deleted. | 252 // deleted. |
| 253 store_->SetForceKeepSessionState(); | 253 store_->SetForceKeepSessionState(); |
| 254 DestroyStore(); | 254 DestroyStore(); |
| 255 STLDeleteElements(&cookies); | 255 base::STLDeleteElements(&cookies); |
| 256 CreateAndLoad(nullptr, &cookies); | 256 CreateAndLoad(nullptr, &cookies); |
| 257 | 257 |
| 258 EXPECT_EQ(3U, cookies.size()); | 258 EXPECT_EQ(3U, cookies.size()); |
| 259 STLDeleteElements(&cookies); | 259 base::STLDeleteElements(&cookies); |
| 260 } | 260 } |
| 261 | 261 |
| 262 // Tests that the special storage policy is properly applied even when the store | 262 // Tests that the special storage policy is properly applied even when the store |
| 263 // is destroyed on a background thread. | 263 // is destroyed on a background thread. |
| 264 TEST_F(QuotaPolicyCookieStoreTest, TestDestroyOnBackgroundThread) { | 264 TEST_F(QuotaPolicyCookieStoreTest, TestDestroyOnBackgroundThread) { |
| 265 // Specify storage policy that makes "nonpersistent.com" session only. | 265 // Specify storage policy that makes "nonpersistent.com" session only. |
| 266 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | 266 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
| 267 new content::MockSpecialStoragePolicy(); | 267 new content::MockSpecialStoragePolicy(); |
| 268 storage_policy->AddSessionOnly( | 268 storage_policy->AddSessionOnly( |
| 269 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | 269 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); |
| 270 | 270 |
| 271 CanonicalCookieVector cookies; | 271 CanonicalCookieVector cookies; |
| 272 CreateAndLoad(storage_policy.get(), &cookies); | 272 CreateAndLoad(storage_policy.get(), &cookies); |
| 273 ASSERT_EQ(0U, cookies.size()); | 273 ASSERT_EQ(0U, cookies.size()); |
| 274 | 274 |
| 275 base::Time t = base::Time::Now(); | 275 base::Time t = base::Time::Now(); |
| 276 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), "/", t); | 276 AddCookie(GURL("http://nonpersistent.com"), "A", "B", std::string(), "/", t); |
| 277 | 277 |
| 278 // Replace the store, which forces the current store to flush data to | 278 // Replace the store, which forces the current store to flush data to |
| 279 // disk. Then, after reloading the store, confirm that the data was flushed by | 279 // disk. Then, after reloading the store, confirm that the data was flushed by |
| 280 // making sure it loads successfully. This ensures that all pending commits | 280 // making sure it loads successfully. This ensures that all pending commits |
| 281 // are made to the store before allowing it to be closed. | 281 // are made to the store before allowing it to be closed. |
| 282 DestroyStoreOnBackgroundThread(); | 282 DestroyStoreOnBackgroundThread(); |
| 283 | 283 |
| 284 // Reload and test for persistence. | 284 // Reload and test for persistence. |
| 285 STLDeleteElements(&cookies); | 285 base::STLDeleteElements(&cookies); |
| 286 CreateAndLoad(storage_policy.get(), &cookies); | 286 CreateAndLoad(storage_policy.get(), &cookies); |
| 287 EXPECT_EQ(0U, cookies.size()); | 287 EXPECT_EQ(0U, cookies.size()); |
| 288 | 288 |
| 289 STLDeleteElements(&cookies); | 289 base::STLDeleteElements(&cookies); |
| 290 } | 290 } |
| 291 | 291 |
| 292 } // namespace | 292 } // namespace |
| 293 } // namespace content | 293 } // namespace content |
| OLD | NEW |