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

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

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

Powered by Google App Engine
This is Rietveld 408576698