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
3 // found in the LICENSE file.
5 #include "chrome/browser/safe_browsing/srt_fetcher_win.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/test/test_simple_task_runner.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "components/component_updater/pref_names.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
21 namespace safe_browsing
{
25 class SRTFetcherTest
: public InProcessBrowserTest
{
27 void SetUpInProcessBrowserTestFixture() override
{
28 task_runner_
= new base::TestSimpleTaskRunner
;
30 SetReporterLauncherForTesting(
31 base::Bind(&SRTFetcherTest::ReporterLauncher
, base::Unretained(this)));
32 SetPromptTriggerForTesting(
33 base::Bind(&SRTFetcherTest::PromptTrigger
, base::Unretained(this)));
36 void TearDownInProcessBrowserTestFixture() override
{
37 SetReporterLauncherForTesting(safe_browsing::ReporterLauncher());
38 SetPromptTriggerForTesting(safe_browsing::PromptTrigger());
42 RunSwReporter(base::FilePath(), "bla", task_runner_
, task_runner_
);
45 void PromptTrigger(Browser
* browser
, const std::string
& version
) {
46 prompt_trigger_called_
= true;
49 int ReporterLauncher(const base::FilePath
& exe_path
,
50 const std::string
& version
) {
51 reporter_launched_
= true;
52 return exit_code_to_report_
;
55 void SetDaysSinceLastReport(int days
) {
56 PrefService
* local_state
= g_browser_process
->local_state();
57 local_state
->SetInt64(prefs::kSwReporterLastTimeTriggered
,
58 (base::Time::Now() - base::TimeDelta::FromDays(days
))
62 void ExpectToRunAgain(int days
) {
63 ASSERT_TRUE(task_runner_
->HasPendingTask());
64 EXPECT_LE(task_runner_
->NextPendingTaskDelay(),
65 base::TimeDelta::FromDays(days
));
66 EXPECT_GT(task_runner_
->NextPendingTaskDelay(),
67 base::TimeDelta::FromDays(days
) - base::TimeDelta::FromHours(1));
70 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner_
;
71 bool prompt_trigger_called_
= false;
72 bool reporter_launched_
= false;
73 int exit_code_to_report_
= kReporterFailureExitCode
;
78 IN_PROC_BROWSER_TEST_F(SRTFetcherTest
, NothingFound
) {
79 exit_code_to_report_
= kSwReporterNothingFound
;
81 task_runner_
->RunPendingTasks();
82 EXPECT_TRUE(reporter_launched_
);
83 base::MessageLoop::current()->RunUntilIdle();
84 EXPECT_FALSE(prompt_trigger_called_
);
85 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns
);
88 IN_PROC_BROWSER_TEST_F(SRTFetcherTest
, CleanupNeeded
) {
89 exit_code_to_report_
= kSwReporterCleanupNeeded
;
92 task_runner_
->RunPendingTasks();
93 EXPECT_TRUE(reporter_launched_
);
94 // The reply task from the task posted to run the reporter is run on a
95 // specific thread, as opposed to a specific task runner, and that thread is
96 // the current message loop's thread.
97 base::MessageLoop::current()->RunUntilIdle();
98 EXPECT_TRUE(prompt_trigger_called_
);
99 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns
);
102 IN_PROC_BROWSER_TEST_F(SRTFetcherTest
, RanRecently
) {
103 static const int kDaysLeft
= 1;
104 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns
- kDaysLeft
);
107 // Here we can't run until idle since the ReporterRunner will re-post
109 task_runner_
->RunPendingTasks();
110 EXPECT_FALSE(reporter_launched_
);
112 ExpectToRunAgain(kDaysLeft
);
113 task_runner_
->ClearPendingTasks();
116 IN_PROC_BROWSER_TEST_F(SRTFetcherTest
, WaitForBrowser
) {
117 Profile
* profile
= browser()->profile();
120 exit_code_to_report_
= kSwReporterCleanupNeeded
;
123 task_runner_
->RunPendingTasks();
124 EXPECT_TRUE(reporter_launched_
);
126 CreateBrowser(profile
);
127 EXPECT_TRUE(prompt_trigger_called_
);
128 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns
);
131 IN_PROC_BROWSER_TEST_F(SRTFetcherTest
, Failure
) {
132 exit_code_to_report_
= kReporterFailureExitCode
;
135 task_runner_
->RunPendingTasks();
136 EXPECT_TRUE(reporter_launched_
);
138 base::MessageLoop::current()->RunUntilIdle();
139 EXPECT_FALSE(prompt_trigger_called_
);
140 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns
);
143 IN_PROC_BROWSER_TEST_F(SRTFetcherTest
, RunDaily
) {
144 exit_code_to_report_
= kSwReporterNothingFound
;
145 PrefService
* local_state
= g_browser_process
->local_state();
146 local_state
->SetBoolean(prefs::kSwReporterPendingPrompt
, true);
147 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns
- 1);
148 DCHECK_GT(kDaysBetweenSuccessfulSwReporterRuns
- 1,
149 kDaysBetweenSwReporterRunsForPendingPrompt
);
152 task_runner_
->RunPendingTasks();
153 EXPECT_TRUE(reporter_launched_
);
154 reporter_launched_
= false;
155 base::MessageLoop::current()->RunUntilIdle();
156 ExpectToRunAgain(kDaysBetweenSwReporterRunsForPendingPrompt
);
158 local_state
->SetBoolean(prefs::kSwReporterPendingPrompt
, false);
159 task_runner_
->RunPendingTasks();
160 EXPECT_FALSE(reporter_launched_
);
161 base::MessageLoop::current()->RunUntilIdle();
162 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns
);
165 } // namespace safe_browsing