forked from youtube/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie_monster_unittest.cc
5867 lines (5060 loc) · 258 KB
/
cookie_monster_unittest.cc
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 "net/cookies/cookie_monster.h"
#include <stdint.h>
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/containers/queue.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
#include "base/ranges/algorithm.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "net/base/features.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/canonical_cookie_test_helpers.h"
#include "net/cookies/cookie_change_dispatcher.h"
#include "net/cookies/cookie_constants.h"
#include "net/cookies/cookie_inclusion_status.h"
#include "net/cookies/cookie_monster_store_test.h" // For CookieStore mock
#include "net/cookies/cookie_partition_key.h"
#include "net/cookies/cookie_store.h"
#include "net/cookies/cookie_store_change_unittest.h"
#include "net/cookies/cookie_store_test_callbacks.h"
#include "net/cookies/cookie_store_test_helpers.h"
#include "net/cookies/cookie_store_unittest.h"
#include "net/cookies/cookie_util.h"
#include "net/cookies/parsed_cookie.h"
#include "net/cookies/test_cookie_access_delegate.h"
#include "net/log/net_log_with_source.h"
#include "net/log/test_net_log.h"
#include "net/log/test_net_log_util.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "url/gurl.h"
#include "url/third_party/mozilla/url_parse.h"
#include "url/url_constants.h"
namespace net {
using base::Time;
using CookieDeletionInfo = net::CookieDeletionInfo;
namespace {
using testing::ElementsAre;
// False means 'less than or equal', so we test both ways for full equal.
MATCHER_P(CookieEquals, expected, "") {
return !(arg.FullCompare(expected) || expected.FullCompare(arg));
}
MATCHER_P2(MatchesCookieNameDomain, name, domain, "") {
return testing::ExplainMatchResult(
testing::AllOf(testing::Property(&net::CanonicalCookie::Name, name),
testing::Property(&net::CanonicalCookie::Domain, domain)),
arg, result_listener);
}
MATCHER_P4(MatchesCookieNameValueCreationExpiry,
name,
value,
creation,
expiry,
"") {
return testing::ExplainMatchResult(
testing::AllOf(
testing::Property(&net::CanonicalCookie::Name, name),
testing::Property(&net::CanonicalCookie::Value, value),
testing::Property(&net::CanonicalCookie::CreationDate, creation),
// We need a margin of error when testing the ExpiryDate as, if
// clamped, it is set relative to the current time.
testing::Property(&net::CanonicalCookie::ExpiryDate,
testing::Gt(expiry - base::Minutes(1))),
testing::Property(&net::CanonicalCookie::ExpiryDate,
testing::Lt(expiry + base::Minutes(1)))),
arg, result_listener);
}
const char kTopLevelDomainPlus1[] = "http://www.harvard.edu";
const char kTopLevelDomainPlus2[] = "http://www.math.harvard.edu";
const char kTopLevelDomainPlus2Secure[] = "https://www.math.harvard.edu";
const char kTopLevelDomainPlus3[] = "http://www.bourbaki.math.harvard.edu";
const char kOtherDomain[] = "http://www.mit.edu";
struct CookieMonsterTestTraits {
static std::unique_ptr<CookieStore> Create() {
return std::make_unique<CookieMonster>(nullptr /* store */,
nullptr /* netlog */);
}
static void DeliverChangeNotifications() { base::RunLoop().RunUntilIdle(); }
static const bool supports_http_only = true;
static const bool supports_non_dotted_domains = true;
static const bool preserves_trailing_dots = true;
static const bool filters_schemes = true;
static const bool has_path_prefix_bug = false;
static const bool forbids_setting_empty_name = false;
static const bool supports_global_cookie_tracking = true;
static const bool supports_url_cookie_tracking = true;
static const bool supports_named_cookie_tracking = true;
static const bool supports_multiple_tracking_callbacks = true;
static const bool has_exact_change_cause = true;
static const bool has_exact_change_ordering = true;
static const int creation_time_granularity_in_ms = 0;
static const bool supports_cookie_access_semantics = true;
static const bool supports_partitioned_cookies = true;
};
INSTANTIATE_TYPED_TEST_SUITE_P(CookieMonster,
CookieStoreTest,
CookieMonsterTestTraits);
INSTANTIATE_TYPED_TEST_SUITE_P(CookieMonster,
CookieStoreChangeGlobalTest,
CookieMonsterTestTraits);
INSTANTIATE_TYPED_TEST_SUITE_P(CookieMonster,
CookieStoreChangeUrlTest,
CookieMonsterTestTraits);
INSTANTIATE_TYPED_TEST_SUITE_P(CookieMonster,
CookieStoreChangeNamedTest,
CookieMonsterTestTraits);
template <typename T>
class CookieMonsterTestBase : public CookieStoreTest<T> {
public:
using CookieStoreTest<T>::SetCookie;
protected:
using CookieStoreTest<T>::http_www_foo_;
using CookieStoreTest<T>::https_www_foo_;
CookieList GetAllCookiesForURLWithOptions(
CookieMonster* cm,
const GURL& url,
const CookieOptions& options,
const CookiePartitionKeyCollection& cookie_partition_key_collection =
CookiePartitionKeyCollection()) {
DCHECK(cm);
GetCookieListCallback callback;
cm->GetCookieListWithOptionsAsync(
url, options, cookie_partition_key_collection, callback.MakeCallback());
callback.WaitUntilDone();
return callback.cookies();
}
CookieAccessResultList GetExcludedCookiesForURLWithOptions(
CookieMonster* cm,
const GURL& url,
const CookieOptions& options,
const CookiePartitionKeyCollection& cookie_partition_key_collection =
CookiePartitionKeyCollection()) {
DCHECK(cm);
GetCookieListCallback callback;
cm->GetCookieListWithOptionsAsync(
url, options, cookie_partition_key_collection, callback.MakeCallback());
callback.WaitUntilDone();
return callback.excluded_cookies();
}
bool SetAllCookies(CookieMonster* cm, const CookieList& list) {
DCHECK(cm);
ResultSavingCookieCallback<CookieAccessResult> callback;
cm->SetAllCookiesAsync(list, callback.MakeCallback());
callback.WaitUntilDone();
return callback.result().status.IsInclude();
}
bool SetCookieWithCreationTime(
CookieMonster* cm,
const GURL& url,
const std::string& cookie_line,
base::Time creation_time,
absl::optional<CookiePartitionKey> cookie_partition_key = absl::nullopt) {
DCHECK(cm);
DCHECK(!creation_time.is_null());
ResultSavingCookieCallback<CookieAccessResult> callback;
cm->SetCanonicalCookieAsync(
CanonicalCookie::Create(url, cookie_line, creation_time,
absl::nullopt /* server_time */,
cookie_partition_key),
url, CookieOptions::MakeAllInclusive(), callback.MakeCallback());
callback.WaitUntilDone();
return callback.result().status.IsInclude();
}
uint32_t DeleteAllCreatedInTimeRange(CookieMonster* cm,
const TimeRange& creation_range) {
DCHECK(cm);
ResultSavingCookieCallback<uint32_t> callback;
cm->DeleteAllCreatedInTimeRangeAsync(creation_range,
callback.MakeCallback());
callback.WaitUntilDone();
return callback.result();
}
uint32_t DeleteAllMatchingInfo(CookieMonster* cm,
CookieDeletionInfo delete_info) {
DCHECK(cm);
ResultSavingCookieCallback<uint32_t> callback;
cm->DeleteAllMatchingInfoAsync(std::move(delete_info),
callback.MakeCallback());
callback.WaitUntilDone();
return callback.result();
}
uint32_t DeleteMatchingCookies(CookieMonster* cm,
CookieStore::DeletePredicate predicate) {
DCHECK(cm);
ResultSavingCookieCallback<uint32_t> callback;
cm->DeleteMatchingCookiesAsync(std::move(predicate),
callback.MakeCallback());
callback.WaitUntilDone();
return callback.result();
}
// Helper for PredicateSeesAllCookies test; repopulates CM with same layout
// each time. Returns the time which is strictly greater than any creation
// time which was passed to created cookies.
base::Time PopulateCmForPredicateCheck(CookieMonster* cm) {
std::string url_top_level_domain_plus_1(GURL(kTopLevelDomainPlus1).host());
std::string url_top_level_domain_plus_2(GURL(kTopLevelDomainPlus2).host());
std::string url_top_level_domain_plus_3(GURL(kTopLevelDomainPlus3).host());
std::string url_top_level_domain_secure(
GURL(kTopLevelDomainPlus2Secure).host());
std::string url_other(GURL(kOtherDomain).host());
this->DeleteAll(cm);
// Static population for probe:
// * Three levels of domain cookie (.b.a, .c.b.a, .d.c.b.a)
// * Three levels of host cookie (w.b.a, w.c.b.a, w.d.c.b.a)
// * http_only cookie (w.c.b.a)
// * same_site cookie (w.c.b.a)
// * same_party cookie (w.c.b.a)
// * Two secure cookies (.c.b.a, w.c.b.a)
// * Two domain path cookies (.c.b.a/dir1, .c.b.a/dir1/dir2)
// * Two host path cookies (w.c.b.a/dir1, w.c.b.a/dir1/dir2)
std::vector<std::unique_ptr<CanonicalCookie>> cookies;
const base::Time now = base::Time::Now();
// Domain cookies
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"dom_1", "A", ".harvard.edu", "/", now, base::Time(), base::Time(),
base::Time(), false, false, CookieSameSite::LAX_MODE,
COOKIE_PRIORITY_DEFAULT, false));
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"dom_2", "B", ".math.harvard.edu", "/", now, base::Time(), base::Time(),
base::Time(), false, false, CookieSameSite::LAX_MODE,
COOKIE_PRIORITY_DEFAULT, false));
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"dom_3", "C", ".bourbaki.math.harvard.edu", "/", now, base::Time(),
base::Time(), base::Time(), false, false, CookieSameSite::LAX_MODE,
COOKIE_PRIORITY_DEFAULT, false));
// Host cookies
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"host_1", "A", url_top_level_domain_plus_1, "/", now, base::Time(),
base::Time(), base::Time(), false, false, CookieSameSite::LAX_MODE,
COOKIE_PRIORITY_DEFAULT, false));
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"host_2", "B", url_top_level_domain_plus_2, "/", now, base::Time(),
base::Time(), base::Time(), false, false, CookieSameSite::LAX_MODE,
COOKIE_PRIORITY_DEFAULT, false));
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"host_3", "C", url_top_level_domain_plus_3, "/", now, base::Time(),
base::Time(), base::Time(), false, false, CookieSameSite::LAX_MODE,
COOKIE_PRIORITY_DEFAULT, false));
// http_only cookie
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"httpo_check", "A", url_top_level_domain_plus_2, "/", now, base::Time(),
base::Time(), base::Time(), false, true, CookieSameSite::LAX_MODE,
COOKIE_PRIORITY_DEFAULT, false));
// same-site cookie
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"same_site_check", "A", url_top_level_domain_plus_2, "/", now,
base::Time(), base::Time(), base::Time(), false, false,
CookieSameSite::STRICT_MODE, COOKIE_PRIORITY_DEFAULT, false));
// same-party cookie
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"same_party_check", "A", url_top_level_domain_plus_2, "/", now,
base::Time(), base::Time(), base::Time(), true /* secure */, false,
CookieSameSite::LAX_MODE, COOKIE_PRIORITY_DEFAULT,
true /* same_party */));
// Secure cookies
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"sec_dom", "A", ".math.harvard.edu", "/", now, base::Time(),
base::Time(), base::Time(), true, false, CookieSameSite::NO_RESTRICTION,
COOKIE_PRIORITY_DEFAULT, false));
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"sec_host", "B", url_top_level_domain_plus_2, "/", now, base::Time(),
base::Time(), base::Time(), true, false, CookieSameSite::NO_RESTRICTION,
COOKIE_PRIORITY_DEFAULT, false));
// Domain path cookies
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"dom_path_1", "A", ".math.harvard.edu", "/dir1", now, base::Time(),
base::Time(), base::Time(), false, false, CookieSameSite::LAX_MODE,
COOKIE_PRIORITY_DEFAULT, false));
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"dom_path_2", "B", ".math.harvard.edu", "/dir1/dir2", now, base::Time(),
base::Time(), base::Time(), false, false, CookieSameSite::LAX_MODE,
COOKIE_PRIORITY_DEFAULT, false));
// Host path cookies
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"host_path_1", "A", url_top_level_domain_plus_2, "/dir1", now,
base::Time(), base::Time(), base::Time(), false, false,
CookieSameSite::LAX_MODE, COOKIE_PRIORITY_DEFAULT, false));
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"host_path_2", "B", url_top_level_domain_plus_2, "/dir1/dir2", now,
base::Time(), base::Time(), base::Time(), false, false,
CookieSameSite::LAX_MODE, COOKIE_PRIORITY_DEFAULT, false));
// Partitioned cookies
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"__Host-pc_1", "A", url_top_level_domain_secure, "/", now, base::Time(),
base::Time(), base::Time(), true, false, CookieSameSite::NO_RESTRICTION,
CookiePriority::COOKIE_PRIORITY_DEFAULT, false,
CookiePartitionKey::FromURLForTesting(GURL(kTopLevelDomainPlus1))));
cookies.push_back(CanonicalCookie::CreateUnsafeCookieForTesting(
"__Host-pc_2", "B", url_top_level_domain_secure, "/", now, base::Time(),
base::Time(), base::Time(), true, false, CookieSameSite::NO_RESTRICTION,
CookiePriority::COOKIE_PRIORITY_DEFAULT, false,
CookiePartitionKey::FromURLForTesting(GURL(kTopLevelDomainPlus1))));
for (auto& cookie : cookies) {
GURL source_url = cookie_util::SimulatedCookieSource(
*cookie, cookie->IsSecure() ? "https" : "http");
EXPECT_TRUE(this->SetCanonicalCookie(cm, std::move(cookie), source_url,
true /* modify_httponly */));
}
EXPECT_EQ(cookies.size(), this->GetAllCookies(cm).size());
return now + base::Milliseconds(100);
}
Time GetFirstCookieAccessDate(CookieMonster* cm) {
const CookieList all_cookies(this->GetAllCookies(cm));
return all_cookies.front().LastAccessDate();
}
bool FindAndDeleteCookie(CookieMonster* cm,
const std::string& domain,
const std::string& name) {
CookieList cookies = this->GetAllCookies(cm);
for (auto& cookie : cookies)
if (cookie.Domain() == domain && cookie.Name() == name)
return this->DeleteCanonicalCookie(cm, cookie);
return false;
}
void TestHostGarbageCollectHelper() {
int domain_max_cookies = CookieMonster::kDomainMaxCookies;
int domain_purge_cookies = CookieMonster::kDomainPurgeCookies;
const int more_than_enough_cookies = domain_max_cookies + 10;
// Add a bunch of cookies on a single host, should purge them.
{
auto cm = std::make_unique<CookieMonster>(nullptr, net::NetLog::Get());
for (int i = 0; i < more_than_enough_cookies; ++i) {
std::string cookie = base::StringPrintf("a%03d=b", i);
EXPECT_TRUE(SetCookie(cm.get(), http_www_foo_.url(), cookie));
std::string cookies = this->GetCookies(cm.get(), http_www_foo_.url());
// Make sure we find it in the cookies.
EXPECT_NE(cookies.find(cookie), std::string::npos);
// Count the number of cookies.
EXPECT_LE(base::ranges::count(cookies, '='), domain_max_cookies);
}
}
// Add a bunch of cookies on multiple hosts within a single eTLD.
// Should keep at least kDomainMaxCookies - kDomainPurgeCookies
// between them. We shouldn't go above kDomainMaxCookies for both together.
GURL url_google_specific(http_www_foo_.Format("http://www.gmail.%D"));
{
auto cm = std::make_unique<CookieMonster>(nullptr, net::NetLog::Get());
for (int i = 0; i < more_than_enough_cookies; ++i) {
std::string cookie_general = base::StringPrintf("a%03d=b", i);
EXPECT_TRUE(SetCookie(cm.get(), http_www_foo_.url(), cookie_general));
std::string cookie_specific = base::StringPrintf("c%03d=b", i);
EXPECT_TRUE(SetCookie(cm.get(), url_google_specific, cookie_specific));
std::string cookies_general =
this->GetCookies(cm.get(), http_www_foo_.url());
EXPECT_NE(cookies_general.find(cookie_general), std::string::npos);
std::string cookies_specific =
this->GetCookies(cm.get(), url_google_specific);
EXPECT_NE(cookies_specific.find(cookie_specific), std::string::npos);
EXPECT_LE((base::ranges::count(cookies_general, '=') +
base::ranges::count(cookies_specific, '=')),
domain_max_cookies);
}
// After all this, there should be at least
// kDomainMaxCookies - kDomainPurgeCookies for both URLs.
std::string cookies_general =
this->GetCookies(cm.get(), http_www_foo_.url());
std::string cookies_specific =
this->GetCookies(cm.get(), url_google_specific);
int total_cookies = (base::ranges::count(cookies_general, '=') +
base::ranges::count(cookies_specific, '='));
EXPECT_GE(total_cookies, domain_max_cookies - domain_purge_cookies);
EXPECT_LE(total_cookies, domain_max_cookies);
}
// Test histogram for the number of registrable domains affected by domain
// purge.
{
auto cm = std::make_unique<CookieMonster>(nullptr, net::NetLog::Get());
GURL url;
for (int domain_num = 0; domain_num < 3; ++domain_num) {
url = GURL(base::StringPrintf("http://domain%d.test", domain_num));
for (int i = 0; i < more_than_enough_cookies; ++i) {
std::string cookie = base::StringPrintf("a%03d=b", i);
EXPECT_TRUE(SetCookie(cm.get(), url, cookie));
std::string cookies = this->GetCookies(cm.get(), url);
// Make sure we find it in the cookies.
EXPECT_NE(cookies.find(cookie), std::string::npos);
// Count the number of cookies.
EXPECT_LE(base::ranges::count(cookies, '='), domain_max_cookies);
}
}
// Triggering eviction again for a previously affected registrable domain
// does not increment the histogram.
for (int i = 0; i < domain_purge_cookies * 2; ++i) {
// Add some extra cookies (different names than before).
std::string cookie = base::StringPrintf("b%03d=b", i);
EXPECT_TRUE(SetCookie(cm.get(), url, cookie));
std::string cookies = this->GetCookies(cm.get(), url);
// Make sure we find it in the cookies.
EXPECT_NE(cookies.find(cookie), std::string::npos);
// Count the number of cookies.
EXPECT_LE(base::ranges::count(cookies, '='), domain_max_cookies);
}
}
}
CookiePriority CharToPriority(char ch) {
switch (ch) {
case 'L':
return COOKIE_PRIORITY_LOW;
case 'M':
return COOKIE_PRIORITY_MEDIUM;
case 'H':
return COOKIE_PRIORITY_HIGH;
}
NOTREACHED();
return COOKIE_PRIORITY_DEFAULT;
}
// Instantiates a CookieMonster, adds multiple cookies (to http_www_foo_)
// with priorities specified by |coded_priority_str|, and tests priority-aware
// domain cookie eviction.
//
// Example: |coded_priority_string| of "2MN 3LS MN 4HN" specifies sequential
// (i.e., from least- to most-recently accessed) insertion of 2
// medium-priority non-secure cookies, 3 low-priority secure cookies, 1
// medium-priority non-secure cookie, and 4 high-priority non-secure cookies.
//
// Within each priority, only the least-accessed cookies should be evicted.
// Thus, to describe expected suriving cookies, it suffices to specify the
// expected population of surviving cookies per priority, i.e.,
// |expected_low_count|, |expected_medium_count|, and |expected_high_count|.
void TestPriorityCookieCase(CookieMonster* cm,
const std::string& coded_priority_str,
size_t expected_low_count,
size_t expected_medium_count,
size_t expected_high_count,
size_t expected_nonsecure,
size_t expected_secure) {
SCOPED_TRACE(coded_priority_str);
this->DeleteAll(cm);
int next_cookie_id = 0;
// A list of cookie IDs, indexed by secure status, then by priority.
std::vector<int> id_list[2][3];
// A list of all the cookies stored, along with their properties.
std::vector<std::pair<bool, CookiePriority>> cookie_data;
// Parse |coded_priority_str| and add cookies.
for (const std::string& token :
base::SplitString(coded_priority_str, " ", base::TRIM_WHITESPACE,
base::SPLIT_WANT_ALL)) {
DCHECK(!token.empty());
bool is_secure = token.back() == 'S';
// The second-to-last character is the priority. Grab and discard it.
CookiePriority priority = CharToPriority(token[token.size() - 2]);
// Discard the security status and priority tokens. The rest of the string
// (possibly empty) specifies repetition.
int rep = 1;
if (!token.empty()) {
bool result = base::StringToInt(
base::MakeStringPiece(token.begin(), token.end() - 2), &rep);
DCHECK(result);
}
for (; rep > 0; --rep, ++next_cookie_id) {
std::string cookie =
base::StringPrintf("a%d=b;priority=%s;%s", next_cookie_id,
CookiePriorityToString(priority).c_str(),
is_secure ? "secure" : "");
EXPECT_TRUE(SetCookie(cm, https_www_foo_.url(), cookie));
cookie_data.emplace_back(is_secure, priority);
id_list[is_secure][priority].push_back(next_cookie_id);
}
}
int num_cookies = static_cast<int>(cookie_data.size());
// A list of cookie IDs, indexed by secure status, then by priority.
std::vector<int> surviving_id_list[2][3];
// Parse the list of cookies
std::string cookie_str = this->GetCookies(cm, https_www_foo_.url());
size_t num_nonsecure = 0;
size_t num_secure = 0;
for (const std::string& token : base::SplitString(
cookie_str, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
// Assuming *it is "a#=b", so extract and parse "#" portion.
int id = -1;
bool result = base::StringToInt(
base::MakeStringPiece(token.begin() + 1, token.end() - 2), &id);
DCHECK(result);
DCHECK_GE(id, 0);
DCHECK_LT(id, num_cookies);
surviving_id_list[cookie_data[id].first][cookie_data[id].second]
.push_back(id);
if (cookie_data[id].first)
num_secure += 1;
else
num_nonsecure += 1;
}
EXPECT_EQ(expected_nonsecure, num_nonsecure);
EXPECT_EQ(expected_secure, num_secure);
// Validate each priority.
size_t expected_count[3] = {expected_low_count, expected_medium_count,
expected_high_count};
for (int i = 0; i < 3; ++i) {
size_t num_for_priority =
surviving_id_list[0][i].size() + surviving_id_list[1][i].size();
EXPECT_EQ(expected_count[i], num_for_priority);
// Verify that the remaining cookies are the most recent among those
// with the same priorities.
if (expected_count[i] == num_for_priority) {
// Non-secure:
std::sort(surviving_id_list[0][i].begin(),
surviving_id_list[0][i].end());
EXPECT_TRUE(std::equal(
surviving_id_list[0][i].begin(), surviving_id_list[0][i].end(),
id_list[0][i].end() - surviving_id_list[0][i].size()));
// Secure:
std::sort(surviving_id_list[1][i].begin(),
surviving_id_list[1][i].end());
EXPECT_TRUE(std::equal(
surviving_id_list[1][i].begin(), surviving_id_list[1][i].end(),
id_list[1][i].end() - surviving_id_list[1][i].size()));
}
}
}
// Represents a number of cookies to create, if they are Secure cookies, and
// a url to add them to.
struct CookiesEntry {
size_t num_cookies;
bool is_secure;
};
// A number of secure and a number of non-secure alternative hosts to create
// for testing.
typedef std::pair<size_t, size_t> AltHosts;
// Takes an array of CookieEntries which specify the number, type, and order
// of cookies to create. Cookies are created in the order they appear in
// cookie_entries. The value of cookie_entries[x].num_cookies specifies how
// many cookies of that type to create consecutively, while if
// cookie_entries[x].is_secure is |true|, those cookies will be marked as
// Secure.
void TestSecureCookieEviction(base::span<const CookiesEntry> cookie_entries,
size_t expected_secure_cookies,
size_t expected_non_secure_cookies,
const AltHosts* alt_host_entries) {
std::unique_ptr<CookieMonster> cm;
if (alt_host_entries == nullptr) {
cm = std::make_unique<CookieMonster>(nullptr, net::NetLog::Get());
} else {
// When generating all of these cookies on alternate hosts, they need to
// be all older than the max "safe" date for GC, which is currently 30
// days, so we set them to 60.
cm = CreateMonsterFromStoreForGC(
alt_host_entries->first, alt_host_entries->first,
alt_host_entries->second, alt_host_entries->second, 60);
}
int next_cookie_id = 0;
for (const auto& cookie_entry : cookie_entries) {
for (size_t j = 0; j < cookie_entry.num_cookies; j++) {
std::string cookie;
if (cookie_entry.is_secure)
cookie = base::StringPrintf("a%d=b; Secure", next_cookie_id);
else
cookie = base::StringPrintf("a%d=b", next_cookie_id);
EXPECT_TRUE(SetCookie(cm.get(), https_www_foo_.url(), cookie));
++next_cookie_id;
}
}
CookieList cookies = this->GetAllCookies(cm.get());
EXPECT_EQ(expected_secure_cookies + expected_non_secure_cookies,
cookies.size());
size_t total_secure_cookies = 0;
size_t total_non_secure_cookies = 0;
for (const auto& cookie : cookies) {
if (cookie.IsSecure())
++total_secure_cookies;
else
++total_non_secure_cookies;
}
EXPECT_EQ(expected_secure_cookies, total_secure_cookies);
EXPECT_EQ(expected_non_secure_cookies, total_non_secure_cookies);
}
void TestPriorityAwareGarbageCollectHelperNonSecure() {
// Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint.
DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies);
DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies -
CookieMonster::kDomainPurgeCookies);
auto cm = std::make_unique<CookieMonster>(nullptr, net::NetLog::Get());
// Key:
// Round 1 => LN; round 2 => LS; round 3 => MN.
// Round 4 => HN; round 5 => MS; round 6 => HS
// Each test case adds 181 cookies, so 31 cookies are evicted.
// Cookie same priority, repeated for each priority.
TestPriorityCookieCase(cm.get(), "181LN", 150U, 0U, 0U, 150U, 0U);
TestPriorityCookieCase(cm.get(), "181MN", 0U, 150U, 0U, 150U, 0U);
TestPriorityCookieCase(cm.get(), "181HN", 0U, 0U, 150U, 150U, 0U);
// Pairwise scenarios.
// Round 1 => none; round2 => 31M; round 3 => none.
TestPriorityCookieCase(cm.get(), "10HN 171MN", 0U, 140U, 10U, 150U, 0U);
// Round 1 => 10L; round2 => 21M; round 3 => none.
TestPriorityCookieCase(cm.get(), "141MN 40LN", 30U, 120U, 0U, 150U, 0U);
// Round 1 => none; round2 => 30M; round 3 => 1H.
TestPriorityCookieCase(cm.get(), "101HN 80MN", 0U, 50U, 100U, 150U, 0U);
// For {low, medium} priorities right on quota, different orders.
// Round 1 => 1L; round 2 => none, round3 => 30H.
TestPriorityCookieCase(cm.get(), "31LN 50MN 100HN", 30U, 50U, 70U, 150U,
0U);
// Round 1 => none; round 2 => 1M, round3 => 30H.
TestPriorityCookieCase(cm.get(), "51MN 100HN 30LN", 30U, 50U, 70U, 150U,
0U);
// Round 1 => none; round 2 => none; round3 => 31H.
TestPriorityCookieCase(cm.get(), "101HN 50MN 30LN", 30U, 50U, 70U, 150U,
0U);
// Round 1 => 10L; round 2 => 10M; round3 => 11H.
TestPriorityCookieCase(cm.get(), "81HN 60MN 40LN", 30U, 50U, 70U, 150U, 0U);
// More complex scenarios.
// Round 1 => 10L; round 2 => 10M; round 3 => 11H.
TestPriorityCookieCase(cm.get(), "21HN 60MN 40LN 60HN", 30U, 50U, 70U, 150U,
0U);
// Round 1 => 10L; round 2 => 21M; round 3 => 0H.
TestPriorityCookieCase(cm.get(), "11HN 10MN 20LN 110MN 20LN 10HN", 30U, 99U,
21U, 150U, 0U);
// Round 1 => none; round 2 => none; round 3 => 31H.
TestPriorityCookieCase(cm.get(), "11LN 10MN 140HN 10MN 10LN", 21U, 20U,
109U, 150U, 0U);
// Round 1 => none; round 2 => 21M; round 3 => 10H.
TestPriorityCookieCase(cm.get(), "11MN 10HN 10LN 60MN 90HN", 10U, 50U, 90U,
150U, 0U);
// Round 1 => none; round 2 => 31M; round 3 => none.
TestPriorityCookieCase(cm.get(), "11MN 10HN 10LN 90MN 60HN", 10U, 70U, 70U,
150U, 0U);
// Round 1 => 20L; round 2 => 0; round 3 => 11H
TestPriorityCookieCase(cm.get(), "50LN 131HN", 30U, 0U, 120U, 150U, 0U);
// Round 1 => 20L; round 2 => 0; round 3 => 11H
TestPriorityCookieCase(cm.get(), "131HN 50LN", 30U, 0U, 120U, 150U, 0U);
// Round 1 => 20L; round 2 => none; round 3 => 11H.
TestPriorityCookieCase(cm.get(), "50HN 50LN 81HN", 30U, 0U, 120U, 150U, 0U);
// Round 1 => 20L; round 2 => none; round 3 => 11H.
TestPriorityCookieCase(cm.get(), "81HN 50LN 50HN", 30U, 0U, 120U, 150U, 0U);
}
void TestPriorityAwareGarbageCollectHelperSecure() {
// Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint.
DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies);
DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies -
CookieMonster::kDomainPurgeCookies);
auto cm = std::make_unique<CookieMonster>(nullptr, net::NetLog::Get());
// Key:
// Round 1 => LN; round 2 => LS; round 3 => MN.
// Round 4 => HN; round 5 => MS; round 6 => HS
// Each test case adds 181 cookies, so 31 cookies are evicted.
// Cookie same priority, repeated for each priority.
// Round 1 => 31L; round2 => none; round 3 => none.
TestPriorityCookieCase(cm.get(), "181LS", 150U, 0U, 0U, 0U, 150U);
// Round 1 => none; round2 => 31M; round 3 => none.
TestPriorityCookieCase(cm.get(), "181MS", 0U, 150U, 0U, 0U, 150U);
// Round 1 => none; round2 => none; round 3 => 31H.
TestPriorityCookieCase(cm.get(), "181HS", 0U, 0U, 150U, 0U, 150U);
// Pairwise scenarios.
// Round 1 => none; round2 => 31M; round 3 => none.
TestPriorityCookieCase(cm.get(), "10HS 171MS", 0U, 140U, 10U, 0U, 150U);
// Round 1 => 10L; round2 => 21M; round 3 => none.
TestPriorityCookieCase(cm.get(), "141MS 40LS", 30U, 120U, 0U, 0U, 150U);
// Round 1 => none; round2 => 30M; round 3 => 1H.
TestPriorityCookieCase(cm.get(), "101HS 80MS", 0U, 50U, 100U, 0U, 150U);
// For {low, medium} priorities right on quota, different orders.
// Round 1 => 1L; round 2 => none, round3 => 30H.
TestPriorityCookieCase(cm.get(), "31LS 50MS 100HS", 30U, 50U, 70U, 0U,
150U);
// Round 1 => none; round 2 => 1M, round3 => 30H.
TestPriorityCookieCase(cm.get(), "51MS 100HS 30LS", 30U, 50U, 70U, 0U,
150U);
// Round 1 => none; round 2 => none; round3 => 31H.
TestPriorityCookieCase(cm.get(), "101HS 50MS 30LS", 30U, 50U, 70U, 0U,
150U);
// Round 1 => 10L; round 2 => 10M; round3 => 11H.
TestPriorityCookieCase(cm.get(), "81HS 60MS 40LS", 30U, 50U, 70U, 0U, 150U);
// More complex scenarios.
// Round 1 => 10L; round 2 => 10M; round 3 => 11H.
TestPriorityCookieCase(cm.get(), "21HS 60MS 40LS 60HS", 30U, 50U, 70U, 0U,
150U);
// Round 1 => 10L; round 2 => 21M; round 3 => none.
TestPriorityCookieCase(cm.get(), "11HS 10MS 20LS 110MS 20LS 10HS", 30U, 99U,
21U, 0U, 150U);
// Round 1 => none; round 2 => none; round 3 => 31H.
TestPriorityCookieCase(cm.get(), "11LS 10MS 140HS 10MS 10LS", 21U, 20U,
109U, 0U, 150U);
// Round 1 => none; round 2 => 21M; round 3 => 10H.
TestPriorityCookieCase(cm.get(), "11MS 10HS 10LS 60MS 90HS", 10U, 50U, 90U,
0U, 150U);
// Round 1 => none; round 2 => 31M; round 3 => none.
TestPriorityCookieCase(cm.get(), "11MS 10HS 10LS 90MS 60HS", 10U, 70U, 70U,
0U, 150U);
}
void TestPriorityAwareGarbageCollectHelperMixed() {
// Hard-coding limits in the test, but use DCHECK_EQ to enforce constraint.
DCHECK_EQ(180U, CookieMonster::kDomainMaxCookies);
DCHECK_EQ(150U, CookieMonster::kDomainMaxCookies -
CookieMonster::kDomainPurgeCookies);
auto cm = std::make_unique<CookieMonster>(nullptr, net::NetLog::Get());
// Key:
// Round 1 => LN; round 2 => LS; round 3 => MN.
// Round 4 => HN; round 5 => MS; round 6 => HS
// Each test case adds 180 secure cookies, and some non-secure cookie. The
// secure cookies take priority, so the non-secure cookie is removed, along
// with 30 secure cookies. Repeated for each priority, and with the
// non-secure cookie as older and newer.
// Round 1 => 1LN; round 2 => 30LS; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "1LN 180LS", 150U, 0U, 0U, 0U, 150U);
// Round 1 => none; round 2 => none; round 3 => 1MN.
// Round 4 => none; round 5 => 30MS; round 6 => none.
TestPriorityCookieCase(cm.get(), "1MN 180MS", 0U, 150U, 0U, 0U, 150U);
// Round 1 => none; round 2 => none; round 3 => none.
// Round 4 => 1HN; round 5 => none; round 6 => 30HS.
TestPriorityCookieCase(cm.get(), "1HN 180HS", 0U, 0U, 150U, 0U, 150U);
// Round 1 => 1LN; round 2 => 30LS; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "180LS 1LN", 150U, 0U, 0U, 0U, 150U);
// Round 1 => none; round 2 => none; round 3 => 1MN.
// Round 4 => none; round 5 => 30MS; round 6 => none.
TestPriorityCookieCase(cm.get(), "180MS 1MN", 0U, 150U, 0U, 0U, 150U);
// Round 1 => none; round 2 => none; round 3 => none.
// Round 4 => 1HN; round 5 => none; round 6 => 30HS.
TestPriorityCookieCase(cm.get(), "180HS 1HN", 0U, 0U, 150U, 0U, 150U);
// Quotas should be correctly maintained when a given priority has both
// secure and non-secure cookies.
//
// Round 1 => 10LN; round 2 => none; round 3 => none.
// Round 4 => 21HN; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "39LN 1LS 141HN", 30U, 0U, 120U, 149U, 1U);
// Round 1 => none; round 2 => none; round 3 => 10MN.
// Round 4 => none; round 5 => none; round 6 => 21HS.
TestPriorityCookieCase(cm.get(), "29LN 1LS 59MN 1MS 91HS", 30U, 50U, 70U,
78U, 72U);
// Low-priority secure cookies are removed before higher priority non-secure
// cookies.
// Round 1 => none; round 2 => 31LS; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "180LS 1MN", 149U, 1U, 0U, 1U, 149U);
// Round 1 => none; round 2 => 31LS; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "180LS 1HN", 149U, 0U, 1U, 1U, 149U);
// Round 1 => none; round 2 => 31LS; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "1MN 180LS", 149U, 1U, 0U, 1U, 149U);
// Round 1 => none; round 2 => 31LS; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "1HN 180LS", 149U, 0U, 1U, 1U, 149U);
// Higher-priority non-secure cookies are removed before any secure cookie
// with greater than low-priority. Is it true? How about the quota?
// Round 1 => none; round 2 => none; round 3 => none.
// Round 4 => none; round 5 => 31MS; round 6 => none.
TestPriorityCookieCase(cm.get(), "180MS 1HN", 0U, 149U, 1U, 1U, 149U);
// Round 1 => none; round 2 => none; round 3 => none.
// Round 4 => none; round 5 => 31MS; round 6 => none.
TestPriorityCookieCase(cm.get(), "1HN 180MS", 0U, 149U, 1U, 1U, 149U);
// Pairwise:
// Round 1 => 31LN; round 2 => none; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "1LS 180LN", 150U, 0U, 0U, 149U, 1U);
// Round 1 => 31LN; round 2 => none; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "100LS 81LN", 150U, 0U, 0U, 50U, 100U);
// Round 1 => 31LN; round 2 => none; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "150LS 31LN", 150U, 0U, 0U, 0U, 150U);
// Round 1 => none; round 2 => none; round 3 => none.
// Round 4 => 31HN; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "1LS 180HN", 1U, 0U, 149U, 149U, 1U);
// Round 1 => none; round 2 => 31LS; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "100LS 81HN", 69U, 0U, 81U, 81U, 69U);
// Round 1 => none; round 2 => 31LS; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "150LS 31HN", 119U, 0U, 31U, 31U, 119U);
// Quota calculations inside non-secure/secure blocks remain in place:
// Round 1 => none; round 2 => 20LS; round 3 => none.
// Round 4 => 11HN; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "50HN 50LS 81HS", 30U, 0U, 120U, 39U,
111U);
// Round 1 => none; round 2 => none; round 3 => 31MN.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "11MS 10HN 10LS 90MN 60HN", 10U, 70U, 70U,
129U, 21U);
// Round 1 => 31LN; round 2 => none; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
TestPriorityCookieCase(cm.get(), "40LS 40LN 101HS", 49U, 0U, 101U, 9U,
141U);
// Multiple GC rounds end up with consistent behavior:
// GC is started as soon as there are 181 cookies in the store.
// On each major round it tries to preserve the quota for each priority.
// It is not aware about more cookies going in.
// 1 GC notices there are 181 cookies - 100HS 81LN 0MN
// Round 1 => 31LN; round 2 => none; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
// 2 GC notices there are 181 cookies - 100HS 69LN 12MN
// Round 1 => 31LN; round 2 => none; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => none.
// 3 GC notices there are 181 cookies - 100HS 38LN 43MN
// Round 1 => 8LN; round 2 => none; round 3 => none.
// Round 4 => none; round 5 => none; round 6 => 23HS.
// 4 GC notcies there are 181 cookies - 77HS 30LN 74MN
// Round 1 => none; round 2 => none; round 3 => 24MN.
// Round 4 => none; round 5 => none; round 6 => 7HS.
TestPriorityCookieCase(cm.get(), "100HS 100LN 100MN", 30U, 76U, 70U, 106U,
70U);
}
// Function for creating a CM with a number of cookies in it,
// no store (and hence no ability to affect access time).
std::unique_ptr<CookieMonster> CreateMonsterForGC(int num_cookies) {
auto cm = std::make_unique<CookieMonster>(nullptr, net::NetLog::Get());
base::Time creation_time = base::Time::Now();
for (int i = 0; i < num_cookies; i++) {
std::unique_ptr<CanonicalCookie> cc(
CanonicalCookie::CreateUnsafeCookieForTesting(
"a", "1", base::StringPrintf("h%05d.izzle", i), /*path=*/"/",
creation_time, /*=expiration_time=*/base::Time(),
/*last_access=*/creation_time, /*last_update=*/creation_time,
/*secure=*/true,
/*httponly=*/false, CookieSameSite::NO_RESTRICTION,
COOKIE_PRIORITY_DEFAULT, false /* same_party */));
GURL source_url = cookie_util::SimulatedCookieSource(*cc, "https");
cm->SetCanonicalCookieAsync(std::move(cc), source_url,
CookieOptions::MakeAllInclusive(),
CookieStore::SetCookiesCallback());
}
return cm;
}
bool IsCookieInList(const CanonicalCookie& cookie, const CookieList& list) {
for (const auto& c : list) {
if (c.Name() == cookie.Name() && c.Value() == cookie.Value() &&
c.Domain() == cookie.Domain() && c.Path() == cookie.Path() &&
c.CreationDate() == cookie.CreationDate() &&
c.ExpiryDate() == cookie.ExpiryDate() &&
c.LastAccessDate() == cookie.LastAccessDate() &&
c.LastUpdateDate() == cookie.LastUpdateDate() &&
c.IsSecure() == cookie.IsSecure() &&
c.IsHttpOnly() == cookie.IsHttpOnly() &&
c.Priority() == cookie.Priority()) {
return true;
}
}
return false;
}
RecordingNetLogObserver net_log_;
};
using CookieMonsterTest = CookieMonsterTestBase<CookieMonsterTestTraits>;
struct CookiesInputInfo {
const GURL url;
const std::string name;
const std::string value;
const std::string domain;
const std::string path;
const base::Time expiration_time;
bool secure;
bool http_only;
CookieSameSite same_site;
CookiePriority priority;
bool same_party;
};
} // namespace
// This test suite verifies the task deferral behaviour of the CookieMonster.
// Specifically, for each asynchronous method, verify that:
// 1. invoking it on an uninitialized cookie store causes the store to begin
// chain-loading its backing data or loading data for a specific domain key
// (eTLD+1).
// 2. The initial invocation does not complete until the loading completes.
// 3. Invocations after the loading has completed complete immediately.
class DeferredCookieTaskTest : public CookieMonsterTest {
protected:
DeferredCookieTaskTest() {
persistent_store_ = base::MakeRefCounted<MockPersistentCookieStore>();
persistent_store_->set_store_load_commands(true);
cookie_monster_ = std::make_unique<CookieMonster>(persistent_store_.get(),
net::NetLog::Get());
}
// Defines a cookie to be returned from PersistentCookieStore::Load
void DeclareLoadedCookie(const GURL& url,
const std::string& cookie_line,
const base::Time& creation_time) {
AddCookieToList(url, cookie_line, creation_time, &loaded_cookies_);
}
void ExecuteLoads(CookieStoreCommand::Type type) {
const auto& commands = persistent_store_->commands();
for (size_t i = 0; i < commands.size(); ++i) {
// Only the first load command will produce the cookies.
if (commands[i].type == type) {
persistent_store_->TakeCallbackAt(i).Run(std::move(loaded_cookies_));
}
}
}
std::string CommandSummary(
const MockPersistentCookieStore::CommandList& commands) {