OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/safe_browsing/chrome_cleaner/mock_chrome_cleaner_proces s_win.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind_helpers.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/run_loop.h" | |
13 #include "base/sequenced_task_runner.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/threading/sequenced_task_runner_handle.h" | |
16 #include "base/threading/thread.h" | |
17 #include "mojo/edk/embedder/connection_params.h" | |
18 #include "mojo/edk/embedder/embedder.h" | |
19 #include "mojo/edk/embedder/incoming_broker_client_invitation.h" | |
20 #include "mojo/edk/embedder/outgoing_broker_client_invitation.h" | |
21 #include "mojo/edk/embedder/platform_channel_pair.h" | |
22 #include "mojo/edk/embedder/scoped_ipc_support.h" | |
23 #include "mojo/edk/embedder/transport_protocol.h" | |
24 #include "testing/gmock/include/gmock/gmock.h" | |
25 #include "testing/gtest/include/gtest/gtest.h" | |
26 | |
27 namespace safe_browsing { | |
28 | |
29 namespace { | |
30 | |
31 using ::chrome_cleaner::mojom::ChromePrompt; | |
32 using ::chrome_cleaner::mojom::ChromePromptPtr; | |
33 using ::chrome_cleaner::mojom::ChromePromptPtrInfo; | |
34 using ::chrome_cleaner::mojom::ElevationStatus; | |
35 using ::chrome_cleaner::mojom::PromptAcceptance; | |
36 using ::chrome_cleaner::mojom::UwSPtr; | |
37 | |
38 constexpr char kCrashPointSwitch[] = "mock-crash-point"; | |
39 constexpr char kUwsFoundSwitch[] = "mock-uws-found"; | |
40 constexpr char kRebootRequiredSwitch[] = "mock-reboot-required"; | |
41 | |
42 std::vector<UwSPtr> clone_uwsptr_vector(const std::vector<UwSPtr>& uws_ptrs) { | |
43 std::vector<UwSPtr> cloned; | |
44 for (const UwSPtr& uws_ptr : uws_ptrs) | |
45 cloned.push_back(uws_ptr->Clone()); | |
46 return cloned; | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 // static | |
52 bool MockChromeCleanerProcess::Options::FromCommandLine( | |
53 const base::CommandLine& command_line, | |
54 Options* options) { | |
55 options->SetDoFindUws(command_line.HasSwitch(kUwsFoundSwitch)); | |
56 options->set_reboot_required(command_line.HasSwitch(kRebootRequiredSwitch)); | |
57 | |
58 if (command_line.HasSwitch(kCrashPointSwitch)) { | |
59 int crash_point_int = 0; | |
60 if (base::StringToInt(command_line.GetSwitchValueASCII(kCrashPointSwitch), | |
61 &crash_point_int) && | |
62 crash_point_int >= 0 && | |
63 crash_point_int < static_cast<int>(CrashPoint::kNumCrashPoints)) { | |
64 options->set_crash_point(static_cast<CrashPoint>(crash_point_int)); | |
65 } else { | |
66 return false; | |
67 } | |
68 } | |
69 | |
70 return true; | |
71 } | |
72 | |
73 MockChromeCleanerProcess::Options::Options() = default; | |
74 | |
75 MockChromeCleanerProcess::Options::Options(const Options& other) | |
76 : files_to_delete_(other.files_to_delete_), | |
77 reboot_required_(other.reboot_required_), | |
78 crash_point_(other.crash_point_) { | |
79 found_uws_ = clone_uwsptr_vector(other.found_uws_); | |
80 } | |
81 | |
82 MockChromeCleanerProcess::Options& MockChromeCleanerProcess::Options::operator=( | |
83 const Options& other) { | |
84 found_uws_ = clone_uwsptr_vector(other.found_uws_); | |
85 files_to_delete_ = other.files_to_delete_; | |
86 reboot_required_ = other.reboot_required_; | |
87 crash_point_ = other.crash_point_; | |
88 return *this; | |
89 } | |
90 | |
91 MockChromeCleanerProcess::Options::~Options() {} | |
92 | |
93 void MockChromeCleanerProcess::Options::AddSwitchesToCommandLine( | |
94 base::CommandLine* command_line) const { | |
95 if (!found_uws_.empty()) | |
96 command_line->AppendSwitch(kUwsFoundSwitch); | |
97 | |
98 if (reboot_required()) | |
99 command_line->AppendSwitch(kRebootRequiredSwitch); | |
100 | |
101 if (crash_point() != CrashPoint::kNone) { | |
102 command_line->AppendSwitchASCII( | |
103 kCrashPointSwitch, base::IntToString(static_cast<int>(crash_point()))); | |
104 } | |
105 } | |
106 | |
107 void MockChromeCleanerProcess::Options::SetDoFindUws(bool do_find_uws) { | |
108 found_uws_.clear(); | |
109 files_to_delete_.clear(); | |
110 if (!do_find_uws) | |
111 return; | |
112 | |
113 UwSPtr uws1 = chrome_cleaner::mojom::UwS::New(); | |
114 uws1->id = 1; | |
115 uws1->name = "Some UwS"; | |
116 uws1->observed_behaviours = chrome_cleaner::mojom::ObservedBehaviours::New(); | |
117 uws1->files_to_delete.push_back( | |
118 base::FilePath((FILE_PATH_LITERAL("/path/to/file1.exe")))); | |
119 uws1->files_to_delete.push_back( | |
120 base::FilePath((FILE_PATH_LITERAL("/path/to/other/file2.exe")))); | |
121 found_uws_.push_back(std::move(uws1)); | |
122 | |
123 UwSPtr uws2 = chrome_cleaner::mojom::UwS::New(); | |
124 uws2->id = 2; | |
125 uws2->name = "Other UwS"; | |
126 uws2->observed_behaviours = chrome_cleaner::mojom::ObservedBehaviours::New(); | |
127 uws2->files_to_delete.push_back( | |
128 base::FilePath((FILE_PATH_LITERAL("/path/to/some file.dll")))); | |
129 found_uws_.push_back(std::move(uws2)); | |
130 | |
131 for (const UwSPtr& uws_ptr : found_uws_) { | |
132 for (const base::FilePath& filepath : uws_ptr->files_to_delete) | |
133 files_to_delete_.insert(filepath); | |
csharp
2017/05/24 20:49:35
Why not insert all the files for each uws at once?
alito
2017/05/24 22:20:50
Indeed. It's just that I love for loops so much.
| |
134 } | |
135 } | |
136 | |
137 int MockChromeCleanerProcess::Options::ExpectedExitCode( | |
138 PromptAcceptance received_prompt_acceptance) const { | |
139 if (crash_point() != CrashPoint::kNone) | |
140 return kDeliberateCrashExitCode; | |
141 | |
142 if (found_uws_.empty()) | |
143 return kNothingFoundExitCode; | |
144 | |
145 if (received_prompt_acceptance == PromptAcceptance::ACCEPTED_WITH_LOGS || | |
146 received_prompt_acceptance == PromptAcceptance::ACCEPTED_WITHOUT_LOGS) { | |
147 return reboot_required() ? kRebootRequiredExitCode | |
148 : kRebootNotRequiredExitCode; | |
149 } | |
150 | |
151 return kCanceledExitCode; | |
csharp
2017/05/24 20:49:35
nit: I think this might be clearer as a new exit c
alito
2017/05/24 22:20:50
Done.
| |
152 } | |
153 | |
154 MockChromeCleanerProcess::MockChromeCleanerProcess( | |
155 const Options& options, | |
156 const std::string& chrome_mojo_pipe_token) | |
157 : options_(options), chrome_mojo_pipe_token_(chrome_mojo_pipe_token) {} | |
158 | |
159 int MockChromeCleanerProcess::Run() { | |
160 // We use EXPECT_*() macros to get good log lines, but since this code is run | |
161 // in a separate process, failing a check in an EXPECT_*() macro will not fail | |
162 // the test. Therefore, we use ::testing::Test::HasFailure() to detect | |
163 // EXPECT_*() failures and return an error code that indicates that the test | |
164 // should fail. | |
165 EXPECT_FALSE(chrome_mojo_pipe_token_.empty()); | |
166 if (::testing::Test::HasFailure()) | |
167 return kInternalTestFailureExitCode; | |
168 | |
169 if (options_.crash_point() == CrashPoint::kOnStartup) | |
170 exit(kDeliberateCrashExitCode); | |
171 | |
172 mojo::edk::Init(); | |
173 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | |
174 base::Thread io_thread("IPCThread"); | |
175 EXPECT_TRUE(io_thread.StartWithOptions(thread_options)); | |
176 if (::testing::Test::HasFailure()) | |
177 return kInternalTestFailureExitCode; | |
178 | |
179 mojo::edk::ScopedIPCSupport ipc_support( | |
180 io_thread.task_runner(), | |
181 mojo::edk::ScopedIPCSupport::ShutdownPolicy::CLEAN); | |
182 | |
183 auto invitation = | |
184 mojo::edk::IncomingBrokerClientInvitation::AcceptFromCommandLine( | |
185 mojo::edk::TransportProtocol::kLegacy); | |
186 ChromePromptPtrInfo prompt_ptr_info( | |
187 invitation->ExtractMessagePipe(chrome_mojo_pipe_token_), 0); | |
188 | |
189 if (options_.crash_point() == CrashPoint::kAfterConnection) | |
190 exit(kDeliberateCrashExitCode); | |
191 | |
192 base::MessageLoop message_loop; | |
193 base::RunLoop run_loop; | |
194 // After the response from the parent process is received, this will post a | |
195 // task to unblock the child process's main thread. | |
196 auto quit_closure = base::BindOnce( | |
197 [](scoped_refptr<base::SequencedTaskRunner> main_runner, | |
198 base::Closure quit_closure) { | |
199 main_runner->PostTask(FROM_HERE, std::move(quit_closure)); | |
200 }, | |
201 base::SequencedTaskRunnerHandle::Get(), | |
202 base::Passed(run_loop.QuitClosure())); | |
203 | |
204 io_thread.task_runner()->PostTask( | |
205 FROM_HERE, | |
206 base::BindOnce(&MockChromeCleanerProcess::SendScanResults, | |
207 base::Unretained(this), std::move(prompt_ptr_info), | |
208 base::Passed(&quit_closure))); | |
209 | |
210 run_loop.Run(); | |
211 | |
212 EXPECT_NE(received_prompt_acceptance_, PromptAcceptance::UNSPECIFIED); | |
213 if (::testing::Test::HasFailure()) | |
214 return kInternalTestFailureExitCode; | |
215 return options_.ExpectedExitCode(received_prompt_acceptance_); | |
216 } | |
217 | |
218 void MockChromeCleanerProcess::SendScanResults( | |
219 ChromePromptPtrInfo prompt_ptr_info, | |
220 base::OnceClosure quit_closure) { | |
221 // This pointer will be deleted by PromptUserCallback. | |
222 chrome_prompt_ptr_ = new ChromePromptPtr(); | |
223 chrome_prompt_ptr_->Bind(std::move(prompt_ptr_info)); | |
224 | |
225 if (options_.crash_point() == CrashPoint::kAfterRequestSent) { | |
226 // This task is posted to the IPC thread so that it will happen after the | |
227 // request is sent to the parent process and before the response gets | |
228 // handled on the IPC thread. | |
229 base::SequencedTaskRunnerHandle::Get()->PostTask( | |
230 FROM_HERE, base::Bind([]() { exit(kDeliberateCrashExitCode); })); | |
231 } | |
232 | |
233 std::vector<UwSPtr> uws_found; | |
234 for (const UwSPtr& uws_ptr : options_.found_uws()) | |
235 uws_found.push_back(uws_ptr->Clone()); | |
236 | |
237 (*chrome_prompt_ptr_) | |
238 ->PromptUser( | |
239 std::move(uws_found), ElevationStatus::REQUIRED, | |
240 base::BindOnce(&MockChromeCleanerProcess::PromptUserCallback, | |
241 base::Unretained(this), std::move(quit_closure))); | |
242 } | |
243 | |
244 void MockChromeCleanerProcess::PromptUserCallback( | |
245 base::OnceClosure quit_closure, | |
246 PromptAcceptance prompt_acceptance) { | |
247 delete chrome_prompt_ptr_; | |
248 chrome_prompt_ptr_ = nullptr; | |
249 | |
250 received_prompt_acceptance_ = prompt_acceptance; | |
251 | |
252 if (options_.crash_point() == CrashPoint::kAfterResponseReceived) | |
253 exit(kDeliberateCrashExitCode); | |
254 | |
255 std::move(quit_closure).Run(); | |
256 } | |
257 | |
258 } // namespace safe_browsing | |
OLD | NEW |