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