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