xref: /aosp_15_r20/external/cronet/net/cookies/cookie_store_test_helpers.cc (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 #include "net/cookies/cookie_store_test_helpers.h"
6 
7 #include <optional>
8 #include <string>
9 #include <utility>
10 
11 #include "base/functional/bind.h"
12 #include "base/location.h"
13 #include "base/strings/string_util.h"
14 #include "base/task/single_thread_task_runner.h"
15 #include "base/time/time.h"
16 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
17 #include "net/cookies/cookie_store.h"
18 #include "net/cookies/cookie_util.h"
19 #include "net/http/http_util.h"
20 #include "url/gurl.h"
21 
22 using net::registry_controlled_domains::GetDomainAndRegistry;
23 using net::registry_controlled_domains::GetRegistryLength;
24 using net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES;
25 using net::registry_controlled_domains::INCLUDE_UNKNOWN_REGISTRIES;
26 using TimeRange = net::CookieDeletionInfo::TimeRange;
27 
28 namespace {
29 
GetRegistry(const GURL & url)30 std::string GetRegistry(const GURL& url) {
31   size_t registry_length = GetRegistryLength(url, INCLUDE_UNKNOWN_REGISTRIES,
32                                              INCLUDE_PRIVATE_REGISTRIES);
33   if (registry_length == 0)
34     return std::string();
35   return std::string(url.host(), url.host().length() - registry_length,
36                      registry_length);
37 }
38 
39 }  // namespace
40 
41 namespace net {
42 
43 const int kDelayedTime = 0;
44 
45 DelayedCookieMonsterChangeDispatcher::DelayedCookieMonsterChangeDispatcher() =
46     default;
47 DelayedCookieMonsterChangeDispatcher::~DelayedCookieMonsterChangeDispatcher() =
48     default;
49 
50 std::unique_ptr<CookieChangeSubscription>
AddCallbackForCookie(const GURL & url,const std::string & name,const std::optional<CookiePartitionKey> & cookie_partition_key,CookieChangeCallback callback)51 DelayedCookieMonsterChangeDispatcher::AddCallbackForCookie(
52     const GURL& url,
53     const std::string& name,
54     const std::optional<CookiePartitionKey>& cookie_partition_key,
55     CookieChangeCallback callback) {
56   ADD_FAILURE();
57   return nullptr;
58 }
59 std::unique_ptr<CookieChangeSubscription>
AddCallbackForUrl(const GURL & url,const std::optional<CookiePartitionKey> & cookie_partition_key,CookieChangeCallback callback)60 DelayedCookieMonsterChangeDispatcher::AddCallbackForUrl(
61     const GURL& url,
62     const std::optional<CookiePartitionKey>& cookie_partition_key,
63     CookieChangeCallback callback) {
64   ADD_FAILURE();
65   return nullptr;
66 }
67 std::unique_ptr<CookieChangeSubscription>
AddCallbackForAllChanges(CookieChangeCallback callback)68 DelayedCookieMonsterChangeDispatcher::AddCallbackForAllChanges(
69     CookieChangeCallback callback) {
70   ADD_FAILURE();
71   return nullptr;
72 }
73 
DelayedCookieMonster()74 DelayedCookieMonster::DelayedCookieMonster()
75     : cookie_monster_(std::make_unique<CookieMonster>(nullptr /* store */,
76                                                       nullptr /* netlog */)),
77       result_(CookieAccessResult(CookieInclusionStatus(
78           CookieInclusionStatus::EXCLUDE_FAILURE_TO_STORE))) {}
79 
80 DelayedCookieMonster::~DelayedCookieMonster() = default;
81 
SetCookiesInternalCallback(CookieAccessResult result)82 void DelayedCookieMonster::SetCookiesInternalCallback(
83     CookieAccessResult result) {
84   result_ = result;
85   did_run_ = true;
86 }
87 
GetCookieListWithOptionsInternalCallback(const CookieAccessResultList & cookie_list,const CookieAccessResultList & excluded_cookies)88 void DelayedCookieMonster::GetCookieListWithOptionsInternalCallback(
89     const CookieAccessResultList& cookie_list,
90     const CookieAccessResultList& excluded_cookies) {
91   cookie_access_result_list_ = cookie_list;
92   cookie_list_ = cookie_util::StripAccessResults(cookie_access_result_list_);
93   did_run_ = true;
94 }
95 
SetCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie,const GURL & source_url,const CookieOptions & options,SetCookiesCallback callback,std::optional<CookieAccessResult> cookie_access_result)96 void DelayedCookieMonster::SetCanonicalCookieAsync(
97     std::unique_ptr<CanonicalCookie> cookie,
98     const GURL& source_url,
99     const CookieOptions& options,
100     SetCookiesCallback callback,
101     std::optional<CookieAccessResult> cookie_access_result) {
102   did_run_ = false;
103   cookie_monster_->SetCanonicalCookieAsync(
104       std::move(cookie), source_url, options,
105       base::BindOnce(&DelayedCookieMonster::SetCookiesInternalCallback,
106                      base::Unretained(this)),
107       std::move(cookie_access_result));
108   DCHECK_EQ(did_run_, true);
109   base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
110       FROM_HERE,
111       base::BindOnce(&DelayedCookieMonster::InvokeSetCookiesCallback,
112                      base::Unretained(this), std::move(callback)),
113       base::Milliseconds(kDelayedTime));
114 }
115 
GetCookieListWithOptionsAsync(const GURL & url,const CookieOptions & options,const CookiePartitionKeyCollection & cookie_partition_key_collection,CookieMonster::GetCookieListCallback callback)116 void DelayedCookieMonster::GetCookieListWithOptionsAsync(
117     const GURL& url,
118     const CookieOptions& options,
119     const CookiePartitionKeyCollection& cookie_partition_key_collection,
120     CookieMonster::GetCookieListCallback callback) {
121   did_run_ = false;
122   cookie_monster_->GetCookieListWithOptionsAsync(
123       url, options, cookie_partition_key_collection,
124       base::BindOnce(
125           &DelayedCookieMonster::GetCookieListWithOptionsInternalCallback,
126           base::Unretained(this)));
127   DCHECK_EQ(did_run_, true);
128   base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
129       FROM_HERE,
130       base::BindOnce(&DelayedCookieMonster::InvokeGetCookieListCallback,
131                      base::Unretained(this), std::move(callback)),
132       base::Milliseconds(kDelayedTime));
133 }
134 
GetAllCookiesAsync(GetAllCookiesCallback callback)135 void DelayedCookieMonster::GetAllCookiesAsync(GetAllCookiesCallback callback) {
136   cookie_monster_->GetAllCookiesAsync(std::move(callback));
137 }
138 
InvokeSetCookiesCallback(CookieMonster::SetCookiesCallback callback)139 void DelayedCookieMonster::InvokeSetCookiesCallback(
140     CookieMonster::SetCookiesCallback callback) {
141   if (!callback.is_null())
142     std::move(callback).Run(result_);
143 }
144 
InvokeGetCookieListCallback(CookieMonster::GetCookieListCallback callback)145 void DelayedCookieMonster::InvokeGetCookieListCallback(
146     CookieMonster::GetCookieListCallback callback) {
147   if (!callback.is_null())
148     std::move(callback).Run(cookie_access_result_list_,
149                             CookieAccessResultList());
150 }
151 
DeleteCanonicalCookieAsync(const CanonicalCookie & cookie,DeleteCallback callback)152 void DelayedCookieMonster::DeleteCanonicalCookieAsync(
153     const CanonicalCookie& cookie,
154     DeleteCallback callback) {
155   ADD_FAILURE();
156 }
157 
DeleteAllCreatedInTimeRangeAsync(const TimeRange & creation_range,DeleteCallback callback)158 void DelayedCookieMonster::DeleteAllCreatedInTimeRangeAsync(
159     const TimeRange& creation_range,
160     DeleteCallback callback) {
161   ADD_FAILURE();
162 }
163 
DeleteAllMatchingInfoAsync(net::CookieDeletionInfo delete_info,DeleteCallback callback)164 void DelayedCookieMonster::DeleteAllMatchingInfoAsync(
165     net::CookieDeletionInfo delete_info,
166     DeleteCallback callback) {
167   ADD_FAILURE();
168 }
169 
DeleteMatchingCookiesAsync(DeletePredicate,DeleteCallback)170 void DelayedCookieMonster::DeleteMatchingCookiesAsync(DeletePredicate,
171                                                       DeleteCallback) {
172   ADD_FAILURE();
173 }
174 
DeleteSessionCookiesAsync(DeleteCallback)175 void DelayedCookieMonster::DeleteSessionCookiesAsync(DeleteCallback) {
176   ADD_FAILURE();
177 }
178 
FlushStore(base::OnceClosure callback)179 void DelayedCookieMonster::FlushStore(base::OnceClosure callback) {
180   ADD_FAILURE();
181 }
182 
GetChangeDispatcher()183 CookieChangeDispatcher& DelayedCookieMonster::GetChangeDispatcher() {
184   return change_dispatcher_;
185 }
186 
SetCookieableSchemes(const std::vector<std::string> & schemes,SetCookieableSchemesCallback callback)187 void DelayedCookieMonster::SetCookieableSchemes(
188     const std::vector<std::string>& schemes,
189     SetCookieableSchemesCallback callback) {
190   ADD_FAILURE();
191 }
192 
193 //
194 // CookieURLHelper
195 //
CookieURLHelper(const std::string & url_string)196 CookieURLHelper::CookieURLHelper(const std::string& url_string)
197     : url_(url_string),
198       registry_(GetRegistry(url_)),
199       domain_and_registry_(
200           GetDomainAndRegistry(url_, INCLUDE_PRIVATE_REGISTRIES)) {}
201 
AppendPath(const std::string & path) const202 const GURL CookieURLHelper::AppendPath(const std::string& path) const {
203   return GURL(url_.spec() + path);
204 }
205 
Format(const std::string & format_string) const206 std::string CookieURLHelper::Format(const std::string& format_string) const {
207   std::string new_string = format_string;
208   base::ReplaceSubstringsAfterOffset(&new_string, 0, "%D",
209                                      domain_and_registry_);
210   base::ReplaceSubstringsAfterOffset(&new_string, 0, "%R", registry_);
211   return new_string;
212 }
213 
214 //
215 // FlushablePersistentStore
216 //
217 FlushablePersistentStore::FlushablePersistentStore() = default;
218 
Load(LoadedCallback loaded_callback,const NetLogWithSource &)219 void FlushablePersistentStore::Load(LoadedCallback loaded_callback,
220                                     const NetLogWithSource& /* net_log */) {
221   std::vector<std::unique_ptr<CanonicalCookie>> out_cookies;
222   base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
223       FROM_HERE,
224       base::BindOnce(std::move(loaded_callback), std::move(out_cookies)));
225 }
226 
LoadCookiesForKey(const std::string & key,LoadedCallback loaded_callback)227 void FlushablePersistentStore::LoadCookiesForKey(
228     const std::string& key,
229     LoadedCallback loaded_callback) {
230   Load(std::move(loaded_callback), NetLogWithSource());
231 }
232 
AddCookie(const CanonicalCookie &)233 void FlushablePersistentStore::AddCookie(const CanonicalCookie&) {}
234 
UpdateCookieAccessTime(const CanonicalCookie &)235 void FlushablePersistentStore::UpdateCookieAccessTime(const CanonicalCookie&) {}
236 
DeleteCookie(const CanonicalCookie &)237 void FlushablePersistentStore::DeleteCookie(const CanonicalCookie&) {}
238 
SetForceKeepSessionState()239 void FlushablePersistentStore::SetForceKeepSessionState() {}
240 
SetBeforeCommitCallback(base::RepeatingClosure callback)241 void FlushablePersistentStore::SetBeforeCommitCallback(
242     base::RepeatingClosure callback) {}
243 
Flush(base::OnceClosure callback)244 void FlushablePersistentStore::Flush(base::OnceClosure callback) {
245   base::AutoLock lock(flush_count_lock_);
246   ++flush_count_;
247   std::move(callback).Run();
248 }
249 
flush_count()250 int FlushablePersistentStore::flush_count() {
251   base::AutoLock lock(flush_count_lock_);
252   return flush_count_;
253 }
254 
255 FlushablePersistentStore::~FlushablePersistentStore() = default;
256 
257 //
258 // CallbackCounter
259 //
260 CallbackCounter::CallbackCounter() = default;
261 
Callback()262 void CallbackCounter::Callback() {
263   base::AutoLock lock(callback_count_lock_);
264   ++callback_count_;
265 }
266 
callback_count()267 int CallbackCounter::callback_count() {
268   base::AutoLock lock(callback_count_lock_);
269   return callback_count_;
270 }
271 
272 CallbackCounter::~CallbackCounter() = default;
273 
FutureCookieExpirationString()274 std::string FutureCookieExpirationString() {
275   return "; expires=" +
276          HttpUtil::TimeFormatHTTP(base::Time::Now() + base::Days(365));
277 }
278 
279 }  // namespace net
280