| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/embedder/embedder.h" | 5 #include "mojo/edk/embedder/embedder.h" |
| 6 | 6 |
| 7 #include <string.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "base/logging.h" | 7 #include "base/logging.h" |
| 12 #include "mojo/edk/embedder/platform_channel_pair.h" | |
| 13 #include "mojo/edk/embedder/test_embedder.h" | 8 #include "mojo/edk/embedder/test_embedder.h" |
| 14 #include "mojo/edk/system/test/test_command_line.h" | |
| 15 #include "mojo/edk/system/test/test_io_thread.h" | 9 #include "mojo/edk/system/test/test_io_thread.h" |
| 16 #include "mojo/edk/system/test/timeouts.h" | 10 #include "mojo/edk/system/test/timeouts.h" |
| 17 #include "mojo/edk/test/multiprocess_test_helper.h" | |
| 18 #include "mojo/edk/test/scoped_ipc_support.h" | |
| 19 #include "mojo/edk/util/command_line.h" | |
| 20 #include "mojo/edk/util/mutex.h" | 11 #include "mojo/edk/util/mutex.h" |
| 21 #include "mojo/edk/util/ref_ptr.h" | 12 #include "mojo/edk/util/ref_ptr.h" |
| 22 #include "mojo/edk/util/thread_annotations.h" | 13 #include "mojo/edk/util/thread_annotations.h" |
| 23 #include "mojo/edk/util/waitable_event.h" | 14 #include "mojo/edk/util/waitable_event.h" |
| 24 #include "mojo/public/c/system/core.h" | 15 #include "mojo/public/c/system/types.h" |
| 25 #include "mojo/public/cpp/system/handle.h" | 16 #include "mojo/public/cpp/system/handle.h" |
| 26 #include "mojo/public/cpp/system/macros.h" | 17 #include "mojo/public/cpp/system/macros.h" |
| 27 #include "mojo/public/cpp/system/message_pipe.h" | 18 #include "mojo/public/cpp/system/message_pipe.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 20 |
| 30 using mojo::platform::PlatformHandleWatcher; | |
| 31 using mojo::platform::ScopedPlatformHandle; | |
| 32 using mojo::platform::TaskRunner; | |
| 33 using mojo::system::test::TestIOThread; | 21 using mojo::system::test::TestIOThread; |
| 34 using mojo::util::ManualResetWaitableEvent; | 22 using mojo::util::ManualResetWaitableEvent; |
| 35 using mojo::util::Mutex; | 23 using mojo::util::Mutex; |
| 36 using mojo::util::MutexLocker; | 24 using mojo::util::MutexLocker; |
| 37 using mojo::util::RefPtr; | |
| 38 | 25 |
| 39 namespace mojo { | 26 namespace mojo { |
| 40 namespace embedder { | 27 namespace embedder { |
| 41 namespace { | 28 namespace { |
| 42 | 29 |
| 43 const MojoHandleSignals kSignalReadadableWritable = | |
| 44 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE; | |
| 45 | |
| 46 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE | | |
| 47 MOJO_HANDLE_SIGNAL_WRITABLE | | |
| 48 MOJO_HANDLE_SIGNAL_PEER_CLOSED; | |
| 49 | |
| 50 const char kConnectionIdFlag[] = "test-connection-id"; | |
| 51 | |
| 52 class ScopedTestChannel { | |
| 53 public: | |
| 54 // Creates a channel, which lives on the I/O thread given to | |
| 55 // |InitIPCSupport()|. After construction, |bootstrap_message_pipe()| gives | |
| 56 // the Mojo handle for the bootstrap message pipe on this channel; it is up to | |
| 57 // the caller to close this handle. Note: The I/O thread must outlive this | |
| 58 // object (and its message loop must continue pumping messages while this | |
| 59 // object is alive). | |
| 60 explicit ScopedTestChannel(ScopedPlatformHandle platform_handle) | |
| 61 : bootstrap_message_pipe_(MOJO_HANDLE_INVALID), | |
| 62 channel_info_(nullptr), | |
| 63 wait_on_shutdown_(true) { | |
| 64 bootstrap_message_pipe_ = CreateChannel(platform_handle.Pass(), | |
| 65 [this](ChannelInfo* channel_info) { | |
| 66 CHECK(channel_info); | |
| 67 CHECK(!channel_info_); | |
| 68 channel_info_ = channel_info; | |
| 69 event_.Signal(); | |
| 70 }, | |
| 71 nullptr) | |
| 72 .release() | |
| 73 .value(); | |
| 74 CHECK_NE(bootstrap_message_pipe_, MOJO_HANDLE_INVALID); | |
| 75 } | |
| 76 | |
| 77 // Destructor: Shuts down the channel. (As noted above, for this to happen, | |
| 78 // the I/O thread must be alive and pumping messages.) | |
| 79 ~ScopedTestChannel() { | |
| 80 // |WaitForChannelCreationCompletion()| must be called before destruction. | |
| 81 CHECK(event_.IsSignaledForTest()); | |
| 82 event_.Reset(); | |
| 83 if (wait_on_shutdown_) { | |
| 84 DestroyChannel(channel_info_, [this]() { event_.Signal(); }, nullptr); | |
| 85 event_.Wait(); | |
| 86 } else { | |
| 87 DestroyChannel(channel_info_, []() {}, nullptr); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 // Waits for channel creation to be completed. | |
| 92 void WaitForChannelCreationCompletion() { event_.Wait(); } | |
| 93 | |
| 94 MojoHandle bootstrap_message_pipe() const { return bootstrap_message_pipe_; } | |
| 95 | |
| 96 // Call only after |WaitForChannelCreationCompletion()|. Use only to check | |
| 97 // that it's not null. | |
| 98 const ChannelInfo* channel_info() const { return channel_info_; } | |
| 99 | |
| 100 // Don't wait for the channel shutdown to finish on destruction. Used to | |
| 101 // exercise races. | |
| 102 void NoWaitOnShutdown() { wait_on_shutdown_ = false; } | |
| 103 | |
| 104 private: | |
| 105 // Valid from creation until whenever it gets closed (by the "owner" of this | |
| 106 // object). | |
| 107 // Note: We don't want use the C++ wrappers here, since we want to test the | |
| 108 // API at the lowest level. | |
| 109 MojoHandle bootstrap_message_pipe_; | |
| 110 | |
| 111 // Set after channel creation has been completed (i.e., the callback to | |
| 112 // |CreateChannel()| has been called). Also used in the destructor to wait for | |
| 113 // |DestroyChannel()| completion. | |
| 114 ManualResetWaitableEvent event_; | |
| 115 | |
| 116 // Valid after channel creation completion until destruction. | |
| 117 ChannelInfo* channel_info_; | |
| 118 | |
| 119 // Whether the destructor should wait until the channel is destroyed. | |
| 120 bool wait_on_shutdown_; | |
| 121 | |
| 122 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedTestChannel); | |
| 123 }; | |
| 124 | |
| 125 class EmbedderTest : public testing::Test { | 30 class EmbedderTest : public testing::Test { |
| 126 public: | 31 public: |
| 127 EmbedderTest() : test_io_thread_(TestIOThread::StartMode::AUTO) {} | 32 EmbedderTest() : test_io_thread_(TestIOThread::StartMode::AUTO) {} |
| 128 ~EmbedderTest() override {} | 33 ~EmbedderTest() override {} |
| 129 | 34 |
| 130 protected: | 35 protected: |
| 131 TestIOThread& test_io_thread() { return test_io_thread_; } | 36 TestIOThread& test_io_thread() { return test_io_thread_; } |
| 132 const RefPtr<TaskRunner>& test_io_task_runner() { | |
| 133 return test_io_thread_.task_runner(); | |
| 134 } | |
| 135 PlatformHandleWatcher* test_io_watcher() { | |
| 136 return test_io_thread_.platform_handle_watcher(); | |
| 137 } | |
| 138 | 37 |
| 139 private: | 38 private: |
| 140 void SetUp() override { test::InitWithSimplePlatformSupport(); } | 39 void SetUp() override { test::InitWithSimplePlatformSupport(); } |
| 141 | 40 |
| 142 void TearDown() override { EXPECT_TRUE(test::Shutdown()); } | 41 void TearDown() override { EXPECT_TRUE(test::Shutdown()); } |
| 143 | 42 |
| 43 // TODO(vtl): We don't really need an I/O thread. |
| 144 TestIOThread test_io_thread_; | 44 TestIOThread test_io_thread_; |
| 145 | 45 |
| 146 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); | 46 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); |
| 147 }; | 47 }; |
| 148 | 48 |
| 149 TEST_F(EmbedderTest, ChannelsBasic) { | |
| 150 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone(), | |
| 151 test_io_watcher()); | |
| 152 | |
| 153 PlatformChannelPair channel_pair; | |
| 154 ScopedTestChannel server_channel(channel_pair.PassServerHandle()); | |
| 155 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | |
| 156 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | |
| 157 ScopedTestChannel client_channel(channel_pair.PassClientHandle()); | |
| 158 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | |
| 159 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | |
| 160 | |
| 161 // We can write to a message pipe handle immediately. | |
| 162 const char kHello[] = "hello"; | |
| 163 EXPECT_EQ( | |
| 164 MOJO_RESULT_OK, | |
| 165 MojoWriteMessage(server_mp, kHello, static_cast<uint32_t>(sizeof(kHello)), | |
| 166 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 167 | |
| 168 // Now wait for the other side to become readable. | |
| 169 MojoHandleSignalsState state; | |
| 170 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, | |
| 171 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 172 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | |
| 173 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | |
| 174 | |
| 175 char buffer[1000] = {}; | |
| 176 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 177 EXPECT_EQ(MOJO_RESULT_OK, | |
| 178 MojoReadMessage(client_mp, buffer, &num_bytes, nullptr, nullptr, | |
| 179 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 180 EXPECT_EQ(sizeof(kHello), num_bytes); | |
| 181 EXPECT_STREQ(kHello, buffer); | |
| 182 | |
| 183 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | |
| 184 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | |
| 185 | |
| 186 // By this point, these waits should basically be no-ops (since we've waited | |
| 187 // for the client message pipe to become readable, which implies that both | |
| 188 // the server and client channels were completely created). | |
| 189 server_channel.WaitForChannelCreationCompletion(); | |
| 190 client_channel.WaitForChannelCreationCompletion(); | |
| 191 EXPECT_TRUE(server_channel.channel_info()); | |
| 192 EXPECT_TRUE(client_channel.channel_info()); | |
| 193 } | |
| 194 | |
| 195 class TestAsyncWaiter { | 49 class TestAsyncWaiter { |
| 196 public: | 50 public: |
| 197 TestAsyncWaiter() : wait_result_(MOJO_RESULT_UNKNOWN) {} | 51 TestAsyncWaiter() : wait_result_(MOJO_RESULT_UNKNOWN) {} |
| 198 | 52 |
| 199 void Awake(MojoResult result) { | 53 void Awake(MojoResult result) { |
| 200 MutexLocker l(&wait_result_mutex_); | 54 MutexLocker l(&wait_result_mutex_); |
| 201 wait_result_ = result; | 55 wait_result_ = result; |
| 202 event_.Signal(); | 56 event_.Signal(); |
| 203 } | 57 } |
| 204 | 58 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 auto server_mp_value = server_mp.release(); | 126 auto server_mp_value = server_mp.release(); |
| 273 test_io_thread().PostTask( | 127 test_io_thread().PostTask( |
| 274 [server_mp_value]() { CloseRaw(server_mp_value); }); | 128 [server_mp_value]() { CloseRaw(server_mp_value); }); |
| 275 } | 129 } |
| 276 | 130 |
| 277 EXPECT_TRUE(unsatisfiable_waiter.TryWait()); | 131 EXPECT_TRUE(unsatisfiable_waiter.TryWait()); |
| 278 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 132 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 279 unsatisfiable_waiter.wait_result()); | 133 unsatisfiable_waiter.wait_result()); |
| 280 } | 134 } |
| 281 | 135 |
| 282 TEST_F(EmbedderTest, ChannelsHandlePassing) { | |
| 283 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone(), | |
| 284 test_io_watcher()); | |
| 285 | |
| 286 PlatformChannelPair channel_pair; | |
| 287 ScopedTestChannel server_channel(channel_pair.PassServerHandle()); | |
| 288 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | |
| 289 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | |
| 290 ScopedTestChannel client_channel(channel_pair.PassClientHandle()); | |
| 291 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | |
| 292 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | |
| 293 | |
| 294 MojoHandle h0, h1; | |
| 295 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1)); | |
| 296 | |
| 297 // Write a message to |h0| (attaching nothing). | |
| 298 const char kHello[] = "hello"; | |
| 299 EXPECT_EQ(MOJO_RESULT_OK, | |
| 300 MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)), | |
| 301 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 302 | |
| 303 // Write one message to |server_mp|, attaching |h1|. | |
| 304 const char kWorld[] = "world!!!"; | |
| 305 EXPECT_EQ( | |
| 306 MOJO_RESULT_OK, | |
| 307 MojoWriteMessage(server_mp, kWorld, static_cast<uint32_t>(sizeof(kWorld)), | |
| 308 &h1, 1, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 309 h1 = MOJO_HANDLE_INVALID; | |
| 310 | |
| 311 // Write another message to |h0|. | |
| 312 const char kFoo[] = "foo"; | |
| 313 EXPECT_EQ(MOJO_RESULT_OK, | |
| 314 MojoWriteMessage(h0, kFoo, static_cast<uint32_t>(sizeof(kFoo)), | |
| 315 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 316 | |
| 317 // Wait for |client_mp| to become readable. | |
| 318 MojoHandleSignalsState state; | |
| 319 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, | |
| 320 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 321 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | |
| 322 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | |
| 323 | |
| 324 // Read a message from |client_mp|. | |
| 325 char buffer[1000] = {}; | |
| 326 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 327 MojoHandle handles[10] = {}; | |
| 328 uint32_t num_handles = MOJO_ARRAYSIZE(handles); | |
| 329 EXPECT_EQ(MOJO_RESULT_OK, | |
| 330 MojoReadMessage(client_mp, buffer, &num_bytes, handles, | |
| 331 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 332 EXPECT_EQ(sizeof(kWorld), num_bytes); | |
| 333 EXPECT_STREQ(kWorld, buffer); | |
| 334 EXPECT_EQ(1u, num_handles); | |
| 335 EXPECT_NE(handles[0], MOJO_HANDLE_INVALID); | |
| 336 h1 = handles[0]; | |
| 337 | |
| 338 // Wait for |h1| to become readable. | |
| 339 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(h1, MOJO_HANDLE_SIGNAL_READABLE, | |
| 340 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 341 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | |
| 342 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | |
| 343 | |
| 344 // Read a message from |h1|. | |
| 345 memset(buffer, 0, sizeof(buffer)); | |
| 346 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 347 memset(handles, 0, sizeof(handles)); | |
| 348 num_handles = MOJO_ARRAYSIZE(handles); | |
| 349 EXPECT_EQ(MOJO_RESULT_OK, | |
| 350 MojoReadMessage(h1, buffer, &num_bytes, handles, &num_handles, | |
| 351 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 352 EXPECT_EQ(sizeof(kHello), num_bytes); | |
| 353 EXPECT_STREQ(kHello, buffer); | |
| 354 EXPECT_EQ(0u, num_handles); | |
| 355 | |
| 356 // Wait for |h1| to become readable (again). | |
| 357 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(h1, MOJO_HANDLE_SIGNAL_READABLE, | |
| 358 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 359 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | |
| 360 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | |
| 361 | |
| 362 // Read the second message from |h1|. | |
| 363 memset(buffer, 0, sizeof(buffer)); | |
| 364 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 365 EXPECT_EQ(MOJO_RESULT_OK, | |
| 366 MojoReadMessage(h1, buffer, &num_bytes, nullptr, nullptr, | |
| 367 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 368 EXPECT_EQ(sizeof(kFoo), num_bytes); | |
| 369 EXPECT_STREQ(kFoo, buffer); | |
| 370 | |
| 371 // Write a message to |h1|. | |
| 372 const char kBarBaz[] = "barbaz"; | |
| 373 EXPECT_EQ( | |
| 374 MOJO_RESULT_OK, | |
| 375 MojoWriteMessage(h1, kBarBaz, static_cast<uint32_t>(sizeof(kBarBaz)), | |
| 376 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 377 | |
| 378 // Wait for |h0| to become readable. | |
| 379 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(h0, MOJO_HANDLE_SIGNAL_READABLE, | |
| 380 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 381 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | |
| 382 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | |
| 383 | |
| 384 // Read a message from |h0|. | |
| 385 memset(buffer, 0, sizeof(buffer)); | |
| 386 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 387 EXPECT_EQ(MOJO_RESULT_OK, | |
| 388 MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr, | |
| 389 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 390 EXPECT_EQ(sizeof(kBarBaz), num_bytes); | |
| 391 EXPECT_STREQ(kBarBaz, buffer); | |
| 392 | |
| 393 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | |
| 394 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | |
| 395 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0)); | |
| 396 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1)); | |
| 397 | |
| 398 server_channel.WaitForChannelCreationCompletion(); | |
| 399 client_channel.WaitForChannelCreationCompletion(); | |
| 400 EXPECT_TRUE(server_channel.channel_info()); | |
| 401 EXPECT_TRUE(client_channel.channel_info()); | |
| 402 } | |
| 403 | |
| 404 #if defined(OS_ANDROID) | |
| 405 // Android multi-process tests are not executing the new process. This is flaky. | |
| 406 // TODO(vtl): I'm guessing this is true of this test too? | |
| 407 #define MAYBE_MultiprocessMasterSlave DISABLED_MultiprocessMasterSlave | |
| 408 #else | |
| 409 #define MAYBE_MultiprocessMasterSlave MultiprocessMasterSlave | |
| 410 #endif // defined(OS_ANDROID) | |
| 411 TEST_F(EmbedderTest, MAYBE_MultiprocessMasterSlave) { | |
| 412 mojo::test::ScopedMasterIPCSupport ipc_support(test_io_task_runner().Clone(), | |
| 413 test_io_watcher()); | |
| 414 | |
| 415 mojo::test::MultiprocessTestHelper multiprocess_test_helper; | |
| 416 std::string connection_id; | |
| 417 ManualResetWaitableEvent event; | |
| 418 ChannelInfo* channel_info = nullptr; | |
| 419 ScopedMessagePipeHandle mp = ConnectToSlave( | |
| 420 nullptr, multiprocess_test_helper.server_platform_handle.Pass(), | |
| 421 [&event]() { event.Signal(); }, nullptr, &connection_id, &channel_info); | |
| 422 ASSERT_TRUE(mp.is_valid()); | |
| 423 EXPECT_TRUE(channel_info); | |
| 424 ASSERT_FALSE(connection_id.empty()); | |
| 425 | |
| 426 multiprocess_test_helper.StartChildWithExtraSwitch( | |
| 427 "MultiprocessMasterSlave", kConnectionIdFlag, connection_id); | |
| 428 | |
| 429 // Send a message saying "hello". | |
| 430 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "hello", 5, nullptr, 0, | |
| 431 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 432 | |
| 433 // Wait for a response. | |
| 434 EXPECT_EQ(MOJO_RESULT_OK, Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 435 mojo::system::test::ActionTimeout(), nullptr)); | |
| 436 | |
| 437 // The response message should say "world". | |
| 438 char buffer[100]; | |
| 439 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 440 EXPECT_EQ(MOJO_RESULT_OK, | |
| 441 ReadMessageRaw(mp.get(), buffer, &num_bytes, nullptr, nullptr, | |
| 442 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 443 EXPECT_EQ(5u, num_bytes); | |
| 444 EXPECT_EQ(0, memcmp(buffer, "world", 5)); | |
| 445 | |
| 446 mp.reset(); | |
| 447 | |
| 448 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); | |
| 449 | |
| 450 EXPECT_FALSE(event.WaitWithTimeout(mojo::system::test::ActionTimeout())); | |
| 451 test_io_thread().PostTaskAndWait( | |
| 452 [channel_info]() { DestroyChannelOnIOThread(channel_info); }); | |
| 453 } | |
| 454 | |
| 455 TEST_F(EmbedderTest, ChannelShutdownRace_MessagePipeClose) { | |
| 456 const size_t kIterations = 1000; | |
| 457 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone(), | |
| 458 test_io_watcher()); | |
| 459 | |
| 460 for (size_t i = 0; i < kIterations; i++) { | |
| 461 PlatformChannelPair channel_pair; | |
| 462 std::unique_ptr<ScopedTestChannel> server_channel( | |
| 463 new ScopedTestChannel(channel_pair.PassServerHandle())); | |
| 464 server_channel->WaitForChannelCreationCompletion(); | |
| 465 server_channel->NoWaitOnShutdown(); | |
| 466 | |
| 467 MojoHandle server_mp = server_channel->bootstrap_message_pipe(); | |
| 468 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | |
| 469 | |
| 470 // Race between channel shutdown and closing a message pipe. The message | |
| 471 // pipe doesn't have to be the bootstrap pipe. It just has to be bound to | |
| 472 // the channel. | |
| 473 server_channel.reset(); | |
| 474 MojoClose(server_mp); | |
| 475 } | |
| 476 } | |
| 477 | |
| 478 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessMasterSlave) { | |
| 479 ScopedPlatformHandle client_platform_handle = | |
| 480 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | |
| 481 EXPECT_TRUE(client_platform_handle.is_valid()); | |
| 482 | |
| 483 TestIOThread test_io_thread(TestIOThread::StartMode::AUTO); | |
| 484 test::InitWithSimplePlatformSupport(); | |
| 485 | |
| 486 { | |
| 487 mojo::test::ScopedSlaveIPCSupport ipc_support( | |
| 488 test_io_thread.task_runner().Clone(), | |
| 489 test_io_thread.platform_handle_watcher(), | |
| 490 client_platform_handle.Pass()); | |
| 491 | |
| 492 std::string connection_id; | |
| 493 ASSERT_TRUE(mojo::system::test::GetTestCommandLine()->GetOptionValue( | |
| 494 kConnectionIdFlag, &connection_id)); | |
| 495 ASSERT_FALSE(connection_id.empty()); | |
| 496 ManualResetWaitableEvent event; | |
| 497 ChannelInfo* channel_info = nullptr; | |
| 498 ScopedMessagePipeHandle mp = ConnectToMaster( | |
| 499 connection_id, [&event]() { event.Signal(); }, nullptr, &channel_info); | |
| 500 ASSERT_TRUE(mp.is_valid()); | |
| 501 EXPECT_TRUE(channel_info); | |
| 502 | |
| 503 // Wait for the master to send us a message. | |
| 504 EXPECT_EQ(MOJO_RESULT_OK, | |
| 505 Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 506 mojo::system::test::ActionTimeout(), nullptr)); | |
| 507 | |
| 508 // It should say "hello". | |
| 509 char buffer[100]; | |
| 510 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 511 EXPECT_EQ(MOJO_RESULT_OK, | |
| 512 ReadMessageRaw(mp.get(), buffer, &num_bytes, nullptr, nullptr, | |
| 513 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 514 EXPECT_EQ(5u, num_bytes); | |
| 515 EXPECT_EQ(0, memcmp(buffer, "hello", 5)); | |
| 516 | |
| 517 // In response send a message saying "world". | |
| 518 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "world", 5, nullptr, 0, | |
| 519 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 520 | |
| 521 mp.reset(); | |
| 522 | |
| 523 EXPECT_FALSE(event.WaitWithTimeout(mojo::system::test::ActionTimeout())); | |
| 524 test_io_thread.PostTaskAndWait( | |
| 525 [channel_info]() { DestroyChannelOnIOThread(channel_info); }); | |
| 526 } | |
| 527 | |
| 528 EXPECT_TRUE(test::Shutdown()); | |
| 529 } | |
| 530 | |
| 531 // The sequence of messages sent is: | |
| 532 // server_mp client_mp mp0 mp1 mp2 mp3 | |
| 533 // 1. "hello" | |
| 534 // 2. "world!" | |
| 535 // 3. "FOO" | |
| 536 // 4. "Bar"+mp1 | |
| 537 // 5. (close) | |
| 538 // 6. (close) | |
| 539 // 7. "baz" | |
| 540 // 8. (closed) | |
| 541 // 9. "quux"+mp2 | |
| 542 // 10. (close) | |
| 543 // 11. (wait/cl.) | |
| 544 // 12. (wait/cl.) | |
| 545 | |
| 546 #if defined(OS_ANDROID) | |
| 547 // Android multi-process tests are not executing the new process. This is flaky. | |
| 548 #define MAYBE_MultiprocessChannels DISABLED_MultiprocessChannels | |
| 549 #else | |
| 550 #define MAYBE_MultiprocessChannels MultiprocessChannels | |
| 551 #endif // defined(OS_ANDROID) | |
| 552 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) { | |
| 553 // TODO(vtl): This should eventually initialize a master process instead, | |
| 554 // probably. | |
| 555 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner().Clone(), | |
| 556 test_io_watcher()); | |
| 557 | |
| 558 mojo::test::MultiprocessTestHelper multiprocess_test_helper; | |
| 559 multiprocess_test_helper.StartChild("MultiprocessChannelsClient"); | |
| 560 | |
| 561 { | |
| 562 ScopedTestChannel server_channel( | |
| 563 multiprocess_test_helper.server_platform_handle.Pass()); | |
| 564 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | |
| 565 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | |
| 566 server_channel.WaitForChannelCreationCompletion(); | |
| 567 EXPECT_TRUE(server_channel.channel_info()); | |
| 568 | |
| 569 // 1. Write a message to |server_mp| (attaching nothing). | |
| 570 const char kHello[] = "hello"; | |
| 571 EXPECT_EQ(MOJO_RESULT_OK, | |
| 572 MojoWriteMessage(server_mp, kHello, | |
| 573 static_cast<uint32_t>(sizeof(kHello)), nullptr, | |
| 574 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 575 | |
| 576 // TODO(vtl): If the scope were ended immediately here (maybe after closing | |
| 577 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|. | |
| 578 | |
| 579 // 2. Read a message from |server_mp|. | |
| 580 MojoHandleSignalsState state; | |
| 581 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(server_mp, MOJO_HANDLE_SIGNAL_READABLE, | |
| 582 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 583 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | |
| 584 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | |
| 585 | |
| 586 char buffer[1000] = {}; | |
| 587 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 588 EXPECT_EQ(MOJO_RESULT_OK, | |
| 589 MojoReadMessage(server_mp, buffer, &num_bytes, nullptr, nullptr, | |
| 590 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 591 const char kWorld[] = "world!"; | |
| 592 EXPECT_EQ(sizeof(kWorld), num_bytes); | |
| 593 EXPECT_STREQ(kWorld, buffer); | |
| 594 | |
| 595 // Create a new message pipe (endpoints |mp0| and |mp1|). | |
| 596 MojoHandle mp0, mp1; | |
| 597 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &mp0, &mp1)); | |
| 598 | |
| 599 // 3. Write something to |mp0|. | |
| 600 const char kFoo[] = "FOO"; | |
| 601 EXPECT_EQ(MOJO_RESULT_OK, | |
| 602 MojoWriteMessage(mp0, kFoo, static_cast<uint32_t>(sizeof(kFoo)), | |
| 603 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 604 | |
| 605 // 4. Write a message to |server_mp|, attaching |mp1|. | |
| 606 const char kBar[] = "Bar"; | |
| 607 EXPECT_EQ( | |
| 608 MOJO_RESULT_OK, | |
| 609 MojoWriteMessage(server_mp, kBar, static_cast<uint32_t>(sizeof(kBar)), | |
| 610 &mp1, 1, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 611 mp1 = MOJO_HANDLE_INVALID; | |
| 612 | |
| 613 // 5. Close |server_mp|. | |
| 614 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | |
| 615 | |
| 616 // 9. Read a message from |mp0|, which should have |mp2| attached. | |
| 617 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp0, MOJO_HANDLE_SIGNAL_READABLE, | |
| 618 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 619 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | |
| 620 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | |
| 621 | |
| 622 memset(buffer, 0, sizeof(buffer)); | |
| 623 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 624 MojoHandle mp2 = MOJO_HANDLE_INVALID; | |
| 625 uint32_t num_handles = 1; | |
| 626 EXPECT_EQ(MOJO_RESULT_OK, | |
| 627 MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles, | |
| 628 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 629 const char kQuux[] = "quux"; | |
| 630 EXPECT_EQ(sizeof(kQuux), num_bytes); | |
| 631 EXPECT_STREQ(kQuux, buffer); | |
| 632 EXPECT_EQ(1u, num_handles); | |
| 633 EXPECT_NE(mp2, MOJO_HANDLE_INVALID); | |
| 634 | |
| 635 // 7. Read a message from |mp2|. | |
| 636 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp2, MOJO_HANDLE_SIGNAL_READABLE, | |
| 637 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 638 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
| 639 state.satisfied_signals); | |
| 640 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
| 641 state.satisfiable_signals); | |
| 642 | |
| 643 memset(buffer, 0, sizeof(buffer)); | |
| 644 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 645 EXPECT_EQ(MOJO_RESULT_OK, | |
| 646 MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr, | |
| 647 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 648 const char kBaz[] = "baz"; | |
| 649 EXPECT_EQ(sizeof(kBaz), num_bytes); | |
| 650 EXPECT_STREQ(kBaz, buffer); | |
| 651 | |
| 652 // 10. Close |mp0|. | |
| 653 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp0)); | |
| 654 | |
| 655 // 12. Wait on |mp2| (which should eventually fail) and then close it. | |
| 656 // TODO(vtl): crbug.com/351768 | |
| 657 #if 0 | |
| 658 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | |
| 659 MojoWait(mp2, MOJO_HANDLE_SIGNAL_READABLE, | |
| 660 MOJO_DEADLINE_INDEFINITE, | |
| 661 &state)); | |
| 662 EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, state.satisfied_signals); | |
| 663 EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, state.satisfiable_signals); | |
| 664 #endif | |
| 665 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp2)); | |
| 666 } | |
| 667 | |
| 668 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); | |
| 669 } | |
| 670 | |
| 671 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) { | |
| 672 ScopedPlatformHandle client_platform_handle = | |
| 673 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | |
| 674 EXPECT_TRUE(client_platform_handle.is_valid()); | |
| 675 | |
| 676 TestIOThread test_io_thread(TestIOThread::StartMode::AUTO); | |
| 677 test::InitWithSimplePlatformSupport(); | |
| 678 | |
| 679 { | |
| 680 // TODO(vtl): This should eventually initialize a slave process instead, | |
| 681 // probably. | |
| 682 mojo::test::ScopedIPCSupport ipc_support( | |
| 683 test_io_thread.task_runner().Clone(), | |
| 684 test_io_thread.platform_handle_watcher()); | |
| 685 | |
| 686 ScopedTestChannel client_channel(client_platform_handle.Pass()); | |
| 687 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | |
| 688 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | |
| 689 client_channel.WaitForChannelCreationCompletion(); | |
| 690 CHECK(client_channel.channel_info() != nullptr); | |
| 691 | |
| 692 // 1. Read the first message from |client_mp|. | |
| 693 MojoHandleSignalsState state; | |
| 694 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, | |
| 695 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 696 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | |
| 697 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | |
| 698 | |
| 699 char buffer[1000] = {}; | |
| 700 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 701 EXPECT_EQ(MOJO_RESULT_OK, | |
| 702 MojoReadMessage(client_mp, buffer, &num_bytes, nullptr, nullptr, | |
| 703 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 704 const char kHello[] = "hello"; | |
| 705 EXPECT_EQ(sizeof(kHello), num_bytes); | |
| 706 EXPECT_STREQ(kHello, buffer); | |
| 707 | |
| 708 // 2. Write a message to |client_mp| (attaching nothing). | |
| 709 const char kWorld[] = "world!"; | |
| 710 EXPECT_EQ(MOJO_RESULT_OK, | |
| 711 MojoWriteMessage(client_mp, kWorld, | |
| 712 static_cast<uint32_t>(sizeof(kWorld)), nullptr, | |
| 713 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 714 | |
| 715 // 4. Read a message from |client_mp|, which should have |mp1| attached. | |
| 716 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, | |
| 717 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 718 // The other end of the handle may or may not be closed at this point, so we | |
| 719 // can't test MOJO_HANDLE_SIGNAL_WRITABLE or MOJO_HANDLE_SIGNAL_PEER_CLOSED. | |
| 720 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, | |
| 721 state.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE); | |
| 722 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, | |
| 723 state.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE); | |
| 724 // TODO(vtl): If the scope were to end here (and |client_mp| closed), we'd | |
| 725 // die (again due to |Channel::HandleLocalError()|). | |
| 726 memset(buffer, 0, sizeof(buffer)); | |
| 727 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 728 MojoHandle mp1 = MOJO_HANDLE_INVALID; | |
| 729 uint32_t num_handles = 1; | |
| 730 EXPECT_EQ(MOJO_RESULT_OK, | |
| 731 MojoReadMessage(client_mp, buffer, &num_bytes, &mp1, &num_handles, | |
| 732 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 733 const char kBar[] = "Bar"; | |
| 734 EXPECT_EQ(sizeof(kBar), num_bytes); | |
| 735 EXPECT_STREQ(kBar, buffer); | |
| 736 EXPECT_EQ(1u, num_handles); | |
| 737 EXPECT_NE(mp1, MOJO_HANDLE_INVALID); | |
| 738 // TODO(vtl): If the scope were to end here (and the two handles closed), | |
| 739 // we'd die due to |Channel::RunRemoteMessagePipeEndpoint()| not handling | |
| 740 // write errors (assuming the parent had closed the pipe). | |
| 741 | |
| 742 // 6. Close |client_mp|. | |
| 743 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | |
| 744 | |
| 745 // Create a new message pipe (endpoints |mp2| and |mp3|). | |
| 746 MojoHandle mp2, mp3; | |
| 747 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &mp2, &mp3)); | |
| 748 | |
| 749 // 7. Write a message to |mp3|. | |
| 750 const char kBaz[] = "baz"; | |
| 751 EXPECT_EQ(MOJO_RESULT_OK, | |
| 752 MojoWriteMessage(mp3, kBaz, static_cast<uint32_t>(sizeof(kBaz)), | |
| 753 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 754 | |
| 755 // 8. Close |mp3|. | |
| 756 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp3)); | |
| 757 | |
| 758 // 9. Write a message to |mp1|, attaching |mp2|. | |
| 759 const char kQuux[] = "quux"; | |
| 760 EXPECT_EQ(MOJO_RESULT_OK, | |
| 761 MojoWriteMessage(mp1, kQuux, static_cast<uint32_t>(sizeof(kQuux)), | |
| 762 &mp2, 1, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 763 mp2 = MOJO_HANDLE_INVALID; | |
| 764 | |
| 765 // 3. Read a message from |mp1|. | |
| 766 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, | |
| 767 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 768 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | |
| 769 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | |
| 770 | |
| 771 memset(buffer, 0, sizeof(buffer)); | |
| 772 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 773 EXPECT_EQ(MOJO_RESULT_OK, | |
| 774 MojoReadMessage(mp1, buffer, &num_bytes, nullptr, nullptr, | |
| 775 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 776 const char kFoo[] = "FOO"; | |
| 777 EXPECT_EQ(sizeof(kFoo), num_bytes); | |
| 778 EXPECT_STREQ(kFoo, buffer); | |
| 779 | |
| 780 // 11. Wait on |mp1| (which should eventually fail) and then close it. | |
| 781 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | |
| 782 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, | |
| 783 MOJO_DEADLINE_INDEFINITE, &state)); | |
| 784 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); | |
| 785 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); | |
| 786 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1)); | |
| 787 } | |
| 788 | |
| 789 EXPECT_TRUE(test::Shutdown()); | |
| 790 } | |
| 791 | |
| 792 // TODO(vtl): Test immediate write & close. | |
| 793 // TODO(vtl): Test broken-connection cases. | |
| 794 | |
| 795 } // namespace | 136 } // namespace |
| 796 } // namespace embedder | 137 } // namespace embedder |
| 797 } // namespace mojo | 138 } // namespace mojo |
| OLD | NEW |