Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/srt_fetcher_win.h" | 5 #include "chrome/browser/safe_browsing/srt_fetcher_win.h" |
| 6 | 6 |
| 7 #include <iterator> | |
| 7 #include <memory> | 8 #include <memory> |
| 9 #include <set> | |
| 8 | 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 11 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 12 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/test/scoped_feature_list.h" | |
| 13 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
| 14 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 15 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/safe_browsing/srt_client_info_win.h" | |
| 17 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/browser_finder.h" | |
| 23 #include "chrome/common/pref_names.h" | |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 24 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "components/component_updater/pref_names.h" | 25 #include "components/component_updater/pref_names.h" |
| 20 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 | 28 |
| 23 namespace safe_browsing { | 29 namespace safe_browsing { |
| 24 | 30 |
| 25 namespace { | 31 namespace { |
| 26 | 32 |
| 33 const char* const kExpectedSwitches[] = {kExtendedSafeBrowsingEnabledSwitch, | |
| 34 kChromeVersionSwitch, | |
| 35 kChromeChannelSwitch}; | |
| 36 | |
| 27 class SRTFetcherTest : public InProcessBrowserTest, | 37 class SRTFetcherTest : public InProcessBrowserTest, |
| 28 public SwReporterTestingDelegate { | 38 public SwReporterTestingDelegate { |
| 29 public: | 39 public: |
| 30 void SetUpInProcessBrowserTestFixture() override { | 40 void SetUpInProcessBrowserTestFixture() override { |
| 31 task_runner_ = new base::TestSimpleTaskRunner; | 41 task_runner_ = new base::TestSimpleTaskRunner; |
| 32 | 42 |
| 33 SetSwReporterTestingDelegate(this); | 43 SetSwReporterTestingDelegate(this); |
| 34 } | 44 } |
| 35 | 45 |
| 36 void TearDownInProcessBrowserTestFixture() override { | 46 void TearDownInProcessBrowserTestFixture() override { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 49 int LaunchReporter(const SwReporterInvocation& invocation) override { | 59 int LaunchReporter(const SwReporterInvocation& invocation) override { |
| 50 ++reporter_launch_count_; | 60 ++reporter_launch_count_; |
| 51 reporter_launch_parameters_ = invocation; | 61 reporter_launch_parameters_ = invocation; |
| 52 return exit_code_to_report_; | 62 return exit_code_to_report_; |
| 53 } | 63 } |
| 54 | 64 |
| 55 void NotifyLaunchReady() override { launch_ready_notified_ = true; } | 65 void NotifyLaunchReady() override { launch_ready_notified_ = true; } |
| 56 | 66 |
| 57 void NotifyReporterDone() override { reporter_done_notified_ = true; } | 67 void NotifyReporterDone() override { reporter_done_notified_ = true; } |
| 58 | 68 |
| 59 void SetDaysSinceLastReport(int days) { | 69 // Sets |path| in the local state to a date corresponding to |days| days ago. |
| 70 void SetDateInLocalState(const std::string& path, int days) { | |
| 60 PrefService* local_state = g_browser_process->local_state(); | 71 PrefService* local_state = g_browser_process->local_state(); |
| 61 local_state->SetInt64(prefs::kSwReporterLastTimeTriggered, | 72 DCHECK_NE(local_state, nullptr); |
| 73 local_state->SetInt64(path, | |
| 62 (base::Time::Now() - base::TimeDelta::FromDays(days)) | 74 (base::Time::Now() - base::TimeDelta::FromDays(days)) |
| 63 .ToInternalValue()); | 75 .ToInternalValue()); |
| 64 } | 76 } |
| 65 | 77 |
| 78 void SetDaysSinceLastReport(int days) { | |
| 79 SetDateInLocalState(prefs::kSwReporterLastTimeTriggered, days); | |
| 80 } | |
| 81 | |
| 66 void ExpectToRunAgain(int days) { | 82 void ExpectToRunAgain(int days) { |
| 67 ASSERT_TRUE(task_runner_->HasPendingTask()); | 83 ASSERT_TRUE(task_runner_->HasPendingTask()); |
| 68 EXPECT_LE(task_runner_->NextPendingTaskDelay(), | 84 EXPECT_LE(task_runner_->NextPendingTaskDelay(), |
| 69 base::TimeDelta::FromDays(days)); | 85 base::TimeDelta::FromDays(days)); |
| 70 EXPECT_GT(task_runner_->NextPendingTaskDelay(), | 86 EXPECT_GT(task_runner_->NextPendingTaskDelay(), |
| 71 base::TimeDelta::FromDays(days) - base::TimeDelta::FromHours(1)); | 87 base::TimeDelta::FromDays(days) - base::TimeDelta::FromHours(1)); |
| 72 } | 88 } |
| 73 | 89 |
| 90 // Clears local state for last time the software reporter sent logs to |days| | |
| 91 // days ago. This prevents potential false positives that could arise from | |
| 92 // state not properly cleaned between successive tests. | |
| 93 void ClearLastTimeSentReport() { | |
| 94 PrefService* local_state = g_browser_process->local_state(); | |
| 95 DCHECK_NE(local_state, nullptr); | |
| 96 local_state->ClearPref(prefs::kSwReporterLastTimeSentReport); | |
| 97 } | |
| 98 | |
| 99 // Sets local state for last time the software reporter sent logs to |days| | |
| 100 // days ago. | |
| 101 void SetLastTimeSentReport(int days) { | |
| 102 SetDateInLocalState(prefs::kSwReporterLastTimeSentReport, days); | |
| 103 } | |
| 104 | |
| 105 int64_t GetLastTimeSentReport() { | |
| 106 const PrefService* local_state = g_browser_process->local_state(); | |
| 107 DCHECK_NE(local_state, nullptr); | |
| 108 DCHECK(local_state->HasPrefPath(prefs::kSwReporterLastTimeSentReport)); | |
| 109 return local_state->GetInt64(prefs::kSwReporterLastTimeSentReport); | |
| 110 } | |
| 111 | |
| 112 void ExpectLastTimeSentReportNotSet() { | |
| 113 PrefService* local_state = g_browser_process->local_state(); | |
| 114 DCHECK_NE(local_state, nullptr); | |
| 115 EXPECT_FALSE( | |
| 116 local_state->HasPrefPath(prefs::kSwReporterLastTimeSentReport)); | |
| 117 } | |
| 118 | |
| 119 void ExpectLastReportSentInTheLastHour() { | |
| 120 const PrefService* local_state = g_browser_process->local_state(); | |
| 121 DCHECK_NE(local_state, nullptr); | |
| 122 const base::Time now = base::Time::Now(); | |
| 123 const base::Time last_time_sent_logs = base::Time::FromInternalValue( | |
| 124 local_state->GetInt64(prefs::kSwReporterLastTimeSentReport)); | |
| 125 | |
| 126 // Checks if the last time sent logs is set as no more than one hour ago, | |
| 127 // which should be enough time if the execution does not fail. | |
| 128 EXPECT_LT(now - base::TimeDelta::FromHours(1), last_time_sent_logs); | |
| 129 EXPECT_LT(last_time_sent_logs, now); | |
| 130 } | |
| 131 | |
| 74 void TestReporterLaunchCycle(int expected_launch_count, | 132 void TestReporterLaunchCycle(int expected_launch_count, |
| 75 const base::FilePath& expected_launch_path) { | 133 const base::FilePath& expected_launch_path) { |
| 76 // This test has an unfortunate amount of knowledge of the internals of | 134 // This test has an unfortunate amount of knowledge of the internals of |
| 77 // ReporterRunner, because it needs to pump the right message loops at the | 135 // ReporterRunner, because it needs to pump the right message loops at the |
| 78 // right time so that all its internal messages are delivered. This | 136 // right time so that all its internal messages are delivered. This |
| 79 // function might need to be updated if the internals change. | 137 // function might need to be updated if the internals change. |
| 80 // | 138 // |
| 81 // The basic sequence is: | 139 // The basic sequence is: |
| 82 | 140 |
| 83 // 1. TryToRun kicks the whole thing off. If the reporter should not be | 141 // 1. TryToRun kicks the whole thing off. If the reporter should not be |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 | 201 |
| 144 // At this point another call to TryToRun should be scheduled, whether or | 202 // At this point another call to TryToRun should be scheduled, whether or |
| 145 // not LaunchAndWaitForExit was called. | 203 // not LaunchAndWaitForExit was called. |
| 146 ASSERT_TRUE(task_runner_->HasPendingTask()); | 204 ASSERT_TRUE(task_runner_->HasPendingTask()); |
| 147 | 205 |
| 148 // Make sure the flags are false for the next launch cycle test. | 206 // Make sure the flags are false for the next launch cycle test. |
| 149 ASSERT_FALSE(launch_ready_notified_); | 207 ASSERT_FALSE(launch_ready_notified_); |
| 150 reporter_done_notified_ = false; | 208 reporter_done_notified_ = false; |
| 151 } | 209 } |
| 152 | 210 |
| 211 // Expects |reporter_launch_parameters_| to contain exactly the command line | |
| 212 // switches specified in |expected_switches|. | |
| 213 void ExpectLoggingSwitches(const std::set<std::string>& expected_switches) { | |
| 214 const base::CommandLine::SwitchMap& invocation_switches = | |
| 215 reporter_launch_parameters_.command_line.GetSwitches(); | |
| 216 EXPECT_EQ(expected_switches.size(), invocation_switches.size()); | |
| 217 // Checks if all expected switches are in the invocation switches. It's not | |
| 218 // necessary to check if all invocation switches are expected, since we | |
| 219 // checked if both sets should have the same size. | |
| 220 for (const std::string& expected_switch : expected_switches) { | |
| 221 EXPECT_NE(invocation_switches.find(expected_switch), | |
| 222 invocation_switches.end()); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 void EnableSBExtendedReporting() { | |
| 227 Browser* browser = chrome::FindLastActive(); | |
| 228 ASSERT_NE(browser, nullptr); | |
| 229 Profile* profile = browser->profile(); | |
| 230 ASSERT_NE(profile, nullptr); | |
| 231 profile->GetPrefs()->SetBoolean( | |
| 232 prefs::kSafeBrowsingExtendedReportingEnabled, true); | |
| 233 } | |
| 234 | |
| 153 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 235 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 154 bool prompt_trigger_called_ = false; | 236 bool prompt_trigger_called_ = false; |
| 155 int reporter_launch_count_ = 0; | 237 int reporter_launch_count_ = 0; |
| 156 SwReporterInvocation reporter_launch_parameters_; | 238 SwReporterInvocation reporter_launch_parameters_; |
| 157 int exit_code_to_report_ = kReporterFailureExitCode; | 239 int exit_code_to_report_ = kReporterFailureExitCode; |
| 158 bool launch_ready_notified_ = false; | 240 bool launch_ready_notified_ = false; |
| 159 bool reporter_done_notified_ = false; | 241 bool reporter_done_notified_ = false; |
| 160 }; | 242 }; |
| 161 | 243 |
| 162 } // namespace | 244 } // namespace |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 // The reporter should launch again using path3, since enough time has | 381 // The reporter should launch again using path3, since enough time has |
| 300 // passed, even though the parameters haven't changed. | 382 // passed, even though the parameters haven't changed. |
| 301 { | 383 { |
| 302 SCOPED_TRACE("Run with same parameters"); | 384 SCOPED_TRACE("Run with same parameters"); |
| 303 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | 385 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); |
| 304 RunReporter(path3); | 386 RunReporter(path3); |
| 305 TestReporterLaunchCycle(3, path3); | 387 TestReporterLaunchCycle(3, path3); |
| 306 } | 388 } |
| 307 } | 389 } |
| 308 | 390 |
| 391 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_FeatureDisabled) { | |
| 392 exit_code_to_report_ = kSwReporterNothingFound; | |
| 393 base::test::ScopedFeatureList scoped_feature_list; | |
| 394 scoped_feature_list.InitAndDisableFeature( | |
| 395 kSwReporterExtendedSafeBrowsingFeature); | |
| 396 ClearLastTimeSentReport(); | |
|
robertshield
2016/09/07 19:21:37
nit: Could you move this into the test harness Set
ftirelo
2016/09/07 20:31:11
Done.
| |
| 397 RunReporter(); | |
| 398 TestReporterLaunchCycle(1, base::FilePath()); | |
| 399 ExpectLoggingSwitches({/*expect no switches*/}); | |
| 400 ExpectLastTimeSentReportNotSet(); | |
| 401 } | |
| 402 | |
| 403 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_NoSBExtendedReporting) { | |
| 404 exit_code_to_report_ = kSwReporterNothingFound; | |
| 405 base::test::ScopedFeatureList scoped_feature_list; | |
| 406 scoped_feature_list.InitAndEnableFeature( | |
| 407 kSwReporterExtendedSafeBrowsingFeature); | |
| 408 ClearLastTimeSentReport(); | |
| 409 RunReporter(); | |
| 410 TestReporterLaunchCycle(1, base::FilePath()); | |
| 411 ExpectLoggingSwitches({/*expect no switches*/}); | |
| 412 ExpectLastTimeSentReportNotSet(); | |
| 413 } | |
| 414 | |
| 415 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledFirstRun) { | |
| 416 exit_code_to_report_ = kSwReporterNothingFound; | |
| 417 base::test::ScopedFeatureList scoped_feature_list; | |
| 418 scoped_feature_list.InitAndEnableFeature( | |
| 419 kSwReporterExtendedSafeBrowsingFeature); | |
| 420 EnableSBExtendedReporting(); | |
| 421 // Note: don't set last time sent logs in the local state. | |
| 422 // SBER is enabled and there is no record in the local state of the last time | |
| 423 // logs have been sent, so we should send logs in this run. | |
| 424 ClearLastTimeSentReport(); | |
| 425 RunReporter(); | |
| 426 TestReporterLaunchCycle(1, base::FilePath()); | |
| 427 ExpectLoggingSwitches(std::set<std::string>(std::begin(kExpectedSwitches), | |
| 428 std::end(kExpectedSwitches))); | |
| 429 ExpectLastReportSentInTheLastHour(); | |
| 430 } | |
| 431 | |
| 432 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledNoRecentLogging) { | |
| 433 exit_code_to_report_ = kSwReporterNothingFound; | |
| 434 base::test::ScopedFeatureList scoped_feature_list; | |
| 435 scoped_feature_list.InitAndEnableFeature( | |
| 436 kSwReporterExtendedSafeBrowsingFeature); | |
| 437 // SBER is enabled and last time logs were sent was more than | |
| 438 // |kDaysBetweenReporterLogsSent| day ago, so we should send logs in this run. | |
| 439 EnableSBExtendedReporting(); | |
| 440 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3); | |
| 441 RunReporter(); | |
| 442 TestReporterLaunchCycle(1, base::FilePath()); | |
| 443 ExpectLoggingSwitches(std::set<std::string>(std::begin(kExpectedSwitches), | |
| 444 std::end(kExpectedSwitches))); | |
| 445 ExpectLastReportSentInTheLastHour(); | |
| 446 } | |
| 447 | |
| 448 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledRecentlyLogged) { | |
| 449 exit_code_to_report_ = kSwReporterNothingFound; | |
| 450 base::test::ScopedFeatureList scoped_feature_list; | |
| 451 scoped_feature_list.InitAndEnableFeature( | |
| 452 kSwReporterExtendedSafeBrowsingFeature); | |
| 453 // SBER is enabled, but logs have been sent less than | |
| 454 // |kDaysBetweenReporterLogsSent| day ago, so we shouldn't send any logs in | |
| 455 // this run. | |
| 456 EnableSBExtendedReporting(); | |
| 457 SetLastTimeSentReport(kDaysBetweenReporterLogsSent - 1); | |
| 458 int64_t last_time_sent_logs = GetLastTimeSentReport(); | |
| 459 RunReporter(); | |
| 460 TestReporterLaunchCycle(1, base::FilePath()); | |
| 461 ExpectLoggingSwitches(std::set<std::string>{/*expect no switches*/}); | |
| 462 EXPECT_EQ(last_time_sent_logs, GetLastTimeSentReport()); | |
| 463 } | |
| 464 | |
| 309 } // namespace safe_browsing | 465 } // namespace safe_browsing |
| OLD | NEW |