xref: /aosp_15_r20/external/cronet/net/cookies/cookie_store_test_helpers.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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_COOKIE_STORE_TEST_HELPERS_H_
6 #define NET_COOKIES_COOKIE_STORE_TEST_HELPERS_H_
7 
8 #include <memory>
9 #include <optional>
10 #include <string>
11 #include <vector>
12 
13 #include "base/functional/callback_forward.h"
14 #include "base/synchronization/lock.h"
15 #include "net/cookies/cookie_change_dispatcher.h"
16 #include "net/cookies/cookie_monster.h"
17 #include "net/log/net_log_with_source.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 
20 class GURL;
21 
22 namespace net {
23 
24 class DelayedCookieMonsterChangeDispatcher : public CookieChangeDispatcher {
25  public:
26   DelayedCookieMonsterChangeDispatcher();
27 
28   DelayedCookieMonsterChangeDispatcher(
29       const DelayedCookieMonsterChangeDispatcher&) = delete;
30   DelayedCookieMonsterChangeDispatcher& operator=(
31       const DelayedCookieMonsterChangeDispatcher&) = delete;
32 
33   ~DelayedCookieMonsterChangeDispatcher() override;
34 
35   // net::CookieChangeDispatcher
36   [[nodiscard]] std::unique_ptr<CookieChangeSubscription> AddCallbackForCookie(
37       const GURL& url,
38       const std::string& name,
39       const std::optional<CookiePartitionKey>& cookie_partition_key,
40       CookieChangeCallback callback) override;
41   [[nodiscard]] std::unique_ptr<CookieChangeSubscription> AddCallbackForUrl(
42       const GURL& url,
43       const std::optional<CookiePartitionKey>& cookie_partition_key,
44       CookieChangeCallback callback) override;
45   [[nodiscard]] std::unique_ptr<CookieChangeSubscription>
46   AddCallbackForAllChanges(CookieChangeCallback callback) override;
47 };
48 
49 class DelayedCookieMonster : public CookieStore {
50  public:
51   DelayedCookieMonster();
52 
53   DelayedCookieMonster(const DelayedCookieMonster&) = delete;
54   DelayedCookieMonster& operator=(const DelayedCookieMonster&) = delete;
55 
56   ~DelayedCookieMonster() override;
57 
58   // Call the asynchronous CookieMonster function, expect it to immediately
59   // invoke the internal callback.
60   // Post a delayed task to invoke the original callback with the results.
61 
62   void SetCanonicalCookieAsync(
63       std::unique_ptr<CanonicalCookie> cookie,
64       const GURL& source_url,
65       const CookieOptions& options,
66       SetCookiesCallback callback,
67       const std::optional<CookieAccessResult> cookie_access_result =
68           std::nullopt) override;
69 
70   void GetCookieListWithOptionsAsync(
71       const GURL& url,
72       const CookieOptions& options,
73       const CookiePartitionKeyCollection& cookie_partition_key_collection,
74       GetCookieListCallback callback) override;
75 
76   void GetAllCookiesAsync(GetAllCookiesCallback callback) override;
77 
78   void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
79                                   DeleteCallback callback) override;
80 
81   void DeleteAllCreatedInTimeRangeAsync(
82       const CookieDeletionInfo::TimeRange& creation_range,
83       DeleteCallback callback) override;
84 
85   void DeleteAllMatchingInfoAsync(net::CookieDeletionInfo delete_info,
86                                   DeleteCallback callback) override;
87 
88   void DeleteSessionCookiesAsync(DeleteCallback) override;
89 
90   void DeleteMatchingCookiesAsync(DeletePredicate, DeleteCallback) override;
91 
92   void FlushStore(base::OnceClosure callback) override;
93 
94   CookieChangeDispatcher& GetChangeDispatcher() override;
95 
96   void SetCookieableSchemes(const std::vector<std::string>& schemes,
97                             SetCookieableSchemesCallback callback) override;
98 
99  private:
100   // Be called immediately from CookieMonster.
101 
102   void SetCookiesInternalCallback(CookieAccessResult result);
103 
104   void GetCookiesWithOptionsInternalCallback(const std::string& cookie);
105   void GetCookieListWithOptionsInternalCallback(
106       const CookieAccessResultList& cookie,
107       const CookieAccessResultList& excluded_cookies);
108 
109   // Invoke the original callbacks.
110 
111   void InvokeSetCookiesCallback(CookieMonster::SetCookiesCallback callback);
112 
113   void InvokeGetCookieListCallback(
114       CookieMonster::GetCookieListCallback callback);
115 
116   friend class base::RefCountedThreadSafe<DelayedCookieMonster>;
117 
118   std::unique_ptr<CookieMonster> cookie_monster_;
119   DelayedCookieMonsterChangeDispatcher change_dispatcher_;
120 
121   bool did_run_ = false;
122   CookieAccessResult result_;
123   std::string cookie_;
124   std::string cookie_line_;
125   CookieAccessResultList cookie_access_result_list_;
126   CookieList cookie_list_;
127 };
128 
129 class CookieURLHelper {
130  public:
131   explicit CookieURLHelper(const std::string& url_string);
132 
domain()133   const std::string& domain() const { return domain_and_registry_; }
host()134   std::string host() const { return url_.host(); }
url()135   const GURL& url() const { return url_; }
136   const GURL AppendPath(const std::string& path) const;
137 
138   // Return a new string with the following substitutions:
139   // 1. "%R" -> Domain registry (i.e. "com")
140   // 2. "%D" -> Domain + registry (i.e. "google.com")
141   std::string Format(const std::string& format_string) const;
142 
143  private:
144   const GURL url_;
145   const std::string registry_;
146   const std::string domain_and_registry_;
147 };
148 
149 // Mock PersistentCookieStore that keeps track of the number of Flush() calls.
150 class FlushablePersistentStore : public CookieMonster::PersistentCookieStore {
151  public:
152   FlushablePersistentStore();
153 
154   // CookieMonster::PersistentCookieStore implementation:
155   void Load(LoadedCallback loaded_callback,
156             const NetLogWithSource& net_log) override;
157   void LoadCookiesForKey(const std::string& key,
158                          LoadedCallback loaded_callback) override;
159   void AddCookie(const CanonicalCookie&) override;
160   void UpdateCookieAccessTime(const CanonicalCookie&) override;
161   void DeleteCookie(const CanonicalCookie&) override;
162   void SetForceKeepSessionState() override;
163   void SetBeforeCommitCallback(base::RepeatingClosure callback) override;
164   void Flush(base::OnceClosure callback) override;
165 
166   int flush_count();
167 
168  private:
169   ~FlushablePersistentStore() override;
170 
171   int flush_count_ = 0;
172   base::Lock flush_count_lock_;  // Protects |flush_count_|.
173 };
174 
175 // Counts the number of times Callback() has been run.
176 class CallbackCounter : public base::RefCountedThreadSafe<CallbackCounter> {
177  public:
178   CallbackCounter();
179   void Callback();
180   int callback_count();
181 
182  private:
183   friend class base::RefCountedThreadSafe<CallbackCounter>;
184   ~CallbackCounter();
185 
186   int callback_count_ = 0;
187   base::Lock callback_count_lock_;  // Protects |callback_count_|.
188 };
189 
190 // Returns a cookie expiration string in the form of "; expires=<date>", where
191 // date is an RFC 7231 date a year in the future, which can be appended to
192 // cookie lines.
193 std::string FutureCookieExpirationString();
194 
195 }  // namespace net
196 
197 #endif  // NET_COOKIES_COOKIE_STORE_TEST_HELPERS_H_
198