Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: net/cookies/cookie_monster_unittest.cc

Issue 10785017: Move CanonicalCookie into separate files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added missing include Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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
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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 // This is a regression test for: http://crbug.com/17855. 1195 // This is a regression test for: http://crbug.com/17855.
1286 TEST_F(CookieMonsterTest, DontImportDuplicateCookies) { 1196 TEST_F(CookieMonsterTest, DontImportDuplicateCookies) {
1287 scoped_refptr<MockPersistentCookieStore> store( 1197 scoped_refptr<MockPersistentCookieStore> store(
1288 new MockPersistentCookieStore); 1198 new MockPersistentCookieStore);
1289 1199
1290 // We will fill some initial cookies into the PersistentCookieStore, 1200 // We will fill some initial cookies into the PersistentCookieStore,
1291 // to simulate a database with 4 duplicates. Note that we need to 1201 // to simulate a database with 4 duplicates. Note that we need to
1292 // be careful not to have any duplicate creation times at all (as it's a 1202 // be careful not to have any duplicate creation times at all (as it's a
1293 // violation of a CookieMonster invariant) even if Time::Now() doesn't 1203 // violation of a CookieMonster invariant) even if Time::Now() doesn't
1294 // move between calls. 1204 // move between calls.
1295 std::vector<CookieMonster::CanonicalCookie*> initial_cookies; 1205 std::vector<CanonicalCookie*> initial_cookies;
1296 1206
1297 // Insert 4 cookies with name "X" on path "/", with varying creation 1207 // Insert 4 cookies with name "X" on path "/", with varying creation
1298 // dates. We expect only the most recent one to be preserved following 1208 // dates. We expect only the most recent one to be preserved following
1299 // the import. 1209 // the import.
1300 1210
1301 AddCookieToList("www.google.com", 1211 AddCookieToList("www.google.com",
1302 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT", 1212 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1303 Time::Now() + TimeDelta::FromDays(3), 1213 Time::Now() + TimeDelta::FromDays(3),
1304 &initial_cookies); 1214 &initial_cookies);
1305 1215
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1370 scoped_refptr<MockPersistentCookieStore> store( 1280 scoped_refptr<MockPersistentCookieStore> store(
1371 new MockPersistentCookieStore); 1281 new MockPersistentCookieStore);
1372 1282
1373 Time now(Time::Now()); 1283 Time now(Time::Now());
1374 Time earlier(now - TimeDelta::FromDays(1)); 1284 Time earlier(now - TimeDelta::FromDays(1));
1375 1285
1376 // Insert 8 cookies, four with the current time as creation times, and 1286 // Insert 8 cookies, four with the current time as creation times, and
1377 // four with the earlier time as creation times. We should only get 1287 // four with the earlier time as creation times. We should only get
1378 // two cookies remaining, but which two (other than that there should 1288 // two cookies remaining, but which two (other than that there should
1379 // be one from each set) will be random. 1289 // be one from each set) will be random.
1380 std::vector<CookieMonster::CanonicalCookie*> initial_cookies; 1290 std::vector<CanonicalCookie*> initial_cookies;
1381 AddCookieToList("www.google.com", "X=1; path=/", now, &initial_cookies); 1291 AddCookieToList("www.google.com", "X=1; path=/", now, &initial_cookies);
1382 AddCookieToList("www.google.com", "X=2; path=/", now, &initial_cookies); 1292 AddCookieToList("www.google.com", "X=2; path=/", now, &initial_cookies);
1383 AddCookieToList("www.google.com", "X=3; path=/", now, &initial_cookies); 1293 AddCookieToList("www.google.com", "X=3; path=/", now, &initial_cookies);
1384 AddCookieToList("www.google.com", "X=4; path=/", now, &initial_cookies); 1294 AddCookieToList("www.google.com", "X=4; path=/", now, &initial_cookies);
1385 1295
1386 AddCookieToList("www.google.com", "Y=1; path=/", earlier, &initial_cookies); 1296 AddCookieToList("www.google.com", "Y=1; path=/", earlier, &initial_cookies);
1387 AddCookieToList("www.google.com", "Y=2; path=/", earlier, &initial_cookies); 1297 AddCookieToList("www.google.com", "Y=2; path=/", earlier, &initial_cookies);
1388 AddCookieToList("www.google.com", "Y=3; path=/", earlier, &initial_cookies); 1298 AddCookieToList("www.google.com", "Y=3; path=/", earlier, &initial_cookies);
1389 AddCookieToList("www.google.com", "Y=4; path=/", earlier, &initial_cookies); 1299 AddCookieToList("www.google.com", "Y=4; path=/", earlier, &initial_cookies);
1390 1300
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
1651 1561
1652 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails1", "A", 1562 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails1", "A",
1653 ".google.com", "/", Time(), false, false); 1563 ".google.com", "/", Time(), false, false);
1654 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails2", "A", 1564 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails2", "A",
1655 ".google.com", "/", Time(), false, false); 1565 ".google.com", "/", Time(), false, false);
1656 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails3", "A", 1566 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails3", "A",
1657 ".google.com", "/", Time(), false, false); 1567 ".google.com", "/", Time(), false, false);
1658 1568
1659 // Now we check 1569 // Now we check
1660 CookieList cookie_list(GetAllCookies(cm)); 1570 CookieList cookie_list(GetAllCookies(cm));
1661 typedef std::map<int64, CookieMonster::CanonicalCookie> TimeCookieMap; 1571 typedef std::map<int64, CanonicalCookie> TimeCookieMap;
1662 TimeCookieMap check_map; 1572 TimeCookieMap check_map;
1663 for (CookieList::const_iterator it = cookie_list.begin(); 1573 for (CookieList::const_iterator it = cookie_list.begin();
1664 it != cookie_list.end(); it++) { 1574 it != cookie_list.end(); it++) {
1665 const int64 creation_date = it->CreationDate().ToInternalValue(); 1575 const int64 creation_date = it->CreationDate().ToInternalValue();
1666 TimeCookieMap::const_iterator 1576 TimeCookieMap::const_iterator
1667 existing_cookie_it(check_map.find(creation_date)); 1577 existing_cookie_it(check_map.find(creation_date));
1668 EXPECT_TRUE(existing_cookie_it == check_map.end()) 1578 EXPECT_TRUE(existing_cookie_it == check_map.end())
1669 << "Cookie " << it->Name() << " has same creation date (" 1579 << "Cookie " << it->Name() << " has same creation date ("
1670 << it->CreationDate().ToInternalValue() 1580 << it->CreationDate().ToInternalValue()
1671 << ") as previously entered cookie " 1581 << ") as previously entered cookie "
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1751 { 1661 {
1752 scoped_refptr<CookieMonster> cmin(new CookieMonster(store, NULL)); 1662 scoped_refptr<CookieMonster> cmin(new CookieMonster(store, NULL));
1753 CookieList cookies(GetAllCookies(cmin)); 1663 CookieList cookies(GetAllCookies(cmin));
1754 ASSERT_EQ(2u, cookies.size()); 1664 ASSERT_EQ(2u, cookies.size());
1755 // Ordering is path length, then creation time. So second cookie 1665 // Ordering is path length, then creation time. So second cookie
1756 // will come first, and we need to swap them. 1666 // will come first, and we need to swap them.
1757 std::swap(cookies[0], cookies[1]); 1667 std::swap(cookies[0], cookies[1]);
1758 for (int output_index = 0; output_index < 2; output_index++) { 1668 for (int output_index = 0; output_index < 2; output_index++) {
1759 int input_index = output_index * 2; 1669 int input_index = output_index * 2;
1760 const CookiesInputInfo* input = &input_info[input_index]; 1670 const CookiesInputInfo* input = &input_info[input_index];
1761 const CookieMonster::CanonicalCookie* output = &cookies[output_index]; 1671 const CanonicalCookie* output = &cookies[output_index];
1762 1672
1763 EXPECT_EQ(input->name, output->Name()); 1673 EXPECT_EQ(input->name, output->Name());
1764 EXPECT_EQ(input->value, output->Value()); 1674 EXPECT_EQ(input->value, output->Value());
1765 EXPECT_EQ(GURL(input->gurl).host(), output->Domain()); 1675 EXPECT_EQ(GURL(input->gurl).host(), output->Domain());
1766 EXPECT_EQ(input->path, output->Path()); 1676 EXPECT_EQ(input->path, output->Path());
1767 EXPECT_LE(current.ToInternalValue(), 1677 EXPECT_LE(current.ToInternalValue(),
1768 output->CreationDate().ToInternalValue()); 1678 output->CreationDate().ToInternalValue());
1769 EXPECT_EQ(input->secure, output->IsSecure()); 1679 EXPECT_EQ(input->secure, output->IsSecure());
1770 EXPECT_EQ(input->http_only, output->IsHttpOnly()); 1680 EXPECT_EQ(input->http_only, output->IsHttpOnly());
1771 EXPECT_TRUE(output->IsPersistent()); 1681 EXPECT_TRUE(output->IsPersistent());
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1926 } 1836 }
1927 1837
1928 namespace { 1838 namespace {
1929 1839
1930 // Mock PersistentCookieStore that keeps track of the number of Flush() calls. 1840 // Mock PersistentCookieStore that keeps track of the number of Flush() calls.
1931 class FlushablePersistentStore : public CookieMonster::PersistentCookieStore { 1841 class FlushablePersistentStore : public CookieMonster::PersistentCookieStore {
1932 public: 1842 public:
1933 FlushablePersistentStore() : flush_count_(0) {} 1843 FlushablePersistentStore() : flush_count_(0) {}
1934 1844
1935 void Load(const LoadedCallback& loaded_callback) { 1845 void Load(const LoadedCallback& loaded_callback) {
1936 std::vector<CookieMonster::CanonicalCookie*> out_cookies; 1846 std::vector<CanonicalCookie*> out_cookies;
1937 MessageLoop::current()->PostTask(FROM_HERE, 1847 MessageLoop::current()->PostTask(FROM_HERE,
1938 base::Bind(&net::LoadedCallbackTask::Run, 1848 base::Bind(&net::LoadedCallbackTask::Run,
1939 new net::LoadedCallbackTask(loaded_callback, out_cookies))); 1849 new net::LoadedCallbackTask(loaded_callback, out_cookies)));
1940 } 1850 }
1941 1851
1942 void LoadCookiesForKey(const std::string& key, 1852 void LoadCookiesForKey(const std::string& key,
1943 const LoadedCallback& loaded_callback) { 1853 const LoadedCallback& loaded_callback) {
1944 Load(loaded_callback); 1854 Load(loaded_callback);
1945 } 1855 }
1946 1856
1947 void AddCookie(const CookieMonster::CanonicalCookie&) {} 1857 void AddCookie(const CanonicalCookie&) {}
1948 void UpdateCookieAccessTime(const CookieMonster::CanonicalCookie&) {} 1858 void UpdateCookieAccessTime(const CanonicalCookie&) {}
1949 void DeleteCookie(const CookieMonster::CanonicalCookie&) {} 1859 void DeleteCookie(const CanonicalCookie&) {}
1950 void SetForceKeepSessionState() {} 1860 void SetForceKeepSessionState() {}
1951 1861
1952 void Flush(const base::Closure& callback) { 1862 void Flush(const base::Closure& callback) {
1953 ++flush_count_; 1863 ++flush_count_;
1954 if (!callback.is_null()) 1864 if (!callback.is_null())
1955 callback.Run(); 1865 callback.Run();
1956 } 1866 }
1957 1867
1958 int flush_count() { 1868 int flush_count() {
1959 return flush_count_; 1869 return flush_count_;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
2032 MessageLoop::current()->RunAllPending(); 1942 MessageLoop::current()->RunAllPending();
2033 1943
2034 ASSERT_EQ(2, counter->callback_count()); 1944 ASSERT_EQ(2, counter->callback_count());
2035 1945
2036 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get())); 1946 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get()));
2037 MessageLoop::current()->RunAllPending(); 1947 MessageLoop::current()->RunAllPending();
2038 1948
2039 ASSERT_EQ(3, counter->callback_count()); 1949 ASSERT_EQ(3, counter->callback_count());
2040 } 1950 }
2041 1951
2042 TEST_F(CookieMonsterTest, GetCookieSourceFromURL) {
2043 EXPECT_EQ("http://example.com/",
2044 CookieMonster::CanonicalCookie::GetCookieSourceFromURL(
2045 GURL("http://example.com")));
2046 EXPECT_EQ("http://example.com/",
2047 CookieMonster::CanonicalCookie::GetCookieSourceFromURL(
2048 GURL("http://example.com/")));
2049 EXPECT_EQ("http://example.com/",
2050 CookieMonster::CanonicalCookie::GetCookieSourceFromURL(
2051 GURL("http://example.com/test")));
2052 EXPECT_EQ("file:///tmp/test.html",
2053 CookieMonster::CanonicalCookie::GetCookieSourceFromURL(
2054 GURL("file:///tmp/test.html")));
2055 EXPECT_EQ("http://example.com/",
2056 CookieMonster::CanonicalCookie::GetCookieSourceFromURL(
2057 GURL("http://example.com:1234/")));
2058 EXPECT_EQ("http://example.com/",
2059 CookieMonster::CanonicalCookie::GetCookieSourceFromURL(
2060 GURL("https://example.com/")));
2061 EXPECT_EQ("http://example.com/",
2062 CookieMonster::CanonicalCookie::GetCookieSourceFromURL(
2063 GURL("http://user:pwd@example.com/")));
2064 EXPECT_EQ("http://example.com/",
2065 CookieMonster::CanonicalCookie::GetCookieSourceFromURL(
2066 GURL("http://example.com/test?foo")));
2067 EXPECT_EQ("http://example.com/",
2068 CookieMonster::CanonicalCookie::GetCookieSourceFromURL(
2069 GURL("http://example.com/test#foo")));
2070 }
2071
2072 TEST_F(CookieMonsterTest, HistogramCheck) { 1952 TEST_F(CookieMonsterTest, HistogramCheck) {
2073 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1953 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2074 // Should match call in InitializeHistograms, but doesn't really matter 1954 // Should match call in InitializeHistograms, but doesn't really matter
2075 // since the histogram should have been initialized by the CM construction 1955 // since the histogram should have been initialized by the CM construction
2076 // above. 1956 // above.
2077 base::Histogram* expired_histogram = 1957 base::Histogram* expired_histogram =
2078 base::Histogram::FactoryGet( 1958 base::Histogram::FactoryGet(
2079 "Cookie.ExpirationDurationMinutes", 1, 10 * 365 * 24 * 60, 50, 1959 "Cookie.ExpirationDurationMinutes", 1, 10 * 365 * 24 * 60, 50,
2080 base::Histogram::kUmaTargetedHistogramFlag); 1960 base::Histogram::kUmaTargetedHistogramFlag);
2081 1961
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
2163 2043
2164 void DeleteAllForHostTask(CookieMonster* cm, 2044 void DeleteAllForHostTask(CookieMonster* cm,
2165 const GURL& url, 2045 const GURL& url,
2166 DeleteCallback* callback) { 2046 DeleteCallback* callback) {
2167 cm->DeleteAllForHostAsync( 2047 cm->DeleteAllForHostAsync(
2168 url, 2048 url,
2169 base::Bind(&DeleteCallback::Run, base::Unretained(callback))); 2049 base::Bind(&DeleteCallback::Run, base::Unretained(callback)));
2170 } 2050 }
2171 2051
2172 void DeleteCanonicalCookieTask(CookieMonster* cm, 2052 void DeleteCanonicalCookieTask(CookieMonster* cm,
2173 const CookieMonster::CanonicalCookie& cookie, 2053 const CanonicalCookie& cookie,
2174 SetCookieCallback* callback) { 2054 SetCookieCallback* callback) {
2175 cm->DeleteCanonicalCookieAsync( 2055 cm->DeleteCanonicalCookieAsync(
2176 cookie, 2056 cookie,
2177 base::Bind(&SetCookieCallback::Run, base::Unretained(callback))); 2057 base::Bind(&SetCookieCallback::Run, base::Unretained(callback)));
2178 } 2058 }
2179 2059
2180 protected: 2060 protected:
2181 void RunOnOtherThread(const base::Closure& task) { 2061 void RunOnOtherThread(const base::Closure& task) {
2182 other_thread_.Start(); 2062 other_thread_.Start();
2183 other_thread_.message_loop()->PostTask(FROM_HERE, task); 2063 other_thread_.message_loop()->PostTask(FROM_HERE, task);
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
2329 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask, 2209 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask,
2330 base::Unretained(this), 2210 base::Unretained(this),
2331 cm, *it, &callback); 2211 cm, *it, &callback);
2332 RunOnOtherThread(task); 2212 RunOnOtherThread(task);
2333 EXPECT_TRUE(callback.did_run()); 2213 EXPECT_TRUE(callback.did_run());
2334 EXPECT_TRUE(callback.result()); 2214 EXPECT_TRUE(callback.result());
2335 } 2215 }
2336 2216
2337 TEST_F(CookieMonsterTest, InvalidExpiryTime) { 2217 TEST_F(CookieMonsterTest, InvalidExpiryTime) {
2338 ParsedCookie pc(std::string(kValidCookieLine) + "; expires=Blarg arg arg"); 2218 ParsedCookie pc(std::string(kValidCookieLine) + "; expires=Blarg arg arg");
2339 scoped_ptr<CookieMonster::CanonicalCookie> cookie( 2219 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create(url_google_, pc));
2340 CookieMonster::CanonicalCookie::Create(url_google_, pc));
2341 2220
2342 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) 2221 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES)
2343 ASSERT_TRUE(cookie->IsPersistent()); 2222 ASSERT_TRUE(cookie->IsPersistent());
2344 #else 2223 #else
2345 ASSERT_FALSE(cookie->IsPersistent()); 2224 ASSERT_FALSE(cookie->IsPersistent());
2346 #endif 2225 #endif
2347 } 2226 }
2348 2227
2349 // Test that CookieMonster writes session cookies into the underlying 2228 // Test that CookieMonster writes session cookies into the underlying
2350 // CookieStore if the "persist session cookies" option is on. 2229 // CookieStore if the "persist session cookies" option is on.
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
2421 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar")); 2300 EXPECT_TRUE(SetCookie(cm, url_google_, "B=Bar"));
2422 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_)); 2301 this->MatchCookieLines("A=Foo; B=Bar", GetCookies(cm, url_google_));
2423 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) 2302 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES)
2424 EXPECT_EQ(6u, store->commands().size()); 2303 EXPECT_EQ(6u, store->commands().size());
2425 #else 2304 #else
2426 EXPECT_EQ(5u, store->commands().size()); 2305 EXPECT_EQ(5u, store->commands().size());
2427 #endif 2306 #endif
2428 } 2307 }
2429 2308
2430 } // namespace net 2309 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698