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 "net/cookies/cookie_store_unittest.h" | 5 #include "net/cookies/cookie_store_unittest.h" |
6 | 6 |
7 #include <time.h> | 7 #include <time.h> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 966 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 options.set_include_httponly(); | 977 options.set_include_httponly(); |
978 | 978 |
979 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); | 979 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); |
980 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); | 980 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); |
981 | 981 |
982 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "C=D; httponly", options)); | 982 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "C=D; httponly", options)); |
983 EXPECT_EQ("A=B; C=D", GetCookiesWithOptions(cm, url_google_, options)); | 983 EXPECT_EQ("A=B; C=D", GetCookiesWithOptions(cm, url_google_, options)); |
984 | 984 |
985 EXPECT_EQ(2, DeleteAll(cm)); | 985 EXPECT_EQ(2, DeleteAll(cm)); |
986 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); | 986 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); |
987 | 987 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
| 988 // If a cookie is persistent then its commands will be recorded. |
| 989 // Each above cookie has 2 commands: 1 for add and 1 for delete. |
| 990 EXPECT_EQ(4u, store->commands().size()); |
| 991 #else |
988 EXPECT_EQ(0u, store->commands().size()); | 992 EXPECT_EQ(0u, store->commands().size()); |
| 993 #endif |
989 | 994 |
990 // Create a persistent cookie. | 995 // Create a persistent cookie. |
991 EXPECT_TRUE(SetCookie(cm, url_google_, | 996 EXPECT_TRUE(SetCookie(cm, url_google_, |
992 std::string(kValidCookieLine) + | 997 std::string(kValidCookieLine) + |
993 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); | 998 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); |
| 999 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
| 1000 ASSERT_EQ(5u, store->commands().size()); |
| 1001 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); |
| 1002 #else |
994 ASSERT_EQ(1u, store->commands().size()); | 1003 ASSERT_EQ(1u, store->commands().size()); |
995 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); | 1004 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); |
| 1005 #endif |
996 | 1006 |
997 EXPECT_EQ(1, DeleteAll(cm)); // sync_to_store = true. | 1007 EXPECT_EQ(1, DeleteAll(cm)); // sync_to_store = true. |
| 1008 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
| 1009 ASSERT_EQ(6u, store->commands().size()); |
| 1010 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[5].type); |
| 1011 #else |
998 ASSERT_EQ(2u, store->commands().size()); | 1012 ASSERT_EQ(2u, store->commands().size()); |
999 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); | 1013 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); |
| 1014 #endif |
1000 | 1015 |
1001 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); | 1016 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); |
1002 } | 1017 } |
1003 | 1018 |
1004 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) { | 1019 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) { |
1005 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 1020 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
1006 Time now = Time::Now(); | 1021 Time now = Time::Now(); |
1007 | 1022 |
1008 // Nothing has been added so nothing should be deleted. | 1023 // Nothing has been added so nothing should be deleted. |
1009 EXPECT_EQ(0, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99), | 1024 EXPECT_EQ(0, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99), |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1424 delegate->reset(); | 1439 delegate->reset(); |
1425 | 1440 |
1426 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E")); | 1441 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E")); |
1427 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_)); | 1442 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_)); |
1428 EXPECT_EQ(0u, delegate->changes().size()); | 1443 EXPECT_EQ(0u, delegate->changes().size()); |
1429 | 1444 |
1430 // Insert a cookie "a" for path "/path1" | 1445 // Insert a cookie "a" for path "/path1" |
1431 EXPECT_TRUE( | 1446 EXPECT_TRUE( |
1432 SetCookie(cm, url_google_, "a=val1; path=/path1; " | 1447 SetCookie(cm, url_google_, "a=val1; path=/path1; " |
1433 "expires=Mon, 18-Apr-22 22:50:13 GMT")); | 1448 "expires=Mon, 18-Apr-22 22:50:13 GMT")); |
| 1449 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
| 1450 ASSERT_EQ(5u, store->commands().size()); |
| 1451 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); |
| 1452 #else |
1434 ASSERT_EQ(1u, store->commands().size()); | 1453 ASSERT_EQ(1u, store->commands().size()); |
1435 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); | 1454 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); |
| 1455 #endif |
1436 ASSERT_EQ(1u, delegate->changes().size()); | 1456 ASSERT_EQ(1u, delegate->changes().size()); |
1437 EXPECT_FALSE(delegate->changes()[0].second); | 1457 EXPECT_FALSE(delegate->changes()[0].second); |
1438 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); | 1458 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); |
1439 EXPECT_EQ("a", delegate->changes()[0].first.Name()); | 1459 EXPECT_EQ("a", delegate->changes()[0].first.Name()); |
1440 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); | 1460 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); |
1441 delegate->reset(); | 1461 delegate->reset(); |
1442 | 1462 |
1443 // Insert a cookie "a" for path "/path1", that is httponly. This should | 1463 // Insert a cookie "a" for path "/path1", that is httponly. This should |
1444 // overwrite the non-http-only version. | 1464 // overwrite the non-http-only version. |
1445 CookieOptions allow_httponly; | 1465 CookieOptions allow_httponly; |
1446 allow_httponly.set_include_httponly(); | 1466 allow_httponly.set_include_httponly(); |
1447 EXPECT_TRUE( | 1467 EXPECT_TRUE( |
1448 SetCookieWithOptions(cm, url_google_, | 1468 SetCookieWithOptions(cm, url_google_, |
1449 "a=val2; path=/path1; httponly; " | 1469 "a=val2; path=/path1; httponly; " |
1450 "expires=Mon, 18-Apr-22 22:50:14 GMT", | 1470 "expires=Mon, 18-Apr-22 22:50:14 GMT", |
1451 allow_httponly)); | 1471 allow_httponly)); |
| 1472 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
| 1473 ASSERT_EQ(7u, store->commands().size()); |
| 1474 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[5].type); |
| 1475 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[6].type); |
| 1476 #else |
1452 ASSERT_EQ(3u, store->commands().size()); | 1477 ASSERT_EQ(3u, store->commands().size()); |
1453 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); | 1478 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); |
1454 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type); | 1479 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type); |
| 1480 #endif |
1455 ASSERT_EQ(2u, delegate->changes().size()); | 1481 ASSERT_EQ(2u, delegate->changes().size()); |
1456 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); | 1482 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); |
1457 EXPECT_TRUE(delegate->changes()[0].second); | 1483 EXPECT_TRUE(delegate->changes()[0].second); |
1458 EXPECT_EQ("a", delegate->changes()[0].first.Name()); | 1484 EXPECT_EQ("a", delegate->changes()[0].first.Name()); |
1459 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); | 1485 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); |
1460 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain()); | 1486 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain()); |
1461 EXPECT_FALSE(delegate->changes()[1].second); | 1487 EXPECT_FALSE(delegate->changes()[1].second); |
1462 EXPECT_EQ("a", delegate->changes()[1].first.Name()); | 1488 EXPECT_EQ("a", delegate->changes()[1].first.Name()); |
1463 EXPECT_EQ("val2", delegate->changes()[1].first.Value()); | 1489 EXPECT_EQ("val2", delegate->changes()[1].first.Value()); |
1464 delegate->reset(); | 1490 delegate->reset(); |
(...skipping 30 matching lines...) Expand all Loading... |
1495 false, false)); | 1521 false, false)); |
1496 | 1522 |
1497 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_); | 1523 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_); |
1498 CookieList::iterator it = cookies.begin(); | 1524 CookieList::iterator it = cookies.begin(); |
1499 | 1525 |
1500 ASSERT_TRUE(it != cookies.end()); | 1526 ASSERT_TRUE(it != cookies.end()); |
1501 EXPECT_EQ("A", it->Name()); | 1527 EXPECT_EQ("A", it->Name()); |
1502 EXPECT_EQ("B", it->Value()); | 1528 EXPECT_EQ("B", it->Value()); |
1503 EXPECT_EQ("www.google.izzle", it->Domain()); | 1529 EXPECT_EQ("www.google.izzle", it->Domain()); |
1504 EXPECT_EQ("/foo", it->Path()); | 1530 EXPECT_EQ("/foo", it->Path()); |
| 1531 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
| 1532 EXPECT_TRUE(it->IsPersistent()); |
| 1533 #else |
1505 EXPECT_FALSE(it->IsPersistent()); | 1534 EXPECT_FALSE(it->IsPersistent()); |
| 1535 #endif |
1506 EXPECT_FALSE(it->IsSecure()); | 1536 EXPECT_FALSE(it->IsSecure()); |
1507 EXPECT_FALSE(it->IsHttpOnly()); | 1537 EXPECT_FALSE(it->IsHttpOnly()); |
1508 | 1538 |
1509 ASSERT_TRUE(++it == cookies.end()); | 1539 ASSERT_TRUE(++it == cookies.end()); |
1510 | 1540 |
1511 cookies = GetAllCookiesForURL(cm, url_google_bar_); | 1541 cookies = GetAllCookiesForURL(cm, url_google_bar_); |
1512 it = cookies.begin(); | 1542 it = cookies.begin(); |
1513 | 1543 |
1514 ASSERT_TRUE(it != cookies.end()); | 1544 ASSERT_TRUE(it != cookies.end()); |
1515 EXPECT_EQ("C", it->Name()); | 1545 EXPECT_EQ("C", it->Name()); |
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2057 false, false)); | 2087 false, false)); |
2058 | 2088 |
2059 base::Histogram::SampleSet histogram_set_2; | 2089 base::Histogram::SampleSet histogram_set_2; |
2060 expired_histogram->SnapshotSample(&histogram_set_2); | 2090 expired_histogram->SnapshotSample(&histogram_set_2); |
2061 EXPECT_EQ(histogram_set_1.TotalCount() + 1, | 2091 EXPECT_EQ(histogram_set_1.TotalCount() + 1, |
2062 histogram_set_2.TotalCount()); | 2092 histogram_set_2.TotalCount()); |
2063 | 2093 |
2064 // kValidCookieLine creates a session cookie. | 2094 // kValidCookieLine creates a session cookie. |
2065 ASSERT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); | 2095 ASSERT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); |
2066 expired_histogram->SnapshotSample(&histogram_set_1); | 2096 expired_histogram->SnapshotSample(&histogram_set_1); |
| 2097 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
| 2098 EXPECT_EQ(histogram_set_2.TotalCount() + 1, |
| 2099 histogram_set_1.TotalCount()); |
| 2100 #else |
2067 EXPECT_EQ(histogram_set_2.TotalCount(), | 2101 EXPECT_EQ(histogram_set_2.TotalCount(), |
2068 histogram_set_1.TotalCount()); | 2102 histogram_set_1.TotalCount()); |
| 2103 #endif |
2069 } | 2104 } |
2070 | 2105 |
2071 namespace { | 2106 namespace { |
2072 | 2107 |
2073 class MultiThreadedCookieMonsterTest : public CookieMonsterTest { | 2108 class MultiThreadedCookieMonsterTest : public CookieMonsterTest { |
2074 public: | 2109 public: |
2075 MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {} | 2110 MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {} |
2076 | 2111 |
2077 // Helper methods for calling the asynchronous CookieMonster methods | 2112 // Helper methods for calling the asynchronous CookieMonster methods |
2078 // from a different thread. | 2113 // from a different thread. |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2297 RunOnOtherThread(task); | 2332 RunOnOtherThread(task); |
2298 EXPECT_TRUE(callback.did_run()); | 2333 EXPECT_TRUE(callback.did_run()); |
2299 EXPECT_TRUE(callback.result()); | 2334 EXPECT_TRUE(callback.result()); |
2300 } | 2335 } |
2301 | 2336 |
2302 TEST_F(CookieMonsterTest, InvalidExpiryTime) { | 2337 TEST_F(CookieMonsterTest, InvalidExpiryTime) { |
2303 ParsedCookie pc(std::string(kValidCookieLine) + "; expires=Blarg arg arg"); | 2338 ParsedCookie pc(std::string(kValidCookieLine) + "; expires=Blarg arg arg"); |
2304 scoped_ptr<CookieMonster::CanonicalCookie> cookie( | 2339 scoped_ptr<CookieMonster::CanonicalCookie> cookie( |
2305 CookieMonster::CanonicalCookie::Create(url_google_, pc)); | 2340 CookieMonster::CanonicalCookie::Create(url_google_, pc)); |
2306 | 2341 |
| 2342 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
| 2343 ASSERT_TRUE(cookie->IsPersistent()); |
| 2344 #else |
2307 ASSERT_FALSE(cookie->IsPersistent()); | 2345 ASSERT_FALSE(cookie->IsPersistent()); |
| 2346 #endif |
2308 } | 2347 } |
2309 | 2348 |
2310 // Test that CookieMonster writes session cookies into the underlying | 2349 // Test that CookieMonster writes session cookies into the underlying |
2311 // CookieStore if the "persist session cookies" option is on. | 2350 // CookieStore if the "persist session cookies" option is on. |
2312 TEST_F(CookieMonsterTest, PersistSessionCookies) { | 2351 TEST_F(CookieMonsterTest, PersistSessionCookies) { |
2313 scoped_refptr<MockPersistentCookieStore> store( | 2352 scoped_refptr<MockPersistentCookieStore> store( |
2314 new MockPersistentCookieStore); | 2353 new MockPersistentCookieStore); |
2315 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); | 2354 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); |
2316 cm->SetPersistSessionCookies(true); | 2355 cm->SetPersistSessionCookies(true); |
2317 | 2356 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2374 "A=Foo; expires=Mon, 18-Apr-22 22:50:14 GMT")); | 2413 "A=Foo; expires=Mon, 18-Apr-22 22:50:14 GMT")); |
2375 this->MatchCookieLines("A=Foo", GetCookies(cm, url_google_)); | 2414 this->MatchCookieLines("A=Foo", GetCookies(cm, url_google_)); |
2376 ASSERT_EQ(5u, store->commands().size()); | 2415 ASSERT_EQ(5u, store->commands().size()); |
2377 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type); | 2416 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type); |
2378 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); | 2417 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); |
2379 | 2418 |
2380 // Create some non-persistent cookies and check that they don't go to the | 2419 // Create some non-persistent cookies and check that they don't go to the |
2381 // persistent storage. | 2420 // persistent storage. |
2382 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); | 2421 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); |
2383 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); | 2422 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); |
| 2423 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
| 2424 EXPECT_EQ(6u, store->commands().size()); |
| 2425 #else |
2384 EXPECT_EQ(5u, store->commands().size()); | 2426 EXPECT_EQ(5u, store->commands().size()); |
| 2427 #endif |
2385 } | 2428 } |
2386 | 2429 |
2387 } // namespace net | 2430 } // namespace net |
OLD | NEW |