blob: a3418fda3c501739328c4efb3775fe58ee2197ff [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"
Matt Menke5577421a2019-10-10 00:04:4920#include "net/base/network_isolation_key.h"
juliatuttle586843332017-03-27 16:22:3721#include "net/reporting/reporting_cache.h"
juliatuttleee4b55e2017-04-07 17:09:4522#include "net/reporting/reporting_context.h"
juliatuttlefcf47202017-05-23 15:53:0223#include "net/reporting/reporting_delegate.h"
juliatuttle830962a2017-04-19 17:50:0424#include "net/reporting/reporting_delivery_agent.h"
Lily Chenfc92ff42019-05-06 22:59:1025#include "net/reporting/reporting_endpoint.h"
juliatuttle9f970c02017-04-10 19:26:3726#include "net/reporting/reporting_garbage_collector.h"
juliatuttleee4b55e2017-04-07 17:09:4527#include "net/reporting/reporting_policy.h"
28#include "net/reporting/reporting_uploader.h"
29#include "testing/gtest/include/gtest/gtest.h"
juliatuttle586843332017-03-27 16:22:3730#include "url/gurl.h"
31#include "url/origin.h"
32
33namespace net {
34
juliatuttleee4b55e2017-04-07 17:09:4535namespace {
36
37class PendingUploadImpl : public TestReportingUploader::PendingUpload {
38 public:
Douglas Creager3428d812018-07-13 03:59:5639 PendingUploadImpl(const url::Origin& report_origin,
40 const GURL& url,
Matt Menke5577421a2019-10-10 00:04:4941 const NetworkIsolationKey& network_isolation_key,
Julia Tuttleed8d84b2017-12-05 18:54:5342 const std::string& json,
43 ReportingUploader::UploadCallback callback,
44 base::OnceCallback<void(PendingUpload*)> complete_callback)
Douglas Creager3428d812018-07-13 03:59:5645 : report_origin_(report_origin),
46 url_(url),
Matt Menke5577421a2019-10-10 00:04:4947 network_isolation_key_(network_isolation_key),
juliatuttleee4b55e2017-04-07 17:09:4548 json_(json),
Julia Tuttleed8d84b2017-12-05 18:54:5349 callback_(std::move(callback)),
50 complete_callback_(std::move(complete_callback)) {}
juliatuttleee4b55e2017-04-07 17:09:4551
Chris Watkins806691b2017-12-01 06:01:2252 ~PendingUploadImpl() override = default;
juliatuttleee4b55e2017-04-07 17:09:4553
Julia Tuttle107e30672018-03-29 18:48:4254 // PendingUpload implementation:
Douglas Creager3428d812018-07-13 03:59:5655 const url::Origin& report_origin() const override { return report_origin_; }
juliatuttleee4b55e2017-04-07 17:09:4556 const GURL& url() const override { return url_; }
57 const std::string& json() const override { return json_; }
58 std::unique_ptr<base::Value> GetValue() const override {
Lei Zhanga8b4c5fb2019-02-16 03:02:0359 return base::JSONReader::ReadDeprecated(json_);
juliatuttleee4b55e2017-04-07 17:09:4560 }
61
62 void Complete(ReportingUploader::Outcome outcome) override {
Julia Tuttleed8d84b2017-12-05 18:54:5363 std::move(callback_).Run(outcome);
juliatuttleee4b55e2017-04-07 17:09:4564 // Deletes |this|.
Julia Tuttleed8d84b2017-12-05 18:54:5365 std::move(complete_callback_).Run(this);
juliatuttleee4b55e2017-04-07 17:09:4566 }
67
68 private:
Douglas Creager3428d812018-07-13 03:59:5669 url::Origin report_origin_;
juliatuttleee4b55e2017-04-07 17:09:4570 GURL url_;
Matt Menke5577421a2019-10-10 00:04:4971 NetworkIsolationKey network_isolation_key_;
juliatuttleee4b55e2017-04-07 17:09:4572 std::string json_;
Julia Tuttleed8d84b2017-12-05 18:54:5373 ReportingUploader::UploadCallback callback_;
74 base::OnceCallback<void(PendingUpload*)> complete_callback_;
juliatuttleee4b55e2017-04-07 17:09:4575};
76
77void ErasePendingUpload(
78 std::vector<std::unique_ptr<TestReportingUploader::PendingUpload>>* uploads,
79 TestReportingUploader::PendingUpload* upload) {
80 for (auto it = uploads->begin(); it != uploads->end(); ++it) {
81 if (it->get() == upload) {
82 uploads->erase(it);
83 return;
84 }
85 }
86 NOTREACHED();
87}
88
89} // namespace
90
Matt Menke515136d2019-10-05 00:27:0991RandIntCallback TestReportingRandIntCallback() {
92 return base::BindRepeating(
93 [](int* rand_counter, int min, int max) {
94 DCHECK_LE(min, max);
95 return min + ((*rand_counter)++ % (max - min + 1));
96 },
97 base::Owned(std::make_unique<int>(0)));
98}
99
Chris Watkins806691b2017-12-01 06:01:22100TestReportingUploader::PendingUpload::~PendingUpload() = default;
101TestReportingUploader::PendingUpload::PendingUpload() = default;
juliatuttleee4b55e2017-04-07 17:09:45102
Chris Watkins806691b2017-12-01 06:01:22103TestReportingUploader::TestReportingUploader() = default;
104TestReportingUploader::~TestReportingUploader() = default;
juliatuttleee4b55e2017-04-07 17:09:45105
Matt Menke5577421a2019-10-10 00:04:49106void TestReportingUploader::StartUpload(
107 const url::Origin& report_origin,
108 const GURL& url,
109 const NetworkIsolationKey& network_isolation_key,
110 const std::string& json,
111 int max_depth,
112 UploadCallback callback) {
Jeremy Roman0579ed62017-08-29 15:56:19113 pending_uploads_.push_back(std::make_unique<PendingUploadImpl>(
Matt Menke5577421a2019-10-10 00:04:49114 report_origin, url, network_isolation_key, json, std::move(callback),
Julia Tuttleed8d84b2017-12-05 18:54:53115 base::BindOnce(&ErasePendingUpload, &pending_uploads_)));
juliatuttleee4b55e2017-04-07 17:09:45116}
117
Lily Chen91b51e62019-03-01 16:46:07118void TestReportingUploader::OnShutdown() {
119 pending_uploads_.clear();
120}
121
122int TestReportingUploader::GetPendingUploadCountForTesting() const {
123 return pending_uploads_.size();
124}
125
Matt Menke515136d2019-10-05 00:27:09126TestReportingDelegate::TestReportingDelegate() = default;
juliatuttlefcf47202017-05-23 15:53:02127
Chris Watkins806691b2017-12-01 06:01:22128TestReportingDelegate::~TestReportingDelegate() = default;
juliatuttlefcf47202017-05-23 15:53:02129
130bool TestReportingDelegate::CanQueueReport(const url::Origin& origin) const {
131 return true;
132}
133
Douglas Creager7b07ea42018-02-27 21:08:08134void TestReportingDelegate::CanSendReports(
135 std::set<url::Origin> origins,
136 base::OnceCallback<void(std::set<url::Origin>)> result_callback) const {
Douglas Creager66494e12018-03-05 22:21:00137 if (pause_permissions_check_) {
138 saved_origins_ = std::move(origins);
139 permissions_check_callback_ = std::move(result_callback);
140 return;
141 }
142
Douglas Creager7b07ea42018-02-27 21:08:08143 if (disallow_report_uploads_)
144 origins.clear();
145 std::move(result_callback).Run(std::move(origins));
juliatuttlefcf47202017-05-23 15:53:02146}
147
Douglas Creager66494e12018-03-05 22:21:00148bool TestReportingDelegate::PermissionsCheckPaused() const {
149 return !permissions_check_callback_.is_null();
150}
151
152void TestReportingDelegate::ResumePermissionsCheck() {
153 if (disallow_report_uploads_)
154 saved_origins_.clear();
155 std::move(permissions_check_callback_).Run(std::move(saved_origins_));
156}
157
juliatuttlefcf47202017-05-23 15:53:02158bool TestReportingDelegate::CanSetClient(const url::Origin& origin,
159 const GURL& endpoint) const {
160 return true;
161}
162
163bool TestReportingDelegate::CanUseClient(const url::Origin& origin,
164 const GURL& endpoint) const {
165 return true;
166}
167
Lily Chen731a95c2019-05-06 22:27:22168TestReportingContext::TestReportingContext(
169 base::Clock* clock,
170 const base::TickClock* tick_clock,
171 const ReportingPolicy& policy,
172 ReportingCache::PersistentReportingStore* store)
Matt Menke515136d2019-10-05 00:27:09173 : ReportingContext(policy,
174 clock,
175 tick_clock,
176 TestReportingRandIntCallback(),
177 std::make_unique<TestReportingUploader>(),
178 std::make_unique<TestReportingDelegate>(),
179 store),
tzik35945aa2018-06-22 00:33:26180 delivery_timer_(new base::MockOneShotTimer()),
181 garbage_collection_timer_(new base::MockOneShotTimer()) {
juliatuttle9f970c02017-04-10 19:26:37182 garbage_collector()->SetTimerForTesting(
183 base::WrapUnique(garbage_collection_timer_));
juliatuttle830962a2017-04-19 17:50:04184 delivery_agent()->SetTimerForTesting(base::WrapUnique(delivery_timer_));
juliatuttle9f970c02017-04-10 19:26:37185}
juliatuttleee4b55e2017-04-07 17:09:45186
juliatuttle9f970c02017-04-10 19:26:37187TestReportingContext::~TestReportingContext() {
juliatuttle830962a2017-04-19 17:50:04188 delivery_timer_ = nullptr;
juliatuttle9f970c02017-04-10 19:26:37189 garbage_collection_timer_ = nullptr;
190}
juliatuttleee4b55e2017-04-07 17:09:45191
Lily Chen731a95c2019-05-06 22:27:22192ReportingTestBase::ReportingTestBase() : store_(nullptr) {
juliatuttleee4b55e2017-04-07 17:09:45193 // For tests, disable jitter.
194 ReportingPolicy policy;
195 policy.endpoint_backoff_policy.jitter_factor = 0.0;
juliatuttle7da13812017-04-13 19:18:19196
juliatuttle42c57312017-04-28 03:01:30197 CreateContext(policy, base::Time::Now(), base::TimeTicks::Now());
juliatuttleee4b55e2017-04-07 17:09:45198}
199
Chris Watkins806691b2017-12-01 06:01:22200ReportingTestBase::~ReportingTestBase() = default;
juliatuttleee4b55e2017-04-07 17:09:45201
juliatuttle7da13812017-04-13 19:18:19202void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) {
juliatuttle42c57312017-04-28 03:01:30203 CreateContext(new_policy, clock()->Now(), tick_clock()->NowTicks());
juliatuttle7da13812017-04-13 19:18:19204}
205
Lily Chen731a95c2019-05-06 22:27:22206void ReportingTestBase::UseStore(
207 ReportingCache::PersistentReportingStore* store) {
208 store_ = store;
209 CreateContext(policy(), clock()->Now(), tick_clock()->NowTicks());
210}
211
Lily Chenfc92ff42019-05-06 22:59:10212const ReportingEndpoint ReportingTestBase::FindEndpointInCache(
Lily Chenad5dd0802020-03-10 21:58:09213 const ReportingEndpointGroupKey& group_key,
Lily Chenefb6fcf2019-04-19 04:17:54214 const GURL& url) {
Lily Chenad5dd0802020-03-10 21:58:09215 return cache()->GetEndpointForTesting(group_key, url);
Lily Chenefb6fcf2019-04-19 04:17:54216}
217
Lily Chenad5dd0802020-03-10 21:58:09218bool ReportingTestBase::SetEndpointInCache(
219 const ReportingEndpointGroupKey& group_key,
220 const GURL& url,
221 base::Time expires,
222 OriginSubdomains include_subdomains,
223 int priority,
224 int weight) {
225 cache()->SetEndpointForTesting(group_key, url, include_subdomains, expires,
226 priority, weight);
227 const ReportingEndpoint endpoint = FindEndpointInCache(group_key, url);
Lily Chenefb6fcf2019-04-19 04:17:54228 return endpoint.is_valid();
229}
230
Lily Chenad5dd0802020-03-10 21:58:09231bool ReportingTestBase::EndpointExistsInCache(
232 const ReportingEndpointGroupKey& group_key,
233 const GURL& url) {
234 ReportingEndpoint endpoint = cache()->GetEndpointForTesting(group_key, url);
Lily Chenefb6fcf2019-04-19 04:17:54235 return endpoint.is_valid();
236}
237
Lily Chenfc92ff42019-05-06 22:59:10238ReportingEndpoint::Statistics ReportingTestBase::GetEndpointStatistics(
Lily Chenad5dd0802020-03-10 21:58:09239 const ReportingEndpointGroupKey& group_key,
Lily Chenefb6fcf2019-04-19 04:17:54240 const GURL& url) {
Lily Chenad5dd0802020-03-10 21:58:09241 ReportingEndpoint endpoint = cache()->GetEndpointForTesting(group_key, url);
Lily Chenefb6fcf2019-04-19 04:17:54242 if (endpoint)
243 return endpoint.stats;
Lily Chenfc92ff42019-05-06 22:59:10244 return ReportingEndpoint::Statistics();
Lily Chenefb6fcf2019-04-19 04:17:54245}
246
247bool ReportingTestBase::EndpointGroupExistsInCache(
Lily Chenad5dd0802020-03-10 21:58:09248 const ReportingEndpointGroupKey& group_key,
Lily Chenefb6fcf2019-04-19 04:17:54249 OriginSubdomains include_subdomains,
250 base::Time expires) {
Lily Chenad5dd0802020-03-10 21:58:09251 return cache()->EndpointGroupExistsForTesting(group_key, include_subdomains,
252 expires);
Lily Chenefb6fcf2019-04-19 04:17:54253}
254
Lily Chenad5dd0802020-03-10 21:58:09255bool ReportingTestBase::ClientExistsInCacheForOrigin(
256 const url::Origin& origin) {
257 std::set<url::Origin> all_origins = cache()->GetAllOrigins();
258 return all_origins.find(origin) != all_origins.end();
Lily Chenefb6fcf2019-04-19 04:17:54259}
260
261GURL ReportingTestBase::MakeURL(size_t index) {
262 return GURL(base::StringPrintf("https://example%zd.test", index));
263}
264
juliatuttle7da13812017-04-13 19:18:19265void ReportingTestBase::SimulateRestart(base::TimeDelta delta,
266 base::TimeDelta delta_ticks) {
juliatuttle42c57312017-04-28 03:01:30267 CreateContext(policy(), clock()->Now() + delta,
268 tick_clock()->NowTicks() + delta_ticks);
juliatuttle7da13812017-04-13 19:18:19269}
270
juliatuttle42c57312017-04-28 03:01:30271void ReportingTestBase::CreateContext(const ReportingPolicy& policy,
272 base::Time now,
273 base::TimeTicks now_ticks) {
Lily Chen731a95c2019-05-06 22:27:22274 context_ = std::make_unique<TestReportingContext>(&clock_, &tick_clock_,
275 policy, store_);
juliatuttle7da13812017-04-13 19:18:19276 clock()->SetNow(now);
277 tick_clock()->SetNowTicks(now_ticks);
juliatuttleee4b55e2017-04-07 17:09:45278}
279
280base::TimeTicks ReportingTestBase::yesterday() {
281 return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1);
282}
283
juliatuttle830962a2017-04-19 17:50:04284base::TimeTicks ReportingTestBase::now() {
285 return tick_clock()->NowTicks();
286}
287
juliatuttleee4b55e2017-04-07 17:09:45288base::TimeTicks ReportingTestBase::tomorrow() {
289 return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1);
290}
291
Lily Chen8a9e53722019-04-26 15:16:02292TestReportingService::Report::Report() = default;
293
294TestReportingService::Report::Report(Report&& other)
295 : url(other.url),
296 user_agent(other.user_agent),
297 group(other.group),
298 type(other.type),
299 body(std::move(other.body)),
300 depth(other.depth) {}
301
Matt Menkedd2ed7a2020-10-22 04:09:20302TestReportingService::Report::Report(
303 const GURL& url,
304 const NetworkIsolationKey& network_isolation_key,
305 const std::string& user_agent,
306 const std::string& group,
307 const std::string& type,
308 std::unique_ptr<const base::Value> body,
309 int depth)
Lily Chen8a9e53722019-04-26 15:16:02310 : url(url),
Matt Menkedd2ed7a2020-10-22 04:09:20311 network_isolation_key(network_isolation_key),
Lily Chen8a9e53722019-04-26 15:16:02312 user_agent(user_agent),
313 group(group),
314 type(type),
315 body(std::move(body)),
316 depth(depth) {}
317
318TestReportingService::Report::~Report() = default;
319
320TestReportingService::TestReportingService() = default;
321
322TestReportingService::~TestReportingService() = default;
323
Matt Menkedd2ed7a2020-10-22 04:09:20324void TestReportingService::QueueReport(
325 const GURL& url,
326 const NetworkIsolationKey& network_isolation_key,
327 const std::string& user_agent,
328 const std::string& group,
329 const std::string& type,
330 std::unique_ptr<const base::Value> body,
331 int depth) {
332 reports_.emplace_back(Report(url, network_isolation_key, user_agent, group,
333 type, std::move(body), depth));
Lily Chen8a9e53722019-04-26 15:16:02334}
335
Matt Menke17b23c42020-10-22 05:14:57336void TestReportingService::ProcessHeader(
337 const GURL& url,
338 const NetworkIsolationKey& network_isolation_key,
339 const std::string& header_value) {
Lily Chen8a9e53722019-04-26 15:16:02340 NOTREACHED();
341}
342
343void TestReportingService::RemoveBrowsingData(
Marcus Pasellc3305c042020-06-03 22:14:36344 uint64_t data_type_mask,
Lily Chen8a9e53722019-04-26 15:16:02345 const base::RepeatingCallback<bool(const GURL&)>& origin_filter) {
346 NOTREACHED();
347}
348
Marcus Pasellc3305c042020-06-03 22:14:36349void TestReportingService::RemoveAllBrowsingData(uint64_t data_type_mask) {
Lily Chen8a9e53722019-04-26 15:16:02350 NOTREACHED();
351}
352
353void TestReportingService::OnShutdown() {}
354
355const ReportingPolicy& TestReportingService::GetPolicy() const {
356 NOTREACHED();
357 return dummy_policy_;
358}
359
360ReportingContext* TestReportingService::GetContextForTesting() const {
361 NOTREACHED();
362 return nullptr;
363}
364
juliatuttle586843332017-03-27 16:22:37365} // namespace net