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

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

Issue 2780873002: Basic IPC communication between Chrome and the SW Reporter. (Closed)
Patch Set: RefCounted subclasses can't have public destructors Created 3 years, 8 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 <initializer_list> 7 #include <initializer_list>
8 #include <iterator>
9 #include <set> 8 #include <set>
9 #include <tuple>
10 #include <utility>
10 #include <vector> 11 #include <vector>
11 12
12 #include "base/bind.h" 13 #include "base/bind.h"
13 #include "base/bind_helpers.h" 14 #include "base/bind_helpers.h"
14 #include "base/callback.h" 15 #include "base/callback.h"
16 #include "base/callback_helpers.h"
15 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
16 #include "base/memory/ref_counted.h" 18 #include "base/memory/ref_counted.h"
17 #include "base/message_loop/message_loop.h" 19 #include "base/message_loop/message_loop.h"
20 #include "base/run_loop.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/test/multiprocess_test.h"
18 #include "base/test/scoped_feature_list.h" 23 #include "base/test/scoped_feature_list.h"
19 #include "base/test/test_mock_time_task_runner.h" 24 #include "base/test/test_mock_time_task_runner.h"
25 #include "base/threading/sequenced_task_runner_handle.h"
20 #include "base/threading/thread_task_runner_handle.h" 26 #include "base/threading/thread_task_runner_handle.h"
21 #include "base/time/time.h" 27 #include "base/time/time.h"
22 #include "base/version.h" 28 #include "base/version.h"
23 #include "chrome/browser/browser_process.h" 29 #include "chrome/browser/browser_process.h"
24 #include "chrome/browser/lifetime/keep_alive_types.h" 30 #include "chrome/browser/lifetime/keep_alive_types.h"
25 #include "chrome/browser/lifetime/scoped_keep_alive.h" 31 #include "chrome/browser/lifetime/scoped_keep_alive.h"
26 #include "chrome/browser/profiles/profile.h" 32 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/browser/safe_browsing/srt_client_info_win.h" 33 #include "chrome/browser/safe_browsing/srt_client_info_win.h"
28 #include "chrome/browser/ui/browser.h" 34 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/browser_finder.h" 35 #include "chrome/browser/ui/browser_finder.h"
30 #include "chrome/browser/ui/test/test_browser_dialog.h" 36 #include "chrome/browser/ui/test/test_browser_dialog.h"
31 #include "chrome/common/pref_names.h" 37 #include "chrome/common/pref_names.h"
32 #include "chrome/test/base/in_process_browser_test.h" 38 #include "chrome/test/base/in_process_browser_test.h"
39 #include "components/chrome_cleaner/public/interfaces/chrome_prompt.mojom.h"
33 #include "components/component_updater/pref_names.h" 40 #include "components/component_updater/pref_names.h"
34 #include "components/prefs/pref_service.h" 41 #include "components/prefs/pref_service.h"
35 #include "components/safe_browsing_db/safe_browsing_prefs.h" 42 #include "components/safe_browsing_db/safe_browsing_prefs.h"
43 #include "mojo/edk/embedder/embedder.h"
44 #include "mojo/edk/embedder/scoped_ipc_support.h"
45 #include "mojo/edk/system/core.h"
46 #include "testing/multiprocess_func_list.h"
36 47
37 namespace safe_browsing { 48 namespace safe_browsing {
38 49
39 namespace { 50 namespace {
40 51
41 const char* const kExpectedSwitches[] = {kExtendedSafeBrowsingEnabledSwitch, 52 using chrome_cleaner::mojom::PromptAcceptance;
42 kChromeVersionSwitch,
43 kChromeChannelSwitch};
44 53
45 class SRTFetcherTest : public InProcessBrowserTest, 54 // Special switches passed by the parent process (test case) to the reporter
46 public SwReporterTestingDelegate { 55 // child process to indicate the behavior that should be mocked.
56 constexpr char kExitCodeToReturnSwitch[] = "exit-code-to-return";
57 constexpr char kReportUwSFoundSwitch[] = "report-uws-found";
58 constexpr char kReportElevationRequiredSwitch[] = "report-elevation-required";
59 constexpr char kExpectedPromptAcceptanceSwitch[] = "expected-prompt-acceptance";
60
61 // The exit code to be returned in case of failure in the child process.
62 // This should never be passed as the expected exit code to be reported by
63 // tests.
64 constexpr int kFailureExitCode = -1;
65
66 // Pass the |prompt_acceptance| to the mock child process in command line
67 // switch kExpectedPromptAcceptanceSwitch.
68 void AddPromptAcceptanceToCommandLine(PromptAcceptance prompt_acceptance,
69 base::CommandLine* command_line) {
70 command_line->AppendSwitchASCII(
71 kExpectedPromptAcceptanceSwitch,
72 base::IntToString(static_cast<int>(prompt_acceptance)));
73 }
74
75 // Parses and returns the prompt acceptance value passed by the parent process
76 // in command line switch kExpectedPromptAcceptanceSwitch. Returns
77 // PromptAcceptance::UNSPECIFIED if the switch doesn't exist or can't be
78 // parsed to a valid PromptAcceptance enumerator.
79 PromptAcceptance PromptAcceptanceFromCommandLine(
80 const base::CommandLine& command_line) {
81 const std::string& prompt_acceptance_str =
82 command_line.GetSwitchValueASCII(kExpectedPromptAcceptanceSwitch);
83 int val = -1;
84 if (base::StringToInt(prompt_acceptance_str, &val)) {
85 PromptAcceptance prompt_acceptance = static_cast<PromptAcceptance>(val);
86 if (chrome_cleaner::mojom::IsKnownEnumValue(prompt_acceptance))
87 return prompt_acceptance;
88 }
89 return PromptAcceptance::UNSPECIFIED;
90 }
91
92 // Pointer to ChromePromptPtr object to be used by the child process. The
93 // object must be created, deleted, and accessed on the IPC thread only.
94 chrome_cleaner::mojom::ChromePromptPtr* g_chrome_prompt_ptr = nullptr;
95
96 // The callback function to be passed to ChromePrompt::PromptUser to check if
97 // the prompt accepted returned by the parent process is equal to
98 // |expected_prompt_acceptance|. Will set |expected_value_received| with the
99 // comparison result, so that the main thread can report failure. Will invoke
100 // |done| callback when done.
101 void PromptUserCallback(const base::Closure& done,
102 PromptAcceptance expected_prompt_acceptance,
103 bool* expected_value_received,
104 PromptAcceptance prompt_acceptance) {
105 *expected_value_received = prompt_acceptance == expected_prompt_acceptance;
106 // It's safe to delete the ChromePromptPtr object here, since it will not be
107 // used anymore by the child process.
108 delete g_chrome_prompt_ptr;
109 g_chrome_prompt_ptr = nullptr;
110 done.Run();
111 }
112
113 // Mocks the sending of scan results from the child process to the parent
114 // process. Obtains the behavior to be mocked from special switches in
115 // |command_line|. Sets |expected_value_received| as true if the parent
116 // process replies with the expected prompt acceptance value.
117 void SendScanResults(const std::string& chrome_mojo_pipe_token,
118 const base::CommandLine& command_line,
119 const base::Closure& done,
120 bool* expected_value_received) {
121 constexpr int kDefaultUwSId = 10;
122 constexpr char kDefaultUwSName[] = "RemovedUwS";
123
124 mojo::ScopedMessagePipeHandle message_pipe_handle =
125 mojo::edk::CreateChildMessagePipe(chrome_mojo_pipe_token);
126 // This pointer will be deleted by PromptUserCallback.
127 g_chrome_prompt_ptr = new chrome_cleaner::mojom::ChromePromptPtr();
128 g_chrome_prompt_ptr->Bind(chrome_cleaner::mojom::ChromePromptPtrInfo(
129 std::move(message_pipe_handle), 0));
130
131 std::vector<chrome_cleaner::mojom::UwSPtr> removable_uws_found;
132 if (command_line.HasSwitch(kReportUwSFoundSwitch)) {
133 chrome_cleaner::mojom::UwSPtr uws = chrome_cleaner::mojom::UwS::New();
134 uws->id = kDefaultUwSId;
135 uws->name = kDefaultUwSName;
136 uws->observed_behaviours = chrome_cleaner::mojom::ObservedBehaviours::New();
137 removable_uws_found.push_back(std::move(uws));
138 }
139 const bool elevation_required =
140 command_line.HasSwitch(kReportElevationRequiredSwitch);
141 const PromptAcceptance expected_prompt_acceptance =
142 PromptAcceptanceFromCommandLine(command_line);
143
144 (*g_chrome_prompt_ptr)
145 ->PromptUser(
146 std::move(removable_uws_found), elevation_required,
147 base::Bind(&PromptUserCallback, done, expected_prompt_acceptance,
148 expected_value_received));
149 }
150
151 // Connects to the parent process and sends mocked scan results. Returns true
152 // if connection was successful and the prompt acceptance results sent by the
153 // parent process are the same as expected.
154 bool ConnectAndSendDataToParentProcess(
155 const std::string& chrome_mojo_pipe_token,
156 const base::CommandLine& command_line) {
157 DCHECK(!chrome_mojo_pipe_token.empty());
158
159 mojo::edk::Init();
160 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0);
161 base::Thread io_thread("IPCThread");
162 if (!io_thread.StartWithOptions(options))
163 return false;
164 mojo::edk::ScopedIPCSupport ipc_support(
165 io_thread.task_runner(),
166 mojo::edk::ScopedIPCSupport::ShutdownPolicy::CLEAN);
167 mojo::edk::SetParentPipeHandleFromCommandLine();
168 base::MessageLoop message_loop;
169 base::RunLoop run_loop;
170 // After the response from the parent process is received, this will post a
171 // task to unblock the child process's main thread.
172 auto done = base::Bind(
173 [](scoped_refptr<base::SequencedTaskRunner> main_runner,
174 base::Closure quit_closure) {
175 main_runner->PostTask(FROM_HERE, std::move(quit_closure));
176 },
177 base::SequencedTaskRunnerHandle::Get(),
178 base::Passed(run_loop.QuitClosure()));
179
180 bool expected_value_received = false;
181 io_thread.task_runner()->PostTask(
182 FROM_HERE, base::Bind(&SendScanResults, chrome_mojo_pipe_token,
183 command_line, done, &expected_value_received));
184 run_loop.Run();
185
186 return expected_value_received;
187 }
188
189 // Mocks a Software Reporter process that returns an exit code specified by
190 // command line switch kExitCodeToReturnSwitch. If a Mojo IPC is available,
191 // this will also connect to the parent process and send mocked scan results
192 // to the parent process using data passed as command line switches.
193 MULTIPROCESS_TEST_MAIN(MockSwReporterProcess) {
194 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
195 const std::string& str =
196 command_line->GetSwitchValueASCII(kExitCodeToReturnSwitch);
197 const std::string& chrome_mojo_pipe_token =
198 command_line->GetSwitchValueASCII(kChromeMojoPipeTokenSwitch);
199 int exit_code_to_report = kFailureExitCode;
200 bool success = base::StringToInt(str, &exit_code_to_report) &&
201 (chrome_mojo_pipe_token.empty() ||
202 ConnectAndSendDataToParentProcess(chrome_mojo_pipe_token,
203 *command_line));
204 return success ? exit_code_to_report : kFailureExitCode;
205 }
206
207 // Parameters for this test:
208 // - bool in_browser_cleaner_ui: indicates if InBrowserCleanerUI experiment
209 // is enabled; if so, the parent and the child processes will communicate
210 // via a Mojo IPC;
211 // - bool elevation_required: indicates if the scan results sent by the child
212 // process should consider that elevation will be required for cleanup.
213 class SRTFetcherTest
214 : public InProcessBrowserTest,
215 public SwReporterTestingDelegate,
216 public ::testing::WithParamInterface<std::tuple<bool, bool>> {
47 public: 217 public:
48 void SetUpInProcessBrowserTestFixture() override { 218 void SetUpInProcessBrowserTestFixture() override {
49 SetSwReporterTestingDelegate(this); 219 SetSwReporterTestingDelegate(this);
220
221 std::tie(in_browser_cleaner_ui_, elevation_required_) = GetParam();
222 // The config should only accept elevation_required_ if InBrowserCleanerUI
223 // feature is enabled.
224 ASSERT_TRUE(!elevation_required_ || in_browser_cleaner_ui_);
225
226 if (in_browser_cleaner_ui_)
227 scoped_feature_list_.InitAndEnableFeature(kInBrowserCleanerUIFeature);
228 else
229 scoped_feature_list_.InitAndDisableFeature(kInBrowserCleanerUIFeature);
50 } 230 }
51 231
52 void SetUpOnMainThread() override { 232 void SetUpOnMainThread() override {
53 // During the test, use a task runner with a mock clock. 233 // During the test, use a task runner with a mock clock.
54 saved_task_runner_ = base::ThreadTaskRunnerHandle::Get(); 234 saved_task_runner_ = base::ThreadTaskRunnerHandle::Get();
55 ASSERT_NE(mock_time_task_runner_, saved_task_runner_); 235 ASSERT_NE(mock_time_task_runner_, saved_task_runner_);
56 base::MessageLoop::current()->SetTaskRunner(mock_time_task_runner_); 236 base::MessageLoop::current()->SetTaskRunner(mock_time_task_runner_);
57 237
58 InProcessBrowserTest::SetUpOnMainThread(); 238 InProcessBrowserTest::SetUpOnMainThread();
59 239
(...skipping 13 matching lines...) Expand all
73 253
74 void TearDownInProcessBrowserTestFixture() override { 254 void TearDownInProcessBrowserTestFixture() override {
75 SetSwReporterTestingDelegate(nullptr); 255 SetSwReporterTestingDelegate(nullptr);
76 } 256 }
77 257
78 // Records that the prompt was shown. 258 // Records that the prompt was shown.
79 void TriggerPrompt(Browser* browser, const std::string& version) override { 259 void TriggerPrompt(Browser* browser, const std::string& version) override {
80 prompt_trigger_called_ = true; 260 prompt_trigger_called_ = true;
81 } 261 }
82 262
83 // Records that the reporter was launched with the parameters given in 263 // Spawns and returns a subprocess to mock an execution of the reporter with
84 // |invocation|. 264 // the parameters given in |invocation| that will return
85 int LaunchReporter(const SwReporterInvocation& invocation) override { 265 // |exit_code_to_report_|. If IPC communication needs to be mocked, this will
266 // also provide values that define the expected behavior of the child
267 // process.
268 // Records the launch and parameters used for further verification.
269 base::Process LaunchReporter(
270 const SwReporterInvocation& invocation,
271 const base::LaunchOptions& launch_options) override {
86 ++reporter_launch_count_; 272 ++reporter_launch_count_;
87 reporter_launch_parameters_.push_back(invocation); 273 reporter_launch_parameters_.push_back(invocation);
88 if (first_launch_callback_) 274 if (first_launch_callback_)
89 std::move(first_launch_callback_).Run(); 275 std::move(first_launch_callback_).Run();
90 return exit_code_to_report_; 276
277 base::CommandLine command_line(
278 base::GetMultiProcessTestChildBaseCommandLine());
279 command_line.AppendArguments(invocation.command_line,
280 /*include_program=*/false);
281 command_line.AppendSwitchASCII(kExitCodeToReturnSwitch,
282 base::IntToString(exit_code_to_report_));
283 if (in_browser_cleaner_ui_) {
284 AddPromptAcceptanceToCommandLine(PromptAcceptance::DENIED, &command_line);
285 if (exit_code_to_report_ == kSwReporterCleanupNeeded) {
286 command_line.AppendSwitch(kReportUwSFoundSwitch);
287 if (elevation_required_)
288 command_line.AppendSwitch(kReportElevationRequiredSwitch);
289 }
290 }
291 base::SpawnChildResult result = base::SpawnMultiProcessTestChild(
292 "MockSwReporterProcess", command_line, launch_options);
293 return std::move(result.process);
91 } 294 }
92 295
93 // Returns the test's idea of the current time. 296 // Returns the test's idea of the current time.
94 base::Time Now() const override { return mock_time_task_runner_->Now(); } 297 base::Time Now() const override { return mock_time_task_runner_->Now(); }
95 298
96 // Returns a task runner to use when launching the reporter (which is 299 // Returns a task runner to use when launching the reporter (which is
97 // normally a blocking action). 300 // normally a blocking action).
98 base::TaskRunner* BlockingTaskRunner() const override { 301 base::TaskRunner* BlockingTaskRunner() const override {
99 // Use the test's main task runner so that we don't need to pump another 302 // Use the test's main task runner so that we don't need to pump another
100 // message loop. Since the test calls LaunchReporter instead of actually 303 // message loop. Since the test calls LaunchReporter instead of actually
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 const base::Time last_time_sent_logs = base::Time::FromInternalValue( 427 const base::Time last_time_sent_logs = base::Time::FromInternalValue(
225 local_state->GetInt64(prefs::kSwReporterLastTimeSentReport)); 428 local_state->GetInt64(prefs::kSwReporterLastTimeSentReport));
226 429
227 // Checks if the last time sent logs is set as no more than one hour ago, 430 // Checks if the last time sent logs is set as no more than one hour ago,
228 // which should be enough time if the execution does not fail. 431 // which should be enough time if the execution does not fail.
229 EXPECT_LT(now - base::TimeDelta::FromHours(1), last_time_sent_logs); 432 EXPECT_LT(now - base::TimeDelta::FromHours(1), last_time_sent_logs);
230 EXPECT_GE(now, last_time_sent_logs); 433 EXPECT_GE(now, last_time_sent_logs);
231 } 434 }
232 435
233 // Expects |invocation|'s command line to contain all the switches required 436 // Expects |invocation|'s command line to contain all the switches required
234 // for reporter logging. 437 // for reporter logging if and only if |expect_switches| is true.
235 void ExpectLoggingSwitches(const SwReporterInvocation& invocation, 438 void ExpectLoggingSwitches(const SwReporterInvocation& invocation,
236 bool expect_switches) { 439 bool expect_switches) {
440 static const std::set<std::string> logging_switches{
441 kExtendedSafeBrowsingEnabledSwitch, kChromeVersionSwitch,
442 kChromeChannelSwitch};
443
237 const base::CommandLine::SwitchMap& invocation_switches = 444 const base::CommandLine::SwitchMap& invocation_switches =
238 invocation.command_line.GetSwitches(); 445 invocation.command_line.GetSwitches();
239 std::set<std::string> expected_switches; 446 // Checks if switches that enable logging on the reporter are present on
240 if (expect_switches) 447 // the invocation if and only if logging is allowed.
241 expected_switches = {std::begin(kExpectedSwitches), 448 for (const std::string& logging_switch : logging_switches) {
242 std::end(kExpectedSwitches)}; 449 EXPECT_EQ(expect_switches, invocation_switches.find(logging_switch) !=
243 EXPECT_EQ(expected_switches.size(), invocation_switches.size()); 450 invocation_switches.end());
244 // Checks if all expected switches are in the invocation switches. It's not
245 // necessary to check if all invocation switches are expected, since we
246 // checked if both sets should have the same size.
247 for (const std::string& expected_switch : expected_switches) {
248 EXPECT_NE(invocation_switches.end(),
249 invocation_switches.find(expected_switch));
250 } 451 }
251 } 452 }
252 453
253 // A task runner with a mock clock. 454 // A task runner with a mock clock.
254 scoped_refptr<base::TestMockTimeTaskRunner> mock_time_task_runner_ = 455 scoped_refptr<base::TestMockTimeTaskRunner> mock_time_task_runner_ =
255 new base::TestMockTimeTaskRunner(); 456 new base::TestMockTimeTaskRunner();
256 457
257 // The task runner that was in use before installing |mock_time_task_runner_|. 458 // The task runner that was in use before installing |mock_time_task_runner_|.
258 scoped_refptr<base::SingleThreadTaskRunner> saved_task_runner_; 459 scoped_refptr<base::SingleThreadTaskRunner> saved_task_runner_;
259 460
461 bool in_browser_cleaner_ui_;
462 bool elevation_required_;
463
260 bool prompt_trigger_called_ = false; 464 bool prompt_trigger_called_ = false;
261 int reporter_launch_count_ = 0; 465 int reporter_launch_count_ = 0;
262 std::vector<SwReporterInvocation> reporter_launch_parameters_; 466 std::vector<SwReporterInvocation> reporter_launch_parameters_;
263 int exit_code_to_report_ = kReporterFailureExitCode; 467 int exit_code_to_report_ = kReporterFailureExitCode;
264 468
265 // A callback to invoke when the first reporter of a queue is launched. This 469 // A callback to invoke when the first reporter of a queue is launched. This
266 // can be used to perform actions in the middle of a queue of reporters which 470 // can be used to perform actions in the middle of a queue of reporters which
267 // all launch on the same mock clock tick. 471 // all launch on the same mock clock tick.
268 base::OnceClosure first_launch_callback_; 472 base::OnceClosure first_launch_callback_;
473
474 base::test::ScopedFeatureList scoped_feature_list_;
269 }; 475 };
270 476
271 class SRTFetcherPromptTest : public DialogBrowserTest { 477 class SRTFetcherPromptTest : public DialogBrowserTest {
272 public: 478 public:
273 void ShowDialog(const std::string& name) override { 479 void ShowDialog(const std::string& name) override {
274 if (name == "SRTErrorNoFile") 480 if (name == "SRTErrorNoFile")
275 DisplaySRTPromptForTesting(base::FilePath()); 481 DisplaySRTPromptForTesting(base::FilePath());
276 else if (name == "SRTErrorFile") 482 else if (name == "SRTErrorFile")
277 DisplaySRTPromptForTesting( 483 DisplaySRTPromptForTesting(
278 base::FilePath().Append(FILE_PATH_LITERAL("c:\temp\testfile.txt"))); 484 base::FilePath().Append(FILE_PATH_LITERAL("c:\temp\testfile.txt")));
279 else 485 else
280 ADD_FAILURE() << "Unknown dialog type."; 486 ADD_FAILURE() << "Unknown dialog type.";
281 } 487 }
282 }; 488 };
283 489
284 } // namespace 490 } // namespace
285 491
286 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, NothingFound) { 492 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, NothingFound) {
287 RunReporter(kSwReporterNothingFound); 493 RunReporter(kSwReporterNothingFound);
288 ExpectReporterLaunches(0, 1, false); 494 ExpectReporterLaunches(0, 1, false);
289 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 495 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
290 } 496 }
291 497
292 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, CleanupNeeded) { 498 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, CleanupNeeded) {
293 RunReporter(kSwReporterCleanupNeeded); 499 RunReporter(kSwReporterCleanupNeeded);
294 ExpectReporterLaunches(0, 1, true); 500 ExpectReporterLaunches(0, 1, true);
295 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 501 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
296 } 502 }
297 503
298 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, RanRecently) { 504 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, RanRecently) {
299 constexpr int kDaysLeft = 1; 505 constexpr int kDaysLeft = 1;
300 SetDaysSinceLastTriggered(kDaysBetweenSuccessfulSwReporterRuns - kDaysLeft); 506 SetDaysSinceLastTriggered(kDaysBetweenSuccessfulSwReporterRuns - kDaysLeft);
301 RunReporter(kSwReporterNothingFound); 507 RunReporter(kSwReporterNothingFound);
302 ExpectReporterLaunches(0, 0, false); 508 ExpectReporterLaunches(0, 0, false);
303 ExpectToRunAgain(kDaysLeft); 509 ExpectToRunAgain(kDaysLeft);
304 ExpectReporterLaunches(kDaysLeft, 1, false); 510 ExpectReporterLaunches(kDaysLeft, 1, false);
305 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 511 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
306 } 512 }
307 513
308 // Test is flaky. crbug.com/705608 514 // Test is flaky. crbug.com/705608
309 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, DISABLED_WaitForBrowser) { 515 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, DISABLED_WaitForBrowser) {
310 Profile* profile = browser()->profile(); 516 Profile* profile = browser()->profile();
311 517
312 // Ensure that even though we're closing the last browser, we don't enter the 518 // Ensure that even though we're closing the last browser, we don't enter the
313 // "shutting down" state, which will prevent the test from starting another 519 // "shutting down" state, which will prevent the test from starting another
314 // browser. 520 // browser.
315 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::SESSION_RESTORE, 521 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::SESSION_RESTORE,
316 KeepAliveRestartOption::ENABLED); 522 KeepAliveRestartOption::ENABLED);
317 523
318 // Use the standard task runner for browser cleanup, which will wait forever 524 // Use the standard task runner for browser cleanup, which will wait forever
319 // with the mock clock. 525 // with the mock clock.
(...skipping 19 matching lines...) Expand all
339 // pending task in the queue is the next reporter check. 545 // pending task in the queue is the next reporter check.
340 mock_time_task_runner_->FastForwardBy(base::TimeDelta::FromMinutes(10)); 546 mock_time_task_runner_->FastForwardBy(base::TimeDelta::FromMinutes(10));
341 547
342 // On opening the new browser, the prompt should be shown and the reporter 548 // On opening the new browser, the prompt should be shown and the reporter
343 // should be scheduled to run later. 549 // should be scheduled to run later.
344 EXPECT_EQ(1, reporter_launch_count_); 550 EXPECT_EQ(1, reporter_launch_count_);
345 EXPECT_TRUE(prompt_trigger_called_); 551 EXPECT_TRUE(prompt_trigger_called_);
346 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 552 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
347 } 553 }
348 554
349 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, Failure) { 555 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, Failure) {
350 RunReporter(kReporterFailureExitCode); 556 RunReporter(kReporterFailureExitCode);
351 ExpectReporterLaunches(0, 1, false); 557 ExpectReporterLaunches(0, 1, false);
352 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 558 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
353 } 559 }
354 560
355 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, RunDaily) { 561 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, RunDaily) {
356 PrefService* local_state = g_browser_process->local_state(); 562 PrefService* local_state = g_browser_process->local_state();
357 local_state->SetBoolean(prefs::kSwReporterPendingPrompt, true); 563 local_state->SetBoolean(prefs::kSwReporterPendingPrompt, true);
358 SetDaysSinceLastTriggered(kDaysBetweenSuccessfulSwReporterRuns - 1); 564 SetDaysSinceLastTriggered(kDaysBetweenSuccessfulSwReporterRuns - 1);
359 ASSERT_GT(kDaysBetweenSuccessfulSwReporterRuns - 1, 565 ASSERT_GT(kDaysBetweenSuccessfulSwReporterRuns - 1,
360 kDaysBetweenSwReporterRunsForPendingPrompt); 566 kDaysBetweenSwReporterRunsForPendingPrompt);
361 RunReporter(kSwReporterNothingFound); 567 RunReporter(kSwReporterNothingFound);
362 568
363 // Expect the reporter to run immediately, since a prompt is pending and it 569 // Expect the reporter to run immediately, since a prompt is pending and it
364 // has been more than kDaysBetweenSwReporterRunsForPendingPrompt days. 570 // has been more than kDaysBetweenSwReporterRunsForPendingPrompt days.
365 ExpectReporterLaunches(0, 1, false); 571 ExpectReporterLaunches(0, 1, false);
366 ExpectToRunAgain(kDaysBetweenSwReporterRunsForPendingPrompt); 572 ExpectToRunAgain(kDaysBetweenSwReporterRunsForPendingPrompt);
367 573
368 // Move the clock ahead kDaysBetweenSwReporterRunsForPendingPrompt days. The 574 // Move the clock ahead kDaysBetweenSwReporterRunsForPendingPrompt days. The
369 // expected run should trigger, but not cause the reporter to launch because 575 // expected run should trigger, but not cause the reporter to launch because
370 // a prompt is no longer pending. 576 // a prompt is no longer pending.
371 local_state->SetBoolean(prefs::kSwReporterPendingPrompt, false); 577 local_state->SetBoolean(prefs::kSwReporterPendingPrompt, false);
372 ExpectReporterLaunches(kDaysBetweenSwReporterRunsForPendingPrompt, 0, false); 578 ExpectReporterLaunches(kDaysBetweenSwReporterRunsForPendingPrompt, 0, false);
373 579
374 // Instead it should now run kDaysBetweenSuccessfulSwReporterRuns after the 580 // Instead it should now run kDaysBetweenSuccessfulSwReporterRuns after the
375 // first prompt (of which kDaysBetweenSwReporterRunsForPendingPrompt has 581 // first prompt (of which kDaysBetweenSwReporterRunsForPendingPrompt has
376 // already passed.) 582 // already passed.)
377 int days_left = kDaysBetweenSuccessfulSwReporterRuns - 583 int days_left = kDaysBetweenSuccessfulSwReporterRuns -
378 kDaysBetweenSwReporterRunsForPendingPrompt; 584 kDaysBetweenSwReporterRunsForPendingPrompt;
379 ExpectToRunAgain(days_left); 585 ExpectToRunAgain(days_left);
380 ExpectReporterLaunches(days_left, 1, false); 586 ExpectReporterLaunches(days_left, 1, false);
381 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 587 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
382 } 588 }
383 589
384 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ParameterChange) { 590 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, ParameterChange) {
385 // If the reporter is run several times with different parameters, it should 591 // If the reporter is run several times with different parameters, it should
386 // only be launched once, with the last parameter set. 592 // only be launched once, with the last parameter set.
387 const base::FilePath path1(L"path1"); 593 const base::FilePath path1(L"path1");
388 const base::FilePath path2(L"path2"); 594 const base::FilePath path2(L"path2");
389 const base::FilePath path3(L"path3"); 595 const base::FilePath path3(L"path3");
390 596
391 // Schedule path1 with a day left in the reporting period. 597 // Schedule path1 with a day left in the reporting period.
392 // The reporter should not launch. 598 // The reporter should not launch.
393 constexpr int kDaysLeft = 1; 599 constexpr int kDaysLeft = 1;
394 { 600 {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 { 637 {
432 SCOPED_TRACE("Run with same parameters"); 638 SCOPED_TRACE("Run with same parameters");
433 RunReporter(kSwReporterNothingFound, path3); 639 RunReporter(kSwReporterNothingFound, path3);
434 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, {path3}, 640 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, {path3},
435 false); 641 false);
436 } 642 }
437 643
438 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 644 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
439 } 645 }
440 646
441 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, MultipleLaunches) { 647 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, MultipleLaunches) {
442 const base::FilePath path1(L"path1"); 648 const base::FilePath path1(L"path1");
443 const base::FilePath path2(L"path2"); 649 const base::FilePath path2(L"path2");
444 const base::FilePath path3(L"path3"); 650 const base::FilePath path3(L"path3");
445 651
446 SwReporterQueue invocations; 652 SwReporterQueue invocations;
447 invocations.push(SwReporterInvocation::FromFilePath(path1)); 653 invocations.push(SwReporterInvocation::FromFilePath(path1));
448 invocations.push(SwReporterInvocation::FromFilePath(path2)); 654 invocations.push(SwReporterInvocation::FromFilePath(path2));
449 655
450 { 656 {
451 SCOPED_TRACE("Launch 2 times"); 657 SCOPED_TRACE("Launch 2 times");
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 // anything. 705 // anything.
500 ExpectReporterLaunches(0, {}, false); 706 ExpectReporterLaunches(0, {}, false);
501 707
502 // After enough time has passed, should try the queue again. 708 // After enough time has passed, should try the queue again.
503 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, {path1}, 709 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, {path1},
504 false); 710 false);
505 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 711 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
506 } 712 }
507 } 713 }
508 714
509 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_NoSBExtendedReporting) { 715 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, ReporterLogging_NoSBExtendedReporting) {
510 base::test::ScopedFeatureList scoped_feature_list;
511 RunReporter(kSwReporterNothingFound); 716 RunReporter(kSwReporterNothingFound);
512 ExpectReporterLaunches(0, 1, false); 717 ExpectReporterLaunches(0, 1, false);
513 ExpectLoggingSwitches(reporter_launch_parameters_.front(), false); 718 ExpectLoggingSwitches(reporter_launch_parameters_.front(), false);
514 ExpectLastTimeSentReportNotSet(); 719 ExpectLastTimeSentReportNotSet();
515 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 720 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
516 } 721 }
517 722
518 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledFirstRun) { 723 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, ReporterLogging_EnabledFirstRun) {
519 base::test::ScopedFeatureList scoped_feature_list;
520 EnableSBExtendedReporting(); 724 EnableSBExtendedReporting();
521 // Note: don't set last time sent logs in the local state. 725 // Note: don't set last time sent logs in the local state.
522 // SBER is enabled and there is no record in the local state of the last time 726 // SBER is enabled and there is no record in the local state of the last time
523 // logs have been sent, so we should send logs in this run. 727 // logs have been sent, so we should send logs in this run.
524 RunReporter(kSwReporterNothingFound); 728 RunReporter(kSwReporterNothingFound);
525 ExpectReporterLaunches(0, 1, false); 729 ExpectReporterLaunches(0, 1, false);
526 ExpectLoggingSwitches(reporter_launch_parameters_.front(), true); 730 ExpectLoggingSwitches(reporter_launch_parameters_.front(), true);
527 ExpectLastReportSentInTheLastHour(); 731 ExpectLastReportSentInTheLastHour();
528 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 732 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
529 } 733 }
530 734
531 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledNoRecentLogging) { 735 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, ReporterLogging_EnabledNoRecentLogging) {
532 base::test::ScopedFeatureList scoped_feature_list;
533 // SBER is enabled and last time logs were sent was more than 736 // SBER is enabled and last time logs were sent was more than
534 // |kDaysBetweenReporterLogsSent| day ago, so we should send logs in this run. 737 // |kDaysBetweenReporterLogsSent| day ago, so we should send logs in this run.
535 EnableSBExtendedReporting(); 738 EnableSBExtendedReporting();
536 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3); 739 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3);
537 RunReporter(kSwReporterNothingFound); 740 RunReporter(kSwReporterNothingFound);
538 ExpectReporterLaunches(0, 1, false); 741 ExpectReporterLaunches(0, 1, false);
539 ExpectLoggingSwitches(reporter_launch_parameters_.front(), true); 742 ExpectLoggingSwitches(reporter_launch_parameters_.front(), true);
540 ExpectLastReportSentInTheLastHour(); 743 ExpectLastReportSentInTheLastHour();
541 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 744 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
542 } 745 }
543 746
544 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledRecentlyLogged) { 747 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, ReporterLogging_EnabledRecentlyLogged) {
545 base::test::ScopedFeatureList scoped_feature_list;
546 // SBER is enabled, but logs have been sent less than 748 // SBER is enabled, but logs have been sent less than
547 // |kDaysBetweenReporterLogsSent| day ago, so we shouldn't send any logs in 749 // |kDaysBetweenReporterLogsSent| day ago, so we shouldn't send any logs in
548 // this run. 750 // this run.
549 EnableSBExtendedReporting(); 751 EnableSBExtendedReporting();
550 SetLastTimeSentReport(kDaysBetweenReporterLogsSent - 1); 752 SetLastTimeSentReport(kDaysBetweenReporterLogsSent - 1);
551 int64_t last_time_sent_logs = GetLastTimeSentReport(); 753 int64_t last_time_sent_logs = GetLastTimeSentReport();
552 RunReporter(kSwReporterNothingFound); 754 RunReporter(kSwReporterNothingFound);
553 ExpectReporterLaunches(0, 1, false); 755 ExpectReporterLaunches(0, 1, false);
554 ExpectLoggingSwitches(reporter_launch_parameters_.front(), false); 756 ExpectLoggingSwitches(reporter_launch_parameters_.front(), false);
555 EXPECT_EQ(last_time_sent_logs, GetLastTimeSentReport()); 757 EXPECT_EQ(last_time_sent_logs, GetLastTimeSentReport());
556 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 758 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
557 } 759 }
558 760
559 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_MultipleLaunches) { 761 IN_PROC_BROWSER_TEST_P(SRTFetcherTest, ReporterLogging_MultipleLaunches) {
560 base::test::ScopedFeatureList scoped_feature_list;
561 EnableSBExtendedReporting(); 762 EnableSBExtendedReporting();
562 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3); 763 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3);
563 764
564 const base::FilePath path1(L"path1"); 765 const base::FilePath path1(L"path1");
565 const base::FilePath path2(L"path2"); 766 const base::FilePath path2(L"path2");
566 SwReporterQueue invocations; 767 SwReporterQueue invocations;
567 for (const auto& path : {path1, path2}) { 768 for (const auto& path : {path1, path2}) {
568 auto invocation = SwReporterInvocation::FromFilePath(path); 769 auto invocation = SwReporterInvocation::FromFilePath(path);
569 invocation.supported_behaviours = 770 invocation.supported_behaviours =
570 SwReporterInvocation::BEHAVIOUR_ALLOW_SEND_REPORTER_LOGS; 771 SwReporterInvocation::BEHAVIOUR_ALLOW_SEND_REPORTER_LOGS;
(...skipping 15 matching lines...) Expand all
586 // Logs should also be sent for the next run, even though LastTimeSentReport 787 // Logs should also be sent for the next run, even though LastTimeSentReport
587 // is now recent, because the run is part of the same set of invocations. 788 // is now recent, because the run is part of the same set of invocations.
588 { 789 {
589 SCOPED_TRACE("second launch"); 790 SCOPED_TRACE("second launch");
590 ExpectLoggingSwitches(reporter_launch_parameters_[1], true); 791 ExpectLoggingSwitches(reporter_launch_parameters_[1], true);
591 ExpectLastReportSentInTheLastHour(); 792 ExpectLastReportSentInTheLastHour();
592 } 793 }
593 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); 794 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns);
594 } 795 }
595 796
797 INSTANTIATE_TEST_CASE_P(NoInBrowserCleanerUI,
798 SRTFetcherTest,
799 testing::Combine(testing::Values(false),
800 testing::Values(false)));
801
802 INSTANTIATE_TEST_CASE_P(InBrowserCleanerUI,
803 SRTFetcherTest,
804 testing::Combine(testing::Values(true),
805 testing::Bool()));
806
596 // This provide tests which allows explicit invocation of the SRT Prompt 807 // This provide tests which allows explicit invocation of the SRT Prompt
597 // useful for checking dialog layout or any other interactive functionality 808 // useful for checking dialog layout or any other interactive functionality
598 // tests. See docs/testing/test_browser_dialog.md for description of the 809 // tests. See docs/testing/test_browser_dialog.md for description of the
599 // testing framework. 810 // testing framework.
600 IN_PROC_BROWSER_TEST_F(SRTFetcherPromptTest, InvokeDialog_SRTErrorNoFile) { 811 IN_PROC_BROWSER_TEST_F(SRTFetcherPromptTest, InvokeDialog_SRTErrorNoFile) {
601 RunDialog(); 812 RunDialog();
602 } 813 }
603 814
604 IN_PROC_BROWSER_TEST_F(SRTFetcherPromptTest, InvokeDialog_SRTErrorFile) { 815 IN_PROC_BROWSER_TEST_F(SRTFetcherPromptTest, InvokeDialog_SRTErrorFile) {
605 RunDialog(); 816 RunDialog();
606 } 817 }
607 818
608 } // namespace safe_browsing 819 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/srt_chrome_prompt_impl.cc ('k') | chrome/browser/safe_browsing/srt_fetcher_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698