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

Side by Side Diff: chrome/browser/safe_browsing/chrome_cleaner/chrome_cleaner_controller_win_unittest.cc

Issue 2906103002: Post-cleanup settings reset. (Closed)
Patch Set: Nits Created 3 years, 6 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/chrome_cleaner/chrome_cleaner_controller_ win.h" 5 #include "chrome/browser/safe_browsing/chrome_cleaner/chrome_cleaner_controller_ win.h"
6 6
7 #include <string> 7 #include <string>
8 #include <tuple> 8 #include <tuple>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/task_scheduler/post_task.h" 13 #include "base/task_scheduler/post_task.h"
14 #include "base/test/multiprocess_test.h" 14 #include "base/test/multiprocess_test.h"
15 #include "base/test/scoped_feature_list.h" 15 #include "base/test/scoped_feature_list.h"
16 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
17 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" 17 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/safe_browsing/chrome_cleaner/mock_chrome_cleaner_proces s_win.h" 20 #include "chrome/browser/safe_browsing/chrome_cleaner/mock_chrome_cleaner_proces s_win.h"
19 #include "chrome/browser/safe_browsing/chrome_cleaner/reporter_runner_win.h" 21 #include "chrome/browser/safe_browsing/chrome_cleaner/reporter_runner_win.h"
20 #include "chrome/browser/safe_browsing/chrome_cleaner/srt_field_trial_win.h" 22 #include "chrome/browser/safe_browsing/chrome_cleaner/srt_field_trial_win.h"
23 #include "chrome/test/base/testing_browser_process.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "chrome/test/base/testing_profile_manager.h"
21 #include "components/chrome_cleaner/public/constants/constants.h" 26 #include "components/chrome_cleaner/public/constants/constants.h"
22 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
23 #include "content/public/test/test_browser_thread_bundle.h" 28 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
26 #include "testing/multiprocess_func_list.h" 31 #include "testing/multiprocess_func_list.h"
27 32
28 namespace safe_browsing { 33 namespace safe_browsing {
29 namespace { 34 namespace {
30 35
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 std::move(fetched_callback) 110 std::move(fetched_callback)
106 .Run(base::FilePath(FILE_PATH_LITERAL("chrome_cleaner.exe"))); 111 .Run(base::FilePath(FILE_PATH_LITERAL("chrome_cleaner.exe")));
107 } 112 }
108 113
109 bool SafeBrowsingExtendedReportingScoutEnabled() override { 114 bool SafeBrowsingExtendedReportingScoutEnabled() override {
110 return scout_enabled_; 115 return scout_enabled_;
111 } 116 }
112 117
113 bool IsMetricsAndCrashReportingEnabled() override { return metrics_enabled_; } 118 bool IsMetricsAndCrashReportingEnabled() override { return metrics_enabled_; }
114 119
120 void InvokeTagProfileForResetting(Profile* profile) override {
121 // This function should never be called by these tests.
122 ASSERT_TRUE(false);
123 }
124
125 void InvokeResetPostCleanupSettingsIfTagged(
126 std::vector<Profile*> profiles,
127 base::OnceClosure continuation) override {
128 // This function should never be called by these tests.
129 ASSERT_TRUE(false);
130 }
131
115 // ChromeCleanerRunnerTestDelegate overrides. 132 // ChromeCleanerRunnerTestDelegate overrides.
116 133
117 base::Process LaunchTestProcess( 134 base::Process LaunchTestProcess(
118 const base::CommandLine& command_line, 135 const base::CommandLine& command_line,
119 const base::LaunchOptions& launch_options) override { 136 const base::LaunchOptions& launch_options) override {
120 command_line_ = command_line; 137 command_line_ = command_line;
121 // Return an invalid process. 138 // Return an invalid process.
122 return base::Process(); 139 return base::Process();
123 } 140 }
124 141
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 // configurations and mocks the user's response. 213 // configurations and mocks the user's response.
197 class ChromeCleanerControllerTest 214 class ChromeCleanerControllerTest
198 : public testing::TestWithParam< 215 : public testing::TestWithParam<
199 std::tuple<CleanerProcessStatus, 216 std::tuple<CleanerProcessStatus,
200 MockChromeCleanerProcess::CrashPoint, 217 MockChromeCleanerProcess::CrashPoint,
201 UwsFoundStatus, 218 UwsFoundStatus,
202 ChromeCleanerController::UserResponse>>, 219 ChromeCleanerController::UserResponse>>,
203 public ChromeCleanerRunnerTestDelegate, 220 public ChromeCleanerRunnerTestDelegate,
204 public ChromeCleanerControllerDelegate { 221 public ChromeCleanerControllerDelegate {
205 public: 222 public:
223 ChromeCleanerControllerTest()
224 : profile_manager_(TestingBrowserProcess::GetGlobal()) {}
206 ~ChromeCleanerControllerTest() override {} 225 ~ChromeCleanerControllerTest() override {}
207 226
208 void SetUp() override { 227 void SetUp() override {
228 ASSERT_TRUE(profile_manager_.SetUp());
229
209 std::tie(process_status_, crash_point_, uws_found_status_, user_response_) = 230 std::tie(process_status_, crash_point_, uws_found_status_, user_response_) =
210 GetParam(); 231 GetParam();
211 232
212 cleaner_process_options_.SetDoFindUws(uws_found_status_ != 233 cleaner_process_options_.SetDoFindUws(uws_found_status_ !=
213 UwsFoundStatus::kNoUwsFound); 234 UwsFoundStatus::kNoUwsFound);
214 cleaner_process_options_.set_reboot_required( 235 cleaner_process_options_.set_reboot_required(
215 uws_found_status_ == UwsFoundStatus::kUwsFoundRebootRequired); 236 uws_found_status_ == UwsFoundStatus::kUwsFoundRebootRequired);
216 cleaner_process_options_.set_crash_point(crash_point_); 237 cleaner_process_options_.set_crash_point(crash_point_);
217 238
218 controller_ = ChromeCleanerController::GetInstance(); 239 controller_ = ChromeCleanerController::GetInstance();
219 ASSERT_TRUE(controller_); 240 ASSERT_TRUE(controller_);
220 241
221 scoped_feature_list_.InitAndEnableFeature(kInBrowserCleanerUIFeature); 242 scoped_feature_list_.InitAndEnableFeature(kInBrowserCleanerUIFeature);
222 SetChromeCleanerRunnerTestDelegateForTesting(this); 243 SetChromeCleanerRunnerTestDelegateForTesting(this);
223 controller_->SetDelegateForTesting(this); 244 controller_->SetDelegateForTesting(this);
224 } 245 }
225 246
226 void TearDown() override { 247 void TearDown() override {
227 if (controller_->state() == State::kRebootRequired) 248 if (controller_->state() == State::kRebootRequired)
228 controller_->DismissRebootForTesting(); 249 controller_->DismissRebootForTesting();
229 250
230 controller_->SetDelegateForTesting(nullptr); 251 controller_->SetDelegateForTesting(nullptr);
231 SetChromeCleanerRunnerTestDelegateForTesting(nullptr); 252 SetChromeCleanerRunnerTestDelegateForTesting(nullptr);
232 } 253 }
233 254
255 TestingProfileManager* profile_manager() { return &profile_manager_; }
256
234 // ChromeCleanerControllerDelegate overrides. 257 // ChromeCleanerControllerDelegate overrides.
235 258
236 void FetchAndVerifyChromeCleaner(FetchedCallback fetched_callback) override { 259 void FetchAndVerifyChromeCleaner(FetchedCallback fetched_callback) override {
237 base::ThreadTaskRunnerHandle::Get()->PostTask( 260 base::ThreadTaskRunnerHandle::Get()->PostTask(
238 FROM_HERE, 261 FROM_HERE,
239 base::BindOnce( 262 base::BindOnce(
240 std::move(fetched_callback), 263 std::move(fetched_callback),
241 process_status_ != CleanerProcessStatus::kFetchFailure 264 process_status_ != CleanerProcessStatus::kFetchFailure
242 ? base::FilePath(FILE_PATH_LITERAL("chrome_cleaner.exe")) 265 ? base::FilePath(FILE_PATH_LITERAL("chrome_cleaner.exe"))
243 : base::FilePath())); 266 : base::FilePath()));
244 } 267 }
245 268
246 bool SafeBrowsingExtendedReportingScoutEnabled() override { 269 bool SafeBrowsingExtendedReportingScoutEnabled() override {
247 // Returning an arbitrary value since this is not being tested in this 270 // Returning an arbitrary value since this is not being tested in this
248 // fixture. 271 // fixture.
249 return false; 272 return false;
250 } 273 }
251 274
252 bool IsMetricsAndCrashReportingEnabled() override { 275 bool IsMetricsAndCrashReportingEnabled() override {
253 // Returning an arbitrary value since this is not being tested in this 276 // Returning an arbitrary value since this is not being tested in this
254 // fixture. 277 // fixture.
255 return false; 278 return false;
256 } 279 }
257 280
281 void InvokeTagProfileForResetting(Profile* profile) override {
282 ++tagged_profiles_;
283 }
284
285 void InvokeResetPostCleanupSettingsIfTagged(
286 std::vector<Profile*> profiles,
287 base::OnceClosure continuation) override {
288 profiles_reset_ += profiles.size();
289 }
290
258 // ChromeCleanerRunnerTestDelegate overrides. 291 // ChromeCleanerRunnerTestDelegate overrides.
259 292
260 base::Process LaunchTestProcess( 293 base::Process LaunchTestProcess(
261 const base::CommandLine& command_line, 294 const base::CommandLine& command_line,
262 const base::LaunchOptions& launch_options) override { 295 const base::LaunchOptions& launch_options) override {
263 if (process_status_ != CleanerProcessStatus::kFetchSuccessValidProcess) 296 if (process_status_ != CleanerProcessStatus::kFetchSuccessValidProcess)
264 return base::Process(); 297 return base::Process();
265 298
266 // Add switches and program name that the test process needs for the multi 299 // Add switches and program name that the test process needs for the multi
267 // process tests. 300 // process tests.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 crash_point_ != CrashPoint::kAfterRequestSent && 338 crash_point_ != CrashPoint::kAfterRequestSent &&
306 user_response_ == UserResponse::kAccepted; 339 user_response_ == UserResponse::kAccepted;
307 } 340 }
308 341
309 bool ExpectedOnRebootRequiredCalled() { 342 bool ExpectedOnRebootRequiredCalled() {
310 return ExpectedFinalState() == State::kRebootRequired; 343 return ExpectedFinalState() == State::kRebootRequired;
311 } 344 }
312 345
313 bool ExpectedUwsFound() { return ExpectedOnInfectedCalled(); } 346 bool ExpectedUwsFound() { return ExpectedOnInfectedCalled(); }
314 347
348 bool ExpectedToTagProfiles() {
349 return process_status_ == CleanerProcessStatus::kFetchSuccessValidProcess &&
350 (crash_point_ == CrashPoint::kNone ||
351 crash_point_ == CrashPoint::kAfterResponseReceived) &&
352 (uws_found_status_ == UwsFoundStatus::kUwsFoundNoRebootRequired ||
353 uws_found_status_ == UwsFoundStatus::kUwsFoundRebootRequired) &&
354 user_response_ == UserResponse::kAccepted;
355 }
356
357 bool ExpectedToResetSettings() {
358 return process_status_ == CleanerProcessStatus::kFetchSuccessValidProcess &&
359 crash_point_ == CrashPoint::kNone &&
360 uws_found_status_ == UwsFoundStatus::kUwsFoundNoRebootRequired &&
361 user_response_ == UserResponse::kAccepted;
362 }
363
315 ChromeCleanerController::IdleReason ExpectedIdleReason() { 364 ChromeCleanerController::IdleReason ExpectedIdleReason() {
316 EXPECT_EQ(ExpectedFinalState(), State::kIdle); 365 EXPECT_EQ(ExpectedFinalState(), State::kIdle);
317 366
318 if (process_status_ != CleanerProcessStatus::kFetchSuccessValidProcess || 367 if (process_status_ != CleanerProcessStatus::kFetchSuccessValidProcess ||
319 crash_point_ == CrashPoint::kOnStartup || 368 crash_point_ == CrashPoint::kOnStartup ||
320 crash_point_ == CrashPoint::kAfterConnection) { 369 crash_point_ == CrashPoint::kAfterConnection) {
321 return IdleReason::kScanningFailed; 370 return IdleReason::kScanningFailed;
322 } 371 }
323 372
324 if (uws_found_status_ == UwsFoundStatus::kNoUwsFound) 373 if (uws_found_status_ == UwsFoundStatus::kNoUwsFound)
(...skipping 23 matching lines...) Expand all
348 397
349 CleanerProcessStatus process_status_; 398 CleanerProcessStatus process_status_;
350 MockChromeCleanerProcess::CrashPoint crash_point_; 399 MockChromeCleanerProcess::CrashPoint crash_point_;
351 UwsFoundStatus uws_found_status_; 400 UwsFoundStatus uws_found_status_;
352 ChromeCleanerController::UserResponse user_response_; 401 ChromeCleanerController::UserResponse user_response_;
353 402
354 MockChromeCleanerProcess::Options cleaner_process_options_; 403 MockChromeCleanerProcess::Options cleaner_process_options_;
355 404
356 StrictMock<MockChromeCleanerControllerObserver> mock_observer_; 405 StrictMock<MockChromeCleanerControllerObserver> mock_observer_;
357 ChromeCleanerController* controller_; 406 ChromeCleanerController* controller_;
407
408 TestingProfileManager profile_manager_;
409 int tagged_profiles_ = 0;
410 int profiles_reset_ = 0;
358 }; 411 };
359 412
360 MULTIPROCESS_TEST_MAIN(MockChromeCleanerProcessMain) { 413 MULTIPROCESS_TEST_MAIN(MockChromeCleanerProcessMain) {
361 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 414 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
362 MockChromeCleanerProcess::Options options; 415 MockChromeCleanerProcess::Options options;
363 EXPECT_TRUE(MockChromeCleanerProcess::Options::FromCommandLine(*command_line, 416 EXPECT_TRUE(MockChromeCleanerProcess::Options::FromCommandLine(*command_line,
364 &options)); 417 &options));
365 418
366 std::string chrome_mojo_pipe_token = command_line->GetSwitchValueASCII( 419 std::string chrome_mojo_pipe_token = command_line->GetSwitchValueASCII(
367 chrome_cleaner::kChromeMojoPipeTokenSwitch); 420 chrome_cleaner::kChromeMojoPipeTokenSwitch);
368 EXPECT_FALSE(chrome_mojo_pipe_token.empty()); 421 EXPECT_FALSE(chrome_mojo_pipe_token.empty());
369 422
370 // Since failures in any of the above calls to EXPECT_*() do not actually fail 423 // Since failures in any of the above calls to EXPECT_*() do not actually fail
371 // the test, we need to ensure that we return an exit code to indicate test 424 // the test, we need to ensure that we return an exit code to indicate test
372 // failure in such cases. 425 // failure in such cases.
373 if (::testing::Test::HasFailure()) 426 if (::testing::Test::HasFailure())
374 return MockChromeCleanerProcess::kInternalTestFailureExitCode; 427 return MockChromeCleanerProcess::kInternalTestFailureExitCode;
375 428
376 MockChromeCleanerProcess mock_cleaner_process(options, 429 MockChromeCleanerProcess mock_cleaner_process(options,
377 chrome_mojo_pipe_token); 430 chrome_mojo_pipe_token);
378 return mock_cleaner_process.Run(); 431 return mock_cleaner_process.Run();
379 } 432 }
380 433
381 TEST_P(ChromeCleanerControllerTest, WithMockCleanerProcess) { 434 TEST_P(ChromeCleanerControllerTest, WithMockCleanerProcess) {
435 ASSERT_TRUE(profile_manager()->CreateTestingProfile("Test 1"));
436 ASSERT_TRUE(profile_manager()->CreateTestingProfile("Test 2"));
437 ASSERT_TRUE(profile_manager()->CreateTestingProfile("Test 3"));
438 const int num_profiles =
439 profile_manager()->profile_manager()->GetNumberOfProfiles();
440 ASSERT_EQ(3, num_profiles);
441
382 EXPECT_CALL(mock_observer_, OnIdle(_)).Times(1); 442 EXPECT_CALL(mock_observer_, OnIdle(_)).Times(1);
383 controller_->AddObserver(&mock_observer_); 443 controller_->AddObserver(&mock_observer_);
384 EXPECT_EQ(controller_->state(), State::kIdle); 444 EXPECT_EQ(controller_->state(), State::kIdle);
385 445
386 EXPECT_CALL(mock_observer_, OnScanning()).Times(1); 446 EXPECT_CALL(mock_observer_, OnScanning()).Times(1);
387 controller_->Scan(GetInvocationWithPromptTrigger()); 447 controller_->Scan(GetInvocationWithPromptTrigger());
388 EXPECT_EQ(controller_->state(), State::kScanning); 448 EXPECT_EQ(controller_->state(), State::kScanning);
389 449
390 base::RunLoop run_loop; 450 base::RunLoop run_loop;
391 451
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 485
426 EXPECT_EQ(controller_->state(), ExpectedFinalState()); 486 EXPECT_EQ(controller_->state(), ExpectedFinalState());
427 EXPECT_EQ(!files_to_delete_on_infected.empty(), ExpectedUwsFound()); 487 EXPECT_EQ(!files_to_delete_on_infected.empty(), ExpectedUwsFound());
428 EXPECT_EQ(!files_to_delete_on_cleaning.empty(), 488 EXPECT_EQ(!files_to_delete_on_cleaning.empty(),
429 ExpectedUwsFound() && ExpectedOnCleaningCalled()); 489 ExpectedUwsFound() && ExpectedOnCleaningCalled());
430 if (!files_to_delete_on_infected.empty() && 490 if (!files_to_delete_on_infected.empty() &&
431 !files_to_delete_on_cleaning.empty()) { 491 !files_to_delete_on_cleaning.empty()) {
432 EXPECT_EQ(files_to_delete_on_infected, files_to_delete_on_cleaning); 492 EXPECT_EQ(files_to_delete_on_infected, files_to_delete_on_cleaning);
433 } 493 }
434 494
495 EXPECT_EQ(ExpectedToTagProfiles() ? num_profiles : 0, tagged_profiles_);
496 EXPECT_EQ(ExpectedToResetSettings() ? num_profiles : 0, profiles_reset_);
497
435 controller_->RemoveObserver(&mock_observer_); 498 controller_->RemoveObserver(&mock_observer_);
436 } 499 }
437 500
438 INSTANTIATE_TEST_CASE_P( 501 INSTANTIATE_TEST_CASE_P(
439 All, 502 All,
440 ChromeCleanerControllerTest, 503 ChromeCleanerControllerTest,
441 Combine(Values(CleanerProcessStatus::kFetchFailure, 504 Combine(Values(CleanerProcessStatus::kFetchFailure,
442 CleanerProcessStatus::kFetchSuccessInvalidProcess, 505 CleanerProcessStatus::kFetchSuccessInvalidProcess,
443 CleanerProcessStatus::kFetchSuccessValidProcess), 506 CleanerProcessStatus::kFetchSuccessValidProcess),
444 Values(CrashPoint::kNone, 507 Values(CrashPoint::kNone,
445 CrashPoint::kOnStartup, 508 CrashPoint::kOnStartup,
446 CrashPoint::kAfterConnection, 509 CrashPoint::kAfterConnection,
447 // CrashPoint::kAfterRequestSent is not used because we 510 // CrashPoint::kAfterRequestSent is not used because we
448 // cannot ensure the order between the Mojo request being 511 // cannot ensure the order between the Mojo request being
449 // received by Chrome and the connection being lost. 512 // received by Chrome and the connection being lost.
450 CrashPoint::kAfterResponseReceived), 513 CrashPoint::kAfterResponseReceived),
451 Values(UwsFoundStatus::kNoUwsFound, 514 Values(UwsFoundStatus::kNoUwsFound,
452 UwsFoundStatus::kUwsFoundRebootRequired, 515 UwsFoundStatus::kUwsFoundRebootRequired,
453 UwsFoundStatus::kUwsFoundNoRebootRequired), 516 UwsFoundStatus::kUwsFoundNoRebootRequired),
454 Values(UserResponse::kAccepted, 517 Values(UserResponse::kAccepted,
455 UserResponse::kDenied, 518 UserResponse::kDenied,
456 UserResponse::kDismissed))); 519 UserResponse::kDismissed)));
457 520
458 } // namespace 521 } // namespace
459 } // namespace safe_browsing 522 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698