| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/memory/ref_counted.h" |
| 7 #include "base/message_loop.h" |
| 8 #include "base/win/scoped_handle.h" |
| 9 #include "ipc/ipc_channel.h" |
| 10 #include "ipc/ipc_channel_proxy.h" |
| 11 #include "ipc/ipc_listener.h" |
| 12 #include "ipc/ipc_message.h" |
| 13 #include "remoting/base/auto_thread_task_runner.h" |
| 14 #include "remoting/host/host_exit_codes.h" |
| 15 #include "remoting/host/win/worker_process_launcher.h" |
| 16 #include "remoting/host/worker_process_ipc_delegate.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using base::win::ScopedHandle; |
| 21 using testing::_; |
| 22 using testing::AnyNumber; |
| 23 using testing::DoAll; |
| 24 using testing::Expectation; |
| 25 using testing::Invoke; |
| 26 using testing::Return; |
| 27 using testing::ReturnPointee; |
| 28 |
| 29 namespace remoting { |
| 30 |
| 31 namespace { |
| 32 |
| 33 const char kIpcSecurityDescriptor[] = "D:(A;;GA;;;AU)"; |
| 34 |
| 35 class MockProcessLauncherDelegate |
| 36 : public WorkerProcessLauncher::Delegate { |
| 37 public: |
| 38 MockProcessLauncherDelegate() {} |
| 39 virtual ~MockProcessLauncherDelegate() {} |
| 40 |
| 41 // WorkerProcessLauncher::Delegate implementation |
| 42 MOCK_METHOD0(GetExitCode, DWORD()); |
| 43 MOCK_METHOD1(KillProcess, void(DWORD)); |
| 44 MOCK_METHOD2(LaunchProcess, bool(const std::string&, ScopedHandle*)); |
| 45 |
| 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(MockProcessLauncherDelegate); |
| 48 }; |
| 49 |
| 50 class MockIpcDelegate |
| 51 : public WorkerProcessIpcDelegate { |
| 52 public: |
| 53 MockIpcDelegate() {} |
| 54 virtual ~MockIpcDelegate() {} |
| 55 |
| 56 // WorkerProcessIpcDelegate implementation |
| 57 MOCK_METHOD0(OnChannelConnected, void()); |
| 58 MOCK_METHOD1(OnMessageReceived, bool(const IPC::Message&)); |
| 59 MOCK_METHOD0(OnPermanentError, void()); |
| 60 |
| 61 private: |
| 62 DISALLOW_COPY_AND_ASSIGN(MockIpcDelegate); |
| 63 }; |
| 64 |
| 65 } // namespace |
| 66 |
| 67 class WorkerProcessLauncherTest |
| 68 : public testing::Test, |
| 69 public IPC::Listener { |
| 70 public: |
| 71 WorkerProcessLauncherTest(); |
| 72 virtual ~WorkerProcessLauncherTest(); |
| 73 |
| 74 virtual void SetUp() OVERRIDE; |
| 75 virtual void TearDown() OVERRIDE; |
| 76 |
| 77 // IPC::Listener implementation |
| 78 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 79 |
| 80 // WorkerProcessLauncher::Delegate mocks |
| 81 void KillProcess(DWORD exit_code); |
| 82 bool LaunchProcess(const std::string& channel_name, |
| 83 ScopedHandle* process_exit_event_out); |
| 84 bool LaunchProcessAndConnect(const std::string& channel_name, |
| 85 ScopedHandle* process_exit_event_out); |
| 86 |
| 87 void ConnectTo(const std::string& channel_name); |
| 88 void Disconnect(); |
| 89 |
| 90 // Starts the worker. |
| 91 void StartWorker(); |
| 92 |
| 93 // Stops the worker. |
| 94 void StopWorker(); |
| 95 |
| 96 // Quits |message_loop_|. |
| 97 void QuitMainMessageLoop(); |
| 98 |
| 99 protected: |
| 100 MessageLoop message_loop_; |
| 101 scoped_refptr<AutoThreadTaskRunner> task_runner_; |
| 102 |
| 103 // Exit code of the worker process. |
| 104 DWORD exit_code_; |
| 105 |
| 106 MockIpcDelegate ipc_delegate_; |
| 107 scoped_ptr<MockProcessLauncherDelegate> launcher_delegate_; |
| 108 |
| 109 // Client end of the IPC channel. |
| 110 scoped_ptr<IPC::ChannelProxy> ipc_channel_; |
| 111 |
| 112 // The worker process launcher. |
| 113 scoped_refptr<WorkerProcessLauncher> launcher_; |
| 114 |
| 115 // The event signalling termination of the worker process. |
| 116 ScopedHandle process_exit_event_; |
| 117 }; |
| 118 |
| 119 |
| 120 WorkerProcessLauncherTest::WorkerProcessLauncherTest() |
| 121 : message_loop_(MessageLoop::TYPE_IO) { |
| 122 } |
| 123 |
| 124 WorkerProcessLauncherTest::~WorkerProcessLauncherTest() { |
| 125 } |
| 126 |
| 127 void WorkerProcessLauncherTest::SetUp() { |
| 128 task_runner_ = new AutoThreadTaskRunner( |
| 129 message_loop_.message_loop_proxy(), |
| 130 base::Bind(&WorkerProcessLauncherTest::QuitMainMessageLoop, |
| 131 base::Unretained(this))); |
| 132 |
| 133 exit_code_ = STILL_ACTIVE; |
| 134 |
| 135 // Set up process launcher delegate |
| 136 launcher_delegate_.reset(new MockProcessLauncherDelegate()); |
| 137 EXPECT_CALL(*launcher_delegate_, GetExitCode()) |
| 138 .Times(AnyNumber()) |
| 139 .WillRepeatedly(ReturnPointee(&exit_code_)); |
| 140 EXPECT_CALL(*launcher_delegate_, KillProcess(_)) |
| 141 .Times(AnyNumber()) |
| 142 .WillRepeatedly(Invoke(this, &WorkerProcessLauncherTest::KillProcess)); |
| 143 |
| 144 // Set up IPC delegate. |
| 145 EXPECT_CALL(ipc_delegate_, OnMessageReceived(_)) |
| 146 .Times(AnyNumber()) |
| 147 .WillRepeatedly(Return(false)); |
| 148 } |
| 149 |
| 150 void WorkerProcessLauncherTest::TearDown() { |
| 151 } |
| 152 |
| 153 bool WorkerProcessLauncherTest::OnMessageReceived(const IPC::Message& message) { |
| 154 return false; |
| 155 } |
| 156 |
| 157 void WorkerProcessLauncherTest::KillProcess(DWORD exit_code) { |
| 158 exit_code_ = exit_code; |
| 159 BOOL result = SetEvent(process_exit_event_); EXPECT_TRUE(result); |
| 160 } |
| 161 |
| 162 bool WorkerProcessLauncherTest::LaunchProcess( |
| 163 const std::string& channel_name, |
| 164 ScopedHandle* process_exit_event_out) { |
| 165 return LaunchProcessAndConnect("", process_exit_event_out); |
| 166 } |
| 167 |
| 168 bool WorkerProcessLauncherTest::LaunchProcessAndConnect( |
| 169 const std::string& channel_name, |
| 170 ScopedHandle* process_exit_event_out) { |
| 171 process_exit_event_.Set(CreateEvent(NULL, TRUE, FALSE, NULL)); |
| 172 if (!process_exit_event_.IsValid()) |
| 173 return false; |
| 174 |
| 175 if (!channel_name.empty()) { |
| 176 task_runner_->PostTask( |
| 177 FROM_HERE, |
| 178 base::Bind(&WorkerProcessLauncherTest::ConnectTo, |
| 179 base::Unretained(this), |
| 180 channel_name)); |
| 181 } |
| 182 |
| 183 exit_code_ = STILL_ACTIVE; |
| 184 return DuplicateHandle(GetCurrentProcess(), |
| 185 process_exit_event_, |
| 186 GetCurrentProcess(), |
| 187 process_exit_event_out->Receive(), |
| 188 0, |
| 189 FALSE, |
| 190 DUPLICATE_SAME_ACCESS) != FALSE; |
| 191 } |
| 192 |
| 193 void WorkerProcessLauncherTest::ConnectTo(const std::string& channel_name) { |
| 194 ipc_channel_.reset(new IPC::ChannelProxy( |
| 195 IPC::ChannelHandle(channel_name), |
| 196 IPC::Channel::MODE_CLIENT, |
| 197 this, |
| 198 task_runner_)); |
| 199 } |
| 200 |
| 201 void WorkerProcessLauncherTest::Disconnect() { |
| 202 ipc_channel_.reset(); |
| 203 } |
| 204 |
| 205 void WorkerProcessLauncherTest::StartWorker() { |
| 206 launcher_ = new WorkerProcessLauncher(task_runner_, |
| 207 task_runner_, |
| 208 launcher_delegate_.Pass(), |
| 209 &ipc_delegate_, |
| 210 kIpcSecurityDescriptor); |
| 211 } |
| 212 |
| 213 void WorkerProcessLauncherTest::StopWorker() { |
| 214 launcher_->Stop(); |
| 215 launcher_ = NULL; |
| 216 Disconnect(); |
| 217 task_runner_ = NULL; |
| 218 } |
| 219 |
| 220 void WorkerProcessLauncherTest::QuitMainMessageLoop() { |
| 221 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 222 } |
| 223 |
| 224 TEST_F(WorkerProcessLauncherTest, Start) { |
| 225 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_, _)) |
| 226 .Times(AnyNumber()) |
| 227 .WillRepeatedly(Invoke(this, &WorkerProcessLauncherTest::LaunchProcess)); |
| 228 |
| 229 EXPECT_CALL(ipc_delegate_, OnChannelConnected()) |
| 230 .Times(0); |
| 231 EXPECT_CALL(ipc_delegate_, OnPermanentError()) |
| 232 .Times(0); |
| 233 |
| 234 StartWorker(); |
| 235 StopWorker(); |
| 236 message_loop_.Run(); |
| 237 |
| 238 EXPECT_EQ(exit_code_, CONTROL_C_EXIT); |
| 239 } |
| 240 |
| 241 // Starts and connects to the worker process. Expect OnChannelConnected to be |
| 242 // called. |
| 243 TEST_F(WorkerProcessLauncherTest, StartAndConnect) { |
| 244 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_, _)) |
| 245 .Times(AnyNumber()) |
| 246 .WillRepeatedly(Invoke( |
| 247 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect)); |
| 248 |
| 249 EXPECT_CALL(ipc_delegate_, OnChannelConnected()) |
| 250 .Times(1) |
| 251 .WillOnce(Invoke(this, &WorkerProcessLauncherTest::StopWorker)); |
| 252 EXPECT_CALL(ipc_delegate_, OnPermanentError()) |
| 253 .Times(0); |
| 254 |
| 255 StartWorker(); |
| 256 message_loop_.Run(); |
| 257 |
| 258 EXPECT_EQ(exit_code_, CONTROL_C_EXIT); |
| 259 } |
| 260 |
| 261 // Kills the worker process after the 1st connect and expects it to be |
| 262 // restarted. |
| 263 TEST_F(WorkerProcessLauncherTest, Restart) { |
| 264 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_, _)) |
| 265 .Times(AnyNumber()) |
| 266 .WillRepeatedly(Invoke( |
| 267 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect)); |
| 268 |
| 269 base::Closure terminate_worker = |
| 270 base::Bind(&WorkerProcessLauncherTest::KillProcess, |
| 271 base::Unretained(this), |
| 272 CONTROL_C_EXIT); |
| 273 Expectation first_connect = |
| 274 EXPECT_CALL(ipc_delegate_, OnChannelConnected()) |
| 275 .Times(2) |
| 276 .WillOnce(Invoke(&terminate_worker, &base::Closure::Run)) |
| 277 .WillOnce(Invoke(this, &WorkerProcessLauncherTest::StopWorker)); |
| 278 |
| 279 EXPECT_CALL(ipc_delegate_, OnPermanentError()) |
| 280 .Times(0); |
| 281 |
| 282 StartWorker(); |
| 283 message_loop_.Run(); |
| 284 |
| 285 EXPECT_EQ(exit_code_, CONTROL_C_EXIT); |
| 286 } |
| 287 |
| 288 // Drops connection to the worker process after the 1st connect and expects |
| 289 // the worker process to be restarted. |
| 290 // restarted. |
| 291 TEST_F(WorkerProcessLauncherTest, DropConnection) { |
| 292 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_, _)) |
| 293 .Times(AnyNumber()) |
| 294 .WillRepeatedly(Invoke( |
| 295 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect)); |
| 296 |
| 297 Expectation first_connect = |
| 298 EXPECT_CALL(ipc_delegate_, OnChannelConnected()) |
| 299 .Times(2) |
| 300 .WillOnce(Invoke(this, &WorkerProcessLauncherTest::Disconnect)) |
| 301 .WillOnce(Invoke(this, &WorkerProcessLauncherTest::StopWorker)); |
| 302 |
| 303 EXPECT_CALL(ipc_delegate_, OnPermanentError()) |
| 304 .Times(0); |
| 305 |
| 306 StartWorker(); |
| 307 message_loop_.Run(); |
| 308 |
| 309 EXPECT_EQ(exit_code_, CONTROL_C_EXIT); |
| 310 } |
| 311 |
| 312 // Returns a permanent error exit code and expects OnPermanentError() to be |
| 313 // invoked. |
| 314 TEST_F(WorkerProcessLauncherTest, PermanentError) { |
| 315 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_, _)) |
| 316 .Times(AnyNumber()) |
| 317 .WillRepeatedly(Invoke( |
| 318 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect)); |
| 319 |
| 320 base::Closure terminate_permanently = |
| 321 base::Bind(&WorkerProcessLauncherTest::KillProcess, |
| 322 base::Unretained(this), |
| 323 kInvalidHostConfigurationExitCode); |
| 324 EXPECT_CALL(ipc_delegate_, OnChannelConnected()) |
| 325 .Times(AnyNumber()) |
| 326 .WillRepeatedly(Invoke(&terminate_permanently, &base::Closure::Run)); |
| 327 |
| 328 EXPECT_CALL(ipc_delegate_, OnPermanentError()) |
| 329 .Times(1) |
| 330 .WillOnce(Invoke(this, &WorkerProcessLauncherTest::StopWorker)); |
| 331 |
| 332 StartWorker(); |
| 333 message_loop_.Run(); |
| 334 |
| 335 EXPECT_EQ(exit_code_, kInvalidHostConfigurationExitCode); |
| 336 } |
| 337 |
| 338 } // namespace remoting |
| OLD | NEW |