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::CertificateReportingServiceTestHelper; |
| 33 using certificate_reporting_test_utils::ReportExpectation; |
| 34 |
28 namespace { | 35 namespace { |
29 | 36 |
30 // Maximum number of reports kept in the certificate reporting service's retry | 37 // Maximum number of reports kept in the certificate reporting service's retry |
31 // queue. | 38 // queue. |
32 const size_t kMaxReportCountInQueue = 3; | 39 const size_t kMaxReportCountInQueue = 3; |
33 | 40 |
| 41 const char* kFailedReportHistogram = "SSL.CertificateErrorReportFailure"; |
| 42 |
34 void ClearURLHandlers() { | 43 void ClearURLHandlers() { |
35 net::URLRequestFilter::GetInstance()->ClearHandlers(); | 44 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
36 } | 45 } |
37 | 46 |
| 47 // A network delegate used to observe URL request destructions. The tests check |
| 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. |
| 70 class ReportHistogramTestHelper { |
| 71 public: |
| 72 // Sets the expected histogram value to be checked during teardown. |
| 73 void SetExpectedFailedReportCount(unsigned int num_expected_failed_report) { |
| 74 num_expected_failed_report_ = num_expected_failed_report; |
| 75 } |
| 76 |
| 77 void CheckHistogram() { |
| 78 if (num_expected_failed_report_ != 0) { |
| 79 histogram_tester_.ExpectUniqueSample(kFailedReportHistogram, |
| 80 -net::ERR_SSL_PROTOCOL_ERROR, |
| 81 num_expected_failed_report_); |
| 82 } else { |
| 83 histogram_tester_.ExpectTotalCount(kFailedReportHistogram, 0); |
| 84 } |
| 85 } |
| 86 |
| 87 private: |
| 88 unsigned int num_expected_failed_report_ = 0; |
| 89 base::HistogramTester histogram_tester_; |
| 90 }; |
| 91 |
38 } // namespace | 92 } // namespace |
39 | 93 |
40 TEST(CertificateReportingServiceReportListTest, BoundedReportList) { | 94 TEST(CertificateReportingServiceReportListTest, BoundedReportList) { |
41 // Create a report list with maximum of 2 items. | 95 // Create a report list with maximum of 2 items. |
42 CertificateReportingService::BoundedReportList list(2 /* max_size */); | 96 CertificateReportingService::BoundedReportList list(2 /* max_size */); |
43 EXPECT_EQ(0u, list.items().size()); | 97 EXPECT_EQ(0u, list.items().size()); |
44 | 98 |
45 // Add a ten minute old report. | 99 // Add a ten minute old report. |
46 list.Add(CertificateReportingService::Report( | 100 list.Add(CertificateReportingService::Report( |
47 1, base::Time::Now() - base::TimeDelta::FromMinutes(10), | 101 1, base::Time::Now() - base::TimeDelta::FromMinutes(10), |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 void SetUp() override { | 136 void SetUp() override { |
83 message_loop_.reset(new base::MessageLoopForIO()); | 137 message_loop_.reset(new base::MessageLoopForIO()); |
84 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, | 138 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, |
85 message_loop_.get())); | 139 message_loop_.get())); |
86 url_request_context_getter_ = | 140 url_request_context_getter_ = |
87 new net::TestURLRequestContextGetter(message_loop_->task_runner()); | 141 new net::TestURLRequestContextGetter(message_loop_->task_runner()); |
88 net::URLRequestFailedJob::AddUrlHandler(); | 142 net::URLRequestFailedJob::AddUrlHandler(); |
89 net::URLRequestMockDataJob::AddUrlHandler(); | 143 net::URLRequestMockDataJob::AddUrlHandler(); |
90 } | 144 } |
91 | 145 |
92 void TearDown() override { ClearURLHandlers(); } | 146 void TearDown() override { |
| 147 ClearURLHandlers(); |
| 148 // Check the histogram as the last thing. This makes sure no in-flight |
| 149 // report is missed. |
| 150 histogram_test_helper_.CheckHistogram(); |
| 151 } |
93 | 152 |
94 protected: | 153 protected: |
95 net::URLRequestContextGetter* url_request_context_getter() { | 154 net::URLRequestContextGetter* url_request_context_getter() { |
96 return url_request_context_getter_.get(); | 155 return url_request_context_getter_.get(); |
97 } | 156 } |
98 | 157 |
| 158 void SetExpectedFailedReportCountOnTearDown(unsigned int count) { |
| 159 histogram_test_helper_.SetExpectedFailedReportCount(count); |
| 160 } |
| 161 |
99 private: | 162 private: |
100 std::unique_ptr<base::MessageLoopForIO> message_loop_; | 163 std::unique_ptr<base::MessageLoopForIO> message_loop_; |
101 std::unique_ptr<content::TestBrowserThread> io_thread_; | 164 std::unique_ptr<content::TestBrowserThread> io_thread_; |
102 | 165 |
103 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 166 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 167 ReportHistogramTestHelper histogram_test_helper_; |
104 }; | 168 }; |
105 | 169 |
106 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, | 170 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, |
107 Reporter_RetriesEnabled) { | 171 Reporter_RetriesEnabled) { |
| 172 SetExpectedFailedReportCountOnTearDown(6); |
| 173 |
108 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); | 174 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
109 base::Time reference_time = base::Time::Now(); | 175 base::Time reference_time = base::Time::Now(); |
110 clock->SetNow(reference_time); | 176 clock->SetNow(reference_time); |
111 | 177 |
112 const GURL kFailureURL = | 178 const GURL kFailureURL = |
113 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); | 179 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); |
114 certificate_reporting::ErrorReporter* certificate_error_reporter = | 180 certificate_reporting::ErrorReporter* certificate_error_reporter = |
115 new certificate_reporting::ErrorReporter( | 181 new certificate_reporting::ErrorReporter( |
116 url_request_context_getter()->GetURLRequestContext(), kFailureURL, | 182 url_request_context_getter()->GetURLRequestContext(), kFailureURL, |
117 net::ReportSender::DO_NOT_SEND_COOKIES); | 183 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)); | 261 clock->Advance(base::TimeDelta::FromSeconds(1)); |
196 reporter.SendPending(); | 262 reporter.SendPending(); |
197 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); | 263 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); |
198 base::RunLoop().RunUntilIdle(); | 264 base::RunLoop().RunUntilIdle(); |
199 EXPECT_EQ(0u, list->items().size()); | 265 EXPECT_EQ(0u, list->items().size()); |
200 } | 266 } |
201 | 267 |
202 // Same as above, but retries are disabled. | 268 // Same as above, but retries are disabled. |
203 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, | 269 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, |
204 Reporter_RetriesDisabled) { | 270 Reporter_RetriesDisabled) { |
| 271 SetExpectedFailedReportCountOnTearDown(2); |
| 272 |
205 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); | 273 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
206 base::Time reference_time = base::Time::Now(); | 274 base::Time reference_time = base::Time::Now(); |
207 clock->SetNow(reference_time); | 275 clock->SetNow(reference_time); |
208 | 276 |
209 const GURL kFailureURL = | 277 const GURL kFailureURL = |
210 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); | 278 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); |
211 certificate_reporting::ErrorReporter* certificate_error_reporter = | 279 certificate_reporting::ErrorReporter* certificate_error_reporter = |
212 new certificate_reporting::ErrorReporter( | 280 new certificate_reporting::ErrorReporter( |
213 url_request_context_getter()->GetURLRequestContext(), kFailureURL, | 281 url_request_context_getter()->GetURLRequestContext(), kFailureURL, |
214 net::ReportSender::DO_NOT_SEND_COOKIES); | 282 net::ReportSender::DO_NOT_SEND_COOKIES); |
(...skipping 25 matching lines...) Expand all Loading... |
240 base::RunLoop().RunUntilIdle(); | 308 base::RunLoop().RunUntilIdle(); |
241 ASSERT_EQ(0u, list->items().size()); | 309 ASSERT_EQ(0u, list->items().size()); |
242 | 310 |
243 // Send pending reports. Nothing should be sent. | 311 // Send pending reports. Nothing should be sent. |
244 clock->Advance(base::TimeDelta::FromSeconds(10)); | 312 clock->Advance(base::TimeDelta::FromSeconds(10)); |
245 reporter.SendPending(); | 313 reporter.SendPending(); |
246 base::RunLoop().RunUntilIdle(); | 314 base::RunLoop().RunUntilIdle(); |
247 ASSERT_EQ(0u, list->items().size()); | 315 ASSERT_EQ(0u, list->items().size()); |
248 } | 316 } |
249 | 317 |
250 class CertificateReportingServiceTest | 318 class CertificateReportingServiceTest : public ::testing::Test { |
251 : public ::testing::Test, | |
252 public certificate_reporting_test_utils:: | |
253 CertificateReportingServiceTestBase { | |
254 public: | 319 public: |
255 CertificateReportingServiceTest() | 320 CertificateReportingServiceTest() |
256 : CertificateReportingServiceTestBase(), | 321 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), |
257 thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), | |
258 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( | 322 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( |
259 content::BrowserThread::IO)) {} | 323 content::BrowserThread::IO)) {} |
260 | 324 |
261 ~CertificateReportingServiceTest() override {} | 325 ~CertificateReportingServiceTest() override {} |
262 | 326 |
263 void SetUp() override { | 327 void SetUp() override { |
264 SetUpInterceptor(); | 328 test_helper_.SetUpInterceptor(); |
265 WaitForIOThread(); | 329 WaitForIOThread(); |
266 | 330 |
267 content::BrowserThread::PostTask( | 331 content::BrowserThread::PostTask( |
268 content::BrowserThread::IO, FROM_HERE, | 332 content::BrowserThread::IO, FROM_HERE, |
269 base::Bind( | 333 base::Bind( |
270 &CertificateReportingServiceTest::SetUpURLRequestContextOnIOThread, | 334 &CertificateReportingServiceTest::SetUpURLRequestContextOnIOThread, |
271 base::Unretained(this))); | 335 base::Unretained(this))); |
272 WaitForIOThread(); | 336 WaitForIOThread(); |
273 | 337 |
274 clock_ = new base::SimpleTestClock(); | 338 safe_browsing::SafeBrowsingService::RegisterFactory(&sb_service_factory); |
| 339 sb_service_ = sb_service_factory.CreateSafeBrowsingService(); |
| 340 |
| 341 clock_.reset(new base::SimpleTestClock()); |
275 service_.reset(new CertificateReportingService( | 342 service_.reset(new CertificateReportingService( |
276 url_request_context_getter(), server_public_key(), | 343 sb_service_.get(), url_request_context_getter(), &profile_, |
277 server_public_key_version(), kMaxReportCountInQueue, | 344 test_helper_.server_public_key(), |
278 base::TimeDelta::FromHours(24), std::unique_ptr<base::Clock>(clock_))); | 345 test_helper_.server_public_key_version(), kMaxReportCountInQueue, |
| 346 base::TimeDelta::FromHours(24), clock_.get())); |
279 // Wait for service reset. | 347 // Wait for service reset. |
280 WaitForIOThread(); | 348 WaitForIOThread(); |
281 } | 349 } |
282 | 350 |
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 { | 351 void TearDown() override { |
293 WaitForIOThread(); | 352 WaitForIOThread(); |
294 EXPECT_TRUE(interceptor()->successful_reports().empty()); | 353 EXPECT_TRUE(test_helper_.interceptor()->successful_reports().empty()); |
295 EXPECT_TRUE(interceptor()->failed_reports().empty()); | 354 EXPECT_TRUE(test_helper_.interceptor()->failed_reports().empty()); |
296 EXPECT_TRUE(interceptor()->delayed_reports().empty()); | 355 EXPECT_TRUE(test_helper_.interceptor()->delayed_reports().empty()); |
297 EXPECT_EQ(0u, service() | 356 EXPECT_EQ(0u, service() |
298 ->GetReporterForTesting() | 357 ->GetReporterForTesting() |
299 ->inflight_report_count_for_testing()); | 358 ->inflight_report_count_for_testing()); |
300 | 359 |
301 service_->Shutdown(); | 360 service_->Shutdown(); |
302 WaitForIOThread(); | 361 WaitForIOThread(); |
| 362 service_.reset(nullptr); |
303 | 363 |
304 service_.reset(nullptr); | 364 content::BrowserThread::PostTask( |
| 365 content::BrowserThread::IO, FROM_HERE, |
| 366 base::Bind(&CertificateReportingServiceTest::TearDownOnIOThread, |
| 367 base::Unretained(this))); |
305 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 368 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, |
306 base::Bind(&ClearURLHandlers)); | 369 base::Bind(&ClearURLHandlers)); |
307 TearDownInterceptor(); | 370 WaitForIOThread(); |
| 371 |
| 372 histogram_test_helper_.CheckHistogram(); |
308 } | 373 } |
309 | 374 |
310 protected: | 375 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 |
311 net::URLRequestContextGetter* url_request_context_getter() { | 387 net::URLRequestContextGetter* url_request_context_getter() { |
312 return url_request_context_getter_.get(); | 388 return url_request_context_getter_.get(); |
313 } | 389 } |
314 | 390 |
315 void WaitForIOThread() { | 391 void WaitForIOThread() { |
316 scoped_refptr<base::ThreadTestHelper> io_helper( | 392 scoped_refptr<base::ThreadTestHelper> io_helper( |
317 new base::ThreadTestHelper(io_task_runner_)); | 393 new base::ThreadTestHelper(io_task_runner_)); |
318 ASSERT_TRUE(io_helper->Run()); | 394 ASSERT_TRUE(io_helper->Run()); |
319 } | 395 } |
320 | 396 |
| 397 CertificateReportingService* service() { return service_.get(); } |
| 398 |
321 // Sets service enabled state and waits for a reset event. | 399 // Sets service enabled state and waits for a reset event. |
322 void SetServiceEnabledAndWait(bool enabled) { | 400 void SetServiceEnabledAndWait(bool enabled) { |
323 service()->SetEnabled(enabled); | 401 service()->SetEnabled(enabled); |
324 WaitForIOThread(); | 402 WaitForIOThread(); |
325 } | 403 } |
326 | 404 |
327 void AdvanceClock(base::TimeDelta delta) { | 405 void AdvanceClock(base::TimeDelta delta) { |
328 content::BrowserThread::PostTask( | 406 content::BrowserThread::PostTask( |
329 content::BrowserThread::IO, FROM_HERE, | 407 content::BrowserThread::IO, FROM_HERE, |
330 base::Bind(&base::SimpleTestClock::Advance, base::Unretained(clock_), | 408 base::Bind(&base::SimpleTestClock::Advance, |
331 delta)); | 409 base::Unretained(clock_.get()), delta)); |
332 } | 410 } |
333 | 411 |
334 void SetNow(base::Time now) { | 412 void SetNow(base::Time now) { |
335 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 413 content::BrowserThread::PostTask( |
336 base::Bind(&base::SimpleTestClock::SetNow, | 414 content::BrowserThread::IO, FROM_HERE, |
337 base::Unretained(clock_), now)); | 415 base::Bind(&base::SimpleTestClock::SetNow, |
| 416 base::Unretained(clock_.get()), now)); |
338 } | 417 } |
339 | 418 |
340 CertificateReportingService* service() { return service_.get(); } | 419 void SetExpectedFailedReportCountOnTearDown(unsigned int count) { |
| 420 histogram_test_helper_.SetExpectedFailedReportCount(count); |
| 421 } |
| 422 |
| 423 CertificateReportingServiceTestHelper* test_helper() { return &test_helper_; } |
341 | 424 |
342 private: | 425 private: |
| 426 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( |
| 432 new net::TestURLRequestContext(true)); |
| 433 url_request_context->set_network_delegate(network_delegate_.get()); |
| 434 url_request_context->Init(); |
| 435 url_request_context_getter_ = new net::TestURLRequestContextGetter( |
| 436 io_task_runner_, std::move(url_request_context)); |
| 437 } |
| 438 |
| 439 void TearDownOnIOThread() { |
| 440 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 } |
| 448 |
343 // Must be initialized before url_request_context_getter_ | 449 // Must be initialized before url_request_context_getter_ |
344 content::TestBrowserThreadBundle thread_bundle_; | 450 content::TestBrowserThreadBundle thread_bundle_; |
345 | 451 |
| 452 std::unique_ptr<TestNetworkDelegate> network_delegate_; |
| 453 certificate_reporting_test_utils::ReportWaitHelper wait_helper_; |
| 454 |
346 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 455 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
347 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 456 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
348 | 457 |
349 std::unique_ptr<CertificateReportingService> service_; | 458 std::unique_ptr<CertificateReportingService> service_; |
350 base::SimpleTestClock* clock_; | 459 std::unique_ptr<base::SimpleTestClock> clock_; |
| 460 |
| 461 scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_; |
| 462 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; |
| 463 TestingProfile profile_; |
| 464 |
| 465 CertificateReportingServiceTestHelper test_helper_; |
| 466 ReportHistogramTestHelper histogram_test_helper_; |
351 }; | 467 }; |
352 | 468 |
353 TEST_F(CertificateReportingServiceTest, Send) { | 469 TEST_F(CertificateReportingServiceTest, Send) { |
354 WaitForIOThread(); | 470 SetExpectedFailedReportCountOnTearDown(4); |
| 471 |
355 // Let all reports fail. | 472 // Let all reports fail. |
356 SetFailureMode( | 473 test_helper()->SetFailureMode( |
357 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 474 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
358 | 475 |
359 // Send two reports. Both should fail and get queued. | 476 // Send two reports. Both should fail and get queued. |
360 service()->Send("report0"); | 477 service()->Send("report0"); |
361 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); | 478 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
362 | 479 |
363 service()->Send("report1"); | 480 service()->Send("report1"); |
364 WaitForRequestsDestroyed(ReportExpectation::Failed({"report1"})); | 481 WaitForRequestsDestroyed(ReportExpectation::Failed({"report1"})); |
365 | 482 |
366 // Send pending reports. Previously queued reports should be observed. They | 483 // Send pending reports. Previously queued reports should be observed. They |
367 // will also be queued again. | 484 // will also be queued again. |
368 service()->SendPending(); | 485 service()->SendPending(); |
369 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0", "report1"})); | 486 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0", "report1"})); |
370 | 487 |
371 // Let all reports succeed. | 488 // Let all reports succeed. |
372 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult:: | 489 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
373 REPORTS_SUCCESSFUL); | 490 ReportSendingResult::REPORTS_SUCCESSFUL); |
374 | 491 |
375 // Send a third report. This should not be queued. | 492 // Send a third report. This should not be queued. |
376 service()->Send("report2"); | 493 service()->Send("report2"); |
377 WaitForRequestsDestroyed(ReportExpectation::Successful({"report2"})); | 494 WaitForRequestsDestroyed(ReportExpectation::Successful({"report2"})); |
378 | 495 |
379 // Send pending reports. Previously failed and queued two reports should be | 496 // Send pending reports. Previously failed and queued two reports should be |
380 // observed. | 497 // observed. |
381 service()->SendPending(); | 498 service()->SendPending(); |
382 WaitForRequestsDestroyed( | 499 WaitForRequestsDestroyed( |
383 ReportExpectation::Successful({"report0", "report1"})); | 500 ReportExpectation::Successful({"report0", "report1"})); |
384 } | 501 } |
385 | 502 |
386 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) { | 503 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) { |
| 504 SetExpectedFailedReportCountOnTearDown(0); |
| 505 |
387 // Let all reports succeed. | 506 // Let all reports succeed. |
388 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult:: | 507 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
389 REPORTS_SUCCESSFUL); | 508 ReportSendingResult::REPORTS_SUCCESSFUL); |
390 | 509 |
391 // Disable the service. | 510 // Disable the service. |
392 SetServiceEnabledAndWait(false); | 511 SetServiceEnabledAndWait(false); |
393 | 512 |
394 // Send a report. Report attempt should be cancelled and no sent reports | 513 // Send a report. Report attempt should be cancelled and no sent reports |
395 // should be observed. | 514 // should be observed. |
396 service()->Send("report0"); | 515 service()->Send("report0"); |
397 | 516 |
398 // Enable the service and send a report again. | 517 // Enable the service and send a report again. It should be sent successfully. |
399 SetServiceEnabledAndWait(true); | 518 SetServiceEnabledAndWait(true); |
400 | 519 |
401 service()->Send("report1"); | 520 service()->Send("report1"); |
402 WaitForRequestsDestroyed(ReportExpectation::Successful({"report1"})); | 521 WaitForRequestsDestroyed(ReportExpectation::Successful({"report1"})); |
403 } | 522 } |
404 | 523 |
405 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) { | 524 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) { |
| 525 SetExpectedFailedReportCountOnTearDown(1); |
| 526 |
406 // Let all reports fail. | 527 // Let all reports fail. |
407 SetFailureMode( | 528 test_helper()->SetFailureMode( |
408 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 529 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
409 | 530 |
410 service()->Send("report0"); | 531 service()->Send("report0"); |
411 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); | 532 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
412 | 533 |
413 // Disable the service. | 534 // Disable the service. |
414 SetServiceEnabledAndWait(false); | 535 SetServiceEnabledAndWait(false); |
415 | 536 |
416 // Sending has no effect while disabled, wait for a single cancelled event. | 537 // Sending has no effect while disabled. |
417 service()->SendPending(); | 538 service()->SendPending(); |
418 | 539 |
419 // Re-enable the service and send pending reports. Pending reports should have | 540 // 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. | 541 // been cleared when the service was disabled, so no report should be seen. |
421 SetServiceEnabledAndWait(true); | 542 SetServiceEnabledAndWait(true); |
422 | 543 |
423 // Sending with empty queue has no effect. | 544 // Sending with empty queue has no effect. |
424 service()->SendPending(); | 545 service()->SendPending(); |
425 } | 546 } |
426 | 547 |
427 TEST_F(CertificateReportingServiceTest, DontSendOldReports) { | 548 TEST_F(CertificateReportingServiceTest, DontSendOldReports) { |
| 549 SetExpectedFailedReportCountOnTearDown(5); |
| 550 |
428 SetNow(base::Time::Now()); | 551 SetNow(base::Time::Now()); |
429 // Let all reports fail. | 552 // Let all reports fail. |
430 SetFailureMode( | 553 test_helper()->SetFailureMode( |
431 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 554 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
432 | 555 |
433 // Send a report. | 556 // Send a report. |
434 service()->Send("report0"); | 557 service()->Send("report0"); |
435 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); | 558 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
436 | 559 |
437 // Advance the clock a bit and trigger another report. | 560 // Advance the clock a bit and trigger another report. |
438 AdvanceClock(base::TimeDelta::FromHours(5)); | 561 AdvanceClock(base::TimeDelta::FromHours(5)); |
439 | 562 |
440 service()->Send("report1"); | 563 service()->Send("report1"); |
(...skipping 21 matching lines...) Expand all Loading... |
462 | 585 |
463 // Advance the clock 20 hours again so that report2 is 25 hours old and is | 586 // Advance the clock 20 hours again so that report2 is 25 hours old and is |
464 // older than max age (24 hours) | 587 // older than max age (24 hours) |
465 AdvanceClock(base::TimeDelta::FromHours(20)); | 588 AdvanceClock(base::TimeDelta::FromHours(20)); |
466 // Send pending reports. report2 should be discarded since it's too old. No | 589 // Send pending reports. report2 should be discarded since it's too old. No |
467 // other reports remain. | 590 // other reports remain. |
468 service()->SendPending(); | 591 service()->SendPending(); |
469 } | 592 } |
470 | 593 |
471 TEST_F(CertificateReportingServiceTest, DiscardOldReports) { | 594 TEST_F(CertificateReportingServiceTest, DiscardOldReports) { |
| 595 SetExpectedFailedReportCountOnTearDown(7); |
| 596 |
472 SetNow(base::Time::Now()); | 597 SetNow(base::Time::Now()); |
473 // Let all reports fail. | 598 // Let all reports fail. |
474 SetFailureMode( | 599 test_helper()->SetFailureMode( |
475 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); | 600 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
476 | 601 |
477 // Send a failed report. | 602 // Send a failed report. |
478 service()->Send("report0"); | 603 service()->Send("report0"); |
479 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); | 604 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
480 | 605 |
481 // Send three more reports within five hours of each other. After this: | 606 // Send three more reports within five hours of each other. After this: |
482 // report0 is 0 hours after reference time (15 hours old). | 607 // report0 is 0 hours after reference time (15 hours old). |
483 // report1 is 5 hours after reference time (10 hours old). | 608 // report1 is 5 hours after reference time (10 hours old). |
484 // report2 is 10 hours after reference time (5 hours old). | 609 // report2 is 10 hours after reference time (5 hours old). |
(...skipping 10 matching lines...) Expand all Loading... |
495 ReportExpectation::Failed({"report1", "report2", "report3"})); | 620 ReportExpectation::Failed({"report1", "report2", "report3"})); |
496 | 621 |
497 // Send pending reports. Four reports were generated above, but the service | 622 // Send pending reports. Four reports were generated above, but the service |
498 // only queues three reports, so the very first one should be dropped since | 623 // only queues three reports, so the very first one should be dropped since |
499 // it's the oldest. | 624 // it's the oldest. |
500 service()->SendPending(); | 625 service()->SendPending(); |
501 WaitForRequestsDestroyed( | 626 WaitForRequestsDestroyed( |
502 ReportExpectation::Failed({"report1", "report2", "report3"})); | 627 ReportExpectation::Failed({"report1", "report2", "report3"})); |
503 | 628 |
504 // Let all reports succeed. | 629 // Let all reports succeed. |
505 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult:: | 630 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
506 REPORTS_SUCCESSFUL); | 631 ReportSendingResult::REPORTS_SUCCESSFUL); |
507 | 632 |
508 // Advance the clock by 15 hours. Current time is now 30 hours after reference | 633 // Advance the clock by 15 hours. Current time is now 30 hours after reference |
509 // time. The ages of reports are now as follows: | 634 // time. The ages of reports are now as follows: |
510 // report1 is 25 hours old. | 635 // report1 is 25 hours old. |
511 // report2 is 20 hours old. | 636 // report2 is 20 hours old. |
512 // report3 is 15 hours old. | 637 // report3 is 15 hours old. |
513 AdvanceClock(base::TimeDelta::FromHours(15)); | 638 AdvanceClock(base::TimeDelta::FromHours(15)); |
514 // Send pending reports. Only report2 and report3 should be sent, report1 | 639 // Send pending reports. Only report2 and report3 should be sent, report1 |
515 // should be ignored because it's too old. | 640 // should be ignored because it's too old. |
516 service()->SendPending(); | 641 service()->SendPending(); |
517 WaitForRequestsDestroyed( | 642 WaitForRequestsDestroyed( |
518 ReportExpectation::Successful({"report2", "report3"})); | 643 ReportExpectation::Successful({"report2", "report3"})); |
519 | 644 |
520 // Do a final send. No reports should be sent. | 645 // Do a final send. No reports should be sent. |
521 service()->SendPending(); | 646 service()->SendPending(); |
522 } | 647 } |
523 | 648 |
524 // A delayed report should successfully upload when it's resumed. | 649 // A delayed report should successfully upload when it's resumed. |
525 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) { | 650 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) { |
| 651 SetExpectedFailedReportCountOnTearDown(0); |
| 652 |
526 // Let reports hang. | 653 // Let reports hang. |
527 SetFailureMode( | 654 test_helper()->SetFailureMode( |
528 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); | 655 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
529 // Send a report. The report upload hangs, so no error or success callbacks | 656 // Send a report. The report upload hangs, so no error or success callbacks |
530 // should be called. | 657 // should be called. |
531 service()->Send("report0"); | 658 service()->Send("report0"); |
532 | 659 |
533 // Resume the report upload and run the callbacks. The report should be | 660 // Resume the report upload and run the callbacks. The report should be |
534 // successfully sent. | 661 // successfully sent. |
535 ResumeDelayedRequest(); | 662 test_helper()->ResumeDelayedRequest(base::Bind(&base::DoNothing)); |
536 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0"})); | 663 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0"})); |
537 } | 664 } |
538 | 665 |
539 // Delayed reports should cleaned when the service is reset. | 666 // Delayed reports should cleaned when the service is reset. |
540 TEST_F(CertificateReportingServiceTest, Delayed_Reset) { | 667 TEST_F(CertificateReportingServiceTest, Delayed_Reset) { |
| 668 SetExpectedFailedReportCountOnTearDown(0); |
| 669 |
541 // Let reports hang. | 670 // Let reports hang. |
542 SetFailureMode( | 671 test_helper()->SetFailureMode( |
543 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); | 672 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
544 // Send a report. The report is triggered but hangs, so no error or success | 673 // Send a report. The report is triggered but hangs, so no error or success |
545 // callbacks should be called. | 674 // callbacks should be called. |
546 service()->Send("report0"); | 675 service()->Send("report0"); |
547 | 676 |
548 // Disable the service. This should reset the reporting service and | 677 // Disable the service. This should reset the reporting service and |
549 // clear all pending reports. | 678 // clear all pending reports. |
550 SetServiceEnabledAndWait(false); | 679 SetServiceEnabledAndWait(false); |
551 | 680 |
552 // Resume delayed report. No report should be observed since the service | 681 // 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 | 682 // should have reset and all pending reports should be cleared. If any report |
554 // is observed, the next WaitForRequestsDestroyed() will fail. | 683 // is observed, the next WaitForRequestsDestroyed() will fail. |
555 ResumeDelayedRequest(); | 684 test_helper()->ResumeDelayedRequest(base::Bind(&base::DoNothing)); |
556 | 685 |
557 // Enable the service. | 686 // Enable the service. |
558 SetServiceEnabledAndWait(true); | 687 SetServiceEnabledAndWait(true); |
559 | 688 |
560 // Send a report. The report is triggered but hangs, so no error or success | 689 // 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 | 690 // callbacks should be called. The report id is again 0 since the pending |
562 // report queue has been cleared above. | 691 // report queue has been cleared above. |
563 service()->Send("report1"); | 692 service()->Send("report1"); |
564 | 693 |
565 // Resume delayed report. The report should be observed. | 694 // Resume delayed report. Two reports are successfully sent. |
566 ResumeDelayedRequest(); | 695 test_helper()->ResumeDelayedRequest(base::Bind(&base::DoNothing)); |
567 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0", "report1"})); | 696 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0", "report1"})); |
568 } | 697 } |
OLD | NEW |