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_(
78 CookieAccessResult(CookieInclusionStatus::MakeFromReasonsForTesting(
79 /*exclusions=*/{
80 CookieInclusionStatus::EXCLUDE_FAILURE_TO_STORE}))) {}
81
82 DelayedCookieMonster::~DelayedCookieMonster() = default;
83
SetCookiesInternalCallback(CookieAccessResult result)84 void DelayedCookieMonster::SetCookiesInternalCallback(
85 CookieAccessResult result) {
86 result_ = result;
87 did_run_ = true;
88 }
89
GetCookieListWithOptionsInternalCallback(const CookieAccessResultList & cookie_list,const CookieAccessResultList & excluded_cookies)90 void DelayedCookieMonster::GetCookieListWithOptionsInternalCallback(
91 const CookieAccessResultList& cookie_list,
92 const CookieAccessResultList& excluded_cookies) {
93 cookie_access_result_list_ = cookie_list;
94 cookie_list_ = cookie_util::StripAccessResults(cookie_access_result_list_);
95 did_run_ = true;
96 }
97
SetCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie,const GURL & source_url,const CookieOptions & options,SetCookiesCallback callback,std::optional<CookieAccessResult> cookie_access_result)98 void DelayedCookieMonster::SetCanonicalCookieAsync(
99 std::unique_ptr<CanonicalCookie> cookie,
100 const GURL& source_url,
101 const CookieOptions& options,
102 SetCookiesCallback callback,
103 std::optional<CookieAccessResult> cookie_access_result) {
104 did_run_ = false;
105 cookie_monster_->SetCanonicalCookieAsync(
106 std::move(cookie), source_url, options,
107 base::BindOnce(&DelayedCookieMonster::SetCookiesInternalCallback,
108 base::Unretained(this)),
109 std::move(cookie_access_result));
110 DCHECK_EQ(did_run_, true);
111 base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
112 FROM_HERE,
113 base::BindOnce(&DelayedCookieMonster::InvokeSetCookiesCallback,
114 base::Unretained(this), std::move(callback)),
115 base::Milliseconds(kDelayedTime));
116 }
117
GetCookieListWithOptionsAsync(const GURL & url,const CookieOptions & options,const CookiePartitionKeyCollection & cookie_partition_key_collection,CookieMonster::GetCookieListCallback callback)118 void DelayedCookieMonster::GetCookieListWithOptionsAsync(
119 const GURL& url,
120 const CookieOptions& options,
121 const CookiePartitionKeyCollection& cookie_partition_key_collection,
122 CookieMonster::GetCookieListCallback callback) {
123 did_run_ = false;
124 cookie_monster_->GetCookieListWithOptionsAsync(
125 url, options, cookie_partition_key_collection,
126 base::BindOnce(
127 &DelayedCookieMonster::GetCookieListWithOptionsInternalCallback,
128 base::Unretained(this)));
129 DCHECK_EQ(did_run_, true);
130 base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
131 FROM_HERE,
132 base::BindOnce(&DelayedCookieMonster::InvokeGetCookieListCallback,
133 base::Unretained(this), std::move(callback)),
134 base::Milliseconds(kDelayedTime));
135 }
136
GetAllCookiesAsync(GetAllCookiesCallback callback)137 void DelayedCookieMonster::GetAllCookiesAsync(GetAllCookiesCallback callback) {
138 cookie_monster_->GetAllCookiesAsync(std::move(callback));
139 }
140
InvokeSetCookiesCallback(CookieMonster::SetCookiesCallback callback)141 void DelayedCookieMonster::InvokeSetCookiesCallback(
142 CookieMonster::SetCookiesCallback callback) {
143 if (!callback.is_null())
144 std::move(callback).Run(result_);
145 }
146
InvokeGetCookieListCallback(CookieMonster::GetCookieListCallback callback)147 void DelayedCookieMonster::InvokeGetCookieListCallback(
148 CookieMonster::GetCookieListCallback callback) {
149 if (!callback.is_null())
150 std::move(callback).Run(cookie_access_result_list_,
151 CookieAccessResultList());
152 }
153
DeleteCanonicalCookieAsync(const CanonicalCookie & cookie,DeleteCallback callback)154 void DelayedCookieMonster::DeleteCanonicalCookieAsync(
155 const CanonicalCookie& cookie,
156 DeleteCallback callback) {
157 ADD_FAILURE();
158 }
159
DeleteAllCreatedInTimeRangeAsync(const TimeRange & creation_range,DeleteCallback callback)160 void DelayedCookieMonster::DeleteAllCreatedInTimeRangeAsync(
161 const TimeRange& creation_range,
162 DeleteCallback callback) {
163 ADD_FAILURE();
164 }
165
DeleteAllMatchingInfoAsync(net::CookieDeletionInfo delete_info,DeleteCallback callback)166 void DelayedCookieMonster::DeleteAllMatchingInfoAsync(
167 net::CookieDeletionInfo delete_info,
168 DeleteCallback callback) {
169 ADD_FAILURE();
170 }
171
DeleteMatchingCookiesAsync(DeletePredicate,DeleteCallback)172 void DelayedCookieMonster::DeleteMatchingCookiesAsync(DeletePredicate,
173 DeleteCallback) {
174 ADD_FAILURE();
175 }
176
DeleteSessionCookiesAsync(DeleteCallback)177 void DelayedCookieMonster::DeleteSessionCookiesAsync(DeleteCallback) {
178 ADD_FAILURE();
179 }
180
FlushStore(base::OnceClosure callback)181 void DelayedCookieMonster::FlushStore(base::OnceClosure callback) {
182 ADD_FAILURE();
183 }
184
GetChangeDispatcher()185 CookieChangeDispatcher& DelayedCookieMonster::GetChangeDispatcher() {
186 return change_dispatcher_;
187 }
188
SetCookieableSchemes(const std::vector<std::string> & schemes,SetCookieableSchemesCallback callback)189 void DelayedCookieMonster::SetCookieableSchemes(
190 const std::vector<std::string>& schemes,
191 SetCookieableSchemesCallback callback) {
192 ADD_FAILURE();
193 }
194
195 //
196 // CookieURLHelper
197 //
CookieURLHelper(const std::string & url_string)198 CookieURLHelper::CookieURLHelper(const std::string& url_string)
199 : url_(url_string),
200 registry_(GetRegistry(url_)),
201 domain_and_registry_(
202 GetDomainAndRegistry(url_, INCLUDE_PRIVATE_REGISTRIES)) {}
203
AppendPath(const std::string & path) const204 const GURL CookieURLHelper::AppendPath(const std::string& path) const {
205 return GURL(url_.spec() + path);
206 }
207
Format(const std::string & format_string) const208 std::string CookieURLHelper::Format(const std::string& format_string) const {
209 std::string new_string = format_string;
210 base::ReplaceSubstringsAfterOffset(&new_string, 0, "%D",
211 domain_and_registry_);
212 base::ReplaceSubstringsAfterOffset(&new_string, 0, "%R", registry_);
213 return new_string;
214 }
215
216 //
217 // FlushablePersistentStore
218 //
219 FlushablePersistentStore::FlushablePersistentStore() = default;
220
Load(LoadedCallback loaded_callback,const NetLogWithSource &)221 void FlushablePersistentStore::Load(LoadedCallback loaded_callback,
222 const NetLogWithSource& /* net_log */) {
223 std::vector<std::unique_ptr<CanonicalCookie>> out_cookies;
224 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
225 FROM_HERE,
226 base::BindOnce(std::move(loaded_callback), std::move(out_cookies)));
227 }
228
LoadCookiesForKey(const std::string & key,LoadedCallback loaded_callback)229 void FlushablePersistentStore::LoadCookiesForKey(
230 const std::string& key,
231 LoadedCallback loaded_callback) {
232 Load(std::move(loaded_callback), NetLogWithSource());
233 }
234
AddCookie(const CanonicalCookie &)235 void FlushablePersistentStore::AddCookie(const CanonicalCookie&) {}
236
UpdateCookieAccessTime(const CanonicalCookie &)237 void FlushablePersistentStore::UpdateCookieAccessTime(const CanonicalCookie&) {}
238
DeleteCookie(const CanonicalCookie &)239 void FlushablePersistentStore::DeleteCookie(const CanonicalCookie&) {}
240
SetForceKeepSessionState()241 void FlushablePersistentStore::SetForceKeepSessionState() {}
242
SetBeforeCommitCallback(base::RepeatingClosure callback)243 void FlushablePersistentStore::SetBeforeCommitCallback(
244 base::RepeatingClosure callback) {}
245
Flush(base::OnceClosure callback)246 void FlushablePersistentStore::Flush(base::OnceClosure callback) {
247 base::AutoLock lock(flush_count_lock_);
248 ++flush_count_;
249 std::move(callback).Run();
250 }
251
flush_count()252 int FlushablePersistentStore::flush_count() {
253 base::AutoLock lock(flush_count_lock_);
254 return flush_count_;
255 }
256
257 FlushablePersistentStore::~FlushablePersistentStore() = default;
258
259 //
260 // CallbackCounter
261 //
262 CallbackCounter::CallbackCounter() = default;
263
Callback()264 void CallbackCounter::Callback() {
265 base::AutoLock lock(callback_count_lock_);
266 ++callback_count_;
267 }
268
callback_count()269 int CallbackCounter::callback_count() {
270 base::AutoLock lock(callback_count_lock_);
271 return callback_count_;
272 }
273
274 CallbackCounter::~CallbackCounter() = default;
275
FutureCookieExpirationString()276 std::string FutureCookieExpirationString() {
277 return "; expires=" +
278 HttpUtil::TimeFormatHTTP(base::Time::Now() + base::Days(365));
279 }
280
281 } // namespace net
282