juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 1 | // 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 | |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <string> |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 11 | #include "base/bind.h" |
| 12 | #include "base/json/json_reader.h" |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 13 | #include "base/logging.h" |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 14 | #include "base/memory/ptr_util.h" |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame^] | 15 | #include "base/strings/stringprintf.h" |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 16 | #include "base/test/simple_test_clock.h" |
| 17 | #include "base/test/simple_test_tick_clock.h" |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 18 | #include "base/timer/mock_timer.h" |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 19 | #include "net/base/rand_callback.h" |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 20 | #include "net/reporting/reporting_cache.h" |
| 21 | #include "net/reporting/reporting_client.h" |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 22 | #include "net/reporting/reporting_context.h" |
juliatuttle | fcf4720 | 2017-05-23 15:53:02 | [diff] [blame] | 23 | #include "net/reporting/reporting_delegate.h" |
juliatuttle | 830962a | 2017-04-19 17:50:04 | [diff] [blame] | 24 | #include "net/reporting/reporting_delivery_agent.h" |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 25 | #include "net/reporting/reporting_garbage_collector.h" |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 26 | #include "net/reporting/reporting_policy.h" |
| 27 | #include "net/reporting/reporting_uploader.h" |
Julia Tuttle | 7d87494 | 2018-03-02 01:19:13 | [diff] [blame] | 28 | #include "net/url_request/url_request_test_util.h" |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 29 | #include "testing/gtest/include/gtest/gtest.h" |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 30 | #include "url/gurl.h" |
| 31 | #include "url/origin.h" |
| 32 | |
| 33 | namespace net { |
| 34 | |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | class PendingUploadImpl : public TestReportingUploader::PendingUpload { |
| 38 | public: |
Douglas Creager | 3428d81 | 2018-07-13 03:59:56 | [diff] [blame] | 39 | PendingUploadImpl(const url::Origin& report_origin, |
| 40 | const GURL& url, |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame] | 41 | const std::string& json, |
| 42 | ReportingUploader::UploadCallback callback, |
| 43 | base::OnceCallback<void(PendingUpload*)> complete_callback) |
Douglas Creager | 3428d81 | 2018-07-13 03:59:56 | [diff] [blame] | 44 | : report_origin_(report_origin), |
| 45 | url_(url), |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 46 | json_(json), |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame] | 47 | callback_(std::move(callback)), |
| 48 | complete_callback_(std::move(complete_callback)) {} |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 49 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 50 | ~PendingUploadImpl() override = default; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 51 | |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 52 | // PendingUpload implementation: |
Douglas Creager | 3428d81 | 2018-07-13 03:59:56 | [diff] [blame] | 53 | const url::Origin& report_origin() const override { return report_origin_; } |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 54 | const GURL& url() const override { return url_; } |
| 55 | const std::string& json() const override { return json_; } |
| 56 | std::unique_ptr<base::Value> GetValue() const override { |
Lei Zhang | a8b4c5fb | 2019-02-16 03:02:03 | [diff] [blame] | 57 | return base::JSONReader::ReadDeprecated(json_); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void Complete(ReportingUploader::Outcome outcome) override { |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame] | 61 | std::move(callback_).Run(outcome); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 62 | // Deletes |this|. |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame] | 63 | std::move(complete_callback_).Run(this); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | private: |
Douglas Creager | 3428d81 | 2018-07-13 03:59:56 | [diff] [blame] | 67 | url::Origin report_origin_; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 68 | GURL url_; |
| 69 | std::string json_; |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame] | 70 | ReportingUploader::UploadCallback callback_; |
| 71 | base::OnceCallback<void(PendingUpload*)> complete_callback_; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | void ErasePendingUpload( |
| 75 | std::vector<std::unique_ptr<TestReportingUploader::PendingUpload>>* uploads, |
| 76 | TestReportingUploader::PendingUpload* upload) { |
| 77 | for (auto it = uploads->begin(); it != uploads->end(); ++it) { |
| 78 | if (it->get() == upload) { |
| 79 | uploads->erase(it); |
| 80 | return; |
| 81 | } |
| 82 | } |
| 83 | NOTREACHED(); |
| 84 | } |
| 85 | |
| 86 | } // namespace |
| 87 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 88 | TestReportingUploader::PendingUpload::~PendingUpload() = default; |
| 89 | TestReportingUploader::PendingUpload::PendingUpload() = default; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 90 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 91 | TestReportingUploader::TestReportingUploader() = default; |
| 92 | TestReportingUploader::~TestReportingUploader() = default; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 93 | |
Douglas Creager | 3428d81 | 2018-07-13 03:59:56 | [diff] [blame] | 94 | void TestReportingUploader::StartUpload(const url::Origin& report_origin, |
| 95 | const GURL& url, |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 96 | const std::string& json, |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 97 | int max_depth, |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame] | 98 | UploadCallback callback) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 99 | pending_uploads_.push_back(std::make_unique<PendingUploadImpl>( |
Douglas Creager | 3428d81 | 2018-07-13 03:59:56 | [diff] [blame] | 100 | report_origin, url, json, std::move(callback), |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame] | 101 | base::BindOnce(&ErasePendingUpload, &pending_uploads_))); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 102 | } |
| 103 | |
Lily Chen | 91b51e6 | 2019-03-01 16:46:07 | [diff] [blame] | 104 | void TestReportingUploader::OnShutdown() { |
| 105 | pending_uploads_.clear(); |
| 106 | } |
| 107 | |
| 108 | int TestReportingUploader::GetPendingUploadCountForTesting() const { |
| 109 | return pending_uploads_.size(); |
| 110 | } |
| 111 | |
Julia Tuttle | 7d87494 | 2018-03-02 01:19:13 | [diff] [blame] | 112 | TestReportingDelegate::TestReportingDelegate() |
Douglas Creager | c8f4b68 | 2018-10-08 12:46:19 | [diff] [blame] | 113 | : test_request_context_(std::make_unique<TestURLRequestContext>()) {} |
juliatuttle | fcf4720 | 2017-05-23 15:53:02 | [diff] [blame] | 114 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 115 | TestReportingDelegate::~TestReportingDelegate() = default; |
juliatuttle | fcf4720 | 2017-05-23 15:53:02 | [diff] [blame] | 116 | |
| 117 | bool TestReportingDelegate::CanQueueReport(const url::Origin& origin) const { |
| 118 | return true; |
| 119 | } |
| 120 | |
Douglas Creager | 7b07ea4 | 2018-02-27 21:08:08 | [diff] [blame] | 121 | void TestReportingDelegate::CanSendReports( |
| 122 | std::set<url::Origin> origins, |
| 123 | base::OnceCallback<void(std::set<url::Origin>)> result_callback) const { |
Douglas Creager | 66494e1 | 2018-03-05 22:21:00 | [diff] [blame] | 124 | if (pause_permissions_check_) { |
| 125 | saved_origins_ = std::move(origins); |
| 126 | permissions_check_callback_ = std::move(result_callback); |
| 127 | return; |
| 128 | } |
| 129 | |
Douglas Creager | 7b07ea4 | 2018-02-27 21:08:08 | [diff] [blame] | 130 | if (disallow_report_uploads_) |
| 131 | origins.clear(); |
| 132 | std::move(result_callback).Run(std::move(origins)); |
juliatuttle | fcf4720 | 2017-05-23 15:53:02 | [diff] [blame] | 133 | } |
| 134 | |
Douglas Creager | 66494e1 | 2018-03-05 22:21:00 | [diff] [blame] | 135 | bool TestReportingDelegate::PermissionsCheckPaused() const { |
| 136 | return !permissions_check_callback_.is_null(); |
| 137 | } |
| 138 | |
| 139 | void TestReportingDelegate::ResumePermissionsCheck() { |
| 140 | if (disallow_report_uploads_) |
| 141 | saved_origins_.clear(); |
| 142 | std::move(permissions_check_callback_).Run(std::move(saved_origins_)); |
| 143 | } |
| 144 | |
juliatuttle | fcf4720 | 2017-05-23 15:53:02 | [diff] [blame] | 145 | bool TestReportingDelegate::CanSetClient(const url::Origin& origin, |
| 146 | const GURL& endpoint) const { |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool TestReportingDelegate::CanUseClient(const url::Origin& origin, |
| 151 | const GURL& endpoint) const { |
| 152 | return true; |
| 153 | } |
| 154 | |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 155 | TestReportingContext::TestReportingContext(base::Clock* clock, |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 156 | const base::TickClock* tick_clock, |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 157 | const ReportingPolicy& policy) |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 158 | : ReportingContext( |
| 159 | policy, |
| 160 | clock, |
| 161 | tick_clock, |
| 162 | base::BindRepeating(&TestReportingContext::RandIntCallback, |
| 163 | base::Unretained(this)), |
| 164 | std::make_unique<TestReportingUploader>(), |
| 165 | std::make_unique<TestReportingDelegate>()), |
| 166 | rand_counter_(0), |
tzik | 35945aa | 2018-06-22 00:33:26 | [diff] [blame] | 167 | delivery_timer_(new base::MockOneShotTimer()), |
| 168 | garbage_collection_timer_(new base::MockOneShotTimer()) { |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 169 | garbage_collector()->SetTimerForTesting( |
| 170 | base::WrapUnique(garbage_collection_timer_)); |
juliatuttle | 830962a | 2017-04-19 17:50:04 | [diff] [blame] | 171 | delivery_agent()->SetTimerForTesting(base::WrapUnique(delivery_timer_)); |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 172 | } |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 173 | |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 174 | TestReportingContext::~TestReportingContext() { |
juliatuttle | 830962a | 2017-04-19 17:50:04 | [diff] [blame] | 175 | delivery_timer_ = nullptr; |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 176 | garbage_collection_timer_ = nullptr; |
| 177 | } |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 178 | |
Julia Tuttle | d56350d | 2017-12-07 19:11:17 | [diff] [blame] | 179 | int TestReportingContext::RandIntCallback(int min, int max) { |
| 180 | DCHECK_LE(min, max); |
| 181 | return min + (rand_counter_++ % (max - min + 1)); |
| 182 | } |
| 183 | |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 184 | ReportingTestBase::ReportingTestBase() { |
| 185 | // For tests, disable jitter. |
| 186 | ReportingPolicy policy; |
| 187 | policy.endpoint_backoff_policy.jitter_factor = 0.0; |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 188 | |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 189 | CreateContext(policy, base::Time::Now(), base::TimeTicks::Now()); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 190 | } |
| 191 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 192 | ReportingTestBase::~ReportingTestBase() = default; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 193 | |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 194 | void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) { |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 195 | CreateContext(new_policy, clock()->Now(), tick_clock()->NowTicks()); |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 196 | } |
| 197 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame^] | 198 | const ReportingClient ReportingTestBase::FindEndpointInCache( |
| 199 | const url::Origin& origin, |
| 200 | const std::string& group_name, |
| 201 | const GURL& url) { |
| 202 | return cache()->GetEndpointForTesting(origin, group_name, url); |
| 203 | } |
| 204 | |
| 205 | bool ReportingTestBase::SetEndpointInCache(const url::Origin& origin, |
| 206 | const std::string& group_name, |
| 207 | const GURL& url, |
| 208 | base::Time expires, |
| 209 | OriginSubdomains include_subdomains, |
| 210 | int priority, |
| 211 | int weight) { |
| 212 | cache()->SetEndpointForTesting(origin, group_name, url, include_subdomains, |
| 213 | expires, priority, weight); |
| 214 | const ReportingClient endpoint = FindEndpointInCache(origin, group_name, url); |
| 215 | return endpoint.is_valid(); |
| 216 | } |
| 217 | |
| 218 | bool ReportingTestBase::EndpointExistsInCache(const url::Origin& origin, |
| 219 | const std::string& group_name, |
| 220 | const GURL& url) { |
| 221 | ReportingClient endpoint = |
| 222 | cache()->GetEndpointForTesting(origin, group_name, url); |
| 223 | return endpoint.is_valid(); |
| 224 | } |
| 225 | |
| 226 | ReportingClient::Statistics ReportingTestBase::GetEndpointStatistics( |
| 227 | const url::Origin& origin, |
| 228 | const std::string& group_name, |
| 229 | const GURL& url) { |
| 230 | ReportingClient endpoint = |
| 231 | cache()->GetEndpointForTesting(origin, group_name, url); |
| 232 | if (endpoint) |
| 233 | return endpoint.stats; |
| 234 | return ReportingClient::Statistics(); |
| 235 | } |
| 236 | |
| 237 | bool ReportingTestBase::EndpointGroupExistsInCache( |
| 238 | const url::Origin& origin, |
| 239 | const std::string& group_name, |
| 240 | OriginSubdomains include_subdomains, |
| 241 | base::Time expires) { |
| 242 | return cache()->EndpointGroupExistsForTesting(origin, group_name, |
| 243 | include_subdomains, expires); |
| 244 | } |
| 245 | |
| 246 | bool ReportingTestBase::OriginClientExistsInCache(const url::Origin& origin) { |
| 247 | std::vector<url::Origin> all_origins = cache()->GetAllOrigins(); |
| 248 | for (const url::Origin& cur_origin : all_origins) { |
| 249 | if (cur_origin == origin) |
| 250 | return true; |
| 251 | } |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | GURL ReportingTestBase::MakeURL(size_t index) { |
| 256 | return GURL(base::StringPrintf("https://example%zd.test", index)); |
| 257 | } |
| 258 | |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 259 | void ReportingTestBase::SimulateRestart(base::TimeDelta delta, |
| 260 | base::TimeDelta delta_ticks) { |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 261 | CreateContext(policy(), clock()->Now() + delta, |
| 262 | tick_clock()->NowTicks() + delta_ticks); |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 263 | } |
| 264 | |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 265 | void ReportingTestBase::CreateContext(const ReportingPolicy& policy, |
| 266 | base::Time now, |
| 267 | base::TimeTicks now_ticks) { |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 268 | context_ = |
| 269 | std::make_unique<TestReportingContext>(&clock_, &tick_clock_, policy); |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 270 | clock()->SetNow(now); |
| 271 | tick_clock()->SetNowTicks(now_ticks); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | base::TimeTicks ReportingTestBase::yesterday() { |
| 275 | return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1); |
| 276 | } |
| 277 | |
juliatuttle | 830962a | 2017-04-19 17:50:04 | [diff] [blame] | 278 | base::TimeTicks ReportingTestBase::now() { |
| 279 | return tick_clock()->NowTicks(); |
| 280 | } |
| 281 | |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 282 | base::TimeTicks ReportingTestBase::tomorrow() { |
| 283 | return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1); |
| 284 | } |
| 285 | |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 286 | } // namespace net |