summaryrefslogtreecommitdiff
path: root/chromium/net/cookies/site_for_cookies_unittest.cc
blob: 47d21225aea6ef323aa3a4503848c23bd840f992 (plain)
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
// Copyright 2019 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/site_for_cookies.h"

#include <string>
#include <vector>

#include "base/strings/strcat.h"
#include "base/test/scoped_feature_list.h"
#include "net/base/features.h"
#include "net/base/url_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_util.h"

namespace net {
namespace {

class SchemelessSiteForCookiesTest : public ::testing::Test {
 public:
  SchemelessSiteForCookiesTest() {
    scope_feature_list_.InitAndDisableFeature(features::kSchemefulSameSite);
  }

 protected:
  base::test::ScopedFeatureList scope_feature_list_;
};

std::string NormalizedScheme(const GURL& url) {
  return url.SchemeIsWSOrWSS() ? ChangeWebSocketSchemeToHttpScheme(url).scheme()
                               : url.scheme();
}

// Tests that all URLs from |equivalent| produce SiteForCookies that match
// URLs in the set and are equivalent to each other, and are distinct and
// don't match |distinct|.
void TestEquivalentAndDistinct(const std::vector<GURL>& equivalent,
                               const std::vector<GURL>& distinct,
                               const std::string& expect_host) {
  for (const GURL& equiv_url_a : equivalent) {
    SiteForCookies equiv_a = SiteForCookies::FromUrl(equiv_url_a);
    EXPECT_EQ(NormalizedScheme(equiv_url_a), equiv_a.scheme());

    EXPECT_EQ(equiv_a.RepresentativeUrl().spec(),
              base::StrCat({equiv_a.scheme(), "://", expect_host, "/"}));

    for (const GURL& equiv_url_b : equivalent) {
      SiteForCookies equiv_b = SiteForCookies::FromUrl(equiv_url_a);

      EXPECT_TRUE(equiv_a.IsEquivalent(equiv_b));
      EXPECT_TRUE(equiv_b.IsEquivalent(equiv_a));
      EXPECT_TRUE(equiv_a.IsFirstParty(equiv_url_a));
      EXPECT_TRUE(equiv_a.IsFirstParty(equiv_url_b));
      EXPECT_TRUE(equiv_b.IsFirstParty(equiv_url_a));
      EXPECT_TRUE(equiv_b.IsFirstParty(equiv_url_b));
    }

    for (const GURL& other_url : distinct) {
      SiteForCookies other = SiteForCookies::FromUrl(other_url);
      EXPECT_EQ(NormalizedScheme(other_url), other.scheme());
      EXPECT_EQ(other.RepresentativeUrl().spec(),
                base::StrCat({other.scheme(), "://", other_url.host(), "/"}));

      EXPECT_FALSE(equiv_a.IsEquivalent(other));
      EXPECT_FALSE(other.IsEquivalent(equiv_a));
      EXPECT_FALSE(equiv_a.IsFirstParty(other_url))
          << equiv_a.ToDebugString() << " " << other_url.spec();
      EXPECT_FALSE(other.IsFirstParty(equiv_url_a));

      EXPECT_TRUE(other.IsFirstParty(other_url));
    }
  }
}

TEST(SiteForCookiesTest, Default) {
  SiteForCookies should_match_none;
  EXPECT_FALSE(should_match_none.IsFirstParty(GURL("http://example.com")));
  EXPECT_FALSE(should_match_none.IsFirstParty(GURL("file:///home/me/.bashrc")));
  EXPECT_FALSE(should_match_none.IsFirstParty(GURL::EmptyGURL()));

  // Before SiteForCookies existed, empty URL would represent match-none
  EXPECT_TRUE(should_match_none.IsEquivalent(
      SiteForCookies::FromUrl(GURL::EmptyGURL())));
  EXPECT_TRUE(should_match_none.RepresentativeUrl().is_empty());
  EXPECT_TRUE(should_match_none.IsEquivalent(
      SiteForCookies::FromOrigin(url::Origin())));

  EXPECT_TRUE(should_match_none.site().opaque());
  EXPECT_EQ("", should_match_none.scheme());
  EXPECT_EQ("SiteForCookies: {site=null; schemefully_same=false}",
            should_match_none.ToDebugString());
}

TEST_F(SchemelessSiteForCookiesTest, Basic) {
  std::vector<GURL> equivalent = {
      GURL("https://example.com"),
      GURL("http://sub1.example.com:42/something"),
      GURL("ws://sub2.example.com/something"),
      // This one is disputable.
      GURL("file://example.com/helo"),
  };

  std::vector<GURL> distinct = {GURL("https://example.org"),
                                GURL("http://com/i_am_a_tld")};

  TestEquivalentAndDistinct(equivalent, distinct, "example.com");
}

// Similar to SchemelessSiteForCookiesTest_Basic with a focus on testing secure
// SFCs.
TEST(SiteForCookiesTest, BasicSecure) {
  std::vector<GURL> equivalent = {GURL("https://example.com"),
                                  GURL("wss://example.com"),
                                  GURL("https://sub1.example.com:42/something"),
                                  GURL("wss://sub2.example.com/something")};

  std::vector<GURL> distinct = {
      GURL("http://example.com"),      GURL("https://example.org"),
      GURL("ws://example.com"),        GURL("https://com/i_am_a_tld"),
      GURL("file://example.com/helo"),
  };

  TestEquivalentAndDistinct(equivalent, distinct, "example.com");
}

// Similar to SchemelessSiteForCookiesTest_Basic with a focus on testing
// insecure SFCs.
TEST(SiteForCookiesTest, BasicInsecure) {
  std::vector<GURL> equivalent = {GURL("http://example.com"),
                                  GURL("ws://example.com"),
                                  GURL("http://sub1.example.com:42/something"),
                                  GURL("ws://sub2.example.com/something")};

  std::vector<GURL> distinct = {
      GURL("https://example.com"),     GURL("http://example.org"),
      GURL("wss://example.com"),       GURL("http://com/i_am_a_tld"),
      GURL("file://example.com/helo"),
  };

  TestEquivalentAndDistinct(equivalent, distinct, "example.com");
}

TEST(SiteForCookiesTest, File) {
  std::vector<GURL> equivalent = {GURL("file:///a/b/c"),
                                  GURL("file:///etc/shaaadow")};

  std::vector<GURL> distinct = {GURL("file://nonlocal/file.txt")};

  TestEquivalentAndDistinct(equivalent, distinct, "");
}

TEST_F(SchemelessSiteForCookiesTest, Extension) {
  url::ScopedSchemeRegistryForTests scoped_registry;
  url::AddStandardScheme("chrome-extension", url::SCHEME_WITH_HOST);
  std::vector<GURL> equivalent = {GURL("chrome-extension://abc/"),
                                  GURL("chrome-extension://abc/foo.txt"),
                                  GURL("https://abc"), GURL("http://abc"),
                                  // This one is disputable.
                                  GURL("file://abc/bar.txt")};

  std::vector<GURL> distinct = {GURL("chrome-extension://def")};

  TestEquivalentAndDistinct(equivalent, distinct, "abc");
}

// Similar to SchemelessSiteForCookiesTest_Extension with a focus on ensuring
// that http(s) schemes are distinct.
TEST(SiteForCookiesTest, Extension) {
  url::ScopedSchemeRegistryForTests scoped_registry;
  url::AddStandardScheme("chrome-extension", url::SCHEME_WITH_HOST);
  std::vector<GURL> equivalent = {
      GURL("chrome-extension://abc/"),
      GURL("chrome-extension://abc/foo.txt"),
  };

  std::vector<GURL> distinct = {GURL("chrome-extension://def"),
                                GURL("https://abc"), GURL("http://abc"),
                                GURL("file://abc/bar.txt")};

  TestEquivalentAndDistinct(equivalent, distinct, "abc");
}

TEST(SiteForCookiesTest, NonStandard) {
  // If we don't register the scheme, nothing matches, even identical ones
  std::vector<GURL> equivalent;
  std::vector<GURL> distinct = {GURL("non-standard://abc"),
                                GURL("non-standard://abc"),
                                GURL("non-standard://def")};

  // Last parameter is "" since GURL doesn't put the hostname in if
  // the URL is non-standard.
  TestEquivalentAndDistinct(equivalent, distinct, "");
}

TEST_F(SchemelessSiteForCookiesTest, Blob) {
  // This case isn't really well-specified and is inconsistent between
  // different user agents; the behavior chosen here was to be more
  // consistent between url and origin handling.
  //
  // Thanks file API spec for the sample blob URL.
  SiteForCookies from_blob = SiteForCookies::FromUrl(
      GURL("blob:https://example.org/9115d58c-bcda-ff47-86e5-083e9a2153041"));

  EXPECT_TRUE(from_blob.IsFirstParty(GURL("http://sub.example.org/resource")));
  EXPECT_EQ("https", from_blob.scheme());
  EXPECT_EQ("SiteForCookies: {site=https://example.org; schemefully_same=true}",
            from_blob.ToDebugString());
  EXPECT_EQ("https://example.org/", from_blob.RepresentativeUrl().spec());
  EXPECT_TRUE(from_blob.IsEquivalent(
      SiteForCookies::FromUrl(GURL("http://www.example.org:631"))));
}

// Similar to SchemelessSiteForCookiesTest_Blob with a focus on a secure blob.
TEST(SiteForCookiesTest, SecureBlob) {
  SiteForCookies from_blob = SiteForCookies::FromUrl(
      GURL("blob:https://example.org/9115d58c-bcda-ff47-86e5-083e9a2153041"));

  EXPECT_TRUE(from_blob.IsFirstParty(GURL("https://sub.example.org/resource")));
  EXPECT_FALSE(from_blob.IsFirstParty(GURL("http://sub.example.org/resource")));
  EXPECT_EQ("https", from_blob.scheme());
  EXPECT_EQ("SiteForCookies: {site=https://example.org; schemefully_same=true}",
            from_blob.ToDebugString());
  EXPECT_EQ("https://example.org/", from_blob.RepresentativeUrl().spec());
  EXPECT_TRUE(from_blob.IsEquivalent(
      SiteForCookies::FromUrl(GURL("https://www.example.org:631"))));
  EXPECT_FALSE(from_blob.IsEquivalent(
      SiteForCookies::FromUrl(GURL("http://www.example.org:631"))));
}

// Similar to SchemelessSiteForCookiesTest_Blob with a focus on an insecure
// blob.
TEST(SiteForCookiesTest, InsecureBlob) {
  SiteForCookies from_blob = SiteForCookies::FromUrl(
      GURL("blob:http://example.org/9115d58c-bcda-ff47-86e5-083e9a2153041"));

  EXPECT_TRUE(from_blob.IsFirstParty(GURL("http://sub.example.org/resource")));
  EXPECT_FALSE(
      from_blob.IsFirstParty(GURL("https://sub.example.org/resource")));
  EXPECT_EQ("http", from_blob.scheme());
  EXPECT_EQ("SiteForCookies: {site=http://example.org; schemefully_same=true}",
            from_blob.ToDebugString());
  EXPECT_EQ("http://example.org/", from_blob.RepresentativeUrl().spec());
  EXPECT_TRUE(from_blob.IsEquivalent(
      SiteForCookies::FromUrl(GURL("http://www.example.org:631"))));
  EXPECT_FALSE(from_blob.IsEquivalent(
      SiteForCookies::FromUrl(GURL("https://www.example.org:631"))));
}

TEST_F(SchemelessSiteForCookiesTest, Wire) {
  SiteForCookies out;

  // Empty one.
  EXPECT_TRUE(SiteForCookies::FromWire(SchemefulSite(), false, &out));
  EXPECT_TRUE(out.IsNull());

  EXPECT_TRUE(SiteForCookies::FromWire(SchemefulSite(), true, &out));
  EXPECT_TRUE(out.IsNull());

  // Not a valid site. (Scheme should have been converted to https.)
  EXPECT_FALSE(SiteForCookies::FromWire(
      SchemefulSite(GURL("wss://host.example.test")), false, &out));
  EXPECT_TRUE(out.IsNull());

  // Not a valid scheme. (Same result as opaque SchemefulSite.)
  EXPECT_TRUE(SiteForCookies::FromWire(SchemefulSite(GURL("aH://example.test")),
                                       false, &out));
  EXPECT_TRUE(out.IsNull());

  // Not a eTLD + 1 (or something hosty), but this is fine. (Is converted to a
  // registrable domain by SchemefulSite constructor.)
  EXPECT_TRUE(SiteForCookies::FromWire(
      SchemefulSite(GURL("http://sub.example.test")), false, &out));
  EXPECT_FALSE(out.IsNull());
  EXPECT_EQ(
      "SiteForCookies: {site=http://example.test; schemefully_same=false}",
      out.ToDebugString());

  // IP address is fine.
  EXPECT_TRUE(SiteForCookies::FromWire(SchemefulSite(GURL("https://127.0.0.1")),
                                       true, &out));
  EXPECT_FALSE(out.IsNull());
  EXPECT_EQ("SiteForCookies: {site=https://127.0.0.1; schemefully_same=true}",
            out.ToDebugString());

  EXPECT_TRUE(SiteForCookies::FromWire(SchemefulSite(GURL("https://127.0.0.1")),
                                       false, &out));
  EXPECT_FALSE(out.IsNull());
  EXPECT_EQ("SiteForCookies: {site=https://127.0.0.1; schemefully_same=false}",
            out.ToDebugString());

  // An actual eTLD+1 is fine.
  EXPECT_TRUE(SiteForCookies::FromWire(
      SchemefulSite(GURL("http://example.test")), true, &out));
  EXPECT_FALSE(out.IsNull());
  EXPECT_EQ("SiteForCookies: {site=http://example.test; schemefully_same=true}",
            out.ToDebugString());
}

// Similar to SchemelessSiteForCookiesTest_Wire except that schemefully_same has
// an effect (makes IsNull() return true if schemefully_same is false).
TEST(SiteForCookiesTest, Wire) {
  SiteForCookies out;

  // Empty one.
  EXPECT_TRUE(SiteForCookies::FromWire(SchemefulSite(), false, &out));
  EXPECT_TRUE(out.IsNull());

  EXPECT_TRUE(SiteForCookies::FromWire(SchemefulSite(), true, &out));
  EXPECT_TRUE(out.IsNull());

  // Not a valid site. (Scheme should have been converted to https.)
  EXPECT_FALSE(SiteForCookies::FromWire(
      SchemefulSite(GURL("wss://host.example.test")), false, &out));
  EXPECT_TRUE(out.IsNull());

  // Not a valid scheme. (Same result as opaque SchemefulSite.)
  EXPECT_TRUE(SiteForCookies::FromWire(SchemefulSite(GURL("aH://example.test")),
                                       false, &out));
  EXPECT_TRUE(out.IsNull());

  // Not a eTLD + 1 (or something hosty), but this is fine. (Is converted to a
  // registrable domain by SchemefulSite constructor.)
  EXPECT_TRUE(SiteForCookies::FromWire(
      SchemefulSite(GURL("http://sub.example.test")), false, &out));
  EXPECT_TRUE(out.IsNull());
  EXPECT_EQ(
      "SiteForCookies: {site=http://example.test; schemefully_same=false}",
      out.ToDebugString());

  // IP address is fine.
  EXPECT_TRUE(SiteForCookies::FromWire(SchemefulSite(GURL("https://127.0.0.1")),
                                       true, &out));
  EXPECT_FALSE(out.IsNull());
  EXPECT_EQ("SiteForCookies: {site=https://127.0.0.1; schemefully_same=true}",
            out.ToDebugString());

  // This one's schemefully_same is false
  EXPECT_TRUE(SiteForCookies::FromWire(SchemefulSite(GURL("https://127.0.0.1")),
                                       false, &out));
  EXPECT_TRUE(out.IsNull());
  EXPECT_EQ("SiteForCookies: {site=https://127.0.0.1; schemefully_same=false}",
            out.ToDebugString());

  // An actual eTLD+1 is fine.
  EXPECT_TRUE(SiteForCookies::FromWire(
      SchemefulSite(GURL("http://example.test")), true, &out));
  EXPECT_FALSE(out.IsNull());
  EXPECT_EQ("SiteForCookies: {site=http://example.test; schemefully_same=true}",
            out.ToDebugString());

  // This one's schemefully_same is false.
  EXPECT_TRUE(SiteForCookies::FromWire(
      SchemefulSite(GURL("http://example.test")), false, &out));
  EXPECT_TRUE(out.IsNull());
  EXPECT_EQ(
      "SiteForCookies: {site=http://example.test; schemefully_same=false}",
      out.ToDebugString());
}

TEST(SiteForCookiesTest, SchemefulSite) {
  const char* kTestCases[] = {"opaque.com",
                              "http://a.com",
                              "https://sub1.example.com:42/something",
                              "https://a.com",
                              "ws://a.com",
                              "wss://a.com",
                              "file://a.com",
                              "file://folder1/folder2/file.txt",
                              "file:///file.txt"};

  for (std::string url : kTestCases) {
    url::Origin origin = url::Origin::Create(GURL(url));
    SiteForCookies from_origin = SiteForCookies::FromOrigin(origin);
    SchemefulSite schemeful_site = SchemefulSite(origin);
    SiteForCookies from_schemeful_site = SiteForCookies(schemeful_site);

    EXPECT_TRUE(from_origin.IsEquivalent(from_schemeful_site));
    EXPECT_TRUE(from_schemeful_site.IsEquivalent(from_origin));
  }
}

TEST(SiteForCookiesTest, CompareWithFrameTreeSiteAndRevise) {
  SchemefulSite secure_example = SchemefulSite(GURL("https://example.com"));
  SchemefulSite insecure_example = SchemefulSite(GURL("http://example.com"));
  SchemefulSite secure_other = SchemefulSite(GURL("https://other.com"));
  SchemefulSite insecure_other = SchemefulSite(GURL("http://other.com"));

  // Other scheme tests.
  url::ScopedSchemeRegistryForTests scoped_registry;
  AddStandardScheme("other", url::SCHEME_WITH_HOST);
  SchemefulSite file_scheme =
      SchemefulSite(GURL("file:///C:/Users/Default/Pictures/photo.png"));
  SchemefulSite file_scheme2 = SchemefulSite(GURL("file:///C:/file.txt"));
  SchemefulSite other_scheme = SchemefulSite(GURL("other://"));

  // This function should work the same regardless the state of Schemeful
  // Same-Site.
  for (const bool toggle : {false, true}) {
    base::test::ScopedFeatureList scope_feature_list;
    scope_feature_list.InitWithFeatureState(features::kSchemefulSameSite,
                                            toggle);

    SiteForCookies candidate1 = SiteForCookies(secure_example);
    EXPECT_TRUE(candidate1.CompareWithFrameTreeSiteAndRevise(secure_example));
    EXPECT_FALSE(candidate1.site().opaque());
    EXPECT_TRUE(candidate1.schemefully_same());

    SiteForCookies candidate2 = SiteForCookies(secure_example);
    EXPECT_TRUE(candidate2.CompareWithFrameTreeSiteAndRevise(insecure_example));
    EXPECT_FALSE(candidate2.site().opaque());
    EXPECT_FALSE(candidate2.schemefully_same());

    SiteForCookies candidate3 = SiteForCookies(secure_example);
    EXPECT_FALSE(candidate3.CompareWithFrameTreeSiteAndRevise(secure_other));
    EXPECT_TRUE(candidate3.site().opaque());
    // schemefully_same is N/A if the site() is opaque.

    SiteForCookies candidate4 = SiteForCookies(secure_example);
    EXPECT_FALSE(candidate4.CompareWithFrameTreeSiteAndRevise(insecure_other));
    EXPECT_TRUE(candidate4.site().opaque());
    // schemefully_same is N/A if the site() is opaque.

    // This function's check is bi-directional, so try reversed pairs just in
    // case.
    SiteForCookies candidate2_reversed = SiteForCookies(insecure_example);
    EXPECT_TRUE(
        candidate2_reversed.CompareWithFrameTreeSiteAndRevise(secure_example));
    EXPECT_FALSE(candidate2_reversed.site().opaque());
    EXPECT_FALSE(candidate2_reversed.schemefully_same());

    SiteForCookies candidate3_reversed = SiteForCookies(secure_other);
    EXPECT_FALSE(
        candidate3_reversed.CompareWithFrameTreeSiteAndRevise(secure_example));
    EXPECT_TRUE(candidate3_reversed.site().opaque());
    // schemefully_same is N/A if the site() is opaque.

    SiteForCookies candidate4_reversed = SiteForCookies(insecure_other);
    EXPECT_FALSE(
        candidate4_reversed.CompareWithFrameTreeSiteAndRevise(secure_example));
    EXPECT_TRUE(candidate4_reversed.site().opaque());
    // schemefully_same is N/A if the site() is opaque.

    // Now try some different schemes.
    SiteForCookies candidate5 = SiteForCookies(file_scheme);
    EXPECT_TRUE(candidate5.CompareWithFrameTreeSiteAndRevise(file_scheme2));
    EXPECT_FALSE(candidate5.site().opaque());
    EXPECT_TRUE(candidate5.schemefully_same());

    SiteForCookies candidate6 = SiteForCookies(file_scheme);
    EXPECT_FALSE(candidate6.CompareWithFrameTreeSiteAndRevise(other_scheme));
    EXPECT_TRUE(candidate6.site().opaque());
    // schemefully_same is N/A if the site() is opaque.

    SiteForCookies candidate5_reversed = SiteForCookies(file_scheme2);
    EXPECT_TRUE(
        candidate5_reversed.CompareWithFrameTreeSiteAndRevise(file_scheme));
    EXPECT_FALSE(candidate5_reversed.site().opaque());
    EXPECT_TRUE(candidate5_reversed.schemefully_same());

    SiteForCookies candidate6_reversed = SiteForCookies(other_scheme);
    EXPECT_FALSE(
        candidate6_reversed.CompareWithFrameTreeSiteAndRevise(file_scheme));
    EXPECT_TRUE(candidate6_reversed.site().opaque());
    // schemefully_same is N/A if the site() is opaque.
  }
}

TEST(SiteForCookiesTest, CompareWithFrameTreeSiteAndReviseOpaque) {
  url::Origin opaque1 = url::Origin();
  url::Origin opaque2 = url::Origin();

  SchemefulSite opaque_site1 = SchemefulSite(opaque1);
  SchemefulSite opaque_site2 = SchemefulSite(opaque2);
  SchemefulSite example = SchemefulSite(GURL("https://example.com"));

  // Opaque origins are able to match on the frame comparison.
  SiteForCookies candidate1 = SiteForCookies(opaque_site1);
  EXPECT_TRUE(candidate1.CompareWithFrameTreeSiteAndRevise(opaque_site1));
  EXPECT_TRUE(candidate1.site().opaque());
  // schemefully_same is N/A if the site() is opaque.
  EXPECT_EQ(candidate1.site(), opaque_site1);

  SiteForCookies candidate2 = SiteForCookies(opaque_site1);
  EXPECT_TRUE(candidate2.CompareWithFrameTreeSiteAndRevise(opaque_site2));
  EXPECT_TRUE(candidate2.site().opaque());
  // schemefully_same is N/A if the site() is opaque.
  EXPECT_EQ(candidate2.site(), opaque_site1);

  // But if only one is opaque they won't match.
  SiteForCookies candidate3 = SiteForCookies(example);
  EXPECT_FALSE(candidate3.CompareWithFrameTreeSiteAndRevise(opaque_site1));
  EXPECT_TRUE(candidate3.site().opaque());
  // schemefully_same is N/A if the site() is opaque.
  EXPECT_NE(candidate3.site(), opaque_site1);

  SiteForCookies candidate4 = SiteForCookies(opaque_site1);
  EXPECT_FALSE(candidate4.CompareWithFrameTreeSiteAndRevise(example));
  EXPECT_TRUE(candidate4.site().opaque());
  // schemefully_same is N/A if the site() is opaque.
  EXPECT_EQ(candidate4.site(), opaque_site1);
}

TEST(SiteForCookiesTest, NotSchemefullySameEquivalent) {
  SiteForCookies first =
      SiteForCookies::FromUrl(GURL("https://www.example.com"));
  SiteForCookies second =
      SiteForCookies::FromUrl(GURL("https://www.example.com"));
  // Smoke check that two SFCs should match when they're the same.
  EXPECT_TRUE(first.IsEquivalent(second));
  EXPECT_TRUE(second.IsEquivalent(first));

  // Two SFC should not be equivalent to each other when one of their
  // schemefully_same_ flags is false, even if they're otherwise the same, when
  // Schemeful Same-Site is enabled.
  second.SetSchemefullySameForTesting(false);
  EXPECT_FALSE(first.IsEquivalent(second));
  EXPECT_FALSE(second.IsEquivalent(first));

  // However, they should match if both their schemefully_same_ flags are false.
  // Because they're both considered null at that point.
  first.SetSchemefullySameForTesting(false);
  EXPECT_TRUE(first.IsEquivalent(second));
  EXPECT_TRUE(second.IsEquivalent(first));
}

}  // namespace

TEST(SiteForCookiesTest, SameScheme) {
  struct TestCase {
    const char* first;
    const char* second;
    bool expected_value;
  };

  const TestCase kTestCases[] = {
      {"http://a.com", "http://a.com", true},
      {"https://a.com", "https://a.com", true},
      {"ws://a.com", "ws://a.com", true},
      {"wss://a.com", "wss://a.com", true},
      {"https://a.com", "wss://a.com", true},
      {"wss://a.com", "https://a.com", true},
      {"http://a.com", "ws://a.com", true},
      {"ws://a.com", "http://a.com", true},
      {"file://a.com", "file://a.com", true},
      {"file://folder1/folder2/file.txt", "file://folder1/folder2/file.txt",
       true},
      {"ftp://a.com", "ftp://a.com", true},
      {"http://a.com", "file://a.com", false},
      {"ws://a.com", "wss://a.com", false},
      {"wss://a.com", "ws://a.com", false},
      {"https://a.com", "http://a.com", false},
      {"file://a.com", "https://a.com", false},
      {"https://a.com", "file://a.com", false},
      {"file://a.com", "ftp://a.com", false},
      {"ftp://a.com", "file://a.com", false},
  };

  for (const TestCase& t : kTestCases) {
    SiteForCookies first = SiteForCookies::FromUrl(GURL(t.first));
    SchemefulSite second(GURL(t.second));
    EXPECT_FALSE(first.IsNull());
    first.MarkIfCrossScheme(second);
    EXPECT_EQ(first.schemefully_same(), t.expected_value);
  }
}

TEST(SiteForCookiesTest, SameSchemeOpaque) {
  url::Origin not_opaque_secure =
      url::Origin::Create(GURL("https://site.example"));
  url::Origin not_opaque_nonsecure =
      url::Origin::Create(GURL("http://site.example"));
  // Check an opaque origin made from a triple origin and one from the default
  // constructor.
  const url::Origin kOpaqueOrigins[] = {
      not_opaque_secure.DeriveNewOpaqueOrigin(),
      not_opaque_nonsecure.DeriveNewOpaqueOrigin(), url::Origin()};

  for (const url::Origin& origin : kOpaqueOrigins) {
    SiteForCookies secure_sfc = SiteForCookies::FromOrigin(not_opaque_secure);
    EXPECT_FALSE(secure_sfc.IsNull());
    SiteForCookies nonsecure_sfc =
        SiteForCookies::FromOrigin(not_opaque_nonsecure);
    EXPECT_FALSE(nonsecure_sfc.IsNull());

    SchemefulSite site(origin);

    EXPECT_TRUE(secure_sfc.schemefully_same());
    secure_sfc.MarkIfCrossScheme(site);
    EXPECT_FALSE(secure_sfc.schemefully_same());

    EXPECT_TRUE(nonsecure_sfc.schemefully_same());
    nonsecure_sfc.MarkIfCrossScheme(site);
    EXPECT_FALSE(nonsecure_sfc.schemefully_same());

    SiteForCookies opaque_sfc = SiteForCookies(site);
    EXPECT_TRUE(opaque_sfc.IsNull());
    // Slightly implementation detail specific as the value isn't relevant for
    // null SFCs.
    EXPECT_FALSE(nonsecure_sfc.schemefully_same());
  }
}

// Quick correctness check that the less-than operator works as expected.
TEST(SiteForCookiesTest, LessThan) {
  SiteForCookies first = SiteForCookies::FromUrl(GURL("https://example.com"));
  SiteForCookies second =
      SiteForCookies::FromUrl(GURL("https://examplelonger.com"));
  SiteForCookies third =
      SiteForCookies::FromUrl(GURL("https://examplelongerstill.com"));

  SiteForCookies null1 = SiteForCookies();
  SiteForCookies null2 =
      SiteForCookies::FromUrl(GURL("https://examplelongerstillstill.com"));
  null2.SetSchemefullySameForTesting(false);

  EXPECT_LT(first, second);
  EXPECT_LT(second, third);
  EXPECT_LT(first, third);
  EXPECT_LT(null1, first);
  EXPECT_LT(null2, first);

  EXPECT_FALSE(second < first);
  EXPECT_FALSE(first < null1);
  EXPECT_FALSE(first < null2);
  EXPECT_FALSE(null1 < null2);
  EXPECT_FALSE(null2 < null1);
}

}  // namespace net