Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: chrome/browser/safe_browsing/srt_fetcher_browsertest_win.cc

Issue 2286743004: Sends switches to the Software Reporter to enable matching data collection. (Closed)
Patch Set: Rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/test/scoped_feature_list.h"
13 #include "base/test/test_simple_task_runner.h" 14 #include "base/test/test_simple_task_runner.h"
14 #include "base/time/time.h" 15 #include "base/time/time.h"
15 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/safe_browsing/srt_client_info_win.h"
17 #include "chrome/browser/ui/browser.h" 19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_finder.h"
21 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/in_process_browser_test.h" 22 #include "chrome/test/base/in_process_browser_test.h"
19 #include "components/component_updater/pref_names.h" 23 #include "components/component_updater/pref_names.h"
20 #include "components/prefs/pref_service.h" 24 #include "components/prefs/pref_service.h"
21 #include "content/public/test/test_browser_thread_bundle.h" 25 #include "content/public/test/test_browser_thread_bundle.h"
22 26
23 namespace safe_browsing { 27 namespace safe_browsing {
24 28
25 namespace { 29 namespace {
26 30
27 class SRTFetcherTest : public InProcessBrowserTest, 31 class SRTFetcherTest : public InProcessBrowserTest,
(...skipping 21 matching lines...) Expand all
49 int LaunchReporter(const SwReporterInvocation& invocation) override { 53 int LaunchReporter(const SwReporterInvocation& invocation) override {
50 ++reporter_launch_count_; 54 ++reporter_launch_count_;
51 reporter_launch_parameters_ = invocation; 55 reporter_launch_parameters_ = invocation;
52 return exit_code_to_report_; 56 return exit_code_to_report_;
53 } 57 }
54 58
55 void NotifyLaunchReady() override { launch_ready_notified_ = true; } 59 void NotifyLaunchReady() override { launch_ready_notified_ = true; }
56 60
57 void NotifyReporterDone() override { reporter_done_notified_ = true; } 61 void NotifyReporterDone() override { reporter_done_notified_ = true; }
58 62
59 void SetDaysSinceLastReport(int days) { 63 // Sets |path| in the local state to a date corresponding to |days| days ago.
64 void SetDateInLocalState(const std::string& path, int days) {
60 PrefService* local_state = g_browser_process->local_state(); 65 PrefService* local_state = g_browser_process->local_state();
61 local_state->SetInt64(prefs::kSwReporterLastTimeTriggered, 66 DCHECK_NE(local_state, nullptr);
67 local_state->SetInt64(path,
62 (base::Time::Now() - base::TimeDelta::FromDays(days)) 68 (base::Time::Now() - base::TimeDelta::FromDays(days))
63 .ToInternalValue()); 69 .ToInternalValue());
64 } 70 }
65 71
72 void SetDaysSinceLastReport(int days) {
73 SetDateInLocalState(prefs::kSwReporterLastTimeTriggered, days);
74 }
75
66 void ExpectToRunAgain(int days) { 76 void ExpectToRunAgain(int days) {
67 ASSERT_TRUE(task_runner_->HasPendingTask()); 77 ASSERT_TRUE(task_runner_->HasPendingTask());
68 EXPECT_LE(task_runner_->NextPendingTaskDelay(), 78 EXPECT_LE(task_runner_->NextPendingTaskDelay(),
69 base::TimeDelta::FromDays(days)); 79 base::TimeDelta::FromDays(days));
70 EXPECT_GT(task_runner_->NextPendingTaskDelay(), 80 EXPECT_GT(task_runner_->NextPendingTaskDelay(),
71 base::TimeDelta::FromDays(days) - base::TimeDelta::FromHours(1)); 81 base::TimeDelta::FromDays(days) - base::TimeDelta::FromHours(1));
72 } 82 }
73 83
84 // Sets local state for last time the software reporter sent logs to |days|
85 // days ago.
86 void SetLastTimeSentReport(int days) {
87 SetDateInLocalState(prefs::kSwReporterLastTimeSentReport, days);
88 }
89
90 int64_t GetLastTimeSentReport() {
91 const PrefService* local_state = g_browser_process->local_state();
92 DCHECK_NE(local_state, nullptr);
93 DCHECK(local_state->HasPrefPath(prefs::kSwReporterLastTimeSentReport));
94 return local_state->GetInt64(prefs::kSwReporterLastTimeSentReport);
95 }
96
97 void ExpectLastTimeSentReportNotSet() {
98 PrefService* local_state = g_browser_process->local_state();
99 DCHECK_NE(local_state, nullptr);
100 EXPECT_FALSE(
101 local_state->HasPrefPath(prefs::kSwReporterLastTimeSentReport));
102 }
103
104 void ExpectLastReportSentAsToday() {
105 const PrefService* local_state = g_browser_process->local_state();
106 DCHECK_NE(local_state, nullptr);
107 const base::Time now = base::Time::Now();
108 const base::Time last_time_sent_logs = base::Time::FromInternalValue(
109 local_state->GetInt64(prefs::kSwReporterLastTimeSentReport));
110
111 // Checks if the last time set logs is set as no more than one hour ago,
112 // which should be enough time if the execution does not fail.
113 EXPECT_LT(now - base::TimeDelta::FromHours(1), last_time_sent_logs);
csharp 2016/09/02 21:16:25 This doesn't seem to quite line up with the functi
ftirelo 2016/09/06 16:37:43 Done.
114 EXPECT_LT(last_time_sent_logs, now);
115 }
116
74 void TestReporterLaunchCycle(int expected_launch_count, 117 void TestReporterLaunchCycle(int expected_launch_count,
75 const base::FilePath& expected_launch_path) { 118 const base::FilePath& expected_launch_path) {
76 // This test has an unfortunate amount of knowledge of the internals of 119 // 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 120 // ReporterRunner, because it needs to pump the right message loops at the
78 // right time so that all its internal messages are delivered. This 121 // right time so that all its internal messages are delivered. This
79 // function might need to be updated if the internals change. 122 // function might need to be updated if the internals change.
80 // 123 //
81 // The basic sequence is: 124 // The basic sequence is:
82 125
83 // 1. TryToRun kicks the whole thing off. If the reporter should not be 126 // 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
143 186
144 // At this point another call to TryToRun should be scheduled, whether or 187 // At this point another call to TryToRun should be scheduled, whether or
145 // not LaunchAndWaitForExit was called. 188 // not LaunchAndWaitForExit was called.
146 ASSERT_TRUE(task_runner_->HasPendingTask()); 189 ASSERT_TRUE(task_runner_->HasPendingTask());
147 190
148 // Make sure the flags are false for the next launch cycle test. 191 // Make sure the flags are false for the next launch cycle test.
149 ASSERT_FALSE(launch_ready_notified_); 192 ASSERT_FALSE(launch_ready_notified_);
150 reporter_done_notified_ = false; 193 reporter_done_notified_ = false;
151 } 194 }
152 195
196 // Expects |reporter_launch_parameters_| contain exactly the command line
csharp 2016/09/02 21:16:25 nit: contain -> to contain(?)
ftirelo 2016/09/06 16:37:43 Done.
197 // switches specified in |expected_switches|.
198 void ExpectLoggingSwitches(const std::set<std::string>& expected_switches) {
199 const base::CommandLine::SwitchMap& invocation_switches =
csharp 2016/09/02 21:16:25 What about using std::set_symmetric_difference
ftirelo 2016/09/06 16:37:43 std::set_symmetric_different relies on the order o
200 reporter_launch_parameters_.command_line.GetSwitches();
201 // Checks if all expected switches are in the invocation switches.
202 for (const std::string& expected_switch : expected_switches) {
203 EXPECT_NE(invocation_switches.find(expected_switch),
204 invocation_switches.end());
205 }
206 // Checks if all invocation switches are in the expected switches.
207 for (const auto& invocation_switch : invocation_switches) {
208 EXPECT_NE(expected_switches.find(invocation_switch.first),
209 expected_switches.end());
210 }
211 }
212
213 void EnableSBExtendedReporting() {
214 Browser* browser = chrome::FindLastActive();
215 ASSERT_NE(browser, nullptr);
216 Profile* profile = browser->profile();
217 ASSERT_NE(profile, nullptr);
218 profile->GetPrefs()->SetBoolean(
219 prefs::kSafeBrowsingExtendedReportingEnabled, true);
220 }
221
153 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 222 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
154 bool prompt_trigger_called_ = false; 223 bool prompt_trigger_called_ = false;
155 int reporter_launch_count_ = 0; 224 int reporter_launch_count_ = 0;
156 SwReporterInvocation reporter_launch_parameters_; 225 SwReporterInvocation reporter_launch_parameters_;
157 int exit_code_to_report_ = kReporterFailureExitCode; 226 int exit_code_to_report_ = kReporterFailureExitCode;
158 bool launch_ready_notified_ = false; 227 bool launch_ready_notified_ = false;
159 bool reporter_done_notified_ = false; 228 bool reporter_done_notified_ = false;
160 }; 229 };
161 230
162 } // namespace 231 } // namespace
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 // The reporter should launch again using path3, since enough time has 368 // The reporter should launch again using path3, since enough time has
300 // passed, even though the parameters haven't changed. 369 // passed, even though the parameters haven't changed.
301 { 370 {
302 SCOPED_TRACE("Run with same parameters"); 371 SCOPED_TRACE("Run with same parameters");
303 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); 372 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns);
304 RunReporter(path3); 373 RunReporter(path3);
305 TestReporterLaunchCycle(3, path3); 374 TestReporterLaunchCycle(3, path3);
306 } 375 }
307 } 376 }
308 377
378 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_FeatureDisabled) {
379 exit_code_to_report_ = kSwReporterNothingFound;
380 base::test::ScopedFeatureList scoped_feature_list;
381 scoped_feature_list.InitAndDisableFeature(
382 kSwReporterExtendedSafeBrowsingFeature);
383 RunReporter();
384 task_runner_->RunPendingTasks();
385 EXPECT_EQ(1, reporter_launch_count_);
386 base::RunLoop().RunUntilIdle();
387 ExpectLoggingSwitches({/*expect no switches*/});
388 ExpectLastTimeSentReportNotSet();
389 }
390
391 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_NoSBExtendedReporting) {
392 exit_code_to_report_ = kSwReporterNothingFound;
393 base::test::ScopedFeatureList scoped_feature_list;
394 scoped_feature_list.InitAndEnableFeature(
395 kSwReporterExtendedSafeBrowsingFeature);
396 RunReporter();
397 task_runner_->RunPendingTasks();
398 EXPECT_EQ(1, reporter_launch_count_);
399 base::RunLoop().RunUntilIdle();
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 task_runner_->RunPendingTasks();
415 EXPECT_EQ(1, reporter_launch_count_);
416 base::RunLoop().RunUntilIdle();
417 ExpectLoggingSwitches(
418 std::set<std::string>{kExtendedSafeBrowsingEnabledSwitch,
csharp 2016/09/02 21:16:25 Could you make this set a const?
ftirelo 2016/09/06 16:37:43 Done.
419 kChromeVersionSwitch, kChromeChannelSwitch});
420 ExpectLastReportSentAsToday();
421 }
422
423 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledNoRecentLogging) {
424 exit_code_to_report_ = kSwReporterNothingFound;
425 base::test::ScopedFeatureList scoped_feature_list;
426 scoped_feature_list.InitAndEnableFeature(
427 kSwReporterExtendedSafeBrowsingFeature);
428 // SBER is enabled and last time logs were sent was more than
429 // |kDaysBetweenReporterLogsSent| day ago, so we should send logs in this run.
430 EnableSBExtendedReporting();
431 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3);
432 RunReporter();
433 task_runner_->RunPendingTasks();
434 EXPECT_EQ(1, reporter_launch_count_);
435 base::RunLoop().RunUntilIdle();
436 ExpectLoggingSwitches(
437 std::set<std::string>{kExtendedSafeBrowsingEnabledSwitch,
438 kChromeVersionSwitch, kChromeChannelSwitch});
439 ExpectLastReportSentAsToday();
440 }
441
442 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledRecentlyLogged) {
443 exit_code_to_report_ = kSwReporterNothingFound;
444 base::test::ScopedFeatureList scoped_feature_list;
445 scoped_feature_list.InitAndEnableFeature(
446 kSwReporterExtendedSafeBrowsingFeature);
447 // SBER is enabled, but logs have been sent less than
448 // |kDaysBetweenReporterLogsSent| day ago, so we shouldn't send any logs in
449 // this run.
450 EnableSBExtendedReporting();
451 SetLastTimeSentReport(kDaysBetweenReporterLogsSent - 1);
452 int64_t last_time_sent_logs = GetLastTimeSentReport();
453 RunReporter();
csharp 2016/09/02 21:16:25 All of your tests have this same four middle lines
ftirelo 2016/09/06 16:37:42 Done.
454 task_runner_->RunPendingTasks();
455 EXPECT_EQ(1, reporter_launch_count_);
456 base::RunLoop().RunUntilIdle();
457 ExpectLoggingSwitches(std::set<std::string>{/*expect no switches*/});
458 EXPECT_EQ(last_time_sent_logs, GetLastTimeSentReport());
459 }
460
309 } // namespace safe_browsing 461 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698