xref: /aosp_15_r20/external/cronet/net/cookies/cookie_deletion_info_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/cookies/cookie_deletion_info.h"
6 
7 #include "base/test/scoped_feature_list.h"
8 #include "net/base/features.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "url/gurl.h"
11 
12 namespace net {
13 
14 using TimeRange = CookieDeletionInfo::TimeRange;
15 
TEST(CookieDeletionInfoTest,TimeRangeValues)16 TEST(CookieDeletionInfoTest, TimeRangeValues) {
17   TimeRange range;
18   EXPECT_EQ(base::Time(), range.start());
19   EXPECT_EQ(base::Time(), range.end());
20 
21   const base::Time kTestStart = base::Time::FromSecondsSinceUnixEpoch(1000);
22   const base::Time kTestEnd = base::Time::FromSecondsSinceUnixEpoch(10000);
23 
24   EXPECT_EQ(kTestStart, TimeRange(kTestStart, base::Time()).start());
25   EXPECT_EQ(base::Time(), TimeRange(kTestStart, base::Time()).end());
26 
27   EXPECT_EQ(kTestStart, TimeRange(kTestStart, kTestEnd).start());
28   EXPECT_EQ(kTestEnd, TimeRange(kTestStart, kTestEnd).end());
29 
30   TimeRange range2;
31   range2.SetStart(kTestStart);
32   EXPECT_EQ(kTestStart, range2.start());
33   EXPECT_EQ(base::Time(), range2.end());
34   range2.SetEnd(kTestEnd);
35   EXPECT_EQ(kTestStart, range2.start());
36   EXPECT_EQ(kTestEnd, range2.end());
37 }
38 
TEST(CookieDeletionInfoTest,TimeRangeContains)39 TEST(CookieDeletionInfoTest, TimeRangeContains) {
40   // Default TimeRange matches all time values.
41   TimeRange range;
42   EXPECT_TRUE(range.Contains(base::Time::Now()));
43   EXPECT_TRUE(range.Contains(base::Time::Max()));
44 
45   // With a start, but no end.
46   const double kTestMinEpoch = 1000;
47   range.SetStart(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch));
48   EXPECT_FALSE(range.Contains(base::Time::Min()));
49   EXPECT_FALSE(
50       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch - 1)));
51   EXPECT_TRUE(
52       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch)));
53   EXPECT_TRUE(
54       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch + 1)));
55   EXPECT_TRUE(range.Contains(base::Time::Max()));
56 
57   // With an end, but no start.
58   const double kTestMaxEpoch = 10000000;
59   range = TimeRange();
60   range.SetEnd(base::Time::FromSecondsSinceUnixEpoch(kTestMaxEpoch));
61   EXPECT_TRUE(range.Contains(base::Time::Min()));
62   EXPECT_TRUE(
63       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMaxEpoch - 1)));
64   EXPECT_FALSE(
65       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMaxEpoch)));
66   EXPECT_FALSE(
67       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMaxEpoch + 1)));
68   EXPECT_FALSE(range.Contains(base::Time::Max()));
69 
70   // With both a start and an end.
71   range.SetStart(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch));
72   EXPECT_FALSE(range.Contains(base::Time::Min()));
73   EXPECT_FALSE(
74       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch - 1)));
75   EXPECT_TRUE(
76       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch)));
77   EXPECT_TRUE(
78       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch + 1)));
79   EXPECT_TRUE(
80       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMaxEpoch - 1)));
81   EXPECT_FALSE(
82       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMaxEpoch)));
83   EXPECT_FALSE(
84       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMaxEpoch + 1)));
85   EXPECT_FALSE(range.Contains(base::Time::Max()));
86 
87   // And where start==end.
88   range = TimeRange(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch),
89                     base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch));
90   EXPECT_FALSE(range.Contains(base::Time::Min()));
91   EXPECT_FALSE(
92       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch - 1)));
93   EXPECT_TRUE(
94       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch)));
95   EXPECT_FALSE(
96       range.Contains(base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch + 1)));
97 }
98 
TEST(CookieDeletionInfoTest,CookieDeletionInfoMatchSessionControl)99 TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchSessionControl) {
100   auto persistent_cookie = CanonicalCookie::CreateUnsafeCookieForTesting(
101       "persistent-cookie", "persistent-value", "persistent-domain",
102       "persistent-path",
103       /*creation=*/base::Time::Now(),
104       /*expiration=*/base::Time::Max(),
105       /*last_access=*/base::Time::Now(),
106       /*last_update=*/base::Time::Now(),
107       /*secure=*/true,
108       /*httponly=*/false, CookieSameSite::NO_RESTRICTION,
109       CookiePriority::COOKIE_PRIORITY_DEFAULT);
110 
111   auto session_cookie = CanonicalCookie::CreateUnsafeCookieForTesting(
112       "session-cookie", "session-value", "session-domain", "session-path",
113       /*creation=*/base::Time::Now(),
114       /*expiration=*/base::Time(),
115       /*last_access=*/base::Time::Now(),
116       /*last_update=*/base::Time::Now(),
117       /*secure=*/true,
118       /*httponly=*/false, CookieSameSite::NO_RESTRICTION,
119       CookiePriority::COOKIE_PRIORITY_DEFAULT);
120 
121   CookieDeletionInfo delete_info;
122   EXPECT_TRUE(delete_info.Matches(
123       *persistent_cookie,
124       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
125                          /*delegate_treats_url_as_trustworthy=*/false}));
126   EXPECT_TRUE(delete_info.Matches(
127       *session_cookie,
128       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
129                          /*delegate_treats_url_as_trustworthy=*/false}));
130 
131   delete_info.session_control =
132       CookieDeletionInfo::SessionControl::PERSISTENT_COOKIES;
133   EXPECT_TRUE(delete_info.Matches(
134       *persistent_cookie,
135       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
136                          /*delegate_treats_url_as_trustworthy=*/false}));
137   EXPECT_FALSE(delete_info.Matches(
138       *session_cookie,
139       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
140                          /*delegate_treats_url_as_trustworthy=*/false}));
141 
142   delete_info.session_control =
143       CookieDeletionInfo::SessionControl::SESSION_COOKIES;
144   EXPECT_FALSE(delete_info.Matches(
145       *persistent_cookie,
146       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
147                          /*delegate_treats_url_as_trustworthy=*/false}));
148   EXPECT_TRUE(delete_info.Matches(
149       *session_cookie,
150       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
151                          /*delegate_treats_url_as_trustworthy=*/false}));
152 }
153 
TEST(CookieDeletionInfoTest,CookieDeletionInfoMatchHost)154 TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchHost) {
155   auto domain_cookie = CanonicalCookie::CreateUnsafeCookieForTesting(
156       "domain-cookie", "domain-cookie-value",
157       /*domain=*/".example.com", "/path",
158       /*creation=*/base::Time::Now(),
159       /*expiration=*/base::Time::Max(),
160       /*last_access=*/base::Time::Now(),
161       /*last_update=*/base::Time::Now(),
162       /*secure=*/true,
163       /*httponly=*/false, CookieSameSite::NO_RESTRICTION,
164       CookiePriority::COOKIE_PRIORITY_DEFAULT);
165 
166   auto host_cookie = CanonicalCookie::CreateUnsafeCookieForTesting(
167       "host-cookie", "host-cookie-value",
168       /*domain=*/"thehost.hosting.com", "/path",
169       /*creation=*/base::Time::Now(),
170       /*expiration=*/base::Time::Max(),
171       /*last_access=*/base::Time::Now(),
172       /*last_update=*/base::Time::Now(),
173       /*secure=*/true,
174       /*httponly=*/false, CookieSameSite::NO_RESTRICTION,
175       CookiePriority::COOKIE_PRIORITY_DEFAULT);
176 
177   EXPECT_TRUE(domain_cookie->IsDomainCookie());
178   EXPECT_TRUE(host_cookie->IsHostCookie());
179 
180   CookieDeletionInfo delete_info;
181   EXPECT_TRUE(delete_info.Matches(
182       *domain_cookie,
183       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
184                          /*delegate_treats_url_as_trustworthy=*/false}));
185   EXPECT_TRUE(delete_info.Matches(
186       *host_cookie,
187       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
188                          /*delegate_treats_url_as_trustworthy=*/false}));
189 
190   delete_info.host = "thehost.hosting.com";
191   EXPECT_FALSE(delete_info.Matches(
192       *domain_cookie,
193       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
194                          /*delegate_treats_url_as_trustworthy=*/false}));
195   EXPECT_TRUE(delete_info.Matches(
196       *host_cookie,
197       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
198                          /*delegate_treats_url_as_trustworthy=*/false}));
199 
200   delete_info.host = "otherhost.hosting.com";
201   EXPECT_FALSE(delete_info.Matches(
202       *domain_cookie,
203       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
204                          /*delegate_treats_url_as_trustworthy=*/false}));
205   EXPECT_FALSE(delete_info.Matches(
206       *host_cookie,
207       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
208                          /*delegate_treats_url_as_trustworthy=*/false}));
209 
210   delete_info.host = "thehost.otherhosting.com";
211   EXPECT_FALSE(delete_info.Matches(
212       *domain_cookie,
213       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
214                          /*delegate_treats_url_as_trustworthy=*/false}));
215   EXPECT_FALSE(delete_info.Matches(
216       *host_cookie,
217       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
218                          /*delegate_treats_url_as_trustworthy=*/false}));
219 }
220 
TEST(CookieDeletionInfoTest,CookieDeletionInfoMatchName)221 TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchName) {
222   auto cookie1 = CanonicalCookie::CreateUnsafeCookieForTesting(
223       "cookie1-name", "cookie1-value",
224       /*domain=*/".example.com", "/path",
225       /*creation=*/base::Time::Now(),
226       /*expiration=*/base::Time::Max(),
227       /*last_access=*/base::Time::Now(),
228       /*last_update=*/base::Time::Now(),
229       /*secure=*/true,
230       /*httponly=*/false, CookieSameSite::NO_RESTRICTION,
231       CookiePriority::COOKIE_PRIORITY_DEFAULT);
232   auto cookie2 = CanonicalCookie::CreateUnsafeCookieForTesting(
233       "cookie2-name", "cookie2-value",
234       /*domain=*/".example.com", "/path",
235       /*creation=*/base::Time::Now(),
236       /*expiration=*/base::Time::Max(),
237       /*last_access=*/base::Time::Now(),
238       /*last_update=*/base::Time::Now(),
239       /*secure=*/true,
240       /*httponly=*/false, CookieSameSite::NO_RESTRICTION,
241       CookiePriority::COOKIE_PRIORITY_DEFAULT);
242 
243   CookieDeletionInfo delete_info;
244   delete_info.name = "cookie1-name";
245   EXPECT_TRUE(delete_info.Matches(
246       *cookie1,
247       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
248                          /*delegate_treats_url_as_trustworthy=*/false}));
249   EXPECT_FALSE(delete_info.Matches(
250       *cookie2,
251       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
252                          /*delegate_treats_url_as_trustworthy=*/false}));
253 }
254 
TEST(CookieDeletionInfoTest,CookieDeletionInfoMatchValue)255 TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchValue) {
256   auto cookie1 = CanonicalCookie::CreateUnsafeCookieForTesting(
257       "cookie1-name", "cookie1-value",
258       /*domain=*/".example.com", "/path",
259       /*creation=*/base::Time::Now(),
260       /*expiration=*/base::Time::Max(),
261       /*last_access=*/base::Time::Now(),
262       /*last_update=*/base::Time::Now(),
263       /*secure=*/true,
264       /*httponly=*/false, CookieSameSite::NO_RESTRICTION,
265       CookiePriority::COOKIE_PRIORITY_DEFAULT);
266   auto cookie2 = CanonicalCookie::CreateUnsafeCookieForTesting(
267       "cookie2-name", "cookie2-value",
268       /*domain=*/".example.com", "/path",
269       /*creation=*/base::Time::Now(),
270       /*expiration=*/base::Time::Max(),
271       /*last_access=*/base::Time::Now(),
272       /*last_update=*/base::Time::Now(),
273       /*secure=*/true,
274       /*httponly=*/false, CookieSameSite::NO_RESTRICTION,
275       CookiePriority::COOKIE_PRIORITY_DEFAULT);
276 
277   CookieDeletionInfo delete_info;
278   delete_info.value_for_testing = "cookie2-value";
279   EXPECT_FALSE(delete_info.Matches(
280       *cookie1,
281       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
282                          /*delegate_treats_url_as_trustworthy=*/false}));
283   EXPECT_TRUE(delete_info.Matches(
284       *cookie2,
285       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
286                          /*delegate_treats_url_as_trustworthy=*/false}));
287 }
288 
TEST(CookieDeletionInfoTest,CookieDeletionInfoMatchUrl)289 TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchUrl) {
290   auto cookie = CanonicalCookie::CreateUnsafeCookieForTesting(
291       "cookie-name", "cookie-value",
292       /*domain=*/"www.example.com", "/path",
293       /*creation=*/base::Time::Now(),
294       /*expiration=*/base::Time::Max(),
295       /*last_access=*/base::Time::Now(),
296       /*last_update=*/base::Time::Now(),
297       /*secure=*/true,
298       /*httponly=*/false, CookieSameSite::NO_RESTRICTION,
299       CookiePriority::COOKIE_PRIORITY_DEFAULT);
300 
301   CookieDeletionInfo delete_info;
302   delete_info.url = GURL("https://www.example.com/path");
303   EXPECT_TRUE(delete_info.Matches(
304       *cookie,
305       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
306                          /*delegate_treats_url_as_trustworthy=*/false}));
307 
308   delete_info.url = GURL("https://www.example.com/another/path");
309   EXPECT_FALSE(delete_info.Matches(
310       *cookie,
311       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
312                          /*delegate_treats_url_as_trustworthy=*/false}));
313 
314   delete_info.url = GURL("http://www.example.com/path");
315   // Secure cookie on http:// URL -> no match.
316   EXPECT_FALSE(delete_info.Matches(
317       *cookie,
318       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
319                          /*delegate_treats_url_as_trustworthy=*/false}));
320 
321   // Secure cookie on http:// URL, but delegate says treat is as trustworhy ->
322   // match.
323   EXPECT_TRUE(delete_info.Matches(
324       *cookie,
325       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
326                          /*delegate_treats_url_as_trustworthy=*/true}));
327 }
328 
TEST(CookieDeletionInfoTest,CookieDeletionInfoDomainMatchesDomain)329 TEST(CookieDeletionInfoTest, CookieDeletionInfoDomainMatchesDomain) {
330   CookieDeletionInfo delete_info;
331 
332   const double kTestMinEpoch = 1000;
333   const double kTestMaxEpoch = 10000000;
334   delete_info.creation_range.SetStart(
335       base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch));
336   delete_info.creation_range.SetEnd(
337       base::Time::FromSecondsSinceUnixEpoch(kTestMaxEpoch));
338 
339   auto create_cookie = [kTestMinEpoch](std::string cookie_domain) {
340     return *CanonicalCookie::CreateUnsafeCookieForTesting(
341         /*name=*/"test-cookie",
342         /*value=*/"cookie-value", cookie_domain,
343         /*path=*/"cookie/path",
344         /*creation=*/base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch + 1),
345         /*expiration=*/base::Time::Max(),
346         /*last_access=*/
347         base::Time::FromSecondsSinceUnixEpoch(kTestMinEpoch + 1),
348         /*last_update=*/base::Time::Now(),
349         /*secure=*/true,
350         /*httponly=*/false,
351         /*same_site=*/CookieSameSite::NO_RESTRICTION,
352         /*priority=*/CookiePriority::COOKIE_PRIORITY_DEFAULT);
353   };
354 
355   // by default empty domain list and default match action will match.
356   EXPECT_TRUE(delete_info.Matches(
357       create_cookie("example.com"),
358       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
359                          /*delegate_treats_url_as_trustworthy=*/false}));
360 
361   const char kExtensionHostname[] = "mgndgikekgjfcpckkfioiadnlibdjbkf";
362 
363   // Only using the inclusion list because this is only testing
364   // DomainMatchesDomainSet and not CookieDeletionInfo::Matches.
365   delete_info.domains_and_ips_to_delete =
366       std::set<std::string>({"example.com", "another.com", "192.168.0.1"});
367   EXPECT_TRUE(delete_info.Matches(
368       create_cookie(".example.com"),
369       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
370                          /*delegate_treats_url_as_trustworthy=*/false}));
371   EXPECT_TRUE(delete_info.Matches(
372       create_cookie("example.com"),
373       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
374                          /*delegate_treats_url_as_trustworthy=*/false}));
375   EXPECT_TRUE(delete_info.Matches(
376       create_cookie(".another.com"),
377       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
378                          /*delegate_treats_url_as_trustworthy=*/false}));
379   EXPECT_TRUE(delete_info.Matches(
380       create_cookie("192.168.0.1"),
381       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
382                          /*delegate_treats_url_as_trustworthy=*/false}));
383   EXPECT_FALSE(delete_info.Matches(
384       create_cookie(".nomatch.com"),
385       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
386                          /*delegate_treats_url_as_trustworthy=*/false}));
387   EXPECT_FALSE(delete_info.Matches(
388       create_cookie("192.168.0.2"),
389       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
390                          /*delegate_treats_url_as_trustworthy=*/false}));
391   EXPECT_FALSE(delete_info.Matches(
392       create_cookie(kExtensionHostname),
393       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
394                          /*delegate_treats_url_as_trustworthy=*/false}));
395 }
396 
TEST(CookieDeletionInfoTest,CookieDeletionInfoMatchesDomainList)397 TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchesDomainList) {
398   CookieDeletionInfo delete_info;
399 
400   auto create_cookie = [](std::string cookie_domain) {
401     return *CanonicalCookie::CreateUnsafeCookieForTesting(
402         /*name=*/"test-cookie",
403         /*value=*/"cookie-value", cookie_domain,
404         /*path=*/"cookie/path",
405         /*creation=*/base::Time::Now(),
406         /*expiration=*/base::Time::Max(),
407         /*last_access=*/base::Time::Now(),
408         /*last_update=*/base::Time::Now(),
409         /*secure=*/false,
410         /*httponly=*/false,
411         /*same_site=*/CookieSameSite::NO_RESTRICTION,
412         /*priority=*/CookiePriority::COOKIE_PRIORITY_DEFAULT);
413   };
414 
415   // With two empty lists (default) should match any domain.
416   EXPECT_TRUE(delete_info.Matches(
417       create_cookie("anything.com"),
418       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
419                          /*delegate_treats_url_as_trustworthy=*/false}));
420 
421   // With only an "to_delete" list.
422   delete_info.domains_and_ips_to_delete = {"includea.com", "includeb.com"};
423   EXPECT_TRUE(delete_info.Matches(
424       create_cookie("includea.com"),
425       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
426                          /*delegate_treats_url_as_trustworthy=*/false}));
427   EXPECT_TRUE(delete_info.Matches(
428       create_cookie("includeb.com"),
429       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
430                          /*delegate_treats_url_as_trustworthy=*/false}));
431   EXPECT_FALSE(delete_info.Matches(
432       create_cookie("anything.com"),
433       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
434                          /*delegate_treats_url_as_trustworthy=*/false}));
435 
436   // With only an "to_ignore" list.
437   delete_info.domains_and_ips_to_delete.reset();
438   delete_info.domains_and_ips_to_ignore = {"exclude.com"};
439   EXPECT_TRUE(delete_info.Matches(
440       create_cookie("anything.com"),
441       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
442                          /*delegate_treats_url_as_trustworthy=*/false}));
443   EXPECT_FALSE(delete_info.Matches(
444       create_cookie("exclude.com"),
445       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
446                          /*delegate_treats_url_as_trustworthy=*/false}));
447 
448   // Now with both lists populated.
449   //
450   // +----------------------+
451   // | to_delete            |  outside.com
452   // |                      |
453   // |  left.com  +---------------------+
454   // |            | mid.com | to_ignore |
455   // |            |         |           |
456   // +------------|---------+           |
457   //              |           right.com |
458   //              |                     |
459   //              +---------------------+
460   delete_info.domains_and_ips_to_delete = {"left.com", "mid.com"};
461   delete_info.domains_and_ips_to_ignore = {"mid.com", "right.com"};
462 
463   EXPECT_TRUE(delete_info.Matches(
464       create_cookie("left.com"),
465       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
466                          /*delegate_treats_url_as_trustworthy=*/false}));
467   EXPECT_FALSE(delete_info.Matches(
468       create_cookie("mid.com"),
469       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
470                          /*delegate_treats_url_as_trustworthy=*/false}));
471   EXPECT_FALSE(delete_info.Matches(
472       create_cookie("right.com"),
473       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
474                          /*delegate_treats_url_as_trustworthy=*/false}));
475   EXPECT_FALSE(delete_info.Matches(
476       create_cookie("outside.com"),
477       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
478                          /*delegate_treats_url_as_trustworthy=*/false}));
479 
480   // An empty list of deleted domains shouldn't delete anything.
481   delete_info.domains_and_ips_to_delete = std::set<std::string>();
482   delete_info.domains_and_ips_to_ignore.reset();
483   EXPECT_FALSE(delete_info.Matches(
484       create_cookie("outside.com"),
485       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
486                          /*delegate_treats_url_as_trustworthy=*/false}));
487 
488   // An empty list of ignored domains should delete everything.
489   delete_info.domains_and_ips_to_delete.reset();
490   delete_info.domains_and_ips_to_ignore = std::set<std::string>();
491   EXPECT_TRUE(delete_info.Matches(
492       create_cookie("inside.com"),
493       CookieAccessParams{net::CookieAccessSemantics::UNKNOWN,
494                          /*delegate_treats_url_as_trustworthy=*/false}));
495 }
496 
497 // Test that Matches() works regardless of the cookie access semantics (because
498 // the IncludeForRequestURL call uses CookieOptions::MakeAllInclusive).
TEST(CookieDeletionInfoTest,MatchesWithCookieAccessSemantics)499 TEST(CookieDeletionInfoTest, MatchesWithCookieAccessSemantics) {
500   // Cookie with unspecified SameSite.
501   auto cookie = CanonicalCookie::CreateForTesting(
502       GURL("https://www.example.com"), "cookie=1", base::Time::Now(),
503       /*server_time=*/std::nullopt,
504       /*cookie_partition_key=*/std::nullopt);
505 
506   CookieDeletionInfo delete_info;
507   delete_info.url = GURL("https://www.example.com/path");
508   EXPECT_TRUE(delete_info.Matches(
509       *cookie,
510       CookieAccessParams{CookieAccessSemantics::UNKNOWN,
511                          /*delegate_treats_url_as_trustworthy=*/false}));
512   EXPECT_TRUE(delete_info.Matches(
513       *cookie,
514       CookieAccessParams{CookieAccessSemantics::LEGACY,
515                          /*delegate_treats_url_as_trustworthy=*/false}));
516   EXPECT_TRUE(delete_info.Matches(
517       *cookie,
518       CookieAccessParams{CookieAccessSemantics::NONLEGACY,
519                          /*delegate_treats_url_as_trustworthy=*/false}));
520 }
521 
TEST(CookieDeletionInfoTest,MatchesCookiePartitionKeyCollection)522 TEST(CookieDeletionInfoTest, MatchesCookiePartitionKeyCollection) {
523   const CookiePartitionKey kPartitionKey =
524       CookiePartitionKey::FromURLForTesting(GURL("https://www.foo.com"));
525   const CookiePartitionKey kOtherPartitionKey =
526       CookiePartitionKey::FromURLForTesting(GURL("https://www.bar.com"));
527   const CookiePartitionKeyCollection kEmptyKeychain;
528   const CookiePartitionKeyCollection kSingletonKeychain(kPartitionKey);
529   const CookiePartitionKeyCollection kMultipleKeysKeychain(
530       {kPartitionKey, kOtherPartitionKey});
531   const CookiePartitionKeyCollection kAllKeysKeychain =
532       CookiePartitionKeyCollection::ContainsAll();
533   const std::optional<CookiePartitionKey> kPartitionKeyOpt =
534       std::make_optional(kPartitionKey);
535   const CookiePartitionKeyCollection kOtherKeySingletonKeychain(
536       kOtherPartitionKey);
537 
538   struct TestCase {
539     const std::string desc;
540     const CookiePartitionKeyCollection filter_cookie_partition_key_collection;
541     const std::optional<CookiePartitionKey> cookie_partition_key;
542     bool expects_match;
543   } test_cases[] = {
544       // Unpartitioned cookie always matches
545       {"Unpartitioned empty keychain", kEmptyKeychain, std::nullopt, true},
546       {"Unpartitioned singleton keychain", kSingletonKeychain, std::nullopt,
547        true},
548       {"Unpartitioned multiple keys", kMultipleKeysKeychain, std::nullopt,
549        true},
550       {"Unpartitioned all keys", kAllKeysKeychain, std::nullopt, true},
551       // Partitioned cookie only matches keychains which contain its partition
552       // key.
553       {"Partitioned empty keychain", kEmptyKeychain, kPartitionKeyOpt, false},
554       {"Partitioned singleton keychain", kSingletonKeychain, kPartitionKeyOpt,
555        true},
556       {"Partitioned multiple keys", kMultipleKeysKeychain, kPartitionKeyOpt,
557        true},
558       {"Partitioned all keys", kAllKeysKeychain, kPartitionKeyOpt, true},
559       {"Partitioned mismatched keys", kOtherKeySingletonKeychain,
560        kPartitionKeyOpt, false},
561   };
562 
563   for (const auto& test_case : test_cases) {
564     SCOPED_TRACE(test_case.desc);
565     auto cookie = CanonicalCookie::CreateForTesting(
566         GURL("https://www.example.com"),
567         "__Host-foo=bar; Secure; Path=/; Partitioned", base::Time::Now(),
568         /*server_time=*/std::nullopt, test_case.cookie_partition_key);
569     CookieDeletionInfo delete_info;
570     delete_info.cookie_partition_key_collection =
571         test_case.filter_cookie_partition_key_collection;
572     EXPECT_EQ(test_case.expects_match,
573               delete_info.Matches(
574                   *cookie, CookieAccessParams{
575                                net::CookieAccessSemantics::UNKNOWN,
576                                /*delegate_treats_url_as_trustworthy=*/false}));
577   }
578 }
579 
TEST(CookieDeletionInfoTest,MatchesExcludeUnpartitionedCookies)580 TEST(CookieDeletionInfoTest, MatchesExcludeUnpartitionedCookies) {
581   struct TestCase {
582     const std::string desc;
583     const std::optional<CookiePartitionKey> cookie_partition_key;
584     bool partitioned_state_only;
585     bool expects_match;
586   } test_cases[] = {
587       {"Unpartitioned cookie not excluded", std::nullopt, false, true},
588       {"Unpartitioned cookie excluded", std::nullopt, true, false},
589       {"Partitioned cookie when unpartitioned not excluded",
590        CookiePartitionKey::FromURLForTesting(GURL("https://foo.com")), false,
591        true},
592       {"Partitioned cookie when unpartitioned excluded",
593        CookiePartitionKey::FromURLForTesting(GURL("https://foo.com")), true,
594        true},
595       {"Nonced partitioned cookie when unpartitioned not excluded",
596        CookiePartitionKey::FromURLForTesting(
597            GURL("https://foo.com"),
598            CookiePartitionKey::AncestorChainBit::kCrossSite,
599            base::UnguessableToken::Create()),
600        false, true},
601       {"Nonced partitioned cookie when unpartitioned excluded",
602        CookiePartitionKey::FromURLForTesting(
603            GURL("https://foo.com"),
604            CookiePartitionKey::AncestorChainBit::kCrossSite,
605            base::UnguessableToken::Create()),
606        true, true},
607   };
608 
609   for (const auto& test_case : test_cases) {
610     SCOPED_TRACE(test_case.desc);
611     auto cookie = CanonicalCookie::CreateForTesting(
612         GURL("https://www.example.com"),
613         "__Host-foo=bar; Secure; Path=/; Partitioned", base::Time::Now(),
614         /*server_time=*/std::nullopt, test_case.cookie_partition_key);
615     CookieDeletionInfo delete_info;
616     delete_info.partitioned_state_only = test_case.partitioned_state_only;
617     EXPECT_EQ(test_case.expects_match,
618               delete_info.Matches(
619                   *cookie, CookieAccessParams{
620                                net::CookieAccessSemantics::UNKNOWN,
621                                /*delegate_treats_url_as_trustworthy=*/false}));
622   }
623 }
624 
625 }  // namespace net
626