Chromium Code Reviews| 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 // Report should appear removed, even though the cache has doomed it. | |
| 243 cache_.RemoveReports(reports); | |
| 244 cache_.GetReports(&reports); | |
|
shivanisha
2017/03/31 18:58:52
IsReportDoomedForTesting() should return true.
Julia Tuttle
2017/04/04 18:09:32
Done.
| |
| 245 EXPECT_TRUE(reports.empty()); | |
| 246 | |
| 247 // Completing upload shouldn't crash, and report should still be gone. | |
| 248 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | |
| 249 cache_.GetReports(&reports); | |
|
shivanisha
2017/03/31 18:58:52
IsReportDoomedForTesting() should return false her
Julia Tuttle
2017/04/04 18:09:31
Done.
| |
| 250 EXPECT_TRUE(reports.empty()); | |
| 251 } | |
| 252 | |
| 253 // Test that the agent will combine reports destined for the same endpoint, even | |
| 254 // if the reports are from different origins. | |
| 255 TEST_F(ReportingDeliveryAgentTest, | |
| 256 BatchReportsFromDifferentOriginsToSameEndpoint) { | |
| 257 static const GURL kDifferentUrl("https://origin2/path"); | |
| 258 static const url::Origin kDifferentOrigin(kDifferentUrl); | |
| 259 | |
| 260 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | |
| 261 kGroup_, tomorrow()); | |
| 262 cache_.SetClient(kDifferentOrigin, kEndpoint_, | |
| 263 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); | |
| 264 | |
| 265 cache_.AddReport(kUrl_, kGroup_, kType_, | |
| 266 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | |
| 267 0); | |
| 268 cache_.AddReport(kDifferentUrl, kGroup_, kType_, | |
| 269 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | |
| 270 0); | |
| 271 | |
| 272 agent_.SendReports(); | |
| 273 ASSERT_EQ(1u, pending_uploads().size()); | |
| 274 | |
| 275 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | |
| 276 EXPECT_EQ(0u, pending_uploads().size()); | |
| 277 } | |
| 278 | |
| 279 // 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 | |
| 281 // pending. | |
| 282 TEST_F(ReportingDeliveryAgentTest, SerializeUploadsToEndpoint) { | |
| 283 static const GURL kDifferentUrl("https://origin2/path"); | |
| 284 static const url::Origin kDifferentOrigin(kDifferentUrl); | |
| 285 | |
| 286 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | |
| 287 kGroup_, tomorrow()); | |
| 288 cache_.SetClient(kDifferentOrigin, kEndpoint_, | |
| 289 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); | |
| 290 | |
| 291 cache_.AddReport(kUrl_, kGroup_, kType_, | |
| 292 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | |
| 293 0); | |
| 294 | |
| 295 agent_.SendReports(); | |
| 296 EXPECT_EQ(1u, pending_uploads().size()); | |
| 297 | |
| 298 cache_.AddReport(kDifferentUrl, kGroup_, kType_, | |
| 299 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | |
| 300 0); | |
| 301 | |
| 302 agent_.SendReports(); | |
| 303 ASSERT_EQ(1u, pending_uploads().size()); | |
| 304 | |
| 305 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | |
| 306 EXPECT_EQ(0u, pending_uploads().size()); | |
| 307 | |
| 308 agent_.SendReports(); | |
| 309 ASSERT_EQ(1u, pending_uploads().size()); | |
| 310 | |
| 311 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | |
| 312 EXPECT_EQ(0u, pending_uploads().size()); | |
| 313 } | |
| 314 | |
| 315 // 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. | |
| 317 TEST_F(ReportingDeliveryAgentTest, SerializeUploadsToGroup) { | |
| 318 static const GURL kDifferentEndpoint("https://endpoint2/"); | |
| 319 static const std::string kDifferentGroup("group2"); | |
| 320 | |
| 321 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | |
| 322 kGroup_, tomorrow()); | |
| 323 cache_.SetClient(kOrigin_, kDifferentEndpoint, | |
| 324 ReportingClient::Subdomains::EXCLUDE, kDifferentGroup, | |
| 325 tomorrow()); | |
| 326 | |
| 327 cache_.AddReport(kUrl_, kGroup_, kType_, | |
| 328 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | |
| 329 0); | |
| 330 | |
| 331 agent_.SendReports(); | |
| 332 EXPECT_EQ(1u, pending_uploads().size()); | |
| 333 | |
| 334 cache_.AddReport(kUrl_, kDifferentGroup, kType_, | |
| 335 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | |
| 336 0); | |
| 337 | |
| 338 agent_.SendReports(); | |
| 339 ASSERT_EQ(2u, pending_uploads().size()); | |
| 340 | |
| 341 pending_uploads()[1]->Complete(ReportingUploader::Outcome::SUCCESS); | |
| 342 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | |
| 343 EXPECT_EQ(0u, pending_uploads().size()); | |
| 344 } | |
| 345 | |
| 346 // 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. | |
| 348 TEST_F(ReportingDeliveryAgentTest, ParallelizeUploadsAcrossGroups) { | |
|
shivanisha
2017/03/31 18:58:52
The comments in this and previous tests are same?
Julia Tuttle
2017/04/04 18:09:32
Oops! The tests also didn't do what they were supp
| |
| 349 static const GURL kDifferentEndpoint("https://endpoint2/"); | |
| 350 | |
| 351 cache_.SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, | |
| 352 kGroup_, tomorrow()); | |
| 353 cache_.SetClient(kOrigin_, kDifferentEndpoint, | |
| 354 ReportingClient::Subdomains::EXCLUDE, kGroup_, tomorrow()); | |
| 355 | |
| 356 cache_.AddReport(kUrl_, kGroup_, kType_, | |
| 357 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | |
| 358 0); | |
| 359 | |
| 360 agent_.SendReports(); | |
| 361 EXPECT_EQ(1u, pending_uploads().size()); | |
| 362 | |
| 363 cache_.AddReport(kUrl_, kGroup_, kType_, | |
| 364 base::MakeUnique<base::DictionaryValue>(), clock_.NowTicks(), | |
| 365 0); | |
| 366 | |
| 367 agent_.SendReports(); | |
| 368 ASSERT_EQ(1u, pending_uploads().size()); | |
| 369 | |
| 370 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | |
| 371 EXPECT_EQ(0u, pending_uploads().size()); | |
| 372 | |
| 373 agent_.SendReports(); | |
| 374 ASSERT_EQ(1u, pending_uploads().size()); | |
| 375 | |
| 376 pending_uploads()[0]->Complete(ReportingUploader::Outcome::SUCCESS); | |
| 377 EXPECT_EQ(0u, pending_uploads().size()); | |
| 378 } | |
| 379 | |
| 380 } // namespace | |
| 381 } // namespace net | |
| OLD | NEW |