1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/edk/embedder/embedder.h"
#include <string.h>
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_io_thread.h"
#include "base/test/test_timeouts.h"
#include "mojo/edk/embedder/platform_channel_pair.h"
#include "mojo/edk/embedder/test_embedder.h"
#include "mojo/edk/system/test_utils.h"
#include "mojo/edk/test/multiprocess_test_helper.h"
#include "mojo/public/c/system/core.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace embedder {
namespace {
const MojoHandleSignals kSignalReadadableWritable =
MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE;
const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE |
MOJO_HANDLE_SIGNAL_WRITABLE |
MOJO_HANDLE_SIGNAL_PEER_CLOSED;
class ScopedTestChannel {
public:
// Creates a channel that lives on a given I/O thread (determined by the given
// |TaskRunner|) attached to the given |platform_handle|. After construction,
// |bootstrap_message_pipe()| gives the Mojo handle for the bootstrap message
// pipe on this channel; it is up to the caller to close this handle.
// Note: The I/O thread must outlive this object (and its message loop must
// continue pumping messages while this object is alive).
ScopedTestChannel(scoped_refptr<base::TaskRunner> io_thread_task_runner,
ScopedPlatformHandle platform_handle)
: io_thread_task_runner_(io_thread_task_runner),
bootstrap_message_pipe_(MOJO_HANDLE_INVALID),
did_create_channel_event_(true, false),
channel_info_(nullptr) {
bootstrap_message_pipe_ =
CreateChannel(platform_handle.Pass(), io_thread_task_runner_,
base::Bind(&ScopedTestChannel::DidCreateChannel,
base::Unretained(this)),
nullptr)
.release()
.value();
CHECK_NE(bootstrap_message_pipe_, MOJO_HANDLE_INVALID);
}
// Destructor: Shuts down the channel. (As noted above, for this to happen,
// the I/O thread must be alive and pumping messages.)
~ScopedTestChannel() { DestroyChannel(channel_info_); }
// Waits for channel creation to be completed.
void WaitForChannelCreationCompletion() { did_create_channel_event_.Wait(); }
MojoHandle bootstrap_message_pipe() const { return bootstrap_message_pipe_; }
// Call only after |WaitForChannelCreationCompletion()|. Use only to check
// that it's not null.
const ChannelInfo* channel_info() const { return channel_info_; }
private:
void DidCreateChannel(ChannelInfo* channel_info) {
CHECK(channel_info);
CHECK(!channel_info_);
channel_info_ = channel_info;
did_create_channel_event_.Signal();
}
scoped_refptr<base::TaskRunner> io_thread_task_runner_;
// Valid from creation until whenever it gets closed (by the "owner" of this
// object).
// Note: We don't want use the C++ wrappers here, since we want to test the
// API at the lowest level.
MojoHandle bootstrap_message_pipe_;
// Set after channel creation has been completed (i.e., the callback to
// |CreateChannel()| has been called).
base::WaitableEvent did_create_channel_event_;
// Valid after channel creation completion until destruction.
ChannelInfo* channel_info_;
DISALLOW_COPY_AND_ASSIGN(ScopedTestChannel);
};
class EmbedderTest : public testing::Test {
public:
EmbedderTest() : test_io_thread_(base::TestIOThread::kAutoStart) {}
~EmbedderTest() override {}
protected:
base::TestIOThread* test_io_thread() { return &test_io_thread_; }
private:
base::TestIOThread test_io_thread_;
DISALLOW_COPY_AND_ASSIGN(EmbedderTest);
};
TEST_F(EmbedderTest, ChannelsBasic) {
mojo::embedder::test::InitWithSimplePlatformSupport();
{
PlatformChannelPair channel_pair;
ScopedTestChannel server_channel(test_io_thread()->task_runner(),
channel_pair.PassServerHandle());
MojoHandle server_mp = server_channel.bootstrap_message_pipe();
EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
ScopedTestChannel client_channel(test_io_thread()->task_runner(),
channel_pair.PassClientHandle());
MojoHandle client_mp = client_channel.bootstrap_message_pipe();
EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
// We can write to a message pipe handle immediately.
const char kHello[] = "hello";
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteMessage(server_mp, kHello,
static_cast<uint32_t>(sizeof(kHello)), nullptr,
0, MOJO_WRITE_MESSAGE_FLAG_NONE));
// Now wait for the other side to become readable.
MojoHandleSignalsState state;
EXPECT_EQ(MOJO_RESULT_OK,
MojoNewWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
EXPECT_EQ(kSignalAll, state.satisfiable_signals);
char buffer[1000] = {};
uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(client_mp, buffer, &num_bytes, nullptr, nullptr,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(sizeof(kHello), num_bytes);
EXPECT_STREQ(kHello, buffer);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
// By this point, these waits should basically be no-ops (since we've waited
// for the client message pipe to become readable, which implies that both
// the server and client channels were completely created).
server_channel.WaitForChannelCreationCompletion();
client_channel.WaitForChannelCreationCompletion();
EXPECT_TRUE(server_channel.channel_info());
EXPECT_TRUE(client_channel.channel_info());
}
EXPECT_TRUE(test::Shutdown());
}
class TestAsyncWaiter {
public:
TestAsyncWaiter() : event_(true, false), wait_result_(MOJO_RESULT_UNKNOWN) {}
void Awake(MojoResult result) {
base::AutoLock l(wait_result_lock_);
wait_result_ = result;
event_.Signal();
}
bool TryWait() { return event_.TimedWait(TestTimeouts::action_timeout()); }
MojoResult wait_result() const {
base::AutoLock l(wait_result_lock_);
return wait_result_;
}
private:
base::WaitableEvent event_;
mutable base::Lock wait_result_lock_;
MojoResult wait_result_;
DISALLOW_COPY_AND_ASSIGN(TestAsyncWaiter);
};
void WriteHello(MessagePipeHandle pipe) {
static const char kHello[] = "hello";
CHECK_EQ(MOJO_RESULT_OK,
WriteMessageRaw(pipe, kHello, static_cast<uint32_t>(sizeof(kHello)),
nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
}
void CloseScopedHandle(ScopedMessagePipeHandle handle) {
// Do nothing and the destructor will close it.
}
TEST_F(EmbedderTest, AsyncWait) {
mojo::embedder::test::InitWithSimplePlatformSupport();
{
ScopedMessagePipeHandle client_mp;
ScopedMessagePipeHandle server_mp;
EXPECT_EQ(MOJO_RESULT_OK,
mojo::CreateMessagePipe(nullptr, &client_mp, &server_mp));
TestAsyncWaiter waiter;
EXPECT_EQ(MOJO_RESULT_OK,
AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
base::Bind(&TestAsyncWaiter::Awake,
base::Unretained(&waiter))));
test_io_thread()->task_runner()->PostTask(
FROM_HERE, base::Bind(&WriteHello, server_mp.get()));
EXPECT_TRUE(waiter.TryWait());
EXPECT_EQ(MOJO_RESULT_OK, waiter.wait_result());
// If message is in the queue, it does't allow us to wait.
TestAsyncWaiter waiter_that_doesnt_wait;
EXPECT_EQ(
MOJO_RESULT_ALREADY_EXISTS,
AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
base::Bind(&TestAsyncWaiter::Awake,
base::Unretained(&waiter_that_doesnt_wait))));
char buffer[1000];
uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
CHECK_EQ(MOJO_RESULT_OK,
ReadMessageRaw(client_mp.get(), buffer, &num_bytes, nullptr,
nullptr, MOJO_READ_MESSAGE_FLAG_NONE));
TestAsyncWaiter unsatisfiable_waiter;
EXPECT_EQ(MOJO_RESULT_OK,
AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
base::Bind(&TestAsyncWaiter::Awake,
base::Unretained(&unsatisfiable_waiter))));
test_io_thread()->task_runner()->PostTask(
FROM_HERE,
base::Bind(&CloseScopedHandle, base::Passed(server_mp.Pass())));
EXPECT_TRUE(unsatisfiable_waiter.TryWait());
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
unsatisfiable_waiter.wait_result());
}
EXPECT_TRUE(test::Shutdown());
}
TEST_F(EmbedderTest, ChannelsHandlePassing) {
mojo::embedder::test::InitWithSimplePlatformSupport();
{
PlatformChannelPair channel_pair;
ScopedTestChannel server_channel(test_io_thread()->task_runner(),
channel_pair.PassServerHandle());
MojoHandle server_mp = server_channel.bootstrap_message_pipe();
EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
ScopedTestChannel client_channel(test_io_thread()->task_runner(),
channel_pair.PassClientHandle());
MojoHandle client_mp = client_channel.bootstrap_message_pipe();
EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
MojoHandle h0, h1;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1));
// Write a message to |h0| (attaching nothing).
const char kHello[] = "hello";
EXPECT_EQ(
MOJO_RESULT_OK,
MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)),
nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
// Write one message to |server_mp|, attaching |h1|.
const char kWorld[] = "world!!!";
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteMessage(server_mp, kWorld,
static_cast<uint32_t>(sizeof(kWorld)), &h1, 1,
MOJO_WRITE_MESSAGE_FLAG_NONE));
h1 = MOJO_HANDLE_INVALID;
// Write another message to |h0|.
const char kFoo[] = "foo";
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteMessage(h0, kFoo, static_cast<uint32_t>(sizeof(kFoo)),
nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
// Wait for |client_mp| to become readable.
MojoHandleSignalsState state;
EXPECT_EQ(MOJO_RESULT_OK,
MojoNewWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
EXPECT_EQ(kSignalAll, state.satisfiable_signals);
// Read a message from |client_mp|.
char buffer[1000] = {};
uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
MojoHandle handles[10] = {};
uint32_t num_handles = arraysize(handles);
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(client_mp, buffer, &num_bytes, handles,
&num_handles, MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(sizeof(kWorld), num_bytes);
EXPECT_STREQ(kWorld, buffer);
EXPECT_EQ(1u, num_handles);
EXPECT_NE(handles[0], MOJO_HANDLE_INVALID);
h1 = handles[0];
// Wait for |h1| to become readable.
EXPECT_EQ(MOJO_RESULT_OK, MojoNewWait(h1, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
EXPECT_EQ(kSignalAll, state.satisfiable_signals);
// Read a message from |h1|.
memset(buffer, 0, sizeof(buffer));
num_bytes = static_cast<uint32_t>(sizeof(buffer));
memset(handles, 0, sizeof(handles));
num_handles = arraysize(handles);
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(h1, buffer, &num_bytes, handles, &num_handles,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(sizeof(kHello), num_bytes);
EXPECT_STREQ(kHello, buffer);
EXPECT_EQ(0u, num_handles);
// Wait for |h1| to become readable (again).
EXPECT_EQ(MOJO_RESULT_OK, MojoNewWait(h1, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
EXPECT_EQ(kSignalAll, state.satisfiable_signals);
// Read the second message from |h1|.
memset(buffer, 0, sizeof(buffer));
num_bytes = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(h1, buffer, &num_bytes, nullptr, nullptr,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(sizeof(kFoo), num_bytes);
EXPECT_STREQ(kFoo, buffer);
// Write a message to |h1|.
const char kBarBaz[] = "barbaz";
EXPECT_EQ(
MOJO_RESULT_OK,
MojoWriteMessage(h1, kBarBaz, static_cast<uint32_t>(sizeof(kBarBaz)),
nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
// Wait for |h0| to become readable.
EXPECT_EQ(MOJO_RESULT_OK, MojoNewWait(h0, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
EXPECT_EQ(kSignalAll, state.satisfiable_signals);
// Read a message from |h0|.
memset(buffer, 0, sizeof(buffer));
num_bytes = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(sizeof(kBarBaz), num_bytes);
EXPECT_STREQ(kBarBaz, buffer);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1));
server_channel.WaitForChannelCreationCompletion();
client_channel.WaitForChannelCreationCompletion();
EXPECT_TRUE(server_channel.channel_info());
EXPECT_TRUE(client_channel.channel_info());
}
EXPECT_TRUE(test::Shutdown());
}
// The sequence of messages sent is:
// server_mp client_mp mp0 mp1 mp2 mp3
// 1. "hello"
// 2. "world!"
// 3. "FOO"
// 4. "Bar"+mp1
// 5. (close)
// 6. (close)
// 7. "baz"
// 8. (closed)
// 9. "quux"+mp2
// 10. (close)
// 11. (wait/cl.)
// 12. (wait/cl.)
#if defined(OS_ANDROID)
// Android multi-process tests are not executing the new process. This is flaky.
#define MAYBE_MultiprocessChannels DISABLED_MultiprocessChannels
#else
#define MAYBE_MultiprocessChannels MultiprocessChannels
#endif // defined(OS_ANDROID)
TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) {
mojo::embedder::test::InitWithSimplePlatformSupport();
mojo::test::MultiprocessTestHelper multiprocess_test_helper;
multiprocess_test_helper.StartChild("MultiprocessChannelsClient");
{
ScopedTestChannel server_channel(
test_io_thread()->task_runner(),
multiprocess_test_helper.server_platform_handle.Pass());
MojoHandle server_mp = server_channel.bootstrap_message_pipe();
EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
server_channel.WaitForChannelCreationCompletion();
EXPECT_TRUE(server_channel.channel_info());
// 1. Write a message to |server_mp| (attaching nothing).
const char kHello[] = "hello";
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteMessage(server_mp, kHello,
static_cast<uint32_t>(sizeof(kHello)), nullptr,
0, MOJO_WRITE_MESSAGE_FLAG_NONE));
// TODO(vtl): If the scope were ended immediately here (maybe after closing
// |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|.
// 2. Read a message from |server_mp|.
MojoHandleSignalsState state;
EXPECT_EQ(MOJO_RESULT_OK,
MojoNewWait(server_mp, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
EXPECT_EQ(kSignalAll, state.satisfiable_signals);
char buffer[1000] = {};
uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(server_mp, buffer, &num_bytes, nullptr, nullptr,
MOJO_READ_MESSAGE_FLAG_NONE));
const char kWorld[] = "world!";
EXPECT_EQ(sizeof(kWorld), num_bytes);
EXPECT_STREQ(kWorld, buffer);
// Create a new message pipe (endpoints |mp0| and |mp1|).
MojoHandle mp0, mp1;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &mp0, &mp1));
// 3. Write something to |mp0|.
const char kFoo[] = "FOO";
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteMessage(mp0, kFoo, static_cast<uint32_t>(sizeof(kFoo)),
nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
// 4. Write a message to |server_mp|, attaching |mp1|.
const char kBar[] = "Bar";
EXPECT_EQ(
MOJO_RESULT_OK,
MojoWriteMessage(server_mp, kBar, static_cast<uint32_t>(sizeof(kBar)),
&mp1, 1, MOJO_WRITE_MESSAGE_FLAG_NONE));
mp1 = MOJO_HANDLE_INVALID;
// 5. Close |server_mp|.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
// 9. Read a message from |mp0|, which should have |mp2| attached.
EXPECT_EQ(MOJO_RESULT_OK, MojoNewWait(mp0, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
EXPECT_EQ(kSignalAll, state.satisfiable_signals);
memset(buffer, 0, sizeof(buffer));
num_bytes = static_cast<uint32_t>(sizeof(buffer));
MojoHandle mp2 = MOJO_HANDLE_INVALID;
uint32_t num_handles = 1;
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles,
MOJO_READ_MESSAGE_FLAG_NONE));
const char kQuux[] = "quux";
EXPECT_EQ(sizeof(kQuux), num_bytes);
EXPECT_STREQ(kQuux, buffer);
EXPECT_EQ(1u, num_handles);
EXPECT_NE(mp2, MOJO_HANDLE_INVALID);
// 7. Read a message from |mp2|.
EXPECT_EQ(MOJO_RESULT_OK, MojoNewWait(mp2, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
state.satisfied_signals);
EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
state.satisfiable_signals);
memset(buffer, 0, sizeof(buffer));
num_bytes = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr,
MOJO_READ_MESSAGE_FLAG_NONE));
const char kBaz[] = "baz";
EXPECT_EQ(sizeof(kBaz), num_bytes);
EXPECT_STREQ(kBaz, buffer);
// 10. Close |mp0|.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp0));
// 12. Wait on |mp2| (which should eventually fail) and then close it.
// TODO(vtl): crbug.com/351768
#if 0
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoNewWait(mp2, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE,
&state));
EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, state.satisfied_signals);
EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, state.satisfiable_signals);
#endif
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp2));
}
EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown());
EXPECT_TRUE(test::Shutdown());
}
MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) {
ScopedPlatformHandle client_platform_handle =
mojo::test::MultiprocessTestHelper::client_platform_handle.Pass();
EXPECT_TRUE(client_platform_handle.is_valid());
base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart);
mojo::embedder::test::InitWithSimplePlatformSupport();
{
ScopedTestChannel client_channel(test_io_thread.task_runner(),
client_platform_handle.Pass());
MojoHandle client_mp = client_channel.bootstrap_message_pipe();
EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
client_channel.WaitForChannelCreationCompletion();
CHECK(client_channel.channel_info() != nullptr);
// 1. Read the first message from |client_mp|.
MojoHandleSignalsState state;
EXPECT_EQ(MOJO_RESULT_OK,
MojoNewWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
EXPECT_EQ(kSignalAll, state.satisfiable_signals);
char buffer[1000] = {};
uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(client_mp, buffer, &num_bytes, nullptr, nullptr,
MOJO_READ_MESSAGE_FLAG_NONE));
const char kHello[] = "hello";
EXPECT_EQ(sizeof(kHello), num_bytes);
EXPECT_STREQ(kHello, buffer);
// 2. Write a message to |client_mp| (attaching nothing).
const char kWorld[] = "world!";
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteMessage(client_mp, kWorld,
static_cast<uint32_t>(sizeof(kWorld)), nullptr,
0, MOJO_WRITE_MESSAGE_FLAG_NONE));
// 4. Read a message from |client_mp|, which should have |mp1| attached.
EXPECT_EQ(MOJO_RESULT_OK,
MojoNewWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
// The other end of the handle may or may not be closed at this point, so we
// can't test MOJO_HANDLE_SIGNAL_WRITABLE or MOJO_HANDLE_SIGNAL_PEER_CLOSED.
EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE,
state.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE);
EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE,
state.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE);
// TODO(vtl): If the scope were to end here (and |client_mp| closed), we'd
// die (again due to |Channel::HandleLocalError()|).
memset(buffer, 0, sizeof(buffer));
num_bytes = static_cast<uint32_t>(sizeof(buffer));
MojoHandle mp1 = MOJO_HANDLE_INVALID;
uint32_t num_handles = 1;
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(client_mp, buffer, &num_bytes, &mp1, &num_handles,
MOJO_READ_MESSAGE_FLAG_NONE));
const char kBar[] = "Bar";
EXPECT_EQ(sizeof(kBar), num_bytes);
EXPECT_STREQ(kBar, buffer);
EXPECT_EQ(1u, num_handles);
EXPECT_NE(mp1, MOJO_HANDLE_INVALID);
// TODO(vtl): If the scope were to end here (and the two handles closed),
// we'd die due to |Channel::RunRemoteMessagePipeEndpoint()| not handling
// write errors (assuming the parent had closed the pipe).
// 6. Close |client_mp|.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
// Create a new message pipe (endpoints |mp2| and |mp3|).
MojoHandle mp2, mp3;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &mp2, &mp3));
// 7. Write a message to |mp3|.
const char kBaz[] = "baz";
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteMessage(mp3, kBaz, static_cast<uint32_t>(sizeof(kBaz)),
nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
// 8. Close |mp3|.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp3));
// 9. Write a message to |mp1|, attaching |mp2|.
const char kQuux[] = "quux";
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteMessage(mp1, kQuux, static_cast<uint32_t>(sizeof(kQuux)),
&mp2, 1, MOJO_WRITE_MESSAGE_FLAG_NONE));
mp2 = MOJO_HANDLE_INVALID;
// 3. Read a message from |mp1|.
EXPECT_EQ(MOJO_RESULT_OK, MojoNewWait(mp1, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
EXPECT_EQ(kSignalAll, state.satisfiable_signals);
memset(buffer, 0, sizeof(buffer));
num_bytes = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(mp1, buffer, &num_bytes, nullptr, nullptr,
MOJO_READ_MESSAGE_FLAG_NONE));
const char kFoo[] = "FOO";
EXPECT_EQ(sizeof(kFoo), num_bytes);
EXPECT_STREQ(kFoo, buffer);
// 11. Wait on |mp1| (which should eventually fail) and then close it.
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoNewWait(mp1, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &state));
EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals);
EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1));
}
EXPECT_TRUE(test::Shutdown());
}
// TODO(vtl): Test immediate write & close.
// TODO(vtl): Test broken-connection cases.
} // namespace
} // namespace embedder
} // namespace mojo
|