File: cookie_helper_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (762 lines) | stat: -rw-r--r-- 29,648 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/browsing_data/content/cookie_helper.h"

#include <algorithm>
#include <optional>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/time/time.h"
#include "content/public/browser/cookie_access_details.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_options.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace browsing_data {
namespace {

net::CookieAccessResultList ConvertCookieListToCookieAccessResultList(
    const net::CookieList& cookie_list) {
  net::CookieAccessResultList result;
  std::ranges::transform(cookie_list, std::back_inserter(result),
                         [](const net::CanonicalCookie& cookie) {
                           return net::CookieWithAccessResult{cookie, {}};
                         });
  return result;
}

// Test expectations for a given cookie.
class CookieExpectation {
 public:
  CookieExpectation() = default;

  bool MatchesCookie(const net::CanonicalCookie& cookie) const {
    if (!domain_.empty() && domain_ != cookie.Domain())
      return false;
    if (!path_.empty() && path_ != cookie.Path())
      return false;
    if (!name_.empty() && name_ != cookie.Name())
      return false;
    if (!value_.empty() && value_ != cookie.Value())
      return false;
    return true;
  }

  GURL source_;
  std::string domain_;
  std::string path_;
  std::string name_;
  std::string value_;
  bool matched_ = false;
};

// Matches a CookieExpectation against a Cookie.
class CookieMatcher {
 public:
  explicit CookieMatcher(const net::CanonicalCookie& cookie)
      : cookie_(cookie) {}
  bool operator()(const CookieExpectation& expectation) {
    return expectation.MatchesCookie(cookie_);
  }
  net::CanonicalCookie cookie_;
};

// Unary predicate to determine whether an expectation has been matched.
bool ExpectationIsMatched(const CookieExpectation& expectation) {
  return expectation.matched_;
}

class CookieHelperTest : public testing::Test {
 public:
  CookieHelperTest()
      : testing_browser_context_(
            std::make_unique<content::TestBrowserContext>()) {}

  void SetUp() override { cookie_expectations_.clear(); }

  // Adds an expectation for a cookie that satisfies the given parameters.
  void AddCookieExpectation(const char* source,
                            const char* domain,
                            const char* path,
                            const char* name,
                            const char* value) {
    CookieExpectation matcher;
    if (source)
      matcher.source_ = GURL(source);
    if (domain)
      matcher.domain_ = domain;
    if (path)
      matcher.path_ = path;
    if (name)
      matcher.name_ = name;
    if (value)
      matcher.value_ = value;
    cookie_expectations_.push_back(matcher);
  }

  // Checks the existing expectations, and then clears all existing
  // expectations.
  void CheckCookieExpectations() {
    ASSERT_EQ(cookie_expectations_.size(), cookie_list_.size());

    // For each cookie, look for a matching expectation.
    for (const auto& cookie : cookie_list_) {
      auto match =
          std::ranges::find_if(cookie_expectations_, CookieMatcher(cookie));
      if (match != cookie_expectations_.end())
        match->matched_ = true;
    }

    // Check that each expectation has been matched.
    unsigned long match_count =
        std::ranges::count_if(cookie_expectations_, ExpectationIsMatched);
    EXPECT_EQ(cookie_expectations_.size(), match_count);

    cookie_expectations_.clear();
  }

  void CreateCookiesForTest() {
    std::optional<base::Time> server_time = std::nullopt;
    GURL cookie1_source("https://www.google.com");
    auto cookie1 = net::CanonicalCookie::CreateForTesting(
        cookie1_source, "A=1", base::Time::Now(), server_time);
    GURL cookie2_source("https://www.gmail.google.com");
    auto cookie2 = net::CanonicalCookie::CreateForTesting(
        cookie2_source, "B=1", base::Time::Now(), server_time);

    network::mojom::CookieManager* cookie_manager =
        storage_partition()->GetCookieManagerForBrowserProcess();
    cookie_manager->SetCanonicalCookie(*cookie1, cookie1_source,
                                       net::CookieOptions::MakeAllInclusive(),
                                       base::DoNothing());
    cookie_manager->SetCanonicalCookie(*cookie2, cookie2_source,
                                       net::CookieOptions::MakeAllInclusive(),
                                       base::DoNothing());
  }

  void CreateCookiesForDomainCookieTest() {
    std::optional<base::Time> server_time = std::nullopt;
    GURL cookie_source("https://www.google.com");
    auto cookie1 = net::CanonicalCookie::CreateForTesting(
        cookie_source, "A=1", base::Time::Now(), server_time);
    auto cookie2 = net::CanonicalCookie::CreateForTesting(
        cookie_source, "A=2; Domain=.www.google.com ", base::Time::Now(),
        server_time);

    network::mojom::CookieManager* cookie_manager =
        storage_partition()->GetCookieManagerForBrowserProcess();
    cookie_manager->SetCanonicalCookie(*cookie1, cookie_source,
                                       net::CookieOptions::MakeAllInclusive(),
                                       base::DoNothing());
    cookie_manager->SetCanonicalCookie(*cookie2, cookie_source,
                                       net::CookieOptions::MakeAllInclusive(),
                                       base::DoNothing());
  }

  void FetchCallback(base::OnceClosure quit_closure,
                     const net::CookieList& cookies) {
    cookie_list_ = cookies;

    AddCookieExpectation(nullptr, "www.google.com", nullptr, "A", nullptr);
    AddCookieExpectation(nullptr, "www.gmail.google.com", nullptr, "B",
                         nullptr);
    CheckCookieExpectations();
    std::move(quit_closure).Run();
  }

  void DomainCookieCallback(base::OnceClosure quit_closure,
                            const net::CookieList& cookies) {
    cookie_list_ = cookies;

    AddCookieExpectation(nullptr, "www.google.com", nullptr, "A", "1");
    AddCookieExpectation(nullptr, ".www.google.com", nullptr, "A", "2");
    CheckCookieExpectations();
    std::move(quit_closure).Run();
  }

  void DeleteCallback(base::OnceClosure quit_closure,
                      const net::CookieList& cookies) {
    cookie_list_ = cookies;
    AddCookieExpectation(nullptr, "www.gmail.google.com", nullptr, "B",
                         nullptr);
    CheckCookieExpectations();
    std::move(quit_closure).Run();
  }

  void CannedUniqueCallback(base::OnceClosure quit_closure,
                            const net::CookieList& cookies) {
    cookie_list_ = cookies;
    AddCookieExpectation("http://www.google.com/", "www.google.com", "/", "A",
                         nullptr);
    CheckCookieExpectations();
    std::move(quit_closure).Run();
  }

  void CannedReplaceCookieCallback(base::OnceClosure quit_closure,
                                   const net::CookieList& cookies) {
    cookie_list_ = cookies;
    AddCookieExpectation("http://www.google.com/", "www.google.com", "/", "A",
                         "2");
    AddCookieExpectation("http://www.google.com/", "www.google.com",
                         "/example/0", "A", "4");
    AddCookieExpectation("http://www.google.com/", ".google.com", "/", "A",
                         "6");
    AddCookieExpectation("http://www.google.com/", ".google.com", "/example/1",
                         "A", "8");
    AddCookieExpectation("http://www.google.com/", ".www.google.com", "/", "A",
                         "10");
    CheckCookieExpectations();
    std::move(quit_closure).Run();
  }

  void CannedDomainCookieCallback(base::OnceClosure quit_closure,
                                  const net::CookieList& cookies) {
    cookie_list_ = cookies;
    AddCookieExpectation("http://www.google.com/", "www.google.com", nullptr,
                         "A", nullptr);
    AddCookieExpectation("http://www.google.com/", ".www.google.com", nullptr,
                         "A", nullptr);
    CheckCookieExpectations();
    std::move(quit_closure).Run();
  }

  void CannedDifferentFramesCallback(base::OnceClosure quit_closure,
                                     const net::CookieList& cookie_list) {
    ASSERT_EQ(3U, cookie_list.size());
    std::move(quit_closure).Run();
  }

  void DeleteCookie(CookieHelper* helper, const std::string& domain) {
    for (const auto& cookie : cookie_list_) {
      if (cookie.Domain() == domain)
        helper->DeleteCookie(cookie);
    }
  }

  content::StoragePartition* storage_partition() {
    return testing_browser_context_->GetDefaultStoragePartition();
  }

 protected:
  content::BrowserTaskEnvironment task_environment_;
  std::unique_ptr<content::TestBrowserContext> testing_browser_context_;

  std::vector<CookieExpectation> cookie_expectations_;
  net::CookieList cookie_list_;
};

TEST_F(CookieHelperTest, FetchData) {
  CreateCookiesForTest();
  auto cookie_helper = base::MakeRefCounted<CookieHelper>(storage_partition(),
                                                          base::NullCallback());

  base::RunLoop run_loop;
  cookie_helper->StartFetching(base::BindOnce(&CookieHelperTest::FetchCallback,
                                              base::Unretained(this),
                                              run_loop.QuitClosure()));
  run_loop.Run();
}

TEST_F(CookieHelperTest, DomainCookie) {
  CreateCookiesForDomainCookieTest();
  auto cookie_helper = base::MakeRefCounted<CookieHelper>(storage_partition(),
                                                          base::NullCallback());
  base::RunLoop run_loop;
  cookie_helper->StartFetching(
      base::BindOnce(&CookieHelperTest::DomainCookieCallback,
                     base::Unretained(this), run_loop.QuitClosure()));
  run_loop.Run();
}

TEST_F(CookieHelperTest, DeleteCookie) {
  CreateCookiesForTest();
  auto cookie_helper = base::MakeRefCounted<CookieHelper>(storage_partition(),
                                                          base::NullCallback());

  {
    base::RunLoop run_loop;
    cookie_helper->StartFetching(
        base::BindOnce(&CookieHelperTest::FetchCallback, base::Unretained(this),
                       run_loop.QuitClosure()));
    run_loop.Run();
  }

  net::CanonicalCookie cookie = cookie_list_[0];
  cookie_helper->DeleteCookie(cookie);

  {
    base::RunLoop run_loop;
    cookie_helper->StartFetching(
        base::BindOnce(&CookieHelperTest::DeleteCallback,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }
}

TEST_F(CookieHelperTest, DeleteCookieWithCallback) {
  CreateCookiesForTest();
  bool disable_delete = true;
  auto cookie_helper = base::MakeRefCounted<CookieHelper>(
      storage_partition(), base::BindLambdaForTesting([&](const GURL& url) {
        return disable_delete;
      }));

  {
    base::RunLoop run_loop;
    cookie_helper->StartFetching(
        base::BindOnce(&CookieHelperTest::FetchCallback, base::Unretained(this),
                       run_loop.QuitClosure()));
    run_loop.Run();
  }

  net::CanonicalCookie cookie = cookie_list_[0];
  cookie_helper->DeleteCookie(cookie);
  {
    base::RunLoop run_loop;
    cookie_helper->StartFetching(
        base::BindOnce(&CookieHelperTest::FetchCallback, base::Unretained(this),
                       run_loop.QuitClosure()));

    run_loop.Run();
  }

  disable_delete = false;
  cookie_helper->DeleteCookie(cookie);
  {
    base::RunLoop run_loop;
    cookie_helper->StartFetching(
        base::BindOnce(&CookieHelperTest::DeleteCallback,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }
}

TEST_F(CookieHelperTest, CannedDeleteCookie) {
  CreateCookiesForTest();
  auto helper = base::MakeRefCounted<CannedCookieHelper>(storage_partition(),
                                                         base::NullCallback());

  ASSERT_TRUE(helper->empty());

  const GURL origin1("http://www.google.com");
  std::unique_ptr<net::CanonicalCookie> cookie1(
      net::CanonicalCookie::CreateForTesting(origin1, "A=1",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie1);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin1,
                      origin1,
                      {{*cookie1}}});
  const GURL origin2("http://www.gmail.google.com");
  std::unique_ptr<net::CanonicalCookie> cookie2(
      net::CanonicalCookie::CreateForTesting(origin2, "B=1",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie2);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin2,
                      origin2,
                      {{*cookie2}}});
  {
    base::RunLoop run_loop;
    helper->StartFetching(base::BindOnce(&CookieHelperTest::FetchCallback,
                                         base::Unretained(this),
                                         run_loop.QuitClosure()));
    run_loop.Run();
  }

  EXPECT_EQ(2u, helper->GetCookieCount());

  DeleteCookie(helper.get(), origin1.host());

  EXPECT_EQ(1u, helper->GetCookieCount());
  {
    base::RunLoop run_loop;
    helper->StartFetching(base::BindOnce(&CookieHelperTest::DeleteCallback,
                                         base::Unretained(this),
                                         run_loop.QuitClosure()));
    run_loop.Run();
  }
}

TEST_F(CookieHelperTest, CannedDomainCookie) {
  const GURL origin("http://www.google.com");
  net::CookieList cookies;

  auto helper = base::MakeRefCounted<CannedCookieHelper>(storage_partition(),
                                                         base::NullCallback());

  ASSERT_TRUE(helper->empty());
  std::unique_ptr<net::CanonicalCookie> cookie1(
      net::CanonicalCookie::CreateForTesting(origin, "A=1", base::Time::Now()));
  ASSERT_TRUE(cookie1);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie1}}});
  std::unique_ptr<net::CanonicalCookie> cookie2(
      net::CanonicalCookie::CreateForTesting(
          origin, "A=1; Domain=.www.google.com", base::Time::Now()));
  ASSERT_TRUE(cookie2);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie2}}});
  {
    base::RunLoop run_loop;
    helper->StartFetching(
        base::BindOnce(&CookieHelperTest::CannedDomainCookieCallback,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }

  cookies = cookie_list_;
  helper->Reset();
  ASSERT_TRUE(helper->empty());

  helper->AddCookies({content::CookieAccessDetails::Type::kRead, origin, origin,
                      ConvertCookieListToCookieAccessResultList(cookies), 1u});
  {
    base::RunLoop run_loop;
    helper->StartFetching(
        base::BindOnce(&CookieHelperTest::CannedDomainCookieCallback,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }
}

TEST_F(CookieHelperTest, CannedUnique) {
  const GURL origin("http://www.google.com");

  auto helper = base::MakeRefCounted<CannedCookieHelper>(storage_partition(),
                                                         base::NullCallback());

  ASSERT_TRUE(helper->empty());
  std::unique_ptr<net::CanonicalCookie> cookie(
      net::CanonicalCookie::CreateForTesting(origin, "A=1", base::Time::Now()));
  ASSERT_TRUE(cookie);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie}}});
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie}}});
  {
    base::RunLoop run_loop;
    helper->StartFetching(
        base::BindOnce(&CookieHelperTest::CannedUniqueCallback,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }

  net::CookieList cookie_list = cookie_list_;
  helper->Reset();
  ASSERT_TRUE(helper->empty());

  helper->AddCookies({content::CookieAccessDetails::Type::kRead, origin, origin,
                      ConvertCookieListToCookieAccessResultList(cookie_list)});
  helper->AddCookies({content::CookieAccessDetails::Type::kRead, origin, origin,
                      ConvertCookieListToCookieAccessResultList(cookie_list)});
  {
    base::RunLoop run_loop;
    helper->StartFetching(
        base::BindOnce(&CookieHelperTest::CannedUniqueCallback,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }
}

TEST_F(CookieHelperTest, CannedReplaceCookie) {
  const GURL origin("http://www.google.com");

  auto helper = base::MakeRefCounted<CannedCookieHelper>(storage_partition(),
                                                         base::NullCallback());

  ASSERT_TRUE(helper->empty());
  std::unique_ptr<net::CanonicalCookie> cookie1(
      net::CanonicalCookie::CreateForTesting(origin, "A=1", base::Time::Now()));
  ASSERT_TRUE(cookie1);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie1}}});
  std::unique_ptr<net::CanonicalCookie> cookie2(
      net::CanonicalCookie::CreateForTesting(origin, "A=2", base::Time::Now()));
  ASSERT_TRUE(cookie2);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie2}}});
  std::unique_ptr<net::CanonicalCookie> cookie3(
      net::CanonicalCookie::CreateForTesting(origin, "A=3; Path=/example/0",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie3);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie3}}});
  std::unique_ptr<net::CanonicalCookie> cookie4(
      net::CanonicalCookie::CreateForTesting(origin, "A=4; Path=/example/0",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie4);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie4}}});
  std::unique_ptr<net::CanonicalCookie> cookie5(
      net::CanonicalCookie::CreateForTesting(origin, "A=5; Domain=google.com",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie5);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie5}}});
  std::unique_ptr<net::CanonicalCookie> cookie6(
      net::CanonicalCookie::CreateForTesting(origin, "A=6; Domain=google.com",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie6);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie6}}});
  std::unique_ptr<net::CanonicalCookie> cookie7(
      net::CanonicalCookie::CreateForTesting(
          origin, "A=7; Domain=google.com; Path=/example/1",
          base::Time::Now()));
  ASSERT_TRUE(cookie7);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie7}}});
  std::unique_ptr<net::CanonicalCookie> cookie8(
      net::CanonicalCookie::CreateForTesting(
          origin, "A=8; Domain=google.com; Path=/example/1",
          base::Time::Now()));
  ASSERT_TRUE(cookie8);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie8}}});

  std::unique_ptr<net::CanonicalCookie> cookie9(
      net::CanonicalCookie::CreateForTesting(
          origin, "A=9; Domain=www.google.com", base::Time::Now()));
  ASSERT_TRUE(cookie9);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie9}}});
  std::unique_ptr<net::CanonicalCookie> cookie10(
      net::CanonicalCookie::CreateForTesting(
          origin, "A=10; Domain=www.google.com", base::Time::Now()));
  ASSERT_TRUE(cookie10);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      origin,
                      origin,
                      {{*cookie10}}});
  {
    base::RunLoop run_loop;
    helper->StartFetching(
        base::BindOnce(&CookieHelperTest::CannedReplaceCookieCallback,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }

  net::CookieList cookie_list = cookie_list_;
  helper->Reset();
  ASSERT_TRUE(helper->empty());

  helper->AddCookies({content::CookieAccessDetails::Type::kRead, origin, origin,
                      ConvertCookieListToCookieAccessResultList(cookie_list)});
  helper->AddCookies({content::CookieAccessDetails::Type::kRead, origin, origin,
                      ConvertCookieListToCookieAccessResultList(cookie_list)});
  {
    base::RunLoop run_loop;
    helper->StartFetching(
        base::BindOnce(&CookieHelperTest::CannedReplaceCookieCallback,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }
}

TEST_F(CookieHelperTest, CannedEmpty) {
  const GURL url_google("http://www.google.com");

  auto helper = base::MakeRefCounted<CannedCookieHelper>(storage_partition(),
                                                         base::NullCallback());

  ASSERT_TRUE(helper->empty());
  std::unique_ptr<net::CanonicalCookie> changed_cookie(
      net::CanonicalCookie::CreateForTesting(url_google, "a=1",
                                             base::Time::Now()));
  ASSERT_TRUE(changed_cookie);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      url_google,
                      url_google,
                      {{*changed_cookie}}});
  ASSERT_FALSE(helper->empty());
  helper->Reset();
  ASSERT_TRUE(helper->empty());

  net::CookieList cookies;
  std::unique_ptr<net::CanonicalCookie> cookie(
      net::CanonicalCookie::CreateForTesting(url_google, "a=1",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie);
  cookies.push_back(*cookie);

  helper->AddCookies({content::CookieAccessDetails::Type::kRead, url_google,
                      url_google,
                      ConvertCookieListToCookieAccessResultList(cookies)});
  ASSERT_FALSE(helper->empty());
  helper->Reset();
  ASSERT_TRUE(helper->empty());
}

TEST_F(CookieHelperTest, CannedDifferentFrames) {
  GURL frame1_url("http://www.google.com");
  GURL frame2_url("http://www.google.de");
  GURL request_url("http://www.google.com");

  auto helper = base::MakeRefCounted<CannedCookieHelper>(storage_partition(),
                                                         base::NullCallback());

  ASSERT_TRUE(helper->empty());
  std::unique_ptr<net::CanonicalCookie> cookie1(
      net::CanonicalCookie::CreateForTesting(request_url, "a=1",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie1);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      frame1_url,
                      request_url,
                      {{*cookie1}}});
  std::unique_ptr<net::CanonicalCookie> cookie2(
      net::CanonicalCookie::CreateForTesting(request_url, "b=1",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie2);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      frame1_url,
                      request_url,
                      {{*cookie2}}});
  std::unique_ptr<net::CanonicalCookie> cookie3(
      net::CanonicalCookie::CreateForTesting(request_url, "c=1",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie3);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      frame1_url,
                      request_url,
                      {{*cookie3}}});

  base::RunLoop run_loop;
  helper->StartFetching(
      base::BindOnce(&CookieHelperTest::CannedDifferentFramesCallback,
                     base::Unretained(this), run_loop.QuitClosure()));
  run_loop.Run();
}

TEST_F(CookieHelperTest, CannedGetCookieCount) {
  // The URL in the omnibox is a frame URL. This is not necessarily the request
  // URL, since websites usually include other resources.
  GURL frame1_url("http://www.google.com");
  GURL frame2_url("http://www.google.de");
  // The request URL used for all cookies that are added to the |helper|.
  GURL request1_url("http://static.google.com/foo/res1.html");
  GURL request2_url("http://static.google.com/bar/res2.html");
  std::string cookie_domain(".www.google.com");

  auto helper = base::MakeRefCounted<CannedCookieHelper>(storage_partition(),
                                                         base::NullCallback());

  // Add two different cookies (distinguished by the tuple [cookie-name,
  // domain-value, path-value]) for a HTTP request to |frame1_url| and verify
  // that the cookie count is increased to two. The set-cookie-string consists
  // only of the cookie-pair. This means that the host and the default-path of
  // the |request_url| are used as domain-value and path-value for the added
  // cookies.
  EXPECT_EQ(0U, helper->GetCookieCount());
  std::unique_ptr<net::CanonicalCookie> cookie1(
      net::CanonicalCookie::CreateForTesting(frame1_url, "A=1",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie1);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      frame1_url,
                      frame1_url,
                      {{*cookie1}}});
  EXPECT_EQ(1U, helper->GetCookieCount());
  std::unique_ptr<net::CanonicalCookie> cookie2(
      net::CanonicalCookie::CreateForTesting(frame1_url, "B=1",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie2);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      frame1_url,
                      frame1_url,
                      {{*cookie2}}});
  EXPECT_EQ(2U, helper->GetCookieCount());

  // Use a different frame URL for adding another cookie that will replace one
  // of the previously added cookies. This could happen during an automatic
  // redirect e.g. |frame1_url| redirects to |frame2_url| and a cookie set by a
  // request to |frame1_url| is updated.
  // The cookie-name of |cookie3| must match the cookie-name of |cookie1|.
  std::unique_ptr<net::CanonicalCookie> cookie3(
      net::CanonicalCookie::CreateForTesting(frame1_url, "A=2",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie3);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      frame2_url,
                      frame1_url,
                      {{*cookie3}}});
  EXPECT_EQ(2U, helper->GetCookieCount());

  // Add two more cookies that are set while loading resources. The two cookies
  // below have a differnt path-value since the request URLs have different
  // paths.
  std::unique_ptr<net::CanonicalCookie> cookie4(
      net::CanonicalCookie::CreateForTesting(request1_url, "A=2",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie4);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      frame2_url,
                      request1_url,
                      {{*cookie4}}});
  EXPECT_EQ(3U, helper->GetCookieCount());
  std::unique_ptr<net::CanonicalCookie> cookie5(
      net::CanonicalCookie::CreateForTesting(request2_url, "A=2",
                                             base::Time::Now()));
  ASSERT_TRUE(cookie5);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      frame2_url,
                      request2_url,
                      {{*cookie5}}});
  EXPECT_EQ(4U, helper->GetCookieCount());

  // Host-only and domain cookies are treated as seperate items. This means that
  // the following two cookie-strings are stored as two separate cookies, even
  // though they have the same name and are send with the same request:
  //   "A=1;
  //   "A=3; Domain=www.google.com"
  // Add a domain cookie and check if it increases the cookie count.
  std::unique_ptr<net::CanonicalCookie> cookie6(
      net::CanonicalCookie::CreateForTesting(
          frame1_url, "A=3; Domain=.www.google.com", base::Time::Now()));
  ASSERT_TRUE(cookie6);
  helper->AddCookies({content::CookieAccessDetails::Type::kChange,
                      frame2_url,
                      frame1_url,
                      {{*cookie6}}});
  EXPECT_EQ(5U, helper->GetCookieCount());
}

}  // namespace
}  // namespace browsing_data