| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <stdint.h> | |
| 6 #include <stdio.h> | |
| 7 #include <string.h> | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/files/file_util.h" | |
| 15 #include "base/files/scoped_file.h" | |
| 16 #include "base/files/scoped_temp_dir.h" | |
| 17 #include "base/location.h" | |
| 18 #include "base/logging.h" | |
| 19 #include "base/macros.h" | |
| 20 #include "build/build_config.h" // TODO(vtl): Remove this. | |
| 21 #include "mojo/edk/embedder/platform_shared_buffer.h" | |
| 22 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
| 23 #include "mojo/edk/system/channel.h" | |
| 24 #include "mojo/edk/system/dispatcher.h" | |
| 25 #include "mojo/edk/system/message_pipe.h" | |
| 26 #include "mojo/edk/system/message_pipe_test_utils.h" | |
| 27 #include "mojo/edk/system/platform_handle_dispatcher.h" | |
| 28 #include "mojo/edk/system/raw_channel.h" | |
| 29 #include "mojo/edk/system/shared_buffer_dispatcher.h" | |
| 30 #include "mojo/edk/system/test_utils.h" | |
| 31 #include "mojo/edk/test/test_utils.h" | |
| 32 #include "testing/gtest/include/gtest/gtest.h" | |
| 33 | |
| 34 namespace mojo { | |
| 35 namespace system { | |
| 36 namespace { | |
| 37 | |
| 38 class MultiprocessMessagePipeTest | |
| 39 : public test::MultiprocessMessagePipeTestBase {}; | |
| 40 | |
| 41 // For each message received, sends a reply message with the same contents | |
| 42 // repeated twice, until the other end is closed or it receives "quitquitquit" | |
| 43 // (which it doesn't reply to). It'll return the number of messages received, | |
| 44 // not including any "quitquitquit" message, modulo 100. | |
| 45 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(EchoEcho) { | |
| 46 embedder::SimplePlatformSupport platform_support; | |
| 47 test::ChannelThread channel_thread(&platform_support); | |
| 48 embedder::ScopedPlatformHandle client_platform_handle = | |
| 49 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | |
| 50 CHECK(client_platform_handle.is_valid()); | |
| 51 scoped_refptr<ChannelEndpoint> ep; | |
| 52 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | |
| 53 channel_thread.Start(client_platform_handle.Pass(), ep); | |
| 54 | |
| 55 const std::string quitquitquit("quitquitquit"); | |
| 56 int rv = 0; | |
| 57 for (;; rv = (rv + 1) % 100) { | |
| 58 // Wait for our end of the message pipe to be readable. | |
| 59 HandleSignalsState hss; | |
| 60 MojoResult result = | |
| 61 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss); | |
| 62 if (result != MOJO_RESULT_OK) { | |
| 63 // It was closed, probably. | |
| 64 CHECK_EQ(result, MOJO_RESULT_FAILED_PRECONDITION); | |
| 65 CHECK_EQ(hss.satisfied_signals, MOJO_HANDLE_SIGNAL_PEER_CLOSED); | |
| 66 CHECK_EQ(hss.satisfiable_signals, MOJO_HANDLE_SIGNAL_PEER_CLOSED); | |
| 67 break; | |
| 68 } else { | |
| 69 CHECK((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); | |
| 70 CHECK((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); | |
| 71 } | |
| 72 | |
| 73 std::string read_buffer(1000, '\0'); | |
| 74 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); | |
| 75 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | |
| 76 MakeUserPointer(&read_buffer_size), nullptr, | |
| 77 nullptr, MOJO_READ_MESSAGE_FLAG_NONE), | |
| 78 MOJO_RESULT_OK); | |
| 79 read_buffer.resize(read_buffer_size); | |
| 80 VLOG(2) << "Child got: " << read_buffer; | |
| 81 | |
| 82 if (read_buffer == quitquitquit) { | |
| 83 VLOG(2) << "Child quitting."; | |
| 84 break; | |
| 85 } | |
| 86 | |
| 87 std::string write_buffer = read_buffer + read_buffer; | |
| 88 CHECK_EQ(mp->WriteMessage(0, UserPointer<const void>(write_buffer.data()), | |
| 89 static_cast<uint32_t>(write_buffer.size()), | |
| 90 nullptr, MOJO_WRITE_MESSAGE_FLAG_NONE), | |
| 91 MOJO_RESULT_OK); | |
| 92 } | |
| 93 | |
| 94 mp->Close(0); | |
| 95 return rv; | |
| 96 } | |
| 97 | |
| 98 // Sends "hello" to child, and expects "hellohello" back. | |
| 99 #if defined(OS_ANDROID) | |
| 100 // Android multi-process tests are not executing the new process. This is flaky. | |
| 101 #define MAYBE_Basic DISABLED_Basic | |
| 102 #else | |
| 103 #define MAYBE_Basic Basic | |
| 104 #endif // defined(OS_ANDROID) | |
| 105 TEST_F(MultiprocessMessagePipeTest, MAYBE_Basic) { | |
| 106 helper()->StartChild("EchoEcho"); | |
| 107 | |
| 108 scoped_refptr<ChannelEndpoint> ep; | |
| 109 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | |
| 110 Init(ep); | |
| 111 | |
| 112 std::string hello("hello"); | |
| 113 EXPECT_EQ(MOJO_RESULT_OK, | |
| 114 mp->WriteMessage(0, UserPointer<const void>(hello.data()), | |
| 115 static_cast<uint32_t>(hello.size()), nullptr, | |
| 116 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 117 | |
| 118 HandleSignalsState hss; | |
| 119 EXPECT_EQ(MOJO_RESULT_OK, | |
| 120 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | |
| 121 // The child may or may not have closed its end of the message pipe and died | |
| 122 // (and we may or may not know it yet), so our end may or may not appear as | |
| 123 // writable. | |
| 124 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); | |
| 125 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); | |
| 126 | |
| 127 std::string read_buffer(1000, '\0'); | |
| 128 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); | |
| 129 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | |
| 130 MakeUserPointer(&read_buffer_size), nullptr, nullptr, | |
| 131 MOJO_READ_MESSAGE_FLAG_NONE), | |
| 132 MOJO_RESULT_OK); | |
| 133 read_buffer.resize(read_buffer_size); | |
| 134 VLOG(2) << "Parent got: " << read_buffer; | |
| 135 EXPECT_EQ(hello + hello, read_buffer); | |
| 136 | |
| 137 mp->Close(0); | |
| 138 | |
| 139 // We sent one message. | |
| 140 EXPECT_EQ(1 % 100, helper()->WaitForChildShutdown()); | |
| 141 } | |
| 142 | |
| 143 // Sends a bunch of messages to the child. Expects them "repeated" back. Waits | |
| 144 // for the child to close its end before quitting. | |
| 145 #if defined(OS_ANDROID) | |
| 146 // Android multi-process tests are not executing the new process. This is flaky. | |
| 147 #define MAYBE_QueueMessages DISABLED_QueueMessages | |
| 148 #else | |
| 149 #define MAYBE_QueueMessages QueueMessages | |
| 150 #endif // defined(OS_ANDROID) | |
| 151 TEST_F(MultiprocessMessagePipeTest, DISABLED_QueueMessages) { | |
| 152 helper()->StartChild("EchoEcho"); | |
| 153 | |
| 154 scoped_refptr<ChannelEndpoint> ep; | |
| 155 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | |
| 156 Init(ep); | |
| 157 | |
| 158 static const size_t kNumMessages = 1001; | |
| 159 for (size_t i = 0; i < kNumMessages; i++) { | |
| 160 std::string write_buffer(i, 'A' + (i % 26)); | |
| 161 EXPECT_EQ(MOJO_RESULT_OK, | |
| 162 mp->WriteMessage(0, UserPointer<const void>(write_buffer.data()), | |
| 163 static_cast<uint32_t>(write_buffer.size()), | |
| 164 nullptr, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 165 } | |
| 166 | |
| 167 const std::string quitquitquit("quitquitquit"); | |
| 168 EXPECT_EQ(MOJO_RESULT_OK, | |
| 169 mp->WriteMessage(0, UserPointer<const void>(quitquitquit.data()), | |
| 170 static_cast<uint32_t>(quitquitquit.size()), | |
| 171 nullptr, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 172 | |
| 173 for (size_t i = 0; i < kNumMessages; i++) { | |
| 174 HandleSignalsState hss; | |
| 175 EXPECT_EQ(MOJO_RESULT_OK, | |
| 176 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | |
| 177 // The child may or may not have closed its end of the message pipe and died | |
| 178 // (and we may or may not know it yet), so our end may or may not appear as | |
| 179 // writable. | |
| 180 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); | |
| 181 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); | |
| 182 | |
| 183 std::string read_buffer(kNumMessages * 2, '\0'); | |
| 184 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); | |
| 185 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | |
| 186 MakeUserPointer(&read_buffer_size), nullptr, | |
| 187 nullptr, MOJO_READ_MESSAGE_FLAG_NONE), | |
| 188 MOJO_RESULT_OK); | |
| 189 read_buffer.resize(read_buffer_size); | |
| 190 | |
| 191 EXPECT_EQ(std::string(i * 2, 'A' + (i % 26)), read_buffer); | |
| 192 } | |
| 193 | |
| 194 // Wait for it to become readable, which should fail (since we sent | |
| 195 // "quitquitquit"). | |
| 196 HandleSignalsState hss; | |
| 197 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | |
| 198 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | |
| 199 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | |
| 200 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | |
| 201 | |
| 202 mp->Close(0); | |
| 203 | |
| 204 EXPECT_EQ(static_cast<int>(kNumMessages % 100), | |
| 205 helper()->WaitForChildShutdown()); | |
| 206 } | |
| 207 | |
| 208 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(CheckSharedBuffer) { | |
| 209 embedder::SimplePlatformSupport platform_support; | |
| 210 test::ChannelThread channel_thread(&platform_support); | |
| 211 embedder::ScopedPlatformHandle client_platform_handle = | |
| 212 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | |
| 213 CHECK(client_platform_handle.is_valid()); | |
| 214 scoped_refptr<ChannelEndpoint> ep; | |
| 215 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | |
| 216 channel_thread.Start(client_platform_handle.Pass(), ep); | |
| 217 | |
| 218 // Wait for the first message from our parent. | |
| 219 HandleSignalsState hss; | |
| 220 CHECK_EQ(test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss), | |
| 221 MOJO_RESULT_OK); | |
| 222 // In this test, the parent definitely doesn't close its end of the message | |
| 223 // pipe before we do. | |
| 224 CHECK_EQ(hss.satisfied_signals, | |
| 225 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); | |
| 226 CHECK_EQ(hss.satisfiable_signals, MOJO_HANDLE_SIGNAL_READABLE | | |
| 227 MOJO_HANDLE_SIGNAL_WRITABLE | | |
| 228 MOJO_HANDLE_SIGNAL_PEER_CLOSED); | |
| 229 | |
| 230 // It should have a shared buffer. | |
| 231 std::string read_buffer(100, '\0'); | |
| 232 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); | |
| 233 DispatcherVector dispatchers; | |
| 234 uint32_t num_dispatchers = 10; // Maximum number to receive. | |
| 235 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | |
| 236 MakeUserPointer(&num_bytes), &dispatchers, | |
| 237 &num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE), | |
| 238 MOJO_RESULT_OK); | |
| 239 read_buffer.resize(num_bytes); | |
| 240 CHECK_EQ(read_buffer, std::string("go 1")); | |
| 241 CHECK_EQ(num_dispatchers, 1u); | |
| 242 | |
| 243 CHECK_EQ(dispatchers[0]->GetType(), Dispatcher::kTypeSharedBuffer); | |
| 244 | |
| 245 scoped_refptr<SharedBufferDispatcher> dispatcher( | |
| 246 static_cast<SharedBufferDispatcher*>(dispatchers[0].get())); | |
| 247 | |
| 248 // Make a mapping. | |
| 249 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; | |
| 250 CHECK_EQ(dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping), | |
| 251 MOJO_RESULT_OK); | |
| 252 CHECK(mapping); | |
| 253 CHECK(mapping->GetBase()); | |
| 254 CHECK_EQ(mapping->GetLength(), 100u); | |
| 255 | |
| 256 // Write some stuff to the shared buffer. | |
| 257 static const char kHello[] = "hello"; | |
| 258 memcpy(mapping->GetBase(), kHello, sizeof(kHello)); | |
| 259 | |
| 260 // We should be able to close the dispatcher now. | |
| 261 dispatcher->Close(); | |
| 262 | |
| 263 // And send a message to signal that we've written stuff. | |
| 264 const std::string go2("go 2"); | |
| 265 CHECK_EQ(mp->WriteMessage(0, UserPointer<const void>(&go2[0]), | |
| 266 static_cast<uint32_t>(go2.size()), nullptr, | |
| 267 MOJO_WRITE_MESSAGE_FLAG_NONE), | |
| 268 MOJO_RESULT_OK); | |
| 269 | |
| 270 // Now wait for our parent to send us a message. | |
| 271 hss = HandleSignalsState(); | |
| 272 CHECK_EQ(test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss), | |
| 273 MOJO_RESULT_OK); | |
| 274 CHECK_EQ(hss.satisfied_signals, | |
| 275 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); | |
| 276 CHECK_EQ(hss.satisfiable_signals, MOJO_HANDLE_SIGNAL_READABLE | | |
| 277 MOJO_HANDLE_SIGNAL_WRITABLE | | |
| 278 MOJO_HANDLE_SIGNAL_PEER_CLOSED); | |
| 279 | |
| 280 read_buffer = std::string(100, '\0'); | |
| 281 num_bytes = static_cast<uint32_t>(read_buffer.size()); | |
| 282 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | |
| 283 MakeUserPointer(&num_bytes), nullptr, nullptr, | |
| 284 MOJO_READ_MESSAGE_FLAG_NONE), | |
| 285 MOJO_RESULT_OK); | |
| 286 read_buffer.resize(num_bytes); | |
| 287 CHECK_EQ(read_buffer, std::string("go 3")); | |
| 288 | |
| 289 // It should have written something to the shared buffer. | |
| 290 static const char kWorld[] = "world!!!"; | |
| 291 CHECK_EQ(memcmp(mapping->GetBase(), kWorld, sizeof(kWorld)), 0); | |
| 292 | |
| 293 // And we're done. | |
| 294 mp->Close(0); | |
| 295 | |
| 296 return 0; | |
| 297 } | |
| 298 | |
| 299 #if defined(OS_POSIX) && !defined(OS_ANDROID) | |
| 300 #define MAYBE_SharedBufferPassing SharedBufferPassing | |
| 301 #else | |
| 302 // Not yet implemented (on Windows). | |
| 303 // Android multi-process tests are not executing the new process. This is flaky. | |
| 304 #define MAYBE_SharedBufferPassing DISABLED_SharedBufferPassing | |
| 305 #endif | |
| 306 TEST_F(MultiprocessMessagePipeTest, MAYBE_SharedBufferPassing) { | |
| 307 helper()->StartChild("CheckSharedBuffer"); | |
| 308 | |
| 309 scoped_refptr<ChannelEndpoint> ep; | |
| 310 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | |
| 311 Init(ep); | |
| 312 | |
| 313 // Make a shared buffer. | |
| 314 scoped_refptr<SharedBufferDispatcher> dispatcher; | |
| 315 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( | |
| 316 platform_support(), | |
| 317 SharedBufferDispatcher::kDefaultCreateOptions, | |
| 318 100, &dispatcher)); | |
| 319 ASSERT_TRUE(dispatcher); | |
| 320 | |
| 321 // Make a mapping. | |
| 322 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; | |
| 323 EXPECT_EQ(MOJO_RESULT_OK, | |
| 324 dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | |
| 325 ASSERT_TRUE(mapping); | |
| 326 ASSERT_TRUE(mapping->GetBase()); | |
| 327 ASSERT_EQ(100u, mapping->GetLength()); | |
| 328 | |
| 329 // Send the shared buffer. | |
| 330 const std::string go1("go 1"); | |
| 331 DispatcherTransport transport( | |
| 332 test::DispatcherTryStartTransport(dispatcher.get())); | |
| 333 ASSERT_TRUE(transport.is_valid()); | |
| 334 | |
| 335 std::vector<DispatcherTransport> transports; | |
| 336 transports.push_back(transport); | |
| 337 EXPECT_EQ(MOJO_RESULT_OK, | |
| 338 mp->WriteMessage(0, UserPointer<const void>(&go1[0]), | |
| 339 static_cast<uint32_t>(go1.size()), &transports, | |
| 340 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 341 transport.End(); | |
| 342 | |
| 343 EXPECT_TRUE(dispatcher->HasOneRef()); | |
| 344 dispatcher = nullptr; | |
| 345 | |
| 346 // Wait for a message from the child. | |
| 347 HandleSignalsState hss; | |
| 348 EXPECT_EQ(MOJO_RESULT_OK, | |
| 349 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | |
| 350 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); | |
| 351 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); | |
| 352 | |
| 353 std::string read_buffer(100, '\0'); | |
| 354 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); | |
| 355 EXPECT_EQ(MOJO_RESULT_OK, | |
| 356 mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | |
| 357 MakeUserPointer(&num_bytes), nullptr, nullptr, | |
| 358 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 359 read_buffer.resize(num_bytes); | |
| 360 EXPECT_EQ(std::string("go 2"), read_buffer); | |
| 361 | |
| 362 // After we get it, the child should have written something to the shared | |
| 363 // buffer. | |
| 364 static const char kHello[] = "hello"; | |
| 365 EXPECT_EQ(0, memcmp(mapping->GetBase(), kHello, sizeof(kHello))); | |
| 366 | |
| 367 // Now we'll write some stuff to the shared buffer. | |
| 368 static const char kWorld[] = "world!!!"; | |
| 369 memcpy(mapping->GetBase(), kWorld, sizeof(kWorld)); | |
| 370 | |
| 371 // And send a message to signal that we've written stuff. | |
| 372 const std::string go3("go 3"); | |
| 373 EXPECT_EQ(MOJO_RESULT_OK, | |
| 374 mp->WriteMessage(0, UserPointer<const void>(&go3[0]), | |
| 375 static_cast<uint32_t>(go3.size()), nullptr, | |
| 376 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 377 | |
| 378 // Wait for |mp| to become readable, which should fail. | |
| 379 hss = HandleSignalsState(); | |
| 380 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | |
| 381 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | |
| 382 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | |
| 383 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | |
| 384 | |
| 385 mp->Close(0); | |
| 386 | |
| 387 EXPECT_EQ(0, helper()->WaitForChildShutdown()); | |
| 388 } | |
| 389 | |
| 390 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(CheckPlatformHandleFile) { | |
| 391 embedder::SimplePlatformSupport platform_support; | |
| 392 test::ChannelThread channel_thread(&platform_support); | |
| 393 embedder::ScopedPlatformHandle client_platform_handle = | |
| 394 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); | |
| 395 CHECK(client_platform_handle.is_valid()); | |
| 396 scoped_refptr<ChannelEndpoint> ep; | |
| 397 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | |
| 398 channel_thread.Start(client_platform_handle.Pass(), ep); | |
| 399 | |
| 400 HandleSignalsState hss; | |
| 401 CHECK_EQ(test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss), | |
| 402 MOJO_RESULT_OK); | |
| 403 CHECK_EQ(hss.satisfied_signals, | |
| 404 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); | |
| 405 CHECK_EQ(hss.satisfiable_signals, MOJO_HANDLE_SIGNAL_READABLE | | |
| 406 MOJO_HANDLE_SIGNAL_WRITABLE | | |
| 407 MOJO_HANDLE_SIGNAL_PEER_CLOSED); | |
| 408 | |
| 409 std::string read_buffer(100, '\0'); | |
| 410 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); | |
| 411 DispatcherVector dispatchers; | |
| 412 uint32_t num_dispatchers = 30; // Maximum number to receive. | |
| 413 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | |
| 414 MakeUserPointer(&num_bytes), &dispatchers, | |
| 415 &num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE), | |
| 416 MOJO_RESULT_OK); | |
| 417 mp->Close(0); | |
| 418 | |
| 419 read_buffer.resize(num_bytes); | |
| 420 char hello[32]; | |
| 421 int num_handles = 0; | |
| 422 sscanf(read_buffer.c_str(), "%s %d", hello, &num_handles); | |
| 423 CHECK_EQ(std::string("hello"), std::string(hello)); | |
| 424 CHECK_GT(num_handles, 0); | |
| 425 | |
| 426 for (int i = 0; i < num_handles; ++i) { | |
| 427 CHECK_EQ(dispatchers[i]->GetType(), Dispatcher::kTypePlatformHandle); | |
| 428 | |
| 429 scoped_refptr<PlatformHandleDispatcher> dispatcher( | |
| 430 static_cast<PlatformHandleDispatcher*>(dispatchers[i].get())); | |
| 431 embedder::ScopedPlatformHandle h = dispatcher->PassPlatformHandle().Pass(); | |
| 432 CHECK(h.is_valid()); | |
| 433 dispatcher->Close(); | |
| 434 | |
| 435 base::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h.Pass(), "r")); | |
| 436 CHECK(fp); | |
| 437 std::string fread_buffer(100, '\0'); | |
| 438 size_t bytes_read = | |
| 439 fread(&fread_buffer[0], 1, fread_buffer.size(), fp.get()); | |
| 440 fread_buffer.resize(bytes_read); | |
| 441 CHECK_EQ(fread_buffer, "world"); | |
| 442 } | |
| 443 | |
| 444 return 0; | |
| 445 } | |
| 446 | |
| 447 class MultiprocessMessagePipeTestWithPipeCount | |
| 448 : public test::MultiprocessMessagePipeTestBase, | |
| 449 public testing::WithParamInterface<size_t> {}; | |
| 450 | |
| 451 TEST_P(MultiprocessMessagePipeTestWithPipeCount, PlatformHandlePassing) { | |
| 452 base::ScopedTempDir temp_dir; | |
| 453 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 454 | |
| 455 helper()->StartChild("CheckPlatformHandleFile"); | |
| 456 | |
| 457 scoped_refptr<ChannelEndpoint> ep; | |
| 458 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | |
| 459 Init(ep); | |
| 460 | |
| 461 std::vector<scoped_refptr<PlatformHandleDispatcher>> dispatchers; | |
| 462 std::vector<DispatcherTransport> transports; | |
| 463 | |
| 464 size_t pipe_count = GetParam(); | |
| 465 for (size_t i = 0; i < pipe_count; ++i) { | |
| 466 base::FilePath unused; | |
| 467 base::ScopedFILE fp( | |
| 468 CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | |
| 469 const std::string world("world"); | |
| 470 CHECK_EQ(fwrite(&world[0], 1, world.size(), fp.get()), world.size()); | |
| 471 fflush(fp.get()); | |
| 472 rewind(fp.get()); | |
| 473 | |
| 474 scoped_refptr<PlatformHandleDispatcher> dispatcher( | |
| 475 new PlatformHandleDispatcher(embedder::ScopedPlatformHandle( | |
| 476 mojo::test::PlatformHandleFromFILE(fp.Pass())))); | |
| 477 dispatchers.push_back(dispatcher); | |
| 478 DispatcherTransport transport( | |
| 479 test::DispatcherTryStartTransport(dispatcher.get())); | |
| 480 ASSERT_TRUE(transport.is_valid()); | |
| 481 transports.push_back(transport); | |
| 482 } | |
| 483 | |
| 484 char message[128]; | |
| 485 sprintf(message, "hello %d", static_cast<int>(pipe_count)); | |
| 486 EXPECT_EQ(MOJO_RESULT_OK, | |
| 487 mp->WriteMessage(0, UserPointer<const void>(message), | |
| 488 static_cast<uint32_t>(strlen(message)), | |
| 489 &transports, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 490 | |
| 491 for (size_t i = 0; i < pipe_count; ++i) { | |
| 492 transports[i].End(); | |
| 493 EXPECT_TRUE(dispatchers[i]->HasOneRef()); | |
| 494 } | |
| 495 | |
| 496 dispatchers.clear(); | |
| 497 | |
| 498 // Wait for it to become readable, which should fail. | |
| 499 HandleSignalsState hss; | |
| 500 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | |
| 501 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | |
| 502 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | |
| 503 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | |
| 504 | |
| 505 mp->Close(0); | |
| 506 | |
| 507 EXPECT_EQ(0, helper()->WaitForChildShutdown()); | |
| 508 } | |
| 509 | |
| 510 // Not yet implemented (on Windows). | |
| 511 // Android multi-process tests are not executing the new process. This is flaky. | |
| 512 #if defined(OS_POSIX) && !defined(OS_ANDROID) | |
| 513 INSTANTIATE_TEST_CASE_P(PipeCount, | |
| 514 MultiprocessMessagePipeTestWithPipeCount, | |
| 515 testing::Values(1u, 10u, 25u)); | |
| 516 #endif | |
| 517 | |
| 518 } // namespace | |
| 519 } // namespace system | |
| 520 } // namespace mojo | |
| OLD | NEW |