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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 876 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 options.set_include_httponly(); | 887 options.set_include_httponly(); |
888 | 888 |
889 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); | 889 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); |
890 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); | 890 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); |
891 | 891 |
892 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "C=D; httponly", options)); | 892 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "C=D; httponly", options)); |
893 EXPECT_EQ("A=B; C=D", GetCookiesWithOptions(cm, url_google_, options)); | 893 EXPECT_EQ("A=B; C=D", GetCookiesWithOptions(cm, url_google_, options)); |
894 | 894 |
895 EXPECT_EQ(2, DeleteAll(cm)); | 895 EXPECT_EQ(2, DeleteAll(cm)); |
896 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); | 896 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); |
897 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | |
898 // If a cookie is persistent then its commands will be recorded. | |
899 // Each above cookie has 2 commands: 1 for add and 1 for delete. | |
900 EXPECT_EQ(4u, store->commands().size()); | |
901 #else | |
902 EXPECT_EQ(0u, store->commands().size()); | 897 EXPECT_EQ(0u, store->commands().size()); |
903 #endif | |
904 | 898 |
905 // Create a persistent cookie. | 899 // Create a persistent cookie. |
906 EXPECT_TRUE(SetCookie(cm, url_google_, | 900 EXPECT_TRUE(SetCookie(cm, url_google_, |
907 std::string(kValidCookieLine) + | 901 std::string(kValidCookieLine) + |
908 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); | 902 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); |
909 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | |
910 ASSERT_EQ(5u, store->commands().size()); | |
911 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); | |
912 #else | |
913 ASSERT_EQ(1u, store->commands().size()); | 903 ASSERT_EQ(1u, store->commands().size()); |
914 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); | 904 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); |
915 #endif | |
916 | 905 |
917 EXPECT_EQ(1, DeleteAll(cm)); // sync_to_store = true. | 906 EXPECT_EQ(1, DeleteAll(cm)); // sync_to_store = true. |
918 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | |
919 ASSERT_EQ(6u, store->commands().size()); | |
920 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[5].type); | |
921 #else | |
922 ASSERT_EQ(2u, store->commands().size()); | 907 ASSERT_EQ(2u, store->commands().size()); |
923 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); | 908 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); |
924 #endif | |
925 | 909 |
926 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); | 910 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); |
927 } | 911 } |
928 | 912 |
929 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) { | 913 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) { |
930 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 914 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
931 Time now = Time::Now(); | 915 Time now = Time::Now(); |
932 | 916 |
933 // Nothing has been added so nothing should be deleted. | 917 // Nothing has been added so nothing should be deleted. |
934 EXPECT_EQ(0, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99), | 918 EXPECT_EQ(0, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99), |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1349 delegate->reset(); | 1333 delegate->reset(); |
1350 | 1334 |
1351 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E")); | 1335 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E")); |
1352 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_)); | 1336 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_)); |
1353 EXPECT_EQ(0u, delegate->changes().size()); | 1337 EXPECT_EQ(0u, delegate->changes().size()); |
1354 | 1338 |
1355 // Insert a cookie "a" for path "/path1" | 1339 // Insert a cookie "a" for path "/path1" |
1356 EXPECT_TRUE( | 1340 EXPECT_TRUE( |
1357 SetCookie(cm, url_google_, "a=val1; path=/path1; " | 1341 SetCookie(cm, url_google_, "a=val1; path=/path1; " |
1358 "expires=Mon, 18-Apr-22 22:50:13 GMT")); | 1342 "expires=Mon, 18-Apr-22 22:50:13 GMT")); |
1359 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | |
1360 ASSERT_EQ(5u, store->commands().size()); | |
1361 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); | |
1362 #else | |
1363 ASSERT_EQ(1u, store->commands().size()); | 1343 ASSERT_EQ(1u, store->commands().size()); |
1364 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); | 1344 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); |
1365 #endif | |
1366 ASSERT_EQ(1u, delegate->changes().size()); | 1345 ASSERT_EQ(1u, delegate->changes().size()); |
1367 EXPECT_FALSE(delegate->changes()[0].second); | 1346 EXPECT_FALSE(delegate->changes()[0].second); |
1368 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); | 1347 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); |
1369 EXPECT_EQ("a", delegate->changes()[0].first.Name()); | 1348 EXPECT_EQ("a", delegate->changes()[0].first.Name()); |
1370 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); | 1349 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); |
1371 delegate->reset(); | 1350 delegate->reset(); |
1372 | 1351 |
1373 // Insert a cookie "a" for path "/path1", that is httponly. This should | 1352 // Insert a cookie "a" for path "/path1", that is httponly. This should |
1374 // overwrite the non-http-only version. | 1353 // overwrite the non-http-only version. |
1375 CookieOptions allow_httponly; | 1354 CookieOptions allow_httponly; |
1376 allow_httponly.set_include_httponly(); | 1355 allow_httponly.set_include_httponly(); |
1377 EXPECT_TRUE( | 1356 EXPECT_TRUE( |
1378 SetCookieWithOptions(cm, url_google_, | 1357 SetCookieWithOptions(cm, url_google_, |
1379 "a=val2; path=/path1; httponly; " | 1358 "a=val2; path=/path1; httponly; " |
1380 "expires=Mon, 18-Apr-22 22:50:14 GMT", | 1359 "expires=Mon, 18-Apr-22 22:50:14 GMT", |
1381 allow_httponly)); | 1360 allow_httponly)); |
1382 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | |
1383 ASSERT_EQ(7u, store->commands().size()); | |
1384 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[5].type); | |
1385 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[6].type); | |
1386 #else | |
1387 ASSERT_EQ(3u, store->commands().size()); | 1361 ASSERT_EQ(3u, store->commands().size()); |
1388 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); | 1362 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); |
1389 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type); | 1363 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type); |
1390 #endif | |
1391 ASSERT_EQ(2u, delegate->changes().size()); | 1364 ASSERT_EQ(2u, delegate->changes().size()); |
1392 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); | 1365 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); |
1393 EXPECT_TRUE(delegate->changes()[0].second); | 1366 EXPECT_TRUE(delegate->changes()[0].second); |
1394 EXPECT_EQ("a", delegate->changes()[0].first.Name()); | 1367 EXPECT_EQ("a", delegate->changes()[0].first.Name()); |
1395 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); | 1368 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); |
1396 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain()); | 1369 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain()); |
1397 EXPECT_FALSE(delegate->changes()[1].second); | 1370 EXPECT_FALSE(delegate->changes()[1].second); |
1398 EXPECT_EQ("a", delegate->changes()[1].first.Name()); | 1371 EXPECT_EQ("a", delegate->changes()[1].first.Name()); |
1399 EXPECT_EQ("val2", delegate->changes()[1].first.Value()); | 1372 EXPECT_EQ("val2", delegate->changes()[1].first.Value()); |
1400 delegate->reset(); | 1373 delegate->reset(); |
(...skipping 30 matching lines...) Expand all Loading... |
1431 false, false)); | 1404 false, false)); |
1432 | 1405 |
1433 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_); | 1406 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_); |
1434 CookieList::iterator it = cookies.begin(); | 1407 CookieList::iterator it = cookies.begin(); |
1435 | 1408 |
1436 ASSERT_TRUE(it != cookies.end()); | 1409 ASSERT_TRUE(it != cookies.end()); |
1437 EXPECT_EQ("A", it->Name()); | 1410 EXPECT_EQ("A", it->Name()); |
1438 EXPECT_EQ("B", it->Value()); | 1411 EXPECT_EQ("B", it->Value()); |
1439 EXPECT_EQ("www.google.izzle", it->Domain()); | 1412 EXPECT_EQ("www.google.izzle", it->Domain()); |
1440 EXPECT_EQ("/foo", it->Path()); | 1413 EXPECT_EQ("/foo", it->Path()); |
1441 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | |
1442 EXPECT_TRUE(it->IsPersistent()); | |
1443 #else | |
1444 EXPECT_FALSE(it->IsPersistent()); | 1414 EXPECT_FALSE(it->IsPersistent()); |
1445 #endif | |
1446 EXPECT_FALSE(it->IsSecure()); | 1415 EXPECT_FALSE(it->IsSecure()); |
1447 EXPECT_FALSE(it->IsHttpOnly()); | 1416 EXPECT_FALSE(it->IsHttpOnly()); |
1448 | 1417 |
1449 ASSERT_TRUE(++it == cookies.end()); | 1418 ASSERT_TRUE(++it == cookies.end()); |
1450 | 1419 |
1451 cookies = GetAllCookiesForURL(cm, url_google_bar_); | 1420 cookies = GetAllCookiesForURL(cm, url_google_bar_); |
1452 it = cookies.begin(); | 1421 it = cookies.begin(); |
1453 | 1422 |
1454 ASSERT_TRUE(it != cookies.end()); | 1423 ASSERT_TRUE(it != cookies.end()); |
1455 EXPECT_EQ("C", it->Name()); | 1424 EXPECT_EQ("C", it->Name()); |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1967 false, false)); | 1936 false, false)); |
1968 | 1937 |
1969 base::Histogram::SampleSet histogram_set_2; | 1938 base::Histogram::SampleSet histogram_set_2; |
1970 expired_histogram->SnapshotSample(&histogram_set_2); | 1939 expired_histogram->SnapshotSample(&histogram_set_2); |
1971 EXPECT_EQ(histogram_set_1.TotalCount() + 1, | 1940 EXPECT_EQ(histogram_set_1.TotalCount() + 1, |
1972 histogram_set_2.TotalCount()); | 1941 histogram_set_2.TotalCount()); |
1973 | 1942 |
1974 // kValidCookieLine creates a session cookie. | 1943 // kValidCookieLine creates a session cookie. |
1975 ASSERT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); | 1944 ASSERT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); |
1976 expired_histogram->SnapshotSample(&histogram_set_1); | 1945 expired_histogram->SnapshotSample(&histogram_set_1); |
1977 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | |
1978 EXPECT_EQ(histogram_set_2.TotalCount() + 1, | |
1979 histogram_set_1.TotalCount()); | |
1980 #else | |
1981 EXPECT_EQ(histogram_set_2.TotalCount(), | 1946 EXPECT_EQ(histogram_set_2.TotalCount(), |
1982 histogram_set_1.TotalCount()); | 1947 histogram_set_1.TotalCount()); |
1983 #endif | |
1984 } | 1948 } |
1985 | 1949 |
1986 namespace { | 1950 namespace { |
1987 | 1951 |
1988 class MultiThreadedCookieMonsterTest : public CookieMonsterTest { | 1952 class MultiThreadedCookieMonsterTest : public CookieMonsterTest { |
1989 public: | 1953 public: |
1990 MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {} | 1954 MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {} |
1991 | 1955 |
1992 // Helper methods for calling the asynchronous CookieMonster methods | 1956 // Helper methods for calling the asynchronous CookieMonster methods |
1993 // from a different thread. | 1957 // from a different thread. |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2210 base::Unretained(this), | 2174 base::Unretained(this), |
2211 cm, *it, &callback); | 2175 cm, *it, &callback); |
2212 RunOnOtherThread(task); | 2176 RunOnOtherThread(task); |
2213 EXPECT_TRUE(callback.did_run()); | 2177 EXPECT_TRUE(callback.did_run()); |
2214 EXPECT_TRUE(callback.result()); | 2178 EXPECT_TRUE(callback.result()); |
2215 } | 2179 } |
2216 | 2180 |
2217 TEST_F(CookieMonsterTest, InvalidExpiryTime) { | 2181 TEST_F(CookieMonsterTest, InvalidExpiryTime) { |
2218 ParsedCookie pc(std::string(kValidCookieLine) + "; expires=Blarg arg arg"); | 2182 ParsedCookie pc(std::string(kValidCookieLine) + "; expires=Blarg arg arg"); |
2219 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create(url_google_, pc)); | 2183 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create(url_google_, pc)); |
2220 | |
2221 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | |
2222 ASSERT_TRUE(cookie->IsPersistent()); | |
2223 #else | |
2224 ASSERT_FALSE(cookie->IsPersistent()); | 2184 ASSERT_FALSE(cookie->IsPersistent()); |
2225 #endif | |
2226 } | 2185 } |
2227 | 2186 |
2228 // Test that CookieMonster writes session cookies into the underlying | 2187 // Test that CookieMonster writes session cookies into the underlying |
2229 // CookieStore if the "persist session cookies" option is on. | 2188 // CookieStore if the "persist session cookies" option is on. |
2230 TEST_F(CookieMonsterTest, PersistSessionCookies) { | 2189 TEST_F(CookieMonsterTest, PersistSessionCookies) { |
2231 scoped_refptr<MockPersistentCookieStore> store( | 2190 scoped_refptr<MockPersistentCookieStore> store( |
2232 new MockPersistentCookieStore); | 2191 new MockPersistentCookieStore); |
2233 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); | 2192 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); |
2234 cm->SetPersistSessionCookies(true); | 2193 cm->SetPersistSessionCookies(true); |
2235 | 2194 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2292 "A=Foo; expires=Mon, 18-Apr-22 22:50:14 GMT")); | 2251 "A=Foo; expires=Mon, 18-Apr-22 22:50:14 GMT")); |
2293 this->MatchCookieLines("A=Foo", GetCookies(cm, url_google_)); | 2252 this->MatchCookieLines("A=Foo", GetCookies(cm, url_google_)); |
2294 ASSERT_EQ(5u, store->commands().size()); | 2253 ASSERT_EQ(5u, store->commands().size()); |
2295 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type); | 2254 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type); |
2296 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); | 2255 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); |
2297 | 2256 |
2298 // Create some non-persistent cookies and check that they don't go to the | 2257 // Create some non-persistent cookies and check that they don't go to the |
2299 // persistent storage. | 2258 // persistent storage. |
2300 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); | 2259 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); |
2301 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); | 2260 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); |
2302 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | |
2303 EXPECT_EQ(6u, store->commands().size()); | |
2304 #else | |
2305 EXPECT_EQ(5u, store->commands().size()); | 2261 EXPECT_EQ(5u, store->commands().size()); |
2306 #endif | |
2307 } | 2262 } |
2308 | 2263 |
2309 } // namespace net | 2264 } // namespace net |
OLD | NEW |