| Index: chrome/browser/safe_browsing/incident_reporting_service_unittest.cc
|
| diff --git a/chrome/browser/safe_browsing/incident_reporting_service_unittest.cc b/chrome/browser/safe_browsing/incident_reporting_service_unittest.cc
|
| index 58bee748f7184ef58f8b34a5581497eb19b3e655..40dd948937885663ff6a2bc2cbb7ecc27fd57340 100644
|
| --- a/chrome/browser/safe_browsing/incident_reporting_service_unittest.cc
|
| +++ b/chrome/browser/safe_browsing/incident_reporting_service_unittest.cc
|
| @@ -12,8 +12,14 @@
|
| #include "base/test/test_simple_task_runner.h"
|
| #include "base/thread_task_runner_handle.h"
|
| #include "base/threading/thread_local.h"
|
| +#include "chrome/browser/prefs/browser_prefs.h"
|
| #include "chrome/browser/safe_browsing/incident_report_uploader.h"
|
| +#include "chrome/common/pref_names.h"
|
| #include "chrome/common/safe_browsing/csd.pb.h"
|
| +#include "chrome/test/base/scoped_testing_local_state.h"
|
| +#include "chrome/test/base/testing_browser_process.h"
|
| +#include "chrome/test/base/testing_pref_service_syncable.h"
|
| +#include "chrome/test/base/testing_profile.h"
|
| #include "net/url_request/url_request_context_getter.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| @@ -27,6 +33,8 @@ class IncidentReportingServiceTest : public testing::Test {
|
| class TestIncidentReportingService
|
| : public safe_browsing::IncidentReportingService {
|
| public:
|
| + typedef base::Callback<void(Profile*)> PreProfileCreateCallback;
|
| +
|
| typedef base::Callback<
|
| void(safe_browsing::ClientIncidentReport_EnvironmentData*)>
|
| CollectEnvironmentCallback;
|
| @@ -37,9 +45,11 @@ class IncidentReportingServiceTest : public testing::Test {
|
|
|
| TestIncidentReportingService(
|
| const scoped_refptr<base::TaskRunner>& task_runner,
|
| + const PreProfileCreateCallback& pre_profile_create_callback,
|
| const CollectEnvironmentCallback& collect_environment_callback,
|
| const StartUploadCallback& start_upload_callback)
|
| : IncidentReportingService(NULL, NULL),
|
| + pre_profile_create_callback_(pre_profile_create_callback),
|
| collect_environment_callback_(collect_environment_callback),
|
| start_upload_callback_(start_upload_callback) {
|
| SetCollectEnvironmentHook(&CollectEnvironmentData, task_runner);
|
| @@ -49,6 +59,11 @@ class IncidentReportingServiceTest : public testing::Test {
|
| 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 scoped_ptr<safe_browsing::IncidentReportUploader> StartReportUpload(
|
| const safe_browsing::IncidentReportUploader::OnResultCallback& callback,
|
| const scoped_refptr<net::URLRequestContextGetter>&
|
| @@ -70,37 +85,87 @@ class IncidentReportingServiceTest : public testing::Test {
|
| static base::LazyInstance<base::ThreadLocalPointer<
|
| TestIncidentReportingService> >::Leaky test_instance_;
|
|
|
| + PreProfileCreateCallback pre_profile_create_callback_;
|
| CollectEnvironmentCallback collect_environment_callback_;
|
| StartUploadCallback start_upload_callback_;
|
| };
|
|
|
| + static const int64 kIncidentTimeMsec;
|
| static const char kFakeOsName[];
|
|
|
| IncidentReportingServiceTest()
|
| : task_runner_(new base::TestSimpleTaskRunner),
|
| thread_task_runner_handle_(task_runner_),
|
| + local_state_(TestingBrowserProcess::GetGlobal()),
|
| instance_(new TestIncidentReportingService(
|
| task_runner_,
|
| + base::Bind(&IncidentReportingServiceTest::PreProfileCreate,
|
| + base::Unretained(this)),
|
| base::Bind(&IncidentReportingServiceTest::CollectEnvironmentData,
|
| base::Unretained(this)),
|
| base::Bind(&IncidentReportingServiceTest::StartUpload,
|
| base::Unretained(this)))),
|
| upload_result_(safe_browsing::IncidentReportUploader::UPLOAD_SUCCESS),
|
| environment_collected_(),
|
| - uploader_destroyed_() {
|
| - instance_->SetEnabled(true);
|
| + uploader_destroyed_() {}
|
| +
|
| + // Begins the test by creating a profile. An incident will be created within
|
| + // PreProfileCreate. Tasks are run to allow the service to operate to
|
| + // completion.
|
| + void CreateProfileAndRunTest(bool safe_browsing_enabled) {
|
| + // Create prefs for the profile with safe browsing enabled or not.
|
| + scoped_ptr<TestingPrefServiceSyncable> prefs(
|
| + new TestingPrefServiceSyncable);
|
| + chrome::RegisterUserProfilePrefs(prefs->registry());
|
| + prefs->SetBoolean(prefs::kSafeBrowsingEnabled, safe_browsing_enabled);
|
| +
|
| + // Build the test profile (PreProfileCreate will be called).
|
| + TestingProfile::Builder builder;
|
| + builder.SetPrefService(prefs.PassAs<PrefServiceSyncable>());
|
| + testing_profile_ = builder.Build().Pass();
|
| +
|
| + // Let all tasks run.
|
| + task_runner_->RunUntilIdle();
|
| + }
|
| +
|
| + // Returns an incident suitable for testing.
|
| + scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData>
|
| + MakeTestIncident() {
|
| + scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident(
|
| + new safe_browsing::ClientIncidentReport_IncidentData());
|
| + incident->set_incident_time_msec(kIncidentTimeMsec);
|
| + incident->mutable_tracked_preference();
|
| + return incident.Pass();
|
| + }
|
| +
|
| + // Confirms that the test incident was uploaded by the service.
|
| + void ExpectTestIncidentUploaded() {
|
| + ASSERT_TRUE(uploaded_report_);
|
| + ASSERT_EQ(1, uploaded_report_->incident_size());
|
| + ASSERT_TRUE(uploaded_report_->incident(0).has_incident_time_msec());
|
| + ASSERT_EQ(kIncidentTimeMsec,
|
| + uploaded_report_->incident(0).incident_time_msec());
|
| + ASSERT_TRUE(uploaded_report_->has_environment());
|
| + ASSERT_TRUE(uploaded_report_->environment().has_os());
|
| + ASSERT_TRUE(uploaded_report_->environment().os().has_os_name());
|
| + ASSERT_EQ(std::string(kFakeOsName),
|
| + uploaded_report_->environment().os().os_name());
|
| }
|
|
|
| + void ExpectNoUpload() { ASSERT_FALSE(uploaded_report_); }
|
| +
|
| bool HasCollectedEnvironmentData() const { return environment_collected_; }
|
| bool UploaderDestroyed() const { return uploader_destroyed_; }
|
|
|
| scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
|
| base::ThreadTaskRunnerHandle thread_task_runner_handle_;
|
| + ScopedTestingLocalState local_state_;
|
| scoped_ptr<safe_browsing::IncidentReportingService> instance_;
|
| safe_browsing::IncidentReportUploader::Result upload_result_;
|
| bool environment_collected_;
|
| scoped_ptr<safe_browsing::ClientIncidentReport> uploaded_report_;
|
| bool uploader_destroyed_;
|
| + scoped_ptr<TestingProfile> testing_profile_;
|
|
|
| private:
|
| // A fake IncidentReportUploader that posts a task to provide a given response
|
| @@ -136,6 +201,17 @@ class IncidentReportingServiceTest : public testing::Test {
|
| DISALLOW_COPY_AND_ASSIGN(FakeUploader);
|
| };
|
|
|
| + // A callback run by the test fixture when a profile is created. An incident
|
| + // is added.
|
| + void PreProfileCreate(Profile* profile) {
|
| + // The instance must have already been created.
|
| + ASSERT_TRUE(instance_);
|
| + // Add a test incident to the service.
|
| + instance_->GetAddIncidentCallback(profile).Run(MakeTestIncident().Pass());
|
| + }
|
| +
|
| + // A fake CollectEnvironmentData implementation invoked by the service during
|
| + // operation.
|
| void CollectEnvironmentData(
|
| safe_browsing::ClientIncidentReport_EnvironmentData* data) {
|
| ASSERT_NE(
|
| @@ -145,6 +221,7 @@ class IncidentReportingServiceTest : public testing::Test {
|
| environment_collected_ = true;
|
| }
|
|
|
| + // A fake StartUpload implementation invoked by the service during operation.
|
| scoped_ptr<safe_browsing::IncidentReportUploader> StartUpload(
|
| const safe_browsing::IncidentReportUploader::OnResultCallback& callback,
|
| const safe_browsing::ClientIncidentReport& report) {
|
| @@ -167,44 +244,36 @@ base::LazyInstance<base::ThreadLocalPointer<
|
| LAZY_INSTANCE_INITIALIZER;
|
|
|
| // static
|
| +const int64 IncidentReportingServiceTest::kIncidentTimeMsec = 47LL;
|
| const char IncidentReportingServiceTest::kFakeOsName[] = "fakedows";
|
|
|
| +// Tests that an incident added during profile initialization when safe browsing
|
| +// is on is uploaded.
|
| TEST_F(IncidentReportingServiceTest, AddIncident) {
|
| - // Make up a dummy incident.
|
| - const int64 kIncidentTimeMsec = 47LL;
|
| - scoped_ptr<safe_browsing::ClientIncidentReport_IncidentData> incident(
|
| - new safe_browsing::ClientIncidentReport_IncidentData());
|
| - incident->set_incident_time_msec(kIncidentTimeMsec);
|
| -
|
| - // Add it to the service.
|
| - instance_->GetAddIncidentCallback().Run(incident.Pass());
|
| -
|
| - // Let all tasks run.
|
| - task_runner_->RunUntilIdle();
|
| + // Create the profile, thereby causing the test to begin.
|
| + CreateProfileAndRunTest(true);
|
|
|
| // Verify that environment collection took place.
|
| EXPECT_TRUE(HasCollectedEnvironmentData());
|
|
|
| // Verify that report upload took place and contained the incident and
|
| // environment data.
|
| - ASSERT_TRUE(uploaded_report_);
|
| - ASSERT_EQ(1, uploaded_report_->incident_size());
|
| - ASSERT_TRUE(uploaded_report_->incident(0).has_incident_time_msec());
|
| - ASSERT_EQ(kIncidentTimeMsec,
|
| - uploaded_report_->incident(0).incident_time_msec());
|
| - ASSERT_TRUE(uploaded_report_->has_environment());
|
| - ASSERT_TRUE(uploaded_report_->environment().has_os());
|
| - ASSERT_TRUE(uploaded_report_->environment().os().has_os_name());
|
| - ASSERT_EQ(std::string(kFakeOsName),
|
| - uploaded_report_->environment().os().os_name());
|
| + ExpectTestIncidentUploaded();
|
|
|
| // Verify that the uploader was destroyed.
|
| ASSERT_TRUE(UploaderDestroyed());
|
| }
|
|
|
| -// Nothing happens when disabled
|
| -// Nothing is sent when disabled before/after environment collection
|
| -// Duplicate incidents are stripped
|
| +// Tests that an incident added during profile initialization when safe browsing
|
| +// is off is not uploaded.
|
| +TEST_F(IncidentReportingServiceTest, NoSafeBrowsing) {
|
| + // Create the profile, thereby causing the test to begin.
|
| + CreateProfileAndRunTest(false);
|
| +
|
| + // Verify that no report upload took place.
|
| + ExpectNoUpload();
|
| +}
|
| +
|
| // Parallel uploads
|
| // Shutdown during processing
|
| // environment colection taking longer than incident delay timer
|
|
|