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