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

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

Issue 7833042: Finalize a CL originally by departed intern ycxiao@ that detaches the loading of cookies from the... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 3 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
« no previous file with comments | « net/base/cookie_monster_store_test.cc ('k') | net/base/cookie_store.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <time.h> 5 #include <time.h>
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop.h" 14 #include "base/message_loop.h"
15 #include "base/metrics/histogram.h" 15 #include "base/metrics/histogram.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/stringprintf.h" 17 #include "base/stringprintf.h"
18 #include "base/threading/platform_thread.h" 18 #include "base/threading/platform_thread.h"
19 #include "base/threading/thread.h" 19 #include "base/threading/thread.h"
20 #include "base/time.h" 20 #include "base/time.h"
21 #include "googleurl/src/gurl.h" 21 #include "googleurl/src/gurl.h"
22 #include "net/base/cookie_monster.h" 22 #include "net/base/cookie_monster.h"
23 #include "net/base/cookie_monster_store_test.h" // For CookieStore Mock 23 #include "net/base/cookie_monster_store_test.h" // For CookieStore mock
24 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 namespace net { 27 namespace net {
27 28
28 using base::Time; 29 using base::Time;
29 using base::TimeDelta; 30 using base::TimeDelta;
30 using base::Thread; 31 using base::Thread;
31 32
32 namespace { 33 namespace {
33 34
35 // TODO(erikwright): Replace the pre-existing MockPersistentCookieStore (and
36 // brethren) with this one, and remove the 'New' prefix.
37 class NewMockPersistentCookieStore
38 : public CookieMonster::PersistentCookieStore {
39 public:
40 MOCK_METHOD1(Load, bool(const LoadedCallback& loaded_callback));
41 MOCK_METHOD1(AddCookie, void(const CookieMonster::CanonicalCookie& cc));
42 MOCK_METHOD1(UpdateCookieAccessTime,
43 void(const CookieMonster::CanonicalCookie& cc));
44 MOCK_METHOD1(DeleteCookie, void(const CookieMonster::CanonicalCookie& cc));
45 MOCK_METHOD1(SetClearLocalStateOnExit, void(bool clear_local_state));
46 MOCK_METHOD1(Flush, void(Task* completion_task));
47 };
48
34 const int kTimeout = 1000; 49 const int kTimeout = 1000;
35 50
36 const char* kTopLevelDomainPlus1 = "http://www.harvard.edu"; 51 const char* kTopLevelDomainPlus1 = "http://www.harvard.edu";
37 const char* kTopLevelDomainPlus2 = "http://www.math.harvard.edu"; 52 const char* kTopLevelDomainPlus2 = "http://www.math.harvard.edu";
38 const char* kTopLevelDomainPlus2Secure = "https://www.math.harvard.edu"; 53 const char* kTopLevelDomainPlus2Secure = "https://www.math.harvard.edu";
39 const char* kTopLevelDomainPlus3 = 54 const char* kTopLevelDomainPlus3 =
40 "http://www.bourbaki.math.harvard.edu"; 55 "http://www.bourbaki.math.harvard.edu";
41 const char* kOtherDomain = "http://www.mit.edu"; 56 const char* kOtherDomain = "http://www.mit.edu";
42 57
43 const char kUrlGoogle[] = "http://www.google.izzle"; 58 const char kUrlGoogle[] = "http://www.google.izzle";
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 std::string cookie_; 153 std::string cookie_;
139 }; 154 };
140 155
141 class GetCookiesWithInfoCallback : public CookieCallback { 156 class GetCookiesWithInfoCallback : public CookieCallback {
142 public: 157 public:
143 GetCookiesWithInfoCallback() {} 158 GetCookiesWithInfoCallback() {}
144 explicit GetCookiesWithInfoCallback(Thread* run_in_thread) 159 explicit GetCookiesWithInfoCallback(Thread* run_in_thread)
145 : CookieCallback(run_in_thread) {} 160 : CookieCallback(run_in_thread) {}
146 161
147 void Run( 162 void Run(
148 std::string* cookie_line, 163 const std::string& cookie_line,
149 std::vector<CookieStore::CookieInfo>* cookie_info) { 164 const std::vector<CookieStore::CookieInfo>& cookie_info) {
150 cookie_line_ = *cookie_line; 165 cookie_line_ = cookie_line;
151 cookie_info_ = *cookie_info; 166 cookie_info_ = cookie_info;
152 CallbackEpilogue(); 167 CallbackEpilogue();
153 } 168 }
154 169
155 const std::string& cookie_line() { return cookie_line_; } 170 const std::string& cookie_line() { return cookie_line_; }
156 const std::vector<CookieStore::CookieInfo>& cookie_info() { 171 const std::vector<CookieStore::CookieInfo>& cookie_info() {
157 return cookie_info_; 172 return cookie_info_;
158 } 173 }
159 174
160 private: 175 private:
161 std::string cookie_line_; 176 std::string cookie_line_;
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 425
411 CookieMonster::ParsedCookie pc1(blankpairs + "secure"); 426 CookieMonster::ParsedCookie pc1(blankpairs + "secure");
412 EXPECT_TRUE(pc1.IsValid()); 427 EXPECT_TRUE(pc1.IsValid());
413 EXPECT_TRUE(pc1.IsSecure()); 428 EXPECT_TRUE(pc1.IsSecure());
414 429
415 CookieMonster::ParsedCookie pc2(blankpairs + ";secure"); 430 CookieMonster::ParsedCookie pc2(blankpairs + ";secure");
416 EXPECT_TRUE(pc2.IsValid()); 431 EXPECT_TRUE(pc2.IsValid());
417 EXPECT_FALSE(pc2.IsSecure()); 432 EXPECT_FALSE(pc2.IsSecure());
418 } 433 }
419 434
420 // TODO some better test cases for invalid cookies. 435 // TODO(erikwright): some better test cases for invalid cookies.
421 TEST(ParsedCookieTest, InvalidWhitespace) { 436 TEST(ParsedCookieTest, InvalidWhitespace) {
422 CookieMonster::ParsedCookie pc(" "); 437 CookieMonster::ParsedCookie pc(" ");
423 EXPECT_FALSE(pc.IsValid()); 438 EXPECT_FALSE(pc.IsValid());
424 } 439 }
425 440
426 TEST(ParsedCookieTest, InvalidTooLong) { 441 TEST(ParsedCookieTest, InvalidTooLong) {
427 std::string maxstr; 442 std::string maxstr;
428 maxstr.resize(CookieMonster::ParsedCookie::kMaxCookieSize, 'a'); 443 maxstr.resize(CookieMonster::ParsedCookie::kMaxCookieSize, 'a');
429 444
430 CookieMonster::ParsedCookie pc1(maxstr); 445 CookieMonster::ParsedCookie pc1(maxstr);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 url_google_bar_(kUrlGoogleBar), 507 url_google_bar_(kUrlGoogleBar),
493 message_loop_factory_(MessageLoop::current()) {} 508 message_loop_factory_(MessageLoop::current()) {}
494 509
495 // Helper methods for the asynchronous Cookie Store API that call the 510 // Helper methods for the asynchronous Cookie Store API that call the
496 // asynchronous method and then pump the loop until the callback is invoked, 511 // asynchronous method and then pump the loop until the callback is invoked,
497 // finally returning the value. 512 // finally returning the value.
498 513
499 std::string GetCookies(CookieMonster* cm, const GURL& url) { 514 std::string GetCookies(CookieMonster* cm, const GURL& url) {
500 DCHECK(cm); 515 DCHECK(cm);
501 GetCookieStringCallback callback; 516 GetCookieStringCallback callback;
502 cm->GetCookiesAsync( 517 cm->GetCookiesWithOptionsAsync(
503 url, base::Bind(&GetCookieStringCallback::Run, 518 url, CookieOptions(), base::Bind(&GetCookieStringCallback::Run,
504 base::Unretained(&callback))); 519 base::Unretained(&callback)));
505 RunFor(kTimeout); 520 RunFor(kTimeout);
506 EXPECT_TRUE(callback.did_run()); 521 EXPECT_TRUE(callback.did_run());
507 return callback.cookie(); 522 return callback.cookie();
508 } 523 }
509 524
510 std::string GetCookiesWithOptions(CookieMonster* cm, 525 std::string GetCookiesWithOptions(CookieMonster* cm,
511 const GURL& url, 526 const GURL& url,
512 const CookieOptions& options) { 527 const CookieOptions& options) {
513 DCHECK(cm); 528 DCHECK(cm);
514 GetCookieStringCallback callback; 529 GetCookieStringCallback callback;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 DCHECK(cm); 597 DCHECK(cm);
583 SetCookieCallback callback; 598 SetCookieCallback callback;
584 cm->SetCookieWithOptionsAsync( 599 cm->SetCookieWithOptionsAsync(
585 url, cookie_line, options, 600 url, cookie_line, options,
586 base::Bind(&SetCookieCallback::Run, base::Unretained(&callback))); 601 base::Bind(&SetCookieCallback::Run, base::Unretained(&callback)));
587 RunFor(kTimeout); 602 RunFor(kTimeout);
588 EXPECT_TRUE(callback.did_run()); 603 EXPECT_TRUE(callback.did_run());
589 return callback.result(); 604 return callback.result();
590 } 605 }
591 606
607 bool SetCookie(CookieMonster* cm,
608 const GURL& url,
609 const std::string& cookie_line) {
610 CookieOptions options;
611 return SetCookieWithOptions(cm, url, cookie_line, options);
612 }
613
592 bool SetCookieWithDetails(CookieMonster* cm, 614 bool SetCookieWithDetails(CookieMonster* cm,
593 const GURL& url, 615 const GURL& url,
594 const std::string& name, 616 const std::string& name,
595 const std::string& value, 617 const std::string& value,
596 const std::string& domain, 618 const std::string& domain,
597 const std::string& path, 619 const std::string& path,
598 const base::Time& expiration_time, 620 const base::Time& expiration_time,
599 bool secure, bool http_only) { 621 bool secure, bool http_only) {
600 DCHECK(cm); 622 DCHECK(cm);
601 SetCookieCallback callback; 623 SetCookieCallback callback;
(...skipping 10 matching lines...) Expand all
612 const std::string& cookie_name) { 634 const std::string& cookie_name) {
613 DCHECK(cm); 635 DCHECK(cm);
614 DeleteCookieCallback callback; 636 DeleteCookieCallback callback;
615 cm->DeleteCookieAsync(url, cookie_name, 637 cm->DeleteCookieAsync(url, cookie_name,
616 base::Bind(&DeleteCookieCallback::Run, 638 base::Bind(&DeleteCookieCallback::Run,
617 base::Unretained(&callback))); 639 base::Unretained(&callback)));
618 RunFor(kTimeout); 640 RunFor(kTimeout);
619 EXPECT_TRUE(callback.did_run()); 641 EXPECT_TRUE(callback.did_run());
620 } 642 }
621 643
644 int DeleteAll(CookieMonster*cm) {
645 DCHECK(cm);
646 DeleteCallback callback;
647 cm->DeleteAllAsync(
648 base::Bind(&DeleteCallback::Run, base::Unretained(&callback)));
649 RunFor(kTimeout);
650 EXPECT_TRUE(callback.did_run());
651 return callback.num_deleted();
652 }
653
622 int DeleteAllCreatedBetween(CookieMonster*cm, 654 int DeleteAllCreatedBetween(CookieMonster*cm,
623 const base::Time& delete_begin, 655 const base::Time& delete_begin,
624 const base::Time& delete_end, 656 const base::Time& delete_end) {
625 bool sync_to_store) {
626 DCHECK(cm); 657 DCHECK(cm);
627 DeleteCallback callback; 658 DeleteCallback callback;
628 cm->DeleteAllCreatedBetweenAsync( 659 cm->DeleteAllCreatedBetweenAsync(
629 delete_begin, delete_end, sync_to_store, 660 delete_begin, delete_end,
630 base::Bind(&DeleteCallback::Run, base::Unretained(&callback))); 661 base::Bind(&DeleteCallback::Run, base::Unretained(&callback)));
631 RunFor(kTimeout); 662 RunFor(kTimeout);
632 EXPECT_TRUE(callback.did_run()); 663 EXPECT_TRUE(callback.did_run());
633 return callback.num_deleted(); 664 return callback.num_deleted();
634 } 665 }
635 666
636 int DeleteAllForHost(CookieMonster*cm, 667 int DeleteAllForHost(CookieMonster*cm,
637 const GURL& url) { 668 const GURL& url) {
638 DCHECK(cm); 669 DCHECK(cm);
639 DeleteCallback callback; 670 DeleteCallback callback;
(...skipping 28 matching lines...) Expand all
668 699
669 // Helper for DeleteAllForHost test; repopulates CM with same layout 700 // Helper for DeleteAllForHost test; repopulates CM with same layout
670 // each time. 701 // each time.
671 void PopulateCmForDeleteAllForHost(scoped_refptr<CookieMonster> cm) { 702 void PopulateCmForDeleteAllForHost(scoped_refptr<CookieMonster> cm) {
672 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1); 703 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1);
673 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2); 704 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2);
674 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure); 705 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure);
675 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3); 706 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3);
676 GURL url_other(kOtherDomain); 707 GURL url_other(kOtherDomain);
677 708
678 cm->DeleteAll(true); 709 DeleteAll(cm);
679 710
680 // Static population for probe: 711 // Static population for probe:
681 // * Three levels of domain cookie (.b.a, .c.b.a, .d.c.b.a) 712 // * Three levels of domain cookie (.b.a, .c.b.a, .d.c.b.a)
682 // * Three levels of host cookie (w.b.a, w.c.b.a, w.d.c.b.a) 713 // * Three levels of host cookie (w.b.a, w.c.b.a, w.d.c.b.a)
683 // * http_only cookie (w.c.b.a) 714 // * http_only cookie (w.c.b.a)
684 // * Two secure cookies (.c.b.a, w.c.b.a) 715 // * Two secure cookies (.c.b.a, w.c.b.a)
685 // * Two domain path cookies (.c.b.a/dir1, .c.b.a/dir1/dir2) 716 // * Two domain path cookies (.c.b.a/dir1, .c.b.a/dir1/dir2)
686 // * Two host path cookies (w.c.b.a/dir1, w.c.b.a/dir1/dir2) 717 // * Two host path cookies (w.c.b.a/dir1, w.c.b.a/dir1/dir2)
687 718
688 // Domain cookies 719 // Domain cookies
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 int domain_purge_cookies, 802 int domain_purge_cookies,
772 CookieMonster::ExpiryAndKeyScheme key_scheme) { 803 CookieMonster::ExpiryAndKeyScheme key_scheme) {
773 const int more_than_enough_cookies = 804 const int more_than_enough_cookies =
774 (domain_max_cookies + domain_purge_cookies) * 2; 805 (domain_max_cookies + domain_purge_cookies) * 2;
775 // Add a bunch of cookies on a single host, should purge them. 806 // Add a bunch of cookies on a single host, should purge them.
776 { 807 {
777 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 808 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
778 cm->SetExpiryAndKeyScheme(key_scheme); 809 cm->SetExpiryAndKeyScheme(key_scheme);
779 for (int i = 0; i < more_than_enough_cookies; ++i) { 810 for (int i = 0; i < more_than_enough_cookies; ++i) {
780 std::string cookie = base::StringPrintf("a%03d=b", i); 811 std::string cookie = base::StringPrintf("a%03d=b", i);
781 EXPECT_TRUE(cm->SetCookie(url_google_, cookie)); 812 EXPECT_TRUE(SetCookie(cm, url_google_, cookie));
782 std::string cookies = this->GetCookies(cm, url_google_); 813 std::string cookies = this->GetCookies(cm, url_google_);
783 // Make sure we find it in the cookies. 814 // Make sure we find it in the cookies.
784 EXPECT_NE(cookies.find(cookie), std::string::npos); 815 EXPECT_NE(cookies.find(cookie), std::string::npos);
785 // Count the number of cookies. 816 // Count the number of cookies.
786 EXPECT_LE(CountInString(cookies, '='), domain_max_cookies); 817 EXPECT_LE(CountInString(cookies, '='), domain_max_cookies);
787 } 818 }
788 } 819 }
789 820
790 // Add a bunch of cookies on multiple hosts within a single eTLD. 821 // Add a bunch of cookies on multiple hosts within a single eTLD.
791 // Should keep at least kDomainMaxCookies - kDomainPurgeCookies 822 // Should keep at least kDomainMaxCookies - kDomainPurgeCookies
792 // between them. If we are using the effective domain keying system 823 // between them. If we are using the effective domain keying system
793 // (EKS_KEEP_RECENT_AND_PURGE_ETLDP1) we shouldn't go above 824 // (EKS_KEEP_RECENT_AND_PURGE_ETLDP1) we shouldn't go above
794 // kDomainMaxCookies for both together. If we're using the domain 825 // kDomainMaxCookies for both together. If we're using the domain
795 // keying system (EKS_DISCARD_RECENT_AND_PURGE_DOMAIN), each 826 // keying system (EKS_DISCARD_RECENT_AND_PURGE_DOMAIN), each
796 // individual domain shouldn't go above kDomainMaxCookies. 827 // individual domain shouldn't go above kDomainMaxCookies.
797 GURL url_google_specific(kUrlGoogleSpecific); 828 GURL url_google_specific(kUrlGoogleSpecific);
798 { 829 {
799 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 830 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
800 cm->SetExpiryAndKeyScheme(key_scheme); 831 cm->SetExpiryAndKeyScheme(key_scheme);
801 for (int i = 0; i < more_than_enough_cookies; ++i) { 832 for (int i = 0; i < more_than_enough_cookies; ++i) {
802 std::string cookie_general = base::StringPrintf("a%03d=b", i); 833 std::string cookie_general = base::StringPrintf("a%03d=b", i);
803 EXPECT_TRUE(cm->SetCookie(url_google_, cookie_general)); 834 EXPECT_TRUE(SetCookie(cm, url_google_, cookie_general));
804 std::string cookie_specific = base::StringPrintf("c%03d=b", i); 835 std::string cookie_specific = base::StringPrintf("c%03d=b", i);
805 EXPECT_TRUE(cm->SetCookie(url_google_specific, cookie_specific)); 836 EXPECT_TRUE(SetCookie(cm, url_google_specific, cookie_specific));
806 std::string cookies_general = this->GetCookies(cm, url_google_); 837 std::string cookies_general = this->GetCookies(cm, url_google_);
807 EXPECT_NE(cookies_general.find(cookie_general), std::string::npos); 838 EXPECT_NE(cookies_general.find(cookie_general), std::string::npos);
808 std::string cookies_specific = 839 std::string cookies_specific =
809 this->GetCookies(cm, url_google_specific); 840 this->GetCookies(cm, url_google_specific);
810 EXPECT_NE(cookies_specific.find(cookie_specific), std::string::npos); 841 EXPECT_NE(cookies_specific.find(cookie_specific), std::string::npos);
811 if (key_scheme == CookieMonster::EKS_KEEP_RECENT_AND_PURGE_ETLDP1) { 842 if (key_scheme == CookieMonster::EKS_KEEP_RECENT_AND_PURGE_ETLDP1) {
812 EXPECT_LE((CountInString(cookies_general, '=') + 843 EXPECT_LE((CountInString(cookies_general, '=') +
813 CountInString(cookies_specific, '=')), 844 CountInString(cookies_specific, '=')),
814 domain_max_cookies); 845 domain_max_cookies);
815 } else { 846 } else {
(...skipping 17 matching lines...) Expand all
833 EXPECT_GE(general_cookies, 864 EXPECT_GE(general_cookies,
834 domain_max_cookies - domain_purge_cookies); 865 domain_max_cookies - domain_purge_cookies);
835 EXPECT_LE(general_cookies, domain_max_cookies); 866 EXPECT_LE(general_cookies, domain_max_cookies);
836 EXPECT_GE(specific_cookies, 867 EXPECT_GE(specific_cookies,
837 domain_max_cookies - domain_purge_cookies); 868 domain_max_cookies - domain_purge_cookies);
838 EXPECT_LE(specific_cookies, domain_max_cookies); 869 EXPECT_LE(specific_cookies, domain_max_cookies);
839 } 870 }
840 } 871 }
841 } 872 }
842 873
874 // Function for creating a CM with a number of cookies in it,
875 // no store (and hence no ability to affect access time).
876 CookieMonster* CreateMonsterForGC(int num_cookies) {
877 CookieMonster* cm(new CookieMonster(NULL, NULL));
878 for (int i = 0; i < num_cookies; i++) {
879 SetCookie(cm, GURL(StringPrintf("http://h%05d.izzle", i)), "a=1");
880 }
881 return cm;
882 }
883
843 protected: 884 protected:
844 GURL url_google_; 885 GURL url_google_;
845 GURL url_google_secure_; 886 GURL url_google_secure_;
846 GURL url_google_foo_; 887 GURL url_google_foo_;
847 GURL url_google_bar_; 888 GURL url_google_bar_;
848 889
849 ScopedRunnableMethodFactory<MessageLoop> message_loop_factory_; 890 ScopedRunnableMethodFactory<MessageLoop> message_loop_factory_;
850 }; 891 };
851 892
893 // TODO(erikwright): Replace the other callbacks and synchronous helper methods
894 // in this test suite with these Mocks.
895 template<typename T, typename C> class MockCookieCallback {
896 public:
897 C AsCallback() {
898 return base::Bind(&T::Invoke, base::Unretained(static_cast<T*>(this)));
899 }
900 };
901
902 class MockGetCookiesCallback
903 : public MockCookieCallback<MockGetCookiesCallback,
904 CookieStore::GetCookiesCallback> {
905 public:
906 MOCK_METHOD1(Invoke, void(const std::string& cookies));
907 };
908
909 class MockGetCookieInfoCallback
910 : public MockCookieCallback<MockGetCookieInfoCallback,
911 CookieStore::GetCookieInfoCallback> {
912 public:
913 MOCK_METHOD2(Invoke,
914 void(const std::string& cookies,
915 const std::vector<CookieStore::CookieInfo>& cookie_infos));
916 };
917
918 class MockSetCookiesCallback
919 : public MockCookieCallback<MockSetCookiesCallback,
920 CookieStore::SetCookiesCallback> {
921 public:
922 MOCK_METHOD1(Invoke, void(bool success));
923 };
924
925 class MockClosure
926 : public MockCookieCallback<MockClosure, base::Closure> {
927 public:
928 MOCK_METHOD0(Invoke, void(void));
929 };
930
931 class MockGetCookieListCallback
932 : public MockCookieCallback<MockGetCookieListCallback,
933 CookieMonster::GetCookieListCallback> {
934 public:
935 MOCK_METHOD1(Invoke, void(const CookieList& cookies));
936 };
937
938 class MockDeleteCallback
939 : public MockCookieCallback<MockDeleteCallback,
940 CookieMonster::DeleteCallback> {
941 public:
942 MOCK_METHOD1(Invoke, void(int num_deleted));
943 };
944
945 class MockDeleteCookieCallback
946 : public MockCookieCallback<MockDeleteCookieCallback,
947 CookieMonster::DeleteCookieCallback> {
948 public:
949 MOCK_METHOD1(Invoke, void(bool success));
950 };
951
952 ACTION(QuitCurrentMessageLoop) {
953 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
954 }
955
956 // TODO(erikwright): When the synchronous helpers 'GetCookies' etc. are removed,
957 // rename these, removing the 'Action' suffix.
958 ACTION_P4(DeleteCookieAction, cookie_monster, url, name, callback) {
959 cookie_monster->DeleteCookieAsync(url, name, callback->AsCallback());
960 }
961 ACTION_P3(GetCookiesAction, cookie_monster, url, callback) {
962 cookie_monster->GetCookiesWithOptionsAsync(
963 url, CookieOptions(), callback->AsCallback());
964 }
965 ACTION_P3(GetCookiesWithInfoAction, cookie_monster, url, callback) {
966 cookie_monster->GetCookiesWithInfoAsync(
967 url, CookieOptions(), callback->AsCallback());
968 }
969 ACTION_P4(SetCookieAction, cookie_monster, url, cookie_line, callback) {
970 cookie_monster->SetCookieWithOptionsAsync(
971 url, cookie_line, CookieOptions(), callback->AsCallback());
972 }
973 ACTION_P4(DeleteAllCreatedBetweenAction,
974 cookie_monster, delete_begin, delete_end, callback) {
975 cookie_monster->DeleteAllCreatedBetweenAsync(
976 delete_begin, delete_end, callback->AsCallback());
977 }
978 ACTION_P10(SetCookieWithDetailsAction,
979 cookie_monster, url, name, value, domain, path, expiration_time,
980 secure, http_only, callback) {
981 cookie_monster->SetCookieWithDetailsAsync(
982 url, name, value, domain, path, expiration_time, secure, http_only,
983 callback->AsCallback());
984 }
985
986 ACTION_P2(GetAllCookiesAction, cookie_monster, callback) {
987 cookie_monster->GetAllCookiesAsync(callback->AsCallback());
988 }
989
990 ACTION_P3(DeleteAllForHostAction, cookie_monster, url, callback) {
991 cookie_monster->DeleteAllForHostAsync(url, callback->AsCallback());
992 }
993
994 ACTION_P3(DeleteCanonicalCookieAction, cookie_monster, cookie, callback) {
995 cookie_monster->DeleteCanonicalCookieAsync(cookie, callback->AsCallback());
996 }
997
998 ACTION_P2(DeleteAllAction, cookie_monster, callback) {
999 cookie_monster->DeleteAllAsync(callback->AsCallback());
1000 }
1001
1002 ACTION_P3(GetAllCookiesForUrlWithOptionsAction, cookie_monster, url, callback) {
1003 cookie_monster->GetAllCookiesForURLWithOptionsAsync(
1004 url, CookieOptions(), callback->AsCallback());
1005 }
1006
1007 ACTION_P3(GetAllCookiesForUrlAction, cookie_monster, url, callback) {
1008 cookie_monster->GetAllCookiesForURLAsync(url, callback->AsCallback());
1009 }
1010
852 } // namespace 1011 } // namespace
853 1012
1013 // This test suite verifies the task deferral behaviour of the CookieMonster.
1014 // Specifically, for each asynchronous method, verify that:
1015 // 1. invoking it on an uninitialized cookie store causes the store to begin
1016 // loading its backing data.
1017 // 2. The initial invocation does not complete until the loading completes.
1018 // 3. Invocations after the loading has completed complete immediately.
1019 class DeferredCookieTaskTest : public CookieMonsterTest {
1020 protected:
1021 DeferredCookieTaskTest() {
1022 persistent_store_ = new NewMockPersistentCookieStore();
1023 cookie_monster_ = new CookieMonster(persistent_store_.get(), NULL);
1024 }
1025
1026 // Defines a cookie to be returned from PersistentCookieStore::Load
1027 void DeclareLoadedCookie(const std::string& key,
1028 const std::string& cookie_line,
1029 const base::Time& creation_time) {
1030 AddCookieToList(key, cookie_line, creation_time, &loaded_cookies_);
1031 }
1032
1033 // Runs the message loop, waiting until PersistentCookieStore::Load is called.
1034 // Call CompleteLoadingAndWait to cause the load to complete.
1035 void WaitForLoadCall() {
1036 RunFor(kTimeout);
1037
1038 // Verify that PeristentStore::Load was called.
1039 testing::Mock::VerifyAndClear(persistent_store_.get());
1040 }
1041
1042 // Invokes the PersistentCookieStore::Load completion callback and waits
1043 // until the message loop is quit.
1044 void CompleteLoadingAndWait() {
1045 loaded_callback_.Run(loaded_cookies_);
1046 RunFor(kTimeout);
1047 }
1048
1049 // Performs the provided action, expecting it to cause a call to
1050 // PersistentCookieStore::Load. Call WaitForLoadCall to verify the load call
1051 // is received.
1052 void BeginWith(testing::Action<void(void)> action) {
1053 EXPECT_CALL(*this, Begin()).WillOnce(action);
1054 ExpectLoadCall();
1055 Begin();
1056 }
1057
1058 // Declares an expectation that PersistentCookieStore::Load will be called,
1059 // saving the provided callback and sending a quit to the message loop.
1060 void ExpectLoadCall() {
1061 EXPECT_CALL(*persistent_store_, Load(testing::_)).WillOnce(testing::DoAll(
1062 testing::SaveArg<0>(&loaded_callback_),
1063 QuitCurrentMessageLoop(),
1064 testing::Return(true)));
1065 }
1066
1067 // Invokes the initial action.
1068 MOCK_METHOD0(Begin, void(void));
1069
1070 // Returns the CookieMonster instance under test.
1071 CookieMonster& cookie_monster() { return *cookie_monster_; }
1072
1073 private:
1074 // Declares that mock expectations in this test suite are strictly ordered.
1075 testing::InSequence in_sequence_;
1076 // Holds cookies to be returned from PersistentCookieStore::Load.
1077 std::vector<CookieMonster::CanonicalCookie*> loaded_cookies_;
1078 // Stores the callback passed from the CookieMonster to the
1079 // PersistentCookieStore
1080 CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback_;
1081 // Stores the CookieMonster under test.
1082 scoped_refptr<CookieMonster> cookie_monster_;
1083 // Stores the mock PersistentCookieStore.
1084 scoped_refptr<NewMockPersistentCookieStore> persistent_store_;
1085 };
1086
1087 TEST_F(DeferredCookieTaskTest, DeferredGetCookies) {
1088 DeclareLoadedCookie("www.google.izzle",
1089 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1090 Time::Now() + TimeDelta::FromDays(3));
1091
1092 MockGetCookiesCallback get_cookies_callback;
1093
1094 BeginWith(GetCookiesAction(
1095 &cookie_monster(), url_google_, &get_cookies_callback));
1096
1097 WaitForLoadCall();
1098
1099 EXPECT_CALL(get_cookies_callback, Invoke("X=1")).WillOnce(
1100 GetCookiesAction(&cookie_monster(), url_google_, &get_cookies_callback));
1101 EXPECT_CALL(get_cookies_callback, Invoke("X=1")).WillOnce(
1102 QuitCurrentMessageLoop());
1103
1104 CompleteLoadingAndWait();
1105 }
1106
1107 TEST_F(DeferredCookieTaskTest, DeferredGetCookiesWithInfo) {
1108 DeclareLoadedCookie("www.google.izzle",
1109 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1110 Time::Now() + TimeDelta::FromDays(3));
1111
1112 MockGetCookieInfoCallback get_cookie_info_callback;
1113
1114 BeginWith(GetCookiesWithInfoAction(
1115 &cookie_monster(), url_google_, &get_cookie_info_callback));
1116
1117 WaitForLoadCall();
1118
1119 EXPECT_CALL(get_cookie_info_callback, Invoke("X=1", testing::_)).WillOnce(
1120 GetCookiesWithInfoAction(
1121 &cookie_monster(), url_google_, &get_cookie_info_callback));
1122 EXPECT_CALL(get_cookie_info_callback, Invoke("X=1", testing::_)).WillOnce(
1123 QuitCurrentMessageLoop());
1124
1125 CompleteLoadingAndWait();
1126 }
1127
1128 TEST_F(DeferredCookieTaskTest, DeferredSetCookie) {
1129 MockSetCookiesCallback set_cookies_callback;
1130
1131 BeginWith(SetCookieAction(
1132 &cookie_monster(), url_google_, "A=B", &set_cookies_callback));
1133
1134 WaitForLoadCall();
1135
1136 EXPECT_CALL(set_cookies_callback, Invoke(true)).WillOnce(
1137 SetCookieAction(
1138 &cookie_monster(), url_google_, "X=Y", &set_cookies_callback));
1139 EXPECT_CALL(set_cookies_callback, Invoke(true)).WillOnce(
1140 QuitCurrentMessageLoop());
1141
1142 CompleteLoadingAndWait();
1143 }
1144
1145 TEST_F(DeferredCookieTaskTest, DeferredDeleteCookie) {
1146 MockClosure delete_cookie_callback;
1147
1148 BeginWith(DeleteCookieAction(
1149 &cookie_monster(), url_google_, "A", &delete_cookie_callback));
1150
1151 WaitForLoadCall();
1152
1153 EXPECT_CALL(delete_cookie_callback, Invoke()).WillOnce(
1154 DeleteCookieAction(
1155 &cookie_monster(), url_google_, "X", &delete_cookie_callback));
1156 EXPECT_CALL(delete_cookie_callback, Invoke()).WillOnce(
1157 QuitCurrentMessageLoop());
1158
1159 CompleteLoadingAndWait();
1160 }
1161
1162 TEST_F(DeferredCookieTaskTest, DeferredSetCookieWithDetails) {
1163 MockSetCookiesCallback set_cookies_callback;
1164
1165 BeginWith(SetCookieWithDetailsAction(
1166 &cookie_monster(), url_google_foo_, "A", "B", std::string(), "/foo",
1167 base::Time(), false, false, &set_cookies_callback));
1168
1169 WaitForLoadCall();
1170
1171 EXPECT_CALL(set_cookies_callback, Invoke(true)).WillOnce(
1172 SetCookieWithDetailsAction(
1173 &cookie_monster(), url_google_foo_, "A", "B", std::string(), "/foo",
1174 base::Time(), false, false, &set_cookies_callback));
1175 EXPECT_CALL(set_cookies_callback, Invoke(true)).WillOnce(
1176 QuitCurrentMessageLoop());
1177
1178 CompleteLoadingAndWait();
1179 }
1180
1181 TEST_F(DeferredCookieTaskTest, DeferredGetAllCookies) {
1182 DeclareLoadedCookie("www.google.izzle",
1183 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1184 Time::Now() + TimeDelta::FromDays(3));
1185
1186 MockGetCookieListCallback get_cookie_list_callback;
1187
1188 BeginWith(GetAllCookiesAction(
1189 &cookie_monster(), &get_cookie_list_callback));
1190
1191 WaitForLoadCall();
1192
1193 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_)).WillOnce(
1194 GetAllCookiesAction(&cookie_monster(), &get_cookie_list_callback));
1195 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_)).WillOnce(
1196 QuitCurrentMessageLoop());
1197
1198 CompleteLoadingAndWait();
1199 }
1200
1201 TEST_F(DeferredCookieTaskTest, DeferredGetAllForUrlCookies) {
1202 DeclareLoadedCookie("www.google.izzle",
1203 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1204 Time::Now() + TimeDelta::FromDays(3));
1205
1206 MockGetCookieListCallback get_cookie_list_callback;
1207
1208 BeginWith(GetAllCookiesForUrlAction(
1209 &cookie_monster(), url_google_, &get_cookie_list_callback));
1210
1211 WaitForLoadCall();
1212
1213 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_)).WillOnce(
1214 GetAllCookiesForUrlAction(
1215 &cookie_monster(), url_google_, &get_cookie_list_callback));
1216 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_)).WillOnce(
1217 QuitCurrentMessageLoop());
1218
1219 CompleteLoadingAndWait();
1220 }
1221
1222
1223 TEST_F(DeferredCookieTaskTest, DeferredGetAllForUrlWithOptionsCookies) {
1224 DeclareLoadedCookie("www.google.izzle",
1225 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1226 Time::Now() + TimeDelta::FromDays(3));
1227
1228 MockGetCookieListCallback get_cookie_list_callback;
1229
1230 BeginWith(GetAllCookiesForUrlWithOptionsAction(
1231 &cookie_monster(), url_google_, &get_cookie_list_callback));
1232
1233 WaitForLoadCall();
1234
1235 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_)).WillOnce(
1236 GetAllCookiesForUrlWithOptionsAction(
1237 &cookie_monster(), url_google_, &get_cookie_list_callback));
1238 EXPECT_CALL(get_cookie_list_callback, Invoke(testing::_)).WillOnce(
1239 QuitCurrentMessageLoop());
1240
1241 CompleteLoadingAndWait();
1242 }
1243
1244 TEST_F(DeferredCookieTaskTest, DeferredDeleteAllCookies) {
1245 MockDeleteCallback delete_callback;
1246
1247 BeginWith(DeleteAllAction(
1248 &cookie_monster(), &delete_callback));
1249
1250 WaitForLoadCall();
1251
1252 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(
1253 DeleteAllAction(&cookie_monster(), &delete_callback));
1254 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(
1255 QuitCurrentMessageLoop());
1256
1257 CompleteLoadingAndWait();
1258 }
1259
1260 TEST_F(DeferredCookieTaskTest, DeferredDeleteAllCreatedBetweenCookies) {
1261 MockDeleteCallback delete_callback;
1262
1263 BeginWith(DeleteAllCreatedBetweenAction(
1264 &cookie_monster(), base::Time(), base::Time::Now(), &delete_callback));
1265
1266 WaitForLoadCall();
1267
1268 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(
1269 DeleteAllCreatedBetweenAction(
1270 &cookie_monster(), base::Time(), base::Time::Now(),
1271 &delete_callback));
1272 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(
1273 QuitCurrentMessageLoop());
1274
1275 CompleteLoadingAndWait();
1276 }
1277
1278 TEST_F(DeferredCookieTaskTest, DeferredDeleteAllForHostCookies) {
1279 MockDeleteCallback delete_callback;
1280
1281 BeginWith(DeleteAllForHostAction(
1282 &cookie_monster(), url_google_, &delete_callback));
1283
1284 WaitForLoadCall();
1285
1286 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(
1287 DeleteAllForHostAction(
1288 &cookie_monster(), url_google_, &delete_callback));
1289 EXPECT_CALL(delete_callback, Invoke(false)).WillOnce(
1290 QuitCurrentMessageLoop());
1291
1292 CompleteLoadingAndWait();
1293 }
1294
1295 TEST_F(DeferredCookieTaskTest, DeferredDeleteCanonicalCookie) {
1296 std::vector<CookieMonster::CanonicalCookie*> cookies;
1297 AddCookieToList("www.google.com", "X=1; path=/", base::Time::Now(), &cookies);
1298
1299 MockDeleteCookieCallback delete_cookie_callback;
1300
1301 BeginWith(DeleteCanonicalCookieAction(
1302 &cookie_monster(), *cookies[0], &delete_cookie_callback));
1303
1304 WaitForLoadCall();
1305
1306 EXPECT_CALL(delete_cookie_callback, Invoke(false)).WillOnce(
1307 DeleteCanonicalCookieAction(
1308 &cookie_monster(), *cookies[0], &delete_cookie_callback));
1309 EXPECT_CALL(delete_cookie_callback, Invoke(false)).WillOnce(
1310 QuitCurrentMessageLoop());
1311
1312 CompleteLoadingAndWait();
1313 }
1314
1315 // Verify that a series of queued tasks are executed in order upon loading of
1316 // the backing store and that new tasks received while the queued tasks are
1317 // being dispatched go to the end of the queue.
1318 TEST_F(DeferredCookieTaskTest, DeferredTaskOrder) {
1319 DeclareLoadedCookie("www.google.izzle",
1320 "X=1; path=/; expires=Mon, 18-Apr-22 22:50:14 GMT",
1321 Time::Now() + TimeDelta::FromDays(3));
1322
1323 MockGetCookiesCallback get_cookies_callback;
1324 MockSetCookiesCallback set_cookies_callback;
1325 MockClosure delete_cookie_callback;
1326 MockGetCookieInfoCallback get_cookie_info_callback;
1327
1328 EXPECT_CALL(*this, Begin()).WillOnce(testing::DoAll(
1329 GetCookiesAction(
1330 &cookie_monster(), url_google_, &get_cookies_callback),
1331 SetCookieAction(
1332 &cookie_monster(), url_google_, "A=B", &set_cookies_callback),
1333 DeleteCookieAction(
1334 &cookie_monster(), url_google_, "A", &delete_cookie_callback)));
1335 ExpectLoadCall();
1336 Begin();
1337
1338 WaitForLoadCall();
1339 EXPECT_CALL(get_cookies_callback, Invoke("X=1")).WillOnce(
1340 GetCookiesWithInfoAction(
1341 &cookie_monster(), url_google_, &get_cookie_info_callback));
1342
1343 EXPECT_CALL(set_cookies_callback, Invoke(true));
1344 EXPECT_CALL(delete_cookie_callback, Invoke());
1345 EXPECT_CALL(get_cookie_info_callback, Invoke("X=1", testing::_)).WillOnce(
1346 QuitCurrentMessageLoop());
1347
1348 CompleteLoadingAndWait();
1349 }
1350
854 TEST_F(CookieMonsterTest, DomainTest) { 1351 TEST_F(CookieMonsterTest, DomainTest) {
855 scoped_refptr<MockPersistentCookieStore> store( 1352 scoped_refptr<MockPersistentCookieStore> store(
856 new MockPersistentCookieStore); 1353 new MockPersistentCookieStore);
857 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 1354 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
858 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 1355 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
859 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1356 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
860 EXPECT_TRUE(cm->SetCookie(url_google_, "C=D; domain=.google.izzle")); 1357 EXPECT_TRUE(SetCookie(cm, url_google_, "C=D; domain=.google.izzle"));
861 EXPECT_EQ("A=B; C=D", GetCookies(cm, url_google_)); 1358 EXPECT_EQ("A=B; C=D", GetCookies(cm, url_google_));
862 1359
863 // Verify that A=B was set as a host cookie rather than a domain 1360 // Verify that A=B was set as a host cookie rather than a domain
864 // cookie -- should not be accessible from a sub sub-domain. 1361 // cookie -- should not be accessible from a sub sub-domain.
865 EXPECT_EQ("C=D", GetCookies(cm, GURL("http://foo.www.google.izzle"))); 1362 EXPECT_EQ("C=D", GetCookies(cm, GURL("http://foo.www.google.izzle")));
866 1363
867 // Test and make sure we find domain cookies on the same domain. 1364 // Test and make sure we find domain cookies on the same domain.
868 EXPECT_TRUE(cm->SetCookie(url_google_, "E=F; domain=.www.google.izzle")); 1365 EXPECT_TRUE(SetCookie(cm, url_google_, "E=F; domain=.www.google.izzle"));
869 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_)); 1366 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_));
870 1367
871 // Test setting a domain= that doesn't start w/ a dot, should 1368 // Test setting a domain= that doesn't start w/ a dot, should
872 // treat it as a domain cookie, as if there was a pre-pended dot. 1369 // treat it as a domain cookie, as if there was a pre-pended dot.
873 EXPECT_TRUE(cm->SetCookie(url_google_, "G=H; domain=www.google.izzle")); 1370 EXPECT_TRUE(SetCookie(cm, url_google_, "G=H; domain=www.google.izzle"));
874 EXPECT_EQ("A=B; C=D; E=F; G=H", GetCookies(cm, url_google_)); 1371 EXPECT_EQ("A=B; C=D; E=F; G=H", GetCookies(cm, url_google_));
875 1372
876 // Test domain enforcement, should fail on a sub-domain or something too deep. 1373 // Test domain enforcement, should fail on a sub-domain or something too deep.
877 EXPECT_FALSE(cm->SetCookie(url_google_, "I=J; domain=.izzle")); 1374 EXPECT_FALSE(SetCookie(cm, url_google_, "I=J; domain=.izzle"));
878 EXPECT_EQ("", GetCookies(cm, GURL("http://a.izzle"))); 1375 EXPECT_EQ("", GetCookies(cm, GURL("http://a.izzle")));
879 EXPECT_FALSE(cm->SetCookie(url_google_, "K=L; domain=.bla.www.google.izzle")); 1376 EXPECT_FALSE(SetCookie(cm, url_google_, "K=L; domain=.bla.www.google.izzle"));
880 EXPECT_EQ("C=D; E=F; G=H", 1377 EXPECT_EQ("C=D; E=F; G=H",
881 GetCookies(cm, GURL("http://bla.www.google.izzle"))); 1378 GetCookies(cm, GURL("http://bla.www.google.izzle")));
882 EXPECT_EQ("A=B; C=D; E=F; G=H", GetCookies(cm, url_google_)); 1379 EXPECT_EQ("A=B; C=D; E=F; G=H", GetCookies(cm, url_google_));
883 1380
884 // Nothing was persisted to the backing store. 1381 // Nothing was persisted to the backing store.
885 EXPECT_EQ(0u, store->commands().size()); 1382 EXPECT_EQ(0u, store->commands().size());
886 } 1383 }
887 1384
888 // FireFox recognizes domains containing trailing periods as valid. 1385 // FireFox recognizes domains containing trailing periods as valid.
889 // IE and Safari do not. Assert the expected policy here. 1386 // IE and Safari do not. Assert the expected policy here.
890 TEST_F(CookieMonsterTest, DomainWithTrailingDotTest) { 1387 TEST_F(CookieMonsterTest, DomainWithTrailingDotTest) {
891 scoped_refptr<MockPersistentCookieStore> store( 1388 scoped_refptr<MockPersistentCookieStore> store(
892 new MockPersistentCookieStore); 1389 new MockPersistentCookieStore);
893 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 1390 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
894 EXPECT_FALSE(cm->SetCookie(url_google_, "a=1; domain=.www.google.com.")); 1391 EXPECT_FALSE(SetCookie(cm, url_google_, "a=1; domain=.www.google.com."));
895 EXPECT_FALSE(cm->SetCookie(url_google_, "b=2; domain=.www.google.com..")); 1392 EXPECT_FALSE(SetCookie(cm, url_google_, "b=2; domain=.www.google.com.."));
896 EXPECT_EQ("", GetCookies(cm, url_google_)); 1393 EXPECT_EQ("", GetCookies(cm, url_google_));
897 1394
898 // Nothing was persisted to the backing store. 1395 // Nothing was persisted to the backing store.
899 EXPECT_EQ(0u, store->commands().size()); 1396 EXPECT_EQ(0u, store->commands().size());
900 } 1397 }
901 1398
902 // Test that cookies can bet set on higher level domains. 1399 // Test that cookies can bet set on higher level domains.
903 // http://b/issue?id=896491 1400 // http://b/issue?id=896491
904 TEST_F(CookieMonsterTest, ValidSubdomainTest) { 1401 TEST_F(CookieMonsterTest, ValidSubdomainTest) {
905 scoped_refptr<MockPersistentCookieStore> store( 1402 scoped_refptr<MockPersistentCookieStore> store(
906 new MockPersistentCookieStore); 1403 new MockPersistentCookieStore);
907 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 1404 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
908 GURL url_abcd("http://a.b.c.d.com"); 1405 GURL url_abcd("http://a.b.c.d.com");
909 GURL url_bcd("http://b.c.d.com"); 1406 GURL url_bcd("http://b.c.d.com");
910 GURL url_cd("http://c.d.com"); 1407 GURL url_cd("http://c.d.com");
911 GURL url_d("http://d.com"); 1408 GURL url_d("http://d.com");
912 1409
913 EXPECT_TRUE(cm->SetCookie(url_abcd, "a=1; domain=.a.b.c.d.com")); 1410 EXPECT_TRUE(SetCookie(cm, url_abcd, "a=1; domain=.a.b.c.d.com"));
914 EXPECT_TRUE(cm->SetCookie(url_abcd, "b=2; domain=.b.c.d.com")); 1411 EXPECT_TRUE(SetCookie(cm, url_abcd, "b=2; domain=.b.c.d.com"));
915 EXPECT_TRUE(cm->SetCookie(url_abcd, "c=3; domain=.c.d.com")); 1412 EXPECT_TRUE(SetCookie(cm, url_abcd, "c=3; domain=.c.d.com"));
916 EXPECT_TRUE(cm->SetCookie(url_abcd, "d=4; domain=.d.com")); 1413 EXPECT_TRUE(SetCookie(cm, url_abcd, "d=4; domain=.d.com"));
917 1414
918 EXPECT_EQ("a=1; b=2; c=3; d=4", GetCookies(cm, url_abcd)); 1415 EXPECT_EQ("a=1; b=2; c=3; d=4", GetCookies(cm, url_abcd));
919 EXPECT_EQ("b=2; c=3; d=4", GetCookies(cm, url_bcd)); 1416 EXPECT_EQ("b=2; c=3; d=4", GetCookies(cm, url_bcd));
920 EXPECT_EQ("c=3; d=4", GetCookies(cm, url_cd)); 1417 EXPECT_EQ("c=3; d=4", GetCookies(cm, url_cd));
921 EXPECT_EQ("d=4", GetCookies(cm, url_d)); 1418 EXPECT_EQ("d=4", GetCookies(cm, url_d));
922 1419
923 // Check that the same cookie can exist on different sub-domains. 1420 // Check that the same cookie can exist on different sub-domains.
924 EXPECT_TRUE(cm->SetCookie(url_bcd, "X=bcd; domain=.b.c.d.com")); 1421 EXPECT_TRUE(SetCookie(cm, url_bcd, "X=bcd; domain=.b.c.d.com"));
925 EXPECT_TRUE(cm->SetCookie(url_bcd, "X=cd; domain=.c.d.com")); 1422 EXPECT_TRUE(SetCookie(cm, url_bcd, "X=cd; domain=.c.d.com"));
926 EXPECT_EQ("b=2; c=3; d=4; X=bcd; X=cd", GetCookies(cm, url_bcd)); 1423 EXPECT_EQ("b=2; c=3; d=4; X=bcd; X=cd", GetCookies(cm, url_bcd));
927 EXPECT_EQ("c=3; d=4; X=cd", GetCookies(cm, url_cd)); 1424 EXPECT_EQ("c=3; d=4; X=cd", GetCookies(cm, url_cd));
928 1425
929 // Nothing was persisted to the backing store. 1426 // Nothing was persisted to the backing store.
930 EXPECT_EQ(0u, store->commands().size()); 1427 EXPECT_EQ(0u, store->commands().size());
931 } 1428 }
932 1429
933 // Test that setting a cookie which specifies an invalid domain has 1430 // Test that setting a cookie which specifies an invalid domain has
934 // no side-effect. An invalid domain in this context is one which does 1431 // no side-effect. An invalid domain in this context is one which does
935 // not match the originating domain. 1432 // not match the originating domain.
936 // http://b/issue?id=896472 1433 // http://b/issue?id=896472
937 TEST_F(CookieMonsterTest, InvalidDomainTest) { 1434 TEST_F(CookieMonsterTest, InvalidDomainTest) {
938 { 1435 {
939 scoped_refptr<MockPersistentCookieStore> store( 1436 scoped_refptr<MockPersistentCookieStore> store(
940 new MockPersistentCookieStore); 1437 new MockPersistentCookieStore);
941 1438
942 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 1439 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
943 GURL url_foobar("http://foo.bar.com"); 1440 GURL url_foobar("http://foo.bar.com");
944 1441
945 // More specific sub-domain than allowed. 1442 // More specific sub-domain than allowed.
946 EXPECT_FALSE(cm->SetCookie(url_foobar, "a=1; domain=.yo.foo.bar.com")); 1443 EXPECT_FALSE(SetCookie(cm, url_foobar, "a=1; domain=.yo.foo.bar.com"));
947 1444
948 EXPECT_FALSE(cm->SetCookie(url_foobar, "b=2; domain=.foo.com")); 1445 EXPECT_FALSE(SetCookie(cm, url_foobar, "b=2; domain=.foo.com"));
949 EXPECT_FALSE(cm->SetCookie(url_foobar, "c=3; domain=.bar.foo.com")); 1446 EXPECT_FALSE(SetCookie(cm, url_foobar, "c=3; domain=.bar.foo.com"));
950 1447
951 // Different TLD, but the rest is a substring. 1448 // Different TLD, but the rest is a substring.
952 EXPECT_FALSE(cm->SetCookie(url_foobar, "d=4; domain=.foo.bar.com.net")); 1449 EXPECT_FALSE(SetCookie(cm, url_foobar, "d=4; domain=.foo.bar.com.net"));
953 1450
954 // A substring that isn't really a parent domain. 1451 // A substring that isn't really a parent domain.
955 EXPECT_FALSE(cm->SetCookie(url_foobar, "e=5; domain=ar.com")); 1452 EXPECT_FALSE(SetCookie(cm, url_foobar, "e=5; domain=ar.com"));
956 1453
957 // Completely invalid domains: 1454 // Completely invalid domains:
958 EXPECT_FALSE(cm->SetCookie(url_foobar, "f=6; domain=.")); 1455 EXPECT_FALSE(SetCookie(cm, url_foobar, "f=6; domain=."));
959 EXPECT_FALSE(cm->SetCookie(url_foobar, "g=7; domain=/")); 1456 EXPECT_FALSE(SetCookie(cm, url_foobar, "g=7; domain=/"));
960 EXPECT_FALSE(cm->SetCookie(url_foobar, "h=8; domain=http://foo.bar.com")); 1457 EXPECT_FALSE(SetCookie(cm, url_foobar, "h=8; domain=http://foo.bar.com"));
961 EXPECT_FALSE(cm->SetCookie(url_foobar, "i=9; domain=..foo.bar.com")); 1458 EXPECT_FALSE(SetCookie(cm, url_foobar, "i=9; domain=..foo.bar.com"));
962 EXPECT_FALSE(cm->SetCookie(url_foobar, "j=10; domain=..bar.com")); 1459 EXPECT_FALSE(SetCookie(cm, url_foobar, "j=10; domain=..bar.com"));
963 1460
964 // Make sure there isn't something quirky in the domain canonicalization 1461 // Make sure there isn't something quirky in the domain canonicalization
965 // that supports full URL semantics. 1462 // that supports full URL semantics.
966 EXPECT_FALSE(cm->SetCookie(url_foobar, "k=11; domain=.foo.bar.com?blah")); 1463 EXPECT_FALSE(SetCookie(cm, url_foobar, "k=11; domain=.foo.bar.com?blah"));
967 EXPECT_FALSE(cm->SetCookie(url_foobar, "l=12; domain=.foo.bar.com/blah")); 1464 EXPECT_FALSE(SetCookie(cm, url_foobar, "l=12; domain=.foo.bar.com/blah"));
968 EXPECT_FALSE(cm->SetCookie(url_foobar, "m=13; domain=.foo.bar.com:80")); 1465 EXPECT_FALSE(SetCookie(cm, url_foobar, "m=13; domain=.foo.bar.com:80"));
969 EXPECT_FALSE(cm->SetCookie(url_foobar, "n=14; domain=.foo.bar.com:")); 1466 EXPECT_FALSE(SetCookie(cm, url_foobar, "n=14; domain=.foo.bar.com:"));
970 EXPECT_FALSE(cm->SetCookie(url_foobar, "o=15; domain=.foo.bar.com#sup")); 1467 EXPECT_FALSE(SetCookie(cm, url_foobar, "o=15; domain=.foo.bar.com#sup"));
971 1468
972 EXPECT_EQ("", GetCookies(cm, url_foobar)); 1469 EXPECT_EQ("", GetCookies(cm, url_foobar));
973 1470
974 // Nothing was persisted to the backing store. 1471 // Nothing was persisted to the backing store.
975 EXPECT_EQ(0u, store->commands().size()); 1472 EXPECT_EQ(0u, store->commands().size());
976 } 1473 }
977 1474
978 { 1475 {
979 // Make sure the cookie code hasn't gotten its subdomain string handling 1476 // Make sure the cookie code hasn't gotten its subdomain string handling
980 // reversed, missed a suffix check, etc. It's important here that the two 1477 // reversed, missed a suffix check, etc. It's important here that the two
981 // hosts below have the same domain + registry. 1478 // hosts below have the same domain + registry.
982 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1479 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
983 GURL url_foocom("http://foo.com.com"); 1480 GURL url_foocom("http://foo.com.com");
984 EXPECT_FALSE(cm->SetCookie(url_foocom, "a=1; domain=.foo.com.com.com")); 1481 EXPECT_FALSE(SetCookie(cm, url_foocom, "a=1; domain=.foo.com.com.com"));
985 EXPECT_EQ("", GetCookies(cm, url_foocom)); 1482 EXPECT_EQ("", GetCookies(cm, url_foocom));
986 } 1483 }
987 } 1484 }
988 1485
989 // Test the behavior of omitting dot prefix from domain, should 1486 // Test the behavior of omitting dot prefix from domain, should
990 // function the same as FireFox. 1487 // function the same as FireFox.
991 // http://b/issue?id=889898 1488 // http://b/issue?id=889898
992 TEST_F(CookieMonsterTest, DomainWithoutLeadingDotTest) { 1489 TEST_F(CookieMonsterTest, DomainWithoutLeadingDotTest) {
993 { // The omission of dot results in setting a domain cookie. 1490 { // The omission of dot results in setting a domain cookie.
994 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1491 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
995 GURL url_hosted("http://manage.hosted.filefront.com"); 1492 GURL url_hosted("http://manage.hosted.filefront.com");
996 GURL url_filefront("http://www.filefront.com"); 1493 GURL url_filefront("http://www.filefront.com");
997 EXPECT_TRUE(cm->SetCookie(url_hosted, "sawAd=1; domain=filefront.com")); 1494 EXPECT_TRUE(SetCookie(cm, url_hosted, "sawAd=1; domain=filefront.com"));
998 EXPECT_EQ("sawAd=1", GetCookies(cm, url_hosted)); 1495 EXPECT_EQ("sawAd=1", GetCookies(cm, url_hosted));
999 EXPECT_EQ("sawAd=1", GetCookies(cm, url_filefront)); 1496 EXPECT_EQ("sawAd=1", GetCookies(cm, url_filefront));
1000 } 1497 }
1001 1498
1002 { // Even when the domains match exactly, don't consider it host cookie. 1499 { // Even when the domains match exactly, don't consider it host cookie.
1003 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1500 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1004 GURL url("http://www.google.com"); 1501 GURL url("http://www.google.com");
1005 EXPECT_TRUE(cm->SetCookie(url, "a=1; domain=www.google.com")); 1502 EXPECT_TRUE(SetCookie(cm, url, "a=1; domain=www.google.com"));
1006 EXPECT_EQ("a=1", GetCookies(cm, url)); 1503 EXPECT_EQ("a=1", GetCookies(cm, url));
1007 EXPECT_EQ("a=1", GetCookies(cm, GURL("http://sub.www.google.com"))); 1504 EXPECT_EQ("a=1", GetCookies(cm, GURL("http://sub.www.google.com")));
1008 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.com"))); 1505 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.com")));
1009 } 1506 }
1010 } 1507 }
1011 1508
1012 // Test that the domain specified in cookie string is treated case-insensitive 1509 // Test that the domain specified in cookie string is treated case-insensitive
1013 // http://b/issue?id=896475. 1510 // http://b/issue?id=896475.
1014 TEST_F(CookieMonsterTest, CaseInsensitiveDomainTest) { 1511 TEST_F(CookieMonsterTest, CaseInsensitiveDomainTest) {
1015 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1512 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1016 GURL url("http://www.google.com"); 1513 GURL url("http://www.google.com");
1017 EXPECT_TRUE(cm->SetCookie(url, "a=1; domain=.GOOGLE.COM")); 1514 EXPECT_TRUE(SetCookie(cm, url, "a=1; domain=.GOOGLE.COM"));
1018 EXPECT_TRUE(cm->SetCookie(url, "b=2; domain=.wWw.gOOgLE.coM")); 1515 EXPECT_TRUE(SetCookie(cm, url, "b=2; domain=.wWw.gOOgLE.coM"));
1019 EXPECT_EQ("a=1; b=2", GetCookies(cm, url)); 1516 EXPECT_EQ("a=1; b=2", GetCookies(cm, url));
1020 } 1517 }
1021 1518
1022 TEST_F(CookieMonsterTest, TestIpAddress) { 1519 TEST_F(CookieMonsterTest, TestIpAddress) {
1023 GURL url_ip("http://1.2.3.4/weee"); 1520 GURL url_ip("http://1.2.3.4/weee");
1024 { 1521 {
1025 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1522 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1026 EXPECT_TRUE(cm->SetCookie(url_ip, kValidCookieLine)); 1523 EXPECT_TRUE(SetCookie(cm, url_ip, kValidCookieLine));
1027 EXPECT_EQ("A=B", GetCookies(cm, url_ip)); 1524 EXPECT_EQ("A=B", GetCookies(cm, url_ip));
1028 } 1525 }
1029 1526
1030 { // IP addresses should not be able to set domain cookies. 1527 { // IP addresses should not be able to set domain cookies.
1031 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1528 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1032 EXPECT_FALSE(cm->SetCookie(url_ip, "b=2; domain=.1.2.3.4")); 1529 EXPECT_FALSE(SetCookie(cm, url_ip, "b=2; domain=.1.2.3.4"));
1033 EXPECT_FALSE(cm->SetCookie(url_ip, "c=3; domain=.3.4")); 1530 EXPECT_FALSE(SetCookie(cm, url_ip, "c=3; domain=.3.4"));
1034 EXPECT_EQ("", GetCookies(cm, url_ip)); 1531 EXPECT_EQ("", GetCookies(cm, url_ip));
1035 // It should be allowed to set a cookie if domain= matches the IP address 1532 // It should be allowed to set a cookie if domain= matches the IP address
1036 // exactly. This matches IE/Firefox, even though it seems a bit wrong. 1533 // exactly. This matches IE/Firefox, even though it seems a bit wrong.
1037 EXPECT_FALSE(cm->SetCookie(url_ip, "b=2; domain=1.2.3.3")); 1534 EXPECT_FALSE(SetCookie(cm, url_ip, "b=2; domain=1.2.3.3"));
1038 EXPECT_EQ("", GetCookies(cm, url_ip)); 1535 EXPECT_EQ("", GetCookies(cm, url_ip));
1039 EXPECT_TRUE(cm->SetCookie(url_ip, "b=2; domain=1.2.3.4")); 1536 EXPECT_TRUE(SetCookie(cm, url_ip, "b=2; domain=1.2.3.4"));
1040 EXPECT_EQ("b=2", GetCookies(cm, url_ip)); 1537 EXPECT_EQ("b=2", GetCookies(cm, url_ip));
1041 } 1538 }
1042 } 1539 }
1043 1540
1044 // Test host cookies, and setting of cookies on TLD. 1541 // Test host cookies, and setting of cookies on TLD.
1045 TEST_F(CookieMonsterTest, TestNonDottedAndTLD) { 1542 TEST_F(CookieMonsterTest, TestNonDottedAndTLD) {
1046 { 1543 {
1047 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1544 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1048 GURL url("http://com/"); 1545 GURL url("http://com/");
1049 // Allow setting on "com", (but only as a host cookie). 1546 // Allow setting on "com", (but only as a host cookie).
1050 EXPECT_TRUE(cm->SetCookie(url, "a=1")); 1547 EXPECT_TRUE(SetCookie(cm, url, "a=1"));
1051 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=.com")); 1548 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=.com"));
1052 EXPECT_FALSE(cm->SetCookie(url, "c=3; domain=com")); 1549 EXPECT_FALSE(SetCookie(cm, url, "c=3; domain=com"));
1053 EXPECT_EQ("a=1", GetCookies(cm, url)); 1550 EXPECT_EQ("a=1", GetCookies(cm, url));
1054 // Make sure it doesn't show up for a normal .com, it should be a host 1551 // Make sure it doesn't show up for a normal .com, it should be a host
1055 // not a domain cookie. 1552 // not a domain cookie.
1056 EXPECT_EQ("", GetCookies(cm, GURL("http://hopefully-no-cookies.com/"))); 1553 EXPECT_EQ("", GetCookies(cm, GURL("http://hopefully-no-cookies.com/")));
1057 EXPECT_EQ("", GetCookies(cm, GURL("http://.com/"))); 1554 EXPECT_EQ("", GetCookies(cm, GURL("http://.com/")));
1058 } 1555 }
1059 1556
1060 { // http://com. should be treated the same as http://com. 1557 { // http://com. should be treated the same as http://com.
1061 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1558 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1062 GURL url("http://com./index.html"); 1559 GURL url("http://com./index.html");
1063 EXPECT_TRUE(cm->SetCookie(url, "a=1")); 1560 EXPECT_TRUE(SetCookie(cm, url, "a=1"));
1064 EXPECT_EQ("a=1", GetCookies(cm, url)); 1561 EXPECT_EQ("a=1", GetCookies(cm, url));
1065 EXPECT_EQ("", GetCookies(cm, GURL("http://hopefully-no-cookies.com./"))); 1562 EXPECT_EQ("", GetCookies(cm, GURL("http://hopefully-no-cookies.com./")));
1066 } 1563 }
1067 1564
1068 { // Should not be able to set host cookie from a subdomain. 1565 { // Should not be able to set host cookie from a subdomain.
1069 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1566 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1070 GURL url("http://a.b"); 1567 GURL url("http://a.b");
1071 EXPECT_FALSE(cm->SetCookie(url, "a=1; domain=.b")); 1568 EXPECT_FALSE(SetCookie(cm, url, "a=1; domain=.b"));
1072 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=b")); 1569 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=b"));
1073 EXPECT_EQ("", GetCookies(cm, url)); 1570 EXPECT_EQ("", GetCookies(cm, url));
1074 } 1571 }
1075 1572
1076 { // Same test as above, but explicitly on a known TLD (com). 1573 { // Same test as above, but explicitly on a known TLD (com).
1077 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1574 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1078 GURL url("http://google.com"); 1575 GURL url("http://google.com");
1079 EXPECT_FALSE(cm->SetCookie(url, "a=1; domain=.com")); 1576 EXPECT_FALSE(SetCookie(cm, url, "a=1; domain=.com"));
1080 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=com")); 1577 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=com"));
1081 EXPECT_EQ("", GetCookies(cm, url)); 1578 EXPECT_EQ("", GetCookies(cm, url));
1082 } 1579 }
1083 1580
1084 { // Make sure can't set cookie on TLD which is dotted. 1581 { // Make sure can't set cookie on TLD which is dotted.
1085 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1582 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1086 GURL url("http://google.co.uk"); 1583 GURL url("http://google.co.uk");
1087 EXPECT_FALSE(cm->SetCookie(url, "a=1; domain=.co.uk")); 1584 EXPECT_FALSE(SetCookie(cm, url, "a=1; domain=.co.uk"));
1088 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=.uk")); 1585 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=.uk"));
1089 EXPECT_EQ("", GetCookies(cm, url)); 1586 EXPECT_EQ("", GetCookies(cm, url));
1090 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.co.uk"))); 1587 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.co.uk")));
1091 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.uk"))); 1588 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.uk")));
1092 } 1589 }
1093 1590
1094 { // Intranet URLs should only be able to set host cookies. 1591 { // Intranet URLs should only be able to set host cookies.
1095 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1592 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1096 GURL url("http://b"); 1593 GURL url("http://b");
1097 EXPECT_TRUE(cm->SetCookie(url, "a=1")); 1594 EXPECT_TRUE(SetCookie(cm, url, "a=1"));
1098 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=.b")); 1595 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=.b"));
1099 EXPECT_FALSE(cm->SetCookie(url, "c=3; domain=b")); 1596 EXPECT_FALSE(SetCookie(cm, url, "c=3; domain=b"));
1100 EXPECT_EQ("a=1", GetCookies(cm, url)); 1597 EXPECT_EQ("a=1", GetCookies(cm, url));
1101 } 1598 }
1102 } 1599 }
1103 1600
1104 // Test reading/writing cookies when the domain ends with a period, 1601 // Test reading/writing cookies when the domain ends with a period,
1105 // as in "www.google.com." 1602 // as in "www.google.com."
1106 TEST_F(CookieMonsterTest, TestHostEndsWithDot) { 1603 TEST_F(CookieMonsterTest, TestHostEndsWithDot) {
1107 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1604 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1108 GURL url("http://www.google.com"); 1605 GURL url("http://www.google.com");
1109 GURL url_with_dot("http://www.google.com."); 1606 GURL url_with_dot("http://www.google.com.");
1110 EXPECT_TRUE(cm->SetCookie(url, "a=1")); 1607 EXPECT_TRUE(SetCookie(cm, url, "a=1"));
1111 EXPECT_EQ("a=1", GetCookies(cm, url)); 1608 EXPECT_EQ("a=1", GetCookies(cm, url));
1112 1609
1113 // Do not share cookie space with the dot version of domain. 1610 // Do not share cookie space with the dot version of domain.
1114 // Note: this is not what FireFox does, but it _is_ what IE+Safari do. 1611 // Note: this is not what FireFox does, but it _is_ what IE+Safari do.
1115 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=.www.google.com.")); 1612 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=.www.google.com."));
1116 EXPECT_EQ("a=1", GetCookies(cm, url)); 1613 EXPECT_EQ("a=1", GetCookies(cm, url));
1117 1614
1118 EXPECT_TRUE(cm->SetCookie(url_with_dot, "b=2; domain=.google.com.")); 1615 EXPECT_TRUE(SetCookie(cm, url_with_dot, "b=2; domain=.google.com."));
1119 EXPECT_EQ("b=2", GetCookies(cm, url_with_dot)); 1616 EXPECT_EQ("b=2", GetCookies(cm, url_with_dot));
1120 1617
1121 // Make sure there weren't any side effects. 1618 // Make sure there weren't any side effects.
1122 EXPECT_EQ(GetCookies(cm, GURL("http://hopefully-no-cookies.com/")), ""); 1619 EXPECT_EQ(GetCookies(cm, GURL("http://hopefully-no-cookies.com/")), "");
1123 EXPECT_EQ("", GetCookies(cm, GURL("http://.com/"))); 1620 EXPECT_EQ("", GetCookies(cm, GURL("http://.com/")));
1124 } 1621 }
1125 1622
1126 TEST_F(CookieMonsterTest, InvalidScheme) { 1623 TEST_F(CookieMonsterTest, InvalidScheme) {
1127 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1624 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1128 EXPECT_FALSE(cm->SetCookie(GURL(kUrlFtp), kValidCookieLine)); 1625 EXPECT_FALSE(SetCookie(cm, GURL(kUrlFtp), kValidCookieLine));
1129 } 1626 }
1130 1627
1131 TEST_F(CookieMonsterTest, InvalidScheme_Read) { 1628 TEST_F(CookieMonsterTest, InvalidScheme_Read) {
1132 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1629 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1133 EXPECT_TRUE(cm->SetCookie(GURL(kUrlGoogle), kValidDomainCookieLine)); 1630 EXPECT_TRUE(SetCookie(cm, GURL(kUrlGoogle), kValidDomainCookieLine));
1134 EXPECT_EQ("", GetCookies(cm, GURL(kUrlFtp))); 1631 EXPECT_EQ("", GetCookies(cm, GURL(kUrlFtp)));
1135 } 1632 }
1136 1633
1137 TEST_F(CookieMonsterTest, PathTest) { 1634 TEST_F(CookieMonsterTest, PathTest) {
1138 std::string url("http://www.google.izzle"); 1635 std::string url("http://www.google.izzle");
1139 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1636 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1140 EXPECT_TRUE(cm->SetCookie(GURL(url), "A=B; path=/wee")); 1637 EXPECT_TRUE(SetCookie(cm, GURL(url), "A=B; path=/wee"));
1141 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee"))); 1638 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee")));
1142 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/"))); 1639 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/")));
1143 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/war"))); 1640 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/war")));
1144 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/war/more/more"))); 1641 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/war/more/more")));
1145 EXPECT_EQ("", GetCookies(cm, GURL(url + "/weehee"))); 1642 EXPECT_EQ("", GetCookies(cm, GURL(url + "/weehee")));
1146 EXPECT_EQ("", GetCookies(cm, GURL(url + "/"))); 1643 EXPECT_EQ("", GetCookies(cm, GURL(url + "/")));
1147 1644
1148 // If we add a 0 length path, it should default to / 1645 // If we add a 0 length path, it should default to /
1149 EXPECT_TRUE(cm->SetCookie(GURL(url), "A=C; path=")); 1646 EXPECT_TRUE(SetCookie(cm, GURL(url), "A=C; path="));
1150 EXPECT_EQ("A=B; A=C", GetCookies(cm, GURL(url + "/wee"))); 1647 EXPECT_EQ("A=B; A=C", GetCookies(cm, GURL(url + "/wee")));
1151 EXPECT_EQ("A=C", GetCookies(cm, GURL(url + "/"))); 1648 EXPECT_EQ("A=C", GetCookies(cm, GURL(url + "/")));
1152 } 1649 }
1153 1650
1154 TEST_F(CookieMonsterTest, HttpOnlyTest) { 1651 TEST_F(CookieMonsterTest, HttpOnlyTest) {
1155 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1652 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1156 CookieOptions options; 1653 CookieOptions options;
1157 options.set_include_httponly(); 1654 options.set_include_httponly();
1158 1655
1159 // Create a httponly cookie. 1656 // Create a httponly cookie.
1160 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B; httponly", options)); 1657 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B; httponly", options));
1161 1658
1162 // Check httponly read protection. 1659 // Check httponly read protection.
1163 EXPECT_EQ("", GetCookies(cm, url_google_)); 1660 EXPECT_EQ("", GetCookies(cm, url_google_));
1164 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options)); 1661 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options));
1165 1662
1166 // Check httponly overwrite protection. 1663 // Check httponly overwrite protection.
1167 EXPECT_FALSE(cm->SetCookie(url_google_, "A=C")); 1664 EXPECT_FALSE(SetCookie(cm, url_google_, "A=C"));
1168 EXPECT_EQ("", GetCookies(cm, url_google_)); 1665 EXPECT_EQ("", GetCookies(cm, url_google_));
1169 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options)); 1666 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options));
1170 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=C", options)); 1667 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=C", options));
1171 EXPECT_EQ("A=C", GetCookies(cm, url_google_)); 1668 EXPECT_EQ("A=C", GetCookies(cm, url_google_));
1172 1669
1173 // Check httponly create protection. 1670 // Check httponly create protection.
1174 EXPECT_FALSE(cm->SetCookie(url_google_, "B=A; httponly")); 1671 EXPECT_FALSE(SetCookie(cm, url_google_, "B=A; httponly"));
1175 EXPECT_EQ("A=C", GetCookiesWithOptions(cm, url_google_, options)); 1672 EXPECT_EQ("A=C", GetCookiesWithOptions(cm, url_google_, options));
1176 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "B=A; httponly", options)); 1673 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "B=A; httponly", options));
1177 EXPECT_EQ("A=C; B=A", GetCookiesWithOptions(cm, url_google_, options)); 1674 EXPECT_EQ("A=C; B=A", GetCookiesWithOptions(cm, url_google_, options));
1178 EXPECT_EQ("A=C", GetCookies(cm, url_google_)); 1675 EXPECT_EQ("A=C", GetCookies(cm, url_google_));
1179 } 1676 }
1180 1677
1181 TEST_F(CookieMonsterTest, GetCookiesWithInfo) { 1678 TEST_F(CookieMonsterTest, GetCookiesWithInfo) {
1182 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1679 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1183 CookieOptions options; 1680 CookieOptions options;
1184 1681
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
1317 CookieMonster::DomainIsHostOnly(tests[i].str)); 1814 CookieMonster::DomainIsHostOnly(tests[i].str));
1318 } 1815 }
1319 } 1816 }
1320 1817
1321 TEST_F(CookieMonsterTest, TestCookieDeletion) { 1818 TEST_F(CookieMonsterTest, TestCookieDeletion) {
1322 scoped_refptr<MockPersistentCookieStore> store( 1819 scoped_refptr<MockPersistentCookieStore> store(
1323 new MockPersistentCookieStore); 1820 new MockPersistentCookieStore);
1324 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 1821 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
1325 1822
1326 // Create a session cookie. 1823 // Create a session cookie.
1327 EXPECT_TRUE(cm->SetCookie(url_google_, kValidCookieLine)); 1824 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine));
1328 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1825 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1329 // Delete it via Max-Age. 1826 // Delete it via Max-Age.
1330 EXPECT_TRUE(cm->SetCookie(url_google_, 1827 EXPECT_TRUE(SetCookie(cm, url_google_,
1331 std::string(kValidCookieLine) + "; max-age=0")); 1828 std::string(kValidCookieLine) + "; max-age=0"));
1332 EXPECT_EQ("", GetCookies(cm, url_google_)); 1829 EXPECT_EQ("", GetCookies(cm, url_google_));
1333 1830
1334 // Create a session cookie. 1831 // Create a session cookie.
1335 EXPECT_TRUE(cm->SetCookie(url_google_, kValidCookieLine)); 1832 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine));
1336 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1833 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1337 // Delete it via Expires. 1834 // Delete it via Expires.
1338 EXPECT_TRUE(cm->SetCookie(url_google_, 1835 EXPECT_TRUE(SetCookie(cm, url_google_,
1339 std::string(kValidCookieLine) + 1836 std::string(kValidCookieLine) +
1340 "; expires=Mon, 18-Apr-1977 22:50:13 GMT")); 1837 "; expires=Mon, 18-Apr-1977 22:50:13 GMT"));
1341 EXPECT_EQ("", GetCookies(cm, url_google_)); 1838 EXPECT_EQ("", GetCookies(cm, url_google_));
1342 1839
1343 // Create a persistent cookie. 1840 // Create a persistent cookie.
1344 EXPECT_TRUE(cm->SetCookie(url_google_, 1841 EXPECT_TRUE(SetCookie(cm, url_google_,
1345 std::string(kValidCookieLine) + 1842 std::string(kValidCookieLine) +
1346 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); 1843 "; expires=Mon, 18-Apr-22 22:50:13 GMT"));
1347 ASSERT_EQ(1u, store->commands().size()); 1844 ASSERT_EQ(1u, store->commands().size());
1348 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); 1845 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
1349 1846
1350 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1847 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1351 // Delete it via Max-Age. 1848 // Delete it via Max-Age.
1352 EXPECT_TRUE(cm->SetCookie(url_google_, 1849 EXPECT_TRUE(SetCookie(cm, url_google_,
1353 std::string(kValidCookieLine) + "; max-age=0")); 1850 std::string(kValidCookieLine) + "; max-age=0"));
1354 ASSERT_EQ(2u, store->commands().size()); 1851 ASSERT_EQ(2u, store->commands().size());
1355 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); 1852 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
1356 EXPECT_EQ("", GetCookies(cm, url_google_)); 1853 EXPECT_EQ("", GetCookies(cm, url_google_));
1357 1854
1358 // Create a persistent cookie. 1855 // Create a persistent cookie.
1359 EXPECT_TRUE(cm->SetCookie(url_google_, 1856 EXPECT_TRUE(SetCookie(cm, url_google_,
1360 std::string(kValidCookieLine) + 1857 std::string(kValidCookieLine) +
1361 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); 1858 "; expires=Mon, 18-Apr-22 22:50:13 GMT"));
1362 ASSERT_EQ(3u, store->commands().size()); 1859 ASSERT_EQ(3u, store->commands().size());
1363 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type); 1860 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type);
1364 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1861 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1365 // Delete it via Expires. 1862 // Delete it via Expires.
1366 EXPECT_TRUE(cm->SetCookie(url_google_, 1863 EXPECT_TRUE(SetCookie(cm, url_google_,
1367 std::string(kValidCookieLine) + 1864 std::string(kValidCookieLine) +
1368 "; expires=Mon, 18-Apr-1977 22:50:13 GMT")); 1865 "; expires=Mon, 18-Apr-1977 22:50:13 GMT"));
1369 ASSERT_EQ(4u, store->commands().size()); 1866 ASSERT_EQ(4u, store->commands().size());
1370 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type); 1867 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type);
1371 EXPECT_EQ("", GetCookies(cm, url_google_)); 1868 EXPECT_EQ("", GetCookies(cm, url_google_));
1372 1869
1373 // Create a persistent cookie. 1870 // Create a persistent cookie.
1374 EXPECT_TRUE(cm->SetCookie(url_google_, 1871 EXPECT_TRUE(SetCookie(cm, url_google_,
1375 std::string(kValidCookieLine) + 1872 std::string(kValidCookieLine) +
1376 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); 1873 "; expires=Mon, 18-Apr-22 22:50:13 GMT"));
1377 ASSERT_EQ(5u, store->commands().size()); 1874 ASSERT_EQ(5u, store->commands().size());
1378 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); 1875 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type);
1379 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1876 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1380 // Delete it via Expires, with a unix epoch of 0. 1877 // Delete it via Expires, with a unix epoch of 0.
1381 EXPECT_TRUE(cm->SetCookie(url_google_, 1878 EXPECT_TRUE(SetCookie(cm, url_google_,
1382 std::string(kValidCookieLine) + 1879 std::string(kValidCookieLine) +
1383 "; expires=Thu, 1-Jan-1970 00:00:00 GMT")); 1880 "; expires=Thu, 1-Jan-1970 00:00:00 GMT"));
1384 ASSERT_EQ(6u, store->commands().size()); 1881 ASSERT_EQ(6u, store->commands().size());
1385 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[5].type); 1882 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[5].type);
1386 EXPECT_EQ("", GetCookies(cm, url_google_)); 1883 EXPECT_EQ("", GetCookies(cm, url_google_));
1387 } 1884 }
1388 1885
1389 TEST_F(CookieMonsterTest, TestCookieDeleteAll) { 1886 TEST_F(CookieMonsterTest, TestCookieDeleteAll) {
1390 scoped_refptr<MockPersistentCookieStore> store( 1887 scoped_refptr<MockPersistentCookieStore> store(
1391 new MockPersistentCookieStore); 1888 new MockPersistentCookieStore);
1392 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 1889 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
1393 CookieOptions options; 1890 CookieOptions options;
1394 options.set_include_httponly(); 1891 options.set_include_httponly();
1395 1892
1396 EXPECT_TRUE(cm->SetCookie(url_google_, kValidCookieLine)); 1893 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine));
1397 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1894 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1398 1895
1399 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "C=D; httponly", options)); 1896 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "C=D; httponly", options));
1400 EXPECT_EQ("A=B; C=D", GetCookiesWithOptions(cm, url_google_, options)); 1897 EXPECT_EQ("A=B; C=D", GetCookiesWithOptions(cm, url_google_, options));
1401 1898
1402 EXPECT_EQ(2, cm->DeleteAll(false)); 1899 EXPECT_EQ(2, DeleteAll(cm));
1403 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); 1900 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options));
1404 1901
1405 EXPECT_EQ(0u, store->commands().size()); 1902 EXPECT_EQ(0u, store->commands().size());
1406 1903
1407 // Create a persistent cookie. 1904 // Create a persistent cookie.
1408 EXPECT_TRUE(cm->SetCookie(url_google_, 1905 EXPECT_TRUE(SetCookie(cm, url_google_,
1409 std::string(kValidCookieLine) + 1906 std::string(kValidCookieLine) +
1410 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); 1907 "; expires=Mon, 18-Apr-22 22:50:13 GMT"));
1411 ASSERT_EQ(1u, store->commands().size()); 1908 ASSERT_EQ(1u, store->commands().size());
1412 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); 1909 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
1413 1910
1414 EXPECT_EQ(1, cm->DeleteAll(true)); // sync_to_store = true. 1911 EXPECT_EQ(1, DeleteAll(cm)); // sync_to_store = true.
1415 ASSERT_EQ(2u, store->commands().size()); 1912 ASSERT_EQ(2u, store->commands().size());
1416 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); 1913 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
1417 1914
1418 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); 1915 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options));
1419 } 1916 }
1420 1917
1421 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) { 1918 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) {
1422 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1919 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1423 Time now = Time::Now(); 1920 Time now = Time::Now();
1424 1921
1425 // Nothing has been added so nothing should be deleted. 1922 // Nothing has been added so nothing should be deleted.
1426 EXPECT_EQ(0, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99), 1923 EXPECT_EQ(0, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99),
1427 Time(), false)); 1924 Time()));
1428 1925
1429 // Create 3 cookies with creation date of today, yesterday and the day before. 1926 // Create 3 cookies with creation date of today, yesterday and the day before.
1430 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-0=Now", now)); 1927 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-0=Now", now));
1431 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-1=Yesterday", 1928 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-1=Yesterday",
1432 now - TimeDelta::FromDays(1))); 1929 now - TimeDelta::FromDays(1)));
1433 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-2=DayBefore", 1930 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-2=DayBefore",
1434 now - TimeDelta::FromDays(2))); 1931 now - TimeDelta::FromDays(2)));
1435 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-3=ThreeDays", 1932 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-3=ThreeDays",
1436 now - TimeDelta::FromDays(3))); 1933 now - TimeDelta::FromDays(3)));
1437 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-7=LastWeek", 1934 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-7=LastWeek",
1438 now - TimeDelta::FromDays(7))); 1935 now - TimeDelta::FromDays(7)));
1439 1936
1440 // Try to delete threedays and the daybefore. 1937 // Try to delete threedays and the daybefore.
1441 EXPECT_EQ(2, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(3), 1938 EXPECT_EQ(2, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(3),
1442 now - TimeDelta::FromDays(1), 1939 now - TimeDelta::FromDays(1)));
1443 false));
1444 1940
1445 // Try to delete yesterday, also make sure that delete_end is not 1941 // Try to delete yesterday, also make sure that delete_end is not
1446 // inclusive. 1942 // inclusive.
1447 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(2), 1943 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(2),
1448 now, 1944 now));
1449 false));
1450 1945
1451 // Make sure the delete_begin is inclusive. 1946 // Make sure the delete_begin is inclusive.
1452 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(7), 1947 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(7),
1453 now, 1948 now));
1454 false));
1455 1949
1456 // Delete the last (now) item. 1950 // Delete the last (now) item.
1457 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, Time(), Time(), false)); 1951 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, Time(), Time()));
1458 1952
1459 // Really make sure everything is gone. 1953 // Really make sure everything is gone.
1460 EXPECT_EQ(0, cm->DeleteAll(false)); 1954 EXPECT_EQ(0, DeleteAll(cm));
1461 } 1955 }
1462 1956
1463 TEST_F(CookieMonsterTest, TestSecure) { 1957 TEST_F(CookieMonsterTest, TestSecure) {
1464 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1958 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1465 1959
1466 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 1960 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
1467 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1961 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1468 EXPECT_EQ("A=B", GetCookies(cm, url_google_secure_)); 1962 EXPECT_EQ("A=B", GetCookies(cm, url_google_secure_));
1469 1963
1470 EXPECT_TRUE(cm->SetCookie(url_google_secure_, "A=B; secure")); 1964 EXPECT_TRUE(SetCookie(cm, url_google_secure_, "A=B; secure"));
1471 // The secure should overwrite the non-secure. 1965 // The secure should overwrite the non-secure.
1472 EXPECT_EQ("", GetCookies(cm, url_google_)); 1966 EXPECT_EQ("", GetCookies(cm, url_google_));
1473 EXPECT_EQ("A=B", GetCookies(cm, url_google_secure_)); 1967 EXPECT_EQ("A=B", GetCookies(cm, url_google_secure_));
1474 1968
1475 EXPECT_TRUE(cm->SetCookie(url_google_secure_, "D=E; secure")); 1969 EXPECT_TRUE(SetCookie(cm, url_google_secure_, "D=E; secure"));
1476 EXPECT_EQ("", GetCookies(cm, url_google_)); 1970 EXPECT_EQ("", GetCookies(cm, url_google_));
1477 EXPECT_EQ("A=B; D=E", GetCookies(cm, url_google_secure_)); 1971 EXPECT_EQ("A=B; D=E", GetCookies(cm, url_google_secure_));
1478 1972
1479 EXPECT_TRUE(cm->SetCookie(url_google_secure_, "A=B")); 1973 EXPECT_TRUE(SetCookie(cm, url_google_secure_, "A=B"));
1480 // The non-secure should overwrite the secure. 1974 // The non-secure should overwrite the secure.
1481 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1975 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1482 EXPECT_EQ("D=E; A=B", GetCookies(cm, url_google_secure_)); 1976 EXPECT_EQ("D=E; A=B", GetCookies(cm, url_google_secure_));
1483 } 1977 }
1484 1978
1485 static const int kLastAccessThresholdMilliseconds = 200; 1979 static const int kLastAccessThresholdMilliseconds = 200;
1486 1980
1487 TEST_F(CookieMonsterTest, TestLastAccess) { 1981 TEST_F(CookieMonsterTest, TestLastAccess) {
1488 scoped_refptr<CookieMonster> cm( 1982 scoped_refptr<CookieMonster> cm(
1489 new CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds)); 1983 new CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds));
1490 1984
1491 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 1985 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
1492 const Time last_access_date(GetFirstCookieAccessDate(cm)); 1986 const Time last_access_date(GetFirstCookieAccessDate(cm));
1493 1987
1494 // Reading the cookie again immediately shouldn't update the access date, 1988 // Reading the cookie again immediately shouldn't update the access date,
1495 // since we're inside the threshold. 1989 // since we're inside the threshold.
1496 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1990 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1497 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm)); 1991 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm));
1498 1992
1499 // Reading after a short wait should update the access date. 1993 // Reading after a short wait should update the access date.
1500 base::PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20); 1994 base::PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20);
1501 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1995 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1502 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm)); 1996 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm));
1503 } 1997 }
1504 1998
1505 TEST_F(CookieMonsterTest, TestHostGarbageCollection) { 1999 TEST_F(CookieMonsterTest, TestHostGarbageCollection) {
1506 TestHostGarbageCollectHelper( 2000 TestHostGarbageCollectHelper(
1507 CookieMonster::kDomainMaxCookies, CookieMonster::kDomainPurgeCookies, 2001 CookieMonster::kDomainMaxCookies, CookieMonster::kDomainPurgeCookies,
1508 CookieMonster::EKS_KEEP_RECENT_AND_PURGE_ETLDP1); 2002 CookieMonster::EKS_KEEP_RECENT_AND_PURGE_ETLDP1);
1509 TestHostGarbageCollectHelper( 2003 TestHostGarbageCollectHelper(
1510 CookieMonster::kDomainMaxCookies, CookieMonster::kDomainPurgeCookies, 2004 CookieMonster::kDomainMaxCookies, CookieMonster::kDomainPurgeCookies,
1511 CookieMonster::EKS_DISCARD_RECENT_AND_PURGE_DOMAIN); 2005 CookieMonster::EKS_DISCARD_RECENT_AND_PURGE_DOMAIN);
1512 } 2006 }
1513 2007
1514 // Formerly NetUtilTest.CookieTest back when we used wininet's cookie handling. 2008 // Formerly NetUtilTest.CookieTest back when we used wininet's cookie handling.
1515 TEST_F(CookieMonsterTest, NetUtilCookieTest) { 2009 TEST_F(CookieMonsterTest, NetUtilCookieTest) {
1516 const GURL test_url("http://mojo.jojo.google.izzle/"); 2010 const GURL test_url("http://mojo.jojo.google.izzle/");
1517 2011
1518 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2012 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1519 2013
1520 EXPECT_TRUE(cm->SetCookie(test_url, "foo=bar")); 2014 EXPECT_TRUE(SetCookie(cm, test_url, "foo=bar"));
1521 std::string value = GetCookies(cm, test_url); 2015 std::string value = GetCookies(cm, test_url);
1522 EXPECT_EQ("foo=bar", value); 2016 EXPECT_EQ("foo=bar", value);
1523 2017
1524 // test that we can retrieve all cookies: 2018 // test that we can retrieve all cookies:
1525 EXPECT_TRUE(cm->SetCookie(test_url, "x=1")); 2019 EXPECT_TRUE(SetCookie(cm, test_url, "x=1"));
1526 EXPECT_TRUE(cm->SetCookie(test_url, "y=2")); 2020 EXPECT_TRUE(SetCookie(cm, test_url, "y=2"));
1527 2021
1528 std::string result = GetCookies(cm, test_url); 2022 std::string result = GetCookies(cm, test_url);
1529 EXPECT_FALSE(result.empty()); 2023 EXPECT_FALSE(result.empty());
1530 EXPECT_NE(result.find("x=1"), std::string::npos) << result; 2024 EXPECT_NE(result.find("x=1"), std::string::npos) << result;
1531 EXPECT_NE(result.find("y=2"), std::string::npos) << result; 2025 EXPECT_NE(result.find("y=2"), std::string::npos) << result;
1532 } 2026 }
1533 2027
1534 TEST_F(CookieMonsterTest, TestDeleteSingleCookie) { 2028 TEST_F(CookieMonsterTest, TestDeleteSingleCookie) {
1535 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2029 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1536 2030
1537 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 2031 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
1538 EXPECT_TRUE(cm->SetCookie(url_google_, "C=D")); 2032 EXPECT_TRUE(SetCookie(cm, url_google_, "C=D"));
1539 EXPECT_TRUE(cm->SetCookie(url_google_, "E=F")); 2033 EXPECT_TRUE(SetCookie(cm, url_google_, "E=F"));
1540 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_)); 2034 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_));
1541 2035
1542 EXPECT_TRUE(FindAndDeleteCookie(cm, url_google_.host(), "C")); 2036 EXPECT_TRUE(FindAndDeleteCookie(cm, url_google_.host(), "C"));
1543 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_)); 2037 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_));
1544 2038
1545 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E")); 2039 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E"));
1546 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_)); 2040 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_));
1547 } 2041 }
1548 2042
1549 TEST_F(CookieMonsterTest, SetCookieableSchemes) { 2043 TEST_F(CookieMonsterTest, SetCookieableSchemes) {
1550 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2044 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1551 scoped_refptr<CookieMonster> cm_foo(new CookieMonster(NULL, NULL)); 2045 scoped_refptr<CookieMonster> cm_foo(new CookieMonster(NULL, NULL));
1552 2046
1553 // Only cm_foo should allow foo:// cookies. 2047 // Only cm_foo should allow foo:// cookies.
1554 const char* kSchemes[] = {"foo"}; 2048 const char* kSchemes[] = {"foo"};
1555 cm_foo->SetCookieableSchemes(kSchemes, 1); 2049 cm_foo->SetCookieableSchemes(kSchemes, 1);
1556 2050
1557 GURL foo_url("foo://host/path"); 2051 GURL foo_url("foo://host/path");
1558 GURL http_url("http://host/path"); 2052 GURL http_url("http://host/path");
1559 2053
1560 EXPECT_TRUE(cm->SetCookie(http_url, "x=1")); 2054 EXPECT_TRUE(SetCookie(cm, http_url, "x=1"));
1561 EXPECT_FALSE(cm->SetCookie(foo_url, "x=1")); 2055 EXPECT_FALSE(SetCookie(cm, foo_url, "x=1"));
1562 EXPECT_TRUE(cm_foo->SetCookie(foo_url, "x=1")); 2056 EXPECT_TRUE(SetCookie(cm_foo, foo_url, "x=1"));
1563 EXPECT_FALSE(cm_foo->SetCookie(http_url, "x=1")); 2057 EXPECT_FALSE(SetCookie(cm_foo, http_url, "x=1"));
1564 } 2058 }
1565 2059
1566 TEST_F(CookieMonsterTest, GetAllCookiesForURL) { 2060 TEST_F(CookieMonsterTest, GetAllCookiesForURL) {
1567 scoped_refptr<CookieMonster> cm( 2061 scoped_refptr<CookieMonster> cm(
1568 new CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds)); 2062 new CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds));
1569 2063
1570 // Create an httponly cookie. 2064 // Create an httponly cookie.
1571 CookieOptions options; 2065 CookieOptions options;
1572 options.set_include_httponly(); 2066 options.set_include_httponly();
1573 2067
1574 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B; httponly", options)); 2068 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B; httponly", options));
1575 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, 2069 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_,
1576 "C=D; domain=.google.izzle", 2070 "C=D; domain=.google.izzle",
1577 options)); 2071 options));
1578 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_secure_, 2072 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_secure_,
1579 "E=F; domain=.google.izzle; secure", 2073 "E=F; domain=.google.izzle; secure",
1580 options)); 2074 options));
1581 2075
1582 const Time last_access_date(GetFirstCookieAccessDate(cm)); 2076 const Time last_access_date(GetFirstCookieAccessDate(cm));
1583 2077
1584 base::PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20); 2078 base::PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20);
1585 2079
1586 // Check cookies for url. 2080 // Check cookies for url.
1587 CookieList cookies = GetAllCookiesForURL(cm, url_google_); 2081 CookieList cookies = GetAllCookiesForURL(cm, url_google_);
1588 CookieList::iterator it = cookies.begin(); 2082 CookieList::iterator it = cookies.begin();
1589 2083
1590 ASSERT_TRUE(it != cookies.end()); 2084 ASSERT_TRUE(it != cookies.end());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1628 2122
1629 // Reading after a short wait should not update the access date. 2123 // Reading after a short wait should not update the access date.
1630 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm)); 2124 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm));
1631 } 2125 }
1632 2126
1633 TEST_F(CookieMonsterTest, GetAllCookiesForURLPathMatching) { 2127 TEST_F(CookieMonsterTest, GetAllCookiesForURLPathMatching) {
1634 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2128 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1635 CookieOptions options; 2129 CookieOptions options;
1636 2130
1637 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_foo_, 2131 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_foo_,
1638 "A=B; path=/foo;", 2132 "A=B; path=/foo;", options));
1639 options));
1640 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_bar_, 2133 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_bar_,
1641 "C=D; path=/bar;", 2134 "C=D; path=/bar;", options));
1642 options));
1643 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, 2135 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_,
1644 "E=F;", 2136 "E=F;", options));
1645 options));
1646 2137
1647 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_); 2138 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_);
1648 CookieList::iterator it = cookies.begin(); 2139 CookieList::iterator it = cookies.begin();
1649 2140
1650 ASSERT_TRUE(it != cookies.end()); 2141 ASSERT_TRUE(it != cookies.end());
1651 EXPECT_EQ("A", it->Name()); 2142 EXPECT_EQ("A", it->Name());
1652 EXPECT_EQ("/foo", it->Path()); 2143 EXPECT_EQ("/foo", it->Path());
1653 2144
1654 ASSERT_TRUE(++it != cookies.end()); 2145 ASSERT_TRUE(++it != cookies.end());
1655 EXPECT_EQ("E", it->Name()); 2146 EXPECT_EQ("E", it->Name());
(...skipping 11 matching lines...) Expand all
1667 ASSERT_TRUE(++it != cookies.end()); 2158 ASSERT_TRUE(++it != cookies.end());
1668 EXPECT_EQ("E", it->Name()); 2159 EXPECT_EQ("E", it->Name());
1669 EXPECT_EQ("/", it->Path()); 2160 EXPECT_EQ("/", it->Path());
1670 2161
1671 ASSERT_TRUE(++it == cookies.end()); 2162 ASSERT_TRUE(++it == cookies.end());
1672 } 2163 }
1673 2164
1674 TEST_F(CookieMonsterTest, DeleteCookieByName) { 2165 TEST_F(CookieMonsterTest, DeleteCookieByName) {
1675 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2166 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1676 2167
1677 EXPECT_TRUE(cm->SetCookie(url_google_, "A=A1; path=/")); 2168 EXPECT_TRUE(SetCookie(cm, url_google_, "A=A1; path=/"));
1678 EXPECT_TRUE(cm->SetCookie(url_google_, "A=A2; path=/foo")); 2169 EXPECT_TRUE(SetCookie(cm, url_google_, "A=A2; path=/foo"));
1679 EXPECT_TRUE(cm->SetCookie(url_google_, "A=A3; path=/bar")); 2170 EXPECT_TRUE(SetCookie(cm, url_google_, "A=A3; path=/bar"));
1680 EXPECT_TRUE(cm->SetCookie(url_google_, "B=B1; path=/")); 2171 EXPECT_TRUE(SetCookie(cm, url_google_, "B=B1; path=/"));
1681 EXPECT_TRUE(cm->SetCookie(url_google_, "B=B2; path=/foo")); 2172 EXPECT_TRUE(SetCookie(cm, url_google_, "B=B2; path=/foo"));
1682 EXPECT_TRUE(cm->SetCookie(url_google_, "B=B3; path=/bar")); 2173 EXPECT_TRUE(SetCookie(cm, url_google_, "B=B3; path=/bar"));
1683 2174
1684 DeleteCookie(cm, GURL(std::string(kUrlGoogle) + "/foo/bar"), "A"); 2175 DeleteCookie(cm, GURL(std::string(kUrlGoogle) + "/foo/bar"), "A");
1685 2176
1686 CookieList cookies = GetAllCookies(cm); 2177 CookieList cookies = GetAllCookies(cm);
1687 size_t expected_size = 4; 2178 size_t expected_size = 4;
1688 EXPECT_EQ(expected_size, cookies.size()); 2179 EXPECT_EQ(expected_size, cookies.size());
1689 for (CookieList::iterator it = cookies.begin(); 2180 for (CookieList::iterator it = cookies.begin();
1690 it != cookies.end(); ++it) { 2181 it != cookies.end(); ++it) {
1691 EXPECT_NE("A1", it->Value()); 2182 EXPECT_NE("A1", it->Value());
1692 EXPECT_NE("A2", it->Value()); 2183 EXPECT_NE("A2", it->Value());
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1735 // backing store. 2226 // backing store.
1736 TEST_F(CookieMonsterTest, OverwritePersistentCookie) { 2227 TEST_F(CookieMonsterTest, OverwritePersistentCookie) {
1737 GURL url_google("http://www.google.com/"); 2228 GURL url_google("http://www.google.com/");
1738 GURL url_chromium("http://chromium.org"); 2229 GURL url_chromium("http://chromium.org");
1739 scoped_refptr<MockPersistentCookieStore> store( 2230 scoped_refptr<MockPersistentCookieStore> store(
1740 new MockPersistentCookieStore); 2231 new MockPersistentCookieStore);
1741 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 2232 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
1742 2233
1743 // Insert a cookie "a" for path "/path1" 2234 // Insert a cookie "a" for path "/path1"
1744 EXPECT_TRUE( 2235 EXPECT_TRUE(
1745 cm->SetCookie(url_google, "a=val1; path=/path1; " 2236 SetCookie(cm, url_google, "a=val1; path=/path1; "
1746 "expires=Mon, 18-Apr-22 22:50:13 GMT")); 2237 "expires=Mon, 18-Apr-22 22:50:13 GMT"));
1747 ASSERT_EQ(1u, store->commands().size()); 2238 ASSERT_EQ(1u, store->commands().size());
1748 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); 2239 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
1749 2240
1750 // Insert a cookie "b" for path "/path1" 2241 // Insert a cookie "b" for path "/path1"
1751 EXPECT_TRUE( 2242 EXPECT_TRUE(
1752 cm->SetCookie(url_google, "b=val1; path=/path1; " 2243 SetCookie(cm, url_google, "b=val1; path=/path1; "
1753 "expires=Mon, 18-Apr-22 22:50:14 GMT")); 2244 "expires=Mon, 18-Apr-22 22:50:14 GMT"));
1754 ASSERT_EQ(2u, store->commands().size()); 2245 ASSERT_EQ(2u, store->commands().size());
1755 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[1].type); 2246 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[1].type);
1756 2247
1757 // Insert a cookie "b" for path "/path1", that is httponly. This should 2248 // Insert a cookie "b" for path "/path1", that is httponly. This should
1758 // overwrite the non-http-only version. 2249 // overwrite the non-http-only version.
1759 CookieOptions allow_httponly; 2250 CookieOptions allow_httponly;
1760 allow_httponly.set_include_httponly(); 2251 allow_httponly.set_include_httponly();
1761 EXPECT_TRUE( 2252 EXPECT_TRUE(
1762 SetCookieWithOptions(cm, url_google, 2253 SetCookieWithOptions(cm, url_google,
1763 "b=val2; path=/path1; httponly; " 2254 "b=val2; path=/path1; httponly; "
1764 "expires=Mon, 18-Apr-22 22:50:14 GMT", 2255 "expires=Mon, 18-Apr-22 22:50:14 GMT",
1765 allow_httponly)); 2256 allow_httponly));
1766 ASSERT_EQ(4u, store->commands().size()); 2257 ASSERT_EQ(4u, store->commands().size());
1767 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[2].type); 2258 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[2].type);
1768 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[3].type); 2259 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[3].type);
1769 2260
1770 // Insert a cookie "a" for path "/path1". This should overwrite. 2261 // Insert a cookie "a" for path "/path1". This should overwrite.
1771 EXPECT_TRUE(cm->SetCookie(url_google, 2262 EXPECT_TRUE(SetCookie(cm, url_google,
1772 "a=val33; path=/path1; " 2263 "a=val33; path=/path1; "
1773 "expires=Mon, 18-Apr-22 22:50:14 GMT")); 2264 "expires=Mon, 18-Apr-22 22:50:14 GMT"));
1774 ASSERT_EQ(6u, store->commands().size()); 2265 ASSERT_EQ(6u, store->commands().size());
1775 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[4].type); 2266 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[4].type);
1776 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[5].type); 2267 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[5].type);
1777 2268
1778 // Insert a cookie "a" for path "/path2". This should NOT overwrite 2269 // Insert a cookie "a" for path "/path2". This should NOT overwrite
1779 // cookie "a", since the path is different. 2270 // cookie "a", since the path is different.
1780 EXPECT_TRUE(cm->SetCookie(url_google, 2271 EXPECT_TRUE(SetCookie(cm, url_google,
1781 "a=val9; path=/path2; " 2272 "a=val9; path=/path2; "
1782 "expires=Mon, 18-Apr-22 22:50:14 GMT")); 2273 "expires=Mon, 18-Apr-22 22:50:14 GMT"));
1783 ASSERT_EQ(7u, store->commands().size()); 2274 ASSERT_EQ(7u, store->commands().size());
1784 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[6].type); 2275 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[6].type);
1785 2276
1786 // Insert a cookie "a" for path "/path1", but this time for "chromium.org". 2277 // Insert a cookie "a" for path "/path1", but this time for "chromium.org".
1787 // Although the name and path match, the hostnames do not, so shouldn't 2278 // Although the name and path match, the hostnames do not, so shouldn't
1788 // overwrite. 2279 // overwrite.
1789 EXPECT_TRUE(cm->SetCookie(url_chromium, 2280 EXPECT_TRUE(SetCookie(cm, url_chromium,
1790 "a=val99; path=/path1; " 2281 "a=val99; path=/path1; "
1791 "expires=Mon, 18-Apr-22 22:50:14 GMT")); 2282 "expires=Mon, 18-Apr-22 22:50:14 GMT"));
1792 ASSERT_EQ(8u, store->commands().size()); 2283 ASSERT_EQ(8u, store->commands().size());
1793 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[7].type); 2284 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[7].type);
1794 2285
1795 EXPECT_EQ("a=val33", GetCookies(cm, GURL("http://www.google.com/path1"))); 2286 EXPECT_EQ("a=val33", GetCookies(cm, GURL("http://www.google.com/path1")));
1796 EXPECT_EQ("a=val9", GetCookies(cm, GURL("http://www.google.com/path2"))); 2287 EXPECT_EQ("a=val9", GetCookies(cm, GURL("http://www.google.com/path2")));
1797 EXPECT_EQ("a=val99", GetCookies(cm, GURL("http://chromium.org/path1"))); 2288 EXPECT_EQ("a=val99", GetCookies(cm, GURL("http://chromium.org/path1")));
1798 } 2289 }
1799 2290
1800 // Tests importing from a persistent cookie store that contains duplicate 2291 // Tests importing from a persistent cookie store that contains duplicate
1801 // equivalent cookies. This situation should be handled by removing the 2292 // equivalent cookies. This situation should be handled by removing the
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
1922 EXPECT_NE(name1, name2); 2413 EXPECT_NE(name1, name2);
1923 } 2414 }
1924 2415
1925 TEST_F(CookieMonsterTest, Delegate) { 2416 TEST_F(CookieMonsterTest, Delegate) {
1926 scoped_refptr<MockPersistentCookieStore> store( 2417 scoped_refptr<MockPersistentCookieStore> store(
1927 new MockPersistentCookieStore); 2418 new MockPersistentCookieStore);
1928 scoped_refptr<MockCookieMonsterDelegate> delegate( 2419 scoped_refptr<MockCookieMonsterDelegate> delegate(
1929 new MockCookieMonsterDelegate); 2420 new MockCookieMonsterDelegate);
1930 scoped_refptr<CookieMonster> cm(new CookieMonster(store, delegate)); 2421 scoped_refptr<CookieMonster> cm(new CookieMonster(store, delegate));
1931 2422
1932 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 2423 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
1933 EXPECT_TRUE(cm->SetCookie(url_google_, "C=D")); 2424 EXPECT_TRUE(SetCookie(cm, url_google_, "C=D"));
1934 EXPECT_TRUE(cm->SetCookie(url_google_, "E=F")); 2425 EXPECT_TRUE(SetCookie(cm, url_google_, "E=F"));
1935 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_)); 2426 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_));
1936 ASSERT_EQ(3u, delegate->changes().size()); 2427 ASSERT_EQ(3u, delegate->changes().size());
1937 EXPECT_FALSE(delegate->changes()[0].second); 2428 EXPECT_FALSE(delegate->changes()[0].second);
1938 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); 2429 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain());
1939 EXPECT_EQ("A", delegate->changes()[0].first.Name()); 2430 EXPECT_EQ("A", delegate->changes()[0].first.Name());
1940 EXPECT_EQ("B", delegate->changes()[0].first.Value()); 2431 EXPECT_EQ("B", delegate->changes()[0].first.Value());
1941 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain()); 2432 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain());
1942 EXPECT_FALSE(delegate->changes()[1].second); 2433 EXPECT_FALSE(delegate->changes()[1].second);
1943 EXPECT_EQ("C", delegate->changes()[1].first.Name()); 2434 EXPECT_EQ("C", delegate->changes()[1].first.Name());
1944 EXPECT_EQ("D", delegate->changes()[1].first.Value()); 2435 EXPECT_EQ("D", delegate->changes()[1].first.Value());
(...skipping 11 matching lines...) Expand all
1956 EXPECT_EQ("C", delegate->changes()[0].first.Name()); 2447 EXPECT_EQ("C", delegate->changes()[0].first.Name());
1957 EXPECT_EQ("D", delegate->changes()[0].first.Value()); 2448 EXPECT_EQ("D", delegate->changes()[0].first.Value());
1958 delegate->reset(); 2449 delegate->reset();
1959 2450
1960 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E")); 2451 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E"));
1961 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_)); 2452 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_));
1962 EXPECT_EQ(0u, delegate->changes().size()); 2453 EXPECT_EQ(0u, delegate->changes().size());
1963 2454
1964 // Insert a cookie "a" for path "/path1" 2455 // Insert a cookie "a" for path "/path1"
1965 EXPECT_TRUE( 2456 EXPECT_TRUE(
1966 cm->SetCookie(url_google_, "a=val1; path=/path1; " 2457 SetCookie(cm, url_google_, "a=val1; path=/path1; "
1967 "expires=Mon, 18-Apr-22 22:50:13 GMT")); 2458 "expires=Mon, 18-Apr-22 22:50:13 GMT"));
1968 ASSERT_EQ(1u, store->commands().size()); 2459 ASSERT_EQ(1u, store->commands().size());
1969 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); 2460 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
1970 ASSERT_EQ(1u, delegate->changes().size()); 2461 ASSERT_EQ(1u, delegate->changes().size());
1971 EXPECT_FALSE(delegate->changes()[0].second); 2462 EXPECT_FALSE(delegate->changes()[0].second);
1972 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); 2463 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain());
1973 EXPECT_EQ("a", delegate->changes()[0].first.Name()); 2464 EXPECT_EQ("a", delegate->changes()[0].first.Name());
1974 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); 2465 EXPECT_EQ("val1", delegate->changes()[0].first.Value());
1975 delegate->reset(); 2466 delegate->reset();
1976 2467
1977 // Insert a cookie "a" for path "/path1", that is httponly. This should 2468 // Insert a cookie "a" for path "/path1", that is httponly. This should
1978 // overwrite the non-http-only version. 2469 // overwrite the non-http-only version.
1979 CookieOptions allow_httponly; 2470 CookieOptions allow_httponly;
1980 allow_httponly.set_include_httponly(); 2471 allow_httponly.set_include_httponly();
1981 EXPECT_TRUE( 2472 EXPECT_TRUE(
1982 SetCookieWithOptions(cm, url_google_, 2473 SetCookieWithOptions(cm, url_google_,
1983 "a=val2; path=/path1; httponly; " 2474 "a=val2; path=/path1; httponly; "
1984 "expires=Mon, 18-Apr-22 22:50:14 GMT", 2475 "expires=Mon, 18-Apr-22 22:50:14 GMT",
1985 allow_httponly)); 2476 allow_httponly));
1986 ASSERT_EQ(3u, store->commands().size()); 2477 ASSERT_EQ(3u, store->commands().size());
1987 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); 2478 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
1988 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type); 2479 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type);
1989 ASSERT_EQ(2u, delegate->changes().size()); 2480 ASSERT_EQ(2u, delegate->changes().size());
1990 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); 2481 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain());
1991 EXPECT_TRUE(delegate->changes()[0].second); 2482 EXPECT_TRUE(delegate->changes()[0].second);
1992 EXPECT_EQ("a", delegate->changes()[0].first.Name()); 2483 EXPECT_EQ("a", delegate->changes()[0].first.Name());
1993 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); 2484 EXPECT_EQ("val1", delegate->changes()[0].first.Value());
1994 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain()); 2485 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain());
1995 EXPECT_FALSE(delegate->changes()[1].second); 2486 EXPECT_FALSE(delegate->changes()[1].second);
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
2136 CookieOptions options; 2627 CookieOptions options;
2137 2628
2138 // Add in three cookies through every public interface to the 2629 // Add in three cookies through every public interface to the
2139 // CookieMonster and confirm that none of them have duplicate 2630 // CookieMonster and confirm that none of them have duplicate
2140 // creation times. 2631 // creation times.
2141 2632
2142 // SetCookieWithCreationTime and SetCookieWithCreationTimeAndOptions 2633 // SetCookieWithCreationTime and SetCookieWithCreationTimeAndOptions
2143 // are not included as they aren't going to be public for very much 2634 // are not included as they aren't going to be public for very much
2144 // longer. 2635 // longer.
2145 2636
2146 // SetCookie, SetCookies, SetCookiesWithOptions, 2637 // SetCookie, SetCookieWithOptions, SetCookieWithDetails
2147 // SetCookieWithOptions, SetCookieWithDetails
2148 2638
2149 cm->SetCookie(url_google_, "SetCookie1=A"); 2639 SetCookie(cm, url_google_, "SetCookie1=A");
2150 cm->SetCookie(url_google_, "SetCookie2=A"); 2640 SetCookie(cm, url_google_, "SetCookie2=A");
2151 cm->SetCookie(url_google_, "SetCookie3=A"); 2641 SetCookie(cm, url_google_, "SetCookie3=A");
2152
2153 {
2154 std::vector<std::string> cookie_lines;
2155 cookie_lines.push_back("setCookies1=A");
2156 cookie_lines.push_back("setCookies2=A");
2157 cookie_lines.push_back("setCookies4=A");
2158 cm->SetCookies(url_google_, cookie_lines);
2159 }
2160
2161 {
2162 std::vector<std::string> cookie_lines;
2163 cookie_lines.push_back("setCookiesWithOptions1=A");
2164 cookie_lines.push_back("setCookiesWithOptions2=A");
2165 cookie_lines.push_back("setCookiesWithOptions3=A");
2166
2167 cm->SetCookiesWithOptions(url_google_, cookie_lines, options);
2168 }
2169 2642
2170 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions1=A", options); 2643 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions1=A", options);
2171 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions2=A", options); 2644 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions2=A", options);
2172 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions3=A", options); 2645 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions3=A", options);
2173 2646
2174 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails1", "A", 2647 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails1", "A",
2175 ".google.com", "/", Time(), false, false); 2648 ".google.com", "/", Time(), false, false);
2176 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails2", "A", 2649 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails2", "A",
2177 ".google.com", "/", Time(), false, false); 2650 ".google.com", "/", Time(), false, false);
2178 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails3", "A", 2651 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails3", "A",
2179 ".google.com", "/", Time(), false, false); 2652 ".google.com", "/", Time(), false, false);
2180 2653
2181 // Now we check 2654 // Now we check
2182 CookieList cookie_list(GetAllCookies(cm)); 2655 CookieList cookie_list(GetAllCookies(cm));
2183 typedef std::map<int64, CookieMonster::CanonicalCookie> TimeCookieMap; 2656 typedef std::map<int64, CookieMonster::CanonicalCookie> TimeCookieMap;
2184 TimeCookieMap check_map; 2657 TimeCookieMap check_map;
2185 for (CookieList::const_iterator it = cookie_list.begin(); 2658 for (CookieList::const_iterator it = cookie_list.begin();
2186 it != cookie_list.end(); it++) { 2659 it != cookie_list.end(); it++) {
2187 const int64 creation_date = it->CreationDate().ToInternalValue(); 2660 const int64 creation_date = it->CreationDate().ToInternalValue();
2188 TimeCookieMap::const_iterator 2661 TimeCookieMap::const_iterator
2189 existing_cookie_it(check_map.find(creation_date)); 2662 existing_cookie_it(check_map.find(creation_date));
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
2256 base::Time::Now() + base::TimeDelta::FromSeconds(100), 2729 base::Time::Now() + base::TimeDelta::FromSeconds(100),
2257 true, false} 2730 true, false}
2258 }; 2731 };
2259 const int INPUT_DELETE = 1; 2732 const int INPUT_DELETE = 1;
2260 2733
2261 // Create new cookies and flush them to the store. 2734 // Create new cookies and flush them to the store.
2262 { 2735 {
2263 scoped_refptr<CookieMonster> cmout(new CookieMonster(store, NULL)); 2736 scoped_refptr<CookieMonster> cmout(new CookieMonster(store, NULL));
2264 for (const CookiesInputInfo* p = input_info; 2737 for (const CookiesInputInfo* p = input_info;
2265 p < &input_info[ARRAYSIZE_UNSAFE(input_info)]; p++) { 2738 p < &input_info[ARRAYSIZE_UNSAFE(input_info)]; p++) {
2266 EXPECT_TRUE(cmout->SetCookieWithDetails(GURL(p->gurl), p->name, p->value, 2739 EXPECT_TRUE(SetCookieWithDetails(cmout, GURL(p->gurl), p->name, p->value,
2267 p->domain, p->path, p->expires, 2740 p->domain, p->path, p->expires,
2268 p->secure, p->http_only)); 2741 p->secure, p->http_only));
2269 } 2742 }
2270 DeleteCookie(cmout, GURL(std::string(input_info[INPUT_DELETE].gurl) + 2743 DeleteCookie(cmout, GURL(std::string(input_info[INPUT_DELETE].gurl) +
2271 input_info[INPUT_DELETE].path), 2744 input_info[INPUT_DELETE].path),
2272 input_info[INPUT_DELETE].name); 2745 input_info[INPUT_DELETE].name);
2273 } 2746 }
2274 2747
2275 // Create a new cookie monster and make sure that everything is correct 2748 // Create a new cookie monster and make sure that everything is correct
2276 { 2749 {
2277 scoped_refptr<CookieMonster> cmin(new CookieMonster(store, NULL)); 2750 scoped_refptr<CookieMonster> cmin(new CookieMonster(store, NULL));
2278 CookieList cookies(GetAllCookies(cmin)); 2751 CookieList cookies(GetAllCookies(cmin));
(...skipping 18 matching lines...) Expand all
2297 EXPECT_EQ(input->expires.ToInternalValue(), 2770 EXPECT_EQ(input->expires.ToInternalValue(),
2298 output->ExpiryDate().ToInternalValue()); 2771 output->ExpiryDate().ToInternalValue());
2299 } 2772 }
2300 } 2773 }
2301 } 2774 }
2302 2775
2303 TEST_F(CookieMonsterTest, CookieOrdering) { 2776 TEST_F(CookieMonsterTest, CookieOrdering) {
2304 // Put a random set of cookies into a monster and make sure 2777 // Put a random set of cookies into a monster and make sure
2305 // they're returned in the right order. 2778 // they're returned in the right order.
2306 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2779 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2307 EXPECT_TRUE(cm->SetCookie(GURL("http://d.c.b.a.google.com/aa/x.html"), 2780 EXPECT_TRUE(SetCookie(cm, GURL("http://d.c.b.a.google.com/aa/x.html"),
2308 "c=1")); 2781 "c=1"));
2309 EXPECT_TRUE(cm->SetCookie(GURL("http://b.a.google.com/aa/bb/cc/x.html"), 2782 EXPECT_TRUE(SetCookie(cm, GURL("http://b.a.google.com/aa/bb/cc/x.html"),
2310 "d=1; domain=b.a.google.com")); 2783 "d=1; domain=b.a.google.com"));
2311 EXPECT_TRUE(cm->SetCookie(GURL("http://b.a.google.com/aa/bb/cc/x.html"), 2784 EXPECT_TRUE(SetCookie(cm, GURL("http://b.a.google.com/aa/bb/cc/x.html"),
2312 "a=4; domain=b.a.google.com")); 2785 "a=4; domain=b.a.google.com"));
2313 EXPECT_TRUE(cm->SetCookie(GURL("http://c.b.a.google.com/aa/bb/cc/x.html"), 2786 EXPECT_TRUE(SetCookie(cm, GURL("http://c.b.a.google.com/aa/bb/cc/x.html"),
2314 "e=1; domain=c.b.a.google.com")); 2787 "e=1; domain=c.b.a.google.com"));
2315 EXPECT_TRUE(cm->SetCookie(GURL("http://d.c.b.a.google.com/aa/bb/x.html"), 2788 EXPECT_TRUE(SetCookie(cm, GURL("http://d.c.b.a.google.com/aa/bb/x.html"),
2316 "b=1")); 2789 "b=1"));
2317 EXPECT_TRUE(cm->SetCookie(GURL("http://news.bbc.co.uk/midpath/x.html"), 2790 EXPECT_TRUE(SetCookie(cm, GURL("http://news.bbc.co.uk/midpath/x.html"),
2318 "g=10")); 2791 "g=10"));
2319 EXPECT_EQ("d=1; a=4; e=1; b=1; c=1", 2792 EXPECT_EQ("d=1; a=4; e=1; b=1; c=1",
2320 GetCookiesWithOptions(cm, 2793 GetCookiesWithOptions(
2321 GURL("http://d.c.b.a.google.com/aa/bb/cc/dd"), 2794 cm, GURL("http://d.c.b.a.google.com/aa/bb/cc/dd"),
2322 CookieOptions())); 2795 CookieOptions()));
2323 { 2796 {
2324 unsigned int i = 0; 2797 unsigned int i = 0;
2325 CookieList cookies(cm->GetAllCookiesForURL( 2798 CookieList cookies(GetAllCookiesForURL(
2326 GURL("http://d.c.b.a.google.com/aa/bb/cc/dd"))); 2799 cm, GURL("http://d.c.b.a.google.com/aa/bb/cc/dd")));
2327 ASSERT_EQ(5u, cookies.size()); 2800 ASSERT_EQ(5u, cookies.size());
2328 EXPECT_EQ("d", cookies[i++].Name()); 2801 EXPECT_EQ("d", cookies[i++].Name());
2329 EXPECT_EQ("a", cookies[i++].Name()); 2802 EXPECT_EQ("a", cookies[i++].Name());
2330 EXPECT_EQ("e", cookies[i++].Name()); 2803 EXPECT_EQ("e", cookies[i++].Name());
2331 EXPECT_EQ("b", cookies[i++].Name()); 2804 EXPECT_EQ("b", cookies[i++].Name());
2332 EXPECT_EQ("c", cookies[i++].Name()); 2805 EXPECT_EQ("c", cookies[i++].Name());
2333 } 2806 }
2334 2807
2335 { 2808 {
2336 unsigned int i = 0; 2809 unsigned int i = 0;
2337 CookieList cookies(GetAllCookies(cm)); 2810 CookieList cookies(GetAllCookies(cm));
2338 ASSERT_EQ(6u, cookies.size()); 2811 ASSERT_EQ(6u, cookies.size());
2339 EXPECT_EQ("d", cookies[i++].Name()); 2812 EXPECT_EQ("d", cookies[i++].Name());
2340 EXPECT_EQ("a", cookies[i++].Name()); 2813 EXPECT_EQ("a", cookies[i++].Name());
2341 EXPECT_EQ("e", cookies[i++].Name()); 2814 EXPECT_EQ("e", cookies[i++].Name());
2342 EXPECT_EQ("g", cookies[i++].Name()); 2815 EXPECT_EQ("g", cookies[i++].Name());
2343 EXPECT_EQ("b", cookies[i++].Name()); 2816 EXPECT_EQ("b", cookies[i++].Name());
2344 EXPECT_EQ("c", cookies[i++].Name()); 2817 EXPECT_EQ("c", cookies[i++].Name());
2345 } 2818 }
2346 } 2819 }
2347 2820
2348
2349 // Function for creating a CM with a number of cookies in it,
2350 // no store (and hence no ability to affect access time).
2351 static CookieMonster* CreateMonsterForGC(int num_cookies) {
2352 CookieMonster* cm(new CookieMonster(NULL, NULL));
2353 for (int i = 0; i < num_cookies; i++)
2354 cm->SetCookie(GURL(StringPrintf("http://h%05d.izzle", i)), "a=1");
2355 return cm;
2356 }
2357
2358 // This test and CookieMonstertest.TestGCTimes (in cookie_monster_perftest.cc) 2821 // This test and CookieMonstertest.TestGCTimes (in cookie_monster_perftest.cc)
2359 // are somewhat complementary twins. This test is probing for whether 2822 // are somewhat complementary twins. This test is probing for whether
2360 // garbage collection always happens when it should (i.e. that we actually 2823 // garbage collection always happens when it should (i.e. that we actually
2361 // get rid of cookies when we should). The perftest is probing for 2824 // get rid of cookies when we should). The perftest is probing for
2362 // whether garbage collection happens when it shouldn't. See comments 2825 // whether garbage collection happens when it shouldn't. See comments
2363 // before that test for more details. 2826 // before that test for more details.
2364 TEST_F(CookieMonsterTest, GarbageCollectionTriggers) { 2827 TEST_F(CookieMonsterTest, GarbageCollectionTriggers) {
2365 // First we check to make sure that a whole lot of recent cookies 2828 // First we check to make sure that a whole lot of recent cookies
2366 // doesn't get rid of anything after garbage collection is checked for. 2829 // doesn't get rid of anything after garbage collection is checked for.
2367 { 2830 {
2368 scoped_refptr<CookieMonster> cm( 2831 scoped_refptr<CookieMonster> cm(
2369 CreateMonsterForGC(CookieMonster::kMaxCookies * 2)); 2832 CreateMonsterForGC(CookieMonster::kMaxCookies * 2));
2370 EXPECT_EQ(CookieMonster::kMaxCookies * 2, GetAllCookies(cm).size()); 2833 EXPECT_EQ(CookieMonster::kMaxCookies * 2, GetAllCookies(cm).size());
2371 cm->SetCookie(GURL("http://newdomain.com"), "b=2"); 2834 SetCookie(cm, GURL("http://newdomain.com"), "b=2");
2372 EXPECT_EQ(CookieMonster::kMaxCookies * 2 + 1, GetAllCookies(cm).size()); 2835 EXPECT_EQ(CookieMonster::kMaxCookies * 2 + 1, GetAllCookies(cm).size());
2373 } 2836 }
2374 2837
2375 // Now we explore a series of relationships between cookie last access 2838 // Now we explore a series of relationships between cookie last access
2376 // time and size of store to make sure we only get rid of cookies when 2839 // time and size of store to make sure we only get rid of cookies when
2377 // we really should. 2840 // we really should.
2378 const struct TestCase { 2841 const struct TestCase {
2379 int num_cookies; 2842 int num_cookies;
2380 int num_old_cookies; 2843 int num_old_cookies;
2381 int expected_initial_cookies; 2844 int expected_initial_cookies;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
2426 const TestCase *test_case = &test_cases[ci]; 2889 const TestCase *test_case = &test_cases[ci];
2427 scoped_refptr<CookieMonster> cm( 2890 scoped_refptr<CookieMonster> cm(
2428 CreateMonsterFromStoreForGC( 2891 CreateMonsterFromStoreForGC(
2429 test_case->num_cookies, test_case->num_old_cookies, 2892 test_case->num_cookies, test_case->num_old_cookies,
2430 CookieMonster::kSafeFromGlobalPurgeDays * 2)); 2893 CookieMonster::kSafeFromGlobalPurgeDays * 2));
2431 cm->SetExpiryAndKeyScheme(schemes[recent_scheme]); 2894 cm->SetExpiryAndKeyScheme(schemes[recent_scheme]);
2432 EXPECT_EQ(test_case->expected_initial_cookies, 2895 EXPECT_EQ(test_case->expected_initial_cookies,
2433 static_cast<int>(GetAllCookies(cm).size())) 2896 static_cast<int>(GetAllCookies(cm).size()))
2434 << "For test case " << ci; 2897 << "For test case " << ci;
2435 // Will trigger GC 2898 // Will trigger GC
2436 cm->SetCookie(GURL("http://newdomain.com"), "b=2"); 2899 SetCookie(cm, GURL("http://newdomain.com"), "b=2");
2437 EXPECT_EQ(test_case->expected_cookies_after_set[recent_scheme], 2900 EXPECT_EQ(test_case->expected_cookies_after_set[recent_scheme],
2438 static_cast<int>((GetAllCookies(cm).size()))) 2901 static_cast<int>((GetAllCookies(cm).size())))
2439 << "For test case (" << ci << ", " << recent_scheme << ")"; 2902 << "For test case (" << ci << ", " << recent_scheme << ")";
2440 } 2903 }
2441 } 2904 }
2442 } 2905 }
2443 2906
2444 // This test checks that setting a cookie forcing it to be a session only 2907 // This test checks that setting a cookie forcing it to be a session only
2445 // cookie works as expected. 2908 // cookie works as expected.
2446 TEST_F(CookieMonsterTest, ForceSessionOnly) { 2909 TEST_F(CookieMonsterTest, ForceSessionOnly) {
2447 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2910 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2448 CookieOptions options; 2911 CookieOptions options;
2449 2912
2450 // Set a persistent cookie, but force it to be a session cookie. 2913 // Set a persistent cookie, but force it to be a session cookie.
2451 options.set_force_session(); 2914 options.set_force_session();
2452 ASSERT_TRUE(SetCookieWithOptions(cm, url_google_, 2915 ASSERT_TRUE(SetCookieWithOptions(
2916 cm, url_google_,
2453 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT", 2917 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT",
2454 options)); 2918 options));
2455 2919
2456 // Get the canonical cookie. 2920 // Get the canonical cookie.
2457 CookieList cookie_list = GetAllCookies(cm); 2921 CookieList cookie_list = GetAllCookies(cm);
2458 ASSERT_EQ(1U, cookie_list.size()); 2922 ASSERT_EQ(1U, cookie_list.size());
2459 ASSERT_FALSE(cookie_list[0].IsPersistent()); 2923 ASSERT_FALSE(cookie_list[0].IsPersistent());
2460 2924
2461 // Use a past expiry date to delete the cookie, but force it to session only. 2925 // Use a past expiry date to delete the cookie, but force it to session only.
2462 ASSERT_TRUE(SetCookieWithOptions(cm, url_google_, 2926 ASSERT_TRUE(SetCookieWithOptions(
2927 cm, url_google_,
2463 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-1977 22:50:13 GMT", 2928 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-1977 22:50:13 GMT",
2464 options)); 2929 options));
2465 2930
2466 // Check that the cookie was deleted. 2931 // Check that the cookie was deleted.
2467 cookie_list = GetAllCookies(cm); 2932 cookie_list = GetAllCookies(cm);
2468 ASSERT_EQ(0U, cookie_list.size()); 2933 ASSERT_EQ(0U, cookie_list.size());
2469 } 2934 }
2470 2935
2471 // This test checks that keep expired cookies flag is working. 2936 // This test checks that keep expired cookies flag is working.
2472 TEST_F(CookieMonsterTest, KeepExpiredCookies) { 2937 TEST_F(CookieMonsterTest, KeepExpiredCookies) {
2473 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2938 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2474 cm->SetKeepExpiredCookies(); 2939 cm->SetKeepExpiredCookies();
2475 CookieOptions options; 2940 CookieOptions options;
2476 2941
2477 // Set a persistent cookie. 2942 // Set a persistent cookie.
2478 ASSERT_TRUE(SetCookieWithOptions(cm, url_google_, 2943 ASSERT_TRUE(SetCookieWithOptions(
2944 cm, url_google_,
2479 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT", 2945 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT",
2480 options)); 2946 options));
2481 2947
2482 // Get the canonical cookie. 2948 // Get the canonical cookie.
2483 CookieList cookie_list = GetAllCookies(cm); 2949 CookieList cookie_list = GetAllCookies(cm);
2484 ASSERT_EQ(1U, cookie_list.size()); 2950 ASSERT_EQ(1U, cookie_list.size());
2485 2951
2486 // Use a past expiry date to delete the cookie. 2952 // Use a past expiry date to delete the cookie.
2487 ASSERT_TRUE(SetCookieWithOptions(cm, url_google_, 2953 ASSERT_TRUE(SetCookieWithOptions(
2954 cm, url_google_,
2488 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-1977 22:50:13 GMT", 2955 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-1977 22:50:13 GMT",
2489 options)); 2956 options));
2490 2957
2491 // Check that the cookie with the past expiry date is still there. 2958 // Check that the cookie with the past expiry date is still there.
2492 // GetAllCookies() also triggers garbage collection. 2959 // GetAllCookies() also triggers garbage collection.
2493 cookie_list = GetAllCookies(cm); 2960 cookie_list = GetAllCookies(cm);
2494 ASSERT_EQ(1U, cookie_list.size()); 2961 ASSERT_EQ(1U, cookie_list.size());
2495 ASSERT_TRUE(cookie_list[0].IsExpired(Time::Now())); 2962 ASSERT_TRUE(cookie_list[0].IsExpired(Time::Now()));
2496 } 2963 }
2497 2964
2498 namespace { 2965 namespace {
2499 2966
2500 // Mock PersistentCookieStore that keeps track of the number of Flush() calls. 2967 // Mock PersistentCookieStore that keeps track of the number of Flush() calls.
2501 class FlushablePersistentStore : public CookieMonster::PersistentCookieStore { 2968 class FlushablePersistentStore : public CookieMonster::PersistentCookieStore {
2502 public: 2969 public:
2503 FlushablePersistentStore() : flush_count_(0) {} 2970 FlushablePersistentStore() : flush_count_(0) {}
2504 2971
2505 bool Load(std::vector<CookieMonster::CanonicalCookie*>*) { 2972 bool Load(const LoadedCallback& loaded_callback) {
2973 std::vector<CookieMonster::CanonicalCookie*> out_cookies;
2974 loaded_callback.Run(out_cookies);
2506 return false; 2975 return false;
2507 } 2976 }
2508 2977
2509 void AddCookie(const CookieMonster::CanonicalCookie&) {} 2978 void AddCookie(const CookieMonster::CanonicalCookie&) {}
2510 void UpdateCookieAccessTime(const CookieMonster::CanonicalCookie&) {} 2979 void UpdateCookieAccessTime(const CookieMonster::CanonicalCookie&) {}
2511 void DeleteCookie(const CookieMonster::CanonicalCookie&) {} 2980 void DeleteCookie(const CookieMonster::CanonicalCookie&) {}
2512 void SetClearLocalStateOnExit(bool clear_local_state) {} 2981 void SetClearLocalStateOnExit(bool clear_local_state) {}
2513 2982
2514 void Flush(Task* completion_callback) { 2983 void Flush(Task* completion_callback) {
2515 ++flush_count_; 2984 ++flush_count_;
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
2645 cm, GURL("http://fake.a.url"), "a", "b", "a.url", "/", 3114 cm, GURL("http://fake.a.url"), "a", "b", "a.url", "/",
2646 base::Time::Now() + base::TimeDelta::FromMinutes(59), 3115 base::Time::Now() + base::TimeDelta::FromMinutes(59),
2647 false, false)); 3116 false, false));
2648 3117
2649 base::Histogram::SampleSet histogram_set_2; 3118 base::Histogram::SampleSet histogram_set_2;
2650 expired_histogram->SnapshotSample(&histogram_set_2); 3119 expired_histogram->SnapshotSample(&histogram_set_2);
2651 EXPECT_EQ(histogram_set_1.TotalCount() + 1, 3120 EXPECT_EQ(histogram_set_1.TotalCount() + 1,
2652 histogram_set_2.TotalCount()); 3121 histogram_set_2.TotalCount());
2653 3122
2654 // kValidCookieLine creates a session cookie. 3123 // kValidCookieLine creates a session cookie.
2655 ASSERT_TRUE(cm->SetCookie(url_google_, kValidCookieLine)); 3124 ASSERT_TRUE(SetCookie(cm, url_google_, kValidCookieLine));
2656 expired_histogram->SnapshotSample(&histogram_set_1); 3125 expired_histogram->SnapshotSample(&histogram_set_1);
2657 EXPECT_EQ(histogram_set_2.TotalCount(), 3126 EXPECT_EQ(histogram_set_2.TotalCount(),
2658 histogram_set_1.TotalCount()); 3127 histogram_set_1.TotalCount());
2659 } 3128 }
2660 3129
2661 namespace { 3130 namespace {
2662 3131
2663 class MultiThreadedCookieMonsterTest : public CookieMonsterTest { 3132 class MultiThreadedCookieMonsterTest : public CookieMonsterTest {
2664 public: 3133 public:
2665 MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {} 3134 MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {}
2666 3135
2667 // Helper methods for calling the asynchronous CookieMonster methods 3136 // Helper methods for calling the asynchronous CookieMonster methods
2668 // from a different thread. 3137 // from a different thread.
2669 3138
2670 void GetCookiesTask(CookieMonster* cm, 3139 void GetCookiesTask(CookieMonster* cm,
2671 const GURL& url, 3140 const GURL& url,
2672 GetCookieStringCallback* callback) { 3141 GetCookieStringCallback* callback) {
2673 cm->GetCookiesAsync( 3142 cm->GetCookiesWithOptionsAsync(
2674 url, 3143 url, CookieOptions(),
2675 base::Bind(&GetCookieStringCallback::Run, base::Unretained(callback))); 3144 base::Bind(&GetCookieStringCallback::Run, base::Unretained(callback)));
2676 } 3145 }
2677 3146
2678 void GetCookiesWithOptionsTask(CookieMonster* cm, 3147 void GetCookiesWithOptionsTask(CookieMonster* cm,
2679 const GURL& url, 3148 const GURL& url,
2680 const CookieOptions& options, 3149 const CookieOptions& options,
2681 GetCookieStringCallback* callback) { 3150 GetCookieStringCallback* callback) {
2682 cm->GetCookiesWithOptionsAsync( 3151 cm->GetCookiesWithOptionsAsync(
2683 url, options, 3152 url, options,
2684 base::Bind(&GetCookieStringCallback::Run, base::Unretained(callback))); 3153 base::Bind(&GetCookieStringCallback::Run, base::Unretained(callback)));
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
2748 const std::string& cookie_name, 3217 const std::string& cookie_name,
2749 DeleteCookieCallback* callback) { 3218 DeleteCookieCallback* callback) {
2750 cm->DeleteCookieAsync( 3219 cm->DeleteCookieAsync(
2751 url, cookie_name, 3220 url, cookie_name,
2752 base::Bind(&DeleteCookieCallback::Run, base::Unretained(callback))); 3221 base::Bind(&DeleteCookieCallback::Run, base::Unretained(callback)));
2753 } 3222 }
2754 3223
2755 void DeleteAllCreatedBetweenTask(CookieMonster* cm, 3224 void DeleteAllCreatedBetweenTask(CookieMonster* cm,
2756 const base::Time& delete_begin, 3225 const base::Time& delete_begin,
2757 const base::Time& delete_end, 3226 const base::Time& delete_end,
2758 bool sync_to_store,
2759 DeleteCallback* callback) { 3227 DeleteCallback* callback) {
2760 cm->DeleteAllCreatedBetweenAsync( 3228 cm->DeleteAllCreatedBetweenAsync(
2761 delete_begin, delete_end, sync_to_store, 3229 delete_begin, delete_end,
2762 base::Bind(&DeleteCallback::Run, 3230 base::Bind(&DeleteCallback::Run,
2763 base::Unretained(callback))); 3231 base::Unretained(callback)));
2764 } 3232 }
2765 3233
2766 void DeleteAllForHostTask(CookieMonster* cm, 3234 void DeleteAllForHostTask(CookieMonster* cm,
2767 const GURL& url, 3235 const GURL& url,
2768 DeleteCallback* callback) { 3236 DeleteCallback* callback) {
2769 cm->DeleteAllForHostAsync( 3237 cm->DeleteAllForHostAsync(
2770 url, 3238 url,
2771 base::Bind(&DeleteCallback::Run, base::Unretained(callback))); 3239 base::Bind(&DeleteCallback::Run, base::Unretained(callback)));
(...skipping 19 matching lines...) Expand all
2791 }; 3259 };
2792 3260
2793 } // namespace 3261 } // namespace
2794 3262
2795 // TODO(ycxiao): Eventually, we will need to create a separate thread, create 3263 // TODO(ycxiao): Eventually, we will need to create a separate thread, create
2796 // the cookie monster on that thread (or at least its store, i.e., the DB 3264 // the cookie monster on that thread (or at least its store, i.e., the DB
2797 // thread). 3265 // thread).
2798 3266
2799 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookies) { 3267 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookies) {
2800 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 3268 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2801 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 3269 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2802 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 3270 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
2803 GetCookieStringCallback callback(&other_thread_); 3271 GetCookieStringCallback callback(&other_thread_);
2804 base::Closure task = base::Bind( 3272 base::Closure task = base::Bind(
2805 &net::MultiThreadedCookieMonsterTest::GetCookiesTask, 3273 &net::MultiThreadedCookieMonsterTest::GetCookiesTask,
2806 base::Unretained(this), 3274 base::Unretained(this),
2807 cm, url_google_, &callback); 3275 cm, url_google_, &callback);
2808 RunOnOtherThread(task); 3276 RunOnOtherThread(task);
2809 EXPECT_TRUE(callback.did_run()); 3277 EXPECT_TRUE(callback.did_run());
2810 EXPECT_EQ("A=B", callback.cookie()); 3278 EXPECT_EQ("A=B", callback.cookie());
2811 } 3279 }
2812 3280
2813 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookiesWithOptions) { 3281 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookiesWithOptions) {
2814 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 3282 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2815 CookieOptions options; 3283 CookieOptions options;
2816 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 3284 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2817 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options)); 3285 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options));
2818 GetCookieStringCallback callback(&other_thread_); 3286 GetCookieStringCallback callback(&other_thread_);
2819 base::Closure task = base::Bind( 3287 base::Closure task = base::Bind(
2820 &net::MultiThreadedCookieMonsterTest::GetCookiesWithOptionsTask, 3288 &net::MultiThreadedCookieMonsterTest::GetCookiesWithOptionsTask,
2821 base::Unretained(this), 3289 base::Unretained(this),
2822 cm, url_google_, options, &callback); 3290 cm, url_google_, options, &callback);
2823 RunOnOtherThread(task); 3291 RunOnOtherThread(task);
2824 EXPECT_TRUE(callback.did_run()); 3292 EXPECT_TRUE(callback.did_run());
2825 EXPECT_EQ("A=B", callback.cookie()); 3293 EXPECT_EQ("A=B", callback.cookie());
2826 } 3294 }
2827 3295
2828 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookiesWithInfo) { 3296 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookiesWithInfo) {
2829 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 3297 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2830 CookieOptions options; 3298 CookieOptions options;
2831 std::string cookie_line; 3299 std::string cookie_line;
2832 std::vector<CookieStore::CookieInfo> cookie_infos; 3300 std::vector<CookieStore::CookieInfo> cookie_infos;
2833 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 3301 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2834 GetCookiesWithInfo(cm, url_google_, options, &cookie_line, &cookie_infos); 3302 GetCookiesWithInfo(cm, url_google_, options, &cookie_line, &cookie_infos);
2835 EXPECT_EQ("A=B", cookie_line); 3303 EXPECT_EQ("A=B", cookie_line);
2836 EXPECT_EQ(1U, cookie_infos.size()); 3304 EXPECT_EQ(1U, cookie_infos.size());
2837 EXPECT_EQ("A", cookie_infos[0].name); 3305 EXPECT_EQ("A", cookie_infos[0].name);
2838 EXPECT_EQ("", cookie_infos[0].mac_key); 3306 EXPECT_EQ("", cookie_infos[0].mac_key);
2839 EXPECT_EQ("", cookie_infos[0].mac_algorithm); 3307 EXPECT_EQ("", cookie_infos[0].mac_algorithm);
2840 GetCookiesWithInfoCallback callback(&other_thread_); 3308 GetCookiesWithInfoCallback callback(&other_thread_);
2841 base::Closure task = 3309 base::Closure task =
2842 base::Bind(&net::MultiThreadedCookieMonsterTest::GetCookiesWithInfoTask, 3310 base::Bind(&net::MultiThreadedCookieMonsterTest::GetCookiesWithInfoTask,
2843 base::Unretained(this), 3311 base::Unretained(this),
2844 cm, url_google_, options, &callback); 3312 cm, url_google_, options, &callback);
2845 RunOnOtherThread(task); 3313 RunOnOtherThread(task);
2846 EXPECT_TRUE(callback.did_run()); 3314 EXPECT_TRUE(callback.did_run());
2847 EXPECT_EQ("A=B", callback.cookie_line()); 3315 EXPECT_EQ("A=B", callback.cookie_line());
2848 EXPECT_EQ(1U, callback.cookie_info().size()); 3316 EXPECT_EQ(1U, callback.cookie_info().size());
2849 EXPECT_EQ("A", callback.cookie_info()[0].name); 3317 EXPECT_EQ("A", callback.cookie_info()[0].name);
2850 EXPECT_EQ("", callback.cookie_info()[0].mac_key); 3318 EXPECT_EQ("", callback.cookie_info()[0].mac_key);
2851 EXPECT_EQ("", callback.cookie_info()[0].mac_algorithm); 3319 EXPECT_EQ("", callback.cookie_info()[0].mac_algorithm);
2852 } 3320 }
2853 3321
2854 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookies) { 3322 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookies) {
2855 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 3323 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2856 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 3324 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2857 CookieList cookies = GetAllCookies(cm); 3325 CookieList cookies = GetAllCookies(cm);
2858 CookieList::const_iterator it = cookies.begin(); 3326 CookieList::const_iterator it = cookies.begin();
2859 ASSERT_TRUE(it != cookies.end()); 3327 ASSERT_TRUE(it != cookies.end());
2860 EXPECT_EQ("www.google.izzle", it->Domain()); 3328 EXPECT_EQ("www.google.izzle", it->Domain());
2861 EXPECT_EQ("A", it->Name()); 3329 EXPECT_EQ("A", it->Name());
2862 ASSERT_TRUE(++it == cookies.end()); 3330 ASSERT_TRUE(++it == cookies.end());
2863 GetCookieListCallback callback(&other_thread_); 3331 GetCookieListCallback callback(&other_thread_);
2864 base::Closure task = 3332 base::Closure task =
2865 base::Bind(&net::MultiThreadedCookieMonsterTest::GetAllCookiesTask, 3333 base::Bind(&net::MultiThreadedCookieMonsterTest::GetAllCookiesTask,
2866 base::Unretained(this), 3334 base::Unretained(this),
2867 cm, &callback); 3335 cm, &callback);
2868 RunOnOtherThread(task); 3336 RunOnOtherThread(task);
2869 EXPECT_TRUE(callback.did_run()); 3337 EXPECT_TRUE(callback.did_run());
2870 it = callback.cookies().begin(); 3338 it = callback.cookies().begin();
2871 ASSERT_TRUE(it != callback.cookies().end()); 3339 ASSERT_TRUE(it != callback.cookies().end());
2872 EXPECT_EQ("www.google.izzle", it->Domain()); 3340 EXPECT_EQ("www.google.izzle", it->Domain());
2873 EXPECT_EQ("A", it->Name()); 3341 EXPECT_EQ("A", it->Name());
2874 ASSERT_TRUE(++it == callback.cookies().end()); 3342 ASSERT_TRUE(++it == callback.cookies().end());
2875 } 3343 }
2876 3344
2877 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURL) { 3345 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURL) {
2878 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 3346 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2879 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 3347 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2880 CookieList cookies = GetAllCookiesForURL(cm, url_google_); 3348 CookieList cookies = GetAllCookiesForURL(cm, url_google_);
2881 CookieList::const_iterator it = cookies.begin(); 3349 CookieList::const_iterator it = cookies.begin();
2882 ASSERT_TRUE(it != cookies.end()); 3350 ASSERT_TRUE(it != cookies.end());
2883 EXPECT_EQ("www.google.izzle", it->Domain()); 3351 EXPECT_EQ("www.google.izzle", it->Domain());
2884 EXPECT_EQ("A", it->Name()); 3352 EXPECT_EQ("A", it->Name());
2885 ASSERT_TRUE(++it == cookies.end()); 3353 ASSERT_TRUE(++it == cookies.end());
2886 GetCookieListCallback callback(&other_thread_); 3354 GetCookieListCallback callback(&other_thread_);
2887 base::Closure task = 3355 base::Closure task =
2888 base::Bind(&net::MultiThreadedCookieMonsterTest::GetAllCookiesForURLTask, 3356 base::Bind(&net::MultiThreadedCookieMonsterTest::GetAllCookiesForURLTask,
2889 base::Unretained(this), 3357 base::Unretained(this),
2890 cm, url_google_, &callback); 3358 cm, url_google_, &callback);
2891 RunOnOtherThread(task); 3359 RunOnOtherThread(task);
2892 EXPECT_TRUE(callback.did_run()); 3360 EXPECT_TRUE(callback.did_run());
2893 it = callback.cookies().begin(); 3361 it = callback.cookies().begin();
2894 ASSERT_TRUE(it != callback.cookies().end()); 3362 ASSERT_TRUE(it != callback.cookies().end());
2895 EXPECT_EQ("www.google.izzle", it->Domain()); 3363 EXPECT_EQ("www.google.izzle", it->Domain());
2896 EXPECT_EQ("A", it->Name()); 3364 EXPECT_EQ("A", it->Name());
2897 ASSERT_TRUE(++it == callback.cookies().end()); 3365 ASSERT_TRUE(++it == callback.cookies().end());
2898 } 3366 }
2899 3367
2900 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURLWithOpt) { 3368 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURLWithOpt) {
2901 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 3369 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2902 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 3370 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2903 CookieOptions options; 3371 CookieOptions options;
2904 CookieList cookies = 3372 CookieList cookies =
2905 GetAllCookiesForURLWithOptions(cm, url_google_, options); 3373 GetAllCookiesForURLWithOptions(cm, url_google_, options);
2906 CookieList::const_iterator it = cookies.begin(); 3374 CookieList::const_iterator it = cookies.begin();
2907 ASSERT_TRUE(it != cookies.end()); 3375 ASSERT_TRUE(it != cookies.end());
2908 EXPECT_EQ("www.google.izzle", it->Domain()); 3376 EXPECT_EQ("www.google.izzle", it->Domain());
2909 EXPECT_EQ("A", it->Name()); 3377 EXPECT_EQ("A", it->Name());
2910 ASSERT_TRUE(++it == cookies.end()); 3378 ASSERT_TRUE(++it == cookies.end());
2911 GetCookieListCallback callback(&other_thread_); 3379 GetCookieListCallback callback(&other_thread_);
2912 base::Closure task = base::Bind( 3380 base::Closure task = base::Bind(
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2966 RunOnOtherThread(task); 3434 RunOnOtherThread(task);
2967 EXPECT_TRUE(callback.did_run()); 3435 EXPECT_TRUE(callback.did_run());
2968 } 3436 }
2969 3437
2970 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllCreatedBetween) { 3438 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllCreatedBetween) {
2971 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 3439 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2972 CookieOptions options; 3440 CookieOptions options;
2973 Time now = Time::Now(); 3441 Time now = Time::Now();
2974 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options)); 3442 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options));
2975 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99), 3443 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99),
2976 Time(), false)); 3444 Time()));
2977 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options)); 3445 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options));
2978 DeleteCallback callback(&other_thread_); 3446 DeleteCallback callback(&other_thread_);
2979 base::Closure task = base::Bind( 3447 base::Closure task = base::Bind(
2980 &net::MultiThreadedCookieMonsterTest::DeleteAllCreatedBetweenTask, 3448 &net::MultiThreadedCookieMonsterTest::DeleteAllCreatedBetweenTask,
2981 base::Unretained(this), 3449 base::Unretained(this),
2982 cm, now - TimeDelta::FromDays(99), 3450 cm, now - TimeDelta::FromDays(99),
2983 Time(), false, &callback); 3451 Time(), &callback);
2984 RunOnOtherThread(task); 3452 RunOnOtherThread(task);
2985 EXPECT_TRUE(callback.did_run()); 3453 EXPECT_TRUE(callback.did_run());
2986 EXPECT_EQ(1, callback.num_deleted()); 3454 EXPECT_EQ(1, callback.num_deleted());
2987 } 3455 }
2988 3456
2989 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllForHost) { 3457 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllForHost) {
2990 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 3458 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2991 CookieOptions options; 3459 CookieOptions options;
2992 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options)); 3460 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options));
2993 EXPECT_EQ(1, DeleteAllForHost(cm, url_google_)); 3461 EXPECT_EQ(1, DeleteAllForHost(cm, url_google_));
(...skipping 23 matching lines...) Expand all
3017 base::Closure task = base::Bind( 3485 base::Closure task = base::Bind(
3018 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask, 3486 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask,
3019 base::Unretained(this), 3487 base::Unretained(this),
3020 cm, *it, &callback); 3488 cm, *it, &callback);
3021 RunOnOtherThread(task); 3489 RunOnOtherThread(task);
3022 EXPECT_TRUE(callback.did_run()); 3490 EXPECT_TRUE(callback.did_run());
3023 EXPECT_TRUE(callback.result()); 3491 EXPECT_TRUE(callback.result());
3024 } 3492 }
3025 3493
3026 } // namespace net 3494 } // namespace net
OLDNEW
« no previous file with comments | « net/base/cookie_monster_store_test.cc ('k') | net/base/cookie_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698