File: cookie_base_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (239 lines) | stat: -rw-r--r-- 8,053 bytes parent folder | download | duplicates (8)
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
// Copyright 2024 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_base.h"

#include <string>

#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "net/cookies/cookie_constants.h"
#include "net/cookies/cookie_partition_key.h"
#include "net/test/test_with_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/third_party/mozilla/url_parse.h"

namespace net {
namespace {

// A subclass of CookieBase to allow access to its protected members. Allows
// customizing the Lax-allow-unsafe threshold age.
class TestCookie : public CookieBase {
 public:
  // Builder interface to allow easier creation, with default values for
  // unspecified fields.
  class Builder {
   public:
    Builder() = default;

    Builder& SetLaxUnsafeAge(base::TimeDelta lax_unsafe_age) {
      lax_unsafe_age_ = lax_unsafe_age;
      return *this;
    }

    Builder& SetName(const std::string& name) {
      name_ = name;
      return *this;
    }

    Builder& SetDomain(const std::string& domain) {
      domain_ = domain;
      return *this;
    }

    Builder& SetPath(const std::string& path) {
      path_ = path;
      return *this;
    }

    Builder& SetCreation(base::Time creation) {
      creation_ = creation;
      return *this;
    }

    Builder& SetSecure(bool secure) {
      secure_ = secure;
      return *this;
    }

    Builder& SetHttpOnly(bool httponly) {
      httponly_ = httponly;
      return *this;
    }

    Builder& SetSameSite(CookieSameSite same_site) {
      same_site_ = same_site;
      return *this;
    }

    Builder& SetPartitionKey(std::optional<CookiePartitionKey> partition_key) {
      partition_key_ = std::move(partition_key);
      return *this;
    }

    Builder& SetSourceScheme(CookieSourceScheme source_scheme) {
      source_scheme_ = source_scheme;
      return *this;
    }

    Builder& SetSourcePort(int source_port) {
      source_port_ = source_port;
      return *this;
    }

    TestCookie Build() {
      return TestCookie(
          lax_unsafe_age_, name_.value_or("name"),
          domain_.value_or("www.example.test"), path_.value_or("/foo"),
          creation_.value_or(base::Time::Now()), secure_.value_or(false),
          httponly_.value_or(false),
          same_site_.value_or(CookieSameSite::UNSPECIFIED), partition_key_,
          source_scheme_.value_or(CookieSourceScheme::kUnset),
          source_port_.value_or(url::PORT_UNSPECIFIED));
    }

   private:
    std::optional<base::TimeDelta> lax_unsafe_age_;
    std::optional<std::string> name_;
    std::optional<std::string> domain_;
    std::optional<std::string> path_;
    std::optional<base::Time> creation_;
    std::optional<bool> secure_;
    std::optional<bool> httponly_;
    std::optional<CookieSameSite> same_site_;
    std::optional<CookiePartitionKey> partition_key_;
    std::optional<CookieSourceScheme> source_scheme_;
    std::optional<int> source_port_;
  };

  CookieEffectiveSameSite GetEffectiveSameSiteForTesting(
      CookieAccessSemantics access_semantics) const {
    return GetEffectiveSameSite(access_semantics);
  }

  bool IsRecentlyCreatedForTesting() const {
    return IsRecentlyCreated(GetLaxAllowUnsafeThresholdAge());
  }

  // CookieBase:
  base::TimeDelta GetLaxAllowUnsafeThresholdAge() const override {
    return lax_unsafe_age_.value_or(
        CookieBase::GetLaxAllowUnsafeThresholdAge());
  }

 private:
  friend class Builder;

  TestCookie(std::optional<base::TimeDelta> lax_unsafe_age,
             std::string name,
             std::string domain,
             std::string path,
             base::Time creation,
             bool secure,
             bool httponly,
             CookieSameSite same_site,
             std::optional<CookiePartitionKey> partition_key,
             CookieSourceScheme source_scheme,
             int source_port)
      : CookieBase(std::move(name),
                   std::move(domain),
                   std::move(path),
                   creation,
                   secure,
                   httponly,
                   same_site,
                   std::move(partition_key),
                   source_scheme,
                   source_port),
        lax_unsafe_age_(lax_unsafe_age) {}

  const std::optional<base::TimeDelta> lax_unsafe_age_;
};

class CookieBaseTest : public ::testing::Test, public WithTaskEnvironment {
 public:
  // Use MOCK_TIME to test the Lax-allow-unsafe age threshold behavior.
  CookieBaseTest()
      : WithTaskEnvironment(
            base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
};

// TODO(crbug.com/324405105): Add tests for other CookieBase functionality.

TEST_F(CookieBaseTest, GetLaxAllowUnsafeThresholdAge) {
  // Create a TestCookie with no override for the Lax-allow-unsafe threshold
  // age. This should just return the base class's value.
  TestCookie c = TestCookie::Builder().Build();

  EXPECT_EQ(c.GetLaxAllowUnsafeThresholdAge(), base::TimeDelta::Min());
}

TEST_F(CookieBaseTest, GetEffectiveSameSite) {
  // Cases whose behavior does not depend on cookie age relative to the
  // threshold.
  const struct {
    CookieSameSite same_site;
    CookieAccessSemantics access_semantics;
    CookieEffectiveSameSite expected_effective_same_site;
  } kCommonTestCases[] = {
      {CookieSameSite::UNSPECIFIED, CookieAccessSemantics::LEGACY,
       CookieEffectiveSameSite::NO_RESTRICTION},
      {CookieSameSite::NO_RESTRICTION, CookieAccessSemantics::NONLEGACY,
       CookieEffectiveSameSite::NO_RESTRICTION},
      {CookieSameSite::NO_RESTRICTION, CookieAccessSemantics::LEGACY,
       CookieEffectiveSameSite::NO_RESTRICTION},
      {CookieSameSite::LAX_MODE, CookieAccessSemantics::NONLEGACY,
       CookieEffectiveSameSite::LAX_MODE},
      {CookieSameSite::LAX_MODE, CookieAccessSemantics::LEGACY,
       CookieEffectiveSameSite::LAX_MODE},
      {CookieSameSite::STRICT_MODE, CookieAccessSemantics::NONLEGACY,
       CookieEffectiveSameSite::STRICT_MODE},
      {CookieSameSite::STRICT_MODE, CookieAccessSemantics::LEGACY,
       CookieEffectiveSameSite::STRICT_MODE},
  };

  for (const auto& test_case : kCommonTestCases) {
    TestCookie c = TestCookie::Builder()
                       .SetLaxUnsafeAge(base::Minutes(1))
                       .SetSameSite(test_case.same_site)
                       .Build();
    EXPECT_EQ(c.GetLaxAllowUnsafeThresholdAge(), base::Minutes(1));
    EXPECT_TRUE(c.IsRecentlyCreatedForTesting());
    EXPECT_EQ(c.GetEffectiveSameSiteForTesting(test_case.access_semantics),
              test_case.expected_effective_same_site);

    // Fast forward time so the cookie is now older than the threshold.
    FastForwardBy(base::Minutes(5));

    EXPECT_EQ(c.GetLaxAllowUnsafeThresholdAge(), base::Minutes(1));
    EXPECT_FALSE(c.IsRecentlyCreatedForTesting());
    EXPECT_EQ(c.GetEffectiveSameSiteForTesting(test_case.access_semantics),
              test_case.expected_effective_same_site);
  }
}

// Test behavior where the effective samesite depends on whether the cookie is
// newer than the Lax-allow-unsafe age threshold.
TEST_F(CookieBaseTest, GetEffectiveSameSiteAgeThreshold) {
  TestCookie c = TestCookie::Builder()
                     .SetLaxUnsafeAge(base::Minutes(1))
                     .SetSameSite(CookieSameSite::UNSPECIFIED)
                     .Build();

  EXPECT_EQ(c.GetLaxAllowUnsafeThresholdAge(), base::Minutes(1));
  EXPECT_TRUE(c.IsRecentlyCreatedForTesting());
  EXPECT_EQ(c.GetEffectiveSameSiteForTesting(CookieAccessSemantics::NONLEGACY),
            CookieEffectiveSameSite::LAX_MODE_ALLOW_UNSAFE);

  // Fast forward time so the cookie is now older than the threshold.
  FastForwardBy(base::Minutes(5));

  EXPECT_FALSE(c.IsRecentlyCreatedForTesting());
  EXPECT_EQ(c.GetEffectiveSameSiteForTesting(CookieAccessSemantics::NONLEGACY),
            CookieEffectiveSameSite::LAX_MODE);
}

}  // namespace
}  // namespace net