blob: bdd83eb1f24f20f8a23234c92bb578ea98aa26da [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"
13#include "base/logging.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090014#include "base/memory/scoped_ptr.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090015#include "base/message_loop.h"
dilmah@chromium.orgdc4b9702011-07-20 07:13:24 +090016#include "base/stl_util.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090017#include "base/string_util.h"
timurrrr@chromium.orgf39c3ff2010-05-14 17:24:42 +090018#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
brettw@google.com7c5cc672011-01-01 11:17:08 +090019#include "base/threading/platform_thread.h"
brettw@chromium.org5b5f5e02011-01-01 10:01:06 +090020#include "base/threading/thread.h"
brettw@chromium.org5238c7d2011-01-02 15:05:39 +090021#include "base/synchronization/waitable_event.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090022#include "ipc/ipc_message.h"
jabdelmalek@google.comeb921652010-04-07 05:33:36 +090023#include "ipc/ipc_sync_message_filter.h"
jam@chromium.org86a8de12010-12-09 08:34:16 +090024#include "ipc/ipc_sync_message_unittest.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090025#include "testing/gtest/include/gtest/gtest.h"
26
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090027using base::WaitableEvent;
28
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +090029namespace IPC {
30
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090031namespace {
32
33// Base class for a "process" with listener and IPC threads.
34class Worker : public Channel::Listener, public Message::Sender {
35 public:
36 // Will create a channel without a name.
37 Worker(Channel::Mode mode, const std::string& thread_name)
38 : done_(new WaitableEvent(false, false)),
39 channel_created_(new WaitableEvent(false, false)),
40 mode_(mode),
41 ipc_thread_((thread_name + "_ipc").c_str()),
42 listener_thread_((thread_name + "_listener").c_str()),
43 overrided_thread_(NULL),
timurrrr@chromium.org03100a82009-10-27 20:28:58 +090044 shutdown_event_(true, false) {
45 // The data race on vfptr is real but is very hard
46 // to suppress using standard Valgrind mechanism (suppressions).
47 // We have to use ANNOTATE_BENIGN_RACE to hide the reports and
48 // make ThreadSanitizer bots green.
49 ANNOTATE_BENIGN_RACE(this, "Race on vfptr, http://crbug.com/25841");
50 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090051
52 // Will create a named channel and use this name for the threads' name.
53 Worker(const std::string& channel_name, Channel::Mode mode)
54 : done_(new WaitableEvent(false, false)),
55 channel_created_(new WaitableEvent(false, false)),
56 channel_name_(channel_name),
57 mode_(mode),
58 ipc_thread_((channel_name + "_ipc").c_str()),
59 listener_thread_((channel_name + "_listener").c_str()),
60 overrided_thread_(NULL),
timurrrr@chromium.org03100a82009-10-27 20:28:58 +090061 shutdown_event_(true, false) {
62 // The data race on vfptr is real but is very hard
63 // to suppress using standard Valgrind mechanism (suppressions).
64 // We have to use ANNOTATE_BENIGN_RACE to hide the reports and
65 // make ThreadSanitizer bots green.
66 ANNOTATE_BENIGN_RACE(this, "Race on vfptr, http://crbug.com/25841");
67 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090068
69 // The IPC thread needs to outlive SyncChannel, so force the correct order of
70 // destruction.
71 virtual ~Worker() {
72 WaitableEvent listener_done(false, false), ipc_done(false, false);
73 ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
74 this, &Worker::OnListenerThreadShutdown1, &listener_done,
75 &ipc_done));
76 listener_done.Wait();
77 ipc_done.Wait();
78 ipc_thread_.Stop();
79 listener_thread_.Stop();
80 }
81 void AddRef() { }
82 void Release() { }
darin@chromium.org5cb996e2009-09-30 13:29:20 +090083 static bool ImplementsThreadSafeReferenceCounting() { return true; }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090084 bool Send(Message* msg) { return channel_->Send(msg); }
85 bool SendWithTimeout(Message* msg, int timeout_ms) {
86 return channel_->SendWithTimeout(msg, timeout_ms);
87 }
88 void WaitForChannelCreation() { channel_created_->Wait(); }
89 void CloseChannel() {
90 DCHECK(MessageLoop::current() == ListenerThread()->message_loop());
91 channel_->Close();
92 }
93 void Start() {
94 StartThread(&listener_thread_, MessageLoop::TYPE_DEFAULT);
95 ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
96 this, &Worker::OnStart));
97 }
98 void OverrideThread(base::Thread* overrided_thread) {
99 DCHECK(overrided_thread_ == NULL);
100 overrided_thread_ = overrided_thread;
101 }
102 bool SendAnswerToLife(bool pump, int timeout, bool succeed) {
103 int answer = 0;
104 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
105 if (pump)
106 msg->EnableMessagePumping();
107 bool result = SendWithTimeout(msg, timeout);
jam@chromium.orgebd07182009-12-01 11:34:18 +0900108 DCHECK_EQ(result, succeed);
109 DCHECK_EQ(answer, (succeed ? 42 : 0));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900110 return result;
111 }
112 bool SendDouble(bool pump, bool succeed) {
113 int answer = 0;
114 SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer);
115 if (pump)
116 msg->EnableMessagePumping();
117 bool result = Send(msg);
jam@chromium.orgebd07182009-12-01 11:34:18 +0900118 DCHECK_EQ(result, succeed);
119 DCHECK_EQ(answer, (succeed ? 10 : 0));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900120 return result;
121 }
122 Channel::Mode mode() { return mode_; }
123 WaitableEvent* done_event() { return done_.get(); }
jabdelmalek@google.comeb921652010-04-07 05:33:36 +0900124 WaitableEvent* shutdown_event() { return &shutdown_event_; }
jam@chromium.orgebd07182009-12-01 11:34:18 +0900125 void ResetChannel() { channel_.reset(); }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900126 // Derived classes need to call this when they've completed their part of
127 // the test.
128 void Done() { done_->Signal(); }
jabdelmalek@google.comeb921652010-04-07 05:33:36 +0900129
130 protected:
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +0900131 SyncChannel* channel() { return channel_.get(); }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900132 // Functions for dervied classes to implement if they wish.
133 virtual void Run() { }
134 virtual void OnAnswer(int* answer) { NOTREACHED(); }
135 virtual void OnAnswerDelay(Message* reply_msg) {
136 // The message handler map below can only take one entry for
137 // SyncChannelTestMsg_AnswerToLife, so since some classes want
138 // the normal version while other want the delayed reply, we
139 // call the normal version if the derived class didn't override
140 // this function.
141 int answer;
142 OnAnswer(&answer);
143 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, answer);
144 Send(reply_msg);
145 }
146 virtual void OnDouble(int in, int* out) { NOTREACHED(); }
147 virtual void OnDoubleDelay(int in, Message* reply_msg) {
148 int result;
149 OnDouble(in, &result);
150 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result);
151 Send(reply_msg);
152 }
153
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900154 virtual void OnNestedTestMsg(Message* reply_msg) {
155 NOTREACHED();
156 }
157
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900158 base::Thread* ListenerThread() {
159 return overrided_thread_ ? overrided_thread_ : &listener_thread_;
160 }
ananta@chromium.org999f2972010-09-03 06:45:50 +0900161
piman@google.com0cbefaa2011-04-08 12:38:21 +0900162 const base::Thread& ipc_thread() const { return ipc_thread_; }
163
ananta@chromium.org999f2972010-09-03 06:45:50 +0900164 private:
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900165 // Called on the listener thread to create the sync channel.
166 void OnStart() {
167 // Link ipc_thread_, listener_thread_ and channel_ altogether.
168 StartThread(&ipc_thread_, MessageLoop::TYPE_IO);
169 channel_.reset(new SyncChannel(
jam@chromium.org06d18442011-05-03 03:00:49 +0900170 channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true,
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900171 &shutdown_event_));
172 channel_created_->Signal();
173 Run();
174 }
175
176 void OnListenerThreadShutdown1(WaitableEvent* listener_event,
177 WaitableEvent* ipc_event) {
178 // SyncChannel needs to be destructed on the thread that it was created on.
179 channel_.reset();
180
181 MessageLoop::current()->RunAllPending();
182
183 ipc_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
184 this, &Worker::OnIPCThreadShutdown, listener_event, ipc_event));
185 }
186
187 void OnIPCThreadShutdown(WaitableEvent* listener_event,
188 WaitableEvent* ipc_event) {
189 MessageLoop::current()->RunAllPending();
190 ipc_event->Signal();
191
192 listener_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
193 this, &Worker::OnListenerThreadShutdown2, listener_event));
194 }
195
196 void OnListenerThreadShutdown2(WaitableEvent* listener_event) {
197 MessageLoop::current()->RunAllPending();
198 listener_event->Signal();
199 }
200
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900201 bool OnMessageReceived(const Message& message) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900202 IPC_BEGIN_MESSAGE_MAP(Worker, message)
203 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay)
204 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife,
205 OnAnswerDelay)
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900206 IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String,
207 OnNestedTestMsg)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900208 IPC_END_MESSAGE_MAP()
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900209 return true;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900210 }
211
212 void StartThread(base::Thread* thread, MessageLoop::Type type) {
213 base::Thread::Options options;
214 options.message_loop_type = type;
215 thread->StartWithOptions(options);
216 }
217
218 scoped_ptr<WaitableEvent> done_;
219 scoped_ptr<WaitableEvent> channel_created_;
220 std::string channel_name_;
221 Channel::Mode mode_;
222 scoped_ptr<SyncChannel> channel_;
223 base::Thread ipc_thread_;
224 base::Thread listener_thread_;
225 base::Thread* overrided_thread_;
226
227 base::WaitableEvent shutdown_event_;
228
tfarina@chromium.orgb73eaee2010-06-07 11:10:18 +0900229 DISALLOW_COPY_AND_ASSIGN(Worker);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900230};
231
232
233// Starts the test with the given workers. This function deletes the workers
234// when it's done.
235void RunTest(std::vector<Worker*> workers) {
236 // First we create the workers that are channel servers, or else the other
237 // workers' channel initialization might fail because the pipe isn't created..
238 for (size_t i = 0; i < workers.size(); ++i) {
dmaclach@chromium.orgf146c292011-02-04 05:35:09 +0900239 if (workers[i]->mode() & Channel::MODE_SERVER_FLAG) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900240 workers[i]->Start();
241 workers[i]->WaitForChannelCreation();
242 }
243 }
244
245 // now create the clients
246 for (size_t i = 0; i < workers.size(); ++i) {
dmaclach@chromium.orgf146c292011-02-04 05:35:09 +0900247 if (workers[i]->mode() & Channel::MODE_CLIENT_FLAG)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900248 workers[i]->Start();
249 }
250
251 // wait for all the workers to finish
252 for (size_t i = 0; i < workers.size(); ++i)
253 workers[i]->done_event()->Wait();
254
255 STLDeleteContainerPointers(workers.begin(), workers.end());
256}
257
258} // namespace
259
260class IPCSyncChannelTest : public testing::Test {
261 private:
262 MessageLoop message_loop_;
263};
264
265//-----------------------------------------------------------------------------
266
267namespace {
268
269class SimpleServer : public Worker {
270 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900271 explicit SimpleServer(bool pump_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900272 : Worker(Channel::MODE_SERVER, "simpler_server"),
273 pump_during_send_(pump_during_send) { }
274 void Run() {
275 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
276 Done();
277 }
278
279 bool pump_during_send_;
280};
281
282class SimpleClient : public Worker {
283 public:
284 SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { }
285
286 void OnAnswer(int* answer) {
287 *answer = 42;
288 Done();
289 }
290};
291
292void Simple(bool pump_during_send) {
293 std::vector<Worker*> workers;
294 workers.push_back(new SimpleServer(pump_during_send));
295 workers.push_back(new SimpleClient());
296 RunTest(workers);
297}
298
299} // namespace
300
301// Tests basic synchronous call
302TEST_F(IPCSyncChannelTest, Simple) {
303 Simple(false);
304 Simple(true);
305}
306
307//-----------------------------------------------------------------------------
308
309namespace {
310
311class DelayClient : public Worker {
312 public:
313 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { }
314
315 void OnAnswerDelay(Message* reply_msg) {
316 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
317 Send(reply_msg);
318 Done();
319 }
320};
321
322void DelayReply(bool pump_during_send) {
323 std::vector<Worker*> workers;
324 workers.push_back(new SimpleServer(pump_during_send));
325 workers.push_back(new DelayClient());
326 RunTest(workers);
327}
328
329} // namespace
330
331// Tests that asynchronous replies work
332TEST_F(IPCSyncChannelTest, DelayReply) {
333 DelayReply(false);
334 DelayReply(true);
335}
336
337//-----------------------------------------------------------------------------
338
339namespace {
340
341class NoHangServer : public Worker {
342 public:
dilmah@chromium.org92b4c8e2011-07-27 02:25:25 +0900343 NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900344 : Worker(Channel::MODE_SERVER, "no_hang_server"),
345 got_first_reply_(got_first_reply),
346 pump_during_send_(pump_during_send) { }
347 void Run() {
348 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
349 got_first_reply_->Signal();
350
351 SendAnswerToLife(pump_during_send_, base::kNoTimeout, false);
352 Done();
353 }
354
355 WaitableEvent* got_first_reply_;
356 bool pump_during_send_;
357};
358
359class NoHangClient : public Worker {
360 public:
361 explicit NoHangClient(WaitableEvent* got_first_reply)
362 : Worker(Channel::MODE_CLIENT, "no_hang_client"),
363 got_first_reply_(got_first_reply) { }
364
365 virtual void OnAnswerDelay(Message* reply_msg) {
366 // Use the DELAY_REPLY macro so that we can force the reply to be sent
367 // before this function returns (when the channel will be reset).
368 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
369 Send(reply_msg);
370 got_first_reply_->Wait();
371 CloseChannel();
372 Done();
373 }
374
375 WaitableEvent* got_first_reply_;
376};
377
378void NoHang(bool pump_during_send) {
379 WaitableEvent got_first_reply(false, false);
380 std::vector<Worker*> workers;
381 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send));
382 workers.push_back(new NoHangClient(&got_first_reply));
383 RunTest(workers);
384}
385
386} // namespace
387
388// Tests that caller doesn't hang if receiver dies
389TEST_F(IPCSyncChannelTest, NoHang) {
390 NoHang(false);
391 NoHang(true);
392}
393
394//-----------------------------------------------------------------------------
395
396namespace {
397
398class UnblockServer : public Worker {
399 public:
jam@chromium.orgebd07182009-12-01 11:34:18 +0900400 UnblockServer(bool pump_during_send, bool delete_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900401 : Worker(Channel::MODE_SERVER, "unblock_server"),
jam@chromium.orgebd07182009-12-01 11:34:18 +0900402 pump_during_send_(pump_during_send),
403 delete_during_send_(delete_during_send) { }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900404 void Run() {
jam@chromium.orgebd07182009-12-01 11:34:18 +0900405 if (delete_during_send_) {
406 // Use custom code since race conditions mean the answer may or may not be
407 // available.
408 int answer = 0;
409 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
410 if (pump_during_send_)
411 msg->EnableMessagePumping();
412 Send(msg);
413 } else {
414 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
415 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900416 Done();
417 }
418
jam@chromium.orgebd07182009-12-01 11:34:18 +0900419 void OnDoubleDelay(int in, Message* reply_msg) {
420 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
421 Send(reply_msg);
422 if (delete_during_send_)
423 ResetChannel();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900424 }
425
426 bool pump_during_send_;
jam@chromium.orgebd07182009-12-01 11:34:18 +0900427 bool delete_during_send_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900428};
429
430class UnblockClient : public Worker {
431 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900432 explicit UnblockClient(bool pump_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900433 : Worker(Channel::MODE_CLIENT, "unblock_client"),
434 pump_during_send_(pump_during_send) { }
435
436 void OnAnswer(int* answer) {
437 SendDouble(pump_during_send_, true);
438 *answer = 42;
439 Done();
440 }
441
442 bool pump_during_send_;
443};
444
jam@chromium.orgebd07182009-12-01 11:34:18 +0900445void Unblock(bool server_pump, bool client_pump, bool delete_during_send) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900446 std::vector<Worker*> workers;
jam@chromium.orgebd07182009-12-01 11:34:18 +0900447 workers.push_back(new UnblockServer(server_pump, delete_during_send));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900448 workers.push_back(new UnblockClient(client_pump));
449 RunTest(workers);
450}
451
452} // namespace
453
454// Tests that the caller unblocks to answer a sync message from the receiver.
455TEST_F(IPCSyncChannelTest, Unblock) {
jam@chromium.orgebd07182009-12-01 11:34:18 +0900456 Unblock(false, false, false);
457 Unblock(false, true, false);
458 Unblock(true, false, false);
459 Unblock(true, true, false);
460}
461
462//-----------------------------------------------------------------------------
463
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +0900464// Tests that the the SyncChannel object can be deleted during a Send.
jam@chromium.orgebd07182009-12-01 11:34:18 +0900465TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) {
466 Unblock(false, false, true);
467 Unblock(false, true, true);
468 Unblock(true, false, true);
469 Unblock(true, true, true);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900470}
471
472//-----------------------------------------------------------------------------
473
474namespace {
475
476class RecursiveServer : public Worker {
477 public:
dilmah@chromium.org92b4c8e2011-07-27 02:25:25 +0900478 RecursiveServer(bool expected_send_result, bool pump_first, bool pump_second)
479 : Worker(Channel::MODE_SERVER, "recursive_server"),
480 expected_send_result_(expected_send_result),
481 pump_first_(pump_first), pump_second_(pump_second) {}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900482 void Run() {
483 SendDouble(pump_first_, expected_send_result_);
484 Done();
485 }
486
487 void OnDouble(int in, int* out) {
488 *out = in * 2;
489 SendAnswerToLife(pump_second_, base::kNoTimeout, expected_send_result_);
490 }
491
492 bool expected_send_result_, pump_first_, pump_second_;
493};
494
495class RecursiveClient : public Worker {
496 public:
dilmah@chromium.org92b4c8e2011-07-27 02:25:25 +0900497 RecursiveClient(bool pump_during_send, bool close_channel)
498 : Worker(Channel::MODE_CLIENT, "recursive_client"),
499 pump_during_send_(pump_during_send), close_channel_(close_channel) {}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900500
501 void OnDoubleDelay(int in, Message* reply_msg) {
502 SendDouble(pump_during_send_, !close_channel_);
503 if (close_channel_) {
504 delete reply_msg;
505 } else {
506 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
507 Send(reply_msg);
508 }
509 Done();
510 }
511
512 void OnAnswerDelay(Message* reply_msg) {
513 if (close_channel_) {
514 delete reply_msg;
515 CloseChannel();
516 } else {
517 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
518 Send(reply_msg);
519 }
520 }
521
522 bool pump_during_send_, close_channel_;
523};
524
525void Recursive(
526 bool server_pump_first, bool server_pump_second, bool client_pump) {
527 std::vector<Worker*> workers;
528 workers.push_back(
529 new RecursiveServer(true, server_pump_first, server_pump_second));
530 workers.push_back(new RecursiveClient(client_pump, false));
531 RunTest(workers);
532}
533
534} // namespace
535
536// Tests a server calling Send while another Send is pending.
537TEST_F(IPCSyncChannelTest, Recursive) {
538 Recursive(false, false, false);
539 Recursive(false, false, true);
540 Recursive(false, true, false);
541 Recursive(false, true, true);
542 Recursive(true, false, false);
543 Recursive(true, false, true);
544 Recursive(true, true, false);
545 Recursive(true, true, true);
546}
547
548//-----------------------------------------------------------------------------
549
550namespace {
551
552void RecursiveNoHang(
553 bool server_pump_first, bool server_pump_second, bool client_pump) {
554 std::vector<Worker*> workers;
555 workers.push_back(
556 new RecursiveServer(false, server_pump_first, server_pump_second));
557 workers.push_back(new RecursiveClient(client_pump, true));
558 RunTest(workers);
559}
560
561} // namespace
562
563// Tests that if a caller makes a sync call during an existing sync call and
564// the receiver dies, neither of the Send() calls hang.
565TEST_F(IPCSyncChannelTest, RecursiveNoHang) {
566 RecursiveNoHang(false, false, false);
567 RecursiveNoHang(false, false, true);
568 RecursiveNoHang(false, true, false);
569 RecursiveNoHang(false, true, true);
570 RecursiveNoHang(true, false, false);
571 RecursiveNoHang(true, false, true);
572 RecursiveNoHang(true, true, false);
573 RecursiveNoHang(true, true, true);
574}
575
576//-----------------------------------------------------------------------------
577
578namespace {
579
580class MultipleServer1 : public Worker {
581 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900582 explicit MultipleServer1(bool pump_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900583 : Worker("test_channel1", Channel::MODE_SERVER),
584 pump_during_send_(pump_during_send) { }
585
586 void Run() {
587 SendDouble(pump_during_send_, true);
588 Done();
589 }
590
591 bool pump_during_send_;
592};
593
594class MultipleClient1 : public Worker {
595 public:
596 MultipleClient1(WaitableEvent* client1_msg_received,
597 WaitableEvent* client1_can_reply) :
598 Worker("test_channel1", Channel::MODE_CLIENT),
599 client1_msg_received_(client1_msg_received),
600 client1_can_reply_(client1_can_reply) { }
601
602 void OnDouble(int in, int* out) {
603 client1_msg_received_->Signal();
604 *out = in * 2;
605 client1_can_reply_->Wait();
606 Done();
607 }
608
609 private:
610 WaitableEvent *client1_msg_received_, *client1_can_reply_;
611};
612
613class MultipleServer2 : public Worker {
614 public:
615 MultipleServer2() : Worker("test_channel2", Channel::MODE_SERVER) { }
616
617 void OnAnswer(int* result) {
618 *result = 42;
619 Done();
620 }
621};
622
623class MultipleClient2 : public Worker {
624 public:
625 MultipleClient2(
626 WaitableEvent* client1_msg_received, WaitableEvent* client1_can_reply,
627 bool pump_during_send)
628 : Worker("test_channel2", Channel::MODE_CLIENT),
629 client1_msg_received_(client1_msg_received),
630 client1_can_reply_(client1_can_reply),
631 pump_during_send_(pump_during_send) { }
632
633 void Run() {
634 client1_msg_received_->Wait();
635 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true);
636 client1_can_reply_->Signal();
637 Done();
638 }
639
640 private:
641 WaitableEvent *client1_msg_received_, *client1_can_reply_;
642 bool pump_during_send_;
643};
644
645void Multiple(bool server_pump, bool client_pump) {
646 std::vector<Worker*> workers;
647
648 // A shared worker thread so that server1 and server2 run on one thread.
649 base::Thread worker_thread("Multiple");
650 ASSERT_TRUE(worker_thread.Start());
651
652 // Server1 sends a sync msg to client1, which blocks the reply until
653 // server2 (which runs on the same worker thread as server1) responds
654 // to a sync msg from client2.
655 WaitableEvent client1_msg_received(false, false);
656 WaitableEvent client1_can_reply(false, false);
657
658 Worker* worker;
659
660 worker = new MultipleServer2();
661 worker->OverrideThread(&worker_thread);
662 workers.push_back(worker);
663
664 worker = new MultipleClient2(
665 &client1_msg_received, &client1_can_reply, client_pump);
666 workers.push_back(worker);
667
668 worker = new MultipleServer1(server_pump);
669 worker->OverrideThread(&worker_thread);
670 workers.push_back(worker);
671
672 worker = new MultipleClient1(
673 &client1_msg_received, &client1_can_reply);
674 workers.push_back(worker);
675
676 RunTest(workers);
677}
678
679} // namespace
680
681// Tests that multiple SyncObjects on the same listener thread can unblock each
682// other.
683TEST_F(IPCSyncChannelTest, Multiple) {
684 Multiple(false, false);
685 Multiple(false, true);
686 Multiple(true, false);
687 Multiple(true, true);
688}
689
690//-----------------------------------------------------------------------------
691
692namespace {
693
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900694// This class provides server side functionality to test the case where
695// multiple sync channels are in use on the same thread on the client and
696// nested calls are issued.
697class QueuedReplyServer : public Worker {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900698 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900699 QueuedReplyServer(base::Thread* listener_thread,
700 const std::string& channel_name,
701 const std::string& reply_text)
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900702 : Worker(channel_name, Channel::MODE_SERVER),
703 reply_text_(reply_text) {
704 Worker::OverrideThread(listener_thread);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900705 }
706
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900707 virtual void OnNestedTestMsg(Message* reply_msg) {
pkasting@chromium.orgfcdd54b2010-10-20 08:50:00 +0900708 VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_;
709 SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_);
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900710 Send(reply_msg);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900711 Done();
712 }
713
714 private:
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900715 std::string reply_text_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900716};
717
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900718// The QueuedReplyClient class provides functionality to test the case where
719// multiple sync channels are in use on the same thread and they make nested
720// sync calls, i.e. while the first channel waits for a response it makes a
721// sync call on another channel.
722// The callstack should unwind correctly, i.e. the outermost call should
723// complete first, and so on.
724class QueuedReplyClient : public Worker {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900725 public:
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900726 QueuedReplyClient(base::Thread* listener_thread,
727 const std::string& channel_name,
728 const std::string& expected_text,
729 bool pump_during_send)
730 : Worker(channel_name, Channel::MODE_CLIENT),
thomasvl@google.com9a242072010-07-23 23:18:59 +0900731 pump_during_send_(pump_during_send),
732 expected_text_(expected_text) {
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900733 Worker::OverrideThread(listener_thread);
734 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900735
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900736 virtual void Run() {
737 std::string response;
738 SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response);
739 if (pump_during_send_)
740 msg->EnableMessagePumping();
741 bool result = Send(msg);
742 DCHECK(result);
jam@chromium.orgebd07182009-12-01 11:34:18 +0900743 DCHECK_EQ(response, expected_text_);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900744
pkasting@chromium.orgfcdd54b2010-10-20 08:50:00 +0900745 VLOG(1) << __FUNCTION__ << " Received reply: " << response;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900746 Done();
747 }
748
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900749 private:
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900750 bool pump_during_send_;
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900751 std::string expected_text_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900752};
753
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900754void QueuedReply(bool client_pump) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900755 std::vector<Worker*> workers;
756
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900757 // A shared worker thread for servers
758 base::Thread server_worker_thread("QueuedReply_ServerListener");
759 ASSERT_TRUE(server_worker_thread.Start());
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900760
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900761 base::Thread client_worker_thread("QueuedReply_ClientListener");
762 ASSERT_TRUE(client_worker_thread.Start());
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900763
764 Worker* worker;
765
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900766 worker = new QueuedReplyServer(&server_worker_thread,
767 "QueuedReply_Server1",
768 "Got first message");
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900769 workers.push_back(worker);
770
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900771 worker = new QueuedReplyServer(&server_worker_thread,
772 "QueuedReply_Server2",
773 "Got second message");
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900774 workers.push_back(worker);
775
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900776 worker = new QueuedReplyClient(&client_worker_thread,
777 "QueuedReply_Server1",
778 "Got first message",
779 client_pump);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900780 workers.push_back(worker);
781
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900782 worker = new QueuedReplyClient(&client_worker_thread,
783 "QueuedReply_Server2",
784 "Got second message",
785 client_pump);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900786 workers.push_back(worker);
787
788 RunTest(workers);
789}
790
791} // namespace
792
793// While a blocking send is in progress, the listener thread might answer other
794// synchronous messages. This tests that if during the response to another
795// message the reply to the original messages comes, it is queued up correctly
796// and the original Send is unblocked later.
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900797// We also test that the send call stacks unwind correctly when the channel
798// pumps messages while waiting for a response.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900799TEST_F(IPCSyncChannelTest, QueuedReply) {
ananta@chromium.org31b338f2009-10-15 01:22:02 +0900800 QueuedReply(false);
801 QueuedReply(true);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900802}
803
804//-----------------------------------------------------------------------------
805
806namespace {
807
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900808class ChattyClient : public Worker {
809 public:
810 ChattyClient() :
811 Worker(Channel::MODE_CLIENT, "chatty_client") { }
812
813 void OnAnswer(int* answer) {
814 // The PostMessage limit is 10k. Send 20% more than that.
815 const int kMessageLimit = 10000;
816 const int kMessagesToSend = kMessageLimit * 120 / 100;
817 for (int i = 0; i < kMessagesToSend; ++i) {
818 if (!SendDouble(false, true))
819 break;
820 }
821 *answer = 42;
822 Done();
823 }
824};
825
826void ChattyServer(bool pump_during_send) {
827 std::vector<Worker*> workers;
jam@chromium.orgebd07182009-12-01 11:34:18 +0900828 workers.push_back(new UnblockServer(pump_during_send, false));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900829 workers.push_back(new ChattyClient());
830 RunTest(workers);
831}
832
833} // namespace
834
835// Tests http://b/1093251 - that sending lots of sync messages while
836// the receiver is waiting for a sync reply does not overflow the PostMessage
837// queue.
838TEST_F(IPCSyncChannelTest, ChattyServer) {
839 ChattyServer(false);
840 ChattyServer(true);
841}
842
843//------------------------------------------------------------------------------
844
845namespace {
846
847class TimeoutServer : public Worker {
848 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900849 TimeoutServer(int timeout_ms,
850 std::vector<bool> timeout_seq,
851 bool pump_during_send)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900852 : Worker(Channel::MODE_SERVER, "timeout_server"),
853 timeout_ms_(timeout_ms),
854 timeout_seq_(timeout_seq),
855 pump_during_send_(pump_during_send) {
856 }
857
858 void Run() {
859 for (std::vector<bool>::const_iterator iter = timeout_seq_.begin();
860 iter != timeout_seq_.end(); ++iter) {
861 SendAnswerToLife(pump_during_send_, timeout_ms_, !*iter);
862 }
863 Done();
864 }
865
866 private:
867 int timeout_ms_;
868 std::vector<bool> timeout_seq_;
869 bool pump_during_send_;
870};
871
872class UnresponsiveClient : public Worker {
873 public:
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900874 explicit UnresponsiveClient(std::vector<bool> timeout_seq)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900875 : Worker(Channel::MODE_CLIENT, "unresponsive_client"),
876 timeout_seq_(timeout_seq) {
thestig@chromium.org60bd7c02010-07-23 14:23:13 +0900877 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900878
879 void OnAnswerDelay(Message* reply_msg) {
880 DCHECK(!timeout_seq_.empty());
881 if (!timeout_seq_[0]) {
882 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
883 Send(reply_msg);
884 } else {
885 // Don't reply.
886 delete reply_msg;
887 }
888 timeout_seq_.erase(timeout_seq_.begin());
889 if (timeout_seq_.empty())
890 Done();
891 }
892
893 private:
894 // Whether we should time-out or respond to the various messages we receive.
895 std::vector<bool> timeout_seq_;
896};
897
898void SendWithTimeoutOK(bool pump_during_send) {
899 std::vector<Worker*> workers;
900 std::vector<bool> timeout_seq;
901 timeout_seq.push_back(false);
902 timeout_seq.push_back(false);
903 timeout_seq.push_back(false);
904 workers.push_back(new TimeoutServer(5000, timeout_seq, pump_during_send));
905 workers.push_back(new SimpleClient());
906 RunTest(workers);
907}
908
909void SendWithTimeoutTimeout(bool pump_during_send) {
910 std::vector<Worker*> workers;
911 std::vector<bool> timeout_seq;
912 timeout_seq.push_back(true);
913 timeout_seq.push_back(false);
914 timeout_seq.push_back(false);
915 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
916 workers.push_back(new UnresponsiveClient(timeout_seq));
917 RunTest(workers);
918}
919
920void SendWithTimeoutMixedOKAndTimeout(bool pump_during_send) {
921 std::vector<Worker*> workers;
922 std::vector<bool> timeout_seq;
923 timeout_seq.push_back(true);
924 timeout_seq.push_back(false);
925 timeout_seq.push_back(false);
926 timeout_seq.push_back(true);
927 timeout_seq.push_back(false);
928 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send));
929 workers.push_back(new UnresponsiveClient(timeout_seq));
930 RunTest(workers);
931}
932
933} // namespace
934
935// Tests that SendWithTimeout does not time-out if the response comes back fast
936// enough.
937TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) {
938 SendWithTimeoutOK(false);
939 SendWithTimeoutOK(true);
940}
941
942// Tests that SendWithTimeout does time-out.
943TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) {
944 SendWithTimeoutTimeout(false);
945 SendWithTimeoutTimeout(true);
946}
947
948// Sends some message that time-out and some that succeed.
phajdan.jr@chromium.orga8e3c092011-01-19 17:11:56 +0900949// Crashes flakily, http://crbug.com/70075.
950TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900951 SendWithTimeoutMixedOKAndTimeout(false);
952 SendWithTimeoutMixedOKAndTimeout(true);
953}
954
955//------------------------------------------------------------------------------
956
957namespace {
958
959class NestedTask : public Task {
960 public:
dilmah@chromium.org92b4c8e2011-07-27 02:25:25 +0900961 explicit NestedTask(Worker* server) : server_(server) {}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900962 void Run() {
963 // Sleep a bit so that we wake up after the reply has been received.
brettw@google.com7c5cc672011-01-01 11:17:08 +0900964 base::PlatformThread::Sleep(250);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900965 server_->SendAnswerToLife(true, base::kNoTimeout, true);
966 }
967
968 Worker* server_;
969};
970
971static bool timeout_occured = false;
972
973class TimeoutTask : public Task {
974 public:
975 void Run() {
976 timeout_occured = true;
977 }
978};
979
980class DoneEventRaceServer : public Worker {
981 public:
982 DoneEventRaceServer()
983 : Worker(Channel::MODE_SERVER, "done_event_race_server") { }
984
985 void Run() {
986 MessageLoop::current()->PostTask(FROM_HERE, new NestedTask(this));
987 MessageLoop::current()->PostDelayedTask(FROM_HERE, new TimeoutTask(), 9000);
988 // Even though we have a timeout on the Send, it will succeed since for this
989 // bug, the reply message comes back and is deserialized, however the done
990 // event wasn't set. So we indirectly use the timeout task to notice if a
991 // timeout occurred.
992 SendAnswerToLife(true, 10000, true);
993 DCHECK(!timeout_occured);
994 Done();
995 }
996};
997
998} // namespace
999
1000// Tests http://b/1474092 - that if after the done_event is set but before
1001// OnObjectSignaled is called another message is sent out, then after its
1002// reply comes back OnObjectSignaled will be called for the first message.
1003TEST_F(IPCSyncChannelTest, DoneEventRace) {
1004 std::vector<Worker*> workers;
1005 workers.push_back(new DoneEventRaceServer());
1006 workers.push_back(new SimpleClient());
1007 RunTest(workers);
1008}
jabdelmalek@google.comeb921652010-04-07 05:33:36 +09001009
1010//-----------------------------------------------------------------------------
1011
1012namespace {
1013
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +09001014class TestSyncMessageFilter : public SyncMessageFilter {
jabdelmalek@google.comeb921652010-04-07 05:33:36 +09001015 public:
1016 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker)
1017 : SyncMessageFilter(shutdown_event),
1018 worker_(worker),
1019 thread_("helper_thread") {
1020 base::Thread::Options options;
1021 options.message_loop_type = MessageLoop::TYPE_DEFAULT;
1022 thread_.StartWithOptions(options);
1023 }
1024
1025 virtual void OnFilterAdded(Channel* channel) {
1026 SyncMessageFilter::OnFilterAdded(channel);
1027 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
1028 this, &TestSyncMessageFilter::SendMessageOnHelperThread));
1029 }
1030
1031 void SendMessageOnHelperThread() {
1032 int answer = 0;
1033 bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer));
1034 DCHECK(result);
1035 DCHECK_EQ(answer, 42);
1036
1037 worker_->Done();
1038 }
1039
1040 Worker* worker_;
1041 base::Thread thread_;
1042};
1043
1044class SyncMessageFilterServer : public Worker {
1045 public:
1046 SyncMessageFilterServer()
1047 : Worker(Channel::MODE_SERVER, "sync_message_filter_server") {
1048 filter_ = new TestSyncMessageFilter(shutdown_event(), this);
1049 }
1050
1051 void Run() {
1052 channel()->AddFilter(filter_.get());
1053 }
1054
1055 scoped_refptr<TestSyncMessageFilter> filter_;
1056};
1057
ananta@chromium.org999f2972010-09-03 06:45:50 +09001058// This class provides functionality to test the case that a Send on the sync
1059// channel does not crash after the channel has been closed.
1060class ServerSendAfterClose : public Worker {
1061 public:
1062 ServerSendAfterClose()
1063 : Worker(Channel::MODE_SERVER, "simpler_server"),
1064 send_result_(true) {
1065 }
1066
1067 bool SendDummy() {
1068 ListenerThread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
1069 this, &ServerSendAfterClose::Send, new SyncChannelTestMsg_NoArgs));
1070 return true;
1071 }
1072
1073 bool send_result() const {
1074 return send_result_;
1075 }
1076
1077 private:
1078 virtual void Run() {
1079 CloseChannel();
1080 Done();
1081 }
1082
1083 bool Send(Message* msg) {
1084 send_result_ = Worker::Send(msg);
1085 Done();
1086 return send_result_;
1087 }
1088
1089 bool send_result_;
1090};
1091
jabdelmalek@google.comeb921652010-04-07 05:33:36 +09001092} // namespace
1093
1094// Tests basic synchronous call
1095TEST_F(IPCSyncChannelTest, SyncMessageFilter) {
1096 std::vector<Worker*> workers;
1097 workers.push_back(new SyncMessageFilterServer());
1098 workers.push_back(new SimpleClient());
1099 RunTest(workers);
1100}
ananta@chromium.org999f2972010-09-03 06:45:50 +09001101
1102// Test the case when the channel is closed and a Send is attempted after that.
1103TEST_F(IPCSyncChannelTest, SendAfterClose) {
1104 ServerSendAfterClose server;
1105 server.Start();
1106
1107 server.done_event()->Wait();
1108 server.done_event()->Reset();
1109
1110 server.SendDummy();
1111 server.done_event()->Wait();
1112
1113 EXPECT_FALSE(server.send_result());
1114}
1115
piman@google.com0cbefaa2011-04-08 12:38:21 +09001116//-----------------------------------------------------------------------------
ananta@chromium.org999f2972010-09-03 06:45:50 +09001117
piman@google.com0cbefaa2011-04-08 12:38:21 +09001118namespace {
1119
1120class RestrictedDispatchServer : public Worker {
1121 public:
1122 RestrictedDispatchServer(WaitableEvent* sent_ping_event)
1123 : Worker("restricted_channel", Channel::MODE_SERVER),
1124 sent_ping_event_(sent_ping_event) { }
1125
1126 void OnDoPing(int ping) {
1127 // Send an asynchronous message that unblocks the caller.
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +09001128 Message* msg = new SyncChannelTestMsg_Ping(ping);
piman@google.com0cbefaa2011-04-08 12:38:21 +09001129 msg->set_unblock(true);
1130 Send(msg);
1131 // Signal the event after the message has been sent on the channel, on the
1132 // IPC thread.
1133 ipc_thread().message_loop()->PostTask(FROM_HERE,
1134 NewRunnableMethod(this, &RestrictedDispatchServer::OnPingSent));
1135 }
1136
1137 base::Thread* ListenerThread() { return Worker::ListenerThread(); }
1138
1139 private:
1140 bool OnMessageReceived(const Message& message) {
1141 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchServer, message)
1142 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1143 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1144 IPC_END_MESSAGE_MAP()
1145 return true;
1146 }
1147
1148 void OnPingSent() {
1149 sent_ping_event_->Signal();
1150 }
1151
1152 void OnNoArgs() { }
1153 WaitableEvent* sent_ping_event_;
1154};
1155
1156class NonRestrictedDispatchServer : public Worker {
1157 public:
1158 NonRestrictedDispatchServer()
1159 : Worker("non_restricted_channel", Channel::MODE_SERVER) {}
1160
1161 private:
1162 bool OnMessageReceived(const Message& message) {
1163 IPC_BEGIN_MESSAGE_MAP(NonRestrictedDispatchServer, message)
1164 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
1165 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
1166 IPC_END_MESSAGE_MAP()
1167 return true;
1168 }
1169
1170 void OnNoArgs() { }
1171};
1172
1173class RestrictedDispatchClient : public Worker {
1174 public:
1175 RestrictedDispatchClient(WaitableEvent* sent_ping_event,
1176 RestrictedDispatchServer* server,
1177 int* success)
1178 : Worker("restricted_channel", Channel::MODE_CLIENT),
1179 ping_(0),
1180 server_(server),
1181 success_(success),
1182 sent_ping_event_(sent_ping_event) {}
1183
1184 void Run() {
1185 // Incoming messages from our channel should only be dispatched when we
1186 // send a message on that same channel.
1187 channel()->SetRestrictDispatchToSameChannel(true);
1188
1189 server_->ListenerThread()->message_loop()->PostTask(FROM_HERE,
1190 NewRunnableMethod(server_, &RestrictedDispatchServer::OnDoPing, 1));
1191 sent_ping_event_->Wait();
1192 Send(new SyncChannelTestMsg_NoArgs);
1193 if (ping_ == 1)
1194 ++*success_;
1195 else
1196 LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
1197
1198 scoped_ptr<SyncChannel> non_restricted_channel(new SyncChannel(
1199 "non_restricted_channel", Channel::MODE_CLIENT, this,
jam@chromium.org06d18442011-05-03 03:00:49 +09001200 ipc_thread().message_loop_proxy(), true, shutdown_event()));
piman@google.com0cbefaa2011-04-08 12:38:21 +09001201
1202 server_->ListenerThread()->message_loop()->PostTask(FROM_HERE,
1203 NewRunnableMethod(server_, &RestrictedDispatchServer::OnDoPing, 2));
1204 sent_ping_event_->Wait();
1205 // Check that the incoming message is *not* dispatched when sending on the
1206 // non restricted channel.
1207 // TODO(piman): there is a possibility of a false positive race condition
1208 // here, if the message that was posted on the server-side end of the pipe
1209 // is not visible yet on the client side, but I don't know how to solve this
1210 // without hooking into the internals of SyncChannel. I haven't seen it in
1211 // practice (i.e. not setting SetRestrictDispatchToSameChannel does cause
1212 // the following to fail).
1213 non_restricted_channel->Send(new SyncChannelTestMsg_NoArgs);
1214 if (ping_ == 1)
1215 ++*success_;
1216 else
1217 LOG(ERROR) << "Send dispatched message from restricted channel";
1218
1219 Send(new SyncChannelTestMsg_NoArgs);
1220 if (ping_ == 2)
1221 ++*success_;
1222 else
1223 LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
1224
1225 non_restricted_channel->Send(new SyncChannelTestMsg_Done);
1226 non_restricted_channel.reset();
1227 Send(new SyncChannelTestMsg_Done);
1228 Done();
1229 }
1230
1231 private:
1232 bool OnMessageReceived(const Message& message) {
1233 IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchClient, message)
1234 IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Ping, OnPing)
1235 IPC_END_MESSAGE_MAP()
1236 return true;
1237 }
1238
1239 void OnPing(int ping) {
1240 ping_ = ping;
1241 }
1242
1243 int ping_;
1244 RestrictedDispatchServer* server_;
1245 int* success_;
1246 WaitableEvent* sent_ping_event_;
1247};
1248
1249} // namespace
1250
1251TEST_F(IPCSyncChannelTest, RestrictedDispatch) {
1252 WaitableEvent sent_ping_event(false, false);
1253
1254 RestrictedDispatchServer* server =
1255 new RestrictedDispatchServer(&sent_ping_event);
1256 int success = 0;
1257 std::vector<Worker*> workers;
1258 workers.push_back(new NonRestrictedDispatchServer);
1259 workers.push_back(server);
1260 workers.push_back(
1261 new RestrictedDispatchClient(&sent_ping_event, server, &success));
1262 RunTest(workers);
1263 EXPECT_EQ(3, success);
1264}
kushi.p@gmail.com5948bf42011-05-14 07:42:41 +09001265
1266} // namespace IPC