• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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 #ifndef NET_COOKIES_TEST_COOKIE_ACCESS_DELEGATE_H_
6 #define NET_COOKIES_TEST_COOKIE_ACCESS_DELEGATE_H_
7 
8 #include <map>
9 #include <optional>
10 #include <set>
11 #include <string>
12 
13 #include "base/containers/flat_map.h"
14 #include "base/containers/flat_set.h"
15 #include "base/functional/callback_forward.h"
16 #include "net/base/schemeful_site.h"
17 #include "net/cookies/cookie_access_delegate.h"
18 #include "net/cookies/cookie_constants.h"
19 #include "net/first_party_sets/first_party_set_entry.h"
20 #include "net/first_party_sets/first_party_set_metadata.h"
21 #include "net/first_party_sets/first_party_sets_cache_filter.h"
22 
23 namespace net {
24 
25 // CookieAccessDelegate for testing. You can set the return value for a given
26 // cookie_domain (modulo any leading dot). Calling GetAccessSemantics() will
27 // then return the given value, or UNKNOWN if you haven't set one.
28 class TestCookieAccessDelegate : public CookieAccessDelegate {
29  public:
30   TestCookieAccessDelegate();
31 
32   TestCookieAccessDelegate(const TestCookieAccessDelegate&) = delete;
33   TestCookieAccessDelegate& operator=(const TestCookieAccessDelegate&) = delete;
34 
35   ~TestCookieAccessDelegate() override;
36 
37   // CookieAccessDelegate implementation:
38   CookieAccessSemantics GetAccessSemantics(
39       const CanonicalCookie& cookie) const override;
40   CookieLegacyScope GetAccessForLegacyCookieScope(
41       const CanonicalCookie& cookie) const override;
42   bool ShouldIgnoreSameSiteRestrictions(
43       const GURL& url,
44       const SiteForCookies& site_for_cookies) const override;
45   bool ShouldTreatUrlAsTrustworthy(const GURL& url) const override;
46   std::optional<
47       std::pair<FirstPartySetMetadata, FirstPartySetsCacheFilter::MatchInfo>>
48   ComputeFirstPartySetMetadataMaybeAsync(
49       const SchemefulSite& site,
50       const SchemefulSite* top_frame_site,
51       base::OnceCallback<void(FirstPartySetMetadata,
52                               FirstPartySetsCacheFilter::MatchInfo)> callback)
53       const override;
54   std::optional<base::flat_map<SchemefulSite, FirstPartySetEntry>>
55   FindFirstPartySetEntries(
56       const base::flat_set<SchemefulSite>& sites,
57       base::OnceCallback<
58           void(base::flat_map<SchemefulSite, FirstPartySetEntry>)> callback)
59       const override;
60 
61   // Sets the expected return value for any cookie whose Domain
62   // matches |cookie_domain|. Pass the value of |cookie.Domain()| and any
63   // leading dot will be discarded.
64   void SetExpectationForCookieDomain(const std::string& cookie_domain,
65                                      CookieAccessSemantics access_semantics);
66 
67   // Sets the expected return value for ShouldAlwaysAttachSameSiteCookies.
68   // Can set schemes that always attach SameSite cookies, or schemes that always
69   // attach SameSite cookies if the request URL is secure.
70   void SetIgnoreSameSiteRestrictionsScheme(
71       const std::string& site_for_cookies_scheme,
72       bool require_secure_origin);
73 
74   // Set the test delegate's First-Party Sets. The map's keys are the sites in
75   // the sets. Primary sites must be included among the keys for a given set.
76   void SetFirstPartySets(
77       const base::flat_map<SchemefulSite, FirstPartySetEntry>& sets);
78 
set_invoke_callbacks_asynchronously(bool async)79   void set_invoke_callbacks_asynchronously(bool async) {
80     invoke_callbacks_asynchronously_ = async;
81   }
82 
set_first_party_sets_cache_filter(FirstPartySetsCacheFilter filter)83   void set_first_party_sets_cache_filter(FirstPartySetsCacheFilter filter) {
84     first_party_sets_cache_filter_ = std::move(filter);
85   }
86 
87  private:
88   // Finds a FirstPartySetEntry for the given site, if one exists.
89   std::optional<FirstPartySetEntry> FindFirstPartySetEntry(
90       const SchemefulSite& site) const;
91 
92   // Discard any leading dot in the domain string.
93   std::string GetKeyForDomainValue(const std::string& domain) const;
94 
95   // Invokes the given `callback` asynchronously or returns the result
96   // synchronously, depending on the configuration of this instance.
97   template <class T>
98   std::optional<T> RunMaybeAsync(T result,
99                                  base::OnceCallback<void(T)> callback) const;
100 
101   std::map<std::string, CookieAccessSemantics> expectations_;
102   std::map<std::string, CookieLegacyScope> expectations_legacy_;
103   std::map<std::string, bool> ignore_samesite_restrictions_schemes_;
104   base::flat_map<SchemefulSite, FirstPartySetEntry> first_party_sets_;
105   FirstPartySetsCacheFilter first_party_sets_cache_filter_;
106   bool invoke_callbacks_asynchronously_ = false;
107   SchemefulSite trustworthy_site_ =
108       SchemefulSite(GURL("http://trustworthysitefortestdelegate.example"));
109 };
110 
111 }  // namespace net
112 
113 #endif  // NET_COOKIES_TEST_COOKIE_ACCESS_DELEGATE_H_
114