Extensions: Remove the legacy GetMessages/HasMessages
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_cookie_helper_unittest.cc
blob064ff055b1ec27214a7aed69f0c3acc86a39220c
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_.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 std::string 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_ = 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() ==
196 net::CanonicalCookie::GetCookieSourceFromURL(origin))
197 helper->DeleteCookie(cookie);
201 protected:
202 content::TestBrowserThreadBundle thread_bundle_;
203 scoped_ptr<TestingProfile> testing_profile_;
205 std::vector<CookieExpectation> cookie_expectations_;
206 net::CookieList cookie_list_;
209 TEST_F(BrowsingDataCookieHelperTest, FetchData) {
210 CreateCookiesForTest();
211 scoped_refptr<BrowsingDataCookieHelper> cookie_helper(
212 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext()));
214 cookie_helper->StartFetching(
215 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback,
216 base::Unretained(this)));
217 base::RunLoop().RunUntilIdle();
220 TEST_F(BrowsingDataCookieHelperTest, DomainCookie) {
221 CreateCookiesForDomainCookieTest();
222 scoped_refptr<BrowsingDataCookieHelper> cookie_helper(
223 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext()));
225 cookie_helper->StartFetching(
226 base::Bind(&BrowsingDataCookieHelperTest::DomainCookieCallback,
227 base::Unretained(this)));
228 base::RunLoop().RunUntilIdle();
231 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) {
232 CreateCookiesForTest();
233 scoped_refptr<BrowsingDataCookieHelper> cookie_helper(
234 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext()));
236 cookie_helper->StartFetching(
237 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback,
238 base::Unretained(this)));
239 base::RunLoop().RunUntilIdle();
241 net::CanonicalCookie cookie = cookie_list_[0];
242 cookie_helper->DeleteCookie(cookie);
244 cookie_helper->StartFetching(
245 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback,
246 base::Unretained(this)));
247 base::RunLoop().RunUntilIdle();
250 TEST_F(BrowsingDataCookieHelperTest, CannedDeleteCookie) {
251 CreateCookiesForTest();
252 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
253 new CannedBrowsingDataCookieHelper(
254 testing_profile_->GetRequestContext()));
256 ASSERT_TRUE(helper->empty());
258 const GURL origin1("http://www.google.com");
259 const GURL origin2("http://www.gmail.google.com");
260 helper->AddChangedCookie(origin1, origin1, "A=1", net::CookieOptions());
261 helper->AddChangedCookie(origin2, origin2, "B=1", net::CookieOptions());
263 helper->StartFetching(
264 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback,
265 base::Unretained(this)));
266 base::RunLoop().RunUntilIdle();
268 EXPECT_EQ(2u, helper->GetCookieCount());
270 DeleteCookie(helper.get(), origin1);
272 EXPECT_EQ(1u, helper->GetCookieCount());
273 helper->StartFetching(
274 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback,
275 base::Unretained(this)));
276 base::RunLoop().RunUntilIdle();
279 TEST_F(BrowsingDataCookieHelperTest, CannedDomainCookie) {
280 const GURL origin("http://www.google.com");
281 net::CookieList cookie;
283 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
284 new CannedBrowsingDataCookieHelper(
285 testing_profile_->GetRequestContext()));
287 ASSERT_TRUE(helper->empty());
288 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions());
289 helper->AddChangedCookie(origin, origin, "A=1; Domain=.www.google.com",
290 net::CookieOptions());
291 // Try adding invalid cookies that will be ignored.
292 helper->AddChangedCookie(origin, origin, std::string(), net::CookieOptions());
293 helper->AddChangedCookie(origin,
294 origin,
295 "C=bad guy; Domain=wrongdomain.com",
296 net::CookieOptions());
298 helper->StartFetching(
299 base::Bind(&BrowsingDataCookieHelperTest::CannedDomainCookieCallback,
300 base::Unretained(this)));
301 cookie = cookie_list_;
303 helper->Reset();
304 ASSERT_TRUE(helper->empty());
306 helper->AddReadCookies(origin, origin, cookie);
307 helper->StartFetching(
308 base::Bind(&BrowsingDataCookieHelperTest::CannedDomainCookieCallback,
309 base::Unretained(this)));
312 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) {
313 const GURL origin("http://www.google.com");
314 net::CookieList cookie;
316 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
317 new CannedBrowsingDataCookieHelper(
318 testing_profile_->GetRequestContext()));
320 ASSERT_TRUE(helper->empty());
321 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions());
322 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions());
323 helper->StartFetching(
324 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback,
325 base::Unretained(this)));
327 cookie = cookie_list_;
328 helper->Reset();
329 ASSERT_TRUE(helper->empty());
331 helper->AddReadCookies(origin, origin, cookie);
332 helper->AddReadCookies(origin, origin, cookie);
333 helper->StartFetching(
334 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback,
335 base::Unretained(this)));
338 TEST_F(BrowsingDataCookieHelperTest, CannedReplaceCookie) {
339 const GURL origin("http://www.google.com");
340 net::CookieList cookie;
342 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
343 new CannedBrowsingDataCookieHelper(
344 testing_profile_->GetRequestContext()));
346 ASSERT_TRUE(helper->empty());
347 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions());
348 helper->AddChangedCookie(origin, origin, "A=2", net::CookieOptions());
349 helper->AddChangedCookie(origin, origin, "A=3; Path=/example/0",
350 net::CookieOptions());
351 helper->AddChangedCookie(origin, origin, "A=4; Path=/example/0",
352 net::CookieOptions());
353 helper->AddChangedCookie(origin, origin, "A=5; Domain=google.com",
354 net::CookieOptions());
355 helper->AddChangedCookie(origin, origin, "A=6; Domain=google.com",
356 net::CookieOptions());
357 helper->AddChangedCookie(origin, origin,
358 "A=7; Domain=google.com; Path=/example/1",
359 net::CookieOptions());
360 helper->AddChangedCookie(origin, origin,
361 "A=8; Domain=google.com; Path=/example/1",
362 net::CookieOptions());
364 helper->AddChangedCookie(origin, origin,
365 "A=9; Domain=www.google.com",
366 net::CookieOptions());
367 helper->AddChangedCookie(origin, origin,
368 "A=10; Domain=www.google.com",
369 net::CookieOptions());
371 helper->StartFetching(
372 base::Bind(&BrowsingDataCookieHelperTest::CannedReplaceCookieCallback,
373 base::Unretained(this)));
375 cookie = cookie_list_;
376 helper->Reset();
377 ASSERT_TRUE(helper->empty());
379 helper->AddReadCookies(origin, origin, cookie);
380 helper->AddReadCookies(origin, origin, cookie);
381 helper->StartFetching(
382 base::Bind(&BrowsingDataCookieHelperTest::CannedReplaceCookieCallback,
383 base::Unretained(this)));
386 TEST_F(BrowsingDataCookieHelperTest, CannedEmpty) {
387 const GURL url_google("http://www.google.com");
389 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
390 new CannedBrowsingDataCookieHelper(
391 testing_profile_->GetRequestContext()));
393 ASSERT_TRUE(helper->empty());
394 helper->AddChangedCookie(url_google, url_google, "a=1",
395 net::CookieOptions());
396 ASSERT_FALSE(helper->empty());
397 helper->Reset();
398 ASSERT_TRUE(helper->empty());
400 net::CookieList cookies;
401 net::ParsedCookie pc("a=1");
402 scoped_ptr<net::CanonicalCookie> cookie(
403 new net::CanonicalCookie(url_google, pc));
404 cookies.push_back(*cookie);
406 helper->AddReadCookies(url_google, url_google, cookies);
407 ASSERT_FALSE(helper->empty());
408 helper->Reset();
409 ASSERT_TRUE(helper->empty());
412 TEST_F(BrowsingDataCookieHelperTest, CannedDifferentFrames) {
413 GURL frame1_url("http://www.google.com");
414 GURL frame2_url("http://www.google.de");
415 GURL request_url("http://www.google.com");
417 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
418 new CannedBrowsingDataCookieHelper(
419 testing_profile_->GetRequestContext()));
421 ASSERT_TRUE(helper->empty());
422 helper->AddChangedCookie(frame1_url, request_url, "a=1",
423 net::CookieOptions());
424 helper->AddChangedCookie(frame1_url, request_url, "b=1",
425 net::CookieOptions());
426 helper->AddChangedCookie(frame2_url, request_url, "c=1",
427 net::CookieOptions());
429 helper->StartFetching(
430 base::Bind(&BrowsingDataCookieHelperTest::CannedDifferentFramesCallback,
431 base::Unretained(this)));
434 TEST_F(BrowsingDataCookieHelperTest, CannedGetCookieCount) {
435 // The URL in the omnibox is a frame URL. This is not necessarily the request
436 // URL, since websites usually include other resources.
437 GURL frame1_url("http://www.google.com");
438 GURL frame2_url("http://www.google.de");
439 // The request URL used for all cookies that are added to the |helper|.
440 GURL request1_url("http://static.google.com/foo/res1.html");
441 GURL request2_url("http://static.google.com/bar/res2.html");
442 std::string cookie_domain(".www.google.com");
443 std::string cookie_pair1("A=1");
444 std::string cookie_pair2("B=1");
445 // The cookie pair used for adding a cookie that overrides the cookie created
446 // with |cookie_pair1|. The cookie-name of |cookie_pair3| must match the
447 // cookie-name of |cookie-pair1|.
448 std::string cookie_pair3("A=2");
449 // The cookie pair used for adding a non host-only cookie. The cookie-name
450 // must match the cookie-name of |cookie_pair1| in order to add a host-only
451 // and a non host-only cookie with the same name below.
452 std::string cookie_pair4("A=3");
454 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
455 new CannedBrowsingDataCookieHelper(
456 testing_profile_->GetRequestContext()));
458 // Add two different cookies (distinguished by the tuple [cookie-name,
459 // domain-value, path-value]) for a HTTP request to |frame1_url| and verify
460 // that the cookie count is increased to two. The set-cookie-string consists
461 // only of the cookie-pair. This means that the host and the default-path of
462 // the |request_url| are used as domain-value and path-value for the added
463 // cookies.
464 EXPECT_EQ(0U, helper->GetCookieCount());
465 helper->AddChangedCookie(frame1_url, frame1_url, cookie_pair1,
466 net::CookieOptions());
467 EXPECT_EQ(1U, helper->GetCookieCount());
468 helper->AddChangedCookie(frame1_url, frame1_url, cookie_pair2,
469 net::CookieOptions());
470 EXPECT_EQ(2U, helper->GetCookieCount());
472 // Use a different frame URL for adding another cookie that will replace one
473 // of the previously added cookies. This could happen during an automatic
474 // redirect e.g. |frame1_url| redirects to |frame2_url| and a cookie set by a
475 // request to |frame1_url| is updated.
476 helper->AddChangedCookie(frame2_url, frame1_url, cookie_pair3,
477 net::CookieOptions());
478 EXPECT_EQ(2U, helper->GetCookieCount());
480 // Add two more cookies that are set while loading resources. The two cookies
481 // below have a differnt path-value since the request URLs have different
482 // paths.
483 helper->AddChangedCookie(frame2_url, request1_url, cookie_pair3,
484 net::CookieOptions());
485 EXPECT_EQ(3U, helper->GetCookieCount());
486 helper->AddChangedCookie(frame2_url, request2_url, cookie_pair3,
487 net::CookieOptions());
488 EXPECT_EQ(4U, helper->GetCookieCount());
490 // Host-only and domain cookies are treated as seperate items. This means that
491 // the following two cookie-strings are stored as two separate cookies, even
492 // though they have the same name and are send with the same request:
493 // "A=1;
494 // "A=3; Domain=www.google.com"
495 // Add a domain cookie and check if it increases the cookie count.
496 helper->AddChangedCookie(frame2_url, frame1_url,
497 cookie_pair4 + "; Domain=" + cookie_domain,
498 net::CookieOptions());
499 EXPECT_EQ(5U, helper->GetCookieCount());
502 } // namespace