1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/embedder/embedder.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "mojo/common/test/multiprocess_test_helper.h"
16 #include "mojo/embedder/platform_channel_pair.h"
17 #include "mojo/embedder/test_embedder.h"
18 #include "mojo/public/c/system/core.h"
19 #include "mojo/system/test_utils.h"
20 #include "testing/gtest/include/gtest/gtest.h"
26 class ScopedTestChannel
{
28 // Creates a channel that lives on a given I/O thread (determined by the given
29 // |TaskRunner|) attached to the given |platform_handle|. After construction,
30 // |bootstrap_message_pipe()| gives the Mojo handle for the bootstrap message
31 // pipe on this channel; it is up to the caller to close this handle.
32 // Note: The I/O thread must outlive this object (and its message loop must
33 // continue pumping messages while this object is alive).
34 ScopedTestChannel(scoped_refptr
<base::TaskRunner
> io_thread_task_runner
,
35 ScopedPlatformHandle platform_handle
)
36 : io_thread_task_runner_(io_thread_task_runner
),
37 bootstrap_message_pipe_(MOJO_HANDLE_INVALID
),
38 did_create_channel_event_(true, false),
40 bootstrap_message_pipe_
= CreateChannel(
41 platform_handle
.Pass(), io_thread_task_runner_
,
42 base::Bind(&ScopedTestChannel::DidCreateChannel
,
43 base::Unretained(this)), NULL
).release().value();
44 CHECK_NE(bootstrap_message_pipe_
, MOJO_HANDLE_INVALID
);
47 // Destructor: Shuts down the channel. (As noted above, for this to happen,
48 // the I/O thread must be alive and pumping messages.)
49 ~ScopedTestChannel() {
50 system::test::PostTaskAndWait(
51 io_thread_task_runner_
,
53 base::Bind(&ScopedTestChannel::DestroyChannel
, base::Unretained(this)));
56 // Waits for channel creation to be completed.
57 void WaitForChannelCreationCompletion() {
58 did_create_channel_event_
.Wait();
61 MojoHandle
bootstrap_message_pipe() const { return bootstrap_message_pipe_
; }
63 // Call only after |WaitForChannelCreationCompletion()|. Use only to check
64 // that it's not null.
65 const ChannelInfo
* channel_info() const { return channel_info_
; }
68 void DidCreateChannel(ChannelInfo
* channel_info
) {
70 CHECK(!channel_info_
);
71 channel_info_
= channel_info
;
72 did_create_channel_event_
.Signal();
75 void DestroyChannel() {
77 DestroyChannelOnIOThread(channel_info_
);
81 scoped_refptr
<base::TaskRunner
> io_thread_task_runner_
;
83 // Valid from creation until whenever it gets closed (by the "owner" of this
85 // Note: We don't want use the C++ wrappers here, since we want to test the
86 // API at the lowest level.
87 MojoHandle bootstrap_message_pipe_
;
89 // Set after channel creation has been completed (i.e., the callback to
90 // |CreateChannel()| has been called).
91 base::WaitableEvent did_create_channel_event_
;
93 // Valid after channel creation completion until destruction.
94 ChannelInfo
* channel_info_
;
96 DISALLOW_COPY_AND_ASSIGN(ScopedTestChannel
);
99 class EmbedderTest
: public testing::Test
{
101 EmbedderTest() : test_io_thread_(system::test::TestIOThread::kAutoStart
) {}
102 virtual ~EmbedderTest() {}
105 system::test::TestIOThread
* test_io_thread() { return &test_io_thread_
; }
108 system::test::TestIOThread test_io_thread_
;
110 DISALLOW_COPY_AND_ASSIGN(EmbedderTest
);
113 TEST_F(EmbedderTest
, ChannelsBasic
) {
117 PlatformChannelPair channel_pair
;
118 ScopedTestChannel
server_channel(test_io_thread()->task_runner(),
119 channel_pair
.PassServerHandle());
120 MojoHandle server_mp
= server_channel
.bootstrap_message_pipe();
121 EXPECT_NE(server_mp
, MOJO_HANDLE_INVALID
);
122 ScopedTestChannel
client_channel(test_io_thread()->task_runner(),
123 channel_pair
.PassClientHandle());
124 MojoHandle client_mp
= client_channel
.bootstrap_message_pipe();
125 EXPECT_NE(client_mp
, MOJO_HANDLE_INVALID
);
127 // We can write to a message pipe handle immediately.
128 const char kHello
[] = "hello";
129 EXPECT_EQ(MOJO_RESULT_OK
,
130 MojoWriteMessage(server_mp
, kHello
,
131 static_cast<uint32_t>(sizeof(kHello
)), NULL
, 0,
132 MOJO_WRITE_MESSAGE_FLAG_NONE
));
134 // Now wait for the other side to become readable.
135 EXPECT_EQ(MOJO_RESULT_OK
,
136 MojoWait(client_mp
, MOJO_WAIT_FLAG_READABLE
,
137 MOJO_DEADLINE_INDEFINITE
));
139 char buffer
[1000] = {};
140 uint32_t num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
141 EXPECT_EQ(MOJO_RESULT_OK
,
142 MojoReadMessage(client_mp
, buffer
, &num_bytes
, NULL
, NULL
,
143 MOJO_READ_MESSAGE_FLAG_NONE
));
144 EXPECT_EQ(sizeof(kHello
), num_bytes
);
145 EXPECT_STREQ(kHello
, buffer
);
147 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(server_mp
));
148 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(client_mp
));
150 // By this point, these waits should basically be no-ops (since we've waited
151 // for the client message pipe to become readable, which implies that both
152 // the server and client channels were completely created).
153 server_channel
.WaitForChannelCreationCompletion();
154 client_channel
.WaitForChannelCreationCompletion();
155 EXPECT_TRUE(server_channel
.channel_info() != NULL
);
156 EXPECT_TRUE(client_channel
.channel_info() != NULL
);
159 EXPECT_TRUE(test::Shutdown());
162 TEST_F(EmbedderTest
, ChannelsHandlePassing
) {
166 PlatformChannelPair channel_pair
;
167 ScopedTestChannel
server_channel(test_io_thread()->task_runner(),
168 channel_pair
.PassServerHandle());
169 MojoHandle server_mp
= server_channel
.bootstrap_message_pipe();
170 EXPECT_NE(server_mp
, MOJO_HANDLE_INVALID
);
171 ScopedTestChannel
client_channel(test_io_thread()->task_runner(),
172 channel_pair
.PassClientHandle());
173 MojoHandle client_mp
= client_channel
.bootstrap_message_pipe();
174 EXPECT_NE(client_mp
, MOJO_HANDLE_INVALID
);
177 EXPECT_EQ(MOJO_RESULT_OK
, MojoCreateMessagePipe(&h0
, &h1
));
179 // Write a message to |h0| (attaching nothing).
180 const char kHello
[] = "hello";
181 EXPECT_EQ(MOJO_RESULT_OK
,
182 MojoWriteMessage(h0
, kHello
,
183 static_cast<uint32_t>(sizeof(kHello
)), NULL
, 0,
184 MOJO_WRITE_MESSAGE_FLAG_NONE
));
186 // Write one message to |server_mp|, attaching |h1|.
187 const char kWorld
[] = "world!!!";
188 EXPECT_EQ(MOJO_RESULT_OK
,
189 MojoWriteMessage(server_mp
, kWorld
,
190 static_cast<uint32_t>(sizeof(kWorld
)), &h1
, 1,
191 MOJO_WRITE_MESSAGE_FLAG_NONE
));
192 h1
= MOJO_HANDLE_INVALID
;
194 // Write another message to |h0|.
195 const char kFoo
[] = "foo";
196 EXPECT_EQ(MOJO_RESULT_OK
,
197 MojoWriteMessage(h0
, kFoo
,
198 static_cast<uint32_t>(sizeof(kFoo
)), NULL
, 0,
199 MOJO_WRITE_MESSAGE_FLAG_NONE
));
201 // Wait for |client_mp| to become readable.
202 EXPECT_EQ(MOJO_RESULT_OK
,
203 MojoWait(client_mp
, MOJO_WAIT_FLAG_READABLE
,
204 MOJO_DEADLINE_INDEFINITE
));
206 // Read a message from |client_mp|.
207 char buffer
[1000] = {};
208 uint32_t num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
209 MojoHandle handles
[10] = {};
210 uint32_t num_handles
= arraysize(handles
);
211 EXPECT_EQ(MOJO_RESULT_OK
,
212 MojoReadMessage(client_mp
, buffer
, &num_bytes
, handles
,
213 &num_handles
, MOJO_READ_MESSAGE_FLAG_NONE
));
214 EXPECT_EQ(sizeof(kWorld
), num_bytes
);
215 EXPECT_STREQ(kWorld
, buffer
);
216 EXPECT_EQ(1u, num_handles
);
217 EXPECT_NE(handles
[0], MOJO_HANDLE_INVALID
);
220 // Wait for |h1| to become readable.
221 EXPECT_EQ(MOJO_RESULT_OK
,
222 MojoWait(h1
, MOJO_WAIT_FLAG_READABLE
, MOJO_DEADLINE_INDEFINITE
));
224 // Read a message from |h1|.
225 memset(buffer
, 0, sizeof(buffer
));
226 num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
227 memset(handles
, 0, sizeof(handles
));
228 num_handles
= arraysize(handles
);
229 EXPECT_EQ(MOJO_RESULT_OK
,
230 MojoReadMessage(h1
, buffer
, &num_bytes
, handles
, &num_handles
,
231 MOJO_READ_MESSAGE_FLAG_NONE
));
232 EXPECT_EQ(sizeof(kHello
), num_bytes
);
233 EXPECT_STREQ(kHello
, buffer
);
234 EXPECT_EQ(0u, num_handles
);
236 // Wait for |h1| to become readable (again).
237 EXPECT_EQ(MOJO_RESULT_OK
,
238 MojoWait(h1
, MOJO_WAIT_FLAG_READABLE
, MOJO_DEADLINE_INDEFINITE
));
240 // Read the second message from |h1|.
241 memset(buffer
, 0, sizeof(buffer
));
242 num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
243 EXPECT_EQ(MOJO_RESULT_OK
,
244 MojoReadMessage(h1
, buffer
, &num_bytes
, NULL
, NULL
,
245 MOJO_READ_MESSAGE_FLAG_NONE
));
246 EXPECT_EQ(sizeof(kFoo
), num_bytes
);
247 EXPECT_STREQ(kFoo
, buffer
);
249 // Write a message to |h1|.
250 const char kBarBaz
[] = "barbaz";
251 EXPECT_EQ(MOJO_RESULT_OK
,
252 MojoWriteMessage(h1
, kBarBaz
,
253 static_cast<uint32_t>(sizeof(kBarBaz
)), NULL
, 0,
254 MOJO_WRITE_MESSAGE_FLAG_NONE
));
256 // Wait for |h0| to become readable.
257 EXPECT_EQ(MOJO_RESULT_OK
,
258 MojoWait(h0
, MOJO_WAIT_FLAG_READABLE
, MOJO_DEADLINE_INDEFINITE
));
260 // Read a message from |h0|.
261 memset(buffer
, 0, sizeof(buffer
));
262 num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
263 EXPECT_EQ(MOJO_RESULT_OK
,
264 MojoReadMessage(h0
, buffer
, &num_bytes
, NULL
, NULL
,
265 MOJO_READ_MESSAGE_FLAG_NONE
));
266 EXPECT_EQ(sizeof(kBarBaz
), num_bytes
);
267 EXPECT_STREQ(kBarBaz
, buffer
);
269 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(server_mp
));
270 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(client_mp
));
271 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(h0
));
272 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(h1
));
274 server_channel
.WaitForChannelCreationCompletion();
275 client_channel
.WaitForChannelCreationCompletion();
276 EXPECT_TRUE(server_channel
.channel_info() != NULL
);
277 EXPECT_TRUE(client_channel
.channel_info() != NULL
);
280 EXPECT_TRUE(test::Shutdown());
283 // The sequence of messages sent is:
284 // server_mp client_mp mp0 mp1 mp2 mp3
297 TEST_F(EmbedderTest
, MultiprocessChannels
) {
299 mojo::test::MultiprocessTestHelper multiprocess_test_helper
;
300 multiprocess_test_helper
.StartChild("MultiprocessChannelsClient");
303 ScopedTestChannel
server_channel(
304 test_io_thread()->task_runner(),
305 multiprocess_test_helper
.server_platform_handle
.Pass());
306 MojoHandle server_mp
= server_channel
.bootstrap_message_pipe();
307 EXPECT_NE(server_mp
, MOJO_HANDLE_INVALID
);
308 server_channel
.WaitForChannelCreationCompletion();
309 EXPECT_TRUE(server_channel
.channel_info() != NULL
);
311 // 1. Write a message to |server_mp| (attaching nothing).
312 const char kHello
[] = "hello";
313 EXPECT_EQ(MOJO_RESULT_OK
,
314 MojoWriteMessage(server_mp
, kHello
,
315 static_cast<uint32_t>(sizeof(kHello
)), NULL
, 0,
316 MOJO_WRITE_MESSAGE_FLAG_NONE
));
318 // TODO(vtl): If the scope were ended immediately here (maybe after closing
319 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|.
321 // 2. Read a message from |server_mp|.
322 EXPECT_EQ(MOJO_RESULT_OK
,
323 MojoWait(server_mp
, MOJO_WAIT_FLAG_READABLE
,
324 MOJO_DEADLINE_INDEFINITE
));
325 char buffer
[1000] = {};
326 uint32_t num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
327 EXPECT_EQ(MOJO_RESULT_OK
,
328 MojoReadMessage(server_mp
, buffer
, &num_bytes
, NULL
, NULL
,
329 MOJO_READ_MESSAGE_FLAG_NONE
));
330 const char kWorld
[] = "world!";
331 EXPECT_EQ(sizeof(kWorld
), num_bytes
);
332 EXPECT_STREQ(kWorld
, buffer
);
334 // Create a new message pipe (endpoints |mp0| and |mp1|).
336 EXPECT_EQ(MOJO_RESULT_OK
, MojoCreateMessagePipe(&mp0
, &mp1
));
338 // 3. Write something to |mp0|.
339 const char kFoo
[] = "FOO";
340 EXPECT_EQ(MOJO_RESULT_OK
,
341 MojoWriteMessage(mp0
, kFoo
,
342 static_cast<uint32_t>(sizeof(kFoo
)), NULL
, 0,
343 MOJO_WRITE_MESSAGE_FLAG_NONE
));
345 // 4. Write a message to |server_mp|, attaching |mp1|.
346 const char kBar
[] = "Bar";
347 EXPECT_EQ(MOJO_RESULT_OK
,
348 MojoWriteMessage(server_mp
, kBar
,
349 static_cast<uint32_t>(sizeof(kBar
)), &mp1
, 1,
350 MOJO_WRITE_MESSAGE_FLAG_NONE
));
351 mp1
= MOJO_HANDLE_INVALID
;
353 // 5. Close |server_mp|.
354 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(server_mp
));
356 // 9. Read a message from |mp0|, which should have |mp2| attached.
357 EXPECT_EQ(MOJO_RESULT_OK
,
358 MojoWait(mp0
, MOJO_WAIT_FLAG_READABLE
, MOJO_DEADLINE_INDEFINITE
));
359 memset(buffer
, 0, sizeof(buffer
));
360 num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
361 MojoHandle mp2
= MOJO_HANDLE_INVALID
;
362 uint32_t num_handles
= 1;
363 EXPECT_EQ(MOJO_RESULT_OK
,
364 MojoReadMessage(mp0
, buffer
, &num_bytes
, &mp2
, &num_handles
,
365 MOJO_READ_MESSAGE_FLAG_NONE
));
366 const char kQuux
[] = "quux";
367 EXPECT_EQ(sizeof(kQuux
), num_bytes
);
368 EXPECT_STREQ(kQuux
, buffer
);
369 EXPECT_EQ(1u, num_handles
);
370 EXPECT_NE(mp2
, MOJO_HANDLE_INVALID
);
372 // 7. Read a message from |mp2|.
373 EXPECT_EQ(MOJO_RESULT_OK
,
374 MojoWait(mp2
, MOJO_WAIT_FLAG_READABLE
, MOJO_DEADLINE_INDEFINITE
));
375 memset(buffer
, 0, sizeof(buffer
));
376 num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
377 EXPECT_EQ(MOJO_RESULT_OK
,
378 MojoReadMessage(mp2
, buffer
, &num_bytes
, NULL
, NULL
,
379 MOJO_READ_MESSAGE_FLAG_NONE
));
380 const char kBaz
[] = "baz";
381 EXPECT_EQ(sizeof(kBaz
), num_bytes
);
382 EXPECT_STREQ(kBaz
, buffer
);
385 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(mp0
));
387 // 12. Wait on |mp2| (which should eventually fail) and then close it.
388 // TODO(vtl): crbug.com/351768
390 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
391 MojoWait(mp2
, MOJO_WAIT_FLAG_READABLE
, MOJO_DEADLINE_INDEFINITE
));
393 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(mp2
));
396 EXPECT_TRUE(multiprocess_test_helper
.WaitForChildTestShutdown());
397 EXPECT_TRUE(test::Shutdown());
400 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient
) {
401 embedder::ScopedPlatformHandle client_platform_handle
=
402 mojo::test::MultiprocessTestHelper::client_platform_handle
.Pass();
403 EXPECT_TRUE(client_platform_handle
.is_valid());
405 system::test::TestIOThread
406 test_io_thread(system::test::TestIOThread::kAutoStart
);
410 ScopedTestChannel
client_channel(test_io_thread
.task_runner(),
411 client_platform_handle
.Pass());
412 MojoHandle client_mp
= client_channel
.bootstrap_message_pipe();
413 EXPECT_NE(client_mp
, MOJO_HANDLE_INVALID
);
414 client_channel
.WaitForChannelCreationCompletion();
415 CHECK(client_channel
.channel_info() != NULL
);
417 // 1. Read the first message from |client_mp|.
418 EXPECT_EQ(MOJO_RESULT_OK
,
419 MojoWait(client_mp
, MOJO_WAIT_FLAG_READABLE
,
420 MOJO_DEADLINE_INDEFINITE
));
421 char buffer
[1000] = {};
422 uint32_t num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
423 EXPECT_EQ(MOJO_RESULT_OK
,
424 MojoReadMessage(client_mp
, buffer
, &num_bytes
, NULL
, NULL
,
425 MOJO_READ_MESSAGE_FLAG_NONE
));
426 const char kHello
[] = "hello";
427 EXPECT_EQ(sizeof(kHello
), num_bytes
);
428 EXPECT_STREQ(kHello
, buffer
);
430 // 2. Write a message to |client_mp| (attaching nothing).
431 const char kWorld
[] = "world!";
432 EXPECT_EQ(MOJO_RESULT_OK
,
433 MojoWriteMessage(client_mp
, kWorld
,
434 static_cast<uint32_t>(sizeof(kWorld
)), NULL
, 0,
435 MOJO_WRITE_MESSAGE_FLAG_NONE
));
437 // 4. Read a message from |client_mp|, which should have |mp1| attached.
438 EXPECT_EQ(MOJO_RESULT_OK
,
439 MojoWait(client_mp
, MOJO_WAIT_FLAG_READABLE
,
440 MOJO_DEADLINE_INDEFINITE
));
441 // TODO(vtl): If the scope were to end here (and |client_mp| closed), we'd
442 // die (again due to |Channel::HandleLocalError()|).
443 memset(buffer
, 0, sizeof(buffer
));
444 num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
445 MojoHandle mp1
= MOJO_HANDLE_INVALID
;
446 uint32_t num_handles
= 1;
447 EXPECT_EQ(MOJO_RESULT_OK
,
448 MojoReadMessage(client_mp
, buffer
, &num_bytes
, &mp1
, &num_handles
,
449 MOJO_READ_MESSAGE_FLAG_NONE
));
450 const char kBar
[] = "Bar";
451 EXPECT_EQ(sizeof(kBar
), num_bytes
);
452 EXPECT_STREQ(kBar
, buffer
);
453 EXPECT_EQ(1u, num_handles
);
454 EXPECT_NE(mp1
, MOJO_HANDLE_INVALID
);
455 // TODO(vtl): If the scope were to end here (and the two handles closed),
456 // we'd die due to |Channel::RunRemoteMessagePipeEndpoint()| not handling
457 // write errors (assuming the parent had closed the pipe).
459 // 6. Close |client_mp|.
460 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(client_mp
));
462 // Create a new message pipe (endpoints |mp2| and |mp3|).
464 EXPECT_EQ(MOJO_RESULT_OK
, MojoCreateMessagePipe(&mp2
, &mp3
));
466 // 7. Write a message to |mp3|.
467 const char kBaz
[] = "baz";
468 EXPECT_EQ(MOJO_RESULT_OK
,
469 MojoWriteMessage(mp3
, kBaz
,
470 static_cast<uint32_t>(sizeof(kBaz
)), NULL
, 0,
471 MOJO_WRITE_MESSAGE_FLAG_NONE
));
474 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(mp3
));
476 // 9. Write a message to |mp1|, attaching |mp2|.
477 const char kQuux
[] = "quux";
478 EXPECT_EQ(MOJO_RESULT_OK
,
479 MojoWriteMessage(mp1
, kQuux
,
480 static_cast<uint32_t>(sizeof(kQuux
)), &mp2
, 1,
481 MOJO_WRITE_MESSAGE_FLAG_NONE
));
482 mp2
= MOJO_HANDLE_INVALID
;
484 // 3. Read a message from |mp1|.
485 EXPECT_EQ(MOJO_RESULT_OK
,
486 MojoWait(mp1
, MOJO_WAIT_FLAG_READABLE
, MOJO_DEADLINE_INDEFINITE
));
487 memset(buffer
, 0, sizeof(buffer
));
488 num_bytes
= static_cast<uint32_t>(sizeof(buffer
));
489 EXPECT_EQ(MOJO_RESULT_OK
,
490 MojoReadMessage(mp1
, buffer
, &num_bytes
, NULL
, NULL
,
491 MOJO_READ_MESSAGE_FLAG_NONE
));
492 const char kFoo
[] = "FOO";
493 EXPECT_EQ(sizeof(kFoo
), num_bytes
);
494 EXPECT_STREQ(kFoo
, buffer
);
496 // 11. Wait on |mp1| (which should eventually fail) and then close it.
497 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION
,
498 MojoWait(mp1
, MOJO_WAIT_FLAG_READABLE
, MOJO_DEADLINE_INDEFINITE
));
499 EXPECT_EQ(MOJO_RESULT_OK
, MojoClose(mp1
));
502 EXPECT_TRUE(test::Shutdown());
505 // TODO(vtl): Test immediate write & close.
506 // TODO(vtl): Test broken-connection cases.
509 } // namespace embedder