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