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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/win/scoped_handle.h"
10 #include "base/win/scoped_process_information.h"
11 #include "ipc/ipc_channel.h"
12 #include "ipc/ipc_channel_proxy.h"
13 #include "ipc/ipc_listener.h"
14 #include "ipc/ipc_message.h"
15 #include "remoting/base/auto_thread_task_runner.h"
16 #include "remoting/host/chromoting_messages.h"
17 #include "remoting/host/host_exit_codes.h"
18 #include "remoting/host/ipc_util.h"
19 #include "remoting/host/win/launch_process_with_token.h"
20 #include "remoting/host/win/worker_process_launcher.h"
21 #include "remoting/host/worker_process_ipc_delegate.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gmock_mutant.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 using base::win::ScopedHandle
;
28 using testing::AnyNumber
;
29 using testing::CreateFunctor
;
31 using testing::Expectation
;
32 using testing::Invoke
;
33 using testing::InvokeWithoutArgs
;
34 using testing::Return
;
40 const char kIpcSecurityDescriptor
[] = "D:(A;;GA;;;AU)";
42 class MockProcessLauncherDelegate
: public WorkerProcessLauncher::Delegate
{
44 MockProcessLauncherDelegate() {}
45 virtual ~MockProcessLauncherDelegate() {}
47 // WorkerProcessLauncher::Delegate interface.
48 MOCK_METHOD1(LaunchProcess
, void(WorkerProcessLauncher
*));
49 MOCK_METHOD1(Send
, void(IPC::Message
*));
50 MOCK_METHOD0(CloseChannel
, void());
51 MOCK_METHOD0(KillProcess
, void());
54 DISALLOW_COPY_AND_ASSIGN(MockProcessLauncherDelegate
);
57 class MockIpcDelegate
: public WorkerProcessIpcDelegate
{
60 virtual ~MockIpcDelegate() {}
62 // WorkerProcessIpcDelegate interface.
63 MOCK_METHOD1(OnChannelConnected
, void(int32
));
64 MOCK_METHOD1(OnMessageReceived
, bool(const IPC::Message
&));
65 MOCK_METHOD1(OnPermanentError
, void(int));
68 DISALLOW_COPY_AND_ASSIGN(MockIpcDelegate
);
71 class MockWorkerListener
: public IPC::Listener
{
73 MockWorkerListener() {}
74 virtual ~MockWorkerListener() {}
76 MOCK_METHOD3(OnCrash
, void(const std::string
&, const std::string
&, int));
78 // IPC::Listener implementation
79 virtual bool OnMessageReceived(const IPC::Message
& message
) override
;
82 DISALLOW_COPY_AND_ASSIGN(MockWorkerListener
);
85 bool MockWorkerListener::OnMessageReceived(const IPC::Message
& message
) {
87 IPC_BEGIN_MESSAGE_MAP(MockWorkerListener
, message
)
88 IPC_MESSAGE_HANDLER(ChromotingDaemonMsg_Crash
, OnCrash
)
89 IPC_MESSAGE_UNHANDLED(handled
= false)
99 class WorkerProcessLauncherTest
100 : public testing::Test
,
101 public IPC::Listener
{
103 WorkerProcessLauncherTest();
104 virtual ~WorkerProcessLauncherTest();
106 virtual void SetUp() override
;
107 virtual void TearDown() override
;
109 // IPC::Listener implementation.
110 virtual bool OnMessageReceived(const IPC::Message
& message
) override
;
111 virtual void OnChannelConnected(int32 peer_pid
) override
;
112 virtual void OnChannelError() override
;
114 // WorkerProcessLauncher::Delegate mocks
116 WorkerProcessLauncher
* event_handler
);
117 void LaunchProcessAndConnect(
118 WorkerProcessLauncher
* event_handler
);
119 void FailLaunchAndStopWorker(
120 WorkerProcessLauncher
* event_handler
);
123 void TerminateWorker(DWORD exit_code
);
125 // Connects the client end of the channel (the worker process's end).
126 void ConnectClient();
128 // Disconnects the client end of the channel.
129 void DisconnectClient();
131 // Disconnects the server end of the channel (the launcher's end).
132 void DisconnectServer();
134 // Sends a message to the worker process.
135 void SendToProcess(IPC::Message
* message
);
137 // Sends a fake message to the launcher.
138 void SendFakeMessageToLauncher();
140 // Requests the worker to crash.
143 // Starts the worker.
149 // Quits |message_loop_|.
150 void QuitMainMessageLoop();
153 void DoLaunchProcess();
155 base::MessageLoopForIO message_loop_
;
156 scoped_refptr
<AutoThreadTaskRunner
> task_runner_
;
158 // Receives messages sent to the worker process.
159 MockWorkerListener client_listener_
;
161 // Receives messages sent from the worker process.
162 MockIpcDelegate server_listener_
;
164 // Implements WorkerProcessLauncher::Delegate.
165 scoped_ptr
<MockProcessLauncherDelegate
> launcher_delegate_
;
167 // The name of the IPC channel.
168 std::string channel_name_
;
170 // Client and server ends of the IPC channel.
171 scoped_ptr
<IPC::ChannelProxy
> channel_client_
;
172 scoped_ptr
<IPC::ChannelProxy
> channel_server_
;
174 WorkerProcessLauncher
* event_handler_
;
176 // The worker process launcher.
177 scoped_ptr
<WorkerProcessLauncher
> launcher_
;
179 // An event that is used to emulate the worker process's handle.
180 ScopedHandle worker_process_
;
183 WorkerProcessLauncherTest::WorkerProcessLauncherTest() : event_handler_(NULL
) {
186 WorkerProcessLauncherTest::~WorkerProcessLauncherTest() {
189 void WorkerProcessLauncherTest::SetUp() {
190 task_runner_
= new AutoThreadTaskRunner(
191 message_loop_
.message_loop_proxy(),
192 base::Bind(&WorkerProcessLauncherTest::QuitMainMessageLoop
,
193 base::Unretained(this)));
195 // Set up process launcher delegate
196 launcher_delegate_
.reset(new MockProcessLauncherDelegate());
197 EXPECT_CALL(*launcher_delegate_
, Send(_
))
199 .WillRepeatedly(Invoke(this, &WorkerProcessLauncherTest::SendToProcess
));
200 EXPECT_CALL(*launcher_delegate_
, CloseChannel())
202 .WillRepeatedly(Invoke(this,
203 &WorkerProcessLauncherTest::DisconnectServer
));
204 EXPECT_CALL(*launcher_delegate_
, KillProcess())
206 .WillRepeatedly(Invoke(this, &WorkerProcessLauncherTest::KillProcess
));
208 // Set up IPC delegate.
209 EXPECT_CALL(server_listener_
, OnMessageReceived(_
))
213 void WorkerProcessLauncherTest::TearDown() {
216 bool WorkerProcessLauncherTest::OnMessageReceived(const IPC::Message
& message
) {
217 return event_handler_
->OnMessageReceived(message
);
220 void WorkerProcessLauncherTest::OnChannelConnected(int32 peer_pid
) {
221 event_handler_
->OnChannelConnected(peer_pid
);
224 void WorkerProcessLauncherTest::OnChannelError() {
225 event_handler_
->OnChannelError();
228 void WorkerProcessLauncherTest::LaunchProcess(
229 WorkerProcessLauncher
* event_handler
) {
230 EXPECT_FALSE(event_handler_
);
231 event_handler_
= event_handler
;
236 void WorkerProcessLauncherTest::LaunchProcessAndConnect(
237 WorkerProcessLauncher
* event_handler
) {
238 EXPECT_FALSE(event_handler_
);
239 event_handler_
= event_handler
;
243 task_runner_
->PostTask(
245 base::Bind(&WorkerProcessLauncherTest::ConnectClient
,
246 base::Unretained(this)));
249 void WorkerProcessLauncherTest::FailLaunchAndStopWorker(
250 WorkerProcessLauncher
* event_handler
) {
251 EXPECT_FALSE(event_handler_
);
253 event_handler
->OnFatalError();
255 task_runner_
->PostTask(
257 base::Bind(&WorkerProcessLauncherTest::StopWorker
,
258 base::Unretained(this)));
261 void WorkerProcessLauncherTest::KillProcess() {
262 event_handler_
= NULL
;
264 if (worker_process_
.IsValid()) {
265 TerminateProcess(worker_process_
.Get(), CONTROL_C_EXIT
);
266 worker_process_
.Close();
270 void WorkerProcessLauncherTest::TerminateWorker(DWORD exit_code
) {
271 if (worker_process_
.IsValid())
272 TerminateProcess(worker_process_
.Get(), exit_code
);
275 void WorkerProcessLauncherTest::ConnectClient() {
276 channel_client_
= IPC::ChannelProxy::Create(IPC::ChannelHandle(channel_name_
),
277 IPC::Channel::MODE_CLIENT
,
281 // Pretend that |kLaunchSuccessTimeoutSeconds| passed since launching
282 // the worker process. This will make the backoff algorithm think that this
283 // launch attempt was successful and it will not delay the next launch.
284 launcher_
->RecordSuccessfulLaunchForTest();
287 void WorkerProcessLauncherTest::DisconnectClient() {
288 channel_client_
.reset();
291 void WorkerProcessLauncherTest::DisconnectServer() {
292 channel_server_
.reset();
295 void WorkerProcessLauncherTest::SendToProcess(IPC::Message
* message
) {
296 if (channel_server_
) {
297 channel_server_
->Send(message
);
304 void WorkerProcessLauncherTest::SendFakeMessageToLauncher() {
306 channel_client_
->Send(new ChromotingDesktopNetworkMsg_DisconnectSession());
309 void WorkerProcessLauncherTest::CrashWorker() {
310 launcher_
->Crash(FROM_HERE
);
313 void WorkerProcessLauncherTest::StartWorker() {
314 launcher_
.reset(new WorkerProcessLauncher(
315 launcher_delegate_
.Pass(),
318 launcher_
->SetKillProcessTimeoutForTest(base::TimeDelta::FromMilliseconds(0));
321 void WorkerProcessLauncherTest::StopWorker() {
324 channel_name_
.clear();
325 channel_server_
.reset();
329 void WorkerProcessLauncherTest::QuitMainMessageLoop() {
330 message_loop_
.PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
333 void WorkerProcessLauncherTest::DoLaunchProcess() {
334 EXPECT_TRUE(event_handler_
);
335 EXPECT_FALSE(worker_process_
.IsValid());
337 WCHAR notepad
[MAX_PATH
+ 1];
338 ASSERT_GT(ExpandEnvironmentStrings(
339 L
"\045SystemRoot\045\\system32\\notepad.exe", notepad
, MAX_PATH
), 0u);
341 STARTUPINFOW startup_info
= { 0 };
342 startup_info
.cb
= sizeof(startup_info
);
344 PROCESS_INFORMATION temp_process_info
= {};
345 ASSERT_TRUE(CreateProcess(NULL
,
347 NULL
, // default process attibutes
348 NULL
, // default thread attibutes
349 FALSE
, // do not inherit handles
351 NULL
, // no environment
352 NULL
, // default current directory
354 &temp_process_info
));
355 base::win::ScopedProcessInformation
process_information(temp_process_info
);
356 worker_process_
.Set(process_information
.TakeProcessHandle());
357 ASSERT_TRUE(worker_process_
.IsValid());
359 channel_name_
= IPC::Channel::GenerateUniqueRandomChannelID();
361 ASSERT_TRUE(CreateIpcChannel(channel_name_
, kIpcSecurityDescriptor
, &pipe
));
363 // Wrap the pipe into an IPC channel.
364 channel_server_
= IPC::ChannelProxy::Create(
365 IPC::ChannelHandle(pipe
.Get()), IPC::Channel::MODE_SERVER
, this,
369 ASSERT_TRUE(DuplicateHandle(GetCurrentProcess(),
370 worker_process_
.Get(),
375 DUPLICATE_SAME_ACCESS
));
376 ScopedHandle
copy(temp_handle
);
378 event_handler_
->OnProcessLaunched(copy
.Pass());
381 TEST_F(WorkerProcessLauncherTest
, Start
) {
382 EXPECT_CALL(*launcher_delegate_
, LaunchProcess(_
))
384 .WillRepeatedly(Invoke(this, &WorkerProcessLauncherTest::LaunchProcess
));
386 EXPECT_CALL(server_listener_
, OnChannelConnected(_
))
388 EXPECT_CALL(server_listener_
, OnPermanentError(_
))
396 // Starts and connects to the worker process. Expect OnChannelConnected to be
398 TEST_F(WorkerProcessLauncherTest
, StartAndConnect
) {
399 EXPECT_CALL(*launcher_delegate_
, LaunchProcess(_
))
401 .WillRepeatedly(Invoke(
402 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect
));
404 EXPECT_CALL(server_listener_
, OnChannelConnected(_
))
406 .WillOnce(InvokeWithoutArgs(this,
407 &WorkerProcessLauncherTest::StopWorker
));
408 EXPECT_CALL(server_listener_
, OnPermanentError(_
))
415 // Kills the worker process after the 1st connect and expects it to be
417 TEST_F(WorkerProcessLauncherTest
, Restart
) {
418 EXPECT_CALL(*launcher_delegate_
, LaunchProcess(_
))
420 .WillRepeatedly(Invoke(
421 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect
));
422 Expectation first_connect
=
423 EXPECT_CALL(server_listener_
, OnChannelConnected(_
))
425 .WillOnce(InvokeWithoutArgs(CreateFunctor(
426 this, &WorkerProcessLauncherTest::TerminateWorker
,
428 .WillOnce(InvokeWithoutArgs(this,
429 &WorkerProcessLauncherTest::StopWorker
));
431 EXPECT_CALL(server_listener_
, OnPermanentError(_
))
438 // Drops the IPC channel to the worker process after the 1st connect and expects
439 // the worker process to be restarted.
440 TEST_F(WorkerProcessLauncherTest
, DropIpcChannel
) {
441 EXPECT_CALL(*launcher_delegate_
, LaunchProcess(_
))
443 .WillRepeatedly(Invoke(
444 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect
));
446 Expectation first_connect
=
447 EXPECT_CALL(server_listener_
, OnChannelConnected(_
))
449 .WillOnce(InvokeWithoutArgs(
450 this, &WorkerProcessLauncherTest::DisconnectClient
))
451 .WillOnce(InvokeWithoutArgs(
452 this, &WorkerProcessLauncherTest::StopWorker
));
454 EXPECT_CALL(server_listener_
, OnPermanentError(_
))
461 // Returns a permanent error exit code and expects OnPermanentError() to be
463 TEST_F(WorkerProcessLauncherTest
, PermanentError
) {
464 EXPECT_CALL(*launcher_delegate_
, LaunchProcess(_
))
466 .WillRepeatedly(Invoke(
467 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect
));
469 EXPECT_CALL(server_listener_
, OnChannelConnected(_
))
471 .WillOnce(InvokeWithoutArgs(CreateFunctor(
472 this, &WorkerProcessLauncherTest::TerminateWorker
,
473 kMinPermanentErrorExitCode
)));
474 EXPECT_CALL(server_listener_
, OnPermanentError(_
))
476 .WillOnce(InvokeWithoutArgs(this,
477 &WorkerProcessLauncherTest::StopWorker
));
483 // Requests the worker to crash and expects it to honor the request.
484 TEST_F(WorkerProcessLauncherTest
, Crash
) {
485 EXPECT_CALL(*launcher_delegate_
, LaunchProcess(_
))
487 .WillRepeatedly(Invoke(
488 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect
));
490 EXPECT_CALL(server_listener_
, OnChannelConnected(_
))
492 .WillOnce(InvokeWithoutArgs(this,
493 &WorkerProcessLauncherTest::CrashWorker
))
494 .WillOnce(InvokeWithoutArgs(this,
495 &WorkerProcessLauncherTest::StopWorker
));
497 EXPECT_CALL(client_listener_
, OnCrash(_
, _
, _
))
499 .WillOnce(InvokeWithoutArgs(CreateFunctor(
500 this, &WorkerProcessLauncherTest::TerminateWorker
,
501 EXCEPTION_BREAKPOINT
)));
507 // Requests the worker to crash and terminates the worker even if it does not
509 TEST_F(WorkerProcessLauncherTest
, CrashAnyway
) {
510 EXPECT_CALL(*launcher_delegate_
, LaunchProcess(_
))
512 .WillRepeatedly(Invoke(
513 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect
));
515 EXPECT_CALL(server_listener_
, OnChannelConnected(_
))
517 .WillOnce(InvokeWithoutArgs(this,
518 &WorkerProcessLauncherTest::CrashWorker
))
519 .WillOnce(InvokeWithoutArgs(this,
520 &WorkerProcessLauncherTest::StopWorker
));
522 // Ignore the crash request and try send another message to the launcher.
523 EXPECT_CALL(client_listener_
, OnCrash(_
, _
, _
))
525 .WillOnce(InvokeWithoutArgs(
526 this, &WorkerProcessLauncherTest::SendFakeMessageToLauncher
));
532 } // namespace remoting