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

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

Issue 7598001: Remove the old synchronous CookieMonster API. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 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"
(...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 url_google_bar_(kUrlGoogleBar), 492 url_google_bar_(kUrlGoogleBar),
493 message_loop_factory_(MessageLoop::current()) {} 493 message_loop_factory_(MessageLoop::current()) {}
494 494
495 // Helper methods for the asynchronous Cookie Store API that call the 495 // Helper methods for the asynchronous Cookie Store API that call the
496 // asynchronous method and then pump the loop until the callback is invoked, 496 // asynchronous method and then pump the loop until the callback is invoked,
497 // finally returning the value. 497 // finally returning the value.
498 498
499 std::string GetCookies(CookieMonster* cm, const GURL& url) { 499 std::string GetCookies(CookieMonster* cm, const GURL& url) {
500 DCHECK(cm); 500 DCHECK(cm);
501 GetCookieStringCallback callback; 501 GetCookieStringCallback callback;
502 cm->GetCookiesAsync( 502 cm->GetCookiesWithOptionsAsync(
503 url, base::Bind(&GetCookieStringCallback::Run, 503 url, CookieOptions(), base::Bind(&GetCookieStringCallback::Run,
504 base::Unretained(&callback))); 504 base::Unretained(&callback)));
505 RunFor(kTimeout); 505 RunFor(kTimeout);
506 EXPECT_TRUE(callback.did_run()); 506 EXPECT_TRUE(callback.did_run());
507 return callback.cookie(); 507 return callback.cookie();
508 } 508 }
509 509
510 std::string GetCookiesWithOptions(CookieMonster* cm, 510 std::string GetCookiesWithOptions(CookieMonster* cm,
511 const GURL& url, 511 const GURL& url,
512 const CookieOptions& options) { 512 const CookieOptions& options) {
513 DCHECK(cm); 513 DCHECK(cm);
514 GetCookieStringCallback callback; 514 GetCookieStringCallback callback;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 DCHECK(cm); 582 DCHECK(cm);
583 SetCookieCallback callback; 583 SetCookieCallback callback;
584 cm->SetCookieWithOptionsAsync( 584 cm->SetCookieWithOptionsAsync(
585 url, cookie_line, options, 585 url, cookie_line, options,
586 base::Bind(&SetCookieCallback::Run, base::Unretained(&callback))); 586 base::Bind(&SetCookieCallback::Run, base::Unretained(&callback)));
587 RunFor(kTimeout); 587 RunFor(kTimeout);
588 EXPECT_TRUE(callback.did_run()); 588 EXPECT_TRUE(callback.did_run());
589 return callback.result(); 589 return callback.result();
590 } 590 }
591 591
592 bool SetCookie(CookieMonster* cm,
593 const GURL& url,
594 const std::string& cookie_line) {
595 CookieOptions options;
596 return SetCookieWithOptions(cm, url, cookie_line, options);
597 }
598
592 bool SetCookieWithDetails(CookieMonster* cm, 599 bool SetCookieWithDetails(CookieMonster* cm,
593 const GURL& url, 600 const GURL& url,
594 const std::string& name, 601 const std::string& name,
595 const std::string& value, 602 const std::string& value,
596 const std::string& domain, 603 const std::string& domain,
597 const std::string& path, 604 const std::string& path,
598 const base::Time& expiration_time, 605 const base::Time& expiration_time,
599 bool secure, bool http_only) { 606 bool secure, bool http_only) {
600 DCHECK(cm); 607 DCHECK(cm);
601 SetCookieCallback callback; 608 SetCookieCallback callback;
(...skipping 10 matching lines...) Expand all
612 const std::string& cookie_name) { 619 const std::string& cookie_name) {
613 DCHECK(cm); 620 DCHECK(cm);
614 DeleteCookieCallback callback; 621 DeleteCookieCallback callback;
615 cm->DeleteCookieAsync(url, cookie_name, 622 cm->DeleteCookieAsync(url, cookie_name,
616 base::Bind(&DeleteCookieCallback::Run, 623 base::Bind(&DeleteCookieCallback::Run,
617 base::Unretained(&callback))); 624 base::Unretained(&callback)));
618 RunFor(kTimeout); 625 RunFor(kTimeout);
619 EXPECT_TRUE(callback.did_run()); 626 EXPECT_TRUE(callback.did_run());
620 } 627 }
621 628
629 int DeleteAll(CookieMonster*cm) {
630 DCHECK(cm);
631 DeleteCallback callback;
632 cm->DeleteAllAsync(
633 base::Bind(&DeleteCallback::Run, base::Unretained(&callback)));
634 RunFor(kTimeout);
635 EXPECT_TRUE(callback.did_run());
636 return callback.num_deleted();
637 }
638
622 int DeleteAllCreatedBetween(CookieMonster*cm, 639 int DeleteAllCreatedBetween(CookieMonster*cm,
623 const base::Time& delete_begin, 640 const base::Time& delete_begin,
624 const base::Time& delete_end, 641 const base::Time& delete_end) {
625 bool sync_to_store) {
626 DCHECK(cm); 642 DCHECK(cm);
627 DeleteCallback callback; 643 DeleteCallback callback;
628 cm->DeleteAllCreatedBetweenAsync( 644 cm->DeleteAllCreatedBetweenAsync(
629 delete_begin, delete_end, sync_to_store, 645 delete_begin, delete_end,
630 base::Bind(&DeleteCallback::Run, base::Unretained(&callback))); 646 base::Bind(&DeleteCallback::Run, base::Unretained(&callback)));
631 RunFor(kTimeout); 647 RunFor(kTimeout);
632 EXPECT_TRUE(callback.did_run()); 648 EXPECT_TRUE(callback.did_run());
633 return callback.num_deleted(); 649 return callback.num_deleted();
634 } 650 }
635 651
636 int DeleteAllForHost(CookieMonster*cm, 652 int DeleteAllForHost(CookieMonster*cm,
637 const GURL& url) { 653 const GURL& url) {
638 DCHECK(cm); 654 DCHECK(cm);
639 DeleteCallback callback; 655 DeleteCallback callback;
(...skipping 28 matching lines...) Expand all
668 684
669 // Helper for DeleteAllForHost test; repopulates CM with same layout 685 // Helper for DeleteAllForHost test; repopulates CM with same layout
670 // each time. 686 // each time.
671 void PopulateCmForDeleteAllForHost(scoped_refptr<CookieMonster> cm) { 687 void PopulateCmForDeleteAllForHost(scoped_refptr<CookieMonster> cm) {
672 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1); 688 GURL url_top_level_domain_plus_1(kTopLevelDomainPlus1);
673 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2); 689 GURL url_top_level_domain_plus_2(kTopLevelDomainPlus2);
674 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure); 690 GURL url_top_level_domain_plus_2_secure(kTopLevelDomainPlus2Secure);
675 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3); 691 GURL url_top_level_domain_plus_3(kTopLevelDomainPlus3);
676 GURL url_other(kOtherDomain); 692 GURL url_other(kOtherDomain);
677 693
678 cm->DeleteAll(true); 694 DeleteAll(cm);
679 695
680 // Static population for probe: 696 // Static population for probe:
681 // * Three levels of domain cookie (.b.a, .c.b.a, .d.c.b.a) 697 // * 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) 698 // * 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) 699 // * http_only cookie (w.c.b.a)
684 // * Two secure cookies (.c.b.a, w.c.b.a) 700 // * Two secure cookies (.c.b.a, w.c.b.a)
685 // * Two domain path cookies (.c.b.a/dir1, .c.b.a/dir1/dir2) 701 // * 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) 702 // * Two host path cookies (w.c.b.a/dir1, w.c.b.a/dir1/dir2)
687 703
688 // Domain cookies 704 // Domain cookies
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 int domain_purge_cookies, 787 int domain_purge_cookies,
772 CookieMonster::ExpiryAndKeyScheme key_scheme) { 788 CookieMonster::ExpiryAndKeyScheme key_scheme) {
773 const int more_than_enough_cookies = 789 const int more_than_enough_cookies =
774 (domain_max_cookies + domain_purge_cookies) * 2; 790 (domain_max_cookies + domain_purge_cookies) * 2;
775 // Add a bunch of cookies on a single host, should purge them. 791 // Add a bunch of cookies on a single host, should purge them.
776 { 792 {
777 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 793 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
778 cm->SetExpiryAndKeyScheme(key_scheme); 794 cm->SetExpiryAndKeyScheme(key_scheme);
779 for (int i = 0; i < more_than_enough_cookies; ++i) { 795 for (int i = 0; i < more_than_enough_cookies; ++i) {
780 std::string cookie = base::StringPrintf("a%03d=b", i); 796 std::string cookie = base::StringPrintf("a%03d=b", i);
781 EXPECT_TRUE(cm->SetCookie(url_google_, cookie)); 797 EXPECT_TRUE(SetCookie(cm, url_google_, cookie));
782 std::string cookies = this->GetCookies(cm, url_google_); 798 std::string cookies = this->GetCookies(cm, url_google_);
783 // Make sure we find it in the cookies. 799 // Make sure we find it in the cookies.
784 EXPECT_NE(cookies.find(cookie), std::string::npos); 800 EXPECT_NE(cookies.find(cookie), std::string::npos);
785 // Count the number of cookies. 801 // Count the number of cookies.
786 EXPECT_LE(CountInString(cookies, '='), domain_max_cookies); 802 EXPECT_LE(CountInString(cookies, '='), domain_max_cookies);
787 } 803 }
788 } 804 }
789 805
790 // Add a bunch of cookies on multiple hosts within a single eTLD. 806 // Add a bunch of cookies on multiple hosts within a single eTLD.
791 // Should keep at least kDomainMaxCookies - kDomainPurgeCookies 807 // Should keep at least kDomainMaxCookies - kDomainPurgeCookies
792 // between them. If we are using the effective domain keying system 808 // between them. If we are using the effective domain keying system
793 // (EKS_KEEP_RECENT_AND_PURGE_ETLDP1) we shouldn't go above 809 // (EKS_KEEP_RECENT_AND_PURGE_ETLDP1) we shouldn't go above
794 // kDomainMaxCookies for both together. If we're using the domain 810 // kDomainMaxCookies for both together. If we're using the domain
795 // keying system (EKS_DISCARD_RECENT_AND_PURGE_DOMAIN), each 811 // keying system (EKS_DISCARD_RECENT_AND_PURGE_DOMAIN), each
796 // individual domain shouldn't go above kDomainMaxCookies. 812 // individual domain shouldn't go above kDomainMaxCookies.
797 GURL url_google_specific(kUrlGoogleSpecific); 813 GURL url_google_specific(kUrlGoogleSpecific);
798 { 814 {
799 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 815 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
800 cm->SetExpiryAndKeyScheme(key_scheme); 816 cm->SetExpiryAndKeyScheme(key_scheme);
801 for (int i = 0; i < more_than_enough_cookies; ++i) { 817 for (int i = 0; i < more_than_enough_cookies; ++i) {
802 std::string cookie_general = base::StringPrintf("a%03d=b", i); 818 std::string cookie_general = base::StringPrintf("a%03d=b", i);
803 EXPECT_TRUE(cm->SetCookie(url_google_, cookie_general)); 819 EXPECT_TRUE(SetCookie(cm, url_google_, cookie_general));
804 std::string cookie_specific = base::StringPrintf("c%03d=b", i); 820 std::string cookie_specific = base::StringPrintf("c%03d=b", i);
805 EXPECT_TRUE(cm->SetCookie(url_google_specific, cookie_specific)); 821 EXPECT_TRUE(SetCookie(cm, url_google_specific, cookie_specific));
806 std::string cookies_general = this->GetCookies(cm, url_google_); 822 std::string cookies_general = this->GetCookies(cm, url_google_);
807 EXPECT_NE(cookies_general.find(cookie_general), std::string::npos); 823 EXPECT_NE(cookies_general.find(cookie_general), std::string::npos);
808 std::string cookies_specific = 824 std::string cookies_specific =
809 this->GetCookies(cm, url_google_specific); 825 this->GetCookies(cm, url_google_specific);
810 EXPECT_NE(cookies_specific.find(cookie_specific), std::string::npos); 826 EXPECT_NE(cookies_specific.find(cookie_specific), std::string::npos);
811 if (key_scheme == CookieMonster::EKS_KEEP_RECENT_AND_PURGE_ETLDP1) { 827 if (key_scheme == CookieMonster::EKS_KEEP_RECENT_AND_PURGE_ETLDP1) {
812 EXPECT_LE((CountInString(cookies_general, '=') + 828 EXPECT_LE((CountInString(cookies_general, '=') +
813 CountInString(cookies_specific, '=')), 829 CountInString(cookies_specific, '=')),
814 domain_max_cookies); 830 domain_max_cookies);
815 } else { 831 } else {
(...skipping 17 matching lines...) Expand all
833 EXPECT_GE(general_cookies, 849 EXPECT_GE(general_cookies,
834 domain_max_cookies - domain_purge_cookies); 850 domain_max_cookies - domain_purge_cookies);
835 EXPECT_LE(general_cookies, domain_max_cookies); 851 EXPECT_LE(general_cookies, domain_max_cookies);
836 EXPECT_GE(specific_cookies, 852 EXPECT_GE(specific_cookies,
837 domain_max_cookies - domain_purge_cookies); 853 domain_max_cookies - domain_purge_cookies);
838 EXPECT_LE(specific_cookies, domain_max_cookies); 854 EXPECT_LE(specific_cookies, domain_max_cookies);
839 } 855 }
840 } 856 }
841 } 857 }
842 858
859 // Function for creating a CM with a number of cookies in it,
860 // no store (and hence no ability to affect access time).
861 CookieMonster* CreateMonsterForGC(int num_cookies) {
862 CookieMonster* cm(new CookieMonster(NULL, NULL));
863 for (int i = 0; i < num_cookies; i++) {
864 SetCookie(cm, GURL(StringPrintf("http://h%05d.izzle", i)), "a=1");
865 }
866 return cm;
867 }
868
843 protected: 869 protected:
844 GURL url_google_; 870 GURL url_google_;
845 GURL url_google_secure_; 871 GURL url_google_secure_;
846 GURL url_google_foo_; 872 GURL url_google_foo_;
847 GURL url_google_bar_; 873 GURL url_google_bar_;
848 874
849 ScopedRunnableMethodFactory<MessageLoop> message_loop_factory_; 875 ScopedRunnableMethodFactory<MessageLoop> message_loop_factory_;
850 }; 876 };
851 877
852 } // namespace 878 } // namespace
853 879
854 TEST_F(CookieMonsterTest, DomainTest) { 880 TEST_F(CookieMonsterTest, DomainTest) {
855 scoped_refptr<MockPersistentCookieStore> store( 881 scoped_refptr<MockPersistentCookieStore> store(
856 new MockPersistentCookieStore); 882 new MockPersistentCookieStore);
857 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 883 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
858 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 884 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
859 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 885 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
860 EXPECT_TRUE(cm->SetCookie(url_google_, "C=D; domain=.google.izzle")); 886 EXPECT_TRUE(SetCookie(cm, url_google_, "C=D; domain=.google.izzle"));
861 EXPECT_EQ("A=B; C=D", GetCookies(cm, url_google_)); 887 EXPECT_EQ("A=B; C=D", GetCookies(cm, url_google_));
862 888
863 // Verify that A=B was set as a host cookie rather than a domain 889 // 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. 890 // cookie -- should not be accessible from a sub sub-domain.
865 EXPECT_EQ("C=D", GetCookies(cm, GURL("http://foo.www.google.izzle"))); 891 EXPECT_EQ("C=D", GetCookies(cm, GURL("http://foo.www.google.izzle")));
866 892
867 // Test and make sure we find domain cookies on the same domain. 893 // 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")); 894 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_)); 895 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_));
870 896
871 // Test setting a domain= that doesn't start w/ a dot, should 897 // 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. 898 // 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")); 899 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_)); 900 EXPECT_EQ("A=B; C=D; E=F; G=H", GetCookies(cm, url_google_));
875 901
876 // Test domain enforcement, should fail on a sub-domain or something too deep. 902 // Test domain enforcement, should fail on a sub-domain or something too deep.
877 EXPECT_FALSE(cm->SetCookie(url_google_, "I=J; domain=.izzle")); 903 EXPECT_FALSE(SetCookie(cm, url_google_, "I=J; domain=.izzle"));
878 EXPECT_EQ("", GetCookies(cm, GURL("http://a.izzle"))); 904 EXPECT_EQ("", GetCookies(cm, GURL("http://a.izzle")));
879 EXPECT_FALSE(cm->SetCookie(url_google_, "K=L; domain=.bla.www.google.izzle")); 905 EXPECT_FALSE(SetCookie(cm, url_google_, "K=L; domain=.bla.www.google.izzle"));
880 EXPECT_EQ("C=D; E=F; G=H", 906 EXPECT_EQ("C=D; E=F; G=H",
881 GetCookies(cm, GURL("http://bla.www.google.izzle"))); 907 GetCookies(cm, GURL("http://bla.www.google.izzle")));
882 EXPECT_EQ("A=B; C=D; E=F; G=H", GetCookies(cm, url_google_)); 908 EXPECT_EQ("A=B; C=D; E=F; G=H", GetCookies(cm, url_google_));
883 909
884 // Nothing was persisted to the backing store. 910 // Nothing was persisted to the backing store.
885 EXPECT_EQ(0u, store->commands().size()); 911 EXPECT_EQ(0u, store->commands().size());
886 } 912 }
887 913
888 // FireFox recognizes domains containing trailing periods as valid. 914 // FireFox recognizes domains containing trailing periods as valid.
889 // IE and Safari do not. Assert the expected policy here. 915 // IE and Safari do not. Assert the expected policy here.
890 TEST_F(CookieMonsterTest, DomainWithTrailingDotTest) { 916 TEST_F(CookieMonsterTest, DomainWithTrailingDotTest) {
891 scoped_refptr<MockPersistentCookieStore> store( 917 scoped_refptr<MockPersistentCookieStore> store(
892 new MockPersistentCookieStore); 918 new MockPersistentCookieStore);
893 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 919 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
894 EXPECT_FALSE(cm->SetCookie(url_google_, "a=1; domain=.www.google.com.")); 920 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..")); 921 EXPECT_FALSE(SetCookie(cm, url_google_, "b=2; domain=.www.google.com.."));
896 EXPECT_EQ("", GetCookies(cm, url_google_)); 922 EXPECT_EQ("", GetCookies(cm, url_google_));
897 923
898 // Nothing was persisted to the backing store. 924 // Nothing was persisted to the backing store.
899 EXPECT_EQ(0u, store->commands().size()); 925 EXPECT_EQ(0u, store->commands().size());
900 } 926 }
901 927
902 // Test that cookies can bet set on higher level domains. 928 // Test that cookies can bet set on higher level domains.
903 // http://b/issue?id=896491 929 // http://b/issue?id=896491
904 TEST_F(CookieMonsterTest, ValidSubdomainTest) { 930 TEST_F(CookieMonsterTest, ValidSubdomainTest) {
905 scoped_refptr<MockPersistentCookieStore> store( 931 scoped_refptr<MockPersistentCookieStore> store(
906 new MockPersistentCookieStore); 932 new MockPersistentCookieStore);
907 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 933 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
908 GURL url_abcd("http://a.b.c.d.com"); 934 GURL url_abcd("http://a.b.c.d.com");
909 GURL url_bcd("http://b.c.d.com"); 935 GURL url_bcd("http://b.c.d.com");
910 GURL url_cd("http://c.d.com"); 936 GURL url_cd("http://c.d.com");
911 GURL url_d("http://d.com"); 937 GURL url_d("http://d.com");
912 938
913 EXPECT_TRUE(cm->SetCookie(url_abcd, "a=1; domain=.a.b.c.d.com")); 939 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")); 940 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")); 941 EXPECT_TRUE(SetCookie(cm, url_abcd, "c=3; domain=.c.d.com"));
916 EXPECT_TRUE(cm->SetCookie(url_abcd, "d=4; domain=.d.com")); 942 EXPECT_TRUE(SetCookie(cm, url_abcd, "d=4; domain=.d.com"));
917 943
918 EXPECT_EQ("a=1; b=2; c=3; d=4", GetCookies(cm, url_abcd)); 944 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)); 945 EXPECT_EQ("b=2; c=3; d=4", GetCookies(cm, url_bcd));
920 EXPECT_EQ("c=3; d=4", GetCookies(cm, url_cd)); 946 EXPECT_EQ("c=3; d=4", GetCookies(cm, url_cd));
921 EXPECT_EQ("d=4", GetCookies(cm, url_d)); 947 EXPECT_EQ("d=4", GetCookies(cm, url_d));
922 948
923 // Check that the same cookie can exist on different sub-domains. 949 // 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")); 950 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")); 951 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)); 952 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)); 953 EXPECT_EQ("c=3; d=4; X=cd", GetCookies(cm, url_cd));
928 954
929 // Nothing was persisted to the backing store. 955 // Nothing was persisted to the backing store.
930 EXPECT_EQ(0u, store->commands().size()); 956 EXPECT_EQ(0u, store->commands().size());
931 } 957 }
932 958
933 // Test that setting a cookie which specifies an invalid domain has 959 // 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 960 // no side-effect. An invalid domain in this context is one which does
935 // not match the originating domain. 961 // not match the originating domain.
936 // http://b/issue?id=896472 962 // http://b/issue?id=896472
937 TEST_F(CookieMonsterTest, InvalidDomainTest) { 963 TEST_F(CookieMonsterTest, InvalidDomainTest) {
938 { 964 {
939 scoped_refptr<MockPersistentCookieStore> store( 965 scoped_refptr<MockPersistentCookieStore> store(
940 new MockPersistentCookieStore); 966 new MockPersistentCookieStore);
941 967
942 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 968 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
943 GURL url_foobar("http://foo.bar.com"); 969 GURL url_foobar("http://foo.bar.com");
944 970
945 // More specific sub-domain than allowed. 971 // More specific sub-domain than allowed.
946 EXPECT_FALSE(cm->SetCookie(url_foobar, "a=1; domain=.yo.foo.bar.com")); 972 EXPECT_FALSE(SetCookie(cm, url_foobar, "a=1; domain=.yo.foo.bar.com"));
947 973
948 EXPECT_FALSE(cm->SetCookie(url_foobar, "b=2; domain=.foo.com")); 974 EXPECT_FALSE(SetCookie(cm, url_foobar, "b=2; domain=.foo.com"));
949 EXPECT_FALSE(cm->SetCookie(url_foobar, "c=3; domain=.bar.foo.com")); 975 EXPECT_FALSE(SetCookie(cm, url_foobar, "c=3; domain=.bar.foo.com"));
950 976
951 // Different TLD, but the rest is a substring. 977 // Different TLD, but the rest is a substring.
952 EXPECT_FALSE(cm->SetCookie(url_foobar, "d=4; domain=.foo.bar.com.net")); 978 EXPECT_FALSE(SetCookie(cm, url_foobar, "d=4; domain=.foo.bar.com.net"));
953 979
954 // A substring that isn't really a parent domain. 980 // A substring that isn't really a parent domain.
955 EXPECT_FALSE(cm->SetCookie(url_foobar, "e=5; domain=ar.com")); 981 EXPECT_FALSE(SetCookie(cm, url_foobar, "e=5; domain=ar.com"));
956 982
957 // Completely invalid domains: 983 // Completely invalid domains:
958 EXPECT_FALSE(cm->SetCookie(url_foobar, "f=6; domain=.")); 984 EXPECT_FALSE(SetCookie(cm, url_foobar, "f=6; domain=."));
959 EXPECT_FALSE(cm->SetCookie(url_foobar, "g=7; domain=/")); 985 EXPECT_FALSE(SetCookie(cm, url_foobar, "g=7; domain=/"));
960 EXPECT_FALSE(cm->SetCookie(url_foobar, "h=8; domain=http://foo.bar.com")); 986 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")); 987 EXPECT_FALSE(SetCookie(cm, url_foobar, "i=9; domain=..foo.bar.com"));
962 EXPECT_FALSE(cm->SetCookie(url_foobar, "j=10; domain=..bar.com")); 988 EXPECT_FALSE(SetCookie(cm, url_foobar, "j=10; domain=..bar.com"));
963 989
964 // Make sure there isn't something quirky in the domain canonicalization 990 // Make sure there isn't something quirky in the domain canonicalization
965 // that supports full URL semantics. 991 // that supports full URL semantics.
966 EXPECT_FALSE(cm->SetCookie(url_foobar, "k=11; domain=.foo.bar.com?blah")); 992 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")); 993 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")); 994 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:")); 995 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")); 996 EXPECT_FALSE(SetCookie(cm, url_foobar, "o=15; domain=.foo.bar.com#sup"));
971 997
972 EXPECT_EQ("", GetCookies(cm, url_foobar)); 998 EXPECT_EQ("", GetCookies(cm, url_foobar));
973 999
974 // Nothing was persisted to the backing store. 1000 // Nothing was persisted to the backing store.
975 EXPECT_EQ(0u, store->commands().size()); 1001 EXPECT_EQ(0u, store->commands().size());
976 } 1002 }
977 1003
978 { 1004 {
979 // Make sure the cookie code hasn't gotten its subdomain string handling 1005 // 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 1006 // reversed, missed a suffix check, etc. It's important here that the two
981 // hosts below have the same domain + registry. 1007 // hosts below have the same domain + registry.
982 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1008 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
983 GURL url_foocom("http://foo.com.com"); 1009 GURL url_foocom("http://foo.com.com");
984 EXPECT_FALSE(cm->SetCookie(url_foocom, "a=1; domain=.foo.com.com.com")); 1010 EXPECT_FALSE(SetCookie(cm, url_foocom, "a=1; domain=.foo.com.com.com"));
985 EXPECT_EQ("", GetCookies(cm, url_foocom)); 1011 EXPECT_EQ("", GetCookies(cm, url_foocom));
986 } 1012 }
987 } 1013 }
988 1014
989 // Test the behavior of omitting dot prefix from domain, should 1015 // Test the behavior of omitting dot prefix from domain, should
990 // function the same as FireFox. 1016 // function the same as FireFox.
991 // http://b/issue?id=889898 1017 // http://b/issue?id=889898
992 TEST_F(CookieMonsterTest, DomainWithoutLeadingDotTest) { 1018 TEST_F(CookieMonsterTest, DomainWithoutLeadingDotTest) {
993 { // The omission of dot results in setting a domain cookie. 1019 { // The omission of dot results in setting a domain cookie.
994 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1020 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
995 GURL url_hosted("http://manage.hosted.filefront.com"); 1021 GURL url_hosted("http://manage.hosted.filefront.com");
996 GURL url_filefront("http://www.filefront.com"); 1022 GURL url_filefront("http://www.filefront.com");
997 EXPECT_TRUE(cm->SetCookie(url_hosted, "sawAd=1; domain=filefront.com")); 1023 EXPECT_TRUE(SetCookie(cm, url_hosted, "sawAd=1; domain=filefront.com"));
998 EXPECT_EQ("sawAd=1", GetCookies(cm, url_hosted)); 1024 EXPECT_EQ("sawAd=1", GetCookies(cm, url_hosted));
999 EXPECT_EQ("sawAd=1", GetCookies(cm, url_filefront)); 1025 EXPECT_EQ("sawAd=1", GetCookies(cm, url_filefront));
1000 } 1026 }
1001 1027
1002 { // Even when the domains match exactly, don't consider it host cookie. 1028 { // Even when the domains match exactly, don't consider it host cookie.
1003 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1029 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1004 GURL url("http://www.google.com"); 1030 GURL url("http://www.google.com");
1005 EXPECT_TRUE(cm->SetCookie(url, "a=1; domain=www.google.com")); 1031 EXPECT_TRUE(SetCookie(cm, url, "a=1; domain=www.google.com"));
1006 EXPECT_EQ("a=1", GetCookies(cm, url)); 1032 EXPECT_EQ("a=1", GetCookies(cm, url));
1007 EXPECT_EQ("a=1", GetCookies(cm, GURL("http://sub.www.google.com"))); 1033 EXPECT_EQ("a=1", GetCookies(cm, GURL("http://sub.www.google.com")));
1008 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.com"))); 1034 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.com")));
1009 } 1035 }
1010 } 1036 }
1011 1037
1012 // Test that the domain specified in cookie string is treated case-insensitive 1038 // Test that the domain specified in cookie string is treated case-insensitive
1013 // http://b/issue?id=896475. 1039 // http://b/issue?id=896475.
1014 TEST_F(CookieMonsterTest, CaseInsensitiveDomainTest) { 1040 TEST_F(CookieMonsterTest, CaseInsensitiveDomainTest) {
1015 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1041 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1016 GURL url("http://www.google.com"); 1042 GURL url("http://www.google.com");
1017 EXPECT_TRUE(cm->SetCookie(url, "a=1; domain=.GOOGLE.COM")); 1043 EXPECT_TRUE(SetCookie(cm, url, "a=1; domain=.GOOGLE.COM"));
1018 EXPECT_TRUE(cm->SetCookie(url, "b=2; domain=.wWw.gOOgLE.coM")); 1044 EXPECT_TRUE(SetCookie(cm, url, "b=2; domain=.wWw.gOOgLE.coM"));
1019 EXPECT_EQ("a=1; b=2", GetCookies(cm, url)); 1045 EXPECT_EQ("a=1; b=2", GetCookies(cm, url));
1020 } 1046 }
1021 1047
1022 TEST_F(CookieMonsterTest, TestIpAddress) { 1048 TEST_F(CookieMonsterTest, TestIpAddress) {
1023 GURL url_ip("http://1.2.3.4/weee"); 1049 GURL url_ip("http://1.2.3.4/weee");
1024 { 1050 {
1025 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1051 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1026 EXPECT_TRUE(cm->SetCookie(url_ip, kValidCookieLine)); 1052 EXPECT_TRUE(SetCookie(cm, url_ip, kValidCookieLine));
1027 EXPECT_EQ("A=B", GetCookies(cm, url_ip)); 1053 EXPECT_EQ("A=B", GetCookies(cm, url_ip));
1028 } 1054 }
1029 1055
1030 { // IP addresses should not be able to set domain cookies. 1056 { // IP addresses should not be able to set domain cookies.
1031 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1057 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1032 EXPECT_FALSE(cm->SetCookie(url_ip, "b=2; domain=.1.2.3.4")); 1058 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")); 1059 EXPECT_FALSE(SetCookie(cm, url_ip, "c=3; domain=.3.4"));
1034 EXPECT_EQ("", GetCookies(cm, url_ip)); 1060 EXPECT_EQ("", GetCookies(cm, url_ip));
1035 // It should be allowed to set a cookie if domain= matches the IP address 1061 // 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. 1062 // 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")); 1063 EXPECT_FALSE(SetCookie(cm, url_ip, "b=2; domain=1.2.3.3"));
1038 EXPECT_EQ("", GetCookies(cm, url_ip)); 1064 EXPECT_EQ("", GetCookies(cm, url_ip));
1039 EXPECT_TRUE(cm->SetCookie(url_ip, "b=2; domain=1.2.3.4")); 1065 EXPECT_TRUE(SetCookie(cm, url_ip, "b=2; domain=1.2.3.4"));
1040 EXPECT_EQ("b=2", GetCookies(cm, url_ip)); 1066 EXPECT_EQ("b=2", GetCookies(cm, url_ip));
1041 } 1067 }
1042 } 1068 }
1043 1069
1044 // Test host cookies, and setting of cookies on TLD. 1070 // Test host cookies, and setting of cookies on TLD.
1045 TEST_F(CookieMonsterTest, TestNonDottedAndTLD) { 1071 TEST_F(CookieMonsterTest, TestNonDottedAndTLD) {
1046 { 1072 {
1047 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1073 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1048 GURL url("http://com/"); 1074 GURL url("http://com/");
1049 // Allow setting on "com", (but only as a host cookie). 1075 // Allow setting on "com", (but only as a host cookie).
1050 EXPECT_TRUE(cm->SetCookie(url, "a=1")); 1076 EXPECT_TRUE(SetCookie(cm, url, "a=1"));
1051 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=.com")); 1077 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=.com"));
1052 EXPECT_FALSE(cm->SetCookie(url, "c=3; domain=com")); 1078 EXPECT_FALSE(SetCookie(cm, url, "c=3; domain=com"));
1053 EXPECT_EQ("a=1", GetCookies(cm, url)); 1079 EXPECT_EQ("a=1", GetCookies(cm, url));
1054 // Make sure it doesn't show up for a normal .com, it should be a host 1080 // Make sure it doesn't show up for a normal .com, it should be a host
1055 // not a domain cookie. 1081 // not a domain cookie.
1056 EXPECT_EQ("", GetCookies(cm, GURL("http://hopefully-no-cookies.com/"))); 1082 EXPECT_EQ("", GetCookies(cm, GURL("http://hopefully-no-cookies.com/")));
1057 EXPECT_EQ("", GetCookies(cm, GURL("http://.com/"))); 1083 EXPECT_EQ("", GetCookies(cm, GURL("http://.com/")));
1058 } 1084 }
1059 1085
1060 { // http://com. should be treated the same as http://com. 1086 { // http://com. should be treated the same as http://com.
1061 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1087 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1062 GURL url("http://com./index.html"); 1088 GURL url("http://com./index.html");
1063 EXPECT_TRUE(cm->SetCookie(url, "a=1")); 1089 EXPECT_TRUE(SetCookie(cm, url, "a=1"));
1064 EXPECT_EQ("a=1", GetCookies(cm, url)); 1090 EXPECT_EQ("a=1", GetCookies(cm, url));
1065 EXPECT_EQ("", GetCookies(cm, GURL("http://hopefully-no-cookies.com./"))); 1091 EXPECT_EQ("", GetCookies(cm, GURL("http://hopefully-no-cookies.com./")));
1066 } 1092 }
1067 1093
1068 { // Should not be able to set host cookie from a subdomain. 1094 { // Should not be able to set host cookie from a subdomain.
1069 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1095 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1070 GURL url("http://a.b"); 1096 GURL url("http://a.b");
1071 EXPECT_FALSE(cm->SetCookie(url, "a=1; domain=.b")); 1097 EXPECT_FALSE(SetCookie(cm, url, "a=1; domain=.b"));
1072 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=b")); 1098 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=b"));
1073 EXPECT_EQ("", GetCookies(cm, url)); 1099 EXPECT_EQ("", GetCookies(cm, url));
1074 } 1100 }
1075 1101
1076 { // Same test as above, but explicitly on a known TLD (com). 1102 { // Same test as above, but explicitly on a known TLD (com).
1077 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1103 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1078 GURL url("http://google.com"); 1104 GURL url("http://google.com");
1079 EXPECT_FALSE(cm->SetCookie(url, "a=1; domain=.com")); 1105 EXPECT_FALSE(SetCookie(cm, url, "a=1; domain=.com"));
1080 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=com")); 1106 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=com"));
1081 EXPECT_EQ("", GetCookies(cm, url)); 1107 EXPECT_EQ("", GetCookies(cm, url));
1082 } 1108 }
1083 1109
1084 { // Make sure can't set cookie on TLD which is dotted. 1110 { // Make sure can't set cookie on TLD which is dotted.
1085 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1111 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1086 GURL url("http://google.co.uk"); 1112 GURL url("http://google.co.uk");
1087 EXPECT_FALSE(cm->SetCookie(url, "a=1; domain=.co.uk")); 1113 EXPECT_FALSE(SetCookie(cm, url, "a=1; domain=.co.uk"));
1088 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=.uk")); 1114 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=.uk"));
1089 EXPECT_EQ("", GetCookies(cm, url)); 1115 EXPECT_EQ("", GetCookies(cm, url));
1090 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.co.uk"))); 1116 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.co.uk")));
1091 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.uk"))); 1117 EXPECT_EQ("", GetCookies(cm, GURL("http://something-else.uk")));
1092 } 1118 }
1093 1119
1094 { // Intranet URLs should only be able to set host cookies. 1120 { // Intranet URLs should only be able to set host cookies.
1095 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1121 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1096 GURL url("http://b"); 1122 GURL url("http://b");
1097 EXPECT_TRUE(cm->SetCookie(url, "a=1")); 1123 EXPECT_TRUE(SetCookie(cm, url, "a=1"));
1098 EXPECT_FALSE(cm->SetCookie(url, "b=2; domain=.b")); 1124 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=.b"));
1099 EXPECT_FALSE(cm->SetCookie(url, "c=3; domain=b")); 1125 EXPECT_FALSE(SetCookie(cm, url, "c=3; domain=b"));
1100 EXPECT_EQ("a=1", GetCookies(cm, url)); 1126 EXPECT_EQ("a=1", GetCookies(cm, url));
1101 } 1127 }
1102 } 1128 }
1103 1129
1104 // Test reading/writing cookies when the domain ends with a period, 1130 // Test reading/writing cookies when the domain ends with a period,
1105 // as in "www.google.com." 1131 // as in "www.google.com."
1106 TEST_F(CookieMonsterTest, TestHostEndsWithDot) { 1132 TEST_F(CookieMonsterTest, TestHostEndsWithDot) {
1107 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1133 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1108 GURL url("http://www.google.com"); 1134 GURL url("http://www.google.com");
1109 GURL url_with_dot("http://www.google.com."); 1135 GURL url_with_dot("http://www.google.com.");
1110 EXPECT_TRUE(cm->SetCookie(url, "a=1")); 1136 EXPECT_TRUE(SetCookie(cm, url, "a=1"));
1111 EXPECT_EQ("a=1", GetCookies(cm, url)); 1137 EXPECT_EQ("a=1", GetCookies(cm, url));
1112 1138
1113 // Do not share cookie space with the dot version of domain. 1139 // 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. 1140 // 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.")); 1141 EXPECT_FALSE(SetCookie(cm, url, "b=2; domain=.www.google.com."));
1116 EXPECT_EQ("a=1", GetCookies(cm, url)); 1142 EXPECT_EQ("a=1", GetCookies(cm, url));
1117 1143
1118 EXPECT_TRUE(cm->SetCookie(url_with_dot, "b=2; domain=.google.com.")); 1144 EXPECT_TRUE(SetCookie(cm, url_with_dot, "b=2; domain=.google.com."));
1119 EXPECT_EQ("b=2", GetCookies(cm, url_with_dot)); 1145 EXPECT_EQ("b=2", GetCookies(cm, url_with_dot));
1120 1146
1121 // Make sure there weren't any side effects. 1147 // Make sure there weren't any side effects.
1122 EXPECT_EQ(GetCookies(cm, GURL("http://hopefully-no-cookies.com/")), ""); 1148 EXPECT_EQ(GetCookies(cm, GURL("http://hopefully-no-cookies.com/")), "");
1123 EXPECT_EQ("", GetCookies(cm, GURL("http://.com/"))); 1149 EXPECT_EQ("", GetCookies(cm, GURL("http://.com/")));
1124 } 1150 }
1125 1151
1126 TEST_F(CookieMonsterTest, InvalidScheme) { 1152 TEST_F(CookieMonsterTest, InvalidScheme) {
1127 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1153 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1128 EXPECT_FALSE(cm->SetCookie(GURL(kUrlFtp), kValidCookieLine)); 1154 EXPECT_FALSE(SetCookie(cm, GURL(kUrlFtp), kValidCookieLine));
1129 } 1155 }
1130 1156
1131 TEST_F(CookieMonsterTest, InvalidScheme_Read) { 1157 TEST_F(CookieMonsterTest, InvalidScheme_Read) {
1132 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1158 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1133 EXPECT_TRUE(cm->SetCookie(GURL(kUrlGoogle), kValidDomainCookieLine)); 1159 EXPECT_TRUE(SetCookie(cm, GURL(kUrlGoogle), kValidDomainCookieLine));
1134 EXPECT_EQ("", GetCookies(cm, GURL(kUrlFtp))); 1160 EXPECT_EQ("", GetCookies(cm, GURL(kUrlFtp)));
1135 } 1161 }
1136 1162
1137 TEST_F(CookieMonsterTest, PathTest) { 1163 TEST_F(CookieMonsterTest, PathTest) {
1138 std::string url("http://www.google.izzle"); 1164 std::string url("http://www.google.izzle");
1139 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1165 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1140 EXPECT_TRUE(cm->SetCookie(GURL(url), "A=B; path=/wee")); 1166 EXPECT_TRUE(SetCookie(cm, GURL(url), "A=B; path=/wee"));
1141 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee"))); 1167 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee")));
1142 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/"))); 1168 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/")));
1143 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/war"))); 1169 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/war")));
1144 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/war/more/more"))); 1170 EXPECT_EQ("A=B", GetCookies(cm, GURL(url + "/wee/war/more/more")));
1145 EXPECT_EQ("", GetCookies(cm, GURL(url + "/weehee"))); 1171 EXPECT_EQ("", GetCookies(cm, GURL(url + "/weehee")));
1146 EXPECT_EQ("", GetCookies(cm, GURL(url + "/"))); 1172 EXPECT_EQ("", GetCookies(cm, GURL(url + "/")));
1147 1173
1148 // If we add a 0 length path, it should default to / 1174 // If we add a 0 length path, it should default to /
1149 EXPECT_TRUE(cm->SetCookie(GURL(url), "A=C; path=")); 1175 EXPECT_TRUE(SetCookie(cm, GURL(url), "A=C; path="));
1150 EXPECT_EQ("A=B; A=C", GetCookies(cm, GURL(url + "/wee"))); 1176 EXPECT_EQ("A=B; A=C", GetCookies(cm, GURL(url + "/wee")));
1151 EXPECT_EQ("A=C", GetCookies(cm, GURL(url + "/"))); 1177 EXPECT_EQ("A=C", GetCookies(cm, GURL(url + "/")));
1152 } 1178 }
1153 1179
1154 TEST_F(CookieMonsterTest, HttpOnlyTest) { 1180 TEST_F(CookieMonsterTest, HttpOnlyTest) {
1155 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1181 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1156 CookieOptions options; 1182 CookieOptions options;
1157 options.set_include_httponly(); 1183 options.set_include_httponly();
1158 1184
1159 // Create a httponly cookie. 1185 // Create a httponly cookie.
1160 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B; httponly", options)); 1186 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B; httponly", options));
1161 1187
1162 // Check httponly read protection. 1188 // Check httponly read protection.
1163 EXPECT_EQ("", GetCookies(cm, url_google_)); 1189 EXPECT_EQ("", GetCookies(cm, url_google_));
1164 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options)); 1190 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options));
1165 1191
1166 // Check httponly overwrite protection. 1192 // Check httponly overwrite protection.
1167 EXPECT_FALSE(cm->SetCookie(url_google_, "A=C")); 1193 EXPECT_FALSE(SetCookie(cm, url_google_, "A=C"));
1168 EXPECT_EQ("", GetCookies(cm, url_google_)); 1194 EXPECT_EQ("", GetCookies(cm, url_google_));
1169 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options)); 1195 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options));
1170 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=C", options)); 1196 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=C", options));
1171 EXPECT_EQ("A=C", GetCookies(cm, url_google_)); 1197 EXPECT_EQ("A=C", GetCookies(cm, url_google_));
1172 1198
1173 // Check httponly create protection. 1199 // Check httponly create protection.
1174 EXPECT_FALSE(cm->SetCookie(url_google_, "B=A; httponly")); 1200 EXPECT_FALSE(SetCookie(cm, url_google_, "B=A; httponly"));
1175 EXPECT_EQ("A=C", GetCookiesWithOptions(cm, url_google_, options)); 1201 EXPECT_EQ("A=C", GetCookiesWithOptions(cm, url_google_, options));
1176 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "B=A; httponly", options)); 1202 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "B=A; httponly", options));
1177 EXPECT_EQ("A=C; B=A", GetCookiesWithOptions(cm, url_google_, options)); 1203 EXPECT_EQ("A=C; B=A", GetCookiesWithOptions(cm, url_google_, options));
1178 EXPECT_EQ("A=C", GetCookies(cm, url_google_)); 1204 EXPECT_EQ("A=C", GetCookies(cm, url_google_));
1179 } 1205 }
1180 1206
1181 TEST_F(CookieMonsterTest, GetCookiesWithInfo) { 1207 TEST_F(CookieMonsterTest, GetCookiesWithInfo) {
1182 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1208 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1183 CookieOptions options; 1209 CookieOptions options;
1184 1210
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
1317 CookieMonster::DomainIsHostOnly(tests[i].str)); 1343 CookieMonster::DomainIsHostOnly(tests[i].str));
1318 } 1344 }
1319 } 1345 }
1320 1346
1321 TEST_F(CookieMonsterTest, TestCookieDeletion) { 1347 TEST_F(CookieMonsterTest, TestCookieDeletion) {
1322 scoped_refptr<MockPersistentCookieStore> store( 1348 scoped_refptr<MockPersistentCookieStore> store(
1323 new MockPersistentCookieStore); 1349 new MockPersistentCookieStore);
1324 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 1350 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
1325 1351
1326 // Create a session cookie. 1352 // Create a session cookie.
1327 EXPECT_TRUE(cm->SetCookie(url_google_, kValidCookieLine)); 1353 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine));
1328 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1354 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1329 // Delete it via Max-Age. 1355 // Delete it via Max-Age.
1330 EXPECT_TRUE(cm->SetCookie(url_google_, 1356 EXPECT_TRUE(SetCookie(cm, url_google_,
1331 std::string(kValidCookieLine) + "; max-age=0")); 1357 std::string(kValidCookieLine) + "; max-age=0"));
1332 EXPECT_EQ("", GetCookies(cm, url_google_)); 1358 EXPECT_EQ("", GetCookies(cm, url_google_));
1333 1359
1334 // Create a session cookie. 1360 // Create a session cookie.
1335 EXPECT_TRUE(cm->SetCookie(url_google_, kValidCookieLine)); 1361 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine));
1336 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1362 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1337 // Delete it via Expires. 1363 // Delete it via Expires.
1338 EXPECT_TRUE(cm->SetCookie(url_google_, 1364 EXPECT_TRUE(SetCookie(cm, url_google_,
1339 std::string(kValidCookieLine) + 1365 std::string(kValidCookieLine) +
1340 "; expires=Mon, 18-Apr-1977 22:50:13 GMT")); 1366 "; expires=Mon, 18-Apr-1977 22:50:13 GMT"));
1341 EXPECT_EQ("", GetCookies(cm, url_google_)); 1367 EXPECT_EQ("", GetCookies(cm, url_google_));
1342 1368
1343 // Create a persistent cookie. 1369 // Create a persistent cookie.
1344 EXPECT_TRUE(cm->SetCookie(url_google_, 1370 EXPECT_TRUE(SetCookie(cm, url_google_,
1345 std::string(kValidCookieLine) + 1371 std::string(kValidCookieLine) +
1346 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); 1372 "; expires=Mon, 18-Apr-22 22:50:13 GMT"));
1347 ASSERT_EQ(1u, store->commands().size()); 1373 ASSERT_EQ(1u, store->commands().size());
1348 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); 1374 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
1349 1375
1350 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1376 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1351 // Delete it via Max-Age. 1377 // Delete it via Max-Age.
1352 EXPECT_TRUE(cm->SetCookie(url_google_, 1378 EXPECT_TRUE(SetCookie(cm, url_google_,
1353 std::string(kValidCookieLine) + "; max-age=0")); 1379 std::string(kValidCookieLine) + "; max-age=0"));
1354 ASSERT_EQ(2u, store->commands().size()); 1380 ASSERT_EQ(2u, store->commands().size());
1355 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); 1381 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
1356 EXPECT_EQ("", GetCookies(cm, url_google_)); 1382 EXPECT_EQ("", GetCookies(cm, url_google_));
1357 1383
1358 // Create a persistent cookie. 1384 // Create a persistent cookie.
1359 EXPECT_TRUE(cm->SetCookie(url_google_, 1385 EXPECT_TRUE(SetCookie(cm, url_google_,
1360 std::string(kValidCookieLine) + 1386 std::string(kValidCookieLine) +
1361 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); 1387 "; expires=Mon, 18-Apr-22 22:50:13 GMT"));
1362 ASSERT_EQ(3u, store->commands().size()); 1388 ASSERT_EQ(3u, store->commands().size());
1363 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type); 1389 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type);
1364 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1390 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1365 // Delete it via Expires. 1391 // Delete it via Expires.
1366 EXPECT_TRUE(cm->SetCookie(url_google_, 1392 EXPECT_TRUE(SetCookie(cm, url_google_,
1367 std::string(kValidCookieLine) + 1393 std::string(kValidCookieLine) +
1368 "; expires=Mon, 18-Apr-1977 22:50:13 GMT")); 1394 "; expires=Mon, 18-Apr-1977 22:50:13 GMT"));
1369 ASSERT_EQ(4u, store->commands().size()); 1395 ASSERT_EQ(4u, store->commands().size());
1370 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type); 1396 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[3].type);
1371 EXPECT_EQ("", GetCookies(cm, url_google_)); 1397 EXPECT_EQ("", GetCookies(cm, url_google_));
1372 1398
1373 // Create a persistent cookie. 1399 // Create a persistent cookie.
1374 EXPECT_TRUE(cm->SetCookie(url_google_, 1400 EXPECT_TRUE(SetCookie(cm, url_google_,
1375 std::string(kValidCookieLine) + 1401 std::string(kValidCookieLine) +
1376 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); 1402 "; expires=Mon, 18-Apr-22 22:50:13 GMT"));
1377 ASSERT_EQ(5u, store->commands().size()); 1403 ASSERT_EQ(5u, store->commands().size());
1378 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type); 1404 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[4].type);
1379 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1405 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1380 // Delete it via Expires, with a unix epoch of 0. 1406 // Delete it via Expires, with a unix epoch of 0.
1381 EXPECT_TRUE(cm->SetCookie(url_google_, 1407 EXPECT_TRUE(SetCookie(cm, url_google_,
1382 std::string(kValidCookieLine) + 1408 std::string(kValidCookieLine) +
1383 "; expires=Thu, 1-Jan-1970 00:00:00 GMT")); 1409 "; expires=Thu, 1-Jan-1970 00:00:00 GMT"));
1384 ASSERT_EQ(6u, store->commands().size()); 1410 ASSERT_EQ(6u, store->commands().size());
1385 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[5].type); 1411 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[5].type);
1386 EXPECT_EQ("", GetCookies(cm, url_google_)); 1412 EXPECT_EQ("", GetCookies(cm, url_google_));
1387 } 1413 }
1388 1414
1389 TEST_F(CookieMonsterTest, TestCookieDeleteAll) { 1415 TEST_F(CookieMonsterTest, TestCookieDeleteAll) {
1390 scoped_refptr<MockPersistentCookieStore> store( 1416 scoped_refptr<MockPersistentCookieStore> store(
1391 new MockPersistentCookieStore); 1417 new MockPersistentCookieStore);
1392 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 1418 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
1393 CookieOptions options; 1419 CookieOptions options;
1394 options.set_include_httponly(); 1420 options.set_include_httponly();
1395 1421
1396 EXPECT_TRUE(cm->SetCookie(url_google_, kValidCookieLine)); 1422 EXPECT_TRUE(SetCookie(cm, url_google_, kValidCookieLine));
1397 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1423 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1398 1424
1399 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "C=D; httponly", options)); 1425 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "C=D; httponly", options));
1400 EXPECT_EQ("A=B; C=D", GetCookiesWithOptions(cm, url_google_, options)); 1426 EXPECT_EQ("A=B; C=D", GetCookiesWithOptions(cm, url_google_, options));
1401 1427
1402 EXPECT_EQ(2, cm->DeleteAll(false)); 1428 EXPECT_EQ(2, DeleteAll(cm));
1403 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); 1429 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options));
1404 1430
1405 EXPECT_EQ(0u, store->commands().size()); 1431 EXPECT_EQ(0u, store->commands().size());
1406 1432
1407 // Create a persistent cookie. 1433 // Create a persistent cookie.
1408 EXPECT_TRUE(cm->SetCookie(url_google_, 1434 EXPECT_TRUE(SetCookie(cm, url_google_,
1409 std::string(kValidCookieLine) + 1435 std::string(kValidCookieLine) +
1410 "; expires=Mon, 18-Apr-22 22:50:13 GMT")); 1436 "; expires=Mon, 18-Apr-22 22:50:13 GMT"));
1411 ASSERT_EQ(1u, store->commands().size()); 1437 ASSERT_EQ(1u, store->commands().size());
1412 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); 1438 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
1413 1439
1414 EXPECT_EQ(1, cm->DeleteAll(true)); // sync_to_store = true. 1440 EXPECT_EQ(1, DeleteAll(cm)); // sync_to_store = true.
1415 ASSERT_EQ(2u, store->commands().size()); 1441 ASSERT_EQ(2u, store->commands().size());
1416 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); 1442 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
1417 1443
1418 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options)); 1444 EXPECT_EQ("", GetCookiesWithOptions(cm, url_google_, options));
1419 } 1445 }
1420 1446
1421 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) { 1447 TEST_F(CookieMonsterTest, TestCookieDeleteAllCreatedBetweenTimestamps) {
1422 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1448 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1423 Time now = Time::Now(); 1449 Time now = Time::Now();
1424 1450
1425 // Nothing has been added so nothing should be deleted. 1451 // Nothing has been added so nothing should be deleted.
1426 EXPECT_EQ(0, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99), 1452 EXPECT_EQ(0, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99),
1427 Time(), false)); 1453 Time()));
1428 1454
1429 // Create 3 cookies with creation date of today, yesterday and the day before. 1455 // Create 3 cookies with creation date of today, yesterday and the day before.
1430 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-0=Now", now)); 1456 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-0=Now", now));
1431 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-1=Yesterday", 1457 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-1=Yesterday",
1432 now - TimeDelta::FromDays(1))); 1458 now - TimeDelta::FromDays(1)));
1433 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-2=DayBefore", 1459 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-2=DayBefore",
1434 now - TimeDelta::FromDays(2))); 1460 now - TimeDelta::FromDays(2)));
1435 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-3=ThreeDays", 1461 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-3=ThreeDays",
1436 now - TimeDelta::FromDays(3))); 1462 now - TimeDelta::FromDays(3)));
1437 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-7=LastWeek", 1463 EXPECT_TRUE(cm->SetCookieWithCreationTime(url_google_, "T-7=LastWeek",
1438 now - TimeDelta::FromDays(7))); 1464 now - TimeDelta::FromDays(7)));
1439 1465
1440 // Try to delete threedays and the daybefore. 1466 // Try to delete threedays and the daybefore.
1441 EXPECT_EQ(2, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(3), 1467 EXPECT_EQ(2, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(3),
1442 now - TimeDelta::FromDays(1), 1468 now - TimeDelta::FromDays(1)));
1443 false));
1444 1469
1445 // Try to delete yesterday, also make sure that delete_end is not 1470 // Try to delete yesterday, also make sure that delete_end is not
1446 // inclusive. 1471 // inclusive.
1447 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(2), 1472 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(2),
1448 now, 1473 now));
1449 false));
1450 1474
1451 // Make sure the delete_begin is inclusive. 1475 // Make sure the delete_begin is inclusive.
1452 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(7), 1476 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(7),
1453 now, 1477 now));
1454 false));
1455 1478
1456 // Delete the last (now) item. 1479 // Delete the last (now) item.
1457 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, Time(), Time(), false)); 1480 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, Time(), Time()));
1458 1481
1459 // Really make sure everything is gone. 1482 // Really make sure everything is gone.
1460 EXPECT_EQ(0, cm->DeleteAll(false)); 1483 EXPECT_EQ(0, DeleteAll(cm));
1461 } 1484 }
1462 1485
1463 TEST_F(CookieMonsterTest, TestSecure) { 1486 TEST_F(CookieMonsterTest, TestSecure) {
1464 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1487 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1465 1488
1466 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 1489 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
1467 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1490 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1468 EXPECT_EQ("A=B", GetCookies(cm, url_google_secure_)); 1491 EXPECT_EQ("A=B", GetCookies(cm, url_google_secure_));
1469 1492
1470 EXPECT_TRUE(cm->SetCookie(url_google_secure_, "A=B; secure")); 1493 EXPECT_TRUE(SetCookie(cm, url_google_secure_, "A=B; secure"));
1471 // The secure should overwrite the non-secure. 1494 // The secure should overwrite the non-secure.
1472 EXPECT_EQ("", GetCookies(cm, url_google_)); 1495 EXPECT_EQ("", GetCookies(cm, url_google_));
1473 EXPECT_EQ("A=B", GetCookies(cm, url_google_secure_)); 1496 EXPECT_EQ("A=B", GetCookies(cm, url_google_secure_));
1474 1497
1475 EXPECT_TRUE(cm->SetCookie(url_google_secure_, "D=E; secure")); 1498 EXPECT_TRUE(SetCookie(cm, url_google_secure_, "D=E; secure"));
1476 EXPECT_EQ("", GetCookies(cm, url_google_)); 1499 EXPECT_EQ("", GetCookies(cm, url_google_));
1477 EXPECT_EQ("A=B; D=E", GetCookies(cm, url_google_secure_)); 1500 EXPECT_EQ("A=B; D=E", GetCookies(cm, url_google_secure_));
1478 1501
1479 EXPECT_TRUE(cm->SetCookie(url_google_secure_, "A=B")); 1502 EXPECT_TRUE(SetCookie(cm, url_google_secure_, "A=B"));
1480 // The non-secure should overwrite the secure. 1503 // The non-secure should overwrite the secure.
1481 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1504 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1482 EXPECT_EQ("D=E; A=B", GetCookies(cm, url_google_secure_)); 1505 EXPECT_EQ("D=E; A=B", GetCookies(cm, url_google_secure_));
1483 } 1506 }
1484 1507
1485 static const int kLastAccessThresholdMilliseconds = 200; 1508 static const int kLastAccessThresholdMilliseconds = 200;
1486 1509
1487 TEST_F(CookieMonsterTest, TestLastAccess) { 1510 TEST_F(CookieMonsterTest, TestLastAccess) {
1488 scoped_refptr<CookieMonster> cm( 1511 scoped_refptr<CookieMonster> cm(
1489 new CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds)); 1512 new CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds));
1490 1513
1491 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 1514 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
1492 const Time last_access_date(GetFirstCookieAccessDate(cm)); 1515 const Time last_access_date(GetFirstCookieAccessDate(cm));
1493 1516
1494 // Reading the cookie again immediately shouldn't update the access date, 1517 // Reading the cookie again immediately shouldn't update the access date,
1495 // since we're inside the threshold. 1518 // since we're inside the threshold.
1496 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1519 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1497 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm)); 1520 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm));
1498 1521
1499 // Reading after a short wait should update the access date. 1522 // Reading after a short wait should update the access date.
1500 base::PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20); 1523 base::PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20);
1501 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 1524 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
1502 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm)); 1525 EXPECT_FALSE(last_access_date == GetFirstCookieAccessDate(cm));
1503 } 1526 }
1504 1527
1505 TEST_F(CookieMonsterTest, TestHostGarbageCollection) { 1528 TEST_F(CookieMonsterTest, TestHostGarbageCollection) {
1506 TestHostGarbageCollectHelper( 1529 TestHostGarbageCollectHelper(
1507 CookieMonster::kDomainMaxCookies, CookieMonster::kDomainPurgeCookies, 1530 CookieMonster::kDomainMaxCookies, CookieMonster::kDomainPurgeCookies,
1508 CookieMonster::EKS_KEEP_RECENT_AND_PURGE_ETLDP1); 1531 CookieMonster::EKS_KEEP_RECENT_AND_PURGE_ETLDP1);
1509 TestHostGarbageCollectHelper( 1532 TestHostGarbageCollectHelper(
1510 CookieMonster::kDomainMaxCookies, CookieMonster::kDomainPurgeCookies, 1533 CookieMonster::kDomainMaxCookies, CookieMonster::kDomainPurgeCookies,
1511 CookieMonster::EKS_DISCARD_RECENT_AND_PURGE_DOMAIN); 1534 CookieMonster::EKS_DISCARD_RECENT_AND_PURGE_DOMAIN);
1512 } 1535 }
1513 1536
1514 // Formerly NetUtilTest.CookieTest back when we used wininet's cookie handling. 1537 // Formerly NetUtilTest.CookieTest back when we used wininet's cookie handling.
1515 TEST_F(CookieMonsterTest, NetUtilCookieTest) { 1538 TEST_F(CookieMonsterTest, NetUtilCookieTest) {
1516 const GURL test_url("http://mojo.jojo.google.izzle/"); 1539 const GURL test_url("http://mojo.jojo.google.izzle/");
1517 1540
1518 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1541 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1519 1542
1520 EXPECT_TRUE(cm->SetCookie(test_url, "foo=bar")); 1543 EXPECT_TRUE(SetCookie(cm, test_url, "foo=bar"));
1521 std::string value = GetCookies(cm, test_url); 1544 std::string value = GetCookies(cm, test_url);
1522 EXPECT_EQ("foo=bar", value); 1545 EXPECT_EQ("foo=bar", value);
1523 1546
1524 // test that we can retrieve all cookies: 1547 // test that we can retrieve all cookies:
1525 EXPECT_TRUE(cm->SetCookie(test_url, "x=1")); 1548 EXPECT_TRUE(SetCookie(cm, test_url, "x=1"));
1526 EXPECT_TRUE(cm->SetCookie(test_url, "y=2")); 1549 EXPECT_TRUE(SetCookie(cm, test_url, "y=2"));
1527 1550
1528 std::string result = GetCookies(cm, test_url); 1551 std::string result = GetCookies(cm, test_url);
1529 EXPECT_FALSE(result.empty()); 1552 EXPECT_FALSE(result.empty());
1530 EXPECT_NE(result.find("x=1"), std::string::npos) << result; 1553 EXPECT_NE(result.find("x=1"), std::string::npos) << result;
1531 EXPECT_NE(result.find("y=2"), std::string::npos) << result; 1554 EXPECT_NE(result.find("y=2"), std::string::npos) << result;
1532 } 1555 }
1533 1556
1534 TEST_F(CookieMonsterTest, TestDeleteSingleCookie) { 1557 TEST_F(CookieMonsterTest, TestDeleteSingleCookie) {
1535 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1558 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1536 1559
1537 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 1560 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
1538 EXPECT_TRUE(cm->SetCookie(url_google_, "C=D")); 1561 EXPECT_TRUE(SetCookie(cm, url_google_, "C=D"));
1539 EXPECT_TRUE(cm->SetCookie(url_google_, "E=F")); 1562 EXPECT_TRUE(SetCookie(cm, url_google_, "E=F"));
1540 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_)); 1563 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_));
1541 1564
1542 EXPECT_TRUE(FindAndDeleteCookie(cm, url_google_.host(), "C")); 1565 EXPECT_TRUE(FindAndDeleteCookie(cm, url_google_.host(), "C"));
1543 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_)); 1566 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_));
1544 1567
1545 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E")); 1568 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E"));
1546 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_)); 1569 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_));
1547 } 1570 }
1548 1571
1549 TEST_F(CookieMonsterTest, SetCookieableSchemes) { 1572 TEST_F(CookieMonsterTest, SetCookieableSchemes) {
1550 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1573 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1551 scoped_refptr<CookieMonster> cm_foo(new CookieMonster(NULL, NULL)); 1574 scoped_refptr<CookieMonster> cm_foo(new CookieMonster(NULL, NULL));
1552 1575
1553 // Only cm_foo should allow foo:// cookies. 1576 // Only cm_foo should allow foo:// cookies.
1554 const char* kSchemes[] = {"foo"}; 1577 const char* kSchemes[] = {"foo"};
1555 cm_foo->SetCookieableSchemes(kSchemes, 1); 1578 cm_foo->SetCookieableSchemes(kSchemes, 1);
1556 1579
1557 GURL foo_url("foo://host/path"); 1580 GURL foo_url("foo://host/path");
1558 GURL http_url("http://host/path"); 1581 GURL http_url("http://host/path");
1559 1582
1560 EXPECT_TRUE(cm->SetCookie(http_url, "x=1")); 1583 EXPECT_TRUE(SetCookie(cm, http_url, "x=1"));
1561 EXPECT_FALSE(cm->SetCookie(foo_url, "x=1")); 1584 EXPECT_FALSE(SetCookie(cm, foo_url, "x=1"));
1562 EXPECT_TRUE(cm_foo->SetCookie(foo_url, "x=1")); 1585 EXPECT_TRUE(SetCookie(cm_foo, foo_url, "x=1"));
1563 EXPECT_FALSE(cm_foo->SetCookie(http_url, "x=1")); 1586 EXPECT_FALSE(SetCookie(cm_foo, http_url, "x=1"));
1564 } 1587 }
1565 1588
1566 TEST_F(CookieMonsterTest, GetAllCookiesForURL) { 1589 TEST_F(CookieMonsterTest, GetAllCookiesForURL) {
1567 scoped_refptr<CookieMonster> cm( 1590 scoped_refptr<CookieMonster> cm(
1568 new CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds)); 1591 new CookieMonster(NULL, NULL, kLastAccessThresholdMilliseconds));
1569 1592
1570 // Create an httponly cookie. 1593 // Create an httponly cookie.
1571 CookieOptions options; 1594 CookieOptions options;
1572 options.set_include_httponly(); 1595 options.set_include_httponly();
1573 1596
1574 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B; httponly", options)); 1597 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B; httponly", options));
1575 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, 1598 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_,
1576 "C=D; domain=.google.izzle", 1599 "C=D; domain=.google.izzle",
1577 options)); 1600 options));
1578 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_secure_, 1601 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_secure_,
1579 "E=F; domain=.google.izzle; secure", 1602 "E=F; domain=.google.izzle; secure",
1580 options)); 1603 options));
1581 1604
1582 const Time last_access_date(GetFirstCookieAccessDate(cm)); 1605 const Time last_access_date(GetFirstCookieAccessDate(cm));
1583 1606
1584 base::PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20); 1607 base::PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20);
1585 1608
1586 // Check cookies for url. 1609 // Check cookies for url.
1587 CookieList cookies = GetAllCookiesForURL(cm, url_google_); 1610 CookieList cookies = GetAllCookiesForURL(cm, url_google_);
1588 CookieList::iterator it = cookies.begin(); 1611 CookieList::iterator it = cookies.begin();
1589 1612
1590 ASSERT_TRUE(it != cookies.end()); 1613 ASSERT_TRUE(it != cookies.end());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1628 1651
1629 // Reading after a short wait should not update the access date. 1652 // Reading after a short wait should not update the access date.
1630 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm)); 1653 EXPECT_TRUE(last_access_date == GetFirstCookieAccessDate(cm));
1631 } 1654 }
1632 1655
1633 TEST_F(CookieMonsterTest, GetAllCookiesForURLPathMatching) { 1656 TEST_F(CookieMonsterTest, GetAllCookiesForURLPathMatching) {
1634 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1657 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1635 CookieOptions options; 1658 CookieOptions options;
1636 1659
1637 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_foo_, 1660 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_foo_,
1638 "A=B; path=/foo;", 1661 "A=B; path=/foo;", options));
1639 options));
1640 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_bar_, 1662 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_bar_,
1641 "C=D; path=/bar;", 1663 "C=D; path=/bar;", options));
1642 options));
1643 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, 1664 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_,
1644 "E=F;", 1665 "E=F;", options));
1645 options));
1646 1666
1647 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_); 1667 CookieList cookies = GetAllCookiesForURL(cm, url_google_foo_);
1648 CookieList::iterator it = cookies.begin(); 1668 CookieList::iterator it = cookies.begin();
1649 1669
1650 ASSERT_TRUE(it != cookies.end()); 1670 ASSERT_TRUE(it != cookies.end());
1651 EXPECT_EQ("A", it->Name()); 1671 EXPECT_EQ("A", it->Name());
1652 EXPECT_EQ("/foo", it->Path()); 1672 EXPECT_EQ("/foo", it->Path());
1653 1673
1654 ASSERT_TRUE(++it != cookies.end()); 1674 ASSERT_TRUE(++it != cookies.end());
1655 EXPECT_EQ("E", it->Name()); 1675 EXPECT_EQ("E", it->Name());
(...skipping 11 matching lines...) Expand all
1667 ASSERT_TRUE(++it != cookies.end()); 1687 ASSERT_TRUE(++it != cookies.end());
1668 EXPECT_EQ("E", it->Name()); 1688 EXPECT_EQ("E", it->Name());
1669 EXPECT_EQ("/", it->Path()); 1689 EXPECT_EQ("/", it->Path());
1670 1690
1671 ASSERT_TRUE(++it == cookies.end()); 1691 ASSERT_TRUE(++it == cookies.end());
1672 } 1692 }
1673 1693
1674 TEST_F(CookieMonsterTest, DeleteCookieByName) { 1694 TEST_F(CookieMonsterTest, DeleteCookieByName) {
1675 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 1695 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
1676 1696
1677 EXPECT_TRUE(cm->SetCookie(url_google_, "A=A1; path=/")); 1697 EXPECT_TRUE(SetCookie(cm, url_google_, "A=A1; path=/"));
1678 EXPECT_TRUE(cm->SetCookie(url_google_, "A=A2; path=/foo")); 1698 EXPECT_TRUE(SetCookie(cm, url_google_, "A=A2; path=/foo"));
1679 EXPECT_TRUE(cm->SetCookie(url_google_, "A=A3; path=/bar")); 1699 EXPECT_TRUE(SetCookie(cm, url_google_, "A=A3; path=/bar"));
1680 EXPECT_TRUE(cm->SetCookie(url_google_, "B=B1; path=/")); 1700 EXPECT_TRUE(SetCookie(cm, url_google_, "B=B1; path=/"));
1681 EXPECT_TRUE(cm->SetCookie(url_google_, "B=B2; path=/foo")); 1701 EXPECT_TRUE(SetCookie(cm, url_google_, "B=B2; path=/foo"));
1682 EXPECT_TRUE(cm->SetCookie(url_google_, "B=B3; path=/bar")); 1702 EXPECT_TRUE(SetCookie(cm, url_google_, "B=B3; path=/bar"));
1683 1703
1684 DeleteCookie(cm, GURL(std::string(kUrlGoogle) + "/foo/bar"), "A"); 1704 DeleteCookie(cm, GURL(std::string(kUrlGoogle) + "/foo/bar"), "A");
1685 1705
1686 CookieList cookies = GetAllCookies(cm); 1706 CookieList cookies = GetAllCookies(cm);
1687 size_t expected_size = 4; 1707 size_t expected_size = 4;
1688 EXPECT_EQ(expected_size, cookies.size()); 1708 EXPECT_EQ(expected_size, cookies.size());
1689 for (CookieList::iterator it = cookies.begin(); 1709 for (CookieList::iterator it = cookies.begin();
1690 it != cookies.end(); ++it) { 1710 it != cookies.end(); ++it) {
1691 EXPECT_NE("A1", it->Value()); 1711 EXPECT_NE("A1", it->Value());
1692 EXPECT_NE("A2", it->Value()); 1712 EXPECT_NE("A2", it->Value());
1693 } 1713 }
1694 } 1714 }
1695 1715
1696 TEST_F(CookieMonsterTest, InitializeFromCookieMonster) { 1716 TEST_F(CookieMonsterTest, InitializeFromCookieMonster) {
1697 scoped_refptr<CookieMonster> cm_1(new CookieMonster(NULL, NULL)); 1717 scoped_refptr<CookieMonster> cm_1(new CookieMonster(NULL, NULL));
1698 CookieOptions options; 1718 CookieOptions options;
1699 1719
1700 EXPECT_TRUE(SetCookieWithOptions(cm_1.get(), url_google_foo_, 1720 EXPECT_TRUE(SetCookieWithOptions(cm_1.get(), url_google_foo_,
1701 "A1=B; path=/foo;", 1721 "A1=B; path=/foo;", options));
1702 options));
1703 EXPECT_TRUE(SetCookieWithOptions(cm_1.get(), url_google_bar_, 1722 EXPECT_TRUE(SetCookieWithOptions(cm_1.get(), url_google_bar_,
1704 "A2=D; path=/bar;", 1723 "A2=D; path=/bar;", options));
1705 options));
1706 EXPECT_TRUE(SetCookieWithOptions(cm_1.get(), url_google_, 1724 EXPECT_TRUE(SetCookieWithOptions(cm_1.get(), url_google_,
1707 "A3=F;", 1725 "A3=F;", options));
1708 options));
1709 1726
1727 CookieList cookies_1 = GetAllCookies(cm_1);
1710 scoped_refptr<CookieMonster> cm_2(new CookieMonster(NULL, NULL)); 1728 scoped_refptr<CookieMonster> cm_2(new CookieMonster(NULL, NULL));
1711 ASSERT_TRUE(cm_2->InitializeFrom(cm_1.get())); 1729 ASSERT_TRUE(cm_2->InitializeFrom(cookies_1));
1712 CookieList cookies = cm_2->GetAllCookies(); 1730 CookieList cookies_2 = GetAllCookies(cm_2);
1713 1731
1714 size_t expected_size = 3; 1732 size_t expected_size = 3;
1715 EXPECT_EQ(expected_size, cookies.size()); 1733 EXPECT_EQ(expected_size, cookies_2.size());
1716 1734
1717 CookieList::iterator it = cookies.begin(); 1735 CookieList::iterator it = cookies_2.begin();
1718 1736
1719 ASSERT_TRUE(it != cookies.end()); 1737 ASSERT_TRUE(it != cookies_2.end());
1720 EXPECT_EQ("A1", it->Name()); 1738 EXPECT_EQ("A1", it->Name());
1721 EXPECT_EQ("/foo", it->Path()); 1739 EXPECT_EQ("/foo", it->Path());
1722 1740
1723 ASSERT_TRUE(++it != cookies.end()); 1741 ASSERT_TRUE(++it != cookies_2.end());
1724 EXPECT_EQ("A2", it->Name()); 1742 EXPECT_EQ("A2", it->Name());
1725 EXPECT_EQ("/bar", it->Path()); 1743 EXPECT_EQ("/bar", it->Path());
1726 1744
1727 ASSERT_TRUE(++it != cookies.end()); 1745 ASSERT_TRUE(++it != cookies_2.end());
1728 EXPECT_EQ("A3", it->Name()); 1746 EXPECT_EQ("A3", it->Name());
1729 EXPECT_EQ("/", it->Path()); 1747 EXPECT_EQ("/", it->Path());
1730 } 1748 }
1731 1749
1732 1750
1733 // Test that overwriting persistent cookies deletes the old one from the 1751 // Test that overwriting persistent cookies deletes the old one from the
1734 // backing store. 1752 // backing store.
1735 TEST_F(CookieMonsterTest, OverwritePersistentCookie) { 1753 TEST_F(CookieMonsterTest, OverwritePersistentCookie) {
1736 GURL url_google("http://www.google.com/"); 1754 GURL url_google("http://www.google.com/");
1737 GURL url_chromium("http://chromium.org"); 1755 GURL url_chromium("http://chromium.org");
1738 scoped_refptr<MockPersistentCookieStore> store( 1756 scoped_refptr<MockPersistentCookieStore> store(
1739 new MockPersistentCookieStore); 1757 new MockPersistentCookieStore);
1740 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL)); 1758 scoped_refptr<CookieMonster> cm(new CookieMonster(store, NULL));
1741 1759
1742 // Insert a cookie "a" for path "/path1" 1760 // Insert a cookie "a" for path "/path1"
1743 EXPECT_TRUE( 1761 EXPECT_TRUE(
1744 cm->SetCookie(url_google, "a=val1; path=/path1; " 1762 SetCookie(cm, url_google, "a=val1; path=/path1; "
1745 "expires=Mon, 18-Apr-22 22:50:13 GMT")); 1763 "expires=Mon, 18-Apr-22 22:50:13 GMT"));
1746 ASSERT_EQ(1u, store->commands().size()); 1764 ASSERT_EQ(1u, store->commands().size());
1747 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); 1765 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
1748 1766
1749 // Insert a cookie "b" for path "/path1" 1767 // Insert a cookie "b" for path "/path1"
1750 EXPECT_TRUE( 1768 EXPECT_TRUE(
1751 cm->SetCookie(url_google, "b=val1; path=/path1; " 1769 SetCookie(cm, url_google, "b=val1; path=/path1; "
1752 "expires=Mon, 18-Apr-22 22:50:14 GMT")); 1770 "expires=Mon, 18-Apr-22 22:50:14 GMT"));
1753 ASSERT_EQ(2u, store->commands().size()); 1771 ASSERT_EQ(2u, store->commands().size());
1754 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[1].type); 1772 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[1].type);
1755 1773
1756 // Insert a cookie "b" for path "/path1", that is httponly. This should 1774 // Insert a cookie "b" for path "/path1", that is httponly. This should
1757 // overwrite the non-http-only version. 1775 // overwrite the non-http-only version.
1758 CookieOptions allow_httponly; 1776 CookieOptions allow_httponly;
1759 allow_httponly.set_include_httponly(); 1777 allow_httponly.set_include_httponly();
1760 EXPECT_TRUE( 1778 EXPECT_TRUE(
1761 SetCookieWithOptions(cm, url_google, 1779 SetCookieWithOptions(cm, url_google,
1762 "b=val2; path=/path1; httponly; " 1780 "b=val2; path=/path1; httponly; "
1763 "expires=Mon, 18-Apr-22 22:50:14 GMT", 1781 "expires=Mon, 18-Apr-22 22:50:14 GMT",
1764 allow_httponly)); 1782 allow_httponly));
1765 ASSERT_EQ(4u, store->commands().size()); 1783 ASSERT_EQ(4u, store->commands().size());
1766 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[2].type); 1784 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[2].type);
1767 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[3].type); 1785 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[3].type);
1768 1786
1769 // Insert a cookie "a" for path "/path1". This should overwrite. 1787 // Insert a cookie "a" for path "/path1". This should overwrite.
1770 EXPECT_TRUE(cm->SetCookie(url_google, 1788 EXPECT_TRUE(SetCookie(cm, url_google,
1771 "a=val33; path=/path1; " 1789 "a=val33; path=/path1; "
1772 "expires=Mon, 18-Apr-22 22:50:14 GMT")); 1790 "expires=Mon, 18-Apr-22 22:50:14 GMT"));
1773 ASSERT_EQ(6u, store->commands().size()); 1791 ASSERT_EQ(6u, store->commands().size());
1774 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[4].type); 1792 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[4].type);
1775 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[5].type); 1793 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[5].type);
1776 1794
1777 // Insert a cookie "a" for path "/path2". This should NOT overwrite 1795 // Insert a cookie "a" for path "/path2". This should NOT overwrite
1778 // cookie "a", since the path is different. 1796 // cookie "a", since the path is different.
1779 EXPECT_TRUE(cm->SetCookie(url_google, 1797 EXPECT_TRUE(SetCookie(cm, url_google,
1780 "a=val9; path=/path2; " 1798 "a=val9; path=/path2; "
1781 "expires=Mon, 18-Apr-22 22:50:14 GMT")); 1799 "expires=Mon, 18-Apr-22 22:50:14 GMT"));
1782 ASSERT_EQ(7u, store->commands().size()); 1800 ASSERT_EQ(7u, store->commands().size());
1783 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[6].type); 1801 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[6].type);
1784 1802
1785 // Insert a cookie "a" for path "/path1", but this time for "chromium.org". 1803 // Insert a cookie "a" for path "/path1", but this time for "chromium.org".
1786 // Although the name and path match, the hostnames do not, so shouldn't 1804 // Although the name and path match, the hostnames do not, so shouldn't
1787 // overwrite. 1805 // overwrite.
1788 EXPECT_TRUE(cm->SetCookie(url_chromium, 1806 EXPECT_TRUE(SetCookie(cm, url_chromium,
1789 "a=val99; path=/path1; " 1807 "a=val99; path=/path1; "
1790 "expires=Mon, 18-Apr-22 22:50:14 GMT")); 1808 "expires=Mon, 18-Apr-22 22:50:14 GMT"));
1791 ASSERT_EQ(8u, store->commands().size()); 1809 ASSERT_EQ(8u, store->commands().size());
1792 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[7].type); 1810 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[7].type);
1793 1811
1794 EXPECT_EQ("a=val33", GetCookies(cm, GURL("http://www.google.com/path1"))); 1812 EXPECT_EQ("a=val33", GetCookies(cm, GURL("http://www.google.com/path1")));
1795 EXPECT_EQ("a=val9", GetCookies(cm, GURL("http://www.google.com/path2"))); 1813 EXPECT_EQ("a=val9", GetCookies(cm, GURL("http://www.google.com/path2")));
1796 EXPECT_EQ("a=val99", GetCookies(cm, GURL("http://chromium.org/path1"))); 1814 EXPECT_EQ("a=val99", GetCookies(cm, GURL("http://chromium.org/path1")));
1797 } 1815 }
1798 1816
1799 // Tests importing from a persistent cookie store that contains duplicate 1817 // Tests importing from a persistent cookie store that contains duplicate
1800 // equivalent cookies. This situation should be handled by removing the 1818 // equivalent cookies. This situation should be handled by removing the
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
1921 EXPECT_NE(name1, name2); 1939 EXPECT_NE(name1, name2);
1922 } 1940 }
1923 1941
1924 TEST_F(CookieMonsterTest, Delegate) { 1942 TEST_F(CookieMonsterTest, Delegate) {
1925 scoped_refptr<MockPersistentCookieStore> store( 1943 scoped_refptr<MockPersistentCookieStore> store(
1926 new MockPersistentCookieStore); 1944 new MockPersistentCookieStore);
1927 scoped_refptr<MockCookieMonsterDelegate> delegate( 1945 scoped_refptr<MockCookieMonsterDelegate> delegate(
1928 new MockCookieMonsterDelegate); 1946 new MockCookieMonsterDelegate);
1929 scoped_refptr<CookieMonster> cm(new CookieMonster(store, delegate)); 1947 scoped_refptr<CookieMonster> cm(new CookieMonster(store, delegate));
1930 1948
1931 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 1949 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
1932 EXPECT_TRUE(cm->SetCookie(url_google_, "C=D")); 1950 EXPECT_TRUE(SetCookie(cm, url_google_, "C=D"));
1933 EXPECT_TRUE(cm->SetCookie(url_google_, "E=F")); 1951 EXPECT_TRUE(SetCookie(cm, url_google_, "E=F"));
1934 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_)); 1952 EXPECT_EQ("A=B; C=D; E=F", GetCookies(cm, url_google_));
1935 ASSERT_EQ(3u, delegate->changes().size()); 1953 ASSERT_EQ(3u, delegate->changes().size());
1936 EXPECT_FALSE(delegate->changes()[0].second); 1954 EXPECT_FALSE(delegate->changes()[0].second);
1937 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); 1955 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain());
1938 EXPECT_EQ("A", delegate->changes()[0].first.Name()); 1956 EXPECT_EQ("A", delegate->changes()[0].first.Name());
1939 EXPECT_EQ("B", delegate->changes()[0].first.Value()); 1957 EXPECT_EQ("B", delegate->changes()[0].first.Value());
1940 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain()); 1958 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain());
1941 EXPECT_FALSE(delegate->changes()[1].second); 1959 EXPECT_FALSE(delegate->changes()[1].second);
1942 EXPECT_EQ("C", delegate->changes()[1].first.Name()); 1960 EXPECT_EQ("C", delegate->changes()[1].first.Name());
1943 EXPECT_EQ("D", delegate->changes()[1].first.Value()); 1961 EXPECT_EQ("D", delegate->changes()[1].first.Value());
(...skipping 11 matching lines...) Expand all
1955 EXPECT_EQ("C", delegate->changes()[0].first.Name()); 1973 EXPECT_EQ("C", delegate->changes()[0].first.Name());
1956 EXPECT_EQ("D", delegate->changes()[0].first.Value()); 1974 EXPECT_EQ("D", delegate->changes()[0].first.Value());
1957 delegate->reset(); 1975 delegate->reset();
1958 1976
1959 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E")); 1977 EXPECT_FALSE(FindAndDeleteCookie(cm, "random.host", "E"));
1960 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_)); 1978 EXPECT_EQ("A=B; E=F", GetCookies(cm, url_google_));
1961 EXPECT_EQ(0u, delegate->changes().size()); 1979 EXPECT_EQ(0u, delegate->changes().size());
1962 1980
1963 // Insert a cookie "a" for path "/path1" 1981 // Insert a cookie "a" for path "/path1"
1964 EXPECT_TRUE( 1982 EXPECT_TRUE(
1965 cm->SetCookie(url_google_, "a=val1; path=/path1; " 1983 SetCookie(cm, url_google_, "a=val1; path=/path1; "
1966 "expires=Mon, 18-Apr-22 22:50:13 GMT")); 1984 "expires=Mon, 18-Apr-22 22:50:13 GMT"));
1967 ASSERT_EQ(1u, store->commands().size()); 1985 ASSERT_EQ(1u, store->commands().size());
1968 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type); 1986 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[0].type);
1969 ASSERT_EQ(1u, delegate->changes().size()); 1987 ASSERT_EQ(1u, delegate->changes().size());
1970 EXPECT_FALSE(delegate->changes()[0].second); 1988 EXPECT_FALSE(delegate->changes()[0].second);
1971 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); 1989 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain());
1972 EXPECT_EQ("a", delegate->changes()[0].first.Name()); 1990 EXPECT_EQ("a", delegate->changes()[0].first.Name());
1973 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); 1991 EXPECT_EQ("val1", delegate->changes()[0].first.Value());
1974 delegate->reset(); 1992 delegate->reset();
1975 1993
1976 // Insert a cookie "a" for path "/path1", that is httponly. This should 1994 // Insert a cookie "a" for path "/path1", that is httponly. This should
1977 // overwrite the non-http-only version. 1995 // overwrite the non-http-only version.
1978 CookieOptions allow_httponly; 1996 CookieOptions allow_httponly;
1979 allow_httponly.set_include_httponly(); 1997 allow_httponly.set_include_httponly();
1980 EXPECT_TRUE( 1998 EXPECT_TRUE(
1981 SetCookieWithOptions(cm, url_google_, 1999 SetCookieWithOptions(cm, url_google_,
1982 "a=val2; path=/path1; httponly; " 2000 "a=val2; path=/path1; httponly; "
1983 "expires=Mon, 18-Apr-22 22:50:14 GMT", 2001 "expires=Mon, 18-Apr-22 22:50:14 GMT",
1984 allow_httponly)); 2002 allow_httponly));
1985 ASSERT_EQ(3u, store->commands().size()); 2003 ASSERT_EQ(3u, store->commands().size());
1986 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type); 2004 EXPECT_EQ(CookieStoreCommand::REMOVE, store->commands()[1].type);
1987 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type); 2005 EXPECT_EQ(CookieStoreCommand::ADD, store->commands()[2].type);
1988 ASSERT_EQ(2u, delegate->changes().size()); 2006 ASSERT_EQ(2u, delegate->changes().size());
1989 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain()); 2007 EXPECT_EQ(url_google_.host(), delegate->changes()[0].first.Domain());
1990 EXPECT_TRUE(delegate->changes()[0].second); 2008 EXPECT_TRUE(delegate->changes()[0].second);
1991 EXPECT_EQ("a", delegate->changes()[0].first.Name()); 2009 EXPECT_EQ("a", delegate->changes()[0].first.Name());
1992 EXPECT_EQ("val1", delegate->changes()[0].first.Value()); 2010 EXPECT_EQ("val1", delegate->changes()[0].first.Value());
1993 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain()); 2011 EXPECT_EQ(url_google_.host(), delegate->changes()[1].first.Domain());
1994 EXPECT_FALSE(delegate->changes()[1].second); 2012 EXPECT_FALSE(delegate->changes()[1].second);
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
2135 CookieOptions options; 2153 CookieOptions options;
2136 2154
2137 // Add in three cookies through every public interface to the 2155 // Add in three cookies through every public interface to the
2138 // CookieMonster and confirm that none of them have duplicate 2156 // CookieMonster and confirm that none of them have duplicate
2139 // creation times. 2157 // creation times.
2140 2158
2141 // SetCookieWithCreationTime and SetCookieWithCreationTimeAndOptions 2159 // SetCookieWithCreationTime and SetCookieWithCreationTimeAndOptions
2142 // are not included as they aren't going to be public for very much 2160 // are not included as they aren't going to be public for very much
2143 // longer. 2161 // longer.
2144 2162
2145 // SetCookie, SetCookies, SetCookiesWithOptions, 2163 // SetCookie, SetCookieWithOptions, SetCookieWithDetails
2146 // SetCookieWithOptions, SetCookieWithDetails
2147 2164
2148 cm->SetCookie(url_google_, "SetCookie1=A"); 2165 SetCookie(cm, url_google_, "SetCookie1=A");
2149 cm->SetCookie(url_google_, "SetCookie2=A"); 2166 SetCookie(cm, url_google_, "SetCookie2=A");
2150 cm->SetCookie(url_google_, "SetCookie3=A"); 2167 SetCookie(cm, url_google_, "SetCookie3=A");
2151
2152 {
2153 std::vector<std::string> cookie_lines;
2154 cookie_lines.push_back("setCookies1=A");
2155 cookie_lines.push_back("setCookies2=A");
2156 cookie_lines.push_back("setCookies4=A");
2157 cm->SetCookies(url_google_, cookie_lines);
2158 }
2159
2160 {
2161 std::vector<std::string> cookie_lines;
2162 cookie_lines.push_back("setCookiesWithOptions1=A");
2163 cookie_lines.push_back("setCookiesWithOptions2=A");
2164 cookie_lines.push_back("setCookiesWithOptions3=A");
2165
2166 cm->SetCookiesWithOptions(url_google_, cookie_lines, options);
2167 }
2168 2168
2169 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions1=A", options); 2169 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions1=A", options);
2170 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions2=A", options); 2170 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions2=A", options);
2171 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions3=A", options); 2171 SetCookieWithOptions(cm, url_google_, "setCookieWithOptions3=A", options);
2172 2172
2173 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails1", "A", 2173 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails1", "A",
2174 ".google.com", "/", Time(), false, false); 2174 ".google.com", "/", Time(), false, false);
2175 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails2", "A", 2175 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails2", "A",
2176 ".google.com", "/", Time(), false, false); 2176 ".google.com", "/", Time(), false, false);
2177 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails3", "A", 2177 SetCookieWithDetails(cm, url_google_, "setCookieWithDetails3", "A",
2178 ".google.com", "/", Time(), false, false); 2178 ".google.com", "/", Time(), false, false);
2179 2179
2180 // Now we check 2180 // Now we check
2181 CookieList cookie_list(GetAllCookies(cm)); 2181 CookieList cookie_list(GetAllCookies(cm));
2182 typedef std::map<int64, CookieMonster::CanonicalCookie> TimeCookieMap; 2182 typedef std::map<int64, CookieMonster::CanonicalCookie> TimeCookieMap;
2183 TimeCookieMap check_map; 2183 TimeCookieMap check_map;
2184 for (CookieList::const_iterator it = cookie_list.begin(); 2184 for (CookieList::const_iterator it = cookie_list.begin();
2185 it != cookie_list.end(); it++) { 2185 it != cookie_list.end(); it++) {
2186 const int64 creation_date = it->CreationDate().ToInternalValue(); 2186 const int64 creation_date = it->CreationDate().ToInternalValue();
2187 TimeCookieMap::const_iterator 2187 TimeCookieMap::const_iterator
2188 existing_cookie_it(check_map.find(creation_date)); 2188 existing_cookie_it(check_map.find(creation_date));
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
2255 base::Time::Now() + base::TimeDelta::FromSeconds(100), 2255 base::Time::Now() + base::TimeDelta::FromSeconds(100),
2256 true, false} 2256 true, false}
2257 }; 2257 };
2258 const int INPUT_DELETE = 1; 2258 const int INPUT_DELETE = 1;
2259 2259
2260 // Create new cookies and flush them to the store. 2260 // Create new cookies and flush them to the store.
2261 { 2261 {
2262 scoped_refptr<CookieMonster> cmout(new CookieMonster(store, NULL)); 2262 scoped_refptr<CookieMonster> cmout(new CookieMonster(store, NULL));
2263 for (const CookiesInputInfo* p = input_info; 2263 for (const CookiesInputInfo* p = input_info;
2264 p < &input_info[ARRAYSIZE_UNSAFE(input_info)]; p++) { 2264 p < &input_info[ARRAYSIZE_UNSAFE(input_info)]; p++) {
2265 EXPECT_TRUE(cmout->SetCookieWithDetails(GURL(p->gurl), p->name, p->value, 2265 EXPECT_TRUE(SetCookieWithDetails(cmout, GURL(p->gurl), p->name, p->value,
2266 p->domain, p->path, p->expires, 2266 p->domain, p->path, p->expires,
2267 p->secure, p->http_only)); 2267 p->secure, p->http_only));
2268 } 2268 }
2269 DeleteCookie(cmout, GURL(std::string(input_info[INPUT_DELETE].gurl) + 2269 DeleteCookie(cmout, GURL(std::string(input_info[INPUT_DELETE].gurl) +
2270 input_info[INPUT_DELETE].path), 2270 input_info[INPUT_DELETE].path),
2271 input_info[INPUT_DELETE].name); 2271 input_info[INPUT_DELETE].name);
2272 } 2272 }
2273 2273
2274 // Create a new cookie monster and make sure that everything is correct 2274 // Create a new cookie monster and make sure that everything is correct
2275 { 2275 {
2276 scoped_refptr<CookieMonster> cmin(new CookieMonster(store, NULL)); 2276 scoped_refptr<CookieMonster> cmin(new CookieMonster(store, NULL));
2277 CookieList cookies(GetAllCookies(cmin)); 2277 CookieList cookies(GetAllCookies(cmin));
(...skipping 18 matching lines...) Expand all
2296 EXPECT_EQ(input->expires.ToInternalValue(), 2296 EXPECT_EQ(input->expires.ToInternalValue(),
2297 output->ExpiryDate().ToInternalValue()); 2297 output->ExpiryDate().ToInternalValue());
2298 } 2298 }
2299 } 2299 }
2300 } 2300 }
2301 2301
2302 TEST_F(CookieMonsterTest, CookieOrdering) { 2302 TEST_F(CookieMonsterTest, CookieOrdering) {
2303 // Put a random set of cookies into a monster and make sure 2303 // Put a random set of cookies into a monster and make sure
2304 // they're returned in the right order. 2304 // they're returned in the right order.
2305 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2305 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2306 EXPECT_TRUE(cm->SetCookie(GURL("http://d.c.b.a.google.com/aa/x.html"), 2306 EXPECT_TRUE(SetCookie(cm, GURL("http://d.c.b.a.google.com/aa/x.html"),
2307 "c=1")); 2307 "c=1"));
2308 EXPECT_TRUE(cm->SetCookie(GURL("http://b.a.google.com/aa/bb/cc/x.html"), 2308 EXPECT_TRUE(SetCookie(cm, GURL("http://b.a.google.com/aa/bb/cc/x.html"),
2309 "d=1; domain=b.a.google.com")); 2309 "d=1; domain=b.a.google.com"));
2310 EXPECT_TRUE(cm->SetCookie(GURL("http://b.a.google.com/aa/bb/cc/x.html"), 2310 EXPECT_TRUE(SetCookie(cm, GURL("http://b.a.google.com/aa/bb/cc/x.html"),
2311 "a=4; domain=b.a.google.com")); 2311 "a=4; domain=b.a.google.com"));
2312 EXPECT_TRUE(cm->SetCookie(GURL("http://c.b.a.google.com/aa/bb/cc/x.html"), 2312 EXPECT_TRUE(SetCookie(cm, GURL("http://c.b.a.google.com/aa/bb/cc/x.html"),
2313 "e=1; domain=c.b.a.google.com")); 2313 "e=1; domain=c.b.a.google.com"));
2314 EXPECT_TRUE(cm->SetCookie(GURL("http://d.c.b.a.google.com/aa/bb/x.html"), 2314 EXPECT_TRUE(SetCookie(cm, GURL("http://d.c.b.a.google.com/aa/bb/x.html"),
2315 "b=1")); 2315 "b=1"));
2316 EXPECT_TRUE(cm->SetCookie(GURL("http://news.bbc.co.uk/midpath/x.html"), 2316 EXPECT_TRUE(SetCookie(cm, GURL("http://news.bbc.co.uk/midpath/x.html"),
2317 "g=10")); 2317 "g=10"));
2318 EXPECT_EQ("d=1; a=4; e=1; b=1; c=1", 2318 EXPECT_EQ("d=1; a=4; e=1; b=1; c=1",
2319 GetCookiesWithOptions(cm, 2319 GetCookiesWithOptions(
2320 GURL("http://d.c.b.a.google.com/aa/bb/cc/dd"), 2320 cm, GURL("http://d.c.b.a.google.com/aa/bb/cc/dd"),
2321 CookieOptions())); 2321 CookieOptions()));
2322 { 2322 {
2323 unsigned int i = 0; 2323 unsigned int i = 0;
2324 CookieList cookies(cm->GetAllCookiesForURL( 2324 CookieList cookies(GetAllCookiesForURL(
2325 GURL("http://d.c.b.a.google.com/aa/bb/cc/dd"))); 2325 cm, GURL("http://d.c.b.a.google.com/aa/bb/cc/dd")));
2326 ASSERT_EQ(5u, cookies.size()); 2326 ASSERT_EQ(5u, cookies.size());
2327 EXPECT_EQ("d", cookies[i++].Name()); 2327 EXPECT_EQ("d", cookies[i++].Name());
2328 EXPECT_EQ("a", cookies[i++].Name()); 2328 EXPECT_EQ("a", cookies[i++].Name());
2329 EXPECT_EQ("e", cookies[i++].Name()); 2329 EXPECT_EQ("e", cookies[i++].Name());
2330 EXPECT_EQ("b", cookies[i++].Name()); 2330 EXPECT_EQ("b", cookies[i++].Name());
2331 EXPECT_EQ("c", cookies[i++].Name()); 2331 EXPECT_EQ("c", cookies[i++].Name());
2332 } 2332 }
2333 2333
2334 { 2334 {
2335 unsigned int i = 0; 2335 unsigned int i = 0;
2336 CookieList cookies(GetAllCookies(cm)); 2336 CookieList cookies(GetAllCookies(cm));
2337 ASSERT_EQ(6u, cookies.size()); 2337 ASSERT_EQ(6u, cookies.size());
2338 EXPECT_EQ("d", cookies[i++].Name()); 2338 EXPECT_EQ("d", cookies[i++].Name());
2339 EXPECT_EQ("a", cookies[i++].Name()); 2339 EXPECT_EQ("a", cookies[i++].Name());
2340 EXPECT_EQ("e", cookies[i++].Name()); 2340 EXPECT_EQ("e", cookies[i++].Name());
2341 EXPECT_EQ("g", cookies[i++].Name()); 2341 EXPECT_EQ("g", cookies[i++].Name());
2342 EXPECT_EQ("b", cookies[i++].Name()); 2342 EXPECT_EQ("b", cookies[i++].Name());
2343 EXPECT_EQ("c", cookies[i++].Name()); 2343 EXPECT_EQ("c", cookies[i++].Name());
2344 } 2344 }
2345 } 2345 }
2346 2346
2347
2348 // Function for creating a CM with a number of cookies in it,
2349 // no store (and hence no ability to affect access time).
2350 static CookieMonster* CreateMonsterForGC(int num_cookies) {
2351 CookieMonster* cm(new CookieMonster(NULL, NULL));
2352 for (int i = 0; i < num_cookies; i++)
2353 cm->SetCookie(GURL(StringPrintf("http://h%05d.izzle", i)), "a=1");
2354 return cm;
2355 }
2356
2357 // This test and CookieMonstertest.TestGCTimes (in cookie_monster_perftest.cc) 2347 // This test and CookieMonstertest.TestGCTimes (in cookie_monster_perftest.cc)
2358 // are somewhat complementary twins. This test is probing for whether 2348 // are somewhat complementary twins. This test is probing for whether
2359 // garbage collection always happens when it should (i.e. that we actually 2349 // garbage collection always happens when it should (i.e. that we actually
2360 // get rid of cookies when we should). The perftest is probing for 2350 // get rid of cookies when we should). The perftest is probing for
2361 // whether garbage collection happens when it shouldn't. See comments 2351 // whether garbage collection happens when it shouldn't. See comments
2362 // before that test for more details. 2352 // before that test for more details.
2363 TEST_F(CookieMonsterTest, GarbageCollectionTriggers) { 2353 TEST_F(CookieMonsterTest, GarbageCollectionTriggers) {
2364 // First we check to make sure that a whole lot of recent cookies 2354 // First we check to make sure that a whole lot of recent cookies
2365 // doesn't get rid of anything after garbage collection is checked for. 2355 // doesn't get rid of anything after garbage collection is checked for.
2366 { 2356 {
2367 scoped_refptr<CookieMonster> cm( 2357 scoped_refptr<CookieMonster> cm(
2368 CreateMonsterForGC(CookieMonster::kMaxCookies * 2)); 2358 CreateMonsterForGC(CookieMonster::kMaxCookies * 2));
2369 EXPECT_EQ(CookieMonster::kMaxCookies * 2, GetAllCookies(cm).size()); 2359 EXPECT_EQ(CookieMonster::kMaxCookies * 2, GetAllCookies(cm).size());
2370 cm->SetCookie(GURL("http://newdomain.com"), "b=2"); 2360 SetCookie(cm, GURL("http://newdomain.com"), "b=2");
2371 EXPECT_EQ(CookieMonster::kMaxCookies * 2 + 1, GetAllCookies(cm).size()); 2361 EXPECT_EQ(CookieMonster::kMaxCookies * 2 + 1, GetAllCookies(cm).size());
2372 } 2362 }
2373 2363
2374 // Now we explore a series of relationships between cookie last access 2364 // Now we explore a series of relationships between cookie last access
2375 // time and size of store to make sure we only get rid of cookies when 2365 // time and size of store to make sure we only get rid of cookies when
2376 // we really should. 2366 // we really should.
2377 const struct TestCase { 2367 const struct TestCase {
2378 int num_cookies; 2368 int num_cookies;
2379 int num_old_cookies; 2369 int num_old_cookies;
2380 int expected_initial_cookies; 2370 int expected_initial_cookies;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
2425 const TestCase *test_case = &test_cases[ci]; 2415 const TestCase *test_case = &test_cases[ci];
2426 scoped_refptr<CookieMonster> cm( 2416 scoped_refptr<CookieMonster> cm(
2427 CreateMonsterFromStoreForGC( 2417 CreateMonsterFromStoreForGC(
2428 test_case->num_cookies, test_case->num_old_cookies, 2418 test_case->num_cookies, test_case->num_old_cookies,
2429 CookieMonster::kSafeFromGlobalPurgeDays * 2)); 2419 CookieMonster::kSafeFromGlobalPurgeDays * 2));
2430 cm->SetExpiryAndKeyScheme(schemes[recent_scheme]); 2420 cm->SetExpiryAndKeyScheme(schemes[recent_scheme]);
2431 EXPECT_EQ(test_case->expected_initial_cookies, 2421 EXPECT_EQ(test_case->expected_initial_cookies,
2432 static_cast<int>(GetAllCookies(cm).size())) 2422 static_cast<int>(GetAllCookies(cm).size()))
2433 << "For test case " << ci; 2423 << "For test case " << ci;
2434 // Will trigger GC 2424 // Will trigger GC
2435 cm->SetCookie(GURL("http://newdomain.com"), "b=2"); 2425 SetCookie(cm, GURL("http://newdomain.com"), "b=2");
2436 EXPECT_EQ(test_case->expected_cookies_after_set[recent_scheme], 2426 EXPECT_EQ(test_case->expected_cookies_after_set[recent_scheme],
2437 static_cast<int>((GetAllCookies(cm).size()))) 2427 static_cast<int>((GetAllCookies(cm).size())))
2438 << "For test case (" << ci << ", " << recent_scheme << ")"; 2428 << "For test case (" << ci << ", " << recent_scheme << ")";
2439 } 2429 }
2440 } 2430 }
2441 } 2431 }
2442 2432
2443 // This test checks that setting a cookie forcing it to be a session only 2433 // This test checks that setting a cookie forcing it to be a session only
2444 // cookie works as expected. 2434 // cookie works as expected.
2445 TEST_F(CookieMonsterTest, ForceSessionOnly) { 2435 TEST_F(CookieMonsterTest, ForceSessionOnly) {
2446 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2436 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2447 CookieOptions options; 2437 CookieOptions options;
2448 2438
2449 // Set a persistent cookie, but force it to be a session cookie. 2439 // Set a persistent cookie, but force it to be a session cookie.
2450 options.set_force_session(); 2440 options.set_force_session();
2451 ASSERT_TRUE(SetCookieWithOptions(cm, url_google_, 2441 ASSERT_TRUE(SetCookieWithOptions(
2442 cm, url_google_,
2452 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT", 2443 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT",
2453 options)); 2444 options));
2454 2445
2455 // Get the canonical cookie. 2446 // Get the canonical cookie.
2456 CookieList cookie_list = GetAllCookies(cm); 2447 CookieList cookie_list = GetAllCookies(cm);
2457 ASSERT_EQ(1U, cookie_list.size()); 2448 ASSERT_EQ(1U, cookie_list.size());
2458 ASSERT_FALSE(cookie_list[0].IsPersistent()); 2449 ASSERT_FALSE(cookie_list[0].IsPersistent());
2459 2450
2460 // Use a past expiry date to delete the cookie, but force it to session only. 2451 // Use a past expiry date to delete the cookie, but force it to session only.
2461 ASSERT_TRUE(SetCookieWithOptions(cm, url_google_, 2452 ASSERT_TRUE(SetCookieWithOptions(
2453 cm, url_google_,
2462 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-1977 22:50:13 GMT", 2454 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-1977 22:50:13 GMT",
2463 options)); 2455 options));
2464 2456
2465 // Check that the cookie was deleted. 2457 // Check that the cookie was deleted.
2466 cookie_list = GetAllCookies(cm); 2458 cookie_list = GetAllCookies(cm);
2467 ASSERT_EQ(0U, cookie_list.size()); 2459 ASSERT_EQ(0U, cookie_list.size());
2468 } 2460 }
2469 2461
2470 // This test checks that keep expired cookies flag is working. 2462 // This test checks that keep expired cookies flag is working.
2471 TEST_F(CookieMonsterTest, KeepExpiredCookies) { 2463 TEST_F(CookieMonsterTest, KeepExpiredCookies) {
2472 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2464 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2473 cm->SetKeepExpiredCookies(); 2465 cm->SetKeepExpiredCookies();
2474 CookieOptions options; 2466 CookieOptions options;
2475 2467
2476 // Set a persistent cookie. 2468 // Set a persistent cookie.
2477 ASSERT_TRUE(SetCookieWithOptions(cm, url_google_, 2469 ASSERT_TRUE(SetCookieWithOptions(
2470 cm, url_google_,
2478 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT", 2471 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-22 22:50:13 GMT",
2479 options)); 2472 options));
2480 2473
2481 // Get the canonical cookie. 2474 // Get the canonical cookie.
2482 CookieList cookie_list = GetAllCookies(cm); 2475 CookieList cookie_list = GetAllCookies(cm);
2483 ASSERT_EQ(1U, cookie_list.size()); 2476 ASSERT_EQ(1U, cookie_list.size());
2484 2477
2485 // Use a past expiry date to delete the cookie. 2478 // Use a past expiry date to delete the cookie.
2486 ASSERT_TRUE(SetCookieWithOptions(cm, url_google_, 2479 ASSERT_TRUE(SetCookieWithOptions(
2480 cm, url_google_,
2487 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-1977 22:50:13 GMT", 2481 std::string(kValidCookieLine) + "; expires=Mon, 18-Apr-1977 22:50:13 GMT",
2488 options)); 2482 options));
2489 2483
2490 // Check that the cookie with the past expiry date is still there. 2484 // Check that the cookie with the past expiry date is still there.
2491 // GetAllCookies() also triggers garbage collection. 2485 // GetAllCookies() also triggers garbage collection.
2492 cookie_list = GetAllCookies(cm); 2486 cookie_list = GetAllCookies(cm);
2493 ASSERT_EQ(1U, cookie_list.size()); 2487 ASSERT_EQ(1U, cookie_list.size());
2494 ASSERT_TRUE(cookie_list[0].IsExpired(Time::Now())); 2488 ASSERT_TRUE(cookie_list[0].IsExpired(Time::Now()));
2495 } 2489 }
2496 2490
2497 namespace { 2491 namespace {
2498 2492
2499 // Mock PersistentCookieStore that keeps track of the number of Flush() calls. 2493 // Mock PersistentCookieStore that keeps track of the number of Flush() calls.
2500 class FlushablePersistentStore : public CookieMonster::PersistentCookieStore { 2494 class FlushablePersistentStore : public CookieMonster::PersistentCookieStore {
2501 public: 2495 public:
2502 FlushablePersistentStore() : flush_count_(0) {} 2496 FlushablePersistentStore() : flush_count_(0) {}
2503 2497
2504 bool Load(std::vector<CookieMonster::CanonicalCookie*>*) { 2498 bool Load(const LoadedCallback& loaded_callback) {
2499 std::vector<CookieMonster::CanonicalCookie*> out_cookies;
2500 loaded_callback.Run(out_cookies);
2505 return false; 2501 return false;
2506 } 2502 }
2507 2503
2508 void AddCookie(const CookieMonster::CanonicalCookie&) {} 2504 void AddCookie(const CookieMonster::CanonicalCookie&) {}
2509 void UpdateCookieAccessTime(const CookieMonster::CanonicalCookie&) {} 2505 void UpdateCookieAccessTime(const CookieMonster::CanonicalCookie&) {}
2510 void DeleteCookie(const CookieMonster::CanonicalCookie&) {} 2506 void DeleteCookie(const CookieMonster::CanonicalCookie&) {}
2511 void SetClearLocalStateOnExit(bool clear_local_state) {} 2507 void SetClearLocalStateOnExit(bool clear_local_state) {}
2512 2508
2513 void Flush(Task* completion_callback) { 2509 void Flush(Task* completion_callback) {
2514 ++flush_count_; 2510 ++flush_count_;
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
2644 cm, GURL("http://fake.a.url"), "a", "b", "a.url", "/", 2640 cm, GURL("http://fake.a.url"), "a", "b", "a.url", "/",
2645 base::Time::Now() + base::TimeDelta::FromMinutes(59), 2641 base::Time::Now() + base::TimeDelta::FromMinutes(59),
2646 false, false)); 2642 false, false));
2647 2643
2648 base::Histogram::SampleSet histogram_set_2; 2644 base::Histogram::SampleSet histogram_set_2;
2649 expired_histogram->SnapshotSample(&histogram_set_2); 2645 expired_histogram->SnapshotSample(&histogram_set_2);
2650 EXPECT_EQ(histogram_set_1.TotalCount() + 1, 2646 EXPECT_EQ(histogram_set_1.TotalCount() + 1,
2651 histogram_set_2.TotalCount()); 2647 histogram_set_2.TotalCount());
2652 2648
2653 // kValidCookieLine creates a session cookie. 2649 // kValidCookieLine creates a session cookie.
2654 ASSERT_TRUE(cm->SetCookie(url_google_, kValidCookieLine)); 2650 ASSERT_TRUE(SetCookie(cm, url_google_, kValidCookieLine));
2655 expired_histogram->SnapshotSample(&histogram_set_1); 2651 expired_histogram->SnapshotSample(&histogram_set_1);
2656 EXPECT_EQ(histogram_set_2.TotalCount(), 2652 EXPECT_EQ(histogram_set_2.TotalCount(),
2657 histogram_set_1.TotalCount()); 2653 histogram_set_1.TotalCount());
2658 } 2654 }
2659 2655
2660 namespace { 2656 namespace {
2661 2657
2662 class MultiThreadedCookieMonsterTest : public CookieMonsterTest { 2658 class MultiThreadedCookieMonsterTest : public CookieMonsterTest {
2663 public: 2659 public:
2664 MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {} 2660 MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {}
2665 2661
2666 // Helper methods for calling the asynchronous CookieMonster methods 2662 // Helper methods for calling the asynchronous CookieMonster methods
2667 // from a different thread. 2663 // from a different thread.
2668 2664
2669 void GetCookiesTask(CookieMonster* cm, 2665 void GetCookiesTask(CookieMonster* cm,
2670 const GURL& url, 2666 const GURL& url,
2671 GetCookieStringCallback* callback) { 2667 GetCookieStringCallback* callback) {
2672 cm->GetCookiesAsync( 2668 cm->GetCookiesWithOptionsAsync(
2673 url, 2669 url, CookieOptions(),
2674 base::Bind(&GetCookieStringCallback::Run, base::Unretained(callback))); 2670 base::Bind(&GetCookieStringCallback::Run, base::Unretained(callback)));
2675 } 2671 }
2676 2672
2677 void GetCookiesWithOptionsTask(CookieMonster* cm, 2673 void GetCookiesWithOptionsTask(CookieMonster* cm,
2678 const GURL& url, 2674 const GURL& url,
2679 const CookieOptions& options, 2675 const CookieOptions& options,
2680 GetCookieStringCallback* callback) { 2676 GetCookieStringCallback* callback) {
2681 cm->GetCookiesWithOptionsAsync( 2677 cm->GetCookiesWithOptionsAsync(
2682 url, options, 2678 url, options,
2683 base::Bind(&GetCookieStringCallback::Run, base::Unretained(callback))); 2679 base::Bind(&GetCookieStringCallback::Run, base::Unretained(callback)));
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
2747 const std::string& cookie_name, 2743 const std::string& cookie_name,
2748 DeleteCookieCallback* callback) { 2744 DeleteCookieCallback* callback) {
2749 cm->DeleteCookieAsync( 2745 cm->DeleteCookieAsync(
2750 url, cookie_name, 2746 url, cookie_name,
2751 base::Bind(&DeleteCookieCallback::Run, base::Unretained(callback))); 2747 base::Bind(&DeleteCookieCallback::Run, base::Unretained(callback)));
2752 } 2748 }
2753 2749
2754 void DeleteAllCreatedBetweenTask(CookieMonster* cm, 2750 void DeleteAllCreatedBetweenTask(CookieMonster* cm,
2755 const base::Time& delete_begin, 2751 const base::Time& delete_begin,
2756 const base::Time& delete_end, 2752 const base::Time& delete_end,
2757 bool sync_to_store,
2758 DeleteCallback* callback) { 2753 DeleteCallback* callback) {
2759 cm->DeleteAllCreatedBetweenAsync( 2754 cm->DeleteAllCreatedBetweenAsync(
2760 delete_begin, delete_end, sync_to_store, 2755 delete_begin, delete_end,
2761 base::Bind(&DeleteCallback::Run, 2756 base::Bind(&DeleteCallback::Run,
2762 base::Unretained(callback))); 2757 base::Unretained(callback)));
2763 } 2758 }
2764 2759
2765 void DeleteAllForHostTask(CookieMonster* cm, 2760 void DeleteAllForHostTask(CookieMonster* cm,
2766 const GURL& url, 2761 const GURL& url,
2767 DeleteCallback* callback) { 2762 DeleteCallback* callback) {
2768 cm->DeleteAllForHostAsync( 2763 cm->DeleteAllForHostAsync(
2769 url, 2764 url,
2770 base::Bind(&DeleteCallback::Run, base::Unretained(callback))); 2765 base::Bind(&DeleteCallback::Run, base::Unretained(callback)));
(...skipping 19 matching lines...) Expand all
2790 }; 2785 };
2791 2786
2792 } // namespace 2787 } // namespace
2793 2788
2794 // TODO(ycxiao): Eventually, we will need to create a separate thread, create 2789 // TODO(ycxiao): Eventually, we will need to create a separate thread, create
2795 // the cookie monster on that thread (or at least its store, i.e., the DB 2790 // the cookie monster on that thread (or at least its store, i.e., the DB
2796 // thread). 2791 // thread).
2797 2792
2798 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookies) { 2793 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookies) {
2799 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2794 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2800 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 2795 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2801 EXPECT_EQ("A=B", GetCookies(cm, url_google_)); 2796 EXPECT_EQ("A=B", GetCookies(cm, url_google_));
2802 GetCookieStringCallback callback(&other_thread_); 2797 GetCookieStringCallback callback(&other_thread_);
2803 base::Closure task = base::Bind( 2798 base::Closure task = base::Bind(
2804 &net::MultiThreadedCookieMonsterTest::GetCookiesTask, 2799 &net::MultiThreadedCookieMonsterTest::GetCookiesTask,
2805 base::Unretained(this), 2800 base::Unretained(this),
2806 cm, url_google_, &callback); 2801 cm, url_google_, &callback);
2807 RunOnOtherThread(task); 2802 RunOnOtherThread(task);
2808 EXPECT_TRUE(callback.did_run()); 2803 EXPECT_TRUE(callback.did_run());
2809 EXPECT_EQ("A=B", callback.cookie()); 2804 EXPECT_EQ("A=B", callback.cookie());
2810 } 2805 }
2811 2806
2812 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookiesWithOptions) { 2807 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookiesWithOptions) {
2813 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2808 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2814 CookieOptions options; 2809 CookieOptions options;
2815 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 2810 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2816 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options)); 2811 EXPECT_EQ("A=B", GetCookiesWithOptions(cm, url_google_, options));
2817 GetCookieStringCallback callback(&other_thread_); 2812 GetCookieStringCallback callback(&other_thread_);
2818 base::Closure task = base::Bind( 2813 base::Closure task = base::Bind(
2819 &net::MultiThreadedCookieMonsterTest::GetCookiesWithOptionsTask, 2814 &net::MultiThreadedCookieMonsterTest::GetCookiesWithOptionsTask,
2820 base::Unretained(this), 2815 base::Unretained(this),
2821 cm, url_google_, options, &callback); 2816 cm, url_google_, options, &callback);
2822 RunOnOtherThread(task); 2817 RunOnOtherThread(task);
2823 EXPECT_TRUE(callback.did_run()); 2818 EXPECT_TRUE(callback.did_run());
2824 EXPECT_EQ("A=B", callback.cookie()); 2819 EXPECT_EQ("A=B", callback.cookie());
2825 } 2820 }
2826 2821
2827 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookiesWithInfo) { 2822 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetCookiesWithInfo) {
2828 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2823 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2829 CookieOptions options; 2824 CookieOptions options;
2830 std::string cookie_line; 2825 std::string cookie_line;
2831 std::vector<CookieStore::CookieInfo> cookie_infos; 2826 std::vector<CookieStore::CookieInfo> cookie_infos;
2832 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 2827 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2833 GetCookiesWithInfo(cm, url_google_, options, &cookie_line, &cookie_infos); 2828 GetCookiesWithInfo(cm, url_google_, options, &cookie_line, &cookie_infos);
2834 EXPECT_EQ("A=B", cookie_line); 2829 EXPECT_EQ("A=B", cookie_line);
2835 EXPECT_EQ(1U, cookie_infos.size()); 2830 EXPECT_EQ(1U, cookie_infos.size());
2836 EXPECT_EQ("A", cookie_infos[0].name); 2831 EXPECT_EQ("A", cookie_infos[0].name);
2837 EXPECT_EQ("", cookie_infos[0].mac_key); 2832 EXPECT_EQ("", cookie_infos[0].mac_key);
2838 EXPECT_EQ("", cookie_infos[0].mac_algorithm); 2833 EXPECT_EQ("", cookie_infos[0].mac_algorithm);
2839 GetCookiesWithInfoCallback callback(&other_thread_); 2834 GetCookiesWithInfoCallback callback(&other_thread_);
2840 base::Closure task = 2835 base::Closure task =
2841 base::Bind(&net::MultiThreadedCookieMonsterTest::GetCookiesWithInfoTask, 2836 base::Bind(&net::MultiThreadedCookieMonsterTest::GetCookiesWithInfoTask,
2842 base::Unretained(this), 2837 base::Unretained(this),
2843 cm, url_google_, options, &callback); 2838 cm, url_google_, options, &callback);
2844 RunOnOtherThread(task); 2839 RunOnOtherThread(task);
2845 EXPECT_TRUE(callback.did_run()); 2840 EXPECT_TRUE(callback.did_run());
2846 EXPECT_EQ("A=B", callback.cookie_line()); 2841 EXPECT_EQ("A=B", callback.cookie_line());
2847 EXPECT_EQ(1U, callback.cookie_info().size()); 2842 EXPECT_EQ(1U, callback.cookie_info().size());
2848 EXPECT_EQ("A", callback.cookie_info()[0].name); 2843 EXPECT_EQ("A", callback.cookie_info()[0].name);
2849 EXPECT_EQ("", callback.cookie_info()[0].mac_key); 2844 EXPECT_EQ("", callback.cookie_info()[0].mac_key);
2850 EXPECT_EQ("", callback.cookie_info()[0].mac_algorithm); 2845 EXPECT_EQ("", callback.cookie_info()[0].mac_algorithm);
2851 } 2846 }
2852 2847
2853 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookies) { 2848 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookies) {
2854 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2849 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2855 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 2850 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2856 CookieList cookies = GetAllCookies(cm); 2851 CookieList cookies = GetAllCookies(cm);
2857 CookieList::const_iterator it = cookies.begin(); 2852 CookieList::const_iterator it = cookies.begin();
2858 ASSERT_TRUE(it != cookies.end()); 2853 ASSERT_TRUE(it != cookies.end());
2859 EXPECT_EQ("www.google.izzle", it->Domain()); 2854 EXPECT_EQ("www.google.izzle", it->Domain());
2860 EXPECT_EQ("A", it->Name()); 2855 EXPECT_EQ("A", it->Name());
2861 ASSERT_TRUE(++it == cookies.end()); 2856 ASSERT_TRUE(++it == cookies.end());
2862 GetCookieListCallback callback(&other_thread_); 2857 GetCookieListCallback callback(&other_thread_);
2863 base::Closure task = 2858 base::Closure task =
2864 base::Bind(&net::MultiThreadedCookieMonsterTest::GetAllCookiesTask, 2859 base::Bind(&net::MultiThreadedCookieMonsterTest::GetAllCookiesTask,
2865 base::Unretained(this), 2860 base::Unretained(this),
2866 cm, &callback); 2861 cm, &callback);
2867 RunOnOtherThread(task); 2862 RunOnOtherThread(task);
2868 EXPECT_TRUE(callback.did_run()); 2863 EXPECT_TRUE(callback.did_run());
2869 it = callback.cookies().begin(); 2864 it = callback.cookies().begin();
2870 ASSERT_TRUE(it != callback.cookies().end()); 2865 ASSERT_TRUE(it != callback.cookies().end());
2871 EXPECT_EQ("www.google.izzle", it->Domain()); 2866 EXPECT_EQ("www.google.izzle", it->Domain());
2872 EXPECT_EQ("A", it->Name()); 2867 EXPECT_EQ("A", it->Name());
2873 ASSERT_TRUE(++it == callback.cookies().end()); 2868 ASSERT_TRUE(++it == callback.cookies().end());
2874 } 2869 }
2875 2870
2876 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURL) { 2871 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURL) {
2877 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2872 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2878 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 2873 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2879 CookieList cookies = GetAllCookiesForURL(cm, url_google_); 2874 CookieList cookies = GetAllCookiesForURL(cm, url_google_);
2880 CookieList::const_iterator it = cookies.begin(); 2875 CookieList::const_iterator it = cookies.begin();
2881 ASSERT_TRUE(it != cookies.end()); 2876 ASSERT_TRUE(it != cookies.end());
2882 EXPECT_EQ("www.google.izzle", it->Domain()); 2877 EXPECT_EQ("www.google.izzle", it->Domain());
2883 EXPECT_EQ("A", it->Name()); 2878 EXPECT_EQ("A", it->Name());
2884 ASSERT_TRUE(++it == cookies.end()); 2879 ASSERT_TRUE(++it == cookies.end());
2885 GetCookieListCallback callback(&other_thread_); 2880 GetCookieListCallback callback(&other_thread_);
2886 base::Closure task = 2881 base::Closure task =
2887 base::Bind(&net::MultiThreadedCookieMonsterTest::GetAllCookiesForURLTask, 2882 base::Bind(&net::MultiThreadedCookieMonsterTest::GetAllCookiesForURLTask,
2888 base::Unretained(this), 2883 base::Unretained(this),
2889 cm, url_google_, &callback); 2884 cm, url_google_, &callback);
2890 RunOnOtherThread(task); 2885 RunOnOtherThread(task);
2891 EXPECT_TRUE(callback.did_run()); 2886 EXPECT_TRUE(callback.did_run());
2892 it = callback.cookies().begin(); 2887 it = callback.cookies().begin();
2893 ASSERT_TRUE(it != callback.cookies().end()); 2888 ASSERT_TRUE(it != callback.cookies().end());
2894 EXPECT_EQ("www.google.izzle", it->Domain()); 2889 EXPECT_EQ("www.google.izzle", it->Domain());
2895 EXPECT_EQ("A", it->Name()); 2890 EXPECT_EQ("A", it->Name());
2896 ASSERT_TRUE(++it == callback.cookies().end()); 2891 ASSERT_TRUE(++it == callback.cookies().end());
2897 } 2892 }
2898 2893
2899 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURLWithOpt) { 2894 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURLWithOpt) {
2900 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2895 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2901 EXPECT_TRUE(cm->SetCookie(url_google_, "A=B")); 2896 EXPECT_TRUE(SetCookie(cm, url_google_, "A=B"));
2902 CookieOptions options; 2897 CookieOptions options;
2903 CookieList cookies = 2898 CookieList cookies =
2904 GetAllCookiesForURLWithOptions(cm, url_google_, options); 2899 GetAllCookiesForURLWithOptions(cm, url_google_, options);
2905 CookieList::const_iterator it = cookies.begin(); 2900 CookieList::const_iterator it = cookies.begin();
2906 ASSERT_TRUE(it != cookies.end()); 2901 ASSERT_TRUE(it != cookies.end());
2907 EXPECT_EQ("www.google.izzle", it->Domain()); 2902 EXPECT_EQ("www.google.izzle", it->Domain());
2908 EXPECT_EQ("A", it->Name()); 2903 EXPECT_EQ("A", it->Name());
2909 ASSERT_TRUE(++it == cookies.end()); 2904 ASSERT_TRUE(++it == cookies.end());
2910 GetCookieListCallback callback(&other_thread_); 2905 GetCookieListCallback callback(&other_thread_);
2911 base::Closure task = base::Bind( 2906 base::Closure task = base::Bind(
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2965 RunOnOtherThread(task); 2960 RunOnOtherThread(task);
2966 EXPECT_TRUE(callback.did_run()); 2961 EXPECT_TRUE(callback.did_run());
2967 } 2962 }
2968 2963
2969 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllCreatedBetween) { 2964 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllCreatedBetween) {
2970 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2965 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2971 CookieOptions options; 2966 CookieOptions options;
2972 Time now = Time::Now(); 2967 Time now = Time::Now();
2973 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options)); 2968 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options));
2974 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99), 2969 EXPECT_EQ(1, DeleteAllCreatedBetween(cm, now - TimeDelta::FromDays(99),
2975 Time(), false)); 2970 Time()));
2976 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options)); 2971 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options));
2977 DeleteCallback callback(&other_thread_); 2972 DeleteCallback callback(&other_thread_);
2978 base::Closure task = base::Bind( 2973 base::Closure task = base::Bind(
2979 &net::MultiThreadedCookieMonsterTest::DeleteAllCreatedBetweenTask, 2974 &net::MultiThreadedCookieMonsterTest::DeleteAllCreatedBetweenTask,
2980 base::Unretained(this), 2975 base::Unretained(this),
2981 cm, now - TimeDelta::FromDays(99), 2976 cm, now - TimeDelta::FromDays(99),
2982 Time(), false, &callback); 2977 Time(), &callback);
2983 RunOnOtherThread(task); 2978 RunOnOtherThread(task);
2984 EXPECT_TRUE(callback.did_run()); 2979 EXPECT_TRUE(callback.did_run());
2985 EXPECT_EQ(1, callback.num_deleted()); 2980 EXPECT_EQ(1, callback.num_deleted());
2986 } 2981 }
2987 2982
2988 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllForHost) { 2983 TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllForHost) {
2989 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2984 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2990 CookieOptions options; 2985 CookieOptions options;
2991 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options)); 2986 EXPECT_TRUE(SetCookieWithOptions(cm, url_google_, "A=B", options));
2992 EXPECT_EQ(1, DeleteAllForHost(cm, url_google_)); 2987 EXPECT_EQ(1, DeleteAllForHost(cm, url_google_));
(...skipping 23 matching lines...) Expand all
3016 base::Closure task = base::Bind( 3011 base::Closure task = base::Bind(
3017 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask, 3012 &net::MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask,
3018 base::Unretained(this), 3013 base::Unretained(this),
3019 cm, *it, &callback); 3014 cm, *it, &callback);
3020 RunOnOtherThread(task); 3015 RunOnOtherThread(task);
3021 EXPECT_TRUE(callback.did_run()); 3016 EXPECT_TRUE(callback.did_run());
3022 EXPECT_TRUE(callback.result()); 3017 EXPECT_TRUE(callback.result());
3023 } 3018 }
3024 3019
3025 } // namespace net 3020 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698