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 // Sets local state for last time the software reporter sent logs to |days| | |
| 91 // days ago. | |
| 92 void SetLastTimeSentReport(int days) { | |
| 93 SetDateInLocalState(prefs::kSwReporterLastTimeSentReport, days); | |
| 94 } | |
| 95 | |
| 96 int64_t GetLastTimeSentReport() { | |
| 97 const PrefService* local_state = g_browser_process->local_state(); | |
| 98 DCHECK_NE(local_state, nullptr); | |
| 99 DCHECK(local_state->HasPrefPath(prefs::kSwReporterLastTimeSentReport)); | |
| 100 return local_state->GetInt64(prefs::kSwReporterLastTimeSentReport); | |
| 101 } | |
| 102 | |
| 103 void ExpectLastTimeSentReportNotSet() { | |
| 104 PrefService* local_state = g_browser_process->local_state(); | |
| 105 DCHECK_NE(local_state, nullptr); | |
| 106 EXPECT_FALSE( | |
| 107 local_state->HasPrefPath(prefs::kSwReporterLastTimeSentReport)); | |
| 108 } | |
| 109 | |
| 110 void ExpectLastReportSentInTheLastHour() { | |
|
robertshield
2016/09/07 14:32:09
note that this might have some false positives in
Fabio Tirelo
2016/09/07 16:48:56
Done.
| |
| 111 const PrefService* local_state = g_browser_process->local_state(); | |
| 112 DCHECK_NE(local_state, nullptr); | |
| 113 const base::Time now = base::Time::Now(); | |
| 114 const base::Time last_time_sent_logs = base::Time::FromInternalValue( | |
| 115 local_state->GetInt64(prefs::kSwReporterLastTimeSentReport)); | |
| 116 | |
| 117 // Checks if the last time set logs is set as no more than one hour ago, | |
|
robertshield
2016/09/07 14:32:09
s/set/sent/
Fabio Tirelo
2016/09/07 16:48:56
Done.
| |
| 118 // which should be enough time if the execution does not fail. | |
| 119 EXPECT_LT(now - base::TimeDelta::FromHours(1), last_time_sent_logs); | |
| 120 EXPECT_LT(last_time_sent_logs, now); | |
| 121 } | |
| 122 | |
| 74 void TestReporterLaunchCycle(int expected_launch_count, | 123 void TestReporterLaunchCycle(int expected_launch_count, |
| 75 const base::FilePath& expected_launch_path) { | 124 const base::FilePath& expected_launch_path) { |
| 76 // This test has an unfortunate amount of knowledge of the internals of | 125 // 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 | 126 // ReporterRunner, because it needs to pump the right message loops at the |
| 78 // right time so that all its internal messages are delivered. This | 127 // right time so that all its internal messages are delivered. This |
| 79 // function might need to be updated if the internals change. | 128 // function might need to be updated if the internals change. |
| 80 // | 129 // |
| 81 // The basic sequence is: | 130 // The basic sequence is: |
| 82 | 131 |
| 83 // 1. TryToRun kicks the whole thing off. If the reporter should not be | 132 // 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 | 192 |
| 144 // At this point another call to TryToRun should be scheduled, whether or | 193 // At this point another call to TryToRun should be scheduled, whether or |
| 145 // not LaunchAndWaitForExit was called. | 194 // not LaunchAndWaitForExit was called. |
| 146 ASSERT_TRUE(task_runner_->HasPendingTask()); | 195 ASSERT_TRUE(task_runner_->HasPendingTask()); |
| 147 | 196 |
| 148 // Make sure the flags are false for the next launch cycle test. | 197 // Make sure the flags are false for the next launch cycle test. |
| 149 ASSERT_FALSE(launch_ready_notified_); | 198 ASSERT_FALSE(launch_ready_notified_); |
| 150 reporter_done_notified_ = false; | 199 reporter_done_notified_ = false; |
| 151 } | 200 } |
| 152 | 201 |
| 202 // Expects |reporter_launch_parameters_| to contain exactly the command line | |
| 203 // switches specified in |expected_switches|. | |
| 204 void ExpectLoggingSwitches(const std::set<std::string>& expected_switches) { | |
| 205 const base::CommandLine::SwitchMap& invocation_switches = | |
| 206 reporter_launch_parameters_.command_line.GetSwitches(); | |
| 207 EXPECT_EQ(expected_switches.size(), invocation_switches.size()); | |
| 208 // Checks if all expected switches are in the invocation switches. It's not | |
| 209 // necessary to check if all invocation switches are expected, since we | |
| 210 // checked if both sets should have the same size. | |
| 211 for (const std::string& expected_switch : expected_switches) { | |
| 212 EXPECT_NE(invocation_switches.find(expected_switch), | |
| 213 invocation_switches.end()); | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 void EnableSBExtendedReporting() { | |
| 218 Browser* browser = chrome::FindLastActive(); | |
| 219 ASSERT_NE(browser, nullptr); | |
| 220 Profile* profile = browser->profile(); | |
| 221 ASSERT_NE(profile, nullptr); | |
| 222 profile->GetPrefs()->SetBoolean( | |
| 223 prefs::kSafeBrowsingExtendedReportingEnabled, true); | |
| 224 } | |
| 225 | |
| 153 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 226 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 154 bool prompt_trigger_called_ = false; | 227 bool prompt_trigger_called_ = false; |
| 155 int reporter_launch_count_ = 0; | 228 int reporter_launch_count_ = 0; |
| 156 SwReporterInvocation reporter_launch_parameters_; | 229 SwReporterInvocation reporter_launch_parameters_; |
| 157 int exit_code_to_report_ = kReporterFailureExitCode; | 230 int exit_code_to_report_ = kReporterFailureExitCode; |
| 158 bool launch_ready_notified_ = false; | 231 bool launch_ready_notified_ = false; |
| 159 bool reporter_done_notified_ = false; | 232 bool reporter_done_notified_ = false; |
| 160 }; | 233 }; |
| 161 | 234 |
| 162 } // namespace | 235 } // 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 | 372 // The reporter should launch again using path3, since enough time has |
| 300 // passed, even though the parameters haven't changed. | 373 // passed, even though the parameters haven't changed. |
| 301 { | 374 { |
| 302 SCOPED_TRACE("Run with same parameters"); | 375 SCOPED_TRACE("Run with same parameters"); |
| 303 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | 376 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); |
| 304 RunReporter(path3); | 377 RunReporter(path3); |
| 305 TestReporterLaunchCycle(3, path3); | 378 TestReporterLaunchCycle(3, path3); |
| 306 } | 379 } |
| 307 } | 380 } |
| 308 | 381 |
| 382 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_FeatureDisabled) { | |
| 383 exit_code_to_report_ = kSwReporterNothingFound; | |
| 384 base::test::ScopedFeatureList scoped_feature_list; | |
| 385 scoped_feature_list.InitAndDisableFeature( | |
| 386 kSwReporterExtendedSafeBrowsingFeature); | |
| 387 RunReporter(); | |
| 388 TestReporterLaunchCycle(1, base::FilePath()); | |
| 389 ExpectLoggingSwitches({/*expect no switches*/}); | |
| 390 ExpectLastTimeSentReportNotSet(); | |
| 391 } | |
| 392 | |
| 393 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_NoSBExtendedReporting) { | |
| 394 exit_code_to_report_ = kSwReporterNothingFound; | |
| 395 base::test::ScopedFeatureList scoped_feature_list; | |
| 396 scoped_feature_list.InitAndEnableFeature( | |
| 397 kSwReporterExtendedSafeBrowsingFeature); | |
| 398 RunReporter(); | |
| 399 TestReporterLaunchCycle(1, base::FilePath()); | |
| 400 ExpectLoggingSwitches({/*expect no switches*/}); | |
| 401 ExpectLastTimeSentReportNotSet(); | |
| 402 } | |
| 403 | |
| 404 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledFirstRun) { | |
| 405 exit_code_to_report_ = kSwReporterNothingFound; | |
| 406 base::test::ScopedFeatureList scoped_feature_list; | |
| 407 scoped_feature_list.InitAndEnableFeature( | |
| 408 kSwReporterExtendedSafeBrowsingFeature); | |
| 409 EnableSBExtendedReporting(); | |
| 410 // Note: don't set last time sent logs in the local state. | |
| 411 // SBER is enabled and there is no record in the local state of the last time | |
| 412 // logs have been sent, so we should send logs in this run. | |
| 413 RunReporter(); | |
| 414 TestReporterLaunchCycle(1, base::FilePath()); | |
| 415 ExpectLoggingSwitches(std::set<std::string>(std::begin(kExpectedSwitches), | |
| 416 std::end(kExpectedSwitches))); | |
| 417 ExpectLastReportSentInTheLastHour(); | |
| 418 } | |
| 419 | |
| 420 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledNoRecentLogging) { | |
| 421 exit_code_to_report_ = kSwReporterNothingFound; | |
| 422 base::test::ScopedFeatureList scoped_feature_list; | |
| 423 scoped_feature_list.InitAndEnableFeature( | |
| 424 kSwReporterExtendedSafeBrowsingFeature); | |
| 425 // SBER is enabled and last time logs were sent was more than | |
| 426 // |kDaysBetweenReporterLogsSent| day ago, so we should send logs in this run. | |
| 427 EnableSBExtendedReporting(); | |
| 428 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3); | |
| 429 RunReporter(); | |
| 430 TestReporterLaunchCycle(1, base::FilePath()); | |
| 431 ExpectLoggingSwitches(std::set<std::string>(std::begin(kExpectedSwitches), | |
| 432 std::end(kExpectedSwitches))); | |
| 433 ExpectLastReportSentInTheLastHour(); | |
| 434 } | |
| 435 | |
| 436 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledRecentlyLogged) { | |
| 437 exit_code_to_report_ = kSwReporterNothingFound; | |
| 438 base::test::ScopedFeatureList scoped_feature_list; | |
| 439 scoped_feature_list.InitAndEnableFeature( | |
| 440 kSwReporterExtendedSafeBrowsingFeature); | |
| 441 // SBER is enabled, but logs have been sent less than | |
| 442 // |kDaysBetweenReporterLogsSent| day ago, so we shouldn't send any logs in | |
| 443 // this run. | |
| 444 EnableSBExtendedReporting(); | |
| 445 SetLastTimeSentReport(kDaysBetweenReporterLogsSent - 1); | |
| 446 int64_t last_time_sent_logs = GetLastTimeSentReport(); | |
| 447 RunReporter(); | |
| 448 TestReporterLaunchCycle(1, base::FilePath()); | |
| 449 ExpectLoggingSwitches(std::set<std::string>{/*expect no switches*/}); | |
| 450 EXPECT_EQ(last_time_sent_logs, GetLastTimeSentReport()); | |
| 451 } | |
| 452 | |
| 309 } // namespace safe_browsing | 453 } // namespace safe_browsing |
| OLD | NEW |