Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_cookie_helper_unittest.cc
bloba663336be69686cf8e72c15b0cf0b33a488cc04b
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
3 // found in the LICENSE file.
5 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h"
7 #include "base/bind.h"
8 #include "base/run_loop.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "net/cookies/canonical_cookie.h"
12 #include "net/cookies/parsed_cookie.h"
13 #include "net/url_request/url_request_context_getter.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace {
18 // Test expectations for a given cookie.
19 class CookieExpectation {
20 public:
21 CookieExpectation() {}
23 bool MatchesCookie(const net::CanonicalCookie& cookie) const {
24 if (!source_.is_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;
37 GURL source_;
38 std::string domain_;
39 std::string path_;
40 std::string name_;
41 std::string value_;
42 bool matched_ = false;
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_);
53 net::CanonicalCookie cookie_;
56 // Unary predicate to determine whether an expectation has been matched.
57 bool ExpectationIsMatched(const CookieExpectation& expectation) {
58 return expectation.matched_;
61 class BrowsingDataCookieHelperTest : public testing::Test {
62 public:
63 BrowsingDataCookieHelperTest()
64 : testing_profile_(new TestingProfile()) {
67 void SetUp() override { cookie_expectations_.clear(); }
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_ = GURL(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);
89 // Checks the existing expectations, and then clears all existing
90 // expectations.
91 void CheckCookieExpectations() {
92 ASSERT_EQ(cookie_expectations_.size(), cookie_list_.size());
94 // For each cookie, look for a matching expectation.
95 for (const auto& cookie : cookie_list_) {
96 CookieMatcher matcher(cookie);
97 std::vector<CookieExpectation>::iterator match = std::find_if(
98 cookie_expectations_.begin(), cookie_expectations_.end(), matcher);
99 if (match != cookie_expectations_.end())
100 match->matched_ = true;
103 // Check that each expectation has been matched.
104 unsigned long match_count = std::count_if(cookie_expectations_.begin(),
105 cookie_expectations_.end(),
106 ExpectationIsMatched);
107 EXPECT_EQ(cookie_expectations_.size(), match_count);
109 cookie_expectations_.clear();
112 void CreateCookiesForTest() {
113 scoped_refptr<net::CookieMonster> cookie_monster =
114 testing_profile_->GetCookieMonster();
115 cookie_monster->SetCookieWithOptionsAsync(
116 GURL("http://www.google.com"), "A=1", net::CookieOptions(),
117 net::CookieMonster::SetCookiesCallback());
118 cookie_monster->SetCookieWithOptionsAsync(
119 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(),
120 net::CookieMonster::SetCookiesCallback());
123 void CreateCookiesForDomainCookieTest() {
124 scoped_refptr<net::CookieMonster> cookie_monster =
125 testing_profile_->GetCookieMonster();
126 cookie_monster->SetCookieWithOptionsAsync(
127 GURL("http://www.google.com"), "A=1", net::CookieOptions(),
128 net::CookieMonster::SetCookiesCallback());
129 cookie_monster->SetCookieWithOptionsAsync(
130 GURL("http://www.google.com"), "A=2; Domain=.www.google.com ",
131 net::CookieOptions(), net::CookieMonster::SetCookiesCallback());
134 void FetchCallback(const net::CookieList& cookies) {
135 cookie_list_ = cookies;
137 AddCookieExpectation(nullptr, "www.google.com", nullptr, "A", nullptr);
138 AddCookieExpectation(nullptr, "www.gmail.google.com", nullptr, "B",
139 nullptr);
140 CheckCookieExpectations();
143 void DomainCookieCallback(const net::CookieList& cookies) {
144 cookie_list_ = cookies;
146 AddCookieExpectation(nullptr, "www.google.com", nullptr, "A", "1");
147 AddCookieExpectation(nullptr, ".www.google.com", nullptr, "A", "2");
148 CheckCookieExpectations();
151 void DeleteCallback(const net::CookieList& cookies) {
152 cookie_list_ = cookies;
153 AddCookieExpectation(nullptr, "www.gmail.google.com", nullptr, "B",
154 nullptr);
155 CheckCookieExpectations();
158 void CannedUniqueCallback(const net::CookieList& cookies) {
159 cookie_list_ = cookies;
160 AddCookieExpectation("http://www.google.com/", "www.google.com", "/", "A",
161 nullptr);
162 CheckCookieExpectations();
165 void CannedReplaceCookieCallback(const net::CookieList& cookies) {
166 cookie_list_ = cookies;
167 AddCookieExpectation(
168 "http://www.google.com/", "www.google.com", "/", "A", "2");
169 AddCookieExpectation(
170 "http://www.google.com/", "www.google.com", "/example/0", "A", "4");
171 AddCookieExpectation(
172 "http://www.google.com/", ".google.com", "/", "A", "6");
173 AddCookieExpectation(
174 "http://www.google.com/", ".google.com", "/example/1", "A", "8");
175 AddCookieExpectation(
176 "http://www.google.com/", ".www.google.com", "/", "A", "10");
177 CheckCookieExpectations();
180 void CannedDomainCookieCallback(const net::CookieList& cookies) {
181 cookie_list_ = cookies;
182 AddCookieExpectation("http://www.google.com/", "www.google.com", nullptr,
183 "A", nullptr);
184 AddCookieExpectation("http://www.google.com/", ".www.google.com", nullptr,
185 "A", nullptr);
186 CheckCookieExpectations();
189 void CannedDifferentFramesCallback(const net::CookieList& cookie_list) {
190 ASSERT_EQ(3U, cookie_list.size());
193 void DeleteCookie(BrowsingDataCookieHelper* helper, const GURL origin) {
194 for (const auto& cookie : cookie_list_) {
195 if (cookie.Source() == origin)
196 helper->DeleteCookie(cookie);
200 protected:
201 content::TestBrowserThreadBundle thread_bundle_;
202 scoped_ptr<TestingProfile> testing_profile_;
204 std::vector<CookieExpectation> cookie_expectations_;
205 net::CookieList cookie_list_;
208 TEST_F(BrowsingDataCookieHelperTest, FetchData) {
209 CreateCookiesForTest();
210 scoped_refptr<BrowsingDataCookieHelper> cookie_helper(
211 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext()));
213 cookie_helper->StartFetching(
214 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback,
215 base::Unretained(this)));
216 base::RunLoop().RunUntilIdle();
219 TEST_F(BrowsingDataCookieHelperTest, DomainCookie) {
220 CreateCookiesForDomainCookieTest();
221 scoped_refptr<BrowsingDataCookieHelper> cookie_helper(
222 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext()));
224 cookie_helper->StartFetching(
225 base::Bind(&BrowsingDataCookieHelperTest::DomainCookieCallback,
226 base::Unretained(this)));
227 base::RunLoop().RunUntilIdle();
230 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) {
231 CreateCookiesForTest();
232 scoped_refptr<BrowsingDataCookieHelper> cookie_helper(
233 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext()));
235 cookie_helper->StartFetching(
236 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback,
237 base::Unretained(this)));
238 base::RunLoop().RunUntilIdle();
240 net::CanonicalCookie cookie = cookie_list_[0];
241 cookie_helper->DeleteCookie(cookie);
243 cookie_helper->StartFetching(
244 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback,
245 base::Unretained(this)));
246 base::RunLoop().RunUntilIdle();
249 TEST_F(BrowsingDataCookieHelperTest, CannedDeleteCookie) {
250 CreateCookiesForTest();
251 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
252 new CannedBrowsingDataCookieHelper(
253 testing_profile_->GetRequestContext()));
255 ASSERT_TRUE(helper->empty());
257 const GURL origin1("http://www.google.com");
258 const GURL origin2("http://www.gmail.google.com");
259 helper->AddChangedCookie(origin1, origin1, "A=1", net::CookieOptions());
260 helper->AddChangedCookie(origin2, origin2, "B=1", net::CookieOptions());
262 helper->StartFetching(
263 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback,
264 base::Unretained(this)));
265 base::RunLoop().RunUntilIdle();
267 EXPECT_EQ(2u, helper->GetCookieCount());
269 DeleteCookie(helper.get(), origin1);
271 EXPECT_EQ(1u, helper->GetCookieCount());
272 helper->StartFetching(
273 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback,
274 base::Unretained(this)));
275 base::RunLoop().RunUntilIdle();
278 TEST_F(BrowsingDataCookieHelperTest, CannedDomainCookie) {
279 const GURL origin("http://www.google.com");
280 net::CookieList cookie;
282 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
283 new CannedBrowsingDataCookieHelper(
284 testing_profile_->GetRequestContext()));
286 ASSERT_TRUE(helper->empty());
287 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions());
288 helper->AddChangedCookie(origin, origin, "A=1; Domain=.www.google.com",
289 net::CookieOptions());
290 // Try adding invalid cookies that will be ignored.
291 helper->AddChangedCookie(origin, origin, std::string(), net::CookieOptions());
292 helper->AddChangedCookie(origin,
293 origin,
294 "C=bad guy; Domain=wrongdomain.com",
295 net::CookieOptions());
297 helper->StartFetching(
298 base::Bind(&BrowsingDataCookieHelperTest::CannedDomainCookieCallback,
299 base::Unretained(this)));
300 cookie = cookie_list_;
302 helper->Reset();
303 ASSERT_TRUE(helper->empty());
305 helper->AddReadCookies(origin, origin, cookie);
306 helper->StartFetching(
307 base::Bind(&BrowsingDataCookieHelperTest::CannedDomainCookieCallback,
308 base::Unretained(this)));
311 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) {
312 const GURL origin("http://www.google.com");
313 net::CookieList cookie;
315 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
316 new CannedBrowsingDataCookieHelper(
317 testing_profile_->GetRequestContext()));
319 ASSERT_TRUE(helper->empty());
320 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions());
321 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions());
322 helper->StartFetching(
323 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback,
324 base::Unretained(this)));
326 cookie = cookie_list_;
327 helper->Reset();
328 ASSERT_TRUE(helper->empty());
330 helper->AddReadCookies(origin, origin, cookie);
331 helper->AddReadCookies(origin, origin, cookie);
332 helper->StartFetching(
333 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback,
334 base::Unretained(this)));
337 TEST_F(BrowsingDataCookieHelperTest, CannedReplaceCookie) {
338 const GURL origin("http://www.google.com");
339 net::CookieList cookie;
341 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
342 new CannedBrowsingDataCookieHelper(
343 testing_profile_->GetRequestContext()));
345 ASSERT_TRUE(helper->empty());
346 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions());
347 helper->AddChangedCookie(origin, origin, "A=2", net::CookieOptions());
348 helper->AddChangedCookie(origin, origin, "A=3; Path=/example/0",
349 net::CookieOptions());
350 helper->AddChangedCookie(origin, origin, "A=4; Path=/example/0",
351 net::CookieOptions());
352 helper->AddChangedCookie(origin, origin, "A=5; Domain=google.com",
353 net::CookieOptions());
354 helper->AddChangedCookie(origin, origin, "A=6; Domain=google.com",
355 net::CookieOptions());
356 helper->AddChangedCookie(origin, origin,
357 "A=7; Domain=google.com; Path=/example/1",
358 net::CookieOptions());
359 helper->AddChangedCookie(origin, origin,
360 "A=8; Domain=google.com; Path=/example/1",
361 net::CookieOptions());
363 helper->AddChangedCookie(origin, origin,
364 "A=9; Domain=www.google.com",
365 net::CookieOptions());
366 helper->AddChangedCookie(origin, origin,
367 "A=10; Domain=www.google.com",
368 net::CookieOptions());
370 helper->StartFetching(
371 base::Bind(&BrowsingDataCookieHelperTest::CannedReplaceCookieCallback,
372 base::Unretained(this)));
374 cookie = cookie_list_;
375 helper->Reset();
376 ASSERT_TRUE(helper->empty());
378 helper->AddReadCookies(origin, origin, cookie);
379 helper->AddReadCookies(origin, origin, cookie);
380 helper->StartFetching(
381 base::Bind(&BrowsingDataCookieHelperTest::CannedReplaceCookieCallback,
382 base::Unretained(this)));
385 TEST_F(BrowsingDataCookieHelperTest, CannedEmpty) {
386 const GURL url_google("http://www.google.com");
388 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
389 new CannedBrowsingDataCookieHelper(
390 testing_profile_->GetRequestContext()));
392 ASSERT_TRUE(helper->empty());
393 helper->AddChangedCookie(url_google, url_google, "a=1",
394 net::CookieOptions());
395 ASSERT_FALSE(helper->empty());
396 helper->Reset();
397 ASSERT_TRUE(helper->empty());
399 net::CookieList cookies;
400 net::ParsedCookie pc("a=1");
401 scoped_ptr<net::CanonicalCookie> cookie(
402 new net::CanonicalCookie(url_google, pc));
403 cookies.push_back(*cookie);
405 helper->AddReadCookies(url_google, url_google, cookies);
406 ASSERT_FALSE(helper->empty());
407 helper->Reset();
408 ASSERT_TRUE(helper->empty());
411 TEST_F(BrowsingDataCookieHelperTest, CannedDifferentFrames) {
412 GURL frame1_url("http://www.google.com");
413 GURL frame2_url("http://www.google.de");
414 GURL request_url("http://www.google.com");
416 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
417 new CannedBrowsingDataCookieHelper(
418 testing_profile_->GetRequestContext()));
420 ASSERT_TRUE(helper->empty());
421 helper->AddChangedCookie(frame1_url, request_url, "a=1",
422 net::CookieOptions());
423 helper->AddChangedCookie(frame1_url, request_url, "b=1",
424 net::CookieOptions());
425 helper->AddChangedCookie(frame2_url, request_url, "c=1",
426 net::CookieOptions());
428 helper->StartFetching(
429 base::Bind(&BrowsingDataCookieHelperTest::CannedDifferentFramesCallback,
430 base::Unretained(this)));
433 TEST_F(BrowsingDataCookieHelperTest, CannedGetCookieCount) {
434 // The URL in the omnibox is a frame URL. This is not necessarily the request
435 // URL, since websites usually include other resources.
436 GURL frame1_url("http://www.google.com");
437 GURL frame2_url("http://www.google.de");
438 // The request URL used for all cookies that are added to the |helper|.
439 GURL request1_url("http://static.google.com/foo/res1.html");
440 GURL request2_url("http://static.google.com/bar/res2.html");
441 std::string cookie_domain(".www.google.com");
442 std::string cookie_pair1("A=1");
443 std::string cookie_pair2("B=1");
444 // The cookie pair used for adding a cookie that overrides the cookie created
445 // with |cookie_pair1|. The cookie-name of |cookie_pair3| must match the
446 // cookie-name of |cookie-pair1|.
447 std::string cookie_pair3("A=2");
448 // The cookie pair used for adding a non host-only cookie. The cookie-name
449 // must match the cookie-name of |cookie_pair1| in order to add a host-only
450 // and a non host-only cookie with the same name below.
451 std::string cookie_pair4("A=3");
453 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
454 new CannedBrowsingDataCookieHelper(
455 testing_profile_->GetRequestContext()));
457 // Add two different cookies (distinguished by the tuple [cookie-name,
458 // domain-value, path-value]) for a HTTP request to |frame1_url| and verify
459 // that the cookie count is increased to two. The set-cookie-string consists
460 // only of the cookie-pair. This means that the host and the default-path of
461 // the |request_url| are used as domain-value and path-value for the added
462 // cookies.
463 EXPECT_EQ(0U, helper->GetCookieCount());
464 helper->AddChangedCookie(frame1_url, frame1_url, cookie_pair1,
465 net::CookieOptions());
466 EXPECT_EQ(1U, helper->GetCookieCount());
467 helper->AddChangedCookie(frame1_url, frame1_url, cookie_pair2,
468 net::CookieOptions());
469 EXPECT_EQ(2U, helper->GetCookieCount());
471 // Use a different frame URL for adding another cookie that will replace one
472 // of the previously added cookies. This could happen during an automatic
473 // redirect e.g. |frame1_url| redirects to |frame2_url| and a cookie set by a
474 // request to |frame1_url| is updated.
475 helper->AddChangedCookie(frame2_url, frame1_url, cookie_pair3,
476 net::CookieOptions());
477 EXPECT_EQ(2U, helper->GetCookieCount());
479 // Add two more cookies that are set while loading resources. The two cookies
480 // below have a differnt path-value since the request URLs have different
481 // paths.
482 helper->AddChangedCookie(frame2_url, request1_url, cookie_pair3,
483 net::CookieOptions());
484 EXPECT_EQ(3U, helper->GetCookieCount());
485 helper->AddChangedCookie(frame2_url, request2_url, cookie_pair3,
486 net::CookieOptions());
487 EXPECT_EQ(4U, helper->GetCookieCount());
489 // Host-only and domain cookies are treated as seperate items. This means that
490 // the following two cookie-strings are stored as two separate cookies, even
491 // though they have the same name and are send with the same request:
492 // "A=1;
493 // "A=3; Domain=www.google.com"
494 // Add a domain cookie and check if it increases the cookie count.
495 helper->AddChangedCookie(frame2_url, frame1_url,
496 cookie_pair4 + "; Domain=" + cookie_domain,
497 net::CookieOptions());
498 EXPECT_EQ(5U, helper->GetCookieCount());
501 } // namespace