Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: chrome/browser/safe_browsing/certificate_reporting_service_unittest.cc

Issue 2862453002: Add metrics for certificate report uploads (Closed)
Patch Set: histograms.xml Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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
208 std::unique_ptr<EventHistogramTester> event_histogram_tester_;
194 }; 209 };
195 210
196 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, 211 TEST_F(CertificateReportingServiceReporterOnIOThreadTest,
197 Reporter_RetriesEnabled) { 212 Reporter_RetriesEnabled) {
198 SetExpectedFailedReportCountOnTearDown(6); 213 SetExpectedFailedReportCountOnTearDown(6);
199 214
200 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); 215 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock());
201 const base::Time reference_time = base::Time::Now(); 216 const base::Time reference_time = base::Time::Now();
202 clock->SetNow(reference_time); 217 clock->SetNow(reference_time);
203 218
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 // Send pending reports again, this time successfully. There should be no 288 // Send pending reports again, this time successfully. There should be no
274 // pending reports left. 289 // pending reports left.
275 const GURL kSuccessURL = 290 const GURL kSuccessURL =
276 net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); 291 net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
277 certificate_error_reporter->set_upload_url_for_testing(kSuccessURL); 292 certificate_error_reporter->set_upload_url_for_testing(kSuccessURL);
278 clock->Advance(base::TimeDelta::FromSeconds(1)); 293 clock->Advance(base::TimeDelta::FromSeconds(1));
279 reporter.SendPending(); 294 reporter.SendPending();
280 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); 295 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing());
281 base::RunLoop().RunUntilIdle(); 296 base::RunLoop().RunUntilIdle();
282 EXPECT_EQ(0u, list->items().size()); 297 EXPECT_EQ(0u, list->items().size());
298
299 // report1 was submitted once, failed once, dropped once.
300 // report2 was submitted twice, failed twice, dropped once.
301 // report3 was submitted four times, failed thrice, succeeded once.
302 event_histogram_tester()->SetExpectedValues(
303 7 /* submitted */, 6 /* failed */, 1 /* successful */, 2 /* dropped */);
283 } 304 }
284 305
285 // Same as above, but retries are disabled. 306 // Same as above, but retries are disabled.
286 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, 307 TEST_F(CertificateReportingServiceReporterOnIOThreadTest,
287 Reporter_RetriesDisabled) { 308 Reporter_RetriesDisabled) {
288 SetExpectedFailedReportCountOnTearDown(2); 309 SetExpectedFailedReportCountOnTearDown(2);
289 310
290 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); 311 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock());
291 base::Time reference_time = base::Time::Now(); 312 base::Time reference_time = base::Time::Now();
292 clock->SetNow(reference_time); 313 clock->SetNow(reference_time);
(...skipping 29 matching lines...) Expand all
322 clock->Advance(base::TimeDelta::FromSeconds(10)); 343 clock->Advance(base::TimeDelta::FromSeconds(10));
323 reporter.Send("report2"); 344 reporter.Send("report2");
324 base::RunLoop().RunUntilIdle(); 345 base::RunLoop().RunUntilIdle();
325 ASSERT_EQ(0u, list->items().size()); 346 ASSERT_EQ(0u, list->items().size());
326 347
327 // Send pending reports. Nothing should be sent. 348 // Send pending reports. Nothing should be sent.
328 clock->Advance(base::TimeDelta::FromSeconds(10)); 349 clock->Advance(base::TimeDelta::FromSeconds(10));
329 reporter.SendPending(); 350 reporter.SendPending();
330 base::RunLoop().RunUntilIdle(); 351 base::RunLoop().RunUntilIdle();
331 ASSERT_EQ(0u, list->items().size()); 352 ASSERT_EQ(0u, list->items().size());
353
354 // report1 and report2 were both submitted once, failed once.
355 event_histogram_tester()->SetExpectedValues(
356 2 /* submitted */, 2 /* failed */, 0 /* successful */, 0 /* dropped */);
332 } 357 }
333 358
334 class CertificateReportingServiceTest : public ::testing::Test { 359 class CertificateReportingServiceTest : public ::testing::Test {
335 public: 360 public:
336 CertificateReportingServiceTest() 361 CertificateReportingServiceTest()
337 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), 362 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD),
338 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( 363 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread(
339 content::BrowserThread::IO)) {} 364 content::BrowserThread::IO)) {}
340 365
341 ~CertificateReportingServiceTest() override {} 366 ~CertificateReportingServiceTest() override {}
(...skipping 15 matching lines...) Expand all
357 382
358 clock_.reset(new base::SimpleTestClock()); 383 clock_.reset(new base::SimpleTestClock());
359 service_.reset(new CertificateReportingService( 384 service_.reset(new CertificateReportingService(
360 sb_service_.get(), url_request_context_getter(), &profile_, 385 sb_service_.get(), url_request_context_getter(), &profile_,
361 test_helper_.server_public_key(), 386 test_helper_.server_public_key(),
362 test_helper_.server_public_key_version(), kMaxReportCountInQueue, 387 test_helper_.server_public_key_version(), kMaxReportCountInQueue,
363 base::TimeDelta::FromHours(24), clock_.get(), 388 base::TimeDelta::FromHours(24), clock_.get(),
364 base::Bind(&CertificateReportingServiceObserver::OnServiceReset, 389 base::Bind(&CertificateReportingServiceObserver::OnServiceReset,
365 base::Unretained(&service_observer_)))); 390 base::Unretained(&service_observer_))));
366 service_observer_.WaitForReset(); 391 service_observer_.WaitForReset();
392
393 event_histogram_tester_.reset(new EventHistogramTester());
367 } 394 }
368 395
369 void TearDown() override { 396 void TearDown() override {
370 WaitForIOThread(); 397 WaitForIOThread();
371 test_helper()->ExpectNoRequests(service()); 398 test_helper()->ExpectNoRequests(service());
372 399
373 service_->Shutdown(); 400 service_->Shutdown();
374 WaitForIOThread(); 401 WaitForIOThread();
375 service_.reset(nullptr); 402 service_.reset(nullptr);
376 403
377 content::BrowserThread::PostTask( 404 content::BrowserThread::PostTask(
378 content::BrowserThread::IO, FROM_HERE, 405 content::BrowserThread::IO, FROM_HERE,
379 base::BindOnce(&CertificateReportingServiceTest::TearDownOnIOThread, 406 base::BindOnce(&CertificateReportingServiceTest::TearDownOnIOThread,
380 base::Unretained(this))); 407 base::Unretained(this)));
381 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, 408 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
382 base::BindOnce(&ClearURLHandlers)); 409 base::BindOnce(&ClearURLHandlers));
383 WaitForIOThread(); 410 WaitForIOThread();
384 411
385 histogram_test_helper_.CheckHistogram(); 412 histogram_test_helper_.CheckHistogram();
413 event_histogram_tester_.reset();
386 } 414 }
387 415
388 protected: 416 protected:
389 net::URLRequestContextGetter* url_request_context_getter() { 417 net::URLRequestContextGetter* url_request_context_getter() {
390 return url_request_context_getter_.get(); 418 return url_request_context_getter_.get();
391 } 419 }
392 420
393 void WaitForIOThread() { 421 void WaitForIOThread() {
394 scoped_refptr<base::ThreadTestHelper> io_helper( 422 scoped_refptr<base::ThreadTestHelper> io_helper(
395 new base::ThreadTestHelper(io_task_runner_)); 423 new base::ThreadTestHelper(io_task_runner_));
(...skipping 22 matching lines...) Expand all
418 base::BindOnce(&base::SimpleTestClock::SetNow, 446 base::BindOnce(&base::SimpleTestClock::SetNow,
419 base::Unretained(clock_.get()), now)); 447 base::Unretained(clock_.get()), now));
420 } 448 }
421 449
422 void SetExpectedFailedReportCountOnTearDown(unsigned int count) { 450 void SetExpectedFailedReportCountOnTearDown(unsigned int count) {
423 histogram_test_helper_.SetExpectedFailedReportCount(count); 451 histogram_test_helper_.SetExpectedFailedReportCount(count);
424 } 452 }
425 453
426 CertificateReportingServiceTestHelper* test_helper() { return &test_helper_; } 454 CertificateReportingServiceTestHelper* test_helper() { return &test_helper_; }
427 455
456 EventHistogramTester* event_histogram_tester() {
457 return event_histogram_tester_.get();
458 }
459
428 private: 460 private:
429 void SetUpURLRequestContextOnIOThread() { 461 void SetUpURLRequestContextOnIOThread() {
430 std::unique_ptr<net::TestURLRequestContext> url_request_context( 462 std::unique_ptr<net::TestURLRequestContext> url_request_context(
431 new net::TestURLRequestContext(false)); 463 new net::TestURLRequestContext(false));
432 url_request_context_getter_ = new net::TestURLRequestContextGetter( 464 url_request_context_getter_ = new net::TestURLRequestContextGetter(
433 io_task_runner_, std::move(url_request_context)); 465 io_task_runner_, std::move(url_request_context));
434 } 466 }
435 467
436 void TearDownOnIOThread() { 468 void TearDownOnIOThread() {
437 url_request_context_getter_ = nullptr; 469 url_request_context_getter_ = nullptr;
438 } 470 }
439 471
440 // Must be initialized before url_request_context_getter_ 472 // Must be initialized before url_request_context_getter_
441 content::TestBrowserThreadBundle thread_bundle_; 473 content::TestBrowserThreadBundle thread_bundle_;
442 474
443 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 475 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
444 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 476 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
445 477
446 std::unique_ptr<CertificateReportingService> service_; 478 std::unique_ptr<CertificateReportingService> service_;
447 std::unique_ptr<base::SimpleTestClock> clock_; 479 std::unique_ptr<base::SimpleTestClock> clock_;
448 480
449 scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_; 481 scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_;
450 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; 482 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory;
451 TestingProfile profile_; 483 TestingProfile profile_;
452 484
453 CertificateReportingServiceTestHelper test_helper_; 485 CertificateReportingServiceTestHelper test_helper_;
454 ReportHistogramTestHelper histogram_test_helper_; 486 ReportHistogramTestHelper histogram_test_helper_;
455 CertificateReportingServiceObserver service_observer_; 487 CertificateReportingServiceObserver service_observer_;
488
489 std::unique_ptr<EventHistogramTester> event_histogram_tester_;
456 }; 490 };
457 491
458 TEST_F(CertificateReportingServiceTest, SendSuccessful) { 492 TEST_F(CertificateReportingServiceTest, SendSuccessful) {
459 SetExpectedFailedReportCountOnTearDown(0); 493 SetExpectedFailedReportCountOnTearDown(0);
460 494
461 // Let all reports succeed. 495 // Let all reports succeed.
462 test_helper()->SetFailureMode(certificate_reporting_test_utils:: 496 test_helper()->SetFailureMode(certificate_reporting_test_utils::
463 ReportSendingResult::REPORTS_SUCCESSFUL); 497 ReportSendingResult::REPORTS_SUCCESSFUL);
464 498
465 service()->Send(MakeReport("report0")); 499 service()->Send(MakeReport("report0"));
466 service()->Send(MakeReport("report1")); 500 service()->Send(MakeReport("report1"));
467 test_helper()->WaitForRequestsDestroyed( 501 test_helper()->WaitForRequestsDestroyed(
468 ReportExpectation::Successful({{"report0", RetryStatus::NOT_RETRIED}, 502 ReportExpectation::Successful({{"report0", RetryStatus::NOT_RETRIED},
469 {"report1", RetryStatus::NOT_RETRIED}})); 503 {"report1", RetryStatus::NOT_RETRIED}}));
504
505 // report0 and report1 were both submitted once, succeeded once.
506 event_histogram_tester()->SetExpectedValues(
507 2 /* submitted */, 0 /* failed */, 2 /* successful */, 0 /* dropped */);
470 } 508 }
471 509
472 TEST_F(CertificateReportingServiceTest, SendFailure) { 510 TEST_F(CertificateReportingServiceTest, SendFailure) {
473 SetExpectedFailedReportCountOnTearDown(4); 511 SetExpectedFailedReportCountOnTearDown(4);
474 512
475 // Let all reports fail. 513 // Let all reports fail.
476 test_helper()->SetFailureMode( 514 test_helper()->SetFailureMode(
477 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); 515 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
478 516
479 // Send two reports. Both should fail and get queued. 517 // Send two reports. Both should fail and get queued.
(...skipping 15 matching lines...) Expand all
495 // Send a third report. This should not be queued. 533 // Send a third report. This should not be queued.
496 service()->Send(MakeReport("report2")); 534 service()->Send(MakeReport("report2"));
497 test_helper()->WaitForRequestsDestroyed( 535 test_helper()->WaitForRequestsDestroyed(
498 ReportExpectation::Successful({{"report2", RetryStatus::NOT_RETRIED}})); 536 ReportExpectation::Successful({{"report2", RetryStatus::NOT_RETRIED}}));
499 537
500 // Send pending reports. Previously failed and queued two reports should be 538 // Send pending reports. Previously failed and queued two reports should be
501 // observed. 539 // observed.
502 service()->SendPending(); 540 service()->SendPending();
503 test_helper()->WaitForRequestsDestroyed(ReportExpectation::Successful( 541 test_helper()->WaitForRequestsDestroyed(ReportExpectation::Successful(
504 {{"report0", RetryStatus::RETRIED}, {"report1", RetryStatus::RETRIED}})); 542 {{"report0", RetryStatus::RETRIED}, {"report1", RetryStatus::RETRIED}}));
543
544 // report0 and report1 were both submitted thrice, failed twice, succeeded
545 // once. report2 was submitted once, succeeded once.
546 event_histogram_tester()->SetExpectedValues(
547 7 /* submitted */, 4 /* failed */, 3 /* successful */, 0 /* dropped */);
505 } 548 }
506 549
507 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) { 550 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) {
508 SetExpectedFailedReportCountOnTearDown(0); 551 SetExpectedFailedReportCountOnTearDown(0);
509 552
510 // Let all reports succeed. 553 // Let all reports succeed.
511 test_helper()->SetFailureMode(certificate_reporting_test_utils:: 554 test_helper()->SetFailureMode(certificate_reporting_test_utils::
512 ReportSendingResult::REPORTS_SUCCESSFUL); 555 ReportSendingResult::REPORTS_SUCCESSFUL);
513 556
514 // Disable the service. 557 // Disable the service.
515 SetServiceEnabledAndWait(false); 558 SetServiceEnabledAndWait(false);
516 559
517 // Send a report. Report attempt should be cancelled and no sent reports 560 // Send a report. Report attempt should be cancelled and no sent reports
518 // should be observed. 561 // should be observed.
519 service()->Send(MakeReport("report0")); 562 service()->Send(MakeReport("report0"));
520 563
521 // Enable the service and send a report again. It should be sent successfully. 564 // Enable the service and send a report again. It should be sent successfully.
522 SetServiceEnabledAndWait(true); 565 SetServiceEnabledAndWait(true);
523 566
524 service()->Send(MakeReport("report1")); 567 service()->Send(MakeReport("report1"));
525 test_helper()->WaitForRequestsDestroyed( 568 test_helper()->WaitForRequestsDestroyed(
526 ReportExpectation::Successful({{"report1", RetryStatus::NOT_RETRIED}})); 569 ReportExpectation::Successful({{"report1", RetryStatus::NOT_RETRIED}}));
570
571 // report0 was never sent. report1 was submitted once, succeeded once.
572 event_histogram_tester()->SetExpectedValues(
573 1 /* submitted */, 0 /* failed */, 1 /* successful */, 0 /* dropped */);
527 } 574 }
528 575
529 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) { 576 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) {
530 SetExpectedFailedReportCountOnTearDown(1); 577 SetExpectedFailedReportCountOnTearDown(1);
531 578
532 // Let all reports fail. 579 // Let all reports fail.
533 test_helper()->SetFailureMode( 580 test_helper()->SetFailureMode(
534 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); 581 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
535 582
536 service()->Send(MakeReport("report0")); 583 service()->Send(MakeReport("report0"));
537 test_helper()->WaitForRequestsDestroyed( 584 test_helper()->WaitForRequestsDestroyed(
538 ReportExpectation::Failed({{"report0", RetryStatus::NOT_RETRIED}})); 585 ReportExpectation::Failed({{"report0", RetryStatus::NOT_RETRIED}}));
539 586
540 // Disable the service. 587 // Disable the service.
541 SetServiceEnabledAndWait(false); 588 SetServiceEnabledAndWait(false);
542 589
543 // Sending has no effect while disabled. 590 // Sending has no effect while disabled.
544 service()->SendPending(); 591 service()->SendPending();
545 592
546 // Re-enable the service and send pending reports. Pending reports should have 593 // 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. 594 // been cleared when the service was disabled, so no report should be seen.
548 SetServiceEnabledAndWait(true); 595 SetServiceEnabledAndWait(true);
549 596
550 // Sending with empty queue has no effect. 597 // Sending with empty queue has no effect.
551 service()->SendPending(); 598 service()->SendPending();
599
600 // report0 was submitted once, failed once.
601 event_histogram_tester()->SetExpectedValues(
602 1 /* submitted */, 1 /* failed */, 0 /* successful */, 0 /* dropped */);
552 } 603 }
553 604
554 TEST_F(CertificateReportingServiceTest, DontSendOldReports) { 605 TEST_F(CertificateReportingServiceTest, DontSendOldReports) {
555 SetExpectedFailedReportCountOnTearDown(5); 606 SetExpectedFailedReportCountOnTearDown(5);
556 607
557 SetNow(base::Time::Now()); 608 SetNow(base::Time::Now());
558 // Let all reports fail. 609 // Let all reports fail.
559 test_helper()->SetFailureMode( 610 test_helper()->SetFailureMode(
560 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); 611 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
561 612
(...skipping 27 matching lines...) Expand all
589 service()->SendPending(); 640 service()->SendPending();
590 test_helper()->WaitForRequestsDestroyed( 641 test_helper()->WaitForRequestsDestroyed(
591 ReportExpectation::Failed({{"report2", RetryStatus::RETRIED}})); 642 ReportExpectation::Failed({{"report2", RetryStatus::RETRIED}}));
592 643
593 // Advance the clock 20 hours again so that report2 is 25 hours old and is 644 // Advance the clock 20 hours again so that report2 is 25 hours old and is
594 // older than max age (24 hours) 645 // older than max age (24 hours)
595 AdvanceClock(base::TimeDelta::FromHours(20)); 646 AdvanceClock(base::TimeDelta::FromHours(20));
596 // Send pending reports. report2 should be discarded since it's too old. No 647 // Send pending reports. report2 should be discarded since it's too old. No
597 // other reports remain. 648 // other reports remain.
598 service()->SendPending(); 649 service()->SendPending();
650
651 // report0 was submitted once, failed once, dropped once.
652 // report1 was submitted twice, failed twice, dropped once.
653 // report2 was submitted twice, failed twice, dropped once.
654 // Need to wait for SendPending() to complete.
655 event_histogram_tester()->SetExpectedValues(
656 5 /* submitted */, 5 /* failed */, 0 /* successful */, 3 /* dropped */);
599 } 657 }
600 658
601 TEST_F(CertificateReportingServiceTest, DiscardOldReports) { 659 TEST_F(CertificateReportingServiceTest, DiscardOldReports) {
602 SetExpectedFailedReportCountOnTearDown(7); 660 SetExpectedFailedReportCountOnTearDown(7);
603 661
604 SetNow(base::Time::Now()); 662 SetNow(base::Time::Now());
605 // Let all reports fail. 663 // Let all reports fail.
606 test_helper()->SetFailureMode( 664 test_helper()->SetFailureMode(
607 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); 665 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
608 666
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 // report3 is 15 hours old. 705 // report3 is 15 hours old.
648 AdvanceClock(base::TimeDelta::FromHours(15)); 706 AdvanceClock(base::TimeDelta::FromHours(15));
649 // Send pending reports. Only report2 and report3 should be sent, report1 707 // Send pending reports. Only report2 and report3 should be sent, report1
650 // should be ignored because it's too old. 708 // should be ignored because it's too old.
651 service()->SendPending(); 709 service()->SendPending();
652 test_helper()->WaitForRequestsDestroyed(ReportExpectation::Successful( 710 test_helper()->WaitForRequestsDestroyed(ReportExpectation::Successful(
653 {{"report2", RetryStatus::RETRIED}, {"report3", RetryStatus::RETRIED}})); 711 {{"report2", RetryStatus::RETRIED}, {"report3", RetryStatus::RETRIED}}));
654 712
655 // Do a final send. No reports should be sent. 713 // Do a final send. No reports should be sent.
656 service()->SendPending(); 714 service()->SendPending();
715
716 // report0 was submitted once, failed once, dropped once.
717 // report1 was submitted twice, failed twice, dropped once.
718 // report2 was submitted thrice, failed twice, succeeded once.
719 // report3 was submitted thrice, failed twice, succeeded once.
720 event_histogram_tester()->SetExpectedValues(
721 9 /* submitted */, 7 /* failed */, 2 /* successful */, 2 /* dropped */);
657 } 722 }
658 723
659 // A delayed report should successfully upload when it's resumed. 724 // A delayed report should successfully upload when it's resumed.
660 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) { 725 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) {
661 SetExpectedFailedReportCountOnTearDown(0); 726 SetExpectedFailedReportCountOnTearDown(0);
662 727
663 // Let reports hang. 728 // Let reports hang.
664 test_helper()->SetFailureMode( 729 test_helper()->SetFailureMode(
665 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); 730 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY);
666 // Send a report. The report upload hangs, so no error or success callbacks 731 // Send a report. The report upload hangs, so no error or success callbacks
667 // should be called. 732 // should be called.
668 service()->Send(MakeReport("report0")); 733 service()->Send(MakeReport("report0"));
669 734
670 // Resume the report upload and run the callbacks. The report should be 735 // Resume the report upload and run the callbacks. The report should be
671 // successfully sent. 736 // successfully sent.
672 test_helper()->ResumeDelayedRequest(); 737 test_helper()->ResumeDelayedRequest();
673 test_helper()->WaitForRequestsDestroyed( 738 test_helper()->WaitForRequestsDestroyed(
674 ReportExpectation::Delayed({{"report0", RetryStatus::NOT_RETRIED}})); 739 ReportExpectation::Delayed({{"report0", RetryStatus::NOT_RETRIED}}));
740
741 // report0 was submitted once, succeeded once.
742 event_histogram_tester()->SetExpectedValues(
743 1 /* submitted */, 0 /* failed */, 1 /* successful */, 0 /* dropped */);
675 } 744 }
676 745
677 // Delayed reports should cleaned when the service is reset. 746 // Delayed reports should cleaned when the service is reset.
678 TEST_F(CertificateReportingServiceTest, Delayed_Reset) { 747 TEST_F(CertificateReportingServiceTest, Delayed_Reset) {
679 SetExpectedFailedReportCountOnTearDown(0); 748 SetExpectedFailedReportCountOnTearDown(0);
680 749
681 // Let reports hang. 750 // Let reports hang.
682 test_helper()->SetFailureMode( 751 test_helper()->SetFailureMode(
683 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); 752 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY);
684 // Send a report. The report is triggered but hangs, so no error or success 753 // Send a report. The report is triggered but hangs, so no error or success
(...skipping 15 matching lines...) Expand all
700 // Send a report. The report is triggered but hangs, so no error or success 769 // 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 770 // callbacks should be called. The report id is again 0 since the pending
702 // report queue has been cleared above. 771 // report queue has been cleared above.
703 service()->Send(MakeReport("report1")); 772 service()->Send(MakeReport("report1"));
704 773
705 // Resume delayed report. Two reports are successfully sent. 774 // Resume delayed report. Two reports are successfully sent.
706 test_helper()->ResumeDelayedRequest(); 775 test_helper()->ResumeDelayedRequest();
707 test_helper()->WaitForRequestsDestroyed( 776 test_helper()->WaitForRequestsDestroyed(
708 ReportExpectation::Delayed({{"report0", RetryStatus::NOT_RETRIED}, 777 ReportExpectation::Delayed({{"report0", RetryStatus::NOT_RETRIED},
709 {"report1", RetryStatus::NOT_RETRIED}})); 778 {"report1", RetryStatus::NOT_RETRIED}}));
779
780 // report0 was submitted once, but neither failed nor succeeded because the
781 // report queue was cleared. report1 was submitted once, succeeded once.
782 event_histogram_tester()->SetExpectedValues(
783 2 /* submitted */, 0 /* failed */, 1 /* successful */, 0 /* dropped */);
710 } 784 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698