| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 11 #include "base/location.h" | |
| 12 #include "base/logging.h" | 9 #include "base/logging.h" |
| 13 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 14 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/test/test_io_thread.h" | 12 #include "base/test/test_io_thread.h" |
| 16 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
| 14 #include "mojo/edk/embedder/embedder.h" |
| 17 #include "mojo/edk/embedder/platform_channel_pair.h" | 15 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 16 #include "mojo/edk/embedder/simple_platform_support.h" |
| 18 #include "mojo/edk/embedder/test_embedder.h" | 17 #include "mojo/edk/embedder/test_embedder.h" |
| 19 #include "mojo/edk/system/mutex.h" | |
| 20 #include "mojo/edk/system/test_utils.h" | 18 #include "mojo/edk/system/test_utils.h" |
| 21 #include "mojo/edk/test/multiprocess_test_helper.h" | 19 #include "mojo/edk/test/multiprocess_test_helper.h" |
| 22 #include "mojo/edk/test/scoped_ipc_support.h" | 20 #include "mojo/edk/test/scoped_ipc_support.h" |
| 21 #include "mojo/message_pump/message_pump_mojo.h" |
| 23 #include "mojo/public/c/system/core.h" | 22 #include "mojo/public/c/system/core.h" |
| 24 #include "mojo/public/cpp/system/handle.h" | 23 #include "mojo/public/cpp/system/handle.h" |
| 25 #include "mojo/public/cpp/system/macros.h" | 24 #include "mojo/public/cpp/system/macros.h" |
| 26 #include "mojo/public/cpp/system/message_pipe.h" | 25 #include "mojo/public/cpp/system/message_pipe.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 27 |
| 29 namespace mojo { | 28 namespace mojo { |
| 30 namespace embedder { | 29 namespace embedder { |
| 31 namespace { | 30 namespace { |
| 32 | 31 |
| 33 const MojoHandleSignals kSignalReadadableWritable = | 32 const MojoHandleSignals kSignalReadadableWritable = |
| 34 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE; | 33 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE; |
| 35 | 34 |
| 36 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE | | 35 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE | |
| 37 MOJO_HANDLE_SIGNAL_WRITABLE | | 36 MOJO_HANDLE_SIGNAL_WRITABLE | |
| 38 MOJO_HANDLE_SIGNAL_PEER_CLOSED; | 37 MOJO_HANDLE_SIGNAL_PEER_CLOSED; |
| 39 | 38 |
| 40 const char kConnectionIdFlag[] = "test-connection-id"; | |
| 41 | |
| 42 void DoNothing() {} | |
| 43 | |
| 44 class ScopedTestChannel { | |
| 45 public: | |
| 46 // Creates a channel, which lives on the I/O thread given to | |
| 47 // |InitIPCSupport()|. After construction, |bootstrap_message_pipe()| gives | |
| 48 // the Mojo handle for the bootstrap message pipe on this channel; it is up to | |
| 49 // the caller to close this handle. Note: The I/O thread must outlive this | |
| 50 // object (and its message loop must continue pumping messages while this | |
| 51 // object is alive). | |
| 52 explicit ScopedTestChannel(ScopedPlatformHandle platform_handle) | |
| 53 : bootstrap_message_pipe_(MOJO_HANDLE_INVALID), | |
| 54 event_(true, false), // Manual reset. | |
| 55 channel_info_(nullptr), | |
| 56 wait_on_shutdown_(true) { | |
| 57 bootstrap_message_pipe_ = | |
| 58 CreateChannel(platform_handle.Pass(), | |
| 59 base::Bind(&ScopedTestChannel::DidCreateChannel, | |
| 60 base::Unretained(this)), | |
| 61 nullptr) | |
| 62 .release() | |
| 63 .value(); | |
| 64 CHECK_NE(bootstrap_message_pipe_, MOJO_HANDLE_INVALID); | |
| 65 } | |
| 66 | |
| 67 // Destructor: Shuts down the channel. (As noted above, for this to happen, | |
| 68 // the I/O thread must be alive and pumping messages.) | |
| 69 ~ScopedTestChannel() { | |
| 70 // |WaitForChannelCreationCompletion()| must be called before destruction. | |
| 71 CHECK(event_.IsSignaled()); | |
| 72 event_.Reset(); | |
| 73 if (wait_on_shutdown_) { | |
| 74 DestroyChannel(channel_info_, | |
| 75 base::Bind(&ScopedTestChannel::DidDestroyChannel, | |
| 76 base::Unretained(this)), | |
| 77 nullptr); | |
| 78 event_.Wait(); | |
| 79 } else { | |
| 80 DestroyChannel(channel_info_, base::Bind(&DoNothing), nullptr); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 // Waits for channel creation to be completed. | |
| 85 void WaitForChannelCreationCompletion() { event_.Wait(); } | |
| 86 | |
| 87 MojoHandle bootstrap_message_pipe() const { return bootstrap_message_pipe_; } | |
| 88 | |
| 89 // Call only after |WaitForChannelCreationCompletion()|. Use only to check | |
| 90 // that it's not null. | |
| 91 const ChannelInfo* channel_info() const { return channel_info_; } | |
| 92 | |
| 93 // Don't wait for the channel shutdown to finish on destruction. Used to | |
| 94 // exercise races. | |
| 95 void NoWaitOnShutdown() { wait_on_shutdown_ = false; } | |
| 96 | |
| 97 private: | |
| 98 void DidCreateChannel(ChannelInfo* channel_info) { | |
| 99 CHECK(channel_info); | |
| 100 CHECK(!channel_info_); | |
| 101 channel_info_ = channel_info; | |
| 102 event_.Signal(); | |
| 103 } | |
| 104 | |
| 105 void DidDestroyChannel() { event_.Signal(); } | |
| 106 | |
| 107 // Valid from creation until whenever it gets closed (by the "owner" of this | |
| 108 // object). | |
| 109 // Note: We don't want use the C++ wrappers here, since we want to test the | |
| 110 // API at the lowest level. | |
| 111 MojoHandle bootstrap_message_pipe_; | |
| 112 | |
| 113 // Set after channel creation has been completed (i.e., the callback to | |
| 114 // |CreateChannel()| has been called). Also used in the destructor to wait for | |
| 115 // |DestroyChannel()| completion. | |
| 116 base::WaitableEvent event_; | |
| 117 | |
| 118 // Valid after channel creation completion until destruction. | |
| 119 ChannelInfo* channel_info_; | |
| 120 | |
| 121 // Whether the destructor should wait until the channel is destroyed. | |
| 122 bool wait_on_shutdown_; | |
| 123 | |
| 124 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedTestChannel); | |
| 125 }; | |
| 126 | |
| 127 class EmbedderTest : public testing::Test { | 39 class EmbedderTest : public testing::Test { |
| 128 public: | 40 public: |
| 129 EmbedderTest() : test_io_thread_(base::TestIOThread::kAutoStart) {} | 41 EmbedderTest() |
| 42 : test_io_thread_(base::TestIOThread::kAutoStart) {} |
| 130 ~EmbedderTest() override {} | 43 ~EmbedderTest() override {} |
| 131 | 44 |
| 132 protected: | 45 protected: |
| 133 base::TestIOThread& test_io_thread() { return test_io_thread_; } | 46 base::TestIOThread& test_io_thread() { return test_io_thread_; } |
| 134 scoped_refptr<base::TaskRunner> test_io_task_runner() { | 47 scoped_refptr<base::TaskRunner> test_io_task_runner() { |
| 135 return test_io_thread_.task_runner(); | 48 return test_io_thread_.task_runner(); |
| 136 } | 49 } |
| 137 | 50 |
| 138 private: | 51 private: |
| 139 void SetUp() override { test::InitWithSimplePlatformSupport(); } | 52 base::MessageLoop message_loop_; |
| 140 | |
| 141 void TearDown() override { EXPECT_TRUE(test::Shutdown()); } | |
| 142 | |
| 143 base::TestIOThread test_io_thread_; | 53 base::TestIOThread test_io_thread_; |
| 144 | 54 |
| 145 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); | 55 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); |
| 146 }; | 56 }; |
| 147 | 57 |
| 148 TEST_F(EmbedderTest, ChannelsBasic) { | 58 TEST_F(EmbedderTest, ChannelBasic) { |
| 149 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); | 59 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); |
| 150 | 60 |
| 151 PlatformChannelPair channel_pair; | 61 MojoHandle server_mp, client_mp; |
| 152 ScopedTestChannel server_channel(channel_pair.PassServerHandle()); | 62 ASSERT_EQ(MOJO_RESULT_OK, |
| 153 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | 63 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp)); |
| 154 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | |
| 155 ScopedTestChannel client_channel(channel_pair.PassClientHandle()); | |
| 156 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | |
| 157 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | |
| 158 | 64 |
| 159 // We can write to a message pipe handle immediately. | 65 // We can write to a message pipe handle immediately. |
| 160 const char kHello[] = "hello"; | 66 const char kHello[] = "hello"; |
| 161 EXPECT_EQ( | 67 |
| 162 MOJO_RESULT_OK, | 68 size_t write_size = sizeof(kHello); |
| 163 MojoWriteMessage(server_mp, kHello, static_cast<uint32_t>(sizeof(kHello)), | 69 const char* write_buffer = kHello; |
| 164 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 70 EXPECT_EQ(MOJO_RESULT_OK, |
| 71 MojoWriteMessage(server_mp, write_buffer, |
| 72 static_cast<uint32_t>(write_size), nullptr, 0, |
| 73 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 165 | 74 |
| 166 // Now wait for the other side to become readable. | 75 // Now wait for the other side to become readable. |
| 167 MojoHandleSignalsState state; | 76 MojoHandleSignalsState state; |
| 168 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, | 77 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
| 169 MOJO_DEADLINE_INDEFINITE, &state)); | 78 MOJO_DEADLINE_INDEFINITE, &state)); |
| 170 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | 79 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); |
| 171 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | 80 EXPECT_EQ(kSignalAll, state.satisfiable_signals); |
| 172 | 81 |
| 173 char buffer[1000] = {}; | 82 char read_buffer[1000] = {}; |
| 174 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 83 uint32_t num_bytes = static_cast<uint32_t>(sizeof(read_buffer)); |
| 175 EXPECT_EQ(MOJO_RESULT_OK, | 84 EXPECT_EQ(MOJO_RESULT_OK, |
| 176 MojoReadMessage(client_mp, buffer, &num_bytes, nullptr, nullptr, | 85 MojoReadMessage(client_mp, read_buffer, &num_bytes, nullptr, |
| 177 MOJO_READ_MESSAGE_FLAG_NONE)); | 86 nullptr, MOJO_READ_MESSAGE_FLAG_NONE)); |
| 178 EXPECT_EQ(sizeof(kHello), num_bytes); | 87 EXPECT_EQ(write_size, num_bytes); |
| 179 EXPECT_STREQ(kHello, buffer); | 88 EXPECT_STREQ(kHello, read_buffer); |
| 180 | 89 |
| 181 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | 90 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); |
| 182 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | 91 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); |
| 183 | |
| 184 // By this point, these waits should basically be no-ops (since we've waited | |
| 185 // for the client message pipe to become readable, which implies that both | |
| 186 // the server and client channels were completely created). | |
| 187 server_channel.WaitForChannelCreationCompletion(); | |
| 188 client_channel.WaitForChannelCreationCompletion(); | |
| 189 EXPECT_TRUE(server_channel.channel_info()); | |
| 190 EXPECT_TRUE(client_channel.channel_info()); | |
| 191 } | 92 } |
| 192 | 93 |
| 193 class TestAsyncWaiter { | 94 // Test sending a MP which has read messages out of the pipe. |
| 194 public: | 95 TEST_F(EmbedderTest, SendReadableMessagePipe) { |
| 195 TestAsyncWaiter() : event_(true, false), wait_result_(MOJO_RESULT_UNKNOWN) {} | 96 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); |
| 196 | 97 |
| 197 void Awake(MojoResult result) { | 98 MojoHandle server_mp, client_mp; |
| 198 system::MutexLocker l(&wait_result_mutex_); | 99 ASSERT_EQ(MOJO_RESULT_OK, |
| 199 wait_result_ = result; | 100 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp)); |
| 200 event_.Signal(); | |
| 201 } | |
| 202 | 101 |
| 203 bool TryWait() { return event_.TimedWait(TestTimeouts::action_timeout()); } | 102 MojoHandle server_mp2, client_mp2; |
| 103 ASSERT_EQ(MOJO_RESULT_OK, |
| 104 MojoCreateMessagePipe(nullptr, &server_mp2, &client_mp2)); |
| 204 | 105 |
| 205 MojoResult wait_result() const { | 106 // Write to server2 and wait for client2 to be readable before sending it. |
| 206 system::MutexLocker l(&wait_result_mutex_); | 107 const char kHello[] = "hello"; |
| 207 return wait_result_; | 108 size_t write_size; |
| 208 } | 109 const char* write_buffer; |
| 110 write_buffer = kHello; |
| 111 write_size = sizeof(kHello); |
| 112 EXPECT_EQ(MOJO_RESULT_OK, |
| 113 MojoWriteMessage(server_mp2, write_buffer, |
| 114 static_cast<uint32_t>(write_size), nullptr, 0, |
| 115 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 209 | 116 |
| 210 private: | 117 MojoHandleSignalsState state; |
| 211 base::WaitableEvent event_; | 118 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp2, MOJO_HANDLE_SIGNAL_READABLE, |
| 119 MOJO_DEADLINE_INDEFINITE, &state)); |
| 120 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); |
| 121 EXPECT_EQ(kSignalAll, state.satisfiable_signals); |
| 212 | 122 |
| 213 mutable system::Mutex wait_result_mutex_; | 123 // Now send client2 |
| 214 MojoResult wait_result_ MOJO_GUARDED_BY(wait_result_mutex_); | 124 EXPECT_EQ(MOJO_RESULT_OK, |
| 125 MojoWriteMessage(server_mp, write_buffer, |
| 126 static_cast<uint32_t>(write_size), &client_mp2, 1, |
| 127 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 215 | 128 |
| 216 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaiter); | |
| 217 }; | |
| 218 | 129 |
| 219 void WriteHello(MessagePipeHandle pipe) { | 130 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
| 220 static const char kHello[] = "hello"; | 131 MOJO_DEADLINE_INDEFINITE, &state)); |
| 221 CHECK_EQ(MOJO_RESULT_OK, | 132 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); |
| 222 WriteMessageRaw(pipe, kHello, static_cast<uint32_t>(sizeof(kHello)), | 133 EXPECT_EQ(kSignalAll, state.satisfiable_signals); |
| 223 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 224 } | |
| 225 | 134 |
| 226 void CloseScopedHandle(ScopedMessagePipeHandle handle) { | 135 char read_buffer[20000] = {}; |
| 227 // Do nothing and the destructor will close it. | 136 uint32_t num_bytes = static_cast<uint32_t>(sizeof(read_buffer)); |
| 228 } | 137 MojoHandle ports[10]; |
| 138 uint32_t num_ports; |
| 139 EXPECT_EQ(MOJO_RESULT_OK, |
| 140 MojoReadMessage(client_mp, read_buffer, &num_bytes, &ports[0], |
| 141 &num_ports, MOJO_READ_MESSAGE_FLAG_NONE)); |
| 142 EXPECT_EQ(write_size, num_bytes); |
| 143 EXPECT_STREQ(kHello, read_buffer); |
| 144 EXPECT_EQ(1, num_ports); |
| 229 | 145 |
| 230 TEST_F(EmbedderTest, AsyncWait) { | |
| 231 ScopedMessagePipeHandle client_mp; | |
| 232 ScopedMessagePipeHandle server_mp; | |
| 233 EXPECT_EQ(MOJO_RESULT_OK, CreateMessagePipe(nullptr, &client_mp, &server_mp)); | |
| 234 | 146 |
| 235 TestAsyncWaiter waiter; | 147 client_mp2 = ports[0]; |
| 148 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp2, MOJO_HANDLE_SIGNAL_READABLE, |
| 149 MOJO_DEADLINE_INDEFINITE, &state)); |
| 150 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); |
| 151 EXPECT_EQ(kSignalAll, state.satisfiable_signals); |
| 152 |
| 153 |
| 236 EXPECT_EQ(MOJO_RESULT_OK, | 154 EXPECT_EQ(MOJO_RESULT_OK, |
| 237 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE, | 155 MojoReadMessage(client_mp2, read_buffer, &num_bytes, nullptr, 0, |
| 238 base::Bind(&TestAsyncWaiter::Awake, | 156 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 239 base::Unretained(&waiter)))); | 157 EXPECT_EQ(write_size, num_bytes); |
| 158 EXPECT_STREQ(kHello, read_buffer);; |
| 240 | 159 |
| 241 test_io_task_runner()->PostTask(FROM_HERE, | |
| 242 base::Bind(&WriteHello, server_mp.get())); | |
| 243 EXPECT_TRUE(waiter.TryWait()); | |
| 244 EXPECT_EQ(MOJO_RESULT_OK, waiter.wait_result()); | |
| 245 | 160 |
| 246 // If message is in the queue, it does't allow us to wait. | 161 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp2)); |
| 247 TestAsyncWaiter waiter_that_doesnt_wait; | 162 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp2)); |
| 248 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | |
| 249 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 250 base::Bind(&TestAsyncWaiter::Awake, | |
| 251 base::Unretained(&waiter_that_doesnt_wait)))); | |
| 252 | 163 |
| 253 char buffer[1000]; | |
| 254 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 255 CHECK_EQ(MOJO_RESULT_OK, | |
| 256 ReadMessageRaw(client_mp.get(), buffer, &num_bytes, nullptr, nullptr, | |
| 257 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 258 | 164 |
| 259 TestAsyncWaiter unsatisfiable_waiter; | 165 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); |
| 260 EXPECT_EQ(MOJO_RESULT_OK, | 166 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); |
| 261 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 262 base::Bind(&TestAsyncWaiter::Awake, | |
| 263 base::Unretained(&unsatisfiable_waiter)))); | |
| 264 | |
| 265 test_io_task_runner()->PostTask( | |
| 266 FROM_HERE, | |
| 267 base::Bind(&CloseScopedHandle, base::Passed(server_mp.Pass()))); | |
| 268 | |
| 269 EXPECT_TRUE(unsatisfiable_waiter.TryWait()); | |
| 270 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | |
| 271 unsatisfiable_waiter.wait_result()); | |
| 272 } | 167 } |
| 273 | 168 |
| 274 TEST_F(EmbedderTest, ChannelsHandlePassing) { | 169 TEST_F(EmbedderTest, ChannelsHandlePassing) { |
| 275 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); | 170 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); |
| 276 | 171 |
| 277 PlatformChannelPair channel_pair; | 172 MojoHandle server_mp, client_mp; |
| 278 ScopedTestChannel server_channel(channel_pair.PassServerHandle()); | 173 ASSERT_EQ(MOJO_RESULT_OK, |
| 279 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | 174 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp)); |
| 280 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | 175 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
| 281 ScopedTestChannel client_channel(channel_pair.PassClientHandle()); | |
| 282 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | |
| 283 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | 176 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
| 284 | 177 |
| 285 MojoHandle h0, h1; | 178 MojoHandle h0, h1; |
| 286 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1)); | 179 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1)); |
| 287 | 180 |
| 288 // Write a message to |h0| (attaching nothing). | 181 // Write a message to |h0| (attaching nothing). |
| 289 const char kHello[] = "hello"; | 182 const char kHello[] = "hello"; |
| 290 EXPECT_EQ(MOJO_RESULT_OK, | 183 EXPECT_EQ(MOJO_RESULT_OK, |
| 291 MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)), | 184 MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)), |
| 292 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 185 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 EXPECT_EQ(MOJO_RESULT_OK, | 271 EXPECT_EQ(MOJO_RESULT_OK, |
| 379 MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr, | 272 MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr, |
| 380 MOJO_READ_MESSAGE_FLAG_NONE)); | 273 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 381 EXPECT_EQ(sizeof(kBarBaz), num_bytes); | 274 EXPECT_EQ(sizeof(kBarBaz), num_bytes); |
| 382 EXPECT_STREQ(kBarBaz, buffer); | 275 EXPECT_STREQ(kBarBaz, buffer); |
| 383 | 276 |
| 384 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | 277 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); |
| 385 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | 278 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); |
| 386 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0)); | 279 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0)); |
| 387 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1)); | 280 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1)); |
| 388 | |
| 389 server_channel.WaitForChannelCreationCompletion(); | |
| 390 client_channel.WaitForChannelCreationCompletion(); | |
| 391 EXPECT_TRUE(server_channel.channel_info()); | |
| 392 EXPECT_TRUE(client_channel.channel_info()); | |
| 393 } | |
| 394 | |
| 395 #if defined(OS_ANDROID) | |
| 396 // Android multi-process tests are not executing the new process. This is flaky. | |
| 397 // TODO(vtl): I'm guessing this is true of this test too? | |
| 398 #define MAYBE_MultiprocessMasterSlave DISABLED_MultiprocessMasterSlave | |
| 399 #else | |
| 400 #define MAYBE_MultiprocessMasterSlave MultiprocessMasterSlave | |
| 401 #endif // defined(OS_ANDROID) | |
| 402 TEST_F(EmbedderTest, MAYBE_MultiprocessMasterSlave) { | |
| 403 mojo::test::ScopedMasterIPCSupport ipc_support(test_io_task_runner()); | |
| 404 | |
| 405 mojo::test::MultiprocessTestHelper multiprocess_test_helper; | |
| 406 std::string connection_id; | |
| 407 base::WaitableEvent event(true, false); | |
| 408 ChannelInfo* channel_info = nullptr; | |
| 409 ScopedMessagePipeHandle mp = ConnectToSlave( | |
| 410 nullptr, multiprocess_test_helper.server_platform_handle.Pass(), | |
| 411 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)), | |
| 412 nullptr, &connection_id, &channel_info); | |
| 413 ASSERT_TRUE(mp.is_valid()); | |
| 414 EXPECT_TRUE(channel_info); | |
| 415 ASSERT_FALSE(connection_id.empty()); | |
| 416 | |
| 417 multiprocess_test_helper.StartChildWithExtraSwitch( | |
| 418 "MultiprocessMasterSlave", kConnectionIdFlag, connection_id); | |
| 419 | |
| 420 // Send a message saying "hello". | |
| 421 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "hello", 5, nullptr, 0, | |
| 422 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 423 | |
| 424 // Wait for a response. | |
| 425 EXPECT_EQ(MOJO_RESULT_OK, | |
| 426 Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 427 mojo::system::test::ActionDeadline(), nullptr)); | |
| 428 | |
| 429 // The response message should say "world". | |
| 430 char buffer[100]; | |
| 431 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 432 EXPECT_EQ(MOJO_RESULT_OK, | |
| 433 ReadMessageRaw(mp.get(), buffer, &num_bytes, nullptr, nullptr, | |
| 434 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 435 EXPECT_EQ(5u, num_bytes); | |
| 436 EXPECT_EQ(0, memcmp(buffer, "world", 5)); | |
| 437 | |
| 438 mp.reset(); | |
| 439 | |
| 440 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); | |
| 441 | |
| 442 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout())); | |
| 443 test_io_thread().PostTaskAndWait( | |
| 444 FROM_HERE, | |
| 445 base::Bind(&DestroyChannelOnIOThread, base::Unretained(channel_info))); | |
| 446 } | |
| 447 | |
| 448 TEST_F(EmbedderTest, ChannelShutdownRace_MessagePipeClose) { | |
| 449 const size_t kIterations = 1000; | |
| 450 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); | |
| 451 | |
| 452 for (size_t i = 0; i < kIterations; i++) { | |
| 453 PlatformChannelPair channel_pair; | |
| 454 scoped_ptr<ScopedTestChannel> server_channel( | |
| 455 new ScopedTestChannel(channel_pair.PassServerHandle())); | |
| 456 server_channel->WaitForChannelCreationCompletion(); | |
| 457 server_channel->NoWaitOnShutdown(); | |
| 458 | |
| 459 MojoHandle server_mp = server_channel->bootstrap_message_pipe(); | |
| 460 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | |
| 461 | |
| 462 // Race between channel shutdown and closing a message pipe. The message | |
| 463 // pipe doesn't have to be the bootstrap pipe. It just has to be bound to | |
| 464 // the channel. | |
| 465 server_channel.reset(); | |
| 466 MojoClose(server_mp); | |
| 467 } | |
| 468 } | |
| 469 | |
| 470 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessMasterSlave) { | |
| 471 ScopedPlatformHandle client_platform_handle = | |
| 472 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | |
| 473 EXPECT_TRUE(client_platform_handle.is_valid()); | |
| 474 | |
| 475 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart); | |
| 476 test::InitWithSimplePlatformSupport(); | |
| 477 | |
| 478 { | |
| 479 mojo::test::ScopedSlaveIPCSupport ipc_support( | |
| 480 test_io_thread.task_runner(), client_platform_handle.Pass()); | |
| 481 | |
| 482 const base::CommandLine& command_line = | |
| 483 *base::CommandLine::ForCurrentProcess(); | |
| 484 ASSERT_TRUE(command_line.HasSwitch(kConnectionIdFlag)); | |
| 485 std::string connection_id = | |
| 486 command_line.GetSwitchValueASCII(kConnectionIdFlag); | |
| 487 ASSERT_FALSE(connection_id.empty()); | |
| 488 base::WaitableEvent event(true, false); | |
| 489 ChannelInfo* channel_info = nullptr; | |
| 490 ScopedMessagePipeHandle mp = ConnectToMaster( | |
| 491 connection_id, | |
| 492 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)), | |
| 493 nullptr, &channel_info); | |
| 494 ASSERT_TRUE(mp.is_valid()); | |
| 495 EXPECT_TRUE(channel_info); | |
| 496 | |
| 497 // Wait for the master to send us a message. | |
| 498 EXPECT_EQ(MOJO_RESULT_OK, | |
| 499 Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 500 mojo::system::test::ActionDeadline(), nullptr)); | |
| 501 | |
| 502 // It should say "hello". | |
| 503 char buffer[100]; | |
| 504 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | |
| 505 EXPECT_EQ(MOJO_RESULT_OK, | |
| 506 ReadMessageRaw(mp.get(), buffer, &num_bytes, nullptr, nullptr, | |
| 507 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 508 EXPECT_EQ(5u, num_bytes); | |
| 509 EXPECT_EQ(0, memcmp(buffer, "hello", 5)); | |
| 510 | |
| 511 // In response send a message saying "world". | |
| 512 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "world", 5, nullptr, 0, | |
| 513 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 514 | |
| 515 mp.reset(); | |
| 516 | |
| 517 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout())); | |
| 518 test_io_thread.PostTaskAndWait( | |
| 519 FROM_HERE, | |
| 520 base::Bind(&DestroyChannelOnIOThread, base::Unretained(channel_info))); | |
| 521 } | |
| 522 | |
| 523 EXPECT_TRUE(test::Shutdown()); | |
| 524 } | 281 } |
| 525 | 282 |
| 526 // The sequence of messages sent is: | 283 // The sequence of messages sent is: |
| 527 // server_mp client_mp mp0 mp1 mp2 mp3 | 284 // server_mp client_mp mp0 mp1 mp2 mp3 |
| 528 // 1. "hello" | 285 // 1. "hello" |
| 529 // 2. "world!" | 286 // 2. "world!" |
| 530 // 3. "FOO" | 287 // 3. "FOO" |
| 531 // 4. "Bar"+mp1 | 288 // 4. "Bar"+mp1 |
| 532 // 5. (close) | 289 // 5. (close) |
| 533 // 6. (close) | 290 // 6. (close) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 546 #endif // defined(OS_ANDROID) | 303 #endif // defined(OS_ANDROID) |
| 547 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) { | 304 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) { |
| 548 // TODO(vtl): This should eventually initialize a master process instead, | 305 // TODO(vtl): This should eventually initialize a master process instead, |
| 549 // probably. | 306 // probably. |
| 550 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); | 307 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); |
| 551 | 308 |
| 552 mojo::test::MultiprocessTestHelper multiprocess_test_helper; | 309 mojo::test::MultiprocessTestHelper multiprocess_test_helper; |
| 553 multiprocess_test_helper.StartChild("MultiprocessChannelsClient"); | 310 multiprocess_test_helper.StartChild("MultiprocessChannelsClient"); |
| 554 | 311 |
| 555 { | 312 { |
| 556 ScopedTestChannel server_channel( | 313 MojoHandle server_mp = mojo::embedder::CreateMessagePipe( |
| 557 multiprocess_test_helper.server_platform_handle.Pass()); | 314 multiprocess_test_helper.server_platform_handle.Pass()).release(). |
| 558 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | 315 value(); |
| 559 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | |
| 560 server_channel.WaitForChannelCreationCompletion(); | |
| 561 EXPECT_TRUE(server_channel.channel_info()); | |
| 562 | 316 |
| 563 // 1. Write a message to |server_mp| (attaching nothing). | 317 // 1. Write a message to |server_mp| (attaching nothing). |
| 564 const char kHello[] = "hello"; | 318 const char kHello[] = "hello"; |
| 565 EXPECT_EQ(MOJO_RESULT_OK, | 319 EXPECT_EQ(MOJO_RESULT_OK, |
| 566 MojoWriteMessage(server_mp, kHello, | 320 MojoWriteMessage(server_mp, kHello, |
| 567 static_cast<uint32_t>(sizeof(kHello)), nullptr, | 321 static_cast<uint32_t>(sizeof(kHello)), nullptr, |
| 568 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 322 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 569 | 323 |
| 570 // TODO(vtl): If the scope were ended immediately here (maybe after closing | 324 // TODO(vtl): If the scope were ended immediately here (maybe after closing |
| 571 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|. | 325 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 EXPECT_EQ(MOJO_RESULT_OK, | 374 EXPECT_EQ(MOJO_RESULT_OK, |
| 621 MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles, | 375 MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles, |
| 622 MOJO_READ_MESSAGE_FLAG_NONE)); | 376 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 623 const char kQuux[] = "quux"; | 377 const char kQuux[] = "quux"; |
| 624 EXPECT_EQ(sizeof(kQuux), num_bytes); | 378 EXPECT_EQ(sizeof(kQuux), num_bytes); |
| 625 EXPECT_STREQ(kQuux, buffer); | 379 EXPECT_STREQ(kQuux, buffer); |
| 626 EXPECT_EQ(1u, num_handles); | 380 EXPECT_EQ(1u, num_handles); |
| 627 EXPECT_NE(mp2, MOJO_HANDLE_INVALID); | 381 EXPECT_NE(mp2, MOJO_HANDLE_INVALID); |
| 628 | 382 |
| 629 // 7. Read a message from |mp2|. | 383 // 7. Read a message from |mp2|. |
| 630 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp2, MOJO_HANDLE_SIGNAL_READABLE, | 384 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp2, MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 631 MOJO_DEADLINE_INDEFINITE, &state)); | 385 MOJO_DEADLINE_INDEFINITE, &state)); |
| 632 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 386 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 633 state.satisfied_signals); | 387 state.satisfied_signals); |
| 634 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 388 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 635 state.satisfiable_signals); | 389 state.satisfiable_signals); |
| 636 | 390 |
| 637 memset(buffer, 0, sizeof(buffer)); | 391 memset(buffer, 0, sizeof(buffer)); |
| 638 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 392 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
| 639 EXPECT_EQ(MOJO_RESULT_OK, | 393 EXPECT_EQ(MOJO_RESULT_OK, |
| 640 MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr, | 394 MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 661 | 415 |
| 662 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); | 416 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); |
| 663 } | 417 } |
| 664 | 418 |
| 665 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) { | 419 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) { |
| 666 ScopedPlatformHandle client_platform_handle = | 420 ScopedPlatformHandle client_platform_handle = |
| 667 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | 421 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); |
| 668 EXPECT_TRUE(client_platform_handle.is_valid()); | 422 EXPECT_TRUE(client_platform_handle.is_valid()); |
| 669 | 423 |
| 670 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart); | 424 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart); |
| 671 test::InitWithSimplePlatformSupport(); | |
| 672 | 425 |
| 673 { | 426 { |
| 674 // TODO(vtl): This should eventually initialize a slave process instead, | |
| 675 // probably. | |
| 676 mojo::test::ScopedIPCSupport ipc_support(test_io_thread.task_runner()); | 427 mojo::test::ScopedIPCSupport ipc_support(test_io_thread.task_runner()); |
| 677 | 428 MojoHandle client_mp = mojo::embedder::CreateMessagePipe( |
| 678 ScopedTestChannel client_channel(client_platform_handle.Pass()); | 429 client_platform_handle.Pass()).release().value(); |
| 679 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | |
| 680 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | |
| 681 client_channel.WaitForChannelCreationCompletion(); | |
| 682 CHECK(client_channel.channel_info() != nullptr); | |
| 683 | 430 |
| 684 // 1. Read the first message from |client_mp|. | 431 // 1. Read the first message from |client_mp|. |
| 685 MojoHandleSignalsState state; | 432 MojoHandleSignalsState state; |
| 686 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, | 433 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
| 687 MOJO_DEADLINE_INDEFINITE, &state)); | 434 MOJO_DEADLINE_INDEFINITE, &state)); |
| 688 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | 435 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); |
| 689 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | 436 EXPECT_EQ(kSignalAll, state.satisfiable_signals); |
| 690 | 437 |
| 691 char buffer[1000] = {}; | 438 char buffer[1000] = {}; |
| 692 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 439 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 770 EXPECT_STREQ(kFoo, buffer); | 517 EXPECT_STREQ(kFoo, buffer); |
| 771 | 518 |
| 772 // 11. Wait on |mp1| (which should eventually fail) and then close it. | 519 // 11. Wait on |mp1| (which should eventually fail) and then close it. |
| 773 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 520 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 774 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, | 521 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, |
| 775 MOJO_DEADLINE_INDEFINITE, &state)); | 522 MOJO_DEADLINE_INDEFINITE, &state)); |
| 776 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); | 523 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); |
| 777 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); | 524 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); |
| 778 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1)); | 525 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1)); |
| 779 } | 526 } |
| 780 | |
| 781 EXPECT_TRUE(test::Shutdown()); | |
| 782 } | 527 } |
| 783 | 528 |
| 784 // TODO(vtl): Test immediate write & close. | 529 // TODO(vtl): Test immediate write & close. |
| 785 // TODO(vtl): Test broken-connection cases. | 530 // TODO(vtl): Test broken-connection cases. |
| 786 | 531 |
| 787 } // namespace | 532 } // namespace |
| 788 } // namespace embedder | 533 } // namespace embedder |
| 789 } // namespace mojo | 534 } // namespace mojo |
| OLD | NEW |