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

Side by Side Diff: remoting/host/win/worker_process_launcher_unittest.cc

Issue 2448243004: Change WorkerProcessLauncherTest to use ChannelMojo. (Closed)
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "remoting/host/win/worker_process_launcher.h" 5 #include "remoting/host/win/worker_process_launcher.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
17 #include "base/win/scoped_handle.h" 17 #include "base/win/scoped_handle.h"
18 #include "base/win/scoped_process_information.h" 18 #include "base/win/scoped_process_information.h"
19 #include "ipc/ipc_channel.h" 19 #include "ipc/ipc_channel.h"
20 #include "ipc/ipc_channel_proxy.h" 20 #include "ipc/ipc_channel_proxy.h"
21 #include "ipc/ipc_listener.h" 21 #include "ipc/ipc_listener.h"
22 #include "ipc/ipc_message.h" 22 #include "ipc/ipc_message.h"
joedow 2016/10/27 18:41:42 Do you want to IWYU here for the mojo class(es)?
Sam McNally 2016/10/27 22:22:39 Done.
23 #include "remoting/base/auto_thread_task_runner.h" 23 #include "remoting/base/auto_thread_task_runner.h"
24 #include "remoting/host/chromoting_messages.h" 24 #include "remoting/host/chromoting_messages.h"
25 #include "remoting/host/host_exit_codes.h" 25 #include "remoting/host/host_exit_codes.h"
26 #include "remoting/host/ipc_util.h"
27 #include "remoting/host/win/launch_process_with_token.h" 26 #include "remoting/host/win/launch_process_with_token.h"
28 #include "remoting/host/worker_process_ipc_delegate.h" 27 #include "remoting/host/worker_process_ipc_delegate.h"
29 #include "testing/gmock/include/gmock/gmock.h" 28 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gmock_mutant.h" 29 #include "testing/gmock_mutant.h"
31 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
32 31
33 using base::win::ScopedHandle; 32 using base::win::ScopedHandle;
34 using testing::_; 33 using testing::_;
35 using testing::AnyNumber; 34 using testing::AnyNumber;
36 using testing::CreateFunctor; 35 using testing::CreateFunctor;
37 using testing::DoAll; 36 using testing::DoAll;
38 using testing::Expectation; 37 using testing::Expectation;
39 using testing::Invoke; 38 using testing::Invoke;
40 using testing::InvokeWithoutArgs; 39 using testing::InvokeWithoutArgs;
41 using testing::Return; 40 using testing::Return;
42 41
43 namespace remoting { 42 namespace remoting {
44 43
45 namespace { 44 namespace {
46 45
47 const char kIpcSecurityDescriptor[] = "D:(A;;GA;;;AU)";
48
49 class MockProcessLauncherDelegate : public WorkerProcessLauncher::Delegate { 46 class MockProcessLauncherDelegate : public WorkerProcessLauncher::Delegate {
50 public: 47 public:
51 MockProcessLauncherDelegate() {} 48 MockProcessLauncherDelegate() {}
52 ~MockProcessLauncherDelegate() override {} 49 ~MockProcessLauncherDelegate() override {}
53 50
54 // WorkerProcessLauncher::Delegate interface. 51 // WorkerProcessLauncher::Delegate interface.
55 MOCK_METHOD1(LaunchProcess, void(WorkerProcessLauncher*)); 52 MOCK_METHOD1(LaunchProcess, void(WorkerProcessLauncher*));
56 MOCK_METHOD1(Send, void(IPC::Message*)); 53 MOCK_METHOD1(Send, void(IPC::Message*));
57 MOCK_METHOD0(CloseChannel, void()); 54 MOCK_METHOD0(CloseChannel, void());
58 MOCK_METHOD0(KillProcess, void()); 55 MOCK_METHOD0(KillProcess, void());
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 161
165 // Receives messages sent to the worker process. 162 // Receives messages sent to the worker process.
166 MockWorkerListener client_listener_; 163 MockWorkerListener client_listener_;
167 164
168 // Receives messages sent from the worker process. 165 // Receives messages sent from the worker process.
169 MockIpcDelegate server_listener_; 166 MockIpcDelegate server_listener_;
170 167
171 // Implements WorkerProcessLauncher::Delegate. 168 // Implements WorkerProcessLauncher::Delegate.
172 std::unique_ptr<MockProcessLauncherDelegate> launcher_delegate_; 169 std::unique_ptr<MockProcessLauncherDelegate> launcher_delegate_;
173 170
174 // The name of the IPC channel. 171 // The client handle to the channel.
175 std::string channel_name_; 172 mojo::ScopedMessagePipeHandle client_channel_handle_;
176 173
177 // Client and server ends of the IPC channel. 174 // Client and server ends of the IPC channel.
178 std::unique_ptr<IPC::ChannelProxy> channel_client_; 175 std::unique_ptr<IPC::ChannelProxy> channel_client_;
179 std::unique_ptr<IPC::ChannelProxy> channel_server_; 176 std::unique_ptr<IPC::ChannelProxy> channel_server_;
180 177
181 WorkerProcessLauncher* event_handler_; 178 WorkerProcessLauncher* event_handler_;
182 179
183 // The worker process launcher. 180 // The worker process launcher.
184 std::unique_ptr<WorkerProcessLauncher> launcher_; 181 std::unique_ptr<WorkerProcessLauncher> launcher_;
185 182
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 259
263 task_runner_->PostTask( 260 task_runner_->PostTask(
264 FROM_HERE, 261 FROM_HERE,
265 base::Bind(&WorkerProcessLauncherTest::StopWorker, 262 base::Bind(&WorkerProcessLauncherTest::StopWorker,
266 base::Unretained(this))); 263 base::Unretained(this)));
267 } 264 }
268 265
269 void WorkerProcessLauncherTest::KillProcess() { 266 void WorkerProcessLauncherTest::KillProcess() {
270 event_handler_ = nullptr; 267 event_handler_ = nullptr;
271 268
269 DisconnectClient();
272 if (worker_process_.IsValid()) { 270 if (worker_process_.IsValid()) {
273 TerminateProcess(worker_process_.Get(), CONTROL_C_EXIT); 271 TerminateProcess(worker_process_.Get(), CONTROL_C_EXIT);
274 worker_process_.Close(); 272 worker_process_.Close();
275 } 273 }
276 } 274 }
277 275
278 void WorkerProcessLauncherTest::TerminateWorker(DWORD exit_code) { 276 void WorkerProcessLauncherTest::TerminateWorker(DWORD exit_code) {
279 if (worker_process_.IsValid()) 277 if (worker_process_.IsValid())
280 TerminateProcess(worker_process_.Get(), exit_code); 278 TerminateProcess(worker_process_.Get(), exit_code);
281 } 279 }
282 280
283 void WorkerProcessLauncherTest::ConnectClient() { 281 void WorkerProcessLauncherTest::ConnectClient() {
284 channel_client_ = IPC::ChannelProxy::Create(IPC::ChannelHandle(channel_name_), 282 channel_client_ = IPC::ChannelProxy::Create(client_channel_handle_.release(),
285 IPC::Channel::MODE_CLIENT, 283 IPC::Channel::MODE_CLIENT,
286 &client_listener_, 284 &client_listener_, task_runner_);
287 task_runner_);
288 285
289 // Pretend that |kLaunchSuccessTimeoutSeconds| passed since launching 286 // Pretend that |kLaunchSuccessTimeoutSeconds| passed since launching
290 // the worker process. This will make the backoff algorithm think that this 287 // the worker process. This will make the backoff algorithm think that this
291 // launch attempt was successful and it will not delay the next launch. 288 // launch attempt was successful and it will not delay the next launch.
292 launcher_->RecordSuccessfulLaunchForTest(); 289 launcher_->RecordSuccessfulLaunchForTest();
293 } 290 }
294 291
295 void WorkerProcessLauncherTest::DisconnectClient() { 292 void WorkerProcessLauncherTest::DisconnectClient() {
296 channel_client_.reset(); 293 if (channel_client_) {
294 channel_client_->Close();
295 channel_client_.reset();
296 }
297 } 297 }
298 298
299 void WorkerProcessLauncherTest::DisconnectServer() { 299 void WorkerProcessLauncherTest::DisconnectServer() {
300 channel_server_.reset(); 300 if (channel_server_) {
301 channel_server_->Close();
302 channel_server_.reset();
303 }
301 } 304 }
302 305
303 void WorkerProcessLauncherTest::SendToProcess(IPC::Message* message) { 306 void WorkerProcessLauncherTest::SendToProcess(IPC::Message* message) {
304 if (channel_server_) { 307 if (channel_server_) {
305 channel_server_->Send(message); 308 channel_server_->Send(message);
306 return; 309 return;
307 } 310 }
308 311
309 delete message; 312 delete message;
310 } 313 }
311 314
312 void WorkerProcessLauncherTest::SendFakeMessageToLauncher() { 315 void WorkerProcessLauncherTest::SendFakeMessageToLauncher() {
313 if (channel_client_) { 316 if (channel_client_) {
314 channel_client_->Send( 317 channel_client_->Send(
315 new ChromotingDesktopNetworkMsg_DisconnectSession(protocol::OK)); 318 new ChromotingDesktopNetworkMsg_DisconnectSession(protocol::OK));
316 } 319 }
317 } 320 }
318 321
319 void WorkerProcessLauncherTest::CrashWorker() { 322 void WorkerProcessLauncherTest::CrashWorker() {
320 launcher_->Crash(FROM_HERE); 323 launcher_->Crash(FROM_HERE);
321 } 324 }
322 325
323 void WorkerProcessLauncherTest::StartWorker() { 326 void WorkerProcessLauncherTest::StartWorker() {
324 launcher_.reset(new WorkerProcessLauncher(std::move(launcher_delegate_), 327 launcher_.reset(new WorkerProcessLauncher(std::move(launcher_delegate_),
325 &server_listener_)); 328 &server_listener_));
326 329
327 launcher_->SetKillProcessTimeoutForTest(base::TimeDelta::FromMilliseconds(0)); 330 launcher_->SetKillProcessTimeoutForTest(
331 base::TimeDelta::FromMilliseconds(100));
328 } 332 }
329 333
330 void WorkerProcessLauncherTest::StopWorker() { 334 void WorkerProcessLauncherTest::StopWorker() {
331 launcher_.reset(); 335 launcher_.reset();
332 DisconnectClient(); 336 DisconnectClient();
333 channel_name_.clear(); 337 client_channel_handle_.reset();
334 channel_server_.reset(); 338 channel_server_.reset();
335 task_runner_ = nullptr; 339 task_runner_ = nullptr;
336 } 340 }
337 341
338 void WorkerProcessLauncherTest::QuitMainMessageLoop() { 342 void WorkerProcessLauncherTest::QuitMainMessageLoop() {
339 message_loop_.task_runner()->PostTask( 343 message_loop_.task_runner()->PostTask(
340 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); 344 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
341 } 345 }
342 346
343 void WorkerProcessLauncherTest::DoLaunchProcess() { 347 void WorkerProcessLauncherTest::DoLaunchProcess() {
(...skipping 15 matching lines...) Expand all
359 FALSE, // do not inherit handles 363 FALSE, // do not inherit handles
360 CREATE_SUSPENDED, 364 CREATE_SUSPENDED,
361 nullptr, // no environment 365 nullptr, // no environment
362 nullptr, // default current directory 366 nullptr, // default current directory
363 &startup_info, 367 &startup_info,
364 &temp_process_info)); 368 &temp_process_info));
365 base::win::ScopedProcessInformation process_information(temp_process_info); 369 base::win::ScopedProcessInformation process_information(temp_process_info);
366 worker_process_.Set(process_information.TakeProcessHandle()); 370 worker_process_.Set(process_information.TakeProcessHandle());
367 ASSERT_TRUE(worker_process_.IsValid()); 371 ASSERT_TRUE(worker_process_.IsValid());
368 372
369 channel_name_ = IPC::Channel::GenerateUniqueRandomChannelID(); 373 mojo::MessagePipe pipe;
370 ScopedHandle pipe; 374 client_channel_handle_ = std::move(pipe.handle0);
371 ASSERT_TRUE(CreateIpcChannel(channel_name_, kIpcSecurityDescriptor, &pipe));
372 375
373 // Wrap the pipe into an IPC channel. 376 // Wrap the pipe into an IPC channel.
374 channel_server_ = IPC::ChannelProxy::Create( 377 channel_server_ = IPC::ChannelProxy::Create(
375 IPC::ChannelHandle(pipe.Get()), IPC::Channel::MODE_SERVER, this, 378 pipe.handle1.release(), IPC::Channel::MODE_SERVER, this, task_runner_);
376 task_runner_);
377 379
378 HANDLE temp_handle; 380 HANDLE temp_handle;
379 ASSERT_TRUE(DuplicateHandle(GetCurrentProcess(), worker_process_.Get(), 381 ASSERT_TRUE(DuplicateHandle(GetCurrentProcess(), worker_process_.Get(),
380 GetCurrentProcess(), &temp_handle, 0, FALSE, 382 GetCurrentProcess(), &temp_handle, 0, FALSE,
381 DUPLICATE_SAME_ACCESS)); 383 DUPLICATE_SAME_ACCESS));
382 event_handler_->OnProcessLaunched(ScopedHandle(temp_handle)); 384 event_handler_->OnProcessLaunched(ScopedHandle(temp_handle));
383 } 385 }
384 386
385 TEST_F(WorkerProcessLauncherTest, Start) { 387 TEST_F(WorkerProcessLauncherTest, Start) {
386 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_)) 388 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_))
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 EXPECT_CALL(client_listener_, OnCrash(_, _, _)) 532 EXPECT_CALL(client_listener_, OnCrash(_, _, _))
531 .Times(1) 533 .Times(1)
532 .WillOnce(InvokeWithoutArgs( 534 .WillOnce(InvokeWithoutArgs(
533 this, &WorkerProcessLauncherTest::SendFakeMessageToLauncher)); 535 this, &WorkerProcessLauncherTest::SendFakeMessageToLauncher));
534 536
535 StartWorker(); 537 StartWorker();
536 base::RunLoop().Run(); 538 base::RunLoop().Run();
537 } 539 }
538 540
539 } // namespace remoting 541 } // namespace remoting
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698