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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 typedef base::Callback<scoped_ptr<safe_browsing::IncidentReportUploader>( | 49 typedef base::Callback<scoped_ptr<safe_browsing::IncidentReportUploader>( |
50 const safe_browsing::IncidentReportUploader::OnResultCallback&, | 50 const safe_browsing::IncidentReportUploader::OnResultCallback&, |
51 const safe_browsing::ClientIncidentReport& report)> StartUploadCallback; | 51 const safe_browsing::ClientIncidentReport& report)> StartUploadCallback; |
52 | 52 |
53 TestIncidentReportingService( | 53 TestIncidentReportingService( |
54 const scoped_refptr<base::TaskRunner>& task_runner, | 54 const scoped_refptr<base::TaskRunner>& task_runner, |
55 const PreProfileAddCallback& pre_profile_add_callback, | 55 const PreProfileAddCallback& pre_profile_add_callback, |
56 const CollectEnvironmentCallback& collect_environment_callback, | 56 const CollectEnvironmentCallback& collect_environment_callback, |
57 const CreateDownloadFinderCallback& create_download_finder_callback, | 57 const CreateDownloadFinderCallback& create_download_finder_callback, |
58 const StartUploadCallback& start_upload_callback) | 58 const StartUploadCallback& start_upload_callback) |
59 : IncidentReportingService(NULL, NULL), | 59 : IncidentReportingService(NULL, |
60 NULL, | |
61 base::TimeDelta::FromMilliseconds(5), | |
62 task_runner), | |
60 pre_profile_add_callback_(pre_profile_add_callback), | 63 pre_profile_add_callback_(pre_profile_add_callback), |
61 collect_environment_callback_(collect_environment_callback), | 64 collect_environment_callback_(collect_environment_callback), |
62 create_download_finder_callback_(create_download_finder_callback), | 65 create_download_finder_callback_(create_download_finder_callback), |
63 start_upload_callback_(start_upload_callback) { | 66 start_upload_callback_(start_upload_callback) { |
64 SetCollectEnvironmentHook(&CollectEnvironmentData, task_runner); | 67 SetCollectEnvironmentHook(&CollectEnvironmentData, task_runner); |
65 test_instance_.Get().Set(this); | 68 test_instance_.Get().Set(this); |
66 } | 69 } |
67 | 70 |
68 virtual ~TestIncidentReportingService() { test_instance_.Get().Set(NULL); } | 71 virtual ~TestIncidentReportingService() { test_instance_.Get().Set(NULL); } |
69 | 72 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 // service creates a LastDownloadFinder. | 128 // service creates a LastDownloadFinder. |
126 enum OnCreateDownloadFinderAction { | 129 enum OnCreateDownloadFinderAction { |
127 // Post a task that reports a download. | 130 // Post a task that reports a download. |
128 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND, | 131 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND, |
129 // Post a task that reports no downloads found. | 132 // Post a task that reports no downloads found. |
130 ON_CREATE_DOWNLOAD_FINDER_NO_DOWNLOADS, | 133 ON_CREATE_DOWNLOAD_FINDER_NO_DOWNLOADS, |
131 // Immediately return due to a lack of eligible profiles. | 134 // Immediately return due to a lack of eligible profiles. |
132 ON_CREATE_DOWNLOAD_FINDER_NO_PROFILES, | 135 ON_CREATE_DOWNLOAD_FINDER_NO_PROFILES, |
133 }; | 136 }; |
134 | 137 |
138 // A type for specifying the action to be taken by the test fixture when its | |
139 // delayed analysis callback is run. | |
140 enum OnDelayedAnalysisAction { | |
141 ON_DELAYED_ANALYSIS_NO_ACTION, | |
142 ON_DELAYED_ANALYSIS_ADD_INCIDENT, // Add an incident to the service. | |
143 }; | |
144 | |
135 static const int64 kIncidentTimeMsec; | 145 static const int64 kIncidentTimeMsec; |
136 static const char kFakeOsName[]; | 146 static const char kFakeOsName[]; |
137 static const char kFakeDownloadToken[]; | 147 static const char kFakeDownloadToken[]; |
138 static const char kTestTrackedPrefPath[]; | 148 static const char kTestTrackedPrefPath[]; |
139 | 149 |
140 IncidentReportingServiceTest() | 150 IncidentReportingServiceTest() |
141 : task_runner_(new base::TestSimpleTaskRunner), | 151 : task_runner_(new base::TestSimpleTaskRunner), |
142 thread_task_runner_handle_(task_runner_), | 152 thread_task_runner_handle_(task_runner_), |
143 profile_manager_(TestingBrowserProcess::GetGlobal()), | 153 profile_manager_(TestingBrowserProcess::GetGlobal()), |
144 instance_(new TestIncidentReportingService( | 154 instance_(new TestIncidentReportingService( |
145 task_runner_, | 155 task_runner_, |
146 base::Bind(&IncidentReportingServiceTest::PreProfileAdd, | 156 base::Bind(&IncidentReportingServiceTest::PreProfileAdd, |
147 base::Unretained(this)), | 157 base::Unretained(this)), |
148 base::Bind(&IncidentReportingServiceTest::CollectEnvironmentData, | 158 base::Bind(&IncidentReportingServiceTest::CollectEnvironmentData, |
149 base::Unretained(this)), | 159 base::Unretained(this)), |
150 base::Bind(&IncidentReportingServiceTest::CreateDownloadFinder, | 160 base::Bind(&IncidentReportingServiceTest::CreateDownloadFinder, |
151 base::Unretained(this)), | 161 base::Unretained(this)), |
152 base::Bind(&IncidentReportingServiceTest::StartUpload, | 162 base::Bind(&IncidentReportingServiceTest::StartUpload, |
153 base::Unretained(this)))), | 163 base::Unretained(this)))), |
154 on_create_download_finder_action_( | 164 on_create_download_finder_action_( |
155 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND), | 165 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND), |
166 on_delayed_analysis_action_(ON_DELAYED_ANALYSIS_NO_ACTION), | |
156 upload_result_(safe_browsing::IncidentReportUploader::UPLOAD_SUCCESS), | 167 upload_result_(safe_browsing::IncidentReportUploader::UPLOAD_SUCCESS), |
157 environment_collected_(), | 168 environment_collected_(), |
158 download_finder_created_(), | 169 download_finder_created_(), |
159 download_finder_destroyed_(), | 170 download_finder_destroyed_(), |
160 uploader_destroyed_() {} | 171 uploader_destroyed_(), |
172 delayed_analysis_ran_() {} | |
161 | 173 |
162 virtual void SetUp() OVERRIDE { | 174 virtual void SetUp() OVERRIDE { |
163 testing::Test::SetUp(); | 175 testing::Test::SetUp(); |
164 ASSERT_TRUE(profile_manager_.SetUp()); | 176 ASSERT_TRUE(profile_manager_.SetUp()); |
165 } | 177 } |
166 | 178 |
167 // Sets the action to be taken by the test fixture when the service creates a | 179 // Sets the action to be taken by the test fixture when the service creates a |
168 // LastDownloadFinder. | 180 // LastDownloadFinder. |
169 void SetCreateDownloadFinderAction(OnCreateDownloadFinderAction action) { | 181 void SetCreateDownloadFinderAction(OnCreateDownloadFinderAction action) { |
170 on_create_download_finder_action_ = action; | 182 on_create_download_finder_action_ = action; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 tp_incident = incident->mutable_tracked_preference(); | 229 tp_incident = incident->mutable_tracked_preference(); |
218 tp_incident->set_path(kTestTrackedPrefPath); | 230 tp_incident->set_path(kTestTrackedPrefPath); |
219 return incident.Pass(); | 231 return incident.Pass(); |
220 } | 232 } |
221 | 233 |
222 // Adds a test incident to the service. | 234 // Adds a test incident to the service. |
223 void AddTestIncident(Profile* profile) { | 235 void AddTestIncident(Profile* profile) { |
224 instance_->GetAddIncidentCallback(profile).Run(MakeTestIncident().Pass()); | 236 instance_->GetAddIncidentCallback(profile).Run(MakeTestIncident().Pass()); |
225 } | 237 } |
226 | 238 |
239 // Registers the callback to be run for delayed analysis. | |
240 void RegisterAnalysis(OnDelayedAnalysisAction on_delayed_analysis_action) { | |
241 on_delayed_analysis_action_ = on_delayed_analysis_action; | |
242 instance_->RegisterDelayedAnalysisCallback( | |
243 base::Bind(&IncidentReportingServiceTest::OnDelayedAnalysis, | |
244 base::Unretained(this))); | |
245 } | |
246 | |
227 // Confirms that the test incident(s) was/were uploaded by the service, then | 247 // Confirms that the test incident(s) was/were uploaded by the service, then |
228 // clears the instance for subsequent incidents. | 248 // clears the instance for subsequent incidents. |
229 void ExpectTestIncidentUploaded(int incident_count) { | 249 void ExpectTestIncidentUploaded(int incident_count) { |
230 ASSERT_TRUE(uploaded_report_); | 250 ASSERT_TRUE(uploaded_report_); |
231 ASSERT_EQ(incident_count, uploaded_report_->incident_size()); | 251 ASSERT_EQ(incident_count, uploaded_report_->incident_size()); |
232 for (int i = 0; i < incident_count; ++i) { | 252 for (int i = 0; i < incident_count; ++i) { |
233 ASSERT_TRUE(uploaded_report_->incident(i).has_incident_time_msec()); | 253 ASSERT_TRUE(uploaded_report_->incident(i).has_incident_time_msec()); |
234 ASSERT_EQ(kIncidentTimeMsec, | 254 ASSERT_EQ(kIncidentTimeMsec, |
235 uploaded_report_->incident(i).incident_time_msec()); | 255 uploaded_report_->incident(i).incident_time_msec()); |
236 ASSERT_TRUE(uploaded_report_->incident(i).has_tracked_preference()); | 256 ASSERT_TRUE(uploaded_report_->incident(i).has_tracked_preference()); |
(...skipping 12 matching lines...) Expand all Loading... | |
249 | 269 |
250 uploaded_report_.reset(); | 270 uploaded_report_.reset(); |
251 } | 271 } |
252 | 272 |
253 void AssertNoUpload() { ASSERT_FALSE(uploaded_report_); } | 273 void AssertNoUpload() { ASSERT_FALSE(uploaded_report_); } |
254 | 274 |
255 bool HasCollectedEnvironmentData() const { return environment_collected_; } | 275 bool HasCollectedEnvironmentData() const { return environment_collected_; } |
256 bool HasCreatedDownloadFinder() const { return download_finder_created_; } | 276 bool HasCreatedDownloadFinder() const { return download_finder_created_; } |
257 bool DownloadFinderDestroyed() const { return download_finder_destroyed_; } | 277 bool DownloadFinderDestroyed() const { return download_finder_destroyed_; } |
258 bool UploaderDestroyed() const { return uploader_destroyed_; } | 278 bool UploaderDestroyed() const { return uploader_destroyed_; } |
279 bool DelayedAnalysisRan() const { return delayed_analysis_ran_; } | |
259 | 280 |
260 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 281 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
261 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 282 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
262 TestingProfileManager profile_manager_; | 283 TestingProfileManager profile_manager_; |
263 scoped_ptr<safe_browsing::IncidentReportingService> instance_; | 284 scoped_ptr<safe_browsing::IncidentReportingService> instance_; |
264 base::Closure on_start_upload_callback_; | 285 base::Closure on_start_upload_callback_; |
265 OnCreateDownloadFinderAction on_create_download_finder_action_; | 286 OnCreateDownloadFinderAction on_create_download_finder_action_; |
287 OnDelayedAnalysisAction on_delayed_analysis_action_; | |
266 safe_browsing::IncidentReportUploader::Result upload_result_; | 288 safe_browsing::IncidentReportUploader::Result upload_result_; |
267 bool environment_collected_; | 289 bool environment_collected_; |
268 bool download_finder_created_; | 290 bool download_finder_created_; |
269 scoped_ptr<safe_browsing::ClientIncidentReport> uploaded_report_; | 291 scoped_ptr<safe_browsing::ClientIncidentReport> uploaded_report_; |
270 bool download_finder_destroyed_; | 292 bool download_finder_destroyed_; |
271 bool uploader_destroyed_; | 293 bool uploader_destroyed_; |
294 bool delayed_analysis_ran_; | |
272 | 295 |
273 private: | 296 private: |
274 // A fake IncidentReportUploader that posts a task to provide a given response | 297 // A fake IncidentReportUploader that posts a task to provide a given response |
275 // back to the incident reporting service. It also reports back to the test | 298 // back to the incident reporting service. It also reports back to the test |
276 // harness via a closure when it is deleted by the incident reporting service. | 299 // harness via a closure when it is deleted by the incident reporting service. |
277 class FakeUploader : public safe_browsing::IncidentReportUploader { | 300 class FakeUploader : public safe_browsing::IncidentReportUploader { |
278 public: | 301 public: |
279 FakeUploader( | 302 FakeUploader( |
280 const base::Closure& on_deleted, | 303 const base::Closure& on_deleted, |
281 const safe_browsing::IncidentReportUploader::OnResultCallback& callback, | 304 const safe_browsing::IncidentReportUploader::OnResultCallback& callback, |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
428 base::Bind( | 451 base::Bind( |
429 &IncidentReportingServiceTest::OnUploaderDestroyed, | 452 &IncidentReportingServiceTest::OnUploaderDestroyed, |
430 base::Unretained(this)), | 453 base::Unretained(this)), |
431 callback, | 454 callback, |
432 upload_result_)).Pass(); | 455 upload_result_)).Pass(); |
433 } | 456 } |
434 | 457 |
435 void OnDownloadFinderDestroyed() { download_finder_destroyed_ = true; } | 458 void OnDownloadFinderDestroyed() { download_finder_destroyed_ = true; } |
436 void OnUploaderDestroyed() { uploader_destroyed_ = true; } | 459 void OnUploaderDestroyed() { uploader_destroyed_ = true; } |
437 | 460 |
461 void OnDelayedAnalysis(const safe_browsing::AddIncidentCallback& callback) { | |
462 delayed_analysis_ran_ = true; | |
463 if (on_delayed_analysis_action_ == ON_DELAYED_ANALYSIS_ADD_INCIDENT) | |
464 callback.Run(MakeTestIncident().Pass()); | |
465 } | |
466 | |
438 // A mapping of profile name to its corresponding properties. | 467 // A mapping of profile name to its corresponding properties. |
439 std::map<std::string, ProfileProperties> profile_properties_; | 468 std::map<std::string, ProfileProperties> profile_properties_; |
440 }; | 469 }; |
441 | 470 |
442 // static | 471 // static |
443 base::LazyInstance<base::ThreadLocalPointer< | 472 base::LazyInstance<base::ThreadLocalPointer< |
444 IncidentReportingServiceTest::TestIncidentReportingService> >::Leaky | 473 IncidentReportingServiceTest::TestIncidentReportingService> >::Leaky |
445 IncidentReportingServiceTest::TestIncidentReportingService::test_instance_ = | 474 IncidentReportingServiceTest::TestIncidentReportingService::test_instance_ = |
446 LAZY_INSTANCE_INITIALIZER; | 475 LAZY_INSTANCE_INITIALIZER; |
447 | 476 |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
673 // Adding the same incident again should still result in no upload. | 702 // Adding the same incident again should still result in no upload. |
674 AddTestIncident(profile); | 703 AddTestIncident(profile); |
675 | 704 |
676 // Let all tasks run. | 705 // Let all tasks run. |
677 task_runner_->RunUntilIdle(); | 706 task_runner_->RunUntilIdle(); |
678 | 707 |
679 // No upload should have taken place. | 708 // No upload should have taken place. |
680 AssertNoUpload(); | 709 AssertNoUpload(); |
681 } | 710 } |
682 | 711 |
712 // Tests that no upload results from adding an incident that is not affiliated | |
713 // with a profile. | |
714 TEST_F(IncidentReportingServiceTest, ProcessWideNoProfileNoUpload) { | |
715 // Add the test incident. | |
716 AddTestIncident(NULL); | |
717 | |
718 // Let all tasks run. | |
719 task_runner_->RunUntilIdle(); | |
720 | |
721 // No upload should have taken place. | |
722 AssertNoUpload(); | |
723 } | |
724 | |
725 // Tests that there is an upload when a profile is present for a proc-wide | |
726 // incident and that pruning works. | |
727 TEST_F(IncidentReportingServiceTest, ProcessWideOneUpload) { | |
728 // Add a profile that participates in safe browsing. | |
729 CreateProfile( | |
730 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_NO_ACTION); | |
731 | |
732 // Add the test incident. | |
733 AddTestIncident(NULL); | |
734 | |
735 // Let all tasks run. | |
736 task_runner_->RunUntilIdle(); | |
737 | |
738 // An upload should have taken place. | |
739 ExpectTestIncidentUploaded(1); | |
740 | |
741 // Add the incident to the service again. | |
742 AddTestIncident(NULL); | |
743 | |
744 // Let all tasks run. | |
745 task_runner_->RunUntilIdle(); | |
746 | |
747 // Verify that no additional report upload took place. | |
748 AssertNoUpload(); | |
749 } | |
750 | |
751 // Tests that there is an upload when a profile appears after a proc-wide | |
752 // incident. | |
753 TEST_F(IncidentReportingServiceTest, ProcessWideOneUploadAfterProfile) { | |
754 // Add the test incident. | |
755 AddTestIncident(NULL); | |
756 | |
757 // Let all tasks run. | |
758 task_runner_->RunUntilIdle(); | |
759 | |
760 // Verify that no report upload took place. | |
761 AssertNoUpload(); | |
762 | |
763 // Add a profile that participates in safe browsing. | |
764 CreateProfile( | |
765 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_NO_ACTION); | |
766 | |
767 // Let all tasks run. | |
768 task_runner_->RunUntilIdle(); | |
769 | |
770 // An upload should have taken place. | |
771 ExpectTestIncidentUploaded(1); | |
772 } | |
773 | |
774 // Tests that delayed analysis callbacks are called after a profile | |
775 // participating in safe browsing is added. | |
robertshield
2014/08/06 18:27:07
perhaps add "but not before" to this comment to di
grt (UTC plus 2)
2014/08/07 01:46:14
Done.
| |
776 TEST_F(IncidentReportingServiceTest, AnalysisAfterProfile) { | |
777 // Register a callback. | |
778 RegisterAnalysis(ON_DELAYED_ANALYSIS_NO_ACTION); | |
779 | |
780 // Let all tasks run. | |
781 task_runner_->RunUntilIdle(); | |
782 | |
783 // Not run yet. | |
784 ASSERT_FALSE(DelayedAnalysisRan()); | |
785 | |
786 // Add a profile that participates in safe browsing. | |
787 CreateProfile( | |
788 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_NO_ACTION); | |
789 | |
790 // Let all tasks run. | |
791 task_runner_->RunUntilIdle(); | |
792 | |
793 // And now they have. | |
794 ASSERT_TRUE(DelayedAnalysisRan()); | |
795 } | |
796 | |
797 // Tests that delayed analysis callbacks are called after a profile | |
798 // participating in safe browsing is added. | |
799 TEST_F(IncidentReportingServiceTest, AnalysisWhenReigsteredWithProfile) { | |
mattm
2014/08/06 22:36:12
Reigstered
grt (UTC plus 2)
2014/08/07 01:46:14
Done.
| |
800 // Add a profile that participates in safe browsing. | |
801 CreateProfile( | |
802 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_NO_ACTION); | |
803 | |
804 // Register a callback. | |
805 RegisterAnalysis(ON_DELAYED_ANALYSIS_NO_ACTION); | |
806 | |
807 // Let all tasks run. | |
808 task_runner_->RunUntilIdle(); | |
809 | |
810 // Confirm that the callbacks were run. | |
811 ASSERT_TRUE(DelayedAnalysisRan()); | |
812 } | |
813 | |
814 // Tests that no upload results from a delayed analysis incident when no | |
815 // safe browsing profile is present. | |
816 TEST_F(IncidentReportingServiceTest, DelayedAnalysisNoProfileNoUpload) { | |
817 // Register a callback that will add an incident. | |
818 RegisterAnalysis(ON_DELAYED_ANALYSIS_ADD_INCIDENT); | |
819 | |
820 // Add a profile that does not participate in safe browsing. | |
821 CreateProfile( | |
822 "profile1", SAFE_BROWSING_OPT_OUT, ON_PROFILE_ADDITION_NO_ACTION); | |
823 | |
824 // Let all tasks run. | |
825 task_runner_->RunUntilIdle(); | |
826 | |
827 // The callback should not have been run. | |
828 ASSERT_FALSE(DelayedAnalysisRan()); | |
829 | |
830 // No upload should have taken place. | |
831 AssertNoUpload(); | |
832 } | |
833 | |
834 // Tests that there is an upload when a profile is present for a delayed | |
835 // analysis incident and that pruning works. | |
836 TEST_F(IncidentReportingServiceTest, DelayedAnalysisOneUpload) { | |
837 // Register a callback that will add an incident. | |
838 RegisterAnalysis(ON_DELAYED_ANALYSIS_ADD_INCIDENT); | |
839 | |
840 // Add a profile that participates in safe browsing. | |
841 CreateProfile( | |
842 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_NO_ACTION); | |
843 | |
844 // Let all tasks run. | |
845 task_runner_->RunUntilIdle(); | |
846 | |
847 // The callback should have been run. | |
848 ASSERT_TRUE(DelayedAnalysisRan()); | |
849 | |
850 // An upload should have taken place. | |
851 ExpectTestIncidentUploaded(1); | |
852 | |
853 // Add the incident to the service again. | |
854 AddTestIncident(NULL); | |
855 | |
856 // Let all tasks run. | |
857 task_runner_->RunUntilIdle(); | |
858 | |
859 // Verify that no additional report upload took place. | |
860 AssertNoUpload(); | |
861 } | |
862 | |
mattm
2014/08/06 22:36:12
The tests which add two incidents only test that t
grt (UTC plus 2)
2014/08/07 01:46:14
Sure. I'd thought that the prune test would cover
| |
683 // Parallel uploads | 863 // Parallel uploads |
684 // Shutdown during processing | 864 // Shutdown during processing |
685 // environment colection taking longer than incident delay timer | 865 // environment colection taking longer than incident delay timer |
686 // environment colection taking longer than incident delay timer, and then | 866 // environment colection taking longer than incident delay timer, and then |
687 // another incident arriving | 867 // another incident arriving |
OLD | NEW |