OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/safe_browsing/certificate_reporting_service.h" | 5 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/atomic_sequence_num.h" | 9 #include "base/atomic_sequence_num.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "net/cert/x509_util.h" | 29 #include "net/cert/x509_util.h" |
30 #include "net/ssl/ssl_info.h" | 30 #include "net/ssl/ssl_info.h" |
31 #include "net/test/url_request/url_request_failed_job.h" | 31 #include "net/test/url_request/url_request_failed_job.h" |
32 #include "net/test/url_request/url_request_mock_data_job.h" | 32 #include "net/test/url_request/url_request_mock_data_job.h" |
33 #include "net/url_request/url_request_filter.h" | 33 #include "net/url_request/url_request_filter.h" |
34 #include "net/url_request/url_request_test_util.h" | 34 #include "net/url_request/url_request_test_util.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
36 | 36 |
37 using certificate_reporting_test_utils::CertificateReportingServiceTestHelper; | 37 using certificate_reporting_test_utils::CertificateReportingServiceTestHelper; |
38 using certificate_reporting_test_utils::CertificateReportingServiceObserver; | 38 using certificate_reporting_test_utils::CertificateReportingServiceObserver; |
| 39 using certificate_reporting_test_utils::EventHistogramTester; |
39 using certificate_reporting_test_utils::ReportExpectation; | 40 using certificate_reporting_test_utils::ReportExpectation; |
40 using certificate_reporting_test_utils::RetryStatus; | 41 using certificate_reporting_test_utils::RetryStatus; |
41 | 42 |
42 namespace { | 43 namespace { |
43 | 44 |
44 // Maximum number of reports kept in the certificate reporting service's retry | 45 // Maximum number of reports kept in the certificate reporting service's retry |
45 // queue. | 46 // queue. |
46 const size_t kMaxReportCountInQueue = 3; | 47 const size_t kMaxReportCountInQueue = 3; |
47 | 48 |
48 const char* kFailedReportHistogram = "SSL.CertificateErrorReportFailure"; | 49 const char* kFailedReportHistogram = "SSL.CertificateErrorReportFailure"; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 113 } |
113 | 114 |
114 private: | 115 private: |
115 unsigned int num_expected_failed_report_ = 0; | 116 unsigned int num_expected_failed_report_ = 0; |
116 base::HistogramTester histogram_tester_; | 117 base::HistogramTester histogram_tester_; |
117 }; | 118 }; |
118 | 119 |
119 } // namespace | 120 } // namespace |
120 | 121 |
121 TEST(CertificateReportingServiceReportListTest, BoundedReportList) { | 122 TEST(CertificateReportingServiceReportListTest, BoundedReportList) { |
| 123 EventHistogramTester event_histogram_tester; |
| 124 |
122 // Create a report list with maximum of 2 items. | 125 // Create a report list with maximum of 2 items. |
123 CertificateReportingService::BoundedReportList list(2 /* max_size */); | 126 CertificateReportingService::BoundedReportList list(2 /* max_size */); |
124 EXPECT_EQ(0u, list.items().size()); | 127 EXPECT_EQ(0u, list.items().size()); |
125 | 128 |
126 // Add a ten minute old report. | 129 // Add a ten minute old report. |
127 list.Add(CertificateReportingService::Report( | 130 list.Add(CertificateReportingService::Report( |
128 1, base::Time::Now() - base::TimeDelta::FromMinutes(10), | 131 1, base::Time::Now() - base::TimeDelta::FromMinutes(10), |
129 std::string("report1_ten_minutes_old"))); | 132 std::string("report1_ten_minutes_old"))); |
130 EXPECT_EQ(1u, list.items().size()); | 133 EXPECT_EQ(1u, list.items().size()); |
131 EXPECT_EQ("report1_ten_minutes_old", list.items()[0].serialized_report); | 134 EXPECT_EQ("report1_ten_minutes_old", list.items()[0].serialized_report); |
(...skipping 15 matching lines...) Expand all Loading... |
147 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); | 150 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); |
148 | 151 |
149 // Adding a report older than the oldest report in the list (report2) is | 152 // Adding a report older than the oldest report in the list (report2) is |
150 // a no-op. | 153 // a no-op. |
151 list.Add(CertificateReportingService::Report( | 154 list.Add(CertificateReportingService::Report( |
152 0, base::Time::Now() - base::TimeDelta::FromMinutes(10), | 155 0, base::Time::Now() - base::TimeDelta::FromMinutes(10), |
153 std::string("report0_ten_minutes_old"))); | 156 std::string("report0_ten_minutes_old"))); |
154 EXPECT_EQ(2u, list.items().size()); | 157 EXPECT_EQ(2u, list.items().size()); |
155 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report); | 158 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report); |
156 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); | 159 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); |
| 160 |
| 161 event_histogram_tester.SetExpectedValues(0 /* submitted */, 0 /* failed */, |
| 162 0 /* successful */, 2 /* dropped */); |
157 } | 163 } |
158 | 164 |
159 class CertificateReportingServiceReporterOnIOThreadTest | 165 class CertificateReportingServiceReporterOnIOThreadTest |
160 : public ::testing::Test { | 166 : public ::testing::Test { |
161 public: | 167 public: |
162 void SetUp() override { | 168 void SetUp() override { |
163 message_loop_.reset(new base::MessageLoopForIO()); | 169 message_loop_.reset(new base::MessageLoopForIO()); |
164 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, | 170 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, |
165 message_loop_.get())); | 171 message_loop_.get())); |
166 url_request_context_getter_ = | 172 url_request_context_getter_ = |
167 new net::TestURLRequestContextGetter(message_loop_->task_runner()); | 173 new net::TestURLRequestContextGetter(message_loop_->task_runner()); |
168 net::URLRequestFailedJob::AddUrlHandler(); | 174 net::URLRequestFailedJob::AddUrlHandler(); |
169 net::URLRequestMockDataJob::AddUrlHandler(); | 175 net::URLRequestMockDataJob::AddUrlHandler(); |
| 176 |
| 177 event_histogram_tester_.reset(new EventHistogramTester()); |
170 } | 178 } |
171 | 179 |
172 void TearDown() override { | 180 void TearDown() override { |
173 ClearURLHandlers(); | 181 ClearURLHandlers(); |
174 // Check the histogram as the last thing. This makes sure no in-flight | 182 // Check histograms as the last thing. This makes sure no in-flight report |
175 // report is missed. | 183 // is missed. |
176 histogram_test_helper_.CheckHistogram(); | 184 histogram_test_helper_.CheckHistogram(); |
| 185 event_histogram_tester_.reset(); |
177 } | 186 } |
178 | 187 |
179 protected: | 188 protected: |
180 net::URLRequestContextGetter* url_request_context_getter() { | 189 net::URLRequestContextGetter* url_request_context_getter() { |
181 return url_request_context_getter_.get(); | 190 return url_request_context_getter_.get(); |
182 } | 191 } |
183 | 192 |
184 void SetExpectedFailedReportCountOnTearDown(unsigned int count) { | 193 void SetExpectedFailedReportCountOnTearDown(unsigned int count) { |
185 histogram_test_helper_.SetExpectedFailedReportCount(count); | 194 histogram_test_helper_.SetExpectedFailedReportCount(count); |
186 } | 195 } |
187 | 196 |
| 197 EventHistogramTester* event_histogram_tester() { |
| 198 return event_histogram_tester_.get(); |
| 199 } |
| 200 |
188 private: | 201 private: |
189 std::unique_ptr<base::MessageLoopForIO> message_loop_; | 202 std::unique_ptr<base::MessageLoopForIO> message_loop_; |
190 std::unique_ptr<content::TestBrowserThread> io_thread_; | 203 std::unique_ptr<content::TestBrowserThread> io_thread_; |
191 | 204 |
192 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 205 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
193 ReportHistogramTestHelper histogram_test_helper_; | 206 ReportHistogramTestHelper histogram_test_helper_; |
| 207 // Histogram tester for reporting events. This is a member instead of a local |
| 208 // so that we can check the histogram after the test teardown. At that point |
| 209 // all in flight reports should be completed or deleted because |
| 210 // of CleanUpOnIOThread(). |
| 211 std::unique_ptr<EventHistogramTester> event_histogram_tester_; |
194 }; | 212 }; |
195 | 213 |
196 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, | 214 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, |
197 Reporter_RetriesEnabled) { | 215 Reporter_RetriesEnabled) { |
198 SetExpectedFailedReportCountOnTearDown(6); | 216 SetExpectedFailedReportCountOnTearDown(6); |
199 | 217 |
200 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); | 218 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
201 const base::Time reference_time = base::Time::Now(); | 219 const base::Time reference_time = base::Time::Now(); |
202 clock->SetNow(reference_time); | 220 clock->SetNow(reference_time); |
203 | 221 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 // Send pending reports again, this time successfully. There should be no | 291 // Send pending reports again, this time successfully. There should be no |
274 // pending reports left. | 292 // pending reports left. |
275 const GURL kSuccessURL = | 293 const GURL kSuccessURL = |
276 net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 294 net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
277 certificate_error_reporter->set_upload_url_for_testing(kSuccessURL); | 295 certificate_error_reporter->set_upload_url_for_testing(kSuccessURL); |
278 clock->Advance(base::TimeDelta::FromSeconds(1)); | 296 clock->Advance(base::TimeDelta::FromSeconds(1)); |
279 reporter.SendPending(); | 297 reporter.SendPending(); |
280 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); | 298 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); |
281 base::RunLoop().RunUntilIdle(); | 299 base::RunLoop().RunUntilIdle(); |
282 EXPECT_EQ(0u, list->items().size()); | 300 EXPECT_EQ(0u, list->items().size()); |
| 301 |
| 302 // report1 was submitted once, failed once, dropped once. |
| 303 // report2 was submitted twice, failed twice, dropped once. |
| 304 // report3 was submitted four times, failed thrice, succeeded once. |
| 305 event_histogram_tester()->SetExpectedValues( |
| 306 7 /* submitted */, 6 /* failed */, 1 /* successful */, 2 /* dropped */); |
283 } | 307 } |
284 | 308 |
285 // Same as above, but retries are disabled. | 309 // Same as above, but retries are disabled. |
286 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, | 310 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, |
287 Reporter_RetriesDisabled) { | 311 Reporter_RetriesDisabled) { |
288 SetExpectedFailedReportCountOnTearDown(2); | 312 SetExpectedFailedReportCountOnTearDown(2); |
289 | 313 |
290 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); | 314 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
291 base::Time reference_time = base::Time::Now(); | 315 base::Time reference_time = base::Time::Now(); |
292 clock->SetNow(reference_time); | 316 clock->SetNow(reference_time); |
(...skipping 29 matching lines...) Expand all Loading... |
322 clock->Advance(base::TimeDelta::FromSeconds(10)); | 346 clock->Advance(base::TimeDelta::FromSeconds(10)); |
323 reporter.Send("report2"); | 347 reporter.Send("report2"); |
324 base::RunLoop().RunUntilIdle(); | 348 base::RunLoop().RunUntilIdle(); |
325 ASSERT_EQ(0u, list->items().size()); | 349 ASSERT_EQ(0u, list->items().size()); |
326 | 350 |
327 // Send pending reports. Nothing should be sent. | 351 // Send pending reports. Nothing should be sent. |
328 clock->Advance(base::TimeDelta::FromSeconds(10)); | 352 clock->Advance(base::TimeDelta::FromSeconds(10)); |
329 reporter.SendPending(); | 353 reporter.SendPending(); |
330 base::RunLoop().RunUntilIdle(); | 354 base::RunLoop().RunUntilIdle(); |
331 ASSERT_EQ(0u, list->items().size()); | 355 ASSERT_EQ(0u, list->items().size()); |
| 356 |
| 357 // report1 and report2 were both submitted once, failed once. |
| 358 event_histogram_tester()->SetExpectedValues( |
| 359 2 /* submitted */, 2 /* failed */, 0 /* successful */, 0 /* dropped */); |
332 } | 360 } |
333 | 361 |
334 class CertificateReportingServiceTest : public ::testing::Test { | 362 class CertificateReportingServiceTest : public ::testing::Test { |
335 public: | 363 public: |
336 CertificateReportingServiceTest() | 364 CertificateReportingServiceTest() |
337 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), | 365 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), |
338 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( | 366 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( |
339 content::BrowserThread::IO)) {} | 367 content::BrowserThread::IO)) {} |
340 | 368 |
341 ~CertificateReportingServiceTest() override {} | 369 ~CertificateReportingServiceTest() override {} |
(...skipping 15 matching lines...) Expand all Loading... |
357 | 385 |
358 clock_.reset(new base::SimpleTestClock()); | 386 clock_.reset(new base::SimpleTestClock()); |
359 service_.reset(new CertificateReportingService( | 387 service_.reset(new CertificateReportingService( |
360 sb_service_.get(), url_request_context_getter(), &profile_, | 388 sb_service_.get(), url_request_context_getter(), &profile_, |
361 test_helper_.server_public_key(), | 389 test_helper_.server_public_key(), |
362 test_helper_.server_public_key_version(), kMaxReportCountInQueue, | 390 test_helper_.server_public_key_version(), kMaxReportCountInQueue, |
363 base::TimeDelta::FromHours(24), clock_.get(), | 391 base::TimeDelta::FromHours(24), clock_.get(), |
364 base::Bind(&CertificateReportingServiceObserver::OnServiceReset, | 392 base::Bind(&CertificateReportingServiceObserver::OnServiceReset, |
365 base::Unretained(&service_observer_)))); | 393 base::Unretained(&service_observer_)))); |
366 service_observer_.WaitForReset(); | 394 service_observer_.WaitForReset(); |
| 395 |
| 396 event_histogram_tester_.reset(new EventHistogramTester()); |
367 } | 397 } |
368 | 398 |
369 void TearDown() override { | 399 void TearDown() override { |
370 WaitForIOThread(); | 400 WaitForIOThread(); |
371 test_helper()->ExpectNoRequests(service()); | 401 test_helper()->ExpectNoRequests(service()); |
372 | 402 |
373 service_->Shutdown(); | 403 service_->Shutdown(); |
374 WaitForIOThread(); | 404 WaitForIOThread(); |
375 service_.reset(nullptr); | 405 service_.reset(nullptr); |
376 | 406 |
377 content::BrowserThread::PostTask( | 407 content::BrowserThread::PostTask( |
378 content::BrowserThread::IO, FROM_HERE, | 408 content::BrowserThread::IO, FROM_HERE, |
379 base::BindOnce(&CertificateReportingServiceTest::TearDownOnIOThread, | 409 base::BindOnce(&CertificateReportingServiceTest::TearDownOnIOThread, |
380 base::Unretained(this))); | 410 base::Unretained(this))); |
381 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 411 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, |
382 base::BindOnce(&ClearURLHandlers)); | 412 base::BindOnce(&ClearURLHandlers)); |
383 WaitForIOThread(); | 413 WaitForIOThread(); |
384 | 414 |
385 histogram_test_helper_.CheckHistogram(); | 415 histogram_test_helper_.CheckHistogram(); |
| 416 event_histogram_tester_.reset(); |
386 } | 417 } |
387 | 418 |
388 protected: | 419 protected: |
389 net::URLRequestContextGetter* url_request_context_getter() { | 420 net::URLRequestContextGetter* url_request_context_getter() { |
390 return url_request_context_getter_.get(); | 421 return url_request_context_getter_.get(); |
391 } | 422 } |
392 | 423 |
393 void WaitForIOThread() { | 424 void WaitForIOThread() { |
394 scoped_refptr<base::ThreadTestHelper> io_helper( | 425 scoped_refptr<base::ThreadTestHelper> io_helper( |
395 new base::ThreadTestHelper(io_task_runner_)); | 426 new base::ThreadTestHelper(io_task_runner_)); |
(...skipping 22 matching lines...) Expand all Loading... |
418 base::BindOnce(&base::SimpleTestClock::SetNow, | 449 base::BindOnce(&base::SimpleTestClock::SetNow, |
419 base::Unretained(clock_.get()), now)); | 450 base::Unretained(clock_.get()), now)); |
420 } | 451 } |
421 | 452 |
422 void SetExpectedFailedReportCountOnTearDown(unsigned int count) { | 453 void SetExpectedFailedReportCountOnTearDown(unsigned int count) { |
423 histogram_test_helper_.SetExpectedFailedReportCount(count); | 454 histogram_test_helper_.SetExpectedFailedReportCount(count); |
424 } | 455 } |
425 | 456 |
426 CertificateReportingServiceTestHelper* test_helper() { return &test_helper_; } | 457 CertificateReportingServiceTestHelper* test_helper() { return &test_helper_; } |
427 | 458 |
| 459 EventHistogramTester* event_histogram_tester() { |
| 460 return event_histogram_tester_.get(); |
| 461 } |
| 462 |
428 private: | 463 private: |
429 void SetUpURLRequestContextOnIOThread() { | 464 void SetUpURLRequestContextOnIOThread() { |
430 std::unique_ptr<net::TestURLRequestContext> url_request_context( | 465 std::unique_ptr<net::TestURLRequestContext> url_request_context( |
431 new net::TestURLRequestContext(false)); | 466 new net::TestURLRequestContext(false)); |
432 url_request_context_getter_ = new net::TestURLRequestContextGetter( | 467 url_request_context_getter_ = new net::TestURLRequestContextGetter( |
433 io_task_runner_, std::move(url_request_context)); | 468 io_task_runner_, std::move(url_request_context)); |
434 } | 469 } |
435 | 470 |
436 void TearDownOnIOThread() { | 471 void TearDownOnIOThread() { |
437 url_request_context_getter_ = nullptr; | 472 url_request_context_getter_ = nullptr; |
438 } | 473 } |
439 | 474 |
440 // Must be initialized before url_request_context_getter_ | 475 // Must be initialized before url_request_context_getter_ |
441 content::TestBrowserThreadBundle thread_bundle_; | 476 content::TestBrowserThreadBundle thread_bundle_; |
442 | 477 |
443 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 478 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
444 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 479 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
445 | 480 |
446 std::unique_ptr<CertificateReportingService> service_; | 481 std::unique_ptr<CertificateReportingService> service_; |
447 std::unique_ptr<base::SimpleTestClock> clock_; | 482 std::unique_ptr<base::SimpleTestClock> clock_; |
448 | 483 |
449 scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_; | 484 scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_; |
450 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; | 485 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; |
451 TestingProfile profile_; | 486 TestingProfile profile_; |
452 | 487 |
453 CertificateReportingServiceTestHelper test_helper_; | 488 CertificateReportingServiceTestHelper test_helper_; |
454 ReportHistogramTestHelper histogram_test_helper_; | 489 ReportHistogramTestHelper histogram_test_helper_; |
455 CertificateReportingServiceObserver service_observer_; | 490 CertificateReportingServiceObserver service_observer_; |
| 491 |
| 492 std::unique_ptr<EventHistogramTester> event_histogram_tester_; |
456 }; | 493 }; |
457 | 494 |
458 TEST_F(CertificateReportingServiceTest, SendSuccessful) { | 495 TEST_F(CertificateReportingServiceTest, SendSuccessful) { |
459 SetExpectedFailedReportCountOnTearDown(0); | 496 SetExpectedFailedReportCountOnTearDown(0); |
460 | 497 |
461 // Let all reports succeed. | 498 // Let all reports succeed. |
462 test_helper()->SetFailureMode(certificate_reporting_test_utils:: | 499 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
463 ReportSendingResult::REPORTS_SUCCESSFUL); | 500 ReportSendingResult::REPORTS_SUCCESSFUL); |
464 | 501 |
465 service()->Send(MakeReport("report0")); | 502 service()->Send(MakeReport("report0")); |
466 service()->Send(MakeReport("report1")); | 503 service()->Send(MakeReport("report1")); |
467 test_helper()->WaitForRequestsDestroyed( | 504 test_helper()->WaitForRequestsDestroyed( |
468 ReportExpectation::Successful({{"report0", RetryStatus::NOT_RETRIED}, | 505 ReportExpectation::Successful({{"report0", RetryStatus::NOT_RETRIED}, |
469 {"report1", RetryStatus::NOT_RETRIED}})); | 506 {"report1", RetryStatus::NOT_RETRIED}})); |
| 507 |
| 508 // report0 and report1 were both submitted once, succeeded once. |
| 509 event_histogram_tester()->SetExpectedValues( |
| 510 2 /* submitted */, 0 /* failed */, 2 /* successful */, 0 /* dropped */); |
470 } | 511 } |
471 | 512 |
472 TEST_F(CertificateReportingServiceTest, SendFailure) { | 513 TEST_F(CertificateReportingServiceTest, SendFailure) { |
473 SetExpectedFailedReportCountOnTearDown(4); | 514 SetExpectedFailedReportCountOnTearDown(4); |
474 | 515 |
475 // Let all reports fail. | 516 // Let all reports fail. |
476 test_helper()->SetFailureMode( | 517 test_helper()->SetFailureMode( |
477 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 518 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
478 | 519 |
479 // Send two reports. Both should fail and get queued. | 520 // Send two reports. Both should fail and get queued. |
(...skipping 15 matching lines...) Expand all Loading... |
495 // Send a third report. This should not be queued. | 536 // Send a third report. This should not be queued. |
496 service()->Send(MakeReport("report2")); | 537 service()->Send(MakeReport("report2")); |
497 test_helper()->WaitForRequestsDestroyed( | 538 test_helper()->WaitForRequestsDestroyed( |
498 ReportExpectation::Successful({{"report2", RetryStatus::NOT_RETRIED}})); | 539 ReportExpectation::Successful({{"report2", RetryStatus::NOT_RETRIED}})); |
499 | 540 |
500 // Send pending reports. Previously failed and queued two reports should be | 541 // Send pending reports. Previously failed and queued two reports should be |
501 // observed. | 542 // observed. |
502 service()->SendPending(); | 543 service()->SendPending(); |
503 test_helper()->WaitForRequestsDestroyed(ReportExpectation::Successful( | 544 test_helper()->WaitForRequestsDestroyed(ReportExpectation::Successful( |
504 {{"report0", RetryStatus::RETRIED}, {"report1", RetryStatus::RETRIED}})); | 545 {{"report0", RetryStatus::RETRIED}, {"report1", RetryStatus::RETRIED}})); |
| 546 |
| 547 // report0 and report1 were both submitted thrice, failed twice, succeeded |
| 548 // once. report2 was submitted once, succeeded once. |
| 549 event_histogram_tester()->SetExpectedValues( |
| 550 7 /* submitted */, 4 /* failed */, 3 /* successful */, 0 /* dropped */); |
505 } | 551 } |
506 | 552 |
507 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) { | 553 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) { |
508 SetExpectedFailedReportCountOnTearDown(0); | 554 SetExpectedFailedReportCountOnTearDown(0); |
509 | 555 |
510 // Let all reports succeed. | 556 // Let all reports succeed. |
511 test_helper()->SetFailureMode(certificate_reporting_test_utils:: | 557 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
512 ReportSendingResult::REPORTS_SUCCESSFUL); | 558 ReportSendingResult::REPORTS_SUCCESSFUL); |
513 | 559 |
514 // Disable the service. | 560 // Disable the service. |
515 SetServiceEnabledAndWait(false); | 561 SetServiceEnabledAndWait(false); |
516 | 562 |
517 // Send a report. Report attempt should be cancelled and no sent reports | 563 // Send a report. Report attempt should be cancelled and no sent reports |
518 // should be observed. | 564 // should be observed. |
519 service()->Send(MakeReport("report0")); | 565 service()->Send(MakeReport("report0")); |
520 | 566 |
521 // Enable the service and send a report again. It should be sent successfully. | 567 // Enable the service and send a report again. It should be sent successfully. |
522 SetServiceEnabledAndWait(true); | 568 SetServiceEnabledAndWait(true); |
523 | 569 |
524 service()->Send(MakeReport("report1")); | 570 service()->Send(MakeReport("report1")); |
525 test_helper()->WaitForRequestsDestroyed( | 571 test_helper()->WaitForRequestsDestroyed( |
526 ReportExpectation::Successful({{"report1", RetryStatus::NOT_RETRIED}})); | 572 ReportExpectation::Successful({{"report1", RetryStatus::NOT_RETRIED}})); |
| 573 |
| 574 // report0 was never sent. report1 was submitted once, succeeded once. |
| 575 event_histogram_tester()->SetExpectedValues( |
| 576 1 /* submitted */, 0 /* failed */, 1 /* successful */, 0 /* dropped */); |
527 } | 577 } |
528 | 578 |
529 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) { | 579 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) { |
530 SetExpectedFailedReportCountOnTearDown(1); | 580 SetExpectedFailedReportCountOnTearDown(1); |
531 | 581 |
532 // Let all reports fail. | 582 // Let all reports fail. |
533 test_helper()->SetFailureMode( | 583 test_helper()->SetFailureMode( |
534 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 584 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
535 | 585 |
536 service()->Send(MakeReport("report0")); | 586 service()->Send(MakeReport("report0")); |
537 test_helper()->WaitForRequestsDestroyed( | 587 test_helper()->WaitForRequestsDestroyed( |
538 ReportExpectation::Failed({{"report0", RetryStatus::NOT_RETRIED}})); | 588 ReportExpectation::Failed({{"report0", RetryStatus::NOT_RETRIED}})); |
539 | 589 |
540 // Disable the service. | 590 // Disable the service. |
541 SetServiceEnabledAndWait(false); | 591 SetServiceEnabledAndWait(false); |
542 | 592 |
543 // Sending has no effect while disabled. | 593 // Sending has no effect while disabled. |
544 service()->SendPending(); | 594 service()->SendPending(); |
545 | 595 |
546 // Re-enable the service and send pending reports. Pending reports should have | 596 // Re-enable the service and send pending reports. Pending reports should have |
547 // been cleared when the service was disabled, so no report should be seen. | 597 // been cleared when the service was disabled, so no report should be seen. |
548 SetServiceEnabledAndWait(true); | 598 SetServiceEnabledAndWait(true); |
549 | 599 |
550 // Sending with empty queue has no effect. | 600 // Sending with empty queue has no effect. |
551 service()->SendPending(); | 601 service()->SendPending(); |
| 602 |
| 603 // report0 was submitted once, failed once. |
| 604 event_histogram_tester()->SetExpectedValues( |
| 605 1 /* submitted */, 1 /* failed */, 0 /* successful */, 0 /* dropped */); |
552 } | 606 } |
553 | 607 |
554 TEST_F(CertificateReportingServiceTest, DontSendOldReports) { | 608 TEST_F(CertificateReportingServiceTest, DontSendOldReports) { |
555 SetExpectedFailedReportCountOnTearDown(5); | 609 SetExpectedFailedReportCountOnTearDown(5); |
556 | 610 |
557 SetNow(base::Time::Now()); | 611 SetNow(base::Time::Now()); |
558 // Let all reports fail. | 612 // Let all reports fail. |
559 test_helper()->SetFailureMode( | 613 test_helper()->SetFailureMode( |
560 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 614 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
561 | 615 |
(...skipping 27 matching lines...) Expand all Loading... |
589 service()->SendPending(); | 643 service()->SendPending(); |
590 test_helper()->WaitForRequestsDestroyed( | 644 test_helper()->WaitForRequestsDestroyed( |
591 ReportExpectation::Failed({{"report2", RetryStatus::RETRIED}})); | 645 ReportExpectation::Failed({{"report2", RetryStatus::RETRIED}})); |
592 | 646 |
593 // Advance the clock 20 hours again so that report2 is 25 hours old and is | 647 // Advance the clock 20 hours again so that report2 is 25 hours old and is |
594 // older than max age (24 hours) | 648 // older than max age (24 hours) |
595 AdvanceClock(base::TimeDelta::FromHours(20)); | 649 AdvanceClock(base::TimeDelta::FromHours(20)); |
596 // Send pending reports. report2 should be discarded since it's too old. No | 650 // Send pending reports. report2 should be discarded since it's too old. No |
597 // other reports remain. | 651 // other reports remain. |
598 service()->SendPending(); | 652 service()->SendPending(); |
| 653 |
| 654 // report0 was submitted once, failed once, dropped once. |
| 655 // report1 was submitted twice, failed twice, dropped once. |
| 656 // report2 was submitted twice, failed twice, dropped once. |
| 657 // Need to wait for SendPending() to complete. |
| 658 event_histogram_tester()->SetExpectedValues( |
| 659 5 /* submitted */, 5 /* failed */, 0 /* successful */, 3 /* dropped */); |
599 } | 660 } |
600 | 661 |
601 TEST_F(CertificateReportingServiceTest, DiscardOldReports) { | 662 TEST_F(CertificateReportingServiceTest, DiscardOldReports) { |
602 SetExpectedFailedReportCountOnTearDown(7); | 663 SetExpectedFailedReportCountOnTearDown(7); |
603 | 664 |
604 SetNow(base::Time::Now()); | 665 SetNow(base::Time::Now()); |
605 // Let all reports fail. | 666 // Let all reports fail. |
606 test_helper()->SetFailureMode( | 667 test_helper()->SetFailureMode( |
607 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 668 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
608 | 669 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 // report3 is 15 hours old. | 708 // report3 is 15 hours old. |
648 AdvanceClock(base::TimeDelta::FromHours(15)); | 709 AdvanceClock(base::TimeDelta::FromHours(15)); |
649 // Send pending reports. Only report2 and report3 should be sent, report1 | 710 // Send pending reports. Only report2 and report3 should be sent, report1 |
650 // should be ignored because it's too old. | 711 // should be ignored because it's too old. |
651 service()->SendPending(); | 712 service()->SendPending(); |
652 test_helper()->WaitForRequestsDestroyed(ReportExpectation::Successful( | 713 test_helper()->WaitForRequestsDestroyed(ReportExpectation::Successful( |
653 {{"report2", RetryStatus::RETRIED}, {"report3", RetryStatus::RETRIED}})); | 714 {{"report2", RetryStatus::RETRIED}, {"report3", RetryStatus::RETRIED}})); |
654 | 715 |
655 // Do a final send. No reports should be sent. | 716 // Do a final send. No reports should be sent. |
656 service()->SendPending(); | 717 service()->SendPending(); |
| 718 |
| 719 // report0 was submitted once, failed once, dropped once. |
| 720 // report1 was submitted twice, failed twice, dropped once. |
| 721 // report2 was submitted thrice, failed twice, succeeded once. |
| 722 // report3 was submitted thrice, failed twice, succeeded once. |
| 723 event_histogram_tester()->SetExpectedValues( |
| 724 9 /* submitted */, 7 /* failed */, 2 /* successful */, 2 /* dropped */); |
657 } | 725 } |
658 | 726 |
659 // A delayed report should successfully upload when it's resumed. | 727 // A delayed report should successfully upload when it's resumed. |
660 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) { | 728 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) { |
661 SetExpectedFailedReportCountOnTearDown(0); | 729 SetExpectedFailedReportCountOnTearDown(0); |
662 | 730 |
663 // Let reports hang. | 731 // Let reports hang. |
664 test_helper()->SetFailureMode( | 732 test_helper()->SetFailureMode( |
665 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); | 733 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
666 // Send a report. The report upload hangs, so no error or success callbacks | 734 // Send a report. The report upload hangs, so no error or success callbacks |
667 // should be called. | 735 // should be called. |
668 service()->Send(MakeReport("report0")); | 736 service()->Send(MakeReport("report0")); |
669 | 737 |
670 // Resume the report upload and run the callbacks. The report should be | 738 // Resume the report upload and run the callbacks. The report should be |
671 // successfully sent. | 739 // successfully sent. |
672 test_helper()->ResumeDelayedRequest(); | 740 test_helper()->ResumeDelayedRequest(); |
673 test_helper()->WaitForRequestsDestroyed( | 741 test_helper()->WaitForRequestsDestroyed( |
674 ReportExpectation::Delayed({{"report0", RetryStatus::NOT_RETRIED}})); | 742 ReportExpectation::Delayed({{"report0", RetryStatus::NOT_RETRIED}})); |
| 743 |
| 744 // report0 was submitted once, succeeded once. |
| 745 event_histogram_tester()->SetExpectedValues( |
| 746 1 /* submitted */, 0 /* failed */, 1 /* successful */, 0 /* dropped */); |
675 } | 747 } |
676 | 748 |
677 // Delayed reports should cleaned when the service is reset. | 749 // Delayed reports should cleaned when the service is reset. |
678 TEST_F(CertificateReportingServiceTest, Delayed_Reset) { | 750 TEST_F(CertificateReportingServiceTest, Delayed_Reset) { |
679 SetExpectedFailedReportCountOnTearDown(0); | 751 SetExpectedFailedReportCountOnTearDown(0); |
680 | 752 |
681 // Let reports hang. | 753 // Let reports hang. |
682 test_helper()->SetFailureMode( | 754 test_helper()->SetFailureMode( |
683 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); | 755 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
684 // Send a report. The report is triggered but hangs, so no error or success | 756 // Send a report. The report is triggered but hangs, so no error or success |
(...skipping 15 matching lines...) Expand all Loading... |
700 // Send a report. The report is triggered but hangs, so no error or success | 772 // Send a report. The report is triggered but hangs, so no error or success |
701 // callbacks should be called. The report id is again 0 since the pending | 773 // callbacks should be called. The report id is again 0 since the pending |
702 // report queue has been cleared above. | 774 // report queue has been cleared above. |
703 service()->Send(MakeReport("report1")); | 775 service()->Send(MakeReport("report1")); |
704 | 776 |
705 // Resume delayed report. Two reports are successfully sent. | 777 // Resume delayed report. Two reports are successfully sent. |
706 test_helper()->ResumeDelayedRequest(); | 778 test_helper()->ResumeDelayedRequest(); |
707 test_helper()->WaitForRequestsDestroyed( | 779 test_helper()->WaitForRequestsDestroyed( |
708 ReportExpectation::Delayed({{"report0", RetryStatus::NOT_RETRIED}, | 780 ReportExpectation::Delayed({{"report0", RetryStatus::NOT_RETRIED}, |
709 {"report1", RetryStatus::NOT_RETRIED}})); | 781 {"report1", RetryStatus::NOT_RETRIED}})); |
| 782 |
| 783 // report0 was submitted once, but neither failed nor succeeded because the |
| 784 // report queue was cleared. report1 was submitted once, succeeded once. |
| 785 event_histogram_tester()->SetExpectedValues( |
| 786 2 /* submitted */, 0 /* failed */, 1 /* successful */, 0 /* dropped */); |
710 } | 787 } |
OLD | NEW |