OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/reporting/reporting_test_util.h" | 5 #include "net/reporting/reporting_test_util.h" |
6 | 6 |
| 7 #include <memory> |
| 8 #include <string> |
7 #include <vector> | 9 #include <vector> |
8 | 10 |
| 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" |
9 #include "net/reporting/reporting_cache.h" | 16 #include "net/reporting/reporting_cache.h" |
10 #include "net/reporting/reporting_client.h" | 17 #include "net/reporting/reporting_client.h" |
| 18 #include "net/reporting/reporting_context.h" |
| 19 #include "net/reporting/reporting_delegate.h" |
| 20 #include "net/reporting/reporting_policy.h" |
| 21 #include "net/reporting/reporting_uploader.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "url/gurl.h" | 23 #include "url/gurl.h" |
12 #include "url/origin.h" | 24 #include "url/origin.h" |
13 | 25 |
14 namespace net { | 26 namespace net { |
15 | 27 |
| 28 namespace { |
| 29 |
| 30 class PendingUploadImpl : public TestReportingUploader::PendingUpload { |
| 31 public: |
| 32 PendingUploadImpl( |
| 33 const GURL& url, |
| 34 const std::string& json, |
| 35 const ReportingUploader::Callback& callback, |
| 36 const base::Callback<void(PendingUpload*)>& complete_callback) |
| 37 : url_(url), |
| 38 json_(json), |
| 39 callback_(callback), |
| 40 complete_callback_(complete_callback) {} |
| 41 |
| 42 ~PendingUploadImpl() override {} |
| 43 |
| 44 // PendingUpload implementationP: |
| 45 const GURL& url() const override { return url_; } |
| 46 const std::string& json() const override { return json_; } |
| 47 std::unique_ptr<base::Value> GetValue() const override { |
| 48 return base::JSONReader::Read(json_); |
| 49 } |
| 50 |
| 51 void Complete(ReportingUploader::Outcome outcome) override { |
| 52 callback_.Run(outcome); |
| 53 // Deletes |this|. |
| 54 complete_callback_.Run(this); |
| 55 } |
| 56 |
| 57 private: |
| 58 GURL url_; |
| 59 std::string json_; |
| 60 ReportingUploader::Callback callback_; |
| 61 base::Callback<void(PendingUpload*)> complete_callback_; |
| 62 }; |
| 63 |
| 64 void ErasePendingUpload( |
| 65 std::vector<std::unique_ptr<TestReportingUploader::PendingUpload>>* uploads, |
| 66 TestReportingUploader::PendingUpload* upload) { |
| 67 for (auto it = uploads->begin(); it != uploads->end(); ++it) { |
| 68 if (it->get() == upload) { |
| 69 uploads->erase(it); |
| 70 return; |
| 71 } |
| 72 } |
| 73 NOTREACHED(); |
| 74 } |
| 75 |
| 76 } // namespace |
| 77 |
16 const ReportingClient* FindClientInCache(const ReportingCache* cache, | 78 const ReportingClient* FindClientInCache(const ReportingCache* cache, |
17 const url::Origin& origin, | 79 const url::Origin& origin, |
18 const GURL& endpoint) { | 80 const GURL& endpoint) { |
19 std::vector<const ReportingClient*> clients; | 81 std::vector<const ReportingClient*> clients; |
20 cache->GetClients(&clients); | 82 cache->GetClients(&clients); |
21 for (const ReportingClient* client : clients) { | 83 for (const ReportingClient* client : clients) { |
22 if (client->origin == origin && client->endpoint == endpoint) | 84 if (client->origin == origin && client->endpoint == endpoint) |
23 return client; | 85 return client; |
24 } | 86 } |
25 return nullptr; | 87 return nullptr; |
26 } | 88 } |
27 | 89 |
| 90 TestReportingDelegate::TestReportingDelegate() {} |
| 91 TestReportingDelegate::~TestReportingDelegate() {} |
| 92 |
| 93 void TestReportingDelegate::PersistData( |
| 94 std::unique_ptr<const base::Value> persisted_data) { |
| 95 persisted_data_ = std::move(persisted_data); |
| 96 } |
| 97 |
| 98 std::unique_ptr<const base::Value> TestReportingDelegate::GetPersistedData() { |
| 99 if (!persisted_data_) |
| 100 return std::unique_ptr<const base::Value>(); |
| 101 return persisted_data_->CreateDeepCopy(); |
| 102 } |
| 103 |
| 104 TestReportingUploader::PendingUpload::~PendingUpload() {} |
| 105 TestReportingUploader::PendingUpload::PendingUpload() {} |
| 106 |
| 107 TestReportingUploader::TestReportingUploader() {} |
| 108 TestReportingUploader::~TestReportingUploader() {} |
| 109 |
| 110 void TestReportingUploader::StartUpload(const GURL& url, |
| 111 const std::string& json, |
| 112 const Callback& callback) { |
| 113 pending_uploads_.push_back(base::MakeUnique<PendingUploadImpl>( |
| 114 url, json, callback, base::Bind(&ErasePendingUpload, &pending_uploads_))); |
| 115 } |
| 116 |
| 117 TestReportingContext::TestReportingContext(const ReportingPolicy& policy) |
| 118 : ReportingContext(policy, |
| 119 base::MakeUnique<TestReportingDelegate>(), |
| 120 base::MakeUnique<base::SimpleTestClock>(), |
| 121 base::MakeUnique<base::SimpleTestTickClock>(), |
| 122 base::MakeUnique<TestReportingUploader>()) {} |
| 123 |
| 124 TestReportingContext::~TestReportingContext() {} |
| 125 |
| 126 ReportingTestBase::ReportingTestBase() { |
| 127 // For tests, disable jitter. |
| 128 ReportingPolicy policy; |
| 129 policy.endpoint_backoff_policy.jitter_factor = 0.0; |
| 130 UsePolicy(policy); |
| 131 } |
| 132 |
| 133 ReportingTestBase::~ReportingTestBase() {} |
| 134 |
| 135 void ReportingTestBase::UsePolicy(const ReportingPolicy& policy) { |
| 136 context_ = base::MakeUnique<TestReportingContext>(policy); |
| 137 } |
| 138 |
| 139 base::TimeTicks ReportingTestBase::yesterday() { |
| 140 return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1); |
| 141 } |
| 142 |
| 143 base::TimeTicks ReportingTestBase::tomorrow() { |
| 144 return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1); |
| 145 } |
| 146 |
28 } // namespace net | 147 } // namespace net |
OLD | NEW |