blob: 029e49c667c09e2d0cff09e4092c090db556f388 [file] [log] [blame]
levin@chromium.org5c528682011-03-28 10:54:15 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// Unit test for SyncChannel.
6
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +09007#include "ipc/ipc_sync_channel.h"
8
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09009#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +090013#include "base/bind.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090014#include "base/logging.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090015#include "base/memory/scoped_ptr.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090016#include "base/message_loop.h"
dilmah@chromium.orgdc4b9702011-07-20 07:13:24 +090017#include "base/stl_util.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090018#include "base/string_util.h"
timurrrr@chromium.orgf39c3ff2010-05-14 17:24:42 +090019#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
brettw@google.com7c5cc672011-01-01 11:17:08 +090020#include "base/threading/platform_thread.h"
brettw@chromium.org5b5f5e02011-01-01 10:01:06 +090021#include "base/threading/thread.h"
brettw@chromium.org5238c7d2011-01-02 15:05:39 +090022#include "base/synchronization/waitable_event.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090023#include "ipc/ipc_message.h"
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090024#include "ipc/ipc_sync_message_filter.h"
jam@chromium.org86a8de12010-12-09 08:34:16 +090025#include "ipc/ipc_sync_message_unittest.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090026#include "testing/gtest/include/gtest/gtest.h"
27
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090028using base::WaitableEvent;
29
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +090030namespace IPC {
31
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090032namespace {
33
34// Base class for a "process" with listener and IPC threads.
35class Worker : public Channel::Listener, public Message::Sender {
36 public:
37 // Will create a channel without a name.
38 Worker(Channel::Mode mode, const std::string& thread_name)
39 : done_(new WaitableEvent(false, false)),
40 channel_created_(new WaitableEvent(false, false)),
41 mode_(mode),
42 ipc_thread_((thread_name + "_ipc").c_str()),
43 listener_thread_((thread_name + "_listener").c_str()),
44 overrided_thread_(NULL),
timurrrr@chromium.org03100a82009-10-27 20:28:58 +090045 shutdown_event_(true, false) {
46 // The data race on vfptr is real but is very hard
47 // to suppress using standard Valgrind mechanism (suppressions).
48 // We have to use ANNOTATE_BENIGN_RACE to hide the reports and
49 // make ThreadSanitizer bots green.
50 ANNOTATE_BENIGN_RACE(this, "Race on vfptr, http://crbug.com/25841");
51 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090052
53 // Will create a named channel and use this name for the threads' name.
54 Worker(const std::string& channel_name, Channel::Mode mode)
55 : done_(new WaitableEvent(false, false)),
56 channel_created_(new WaitableEvent(false, false)),
57 channel_name_(channel_name),
58 mode_(mode),
59 ipc_thread_((channel_name + "_ipc").c_str()),
60 listener_thread_((channel_name + "_listener").c_str()),
61 overrided_thread_(NULL),
timurrrr@chromium.org03100a82009-10-27 20:28:58 +090062 shutdown_event_(true, false) {
63 // The data race on vfptr is real but is very hard
64 // to suppress using standard Valgrind mechanism (suppressions).
65 // We have to use ANNOTATE_BENIGN_RACE to hide the reports and
66 // make ThreadSanitizer bots green.
67 ANNOTATE_BENIGN_RACE(this, "Race on vfptr, http://crbug.com/25841");
68 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090069
70 // The IPC thread needs to outlive SyncChannel, so force the correct order of
71 // destruction.
72 virtual ~Worker() {
73 WaitableEvent listener_done(false, false), ipc_done(false, false);
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +090074 ListenerThread()->message_loop()->PostTask(
75 FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown1, this,
76 &listener_done, &ipc_done));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090077 listener_done.Wait();
78 ipc_done.Wait();
79 ipc_thread_.Stop();
80 listener_thread_.Stop();
81 }
82 void AddRef() { }
83 void Release() { }
darin@chromium.org5cb996e2009-09-30 13:29:20 +090084 static bool ImplementsThreadSafeReferenceCounting() { return true; }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090085 bool Send(Message* msg) { return channel_->Send(msg); }
86 bool SendWithTimeout(Message* msg, int timeout_ms) {
87 return channel_->SendWithTimeout(msg, timeout_ms);
88 }
89 void WaitForChannelCreation() { channel_created_->Wait(); }
90 void CloseChannel() {
91 DCHECK(MessageLoop::current() == ListenerThread()->message_loop());
92 channel_->Close();
93 }
94 void Start() {
95 StartThread(&listener_thread_, MessageLoop::TYPE_DEFAULT);
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +090096 ListenerThread()->message_loop()->PostTask(
97 FROM_HERE, base::Bind(&Worker::OnStart, this));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090098 }
99 void OverrideThread(base::Thread* overrided_thread) {
100 DCHECK(overrided_thread_ == NULL);
101 overrided_thread_ = overrided_thread;
102 }
103 bool SendAnswerToLife(bool pump, int timeout, bool succeed) {
104 int answer = 0;
105 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
106 if (pump)
107 msg->EnableMessagePumping();
108 bool result = SendWithTimeout(msg, timeout);
jam@chromium.orgebd07182009-12-01 11:34:18 +0900109 DCHECK_EQ(result, succeed);
110 DCHECK_EQ(answer, (succeed ? 42 : 0));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900111 return result;
112 }
113 bool SendDouble(bool pump, bool succeed) {
114 int answer = 0;
115 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer);
116 if (pump)
117 msg->EnableMessagePumping();
118 bool result = Send(msg);
jam@chromium.orgebd07182009-12-01 11:34:18 +0900119 DCHECK_EQ(result, succeed);
120 DCHECK_EQ(answer, (succeed ? 10 : 0));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900121 return result;
122 }
123 Channel::Mode mode() { return mode_; }
124 WaitableEvent* done_event() { return done_.get(); }
jabdelmalek@google.comeb921652010-04-07 05:33:36 +0900125 WaitableEvent* shutdown_event() { return &shutdown_event_; }
jam@chromium.orgebd07182009-12-01 11:34:18 +0900126 void ResetChannel() { channel_.reset(); }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900127 // Derived classes need to call this when they've completed their part of
128 // the test.
129 void Done() { done_->Signal(); }
jabdelmalek@google.comeb921652010-04-07 05:33:36 +0900130
131 protected:
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +0900132 SyncChannel* channel() { return channel_.get(); }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900133 // Functions for dervied classes to implement if they wish.
134 virtual void Run() { }
135 virtual void OnAnswer(int* answer) { NOTREACHED(); }
136 virtual void OnAnswerDelay(Message* reply_msg) {
137 // The message handler map below can only take one entry for
138 // SyncChannelTestMsg_AnswerToLife, so since some classes want
139 // the normal version while other want the delayed reply, we
140 // call the normal version if the derived class didn't override
141 // this function.
142 int answer;
143 OnAnswer(&answer);
144 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, answer);
145 Send(reply_msg);
146 }
147 virtual void OnDouble(int in, int* out) { NOTREACHED(); }
148 virtual void OnDoubleDelay(int in, Message* reply_msg) {
149 int result;
150 OnDouble(in, &result);
151 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result);
152 Send(reply_msg);
153 }
154
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900155 virtual void OnNestedTestMsg(Message* reply_msg) {
156 NOTREACHED();
157 }
158
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900159 base::Thread* ListenerThread() {
160 return overrided_thread_ ? overrided_thread_ : &listener_thread_;
161 }
ananta@chromium.org999f2972010-09-03 06:45:50 +0900162
piman@google.com0cbefaa2011-04-08 12:38:21 +0900163 const base::Thread& ipc_thread() const { return ipc_thread_; }
164
ananta@chromium.org999f2972010-09-03 06:45:50 +0900165 private:
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900166 // Called on the listener thread to create the sync channel.
167 void OnStart() {
168 // Link ipc_thread_, listener_thread_ and channel_ altogether.
169 StartThread(&ipc_thread_, MessageLoop::TYPE_IO);
170 channel_.reset(new SyncChannel(
jam@chromium.org06d18442011-05-03 03:00:49 +0900171 channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true,
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900172 &shutdown_event_));
173 channel_created_->Signal();
174 Run();
175 }
176
177 void OnListenerThreadShutdown1(WaitableEvent* listener_event,
178 WaitableEvent* ipc_event) {
179 // SyncChannel needs to be destructed on the thread that it was created on.
180 channel_.reset();
181
182 MessageLoop::current()->RunAllPending();
183
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +0900184 ipc_thread_.message_loop()->PostTask(
185 FROM_HERE, base::Bind(&Worker::OnIPCThreadShutdown, this,
186 listener_event, ipc_event));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900187 }
188
189 void OnIPCThreadShutdown(WaitableEvent* listener_event,
190 WaitableEvent* ipc_event) {
191 MessageLoop::current()->RunAllPending();
192 ipc_event->Signal();
193
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +0900194 listener_thread_.message_loop()->PostTask(
195 FROM_HERE, base::Bind(&Worker::OnListenerThreadShutdown2, this,
196 listener_event));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900197 }
198
199 void OnListenerThreadShutdown2(WaitableEvent* listener_event) {
200 MessageLoop::current()->RunAllPending();
201 listener_event->Signal();
202 }
203
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900204 bool OnMessageReceived(const Message& message) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900205 IPC_BEGIN_MESSAGE_MAP(Worker, message)
206 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay)
207 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife,
208 OnAnswerDelay)
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900209 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String,
210 OnNestedTestMsg)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900211 IPC_END_MESSAGE_MAP()
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900212 return true;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900213 }
214
215 void StartThread(base::Thread* thread, MessageLoop::Type type) {
216 base::Thread::Options options;
217 options.message_loop_type = type;
218 thread->StartWithOptions(options);
219 }
220
221 scoped_ptr<WaitableEvent> done_;
222 scoped_ptr<WaitableEvent> channel_created_;
223 std::string channel_name_;
224 Channel::Mode mode_;
225 scoped_ptr<SyncChannel> channel_;
226 base::Thread ipc_thread_;
227 base::Thread listener_thread_;
228 base::Thread* overrided_thread_;
229
230 base::WaitableEvent shutdown_event_;
231
tfarina@chromium.orgb73eaee2010-06-07 11:10:18 +0900232 DISALLOW_COPY_AND_ASSIGN(Worker);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900233};
234
235
236// Starts the test with the given workers. This function deletes the workers
237// when it's done.
238void RunTest(std::vector<Worker*> workers) {
239 // First we create the workers that are channel servers, or else the other
240 // workers' channel initialization might fail because the pipe isn't created..
241 for (size_t i = 0; i < workers.size(); ++i) {
dmaclach@chromium.orgf146c292011-02-04 05:35:09 +0900242 if (workers[i]->mode() & Channel::MODE_SERVER_FLAG) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900243 workers[i]->Start();
244 workers[i]->WaitForChannelCreation();
245 }
246 }
247
248 // now create the clients
249 for (size_t i = 0; i < workers.size(); ++i) {
dmaclach@chromium.orgf146c292011-02-04 05:35:09 +0900250 if (workers[i]->mode() & Channel::MODE_CLIENT_FLAG)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900251 workers[i]->Start();
252 }
253
254 // wait for all the workers to finish
255 for (size_t i = 0; i < workers.size(); ++i)
256 workers[i]->done_event()->Wait();
257
258 STLDeleteContainerPointers(workers.begin(), workers.end());
259}
260
261} // namespace
262
263class IPCSyncChannelTest : public testing::Test {
264 private:
265 MessageLoop message_loop_;
266};
267
268//-----------------------------------------------------------------------------
269
270namespace {
271
272class SimpleServer : public Worker {
273 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900274 explicit SimpleServer(bool pump_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900275 : Worker(Channel::MODE_SERVER, "simpler_server"),
276 pump_during_send_(pump_during_send) { }
277 void Run() {
278 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
279 Done();
280 }
281
282 bool pump_during_send_;
283};
284
285class SimpleClient : public Worker {
286 public:
287 SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { }
288
289 void OnAnswer(int* answer) {
290 *answer = 42;
291 Done();
292 }
293};
294
295void Simple(bool pump_during_send) {
296 std::vector<Worker*> workers;
297 workers.push_back(new SimpleServer(pump_during_send));
298 workers.push_back(new SimpleClient());
299 RunTest(workers);
300}
301
302} // namespace
303
304// Tests basic synchronous call
305TEST_F(IPCSyncChannelTest, Simple) {
306 Simple(false);
307 Simple(true);
308}
309
310//-----------------------------------------------------------------------------
311
312namespace {
313
314class DelayClient : public Worker {
315 public:
316 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { }
317
318 void OnAnswerDelay(Message* reply_msg) {
319 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
320 Send(reply_msg);
321 Done();
322 }
323};
324
325void DelayReply(bool pump_during_send) {
326 std::vector<Worker*> workers;
327 workers.push_back(new SimpleServer(pump_during_send));
328 workers.push_back(new DelayClient());
329 RunTest(workers);
330}
331
332} // namespace
333
334// Tests that asynchronous replies work
335TEST_F(IPCSyncChannelTest, DelayReply) {
336 DelayReply(false);
337 DelayReply(true);
338}
339
340//-----------------------------------------------------------------------------
341
342namespace {
343
344class NoHangServer : public Worker {
345 public:
dilmah@chromium.org92b4c8e2011-07-27 02:25:25 +0900346 NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900347 : Worker(Channel::MODE_SERVER, "no_hang_server"),
348 got_first_reply_(got_first_reply),
349 pump_during_send_(pump_during_send) { }
350 void Run() {
351 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
352 got_first_reply_->Signal();
353
354 SendAnswerToLife(pump_during_send_, base::kNoTimeout, false);
355 Done();
356 }
357
358 WaitableEvent* got_first_reply_;
359 bool pump_during_send_;
360};
361
362class NoHangClient : public Worker {
363 public:
364 explicit NoHangClient(WaitableEvent* got_first_reply)
365 : Worker(Channel::MODE_CLIENT, "no_hang_client"),
366 got_first_reply_(got_first_reply) { }
367
368 virtual void OnAnswerDelay(Message* reply_msg) {
369 // Use the DELAY_REPLY macro so that we can force the reply to be sent
370 // before this function returns (when the channel will be reset).
371 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
372 Send(reply_msg);
373 got_first_reply_->Wait();
374 CloseChannel();
375 Done();
376 }
377
378 WaitableEvent* got_first_reply_;
379};
380
381void NoHang(bool pump_during_send) {
382 WaitableEvent got_first_reply(false, false);
383 std::vector<Worker*> workers;
384 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send));
385 workers.push_back(new NoHangClient(&got_first_reply));
386 RunTest(workers);
387}
388
389} // namespace
390
391// Tests that caller doesn't hang if receiver dies
392TEST_F(IPCSyncChannelTest, NoHang) {
393 NoHang(false);
394 NoHang(true);
395}
396
397//-----------------------------------------------------------------------------
398
399namespace {
400
401class UnblockServer : public Worker {
402 public:
jam@chromium.orgebd07182009-12-01 11:34:18 +0900403 UnblockServer(bool pump_during_send, bool delete_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900404 : Worker(Channel::MODE_SERVER, "unblock_server"),
jam@chromium.orgebd07182009-12-01 11:34:18 +0900405 pump_during_send_(pump_during_send),
406 delete_during_send_(delete_during_send) { }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900407 void Run() {
jam@chromium.orgebd07182009-12-01 11:34:18 +0900408 if (delete_during_send_) {
409 // Use custom code since race conditions mean the answer may or may not be
410 // available.
411 int answer = 0;
412 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
413 if (pump_during_send_)
414 msg->EnableMessagePumping();
415 Send(msg);
416 } else {
417 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
418 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900419 Done();
420 }
421
jam@chromium.orgebd07182009-12-01 11:34:18 +0900422 void OnDoubleDelay(int in, Message* reply_msg) {
423 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
424 Send(reply_msg);
425 if (delete_during_send_)
426 ResetChannel();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900427 }
428
429 bool pump_during_send_;
jam@chromium.orgebd07182009-12-01 11:34:18 +0900430 bool delete_during_send_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900431};
432
433class UnblockClient : public Worker {
434 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900435 explicit UnblockClient(bool pump_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900436 : Worker(Channel::MODE_CLIENT, "unblock_client"),
437 pump_during_send_(pump_during_send) { }
438
439 void OnAnswer(int* answer) {
440 SendDouble(pump_during_send_, true);
441 *answer = 42;
442 Done();
443 }
444
445 bool pump_during_send_;
446};
447
jam@chromium.orgebd07182009-12-01 11:34:18 +0900448void Unblock(bool server_pump, bool client_pump, bool delete_during_send) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900449 std::vector<Worker*> workers;
jam@chromium.orgebd07182009-12-01 11:34:18 +0900450 workers.push_back(new UnblockServer(server_pump, delete_during_send));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900451 workers.push_back(new UnblockClient(client_pump));
452 RunTest(workers);
453}
454
455} // namespace
456
457// Tests that the caller unblocks to answer a sync message from the receiver.
458TEST_F(IPCSyncChannelTest, Unblock) {
jam@chromium.orgebd07182009-12-01 11:34:18 +0900459 Unblock(false, false, false);
460 Unblock(false, true, false);
461 Unblock(true, false, false);
462 Unblock(true, true, false);
463}
464
465//-----------------------------------------------------------------------------
466
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +0900467// Tests that the the SyncChannel object can be deleted during a Send.
jam@chromium.orgebd07182009-12-01 11:34:18 +0900468TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) {
469 Unblock(false, false, true);
470 Unblock(false, true, true);
471 Unblock(true, false, true);
472 Unblock(true, true, true);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900473}
474
475//-----------------------------------------------------------------------------
476
477namespace {
478
479class RecursiveServer : public Worker {
480 public:
dilmah@chromium.org92b4c8e2011-07-27 02:25:25 +0900481 RecursiveServer(bool expected_send_result, bool pump_first, bool pump_second)
482 : Worker(Channel::MODE_SERVER, "recursive_server"),
483 expected_send_result_(expected_send_result),
484 pump_first_(pump_first), pump_second_(pump_second) {}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900485 void Run() {
486 SendDouble(pump_first_, expected_send_result_);
487 Done();
488 }
489
490 void OnDouble(int in, int* out) {
491 *out = in * 2;
492 SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_);
493 }
494
495 bool expected_send_result_, pump_first_, pump_second_;
496};
497
498class RecursiveClient : public Worker {
499 public:
dilmah@chromium.org92b4c8e2011-07-27 02:25:25 +0900500 RecursiveClient(bool pump_during_send, bool close_channel)
501 : Worker(Channel::MODE_CLIENT, "recursive_client"),
502 pump_during_send_(pump_during_send), close_channel_(close_channel) {}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900503
504 void OnDoubleDelay(int in, Message* reply_msg) {
505 SendDouble(pump_during_send_, !close_channel_);
506 if (close_channel_) {
507 delete reply_msg;
508 } else {
509 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
510 Send(reply_msg);
511 }
512 Done();
513 }
514
515 void OnAnswerDelay(Message* reply_msg) {
516 if (close_channel_) {
517 delete reply_msg;
518 CloseChannel();
519 } else {
520 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
521 Send(reply_msg);
522 }
523 }
524
525 bool pump_during_send_, close_channel_;
526};
527
528void Recursive(
529 bool server_pump_first, bool server_pump_second, bool client_pump) {
530 std::vector<Worker*> workers;
531 workers.push_back(
532 new RecursiveServer(true, server_pump_first, server_pump_second));
533 workers.push_back(new RecursiveClient(client_pump, false));
534 RunTest(workers);
535}
536
537} // namespace
538
539// Tests a server calling Send while another Send is pending.
540TEST_F(IPCSyncChannelTest, Recursive) {
541 Recursive(false, false, false);
542 Recursive(false, false, true);
543 Recursive(false, true, false);
544 Recursive(false, true, true);
545 Recursive(true, false, false);
546 Recursive(true, false, true);
547 Recursive(true, true, false);
548 Recursive(true, true, true);
549}
550
551//-----------------------------------------------------------------------------
552
553namespace {
554
555void RecursiveNoHang(
556 bool server_pump_first, bool server_pump_second, bool client_pump) {
557 std::vector<Worker*> workers;
558 workers.push_back(
559 new RecursiveServer(false, server_pump_first, server_pump_second));
560 workers.push_back(new RecursiveClient(client_pump, true));
561 RunTest(workers);
562}
563
564} // namespace
565
566// Tests that if a caller makes a sync call during an existing sync call and
567// the receiver dies, neither of the Send() calls hang.
568TEST_F(IPCSyncChannelTest, RecursiveNoHang) {
569 RecursiveNoHang(false, false, false);
570 RecursiveNoHang(false, false, true);
571 RecursiveNoHang(false, true, false);
572 RecursiveNoHang(false, true, true);
573 RecursiveNoHang(true, false, false);
574 RecursiveNoHang(true, false, true);
575 RecursiveNoHang(true, true, false);
576 RecursiveNoHang(true, true, true);
577}
578
579//-----------------------------------------------------------------------------
580
581namespace {
582
583class MultipleServer1 : public Worker {
584 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900585 explicit MultipleServer1(bool pump_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900586 : Worker("test_channel1", Channel::MODE_SERVER),
587 pump_during_send_(pump_during_send) { }
588
589 void Run() {
590 SendDouble(pump_during_send_, true);
591 Done();
592 }
593
594 bool pump_during_send_;
595};
596
597class MultipleClient1 : public Worker {
598 public:
599 MultipleClient1(WaitableEvent* client1_msg_received,
600 WaitableEvent* client1_can_reply) :
601 Worker("test_channel1", Channel::MODE_CLIENT),
602 client1_msg_received_(client1_msg_received),
603 client1_can_reply_(client1_can_reply) { }
604
605 void OnDouble(int in, int* out) {
606 client1_msg_received_->Signal();
607 *out = in * 2;
608 client1_can_reply_->Wait();
609 Done();
610 }
611
612 private:
613 WaitableEvent *client1_msg_received_, *client1_can_reply_;
614};
615
616class MultipleServer2 : public Worker {
617 public:
618 MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { }
619
620 void OnAnswer(int* result) {
621 *result = 42;
622 Done();
623 }
624};
625
626class MultipleClient2 : public Worker {
627 public:
628 MultipleClient2(
629 WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply,
630 bool pump_during_send)
631 : Worker("test_channel2", Channel::MODE_CLIENT),
632 client1_msg_received_(client1_msg_received),
633 client1_can_reply_(client1_can_reply),
634 pump_during_send_(pump_during_send) { }
635
636 void Run() {
637 client1_msg_received_->Wait();
638 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
639 client1_can_reply_->Signal();
640 Done();
641 }
642
643 private:
644 WaitableEvent *client1_msg_received_, *client1_can_reply_;
645 bool pump_during_send_;
646};
647
648void Multiple(bool server_pump, bool client_pump) {
649 std::vector<Worker*> workers;
650
651 // A shared worker thread so that server1 and server2 run on one thread.
652 base::Thread worker_thread("Multiple");
653 ASSERT_TRUE(worker_thread.Start());
654
655 // Server1 sends a sync msg to client1, which blocks the reply until
656 // server2 (which runs on the same worker thread as server1) responds
657 // to a sync msg from client2.
658 WaitableEvent client1_msg_received(false, false);
659 WaitableEvent client1_can_reply(false, false);
660
661 Worker* worker;
662
663 worker = new MultipleServer2();
664 worker->OverrideThread(&worker_thread);
665 workers.push_back(worker);
666
667 worker = new MultipleClient2(
668 &client1_msg_received, &client1_can_reply, client_pump);
669 workers.push_back(worker);
670
671 worker = new MultipleServer1(server_pump);
672 worker->OverrideThread(&worker_thread);
673 workers.push_back(worker);
674
675 worker = new MultipleClient1(
676 &client1_msg_received, &client1_can_reply);
677 workers.push_back(worker);
678
679 RunTest(workers);
680}
681
682} // namespace
683
684// Tests that multiple SyncObjects on the same listener thread can unblock each
685// other.
686TEST_F(IPCSyncChannelTest, Multiple) {
687 Multiple(false, false);
688 Multiple(false, true);
689 Multiple(true, false);
690 Multiple(true, true);
691}
692
693//-----------------------------------------------------------------------------
694
695namespace {
696
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900697// This class provides server side functionality to test the case where
698// multiple sync channels are in use on the same thread on the client and
699// nested calls are issued.
700class QueuedReplyServer : public Worker {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900701 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900702 QueuedReplyServer(base::Thread* listener_thread,
703 const std::string& channel_name,
704 const std::string& reply_text)
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900705 : Worker(channel_name, Channel::MODE_SERVER),
706 reply_text_(reply_text) {
707 Worker::OverrideThread(listener_thread);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900708 }
709
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900710 virtual void OnNestedTestMsg(Message* reply_msg) {
pkasting@chromium.orgfcdd54b2010-10-20 08:50:00 +0900711 VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_;
712 SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_);
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900713 Send(reply_msg);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900714 Done();
715 }
716
717 private:
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900718 std::string reply_text_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900719};
720
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900721// The QueuedReplyClient class provides functionality to test the case where
722// multiple sync channels are in use on the same thread and they make nested
723// sync calls, i.e. while the first channel waits for a response it makes a
724// sync call on another channel.
725// The callstack should unwind correctly, i.e. the outermost call should
726// complete first, and so on.
727class QueuedReplyClient : public Worker {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900728 public:
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900729 QueuedReplyClient(base::Thread* listener_thread,
730 const std::string& channel_name,
731 const std::string& expected_text,
732 bool pump_during_send)
733 : Worker(channel_name, Channel::MODE_CLIENT),
thomasvl@google.com9a242072010-07-23 23:18:59 +0900734 pump_during_send_(pump_during_send),
735 expected_text_(expected_text) {
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900736 Worker::OverrideThread(listener_thread);
737 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900738
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900739 virtual void Run() {
740 std::string response;
741 SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response);
742 if (pump_during_send_)
743 msg->EnableMessagePumping();
744 bool result = Send(msg);
745 DCHECK(result);
jam@chromium.orgebd07182009-12-01 11:34:18 +0900746 DCHECK_EQ(response, expected_text_);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900747
pkasting@chromium.orgfcdd54b2010-10-20 08:50:00 +0900748 VLOG(1) << __FUNCTION__ << " Received reply: " << response;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900749 Done();
750 }
751
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900752 private:
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900753 bool pump_during_send_;
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900754 std::string expected_text_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900755};
756
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900757void QueuedReply(bool client_pump) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900758 std::vector<Worker*> workers;
759
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900760 // A shared worker thread for servers
761 base::Thread server_worker_thread("QueuedReply_ServerListener");
762 ASSERT_TRUE(server_worker_thread.Start());
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900763
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900764 base::Thread client_worker_thread("QueuedReply_ClientListener");
765 ASSERT_TRUE(client_worker_thread.Start());
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900766
767 Worker* worker;
768
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900769 worker = new QueuedReplyServer(&server_worker_thread,
770 "QueuedReply_Server1",
771 "Got first message");
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900772 workers.push_back(worker);
773
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900774 worker = new QueuedReplyServer(&server_worker_thread,
775 "QueuedReply_Server2",
776 "Got second message");
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900777 workers.push_back(worker);
778
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900779 worker = new QueuedReplyClient(&client_worker_thread,
780 "QueuedReply_Server1",
781 "Got first message",
782 client_pump);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900783 workers.push_back(worker);
784
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900785 worker = new QueuedReplyClient(&client_worker_thread,
786 "QueuedReply_Server2",
787 "Got second message",
788 client_pump);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900789 workers.push_back(worker);
790
791 RunTest(workers);
792}
793
794} // namespace
795
796// While a blocking send is in progress, the listener thread might answer other
797// synchronous messages. This tests that if during the response to another
798// message the reply to the original messages comes, it is queued up correctly
799// and the original Send is unblocked later.
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900800// We also test that the send call stacks unwind correctly when the channel
801// pumps messages while waiting for a response.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900802TEST_F(IPCSyncChannelTest, QueuedReply) {
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900803 QueuedReply(false);
804 QueuedReply(true);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900805}
806
807//-----------------------------------------------------------------------------
808
809namespace {
810
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900811class ChattyClient : public Worker {
812 public:
813 ChattyClient() :
814 Worker(Channel::MODE_CLIENT, "chatty_client") { }
815
816 void OnAnswer(int* answer) {
817 // The PostMessage limit is 10k. Send 20% more than that.
818 const int kMessageLimit = 10000;
819 const int kMessagesToSend = kMessageLimit * 120 / 100;
820 for (int i = 0; i < kMessagesToSend; ++i) {
821 if (!SendDouble(false, true))
822 break;
823 }
824 *answer = 42;
825 Done();
826 }
827};
828
829void ChattyServer(bool pump_during_send) {
830 std::vector<Worker*> workers;
jam@chromium.orgebd07182009-12-01 11:34:18 +0900831 workers.push_back(new UnblockServer(pump_during_send, false));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900832 workers.push_back(new ChattyClient());
833 RunTest(workers);
834}
835
836} // namespace
837
838// Tests http://b/1093251 - that sending lots of sync messages while
839// the receiver is waiting for a sync reply does not overflow the PostMessage
840// queue.
841TEST_F(IPCSyncChannelTest, ChattyServer) {
842 ChattyServer(false);
843 ChattyServer(true);
844}
845
846//------------------------------------------------------------------------------
847
848namespace {
849
850class TimeoutServer : public Worker {
851 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900852 TimeoutServer(int timeout_ms,
853 std::vector<bool> timeout_seq,
854 bool pump_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900855 : Worker(Channel::MODE_SERVER, "timeout_server"),
856 timeout_ms_(timeout_ms),
857 timeout_seq_(timeout_seq),
858 pump_during_send_(pump_during_send) {
859 }
860
861 void Run() {
862 for (std::vector<bool>::const_iterator iter = timeout_seq_.begin();
863 iter != timeout_seq_.end(); ++iter) {
864 SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter);
865 }
866 Done();
867 }
868
869 private:
870 int timeout_ms_;
871 std::vector<bool> timeout_seq_;
872 bool pump_during_send_;
873};
874
875class UnresponsiveClient : public Worker {
876 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900877 explicit UnresponsiveClient(std::vector<bool> timeout_seq)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900878 : Worker(Channel::MODE_CLIENT, "unresponsive_client"),
879 timeout_seq_(timeout_seq) {
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900880 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900881
882 void OnAnswerDelay(Message* reply_msg) {
883 DCHECK(!timeout_seq_.empty());
884 if (!timeout_seq_[0]) {
885 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
886 Send(reply_msg);
887 } else {
888 // Don't reply.
889 delete reply_msg;
890 }
891 timeout_seq_.erase(timeout_seq_.begin());
892 if (timeout_seq_.empty())
893 Done();
894 }
895
896 private:
897 // Whether we should time-out or respond to the various messages we receive.
898 std::vector<bool> timeout_seq_;
899};
900
901void SendWithTimeoutOK(bool pump_during_send) {
902 std::vector<Worker*> workers;
903 std::vector<bool> timeout_seq;
904 timeout_seq.push_back(false);
905 timeout_seq.push_back(false);
906 timeout_seq.push_back(false);
907 workers.push_back(new TimeoutServer(5000, timeout_seq, pump_during_send));
908 workers.push_back(new SimpleClient());
909 RunTest(workers);
910}
911
912void SendWithTimeoutTimeout(bool pump_during_send) {
913 std::vector<Worker*> workers;
914 std::vector<bool> timeout_seq;
915 timeout_seq.push_back(true);
916 timeout_seq.push_back(false);
917 timeout_seq.push_back(false);
918 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
919 workers.push_back(new UnresponsiveClient(timeout_seq));
920 RunTest(workers);
921}
922
923void SendWithTimeoutMixedOKAndTimeout(bool pump_during_send) {
924 std::vector<Worker*> workers;
925 std::vector<bool> timeout_seq;
926 timeout_seq.push_back(true);
927 timeout_seq.push_back(false);
928 timeout_seq.push_back(false);
929 timeout_seq.push_back(true);
930 timeout_seq.push_back(false);
931 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
932 workers.push_back(new UnresponsiveClient(timeout_seq));
933 RunTest(workers);
934}
935
936} // namespace
937
938// Tests that SendWithTimeout does not time-out if the response comes back fast
939// enough.
940TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) {
941 SendWithTimeoutOK(false);
942 SendWithTimeoutOK(true);
943}
944
945// Tests that SendWithTimeout does time-out.
946TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) {
947 SendWithTimeoutTimeout(false);
948 SendWithTimeoutTimeout(true);
949}
950
951// Sends some message that time-out and some that succeed.
phajdan.jr@chromium.orga8e3c092011-01-19 17:11:56 +0900952// Crashes flakily, http://crbug.com/70075.
953TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900954 SendWithTimeoutMixedOKAndTimeout(false);
955 SendWithTimeoutMixedOKAndTimeout(true);
956}
957
958//------------------------------------------------------------------------------
959
960namespace {
961
962class NestedTask : public Task {
963 public:
dilmah@chromium.org92b4c8e2011-07-27 02:25:25 +0900964 explicit NestedTask(Worker* server) : server_(server) {}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900965 void Run() {
966 // Sleep a bit so that we wake up after the reply has been received.
brettw@google.com7c5cc672011-01-01 11:17:08 +0900967 base::PlatformThread::Sleep(250);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900968 server_->SendAnswerToLife(true, base::kNoTimeout, true);
969 }
970
971 Worker* server_;
972};
973
974static bool timeout_occured = false;
975
976class TimeoutTask : public Task {
977 public:
978 void Run() {
979 timeout_occured = true;
980 }
981};
982
983class DoneEventRaceServer : public Worker {
984 public:
985 DoneEventRaceServer()
986 : Worker(Channel::MODE_SERVER, "done_event_race_server") { }
987
988 void Run() {
989 MessageLoop::current()->PostTask(FROM_HERE, new NestedTask(this));
990 MessageLoop::current()->PostDelayedTask(FROM_HERE, new TimeoutTask(), 9000);
991 // Even though we have a timeout on the Send, it will succeed since for this
992 // bug, the reply message comes back and is deserialized, however the done
993 // event wasn't set. So we indirectly use the timeout task to notice if a
994 // timeout occurred.
995 SendAnswerToLife(true, 10000, true);
996 DCHECK(!timeout_occured);
997 Done();
998 }
999};
1000
1001} // namespace
1002
1003// Tests http://b/1474092 - that if after the done_event is set but before
1004// OnObjectSignaled is called another message is sent out, then after its
1005// reply comes back OnObjectSignaled will be called for the first message.
1006TEST_F(IPCSyncChannelTest, DoneEventRace) {
1007 std::vector<Worker*> workers;
1008 workers.push_back(new DoneEventRaceServer());
1009 workers.push_back(new SimpleClient());
1010 RunTest(workers);
1011}
jabdelmalek@google.comeb921652010-04-07 05:33:36 +09001012
1013//-----------------------------------------------------------------------------
1014
1015namespace {
1016
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +09001017class TestSyncMessageFilter : public SyncMessageFilter {
jabdelmalek@google.comeb921652010-04-07 05:33:36 +09001018 public:
1019 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker)
1020 : SyncMessageFilter(shutdown_event),
1021 worker_(worker),
1022 thread_("helper_thread") {
1023 base::Thread::Options options;
1024 options.message_loop_type = MessageLoop::TYPE_DEFAULT;
1025 thread_.StartWithOptions(options);
1026 }
1027
1028 virtual void OnFilterAdded(Channel* channel) {
1029 SyncMessageFilter::OnFilterAdded(channel);
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +09001030 thread_.message_loop()->PostTask(
1031 FROM_HERE,
1032 base::Bind(&TestSyncMessageFilter::SendMessageOnHelperThread, this));
jabdelmalek@google.comeb921652010-04-07 05:33:36 +09001033 }
1034
1035 void SendMessageOnHelperThread() {
1036 int answer = 0;
1037 bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer));
1038 DCHECK(result);
1039 DCHECK_EQ(answer, 42);
1040
1041 worker_->Done();
1042 }
1043
1044 Worker* worker_;
1045 base::Thread thread_;
1046};
1047
1048class SyncMessageFilterServer : public Worker {
1049 public:
1050 SyncMessageFilterServer()
1051 : Worker(Channel::MODE_SERVER, "sync_message_filter_server") {
1052 filter_ = new TestSyncMessageFilter(shutdown_event(), this);
1053 }
1054
1055 void Run() {
1056 channel()->AddFilter(filter_.get());
1057 }
1058
1059 scoped_refptr<TestSyncMessageFilter> filter_;
1060};
1061
ananta@chromium.org999f2972010-09-03 06:45:50 +09001062// This class provides functionality to test the case that a Send on the sync
1063// channel does not crash after the channel has been closed.
1064class ServerSendAfterClose : public Worker {
1065 public:
1066 ServerSendAfterClose()
1067 : Worker(Channel::MODE_SERVER, "simpler_server"),
1068 send_result_(true) {
1069 }
1070
1071 bool SendDummy() {
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +09001072 ListenerThread()->message_loop()->PostTask(
1073 FROM_HERE, base::IgnoreReturn<bool>(
1074 base::Bind(&ServerSendAfterClose::Send, this,
1075 new SyncChannelTestMsg_NoArgs)));
ananta@chromium.org999f2972010-09-03 06:45:50 +09001076 return true;
1077 }
1078
1079 bool send_result() const {
1080 return send_result_;
1081 }
1082
1083 private:
1084 virtual void Run() {
1085 CloseChannel();
1086 Done();
1087 }
1088
1089 bool Send(Message* msg) {
1090 send_result_ = Worker::Send(msg);
1091 Done();
1092 return send_result_;
1093 }
1094
1095 bool send_result_;
1096};
1097
jabdelmalek@google.comeb921652010-04-07 05:33:36 +09001098} // namespace
1099
1100// Tests basic synchronous call
1101TEST_F(IPCSyncChannelTest, SyncMessageFilter) {
1102 std::vector<Worker*> workers;
1103 workers.push_back(new SyncMessageFilterServer());
1104 workers.push_back(new SimpleClient());
1105 RunTest(workers);
1106}
ananta@chromium.org999f2972010-09-03 06:45:50 +09001107
1108// Test the case when the channel is closed and a Send is attempted after that.
1109TEST_F(IPCSyncChannelTest, SendAfterClose) {
1110 ServerSendAfterClose server;
1111 server.Start();
1112
1113 server.done_event()->Wait();
1114 server.done_event()->Reset();
1115
1116 server.SendDummy();
1117 server.done_event()->Wait();
1118
1119 EXPECT_FALSE(server.send_result());
1120}
1121
piman@google.com0cbefaa2011-04-08 12:38:21 +09001122//-----------------------------------------------------------------------------
ananta@chromium.org999f2972010-09-03 06:45:50 +09001123
piman@google.com0cbefaa2011-04-08 12:38:21 +09001124namespace {
1125
1126class RestrictedDispatchServer : public Worker {
1127 public:
1128 RestrictedDispatchServer(WaitableEvent* sent_ping_event)
1129 : Worker("restricted_channel", Channel::MODE_SERVER),
1130 sent_ping_event_(sent_ping_event) { }
1131
1132 void OnDoPing(int ping) {
1133 // Send an asynchronous message that unblocks the caller.
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +09001134 Message* msg = new SyncChannelTestMsg_Ping(ping);
piman@google.com0cbefaa2011-04-08 12:38:21 +09001135 msg->set_unblock(true);
1136 Send(msg);
1137 // Signal the event after the message has been sent on the channel, on the
1138 // IPC thread.
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +09001139 ipc_thread().message_loop()->PostTask(
1140 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnPingSent, this));
piman@google.com0cbefaa2011-04-08 12:38:21 +09001141 }
1142
1143 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1144
1145 private:
1146 bool OnMessageReceived(const Message& message) {
1147 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchServer, message)
1148 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1149 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1150 IPC_END_MESSAGE_MAP()
1151 return true;
1152 }
1153
1154 void OnPingSent() {
1155 sent_ping_event_->Signal();
1156 }
1157
1158 void OnNoArgs() { }
1159 WaitableEvent* sent_ping_event_;
1160};
1161
1162class NonRestrictedDispatchServer : public Worker {
1163 public:
1164 NonRestrictedDispatchServer()
1165 : Worker("non_restricted_channel", Channel::MODE_SERVER) {}
1166
1167 private:
1168 bool OnMessageReceived(const Message& message) {
1169 IPC_BEGIN_MESSAGE_MAP(NonRestrictedDispatchServer, message)
1170 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1171 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1172 IPC_END_MESSAGE_MAP()
1173 return true;
1174 }
1175
1176 void OnNoArgs() { }
1177};
1178
1179class RestrictedDispatchClient : public Worker {
1180 public:
1181 RestrictedDispatchClient(WaitableEvent* sent_ping_event,
1182 RestrictedDispatchServer* server,
1183 int* success)
1184 : Worker("restricted_channel", Channel::MODE_CLIENT),
1185 ping_(0),
1186 server_(server),
1187 success_(success),
1188 sent_ping_event_(sent_ping_event) {}
1189
1190 void Run() {
1191 // Incoming messages from our channel should only be dispatched when we
1192 // send a message on that same channel.
1193 channel()->SetRestrictDispatchToSameChannel(true);
1194
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +09001195 server_->ListenerThread()->message_loop()->PostTask(
1196 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 1));
piman@google.com0cbefaa2011-04-08 12:38:21 +09001197 sent_ping_event_->Wait();
1198 Send(new SyncChannelTestMsg_NoArgs);
1199 if (ping_ == 1)
1200 ++*success_;
1201 else
1202 LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
1203
1204 scoped_ptr<SyncChannel> non_restricted_channel(new SyncChannel(
1205 "non_restricted_channel", Channel::MODE_CLIENT, this,
jam@chromium.org06d18442011-05-03 03:00:49 +09001206 ipc_thread().message_loop_proxy(), true, shutdown_event()));
piman@google.com0cbefaa2011-04-08 12:38:21 +09001207
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +09001208 server_->ListenerThread()->message_loop()->PostTask(
1209 FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 2));
piman@google.com0cbefaa2011-04-08 12:38:21 +09001210 sent_ping_event_->Wait();
1211 // Check that the incoming message is *not* dispatched when sending on the
1212 // non restricted channel.
1213 // TODO(piman): there is a possibility of a false positive race condition
1214 // here, if the message that was posted on the server-side end of the pipe
1215 // is not visible yet on the client side, but I don't know how to solve this
1216 // without hooking into the internals of SyncChannel. I haven't seen it in
1217 // practice (i.e. not setting SetRestrictDispatchToSameChannel does cause
1218 // the following to fail).
1219 non_restricted_channel->Send(new SyncChannelTestMsg_NoArgs);
1220 if (ping_ == 1)
1221 ++*success_;
1222 else
1223 LOG(ERROR) << "Send dispatched message from restricted channel";
1224
1225 Send(new SyncChannelTestMsg_NoArgs);
1226 if (ping_ == 2)
1227 ++*success_;
1228 else
1229 LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
1230
1231 non_restricted_channel->Send(new SyncChannelTestMsg_Done);
1232 non_restricted_channel.reset();
1233 Send(new SyncChannelTestMsg_Done);
1234 Done();
1235 }
1236
1237 private:
1238 bool OnMessageReceived(const Message& message) {
1239 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchClient, message)
1240 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Ping, OnPing)
1241 IPC_END_MESSAGE_MAP()
1242 return true;
1243 }
1244
1245 void OnPing(int ping) {
1246 ping_ = ping;
1247 }
1248
1249 int ping_;
1250 RestrictedDispatchServer* server_;
1251 int* success_;
1252 WaitableEvent* sent_ping_event_;
1253};
1254
1255} // namespace
1256
1257TEST_F(IPCSyncChannelTest, RestrictedDispatch) {
1258 WaitableEvent sent_ping_event(false, false);
1259
1260 RestrictedDispatchServer* server =
1261 new RestrictedDispatchServer(&sent_ping_event);
1262 int success = 0;
1263 std::vector<Worker*> workers;
1264 workers.push_back(new NonRestrictedDispatchServer);
1265 workers.push_back(server);
1266 workers.push_back(
1267 new RestrictedDispatchClient(&sent_ping_event, server, &success));
1268 RunTest(workers);
1269 EXPECT_EQ(3, success);
1270}
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +09001271
1272} // namespace IPC