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" |
| 13 | #include "base/memory/ptr_util.h" |
| 14 | #include "base/test/simple_test_clock.h" |
| 15 | #include "base/test/simple_test_tick_clock.h" |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 16 | #include "base/timer/mock_timer.h" |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 17 | #include "net/reporting/reporting_cache.h" |
| 18 | #include "net/reporting/reporting_client.h" |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 19 | #include "net/reporting/reporting_context.h" |
juliatuttle | fcf4720 | 2017-05-23 15:53:02 | [diff] [blame] | 20 | #include "net/reporting/reporting_delegate.h" |
juliatuttle | 830962a | 2017-04-19 17:50:04 | [diff] [blame] | 21 | #include "net/reporting/reporting_delivery_agent.h" |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 22 | #include "net/reporting/reporting_garbage_collector.h" |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 23 | #include "net/reporting/reporting_policy.h" |
| 24 | #include "net/reporting/reporting_uploader.h" |
| 25 | #include "testing/gtest/include/gtest/gtest.h" |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 26 | #include "url/gurl.h" |
| 27 | #include "url/origin.h" |
| 28 | |
| 29 | namespace net { |
| 30 | |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | class PendingUploadImpl : public TestReportingUploader::PendingUpload { |
| 34 | public: |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame^] | 35 | PendingUploadImpl(const GURL& url, |
| 36 | const std::string& json, |
| 37 | ReportingUploader::UploadCallback callback, |
| 38 | base::OnceCallback<void(PendingUpload*)> complete_callback) |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 39 | : url_(url), |
| 40 | json_(json), |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame^] | 41 | callback_(std::move(callback)), |
| 42 | complete_callback_(std::move(complete_callback)) {} |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 43 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 44 | ~PendingUploadImpl() override = default; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 45 | |
| 46 | // PendingUpload implementationP: |
| 47 | const GURL& url() const override { return url_; } |
| 48 | const std::string& json() const override { return json_; } |
| 49 | std::unique_ptr<base::Value> GetValue() const override { |
| 50 | return base::JSONReader::Read(json_); |
| 51 | } |
| 52 | |
| 53 | void Complete(ReportingUploader::Outcome outcome) override { |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame^] | 54 | std::move(callback_).Run(outcome); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 55 | // Deletes |this|. |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame^] | 56 | std::move(complete_callback_).Run(this); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | private: |
| 60 | GURL url_; |
| 61 | std::string json_; |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame^] | 62 | ReportingUploader::UploadCallback callback_; |
| 63 | base::OnceCallback<void(PendingUpload*)> complete_callback_; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | void ErasePendingUpload( |
| 67 | std::vector<std::unique_ptr<TestReportingUploader::PendingUpload>>* uploads, |
| 68 | TestReportingUploader::PendingUpload* upload) { |
| 69 | for (auto it = uploads->begin(); it != uploads->end(); ++it) { |
| 70 | if (it->get() == upload) { |
| 71 | uploads->erase(it); |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 | NOTREACHED(); |
| 76 | } |
| 77 | |
| 78 | } // namespace |
| 79 | |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 80 | const ReportingClient* FindClientInCache(const ReportingCache* cache, |
| 81 | const url::Origin& origin, |
| 82 | const GURL& endpoint) { |
| 83 | std::vector<const ReportingClient*> clients; |
| 84 | cache->GetClients(&clients); |
| 85 | for (const ReportingClient* client : clients) { |
| 86 | if (client->origin == origin && client->endpoint == endpoint) |
| 87 | return client; |
| 88 | } |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 92 | TestReportingUploader::PendingUpload::~PendingUpload() = default; |
| 93 | TestReportingUploader::PendingUpload::PendingUpload() = default; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 94 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 95 | TestReportingUploader::TestReportingUploader() = default; |
| 96 | TestReportingUploader::~TestReportingUploader() = default; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 97 | |
| 98 | void TestReportingUploader::StartUpload(const GURL& url, |
| 99 | const std::string& json, |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame^] | 100 | UploadCallback callback) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 101 | pending_uploads_.push_back(std::make_unique<PendingUploadImpl>( |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame^] | 102 | url, json, std::move(callback), |
| 103 | base::BindOnce(&ErasePendingUpload, &pending_uploads_))); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 104 | } |
| 105 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 106 | TestReportingDelegate::TestReportingDelegate() = default; |
juliatuttle | fcf4720 | 2017-05-23 15:53:02 | [diff] [blame] | 107 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 108 | TestReportingDelegate::~TestReportingDelegate() = default; |
juliatuttle | fcf4720 | 2017-05-23 15:53:02 | [diff] [blame] | 109 | |
| 110 | bool TestReportingDelegate::CanQueueReport(const url::Origin& origin) const { |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | bool TestReportingDelegate::CanSendReport(const url::Origin& origin) const { |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool TestReportingDelegate::CanSetClient(const url::Origin& origin, |
| 119 | const GURL& endpoint) const { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | bool TestReportingDelegate::CanUseClient(const url::Origin& origin, |
| 124 | const GURL& endpoint) const { |
| 125 | return true; |
| 126 | } |
| 127 | |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 128 | TestReportingContext::TestReportingContext(const ReportingPolicy& policy) |
| 129 | : ReportingContext(policy, |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 130 | std::make_unique<base::SimpleTestClock>(), |
| 131 | std::make_unique<base::SimpleTestTickClock>(), |
| 132 | std::make_unique<TestReportingUploader>(), |
| 133 | std::make_unique<TestReportingDelegate>()), |
juliatuttle | 830962a | 2017-04-19 17:50:04 | [diff] [blame] | 134 | delivery_timer_(new base::MockTimer(/* retain_user_task= */ false, |
| 135 | /* is_repeating= */ false)), |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 136 | garbage_collection_timer_( |
| 137 | new base::MockTimer(/* retain_user_task= */ false, |
| 138 | /* is_repeating= */ false)) { |
| 139 | garbage_collector()->SetTimerForTesting( |
| 140 | base::WrapUnique(garbage_collection_timer_)); |
juliatuttle | 830962a | 2017-04-19 17:50:04 | [diff] [blame] | 141 | delivery_agent()->SetTimerForTesting(base::WrapUnique(delivery_timer_)); |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 142 | } |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 143 | |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 144 | TestReportingContext::~TestReportingContext() { |
juliatuttle | 830962a | 2017-04-19 17:50:04 | [diff] [blame] | 145 | delivery_timer_ = nullptr; |
juliatuttle | 9f970c0 | 2017-04-10 19:26:37 | [diff] [blame] | 146 | garbage_collection_timer_ = nullptr; |
| 147 | } |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 148 | |
| 149 | ReportingTestBase::ReportingTestBase() { |
| 150 | // For tests, disable jitter. |
| 151 | ReportingPolicy policy; |
| 152 | policy.endpoint_backoff_policy.jitter_factor = 0.0; |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 153 | |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 154 | CreateContext(policy, base::Time::Now(), base::TimeTicks::Now()); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 155 | } |
| 156 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 157 | ReportingTestBase::~ReportingTestBase() = default; |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 158 | |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 159 | void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) { |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 160 | CreateContext(new_policy, clock()->Now(), tick_clock()->NowTicks()); |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void ReportingTestBase::SimulateRestart(base::TimeDelta delta, |
| 164 | base::TimeDelta delta_ticks) { |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 165 | CreateContext(policy(), clock()->Now() + delta, |
| 166 | tick_clock()->NowTicks() + delta_ticks); |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 167 | } |
| 168 | |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 169 | void ReportingTestBase::CreateContext(const ReportingPolicy& policy, |
| 170 | base::Time now, |
| 171 | base::TimeTicks now_ticks) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 172 | context_ = std::make_unique<TestReportingContext>(policy); |
juliatuttle | 7da1381 | 2017-04-13 19:18:19 | [diff] [blame] | 173 | clock()->SetNow(now); |
| 174 | tick_clock()->SetNowTicks(now_ticks); |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | base::TimeTicks ReportingTestBase::yesterday() { |
| 178 | return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1); |
| 179 | } |
| 180 | |
juliatuttle | 830962a | 2017-04-19 17:50:04 | [diff] [blame] | 181 | base::TimeTicks ReportingTestBase::now() { |
| 182 | return tick_clock()->NowTicks(); |
| 183 | } |
| 184 | |
juliatuttle | ee4b55e | 2017-04-07 17:09:45 | [diff] [blame] | 185 | base::TimeTicks ReportingTestBase::tomorrow() { |
| 186 | return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1); |
| 187 | } |
| 188 | |
juliatuttle | 58684333 | 2017-03-27 16:22:37 | [diff] [blame] | 189 | } // namespace net |