| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 MOJO_RESULT_OK); | 402 MOJO_RESULT_OK); |
| 403 CHECK_EQ(hss.satisfied_signals, | 403 CHECK_EQ(hss.satisfied_signals, |
| 404 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); | 404 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); |
| 405 CHECK_EQ(hss.satisfiable_signals, MOJO_HANDLE_SIGNAL_READABLE | | 405 CHECK_EQ(hss.satisfiable_signals, MOJO_HANDLE_SIGNAL_READABLE | |
| 406 MOJO_HANDLE_SIGNAL_WRITABLE | | 406 MOJO_HANDLE_SIGNAL_WRITABLE | |
| 407 MOJO_HANDLE_SIGNAL_PEER_CLOSED); | 407 MOJO_HANDLE_SIGNAL_PEER_CLOSED); |
| 408 | 408 |
| 409 std::string read_buffer(100, '\0'); | 409 std::string read_buffer(100, '\0'); |
| 410 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); | 410 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); |
| 411 DispatcherVector dispatchers; | 411 DispatcherVector dispatchers; |
| 412 uint32_t num_dispatchers = 10; // Maximum number to receive. | 412 uint32_t num_dispatchers = 30; // Maximum number to receive. |
| 413 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 413 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), |
| 414 MakeUserPointer(&num_bytes), &dispatchers, | 414 MakeUserPointer(&num_bytes), &dispatchers, |
| 415 &num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE), | 415 &num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE), |
| 416 MOJO_RESULT_OK); | 416 MOJO_RESULT_OK); |
| 417 mp->Close(0); | 417 mp->Close(0); |
| 418 | 418 |
| 419 read_buffer.resize(num_bytes); | 419 read_buffer.resize(num_bytes); |
| 420 CHECK_EQ(read_buffer, std::string("hello")); | 420 char hello[32]; |
| 421 CHECK_EQ(num_dispatchers, 1u); | 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); |
| 422 | 425 |
| 423 CHECK_EQ(dispatchers[0]->GetType(), Dispatcher::kTypePlatformHandle); | 426 for (int i = 0; i < num_handles; ++i) { |
| 427 CHECK_EQ(dispatchers[i]->GetType(), Dispatcher::kTypePlatformHandle); |
| 424 | 428 |
| 425 scoped_refptr<PlatformHandleDispatcher> dispatcher( | 429 scoped_refptr<PlatformHandleDispatcher> dispatcher( |
| 426 static_cast<PlatformHandleDispatcher*>(dispatchers[0].get())); | 430 static_cast<PlatformHandleDispatcher*>(dispatchers[i].get())); |
| 427 embedder::ScopedPlatformHandle h = dispatcher->PassPlatformHandle().Pass(); | 431 embedder::ScopedPlatformHandle h = dispatcher->PassPlatformHandle().Pass(); |
| 428 CHECK(h.is_valid()); | 432 CHECK(h.is_valid()); |
| 429 dispatcher->Close(); | 433 dispatcher->Close(); |
| 430 | 434 |
| 431 base::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h.Pass(), "r")); | 435 base::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h.Pass(), "r")); |
| 432 CHECK(fp); | 436 CHECK(fp); |
| 433 std::string fread_buffer(100, '\0'); | 437 std::string fread_buffer(100, '\0'); |
| 434 size_t bytes_read = fread(&fread_buffer[0], 1, fread_buffer.size(), fp.get()); | 438 size_t bytes_read = |
| 435 fread_buffer.resize(bytes_read); | 439 fread(&fread_buffer[0], 1, fread_buffer.size(), fp.get()); |
| 436 CHECK_EQ(fread_buffer, "world"); | 440 fread_buffer.resize(bytes_read); |
| 441 CHECK_EQ(fread_buffer, "world"); |
| 442 } |
| 437 | 443 |
| 438 return 0; | 444 return 0; |
| 439 } | 445 } |
| 440 | 446 |
| 441 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 447 class MultiprocessMessagePipeTestWithPipeCount |
| 442 #define MAYBE_PlatformHandlePassing PlatformHandlePassing | 448 : public test::MultiprocessMessagePipeTestBase, |
| 443 #else | 449 public testing::WithParamInterface<size_t> {}; |
| 444 // Not yet implemented (on Windows). | 450 |
| 445 // Android multi-process tests are not executing the new process. This is flaky. | 451 TEST_P(MultiprocessMessagePipeTestWithPipeCount, PlatformHandlePassing) { |
| 446 #define MAYBE_PlatformHandlePassing DISABLED_PlatformHandlePassing | |
| 447 #endif | |
| 448 TEST_F(MultiprocessMessagePipeTest, MAYBE_PlatformHandlePassing) { | |
| 449 base::ScopedTempDir temp_dir; | 452 base::ScopedTempDir temp_dir; |
| 450 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 453 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 451 | 454 |
| 452 helper()->StartChild("CheckPlatformHandleFile"); | 455 helper()->StartChild("CheckPlatformHandleFile"); |
| 453 | 456 |
| 454 scoped_refptr<ChannelEndpoint> ep; | 457 scoped_refptr<ChannelEndpoint> ep; |
| 455 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | 458 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); |
| 456 Init(ep); | 459 Init(ep); |
| 457 | 460 |
| 458 base::FilePath unused; | 461 std::vector<scoped_refptr<PlatformHandleDispatcher>> dispatchers; |
| 459 base::ScopedFILE fp( | 462 std::vector<DispatcherTransport> transports; |
| 460 CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | |
| 461 const std::string world("world"); | |
| 462 ASSERT_EQ(fwrite(&world[0], 1, world.size(), fp.get()), world.size()); | |
| 463 fflush(fp.get()); | |
| 464 rewind(fp.get()); | |
| 465 | 463 |
| 466 embedder::ScopedPlatformHandle h( | 464 size_t pipe_count = GetParam(); |
| 467 mojo::test::PlatformHandleFromFILE(fp.Pass())); | 465 for (size_t i = 0; i < pipe_count; ++i) { |
| 468 scoped_refptr<PlatformHandleDispatcher> dispatcher( | 466 base::FilePath unused; |
| 469 new PlatformHandleDispatcher(h.Pass())); | 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()); |
| 470 | 473 |
| 471 const std::string hello("hello"); | 474 scoped_refptr<PlatformHandleDispatcher> dispatcher( |
| 472 DispatcherTransport transport( | 475 new PlatformHandleDispatcher(embedder::ScopedPlatformHandle( |
| 473 test::DispatcherTryStartTransport(dispatcher.get())); | 476 mojo::test::PlatformHandleFromFILE(fp.Pass())))); |
| 474 ASSERT_TRUE(transport.is_valid()); | 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 } |
| 475 | 483 |
| 476 std::vector<DispatcherTransport> transports; | 484 char message[128]; |
| 477 transports.push_back(transport); | 485 sprintf(message, "hello %d", static_cast<int>(pipe_count)); |
| 478 EXPECT_EQ(MOJO_RESULT_OK, | 486 EXPECT_EQ(MOJO_RESULT_OK, |
| 479 mp->WriteMessage(0, UserPointer<const void>(&hello[0]), | 487 mp->WriteMessage(0, UserPointer<const void>(message), |
| 480 static_cast<uint32_t>(hello.size()), &transports, | 488 static_cast<uint32_t>(strlen(message)), |
| 481 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 489 &transports, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 482 transport.End(); | |
| 483 | 490 |
| 484 EXPECT_TRUE(dispatcher->HasOneRef()); | 491 for (size_t i = 0; i < pipe_count; ++i) { |
| 485 dispatcher = nullptr; | 492 transports[i].End(); |
| 493 EXPECT_TRUE(dispatchers[i]->HasOneRef()); |
| 494 } |
| 495 |
| 496 dispatchers.clear(); |
| 486 | 497 |
| 487 // Wait for it to become readable, which should fail. | 498 // Wait for it to become readable, which should fail. |
| 488 HandleSignalsState hss; | 499 HandleSignalsState hss; |
| 489 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 500 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 490 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | 501 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); |
| 491 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 502 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
| 492 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 503 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
| 493 | 504 |
| 494 mp->Close(0); | 505 mp->Close(0); |
| 495 | 506 |
| 496 EXPECT_EQ(0, helper()->WaitForChildShutdown()); | 507 EXPECT_EQ(0, helper()->WaitForChildShutdown()); |
| 497 } | 508 } |
| 498 | 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) and !defined(OS_ANDROID) |
| 513 INSTANTIATE_TEST_CASE_P(PipeCount, |
| 514 MultiprocessMessagePipeTestWithPipeCount, |
| 515 testing::Values(1u, 10u, 25u)); |
| 516 #endif |
| 517 |
| 499 } // namespace | 518 } // namespace |
| 500 } // namespace system | 519 } // namespace system |
| 501 } // namespace mojo | 520 } // namespace mojo |
| OLD | NEW |