blob: f39c2820f467741ccaabd6519b019decced998a6 [file] [log] [blame]
juliatuttle586843332017-03-27 16:22:371// Copyright 2017 The Chromium Authors. All rights reserved.
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/reporting/reporting_test_util.h"
6
juliatuttleee4b55e2017-04-07 17:09:457#include <memory>
8#include <string>
juliatuttle586843332017-03-27 16:22:379#include <vector>
10
juliatuttleee4b55e2017-04-07 17:09:4511#include "base/bind.h"
Hans Wennborg0924470b2020-04-27 21:08:0512#include "base/check_op.h"
juliatuttleee4b55e2017-04-07 17:09:4513#include "base/json/json_reader.h"
14#include "base/memory/ptr_util.h"
Hans Wennborg0924470b2020-04-27 21:08:0515#include "base/notreached.h"
Lily Chenefb6fcf2019-04-19 04:17:5416#include "base/strings/stringprintf.h"
juliatuttleee4b55e2017-04-07 17:09:4517#include "base/test/simple_test_clock.h"
18#include "base/test/simple_test_tick_clock.h"
juliatuttle9f970c02017-04-10 19:26:3719#include "base/timer/mock_timer.h"
Ian Clelland52035be2021-10-07 16:38:5020#include "net/base/isolation_info.h"
Matt Menke5577421a2019-10-10 00:04:4921#include "net/base/network_isolation_key.h"
juliatuttle586843332017-03-27 16:22:3722#include "net/reporting/reporting_cache.h"
juliatuttleee4b55e2017-04-07 17:09:4523#include "net/reporting/reporting_context.h"
juliatuttlefcf47202017-05-23 15:53:0224#include "net/reporting/reporting_delegate.h"
juliatuttle830962a2017-04-19 17:50:0425#include "net/reporting/reporting_delivery_agent.h"
Lily Chenfc92ff42019-05-06 22:59:1026#include "net/reporting/reporting_endpoint.h"
juliatuttle9f970c02017-04-10 19:26:3727#include "net/reporting/reporting_garbage_collector.h"
juliatuttleee4b55e2017-04-07 17:09:4528#include "net/reporting/reporting_policy.h"
29#include "net/reporting/reporting_uploader.h"
30#include "testing/gtest/include/gtest/gtest.h"
Matt Menke136ff2e2022-05-31 06:06:5131#include "third_party/abseil-cpp/absl/types/optional.h"
juliatuttle586843332017-03-27 16:22:3732#include "url/gurl.h"
33#include "url/origin.h"
34
35namespace net {
36
juliatuttleee4b55e2017-04-07 17:09:4537namespace {
38
39class PendingUploadImpl : public TestReportingUploader::PendingUpload {
40 public:
Douglas Creager3428d812018-07-13 03:59:5641 PendingUploadImpl(const url::Origin& report_origin,
42 const GURL& url,
Ian Clelland52035be2021-10-07 16:38:5043 const IsolationInfo& isolation_info,
Julia Tuttleed8d84b2017-12-05 18:54:5344 const std::string& json,
45 ReportingUploader::UploadCallback callback,
46 base::OnceCallback<void(PendingUpload*)> complete_callback)
Douglas Creager3428d812018-07-13 03:59:5647 : report_origin_(report_origin),
48 url_(url),
Ian Clelland52035be2021-10-07 16:38:5049 isolation_info_(isolation_info),
juliatuttleee4b55e2017-04-07 17:09:4550 json_(json),
Julia Tuttleed8d84b2017-12-05 18:54:5351 callback_(std::move(callback)),
52 complete_callback_(std::move(complete_callback)) {}
juliatuttleee4b55e2017-04-07 17:09:4553
Chris Watkins806691b2017-12-01 06:01:2254 ~PendingUploadImpl() override = default;
juliatuttleee4b55e2017-04-07 17:09:4555
Julia Tuttle107e30672018-03-29 18:48:4256 // PendingUpload implementation:
Douglas Creager3428d812018-07-13 03:59:5657 const url::Origin& report_origin() const override { return report_origin_; }
juliatuttleee4b55e2017-04-07 17:09:4558 const GURL& url() const override { return url_; }
59 const std::string& json() const override { return json_; }
Matt Menke136ff2e2022-05-31 06:06:5160 absl::optional<base::Value> GetValue() const override {
61 return base::JSONReader::Read(json_);
juliatuttleee4b55e2017-04-07 17:09:4562 }
63
64 void Complete(ReportingUploader::Outcome outcome) override {
Julia Tuttleed8d84b2017-12-05 18:54:5365 std::move(callback_).Run(outcome);
juliatuttleee4b55e2017-04-07 17:09:4566 // Deletes |this|.
Julia Tuttleed8d84b2017-12-05 18:54:5367 std::move(complete_callback_).Run(this);
juliatuttleee4b55e2017-04-07 17:09:4568 }
69
70 private:
Douglas Creager3428d812018-07-13 03:59:5671 url::Origin report_origin_;
juliatuttleee4b55e2017-04-07 17:09:4572 GURL url_;
Ian Clelland52035be2021-10-07 16:38:5073 IsolationInfo isolation_info_;
juliatuttleee4b55e2017-04-07 17:09:4574 std::string json_;
Julia Tuttleed8d84b2017-12-05 18:54:5375 ReportingUploader::UploadCallback callback_;
76 base::OnceCallback<void(PendingUpload*)> complete_callback_;
juliatuttleee4b55e2017-04-07 17:09:4577};
78
79void ErasePendingUpload(
80 std::vector<std::unique_ptr<TestReportingUploader::PendingUpload>>* uploads,
81 TestReportingUploader::PendingUpload* upload) {
82 for (auto it = uploads->begin(); it != uploads->end(); ++it) {
83 if (it->get() == upload) {
84 uploads->erase(it);
85 return;
86 }
87 }
88 NOTREACHED();
89}
90
91} // namespace
92
Matt Menke515136d2019-10-05 00:27:0993RandIntCallback TestReportingRandIntCallback() {
94 return base::BindRepeating(
95 [](int* rand_counter, int min, int max) {
96 DCHECK_LE(min, max);
97 return min + ((*rand_counter)++ % (max - min + 1));
98 },
99 base::Owned(std::make_unique<int>(0)));
100}
101
Chris Watkins806691b2017-12-01 06:01:22102TestReportingUploader::PendingUpload::~PendingUpload() = default;
103TestReportingUploader::PendingUpload::PendingUpload() = default;
juliatuttleee4b55e2017-04-07 17:09:45104
Chris Watkins806691b2017-12-01 06:01:22105TestReportingUploader::TestReportingUploader() = default;
106TestReportingUploader::~TestReportingUploader() = default;
juliatuttleee4b55e2017-04-07 17:09:45107
Ian Clelland52035be2021-10-07 16:38:50108void TestReportingUploader::StartUpload(const url::Origin& report_origin,
109 const GURL& url,
110 const IsolationInfo& isolation_info,
111 const std::string& json,
112 int max_depth,
113 bool eligible_for_credentials,
114 UploadCallback callback) {
Jeremy Roman0579ed62017-08-29 15:56:19115 pending_uploads_.push_back(std::make_unique<PendingUploadImpl>(
Ian Clelland52035be2021-10-07 16:38:50116 report_origin, url, isolation_info, json, std::move(callback),
Julia Tuttleed8d84b2017-12-05 18:54:53117 base::BindOnce(&ErasePendingUpload, &pending_uploads_)));
juliatuttleee4b55e2017-04-07 17:09:45118}
119
Lily Chen91b51e62019-03-01 16:46:07120void TestReportingUploader::OnShutdown() {
121 pending_uploads_.clear();
122}
123
124int TestReportingUploader::GetPendingUploadCountForTesting() const {
125 return pending_uploads_.size();
126}
127
Matt Menke515136d2019-10-05 00:27:09128TestReportingDelegate::TestReportingDelegate() = default;
juliatuttlefcf47202017-05-23 15:53:02129
Chris Watkins806691b2017-12-01 06:01:22130TestReportingDelegate::~TestReportingDelegate() = default;
juliatuttlefcf47202017-05-23 15:53:02131
132bool TestReportingDelegate::CanQueueReport(const url::Origin& origin) const {
133 return true;
134}
135
Douglas Creager7b07ea42018-02-27 21:08:08136void TestReportingDelegate::CanSendReports(
137 std::set<url::Origin> origins,
138 base::OnceCallback<void(std::set<url::Origin>)> result_callback) const {
Douglas Creager66494e12018-03-05 22:21:00139 if (pause_permissions_check_) {
140 saved_origins_ = std::move(origins);
141 permissions_check_callback_ = std::move(result_callback);
142 return;
143 }
144
Douglas Creager7b07ea42018-02-27 21:08:08145 if (disallow_report_uploads_)
146 origins.clear();
147 std::move(result_callback).Run(std::move(origins));
juliatuttlefcf47202017-05-23 15:53:02148}
149
Douglas Creager66494e12018-03-05 22:21:00150bool TestReportingDelegate::PermissionsCheckPaused() const {
151 return !permissions_check_callback_.is_null();
152}
153
154void TestReportingDelegate::ResumePermissionsCheck() {
155 if (disallow_report_uploads_)
156 saved_origins_.clear();
157 std::move(permissions_check_callback_).Run(std::move(saved_origins_));
158}
159
juliatuttlefcf47202017-05-23 15:53:02160bool TestReportingDelegate::CanSetClient(const url::Origin& origin,
161 const GURL& endpoint) const {
162 return true;
163}
164
165bool TestReportingDelegate::CanUseClient(const url::Origin& origin,
166 const GURL& endpoint) const {
167 return true;
168}
169
Lily Chen731a95c2019-05-06 22:27:22170TestReportingContext::TestReportingContext(
171 base::Clock* clock,
172 const base::TickClock* tick_clock,
173 const ReportingPolicy& policy,
174 ReportingCache::PersistentReportingStore* store)
Matt Menke515136d2019-10-05 00:27:09175 : ReportingContext(policy,
176 clock,
177 tick_clock,
178 TestReportingRandIntCallback(),
179 std::make_unique<TestReportingUploader>(),
180 std::make_unique<TestReportingDelegate>(),
Tsuyoshi Horoc39623a82022-07-11 01:27:58181 store) {
182 auto delivery_timer = std::make_unique<base::MockOneShotTimer>();
183 delivery_timer_ = delivery_timer.get();
184 auto garbage_collection_timer = std::make_unique<base::MockOneShotTimer>();
185 garbage_collection_timer_ = garbage_collection_timer.get();
186 garbage_collector()->SetTimerForTesting(std::move(garbage_collection_timer));
187 delivery_agent()->SetTimerForTesting(std::move(delivery_timer));
juliatuttle9f970c02017-04-10 19:26:37188}
juliatuttleee4b55e2017-04-07 17:09:45189
juliatuttle9f970c02017-04-10 19:26:37190TestReportingContext::~TestReportingContext() {
juliatuttle830962a2017-04-19 17:50:04191 delivery_timer_ = nullptr;
juliatuttle9f970c02017-04-10 19:26:37192 garbage_collection_timer_ = nullptr;
193}
juliatuttleee4b55e2017-04-07 17:09:45194
Tsuyoshi Horo432981d52022-06-09 09:50:13195ReportingTestBase::ReportingTestBase() {
juliatuttleee4b55e2017-04-07 17:09:45196 // For tests, disable jitter.
197 ReportingPolicy policy;
198 policy.endpoint_backoff_policy.jitter_factor = 0.0;
juliatuttle7da13812017-04-13 19:18:19199
juliatuttle42c57312017-04-28 03:01:30200 CreateContext(policy, base::Time::Now(), base::TimeTicks::Now());
juliatuttleee4b55e2017-04-07 17:09:45201}
202
Chris Watkins806691b2017-12-01 06:01:22203ReportingTestBase::~ReportingTestBase() = default;
juliatuttleee4b55e2017-04-07 17:09:45204
juliatuttle7da13812017-04-13 19:18:19205void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) {
juliatuttle42c57312017-04-28 03:01:30206 CreateContext(new_policy, clock()->Now(), tick_clock()->NowTicks());
juliatuttle7da13812017-04-13 19:18:19207}
208
Lily Chen731a95c2019-05-06 22:27:22209void ReportingTestBase::UseStore(
210 ReportingCache::PersistentReportingStore* store) {
211 store_ = store;
212 CreateContext(policy(), clock()->Now(), tick_clock()->NowTicks());
213}
214
Lily Chenfc92ff42019-05-06 22:59:10215const ReportingEndpoint ReportingTestBase::FindEndpointInCache(
Lily Chenad5dd0802020-03-10 21:58:09216 const ReportingEndpointGroupKey& group_key,
Lily Chenefb6fcf2019-04-19 04:17:54217 const GURL& url) {
Lily Chenad5dd0802020-03-10 21:58:09218 return cache()->GetEndpointForTesting(group_key, url);
Lily Chenefb6fcf2019-04-19 04:17:54219}
220
Lily Chenad5dd0802020-03-10 21:58:09221bool ReportingTestBase::SetEndpointInCache(
222 const ReportingEndpointGroupKey& group_key,
223 const GURL& url,
224 base::Time expires,
225 OriginSubdomains include_subdomains,
226 int priority,
227 int weight) {
228 cache()->SetEndpointForTesting(group_key, url, include_subdomains, expires,
229 priority, weight);
230 const ReportingEndpoint endpoint = FindEndpointInCache(group_key, url);
Lily Chenefb6fcf2019-04-19 04:17:54231 return endpoint.is_valid();
232}
233
Ian Clellanda52d5472021-08-23 18:33:53234void ReportingTestBase::SetV1EndpointInCache(
235 const ReportingEndpointGroupKey& group_key,
236 const base::UnguessableToken& reporting_source,
Ian Clelland52035be2021-10-07 16:38:50237 const IsolationInfo& isolation_info,
Ian Clellanda52d5472021-08-23 18:33:53238 const GURL& url) {
Ian Clelland52035be2021-10-07 16:38:50239 cache()->SetV1EndpointForTesting(group_key, reporting_source, isolation_info,
240 url);
Ian Clellanda52d5472021-08-23 18:33:53241}
242
Lily Chenad5dd0802020-03-10 21:58:09243bool ReportingTestBase::EndpointExistsInCache(
244 const ReportingEndpointGroupKey& group_key,
245 const GURL& url) {
246 ReportingEndpoint endpoint = cache()->GetEndpointForTesting(group_key, url);
Lily Chenefb6fcf2019-04-19 04:17:54247 return endpoint.is_valid();
248}
249
Lily Chenfc92ff42019-05-06 22:59:10250ReportingEndpoint::Statistics ReportingTestBase::GetEndpointStatistics(
Lily Chenad5dd0802020-03-10 21:58:09251 const ReportingEndpointGroupKey& group_key,
Lily Chenefb6fcf2019-04-19 04:17:54252 const GURL& url) {
Rodney Ding1e758462021-09-16 13:59:07253 ReportingEndpoint endpoint;
254 if (group_key.IsDocumentEndpoint()) {
255 endpoint = cache()->GetV1EndpointForTesting(
256 group_key.reporting_source.value(), group_key.group_name);
257 } else {
258 endpoint = cache()->GetEndpointForTesting(group_key, url);
259 }
Lily Chenefb6fcf2019-04-19 04:17:54260 if (endpoint)
261 return endpoint.stats;
Lily Chenfc92ff42019-05-06 22:59:10262 return ReportingEndpoint::Statistics();
Lily Chenefb6fcf2019-04-19 04:17:54263}
264
265bool ReportingTestBase::EndpointGroupExistsInCache(
Lily Chenad5dd0802020-03-10 21:58:09266 const ReportingEndpointGroupKey& group_key,
Lily Chenefb6fcf2019-04-19 04:17:54267 OriginSubdomains include_subdomains,
268 base::Time expires) {
Lily Chenad5dd0802020-03-10 21:58:09269 return cache()->EndpointGroupExistsForTesting(group_key, include_subdomains,
270 expires);
Lily Chenefb6fcf2019-04-19 04:17:54271}
272
Lily Chenad5dd0802020-03-10 21:58:09273bool ReportingTestBase::ClientExistsInCacheForOrigin(
274 const url::Origin& origin) {
275 std::set<url::Origin> all_origins = cache()->GetAllOrigins();
276 return all_origins.find(origin) != all_origins.end();
Lily Chenefb6fcf2019-04-19 04:17:54277}
278
279GURL ReportingTestBase::MakeURL(size_t index) {
280 return GURL(base::StringPrintf("https://example%zd.test", index));
281}
282
juliatuttle7da13812017-04-13 19:18:19283void ReportingTestBase::SimulateRestart(base::TimeDelta delta,
284 base::TimeDelta delta_ticks) {
juliatuttle42c57312017-04-28 03:01:30285 CreateContext(policy(), clock()->Now() + delta,
286 tick_clock()->NowTicks() + delta_ticks);
juliatuttle7da13812017-04-13 19:18:19287}
288
juliatuttle42c57312017-04-28 03:01:30289void ReportingTestBase::CreateContext(const ReportingPolicy& policy,
290 base::Time now,
291 base::TimeTicks now_ticks) {
Lily Chen731a95c2019-05-06 22:27:22292 context_ = std::make_unique<TestReportingContext>(&clock_, &tick_clock_,
293 policy, store_);
juliatuttle7da13812017-04-13 19:18:19294 clock()->SetNow(now);
295 tick_clock()->SetNowTicks(now_ticks);
juliatuttleee4b55e2017-04-07 17:09:45296}
297
298base::TimeTicks ReportingTestBase::yesterday() {
Peter Kastinge5a38ed2021-10-02 03:06:35299 return tick_clock()->NowTicks() - base::Days(1);
juliatuttleee4b55e2017-04-07 17:09:45300}
301
juliatuttle830962a2017-04-19 17:50:04302base::TimeTicks ReportingTestBase::now() {
303 return tick_clock()->NowTicks();
304}
305
juliatuttleee4b55e2017-04-07 17:09:45306base::TimeTicks ReportingTestBase::tomorrow() {
Peter Kastinge5a38ed2021-10-02 03:06:35307 return tick_clock()->NowTicks() + base::Days(1);
juliatuttleee4b55e2017-04-07 17:09:45308}
309
Lily Chen8a9e53722019-04-26 15:16:02310TestReportingService::Report::Report() = default;
311
Matt Menke643ae972020-10-22 13:47:02312TestReportingService::Report::Report(Report&& other) = default;
Lily Chen8a9e53722019-04-26 15:16:02313
Matt Menkedd2ed7a2020-10-22 04:09:20314TestReportingService::Report::Report(
315 const GURL& url,
316 const NetworkIsolationKey& network_isolation_key,
317 const std::string& user_agent,
318 const std::string& group,
319 const std::string& type,
320 std::unique_ptr<const base::Value> body,
321 int depth)
Lily Chen8a9e53722019-04-26 15:16:02322 : url(url),
Matt Menkedd2ed7a2020-10-22 04:09:20323 network_isolation_key(network_isolation_key),
Lily Chen8a9e53722019-04-26 15:16:02324 user_agent(user_agent),
325 group(group),
326 type(type),
327 body(std::move(body)),
328 depth(depth) {}
329
330TestReportingService::Report::~Report() = default;
331
332TestReportingService::TestReportingService() = default;
333
334TestReportingService::~TestReportingService() = default;
335
Matt Menkedd2ed7a2020-10-22 04:09:20336void TestReportingService::QueueReport(
337 const GURL& url,
Ian Clellande07e64b2021-08-23 16:29:43338 const absl::optional<base::UnguessableToken>& reporting_source,
Matt Menkedd2ed7a2020-10-22 04:09:20339 const NetworkIsolationKey& network_isolation_key,
340 const std::string& user_agent,
341 const std::string& group,
342 const std::string& type,
Matt Menked0af7b62022-06-07 17:09:31343 base::Value::Dict body,
Matt Menkedd2ed7a2020-10-22 04:09:20344 int depth) {
Matt Menked0af7b62022-06-07 17:09:31345 reports_.emplace_back(
346 Report(url, network_isolation_key, user_agent, group, type,
347 std::make_unique<base::Value>(std::move(body)), depth));
Lily Chen8a9e53722019-04-26 15:16:02348}
349
Rodney Ding329e4bb2021-03-19 22:21:53350void TestReportingService::ProcessReportToHeader(
Matt Menkee4399022021-10-21 18:04:42351 const url::Origin& origin,
Matt Menke17b23c42020-10-22 05:14:57352 const NetworkIsolationKey& network_isolation_key,
353 const std::string& header_value) {
Lily Chen8a9e53722019-04-26 15:16:02354 NOTREACHED();
355}
356
357void TestReportingService::RemoveBrowsingData(
Marcus Pasellc3305c042020-06-03 22:14:36358 uint64_t data_type_mask,
Kunihiko Sakamotoe167a952022-04-13 00:24:22359 const base::RepeatingCallback<bool(const url::Origin&)>& origin_filter) {
Lily Chen8a9e53722019-04-26 15:16:02360 NOTREACHED();
361}
362
Marcus Pasellc3305c042020-06-03 22:14:36363void TestReportingService::RemoveAllBrowsingData(uint64_t data_type_mask) {
Lily Chen8a9e53722019-04-26 15:16:02364 NOTREACHED();
365}
366
367void TestReportingService::OnShutdown() {}
368
369const ReportingPolicy& TestReportingService::GetPolicy() const {
370 NOTREACHED();
371 return dummy_policy_;
372}
373
374ReportingContext* TestReportingService::GetContextForTesting() const {
375 NOTREACHED();
376 return nullptr;
377}
378
Wolfgang Beyer14d8d41b82021-08-17 15:52:07379std::vector<const ReportingReport*> TestReportingService::GetReports() const {
380 NOTREACHED();
381 return std::vector<const ReportingReport*>();
382}
383
Wolfgang Beyerba1bf702021-11-15 12:49:47384base::flat_map<url::Origin, std::vector<ReportingEndpoint>>
385TestReportingService::GetV1ReportingEndpointsByOrigin() const {
386 NOTREACHED();
387 return base::flat_map<url::Origin, std::vector<ReportingEndpoint>>();
388}
389
Wolfgang Beyer14d8d41b82021-08-17 15:52:07390void TestReportingService::AddReportingCacheObserver(
391 ReportingCacheObserver* observer) {
392 NOTREACHED();
393}
394
395void TestReportingService::RemoveReportingCacheObserver(
396 ReportingCacheObserver* observer) {
397 NOTREACHED();
398}
399
juliatuttle586843332017-03-27 16:22:37400} // namespace net