OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/sqlite_persistent_cookie_store.h" | 5 #include "content/browser/net/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 void AddCookie(const std::string& name, | 157 void AddCookie(const std::string& name, |
158 const std::string& value, | 158 const std::string& value, |
159 const std::string& domain, | 159 const std::string& domain, |
160 const std::string& path, | 160 const std::string& path, |
161 const base::Time& creation) { | 161 const base::Time& creation) { |
162 store_->AddCookie(net::CanonicalCookie( | 162 store_->AddCookie(net::CanonicalCookie( |
163 GURL(), name, value, domain, path, creation, creation, creation, false, | 163 GURL(), name, value, domain, path, creation, creation, creation, false, |
164 false, false, net::COOKIE_PRIORITY_DEFAULT)); | 164 false, false, net::COOKIE_PRIORITY_DEFAULT)); |
165 } | 165 } |
166 | 166 |
| 167 void AddCookieWithExpiration(const std::string& name, |
| 168 const std::string& value, |
| 169 const std::string& domain, |
| 170 const std::string& path, |
| 171 const base::Time& creation, |
| 172 const base::Time& expiration) { |
| 173 store_->AddCookie(net::CanonicalCookie( |
| 174 GURL(), name, value, domain, path, creation, expiration, creation, |
| 175 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); |
| 176 } |
| 177 |
167 std::string ReadRawDBContents() { | 178 std::string ReadRawDBContents() { |
168 std::string contents; | 179 std::string contents; |
169 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), | 180 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), |
170 &contents)) | 181 &contents)) |
171 return std::string(); | 182 return std::string(); |
172 return contents; | 183 return contents; |
173 } | 184 } |
174 | 185 |
175 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 186 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
176 | 187 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 // Now delete the cookie and check persistence again. | 261 // Now delete the cookie and check persistence again. |
251 store_->DeleteCookie(*cookies[0]); | 262 store_->DeleteCookie(*cookies[0]); |
252 DestroyStore(); | 263 DestroyStore(); |
253 STLDeleteElements(&cookies); | 264 STLDeleteElements(&cookies); |
254 | 265 |
255 // Reload and check if the cookie has been removed. | 266 // Reload and check if the cookie has been removed. |
256 CreateAndLoad(false, false, &cookies); | 267 CreateAndLoad(false, false, &cookies); |
257 ASSERT_EQ(0U, cookies.size()); | 268 ASSERT_EQ(0U, cookies.size()); |
258 } | 269 } |
259 | 270 |
| 271 TEST_F(SQLitePersistentCookieStoreTest, TestSessionCookiesDeletedOnStartup) { |
| 272 // Initialize the cookie store with 3 persistent cookies, 5 transient |
| 273 // cookies. |
| 274 InitializeStore(false, false); |
| 275 |
| 276 // Add persistent cookies. |
| 277 base::Time t = base::Time::Now(); |
| 278 AddCookie("A", "B", "a1.com", "/", t); |
| 279 t += base::TimeDelta::FromInternalValue(10); |
| 280 AddCookie("A", "B", "a2.com", "/", t); |
| 281 t += base::TimeDelta::FromInternalValue(10); |
| 282 AddCookie("A", "B", "a3.com", "/", t); |
| 283 |
| 284 // Add transient cookies. |
| 285 t += base::TimeDelta::FromInternalValue(10); |
| 286 AddCookieWithExpiration("A", "B", "b1.com", "/", t, base::Time()); |
| 287 t += base::TimeDelta::FromInternalValue(10); |
| 288 AddCookieWithExpiration("A", "B", "b2.com", "/", t, base::Time()); |
| 289 t += base::TimeDelta::FromInternalValue(10); |
| 290 AddCookieWithExpiration("A", "B", "b3.com", "/", t, base::Time()); |
| 291 t += base::TimeDelta::FromInternalValue(10); |
| 292 AddCookieWithExpiration("A", "B", "b4.com", "/", t, base::Time()); |
| 293 t += base::TimeDelta::FromInternalValue(10); |
| 294 AddCookieWithExpiration("A", "B", "b5.com", "/", t, base::Time()); |
| 295 DestroyStore(); |
| 296 |
| 297 // Load the store a second time. Before the store finishes loading, add a |
| 298 // transient cookie and flush it to disk. |
| 299 store_ = new SQLitePersistentCookieStore( |
| 300 temp_dir_.path().Append(kCookieFilename), |
| 301 client_task_runner(), |
| 302 background_task_runner(), |
| 303 false, NULL, NULL); |
| 304 |
| 305 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
| 306 // until both Load and Flush have been posted to its task queue. |
| 307 background_task_runner()->PostTask( |
| 308 FROM_HERE, |
| 309 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
| 310 base::Unretained(this))); |
| 311 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 312 base::Unretained(this))); |
| 313 t += base::TimeDelta::FromInternalValue(10); |
| 314 AddCookieWithExpiration("A", "B", "c.com", "/", t, base::Time()); |
| 315 base::WaitableEvent event(false, false); |
| 316 store_->Flush(base::Bind(&base::WaitableEvent::Signal, |
| 317 base::Unretained(&event))); |
| 318 |
| 319 // Now the DB-thread queue contains: |
| 320 // (active:) |
| 321 // 1. Wait (on db_event) |
| 322 // (pending:) |
| 323 // 2. "Init And Chain-Load First Domain" |
| 324 // 3. Add Cookie (c.com) |
| 325 // 4. Flush Cookie (c.com) |
| 326 db_thread_event_.Signal(); |
| 327 event.Wait(); |
| 328 loaded_event_.Wait(); |
| 329 STLDeleteElements(&cookies_); |
| 330 DestroyStore(); |
| 331 |
| 332 // Load the store a third time, this time restoring session cookies. The |
| 333 // store should contain exactly 4 cookies: the 3 persistent, and "c.com", |
| 334 // which was added during the second cookie store load. |
| 335 store_ = new SQLitePersistentCookieStore( |
| 336 temp_dir_.path().Append(kCookieFilename), |
| 337 client_task_runner(), |
| 338 background_task_runner(), |
| 339 true, NULL, NULL); |
| 340 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 341 base::Unretained(this))); |
| 342 loaded_event_.Wait(); |
| 343 ASSERT_EQ(4u, cookies_.size()); |
| 344 STLDeleteElements(&cookies_); |
| 345 } |
| 346 |
260 // Test that priority load of cookies for a specfic domain key could be | 347 // Test that priority load of cookies for a specfic domain key could be |
261 // completed before the entire store is loaded | 348 // completed before the entire store is loaded |
262 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | 349 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
263 InitializeStore(false, false); | 350 InitializeStore(false, false); |
264 base::Time t = base::Time::Now(); | 351 base::Time t = base::Time::Now(); |
265 AddCookie("A", "B", "foo.bar", "/", t); | 352 AddCookie("A", "B", "foo.bar", "/", t); |
266 t += base::TimeDelta::FromInternalValue(10); | 353 t += base::TimeDelta::FromInternalValue(10); |
267 AddCookie("A", "B", "www.aaa.com", "/", t); | 354 AddCookie("A", "B", "www.aaa.com", "/", t); |
268 t += base::TimeDelta::FromInternalValue(10); | 355 t += base::TimeDelta::FromInternalValue(10); |
269 AddCookie("A", "B", "travel.aaa.com", "/", t); | 356 AddCookie("A", "B", "travel.aaa.com", "/", t); |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 EXPECT_EQ(2, resultcount); | 672 EXPECT_EQ(2, resultcount); |
586 | 673 |
587 // Verify that "encrypted_value" is NOT visible in the file. | 674 // Verify that "encrypted_value" is NOT visible in the file. |
588 contents = ReadRawDBContents(); | 675 contents = ReadRawDBContents(); |
589 EXPECT_NE(0U, contents.length()); | 676 EXPECT_NE(0U, contents.length()); |
590 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); | 677 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); |
591 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); | 678 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); |
592 } | 679 } |
593 | 680 |
594 } // namespace content | 681 } // namespace content |
OLD | NEW |