| 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 <memory> |    9 #include <memory> | 
|   10 #include <string> |   10 #include <string> | 
|   11 #include <utility> |   11 #include <utility> | 
|   12 #include <vector> |   12 #include <vector> | 
|   13  |   13  | 
|   14 #include "base/logging.h" |   14 #include "base/logging.h" | 
|   15 #include "build/build_config.h"  // TODO(vtl): Remove this. |   15 #include "build/build_config.h"  // TODO(vtl): Remove this. | 
|   16 #include "mojo/edk/embedder/simple_platform_support.h" |   16 #include "mojo/edk/embedder/simple_platform_support.h" | 
|   17 #include "mojo/edk/platform/platform_handle_utils_posix.h" |   17 #include "mojo/edk/platform/platform_handle_utils_posix.h" | 
|   18 #include "mojo/edk/platform/platform_shared_buffer.h" |   18 #include "mojo/edk/platform/platform_shared_buffer.h" | 
|   19 #include "mojo/edk/platform/scoped_platform_handle.h" |   19 #include "mojo/edk/platform/scoped_platform_handle.h" | 
|   20 #include "mojo/edk/system/channel.h" |   20 #include "mojo/edk/system/channel.h" | 
|   21 #include "mojo/edk/system/dispatcher.h" |   21 #include "mojo/edk/system/dispatcher.h" | 
 |   22 #include "mojo/edk/system/handle.h" | 
|   22 #include "mojo/edk/system/handle_transport.h" |   23 #include "mojo/edk/system/handle_transport.h" | 
|   23 #include "mojo/edk/system/message_pipe.h" |   24 #include "mojo/edk/system/message_pipe.h" | 
|   24 #include "mojo/edk/system/message_pipe_test_utils.h" |   25 #include "mojo/edk/system/message_pipe_test_utils.h" | 
|   25 #include "mojo/edk/system/platform_handle_dispatcher.h" |   26 #include "mojo/edk/system/platform_handle_dispatcher.h" | 
|   26 #include "mojo/edk/system/raw_channel.h" |   27 #include "mojo/edk/system/raw_channel.h" | 
|   27 #include "mojo/edk/system/shared_buffer_dispatcher.h" |   28 #include "mojo/edk/system/shared_buffer_dispatcher.h" | 
|   28 #include "mojo/edk/system/test/scoped_test_dir.h" |   29 #include "mojo/edk/system/test/scoped_test_dir.h" | 
|   29 #include "mojo/edk/util/ref_ptr.h" |   30 #include "mojo/edk/util/ref_ptr.h" | 
|   30 #include "mojo/edk/util/scoped_file.h" |   31 #include "mojo/edk/util/scoped_file.h" | 
|   31 #include "testing/gtest/include/gtest/gtest.h" |   32 #include "testing/gtest/include/gtest/gtest.h" | 
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  316   auto mp = MessagePipe::CreateLocalProxy(&ep); |  317   auto mp = MessagePipe::CreateLocalProxy(&ep); | 
|  317   Init(std::move(ep)); |  318   Init(std::move(ep)); | 
|  318  |  319  | 
|  319   // Make a shared buffer. |  320   // Make a shared buffer. | 
|  320   MojoResult result = MOJO_RESULT_INTERNAL; |  321   MojoResult result = MOJO_RESULT_INTERNAL; | 
|  321   auto dispatcher = SharedBufferDispatcher::Create( |  322   auto dispatcher = SharedBufferDispatcher::Create( | 
|  322       platform_support(), SharedBufferDispatcher::kDefaultCreateOptions, 100, |  323       platform_support(), SharedBufferDispatcher::kDefaultCreateOptions, 100, | 
|  323       &result); |  324       &result); | 
|  324   EXPECT_EQ(MOJO_RESULT_OK, result); |  325   EXPECT_EQ(MOJO_RESULT_OK, result); | 
|  325   ASSERT_TRUE(dispatcher); |  326   ASSERT_TRUE(dispatcher); | 
 |  327   Handle handle(std::move(dispatcher), | 
 |  328                 MOJO_HANDLE_RIGHT_DUPLICATE | MOJO_HANDLE_RIGHT_TRANSFER | | 
 |  329                     MOJO_HANDLE_RIGHT_READ | MOJO_HANDLE_RIGHT_WRITE); | 
|  326  |  330  | 
|  327   // Make a mapping. |  331   // Make a mapping. | 
|  328   std::unique_ptr<PlatformSharedBufferMapping> mapping; |  332   std::unique_ptr<PlatformSharedBufferMapping> mapping; | 
|  329   EXPECT_EQ(MOJO_RESULT_OK, |  333   EXPECT_EQ(MOJO_RESULT_OK, handle.dispatcher->MapBuffer( | 
|  330             dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |  334                                 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | 
|  331   ASSERT_TRUE(mapping); |  335   ASSERT_TRUE(mapping); | 
|  332   ASSERT_TRUE(mapping->GetBase()); |  336   ASSERT_TRUE(mapping->GetBase()); | 
|  333   ASSERT_EQ(100u, mapping->GetLength()); |  337   ASSERT_EQ(100u, mapping->GetLength()); | 
|  334  |  338  | 
|  335   // Send the shared buffer. |  339   // Send the shared buffer. | 
|  336   const std::string go1("go 1"); |  340   const std::string go1("go 1"); | 
|  337   DispatcherTransport transport( |  341   DispatcherTransport transport(test::HandleTryStartTransport(handle)); | 
|  338       test::DispatcherTryStartTransport(dispatcher.get())); |  | 
|  339   ASSERT_TRUE(transport.is_valid()); |  342   ASSERT_TRUE(transport.is_valid()); | 
|  340  |  343  | 
|  341   std::vector<DispatcherTransport> transports; |  344   std::vector<DispatcherTransport> transports; | 
|  342   transports.push_back(transport); |  345   transports.push_back(transport); | 
|  343   EXPECT_EQ(MOJO_RESULT_OK, |  346   EXPECT_EQ(MOJO_RESULT_OK, | 
|  344             mp->WriteMessage(0, UserPointer<const void>(&go1[0]), |  347             mp->WriteMessage(0, UserPointer<const void>(&go1[0]), | 
|  345                              static_cast<uint32_t>(go1.size()), &transports, |  348                              static_cast<uint32_t>(go1.size()), &transports, | 
|  346                              MOJO_WRITE_MESSAGE_FLAG_NONE)); |  349                              MOJO_WRITE_MESSAGE_FLAG_NONE)); | 
|  347   transport.End(); |  350   transport.End(); | 
|  348  |  351  | 
|  349   EXPECT_TRUE(dispatcher->HasOneRef()); |  352   EXPECT_TRUE(handle.dispatcher->HasOneRef()); | 
|  350   dispatcher = nullptr; |  353   handle.reset(); | 
|  351  |  354  | 
|  352   // Wait for a message from the child. |  355   // Wait for a message from the child. | 
|  353   HandleSignalsState hss; |  356   HandleSignalsState hss; | 
|  354   EXPECT_EQ(MOJO_RESULT_OK, |  357   EXPECT_EQ(MOJO_RESULT_OK, | 
|  355             test::WaitIfNecessary(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, &hss)); |  358             test::WaitIfNecessary(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, &hss)); | 
|  356   EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); |  359   EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 
|  357   EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); |  360   EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 
|  358  |  361  | 
|  359   std::string read_buffer(100, '\0'); |  362   std::string read_buffer(100, '\0'); | 
|  360   uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); |  363   uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); | 
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  457  |  460  | 
|  458 TEST_P(MultiprocessMessagePipeTestWithPipeCount, PlatformHandlePassing) { |  461 TEST_P(MultiprocessMessagePipeTestWithPipeCount, PlatformHandlePassing) { | 
|  459   test::ScopedTestDir test_dir; |  462   test::ScopedTestDir test_dir; | 
|  460  |  463  | 
|  461   helper()->StartChild("CheckPlatformHandleFile"); |  464   helper()->StartChild("CheckPlatformHandleFile"); | 
|  462  |  465  | 
|  463   RefPtr<ChannelEndpoint> ep; |  466   RefPtr<ChannelEndpoint> ep; | 
|  464   auto mp = MessagePipe::CreateLocalProxy(&ep); |  467   auto mp = MessagePipe::CreateLocalProxy(&ep); | 
|  465   Init(std::move(ep)); |  468   Init(std::move(ep)); | 
|  466  |  469  | 
|  467   std::vector<RefPtr<PlatformHandleDispatcher>> dispatchers; |  470   std::vector<Handle> handles; | 
|  468   std::vector<DispatcherTransport> transports; |  471   std::vector<DispatcherTransport> transports; | 
|  469  |  472  | 
|  470   size_t pipe_count = GetParam(); |  473   size_t pipe_count = GetParam(); | 
|  471   for (size_t i = 0; i < pipe_count; ++i) { |  474   for (size_t i = 0; i < pipe_count; ++i) { | 
|  472     util::ScopedFILE fp(test_dir.CreateFile()); |  475     util::ScopedFILE fp(test_dir.CreateFile()); | 
|  473     const std::string world("world"); |  476     const std::string world("world"); | 
|  474     CHECK_EQ(fwrite(&world[0], 1, world.size(), fp.get()), world.size()); |  477     CHECK_EQ(fwrite(&world[0], 1, world.size(), fp.get()), world.size()); | 
|  475     fflush(fp.get()); |  478     fflush(fp.get()); | 
|  476     rewind(fp.get()); |  479     rewind(fp.get()); | 
|  477  |  480  | 
|  478     auto dispatcher = PlatformHandleDispatcher::Create( |  481     Handle handle(PlatformHandleDispatcher::Create(ScopedPlatformHandle( | 
|  479         ScopedPlatformHandle(PlatformHandleFromFILE(std::move(fp)))); |  482                       PlatformHandleFromFILE(std::move(fp)))), | 
|  480     dispatchers.push_back(dispatcher); |  483                   MOJO_HANDLE_RIGHT_TRANSFER | MOJO_HANDLE_RIGHT_READ | | 
 |  484                       MOJO_HANDLE_RIGHT_WRITE); | 
 |  485     handles.push_back(std::move(handle)); | 
|  481     DispatcherTransport transport( |  486     DispatcherTransport transport( | 
|  482         test::DispatcherTryStartTransport(dispatcher.get())); |  487         test::HandleTryStartTransport(handles.back())); | 
|  483     ASSERT_TRUE(transport.is_valid()); |  488     ASSERT_TRUE(transport.is_valid()); | 
|  484     transports.push_back(transport); |  489     transports.push_back(transport); | 
|  485   } |  490   } | 
|  486  |  491  | 
|  487   char message[128]; |  492   char message[128]; | 
|  488   sprintf(message, "hello %d", static_cast<int>(pipe_count)); |  493   sprintf(message, "hello %d", static_cast<int>(pipe_count)); | 
|  489   EXPECT_EQ(MOJO_RESULT_OK, |  494   EXPECT_EQ(MOJO_RESULT_OK, | 
|  490             mp->WriteMessage(0, UserPointer<const void>(message), |  495             mp->WriteMessage(0, UserPointer<const void>(message), | 
|  491                              static_cast<uint32_t>(strlen(message)), |  496                              static_cast<uint32_t>(strlen(message)), | 
|  492                              &transports, MOJO_WRITE_MESSAGE_FLAG_NONE)); |  497                              &transports, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 
|  493  |  498  | 
|  494   for (size_t i = 0; i < pipe_count; ++i) { |  499   for (size_t i = 0; i < pipe_count; ++i) { | 
|  495     transports[i].End(); |  500     transports[i].End(); | 
|  496     EXPECT_TRUE(dispatchers[i]->HasOneRef()); |  501     EXPECT_TRUE(handles[i].dispatcher->HasOneRef()); | 
|  497   } |  502   } | 
|  498  |  503  | 
|  499   dispatchers.clear(); |  504   handles.clear(); | 
|  500  |  505  | 
|  501   // Wait for it to become readable, which should fail. |  506   // Wait for it to become readable, which should fail. | 
|  502   HandleSignalsState hss; |  507   HandleSignalsState hss; | 
|  503   EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |  508   EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 
|  504             test::WaitIfNecessary(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, &hss)); |  509             test::WaitIfNecessary(mp.get(), MOJO_HANDLE_SIGNAL_READABLE, &hss)); | 
|  505   EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |  510   EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 
|  506   EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |  511   EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 
|  507  |  512  | 
|  508   mp->Close(0); |  513   mp->Close(0); | 
|  509  |  514  | 
|  510   EXPECT_EQ(0, helper()->WaitForChildShutdown()); |  515   EXPECT_EQ(0, helper()->WaitForChildShutdown()); | 
|  511 } |  516 } | 
|  512  |  517  | 
|  513 // Android multi-process tests are not executing the new process. This is flaky. |  518 // Android multi-process tests are not executing the new process. This is flaky. | 
|  514 #if !defined(OS_ANDROID) |  519 #if !defined(OS_ANDROID) | 
|  515 INSTANTIATE_TEST_CASE_P(PipeCount, |  520 INSTANTIATE_TEST_CASE_P(PipeCount, | 
|  516                         MultiprocessMessagePipeTestWithPipeCount, |  521                         MultiprocessMessagePipeTestWithPipeCount, | 
|  517                         testing::Values(1u, 128u, 140u)); |  522                         testing::Values(1u, 128u, 140u)); | 
|  518 #endif |  523 #endif | 
|  519  |  524  | 
|  520 }  // namespace |  525 }  // namespace | 
|  521 }  // namespace system |  526 }  // namespace system | 
|  522 }  // namespace mojo |  527 }  // namespace mojo | 
| OLD | NEW |