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

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

Issue 2503243003: Wire up CertificateReportingService to handle report uploads (Closed)
Patch Set: Rebase onto crrev/2543523002 Created 4 years 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 <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.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/simple_test_clock.h" 13 #include "base/test/simple_test_clock.h"
14 #include "base/time/clock.h" 14 #include "base/time/clock.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "chrome/browser/safe_browsing/certificate_reporting_service_test_utils. h" 16 #include "chrome/browser/safe_browsing/certificate_reporting_service_test_utils. h"
17 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
18 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
19 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread.h" 21 #include "content/public/test/test_browser_thread.h"
19 #include "content/public/test/test_browser_thread_bundle.h" 22 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "net/base/network_delegate_impl.h" 23 #include "net/base/network_delegate_impl.h"
21 #include "net/test/url_request/url_request_failed_job.h" 24 #include "net/test/url_request/url_request_failed_job.h"
22 #include "net/test/url_request/url_request_mock_data_job.h" 25 #include "net/test/url_request/url_request_mock_data_job.h"
23 #include "net/url_request/url_request_filter.h" 26 #include "net/url_request/url_request_filter.h"
24 #include "net/url_request/url_request_test_util.h" 27 #include "net/url_request/url_request_test_util.h"
25 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
26 29
27 namespace { 30 namespace {
28 void ClearURLHandlers() { 31 void ClearURLHandlers() {
29 net::URLRequestFilter::GetInstance()->ClearHandlers(); 32 net::URLRequestFilter::GetInstance()->ClearHandlers();
30 } 33 }
31
32 // Maximum number of reports kept in the certificate reporting service's retry
33 // queue.
34 const size_t kMaxReportCountInQueue = 3;
35
36 } // namespace 34 } // namespace
37 35
38 TEST(CertificateReportingServiceReportListTest, BoundedReportList) { 36 TEST(CertificateReportingServiceReportListTest, BoundedReportList) {
39 // Create a report list with maximum of 2 items. 37 // Create a report list with maximum of 2 items.
40 CertificateReportingService::BoundedReportList list(2 /* max_size */); 38 CertificateReportingService::BoundedReportList list(2 /* max_size */);
41 EXPECT_EQ(0u, list.items().size()); 39 EXPECT_EQ(0u, list.items().size());
42 40
43 // Add a ten minute old report. 41 // Add a ten minute old report.
44 list.Add(CertificateReportingService::Report( 42 list.Add(CertificateReportingService::Report(
45 1, base::Time::Now() - base::TimeDelta::FromMinutes(10), 43 1, base::Time::Now() - base::TimeDelta::FromMinutes(10),
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 base::RunLoop().RunUntilIdle(); 236 base::RunLoop().RunUntilIdle();
239 ASSERT_EQ(0u, list->items().size()); 237 ASSERT_EQ(0u, list->items().size());
240 238
241 // Send pending reports. Nothing should be sent. 239 // Send pending reports. Nothing should be sent.
242 clock->Advance(base::TimeDelta::FromSeconds(10)); 240 clock->Advance(base::TimeDelta::FromSeconds(10));
243 reporter.SendPending(); 241 reporter.SendPending();
244 base::RunLoop().RunUntilIdle(); 242 base::RunLoop().RunUntilIdle();
245 ASSERT_EQ(0u, list->items().size()); 243 ASSERT_EQ(0u, list->items().size());
246 } 244 }
247 245
246 namespace {
247
248 const size_t kMaxReportCountInQueue = 3;
249
250 } // namespace
251
248 class CertificateReportingServiceTest 252 class CertificateReportingServiceTest
249 : public ::testing::Test, 253 : public ::testing::Test,
250 public certificate_reporting_test_utils:: 254 public certificate_reporting_test_utils::
251 CertificateReportingServiceTestBase { 255 CertificateReportingServiceTestBase {
252 public: 256 public:
253 enum ReportSendStatus {
254 // Indicates that the send attempt is completed.
255 EXPECT_SEND_ATTEMPT_COMPLETED,
256 // Indicates that the send attempt is cancelled (e.g. extended reporting was
257 // not opted in).
258 EXPECT_SEND_ATTEMPT_CANCELLED,
259 };
260
261 CertificateReportingServiceTest() 257 CertificateReportingServiceTest()
262 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), 258 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD),
263 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( 259 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread(
264 content::BrowserThread::IO)), 260 content::BrowserThread::IO)),
265 url_request_context_getter_( 261 url_request_context_getter_(
266 new net::TestURLRequestContextGetter(io_task_runner_)) {} 262 new net::TestURLRequestContextGetter(io_task_runner_)) {}
267 263
268 ~CertificateReportingServiceTest() override {} 264 ~CertificateReportingServiceTest() override {}
269 265
270 void SetUp() override { 266 void SetUp() override {
271 SetUpInterceptor(); 267 SetUpInterceptor();
272 268
269 safe_browsing::SafeBrowsingService::RegisterFactory(&sb_service_factory);
270 sb_service_ = sb_service_factory.CreateSafeBrowsingService();
271
273 // Create an event observer to wait for a single "Reset" event that happens 272 // Create an event observer to wait for a single "Reset" event that happens
274 // on service initialization. 273 // on service initialization.
275 certificate_reporting_test_utils::ReportEventObserver* observer = 274 certificate_reporting_test_utils::ReportEventObserver* observer =
276 new certificate_reporting_test_utils::ReportEventObserver(1); 275 new certificate_reporting_test_utils::ReportEventObserver(1);
277 276
278 service_.reset(new CertificateReportingService( 277 service_.reset(new CertificateReportingService(
279 url_request_context_getter_, 278 sb_service_, url_request_context_getter_, &profile_,
280 std::unique_ptr<certificate_reporting_test_utils::ReportEventObserver>( 279 std::unique_ptr<certificate_reporting_test_utils::ReportEventObserver>(
281 observer), 280 observer),
282 kMaxReportCountInQueue, base::TimeDelta::FromHours(24), &clock_)); 281 kMaxReportCountInQueue, base::TimeDelta::FromHours(24), &clock_));
283 // Wait for reporting service initialization. 282 // Wait for reporting service initialization.
284 observer->WaitForEvents(); 283 observer->WaitForEvents();
285 } 284 }
286 285
287 void TearDown() override { 286 void TearDown() override {
288 // Wait for reporting service shutdown. 287 // Wait for reporting service shutdown.
289 certificate_reporting_test_utils::ReportEventObserver* event_observer = 288 certificate_reporting_test_utils::ReportEventObserver* event_observer =
(...skipping 16 matching lines...) Expand all
306 new certificate_reporting_test_utils::ReportEventObserver( 305 new certificate_reporting_test_utils::ReportEventObserver(
307 num_events_to_wait_for); 306 num_events_to_wait_for);
308 service_->SetEventObserverForTesting( 307 service_->SetEventObserverForTesting(
309 std::unique_ptr<CertificateReportingService::EventObserver>(observer)); 308 std::unique_ptr<CertificateReportingService::EventObserver>(observer));
310 return observer; 309 return observer;
311 } 310 }
312 311
313 CertificateReportingService* service() { return service_.get(); } 312 CertificateReportingService* service() { return service_.get(); }
314 313
315 // Sends a test report and checks expected events. 314 // Sends a test report and checks expected events.
316 void SendReport(ReportSendStatus expected_send_attempt_status, 315 void SendReport(ObservedReportEvent expected_observed_event,
317 const std::set<int>& expected_successful_report_ids, 316 const std::set<int>& expected_successful_report_ids,
318 const std::set<int>& expected_failed_report_ids) { 317 const std::set<int>& expected_failed_report_ids) {
319 // Observe events: 1 event for send completion or cancellation, 318 // Observe events: 1 event for send completion or cancellation,
320 // the rest for observed reports after send completion. 319 // the rest for observed reports after send completion.
321 certificate_reporting_test_utils::ReportEventObserver* report_observer = 320 certificate_reporting_test_utils::ReportEventObserver* report_observer =
322 NewEventObserver(1u + expected_successful_report_ids.size() + 321 NewEventObserver(1u + expected_successful_report_ids.size() +
323 expected_failed_report_ids.size()); 322 expected_failed_report_ids.size());
324 service()->Send("test_report"); 323 service()->Send("test_report");
325 report_observer->WaitForEvents(); 324 report_observer->WaitForEvents();
326 325
327 EXPECT_EQ(0u, report_observer->num_resets()); 326 EXPECT_NE(EXPECT_SERVICE_RESET, expected_observed_event);
328 if (expected_send_attempt_status == EXPECT_SEND_ATTEMPT_COMPLETED) { 327 CheckExpectedResults(*report_observer, expected_observed_event,
329 EXPECT_EQ(1u, report_observer->num_completed_attempts()); 328 expected_successful_report_ids,
330 EXPECT_EQ(0u, report_observer->num_cancelled_attempts()); 329 expected_failed_report_ids);
331 } else {
332 EXPECT_EQ(0u, report_observer->num_completed_attempts());
333 EXPECT_EQ(1u, report_observer->num_cancelled_attempts());
334 }
335 EXPECT_EQ(expected_successful_report_ids,
336 report_observer->successful_report_ids());
337 EXPECT_EQ(expected_failed_report_ids, report_observer->failed_report_ids());
338 } 330 }
339 331
340 // Sends pending reports and checks expected events. 332 // Sends pending reports and checks expected events.
341 void SendPendingReports(ReportSendStatus expected_send_attempt_status, 333 void SendPendingReports(ObservedReportEvent expected_observed_event,
342 const std::set<int>& expected_successful_report_ids, 334 const std::set<int>& expected_successful_report_ids,
343 const std::set<int>& expected_failed_report_ids) { 335 const std::set<int>& expected_failed_report_ids) {
344 // Observe events: 1 event for send completion or cancellation, 336 // Observe events: 1 event for send completion or cancellation,
345 // the rest for observed reports after send completion. 337 // the rest for observed reports after send completion.
346 certificate_reporting_test_utils::ReportEventObserver* report_observer = 338 certificate_reporting_test_utils::ReportEventObserver* report_observer =
347 NewEventObserver(1u + expected_successful_report_ids.size() + 339 NewEventObserver(1u + expected_successful_report_ids.size() +
348 expected_failed_report_ids.size()); 340 expected_failed_report_ids.size());
349 service()->SendPending(); 341 service()->SendPending();
350 report_observer->WaitForEvents(); 342 report_observer->WaitForEvents();
351 343
352 EXPECT_EQ(0u, report_observer->num_resets()); 344 EXPECT_NE(EXPECT_SERVICE_RESET, expected_observed_event);
353 if (expected_send_attempt_status == EXPECT_SEND_ATTEMPT_COMPLETED) { 345 CheckExpectedResults(*report_observer, expected_observed_event,
354 EXPECT_EQ(1u, report_observer->num_completed_attempts()); 346 expected_successful_report_ids,
355 EXPECT_EQ(0u, report_observer->num_cancelled_attempts()); 347 expected_failed_report_ids);
356 } else {
357 EXPECT_EQ(0u, report_observer->num_completed_attempts());
358 EXPECT_EQ(1u, report_observer->num_cancelled_attempts());
359 }
360 EXPECT_EQ(expected_successful_report_ids,
361 report_observer->successful_report_ids());
362 EXPECT_EQ(expected_failed_report_ids, report_observer->failed_report_ids());
363 } 348 }
364 349
365 // Sets service enabled state and waits for a reset event. 350 // Sets service enabled state and waits for a reset event.
366 void SetServiceEnabledAndWait(bool enabled) { 351 void SetServiceEnabledAndWait(bool enabled) {
367 certificate_reporting_test_utils::ReportEventObserver* report_observer = 352 certificate_reporting_test_utils::ReportEventObserver* report_observer =
368 NewEventObserver(1u); 353 NewEventObserver(1u);
369 service()->SetEnabled(enabled); 354 service()->SetEnabled(enabled);
370 report_observer->WaitForEvents(); 355 report_observer->WaitForEvents();
371 EXPECT_EQ(1u, report_observer->num_resets()); 356
372 EXPECT_EQ(0u, report_observer->num_cancelled_attempts()); 357 CheckExpectedResults(*report_observer, EXPECT_SERVICE_RESET,
373 EXPECT_EQ(0u, report_observer->num_completed_attempts()); 358 std::set<int>(), std::set<int>());
374 EXPECT_EQ(std::set<int>(), report_observer->successful_report_ids());
375 EXPECT_EQ(std::set<int>(), report_observer->failed_report_ids());
376 } 359 }
377 360
378 // Resumes delayed requests and checks expected events. 361 // Resumes delayed requests and checks expected events.
379 void ResumeDelayedRequests( 362 void ResumeDelayedRequests(
380 size_t num_expected_events, 363 size_t num_expected_events,
381 const std::set<int>& expected_successful_report_ids, 364 const std::set<int>& expected_successful_report_ids,
382 const std::set<int>& expected_failed_report_ids) { 365 const std::set<int>& expected_failed_report_ids) {
383 certificate_reporting_test_utils::ReportEventObserver* report_observer = 366 certificate_reporting_test_utils::ReportEventObserver* report_observer =
384 NewEventObserver(num_expected_events); 367 NewEventObserver(num_expected_events);
385 ResumeDelayedRequest(); 368 ResumeDelayedRequest();
386 report_observer->WaitForEvents(); 369 report_observer->WaitForEvents();
387 EXPECT_EQ(0u, report_observer->num_completed_attempts()); 370
388 EXPECT_EQ(0u, report_observer->num_cancelled_attempts()); 371 CheckExpectedResults(*report_observer, EXPECT_NONE,
389 EXPECT_EQ(0u, report_observer->num_resets()); 372 expected_successful_report_ids,
390 EXPECT_EQ(expected_successful_report_ids, 373 expected_failed_report_ids);
391 report_observer->successful_report_ids());
392 EXPECT_EQ(expected_failed_report_ids, report_observer->failed_report_ids());
393 } 374 }
394 375
395 base::SimpleTestClock* clock() { return &clock_; } 376 base::SimpleTestClock* clock() { return &clock_; }
396 377
397 private: 378 private:
398 // Must be initialized before url_request_context_getter_ 379 // Must be initialized before url_request_context_getter_
399 content::TestBrowserThreadBundle thread_bundle_; 380 content::TestBrowserThreadBundle thread_bundle_;
400 381
401 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 382 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
402 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 383 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
403 384
404 std::unique_ptr<CertificateReportingService> service_; 385 std::unique_ptr<CertificateReportingService> service_;
405 base::SimpleTestClock clock_; 386 base::SimpleTestClock clock_;
387
388 safe_browsing::SafeBrowsingService* sb_service_;
389 safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory;
390 TestingProfile profile_;
406 }; 391 };
407 392
408 TEST_F(CertificateReportingServiceTest, Send) { 393 TEST_F(CertificateReportingServiceTest, Send) {
409 // Let all reports fail. 394 // Let all reports fail.
410 SetFailureMode( 395 SetFailureMode(
411 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); 396 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
412 397
413 // Send a report. A failed report with id = 0 should be observed. 398 // Send a report. A failed report with id = 0 should be observed.
414 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED, 399 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
415 std::set<int>() /* expected_successful_report_ids */, 400 std::set<int>() /* expected_successful_report_ids */,
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 // report queue has been cleared above. 629 // report queue has been cleared above.
645 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED, 630 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
646 std::set<int>() /* expected_successful_report_ids */, 631 std::set<int>() /* expected_successful_report_ids */,
647 std::set<int>() /* expected_failed_report_ids */); 632 std::set<int>() /* expected_failed_report_ids */);
648 633
649 // Resume delayed report. The report should be observed. 634 // Resume delayed report. The report should be observed.
650 ResumeDelayedRequests(1u /* num_expected_events */, 635 ResumeDelayedRequests(1u /* num_expected_events */,
651 std::set<int>{0} /* expected_successful_report_ids */, 636 std::set<int>{0} /* expected_successful_report_ids */,
652 std::set<int>() /* expected_failed_report_ids */); 637 std::set<int>() /* expected_failed_report_ids */);
653 } 638 }
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/certificate_reporting_service_test_utils.cc ('k') | chrome/browser/safe_browsing/ping_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698