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

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

Issue 2605403002: Fix flaky CertificateReportingService browser tests. (Closed)
Patch Set: Fix broken tests Created 3 years, 11 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/bind.h" 10 #include "base/bind.h"
10 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
11 #include "base/run_loop.h" 12 #include "base/run_loop.h"
12 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
13 #include "base/test/histogram_tester.h" 14 #include "base/test/histogram_tester.h"
14 #include "base/test/simple_test_clock.h" 15 #include "base/test/simple_test_clock.h"
15 #include "base/test/thread_test_helper.h" 16 #include "base/test/thread_test_helper.h"
16 #include "base/time/clock.h" 17 #include "base/time/clock.h"
17 #include "base/time/time.h" 18 #include "base/time/time.h"
18 #include "chrome/browser/safe_browsing/certificate_reporting_service_test_utils. h" 19 #include "chrome/browser/safe_browsing/certificate_reporting_service_test_utils. h"
19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 20 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
20 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" 21 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
21 #include "chrome/test/base/testing_profile.h" 22 #include "chrome/test/base/testing_profile.h"
23 #include "components/certificate_reporting/error_report.h"
22 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
23 #include "content/public/test/test_browser_thread.h" 25 #include "content/public/test/test_browser_thread.h"
24 #include "content/public/test/test_browser_thread_bundle.h" 26 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "net/base/network_delegate_impl.h" 27 #include "crypto/rsa_private_key.h"
28 #include "net/cert/x509_certificate.h"
29 #include "net/cert/x509_util.h"
30 #include "net/ssl/ssl_info.h"
26 #include "net/test/url_request/url_request_failed_job.h" 31 #include "net/test/url_request/url_request_failed_job.h"
27 #include "net/test/url_request/url_request_mock_data_job.h" 32 #include "net/test/url_request/url_request_mock_data_job.h"
28 #include "net/url_request/url_request_filter.h" 33 #include "net/url_request/url_request_filter.h"
29 #include "net/url_request/url_request_test_util.h" 34 #include "net/url_request/url_request_test_util.h"
30 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
31 36
32 using certificate_reporting_test_utils::CertificateReportingServiceTestHelper; 37 using certificate_reporting_test_utils::CertificateReportingServiceTestHelper;
38 using certificate_reporting_test_utils::CertificateReportingServiceObserver;
33 using certificate_reporting_test_utils::ReportExpectation; 39 using certificate_reporting_test_utils::ReportExpectation;
34 40
35 namespace { 41 namespace {
36 42
37 // Maximum number of reports kept in the certificate reporting service's retry 43 // Maximum number of reports kept in the certificate reporting service's retry
38 // queue. 44 // queue.
39 const size_t kMaxReportCountInQueue = 3; 45 const size_t kMaxReportCountInQueue = 3;
40 46
41 const char* kFailedReportHistogram = "SSL.CertificateErrorReportFailure"; 47 const char* kFailedReportHistogram = "SSL.CertificateErrorReportFailure";
42 48
49 // NSS requires that serial numbers be unique even for the same issuer;
50 // as all fake certificates will contain the same issuer name, it's
51 // necessary to ensure the serial number is unique, as otherwise
52 // NSS will fail to parse.
53 base::StaticAtomicSequenceNumber g_serial_number;
54
55 scoped_refptr<net::X509Certificate> CreateFakeCert() {
56 std::unique_ptr<crypto::RSAPrivateKey> unused_key;
57 std::string cert_der;
58 if (!net::x509_util::CreateKeyAndSelfSignedCert(
59 "CN=Error", static_cast<uint32_t>(g_serial_number.GetNext()),
60 base::Time::Now() - base::TimeDelta::FromMinutes(5),
61 base::Time::Now() + base::TimeDelta::FromMinutes(5), &unused_key,
62 &cert_der)) {
63 return nullptr;
64 }
65 return net::X509Certificate::CreateFromBytes(cert_der.data(),
66 cert_der.size());
67 }
68
69 std::string MakeReport(const std::string& hostname) {
70 net::SSLInfo ssl_info;
71 ssl_info.cert = ssl_info.unverified_cert = CreateFakeCert();
72
73 certificate_reporting::ErrorReport report(hostname, ssl_info);
74 std::string serialized_report;
75 EXPECT_TRUE(report.Serialize(&serialized_report));
76 return serialized_report;
77 }
78
43 void ClearURLHandlers() { 79 void ClearURLHandlers() {
44 net::URLRequestFilter::GetInstance()->ClearHandlers(); 80 net::URLRequestFilter::GetInstance()->ClearHandlers();
45 } 81 }
46 82
47 // A network delegate used to observe URL request destructions. The tests check 83 // Class for histogram testing. The failed report histogram is checked once
48 // that no outstanding URL request is present during tear down.
49 class TestNetworkDelegate : public net::NetworkDelegateImpl {
50 public:
51 TestNetworkDelegate(
52 const base::Callback<void()>& url_request_destroyed_callback)
53 : url_request_destroyed_callback_(url_request_destroyed_callback) {}
54
55 ~TestNetworkDelegate() override {}
56
57 // net::NetworkDelegate method:
58 void OnURLRequestDestroyed(net::URLRequest* request) override {
59 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
60 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
61 url_request_destroyed_callback_);
62 }
63
64 private:
65 base::Callback<void()> url_request_destroyed_callback_;
66 };
67
68 // Base class for histogram testing. The failed report histogram is checked once
69 // after teardown to ensure all in flight requests have completed. 84 // after teardown to ensure all in flight requests have completed.
70 class ReportHistogramTestHelper { 85 class ReportHistogramTestHelper {
71 public: 86 public:
72 // Sets the expected histogram value to be checked during teardown. 87 // Sets the expected histogram value to be checked during teardown.
73 void SetExpectedFailedReportCount(unsigned int num_expected_failed_report) { 88 void SetExpectedFailedReportCount(unsigned int num_expected_failed_report) {
74 num_expected_failed_report_ = num_expected_failed_report; 89 num_expected_failed_report_ = num_expected_failed_report;
75 } 90 }
76 91
77 void CheckHistogram() { 92 void CheckHistogram() {
78 if (num_expected_failed_report_ != 0) { 93 if (num_expected_failed_report_ != 0) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 // Adding a third report removes the oldest report (report1) from the list. 130 // Adding a third report removes the oldest report (report1) from the list.
116 list.Add(CertificateReportingService::Report( 131 list.Add(CertificateReportingService::Report(
117 3, base::Time::Now(), std::string("report3_zero_minutes_old"))); 132 3, base::Time::Now(), std::string("report3_zero_minutes_old")));
118 EXPECT_EQ(2u, list.items().size()); 133 EXPECT_EQ(2u, list.items().size());
119 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report); 134 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report);
120 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); 135 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report);
121 136
122 // Adding a report older than the oldest report in the list (report2) is 137 // Adding a report older than the oldest report in the list (report2) is
123 // a no-op. 138 // a no-op.
124 list.Add(CertificateReportingService::Report( 139 list.Add(CertificateReportingService::Report(
125 0, base::Time::Now() - base::TimeDelta::FromMinutes( 140 0, base::Time::Now() - base::TimeDelta::FromMinutes(10),
126 10) /* 5 minutes older than report2 */,
127 std::string("report0_ten_minutes_old"))); 141 std::string("report0_ten_minutes_old")));
128 EXPECT_EQ(2u, list.items().size()); 142 EXPECT_EQ(2u, list.items().size());
129 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report); 143 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report);
130 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); 144 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report);
131 } 145 }
132 146
133 class CertificateReportingServiceReporterOnIOThreadTest 147 class CertificateReportingServiceReporterOnIOThreadTest
134 : public ::testing::Test { 148 : public ::testing::Test {
135 public: 149 public:
136 void SetUp() override { 150 void SetUp() override {
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 class CertificateReportingServiceTest : public ::testing::Test { 332 class CertificateReportingServiceTest : public ::testing::Test {
319 public: 333 public:
320 CertificateReportingServiceTest() 334 CertificateReportingServiceTest()
321 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), 335 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD),
322 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( 336 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread(
323 content::BrowserThread::IO)) {} 337 content::BrowserThread::IO)) {}
324 338
325 ~CertificateReportingServiceTest() override {} 339 ~CertificateReportingServiceTest() override {}
326 340
327 void SetUp() override { 341 void SetUp() override {
342 service_observer_.Clear();
328 test_helper_.SetUpInterceptor(); 343 test_helper_.SetUpInterceptor();
329 WaitForIOThread(); 344 WaitForIOThread();
330 345
331 content::BrowserThread::PostTask( 346 content::BrowserThread::PostTask(
332 content::BrowserThread::IO, FROM_HERE, 347 content::BrowserThread::IO, FROM_HERE,
333 base::Bind( 348 base::Bind(
334 &CertificateReportingServiceTest::SetUpURLRequestContextOnIOThread, 349 &CertificateReportingServiceTest::SetUpURLRequestContextOnIOThread,
335 base::Unretained(this))); 350 base::Unretained(this)));
336 WaitForIOThread(); 351 WaitForIOThread();
337 352
338 safe_browsing::SafeBrowsingService::RegisterFactory(&sb_service_factory); 353 safe_browsing::SafeBrowsingService::RegisterFactory(&sb_service_factory);
339 sb_service_ = sb_service_factory.CreateSafeBrowsingService(); 354 sb_service_ = sb_service_factory.CreateSafeBrowsingService();
340 355
341 clock_.reset(new base::SimpleTestClock()); 356 clock_.reset(new base::SimpleTestClock());
342 service_.reset(new CertificateReportingService( 357 service_.reset(new CertificateReportingService(
343 sb_service_.get(), url_request_context_getter(), &profile_, 358 sb_service_.get(), url_request_context_getter(), &profile_,
344 test_helper_.server_public_key(), 359 test_helper_.server_public_key(),
345 test_helper_.server_public_key_version(), kMaxReportCountInQueue, 360 test_helper_.server_public_key_version(), kMaxReportCountInQueue,
346 base::TimeDelta::FromHours(24), clock_.get())); 361 base::TimeDelta::FromHours(24), clock_.get(),
347 // Wait for service reset. 362 base::Bind(&CertificateReportingServiceObserver::OnServiceReset,
348 WaitForIOThread(); 363 base::Unretained(&service_observer_))));
364 service_observer_.WaitForReset();
349 } 365 }
350 366
351 void TearDown() override { 367 void TearDown() override {
352 WaitForIOThread(); 368 WaitForIOThread();
353 EXPECT_TRUE(test_helper_.interceptor()->successful_reports().empty()); 369 test_helper()->ExpectNoRequests(service());
354 EXPECT_TRUE(test_helper_.interceptor()->failed_reports().empty());
355 EXPECT_TRUE(test_helper_.interceptor()->delayed_reports().empty());
356 EXPECT_EQ(0u, service()
357 ->GetReporterForTesting()
358 ->inflight_report_count_for_testing());
359 370
360 service_->Shutdown(); 371 service_->Shutdown();
361 WaitForIOThread(); 372 WaitForIOThread();
362 service_.reset(nullptr); 373 service_.reset(nullptr);
363 374
364 content::BrowserThread::PostTask( 375 content::BrowserThread::PostTask(
365 content::BrowserThread::IO, FROM_HERE, 376 content::BrowserThread::IO, FROM_HERE,
366 base::Bind(&CertificateReportingServiceTest::TearDownOnIOThread, 377 base::Bind(&CertificateReportingServiceTest::TearDownOnIOThread,
367 base::Unretained(this))); 378 base::Unretained(this)));
368 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, 379 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
369 base::Bind(&ClearURLHandlers)); 380 base::Bind(&ClearURLHandlers));
370 WaitForIOThread(); 381 WaitForIOThread();
371 382
372 histogram_test_helper_.CheckHistogram(); 383 histogram_test_helper_.CheckHistogram();
373 } 384 }
374 385
375 protected: 386 protected:
376 void WaitForRequestsDestroyed(const ReportExpectation& expectation) {
377 wait_helper_.Wait(expectation.num_reports());
378 EXPECT_EQ(expectation.successful_reports,
379 test_helper_.interceptor()->successful_reports());
380 EXPECT_EQ(expectation.failed_reports,
381 test_helper_.interceptor()->failed_reports());
382 EXPECT_EQ(expectation.delayed_reports,
383 test_helper_.interceptor()->delayed_reports());
384 test_helper_.interceptor()->ClearObservedReports();
385 }
386
387 net::URLRequestContextGetter* url_request_context_getter() { 387 net::URLRequestContextGetter* url_request_context_getter() {
388 return url_request_context_getter_.get(); 388 return url_request_context_getter_.get();
389 } 389 }
390 390
391 void WaitForIOThread() { 391 void WaitForIOThread() {
392 scoped_refptr<base::ThreadTestHelper> io_helper( 392 scoped_refptr<base::ThreadTestHelper> io_helper(
393 new base::ThreadTestHelper(io_task_runner_)); 393 new base::ThreadTestHelper(io_task_runner_));
394 ASSERT_TRUE(io_helper->Run()); 394 ASSERT_TRUE(io_helper->Run());
395 } 395 }
396 396
397 CertificateReportingService* service() { return service_.get(); } 397 CertificateReportingService* service() { return service_.get(); }
398 398
399 // Sets service enabled state and waits for a reset event. 399 // Sets service enabled state and waits for a reset event.
400 void SetServiceEnabledAndWait(bool enabled) { 400 void SetServiceEnabledAndWait(bool enabled) {
401 service_observer_.Clear();
401 service()->SetEnabled(enabled); 402 service()->SetEnabled(enabled);
402 WaitForIOThread(); 403 service_observer_.WaitForReset();
403 } 404 }
404 405
405 void AdvanceClock(base::TimeDelta delta) { 406 void AdvanceClock(base::TimeDelta delta) {
406 content::BrowserThread::PostTask( 407 content::BrowserThread::PostTask(
407 content::BrowserThread::IO, FROM_HERE, 408 content::BrowserThread::IO, FROM_HERE,
408 base::Bind(&base::SimpleTestClock::Advance, 409 base::Bind(&base::SimpleTestClock::Advance,
409 base::Unretained(clock_.get()), delta)); 410 base::Unretained(clock_.get()), delta));
410 } 411 }
411 412
412 void SetNow(base::Time now) { 413 void SetNow(base::Time now) {
413 content::BrowserThread::PostTask( 414 content::BrowserThread::PostTask(
414 content::BrowserThread::IO, FROM_HERE, 415 content::BrowserThread::IO, FROM_HERE,
415 base::Bind(&base::SimpleTestClock::SetNow, 416 base::Bind(&base::SimpleTestClock::SetNow,
416 base::Unretained(clock_.get()), now)); 417 base::Unretained(clock_.get()), now));
417 } 418 }
418 419
419 void SetExpectedFailedReportCountOnTearDown(unsigned int count) { 420 void SetExpectedFailedReportCountOnTearDown(unsigned int count) {
420 histogram_test_helper_.SetExpectedFailedReportCount(count); 421 histogram_test_helper_.SetExpectedFailedReportCount(count);
421 } 422 }
422 423
423 CertificateReportingServiceTestHelper* test_helper() { return &test_helper_; } 424 CertificateReportingServiceTestHelper* test_helper() { return &test_helper_; }
424 425
425 private: 426 private:
426 void SetUpURLRequestContextOnIOThread() { 427 void SetUpURLRequestContextOnIOThread() {
427 network_delegate_.reset(new TestNetworkDelegate(
428 base::Bind(&CertificateReportingServiceTest::OnURLRequestDestroyed,
429 base::Unretained(this))));
430
431 std::unique_ptr<net::TestURLRequestContext> url_request_context( 428 std::unique_ptr<net::TestURLRequestContext> url_request_context(
432 new net::TestURLRequestContext(true)); 429 new net::TestURLRequestContext(false));
433 url_request_context->set_network_delegate(network_delegate_.get());
434 url_request_context->Init();
435 url_request_context_getter_ = new net::TestURLRequestContextGetter( 430 url_request_context_getter_ = new net::TestURLRequestContextGetter(
436 io_task_runner_, std::move(url_request_context)); 431 io_task_runner_, std::move(url_request_context));
437 } 432 }
438 433
439 void TearDownOnIOThread() { 434 void TearDownOnIOThread() {
440 url_request_context_getter_ = nullptr; 435 url_request_context_getter_ = nullptr;
441 network_delegate_.reset(nullptr);
442 }
443
444 void OnURLRequestDestroyed() {
445 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
446 wait_helper_.OnEvent();
447 } 436 }
448 437
449 // Must be initialized before url_request_context_getter_ 438 // Must be initialized before url_request_context_getter_
450 content::TestBrowserThreadBundle thread_bundle_; 439 content::TestBrowserThreadBundle thread_bundle_;
451 440
452 std::unique_ptr<TestNetworkDelegate> network_delegate_;
453 certificate_reporting_test_utils::ReportWaitHelper wait_helper_;
454
455 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 441 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
456 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 442 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
457 443
458 std::unique_ptr<CertificateReportingService> service_; 444 std::unique_ptr<CertificateReportingService> service_;
459 std::unique_ptr<base::SimpleTestClock> clock_; 445 std::unique_ptr<base::SimpleTestClock> clock_;
460 446
461 scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_; 447 scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_;
462 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; 448 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory;
463 TestingProfile profile_; 449 TestingProfile profile_;
464 450
465 CertificateReportingServiceTestHelper test_helper_; 451 CertificateReportingServiceTestHelper test_helper_;
466 ReportHistogramTestHelper histogram_test_helper_; 452 ReportHistogramTestHelper histogram_test_helper_;
453 CertificateReportingServiceObserver service_observer_;
467 }; 454 };
468 455
469 TEST_F(CertificateReportingServiceTest, Send) { 456 TEST_F(CertificateReportingServiceTest, Send) {
470 SetExpectedFailedReportCountOnTearDown(4); 457 SetExpectedFailedReportCountOnTearDown(4);
471 458
472 // Let all reports fail. 459 // Let all reports fail.
473 test_helper()->SetFailureMode( 460 test_helper()->SetFailureMode(
474 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); 461 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
475 462
476 // Send two reports. Both should fail and get queued. 463 // Send two reports. Both should fail and get queued.
477 service()->Send("report0"); 464 service()->Send(MakeReport("report0"));
478 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); 465 service()->Send(MakeReport("report1"));
466 test_helper()->WaitForRequestsDestroyed(
467 ReportExpectation::Failed({"report0", "report1"}));
479 468
480 service()->Send("report1"); 469 // Send pending reports. Previously queued reports should be queued again.
481 WaitForRequestsDestroyed(ReportExpectation::Failed({"report1"}));
482
483 // Send pending reports. Previously queued reports should be observed. They
484 // will also be queued again.
485 service()->SendPending(); 470 service()->SendPending();
486 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0", "report1"})); 471 test_helper()->WaitForRequestsDestroyed(
472 ReportExpectation::Failed({"report0", "report1"}));
487 473
488 // Let all reports succeed. 474 // Let all reports succeed.
489 test_helper()->SetFailureMode(certificate_reporting_test_utils:: 475 test_helper()->SetFailureMode(certificate_reporting_test_utils::
490 ReportSendingResult::REPORTS_SUCCESSFUL); 476 ReportSendingResult::REPORTS_SUCCESSFUL);
491 477
492 // Send a third report. This should not be queued. 478 // Send a third report. This should not be queued.
493 service()->Send("report2"); 479 service()->Send(MakeReport("report2"));
494 WaitForRequestsDestroyed(ReportExpectation::Successful({"report2"})); 480 test_helper()->WaitForRequestsDestroyed(
481 ReportExpectation::Successful({"report2"}));
495 482
496 // Send pending reports. Previously failed and queued two reports should be 483 // Send pending reports. Previously failed and queued two reports should be
497 // observed. 484 // observed.
498 service()->SendPending(); 485 service()->SendPending();
499 WaitForRequestsDestroyed( 486 test_helper()->WaitForRequestsDestroyed(
500 ReportExpectation::Successful({"report0", "report1"})); 487 ReportExpectation::Successful({"report0", "report1"}));
501 } 488 }
502 489
503 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) { 490 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) {
504 SetExpectedFailedReportCountOnTearDown(0); 491 SetExpectedFailedReportCountOnTearDown(0);
505 492
506 // Let all reports succeed. 493 // Let all reports succeed.
507 test_helper()->SetFailureMode(certificate_reporting_test_utils:: 494 test_helper()->SetFailureMode(certificate_reporting_test_utils::
508 ReportSendingResult::REPORTS_SUCCESSFUL); 495 ReportSendingResult::REPORTS_SUCCESSFUL);
509 496
510 // Disable the service. 497 // Disable the service.
511 SetServiceEnabledAndWait(false); 498 SetServiceEnabledAndWait(false);
512 499
513 // Send a report. Report attempt should be cancelled and no sent reports 500 // Send a report. Report attempt should be cancelled and no sent reports
514 // should be observed. 501 // should be observed.
515 service()->Send("report0"); 502 service()->Send(MakeReport("report0"));
516 503
517 // Enable the service and send a report again. It should be sent successfully. 504 // Enable the service and send a report again. It should be sent successfully.
518 SetServiceEnabledAndWait(true); 505 SetServiceEnabledAndWait(true);
519 506
520 service()->Send("report1"); 507 service()->Send(MakeReport("report1"));
521 WaitForRequestsDestroyed(ReportExpectation::Successful({"report1"})); 508 test_helper()->WaitForRequestsDestroyed(
509 ReportExpectation::Successful({"report1"}));
522 } 510 }
523 511
524 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) { 512 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) {
525 SetExpectedFailedReportCountOnTearDown(1); 513 SetExpectedFailedReportCountOnTearDown(1);
526 514
527 // Let all reports fail. 515 // Let all reports fail.
528 test_helper()->SetFailureMode( 516 test_helper()->SetFailureMode(
529 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); 517 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
530 518
531 service()->Send("report0"); 519 service()->Send(MakeReport("report0"));
532 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); 520 test_helper()->WaitForRequestsDestroyed(
521 ReportExpectation::Failed({"report0"}));
533 522
534 // Disable the service. 523 // Disable the service.
535 SetServiceEnabledAndWait(false); 524 SetServiceEnabledAndWait(false);
536 525
537 // Sending has no effect while disabled. 526 // Sending has no effect while disabled.
538 service()->SendPending(); 527 service()->SendPending();
539 528
540 // Re-enable the service and send pending reports. Pending reports should have 529 // Re-enable the service and send pending reports. Pending reports should have
541 // been cleared when the service was disabled, so no report should be seen. 530 // been cleared when the service was disabled, so no report should be seen.
542 SetServiceEnabledAndWait(true); 531 SetServiceEnabledAndWait(true);
543 532
544 // Sending with empty queue has no effect. 533 // Sending with empty queue has no effect.
545 service()->SendPending(); 534 service()->SendPending();
546 } 535 }
547 536
548 TEST_F(CertificateReportingServiceTest, DontSendOldReports) { 537 TEST_F(CertificateReportingServiceTest, DontSendOldReports) {
549 SetExpectedFailedReportCountOnTearDown(5); 538 SetExpectedFailedReportCountOnTearDown(5);
550 539
551 SetNow(base::Time::Now()); 540 SetNow(base::Time::Now());
552 // Let all reports fail. 541 // Let all reports fail.
553 test_helper()->SetFailureMode( 542 test_helper()->SetFailureMode(
554 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); 543 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
555 544
556 // Send a report. 545 // Send a report, then advance the clock and send another report.
557 service()->Send("report0"); 546 service()->Send(MakeReport("report0"));
558 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"}));
559
560 // Advance the clock a bit and trigger another report.
561 AdvanceClock(base::TimeDelta::FromHours(5)); 547 AdvanceClock(base::TimeDelta::FromHours(5));
562 548 service()->Send(MakeReport("report1"));
563 service()->Send("report1"); 549 test_helper()->WaitForRequestsDestroyed(
564 WaitForRequestsDestroyed(ReportExpectation::Failed({"report1"})); 550 ReportExpectation::Failed({"report0", "report1"}));
565 551
566 // Advance the clock to 20 hours, putting it 25 hours ahead of the reference 552 // Advance the clock to 20 hours, putting it 25 hours ahead of the reference
567 // time. This makes the report0 older than max age (24 hours). The report1 is 553 // time. This makes the report0 older than max age (24 hours). The report1 is
568 // now 20 hours old. 554 // now 20 hours old.
569 AdvanceClock(base::TimeDelta::FromHours(20)); 555 AdvanceClock(base::TimeDelta::FromHours(20));
570 // Send pending reports. report0 should be discarded since it's too old. 556 // Send pending reports. report0 should be discarded since it's too old.
571 // report1 should be queued again. 557 // report1 should be queued again.
572 service()->SendPending(); 558 service()->SendPending();
573 WaitForRequestsDestroyed(ReportExpectation::Failed({"report1"})); 559 test_helper()->WaitForRequestsDestroyed(
560 ReportExpectation::Failed({"report1"}));
574 561
575 // Send a third report. 562 // Send a third report.
576 service()->Send("report2"); 563 service()->Send(MakeReport("report2"));
577 WaitForRequestsDestroyed(ReportExpectation::Failed({"report2"})); 564 test_helper()->WaitForRequestsDestroyed(
565 ReportExpectation::Failed({"report2"}));
578 566
579 // Advance the clock 5 hours. The report1 will now be 25 hours old. 567 // Advance the clock 5 hours. The report1 will now be 25 hours old.
580 AdvanceClock(base::TimeDelta::FromHours(5)); 568 AdvanceClock(base::TimeDelta::FromHours(5));
581 // Send pending reports. report1 should be discarded since it's too old. 569 // Send pending reports. report1 should be discarded since it's too old.
582 // report2 should be queued again. 570 // report2 should be queued again.
583 service()->SendPending(); 571 service()->SendPending();
584 WaitForRequestsDestroyed(ReportExpectation::Failed({"report2"})); 572 test_helper()->WaitForRequestsDestroyed(
573 ReportExpectation::Failed({"report2"}));
585 574
586 // Advance the clock 20 hours again so that report2 is 25 hours old and is 575 // Advance the clock 20 hours again so that report2 is 25 hours old and is
587 // older than max age (24 hours) 576 // older than max age (24 hours)
588 AdvanceClock(base::TimeDelta::FromHours(20)); 577 AdvanceClock(base::TimeDelta::FromHours(20));
589 // Send pending reports. report2 should be discarded since it's too old. No 578 // Send pending reports. report2 should be discarded since it's too old. No
590 // other reports remain. 579 // other reports remain.
591 service()->SendPending(); 580 service()->SendPending();
592 } 581 }
593 582
594 TEST_F(CertificateReportingServiceTest, DiscardOldReports) { 583 TEST_F(CertificateReportingServiceTest, DiscardOldReports) {
595 SetExpectedFailedReportCountOnTearDown(7); 584 SetExpectedFailedReportCountOnTearDown(7);
596 585
597 SetNow(base::Time::Now()); 586 SetNow(base::Time::Now());
598 // Let all reports fail. 587 // Let all reports fail.
599 test_helper()->SetFailureMode( 588 test_helper()->SetFailureMode(
600 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); 589 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
601 590
602 // Send a failed report.
603 service()->Send("report0");
604 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"}));
605
606 // Send three more reports within five hours of each other. After this: 591 // Send three more reports within five hours of each other. After this:
607 // report0 is 0 hours after reference time (15 hours old). 592 // report0 is 0 hours after reference time (15 hours old).
608 // report1 is 5 hours after reference time (10 hours old). 593 // report1 is 5 hours after reference time (10 hours old).
609 // report2 is 10 hours after reference time (5 hours old). 594 // report2 is 10 hours after reference time (5 hours old).
610 // report3 is 15 hours after reference time (0 hours old). 595 // report3 is 15 hours after reference time (0 hours old).
611 AdvanceClock(base::TimeDelta::FromHours(5)); 596 service()->Send(MakeReport("report0"));
612 service()->Send("report1");
613 597
614 AdvanceClock(base::TimeDelta::FromHours(5)); 598 AdvanceClock(base::TimeDelta::FromHours(5));
615 service()->Send("report2"); 599 service()->Send(MakeReport("report1"));
616 600
617 AdvanceClock(base::TimeDelta::FromHours(5)); 601 AdvanceClock(base::TimeDelta::FromHours(5));
618 service()->Send("report3"); 602 service()->Send(MakeReport("report2"));
619 WaitForRequestsDestroyed( 603
620 ReportExpectation::Failed({"report1", "report2", "report3"})); 604 AdvanceClock(base::TimeDelta::FromHours(5));
605 service()->Send(MakeReport("report3"));
606 test_helper()->WaitForRequestsDestroyed(
607 ReportExpectation::Failed({"report0", "report1", "report2", "report3"}));
621 608
622 // Send pending reports. Four reports were generated above, but the service 609 // Send pending reports. Four reports were generated above, but the service
623 // only queues three reports, so the very first one should be dropped since 610 // only queues three reports, so the very first one should be dropped since
624 // it's the oldest. 611 // it's the oldest.
625 service()->SendPending(); 612 service()->SendPending();
626 WaitForRequestsDestroyed( 613 test_helper()->WaitForRequestsDestroyed(
627 ReportExpectation::Failed({"report1", "report2", "report3"})); 614 ReportExpectation::Failed({"report1", "report2", "report3"}));
628 615
629 // Let all reports succeed. 616 // Let all reports succeed.
630 test_helper()->SetFailureMode(certificate_reporting_test_utils:: 617 test_helper()->SetFailureMode(certificate_reporting_test_utils::
631 ReportSendingResult::REPORTS_SUCCESSFUL); 618 ReportSendingResult::REPORTS_SUCCESSFUL);
632 619
633 // Advance the clock by 15 hours. Current time is now 30 hours after reference 620 // Advance the clock by 15 hours. Current time is now 30 hours after reference
634 // time. The ages of reports are now as follows: 621 // time. The ages of reports are now as follows:
635 // report1 is 25 hours old. 622 // report1 is 25 hours old.
636 // report2 is 20 hours old. 623 // report2 is 20 hours old.
637 // report3 is 15 hours old. 624 // report3 is 15 hours old.
638 AdvanceClock(base::TimeDelta::FromHours(15)); 625 AdvanceClock(base::TimeDelta::FromHours(15));
639 // Send pending reports. Only report2 and report3 should be sent, report1 626 // Send pending reports. Only report2 and report3 should be sent, report1
640 // should be ignored because it's too old. 627 // should be ignored because it's too old.
641 service()->SendPending(); 628 service()->SendPending();
642 WaitForRequestsDestroyed( 629 test_helper()->WaitForRequestsDestroyed(
643 ReportExpectation::Successful({"report2", "report3"})); 630 ReportExpectation::Successful({"report2", "report3"}));
644 631
645 // Do a final send. No reports should be sent. 632 // Do a final send. No reports should be sent.
646 service()->SendPending(); 633 service()->SendPending();
647 } 634 }
648 635
649 // A delayed report should successfully upload when it's resumed. 636 // A delayed report should successfully upload when it's resumed.
650 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) { 637 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) {
651 SetExpectedFailedReportCountOnTearDown(0); 638 SetExpectedFailedReportCountOnTearDown(0);
652 639
653 // Let reports hang. 640 // Let reports hang.
654 test_helper()->SetFailureMode( 641 test_helper()->SetFailureMode(
655 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); 642 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY);
656 // Send a report. The report upload hangs, so no error or success callbacks 643 // Send a report. The report upload hangs, so no error or success callbacks
657 // should be called. 644 // should be called.
658 service()->Send("report0"); 645 service()->Send(MakeReport("report0"));
659 646
660 // Resume the report upload and run the callbacks. The report should be 647 // Resume the report upload and run the callbacks. The report should be
661 // successfully sent. 648 // successfully sent.
662 test_helper()->ResumeDelayedRequest(base::Bind(&base::DoNothing)); 649 test_helper()->ResumeDelayedRequest();
663 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0"})); 650 test_helper()->WaitForRequestsDestroyed(
651 ReportExpectation::Delayed({"report0"}));
664 } 652 }
665 653
666 // Delayed reports should cleaned when the service is reset. 654 // Delayed reports should cleaned when the service is reset.
667 TEST_F(CertificateReportingServiceTest, Delayed_Reset) { 655 TEST_F(CertificateReportingServiceTest, Delayed_Reset) {
668 SetExpectedFailedReportCountOnTearDown(0); 656 SetExpectedFailedReportCountOnTearDown(0);
669 657
670 // Let reports hang. 658 // Let reports hang.
671 test_helper()->SetFailureMode( 659 test_helper()->SetFailureMode(
672 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); 660 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY);
673 // Send a report. The report is triggered but hangs, so no error or success 661 // Send a report. The report is triggered but hangs, so no error or success
674 // callbacks should be called. 662 // callbacks should be called.
675 service()->Send("report0"); 663 service()->Send(MakeReport("report0"));
676 664
677 // Disable the service. This should reset the reporting service and 665 // Disable the service. This should reset the reporting service and
678 // clear all pending reports. 666 // clear all pending reports.
679 SetServiceEnabledAndWait(false); 667 SetServiceEnabledAndWait(false);
680 668
681 // Resume delayed report. No report should be observed since the service 669 // Resume delayed report. No report should be observed since the service
682 // should have reset and all pending reports should be cleared. If any report 670 // should have reset and all pending reports should be cleared. If any report
683 // is observed, the next WaitForRequestsDestroyed() will fail. 671 // is observed, the next test_helper()->WaitForRequestsDestroyed() will fail.
684 test_helper()->ResumeDelayedRequest(base::Bind(&base::DoNothing)); 672 test_helper()->ResumeDelayedRequest();
685 673
686 // Enable the service. 674 // Enable the service.
687 SetServiceEnabledAndWait(true); 675 SetServiceEnabledAndWait(true);
688 676
689 // Send a report. The report is triggered but hangs, so no error or success 677 // Send a report. The report is triggered but hangs, so no error or success
690 // callbacks should be called. The report id is again 0 since the pending 678 // callbacks should be called. The report id is again 0 since the pending
691 // report queue has been cleared above. 679 // report queue has been cleared above.
692 service()->Send("report1"); 680 service()->Send(MakeReport("report1"));
693 681
694 // Resume delayed report. Two reports are successfully sent. 682 // Resume delayed report. Two reports are successfully sent.
695 test_helper()->ResumeDelayedRequest(base::Bind(&base::DoNothing)); 683 test_helper()->ResumeDelayedRequest();
696 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0", "report1"})); 684 test_helper()->WaitForRequestsDestroyed(
685 ReportExpectation::Delayed({"report0", "report1"}));
697 } 686 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698