summaryrefslogtreecommitdiff
path: root/chromium/net/cookies/cookie_partition_key_collection_unittest.cc
blob: 01a9bad3d596d391364dd280b51c0e0c8945caf4 (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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/cookies/cookie_partition_key_collection.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "net/cookies/test_cookie_access_delegate.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

using testing::UnorderedElementsAre;

namespace {

// Synchronous wrapper around CookiePartitionKeyCollection::FirstPartySetify.
// Spins the event loop.
CookiePartitionKeyCollection FirstPartySetifyAndWait(
    const CookiePartitionKeyCollection& collection,
    const CookieAccessDelegate* cookie_access_delegate) {
  base::RunLoop run_loop;
  CookiePartitionKeyCollection canonicalized_collection;
  absl::optional<CookiePartitionKeyCollection> maybe_collection =
      collection.FirstPartySetify(
          cookie_access_delegate,
          base::BindLambdaForTesting([&](CookiePartitionKeyCollection result) {
            canonicalized_collection = result;
            run_loop.Quit();
          }));
  if (maybe_collection.has_value())
    return maybe_collection.value();
  run_loop.Run();
  return canonicalized_collection;
}

}  // namespace

TEST(CookiePartitionKeyCollectionTest, EmptySet) {
  CookiePartitionKeyCollection key_collection;

  EXPECT_TRUE(key_collection.IsEmpty());
  EXPECT_FALSE(key_collection.ContainsAllKeys());
  EXPECT_EQ(0u, key_collection.PartitionKeys().size());
}

TEST(CookiePartitionKeyCollectionTest, SingletonSet) {
  CookiePartitionKeyCollection key_collection(
      CookiePartitionKey::FromURLForTesting(GURL("https://www.foo.com")));

  EXPECT_FALSE(key_collection.IsEmpty());
  EXPECT_FALSE(key_collection.ContainsAllKeys());
  EXPECT_THAT(key_collection.PartitionKeys(),
              UnorderedElementsAre(CookiePartitionKey::FromURLForTesting(
                  GURL("https://www.foo.com"))));
}

TEST(CookiePartitionKeyCollectionTest, MultipleElements) {
  CookiePartitionKeyCollection key_collection({
      CookiePartitionKey::FromURLForTesting(GURL("https://www.foo.com")),
      CookiePartitionKey::FromURLForTesting(GURL("https://www.bar.com")),
  });

  EXPECT_FALSE(key_collection.IsEmpty());
  EXPECT_FALSE(key_collection.ContainsAllKeys());
  EXPECT_THAT(key_collection.PartitionKeys(),
              UnorderedElementsAre(CookiePartitionKey::FromURLForTesting(
                                       GURL("https://subdomain.foo.com")),
                                   CookiePartitionKey::FromURLForTesting(
                                       GURL("https://www.bar.com"))));
}

TEST(CookiePartitionKeyCollectionTest, ContainsAll) {
  CookiePartitionKeyCollection key_collection =
      CookiePartitionKeyCollection::ContainsAll();
  EXPECT_FALSE(key_collection.IsEmpty());
  EXPECT_TRUE(key_collection.ContainsAllKeys());
}

TEST(CookiePartitionKeyCollectionTest, FromOptional) {
  CookiePartitionKeyCollection key_collection =
      CookiePartitionKeyCollection::FromOptional(absl::nullopt);
  EXPECT_TRUE(key_collection.IsEmpty());
  EXPECT_FALSE(key_collection.ContainsAllKeys());

  key_collection = CookiePartitionKeyCollection::FromOptional(
      absl::make_optional<CookiePartitionKey>(
          CookiePartitionKey::FromURLForTesting(GURL("https://www.foo.com"))));
  EXPECT_FALSE(key_collection.IsEmpty());
  EXPECT_FALSE(key_collection.ContainsAllKeys());
  EXPECT_THAT(key_collection.PartitionKeys(),
              UnorderedElementsAre(CookiePartitionKey::FromURLForTesting(
                  GURL("https://www.foo.com"))));
}

TEST(CookiePartitionKeyCollectionTest, FirstPartySetify) {
  base::test::TaskEnvironment env;
  const GURL kOwnerURL("https://owner.com");
  const SchemefulSite kOwnerSite(kOwnerURL);
  const CookiePartitionKey kOwnerPartitionKey =
      CookiePartitionKey::FromURLForTesting(kOwnerURL);

  const GURL kMemberURL("https://member.com");
  const SchemefulSite kMemberSite(kMemberURL);
  const CookiePartitionKey kMemberPartitionKey =
      CookiePartitionKey::FromURLForTesting(kMemberURL);

  const GURL kNonMemberURL("https://nonmember.com");
  const CookiePartitionKey kNonMemberPartitionKey =
      CookiePartitionKey::FromURLForTesting(kNonMemberURL);

  TestCookieAccessDelegate delegate;
  delegate.SetFirstPartySets({
      {kOwnerSite, net::FirstPartySetEntry(kOwnerSite, net::SiteType::kPrimary,
                                           absl::nullopt)},
      {kMemberSite,
       net::FirstPartySetEntry(kOwnerSite, net::SiteType::kAssociated, 0)},
  });

  CookiePartitionKeyCollection empty_key_collection;
  EXPECT_TRUE(
      FirstPartySetifyAndWait(empty_key_collection, &delegate).IsEmpty());
  EXPECT_TRUE(FirstPartySetifyAndWait(empty_key_collection, nullptr).IsEmpty());

  CookiePartitionKeyCollection contains_all_keys =
      CookiePartitionKeyCollection::ContainsAll();
  EXPECT_TRUE(
      FirstPartySetifyAndWait(contains_all_keys, &delegate).ContainsAllKeys());
  EXPECT_TRUE(
      FirstPartySetifyAndWait(contains_all_keys, nullptr).ContainsAllKeys());

  // An owner site of an FPS should not have its partition key changed.
  EXPECT_THAT(FirstPartySetifyAndWait(
                  CookiePartitionKeyCollection(kOwnerPartitionKey), &delegate)
                  .PartitionKeys(),
              UnorderedElementsAre(kOwnerPartitionKey));

  // A member site should have its partition key changed to the owner site.
  EXPECT_THAT(FirstPartySetifyAndWait(
                  CookiePartitionKeyCollection(kMemberPartitionKey), &delegate)
                  .PartitionKeys(),
              UnorderedElementsAre(kOwnerPartitionKey));

  // A member site's partition key should not change if the CookieAccessDelegate
  // is null.
  EXPECT_THAT(FirstPartySetifyAndWait(
                  CookiePartitionKeyCollection(kMemberPartitionKey), nullptr)
                  .PartitionKeys(),
              UnorderedElementsAre(kMemberPartitionKey));

  // A non-member site should not have its partition key changed.
  EXPECT_THAT(
      FirstPartySetifyAndWait(
          CookiePartitionKeyCollection(kNonMemberPartitionKey), &delegate)
          .PartitionKeys(),
      UnorderedElementsAre(kNonMemberPartitionKey));

  // A key collection that contains a member site and non-member site should be
  // changed to include the owner site and the unmodified non-member site.
  EXPECT_THAT(FirstPartySetifyAndWait(
                  CookiePartitionKeyCollection(
                      {kMemberPartitionKey, kNonMemberPartitionKey}),
                  &delegate)
                  .PartitionKeys(),
              UnorderedElementsAre(kOwnerPartitionKey, kNonMemberPartitionKey));

  // Test that FirstPartySetify does not modify partition keys with nonces.
  const CookiePartitionKey kNoncedPartitionKey =
      CookiePartitionKey::FromURLForTesting(kMemberURL,
                                            base::UnguessableToken::Create());
  EXPECT_THAT(
      FirstPartySetifyAndWait(
          CookiePartitionKeyCollection({kNoncedPartitionKey}), &delegate)
          .PartitionKeys(),
      UnorderedElementsAre(kNoncedPartitionKey));
  EXPECT_THAT(
      FirstPartySetifyAndWait(CookiePartitionKeyCollection(
                                  {kNoncedPartitionKey, kMemberPartitionKey}),
                              &delegate)
          .PartitionKeys(),
      UnorderedElementsAre(kNoncedPartitionKey, kOwnerPartitionKey));
}

TEST(CookiePartitionKeyCollectionTest, Contains) {
  const CookiePartitionKey kPartitionKey =
      CookiePartitionKey::FromURLForTesting(GURL("https://www.foo.com"));
  const CookiePartitionKey kOtherPartitionKey =
      CookiePartitionKey::FromURLForTesting(GURL("https://www.bar.com"));

  struct TestCase {
    const CookiePartitionKeyCollection keychain;
    const CookiePartitionKey key;
    bool expects_contains;
  } test_cases[] = {
      // Empty keychain
      {CookiePartitionKeyCollection(), kPartitionKey, false},
      // Singleton keychain with key
      {CookiePartitionKeyCollection(kPartitionKey), kPartitionKey, true},
      // Singleton keychain with different key
      {CookiePartitionKeyCollection(kOtherPartitionKey), kPartitionKey, false},
      // Multiple keys
      {CookiePartitionKeyCollection({kPartitionKey, kOtherPartitionKey}),
       kPartitionKey, true},
      // Contains all keys
      {CookiePartitionKeyCollection::ContainsAll(), kPartitionKey, true},
  };

  for (const auto& test_case : test_cases) {
    EXPECT_EQ(test_case.expects_contains,
              test_case.keychain.Contains(test_case.key));
  }
}

}  // namespace net