OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/incident_reporting_service.h" | 5 #include "chrome/browser/safe_browsing/incident_reporting_service.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 enum SafeBrowsingDisposition { | 111 enum SafeBrowsingDisposition { |
112 SAFE_BROWSING_OPT_OUT, | 112 SAFE_BROWSING_OPT_OUT, |
113 SAFE_BROWSING_OPT_IN, | 113 SAFE_BROWSING_OPT_IN, |
114 }; | 114 }; |
115 | 115 |
116 // A type for specifying the action to be taken by the test fixture during | 116 // A type for specifying the action to be taken by the test fixture during |
117 // profile initialization (before NOTIFICATION_PROFILE_ADDED is sent). | 117 // profile initialization (before NOTIFICATION_PROFILE_ADDED is sent). |
118 enum OnProfileAdditionAction { | 118 enum OnProfileAdditionAction { |
119 ON_PROFILE_ADDITION_NO_ACTION, | 119 ON_PROFILE_ADDITION_NO_ACTION, |
120 ON_PROFILE_ADDITION_ADD_INCIDENT, // Add an incident to the service. | 120 ON_PROFILE_ADDITION_ADD_INCIDENT, // Add an incident to the service. |
| 121 ON_PROFILE_ADDITION_ADD_TWO_INCIDENTS, // Add two incidents to the service. |
121 }; | 122 }; |
122 | 123 |
123 // A type for specifying the action to be taken by the test fixture when the | 124 // A type for specifying the action to be taken by the test fixture when the |
124 // service creates a LastDownloadFinder. | 125 // service creates a LastDownloadFinder. |
125 enum OnCreateDownloadFinderAction { | 126 enum OnCreateDownloadFinderAction { |
126 // Post a task that reports a download. | 127 // Post a task that reports a download. |
127 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND, | 128 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND, |
128 // Post a task that reports no downloads found. | 129 // Post a task that reports no downloads found. |
129 ON_CREATE_DOWNLOAD_FINDER_NO_DOWNLOADS, | 130 ON_CREATE_DOWNLOAD_FINDER_NO_DOWNLOADS, |
130 // Immediately return due to a lack of eligible profiles. | 131 // Immediately return due to a lack of eligible profiles. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 incident->set_incident_time_msec(kIncidentTimeMsec); | 214 incident->set_incident_time_msec(kIncidentTimeMsec); |
214 incident->mutable_tracked_preference(); | 215 incident->mutable_tracked_preference(); |
215 return incident.Pass(); | 216 return incident.Pass(); |
216 } | 217 } |
217 | 218 |
218 // Adds a test incident to the service. | 219 // Adds a test incident to the service. |
219 void AddTestIncident(Profile* profile) { | 220 void AddTestIncident(Profile* profile) { |
220 instance_->GetAddIncidentCallback(profile).Run(MakeTestIncident().Pass()); | 221 instance_->GetAddIncidentCallback(profile).Run(MakeTestIncident().Pass()); |
221 } | 222 } |
222 | 223 |
223 // Confirms that the test incident was uploaded by the service, then clears | 224 // Confirms that the test incident(s) was/were uploaded by the service, then |
224 // the instance for subsequent incidents. | 225 // clears the instance for subsequent incidents. |
225 void ExpectTestIncidentUploaded() { | 226 void ExpectTestIncidentUploaded(int incident_count) { |
226 ASSERT_TRUE(uploaded_report_); | 227 ASSERT_TRUE(uploaded_report_); |
227 ASSERT_EQ(1, uploaded_report_->incident_size()); | 228 ASSERT_EQ(incident_count, uploaded_report_->incident_size()); |
228 ASSERT_TRUE(uploaded_report_->incident(0).has_incident_time_msec()); | 229 for (int i = 0; i < incident_count; ++i) { |
229 ASSERT_EQ(kIncidentTimeMsec, | 230 ASSERT_TRUE(uploaded_report_->incident(i).has_incident_time_msec()); |
230 uploaded_report_->incident(0).incident_time_msec()); | 231 ASSERT_EQ(kIncidentTimeMsec, |
| 232 uploaded_report_->incident(i).incident_time_msec()); |
| 233 } |
231 ASSERT_TRUE(uploaded_report_->has_environment()); | 234 ASSERT_TRUE(uploaded_report_->has_environment()); |
232 ASSERT_TRUE(uploaded_report_->environment().has_os()); | 235 ASSERT_TRUE(uploaded_report_->environment().has_os()); |
233 ASSERT_TRUE(uploaded_report_->environment().os().has_os_name()); | 236 ASSERT_TRUE(uploaded_report_->environment().os().has_os_name()); |
234 ASSERT_EQ(std::string(kFakeOsName), | 237 ASSERT_EQ(std::string(kFakeOsName), |
235 uploaded_report_->environment().os().os_name()); | 238 uploaded_report_->environment().os().os_name()); |
236 ASSERT_EQ(std::string(kFakeDownloadToken), | 239 ASSERT_EQ(std::string(kFakeDownloadToken), |
237 uploaded_report_->download().token()); | 240 uploaded_report_->download().token()); |
238 | 241 |
239 uploaded_report_.reset(); | 242 uploaded_report_.reset(); |
240 } | 243 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 // A callback run by the test fixture when a profile is added. An incident | 351 // A callback run by the test fixture when a profile is added. An incident |
349 // is added. | 352 // is added. |
350 void PreProfileAdd(Profile* profile) { | 353 void PreProfileAdd(Profile* profile) { |
351 // The instance must have already been created. | 354 // The instance must have already been created. |
352 ASSERT_TRUE(instance_); | 355 ASSERT_TRUE(instance_); |
353 // Add a test incident to the service if requested. | 356 // Add a test incident to the service if requested. |
354 switch (profile_properties_[GetProfileName(profile)].on_addition_action) { | 357 switch (profile_properties_[GetProfileName(profile)].on_addition_action) { |
355 case ON_PROFILE_ADDITION_ADD_INCIDENT: | 358 case ON_PROFILE_ADDITION_ADD_INCIDENT: |
356 AddTestIncident(profile); | 359 AddTestIncident(profile); |
357 break; | 360 break; |
| 361 case ON_PROFILE_ADDITION_ADD_TWO_INCIDENTS: |
| 362 AddTestIncident(profile); |
| 363 AddTestIncident(profile); |
| 364 break; |
358 default: | 365 default: |
359 ASSERT_EQ( | 366 ASSERT_EQ( |
360 ON_PROFILE_ADDITION_NO_ACTION, | 367 ON_PROFILE_ADDITION_NO_ACTION, |
361 profile_properties_[GetProfileName(profile)].on_addition_action); | 368 profile_properties_[GetProfileName(profile)].on_addition_action); |
362 break; | 369 break; |
363 } | 370 } |
364 } | 371 } |
365 | 372 |
366 // A fake CollectEnvironmentData implementation invoked by the service during | 373 // A fake CollectEnvironmentData implementation invoked by the service during |
367 // operation. | 374 // operation. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 task_runner_->RunUntilIdle(); | 451 task_runner_->RunUntilIdle(); |
445 | 452 |
446 // Verify that environment collection took place. | 453 // Verify that environment collection took place. |
447 EXPECT_TRUE(HasCollectedEnvironmentData()); | 454 EXPECT_TRUE(HasCollectedEnvironmentData()); |
448 | 455 |
449 // Verify that the most recent download was looked for. | 456 // Verify that the most recent download was looked for. |
450 EXPECT_TRUE(HasCreatedDownloadFinder()); | 457 EXPECT_TRUE(HasCreatedDownloadFinder()); |
451 | 458 |
452 // Verify that report upload took place and contained the incident, | 459 // Verify that report upload took place and contained the incident, |
453 // environment data, and download details. | 460 // environment data, and download details. |
454 ExpectTestIncidentUploaded(); | 461 ExpectTestIncidentUploaded(1); |
455 | 462 |
456 // Verify that the download finder and the uploader were destroyed. | 463 // Verify that the download finder and the uploader were destroyed. |
457 ASSERT_TRUE(DownloadFinderDestroyed()); | 464 ASSERT_TRUE(DownloadFinderDestroyed()); |
| 465 ASSERT_TRUE(UploaderDestroyed()); |
| 466 } |
| 467 |
| 468 // Tests that multiple incidents are coalesced into the same report. |
| 469 TEST_F(IncidentReportingServiceTest, CoalesceIncidents) { |
| 470 CreateProfile( |
| 471 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_TWO_INCIDENTS); |
| 472 |
| 473 // Let all tasks run. |
| 474 task_runner_->RunUntilIdle(); |
| 475 |
| 476 // Verify that environment collection took place. |
| 477 EXPECT_TRUE(HasCollectedEnvironmentData()); |
| 478 |
| 479 // Verify that the most recent download was looked for. |
| 480 EXPECT_TRUE(HasCreatedDownloadFinder()); |
| 481 |
| 482 // Verify that report upload took place and contained the incident, |
| 483 // environment data, and download details. |
| 484 ExpectTestIncidentUploaded(2); |
| 485 |
| 486 // Verify that the download finder and the uploader were destroyed. |
| 487 ASSERT_TRUE(DownloadFinderDestroyed()); |
458 ASSERT_TRUE(UploaderDestroyed()); | 488 ASSERT_TRUE(UploaderDestroyed()); |
459 } | 489 } |
460 | 490 |
461 // Tests that an incident added during profile initialization when safe browsing | 491 // Tests that an incident added during profile initialization when safe browsing |
462 // is off is not uploaded. | 492 // is off is not uploaded. |
463 TEST_F(IncidentReportingServiceTest, NoSafeBrowsing) { | 493 TEST_F(IncidentReportingServiceTest, NoSafeBrowsing) { |
464 // Create the profile, thereby causing the test to begin. | 494 // Create the profile, thereby causing the test to begin. |
465 CreateProfile( | 495 CreateProfile( |
466 "profile1", SAFE_BROWSING_OPT_OUT, ON_PROFILE_ADDITION_ADD_INCIDENT); | 496 "profile1", SAFE_BROWSING_OPT_OUT, ON_PROFILE_ADDITION_ADD_INCIDENT); |
467 | 497 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 TEST_F(IncidentReportingServiceTest, OnlyOneUpload) { | 547 TEST_F(IncidentReportingServiceTest, OnlyOneUpload) { |
518 // Create the profile, thereby causing the test to begin. | 548 // Create the profile, thereby causing the test to begin. |
519 Profile* profile = CreateProfile( | 549 Profile* profile = CreateProfile( |
520 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); | 550 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
521 | 551 |
522 // Let all tasks run. | 552 // Let all tasks run. |
523 task_runner_->RunUntilIdle(); | 553 task_runner_->RunUntilIdle(); |
524 | 554 |
525 // Verify that report upload took place and contained the incident and | 555 // Verify that report upload took place and contained the incident and |
526 // environment data. | 556 // environment data. |
527 ExpectTestIncidentUploaded(); | 557 ExpectTestIncidentUploaded(1); |
528 | 558 |
529 // Add the incident to the service again. | 559 // Add the incident to the service again. |
530 AddTestIncident(profile); | 560 AddTestIncident(profile); |
531 | 561 |
532 // Let all tasks run. | 562 // Let all tasks run. |
533 task_runner_->RunUntilIdle(); | 563 task_runner_->RunUntilIdle(); |
534 | 564 |
535 // Verify that no additional report upload took place. | 565 // Verify that no additional report upload took place. |
536 AssertNoUpload(); | 566 AssertNoUpload(); |
537 } | 567 } |
538 | 568 |
539 // Tests that the same incident added for two different profiles in sequence | 569 // Tests that the same incident added for two different profiles in sequence |
540 // results in two uploads. | 570 // results in two uploads. |
541 TEST_F(IncidentReportingServiceTest, TwoProfilesTwoUploads) { | 571 TEST_F(IncidentReportingServiceTest, TwoProfilesTwoUploads) { |
542 // Create the profile, thereby causing the test to begin. | 572 // Create the profile, thereby causing the test to begin. |
543 CreateProfile( | 573 CreateProfile( |
544 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); | 574 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
545 | 575 |
546 // Let all tasks run. | 576 // Let all tasks run. |
547 task_runner_->RunUntilIdle(); | 577 task_runner_->RunUntilIdle(); |
548 | 578 |
549 // Verify that report upload took place and contained the incident and | 579 // Verify that report upload took place and contained the incident and |
550 // environment data. | 580 // environment data. |
551 ExpectTestIncidentUploaded(); | 581 ExpectTestIncidentUploaded(1); |
552 | 582 |
553 // Create a second profile with its own incident on addition. | 583 // Create a second profile with its own incident on addition. |
554 CreateProfile( | 584 CreateProfile( |
555 "profile2", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); | 585 "profile2", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
556 | 586 |
557 // Let all tasks run. | 587 // Let all tasks run. |
558 task_runner_->RunUntilIdle(); | 588 task_runner_->RunUntilIdle(); |
559 | 589 |
560 // Verify that a second report upload took place. | 590 // Verify that a second report upload took place. |
561 ExpectTestIncidentUploaded(); | 591 ExpectTestIncidentUploaded(1); |
562 } | 592 } |
563 | 593 |
564 // Tests that an upload succeeds if the profile is destroyed while it is | 594 // Tests that an upload succeeds if the profile is destroyed while it is |
565 // pending. | 595 // pending. |
566 TEST_F(IncidentReportingServiceTest, ProfileDestroyedDuringUpload) { | 596 TEST_F(IncidentReportingServiceTest, ProfileDestroyedDuringUpload) { |
567 // Create a profile for which an incident will be added. | 597 // Create a profile for which an incident will be added. |
568 Profile* profile = CreateProfile( | 598 Profile* profile = CreateProfile( |
569 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); | 599 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
570 | 600 |
571 // Hook up a callback to run when the upload is started that will post a task | 601 // Hook up a callback to run when the upload is started that will post a task |
572 // to delete the profile. This task will run before the upload finishes. | 602 // to delete the profile. This task will run before the upload finishes. |
573 DeleteProfileOnUpload(profile); | 603 DeleteProfileOnUpload(profile); |
574 | 604 |
575 // Let all tasks run. | 605 // Let all tasks run. |
576 task_runner_->RunUntilIdle(); | 606 task_runner_->RunUntilIdle(); |
577 | 607 |
578 // Verify that report upload took place and contained the incident and | 608 // Verify that report upload took place and contained the incident and |
579 // environment data. | 609 // environment data. |
580 ExpectTestIncidentUploaded(); | 610 ExpectTestIncidentUploaded(1); |
581 | 611 |
582 // The lack of a crash indicates that the deleted profile was not accessed by | 612 // The lack of a crash indicates that the deleted profile was not accessed by |
583 // the service while handling the upload response. | 613 // the service while handling the upload response. |
584 } | 614 } |
585 | 615 |
586 // Parallel uploads | 616 // Parallel uploads |
587 // Shutdown during processing | 617 // Shutdown during processing |
588 // environment colection taking longer than incident delay timer | 618 // environment colection taking longer than incident delay timer |
589 // environment colection taking longer than incident delay timer, and then | 619 // environment colection taking longer than incident delay timer, and then |
590 // another incident arriving | 620 // another incident arriving |
OLD | NEW |