Chromium Code Reviews| 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" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/test/test_simple_task_runner.h" | 14 #include "base/test/test_simple_task_runner.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "base/threading/thread_local.h" | 16 #include "base/threading/thread_local.h" |
| 17 #include "chrome/browser/prefs/browser_prefs.h" | 17 #include "chrome/browser/prefs/browser_prefs.h" |
| 18 #include "chrome/browser/safe_browsing/incident_report_uploader.h" | 18 #include "chrome/browser/safe_browsing/incident_report_uploader.h" |
| 19 #include "chrome/browser/safe_browsing/last_download_finder.h" | |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/common/safe_browsing/csd.pb.h" | 21 #include "chrome/common/safe_browsing/csd.pb.h" |
| 21 #include "chrome/test/base/testing_browser_process.h" | 22 #include "chrome/test/base/testing_browser_process.h" |
| 22 #include "chrome/test/base/testing_pref_service_syncable.h" | 23 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 23 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 24 #include "chrome/test/base/testing_profile_manager.h" | 25 #include "chrome/test/base/testing_profile_manager.h" |
| 25 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 28 // A test fixture that sets up a test task runner and makes it the thread's | 29 // A test fixture that sets up a test task runner and makes it the thread's |
| 29 // runner. The fixture implements a fake envrionment data collector and a fake | 30 // runner. The fixture implements a fake envrionment data collector and a fake |
| 30 // report uploader. | 31 // report uploader. |
| 31 class IncidentReportingServiceTest : public testing::Test { | 32 class IncidentReportingServiceTest : public testing::Test { |
| 32 protected: | 33 protected: |
| 33 // An IRS class that allows a test harness to provide a fake environment | 34 // An IRS class that allows a test harness to provide a fake environment |
| 34 // collector and report uploader via callbacks. | 35 // collector and report uploader via callbacks. |
| 35 class TestIncidentReportingService | 36 class TestIncidentReportingService |
| 36 : public safe_browsing::IncidentReportingService { | 37 : public safe_browsing::IncidentReportingService { |
| 37 public: | 38 public: |
| 38 typedef base::Callback<void(Profile*)> PreProfileCreateCallback; | 39 typedef base::Callback<void(Profile*)> PreProfileAddCallback; |
| 39 | 40 |
| 40 typedef base::Callback< | 41 typedef base::Callback< |
| 41 void(safe_browsing::ClientIncidentReport_EnvironmentData*)> | 42 void(safe_browsing::ClientIncidentReport_EnvironmentData*)> |
| 42 CollectEnvironmentCallback; | 43 CollectEnvironmentCallback; |
| 43 | 44 |
| 45 typedef base::Callback<scoped_ptr<safe_browsing::LastDownloadFinder>( | |
| 46 const safe_browsing::LastDownloadFinder::LastDownloadCallback& | |
| 47 callback)> CreateDownloadFinderCallback; | |
| 48 | |
| 44 typedef base::Callback<scoped_ptr<safe_browsing::IncidentReportUploader>( | 49 typedef base::Callback<scoped_ptr<safe_browsing::IncidentReportUploader>( |
| 45 const safe_browsing::IncidentReportUploader::OnResultCallback&, | 50 const safe_browsing::IncidentReportUploader::OnResultCallback&, |
| 46 const safe_browsing::ClientIncidentReport& report)> StartUploadCallback; | 51 const safe_browsing::ClientIncidentReport& report)> StartUploadCallback; |
| 47 | 52 |
| 48 TestIncidentReportingService( | 53 TestIncidentReportingService( |
| 49 const scoped_refptr<base::TaskRunner>& task_runner, | 54 const scoped_refptr<base::TaskRunner>& task_runner, |
| 50 const PreProfileCreateCallback& pre_profile_create_callback, | 55 const PreProfileAddCallback& pre_profile_add_callback, |
| 51 const CollectEnvironmentCallback& collect_environment_callback, | 56 const CollectEnvironmentCallback& collect_environment_callback, |
| 57 const CreateDownloadFinderCallback& create_download_finder_callback, | |
| 52 const StartUploadCallback& start_upload_callback) | 58 const StartUploadCallback& start_upload_callback) |
| 53 : IncidentReportingService(NULL, NULL), | 59 : IncidentReportingService(NULL, NULL), |
| 54 pre_profile_create_callback_(pre_profile_create_callback), | 60 pre_profile_add_callback_(pre_profile_add_callback), |
| 55 collect_environment_callback_(collect_environment_callback), | 61 collect_environment_callback_(collect_environment_callback), |
| 62 create_download_finder_callback_(create_download_finder_callback), | |
| 56 start_upload_callback_(start_upload_callback) { | 63 start_upload_callback_(start_upload_callback) { |
| 57 SetCollectEnvironmentHook(&CollectEnvironmentData, task_runner); | 64 SetCollectEnvironmentHook(&CollectEnvironmentData, task_runner); |
| 58 test_instance_.Get().Set(this); | 65 test_instance_.Get().Set(this); |
| 59 } | 66 } |
| 60 | 67 |
| 61 virtual ~TestIncidentReportingService() { test_instance_.Get().Set(NULL); } | 68 virtual ~TestIncidentReportingService() { test_instance_.Get().Set(NULL); } |
| 62 | 69 |
| 63 protected: | 70 protected: |
| 64 virtual void OnProfileCreated(Profile* profile) OVERRIDE { | 71 virtual void OnProfileAdded(Profile* profile) OVERRIDE { |
| 65 pre_profile_create_callback_.Run(profile); | 72 pre_profile_add_callback_.Run(profile); |
| 66 safe_browsing::IncidentReportingService::OnProfileCreated(profile); | 73 safe_browsing::IncidentReportingService::OnProfileAdded(profile); |
| 74 } | |
| 75 | |
| 76 virtual scoped_ptr<safe_browsing::LastDownloadFinder> CreateDownloadFinder( | |
| 77 const safe_browsing::LastDownloadFinder::LastDownloadCallback& callback) | |
| 78 OVERRIDE { | |
| 79 return create_download_finder_callback_.Run(callback); | |
| 67 } | 80 } |
| 68 | 81 |
| 69 virtual scoped_ptr<safe_browsing::IncidentReportUploader> StartReportUpload( | 82 virtual scoped_ptr<safe_browsing::IncidentReportUploader> StartReportUpload( |
| 70 const safe_browsing::IncidentReportUploader::OnResultCallback& callback, | 83 const safe_browsing::IncidentReportUploader::OnResultCallback& callback, |
| 71 const scoped_refptr<net::URLRequestContextGetter>& | 84 const scoped_refptr<net::URLRequestContextGetter>& |
| 72 request_context_getter, | 85 request_context_getter, |
| 73 const safe_browsing::ClientIncidentReport& report) OVERRIDE { | 86 const safe_browsing::ClientIncidentReport& report) OVERRIDE { |
| 74 return start_upload_callback_.Run(callback, report); | 87 return start_upload_callback_.Run(callback, report); |
| 75 } | 88 } |
| 76 | 89 |
| 77 private: | 90 private: |
| 78 static TestIncidentReportingService& current() { | 91 static TestIncidentReportingService& current() { |
| 79 return *test_instance_.Get().Get(); | 92 return *test_instance_.Get().Get(); |
| 80 } | 93 } |
| 81 | 94 |
| 82 static void CollectEnvironmentData( | 95 static void CollectEnvironmentData( |
| 83 safe_browsing::ClientIncidentReport_EnvironmentData* data) { | 96 safe_browsing::ClientIncidentReport_EnvironmentData* data) { |
| 84 current().collect_environment_callback_.Run(data); | 97 current().collect_environment_callback_.Run(data); |
| 85 }; | 98 }; |
| 86 | 99 |
| 87 static base::LazyInstance<base::ThreadLocalPointer< | 100 static base::LazyInstance<base::ThreadLocalPointer< |
| 88 TestIncidentReportingService> >::Leaky test_instance_; | 101 TestIncidentReportingService> >::Leaky test_instance_; |
| 89 | 102 |
| 90 PreProfileCreateCallback pre_profile_create_callback_; | 103 PreProfileAddCallback pre_profile_add_callback_; |
| 91 CollectEnvironmentCallback collect_environment_callback_; | 104 CollectEnvironmentCallback collect_environment_callback_; |
| 105 CreateDownloadFinderCallback create_download_finder_callback_; | |
| 92 StartUploadCallback start_upload_callback_; | 106 StartUploadCallback start_upload_callback_; |
| 93 }; | 107 }; |
| 94 | 108 |
| 95 // A type for specifying whether or not a profile created by CreateProfile | 109 // A type for specifying whether or not a profile created by CreateProfile |
| 96 // participates in safe browsing. | 110 // participates in safe browsing. |
| 97 enum SafeBrowsingDisposition { | 111 enum SafeBrowsingDisposition { |
| 98 SAFE_BROWSING_OPT_OUT, | 112 SAFE_BROWSING_OPT_OUT, |
| 99 SAFE_BROWSING_OPT_IN, | 113 SAFE_BROWSING_OPT_IN, |
| 100 }; | 114 }; |
| 101 | 115 |
| 102 // 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 |
| 103 // profile initialization (before NOTIFICATION_PROFILE_CREATED is sent). | 117 // profile initialization (before NOTIFICATION_PROFILE_ADDED is sent). |
| 104 enum OnProfileCreationAction { | 118 enum OnProfileAdditionAction { |
| 105 ON_PROFILE_CREATION_NO_ACTION, | 119 ON_PROFILE_ADDITION_NO_ACTION, |
| 106 ON_PROFILE_CREATION_ADD_INCIDENT, // Add an incident to the service. | 120 ON_PROFILE_ADDITION_ADD_INCIDENT, // Add an incident to the service. |
| 121 }; | |
| 122 | |
| 123 // A type for specifying the action to be taken by the test fixture when the | |
| 124 // service creates a LastDownloadFinder. | |
| 125 enum OnCreateDownloadFinderAction { | |
| 126 // Post a task that reports a download. | |
| 127 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND, | |
| 128 // Post a task that reports no downloads found. | |
| 129 ON_CREATE_DOWNLOAD_FINDER_NO_DOWNLOADS, | |
| 130 // Immediately return due to a lack of eligible profiles. | |
| 131 ON_CREATE_DOWNLOAD_FINDER_NO_PROFILES, | |
| 107 }; | 132 }; |
| 108 | 133 |
| 109 static const int64 kIncidentTimeMsec; | 134 static const int64 kIncidentTimeMsec; |
| 110 static const char kFakeOsName[]; | 135 static const char kFakeOsName[]; |
| 136 static const char kFakeDownloadToken[]; | |
| 111 | 137 |
| 112 IncidentReportingServiceTest() | 138 IncidentReportingServiceTest() |
| 113 : task_runner_(new base::TestSimpleTaskRunner), | 139 : task_runner_(new base::TestSimpleTaskRunner), |
| 114 thread_task_runner_handle_(task_runner_), | 140 thread_task_runner_handle_(task_runner_), |
| 115 profile_manager_(TestingBrowserProcess::GetGlobal()), | 141 profile_manager_(TestingBrowserProcess::GetGlobal()), |
| 116 instance_(new TestIncidentReportingService( | 142 instance_(new TestIncidentReportingService( |
| 117 task_runner_, | 143 task_runner_, |
| 118 base::Bind(&IncidentReportingServiceTest::PreProfileCreate, | 144 base::Bind(&IncidentReportingServiceTest::PreProfileAdd, |
| 119 base::Unretained(this)), | 145 base::Unretained(this)), |
| 120 base::Bind(&IncidentReportingServiceTest::CollectEnvironmentData, | 146 base::Bind(&IncidentReportingServiceTest::CollectEnvironmentData, |
| 121 base::Unretained(this)), | 147 base::Unretained(this)), |
| 148 base::Bind(&IncidentReportingServiceTest::CreateDownloadFinder, | |
| 149 base::Unretained(this)), | |
| 122 base::Bind(&IncidentReportingServiceTest::StartUpload, | 150 base::Bind(&IncidentReportingServiceTest::StartUpload, |
| 123 base::Unretained(this)))), | 151 base::Unretained(this)))), |
| 152 on_create_download_finder_action_( | |
| 153 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND), | |
| 124 upload_result_(safe_browsing::IncidentReportUploader::UPLOAD_SUCCESS), | 154 upload_result_(safe_browsing::IncidentReportUploader::UPLOAD_SUCCESS), |
| 125 environment_collected_(), | 155 environment_collected_(), |
| 156 download_finder_created_(), | |
| 157 download_finder_destroyed_(), | |
| 126 uploader_destroyed_() {} | 158 uploader_destroyed_() {} |
| 127 | 159 |
| 128 virtual void SetUp() OVERRIDE { | 160 virtual void SetUp() OVERRIDE { |
| 129 testing::Test::SetUp(); | 161 testing::Test::SetUp(); |
| 130 ASSERT_TRUE(profile_manager_.SetUp()); | 162 ASSERT_TRUE(profile_manager_.SetUp()); |
| 131 } | 163 } |
| 132 | 164 |
| 165 // Sets the action to be taken by the test fixture when the service creates a | |
| 166 // LastDownloadFinder. | |
| 167 void SetCreateDownloadFinderAction(OnCreateDownloadFinderAction action) { | |
| 168 on_create_download_finder_action_ = action; | |
| 169 } | |
| 170 | |
| 133 // Creates and returns a profile (owned by the profile manager) with or | 171 // Creates and returns a profile (owned by the profile manager) with or |
| 134 // without safe browsing enabled. An incident will be created within | 172 // without safe browsing enabled. An incident will be created within |
| 135 // PreProfileCreate if requested. | 173 // PreProfileAdd if requested. |
| 136 TestingProfile* CreateProfile(const std::string& profile_name, | 174 TestingProfile* CreateProfile(const std::string& profile_name, |
| 137 SafeBrowsingDisposition safe_browsing_opt_in, | 175 SafeBrowsingDisposition safe_browsing_opt_in, |
| 138 OnProfileCreationAction on_creation_action) { | 176 OnProfileAdditionAction on_addition_action) { |
| 139 // Create prefs for the profile with safe browsing enabled or not. | 177 // Create prefs for the profile with safe browsing enabled or not. |
| 140 scoped_ptr<TestingPrefServiceSyncable> prefs( | 178 scoped_ptr<TestingPrefServiceSyncable> prefs( |
| 141 new TestingPrefServiceSyncable); | 179 new TestingPrefServiceSyncable); |
| 142 chrome::RegisterUserProfilePrefs(prefs->registry()); | 180 chrome::RegisterUserProfilePrefs(prefs->registry()); |
| 143 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, | 181 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, |
| 144 safe_browsing_opt_in == SAFE_BROWSING_OPT_IN); | 182 safe_browsing_opt_in == SAFE_BROWSING_OPT_IN); |
| 145 | 183 |
| 146 // Remember whether or not to create an incident. | 184 // Remember whether or not to create an incident. |
| 147 profile_properties_[profile_name].on_creation_action = on_creation_action; | 185 profile_properties_[profile_name].on_addition_action = on_addition_action; |
| 148 | 186 |
| 149 // Boom (or fizzle). | 187 // Boom (or fizzle). |
| 150 return profile_manager_.CreateTestingProfile( | 188 return profile_manager_.CreateTestingProfile( |
| 151 profile_name, | 189 profile_name, |
| 152 prefs.PassAs<PrefServiceSyncable>(), | 190 prefs.PassAs<PrefServiceSyncable>(), |
| 153 base::ASCIIToUTF16(profile_name), | 191 base::ASCIIToUTF16(profile_name), |
| 154 0, // avatar_id (unused) | 192 0, // avatar_id (unused) |
| 155 std::string(), // supervised_user_id (unused) | 193 std::string(), // supervised_user_id (unused) |
| 156 TestingProfile::TestingFactories()); | 194 TestingProfile::TestingFactories()); |
| 157 } | 195 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 188 ASSERT_TRUE(uploaded_report_); | 226 ASSERT_TRUE(uploaded_report_); |
| 189 ASSERT_EQ(1, uploaded_report_->incident_size()); | 227 ASSERT_EQ(1, uploaded_report_->incident_size()); |
| 190 ASSERT_TRUE(uploaded_report_->incident(0).has_incident_time_msec()); | 228 ASSERT_TRUE(uploaded_report_->incident(0).has_incident_time_msec()); |
| 191 ASSERT_EQ(kIncidentTimeMsec, | 229 ASSERT_EQ(kIncidentTimeMsec, |
| 192 uploaded_report_->incident(0).incident_time_msec()); | 230 uploaded_report_->incident(0).incident_time_msec()); |
| 193 ASSERT_TRUE(uploaded_report_->has_environment()); | 231 ASSERT_TRUE(uploaded_report_->has_environment()); |
| 194 ASSERT_TRUE(uploaded_report_->environment().has_os()); | 232 ASSERT_TRUE(uploaded_report_->environment().has_os()); |
| 195 ASSERT_TRUE(uploaded_report_->environment().os().has_os_name()); | 233 ASSERT_TRUE(uploaded_report_->environment().os().has_os_name()); |
| 196 ASSERT_EQ(std::string(kFakeOsName), | 234 ASSERT_EQ(std::string(kFakeOsName), |
| 197 uploaded_report_->environment().os().os_name()); | 235 uploaded_report_->environment().os().os_name()); |
| 236 ASSERT_EQ(std::string(kFakeDownloadToken), | |
| 237 uploaded_report_->download().token()); | |
| 198 | 238 |
| 199 uploaded_report_.reset(); | 239 uploaded_report_.reset(); |
| 200 } | 240 } |
| 201 | 241 |
| 202 void ExpectNoUpload() { ASSERT_FALSE(uploaded_report_); } | 242 void AssertNoUpload() { ASSERT_FALSE(uploaded_report_); } |
| 203 | 243 |
| 204 bool HasCollectedEnvironmentData() const { return environment_collected_; } | 244 bool HasCollectedEnvironmentData() const { return environment_collected_; } |
| 245 bool HasCreatedDownloadFinder() const { return download_finder_created_; } | |
| 246 bool DownloadFinderDestroyed() const { return download_finder_destroyed_; } | |
| 205 bool UploaderDestroyed() const { return uploader_destroyed_; } | 247 bool UploaderDestroyed() const { return uploader_destroyed_; } |
| 206 | 248 |
| 207 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 249 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 208 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 250 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
| 209 TestingProfileManager profile_manager_; | 251 TestingProfileManager profile_manager_; |
| 210 scoped_ptr<safe_browsing::IncidentReportingService> instance_; | 252 scoped_ptr<safe_browsing::IncidentReportingService> instance_; |
| 211 base::Closure on_start_upload_callback_; | 253 base::Closure on_start_upload_callback_; |
| 254 OnCreateDownloadFinderAction on_create_download_finder_action_; | |
| 212 safe_browsing::IncidentReportUploader::Result upload_result_; | 255 safe_browsing::IncidentReportUploader::Result upload_result_; |
| 213 bool environment_collected_; | 256 bool environment_collected_; |
| 257 bool download_finder_created_; | |
| 214 scoped_ptr<safe_browsing::ClientIncidentReport> uploaded_report_; | 258 scoped_ptr<safe_browsing::ClientIncidentReport> uploaded_report_; |
| 259 bool download_finder_destroyed_; | |
| 215 bool uploader_destroyed_; | 260 bool uploader_destroyed_; |
| 216 | 261 |
| 217 private: | 262 private: |
| 218 // A fake IncidentReportUploader that posts a task to provide a given response | 263 // A fake IncidentReportUploader that posts a task to provide a given response |
| 219 // back to the incident reporting service. It also reports back to the test | 264 // back to the incident reporting service. It also reports back to the test |
| 220 // harness via a closure when it is deleted by the incident reporting service. | 265 // harness via a closure when it is deleted by the incident reporting service. |
| 221 class FakeUploader : public safe_browsing::IncidentReportUploader { | 266 class FakeUploader : public safe_browsing::IncidentReportUploader { |
| 222 public: | 267 public: |
| 223 FakeUploader( | 268 FakeUploader( |
| 224 const base::Closure& on_deleted, | 269 const base::Closure& on_deleted, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 241 callback_.Run(result_, | 286 callback_.Run(result_, |
| 242 scoped_ptr<safe_browsing::ClientIncidentResponse>()); | 287 scoped_ptr<safe_browsing::ClientIncidentResponse>()); |
| 243 } | 288 } |
| 244 | 289 |
| 245 base::Closure on_deleted_; | 290 base::Closure on_deleted_; |
| 246 safe_browsing::IncidentReportUploader::Result result_; | 291 safe_browsing::IncidentReportUploader::Result result_; |
| 247 | 292 |
| 248 DISALLOW_COPY_AND_ASSIGN(FakeUploader); | 293 DISALLOW_COPY_AND_ASSIGN(FakeUploader); |
| 249 }; | 294 }; |
| 250 | 295 |
| 296 class FakeDownloadFinder : public safe_browsing::LastDownloadFinder { | |
| 297 public: | |
| 298 static scoped_ptr<safe_browsing::LastDownloadFinder> Create( | |
| 299 const base::Closure& on_deleted, | |
| 300 scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails> | |
| 301 download, | |
| 302 const safe_browsing::LastDownloadFinder::LastDownloadCallback& | |
| 303 callback) { | |
| 304 // Post a task to run the callback. | |
| 305 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 306 FROM_HERE, base::Bind(callback, base::Passed(&download))); | |
| 307 return scoped_ptr<safe_browsing::LastDownloadFinder>( | |
| 308 new FakeDownloadFinder(on_deleted)); | |
| 309 } | |
| 310 | |
| 311 virtual ~FakeDownloadFinder() { on_deleted_.Run(); } | |
| 312 | |
| 313 private: | |
| 314 explicit FakeDownloadFinder(const base::Closure& on_deleted) | |
| 315 : on_deleted_(on_deleted) {} | |
| 316 | |
| 317 base::Closure on_deleted_; | |
| 318 | |
| 319 DISALLOW_COPY_AND_ASSIGN(FakeDownloadFinder); | |
| 320 }; | |
| 321 | |
| 251 // Properties for a profile that impact the behavior of the test. | 322 // Properties for a profile that impact the behavior of the test. |
| 252 struct ProfileProperties { | 323 struct ProfileProperties { |
| 253 ProfileProperties() : on_creation_action(ON_PROFILE_CREATION_NO_ACTION) {} | 324 ProfileProperties() : on_addition_action(ON_PROFILE_ADDITION_NO_ACTION) {} |
| 254 | 325 |
| 255 // The action taken by the test fixture during profile initialization | 326 // The action taken by the test fixture during profile initialization |
| 256 // (before NOTIFICATION_PROFILE_CREATED is sent). | 327 // (before NOTIFICATION_PROFILE_ADDED is sent). |
| 257 OnProfileCreationAction on_creation_action; | 328 OnProfileAdditionAction on_addition_action; |
| 258 }; | 329 }; |
| 259 | 330 |
| 260 // Returns the name of a profile as provided to CreateProfile. | 331 // Returns the name of a profile as provided to CreateProfile. |
| 261 static std::string GetProfileName(Profile* profile) { | 332 static std::string GetProfileName(Profile* profile) { |
| 262 // Cannot reliably use profile->GetProfileName() since the test needs the | 333 // Cannot reliably use profile->GetProfileName() since the test needs the |
| 263 // name before the profile manager sets it (which happens after profile | 334 // name before the profile manager sets it (which happens after profile |
| 264 // creation). | 335 // addition). |
| 265 return profile->GetPath().BaseName().AsUTF8Unsafe(); | 336 return profile->GetPath().BaseName().AsUTF8Unsafe(); |
| 266 } | 337 } |
| 267 | 338 |
| 268 // Posts a task to delete the profile. | 339 // Posts a task to delete the profile. |
| 269 void DelayedDeleteProfile(Profile* profile) { | 340 void DelayedDeleteProfile(Profile* profile) { |
| 270 base::ThreadTaskRunnerHandle::Get()->PostTask( | 341 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 271 FROM_HERE, | 342 FROM_HERE, |
| 272 base::Bind(&TestingProfileManager::DeleteTestingProfile, | 343 base::Bind(&TestingProfileManager::DeleteTestingProfile, |
| 273 base::Unretained(&profile_manager_), | 344 base::Unretained(&profile_manager_), |
| 274 GetProfileName(profile))); | 345 GetProfileName(profile))); |
| 275 } | 346 } |
| 276 | 347 |
| 277 // A callback run by the test fixture when a profile is created. An incident | 348 // A callback run by the test fixture when a profile is added. An incident |
| 278 // is added. | 349 // is added. |
| 279 void PreProfileCreate(Profile* profile) { | 350 void PreProfileAdd(Profile* profile) { |
| 280 // The instance must have already been created. | 351 // The instance must have already been created. |
| 281 ASSERT_TRUE(instance_); | 352 ASSERT_TRUE(instance_); |
| 282 // Add a test incident to the service if requested. | 353 // Add a test incident to the service if requested. |
| 283 switch (profile_properties_[GetProfileName(profile)].on_creation_action) { | 354 switch (profile_properties_[GetProfileName(profile)].on_addition_action) { |
| 284 case ON_PROFILE_CREATION_ADD_INCIDENT: | 355 case ON_PROFILE_ADDITION_ADD_INCIDENT: |
| 285 AddTestIncident(profile); | 356 AddTestIncident(profile); |
| 286 break; | 357 break; |
| 287 default: | 358 default: |
| 288 ASSERT_EQ( | 359 ASSERT_EQ( |
| 289 ON_PROFILE_CREATION_NO_ACTION, | 360 ON_PROFILE_ADDITION_NO_ACTION, |
| 290 profile_properties_[GetProfileName(profile)].on_creation_action); | 361 profile_properties_[GetProfileName(profile)].on_addition_action); |
| 291 break; | 362 break; |
| 292 } | 363 } |
| 293 } | 364 } |
| 294 | 365 |
| 295 // A fake CollectEnvironmentData implementation invoked by the service during | 366 // A fake CollectEnvironmentData implementation invoked by the service during |
| 296 // operation. | 367 // operation. |
| 297 void CollectEnvironmentData( | 368 void CollectEnvironmentData( |
| 298 safe_browsing::ClientIncidentReport_EnvironmentData* data) { | 369 safe_browsing::ClientIncidentReport_EnvironmentData* data) { |
| 299 ASSERT_NE( | 370 ASSERT_NE( |
| 300 static_cast<safe_browsing::ClientIncidentReport_EnvironmentData*>(NULL), | 371 static_cast<safe_browsing::ClientIncidentReport_EnvironmentData*>(NULL), |
| 301 data); | 372 data); |
| 302 data->mutable_os()->set_os_name(kFakeOsName); | 373 data->mutable_os()->set_os_name(kFakeOsName); |
| 303 environment_collected_ = true; | 374 environment_collected_ = true; |
| 304 } | 375 } |
| 305 | 376 |
| 377 // A fake CreateDownloadFinder implementation invoked by the service during | |
| 378 // operation. | |
| 379 scoped_ptr<safe_browsing::LastDownloadFinder> CreateDownloadFinder( | |
| 380 const safe_browsing::LastDownloadFinder::LastDownloadCallback& callback) { | |
| 381 download_finder_created_ = true; | |
| 382 scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails> download; | |
| 383 if (on_create_download_finder_action_ == | |
| 384 ON_CREATE_DOWNLOAD_FINDER_NO_PROFILES) { | |
| 385 return scoped_ptr<safe_browsing::LastDownloadFinder>(); | |
| 386 } | |
| 387 if (on_create_download_finder_action_ == | |
| 388 ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND) { | |
| 389 download.reset(new safe_browsing::ClientIncidentReport_DownloadDetails); | |
| 390 download->set_token(kFakeDownloadToken); | |
| 391 } | |
| 392 return scoped_ptr<safe_browsing::LastDownloadFinder>( | |
| 393 FakeDownloadFinder::Create( | |
| 394 base::Bind(&IncidentReportingServiceTest::OnDownloadFinderDestroyed, | |
| 395 base::Unretained(this)), | |
| 396 download.Pass(), | |
| 397 callback)); | |
| 398 } | |
| 399 | |
| 306 // A fake StartUpload implementation invoked by the service during operation. | 400 // A fake StartUpload implementation invoked by the service during operation. |
| 307 scoped_ptr<safe_browsing::IncidentReportUploader> StartUpload( | 401 scoped_ptr<safe_browsing::IncidentReportUploader> StartUpload( |
| 308 const safe_browsing::IncidentReportUploader::OnResultCallback& callback, | 402 const safe_browsing::IncidentReportUploader::OnResultCallback& callback, |
| 309 const safe_browsing::ClientIncidentReport& report) { | 403 const safe_browsing::ClientIncidentReport& report) { |
| 310 // Remember the report that is being uploaded. | 404 // Remember the report that is being uploaded. |
| 311 uploaded_report_.reset(new safe_browsing::ClientIncidentReport(report)); | 405 uploaded_report_.reset(new safe_browsing::ClientIncidentReport(report)); |
| 312 // Run and clear the OnStartUpload callback, if provided. | 406 // Run and clear the OnStartUpload callback, if provided. |
| 313 if (!on_start_upload_callback_.is_null()) { | 407 if (!on_start_upload_callback_.is_null()) { |
| 314 on_start_upload_callback_.Run(); | 408 on_start_upload_callback_.Run(); |
| 315 on_start_upload_callback_ = base::Closure(); | 409 on_start_upload_callback_ = base::Closure(); |
| 316 } | 410 } |
| 317 return scoped_ptr<safe_browsing::IncidentReportUploader>(new FakeUploader( | 411 return scoped_ptr<safe_browsing::IncidentReportUploader>( |
| 318 base::Bind(&IncidentReportingServiceTest::OnUploaderDestroyed, | 412 new FakeUploader( |
| 319 base::Unretained(this)), | 413 base::Bind( |
| 320 callback, | 414 &IncidentReportingServiceTest::OnUploaderDestroyed, |
| 321 upload_result_)); | 415 base::Unretained(this)), |
| 416 callback, | |
| 417 upload_result_)).Pass(); | |
| 322 } | 418 } |
| 323 | 419 |
| 420 void OnDownloadFinderDestroyed() { download_finder_destroyed_ = true; } | |
| 324 void OnUploaderDestroyed() { uploader_destroyed_ = true; } | 421 void OnUploaderDestroyed() { uploader_destroyed_ = true; } |
| 325 | 422 |
| 326 // A mapping of profile name to its corresponding properties. | 423 // A mapping of profile name to its corresponding properties. |
| 327 std::map<std::string, ProfileProperties> profile_properties_; | 424 std::map<std::string, ProfileProperties> profile_properties_; |
| 328 }; | 425 }; |
| 329 | 426 |
| 330 // static | 427 // static |
| 331 base::LazyInstance<base::ThreadLocalPointer< | 428 base::LazyInstance<base::ThreadLocalPointer< |
| 332 IncidentReportingServiceTest::TestIncidentReportingService> >::Leaky | 429 IncidentReportingServiceTest::TestIncidentReportingService> >::Leaky |
| 333 IncidentReportingServiceTest::TestIncidentReportingService::test_instance_ = | 430 IncidentReportingServiceTest::TestIncidentReportingService::test_instance_ = |
| 334 LAZY_INSTANCE_INITIALIZER; | 431 LAZY_INSTANCE_INITIALIZER; |
| 335 | 432 |
| 336 const int64 IncidentReportingServiceTest::kIncidentTimeMsec = 47LL; | 433 const int64 IncidentReportingServiceTest::kIncidentTimeMsec = 47LL; |
| 337 const char IncidentReportingServiceTest::kFakeOsName[] = "fakedows"; | 434 const char IncidentReportingServiceTest::kFakeOsName[] = "fakedows"; |
| 435 const char IncidentReportingServiceTest::kFakeDownloadToken[] = "fakedlt"; | |
| 338 | 436 |
| 339 // Tests that an incident added during profile initialization when safe browsing | 437 // Tests that an incident added during profile initialization when safe browsing |
| 340 // is on is uploaded. | 438 // is on is uploaded. |
| 341 TEST_F(IncidentReportingServiceTest, AddIncident) { | 439 TEST_F(IncidentReportingServiceTest, AddIncident) { |
| 342 CreateProfile( | 440 CreateProfile( |
| 343 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_CREATION_ADD_INCIDENT); | 441 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
| 344 | 442 |
| 345 // Let all tasks run. | 443 // Let all tasks run. |
| 346 task_runner_->RunUntilIdle(); | 444 task_runner_->RunUntilIdle(); |
| 347 | 445 |
| 348 // Verify that environment collection took place. | 446 // Verify that environment collection took place. |
| 349 EXPECT_TRUE(HasCollectedEnvironmentData()); | 447 EXPECT_TRUE(HasCollectedEnvironmentData()); |
| 350 | 448 |
| 351 // Verify that report upload took place and contained the incident and | 449 // Verify that the most recent download was looked for. |
| 352 // environment data. | 450 EXPECT_TRUE(HasCreatedDownloadFinder()); |
| 451 | |
| 452 // Verify that report upload took place and contained the incident, | |
| 453 // environment data, and download details. | |
| 353 ExpectTestIncidentUploaded(); | 454 ExpectTestIncidentUploaded(); |
| 354 | 455 |
| 355 // Verify that the uploader was destroyed. | 456 // Verify that the download finder and the uploader were destroyed. |
| 457 ASSERT_TRUE(DownloadFinderDestroyed()); | |
| 356 ASSERT_TRUE(UploaderDestroyed()); | 458 ASSERT_TRUE(UploaderDestroyed()); |
| 357 } | 459 } |
| 358 | 460 |
| 359 // Tests that an incident added during profile initialization when safe browsing | 461 // Tests that an incident added during profile initialization when safe browsing |
| 360 // is off is not uploaded. | 462 // is off is not uploaded. |
| 361 TEST_F(IncidentReportingServiceTest, NoSafeBrowsing) { | 463 TEST_F(IncidentReportingServiceTest, NoSafeBrowsing) { |
| 362 // Create the profile, thereby causing the test to begin. | 464 // Create the profile, thereby causing the test to begin. |
| 363 CreateProfile( | 465 CreateProfile( |
| 364 "profile1", SAFE_BROWSING_OPT_OUT, ON_PROFILE_CREATION_ADD_INCIDENT); | 466 "profile1", SAFE_BROWSING_OPT_OUT, ON_PROFILE_ADDITION_ADD_INCIDENT); |
| 365 | 467 |
| 366 // Let all tasks run. | 468 // Let all tasks run. |
| 367 task_runner_->RunUntilIdle(); | 469 task_runner_->RunUntilIdle(); |
| 368 | 470 |
| 369 // Verify that no report upload took place. | 471 // Verify that no report upload took place. |
| 370 ExpectNoUpload(); | 472 AssertNoUpload(); |
| 473 } | |
| 474 | |
| 475 // Tests that no incident report is uploaded if there is no recent download. | |
| 476 TEST_F(IncidentReportingServiceTest, NoDownloadNoUpload) { | |
| 477 // Tell the fixture to return no downloads found. | |
| 478 SetCreateDownloadFinderAction(ON_CREATE_DOWNLOAD_FINDER_NO_DOWNLOADS); | |
| 479 | |
| 480 // Create the profile, thereby causing the test to begin. | |
| 481 CreateProfile( | |
| 482 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); | |
| 483 | |
| 484 // Let all tasks run. | |
| 485 task_runner_->RunUntilIdle(); | |
| 486 | |
| 487 // Verify that the download finder was run but that no report upload took | |
| 488 // place. | |
| 489 EXPECT_TRUE(HasCreatedDownloadFinder()); | |
| 490 AssertNoUpload(); | |
| 491 } | |
| 492 | |
| 493 // Tests that no incident report is uploaded if there is no recent download. | |
| 494 TEST_F(IncidentReportingServiceTest, NoProfilesNoUpload) { | |
| 495 // Tell the fixture to pretend there are no profiles eligible for finding | |
| 496 // downloads. | |
| 497 SetCreateDownloadFinderAction(ON_CREATE_DOWNLOAD_FINDER_NO_PROFILES); | |
| 498 | |
| 499 // Create the profile, thereby causing the test to begin. | |
| 500 CreateProfile( | |
| 501 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); | |
| 502 | |
| 503 // Let all tasks run. | |
| 504 task_runner_->RunUntilIdle(); | |
| 505 | |
| 506 // Verify that the download finder was run but that no report upload took | |
| 507 // place. | |
| 508 EXPECT_TRUE(HasCreatedDownloadFinder()); | |
|
mattm
2014/06/27 21:50:32
should these expect that the downloadfinder was de
grt (UTC plus 2)
2014/06/30 16:53:37
Sure, done.
| |
| 509 AssertNoUpload(); | |
| 371 } | 510 } |
| 372 | 511 |
| 373 // Tests that an incident added after upload is not uploaded again. | 512 // Tests that an incident added after upload is not uploaded again. |
| 374 TEST_F(IncidentReportingServiceTest, OnlyOneUpload) { | 513 TEST_F(IncidentReportingServiceTest, OnlyOneUpload) { |
| 375 // Create the profile, thereby causing the test to begin. | 514 // Create the profile, thereby causing the test to begin. |
| 376 Profile* profile = CreateProfile( | 515 Profile* profile = CreateProfile( |
| 377 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_CREATION_ADD_INCIDENT); | 516 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
| 378 | 517 |
| 379 // Let all tasks run. | 518 // Let all tasks run. |
| 380 task_runner_->RunUntilIdle(); | 519 task_runner_->RunUntilIdle(); |
| 381 | 520 |
| 382 // Verify that report upload took place and contained the incident and | 521 // Verify that report upload took place and contained the incident and |
| 383 // environment data. | 522 // environment data. |
| 384 ExpectTestIncidentUploaded(); | 523 ExpectTestIncidentUploaded(); |
| 385 | 524 |
| 386 // Add the incident to the service again. | 525 // Add the incident to the service again. |
| 387 AddTestIncident(profile); | 526 AddTestIncident(profile); |
| 388 | 527 |
| 389 // Let all tasks run. | 528 // Let all tasks run. |
| 390 task_runner_->RunUntilIdle(); | 529 task_runner_->RunUntilIdle(); |
| 391 | 530 |
| 392 // Verify that no additional report upload took place. | 531 // Verify that no additional report upload took place. |
| 393 ExpectNoUpload(); | 532 AssertNoUpload(); |
| 394 } | 533 } |
| 395 | 534 |
| 396 // Tests that the same incident added for two different profiles in sequence | 535 // Tests that the same incident added for two different profiles in sequence |
| 397 // results in two uploads. | 536 // results in two uploads. |
| 398 TEST_F(IncidentReportingServiceTest, TwoProfilesTwoUploads) { | 537 TEST_F(IncidentReportingServiceTest, TwoProfilesTwoUploads) { |
| 399 // Create the profile, thereby causing the test to begin. | 538 // Create the profile, thereby causing the test to begin. |
| 400 CreateProfile( | 539 CreateProfile( |
| 401 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_CREATION_ADD_INCIDENT); | 540 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
| 402 | 541 |
| 403 // Let all tasks run. | 542 // Let all tasks run. |
| 404 task_runner_->RunUntilIdle(); | 543 task_runner_->RunUntilIdle(); |
| 405 | 544 |
| 406 // Verify that report upload took place and contained the incident and | 545 // Verify that report upload took place and contained the incident and |
| 407 // environment data. | 546 // environment data. |
| 408 ExpectTestIncidentUploaded(); | 547 ExpectTestIncidentUploaded(); |
| 409 | 548 |
| 410 // Create a second profile with its own incident on creation. | 549 // Create a second profile with its own incident on addition. |
| 411 CreateProfile( | 550 CreateProfile( |
| 412 "profile2", SAFE_BROWSING_OPT_IN, ON_PROFILE_CREATION_ADD_INCIDENT); | 551 "profile2", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
| 413 | 552 |
| 414 // Let all tasks run. | 553 // Let all tasks run. |
| 415 task_runner_->RunUntilIdle(); | 554 task_runner_->RunUntilIdle(); |
| 416 | 555 |
| 417 // Verify that a second report upload took place. | 556 // Verify that a second report upload took place. |
| 418 ExpectTestIncidentUploaded(); | 557 ExpectTestIncidentUploaded(); |
| 419 } | 558 } |
| 420 | 559 |
| 421 // Tests that an upload succeeds if the profile is destroyed while it is | 560 // Tests that an upload succeeds if the profile is destroyed while it is |
| 422 // pending. | 561 // pending. |
| 423 TEST_F(IncidentReportingServiceTest, ProfileDestroyedDuringUpload) { | 562 TEST_F(IncidentReportingServiceTest, ProfileDestroyedDuringUpload) { |
| 424 // Create a profile for which an incident will be added. | 563 // Create a profile for which an incident will be added. |
| 425 Profile* profile = CreateProfile( | 564 Profile* profile = CreateProfile( |
| 426 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_CREATION_ADD_INCIDENT); | 565 "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT); |
| 427 | 566 |
| 428 // Hook up a callback to run when the upload is started that will post a task | 567 // Hook up a callback to run when the upload is started that will post a task |
| 429 // to delete the profile. This task will run before the upload finishes. | 568 // to delete the profile. This task will run before the upload finishes. |
| 430 DeleteProfileOnUpload(profile); | 569 DeleteProfileOnUpload(profile); |
| 431 | 570 |
| 432 // Let all tasks run. | 571 // Let all tasks run. |
| 433 task_runner_->RunUntilIdle(); | 572 task_runner_->RunUntilIdle(); |
| 434 | 573 |
| 435 // Verify that report upload took place and contained the incident and | 574 // Verify that report upload took place and contained the incident and |
| 436 // environment data. | 575 // environment data. |
| 437 ExpectTestIncidentUploaded(); | 576 ExpectTestIncidentUploaded(); |
| 438 | 577 |
| 439 // The lack of a crash indicates that the deleted profile was not accessed by | 578 // The lack of a crash indicates that the deleted profile was not accessed by |
| 440 // the service while handling the upload response. | 579 // the service while handling the upload response. |
| 441 } | 580 } |
| 442 | 581 |
| 443 // Parallel uploads | 582 // Parallel uploads |
| 444 // Shutdown during processing | 583 // Shutdown during processing |
| 445 // environment colection taking longer than incident delay timer | 584 // environment colection taking longer than incident delay timer |
| 446 // environment colection taking longer than incident delay timer, and then | 585 // environment colection taking longer than incident delay timer, and then |
| 447 // another incident arriving | 586 // another incident arriving |
| OLD | NEW |