| 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> | |
| 8 #include <string> | 7 #include <string> |
| 9 | 8 |
| 10 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 11 #include "base/bind.h" | 10 #include "base/bind.h" |
| 12 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 15 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 16 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 17 #include "base/string_tokenizer.h" | 16 #include "base/string_tokenizer.h" |
| 18 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 19 #include "base/time.h" | 18 #include "base/time.h" |
| 20 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 20 #include "net/cookies/canonical_cookie.h" |
| 21 #include "net/cookies/cookie_monster.h" | 21 #include "net/cookies/cookie_monster.h" |
| 22 #include "net/cookies/cookie_monster_store_test.h" // For CookieStore mock | 22 #include "net/cookies/cookie_monster_store_test.h" // For CookieStore mock |
| 23 #include "net/cookies/cookie_util.h" | 23 #include "net/cookies/cookie_util.h" |
| 24 #include "net/cookies/parsed_cookie.h" | 24 #include "net/cookies/parsed_cookie.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 27 |
| 28 namespace net { | 28 namespace net { |
| 29 | 29 |
| 30 using base::Time; | 30 using base::Time; |
| 31 using base::TimeDelta; | 31 using base::TimeDelta; |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 // TODO(erikwright): Replace the pre-existing MockPersistentCookieStore (and | 35 // TODO(erikwright): Replace the pre-existing MockPersistentCookieStore (and |
| 36 // brethren) with this one, and remove the 'New' prefix. | 36 // brethren) with this one, and remove the 'New' prefix. |
| 37 class NewMockPersistentCookieStore | 37 class NewMockPersistentCookieStore |
| 38 : public CookieMonster::PersistentCookieStore { | 38 : public CookieMonster::PersistentCookieStore { |
| 39 public: | 39 public: |
| 40 MOCK_METHOD1(Load, void(const LoadedCallback& loaded_callback)); | 40 MOCK_METHOD1(Load, void(const LoadedCallback& loaded_callback)); |
| 41 MOCK_METHOD2(LoadCookiesForKey, void(const std::string& key, | 41 MOCK_METHOD2(LoadCookiesForKey, void(const std::string& key, |
| 42 const LoadedCallback& loaded_callback)); | 42 const LoadedCallback& loaded_callback)); |
| 43 MOCK_METHOD1(AddCookie, void(const CookieMonster::CanonicalCookie& cc)); | 43 MOCK_METHOD1(AddCookie, void(const CanonicalCookie& cc)); |
| 44 MOCK_METHOD1(UpdateCookieAccessTime, | 44 MOCK_METHOD1(UpdateCookieAccessTime, void(const CanonicalCookie& cc)); |
| 45 void(const CookieMonster::CanonicalCookie& cc)); | 45 MOCK_METHOD1(DeleteCookie, void(const CanonicalCookie& cc)); |
| 46 MOCK_METHOD1(DeleteCookie, void(const CookieMonster::CanonicalCookie& cc)); | |
| 47 MOCK_METHOD1(Flush, void(const base::Closure& callback)); | 46 MOCK_METHOD1(Flush, void(const base::Closure& callback)); |
| 48 MOCK_METHOD0(SetForceKeepSessionState, void()); | 47 MOCK_METHOD0(SetForceKeepSessionState, void()); |
| 49 | 48 |
| 50 private: | 49 private: |
| 51 virtual ~NewMockPersistentCookieStore() {} | 50 virtual ~NewMockPersistentCookieStore() {} |
| 52 }; | 51 }; |
| 53 | 52 |
| 54 const char* kTopLevelDomainPlus1 = "http://www.harvard.edu"; | 53 const char* kTopLevelDomainPlus1 = "http://www.harvard.edu"; |
| 55 const char* kTopLevelDomainPlus2 = "http://www.math.harvard.edu"; | 54 const char* kTopLevelDomainPlus2 = "http://www.math.harvard.edu"; |
| 56 const char* kTopLevelDomainPlus2Secure = "https://www.math.harvard.edu"; | 55 const char* kTopLevelDomainPlus2Secure = "https://www.math.harvard.edu"; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 const GURL& url) { | 182 const GURL& url) { |
| 184 DCHECK(cm); | 183 DCHECK(cm); |
| 185 DeleteCallback callback; | 184 DeleteCallback callback; |
| 186 cm->DeleteAllForHostAsync( | 185 cm->DeleteAllForHostAsync( |
| 187 url, base::Bind(&DeleteCallback::Run, base::Unretained(&callback))); | 186 url, base::Bind(&DeleteCallback::Run, base::Unretained(&callback))); |
| 188 RunFor(kTimeout); | 187 RunFor(kTimeout); |
| 189 EXPECT_TRUE(callback.did_run()); | 188 EXPECT_TRUE(callback.did_run()); |
| 190 return callback.num_deleted(); | 189 return callback.num_deleted(); |
| 191 } | 190 } |
| 192 | 191 |
| 193 bool DeleteCanonicalCookie(CookieMonster*cm, | 192 bool DeleteCanonicalCookie(CookieMonster*cm, const CanonicalCookie& cookie) { |
| 194 const CookieMonster::CanonicalCookie& cookie) { | |
| 195 DCHECK(cm); | 193 DCHECK(cm); |
| 196 SetCookieCallback callback; | 194 SetCookieCallback callback; |
| 197 cm->DeleteCanonicalCookieAsync( | 195 cm->DeleteCanonicalCookieAsync( |
| 198 cookie, | 196 cookie, |
| 199 base::Bind(&SetCookieCallback::Run, base::Unretained(&callback))); | 197 base::Bind(&SetCookieCallback::Run, base::Unretained(&callback))); |
| 200 RunFor(kTimeout); | 198 RunFor(kTimeout); |
| 201 EXPECT_TRUE(callback.did_run()); | 199 EXPECT_TRUE(callback.did_run()); |
| 202 return callback.result(); | 200 return callback.result(); |
| 203 } | 201 } |
| 204 | 202 |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 MOCK_METHOD0(Begin, void(void)); | 577 MOCK_METHOD0(Begin, void(void)); |
| 580 | 578 |
| 581 // Returns the CookieMonster instance under test. | 579 // Returns the CookieMonster instance under test. |
| 582 CookieMonster& cookie_monster() { return *cookie_monster_; } | 580 CookieMonster& cookie_monster() { return *cookie_monster_; } |
| 583 | 581 |
| 584 private: | 582 private: |
| 585 // Declares that mock expectations in this test suite are strictly ordered. | 583 // Declares that mock expectations in this test suite are strictly ordered. |
| 586 testing::InSequence in_sequence_; | 584 testing::InSequence in_sequence_; |
| 587 // Holds cookies to be returned from PersistentCookieStore::Load or | 585 // Holds cookies to be returned from PersistentCookieStore::Load or |
| 588 // PersistentCookieStore::LoadCookiesForKey. | 586 // PersistentCookieStore::LoadCookiesForKey. |
| 589 std::vector<CookieMonster::CanonicalCookie*> loaded_cookies_; | 587 std::vector<CanonicalCookie*> loaded_cookies_; |
| 590 // Stores the callback passed from the CookieMonster to the | 588 // Stores the callback passed from the CookieMonster to the |
| 591 // PersistentCookieStore::Load | 589 // PersistentCookieStore::Load |
| 592 CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback_; | 590 CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback_; |
| 593 // Stores the callback passed from the CookieMonster to the | 591 // Stores the callback passed from the CookieMonster to the |
| 594 // PersistentCookieStore::LoadCookiesForKey | 592 // PersistentCookieStore::LoadCookiesForKey |
| 595 std::queue<CookieMonster::PersistentCookieStore::LoadedCallback> | 593 std::queue<CookieMonster::PersistentCookieStore::LoadedCallback> |
| 596 loaded_for_key_callbacks_; | 594 loaded_for_key_callbacks_; |
| 597 | 595 |
| 598 // Stores the CookieMonster under test. | 596 // Stores the CookieMonster under test. |
| 599 scoped_refptr<CookieMonster> cookie_monster_; | 597 scoped_refptr<CookieMonster> cookie_monster_; |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce( | 800 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce( |
| 803 DeleteAllForHostAction( | 801 DeleteAllForHostAction( |
| 804 &cookie_monster(), url_google_, &delete_callback)); | 802 &cookie_monster(), url_google_, &delete_callback)); |
| 805 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce( | 803 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce( |
| 806 QuitCurrentMessageLoop()); | 804 QuitCurrentMessageLoop()); |
| 807 | 805 |
| 808 CompleteLoadingAndWait(); | 806 CompleteLoadingAndWait(); |
| 809 } | 807 } |
| 810 | 808 |
| 811 TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) { | 809 TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) { |
| 812 std::vector<CookieMonster::CanonicalCookie*> cookies; | 810 std::vector<CanonicalCookie*> cookies; |
| 813 CookieMonster::CanonicalCookie cookie = BuildCanonicalCookie( | 811 CanonicalCookie cookie = BuildCanonicalCookie( |
| 814 "www.google.com", "X=1; path=/", base::Time::Now()); | 812 "www.google.com", "X=1; path=/", base::Time::Now()); |
| 815 | 813 |
| 816 MockDeleteCookieCallback delete_cookie_callback; | 814 MockDeleteCookieCallback delete_cookie_callback; |
| 817 | 815 |
| 818 BeginWith(DeleteCanonicalCookieAction( | 816 BeginWith(DeleteCanonicalCookieAction( |
| 819 &cookie_monster(), cookie, &delete_cookie_callback)); | 817 &cookie_monster(), cookie, &delete_cookie_callback)); |
| 820 | 818 |
| 821 WaitForLoadCall(); | 819 WaitForLoadCall(); |
| 822 | 820 |
| 823 EXPECT_CALL(delete_cookie_callback, Invoke(false)).WillOnce( | 821 EXPECT_CALL(delete_cookie_callback, Invoke(false)).WillOnce( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 874 GetCookiesWithInfoAction( | 872 GetCookiesWithInfoAction( |
| 875 &cookie_monster(), url_google_, &get_cookie_info_callback)); | 873 &cookie_monster(), url_google_, &get_cookie_info_callback)); |
| 876 EXPECT_CALL(get_cookie_info_callback, Invoke("X=1", testing::_)).WillOnce( | 874 EXPECT_CALL(get_cookie_info_callback, Invoke("X=1", testing::_)).WillOnce( |
| 877 QuitCurrentMessageLoop()); | 875 QuitCurrentMessageLoop()); |
| 878 EXPECT_CALL(set_cookies_callback, Invoke(true)); | 876 EXPECT_CALL(set_cookies_callback, Invoke(true)); |
| 879 EXPECT_CALL(delete_cookie_callback, Invoke()); | 877 EXPECT_CALL(delete_cookie_callback, Invoke()); |
| 880 | 878 |
| 881 CompleteLoadingAndWait(); | 879 CompleteLoadingAndWait(); |
| 882 } | 880 } |
| 883 | 881 |
| 884 TEST_F(CookieMonsterTest, TestCookieDateParsing) { | |
| 885 const struct { | |
| 886 const char* str; | |
| 887 const bool valid; | |
| 888 const time_t epoch; | |
| 889 } tests[] = { | |
| 890 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 }, | |
| 891 { "Thu, 19-Apr-2007 16:00:00 GMT", true, 1176998400 }, | |
| 892 { "Wed, 25 Apr 2007 21:02:13 GMT", true, 1177534933 }, | |
| 893 { "Thu, 19/Apr\\2007 16:00:00 GMT", true, 1176998400 }, | |
| 894 { "Fri, 1 Jan 2010 01:01:50 GMT", true, 1262307710 }, | |
| 895 { "Wednesday, 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, | |
| 896 { ", 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, | |
| 897 { " 1-Jan-2003 00:00:00 GMT", true, 1041379200 }, | |
| 898 { "1-Jan-2003 00:00:00 GMT", true, 1041379200 }, | |
| 899 { "Wed,18-Apr-07 22:50:12 GMT", true, 1176936612 }, | |
| 900 { "WillyWonka , 18-Apr-07 22:50:12 GMT", true, 1176936612 }, | |
| 901 { "WillyWonka , 18-Apr-07 22:50:12", true, 1176936612 }, | |
| 902 { "WillyWonka , 18-apr-07 22:50:12", true, 1176936612 }, | |
| 903 { "Mon, 18-Apr-1977 22:50:13 GMT", true, 230251813 }, | |
| 904 { "Mon, 18-Apr-77 22:50:13 GMT", true, 230251813 }, | |
| 905 // If the cookie came in with the expiration quoted (which in terms of | |
| 906 // the RFC you shouldn't do), we will get string quoted. Bug 1261605. | |
| 907 { "\"Sat, 15-Apr-17\\\"21:01:22\\\"GMT\"", true, 1492290082 }, | |
| 908 // Test with full month names and partial names. | |
| 909 { "Partyday, 18- April-07 22:50:12", true, 1176936612 }, | |
| 910 { "Partyday, 18 - Apri-07 22:50:12", true, 1176936612 }, | |
| 911 { "Wednes, 1-Januar-2003 00:00:00 GMT", true, 1041379200 }, | |
| 912 // Test that we always take GMT even with other time zones or bogus | |
| 913 // values. The RFC says everything should be GMT, and in the worst case | |
| 914 // we are 24 hours off because of zone issues. | |
| 915 { "Sat, 15-Apr-17 21:01:22", true, 1492290082 }, | |
| 916 { "Sat, 15-Apr-17 21:01:22 GMT-2", true, 1492290082 }, | |
| 917 { "Sat, 15-Apr-17 21:01:22 GMT BLAH", true, 1492290082 }, | |
| 918 { "Sat, 15-Apr-17 21:01:22 GMT-0400", true, 1492290082 }, | |
| 919 { "Sat, 15-Apr-17 21:01:22 GMT-0400 (EDT)",true, 1492290082 }, | |
| 920 { "Sat, 15-Apr-17 21:01:22 DST", true, 1492290082 }, | |
| 921 { "Sat, 15-Apr-17 21:01:22 -0400", true, 1492290082 }, | |
| 922 { "Sat, 15-Apr-17 21:01:22 (hello there)", true, 1492290082 }, | |
| 923 // Test that if we encounter multiple : fields, that we take the first | |
| 924 // that correctly parses. | |
| 925 { "Sat, 15-Apr-17 21:01:22 11:22:33", true, 1492290082 }, | |
| 926 { "Sat, 15-Apr-17 ::00 21:01:22", true, 1492290082 }, | |
| 927 { "Sat, 15-Apr-17 boink:z 21:01:22", true, 1492290082 }, | |
| 928 // We take the first, which in this case is invalid. | |
| 929 { "Sat, 15-Apr-17 91:22:33 21:01:22", false, 0 }, | |
| 930 // amazon.com formats their cookie expiration like this. | |
| 931 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 }, | |
| 932 // Test that hh:mm:ss can occur anywhere. | |
| 933 { "22:50:12 Thu Apr 18 2007 GMT", true, 1176936612 }, | |
| 934 { "Thu 22:50:12 Apr 18 2007 GMT", true, 1176936612 }, | |
| 935 { "Thu Apr 22:50:12 18 2007 GMT", true, 1176936612 }, | |
| 936 { "Thu Apr 18 22:50:12 2007 GMT", true, 1176936612 }, | |
| 937 { "Thu Apr 18 2007 22:50:12 GMT", true, 1176936612 }, | |
| 938 { "Thu Apr 18 2007 GMT 22:50:12", true, 1176936612 }, | |
| 939 // Test that the day and year can be anywhere if they are unambigious. | |
| 940 { "Sat, 15-Apr-17 21:01:22 GMT", true, 1492290082 }, | |
| 941 { "15-Sat, Apr-17 21:01:22 GMT", true, 1492290082 }, | |
| 942 { "15-Sat, Apr 21:01:22 GMT 17", true, 1492290082 }, | |
| 943 { "15-Sat, Apr 21:01:22 GMT 2017", true, 1492290082 }, | |
| 944 { "15 Apr 21:01:22 2017", true, 1492290082 }, | |
| 945 { "15 17 Apr 21:01:22", true, 1492290082 }, | |
| 946 { "Apr 15 17 21:01:22", true, 1492290082 }, | |
| 947 { "Apr 15 21:01:22 17", true, 1492290082 }, | |
| 948 { "2017 April 15 21:01:22", true, 1492290082 }, | |
| 949 { "15 April 2017 21:01:22", true, 1492290082 }, | |
| 950 // Some invalid dates | |
| 951 { "98 April 17 21:01:22", false, 0 }, | |
| 952 { "Thu, 012-Aug-2008 20:49:07 GMT", false, 0 }, | |
| 953 { "Thu, 12-Aug-31841 20:49:07 GMT", false, 0 }, | |
| 954 { "Thu, 12-Aug-9999999999 20:49:07 GMT", false, 0 }, | |
| 955 { "Thu, 999999999999-Aug-2007 20:49:07 GMT", false, 0 }, | |
| 956 { "Thu, 12-Aug-2007 20:61:99999999999 GMT", false, 0 }, | |
| 957 { "IAintNoDateFool", false, 0 }, | |
| 958 }; | |
| 959 | |
| 960 Time parsed_time; | |
| 961 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | |
| 962 parsed_time = CookieMonster::ParseCookieTime(tests[i].str); | |
| 963 if (!tests[i].valid) { | |
| 964 EXPECT_FALSE(!parsed_time.is_null()) << tests[i].str; | |
| 965 continue; | |
| 966 } | |
| 967 EXPECT_TRUE(!parsed_time.is_null()) << tests[i].str; | |
| 968 EXPECT_EQ(tests[i].epoch, parsed_time.ToTimeT()) << tests[i].str; | |
| 969 } | |
| 970 } | |
| 971 | |
| 972 TEST_F(CookieMonsterTest, TestCookieDeleteAll) { | 882 TEST_F(CookieMonsterTest, TestCookieDeleteAll) { |
| 973 scoped_refptr<MockPersistentCookieStore> store( | 883 scoped_refptr<MockPersistentCookieStore> store( |
| 974 new MockPersistentCookieStore); | 884 new MockPersistentCookieStore); |
| 975 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); | 885 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); |
| 976 CookieOptions options; | 886 CookieOptions options; |
| 977 options.set_include_httponly(); | 887 options.set_include_httponly(); |
| 978 | 888 |
| 979 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); | 889 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine)); |
| 980 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); | 890 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); |
| 981 | 891 |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1270 // This is a regression test for: http://crbug.com/17855. | 1180 // This is a regression test for: http://crbug.com/17855. |
| 1271 TEST_F(CookieMonsterTest, DontImportDuplicateCookies) { | 1181 TEST_F(CookieMonsterTest, DontImportDuplicateCookies) { |
| 1272 scoped_refptr<MockPersistentCookieStore> store( | 1182 scoped_refptr<MockPersistentCookieStore> store( |
| 1273 new MockPersistentCookieStore); | 1183 new MockPersistentCookieStore); |
| 1274 | 1184 |
| 1275 // We will fill some initial cookies into the PersistentCookieStore, | 1185 // We will fill some initial cookies into the PersistentCookieStore, |
| 1276 // to simulate a database with 4 duplicates. Note that we need to | 1186 // to simulate a database with 4 duplicates. Note that we need to |
| 1277 // be careful not to have any duplicate creation times at all (as it's a | 1187 // be careful not to have any duplicate creation times at all (as it's a |
| 1278 // violation of a CookieMonster invariant) even if Time::Now() doesn't | 1188 // violation of a CookieMonster invariant) even if Time::Now() doesn't |
| 1279 // move between calls. | 1189 // move between calls. |
| 1280 std::vector<CookieMonster::CanonicalCookie*> initial_cookies; | 1190 std::vector<CanonicalCookie*> initial_cookies; |
| 1281 | 1191 |
| 1282 // Insert 4 cookies with name "X" on path "/", with varying creation | 1192 // Insert 4 cookies with name "X" on path "/", with varying creation |
| 1283 // dates. We expect only the most recent one to be preserved following | 1193 // dates. We expect only the most recent one to be preserved following |
| 1284 // the import. | 1194 // the import. |
| 1285 | 1195 |
| 1286 AddCookieToList("www.google.com", | 1196 AddCookieToList("www.google.com", |
| 1287 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT", | 1197 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT", |
| 1288 Time::Now() + TimeDelta::FromDays(3), | 1198 Time::Now() + TimeDelta::FromDays(3), |
| 1289 &initial_cookies); | 1199 &initial_cookies); |
| 1290 | 1200 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1355 scoped_refptr<MockPersistentCookieStore> store( | 1265 scoped_refptr<MockPersistentCookieStore> store( |
| 1356 new MockPersistentCookieStore); | 1266 new MockPersistentCookieStore); |
| 1357 | 1267 |
| 1358 Time now(Time::Now()); | 1268 Time now(Time::Now()); |
| 1359 Time earlier(now - TimeDelta::FromDays(1)); | 1269 Time earlier(now - TimeDelta::FromDays(1)); |
| 1360 | 1270 |
| 1361 // Insert 8 cookies, four with the current time as creation times, and | 1271 // Insert 8 cookies, four with the current time as creation times, and |
| 1362 // four with the earlier time as creation times. We should only get | 1272 // four with the earlier time as creation times. We should only get |
| 1363 // two cookies remaining, but which two (other than that there should | 1273 // two cookies remaining, but which two (other than that there should |
| 1364 // be one from each set) will be random. | 1274 // be one from each set) will be random. |
| 1365 std::vector<CookieMonster::CanonicalCookie*> initial_cookies; | 1275 std::vector<CanonicalCookie*> initial_cookies; |
| 1366 AddCookieToList("www.google.com", "X=1; path=/", now, &initial_cookies); | 1276 AddCookieToList("www.google.com", "X=1; path=/", now, &initial_cookies); |
| 1367 AddCookieToList("www.google.com", "X=2; path=/", now, &initial_cookies); | 1277 AddCookieToList("www.google.com", "X=2; path=/", now, &initial_cookies); |
| 1368 AddCookieToList("www.google.com", "X=3; path=/", now, &initial_cookies); | 1278 AddCookieToList("www.google.com", "X=3; path=/", now, &initial_cookies); |
| 1369 AddCookieToList("www.google.com", "X=4; path=/", now, &initial_cookies); | 1279 AddCookieToList("www.google.com", "X=4; path=/", now, &initial_cookies); |
| 1370 | 1280 |
| 1371 AddCookieToList("www.google.com", "Y=1; path=/", earlier, &initial_cookies); | 1281 AddCookieToList("www.google.com", "Y=1; path=/", earlier, &initial_cookies); |
| 1372 AddCookieToList("www.google.com", "Y=2; path=/", earlier, &initial_cookies); | 1282 AddCookieToList("www.google.com", "Y=2; path=/", earlier, &initial_cookies); |
| 1373 AddCookieToList("www.google.com", "Y=3; path=/", earlier, &initial_cookies); | 1283 AddCookieToList("www.google.com", "Y=3; path=/", earlier, &initial_cookies); |
| 1374 AddCookieToList("www.google.com", "Y=4; path=/", earlier, &initial_cookies); | 1284 AddCookieToList("www.google.com", "Y=4; path=/", earlier, &initial_cookies); |
| 1375 | 1285 |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1621 | 1531 |
| 1622 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails1", "A", | 1532 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails1", "A", |
| 1623 ".google.com", "/", Time(), false, false); | 1533 ".google.com", "/", Time(), false, false); |
| 1624 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails2", "A", | 1534 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails2", "A", |
| 1625 ".google.com", "/", Time(), false, false); | 1535 ".google.com", "/", Time(), false, false); |
| 1626 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails3", "A", | 1536 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails3", "A", |
| 1627 ".google.com", "/", Time(), false, false); | 1537 ".google.com", "/", Time(), false, false); |
| 1628 | 1538 |
| 1629 // Now we check | 1539 // Now we check |
| 1630 CookieList cookie_list(GetAllCookies(cm)); | 1540 CookieList cookie_list(GetAllCookies(cm)); |
| 1631 typedef std::map<int64, CookieMonster::CanonicalCookie> TimeCookieMap; | 1541 typedef std::map<int64, CanonicalCookie> TimeCookieMap; |
| 1632 TimeCookieMap check_map; | 1542 TimeCookieMap check_map; |
| 1633 for (CookieList::const_iterator it = cookie_list.begin(); | 1543 for (CookieList::const_iterator it = cookie_list.begin(); |
| 1634 it != cookie_list.end(); it++) { | 1544 it != cookie_list.end(); it++) { |
| 1635 const int64 creation_date = it->CreationDate().ToInternalValue(); | 1545 const int64 creation_date = it->CreationDate().ToInternalValue(); |
| 1636 TimeCookieMap::const_iterator | 1546 TimeCookieMap::const_iterator |
| 1637 existing_cookie_it(check_map.find(creation_date)); | 1547 existing_cookie_it(check_map.find(creation_date)); |
| 1638 EXPECT_TRUE(existing_cookie_it == check_map.end()) | 1548 EXPECT_TRUE(existing_cookie_it == check_map.end()) |
| 1639 << "Cookie " << it->Name() << " has same creation date (" | 1549 << "Cookie " << it->Name() << " has same creation date (" |
| 1640 << it->CreationDate().ToInternalValue() | 1550 << it->CreationDate().ToInternalValue() |
| 1641 << ") as previously entered cookie " | 1551 << ") as previously entered cookie " |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1721 { | 1631 { |
| 1722 scoped_refptr<CookieMonster> cmin(new CookieMonster(store, NULL)); | 1632 scoped_refptr<CookieMonster> cmin(new CookieMonster(store, NULL)); |
| 1723 CookieList cookies(GetAllCookies(cmin)); | 1633 CookieList cookies(GetAllCookies(cmin)); |
| 1724 ASSERT_EQ(2u, cookies.size()); | 1634 ASSERT_EQ(2u, cookies.size()); |
| 1725 // Ordering is path length, then creation time. So second cookie | 1635 // Ordering is path length, then creation time. So second cookie |
| 1726 // will come first, and we need to swap them. | 1636 // will come first, and we need to swap them. |
| 1727 std::swap(cookies[0], cookies[1]); | 1637 std::swap(cookies[0], cookies[1]); |
| 1728 for (int output_index = 0; output_index < 2; output_index++) { | 1638 for (int output_index = 0; output_index < 2; output_index++) { |
| 1729 int input_index = output_index * 2; | 1639 int input_index = output_index * 2; |
| 1730 const CookiesInputInfo* input = &input_info[input_index]; | 1640 const CookiesInputInfo* input = &input_info[input_index]; |
| 1731 const CookieMonster::CanonicalCookie* output = &cookies[output_index]; | 1641 const CanonicalCookie* output = &cookies[output_index]; |
| 1732 | 1642 |
| 1733 EXPECT_EQ(input->name, output->Name()); | 1643 EXPECT_EQ(input->name, output->Name()); |
| 1734 EXPECT_EQ(input->value, output->Value()); | 1644 EXPECT_EQ(input->value, output->Value()); |
| 1735 EXPECT_EQ(GURL(input->gurl).host(), output->Domain()); | 1645 EXPECT_EQ(GURL(input->gurl).host(), output->Domain()); |
| 1736 EXPECT_EQ(input->path, output->Path()); | 1646 EXPECT_EQ(input->path, output->Path()); |
| 1737 EXPECT_LE(current.ToInternalValue(), | 1647 EXPECT_LE(current.ToInternalValue(), |
| 1738 output->CreationDate().ToInternalValue()); | 1648 output->CreationDate().ToInternalValue()); |
| 1739 EXPECT_EQ(input->secure, output->IsSecure()); | 1649 EXPECT_EQ(input->secure, output->IsSecure()); |
| 1740 EXPECT_EQ(input->http_only, output->IsHttpOnly()); | 1650 EXPECT_EQ(input->http_only, output->IsHttpOnly()); |
| 1741 EXPECT_TRUE(output->IsPersistent()); | 1651 EXPECT_TRUE(output->IsPersistent()); |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1896 } | 1806 } |
| 1897 | 1807 |
| 1898 namespace { | 1808 namespace { |
| 1899 | 1809 |
| 1900 // Mock PersistentCookieStore that keeps track of the number of Flush() calls. | 1810 // Mock PersistentCookieStore that keeps track of the number of Flush() calls. |
| 1901 class FlushablePersistentStore : public CookieMonster::PersistentCookieStore { | 1811 class FlushablePersistentStore : public CookieMonster::PersistentCookieStore { |
| 1902 public: | 1812 public: |
| 1903 FlushablePersistentStore() : flush_count_(0) {} | 1813 FlushablePersistentStore() : flush_count_(0) {} |
| 1904 | 1814 |
| 1905 void Load(const LoadedCallback& loaded_callback) { | 1815 void Load(const LoadedCallback& loaded_callback) { |
| 1906 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | 1816 std::vector<CanonicalCookie*> out_cookies; |
| 1907 MessageLoop::current()->PostTask(FROM_HERE, | 1817 MessageLoop::current()->PostTask(FROM_HERE, |
| 1908 base::Bind(&net::LoadedCallbackTask::Run, | 1818 base::Bind(&net::LoadedCallbackTask::Run, |
| 1909 new net::LoadedCallbackTask(loaded_callback, out_cookies))); | 1819 new net::LoadedCallbackTask(loaded_callback, out_cookies))); |
| 1910 } | 1820 } |
| 1911 | 1821 |
| 1912 void LoadCookiesForKey(const std::string& key, | 1822 void LoadCookiesForKey(const std::string& key, |
| 1913 const LoadedCallback& loaded_callback) { | 1823 const LoadedCallback& loaded_callback) { |
| 1914 Load(loaded_callback); | 1824 Load(loaded_callback); |
| 1915 } | 1825 } |
| 1916 | 1826 |
| 1917 void AddCookie(const CookieMonster::CanonicalCookie&) {} | 1827 void AddCookie(const CanonicalCookie&) {} |
| 1918 void UpdateCookieAccessTime(const CookieMonster::CanonicalCookie&) {} | 1828 void UpdateCookieAccessTime(const CanonicalCookie&) {} |
| 1919 void DeleteCookie(const CookieMonster::CanonicalCookie&) {} | 1829 void DeleteCookie(const CanonicalCookie&) {} |
| 1920 void SetForceKeepSessionState() {} | 1830 void SetForceKeepSessionState() {} |
| 1921 | 1831 |
| 1922 void Flush(const base::Closure& callback) { | 1832 void Flush(const base::Closure& callback) { |
| 1923 ++flush_count_; | 1833 ++flush_count_; |
| 1924 if (!callback.is_null()) | 1834 if (!callback.is_null()) |
| 1925 callback.Run(); | 1835 callback.Run(); |
| 1926 } | 1836 } |
| 1927 | 1837 |
| 1928 int flush_count() { | 1838 int flush_count() { |
| 1929 return flush_count_; | 1839 return flush_count_; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2002 MessageLoop::current()->RunAllPending(); | 1912 MessageLoop::current()->RunAllPending(); |
| 2003 | 1913 |
| 2004 ASSERT_EQ(2, counter->callback_count()); | 1914 ASSERT_EQ(2, counter->callback_count()); |
| 2005 | 1915 |
| 2006 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get())); | 1916 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get())); |
| 2007 MessageLoop::current()->RunAllPending(); | 1917 MessageLoop::current()->RunAllPending(); |
| 2008 | 1918 |
| 2009 ASSERT_EQ(3, counter->callback_count()); | 1919 ASSERT_EQ(3, counter->callback_count()); |
| 2010 } | 1920 } |
| 2011 | 1921 |
| 2012 TEST_F(CookieMonsterTest, GetCookieSourceFromURL) { | |
| 2013 EXPECT_EQ("http://example.com/", | |
| 2014 CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | |
| 2015 GURL("http://example.com"))); | |
| 2016 EXPECT_EQ("http://example.com/", | |
| 2017 CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | |
| 2018 GURL("http://example.com/"))); | |
| 2019 EXPECT_EQ("http://example.com/", | |
| 2020 CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | |
| 2021 GURL("http://example.com/test"))); | |
| 2022 EXPECT_EQ("file:///tmp/test.html", | |
| 2023 CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | |
| 2024 GURL("file:///tmp/test.html"))); | |
| 2025 EXPECT_EQ("http://example.com/", | |
| 2026 CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | |
| 2027 GURL("http://example.com:1234/"))); | |
| 2028 EXPECT_EQ("http://example.com/", | |
| 2029 CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | |
| 2030 GURL("https://example.com/"))); | |
| 2031 EXPECT_EQ("http://example.com/", | |
| 2032 CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | |
| 2033 GURL("http://user:pwd@example.com/"))); | |
| 2034 EXPECT_EQ("http://example.com/", | |
| 2035 CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | |
| 2036 GURL("http://example.com/test?foo"))); | |
| 2037 EXPECT_EQ("http://example.com/", | |
| 2038 CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | |
| 2039 GURL("http://example.com/test#foo"))); | |
| 2040 } | |
| 2041 | |
| 2042 TEST_F(CookieMonsterTest, HistogramCheck) { | 1922 TEST_F(CookieMonsterTest, HistogramCheck) { |
| 2043 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); | 1923 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); |
| 2044 // Should match call in InitializeHistograms, but doesn't really matter | 1924 // Should match call in InitializeHistograms, but doesn't really matter |
| 2045 // since the histogram should have been initialized by the CM construction | 1925 // since the histogram should have been initialized by the CM construction |
| 2046 // above. | 1926 // above. |
| 2047 base::Histogram* expired_histogram = | 1927 base::Histogram* expired_histogram = |
| 2048 base::Histogram::FactoryGet( | 1928 base::Histogram::FactoryGet( |
| 2049 "Cookie.ExpirationDurationMinutes", 1, 10 * 365 * 24 * 60, 50, | 1929 "Cookie.ExpirationDurationMinutes", 1, 10 * 365 * 24 * 60, 50, |
| 2050 base::Histogram::kUmaTargetedHistogramFlag); | 1930 base::Histogram::kUmaTargetedHistogramFlag); |
| 2051 | 1931 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2128 | 2008 |
| 2129 void DeleteAllForHostTask(CookieMonster* cm, | 2009 void DeleteAllForHostTask(CookieMonster* cm, |
| 2130 const GURL& url, | 2010 const GURL& url, |
| 2131 DeleteCallback* callback) { | 2011 DeleteCallback* callback) { |
| 2132 cm->DeleteAllForHostAsync( | 2012 cm->DeleteAllForHostAsync( |
| 2133 url, | 2013 url, |
| 2134 base::Bind(&DeleteCallback::Run, base::Unretained(callback))); | 2014 base::Bind(&DeleteCallback::Run, base::Unretained(callback))); |
| 2135 } | 2015 } |
| 2136 | 2016 |
| 2137 void DeleteCanonicalCookieTask(CookieMonster* cm, | 2017 void DeleteCanonicalCookieTask(CookieMonster* cm, |
| 2138 const CookieMonster::CanonicalCookie& cookie, | 2018 const CanonicalCookie& cookie, |
| 2139 SetCookieCallback* callback) { | 2019 SetCookieCallback* callback) { |
| 2140 cm->DeleteCanonicalCookieAsync( | 2020 cm->DeleteCanonicalCookieAsync( |
| 2141 cookie, | 2021 cookie, |
| 2142 base::Bind(&SetCookieCallback::Run, base::Unretained(callback))); | 2022 base::Bind(&SetCookieCallback::Run, base::Unretained(callback))); |
| 2143 } | 2023 } |
| 2144 | 2024 |
| 2145 protected: | 2025 protected: |
| 2146 void RunOnOtherThread(const base::Closure& task) { | 2026 void RunOnOtherThread(const base::Closure& task) { |
| 2147 other_thread_.Start(); | 2027 other_thread_.Start(); |
| 2148 other_thread_.message_loop()->PostTask(FROM_HERE, task); | 2028 other_thread_.message_loop()->PostTask(FROM_HERE, task); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2294 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask, | 2174 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask, |
| 2295 base::Unretained(this), | 2175 base::Unretained(this), |
| 2296 cm, *it, &callback); | 2176 cm, *it, &callback); |
| 2297 RunOnOtherThread(task); | 2177 RunOnOtherThread(task); |
| 2298 EXPECT_TRUE(callback.did_run()); | 2178 EXPECT_TRUE(callback.did_run()); |
| 2299 EXPECT_TRUE(callback.result()); | 2179 EXPECT_TRUE(callback.result()); |
| 2300 } | 2180 } |
| 2301 | 2181 |
| 2302 TEST_F(CookieMonsterTest, InvalidExpiryTime) { | 2182 TEST_F(CookieMonsterTest, InvalidExpiryTime) { |
| 2303 ParsedCookie pc(std::string(kValidCookieLine) + "; expires=Blarg arg arg"); | 2183 ParsedCookie pc(std::string(kValidCookieLine) + "; expires=Blarg arg arg"); |
| 2304 scoped_ptr<CookieMonster::CanonicalCookie> cookie( | 2184 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create(url_google_, pc)); |
| 2305 CookieMonster::CanonicalCookie::Create(url_google_, pc)); | |
| 2306 | 2185 |
| 2307 ASSERT_FALSE(cookie->IsPersistent()); | 2186 ASSERT_FALSE(cookie->IsPersistent()); |
| 2308 } | 2187 } |
| 2309 | 2188 |
| 2310 // Test that CookieMonster writes session cookies into the underlying | 2189 // Test that CookieMonster writes session cookies into the underlying |
| 2311 // CookieStore if the "persist session cookies" option is on. | 2190 // CookieStore if the "persist session cookies" option is on. |
| 2312 TEST_F(CookieMonsterTest, PersistSessionCookies) { | 2191 TEST_F(CookieMonsterTest, PersistSessionCookies) { |
| 2313 scoped_refptr<MockPersistentCookieStore> store( | 2192 scoped_refptr<MockPersistentCookieStore> store( |
| 2314 new MockPersistentCookieStore); | 2193 new MockPersistentCookieStore); |
| 2315 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); | 2194 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2378 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); | 2257 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); |
| 2379 | 2258 |
| 2380 // Create some non-persistent cookies and check that they don't go to the | 2259 // Create some non-persistent cookies and check that they don't go to the |
| 2381 // persistent storage. | 2260 // persistent storage. |
| 2382 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); | 2261 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); |
| 2383 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); | 2262 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); |
| 2384 EXPECT_EQ(5u, store->commands().size()); | 2263 EXPECT_EQ(5u, store->commands().size()); |
| 2385 } | 2264 } |
| 2386 | 2265 |
| 2387 } // namespace net | 2266 } // namespace net |
| OLD | NEW |