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 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 | 324 |
325 // Create a store that loads session cookies and test that the session cookie | 325 // Create a store that loads session cookies and test that the session cookie |
326 // was loaded. | 326 // was loaded. |
327 CanonicalCookieVector cookies; | 327 CanonicalCookieVector cookies; |
328 CreateAndLoad(true, &cookies); | 328 CreateAndLoad(true, &cookies); |
329 | 329 |
330 ASSERT_EQ(1U, cookies.size()); | 330 ASSERT_EQ(1U, cookies.size()); |
331 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); | 331 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
332 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 332 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
333 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 333 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
| 334 ASSERT_EQ(net::COOKIE_PRIORITY_DEFAULT, cookies[0]->Priority()); |
334 | 335 |
335 STLDeleteElements(&cookies); | 336 STLDeleteElements(&cookies); |
336 } | 337 } |
337 | 338 |
338 // Test loading old session cookies from the disk. | 339 // Test loading old session cookies from the disk. |
339 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 340 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
340 InitializeStore(true); | 341 InitializeStore(true); |
341 | 342 |
342 // Add a session cookie. | 343 // Add a session cookie. |
343 store_->AddCookie( | 344 store_->AddCookie( |
(...skipping 29 matching lines...) Expand all Loading... |
373 // Add a session cookie. | 374 // Add a session cookie. |
374 store_->AddCookie( | 375 store_->AddCookie( |
375 net::CanonicalCookie( | 376 net::CanonicalCookie( |
376 GURL(), kSessionName, "val", "sessioncookie.com", "/", | 377 GURL(), kSessionName, "val", "sessioncookie.com", "/", |
377 base::Time::Now(), base::Time(), base::Time::Now(), false, false, | 378 base::Time::Now(), base::Time(), base::Time::Now(), false, false, |
378 net::COOKIE_PRIORITY_DEFAULT)); | 379 net::COOKIE_PRIORITY_DEFAULT)); |
379 // Add a persistent cookie. | 380 // Add a persistent cookie. |
380 store_->AddCookie( | 381 store_->AddCookie( |
381 net::CanonicalCookie( | 382 net::CanonicalCookie( |
382 GURL(), kPersistentName, "val", "sessioncookie.com", "/", | 383 GURL(), kPersistentName, "val", "sessioncookie.com", "/", |
383 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), | 384 base::Time::Now() - base::TimeDelta::FromDays(1), |
| 385 base::Time::Now() + base::TimeDelta::FromDays(1), |
384 base::Time::Now(), false, false, | 386 base::Time::Now(), false, false, |
385 net::COOKIE_PRIORITY_DEFAULT)); | 387 net::COOKIE_PRIORITY_DEFAULT)); |
386 | 388 |
387 // Create a store that loads session cookie and test that the the IsPersistent | 389 // Force the store to write its data to the disk. |
| 390 DestroyStore(); |
| 391 |
| 392 // Create a store that loads session cookie and test that the IsPersistent |
388 // attribute is restored. | 393 // attribute is restored. |
389 CanonicalCookieVector cookies; | 394 CanonicalCookieVector cookies; |
390 CreateAndLoad(true, &cookies); | 395 CreateAndLoad(true, &cookies); |
391 ASSERT_EQ(2U, cookies.size()); | 396 ASSERT_EQ(2U, cookies.size()); |
392 | 397 |
393 std::map<std::string, net::CanonicalCookie*> cookie_map; | 398 std::map<std::string, net::CanonicalCookie*> cookie_map; |
394 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 399 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
395 it != cookies.end(); | 400 it != cookies.end(); |
396 ++it) { | 401 ++it) { |
397 cookie_map[(*it)->Name()] = *it; | 402 cookie_map[(*it)->Name()] = *it; |
398 } | 403 } |
399 | 404 |
400 std::map<std::string, net::CanonicalCookie*>::const_iterator it = | 405 std::map<std::string, net::CanonicalCookie*>::const_iterator it = |
401 cookie_map.find(kSessionName); | 406 cookie_map.find(kSessionName); |
402 ASSERT_TRUE(it != cookie_map.end()); | 407 ASSERT_TRUE(it != cookie_map.end()); |
403 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); | 408 EXPECT_FALSE(cookie_map[kSessionName]->IsPersistent()); |
404 | 409 |
405 it = cookie_map.find(kPersistentName); | 410 it = cookie_map.find(kPersistentName); |
406 ASSERT_TRUE(it != cookie_map.end()); | 411 ASSERT_TRUE(it != cookie_map.end()); |
407 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); | 412 EXPECT_TRUE(cookie_map[kPersistentName]->IsPersistent()); |
408 | 413 |
409 STLDeleteElements(&cookies); | 414 STLDeleteElements(&cookies); |
410 } | 415 } |
411 | 416 |
| 417 TEST_F(SQLitePersistentCookieStoreTest, PriorityIsPersistent) { |
| 418 static const char kLowName[] = "low"; |
| 419 static const char kMediumName[] = "medium"; |
| 420 static const char kHighName[] = "high"; |
| 421 static const char kCookieDomain[] = "sessioncookie.com"; |
| 422 static const char kCookieValue[] = "value"; |
| 423 static const char kCookiePath[] = "/"; |
| 424 |
| 425 InitializeStore(true); |
| 426 |
| 427 // Add a low-priority persistent cookie. |
| 428 store_->AddCookie( |
| 429 net::CanonicalCookie( |
| 430 GURL(), kLowName, kCookieValue, kCookieDomain, kCookiePath, |
| 431 base::Time::Now() - base::TimeDelta::FromMinutes(1), |
| 432 base::Time::Now() + base::TimeDelta::FromDays(1), |
| 433 base::Time::Now(), false, false, |
| 434 net::COOKIE_PRIORITY_LOW)); |
| 435 |
| 436 // Add a medium-priority persistent cookie. |
| 437 store_->AddCookie( |
| 438 net::CanonicalCookie( |
| 439 GURL(), kMediumName, kCookieValue, kCookieDomain, kCookiePath, |
| 440 base::Time::Now() - base::TimeDelta::FromMinutes(2), |
| 441 base::Time::Now() + base::TimeDelta::FromDays(1), |
| 442 base::Time::Now(), false, false, |
| 443 net::COOKIE_PRIORITY_MEDIUM)); |
| 444 |
| 445 // Add a high-priority peristent cookie. |
| 446 store_->AddCookie( |
| 447 net::CanonicalCookie( |
| 448 GURL(), kHighName, kCookieValue, kCookieDomain, kCookiePath, |
| 449 base::Time::Now() - base::TimeDelta::FromMinutes(3), |
| 450 base::Time::Now() + base::TimeDelta::FromDays(1), |
| 451 base::Time::Now(), false, false, |
| 452 net::COOKIE_PRIORITY_HIGH)); |
| 453 |
| 454 // Force the store to write its data to the disk. |
| 455 DestroyStore(); |
| 456 |
| 457 // Create a store that loads session cookie and test that the priority |
| 458 // attribute values are restored. |
| 459 CanonicalCookieVector cookies; |
| 460 CreateAndLoad(true, &cookies); |
| 461 ASSERT_EQ(3U, cookies.size()); |
| 462 |
| 463 // Put the cookies into a map, by name, so we can easily find them. |
| 464 std::map<std::string, net::CanonicalCookie*> cookie_map; |
| 465 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
| 466 it != cookies.end(); |
| 467 ++it) { |
| 468 cookie_map[(*it)->Name()] = *it; |
| 469 } |
| 470 |
| 471 // Validate that each cookie has the correct priority. |
| 472 std::map<std::string, net::CanonicalCookie*>::const_iterator it = |
| 473 cookie_map.find(kLowName); |
| 474 ASSERT_TRUE(it != cookie_map.end()); |
| 475 EXPECT_EQ(net::COOKIE_PRIORITY_LOW, cookie_map[kLowName]->Priority()); |
| 476 |
| 477 it = cookie_map.find(kMediumName); |
| 478 ASSERT_TRUE(it != cookie_map.end()); |
| 479 EXPECT_EQ(net::COOKIE_PRIORITY_MEDIUM, cookie_map[kMediumName]->Priority()); |
| 480 |
| 481 it = cookie_map.find(kHighName); |
| 482 ASSERT_TRUE(it != cookie_map.end()); |
| 483 EXPECT_EQ(net::COOKIE_PRIORITY_HIGH, cookie_map[kHighName]->Priority()); |
| 484 |
| 485 STLDeleteElements(&cookies); |
| 486 } |
| 487 |
412 } // namespace content | 488 } // namespace content |
OLD | NEW |