Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" | 5 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/test/histogram_tester.h" | |
| 13 #include "base/test/simple_test_clock.h" | 14 #include "base/test/simple_test_clock.h" |
| 14 #include "base/test/thread_test_helper.h" | 15 #include "base/test/thread_test_helper.h" |
| 15 #include "base/time/clock.h" | 16 #include "base/time/clock.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "chrome/browser/safe_browsing/certificate_reporting_service_test_utils. h" | 18 #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/test_safe_browsing_service.h" | |
| 21 #include "chrome/test/base/testing_profile.h" | |
| 18 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "net/base/network_delegate_impl.h" | 25 #include "net/base/network_delegate_impl.h" |
| 22 #include "net/test/url_request/url_request_failed_job.h" | 26 #include "net/test/url_request/url_request_failed_job.h" |
| 23 #include "net/test/url_request/url_request_mock_data_job.h" | 27 #include "net/test/url_request/url_request_mock_data_job.h" |
| 24 #include "net/url_request/url_request_filter.h" | 28 #include "net/url_request/url_request_filter.h" |
| 25 #include "net/url_request/url_request_test_util.h" | 29 #include "net/url_request/url_request_test_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 31 |
| 32 using certificate_reporting_test_utils::ReportExpectation; | |
| 33 | |
| 28 namespace { | 34 namespace { |
| 29 | 35 |
| 30 // Maximum number of reports kept in the certificate reporting service's retry | 36 // Maximum number of reports kept in the certificate reporting service's retry |
| 31 // queue. | 37 // queue. |
| 32 const size_t kMaxReportCountInQueue = 3; | 38 const size_t kMaxReportCountInQueue = 3; |
| 33 | 39 |
| 40 const char* kFailedReportHistogram = "SSL.CertificateErrorReportFailure"; | |
| 41 | |
| 34 void ClearURLHandlers() { | 42 void ClearURLHandlers() { |
| 35 net::URLRequestFilter::GetInstance()->ClearHandlers(); | 43 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 36 } | 44 } |
| 37 | 45 |
| 46 // A network delegate used to observe URL request destructions. The tests check | |
| 47 // that no outstanding URL request is present during tear down. | |
| 48 class TestNetworkDelegate : public net::NetworkDelegateImpl { | |
| 49 public: | |
| 50 TestNetworkDelegate( | |
| 51 const base::Callback<void()>& url_request_destroyed_callback) | |
| 52 : url_request_destroyed_callback_(url_request_destroyed_callback) {} | |
| 53 | |
| 54 ~TestNetworkDelegate() override {} | |
| 55 | |
| 56 // net::NetworkDelegate method: | |
| 57 void OnURLRequestDestroyed(net::URLRequest* request) override { | |
| 58 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 59 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 60 url_request_destroyed_callback_); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 base::Callback<void()> url_request_destroyed_callback_; | |
| 65 }; | |
| 66 | |
| 67 // Base class for histogram testing. The failed report histogram is checked once | |
| 68 // after teardown to ensure all in flight requests have completed. | |
| 69 class HistogramTestBase { | |
| 70 protected: | |
| 71 virtual ~HistogramTestBase() {} | |
| 72 | |
| 73 // Sets the expected histogram value to be checked during teardown. | |
|
estark
2016/12/16 01:57:59
nit: maybe make this SetExpectedHistogramFailedCou
meacer
2016/12/16 20:26:36
Changed it to SetExpectedFailedReportCount, added
| |
| 74 void SetExpectedHistogramCountOnTeardown(int num_expected_failed_report) { | |
| 75 num_expected_failed_report_ = num_expected_failed_report; | |
| 76 } | |
| 77 | |
| 78 void CheckHistogram() { | |
| 79 if (num_expected_failed_report_ != 0) { | |
| 80 histogram_tester_.ExpectUniqueSample(kFailedReportHistogram, | |
| 81 -net::ERR_SSL_PROTOCOL_ERROR, | |
| 82 num_expected_failed_report_); | |
| 83 } else { | |
| 84 histogram_tester_.ExpectTotalCount(kFailedReportHistogram, 0); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 int num_expected_failed_report_ = 0; | |
|
estark
2016/12/16 01:57:59
unsigned int
meacer
2016/12/16 20:26:36
Done.
| |
| 90 base::HistogramTester histogram_tester_; | |
| 91 }; | |
| 92 | |
| 38 } // namespace | 93 } // namespace |
| 39 | 94 |
| 40 TEST(CertificateReportingServiceReportListTest, BoundedReportList) { | 95 TEST(CertificateReportingServiceReportListTest, BoundedReportList) { |
| 41 // Create a report list with maximum of 2 items. | 96 // Create a report list with maximum of 2 items. |
| 42 CertificateReportingService::BoundedReportList list(2 /* max_size */); | 97 CertificateReportingService::BoundedReportList list(2 /* max_size */); |
| 43 EXPECT_EQ(0u, list.items().size()); | 98 EXPECT_EQ(0u, list.items().size()); |
| 44 | 99 |
| 45 // Add a ten minute old report. | 100 // Add a ten minute old report. |
| 46 list.Add(CertificateReportingService::Report( | 101 list.Add(CertificateReportingService::Report( |
| 47 1, base::Time::Now() - base::TimeDelta::FromMinutes(10), | 102 1, base::Time::Now() - base::TimeDelta::FromMinutes(10), |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 70 list.Add(CertificateReportingService::Report( | 125 list.Add(CertificateReportingService::Report( |
| 71 0, base::Time::Now() - base::TimeDelta::FromMinutes( | 126 0, base::Time::Now() - base::TimeDelta::FromMinutes( |
| 72 10) /* 5 minutes older than report2 */, | 127 10) /* 5 minutes older than report2 */, |
| 73 std::string("report0_ten_minutes_old"))); | 128 std::string("report0_ten_minutes_old"))); |
| 74 EXPECT_EQ(2u, list.items().size()); | 129 EXPECT_EQ(2u, list.items().size()); |
| 75 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report); | 130 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report); |
| 76 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); | 131 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); |
| 77 } | 132 } |
| 78 | 133 |
| 79 class CertificateReportingServiceReporterOnIOThreadTest | 134 class CertificateReportingServiceReporterOnIOThreadTest |
| 80 : public ::testing::Test { | 135 : public ::testing::Test, |
| 136 public HistogramTestBase { | |
|
estark
2016/12/16 01:57:59
Instead of the multiple inheritance, could the tes
meacer
2016/12/16 20:26:36
Done.
| |
| 81 public: | 137 public: |
| 82 void SetUp() override { | 138 void SetUp() override { |
| 83 message_loop_.reset(new base::MessageLoopForIO()); | 139 message_loop_.reset(new base::MessageLoopForIO()); |
| 84 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, | 140 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, |
| 85 message_loop_.get())); | 141 message_loop_.get())); |
| 86 url_request_context_getter_ = | 142 url_request_context_getter_ = |
| 87 new net::TestURLRequestContextGetter(message_loop_->task_runner()); | 143 new net::TestURLRequestContextGetter(message_loop_->task_runner()); |
| 88 net::URLRequestFailedJob::AddUrlHandler(); | 144 net::URLRequestFailedJob::AddUrlHandler(); |
| 89 net::URLRequestMockDataJob::AddUrlHandler(); | 145 net::URLRequestMockDataJob::AddUrlHandler(); |
| 90 } | 146 } |
| 91 | 147 |
| 92 void TearDown() override { ClearURLHandlers(); } | 148 void TearDown() override { |
| 149 ClearURLHandlers(); | |
| 150 // Check the histogram as the last thing. This makes sure no in-flight | |
| 151 // report is missed. | |
| 152 CheckHistogram(); | |
| 153 } | |
| 93 | 154 |
| 94 protected: | 155 protected: |
| 95 net::URLRequestContextGetter* url_request_context_getter() { | 156 net::URLRequestContextGetter* url_request_context_getter() { |
| 96 return url_request_context_getter_.get(); | 157 return url_request_context_getter_.get(); |
| 97 } | 158 } |
| 98 | 159 |
| 99 private: | 160 private: |
| 100 std::unique_ptr<base::MessageLoopForIO> message_loop_; | 161 std::unique_ptr<base::MessageLoopForIO> message_loop_; |
| 101 std::unique_ptr<content::TestBrowserThread> io_thread_; | 162 std::unique_ptr<content::TestBrowserThread> io_thread_; |
| 102 | 163 |
| 103 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 164 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 104 }; | 165 }; |
| 105 | 166 |
| 106 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, | 167 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, |
| 107 Reporter_RetriesEnabled) { | 168 Reporter_RetriesEnabled) { |
| 169 SetExpectedHistogramCountOnTeardown(6); | |
| 170 | |
| 108 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); | 171 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
| 109 base::Time reference_time = base::Time::Now(); | 172 base::Time reference_time = base::Time::Now(); |
| 110 clock->SetNow(reference_time); | 173 clock->SetNow(reference_time); |
| 111 | 174 |
| 112 const GURL kFailureURL = | 175 const GURL kFailureURL = |
| 113 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); | 176 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); |
| 114 certificate_reporting::ErrorReporter* certificate_error_reporter = | 177 certificate_reporting::ErrorReporter* certificate_error_reporter = |
| 115 new certificate_reporting::ErrorReporter( | 178 new certificate_reporting::ErrorReporter( |
| 116 url_request_context_getter()->GetURLRequestContext(), kFailureURL, | 179 url_request_context_getter()->GetURLRequestContext(), kFailureURL, |
| 117 net::ReportSender::DO_NOT_SEND_COOKIES); | 180 net::ReportSender::DO_NOT_SEND_COOKIES); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 clock->Advance(base::TimeDelta::FromSeconds(1)); | 258 clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 196 reporter.SendPending(); | 259 reporter.SendPending(); |
| 197 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); | 260 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); |
| 198 base::RunLoop().RunUntilIdle(); | 261 base::RunLoop().RunUntilIdle(); |
| 199 EXPECT_EQ(0u, list->items().size()); | 262 EXPECT_EQ(0u, list->items().size()); |
| 200 } | 263 } |
| 201 | 264 |
| 202 // Same as above, but retries are disabled. | 265 // Same as above, but retries are disabled. |
| 203 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, | 266 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, |
| 204 Reporter_RetriesDisabled) { | 267 Reporter_RetriesDisabled) { |
| 268 SetExpectedHistogramCountOnTeardown(2); | |
| 269 | |
| 205 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); | 270 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
| 206 base::Time reference_time = base::Time::Now(); | 271 base::Time reference_time = base::Time::Now(); |
| 207 clock->SetNow(reference_time); | 272 clock->SetNow(reference_time); |
| 208 | 273 |
| 209 const GURL kFailureURL = | 274 const GURL kFailureURL = |
| 210 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); | 275 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); |
| 211 certificate_reporting::ErrorReporter* certificate_error_reporter = | 276 certificate_reporting::ErrorReporter* certificate_error_reporter = |
| 212 new certificate_reporting::ErrorReporter( | 277 new certificate_reporting::ErrorReporter( |
| 213 url_request_context_getter()->GetURLRequestContext(), kFailureURL, | 278 url_request_context_getter()->GetURLRequestContext(), kFailureURL, |
| 214 net::ReportSender::DO_NOT_SEND_COOKIES); | 279 net::ReportSender::DO_NOT_SEND_COOKIES); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 243 // Send pending reports. Nothing should be sent. | 308 // Send pending reports. Nothing should be sent. |
| 244 clock->Advance(base::TimeDelta::FromSeconds(10)); | 309 clock->Advance(base::TimeDelta::FromSeconds(10)); |
| 245 reporter.SendPending(); | 310 reporter.SendPending(); |
| 246 base::RunLoop().RunUntilIdle(); | 311 base::RunLoop().RunUntilIdle(); |
| 247 ASSERT_EQ(0u, list->items().size()); | 312 ASSERT_EQ(0u, list->items().size()); |
| 248 } | 313 } |
| 249 | 314 |
| 250 class CertificateReportingServiceTest | 315 class CertificateReportingServiceTest |
| 251 : public ::testing::Test, | 316 : public ::testing::Test, |
| 252 public certificate_reporting_test_utils:: | 317 public certificate_reporting_test_utils:: |
| 253 CertificateReportingServiceTestBase { | 318 CertificateReportingServiceTestBase, |
| 319 public HistogramTestBase { | |
|
estark
2016/12/16 01:57:59
same here
meacer
2016/12/16 20:26:36
Done.
| |
| 254 public: | 320 public: |
| 255 CertificateReportingServiceTest() | 321 CertificateReportingServiceTest() |
| 256 : CertificateReportingServiceTestBase(), | 322 : CertificateReportingServiceTestBase(), |
| 257 thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), | 323 thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), |
| 258 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( | 324 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( |
| 259 content::BrowserThread::IO)) {} | 325 content::BrowserThread::IO)) {} |
| 260 | 326 |
| 261 ~CertificateReportingServiceTest() override {} | 327 ~CertificateReportingServiceTest() override {} |
| 262 | 328 |
| 263 void SetUp() override { | 329 void SetUp() override { |
| 264 SetUpInterceptor(); | 330 SetUpInterceptor(); |
| 265 WaitForIOThread(); | 331 WaitForIOThread(); |
| 266 | 332 |
| 267 content::BrowserThread::PostTask( | 333 content::BrowserThread::PostTask( |
| 268 content::BrowserThread::IO, FROM_HERE, | 334 content::BrowserThread::IO, FROM_HERE, |
| 269 base::Bind( | 335 base::Bind( |
| 270 &CertificateReportingServiceTest::SetUpURLRequestContextOnIOThread, | 336 &CertificateReportingServiceTest::SetUpURLRequestContextOnIOThread, |
| 271 base::Unretained(this))); | 337 base::Unretained(this))); |
| 272 WaitForIOThread(); | 338 WaitForIOThread(); |
| 273 | 339 |
| 274 clock_ = new base::SimpleTestClock(); | 340 safe_browsing::SafeBrowsingService::RegisterFactory(&sb_service_factory); |
| 341 sb_service_ = sb_service_factory.CreateSafeBrowsingService(); | |
| 342 | |
| 343 clock_.reset(new base::SimpleTestClock()); | |
| 275 service_.reset(new CertificateReportingService( | 344 service_.reset(new CertificateReportingService( |
| 276 url_request_context_getter(), server_public_key(), | 345 sb_service_.get(), url_request_context_getter(), &profile_, |
| 277 server_public_key_version(), kMaxReportCountInQueue, | 346 server_public_key(), server_public_key_version(), |
| 278 base::TimeDelta::FromHours(24), std::unique_ptr<base::Clock>(clock_))); | 347 kMaxReportCountInQueue, base::TimeDelta::FromHours(24), clock_.get())); |
| 279 // Wait for service reset. | 348 // Wait for service reset. |
| 280 WaitForIOThread(); | 349 WaitForIOThread(); |
| 281 } | 350 } |
| 282 | 351 |
| 283 void SetUpURLRequestContextOnIOThread() { | |
| 284 std::unique_ptr<net::TestURLRequestContext> url_request_context( | |
| 285 new net::TestURLRequestContext(true)); | |
| 286 url_request_context->set_network_delegate(network_delegate()); | |
| 287 url_request_context->Init(); | |
| 288 url_request_context_getter_ = new net::TestURLRequestContextGetter( | |
| 289 io_task_runner_, std::move(url_request_context)); | |
| 290 } | |
| 291 | |
| 292 void TearDown() override { | 352 void TearDown() override { |
| 293 WaitForIOThread(); | 353 WaitForIOThread(); |
| 294 EXPECT_TRUE(interceptor()->successful_reports().empty()); | 354 EXPECT_TRUE(interceptor()->successful_reports().empty()); |
| 295 EXPECT_TRUE(interceptor()->failed_reports().empty()); | 355 EXPECT_TRUE(interceptor()->failed_reports().empty()); |
| 296 EXPECT_TRUE(interceptor()->delayed_reports().empty()); | 356 EXPECT_TRUE(interceptor()->delayed_reports().empty()); |
| 297 EXPECT_EQ(0u, service() | 357 EXPECT_EQ(0u, service() |
| 298 ->GetReporterForTesting() | 358 ->GetReporterForTesting() |
| 299 ->inflight_report_count_for_testing()); | 359 ->inflight_report_count_for_testing()); |
| 300 | 360 |
| 301 service_->Shutdown(); | 361 service_->Shutdown(); |
| 302 WaitForIOThread(); | 362 WaitForIOThread(); |
| 303 | 363 |
| 304 service_.reset(nullptr); | 364 service_.reset(nullptr); |
| 365 | |
| 366 content::BrowserThread::PostTask( | |
| 367 content::BrowserThread::IO, FROM_HERE, | |
| 368 base::Bind(&CertificateReportingServiceTest::TearDownOnIOThread, | |
| 369 base::Unretained(this))); | |
|
estark
2016/12/16 01:57:59
This looks a little sketchy to me. I'd think we ne
meacer
2016/12/16 20:26:36
Done.
| |
| 305 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 370 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, |
| 306 base::Bind(&ClearURLHandlers)); | 371 base::Bind(&ClearURLHandlers)); |
| 307 TearDownInterceptor(); | 372 |
| 373 CheckHistogram(); | |
| 308 } | 374 } |
| 309 | 375 |
| 310 protected: | 376 protected: |
| 377 void WaitForRequestsDestroyed(const ReportExpectation& expectation) { | |
| 378 wait_helper_.Wait(expectation.num_reports()); | |
| 379 EXPECT_EQ(expectation.successful_reports, | |
| 380 interceptor()->successful_reports()); | |
| 381 EXPECT_EQ(expectation.failed_reports, interceptor()->failed_reports()); | |
| 382 EXPECT_EQ(expectation.delayed_reports, interceptor()->delayed_reports()); | |
| 383 interceptor()->ClearObservedReports(); | |
| 384 } | |
| 385 | |
| 311 net::URLRequestContextGetter* url_request_context_getter() { | 386 net::URLRequestContextGetter* url_request_context_getter() { |
| 312 return url_request_context_getter_.get(); | 387 return url_request_context_getter_.get(); |
| 313 } | 388 } |
| 314 | 389 |
| 315 void WaitForIOThread() { | 390 void WaitForIOThread() { |
| 316 scoped_refptr<base::ThreadTestHelper> io_helper( | 391 scoped_refptr<base::ThreadTestHelper> io_helper( |
| 317 new base::ThreadTestHelper(io_task_runner_)); | 392 new base::ThreadTestHelper(io_task_runner_)); |
| 318 ASSERT_TRUE(io_helper->Run()); | 393 ASSERT_TRUE(io_helper->Run()); |
| 319 } | 394 } |
| 320 | 395 |
| 396 CertificateReportingService* service() { return service_.get(); } | |
| 397 | |
| 321 // Sets service enabled state and waits for a reset event. | 398 // Sets service enabled state and waits for a reset event. |
| 322 void SetServiceEnabledAndWait(bool enabled) { | 399 void SetServiceEnabledAndWait(bool enabled) { |
| 323 service()->SetEnabled(enabled); | 400 service()->SetEnabled(enabled); |
| 324 WaitForIOThread(); | 401 WaitForIOThread(); |
| 325 } | 402 } |
| 326 | 403 |
| 327 void AdvanceClock(base::TimeDelta delta) { | 404 void AdvanceClock(base::TimeDelta delta) { |
| 328 content::BrowserThread::PostTask( | 405 content::BrowserThread::PostTask( |
| 329 content::BrowserThread::IO, FROM_HERE, | 406 content::BrowserThread::IO, FROM_HERE, |
| 330 base::Bind(&base::SimpleTestClock::Advance, base::Unretained(clock_), | 407 base::Bind(&base::SimpleTestClock::Advance, |
| 331 delta)); | 408 base::Unretained(clock_.get()), delta)); |
| 332 } | 409 } |
| 333 | 410 |
| 334 void SetNow(base::Time now) { | 411 void SetNow(base::Time now) { |
| 335 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 412 content::BrowserThread::PostTask( |
| 336 base::Bind(&base::SimpleTestClock::SetNow, | 413 content::BrowserThread::IO, FROM_HERE, |
| 337 base::Unretained(clock_), now)); | 414 base::Bind(&base::SimpleTestClock::SetNow, |
| 415 base::Unretained(clock_.get()), now)); | |
| 338 } | 416 } |
| 339 | 417 |
| 340 CertificateReportingService* service() { return service_.get(); } | 418 private: |
| 419 void SetUpURLRequestContextOnIOThread() { | |
| 420 network_delegate_.reset(new TestNetworkDelegate( | |
| 421 base::Bind(&CertificateReportingServiceTest::OnURLRequestDestroyed, | |
| 422 base::Unretained(this)))); | |
| 341 | 423 |
| 342 private: | 424 std::unique_ptr<net::TestURLRequestContext> url_request_context( |
| 425 new net::TestURLRequestContext(true)); | |
| 426 url_request_context->set_network_delegate(network_delegate_.get()); | |
| 427 url_request_context->Init(); | |
| 428 url_request_context_getter_ = new net::TestURLRequestContextGetter( | |
| 429 io_task_runner_, std::move(url_request_context)); | |
| 430 } | |
| 431 | |
| 432 void TearDownOnIOThread() { | |
| 433 url_request_context_getter_ = nullptr; | |
| 434 network_delegate_.reset(nullptr); | |
| 435 } | |
| 436 | |
| 437 void OnURLRequestDestroyed() { | |
| 438 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 439 wait_helper_.OnEvent(); | |
| 440 } | |
| 441 | |
| 343 // Must be initialized before url_request_context_getter_ | 442 // Must be initialized before url_request_context_getter_ |
| 344 content::TestBrowserThreadBundle thread_bundle_; | 443 content::TestBrowserThreadBundle thread_bundle_; |
| 345 | 444 |
| 445 std::unique_ptr<TestNetworkDelegate> network_delegate_; | |
| 446 certificate_reporting_test_utils::ReportWaitHelper wait_helper_; | |
| 447 | |
| 346 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 448 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 347 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 449 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 348 | 450 |
| 349 std::unique_ptr<CertificateReportingService> service_; | 451 std::unique_ptr<CertificateReportingService> service_; |
| 350 base::SimpleTestClock* clock_; | 452 std::unique_ptr<base::SimpleTestClock> clock_; |
| 453 | |
| 454 scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_; | |
| 455 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; | |
| 456 TestingProfile profile_; | |
| 351 }; | 457 }; |
| 352 | 458 |
| 353 TEST_F(CertificateReportingServiceTest, Send) { | 459 TEST_F(CertificateReportingServiceTest, Send) { |
| 460 SetExpectedHistogramCountOnTeardown(4); | |
| 461 | |
| 354 WaitForIOThread(); | 462 WaitForIOThread(); |
| 355 // Let all reports fail. | 463 // Let all reports fail. |
| 356 SetFailureMode( | 464 SetFailureMode( |
| 357 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 465 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 358 | 466 |
| 359 // Send two reports. Both should fail and get queued. | 467 // Send two reports. Both should fail and get queued. |
| 360 service()->Send("report0"); | 468 service()->Send("report0"); |
| 361 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); | 469 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
| 362 | 470 |
| 363 service()->Send("report1"); | 471 service()->Send("report1"); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 377 WaitForRequestsDestroyed(ReportExpectation::Successful({"report2"})); | 485 WaitForRequestsDestroyed(ReportExpectation::Successful({"report2"})); |
| 378 | 486 |
| 379 // Send pending reports. Previously failed and queued two reports should be | 487 // Send pending reports. Previously failed and queued two reports should be |
| 380 // observed. | 488 // observed. |
| 381 service()->SendPending(); | 489 service()->SendPending(); |
| 382 WaitForRequestsDestroyed( | 490 WaitForRequestsDestroyed( |
| 383 ReportExpectation::Successful({"report0", "report1"})); | 491 ReportExpectation::Successful({"report0", "report1"})); |
| 384 } | 492 } |
| 385 | 493 |
| 386 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) { | 494 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) { |
| 495 SetExpectedHistogramCountOnTeardown(0); | |
| 496 | |
| 387 // Let all reports succeed. | 497 // Let all reports succeed. |
| 388 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult:: | 498 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult:: |
| 389 REPORTS_SUCCESSFUL); | 499 REPORTS_SUCCESSFUL); |
| 390 | 500 |
| 391 // Disable the service. | 501 // Disable the service. |
| 392 SetServiceEnabledAndWait(false); | 502 SetServiceEnabledAndWait(false); |
| 393 | 503 |
| 394 // Send a report. Report attempt should be cancelled and no sent reports | 504 // Send a report. Report attempt should be cancelled and no sent reports |
| 395 // should be observed. | 505 // should be observed. |
| 396 service()->Send("report0"); | 506 service()->Send("report0"); |
| 397 | 507 |
| 398 // Enable the service and send a report again. | 508 // Enable the service and send a report again. It should be sent successfully. |
| 399 SetServiceEnabledAndWait(true); | 509 SetServiceEnabledAndWait(true); |
| 400 | 510 |
| 401 service()->Send("report1"); | 511 service()->Send("report1"); |
| 402 WaitForRequestsDestroyed(ReportExpectation::Successful({"report1"})); | 512 WaitForRequestsDestroyed(ReportExpectation::Successful({"report1"})); |
| 403 } | 513 } |
| 404 | 514 |
| 405 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) { | 515 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) { |
| 516 SetExpectedHistogramCountOnTeardown(1); | |
| 517 | |
| 406 // Let all reports fail. | 518 // Let all reports fail. |
| 407 SetFailureMode( | 519 SetFailureMode( |
| 408 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 520 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 409 | 521 |
| 410 service()->Send("report0"); | 522 service()->Send("report0"); |
| 411 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); | 523 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
| 412 | 524 |
| 413 // Disable the service. | 525 // Disable the service. |
| 414 SetServiceEnabledAndWait(false); | 526 SetServiceEnabledAndWait(false); |
| 415 | 527 |
| 416 // Sending has no effect while disabled, wait for a single cancelled event. | 528 // Sending has no effect while disabled. |
| 417 service()->SendPending(); | 529 service()->SendPending(); |
| 418 | 530 |
| 419 // Re-enable the service and send pending reports. Pending reports should have | 531 // Re-enable the service and send pending reports. Pending reports should have |
| 420 // been cleared when the service was disabled, so no report should be seen. | 532 // been cleared when the service was disabled, so no report should be seen. |
| 421 SetServiceEnabledAndWait(true); | 533 SetServiceEnabledAndWait(true); |
| 422 | 534 |
| 423 // Sending with empty queue has no effect. | 535 // Sending with empty queue has no effect. |
| 424 service()->SendPending(); | 536 service()->SendPending(); |
| 425 } | 537 } |
| 426 | 538 |
| 427 TEST_F(CertificateReportingServiceTest, DontSendOldReports) { | 539 TEST_F(CertificateReportingServiceTest, DontSendOldReports) { |
| 540 SetExpectedHistogramCountOnTeardown(5); | |
| 541 | |
| 428 SetNow(base::Time::Now()); | 542 SetNow(base::Time::Now()); |
| 429 // Let all reports fail. | 543 // Let all reports fail. |
| 430 SetFailureMode( | 544 SetFailureMode( |
| 431 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 545 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 432 | 546 |
| 433 // Send a report. | 547 // Send a report. |
| 434 service()->Send("report0"); | 548 service()->Send("report0"); |
| 435 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); | 549 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
| 436 | 550 |
| 437 // Advance the clock a bit and trigger another report. | 551 // Advance the clock a bit and trigger another report. |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 462 | 576 |
| 463 // Advance the clock 20 hours again so that report2 is 25 hours old and is | 577 // Advance the clock 20 hours again so that report2 is 25 hours old and is |
| 464 // older than max age (24 hours) | 578 // older than max age (24 hours) |
| 465 AdvanceClock(base::TimeDelta::FromHours(20)); | 579 AdvanceClock(base::TimeDelta::FromHours(20)); |
| 466 // Send pending reports. report2 should be discarded since it's too old. No | 580 // Send pending reports. report2 should be discarded since it's too old. No |
| 467 // other reports remain. | 581 // other reports remain. |
| 468 service()->SendPending(); | 582 service()->SendPending(); |
| 469 } | 583 } |
| 470 | 584 |
| 471 TEST_F(CertificateReportingServiceTest, DiscardOldReports) { | 585 TEST_F(CertificateReportingServiceTest, DiscardOldReports) { |
| 586 SetExpectedHistogramCountOnTeardown(7); | |
| 587 | |
| 472 SetNow(base::Time::Now()); | 588 SetNow(base::Time::Now()); |
| 473 // Let all reports fail. | 589 // Let all reports fail. |
| 474 SetFailureMode( | 590 SetFailureMode( |
| 475 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 591 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 476 | 592 |
| 477 // Send a failed report. | 593 // Send a failed report. |
| 478 service()->Send("report0"); | 594 service()->Send("report0"); |
| 479 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); | 595 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
| 480 | 596 |
| 481 // Send three more reports within five hours of each other. After this: | 597 // Send three more reports within five hours of each other. After this: |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 516 service()->SendPending(); | 632 service()->SendPending(); |
| 517 WaitForRequestsDestroyed( | 633 WaitForRequestsDestroyed( |
| 518 ReportExpectation::Successful({"report2", "report3"})); | 634 ReportExpectation::Successful({"report2", "report3"})); |
| 519 | 635 |
| 520 // Do a final send. No reports should be sent. | 636 // Do a final send. No reports should be sent. |
| 521 service()->SendPending(); | 637 service()->SendPending(); |
| 522 } | 638 } |
| 523 | 639 |
| 524 // A delayed report should successfully upload when it's resumed. | 640 // A delayed report should successfully upload when it's resumed. |
| 525 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) { | 641 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) { |
| 642 SetExpectedHistogramCountOnTeardown(0); | |
| 643 | |
| 526 // Let reports hang. | 644 // Let reports hang. |
| 527 SetFailureMode( | 645 SetFailureMode( |
| 528 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); | 646 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 529 // Send a report. The report upload hangs, so no error or success callbacks | 647 // Send a report. The report upload hangs, so no error or success callbacks |
| 530 // should be called. | 648 // should be called. |
| 531 service()->Send("report0"); | 649 service()->Send("report0"); |
| 532 | 650 |
| 533 // Resume the report upload and run the callbacks. The report should be | 651 // Resume the report upload and run the callbacks. The report should be |
| 534 // successfully sent. | 652 // successfully sent. |
| 535 ResumeDelayedRequest(); | 653 ResumeDelayedRequest(base::Bind(&base::DoNothing)); |
| 536 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0"})); | 654 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0"})); |
| 537 } | 655 } |
| 538 | 656 |
| 539 // Delayed reports should cleaned when the service is reset. | 657 // Delayed reports should cleaned when the service is reset. |
| 540 TEST_F(CertificateReportingServiceTest, Delayed_Reset) { | 658 TEST_F(CertificateReportingServiceTest, Delayed_Reset) { |
| 659 SetExpectedHistogramCountOnTeardown(0); | |
| 660 | |
| 541 // Let reports hang. | 661 // Let reports hang. |
| 542 SetFailureMode( | 662 SetFailureMode( |
| 543 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); | 663 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 544 // Send a report. The report is triggered but hangs, so no error or success | 664 // Send a report. The report is triggered but hangs, so no error or success |
| 545 // callbacks should be called. | 665 // callbacks should be called. |
| 546 service()->Send("report0"); | 666 service()->Send("report0"); |
| 547 | 667 |
| 548 // Disable the service. This should reset the reporting service and | 668 // Disable the service. This should reset the reporting service and |
| 549 // clear all pending reports. | 669 // clear all pending reports. |
| 550 SetServiceEnabledAndWait(false); | 670 SetServiceEnabledAndWait(false); |
| 551 | 671 |
| 552 // Resume delayed report. No report should be observed since the service | 672 // Resume delayed report. No report should be observed since the service |
| 553 // should have reset and all pending reports should be cleared. If any report | 673 // should have reset and all pending reports should be cleared. If any report |
| 554 // is observed, the next WaitForRequestsDestroyed() will fail. | 674 // is observed, the next WaitForRequestsDestroyed() will fail. |
| 555 ResumeDelayedRequest(); | 675 ResumeDelayedRequest(base::Bind(&base::DoNothing)); |
| 556 | 676 |
| 557 // Enable the service. | 677 // Enable the service. |
| 558 SetServiceEnabledAndWait(true); | 678 SetServiceEnabledAndWait(true); |
| 559 | 679 |
| 560 // Send a report. The report is triggered but hangs, so no error or success | 680 // Send a report. The report is triggered but hangs, so no error or success |
| 561 // callbacks should be called. The report id is again 0 since the pending | 681 // callbacks should be called. The report id is again 0 since the pending |
| 562 // report queue has been cleared above. | 682 // report queue has been cleared above. |
| 563 service()->Send("report1"); | 683 service()->Send("report1"); |
| 564 | 684 |
| 565 // Resume delayed report. The report should be observed. | 685 // Resume delayed report. Two reports are successfully sent. |
| 566 ResumeDelayedRequest(); | 686 ResumeDelayedRequest(base::Bind(&base::DoNothing)); |
| 567 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0", "report1"})); | 687 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0", "report1"})); |
| 568 } | 688 } |
| OLD | NEW |