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

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

Issue 11040065: [Chromoting] Reimplemented the worker process launcher to take into account the encountered issues: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CR feedback. Created 8 years, 2 months 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 | Annotate | Revision Log
OLDNEW
(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);
Wez 2012/10/09 03:40:39 nit: EXPECT_TRUE on the wrong line.
alexeypa (please no reviews) 2012/10/09 19:42:04 Done.
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 launcher_->Start();
212 }
213
214 void WorkerProcessLauncherTest::StopWorker() {
215 launcher_->Stop();
216 launcher_ = NULL;
217 Disconnect();
218 task_runner_ = NULL;
219 }
220
221 void WorkerProcessLauncherTest::QuitMainMessageLoop() {
222 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure());
223 }
224
225 TEST_F(WorkerProcessLauncherTest, Start) {
226 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_, _))
227 .Times(AnyNumber())
Wez 2012/10/09 03:40:39 Why is this AnyNumber(); shouldn't it be exactly 1
alexeypa (please no reviews) 2012/10/09 19:42:04 Done.
228 .WillRepeatedly(Invoke(this, &WorkerProcessLauncherTest::LaunchProcess));
229
230 EXPECT_CALL(ipc_delegate_, OnChannelConnected())
231 .Times(0);
232 EXPECT_CALL(ipc_delegate_, OnPermanentError())
233 .Times(0);
234
235 StartWorker();
236 StopWorker();
237 message_loop_.Run();
238
239 EXPECT_EQ(exit_code_, CONTROL_C_EXIT);
240 }
241
242 // Starts and connects to the worker process. Expect OnChannelConnected to be
243 // called.
244 TEST_F(WorkerProcessLauncherTest, StartAndConnect) {
245 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_, _))
246 .Times(AnyNumber())
Wez 2012/10/09 03:40:39 Similarly here.
alexeypa (please no reviews) 2012/10/09 19:42:04 Done.
247 .WillRepeatedly(Invoke(
248 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect));
249
250 EXPECT_CALL(ipc_delegate_, OnChannelConnected())
251 .Times(1)
252 .WillOnce(Invoke(this, &WorkerProcessLauncherTest::StopWorker));
253 EXPECT_CALL(ipc_delegate_, OnPermanentError())
254 .Times(0);
255
256 StartWorker();
257 message_loop_.Run();
258
259 EXPECT_EQ(exit_code_, CONTROL_C_EXIT);
260 }
261
262 // Kills the worker process after the 1st connect and expects it to be
263 // restarted.
264 TEST_F(WorkerProcessLauncherTest, Restart) {
265 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_, _))
266 .Times(AnyNumber())
267 .WillRepeatedly(Invoke(
268 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect));
269
270 base::Closure terminate_worker =
271 base::Bind(&WorkerProcessLauncherTest::KillProcess,
272 base::Unretained(this),
273 CONTROL_C_EXIT);
274 Expectation first_connect =
275 EXPECT_CALL(ipc_delegate_, OnChannelConnected())
276 .Times(2)
277 .WillOnce(Invoke(&terminate_worker, &base::Closure::Run))
Wez 2012/10/09 03:40:39 nit: Is there no way to do parameterized invoke, i
alexeypa (please no reviews) 2012/10/09 19:42:04 There is testing::CreateFunctor.
278 .WillOnce(Invoke(this, &WorkerProcessLauncherTest::StopWorker));
279
280 EXPECT_CALL(ipc_delegate_, OnPermanentError())
281 .Times(0);
282
283 StartWorker();
284 message_loop_.Run();
285
286 EXPECT_EQ(exit_code_, CONTROL_C_EXIT);
287 }
288
289 // Drops connection to the worker process after the 1st connect and expects
290 // the worker process to be restarted.
291 // restarted.
292 TEST_F(WorkerProcessLauncherTest, DropConnection) {
Wez 2012/10/09 03:40:39 nit: DropIpcChannel, to avoid ambiguity w/ use of
alexeypa (please no reviews) 2012/10/09 19:42:04 Done.
293 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_, _))
294 .Times(AnyNumber())
295 .WillRepeatedly(Invoke(
296 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect));
297
298 Expectation first_connect =
299 EXPECT_CALL(ipc_delegate_, OnChannelConnected())
300 .Times(2)
301 .WillOnce(Invoke(this, &WorkerProcessLauncherTest::Disconnect))
302 .WillOnce(Invoke(this, &WorkerProcessLauncherTest::StopWorker));
303
304 EXPECT_CALL(ipc_delegate_, OnPermanentError())
305 .Times(0);
306
307 StartWorker();
308 message_loop_.Run();
309
310 EXPECT_EQ(exit_code_, CONTROL_C_EXIT);
311 }
312
313 // Returns a permanent error exit code and expects OnPermanentError() to be
314 // invoked.
315 TEST_F(WorkerProcessLauncherTest, PermanentError) {
316 EXPECT_CALL(*launcher_delegate_, LaunchProcess(_, _))
317 .Times(AnyNumber())
318 .WillRepeatedly(Invoke(
319 this, &WorkerProcessLauncherTest::LaunchProcessAndConnect));
320
321 base::Closure terminate_permanently =
322 base::Bind(&WorkerProcessLauncherTest::KillProcess,
323 base::Unretained(this),
324 kInvalidHostConfigurationExitCode);
325 EXPECT_CALL(ipc_delegate_, OnChannelConnected())
326 .Times(AnyNumber())
327 .WillRepeatedly(Invoke(&terminate_permanently, &base::Closure::Run));
328
329 EXPECT_CALL(ipc_delegate_, OnPermanentError())
330 .Times(1)
331 .WillOnce(Invoke(this, &WorkerProcessLauncherTest::StopWorker));
332
333 StartWorker();
334 message_loop_.Run();
335
336 EXPECT_EQ(exit_code_, kInvalidHostConfigurationExitCode);
337 }
338
339 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698