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 <string> | 8 #include <string> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "base/test/test_simple_task_runner.h" | 14 #include "base/test/test_simple_task_runner.h" |
| 13 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/thread_local.h" | 16 #include "base/threading/thread_local.h" |
| 15 #include "chrome/browser/prefs/browser_prefs.h" | 17 #include "chrome/browser/prefs/browser_prefs.h" |
| 16 #include "chrome/browser/safe_browsing/incident_report_uploader.h" | 18 #include "chrome/browser/safe_browsing/incident_report_uploader.h" |
| 17 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/common/safe_browsing/csd.pb.h" | 20 #include "chrome/common/safe_browsing/csd.pb.h" |
| 19 #include "chrome/test/base/scoped_testing_local_state.h" | |
| 20 #include "chrome/test/base/testing_browser_process.h" | 21 #include "chrome/test/base/testing_browser_process.h" |
| 21 #include "chrome/test/base/testing_pref_service_syncable.h" | 22 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 24 #include "chrome/test/base/testing_profile_manager.h" | |
| 23 #include "net/url_request/url_request_context_getter.h" | 25 #include "net/url_request/url_request_context_getter.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 27 |
| 26 // A test fixture that sets up a test task runner and makes it the thread's | 28 // A test fixture that sets up a test task runner and makes it the thread's |
| 27 // runner. The fixture implements a fake envrionment data collector and a fake | 29 // runner. The fixture implements a fake envrionment data collector and a fake |
| 28 // report uploader. | 30 // report uploader. |
| 29 class IncidentReportingServiceTest : public testing::Test { | 31 class IncidentReportingServiceTest : public testing::Test { |
| 30 protected: | 32 protected: |
| 31 // An IRS class that allows a test harness to provide a fake environment | 33 // An IRS class that allows a test harness to provide a fake environment |
| 32 // collector and report uploader via callbacks. | 34 // collector and report uploader via callbacks. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 }; | 85 }; |
| 84 | 86 |
| 85 static base::LazyInstance<base::ThreadLocalPointer< | 87 static base::LazyInstance<base::ThreadLocalPointer< |
| 86 TestIncidentReportingService> >::Leaky test_instance_; | 88 TestIncidentReportingService> >::Leaky test_instance_; |
| 87 | 89 |
| 88 PreProfileCreateCallback pre_profile_create_callback_; | 90 PreProfileCreateCallback pre_profile_create_callback_; |
| 89 CollectEnvironmentCallback collect_environment_callback_; | 91 CollectEnvironmentCallback collect_environment_callback_; |
| 90 StartUploadCallback start_upload_callback_; | 92 StartUploadCallback start_upload_callback_; |
| 91 }; | 93 }; |
| 92 | 94 |
| 95 // Handy constants to make calls to CreateProfile easy to understand. | |
|
mattm
2014/06/20 05:44:34
If you use enums it'll also be type-safe
grt (UTC plus 2)
2014/06/20 14:52:53
Indeed. I chose not to since it would be more text
| |
| 96 static const bool kSafeBrowsingOptIn; | |
| 97 static const bool kSafeBrowsingOptOut; | |
| 98 static const bool kOnCreationAddIncident; | |
| 99 static const bool kOnCreationDoNotAddIncident; | |
| 100 | |
| 93 static const int64 kIncidentTimeMsec; | 101 static const int64 kIncidentTimeMsec; |
| 94 static const char kFakeOsName[]; | 102 static const char kFakeOsName[]; |
| 95 | 103 |
| 96 IncidentReportingServiceTest() | 104 IncidentReportingServiceTest() |
| 97 : task_runner_(new base::TestSimpleTaskRunner), | 105 : task_runner_(new base::TestSimpleTaskRunner), |
| 98 thread_task_runner_handle_(task_runner_), | 106 thread_task_runner_handle_(task_runner_), |
| 99 local_state_(TestingBrowserProcess::GetGlobal()), | 107 profile_manager_(TestingBrowserProcess::GetGlobal()), |
| 100 instance_(new TestIncidentReportingService( | 108 instance_(new TestIncidentReportingService( |
| 101 task_runner_, | 109 task_runner_, |
| 102 base::Bind(&IncidentReportingServiceTest::PreProfileCreate, | 110 base::Bind(&IncidentReportingServiceTest::PreProfileCreate, |
| 103 base::Unretained(this)), | 111 base::Unretained(this)), |
| 104 base::Bind(&IncidentReportingServiceTest::CollectEnvironmentData, | 112 base::Bind(&IncidentReportingServiceTest::CollectEnvironmentData, |
| 105 base::Unretained(this)), | 113 base::Unretained(this)), |
| 106 base::Bind(&IncidentReportingServiceTest::StartUpload, | 114 base::Bind(&IncidentReportingServiceTest::StartUpload, |
| 107 base::Unretained(this)))), | 115 base::Unretained(this)))), |
| 108 upload_result_(safe_browsing::IncidentReportUploader::UPLOAD_SUCCESS), | 116 upload_result_(safe_browsing::IncidentReportUploader::UPLOAD_SUCCESS), |
| 109 environment_collected_(), | 117 environment_collected_(), |
| 110 uploader_destroyed_() {} | 118 uploader_destroyed_() {} |
| 111 | 119 |
| 112 // Begins the test by creating a profile. An incident will be created within | 120 virtual void SetUp() OVERRIDE { |
| 113 // PreProfileCreate. Tasks are run to allow the service to operate to | 121 testing::Test::SetUp(); |
| 114 // completion. | 122 ASSERT_TRUE(profile_manager_.SetUp()); |
| 115 void CreateProfileAndRunTest(bool safe_browsing_enabled) { | 123 } |
| 124 | |
| 125 // Creates and returns a profile (owned by the profile manager) with or | |
| 126 // without safe browsing enabled. An incident will be created within | |
| 127 // PreProfileCreate if requested. See the handy constants above for making | |
| 128 // consumption of this easy to understand. Take care, though, they aren't | |
| 129 // typesafe. | |
| 130 TestingProfile* CreateProfile(const std::string& profile_name, | |
| 131 bool safe_browsing_opt_in, | |
| 132 bool on_creation) { | |
| 116 // Create prefs for the profile with safe browsing enabled or not. | 133 // Create prefs for the profile with safe browsing enabled or not. |
| 117 scoped_ptr<TestingPrefServiceSyncable> prefs( | 134 scoped_ptr<TestingPrefServiceSyncable> prefs( |
| 118 new TestingPrefServiceSyncable); | 135 new TestingPrefServiceSyncable); |
| 119 chrome::RegisterUserProfilePrefs(prefs->registry()); | 136 chrome::RegisterUserProfilePrefs(prefs->registry()); |
| 120 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, safe_browsing_enabled); | 137 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, |
| 138 safe_browsing_opt_in == kSafeBrowsingOptIn); | |
| 121 | 139 |
| 122 // Build the test profile (PreProfileCreate will be called). | 140 // Remember whether or not to create an incident. |
| 123 TestingProfile::Builder builder; | 141 profile_properties_[profile_name].on_creation_add_incident = |
| 124 builder.SetPrefService(prefs.PassAs<PrefServiceSyncable>()); | 142 (on_creation == kOnCreationAddIncident); |
| 125 testing_profile_ = builder.Build().Pass(); | |
| 126 | 143 |
| 127 // Let all tasks run. | 144 // Boom (or fizzle). |
| 128 task_runner_->RunUntilIdle(); | 145 return profile_manager_.CreateTestingProfile( |
| 146 profile_name, | |
| 147 prefs.PassAs<PrefServiceSyncable>(), | |
| 148 base::ASCIIToUTF16(profile_name), | |
| 149 0, | |
| 150 std::string(), | |
|
mattm
2014/06/20 05:44:34
Add comments with the parameter names.
grt (UTC plus 2)
2014/06/20 14:52:53
Done.
| |
| 151 TestingProfile::TestingFactories()); | |
| 152 } | |
| 153 | |
| 154 // Configures a callback to run when the next upload is started that will post | |
| 155 // a task to delete the profile. This task will run before the upload | |
| 156 // finishes. | |
| 157 void DeleteProfileOnUpload(Profile* profile) { | |
|
mattm
2014/06/20 05:44:34
add an EXPECT/ASSERT_TRUE(on_start_upload_callback
grt (UTC plus 2)
2014/06/20 14:52:53
Done.
| |
| 158 on_start_upload_callback_ = | |
| 159 base::Bind(&IncidentReportingServiceTest::DelayedDeleteProfile, | |
| 160 base::Unretained(this), | |
| 161 profile); | |
| 129 } | 162 } |
| 130 | 163 |
| 131 // Returns an incident suitable for testing. | 164 // Returns an incident suitable for testing. |
| 132 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> | 165 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> |
| 133 MakeTestIncident() { | 166 MakeTestIncident() { |
| 134 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( | 167 scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident( |
| 135 new safe_browsing::ClientIncidentReport_IncidentData()); | 168 new safe_browsing::ClientIncidentReport_IncidentData()); |
| 136 incident->set_incident_time_msec(kIncidentTimeMsec); | 169 incident->set_incident_time_msec(kIncidentTimeMsec); |
| 137 incident->mutable_tracked_preference(); | 170 incident->mutable_tracked_preference(); |
| 138 return incident.Pass(); | 171 return incident.Pass(); |
| 139 } | 172 } |
| 140 | 173 |
| 141 // Confirms that the test incident was uploaded by the service. | 174 // Adds a test incident to the service. |
| 175 void AddTestIncident(Profile* profile) { | |
| 176 instance_->GetAddIncidentCallback(profile).Run(MakeTestIncident().Pass()); | |
| 177 } | |
| 178 | |
| 179 // Confirms that the test incident was uploaded by the service, then clears | |
| 180 // the instance for subsequent incidents. | |
| 142 void ExpectTestIncidentUploaded() { | 181 void ExpectTestIncidentUploaded() { |
| 143 ASSERT_TRUE(uploaded_report_); | 182 ASSERT_TRUE(uploaded_report_); |
| 144 ASSERT_EQ(1, uploaded_report_->incident_size()); | 183 ASSERT_EQ(1, uploaded_report_->incident_size()); |
| 145 ASSERT_TRUE(uploaded_report_->incident(0).has_incident_time_msec()); | 184 ASSERT_TRUE(uploaded_report_->incident(0).has_incident_time_msec()); |
| 146 ASSERT_EQ(kIncidentTimeMsec, | 185 ASSERT_EQ(kIncidentTimeMsec, |
| 147 uploaded_report_->incident(0).incident_time_msec()); | 186 uploaded_report_->incident(0).incident_time_msec()); |
| 148 ASSERT_TRUE(uploaded_report_->has_environment()); | 187 ASSERT_TRUE(uploaded_report_->has_environment()); |
| 149 ASSERT_TRUE(uploaded_report_->environment().has_os()); | 188 ASSERT_TRUE(uploaded_report_->environment().has_os()); |
| 150 ASSERT_TRUE(uploaded_report_->environment().os().has_os_name()); | 189 ASSERT_TRUE(uploaded_report_->environment().os().has_os_name()); |
| 151 ASSERT_EQ(std::string(kFakeOsName), | 190 ASSERT_EQ(std::string(kFakeOsName), |
| 152 uploaded_report_->environment().os().os_name()); | 191 uploaded_report_->environment().os().os_name()); |
| 192 | |
| 193 uploaded_report_.reset(); | |
| 153 } | 194 } |
| 154 | 195 |
| 155 void ExpectNoUpload() { ASSERT_FALSE(uploaded_report_); } | 196 void ExpectNoUpload() { ASSERT_FALSE(uploaded_report_); } |
| 156 | 197 |
| 157 bool HasCollectedEnvironmentData() const { return environment_collected_; } | 198 bool HasCollectedEnvironmentData() const { return environment_collected_; } |
| 158 bool UploaderDestroyed() const { return uploader_destroyed_; } | 199 bool UploaderDestroyed() const { return uploader_destroyed_; } |
| 159 | 200 |
| 160 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 201 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 161 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 202 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
| 162 ScopedTestingLocalState local_state_; | 203 TestingProfileManager profile_manager_; |
| 163 scoped_ptr<safe_browsing::IncidentReportingService> instance_; | 204 scoped_ptr<safe_browsing::IncidentReportingService> instance_; |
| 205 base::Closure on_start_upload_callback_; | |
| 164 safe_browsing::IncidentReportUploader::Result upload_result_; | 206 safe_browsing::IncidentReportUploader::Result upload_result_; |
| 165 bool environment_collected_; | 207 bool environment_collected_; |
| 166 scoped_ptr<safe_browsing::ClientIncidentReport> uploaded_report_; | 208 scoped_ptr<safe_browsing::ClientIncidentReport> uploaded_report_; |
| 167 bool uploader_destroyed_; | 209 bool uploader_destroyed_; |
| 168 scoped_ptr<TestingProfile> testing_profile_; | |
| 169 | 210 |
| 170 private: | 211 private: |
| 171 // A fake IncidentReportUploader that posts a task to provide a given response | 212 // A fake IncidentReportUploader that posts a task to provide a given response |
| 172 // back to the incident reporting service. It also reports back to the test | 213 // back to the incident reporting service. It also reports back to the test |
| 173 // harness via a closure when it is deleted by the incident reporting service. | 214 // harness via a closure when it is deleted by the incident reporting service. |
| 174 class FakeUploader : public safe_browsing::IncidentReportUploader { | 215 class FakeUploader : public safe_browsing::IncidentReportUploader { |
| 175 public: | 216 public: |
| 176 FakeUploader( | 217 FakeUploader( |
| 177 const base::Closure& on_deleted, | 218 const base::Closure& on_deleted, |
| 178 const safe_browsing::IncidentReportUploader::OnResultCallback& callback, | 219 const safe_browsing::IncidentReportUploader::OnResultCallback& callback, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 194 callback_.Run(result_, | 235 callback_.Run(result_, |
| 195 scoped_ptr<safe_browsing::ClientIncidentResponse>()); | 236 scoped_ptr<safe_browsing::ClientIncidentResponse>()); |
| 196 } | 237 } |
| 197 | 238 |
| 198 base::Closure on_deleted_; | 239 base::Closure on_deleted_; |
| 199 safe_browsing::IncidentReportUploader::Result result_; | 240 safe_browsing::IncidentReportUploader::Result result_; |
| 200 | 241 |
| 201 DISALLOW_COPY_AND_ASSIGN(FakeUploader); | 242 DISALLOW_COPY_AND_ASSIGN(FakeUploader); |
| 202 }; | 243 }; |
| 203 | 244 |
| 245 // Properties for a profile that impact the behavior of the test. | |
| 246 struct ProfileProperties { | |
| 247 ProfileProperties() : on_creation_add_incident() {} | |
| 248 | |
| 249 // If true (kOnCreationAddIncident), the test fixture will add a test | |
| 250 // incident to the service during profile initialization (before | |
| 251 // NOTIFICATION_PROFILE_CREATED is sent). | |
| 252 bool on_creation_add_incident; | |
| 253 }; | |
| 254 | |
| 255 // Returns the name of a profile as provided to CreateProfile. | |
| 256 static std::string GetProfileName(Profile* profile) { | |
| 257 // Cannot reliably use profile->GetProfileName() since the test needs the | |
| 258 // name before the profile manager sets it (which happens after profile | |
| 259 // creation). | |
| 260 return profile->GetPath().BaseName().AsUTF8Unsafe(); | |
| 261 } | |
| 262 | |
| 263 // Posts a task to delete the profile. | |
| 264 void DelayedDeleteProfile(Profile* profile) { | |
| 265 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 266 FROM_HERE, | |
| 267 base::Bind(&TestingProfileManager::DeleteTestingProfile, | |
| 268 base::Unretained(&profile_manager_), | |
| 269 GetProfileName(profile))); | |
| 270 } | |
| 271 | |
| 204 // A callback run by the test fixture when a profile is created. An incident | 272 // A callback run by the test fixture when a profile is created. An incident |
| 205 // is added. | 273 // is added. |
| 206 void PreProfileCreate(Profile* profile) { | 274 void PreProfileCreate(Profile* profile) { |
| 207 // The instance must have already been created. | 275 // The instance must have already been created. |
| 208 ASSERT_TRUE(instance_); | 276 ASSERT_TRUE(instance_); |
| 209 // Add a test incident to the service. | 277 // Add a test incident to the service if requested. |
| 210 instance_->GetAddIncidentCallback(profile).Run(MakeTestIncident().Pass()); | 278 if (profile_properties_[GetProfileName(profile)].on_creation_add_incident) |
| 279 AddTestIncident(profile); | |
| 211 } | 280 } |
| 212 | 281 |
| 213 // A fake CollectEnvironmentData implementation invoked by the service during | 282 // A fake CollectEnvironmentData implementation invoked by the service during |
| 214 // operation. | 283 // operation. |
| 215 void CollectEnvironmentData( | 284 void CollectEnvironmentData( |
| 216 safe_browsing::ClientIncidentReport_EnvironmentData* data) { | 285 safe_browsing::ClientIncidentReport_EnvironmentData* data) { |
| 217 ASSERT_NE( | 286 ASSERT_NE( |
| 218 static_cast<safe_browsing::ClientIncidentReport_EnvironmentData*>(NULL), | 287 static_cast<safe_browsing::ClientIncidentReport_EnvironmentData*>(NULL), |
| 219 data); | 288 data); |
| 220 data->mutable_os()->set_os_name(kFakeOsName); | 289 data->mutable_os()->set_os_name(kFakeOsName); |
| 221 environment_collected_ = true; | 290 environment_collected_ = true; |
| 222 } | 291 } |
| 223 | 292 |
| 224 // A fake StartUpload implementation invoked by the service during operation. | 293 // A fake StartUpload implementation invoked by the service during operation. |
| 225 scoped_ptr<safe_browsing::IncidentReportUploader> StartUpload( | 294 scoped_ptr<safe_browsing::IncidentReportUploader> StartUpload( |
| 226 const safe_browsing::IncidentReportUploader::OnResultCallback& callback, | 295 const safe_browsing::IncidentReportUploader::OnResultCallback& callback, |
| 227 const safe_browsing::ClientIncidentReport& report) { | 296 const safe_browsing::ClientIncidentReport& report) { |
| 228 // Remember the report that is being uploaded. | 297 // Remember the report that is being uploaded. |
| 229 uploaded_report_.reset(new safe_browsing::ClientIncidentReport(report)); | 298 uploaded_report_.reset(new safe_browsing::ClientIncidentReport(report)); |
| 299 // Run and clear the OnStartUpload callback, if provided. | |
| 300 if (!on_start_upload_callback_.is_null()) { | |
| 301 on_start_upload_callback_.Run(); | |
| 302 on_start_upload_callback_ = base::Closure(); | |
| 303 } | |
| 230 return scoped_ptr<safe_browsing::IncidentReportUploader>(new FakeUploader( | 304 return scoped_ptr<safe_browsing::IncidentReportUploader>(new FakeUploader( |
| 231 base::Bind(&IncidentReportingServiceTest::OnUploaderDestroyed, | 305 base::Bind(&IncidentReportingServiceTest::OnUploaderDestroyed, |
| 232 base::Unretained(this)), | 306 base::Unretained(this)), |
| 233 callback, | 307 callback, |
| 234 upload_result_)); | 308 upload_result_)); |
| 235 } | 309 } |
| 236 | 310 |
| 237 void OnUploaderDestroyed() { uploader_destroyed_ = true; } | 311 void OnUploaderDestroyed() { uploader_destroyed_ = true; } |
| 312 | |
| 313 // A mapping of profile name to its corresponding properties. | |
| 314 std::map<std::string, ProfileProperties> profile_properties_; | |
| 238 }; | 315 }; |
| 239 | 316 |
| 240 // static | 317 // static |
| 241 base::LazyInstance<base::ThreadLocalPointer< | 318 base::LazyInstance<base::ThreadLocalPointer< |
| 242 IncidentReportingServiceTest::TestIncidentReportingService> >::Leaky | 319 IncidentReportingServiceTest::TestIncidentReportingService> >::Leaky |
| 243 IncidentReportingServiceTest::TestIncidentReportingService::test_instance_ = | 320 IncidentReportingServiceTest::TestIncidentReportingService::test_instance_ = |
| 244 LAZY_INSTANCE_INITIALIZER; | 321 LAZY_INSTANCE_INITIALIZER; |
| 245 | 322 |
| 246 // static | 323 // static |
| 324 const bool IncidentReportingServiceTest::kSafeBrowsingOptIn = true; | |
| 325 const bool IncidentReportingServiceTest::kSafeBrowsingOptOut = false; | |
| 326 const bool IncidentReportingServiceTest::kOnCreationAddIncident = true; | |
| 327 const bool IncidentReportingServiceTest::kOnCreationDoNotAddIncident = false; | |
| 328 | |
| 247 const int64 IncidentReportingServiceTest::kIncidentTimeMsec = 47LL; | 329 const int64 IncidentReportingServiceTest::kIncidentTimeMsec = 47LL; |
| 248 const char IncidentReportingServiceTest::kFakeOsName[] = "fakedows"; | 330 const char IncidentReportingServiceTest::kFakeOsName[] = "fakedows"; |
| 249 | 331 |
| 250 // Tests that an incident added during profile initialization when safe browsing | 332 // Tests that an incident added during profile initialization when safe browsing |
| 251 // is on is uploaded. | 333 // is on is uploaded. |
| 252 TEST_F(IncidentReportingServiceTest, AddIncident) { | 334 TEST_F(IncidentReportingServiceTest, AddIncident) { |
| 253 // Create the profile, thereby causing the test to begin. | 335 CreateProfile("profile1", kSafeBrowsingOptIn, kOnCreationAddIncident); |
| 254 CreateProfileAndRunTest(true); | 336 |
| 337 // Let all tasks run. | |
| 338 task_runner_->RunUntilIdle(); | |
| 255 | 339 |
| 256 // Verify that environment collection took place. | 340 // Verify that environment collection took place. |
| 257 EXPECT_TRUE(HasCollectedEnvironmentData()); | 341 EXPECT_TRUE(HasCollectedEnvironmentData()); |
| 258 | 342 |
| 259 // Verify that report upload took place and contained the incident and | 343 // Verify that report upload took place and contained the incident and |
| 260 // environment data. | 344 // environment data. |
| 261 ExpectTestIncidentUploaded(); | 345 ExpectTestIncidentUploaded(); |
| 262 | 346 |
| 263 // Verify that the uploader was destroyed. | 347 // Verify that the uploader was destroyed. |
| 264 ASSERT_TRUE(UploaderDestroyed()); | 348 ASSERT_TRUE(UploaderDestroyed()); |
| 265 } | 349 } |
| 266 | 350 |
| 267 // Tests that an incident added during profile initialization when safe browsing | 351 // Tests that an incident added during profile initialization when safe browsing |
| 268 // is off is not uploaded. | 352 // is off is not uploaded. |
| 269 TEST_F(IncidentReportingServiceTest, NoSafeBrowsing) { | 353 TEST_F(IncidentReportingServiceTest, NoSafeBrowsing) { |
| 270 // Create the profile, thereby causing the test to begin. | 354 // Create the profile, thereby causing the test to begin. |
| 271 CreateProfileAndRunTest(false); | 355 CreateProfile("profile1", kSafeBrowsingOptOut, kOnCreationAddIncident); |
| 356 | |
| 357 // Let all tasks run. | |
| 358 task_runner_->RunUntilIdle(); | |
| 272 | 359 |
| 273 // Verify that no report upload took place. | 360 // Verify that no report upload took place. |
| 274 ExpectNoUpload(); | 361 ExpectNoUpload(); |
| 275 } | 362 } |
| 276 | 363 |
| 364 // Tests that an incident added after upload is not uploaded again. | |
| 365 TEST_F(IncidentReportingServiceTest, OnlyOneUpload) { | |
| 366 // Create the profile, thereby causing the test to begin. | |
| 367 Profile* profile = | |
| 368 CreateProfile("profile1", kSafeBrowsingOptIn, kOnCreationAddIncident); | |
| 369 | |
| 370 // Let all tasks run. | |
| 371 task_runner_->RunUntilIdle(); | |
| 372 | |
| 373 // Verify that report upload took place and contained the incident and | |
| 374 // environment data. | |
| 375 ExpectTestIncidentUploaded(); | |
| 376 | |
| 377 // Add the incident to the service again. | |
| 378 AddTestIncident(profile); | |
| 379 | |
| 380 // Let all tasks run. | |
| 381 task_runner_->RunUntilIdle(); | |
| 382 | |
| 383 // Verify that no additional report upload took place. | |
| 384 ExpectNoUpload(); | |
| 385 } | |
| 386 | |
| 387 // Tests that the same incident added for two different profiles in sequence | |
| 388 // results in two uploads. | |
| 389 TEST_F(IncidentReportingServiceTest, TwoProfilesTwoUploads) { | |
| 390 // Create the profile, thereby causing the test to begin. | |
| 391 CreateProfile("profile1", kSafeBrowsingOptIn, kOnCreationAddIncident); | |
| 392 | |
| 393 // Let all tasks run. | |
| 394 task_runner_->RunUntilIdle(); | |
| 395 | |
| 396 // Verify that report upload took place and contained the incident and | |
| 397 // environment data. | |
| 398 ExpectTestIncidentUploaded(); | |
| 399 | |
| 400 // Create a second profile with its own incident on creation. | |
| 401 CreateProfile("profile2", kSafeBrowsingOptIn, kOnCreationAddIncident); | |
| 402 | |
| 403 // Let all tasks run. | |
| 404 task_runner_->RunUntilIdle(); | |
| 405 | |
| 406 // Verify that a second report upload took place. | |
| 407 ExpectTestIncidentUploaded(); | |
| 408 } | |
| 409 | |
| 410 // Tests that an upload succeeds if the profile is destroyed while it is | |
| 411 // pending. | |
| 412 TEST_F(IncidentReportingServiceTest, ProfileDestroyedDuringUpload) { | |
| 413 // Create a profile for which an incident will be added. | |
| 414 Profile* profile = | |
| 415 CreateProfile("profile1", kSafeBrowsingOptIn, kOnCreationAddIncident); | |
| 416 | |
| 417 // Hook up a callback to run when the upload is started that will post a task | |
| 418 // to delete the profile. This task will run before the upload finishes. | |
| 419 DeleteProfileOnUpload(profile); | |
| 420 | |
| 421 // Let all tasks run. | |
| 422 task_runner_->RunUntilIdle(); | |
| 423 | |
| 424 // Verify that report upload took place and contained the incident and | |
| 425 // environment data. | |
| 426 ExpectTestIncidentUploaded(); | |
| 427 | |
| 428 // The lack of a crash indicates that the deleted profile was not accessed by | |
| 429 // the service while handling the upload response. | |
| 430 } | |
| 431 | |
| 277 // Parallel uploads | 432 // Parallel uploads |
| 278 // Shutdown during processing | 433 // Shutdown during processing |
| 279 // environment colection taking longer than incident delay timer | 434 // environment colection taking longer than incident delay timer |
| 280 // environment colection taking longer than incident delay timer, and then | 435 // environment colection taking longer than incident delay timer, and then |
| 281 // another incident arriving | 436 // another incident arriving |
| OLD | NEW |