| 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" | |
| 16 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
| 13 #include "mojo/edk/embedder/embedder.h" |
| 17 #include "mojo/edk/embedder/platform_channel_pair.h" | 14 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 15 #include "mojo/edk/embedder/simple_platform_support.h" |
| 18 #include "mojo/edk/embedder/test_embedder.h" | 16 #include "mojo/edk/embedder/test_embedder.h" |
| 19 #include "mojo/edk/system/mutex.h" | |
| 20 #include "mojo/edk/system/test_utils.h" | 17 #include "mojo/edk/system/test_utils.h" |
| 21 #include "mojo/edk/test/multiprocess_test_helper.h" | 18 #include "mojo/edk/test/multiprocess_test_helper.h" |
| 22 #include "mojo/edk/test/scoped_ipc_support.h" | 19 #include "mojo/message_pump/message_pump_mojo.h" |
| 23 #include "mojo/public/c/system/core.h" | 20 #include "mojo/public/c/system/core.h" |
| 24 #include "mojo/public/cpp/system/handle.h" | 21 #include "mojo/public/cpp/system/handle.h" |
| 25 #include "mojo/public/cpp/system/macros.h" | 22 #include "mojo/public/cpp/system/macros.h" |
| 26 #include "mojo/public/cpp/system/message_pipe.h" | 23 #include "mojo/public/cpp/system/message_pipe.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 25 |
| 29 namespace mojo { | 26 namespace mojo { |
| 30 namespace embedder { | 27 namespace edk { |
| 31 namespace { | 28 namespace { |
| 32 | 29 |
| 33 const MojoHandleSignals kSignalReadadableWritable = | 30 const MojoHandleSignals kSignalReadadableWritable = |
| 34 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE; | 31 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE; |
| 35 | 32 |
| 36 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE | | 33 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE | |
| 37 MOJO_HANDLE_SIGNAL_WRITABLE | | 34 MOJO_HANDLE_SIGNAL_WRITABLE | |
| 38 MOJO_HANDLE_SIGNAL_PEER_CLOSED; | 35 MOJO_HANDLE_SIGNAL_PEER_CLOSED; |
| 39 | 36 |
| 40 const char kConnectionIdFlag[] = "test-connection-id"; | 37 class EmbedderTest : public test::MojoSystemTest { |
| 41 | |
| 42 void DoNothing() {} | |
| 43 | |
| 44 class ScopedTestChannel { | |
| 45 public: | 38 public: |
| 46 // Creates a channel, which lives on the I/O thread given to | 39 EmbedderTest() {} |
| 47 // |InitIPCSupport()|. After construction, |bootstrap_message_pipe()| gives | 40 ~EmbedderTest() override {} |
| 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 | 41 |
| 97 private: | 42 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 { | |
| 128 public: | |
| 129 EmbedderTest() : test_io_thread_(base::TestIOThread::kAutoStart) {} | |
| 130 ~EmbedderTest() override {} | |
| 131 | |
| 132 protected: | |
| 133 base::TestIOThread& test_io_thread() { return test_io_thread_; } | |
| 134 scoped_refptr<base::TaskRunner> test_io_task_runner() { | |
| 135 return test_io_thread_.task_runner(); | |
| 136 } | |
| 137 | |
| 138 private: | |
| 139 void SetUp() override { test::InitWithSimplePlatformSupport(); } | |
| 140 | |
| 141 void TearDown() override { EXPECT_TRUE(test::Shutdown()); } | |
| 142 | |
| 143 base::TestIOThread test_io_thread_; | |
| 144 | |
| 145 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); | 43 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); |
| 146 }; | 44 }; |
| 147 | 45 |
| 148 TEST_F(EmbedderTest, ChannelsBasic) { | 46 TEST_F(EmbedderTest, ChannelBasic) { |
| 149 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); | 47 MojoHandle server_mp, client_mp; |
| 150 | 48 ASSERT_EQ(MOJO_RESULT_OK, |
| 151 PlatformChannelPair channel_pair; | 49 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp)); |
| 152 ScopedTestChannel server_channel(channel_pair.PassServerHandle()); | |
| 153 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | |
| 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 | 50 |
| 159 // We can write to a message pipe handle immediately. | 51 // We can write to a message pipe handle immediately. |
| 160 const char kHello[] = "hello"; | 52 const char kHello[] = "hello"; |
| 161 EXPECT_EQ( | 53 |
| 162 MOJO_RESULT_OK, | 54 size_t write_size = sizeof(kHello); |
| 163 MojoWriteMessage(server_mp, kHello, static_cast<uint32_t>(sizeof(kHello)), | 55 const char* write_buffer = kHello; |
| 164 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 56 EXPECT_EQ(MOJO_RESULT_OK, |
| 57 MojoWriteMessage(server_mp, write_buffer, |
| 58 static_cast<uint32_t>(write_size), nullptr, 0, |
| 59 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 165 | 60 |
| 166 // Now wait for the other side to become readable. | 61 // Now wait for the other side to become readable. |
| 167 MojoHandleSignalsState state; | 62 MojoHandleSignalsState state; |
| 168 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, | 63 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
| 169 MOJO_DEADLINE_INDEFINITE, &state)); | 64 MOJO_DEADLINE_INDEFINITE, &state)); |
| 170 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | 65 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); |
| 171 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | 66 EXPECT_EQ(kSignalAll, state.satisfiable_signals); |
| 172 | 67 |
| 173 char buffer[1000] = {}; | 68 char read_buffer[1000] = {}; |
| 174 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 69 uint32_t num_bytes = static_cast<uint32_t>(sizeof(read_buffer)); |
| 175 EXPECT_EQ(MOJO_RESULT_OK, | 70 EXPECT_EQ(MOJO_RESULT_OK, |
| 176 MojoReadMessage(client_mp, buffer, &num_bytes, nullptr, nullptr, | 71 MojoReadMessage(client_mp, read_buffer, &num_bytes, nullptr, |
| 177 MOJO_READ_MESSAGE_FLAG_NONE)); | 72 nullptr, MOJO_READ_MESSAGE_FLAG_NONE)); |
| 178 EXPECT_EQ(sizeof(kHello), num_bytes); | 73 EXPECT_EQ(write_size, num_bytes); |
| 179 EXPECT_STREQ(kHello, buffer); | 74 EXPECT_STREQ(kHello, read_buffer); |
| 180 | 75 |
| 181 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | 76 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); |
| 182 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | 77 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 } | 78 } |
| 192 | 79 |
| 193 class TestAsyncWaiter { | 80 // Test sending a MP which has read messages out of the pipe. |
| 194 public: | 81 TEST_F(EmbedderTest, SendReadableMessagePipe) { |
| 195 TestAsyncWaiter() : event_(true, false), wait_result_(MOJO_RESULT_UNKNOWN) {} | 82 MojoHandle server_mp, client_mp; |
| 83 ASSERT_EQ(MOJO_RESULT_OK, |
| 84 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp)); |
| 196 | 85 |
| 197 void Awake(MojoResult result) { | 86 MojoHandle server_mp2, client_mp2; |
| 198 system::MutexLocker l(&wait_result_mutex_); | 87 ASSERT_EQ(MOJO_RESULT_OK, |
| 199 wait_result_ = result; | 88 MojoCreateMessagePipe(nullptr, &server_mp2, &client_mp2)); |
| 200 event_.Signal(); | |
| 201 } | |
| 202 | 89 |
| 203 bool TryWait() { return event_.TimedWait(TestTimeouts::action_timeout()); } | 90 // Write to server2 and wait for client2 to be readable before sending it. |
| 91 const char kHello[] = "hello"; |
| 92 size_t write_size; |
| 93 const char* write_buffer; |
| 94 write_buffer = kHello; |
| 95 write_size = sizeof(kHello); |
| 96 EXPECT_EQ(MOJO_RESULT_OK, |
| 97 MojoWriteMessage(server_mp2, write_buffer, |
| 98 static_cast<uint32_t>(write_size), nullptr, 0, |
| 99 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 204 | 100 |
| 205 MojoResult wait_result() const { | 101 MojoHandleSignalsState state; |
| 206 system::MutexLocker l(&wait_result_mutex_); | 102 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp2, MOJO_HANDLE_SIGNAL_READABLE, |
| 207 return wait_result_; | 103 MOJO_DEADLINE_INDEFINITE, &state)); |
| 208 } | 104 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); |
| 105 EXPECT_EQ(kSignalAll, state.satisfiable_signals); |
| 209 | 106 |
| 210 private: | 107 // Now send client2 |
| 211 base::WaitableEvent event_; | 108 EXPECT_EQ(MOJO_RESULT_OK, |
| 109 MojoWriteMessage(server_mp, write_buffer, |
| 110 static_cast<uint32_t>(write_size), &client_mp2, 1, |
| 111 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 212 | 112 |
| 213 mutable system::Mutex wait_result_mutex_; | |
| 214 MojoResult wait_result_ MOJO_GUARDED_BY(wait_result_mutex_); | |
| 215 | 113 |
| 216 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaiter); | 114 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
| 217 }; | 115 MOJO_DEADLINE_INDEFINITE, &state)); |
| 116 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); |
| 117 EXPECT_EQ(kSignalAll, state.satisfiable_signals); |
| 218 | 118 |
| 219 void WriteHello(MessagePipeHandle pipe) { | 119 char read_buffer[20000] = {}; |
| 220 static const char kHello[] = "hello"; | 120 uint32_t num_bytes = static_cast<uint32_t>(sizeof(read_buffer)); |
| 221 CHECK_EQ(MOJO_RESULT_OK, | 121 MojoHandle ports[10]; |
| 222 WriteMessageRaw(pipe, kHello, static_cast<uint32_t>(sizeof(kHello)), | 122 uint32_t num_ports; |
| 223 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 123 EXPECT_EQ(MOJO_RESULT_OK, |
| 224 } | 124 MojoReadMessage(client_mp, read_buffer, &num_bytes, &ports[0], |
| 125 &num_ports, MOJO_READ_MESSAGE_FLAG_NONE)); |
| 126 EXPECT_EQ(write_size, num_bytes); |
| 127 EXPECT_STREQ(kHello, read_buffer); |
| 128 EXPECT_EQ(1, num_ports); |
| 225 | 129 |
| 226 void CloseScopedHandle(ScopedMessagePipeHandle handle) { | |
| 227 // Do nothing and the destructor will close it. | |
| 228 } | |
| 229 | 130 |
| 230 TEST_F(EmbedderTest, AsyncWait) { | 131 client_mp2 = ports[0]; |
| 231 ScopedMessagePipeHandle client_mp; | 132 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp2, MOJO_HANDLE_SIGNAL_READABLE, |
| 232 ScopedMessagePipeHandle server_mp; | 133 MOJO_DEADLINE_INDEFINITE, &state)); |
| 233 EXPECT_EQ(MOJO_RESULT_OK, CreateMessagePipe(nullptr, &client_mp, &server_mp)); | 134 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); |
| 135 EXPECT_EQ(kSignalAll, state.satisfiable_signals); |
| 234 | 136 |
| 235 TestAsyncWaiter waiter; | 137 |
| 236 EXPECT_EQ(MOJO_RESULT_OK, | 138 EXPECT_EQ(MOJO_RESULT_OK, |
| 237 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE, | 139 MojoReadMessage(client_mp2, read_buffer, &num_bytes, nullptr, 0, |
| 238 base::Bind(&TestAsyncWaiter::Awake, | 140 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 239 base::Unretained(&waiter)))); | 141 EXPECT_EQ(write_size, num_bytes); |
| 142 EXPECT_STREQ(kHello, read_buffer);; |
| 240 | 143 |
| 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 | 144 |
| 246 // If message is in the queue, it does't allow us to wait. | 145 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp2)); |
| 247 TestAsyncWaiter waiter_that_doesnt_wait; | 146 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 | 147 |
| 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 | 148 |
| 259 TestAsyncWaiter unsatisfiable_waiter; | 149 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); |
| 260 EXPECT_EQ(MOJO_RESULT_OK, | 150 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 } | 151 } |
| 273 | 152 |
| 274 TEST_F(EmbedderTest, ChannelsHandlePassing) { | 153 TEST_F(EmbedderTest, ChannelsHandlePassing) { |
| 275 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); | 154 MojoHandle server_mp, client_mp; |
| 276 | 155 ASSERT_EQ(MOJO_RESULT_OK, |
| 277 PlatformChannelPair channel_pair; | 156 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp)); |
| 278 ScopedTestChannel server_channel(channel_pair.PassServerHandle()); | |
| 279 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | |
| 280 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | 157 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); | 158 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
| 284 | 159 |
| 285 MojoHandle h0, h1; | 160 MojoHandle h0, h1; |
| 286 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1)); | 161 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1)); |
| 287 | 162 |
| 288 // Write a message to |h0| (attaching nothing). | 163 // Write a message to |h0| (attaching nothing). |
| 289 const char kHello[] = "hello"; | 164 const char kHello[] = "hello"; |
| 290 EXPECT_EQ(MOJO_RESULT_OK, | 165 EXPECT_EQ(MOJO_RESULT_OK, |
| 291 MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)), | 166 MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)), |
| 292 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 167 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, | 253 EXPECT_EQ(MOJO_RESULT_OK, |
| 379 MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr, | 254 MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr, |
| 380 MOJO_READ_MESSAGE_FLAG_NONE)); | 255 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 381 EXPECT_EQ(sizeof(kBarBaz), num_bytes); | 256 EXPECT_EQ(sizeof(kBarBaz), num_bytes); |
| 382 EXPECT_STREQ(kBarBaz, buffer); | 257 EXPECT_STREQ(kBarBaz, buffer); |
| 383 | 258 |
| 384 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | 259 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); |
| 385 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | 260 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); |
| 386 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0)); | 261 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0)); |
| 387 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1)); | 262 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 } | 263 } |
| 525 | 264 |
| 526 // The sequence of messages sent is: | 265 // The sequence of messages sent is: |
| 527 // server_mp client_mp mp0 mp1 mp2 mp3 | 266 // server_mp client_mp mp0 mp1 mp2 mp3 |
| 528 // 1. "hello" | 267 // 1. "hello" |
| 529 // 2. "world!" | 268 // 2. "world!" |
| 530 // 3. "FOO" | 269 // 3. "FOO" |
| 531 // 4. "Bar"+mp1 | 270 // 4. "Bar"+mp1 |
| 532 // 5. (close) | 271 // 5. (close) |
| 533 // 6. (close) | 272 // 6. (close) |
| 534 // 7. "baz" | 273 // 7. "baz" |
| 535 // 8. (closed) | 274 // 8. (closed) |
| 536 // 9. "quux"+mp2 | 275 // 9. "quux"+mp2 |
| 537 // 10. (close) | 276 // 10. (close) |
| 538 // 11. (wait/cl.) | 277 // 11. (wait/cl.) |
| 539 // 12. (wait/cl.) | 278 // 12. (wait/cl.) |
| 540 | 279 |
| 541 #if defined(OS_ANDROID) | 280 #if defined(OS_ANDROID) |
| 542 // Android multi-process tests are not executing the new process. This is flaky. | 281 // Android multi-process tests are not executing the new process. This is flaky. |
| 543 #define MAYBE_MultiprocessChannels DISABLED_MultiprocessChannels | 282 #define MAYBE_MultiprocessChannels DISABLED_MultiprocessChannels |
| 544 #else | 283 #else |
| 545 #define MAYBE_MultiprocessChannels MultiprocessChannels | 284 #define MAYBE_MultiprocessChannels MultiprocessChannels |
| 546 #endif // defined(OS_ANDROID) | 285 #endif // defined(OS_ANDROID) |
| 547 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) { | 286 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) { |
| 548 // TODO(vtl): This should eventually initialize a master process instead, | 287 test::MultiprocessTestHelper multiprocess_test_helper; |
| 549 // probably. | |
| 550 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); | |
| 551 | |
| 552 mojo::test::MultiprocessTestHelper multiprocess_test_helper; | |
| 553 multiprocess_test_helper.StartChild("MultiprocessChannelsClient"); | 288 multiprocess_test_helper.StartChild("MultiprocessChannelsClient"); |
| 554 | 289 |
| 555 { | 290 { |
| 556 ScopedTestChannel server_channel( | 291 MojoHandle server_mp = CreateMessagePipe( |
| 557 multiprocess_test_helper.server_platform_handle.Pass()); | 292 multiprocess_test_helper.server_platform_handle.Pass()).release(). |
| 558 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | 293 value(); |
| 559 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | |
| 560 server_channel.WaitForChannelCreationCompletion(); | |
| 561 EXPECT_TRUE(server_channel.channel_info()); | |
| 562 | 294 |
| 563 // 1. Write a message to |server_mp| (attaching nothing). | 295 // 1. Write a message to |server_mp| (attaching nothing). |
| 564 const char kHello[] = "hello"; | 296 const char kHello[] = "hello"; |
| 565 EXPECT_EQ(MOJO_RESULT_OK, | 297 EXPECT_EQ(MOJO_RESULT_OK, |
| 566 MojoWriteMessage(server_mp, kHello, | 298 MojoWriteMessage(server_mp, kHello, |
| 567 static_cast<uint32_t>(sizeof(kHello)), nullptr, | 299 static_cast<uint32_t>(sizeof(kHello)), nullptr, |
| 568 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 300 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 569 | 301 |
| 570 // TODO(vtl): If the scope were ended immediately here (maybe after closing | 302 // TODO(vtl): If the scope were ended immediately here (maybe after closing |
| 571 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|. | 303 // |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, | 352 EXPECT_EQ(MOJO_RESULT_OK, |
| 621 MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles, | 353 MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles, |
| 622 MOJO_READ_MESSAGE_FLAG_NONE)); | 354 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 623 const char kQuux[] = "quux"; | 355 const char kQuux[] = "quux"; |
| 624 EXPECT_EQ(sizeof(kQuux), num_bytes); | 356 EXPECT_EQ(sizeof(kQuux), num_bytes); |
| 625 EXPECT_STREQ(kQuux, buffer); | 357 EXPECT_STREQ(kQuux, buffer); |
| 626 EXPECT_EQ(1u, num_handles); | 358 EXPECT_EQ(1u, num_handles); |
| 627 EXPECT_NE(mp2, MOJO_HANDLE_INVALID); | 359 EXPECT_NE(mp2, MOJO_HANDLE_INVALID); |
| 628 | 360 |
| 629 // 7. Read a message from |mp2|. | 361 // 7. Read a message from |mp2|. |
| 630 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp2, MOJO_HANDLE_SIGNAL_READABLE, | 362 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp2, MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 631 MOJO_DEADLINE_INDEFINITE, &state)); | 363 MOJO_DEADLINE_INDEFINITE, &state)); |
| 632 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 364 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 633 state.satisfied_signals); | 365 state.satisfied_signals); |
| 634 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 366 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 635 state.satisfiable_signals); | 367 state.satisfiable_signals); |
| 636 | 368 |
| 637 memset(buffer, 0, sizeof(buffer)); | 369 memset(buffer, 0, sizeof(buffer)); |
| 638 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 370 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
| 639 EXPECT_EQ(MOJO_RESULT_OK, | 371 EXPECT_EQ(MOJO_RESULT_OK, |
| 640 MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr, | 372 MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 657 EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, state.satisfiable_signals); | 389 EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, state.satisfiable_signals); |
| 658 #endif | 390 #endif |
| 659 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp2)); | 391 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp2)); |
| 660 } | 392 } |
| 661 | 393 |
| 662 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); | 394 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); |
| 663 } | 395 } |
| 664 | 396 |
| 665 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) { | 397 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) { |
| 666 ScopedPlatformHandle client_platform_handle = | 398 ScopedPlatformHandle client_platform_handle = |
| 667 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | 399 test::MultiprocessTestHelper::client_platform_handle.Pass(); |
| 668 EXPECT_TRUE(client_platform_handle.is_valid()); | 400 EXPECT_TRUE(client_platform_handle.is_valid()); |
| 669 | 401 |
| 670 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart); | 402 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart); |
| 671 test::InitWithSimplePlatformSupport(); | |
| 672 | 403 |
| 673 { | 404 { |
| 674 // TODO(vtl): This should eventually initialize a slave process instead, | 405 test::ScopedIPCSupport ipc_support(test_io_thread.task_runner()); |
| 675 // probably. | 406 MojoHandle client_mp = CreateMessagePipe( |
| 676 mojo::test::ScopedIPCSupport ipc_support(test_io_thread.task_runner()); | 407 client_platform_handle.Pass()).release().value(); |
| 677 | |
| 678 ScopedTestChannel client_channel(client_platform_handle.Pass()); | |
| 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 | 408 |
| 684 // 1. Read the first message from |client_mp|. | 409 // 1. Read the first message from |client_mp|. |
| 685 MojoHandleSignalsState state; | 410 MojoHandleSignalsState state; |
| 686 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, | 411 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
| 687 MOJO_DEADLINE_INDEFINITE, &state)); | 412 MOJO_DEADLINE_INDEFINITE, &state)); |
| 688 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); | 413 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); |
| 689 EXPECT_EQ(kSignalAll, state.satisfiable_signals); | 414 EXPECT_EQ(kSignalAll, state.satisfiable_signals); |
| 690 | 415 |
| 691 char buffer[1000] = {}; | 416 char buffer[1000] = {}; |
| 692 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 417 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); | 495 EXPECT_STREQ(kFoo, buffer); |
| 771 | 496 |
| 772 // 11. Wait on |mp1| (which should eventually fail) and then close it. | 497 // 11. Wait on |mp1| (which should eventually fail) and then close it. |
| 773 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 498 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 774 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, | 499 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, |
| 775 MOJO_DEADLINE_INDEFINITE, &state)); | 500 MOJO_DEADLINE_INDEFINITE, &state)); |
| 776 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); | 501 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); |
| 777 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); | 502 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); |
| 778 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1)); | 503 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1)); |
| 779 } | 504 } |
| 780 | |
| 781 EXPECT_TRUE(test::Shutdown()); | |
| 782 } | 505 } |
| 783 | 506 |
| 784 // TODO(vtl): Test immediate write & close. | 507 // TODO(vtl): Test immediate write & close. |
| 785 // TODO(vtl): Test broken-connection cases. | 508 // TODO(vtl): Test broken-connection cases. |
| 786 | 509 |
| 787 } // namespace | 510 } // namespace |
| 788 } // namespace embedder | 511 } // namespace edk |
| 789 } // namespace mojo | 512 } // namespace mojo |
| OLD | NEW |