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