| Index: chrome/browser/safe_browsing/incident_reporting_service_unittest.cc
|
| ===================================================================
|
| --- chrome/browser/safe_browsing/incident_reporting_service_unittest.cc (revision 281829)
|
| +++ chrome/browser/safe_browsing/incident_reporting_service_unittest.cc (working copy)
|
| @@ -16,6 +16,7 @@
|
| #include "base/threading/thread_local.h"
|
| #include "chrome/browser/prefs/browser_prefs.h"
|
| #include "chrome/browser/safe_browsing/incident_report_uploader.h"
|
| +#include "chrome/browser/safe_browsing/last_download_finder.h"
|
| #include "chrome/common/pref_names.h"
|
| #include "chrome/common/safe_browsing/csd.pb.h"
|
| #include "chrome/test/base/testing_browser_process.h"
|
| @@ -35,24 +36,30 @@
|
| class TestIncidentReportingService
|
| : public safe_browsing::IncidentReportingService {
|
| public:
|
| - typedef base::Callback<void(Profile*)> PreProfileCreateCallback;
|
| + typedef base::Callback<void(Profile*)> PreProfileAddCallback;
|
|
|
| typedef base::Callback<
|
| void(safe_browsing::ClientIncidentReport_EnvironmentData*)>
|
| CollectEnvironmentCallback;
|
|
|
| + typedef base::Callback<scoped_ptr<safe_browsing::LastDownloadFinder>(
|
| + const safe_browsing::LastDownloadFinder::LastDownloadCallback&
|
| + callback)> CreateDownloadFinderCallback;
|
| +
|
| typedef base::Callback<scoped_ptr<safe_browsing::IncidentReportUploader>(
|
| const safe_browsing::IncidentReportUploader::OnResultCallback&,
|
| const safe_browsing::ClientIncidentReport& report)> StartUploadCallback;
|
|
|
| TestIncidentReportingService(
|
| const scoped_refptr<base::TaskRunner>& task_runner,
|
| - const PreProfileCreateCallback& pre_profile_create_callback,
|
| + const PreProfileAddCallback& pre_profile_add_callback,
|
| const CollectEnvironmentCallback& collect_environment_callback,
|
| + const CreateDownloadFinderCallback& create_download_finder_callback,
|
| const StartUploadCallback& start_upload_callback)
|
| : IncidentReportingService(NULL, NULL),
|
| - pre_profile_create_callback_(pre_profile_create_callback),
|
| + pre_profile_add_callback_(pre_profile_add_callback),
|
| collect_environment_callback_(collect_environment_callback),
|
| + create_download_finder_callback_(create_download_finder_callback),
|
| start_upload_callback_(start_upload_callback) {
|
| SetCollectEnvironmentHook(&CollectEnvironmentData, task_runner);
|
| test_instance_.Get().Set(this);
|
| @@ -61,11 +68,17 @@
|
| virtual ~TestIncidentReportingService() { test_instance_.Get().Set(NULL); }
|
|
|
| protected:
|
| - virtual void OnProfileCreated(Profile* profile) OVERRIDE {
|
| - pre_profile_create_callback_.Run(profile);
|
| - safe_browsing::IncidentReportingService::OnProfileCreated(profile);
|
| + virtual void OnProfileAdded(Profile* profile) OVERRIDE {
|
| + pre_profile_add_callback_.Run(profile);
|
| + safe_browsing::IncidentReportingService::OnProfileAdded(profile);
|
| }
|
|
|
| + virtual scoped_ptr<safe_browsing::LastDownloadFinder> CreateDownloadFinder(
|
| + const safe_browsing::LastDownloadFinder::LastDownloadCallback& callback)
|
| + OVERRIDE {
|
| + return create_download_finder_callback_.Run(callback);
|
| + }
|
| +
|
| virtual scoped_ptr<safe_browsing::IncidentReportUploader> StartReportUpload(
|
| const safe_browsing::IncidentReportUploader::OnResultCallback& callback,
|
| const scoped_refptr<net::URLRequestContextGetter>&
|
| @@ -87,8 +100,9 @@
|
| static base::LazyInstance<base::ThreadLocalPointer<
|
| TestIncidentReportingService> >::Leaky test_instance_;
|
|
|
| - PreProfileCreateCallback pre_profile_create_callback_;
|
| + PreProfileAddCallback pre_profile_add_callback_;
|
| CollectEnvironmentCallback collect_environment_callback_;
|
| + CreateDownloadFinderCallback create_download_finder_callback_;
|
| StartUploadCallback start_upload_callback_;
|
| };
|
|
|
| @@ -100,14 +114,26 @@
|
| };
|
|
|
| // A type for specifying the action to be taken by the test fixture during
|
| - // profile initialization (before NOTIFICATION_PROFILE_CREATED is sent).
|
| - enum OnProfileCreationAction {
|
| - ON_PROFILE_CREATION_NO_ACTION,
|
| - ON_PROFILE_CREATION_ADD_INCIDENT, // Add an incident to the service.
|
| + // profile initialization (before NOTIFICATION_PROFILE_ADDED is sent).
|
| + enum OnProfileAdditionAction {
|
| + ON_PROFILE_ADDITION_NO_ACTION,
|
| + ON_PROFILE_ADDITION_ADD_INCIDENT, // Add an incident to the service.
|
| };
|
|
|
| + // A type for specifying the action to be taken by the test fixture when the
|
| + // service creates a LastDownloadFinder.
|
| + enum OnCreateDownloadFinderAction {
|
| + // Post a task that reports a download.
|
| + ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND,
|
| + // Post a task that reports no downloads found.
|
| + ON_CREATE_DOWNLOAD_FINDER_NO_DOWNLOADS,
|
| + // Immediately return due to a lack of eligible profiles.
|
| + ON_CREATE_DOWNLOAD_FINDER_NO_PROFILES,
|
| + };
|
| +
|
| static const int64 kIncidentTimeMsec;
|
| static const char kFakeOsName[];
|
| + static const char kFakeDownloadToken[];
|
|
|
| IncidentReportingServiceTest()
|
| : task_runner_(new base::TestSimpleTaskRunner),
|
| @@ -115,14 +141,20 @@
|
| profile_manager_(TestingBrowserProcess::GetGlobal()),
|
| instance_(new TestIncidentReportingService(
|
| task_runner_,
|
| - base::Bind(&IncidentReportingServiceTest::PreProfileCreate,
|
| + base::Bind(&IncidentReportingServiceTest::PreProfileAdd,
|
| base::Unretained(this)),
|
| base::Bind(&IncidentReportingServiceTest::CollectEnvironmentData,
|
| base::Unretained(this)),
|
| + base::Bind(&IncidentReportingServiceTest::CreateDownloadFinder,
|
| + base::Unretained(this)),
|
| base::Bind(&IncidentReportingServiceTest::StartUpload,
|
| base::Unretained(this)))),
|
| + on_create_download_finder_action_(
|
| + ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND),
|
| upload_result_(safe_browsing::IncidentReportUploader::UPLOAD_SUCCESS),
|
| environment_collected_(),
|
| + download_finder_created_(),
|
| + download_finder_destroyed_(),
|
| uploader_destroyed_() {}
|
|
|
| virtual void SetUp() OVERRIDE {
|
| @@ -130,12 +162,18 @@
|
| ASSERT_TRUE(profile_manager_.SetUp());
|
| }
|
|
|
| + // Sets the action to be taken by the test fixture when the service creates a
|
| + // LastDownloadFinder.
|
| + void SetCreateDownloadFinderAction(OnCreateDownloadFinderAction action) {
|
| + on_create_download_finder_action_ = action;
|
| + }
|
| +
|
| // Creates and returns a profile (owned by the profile manager) with or
|
| // without safe browsing enabled. An incident will be created within
|
| - // PreProfileCreate if requested.
|
| + // PreProfileAdd if requested.
|
| TestingProfile* CreateProfile(const std::string& profile_name,
|
| SafeBrowsingDisposition safe_browsing_opt_in,
|
| - OnProfileCreationAction on_creation_action) {
|
| + OnProfileAdditionAction on_addition_action) {
|
| // Create prefs for the profile with safe browsing enabled or not.
|
| scoped_ptr<TestingPrefServiceSyncable> prefs(
|
| new TestingPrefServiceSyncable);
|
| @@ -144,7 +182,7 @@
|
| safe_browsing_opt_in == SAFE_BROWSING_OPT_IN);
|
|
|
| // Remember whether or not to create an incident.
|
| - profile_properties_[profile_name].on_creation_action = on_creation_action;
|
| + profile_properties_[profile_name].on_addition_action = on_addition_action;
|
|
|
| // Boom (or fizzle).
|
| return profile_manager_.CreateTestingProfile(
|
| @@ -195,13 +233,17 @@
|
| ASSERT_TRUE(uploaded_report_->environment().os().has_os_name());
|
| ASSERT_EQ(std::string(kFakeOsName),
|
| uploaded_report_->environment().os().os_name());
|
| + ASSERT_EQ(std::string(kFakeDownloadToken),
|
| + uploaded_report_->download().token());
|
|
|
| uploaded_report_.reset();
|
| }
|
|
|
| - void ExpectNoUpload() { ASSERT_FALSE(uploaded_report_); }
|
| + void AssertNoUpload() { ASSERT_FALSE(uploaded_report_); }
|
|
|
| bool HasCollectedEnvironmentData() const { return environment_collected_; }
|
| + bool HasCreatedDownloadFinder() const { return download_finder_created_; }
|
| + bool DownloadFinderDestroyed() const { return download_finder_destroyed_; }
|
| bool UploaderDestroyed() const { return uploader_destroyed_; }
|
|
|
| scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
|
| @@ -209,9 +251,12 @@
|
| TestingProfileManager profile_manager_;
|
| scoped_ptr<safe_browsing::IncidentReportingService> instance_;
|
| base::Closure on_start_upload_callback_;
|
| + OnCreateDownloadFinderAction on_create_download_finder_action_;
|
| safe_browsing::IncidentReportUploader::Result upload_result_;
|
| bool environment_collected_;
|
| + bool download_finder_created_;
|
| scoped_ptr<safe_browsing::ClientIncidentReport> uploaded_report_;
|
| + bool download_finder_destroyed_;
|
| bool uploader_destroyed_;
|
|
|
| private:
|
| @@ -248,20 +293,46 @@
|
| DISALLOW_COPY_AND_ASSIGN(FakeUploader);
|
| };
|
|
|
| + class FakeDownloadFinder : public safe_browsing::LastDownloadFinder {
|
| + public:
|
| + static scoped_ptr<safe_browsing::LastDownloadFinder> Create(
|
| + const base::Closure& on_deleted,
|
| + scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails>
|
| + download,
|
| + const safe_browsing::LastDownloadFinder::LastDownloadCallback&
|
| + callback) {
|
| + // Post a task to run the callback.
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE, base::Bind(callback, base::Passed(&download)));
|
| + return scoped_ptr<safe_browsing::LastDownloadFinder>(
|
| + new FakeDownloadFinder(on_deleted));
|
| + }
|
| +
|
| + virtual ~FakeDownloadFinder() { on_deleted_.Run(); }
|
| +
|
| + private:
|
| + explicit FakeDownloadFinder(const base::Closure& on_deleted)
|
| + : on_deleted_(on_deleted) {}
|
| +
|
| + base::Closure on_deleted_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(FakeDownloadFinder);
|
| + };
|
| +
|
| // Properties for a profile that impact the behavior of the test.
|
| struct ProfileProperties {
|
| - ProfileProperties() : on_creation_action(ON_PROFILE_CREATION_NO_ACTION) {}
|
| + ProfileProperties() : on_addition_action(ON_PROFILE_ADDITION_NO_ACTION) {}
|
|
|
| // The action taken by the test fixture during profile initialization
|
| - // (before NOTIFICATION_PROFILE_CREATED is sent).
|
| - OnProfileCreationAction on_creation_action;
|
| + // (before NOTIFICATION_PROFILE_ADDED is sent).
|
| + OnProfileAdditionAction on_addition_action;
|
| };
|
|
|
| // Returns the name of a profile as provided to CreateProfile.
|
| static std::string GetProfileName(Profile* profile) {
|
| // Cannot reliably use profile->GetProfileName() since the test needs the
|
| // name before the profile manager sets it (which happens after profile
|
| - // creation).
|
| + // addition).
|
| return profile->GetPath().BaseName().AsUTF8Unsafe();
|
| }
|
|
|
| @@ -274,20 +345,20 @@
|
| GetProfileName(profile)));
|
| }
|
|
|
| - // A callback run by the test fixture when a profile is created. An incident
|
| + // A callback run by the test fixture when a profile is added. An incident
|
| // is added.
|
| - void PreProfileCreate(Profile* profile) {
|
| + void PreProfileAdd(Profile* profile) {
|
| // The instance must have already been created.
|
| ASSERT_TRUE(instance_);
|
| // Add a test incident to the service if requested.
|
| - switch (profile_properties_[GetProfileName(profile)].on_creation_action) {
|
| - case ON_PROFILE_CREATION_ADD_INCIDENT:
|
| + switch (profile_properties_[GetProfileName(profile)].on_addition_action) {
|
| + case ON_PROFILE_ADDITION_ADD_INCIDENT:
|
| AddTestIncident(profile);
|
| break;
|
| default:
|
| ASSERT_EQ(
|
| - ON_PROFILE_CREATION_NO_ACTION,
|
| - profile_properties_[GetProfileName(profile)].on_creation_action);
|
| + ON_PROFILE_ADDITION_NO_ACTION,
|
| + profile_properties_[GetProfileName(profile)].on_addition_action);
|
| break;
|
| }
|
| }
|
| @@ -303,6 +374,29 @@
|
| environment_collected_ = true;
|
| }
|
|
|
| + // A fake CreateDownloadFinder implementation invoked by the service during
|
| + // operation.
|
| + scoped_ptr<safe_browsing::LastDownloadFinder> CreateDownloadFinder(
|
| + const safe_browsing::LastDownloadFinder::LastDownloadCallback& callback) {
|
| + download_finder_created_ = true;
|
| + scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails> download;
|
| + if (on_create_download_finder_action_ ==
|
| + ON_CREATE_DOWNLOAD_FINDER_NO_PROFILES) {
|
| + return scoped_ptr<safe_browsing::LastDownloadFinder>();
|
| + }
|
| + if (on_create_download_finder_action_ ==
|
| + ON_CREATE_DOWNLOAD_FINDER_DOWNLOAD_FOUND) {
|
| + download.reset(new safe_browsing::ClientIncidentReport_DownloadDetails);
|
| + download->set_token(kFakeDownloadToken);
|
| + }
|
| + return scoped_ptr<safe_browsing::LastDownloadFinder>(
|
| + FakeDownloadFinder::Create(
|
| + base::Bind(&IncidentReportingServiceTest::OnDownloadFinderDestroyed,
|
| + base::Unretained(this)),
|
| + download.Pass(),
|
| + callback));
|
| + }
|
| +
|
| // A fake StartUpload implementation invoked by the service during operation.
|
| scoped_ptr<safe_browsing::IncidentReportUploader> StartUpload(
|
| const safe_browsing::IncidentReportUploader::OnResultCallback& callback,
|
| @@ -314,13 +408,16 @@
|
| on_start_upload_callback_.Run();
|
| on_start_upload_callback_ = base::Closure();
|
| }
|
| - return scoped_ptr<safe_browsing::IncidentReportUploader>(new FakeUploader(
|
| - base::Bind(&IncidentReportingServiceTest::OnUploaderDestroyed,
|
| - base::Unretained(this)),
|
| - callback,
|
| - upload_result_));
|
| + return scoped_ptr<safe_browsing::IncidentReportUploader>(
|
| + new FakeUploader(
|
| + base::Bind(
|
| + &IncidentReportingServiceTest::OnUploaderDestroyed,
|
| + base::Unretained(this)),
|
| + callback,
|
| + upload_result_)).Pass();
|
| }
|
|
|
| + void OnDownloadFinderDestroyed() { download_finder_destroyed_ = true; }
|
| void OnUploaderDestroyed() { uploader_destroyed_ = true; }
|
|
|
| // A mapping of profile name to its corresponding properties.
|
| @@ -335,12 +432,13 @@
|
|
|
| const int64 IncidentReportingServiceTest::kIncidentTimeMsec = 47LL;
|
| const char IncidentReportingServiceTest::kFakeOsName[] = "fakedows";
|
| +const char IncidentReportingServiceTest::kFakeDownloadToken[] = "fakedlt";
|
|
|
| // Tests that an incident added during profile initialization when safe browsing
|
| // is on is uploaded.
|
| TEST_F(IncidentReportingServiceTest, AddIncident) {
|
| CreateProfile(
|
| - "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_CREATION_ADD_INCIDENT);
|
| + "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT);
|
|
|
| // Let all tasks run.
|
| task_runner_->RunUntilIdle();
|
| @@ -348,11 +446,15 @@
|
| // Verify that environment collection took place.
|
| EXPECT_TRUE(HasCollectedEnvironmentData());
|
|
|
| - // Verify that report upload took place and contained the incident and
|
| - // environment data.
|
| + // Verify that the most recent download was looked for.
|
| + EXPECT_TRUE(HasCreatedDownloadFinder());
|
| +
|
| + // Verify that report upload took place and contained the incident,
|
| + // environment data, and download details.
|
| ExpectTestIncidentUploaded();
|
|
|
| - // Verify that the uploader was destroyed.
|
| + // Verify that the download finder and the uploader were destroyed.
|
| + ASSERT_TRUE(DownloadFinderDestroyed());
|
| ASSERT_TRUE(UploaderDestroyed());
|
| }
|
|
|
| @@ -361,20 +463,61 @@
|
| TEST_F(IncidentReportingServiceTest, NoSafeBrowsing) {
|
| // Create the profile, thereby causing the test to begin.
|
| CreateProfile(
|
| - "profile1", SAFE_BROWSING_OPT_OUT, ON_PROFILE_CREATION_ADD_INCIDENT);
|
| + "profile1", SAFE_BROWSING_OPT_OUT, ON_PROFILE_ADDITION_ADD_INCIDENT);
|
|
|
| // Let all tasks run.
|
| task_runner_->RunUntilIdle();
|
|
|
| // Verify that no report upload took place.
|
| - ExpectNoUpload();
|
| + AssertNoUpload();
|
| }
|
|
|
| +// Tests that no incident report is uploaded if there is no recent download.
|
| +TEST_F(IncidentReportingServiceTest, NoDownloadNoUpload) {
|
| + // Tell the fixture to return no downloads found.
|
| + SetCreateDownloadFinderAction(ON_CREATE_DOWNLOAD_FINDER_NO_DOWNLOADS);
|
| +
|
| + // Create the profile, thereby causing the test to begin.
|
| + CreateProfile(
|
| + "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT);
|
| +
|
| + // Let all tasks run.
|
| + task_runner_->RunUntilIdle();
|
| +
|
| + // Verify that the download finder was run but that no report upload took
|
| + // place.
|
| + EXPECT_TRUE(HasCreatedDownloadFinder());
|
| + AssertNoUpload();
|
| + EXPECT_TRUE(DownloadFinderDestroyed());
|
| +}
|
| +
|
| +// Tests that no incident report is uploaded if there is no recent download.
|
| +TEST_F(IncidentReportingServiceTest, NoProfilesNoUpload) {
|
| + // Tell the fixture to pretend there are no profiles eligible for finding
|
| + // downloads.
|
| + SetCreateDownloadFinderAction(ON_CREATE_DOWNLOAD_FINDER_NO_PROFILES);
|
| +
|
| + // Create the profile, thereby causing the test to begin.
|
| + CreateProfile(
|
| + "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT);
|
| +
|
| + // Let all tasks run.
|
| + task_runner_->RunUntilIdle();
|
| +
|
| + // Verify that the download finder was run but that no report upload took
|
| + // place.
|
| + EXPECT_TRUE(HasCreatedDownloadFinder());
|
| + AssertNoUpload();
|
| + // Although CreateDownloadFinder was called, no instance was returned so there
|
| + // is nothing to have been destroyed.
|
| + EXPECT_FALSE(DownloadFinderDestroyed());
|
| +}
|
| +
|
| // Tests that an incident added after upload is not uploaded again.
|
| TEST_F(IncidentReportingServiceTest, OnlyOneUpload) {
|
| // Create the profile, thereby causing the test to begin.
|
| Profile* profile = CreateProfile(
|
| - "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_CREATION_ADD_INCIDENT);
|
| + "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT);
|
|
|
| // Let all tasks run.
|
| task_runner_->RunUntilIdle();
|
| @@ -390,7 +533,7 @@
|
| task_runner_->RunUntilIdle();
|
|
|
| // Verify that no additional report upload took place.
|
| - ExpectNoUpload();
|
| + AssertNoUpload();
|
| }
|
|
|
| // Tests that the same incident added for two different profiles in sequence
|
| @@ -398,7 +541,7 @@
|
| TEST_F(IncidentReportingServiceTest, TwoProfilesTwoUploads) {
|
| // Create the profile, thereby causing the test to begin.
|
| CreateProfile(
|
| - "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_CREATION_ADD_INCIDENT);
|
| + "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT);
|
|
|
| // Let all tasks run.
|
| task_runner_->RunUntilIdle();
|
| @@ -407,9 +550,9 @@
|
| // environment data.
|
| ExpectTestIncidentUploaded();
|
|
|
| - // Create a second profile with its own incident on creation.
|
| + // Create a second profile with its own incident on addition.
|
| CreateProfile(
|
| - "profile2", SAFE_BROWSING_OPT_IN, ON_PROFILE_CREATION_ADD_INCIDENT);
|
| + "profile2", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT);
|
|
|
| // Let all tasks run.
|
| task_runner_->RunUntilIdle();
|
| @@ -423,7 +566,7 @@
|
| TEST_F(IncidentReportingServiceTest, ProfileDestroyedDuringUpload) {
|
| // Create a profile for which an incident will be added.
|
| Profile* profile = CreateProfile(
|
| - "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_CREATION_ADD_INCIDENT);
|
| + "profile1", SAFE_BROWSING_OPT_IN, ON_PROFILE_ADDITION_ADD_INCIDENT);
|
|
|
| // Hook up a callback to run when the upload is started that will post a task
|
| // to delete the profile. This task will run before the upload finishes.
|
|
|