| OLD | NEW |
| 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/mock_chrome_cleaner_proces
s_win.h" | 5 #include "chrome/browser/safe_browsing/chrome_cleaner/mock_chrome_cleaner_proces
s_win.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/threading/sequenced_task_runner_handle.h" | 16 #include "base/threading/sequenced_task_runner_handle.h" |
| 16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 17 #include "mojo/edk/embedder/connection_params.h" | 18 #include "mojo/edk/embedder/connection_params.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 using ::chrome_cleaner::mojom::ChromePrompt; | 32 using ::chrome_cleaner::mojom::ChromePrompt; |
| 32 using ::chrome_cleaner::mojom::ChromePromptPtr; | 33 using ::chrome_cleaner::mojom::ChromePromptPtr; |
| 33 using ::chrome_cleaner::mojom::ChromePromptPtrInfo; | 34 using ::chrome_cleaner::mojom::ChromePromptPtrInfo; |
| 34 using ::chrome_cleaner::mojom::PromptAcceptance; | 35 using ::chrome_cleaner::mojom::PromptAcceptance; |
| 35 | 36 |
| 36 constexpr char kCrashPointSwitch[] = "mock-crash-point"; | 37 constexpr char kCrashPointSwitch[] = "mock-crash-point"; |
| 37 constexpr char kUwsFoundSwitch[] = "mock-uws-found"; | 38 constexpr char kUwsFoundSwitch[] = "mock-uws-found"; |
| 38 constexpr char kRebootRequiredSwitch[] = "mock-reboot-required"; | 39 constexpr char kRebootRequiredSwitch[] = "mock-reboot-required"; |
| 40 constexpr char kExpectedUserResponseSwitch[] = "mock-expected-user-response"; |
| 39 | 41 |
| 40 } // namespace | 42 } // namespace |
| 41 | 43 |
| 42 // static | 44 // static |
| 43 bool MockChromeCleanerProcess::Options::FromCommandLine( | 45 bool MockChromeCleanerProcess::Options::FromCommandLine( |
| 44 const base::CommandLine& command_line, | 46 const base::CommandLine& command_line, |
| 45 Options* options) { | 47 Options* options) { |
| 46 options->SetDoFindUws(command_line.HasSwitch(kUwsFoundSwitch)); | 48 options->SetDoFindUws(command_line.HasSwitch(kUwsFoundSwitch)); |
| 47 options->set_reboot_required(command_line.HasSwitch(kRebootRequiredSwitch)); | 49 options->set_reboot_required(command_line.HasSwitch(kRebootRequiredSwitch)); |
| 48 | 50 |
| 49 if (command_line.HasSwitch(kCrashPointSwitch)) { | 51 if (command_line.HasSwitch(kCrashPointSwitch)) { |
| 50 int crash_point_int = 0; | 52 int crash_point_int = 0; |
| 51 if (base::StringToInt(command_line.GetSwitchValueASCII(kCrashPointSwitch), | 53 if (base::StringToInt(command_line.GetSwitchValueASCII(kCrashPointSwitch), |
| 52 &crash_point_int) && | 54 &crash_point_int) && |
| 53 crash_point_int >= 0 && | 55 crash_point_int >= 0 && |
| 54 crash_point_int < static_cast<int>(CrashPoint::kNumCrashPoints)) { | 56 crash_point_int < static_cast<int>(CrashPoint::kNumCrashPoints)) { |
| 55 options->set_crash_point(static_cast<CrashPoint>(crash_point_int)); | 57 options->set_crash_point(static_cast<CrashPoint>(crash_point_int)); |
| 56 } else { | 58 } else { |
| 57 return false; | 59 return false; |
| 58 } | 60 } |
| 59 } | 61 } |
| 60 | 62 |
| 63 if (command_line.HasSwitch(kExpectedUserResponseSwitch)) { |
| 64 int expected_response_int = 0; |
| 65 if (base::StringToInt( |
| 66 command_line.GetSwitchValueASCII(kExpectedUserResponseSwitch), |
| 67 &expected_response_int) && |
| 68 expected_response_int >= 0 && |
| 69 expected_response_int < |
| 70 static_cast<int>(PromptAcceptance::NUM_VALUES)) { |
| 71 options->set_expected_user_response( |
| 72 static_cast<PromptAcceptance>(expected_response_int)); |
| 73 } else { |
| 74 return false; |
| 75 } |
| 76 } |
| 77 |
| 61 return true; | 78 return true; |
| 62 } | 79 } |
| 63 | 80 |
| 64 MockChromeCleanerProcess::Options::Options() = default; | 81 MockChromeCleanerProcess::Options::Options() = default; |
| 65 | 82 |
| 66 MockChromeCleanerProcess::Options::Options(const Options& other) | 83 MockChromeCleanerProcess::Options::Options(const Options& other) |
| 67 : files_to_delete_(other.files_to_delete_), | 84 : files_to_delete_(other.files_to_delete_), |
| 68 reboot_required_(other.reboot_required_), | 85 reboot_required_(other.reboot_required_), |
| 69 crash_point_(other.crash_point_) { | 86 crash_point_(other.crash_point_), |
| 70 } | 87 expected_user_response_(other.expected_user_response_) {} |
| 71 | 88 |
| 72 MockChromeCleanerProcess::Options& MockChromeCleanerProcess::Options::operator=( | 89 MockChromeCleanerProcess::Options& MockChromeCleanerProcess::Options::operator=( |
| 73 const Options& other) { | 90 const Options& other) { |
| 74 files_to_delete_ = other.files_to_delete_; | 91 files_to_delete_ = other.files_to_delete_; |
| 75 reboot_required_ = other.reboot_required_; | 92 reboot_required_ = other.reboot_required_; |
| 76 crash_point_ = other.crash_point_; | 93 crash_point_ = other.crash_point_; |
| 94 expected_user_response_ = other.expected_user_response_; |
| 77 return *this; | 95 return *this; |
| 78 } | 96 } |
| 79 | 97 |
| 80 MockChromeCleanerProcess::Options::~Options() {} | 98 MockChromeCleanerProcess::Options::~Options() {} |
| 81 | 99 |
| 82 void MockChromeCleanerProcess::Options::AddSwitchesToCommandLine( | 100 void MockChromeCleanerProcess::Options::AddSwitchesToCommandLine( |
| 83 base::CommandLine* command_line) const { | 101 base::CommandLine* command_line) const { |
| 84 if (!files_to_delete_.empty()) | 102 if (!files_to_delete_.empty()) |
| 85 command_line->AppendSwitch(kUwsFoundSwitch); | 103 command_line->AppendSwitch(kUwsFoundSwitch); |
| 86 | 104 |
| 87 if (reboot_required()) | 105 if (reboot_required()) |
| 88 command_line->AppendSwitch(kRebootRequiredSwitch); | 106 command_line->AppendSwitch(kRebootRequiredSwitch); |
| 89 | 107 |
| 90 if (crash_point() != CrashPoint::kNone) { | 108 if (crash_point() != CrashPoint::kNone) { |
| 91 command_line->AppendSwitchASCII( | 109 command_line->AppendSwitchASCII( |
| 92 kCrashPointSwitch, base::IntToString(static_cast<int>(crash_point()))); | 110 kCrashPointSwitch, base::IntToString(static_cast<int>(crash_point()))); |
| 93 } | 111 } |
| 112 |
| 113 if (expected_user_response() != PromptAcceptance::UNSPECIFIED) { |
| 114 command_line->AppendSwitchASCII( |
| 115 kExpectedUserResponseSwitch, |
| 116 base::IntToString(static_cast<int>(expected_user_response()))); |
| 117 } |
| 94 } | 118 } |
| 95 | 119 |
| 96 void MockChromeCleanerProcess::Options::SetDoFindUws(bool do_find_uws) { | 120 void MockChromeCleanerProcess::Options::SetDoFindUws(bool do_find_uws) { |
| 97 files_to_delete_.clear(); | 121 files_to_delete_.clear(); |
| 98 if (!do_find_uws) | 122 if (!do_find_uws) |
| 99 return; | 123 return; |
| 100 | 124 |
| 101 files_to_delete_.insert( | 125 files_to_delete_.insert( |
| 102 base::FilePath(FILE_PATH_LITERAL("/path/to/file1.exe"))); | 126 base::FilePath(FILE_PATH_LITERAL("/path/to/file1.exe"))); |
| 103 files_to_delete_.insert( | 127 files_to_delete_.insert( |
| 104 base::FilePath(FILE_PATH_LITERAL("/path/to/other/file2.exe"))); | 128 base::FilePath(FILE_PATH_LITERAL("/path/to/other/file2.exe"))); |
| 105 files_to_delete_.insert( | 129 files_to_delete_.insert( |
| 106 base::FilePath(FILE_PATH_LITERAL("/path/to/some file.dll"))); | 130 base::FilePath(FILE_PATH_LITERAL("/path/to/some file.dll"))); |
| 107 } | 131 } |
| 108 | 132 |
| 109 int MockChromeCleanerProcess::Options::ExpectedExitCode( | 133 int MockChromeCleanerProcess::Options::ExpectedExitCode( |
| 110 PromptAcceptance received_prompt_acceptance) const { | 134 PromptAcceptance received_prompt_acceptance) const { |
| 111 if (crash_point() != CrashPoint::kNone) | 135 if (crash_point() != CrashPoint::kNone) |
| 112 return kDeliberateCrashExitCode; | 136 return kDeliberateCrashExitCode; |
| 113 | 137 |
| 114 if (files_to_delete_.empty()) | 138 if (files_to_delete_.empty()) |
| 115 return kNothingFoundExitCode; | 139 return kNothingFoundExitCode; |
| 116 | 140 |
| 117 if (received_prompt_acceptance == PromptAcceptance::ACCEPTED) { | 141 if (received_prompt_acceptance == PromptAcceptance::ACCEPTED_WITH_LOGS || |
| 142 received_prompt_acceptance == PromptAcceptance::ACCEPTED_WITHOUT_LOGS) { |
| 118 return reboot_required() ? kRebootRequiredExitCode | 143 return reboot_required() ? kRebootRequiredExitCode |
| 119 : kRebootNotRequiredExitCode; | 144 : kRebootNotRequiredExitCode; |
| 120 } | 145 } |
| 121 | 146 |
| 122 return kDeclinedExitCode; | 147 return kDeclinedExitCode; |
| 123 } | 148 } |
| 124 | 149 |
| 125 MockChromeCleanerProcess::MockChromeCleanerProcess( | 150 MockChromeCleanerProcess::MockChromeCleanerProcess( |
| 126 const Options& options, | 151 const Options& options, |
| 127 const std::string& chrome_mojo_pipe_token) | 152 const std::string& chrome_mojo_pipe_token) |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 199 |
| 175 io_thread.task_runner()->PostTask( | 200 io_thread.task_runner()->PostTask( |
| 176 FROM_HERE, | 201 FROM_HERE, |
| 177 base::BindOnce(&MockChromeCleanerProcess::SendScanResults, | 202 base::BindOnce(&MockChromeCleanerProcess::SendScanResults, |
| 178 base::Unretained(this), std::move(prompt_ptr_info), | 203 base::Unretained(this), std::move(prompt_ptr_info), |
| 179 base::Passed(&quit_closure))); | 204 base::Passed(&quit_closure))); |
| 180 | 205 |
| 181 run_loop.Run(); | 206 run_loop.Run(); |
| 182 | 207 |
| 183 EXPECT_NE(received_prompt_acceptance_, PromptAcceptance::UNSPECIFIED); | 208 EXPECT_NE(received_prompt_acceptance_, PromptAcceptance::UNSPECIFIED); |
| 209 EXPECT_EQ(received_prompt_acceptance_, options_.expected_user_response()); |
| 184 if (::testing::Test::HasFailure()) | 210 if (::testing::Test::HasFailure()) |
| 185 return kInternalTestFailureExitCode; | 211 return kInternalTestFailureExitCode; |
| 186 return options_.ExpectedExitCode(received_prompt_acceptance_); | 212 return options_.ExpectedExitCode(received_prompt_acceptance_); |
| 187 } | 213 } |
| 188 | 214 |
| 189 void MockChromeCleanerProcess::SendScanResults( | 215 void MockChromeCleanerProcess::SendScanResults( |
| 190 ChromePromptPtrInfo prompt_ptr_info, | 216 ChromePromptPtrInfo prompt_ptr_info, |
| 191 base::OnceClosure quit_closure) { | 217 base::OnceClosure quit_closure) { |
| 192 // This pointer will be deleted by PromptUserCallback. | 218 // This pointer will be deleted by PromptUserCallback. |
| 193 chrome_prompt_ptr_ = new ChromePromptPtr(); | 219 chrome_prompt_ptr_ = new ChromePromptPtr(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 217 | 243 |
| 218 received_prompt_acceptance_ = prompt_acceptance; | 244 received_prompt_acceptance_ = prompt_acceptance; |
| 219 | 245 |
| 220 if (options_.crash_point() == CrashPoint::kAfterResponseReceived) | 246 if (options_.crash_point() == CrashPoint::kAfterResponseReceived) |
| 221 exit(kDeliberateCrashExitCode); | 247 exit(kDeliberateCrashExitCode); |
| 222 | 248 |
| 223 std::move(quit_closure).Run(); | 249 std::move(quit_closure).Run(); |
| 224 } | 250 } |
| 225 | 251 |
| 226 } // namespace safe_browsing | 252 } // namespace safe_browsing |
| OLD | NEW |