| 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_delivery_agent.h" | 5 #include "net/reporting/reporting_delivery_agent.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/test/simple_test_tick_clock.h" | 11 #include "base/test/simple_test_tick_clock.h" |
| 12 #include "base/test/values_test_util.h" | 12 #include "base/test/values_test_util.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "net/base/backoff_entry.h" | 15 #include "net/base/backoff_entry.h" |
| 16 #include "net/reporting/reporting_cache.h" | 16 #include "net/reporting/reporting_cache.h" |
| 17 #include "net/reporting/reporting_report.h" | 17 #include "net/reporting/reporting_report.h" |
| 18 #include "net/reporting/reporting_test_util.h" | 18 #include "net/reporting/reporting_test_util.h" |
| 19 #include "net/reporting/reporting_uploader.h" | 19 #include "net/reporting/reporting_uploader.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 22 #include "url/origin.h" | 22 #include "url/origin.h" |
| 23 | 23 |
| 24 namespace net { | 24 namespace net { |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 class MockUploader : public ReportingUploader { | 27 class ReportingDeliveryAgentTest : public ReportingTestBase { |
| 28 public: | |
| 29 class PendingUpload { | |
| 30 public: | |
| 31 PendingUpload(MockUploader* uploader, | |
| 32 const GURL& url, | |
| 33 const std::string& json, | |
| 34 const Callback& callback) | |
| 35 : uploader_(uploader), url_(url), json_(json), callback_(callback) { | |
| 36 DCHECK(uploader_); | |
| 37 } | |
| 38 | |
| 39 ~PendingUpload() {} | |
| 40 | |
| 41 void Complete(Outcome outcome) { | |
| 42 callback_.Run(outcome); | |
| 43 // Deletes |this|. | |
| 44 uploader_->OnUploadComplete(this); | |
| 45 } | |
| 46 | |
| 47 const GURL& url() const { return url_; } | |
| 48 const std::string& json() const { return json_; } | |
| 49 | |
| 50 std::unique_ptr<base::Value> GetValue() const { | |
| 51 return base::JSONReader::Read(json_); | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 MockUploader* uploader_; | |
| 56 GURL url_; | |
| 57 std::string json_; | |
| 58 Callback callback_; | |
| 59 }; | |
| 60 | |
| 61 MockUploader() {} | |
| 62 ~MockUploader() override {} | |
| 63 | |
| 64 void StartUpload(const GURL& url, | |
| 65 const std::string& json, | |
| 66 const Callback& callback) override { | |
| 67 uploads_.push_back( | |
| 68 base::MakeUnique<PendingUpload>(this, url, json, callback)); | |
| 69 } | |
| 70 | |
| 71 const std::vector<std::unique_ptr<PendingUpload>>& pending_uploads() const { | |
| 72 return uploads_; | |
| 73 } | |
| 74 | |
| 75 void OnUploadComplete(PendingUpload* upload) { | |
| 76 for (auto it = uploads_.begin(); it != uploads_.end(); ++it) { | |
| 77 if (it->get() == upload) { | |
| 78 uploads_.erase(it); | |
| 79 return; | |
| 80 } | |
| 81 } | |
| 82 NOTREACHED(); | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 std::vector<std::unique_ptr<PendingUpload>> uploads_; | |
| 87 }; | |
| 88 | |
| 89 class ReportingDeliveryAgentTest : public ::testing::Test { | |
| 90 protected: | 28 protected: |
| 91 ReportingDeliveryAgentTest() | 29 ReportingDeliveryAgentTest() { |
| 92 : agent_(&clock_, &cache_, &uploader_, &backoff_policy_) { | 30 ReportingPolicy policy; |
| 93 backoff_policy_.num_errors_to_ignore = 0; | 31 policy.endpoint_backoff_policy.num_errors_to_ignore = 0; |
| 94 backoff_policy_.initial_delay_ms = 60000; | 32 policy.endpoint_backoff_policy.initial_delay_ms = 60000; |
| 95 backoff_policy_.multiply_factor = 2.0; | 33 policy.endpoint_backoff_policy.multiply_factor = 2.0; |
| 96 backoff_policy_.jitter_factor = 0.0; | 34 policy.endpoint_backoff_policy.jitter_factor = 0.0; |
| 97 backoff_policy_.maximum_backoff_ms = -1; | 35 policy.endpoint_backoff_policy.maximum_backoff_ms = -1; |
| 98 backoff_policy_.entry_lifetime_ms = 0; | 36 policy.endpoint_backoff_policy.entry_lifetime_ms = 0; |
| 99 backoff_policy_.always_use_initial_delay = false; | 37 policy.endpoint_backoff_policy.always_use_initial_delay = false; |
| 38 UsePolicy(policy); |
| 100 } | 39 } |
| 101 | 40 |
| 102 base::TimeTicks tomorrow() { | 41 base::TimeTicks tomorrow() { |
| 103 return clock_.NowTicks() + base::TimeDelta::FromDays(1); | 42 return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1); |
| 104 } | 43 } |
| 105 | 44 |
| 106 const std::vector<std::unique_ptr<MockUploader::PendingUpload>>& | 45 const std::vector<std::unique_ptr<TestReportingUploader::PendingUpload>>& |
| 107 pending_uploads() const { | 46 pending_uploads() { |
| 108 return uploader_.pending_uploads(); | 47 return uploader()->pending_uploads(); |
| 109 } | 48 } |
| 110 | 49 |
| 111 const GURL kUrl_ = GURL("https://origin/path"); | 50 const GURL kUrl_ = GURL("https://origin/path"); |
| 112 const url::Origin kOrigin_ = url::Origin(GURL("https://origin/")); | 51 const url::Origin kOrigin_ = url::Origin(GURL("https://origin/")); |
| 113 const GURL kEndpoint_ = GURL("https://endpoint/"); | 52 const GURL kEndpoint_ = GURL("https://endpoint/"); |
| 114 const std::string kGroup_ = "group"; | 53 const std::string kGroup_ = "group"; |
| 115 const std::string kType_ = "type"; | 54 const std::string kType_ = "type"; |
| 116 | |
| 117 base::SimpleTestTickClock clock_; | |
| 118 ReportingCache cache_; | |
| 119 MockUploader uploader_; | |
| 120 BackoffEntry::Policy backoff_policy_; | |
| 121 ReportingDeliveryAgent agent_; | |
| 122 }; | 55 }; |
| 123 | 56 |
| 124 TEST_F(ReportingDeliveryAgentTest, SuccessfulUpload) { | 57 TEST_F(ReportingDeliveryAgentTest, SuccessfulUpload) { |
| 125 static const int kAgeMillis = 12345; | 58 static const int kAgeMillis = 12345; |
| 126 | 59 |
| 127 base::DictionaryValue body; | 60 base::DictionaryValue body; |
| 128 body.SetString("key", "value"); | 61 body.SetString("key", "value"); |
| 129 | 62 |
| 130 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | 63 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 131 kGroup_, tomorrow()); | 64 kGroup_, tomorrow()); |
| 132 cache_.AddReport(kUrl_, kGroup_, kType_, body.CreateDeepCopy(), | 65 cache()->AddReport(kUrl_, kGroup_, kType_, body.CreateDeepCopy(), |
| 133 clock_.NowTicks(), 0); | 66 tick_clock()->NowTicks(), 0); |
| 134 | 67 |
| 135 clock_.Advance(base::TimeDelta::FromMilliseconds(kAgeMillis)); | 68 tick_clock()->Advance(base::TimeDelta::FromMilliseconds(kAgeMillis)); |
| 136 | 69 |
| 137 agent_.SendReports(); | 70 delivery_agent()->SendReports(); |
| 138 | 71 |
| 139 ASSERT_EQ(1u, pending_uploads().size()); | 72 ASSERT_EQ(1u, pending_uploads().size()); |
| 140 EXPECT_EQ(kEndpoint_, pending_uploads()[0]->url()); | 73 EXPECT_EQ(kEndpoint_, pending_uploads()[0]->url()); |
| 141 { | 74 { |
| 142 auto value = pending_uploads()[0]->GetValue(); | 75 auto value = pending_uploads()[0]->GetValue(); |
| 143 | 76 |
| 144 base::ListValue* list; | 77 base::ListValue* list; |
| 145 ASSERT_TRUE(value->GetAsList(&list)); | 78 ASSERT_TRUE(value->GetAsList(&list)); |
| 146 EXPECT_EQ(1u, list->GetSize()); | 79 EXPECT_EQ(1u, list->GetSize()); |
| 147 | 80 |
| 148 base::DictionaryValue* report; | 81 base::DictionaryValue* report; |
| 149 ASSERT_TRUE(list->GetDictionary(0, &report)); | 82 ASSERT_TRUE(list->GetDictionary(0, &report)); |
| 150 EXPECT_EQ(4u, report->size()); | 83 EXPECT_EQ(4u, report->size()); |
| 151 | 84 |
| 152 ExpectDictIntegerValue(kAgeMillis, *report, "age"); | 85 ExpectDictIntegerValue(kAgeMillis, *report, "age"); |
| 153 ExpectDictStringValue(kType_, *report, "type"); | 86 ExpectDictStringValue(kType_, *report, "type"); |
| 154 ExpectDictStringValue(kUrl_.spec(), *report, "url"); | 87 ExpectDictStringValue(kUrl_.spec(), *report, "url"); |
| 155 ExpectDictDictionaryValue(body, *report, "report"); | 88 ExpectDictDictionaryValue(body, *report, "report"); |
| 156 } | 89 } |
| 157 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | 90 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); |
| 158 | 91 |
| 159 // Successful upload should remove delivered reports. | 92 // Successful upload should remove delivered reports. |
| 160 std::vector<const ReportingReport*> reports; | 93 std::vector<const ReportingReport*> reports; |
| 161 cache_.GetReports(&reports); | 94 cache()->GetReports(&reports); |
| 162 EXPECT_TRUE(reports.empty()); | 95 EXPECT_TRUE(reports.empty()); |
| 163 | 96 |
| 164 // TODO(juliatuttle): Check that BackoffEntry was informed of success. | 97 // TODO(juliatuttle): Check that BackoffEntry was informed of success. |
| 165 } | 98 } |
| 166 | 99 |
| 167 TEST_F(ReportingDeliveryAgentTest, FailedUpload) { | 100 TEST_F(ReportingDeliveryAgentTest, FailedUpload) { |
| 168 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | 101 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 169 kGroup_, tomorrow()); | 102 kGroup_, tomorrow()); |
| 170 cache_.AddReport(kUrl_, kGroup_, kType_, | 103 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 171 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 104 base::MakeUnique<base::DictionaryValue>(), |
| 172 0); | 105 tick_clock()->NowTicks(), 0); |
| 173 | 106 |
| 174 agent_.SendReports(); | 107 delivery_agent()->SendReports(); |
| 175 | 108 |
| 176 ASSERT_EQ(1u, pending_uploads().size()); | 109 ASSERT_EQ(1u, pending_uploads().size()); |
| 177 pending_uploads()[0]->Complete(ReportingUploader::Outcome::FAILURE); | 110 pending_uploads()[0]->Complete(ReportingUploader::Outcome::FAILURE); |
| 178 | 111 |
| 179 // Failed upload should increment reports' attempts. | 112 // Failed upload should increment reports' attempts. |
| 180 std::vector<const ReportingReport*> reports; | 113 std::vector<const ReportingReport*> reports; |
| 181 cache_.GetReports(&reports); | 114 cache()->GetReports(&reports); |
| 182 ASSERT_EQ(1u, reports.size()); | 115 ASSERT_EQ(1u, reports.size()); |
| 183 EXPECT_EQ(1, reports[0]->attempts); | 116 EXPECT_EQ(1, reports[0]->attempts); |
| 184 | 117 |
| 185 // Since endpoint is now failing, an upload won't be started despite a pending | 118 // Since endpoint is now failing, an upload won't be started despite a pending |
| 186 // report. | 119 // report. |
| 187 ASSERT_TRUE(pending_uploads().empty()); | 120 ASSERT_TRUE(pending_uploads().empty()); |
| 188 agent_.SendReports(); | 121 delivery_agent()->SendReports(); |
| 189 EXPECT_TRUE(pending_uploads().empty()); | 122 EXPECT_TRUE(pending_uploads().empty()); |
| 190 } | 123 } |
| 191 | 124 |
| 192 TEST_F(ReportingDeliveryAgentTest, RemoveEndpointUpload) { | 125 TEST_F(ReportingDeliveryAgentTest, RemoveEndpointUpload) { |
| 193 static const url::Origin kDifferentOrigin(GURL("https://origin2/")); | 126 static const url::Origin kDifferentOrigin(GURL("https://origin2/")); |
| 194 | 127 |
| 195 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | 128 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 196 kGroup_, tomorrow()); | 129 kGroup_, tomorrow()); |
| 197 cache_.SetClient(kDifferentOrigin, kEndpoint_, | 130 cache()->SetClient(kDifferentOrigin, kEndpoint_, |
| 198 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); | 131 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); |
| 199 ASSERT_TRUE(FindClientInCache(&cache_, kOrigin_, kEndpoint_)); | 132 ASSERT_TRUE(FindClientInCache(cache(), kOrigin_, kEndpoint_)); |
| 200 ASSERT_TRUE(FindClientInCache(&cache_, kDifferentOrigin, kEndpoint_)); | 133 ASSERT_TRUE(FindClientInCache(cache(), kDifferentOrigin, kEndpoint_)); |
| 201 | 134 |
| 202 cache_.AddReport(kUrl_, kGroup_, kType_, | 135 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 203 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 136 base::MakeUnique<base::DictionaryValue>(), |
| 204 0); | 137 tick_clock()->NowTicks(), 0); |
| 205 | 138 |
| 206 agent_.SendReports(); | 139 delivery_agent()->SendReports(); |
| 207 | 140 |
| 208 ASSERT_EQ(1u, pending_uploads().size()); | 141 ASSERT_EQ(1u, pending_uploads().size()); |
| 209 pending_uploads()[0]->Complete(ReportingUploader::Outcome::REMOVE_ENDPOINT); | 142 pending_uploads()[0]->Complete(ReportingUploader::Outcome::REMOVE_ENDPOINT); |
| 210 | 143 |
| 211 // "Remove endpoint" upload should remove endpoint from *all* origins and | 144 // "Remove endpoint" upload should remove endpoint from *all* origins and |
| 212 // increment reports' attempts. | 145 // increment reports' attempts. |
| 213 std::vector<const ReportingReport*> reports; | 146 std::vector<const ReportingReport*> reports; |
| 214 cache_.GetReports(&reports); | 147 cache()->GetReports(&reports); |
| 215 ASSERT_EQ(1u, reports.size()); | 148 ASSERT_EQ(1u, reports.size()); |
| 216 EXPECT_EQ(1, reports[0]->attempts); | 149 EXPECT_EQ(1, reports[0]->attempts); |
| 217 | 150 |
| 218 EXPECT_FALSE(FindClientInCache(&cache_, kOrigin_, kEndpoint_)); | 151 EXPECT_FALSE(FindClientInCache(cache(), kOrigin_, kEndpoint_)); |
| 219 EXPECT_FALSE(FindClientInCache(&cache_, kDifferentOrigin, kEndpoint_)); | 152 EXPECT_FALSE(FindClientInCache(cache(), kDifferentOrigin, kEndpoint_)); |
| 220 | 153 |
| 221 // Since endpoint is now failing, an upload won't be started despite a pending | 154 // Since endpoint is now failing, an upload won't be started despite a pending |
| 222 // report. | 155 // report. |
| 223 agent_.SendReports(); | 156 delivery_agent()->SendReports(); |
| 224 EXPECT_TRUE(pending_uploads().empty()); | 157 EXPECT_TRUE(pending_uploads().empty()); |
| 225 } | 158 } |
| 226 | 159 |
| 227 TEST_F(ReportingDeliveryAgentTest, ConcurrentRemove) { | 160 TEST_F(ReportingDeliveryAgentTest, ConcurrentRemove) { |
| 228 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | 161 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 229 kGroup_, tomorrow()); | 162 kGroup_, tomorrow()); |
| 230 cache_.AddReport(kUrl_, kGroup_, kType_, | 163 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 231 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 164 base::MakeUnique<base::DictionaryValue>(), |
| 232 0); | 165 tick_clock()->NowTicks(), 0); |
| 233 | 166 |
| 234 agent_.SendReports(); | 167 delivery_agent()->SendReports(); |
| 235 ASSERT_EQ(1u, pending_uploads().size()); | 168 ASSERT_EQ(1u, pending_uploads().size()); |
| 236 | 169 |
| 237 // Remove the report while the upload is running. | 170 // Remove the report while the upload is running. |
| 238 std::vector<const ReportingReport*> reports; | 171 std::vector<const ReportingReport*> reports; |
| 239 cache_.GetReports(&reports); | 172 cache()->GetReports(&reports); |
| 240 EXPECT_EQ(1u, reports.size()); | 173 EXPECT_EQ(1u, reports.size()); |
| 241 | 174 |
| 242 // Report should appear removed, even though the cache has doomed it. | 175 // Report should appear removed, even though the cache has doomed it. |
| 243 cache_.RemoveReports(reports); | 176 cache()->RemoveReports(reports); |
| 244 cache_.GetReports(&reports); | 177 cache()->GetReports(&reports); |
| 245 EXPECT_TRUE(reports.empty()); | 178 EXPECT_TRUE(reports.empty()); |
| 246 | 179 |
| 247 // Completing upload shouldn't crash, and report should still be gone. | 180 // Completing upload shouldn't crash, and report should still be gone. |
| 248 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | 181 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); |
| 249 cache_.GetReports(&reports); | 182 cache()->GetReports(&reports); |
| 250 EXPECT_TRUE(reports.empty()); | 183 EXPECT_TRUE(reports.empty()); |
| 251 } | 184 } |
| 252 | 185 |
| 253 // Test that the agent will combine reports destined for the same endpoint, even | 186 // Test that the agent will combine reports destined for the same endpoint, even |
| 254 // if the reports are from different origins. | 187 // if the reports are from different origins. |
| 255 TEST_F(ReportingDeliveryAgentTest, | 188 TEST_F(ReportingDeliveryAgentTest, |
| 256 BatchReportsFromDifferentOriginsToSameEndpoint) { | 189 BatchReportsFromDifferentOriginsToSameEndpoint) { |
| 257 static const GURL kDifferentUrl("https://origin2/path"); | 190 static const GURL kDifferentUrl("https://origin2/path"); |
| 258 static const url::Origin kDifferentOrigin(kDifferentUrl); | 191 static const url::Origin kDifferentOrigin(kDifferentUrl); |
| 259 | 192 |
| 260 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | 193 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 261 kGroup_, tomorrow()); | 194 kGroup_, tomorrow()); |
| 262 cache_.SetClient(kDifferentOrigin, kEndpoint_, | 195 cache()->SetClient(kDifferentOrigin, kEndpoint_, |
| 263 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); | 196 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); |
| 264 | 197 |
| 265 cache_.AddReport(kUrl_, kGroup_, kType_, | 198 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 266 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 199 base::MakeUnique<base::DictionaryValue>(), |
| 267 0); | 200 tick_clock()->NowTicks(), 0); |
| 268 cache_.AddReport(kDifferentUrl, kGroup_, kType_, | 201 cache()->AddReport(kDifferentUrl, kGroup_, kType_, |
| 269 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 202 base::MakeUnique<base::DictionaryValue>(), |
| 270 0); | 203 tick_clock()->NowTicks(), 0); |
| 271 | 204 |
| 272 agent_.SendReports(); | 205 delivery_agent()->SendReports(); |
| 273 ASSERT_EQ(1u, pending_uploads().size()); | 206 ASSERT_EQ(1u, pending_uploads().size()); |
| 274 | 207 |
| 275 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | 208 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); |
| 276 EXPECT_EQ(0u, pending_uploads().size()); | 209 EXPECT_EQ(0u, pending_uploads().size()); |
| 277 } | 210 } |
| 278 | 211 |
| 279 // Test that the agent won't start a second upload to the same endpoint (even | 212 // Test that the agent won't start a second upload to the same endpoint (even |
| 280 // for a different origin) while one is pending, but will once it is no longer | 213 // for a different origin) while one is pending, but will once it is no longer |
| 281 // pending. | 214 // pending. |
| 282 TEST_F(ReportingDeliveryAgentTest, SerializeUploadsToEndpoint) { | 215 TEST_F(ReportingDeliveryAgentTest, SerializeUploadsToEndpoint) { |
| 283 static const GURL kDifferentUrl("https://origin2/path"); | 216 static const GURL kDifferentUrl("https://origin2/path"); |
| 284 static const url::Origin kDifferentOrigin(kDifferentUrl); | 217 static const url::Origin kDifferentOrigin(kDifferentUrl); |
| 285 | 218 |
| 286 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | 219 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 287 kGroup_, tomorrow()); | 220 kGroup_, tomorrow()); |
| 288 cache_.SetClient(kDifferentOrigin, kEndpoint_, | 221 cache()->SetClient(kDifferentOrigin, kEndpoint_, |
| 289 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); | 222 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); |
| 290 | 223 |
| 291 cache_.AddReport(kUrl_, kGroup_, kType_, | 224 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 292 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 225 base::MakeUnique<base::DictionaryValue>(), |
| 293 0); | 226 tick_clock()->NowTicks(), 0); |
| 294 | 227 |
| 295 agent_.SendReports(); | 228 delivery_agent()->SendReports(); |
| 296 EXPECT_EQ(1u, pending_uploads().size()); | 229 EXPECT_EQ(1u, pending_uploads().size()); |
| 297 | 230 |
| 298 cache_.AddReport(kDifferentUrl, kGroup_, kType_, | 231 cache()->AddReport(kDifferentUrl, kGroup_, kType_, |
| 299 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 232 base::MakeUnique<base::DictionaryValue>(), |
| 300 0); | 233 tick_clock()->NowTicks(), 0); |
| 301 | 234 |
| 302 agent_.SendReports(); | 235 delivery_agent()->SendReports(); |
| 303 ASSERT_EQ(1u, pending_uploads().size()); | 236 ASSERT_EQ(1u, pending_uploads().size()); |
| 304 | 237 |
| 305 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | 238 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); |
| 306 EXPECT_EQ(0u, pending_uploads().size()); | 239 EXPECT_EQ(0u, pending_uploads().size()); |
| 307 | 240 |
| 308 agent_.SendReports(); | 241 delivery_agent()->SendReports(); |
| 309 ASSERT_EQ(1u, pending_uploads().size()); | 242 ASSERT_EQ(1u, pending_uploads().size()); |
| 310 | 243 |
| 311 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | 244 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); |
| 312 EXPECT_EQ(0u, pending_uploads().size()); | 245 EXPECT_EQ(0u, pending_uploads().size()); |
| 313 } | 246 } |
| 314 | 247 |
| 315 // Test that the agent won't start a second upload for an (origin, group) while | 248 // Test that the agent won't start a second upload for an (origin, group) while |
| 316 // one is pending, but will once it is no longer pending. | 249 // one is pending, but will once it is no longer pending. |
| 317 TEST_F(ReportingDeliveryAgentTest, SerializeUploadsToGroup) { | 250 TEST_F(ReportingDeliveryAgentTest, SerializeUploadsToGroup) { |
| 318 static const GURL kDifferentEndpoint("https://endpoint2/"); | 251 static const GURL kDifferentEndpoint("https://endpoint2/"); |
| 319 static const std::string kDifferentGroup("group2"); | 252 static const std::string kDifferentGroup("group2"); |
| 320 | 253 |
| 321 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | 254 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 322 kGroup_, tomorrow()); | 255 kGroup_, tomorrow()); |
| 323 cache_.SetClient(kOrigin_, kDifferentEndpoint, | 256 cache()->SetClient(kOrigin_, kDifferentEndpoint, |
| 324 ReportingClient::Subdomains::EXCLUDE, kDifferentGroup, | 257 ReportingClient::Subdomains::EXCLUDE, kDifferentGroup, |
| 325 tomorrow()); | 258 tomorrow()); |
| 326 | 259 |
| 327 cache_.AddReport(kUrl_, kGroup_, kType_, | 260 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 328 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 261 base::MakeUnique<base::DictionaryValue>(), |
| 329 0); | 262 tick_clock()->NowTicks(), 0); |
| 330 | 263 |
| 331 agent_.SendReports(); | 264 delivery_agent()->SendReports(); |
| 332 EXPECT_EQ(1u, pending_uploads().size()); | 265 EXPECT_EQ(1u, pending_uploads().size()); |
| 333 | 266 |
| 334 cache_.AddReport(kUrl_, kDifferentGroup, kType_, | 267 cache()->AddReport(kUrl_, kDifferentGroup, kType_, |
| 335 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 268 base::MakeUnique<base::DictionaryValue>(), |
| 336 0); | 269 tick_clock()->NowTicks(), 0); |
| 337 | 270 |
| 338 agent_.SendReports(); | 271 delivery_agent()->SendReports(); |
| 339 ASSERT_EQ(2u, pending_uploads().size()); | 272 ASSERT_EQ(2u, pending_uploads().size()); |
| 340 | 273 |
| 341 pending_uploads()[1]->Complete(ReportingUploader::Outcome::SUCCESS); | 274 pending_uploads()[1]->Complete(ReportingUploader::Outcome::SUCCESS); |
| 342 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | 275 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); |
| 343 EXPECT_EQ(0u, pending_uploads().size()); | 276 EXPECT_EQ(0u, pending_uploads().size()); |
| 344 } | 277 } |
| 345 | 278 |
| 346 // Test that the agent won't start a second upload for an (origin, group) while | 279 // Test that the agent won't start a second upload for an (origin, group) while |
| 347 // one is pending, but will once it is no longer pending. | 280 // one is pending, but will once it is no longer pending. |
| 348 TEST_F(ReportingDeliveryAgentTest, ParallelizeUploadsAcrossGroups) { | 281 TEST_F(ReportingDeliveryAgentTest, ParallelizeUploadsAcrossGroups) { |
| 349 static const GURL kDifferentEndpoint("https://endpoint2/"); | 282 static const GURL kDifferentEndpoint("https://endpoint2/"); |
| 350 | 283 |
| 351 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | 284 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 352 kGroup_, tomorrow()); | 285 kGroup_, tomorrow()); |
| 353 cache_.SetClient(kOrigin_, kDifferentEndpoint, | 286 cache()->SetClient(kOrigin_, kDifferentEndpoint, |
| 354 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); | 287 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); |
| 355 | 288 |
| 356 cache_.AddReport(kUrl_, kGroup_, kType_, | 289 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 357 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 290 base::MakeUnique<base::DictionaryValue>(), |
| 358 0); | 291 tick_clock()->NowTicks(), 0); |
| 359 | 292 |
| 360 agent_.SendReports(); | 293 delivery_agent()->SendReports(); |
| 361 EXPECT_EQ(1u, pending_uploads().size()); | 294 EXPECT_EQ(1u, pending_uploads().size()); |
| 362 | 295 |
| 363 cache_.AddReport(kUrl_, kGroup_, kType_, | 296 cache()->AddReport(kUrl_, kGroup_, kType_, |
| 364 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | 297 base::MakeUnique<base::DictionaryValue>(), |
| 365 0); | 298 tick_clock()->NowTicks(), 0); |
| 366 | 299 |
| 367 agent_.SendReports(); | 300 delivery_agent()->SendReports(); |
| 368 ASSERT_EQ(1u, pending_uploads().size()); | 301 ASSERT_EQ(1u, pending_uploads().size()); |
| 369 | 302 |
| 370 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | 303 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); |
| 371 EXPECT_EQ(0u, pending_uploads().size()); | 304 EXPECT_EQ(0u, pending_uploads().size()); |
| 372 | 305 |
| 373 agent_.SendReports(); | 306 delivery_agent()->SendReports(); |
| 374 ASSERT_EQ(1u, pending_uploads().size()); | 307 ASSERT_EQ(1u, pending_uploads().size()); |
| 375 | 308 |
| 376 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | 309 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); |
| 377 EXPECT_EQ(0u, pending_uploads().size()); | 310 EXPECT_EQ(0u, pending_uploads().size()); |
| 378 } | 311 } |
| 379 | 312 |
| 380 } // namespace | 313 } // namespace |
| 381 } // namespace net | 314 } // namespace net |
| OLD | NEW |