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