| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h" | 5 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "net/cookies/canonical_cookie.h" | 11 #include "net/cookies/canonical_cookie.h" |
| 12 #include "net/cookies/parsed_cookie.h" | 12 #include "net/cookies/parsed_cookie.h" |
| 13 #include "net/url_request/url_request_context_getter.h" | 13 #include "net/url_request/url_request_context_getter.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // Test expectations for a given cookie. |
| 19 class CookieExpectation { |
| 20 public: |
| 21 CookieExpectation() : matched_(false) {} |
| 22 |
| 23 bool MatchesCookie(const net::CanonicalCookie& cookie) const { |
| 24 if (!source_.empty() && source_ != cookie.Source()) |
| 25 return false; |
| 26 if (!domain_.empty() && domain_ != cookie.Domain()) |
| 27 return false; |
| 28 if (!path_.empty() && path_ != cookie.Path()) |
| 29 return false; |
| 30 if (!name_.empty() && name_ != cookie.Name()) |
| 31 return false; |
| 32 if (!value_.empty() && value_ != cookie.Value()) |
| 33 return false; |
| 34 return true; |
| 35 } |
| 36 |
| 37 std::string source_; |
| 38 std::string domain_; |
| 39 std::string path_; |
| 40 std::string name_; |
| 41 std::string value_; |
| 42 bool matched_; |
| 43 }; |
| 44 |
| 45 // Matches a CookieExpectation against a Cookie. |
| 46 class CookieMatcher { |
| 47 public: |
| 48 explicit CookieMatcher(const net::CanonicalCookie& cookie) |
| 49 : cookie_(cookie) {} |
| 50 bool operator()(const CookieExpectation& expectation) { |
| 51 return expectation.MatchesCookie(cookie_); |
| 52 } |
| 53 net::CanonicalCookie cookie_; |
| 54 }; |
| 55 |
| 56 // Unary predicate to determine whether an expectation has been matched. |
| 57 bool ExpectationIsMatched(const CookieExpectation& expectation) { |
| 58 return expectation.matched_; |
| 59 } |
| 60 |
| 18 class BrowsingDataCookieHelperTest : public testing::Test { | 61 class BrowsingDataCookieHelperTest : public testing::Test { |
| 19 public: | 62 public: |
| 20 BrowsingDataCookieHelperTest() | 63 BrowsingDataCookieHelperTest() |
| 21 : testing_profile_(new TestingProfile()) { | 64 : testing_profile_(new TestingProfile()) { |
| 22 } | 65 } |
| 23 | 66 |
| 67 virtual void SetUp() OVERRIDE { cookie_expectations_.clear(); } |
| 68 |
| 69 // Adds an expectation for a cookie that satisfies the given parameters. |
| 70 void AddCookieExpectation(const char* source, |
| 71 const char* domain, |
| 72 const char* path, |
| 73 const char* name, |
| 74 const char* value) { |
| 75 CookieExpectation matcher; |
| 76 if (source) |
| 77 matcher.source_ = source; |
| 78 if (domain) |
| 79 matcher.domain_ = domain; |
| 80 if (path) |
| 81 matcher.path_ = path; |
| 82 if (name) |
| 83 matcher.name_ = name; |
| 84 if (value) |
| 85 matcher.value_ = value; |
| 86 cookie_expectations_.push_back(matcher); |
| 87 } |
| 88 |
| 89 // Checks the existing expectations, and then clears all existing |
| 90 // expectations. |
| 91 void CheckCookieExpectations() { |
| 92 ASSERT_EQ(cookie_expectations_.size(), cookie_list_.size()); |
| 93 |
| 94 // For each cookie, look for a matching expectation. |
| 95 for (net::CookieList::iterator it = cookie_list_.begin(); |
| 96 it != cookie_list_.end(); |
| 97 ++it) { |
| 98 CookieMatcher matcher(*it); |
| 99 std::vector<CookieExpectation>::iterator match = std::find_if( |
| 100 cookie_expectations_.begin(), cookie_expectations_.end(), matcher); |
| 101 if (match != cookie_expectations_.end()) |
| 102 match->matched_ = true; |
| 103 } |
| 104 |
| 105 // Check that each expectation has been matched. |
| 106 unsigned long match_count = std::count_if(cookie_expectations_.begin(), |
| 107 cookie_expectations_.end(), |
| 108 ExpectationIsMatched); |
| 109 EXPECT_EQ(cookie_expectations_.size(), match_count); |
| 110 |
| 111 cookie_expectations_.clear(); |
| 112 } |
| 113 |
| 24 void CreateCookiesForTest() { | 114 void CreateCookiesForTest() { |
| 25 scoped_refptr<net::CookieMonster> cookie_monster = | 115 scoped_refptr<net::CookieMonster> cookie_monster = |
| 26 testing_profile_->GetCookieMonster(); | 116 testing_profile_->GetCookieMonster(); |
| 27 cookie_monster->SetCookieWithOptionsAsync( | 117 cookie_monster->SetCookieWithOptionsAsync( |
| 28 GURL("http://www.google.com"), "A=1", net::CookieOptions(), | 118 GURL("http://www.google.com"), "A=1", net::CookieOptions(), |
| 29 net::CookieMonster::SetCookiesCallback()); | 119 net::CookieMonster::SetCookiesCallback()); |
| 30 cookie_monster->SetCookieWithOptionsAsync( | 120 cookie_monster->SetCookieWithOptionsAsync( |
| 31 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(), | 121 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(), |
| 32 net::CookieMonster::SetCookiesCallback()); | 122 net::CookieMonster::SetCookiesCallback()); |
| 33 } | 123 } |
| 34 | 124 |
| 35 void CreateCookiesForDomainCookieTest() { | 125 void CreateCookiesForDomainCookieTest() { |
| 36 scoped_refptr<net::CookieMonster> cookie_monster = | 126 scoped_refptr<net::CookieMonster> cookie_monster = |
| 37 testing_profile_->GetCookieMonster(); | 127 testing_profile_->GetCookieMonster(); |
| 38 cookie_monster->SetCookieWithOptionsAsync( | 128 cookie_monster->SetCookieWithOptionsAsync( |
| 39 GURL("http://www.google.com"), "A=1", net::CookieOptions(), | 129 GURL("http://www.google.com"), "A=1", net::CookieOptions(), |
| 40 net::CookieMonster::SetCookiesCallback()); | 130 net::CookieMonster::SetCookiesCallback()); |
| 41 cookie_monster->SetCookieWithOptionsAsync( | 131 cookie_monster->SetCookieWithOptionsAsync( |
| 42 GURL("http://www.google.com"), "A=2; Domain=.www.google.com ", | 132 GURL("http://www.google.com"), "A=2; Domain=.www.google.com ", |
| 43 net::CookieOptions(), net::CookieMonster::SetCookiesCallback()); | 133 net::CookieOptions(), net::CookieMonster::SetCookiesCallback()); |
| 44 } | 134 } |
| 45 | 135 |
| 46 void FetchCallback(const net::CookieList& cookies) { | 136 void FetchCallback(const net::CookieList& cookies) { |
| 47 ASSERT_EQ(2UL, cookies.size()); | |
| 48 cookie_list_ = cookies; | 137 cookie_list_ = cookies; |
| 49 net::CookieList::const_iterator it = cookies.begin(); | |
| 50 | 138 |
| 51 // Correct because fetching cookies will get a sorted cookie list. | 139 AddCookieExpectation(NULL, "www.google.com", NULL, "A", NULL); |
| 52 ASSERT_TRUE(it != cookies.end()); | 140 AddCookieExpectation(NULL, "www.gmail.google.com", NULL, "B", NULL); |
| 53 EXPECT_EQ("www.google.com", it->Domain()); | 141 CheckCookieExpectations(); |
| 54 EXPECT_EQ("A", it->Name()); | |
| 55 | |
| 56 ASSERT_TRUE(++it != cookies.end()); | |
| 57 EXPECT_EQ("www.gmail.google.com", it->Domain()); | |
| 58 EXPECT_EQ("B", it->Name()); | |
| 59 | |
| 60 ASSERT_TRUE(++it == cookies.end()); | |
| 61 } | 142 } |
| 62 | 143 |
| 63 void DomainCookieCallback(const net::CookieList& cookies) { | 144 void DomainCookieCallback(const net::CookieList& cookies) { |
| 64 ASSERT_EQ(2UL, cookies.size()); | |
| 65 cookie_list_ = cookies; | 145 cookie_list_ = cookies; |
| 66 net::CookieList::const_iterator it = cookies.begin(); | |
| 67 | 146 |
| 68 // Correct because fetching cookies will get a sorted cookie list. | 147 AddCookieExpectation(NULL, "www.google.com", NULL, "A", "1"); |
| 69 ASSERT_TRUE(it != cookies.end()); | 148 AddCookieExpectation(NULL, ".www.google.com", NULL, "A", "2"); |
| 70 EXPECT_EQ("www.google.com", it->Domain()); | 149 CheckCookieExpectations(); |
| 71 EXPECT_EQ("A", it->Name()); | |
| 72 EXPECT_EQ("1", it->Value()); | |
| 73 | |
| 74 ASSERT_TRUE(++it != cookies.end()); | |
| 75 EXPECT_EQ(".www.google.com", it->Domain()); | |
| 76 EXPECT_EQ("A", it->Name()); | |
| 77 EXPECT_EQ("2", it->Value()); | |
| 78 | |
| 79 ASSERT_TRUE(++it == cookies.end()); | |
| 80 } | 150 } |
| 81 | 151 |
| 82 void DeleteCallback(const net::CookieList& cookies) { | 152 void DeleteCallback(const net::CookieList& cookies) { |
| 83 ASSERT_EQ(1UL, cookies.size()); | 153 cookie_list_ = cookies; |
| 84 net::CookieList::const_iterator it = cookies.begin(); | 154 AddCookieExpectation(NULL, "www.gmail.google.com", NULL, "B", NULL); |
| 85 | 155 CheckCookieExpectations(); |
| 86 ASSERT_TRUE(it != cookies.end()); | |
| 87 EXPECT_EQ("www.gmail.google.com", it->Domain()); | |
| 88 EXPECT_EQ("B", it->Name()); | |
| 89 | |
| 90 ASSERT_TRUE(++it == cookies.end()); | |
| 91 } | 156 } |
| 92 | 157 |
| 93 void CannedUniqueCallback(const net::CookieList& cookies) { | 158 void CannedUniqueCallback(const net::CookieList& cookies) { |
| 94 EXPECT_EQ(1UL, cookies.size()); | |
| 95 cookie_list_ = cookies; | 159 cookie_list_ = cookies; |
| 96 net::CookieList::const_iterator it = cookies.begin(); | 160 AddCookieExpectation( |
| 97 | 161 "http://www.google.com/", "www.google.com", "/", "A", NULL); |
| 98 ASSERT_TRUE(it != cookies.end()); | 162 CheckCookieExpectations(); |
| 99 EXPECT_EQ("http://www.google.com/", it->Source()); | |
| 100 EXPECT_EQ("www.google.com", it->Domain()); | |
| 101 EXPECT_EQ("/", it->Path()); | |
| 102 EXPECT_EQ("A", it->Name()); | |
| 103 | |
| 104 ASSERT_TRUE(++it == cookies.end()); | |
| 105 } | 163 } |
| 106 | 164 |
| 107 void CannedReplaceCookieCallback(const net::CookieList& cookies) { | 165 void CannedReplaceCookieCallback(const net::CookieList& cookies) { |
| 108 EXPECT_EQ(5UL, cookies.size()); | |
| 109 cookie_list_ = cookies; | 166 cookie_list_ = cookies; |
| 110 net::CookieList::const_iterator it = cookies.begin(); | 167 AddCookieExpectation( |
| 111 | 168 "http://www.google.com/", "www.google.com", "/", "A", "2"); |
| 112 ASSERT_TRUE(it != cookies.end()); | 169 AddCookieExpectation( |
| 113 EXPECT_EQ("http://www.google.com/", it->Source()); | 170 "http://www.google.com/", "www.google.com", "/example/0", "A", "4"); |
| 114 EXPECT_EQ("www.google.com", it->Domain()); | 171 AddCookieExpectation( |
| 115 EXPECT_EQ("/", it->Path()); | 172 "http://www.google.com/", ".google.com", "/", "A", "6"); |
| 116 EXPECT_EQ("A", it->Name()); | 173 AddCookieExpectation( |
| 117 EXPECT_EQ("2", it->Value()); | 174 "http://www.google.com/", ".google.com", "/example/1", "A", "8"); |
| 118 | 175 AddCookieExpectation( |
| 119 ASSERT_TRUE(++it != cookies.end()); | 176 "http://www.google.com/", ".www.google.com", "/", "A", "10"); |
| 120 EXPECT_EQ("http://www.google.com/", it->Source()); | 177 CheckCookieExpectations(); |
| 121 EXPECT_EQ("www.google.com", it->Domain()); | |
| 122 EXPECT_EQ("/example/0", it->Path()); | |
| 123 EXPECT_EQ("A", it->Name()); | |
| 124 EXPECT_EQ("4", it->Value()); | |
| 125 | |
| 126 ASSERT_TRUE(++it != cookies.end()); | |
| 127 EXPECT_EQ("http://www.google.com/", it->Source()); | |
| 128 EXPECT_EQ(".google.com", it->Domain()); | |
| 129 EXPECT_EQ("/", it->Path()); | |
| 130 EXPECT_EQ("A", it->Name()); | |
| 131 EXPECT_EQ("6", it->Value()); | |
| 132 | |
| 133 ASSERT_TRUE(++it != cookies.end()); | |
| 134 EXPECT_EQ("http://www.google.com/", it->Source()); | |
| 135 EXPECT_EQ(".google.com", it->Domain()); | |
| 136 EXPECT_EQ("/example/1", it->Path()); | |
| 137 EXPECT_EQ("A", it->Name()); | |
| 138 EXPECT_EQ("8", it->Value()); | |
| 139 | |
| 140 ASSERT_TRUE(++it != cookies.end()); | |
| 141 EXPECT_EQ("http://www.google.com/", it->Source()); | |
| 142 EXPECT_EQ(".www.google.com", it->Domain()); | |
| 143 EXPECT_EQ("/", it->Path()); | |
| 144 EXPECT_EQ("A", it->Name()); | |
| 145 EXPECT_EQ("10", it->Value()); | |
| 146 | |
| 147 ASSERT_TRUE(++it == cookies.end()); | |
| 148 } | 178 } |
| 149 | 179 |
| 150 void CannedDomainCookieCallback(const net::CookieList& cookies) { | 180 void CannedDomainCookieCallback(const net::CookieList& cookies) { |
| 151 ASSERT_EQ(2UL, cookies.size()); | |
| 152 cookie_list_ = cookies; | 181 cookie_list_ = cookies; |
| 153 net::CookieList::const_iterator it = cookies.begin(); | 182 AddCookieExpectation( |
| 154 | 183 "http://www.google.com/", "www.google.com", NULL, "A", NULL); |
| 155 ASSERT_TRUE(it != cookies.end()); | 184 AddCookieExpectation( |
| 156 EXPECT_EQ("http://www.google.com/", it->Source()); | 185 "http://www.google.com/", ".www.google.com", NULL, "A", NULL); |
| 157 EXPECT_EQ("A", it->Name()); | 186 CheckCookieExpectations(); |
| 158 EXPECT_EQ("www.google.com", it->Domain()); | |
| 159 | |
| 160 ASSERT_TRUE(++it != cookies.end()); | |
| 161 EXPECT_EQ("http://www.google.com/", it->Source()); | |
| 162 EXPECT_EQ("A", it->Name()); | |
| 163 EXPECT_EQ(".www.google.com", it->Domain()); | |
| 164 | |
| 165 ASSERT_TRUE(++it == cookies.end()); | |
| 166 } | 187 } |
| 167 | 188 |
| 168 void CannedDifferentFramesCallback(const net::CookieList& cookie_list) { | 189 void CannedDifferentFramesCallback(const net::CookieList& cookie_list) { |
| 169 ASSERT_EQ(3U, cookie_list.size()); | 190 ASSERT_EQ(3U, cookie_list.size()); |
| 170 } | 191 } |
| 171 | 192 |
| 193 void DeleteCookie(BrowsingDataCookieHelper* helper, const GURL origin) { |
| 194 for (net::CookieList::iterator it = cookie_list_.begin(); |
| 195 it != cookie_list_.end(); |
| 196 ++it) { |
| 197 if (it->Source() == net::CanonicalCookie::GetCookieSourceFromURL(origin)) |
| 198 helper->DeleteCookie(*it); |
| 199 } |
| 200 } |
| 201 |
| 172 protected: | 202 protected: |
| 173 content::TestBrowserThreadBundle thread_bundle_; | 203 content::TestBrowserThreadBundle thread_bundle_; |
| 174 scoped_ptr<TestingProfile> testing_profile_; | 204 scoped_ptr<TestingProfile> testing_profile_; |
| 175 | 205 |
| 206 std::vector<CookieExpectation> cookie_expectations_; |
| 176 net::CookieList cookie_list_; | 207 net::CookieList cookie_list_; |
| 177 }; | 208 }; |
| 178 | 209 |
| 179 TEST_F(BrowsingDataCookieHelperTest, FetchData) { | 210 TEST_F(BrowsingDataCookieHelperTest, FetchData) { |
| 180 CreateCookiesForTest(); | 211 CreateCookiesForTest(); |
| 181 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( | 212 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
| 182 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); | 213 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); |
| 183 | 214 |
| 184 cookie_helper->StartFetching( | 215 cookie_helper->StartFetching( |
| 185 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, | 216 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 helper->AddChangedCookie(origin1, origin1, "A=1", net::CookieOptions()); | 261 helper->AddChangedCookie(origin1, origin1, "A=1", net::CookieOptions()); |
| 231 helper->AddChangedCookie(origin2, origin2, "B=1", net::CookieOptions()); | 262 helper->AddChangedCookie(origin2, origin2, "B=1", net::CookieOptions()); |
| 232 | 263 |
| 233 helper->StartFetching( | 264 helper->StartFetching( |
| 234 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, | 265 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
| 235 base::Unretained(this))); | 266 base::Unretained(this))); |
| 236 base::RunLoop().RunUntilIdle(); | 267 base::RunLoop().RunUntilIdle(); |
| 237 | 268 |
| 238 EXPECT_EQ(2u, helper->GetCookieCount()); | 269 EXPECT_EQ(2u, helper->GetCookieCount()); |
| 239 | 270 |
| 240 helper->DeleteCookie(cookie_list_[0]); | 271 DeleteCookie(helper.get(), origin1); |
| 241 | 272 |
| 242 EXPECT_EQ(1u, helper->GetCookieCount()); | 273 EXPECT_EQ(1u, helper->GetCookieCount()); |
| 243 helper->StartFetching( | 274 helper->StartFetching( |
| 244 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, | 275 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, |
| 245 base::Unretained(this))); | 276 base::Unretained(this))); |
| 246 base::RunLoop().RunUntilIdle(); | 277 base::RunLoop().RunUntilIdle(); |
| 247 } | 278 } |
| 248 | 279 |
| 249 TEST_F(BrowsingDataCookieHelperTest, CannedDomainCookie) { | 280 TEST_F(BrowsingDataCookieHelperTest, CannedDomainCookie) { |
| 250 const GURL origin("http://www.google.com"); | 281 const GURL origin("http://www.google.com"); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 // "A=1; | 494 // "A=1; |
| 464 // "A=3; Domain=www.google.com" | 495 // "A=3; Domain=www.google.com" |
| 465 // Add a domain cookie and check if it increases the cookie count. | 496 // Add a domain cookie and check if it increases the cookie count. |
| 466 helper->AddChangedCookie(frame2_url, frame1_url, | 497 helper->AddChangedCookie(frame2_url, frame1_url, |
| 467 cookie_pair4 + "; Domain=" + cookie_domain, | 498 cookie_pair4 + "; Domain=" + cookie_domain, |
| 468 net::CookieOptions()); | 499 net::CookieOptions()); |
| 469 EXPECT_EQ(5U, helper->GetCookieCount()); | 500 EXPECT_EQ(5U, helper->GetCookieCount()); |
| 470 } | 501 } |
| 471 | 502 |
| 472 } // namespace | 503 } // namespace |
| OLD | NEW |