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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND, | 128 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND, |
129 // Post a task that reports no downloads found. | 129 // Post a task that reports no downloads found. |
130 ON_CREATE_DOWNLOAD_FINDER_NO_DOWNLOADS, | 130 ON_CREATE_DOWNLOAD_FINDER_NO_DOWNLOADS, |
131 // Immediately return due to a lack of eligible profiles. | 131 // Immediately return due to a lack of eligible profiles. |
132 ON_CREATE_DOWNLOAD_FINDER_NO_PROFILES, | 132 ON_CREATE_DOWNLOAD_FINDER_NO_PROFILES, |
133 }; | 133 }; |
134 | 134 |
135 static const int64 kIncidentTimeMsec; | 135 static const int64 kIncidentTimeMsec; |
136 static const char kFakeOsName[]; | 136 static const char kFakeOsName[]; |
137 static const char kFakeDownloadToken[]; | 137 static const char kFakeDownloadToken[]; |
| 138 static const char kTestTrackedPrefPath[]; |
138 | 139 |
139 IncidentReportingServiceTest() | 140 IncidentReportingServiceTest() |
140 : task_runner_(new base::TestSimpleTaskRunner), | 141 : task_runner_(new base::TestSimpleTaskRunner), |
141 thread_task_runner_handle_(task_runner_), | 142 thread_task_runner_handle_(task_runner_), |
142 profile_manager_(TestingBrowserProcess::GetGlobal()), | 143 profile_manager_(TestingBrowserProcess::GetGlobal()), |
143 instance_(new TestIncidentReportingService( | 144 instance_(new TestIncidentReportingService( |
144 task_runner_, | 145 task_runner_, |
145 base::Bind(&IncidentReportingServiceTest::PreProfileAdd, | 146 base::Bind(&IncidentReportingServiceTest::PreProfileAdd, |
146 base::Unretained(this)), | 147 base::Unretained(this)), |
147 base::Bind(&IncidentReportingServiceTest::CollectEnvironmentData, | 148 base::Bind(&IncidentReportingServiceTest::CollectEnvironmentData, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 base::Unretained(this), | 206 base::Unretained(this), |
206 profile); | 207 profile); |
207 } | 208 } |
208 | 209 |
209 // Returns an incident suitable for testing. | 210 // Returns an incident suitable for testing. |
210 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> | 211 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> |
211 MakeTestIncident() { | 212 MakeTestIncident() { |
212 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( | 213 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
213 new safe_browsing::ClientIncidentReport_IncidentData()); | 214 new safe_browsing::ClientIncidentReport_IncidentData()); |
214 incident->set_incident_time_msec(kIncidentTimeMsec); | 215 incident->set_incident_time_msec(kIncidentTimeMsec); |
215 incident->mutable_tracked_preference(); | 216 safe_browsing::ClientIncidentReport_IncidentData_TrackedPreferenceIncident* |
| 217 tp_incident = incident->mutable_tracked_preference(); |
| 218 tp_incident->set_path(kTestTrackedPrefPath); |
216 return incident.Pass(); | 219 return incident.Pass(); |
217 } | 220 } |
218 | 221 |
219 // Adds a test incident to the service. | 222 // Adds a test incident to the service. |
220 void AddTestIncident(Profile* profile) { | 223 void AddTestIncident(Profile* profile) { |
221 instance_->GetAddIncidentCallback(profile).Run(MakeTestIncident().Pass()); | 224 instance_->GetAddIncidentCallback(profile).Run(MakeTestIncident().Pass()); |
222 } | 225 } |
223 | 226 |
224 // Confirms that the test incident(s) was/were uploaded by the service, then | 227 // Confirms that the test incident(s) was/were uploaded by the service, then |
225 // clears the instance for subsequent incidents. | 228 // clears the instance for subsequent incidents. |
226 void ExpectTestIncidentUploaded(int incident_count) { | 229 void ExpectTestIncidentUploaded(int incident_count) { |
227 ASSERT_TRUE(uploaded_report_); | 230 ASSERT_TRUE(uploaded_report_); |
228 ASSERT_EQ(incident_count, uploaded_report_->incident_size()); | 231 ASSERT_EQ(incident_count, uploaded_report_->incident_size()); |
229 for (int i = 0; i < incident_count; ++i) { | 232 for (int i = 0; i < incident_count; ++i) { |
230 ASSERT_TRUE(uploaded_report_->incident(i).has_incident_time_msec()); | 233 ASSERT_TRUE(uploaded_report_->incident(i).has_incident_time_msec()); |
231 ASSERT_EQ(kIncidentTimeMsec, | 234 ASSERT_EQ(kIncidentTimeMsec, |
232 uploaded_report_->incident(i).incident_time_msec()); | 235 uploaded_report_->incident(i).incident_time_msec()); |
| 236 ASSERT_TRUE(uploaded_report_->incident(i).has_tracked_preference()); |
| 237 ASSERT_TRUE( |
| 238 uploaded_report_->incident(i).tracked_preference().has_path()); |
| 239 ASSERT_EQ(std::string(kTestTrackedPrefPath), |
| 240 uploaded_report_->incident(i).tracked_preference().path()); |
233 } | 241 } |
234 ASSERT_TRUE(uploaded_report_->has_environment()); | 242 ASSERT_TRUE(uploaded_report_->has_environment()); |
235 ASSERT_TRUE(uploaded_report_->environment().has_os()); | 243 ASSERT_TRUE(uploaded_report_->environment().has_os()); |
236 ASSERT_TRUE(uploaded_report_->environment().os().has_os_name()); | 244 ASSERT_TRUE(uploaded_report_->environment().os().has_os_name()); |
237 ASSERT_EQ(std::string(kFakeOsName), | 245 ASSERT_EQ(std::string(kFakeOsName), |
238 uploaded_report_->environment().os().os_name()); | 246 uploaded_report_->environment().os().os_name()); |
239 ASSERT_EQ(std::string(kFakeDownloadToken), | 247 ASSERT_EQ(std::string(kFakeDownloadToken), |
240 uploaded_report_->download().token()); | 248 uploaded_report_->download().token()); |
241 | 249 |
242 uploaded_report_.reset(); | 250 uploaded_report_.reset(); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 | 441 |
434 // static | 442 // static |
435 base::LazyInstance<base::ThreadLocalPointer< | 443 base::LazyInstance<base::ThreadLocalPointer< |
436 IncidentReportingServiceTest::TestIncidentReportingService> >::Leaky | 444 IncidentReportingServiceTest::TestIncidentReportingService> >::Leaky |
437 IncidentReportingServiceTest::TestIncidentReportingService::test_instance_ = | 445 IncidentReportingServiceTest::TestIncidentReportingService::test_instance_ = |
438 LAZY_INSTANCE_INITIALIZER; | 446 LAZY_INSTANCE_INITIALIZER; |
439 | 447 |
440 const int64 IncidentReportingServiceTest::kIncidentTimeMsec = 47LL; | 448 const int64 IncidentReportingServiceTest::kIncidentTimeMsec = 47LL; |
441 const char IncidentReportingServiceTest::kFakeOsName[] = "fakedows"; | 449 const char IncidentReportingServiceTest::kFakeOsName[] = "fakedows"; |
442 const char IncidentReportingServiceTest::kFakeDownloadToken[] = "fakedlt"; | 450 const char IncidentReportingServiceTest::kFakeDownloadToken[] = "fakedlt"; |
| 451 const char IncidentReportingServiceTest::kTestTrackedPrefPath[] = "some_pref"; |
443 | 452 |
444 // Tests that an incident added during profile initialization when safe browsing | 453 // Tests that an incident added during profile initialization when safe browsing |
445 // is on is uploaded. | 454 // is on is uploaded. |
446 TEST_F(IncidentReportingServiceTest, AddIncident) { | 455 TEST_F(IncidentReportingServiceTest, AddIncident) { |
447 CreateProfile( | 456 CreateProfile( |
448 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); | 457 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
449 | 458 |
450 // Let all tasks run. | 459 // Let all tasks run. |
451 task_runner_->RunUntilIdle(); | 460 task_runner_->RunUntilIdle(); |
452 | 461 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 | 545 |
537 // Verify that the download finder was run but that no report upload took | 546 // Verify that the download finder was run but that no report upload took |
538 // place. | 547 // place. |
539 EXPECT_TRUE(HasCreatedDownloadFinder()); | 548 EXPECT_TRUE(HasCreatedDownloadFinder()); |
540 AssertNoUpload(); | 549 AssertNoUpload(); |
541 // Although CreateDownloadFinder was called, no instance was returned so there | 550 // Although CreateDownloadFinder was called, no instance was returned so there |
542 // is nothing to have been destroyed. | 551 // is nothing to have been destroyed. |
543 EXPECT_FALSE(DownloadFinderDestroyed()); | 552 EXPECT_FALSE(DownloadFinderDestroyed()); |
544 } | 553 } |
545 | 554 |
546 // Tests that an incident added after upload is not uploaded again. | 555 // Tests that an identical incident added after upload is not uploaded again. |
547 TEST_F(IncidentReportingServiceTest, OnlyOneUpload) { | 556 TEST_F(IncidentReportingServiceTest, OneIncidentOneUpload) { |
548 // Create the profile, thereby causing the test to begin. | 557 // Create the profile, thereby causing the test to begin. |
549 Profile* profile = CreateProfile( | 558 Profile* profile = CreateProfile( |
550 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); | 559 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
551 | 560 |
552 // Let all tasks run. | 561 // Let all tasks run. |
553 task_runner_->RunUntilIdle(); | 562 task_runner_->RunUntilIdle(); |
554 | 563 |
555 // Verify that report upload took place and contained the incident and | 564 // Verify that report upload took place and contained the incident and |
556 // environment data. | 565 // environment data. |
557 ExpectTestIncidentUploaded(1); | 566 ExpectTestIncidentUploaded(1); |
558 | 567 |
559 // Add the incident to the service again. | 568 // Add the incident to the service again. |
560 AddTestIncident(profile); | 569 AddTestIncident(profile); |
561 | 570 |
562 // Let all tasks run. | 571 // Let all tasks run. |
563 task_runner_->RunUntilIdle(); | 572 task_runner_->RunUntilIdle(); |
564 | 573 |
565 // Verify that no additional report upload took place. | 574 // Verify that no additional report upload took place. |
566 AssertNoUpload(); | 575 AssertNoUpload(); |
567 } | 576 } |
568 | 577 |
| 578 // Tests that two incidents of the same type with different payloads lead to two |
| 579 // uploads. |
| 580 TEST_F(IncidentReportingServiceTest, TwoIncidentsTwoUploads) { |
| 581 // Create the profile, thereby causing the test to begin. |
| 582 Profile* profile = CreateProfile( |
| 583 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
| 584 |
| 585 // Let all tasks run. |
| 586 task_runner_->RunUntilIdle(); |
| 587 |
| 588 // Verify that report upload took place and contained the incident and |
| 589 // environment data. |
| 590 ExpectTestIncidentUploaded(1); |
| 591 |
| 592 // Add a variation on the incident to the service. |
| 593 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
| 594 MakeTestIncident()); |
| 595 incident->mutable_tracked_preference()->set_atomic_value("leeches"); |
| 596 instance_->GetAddIncidentCallback(profile).Run(incident.Pass()); |
| 597 |
| 598 // Let all tasks run. |
| 599 task_runner_->RunUntilIdle(); |
| 600 |
| 601 // Verify that an additional report upload took place. |
| 602 ExpectTestIncidentUploaded(1); |
| 603 } |
| 604 |
569 // Tests that the same incident added for two different profiles in sequence | 605 // Tests that the same incident added for two different profiles in sequence |
570 // results in two uploads. | 606 // results in two uploads. |
571 TEST_F(IncidentReportingServiceTest, TwoProfilesTwoUploads) { | 607 TEST_F(IncidentReportingServiceTest, TwoProfilesTwoUploads) { |
572 // Create the profile, thereby causing the test to begin. | 608 // Create the profile, thereby causing the test to begin. |
573 CreateProfile( | 609 CreateProfile( |
574 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); | 610 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
575 | 611 |
576 // Let all tasks run. | 612 // Let all tasks run. |
577 task_runner_->RunUntilIdle(); | 613 task_runner_->RunUntilIdle(); |
578 | 614 |
(...skipping 27 matching lines...) Expand all Loading... |
606 task_runner_->RunUntilIdle(); | 642 task_runner_->RunUntilIdle(); |
607 | 643 |
608 // Verify that report upload took place and contained the incident and | 644 // Verify that report upload took place and contained the incident and |
609 // environment data. | 645 // environment data. |
610 ExpectTestIncidentUploaded(1); | 646 ExpectTestIncidentUploaded(1); |
611 | 647 |
612 // The lack of a crash indicates that the deleted profile was not accessed by | 648 // The lack of a crash indicates that the deleted profile was not accessed by |
613 // the service while handling the upload response. | 649 // the service while handling the upload response. |
614 } | 650 } |
615 | 651 |
| 652 // Tests that no upload takes place if the old pref was present. |
| 653 TEST_F(IncidentReportingServiceTest, MigrateOldPref) { |
| 654 Profile* profile = CreateProfile( |
| 655 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_NO_ACTION); |
| 656 |
| 657 // This is a legacy profile. |
| 658 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingIncidentReportSent, true); |
| 659 |
| 660 // Add the test incident. |
| 661 AddTestIncident(profile); |
| 662 |
| 663 // Let all tasks run. |
| 664 task_runner_->RunUntilIdle(); |
| 665 |
| 666 // No upload should have taken place. |
| 667 AssertNoUpload(); |
| 668 |
| 669 // The legacy pref should have been cleared. |
| 670 ASSERT_FALSE( |
| 671 profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingIncidentReportSent)); |
| 672 |
| 673 // Adding the same incident again should still result in no upload. |
| 674 AddTestIncident(profile); |
| 675 |
| 676 // Let all tasks run. |
| 677 task_runner_->RunUntilIdle(); |
| 678 |
| 679 // No upload should have taken place. |
| 680 AssertNoUpload(); |
| 681 } |
| 682 |
616 // Parallel uploads | 683 // Parallel uploads |
617 // Shutdown during processing | 684 // Shutdown during processing |
618 // environment colection taking longer than incident delay timer | 685 // environment colection taking longer than incident delay timer |
619 // environment colection taking longer than incident delay timer, and then | 686 // environment colection taking longer than incident delay timer, and then |
620 // another incident arriving | 687 // another incident arriving |
OLD | NEW |