| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/system/ipc_support.h" | 5 #include "mojo/edk/system/ipc_support.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 const char kConnectionIdFlag[] = "test-connection-id"; | 45 const char kConnectionIdFlag[] = "test-connection-id"; |
| 46 | 46 |
| 47 // Tests writing a message (containing just data) to |write_mp| and then reading | 47 // Tests writing a message (containing just data) to |write_mp| and then reading |
| 48 // it from |read_mp| (it should be the next message, i.e., there should be no | 48 // it from |read_mp| (it should be the next message, i.e., there should be no |
| 49 // other messages already enqueued in that direction). | 49 // other messages already enqueued in that direction). |
| 50 void TestWriteReadMessage(MessagePipeDispatcher* write_mp, | 50 void TestWriteReadMessage(MessagePipeDispatcher* write_mp, |
| 51 MessagePipeDispatcher* read_mp) { | 51 MessagePipeDispatcher* read_mp) { |
| 52 // Set up waiting on the read end first (to avoid racing). | 52 // Set up waiting on the read end first (to avoid racing). |
| 53 Waiter waiter; | 53 Waiter waiter; |
| 54 waiter.Init(); | 54 waiter.Init(); |
| 55 ASSERT_EQ( | 55 ASSERT_EQ(MOJO_RESULT_OK, |
| 56 MOJO_RESULT_OK, | 56 read_mp->AddAwakable(&waiter, 0, false, MOJO_HANDLE_SIGNAL_READABLE, |
| 57 read_mp->AddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 0, nullptr)); | 57 nullptr)); |
| 58 | 58 |
| 59 // Write a message with just 'x' through the write end. | 59 // Write a message with just 'x' through the write end. |
| 60 EXPECT_EQ(MOJO_RESULT_OK, | 60 EXPECT_EQ(MOJO_RESULT_OK, |
| 61 write_mp->WriteMessage(UserPointer<const void>("x"), 1, nullptr, | 61 write_mp->WriteMessage(UserPointer<const void>("x"), 1, nullptr, |
| 62 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 62 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 63 | 63 |
| 64 // Wait for it to arrive. | 64 // Wait for it to arrive. |
| 65 EXPECT_EQ(MOJO_RESULT_OK, | 65 EXPECT_EQ(MOJO_RESULT_OK, |
| 66 waiter.Wait(test::ActionTimeout(), nullptr, nullptr)); | 66 waiter.Wait(test::ActionTimeout(), nullptr, nullptr)); |
| 67 read_mp->RemoveAwakable(&waiter, nullptr); | 67 read_mp->RemoveAwakable(false, &waiter, 0, nullptr); |
| 68 | 68 |
| 69 // Read the message from the read end. | 69 // Read the message from the read end. |
| 70 char buffer[10] = {}; | 70 char buffer[10] = {}; |
| 71 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 71 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
| 72 EXPECT_EQ(MOJO_RESULT_OK, | 72 EXPECT_EQ(MOJO_RESULT_OK, |
| 73 read_mp->ReadMessage(UserPointer<void>(buffer), | 73 read_mp->ReadMessage(UserPointer<void>(buffer), |
| 74 MakeUserPointer(&buffer_size), 0, nullptr, | 74 MakeUserPointer(&buffer_size), 0, nullptr, |
| 75 MOJO_READ_MESSAGE_FLAG_NONE)); | 75 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 76 EXPECT_EQ(1u, buffer_size); | 76 EXPECT_EQ(1u, buffer_size); |
| 77 EXPECT_EQ('x', buffer[0]); | 77 EXPECT_EQ('x', buffer[0]); |
| 78 } | 78 } |
| 79 | 79 |
| 80 // Writes a message pipe dispatcher (in a message) to |write_mp| and reads it | 80 // Writes a message pipe dispatcher (in a message) to |write_mp| and reads it |
| 81 // from |read_mp| (it should be the next message, i.e., there should be no other | 81 // from |read_mp| (it should be the next message, i.e., there should be no other |
| 82 // other messages already enqueued in that direction). | 82 // other messages already enqueued in that direction). |
| 83 // TODO(vtl): Probably |mp_to_send| should be a |Handle|, and so should the | 83 // TODO(vtl): Probably |mp_to_send| should be a |Handle|, and so should the |
| 84 // return value. | 84 // return value. |
| 85 RefPtr<MessagePipeDispatcher> SendMessagePipeDispatcher( | 85 RefPtr<MessagePipeDispatcher> SendMessagePipeDispatcher( |
| 86 MessagePipeDispatcher* write_mp, | 86 MessagePipeDispatcher* write_mp, |
| 87 MessagePipeDispatcher* read_mp, | 87 MessagePipeDispatcher* read_mp, |
| 88 RefPtr<MessagePipeDispatcher>&& mp_to_send) { | 88 RefPtr<MessagePipeDispatcher>&& mp_to_send) { |
| 89 CHECK_NE(mp_to_send.get(), write_mp); | 89 CHECK_NE(mp_to_send.get(), write_mp); |
| 90 CHECK_NE(mp_to_send.get(), read_mp); | 90 CHECK_NE(mp_to_send.get(), read_mp); |
| 91 Handle mp_handle_to_send(std::move(mp_to_send), | 91 Handle mp_handle_to_send(std::move(mp_to_send), |
| 92 MessagePipeDispatcher::kDefaultHandleRights); | 92 MessagePipeDispatcher::kDefaultHandleRights); |
| 93 | 93 |
| 94 // Set up waiting on the read end first (to avoid racing). | 94 // Set up waiting on the read end first (to avoid racing). |
| 95 Waiter waiter; | 95 Waiter waiter; |
| 96 waiter.Init(); | 96 waiter.Init(); |
| 97 CHECK_EQ( | 97 CHECK_EQ(read_mp->AddAwakable(&waiter, 0, false, MOJO_HANDLE_SIGNAL_READABLE, |
| 98 read_mp->AddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 0, nullptr), | 98 nullptr), |
| 99 MOJO_RESULT_OK); | 99 MOJO_RESULT_OK); |
| 100 | 100 |
| 101 // Write a message with just |mp_handle_to_send| through the write end. | 101 // Write a message with just |mp_handle_to_send| through the write end. |
| 102 HandleTransport transport(test::HandleTryStartTransport(mp_handle_to_send)); | 102 HandleTransport transport(test::HandleTryStartTransport(mp_handle_to_send)); |
| 103 CHECK(transport.is_valid()); | 103 CHECK(transport.is_valid()); |
| 104 std::vector<HandleTransport> transports; | 104 std::vector<HandleTransport> transports; |
| 105 transports.push_back(transport); | 105 transports.push_back(transport); |
| 106 CHECK_EQ(write_mp->WriteMessage(NullUserPointer(), 0, &transports, | 106 CHECK_EQ(write_mp->WriteMessage(NullUserPointer(), 0, &transports, |
| 107 MOJO_WRITE_MESSAGE_FLAG_NONE), | 107 MOJO_WRITE_MESSAGE_FLAG_NONE), |
| 108 MOJO_RESULT_OK); | 108 MOJO_RESULT_OK); |
| 109 transport.End(); | 109 transport.End(); |
| 110 mp_handle_to_send.reset(); | 110 mp_handle_to_send.reset(); |
| 111 | 111 |
| 112 // Wait for it to arrive. | 112 // Wait for it to arrive. |
| 113 CHECK_EQ(waiter.Wait(test::ActionTimeout(), nullptr, nullptr), | 113 CHECK_EQ(waiter.Wait(test::ActionTimeout(), nullptr, nullptr), |
| 114 MOJO_RESULT_OK); | 114 MOJO_RESULT_OK); |
| 115 read_mp->RemoveAwakable(&waiter, nullptr); | 115 read_mp->RemoveAwakable(false, &waiter, 0, nullptr); |
| 116 | 116 |
| 117 // Read the message from the read end. | 117 // Read the message from the read end. |
| 118 HandleVector handles; | 118 HandleVector handles; |
| 119 uint32_t num_handles = 10; | 119 uint32_t num_handles = 10; |
| 120 CHECK_EQ(read_mp->ReadMessage(NullUserPointer(), NullUserPointer(), &handles, | 120 CHECK_EQ(read_mp->ReadMessage(NullUserPointer(), NullUserPointer(), &handles, |
| 121 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE), | 121 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE), |
| 122 MOJO_RESULT_OK); | 122 MOJO_RESULT_OK); |
| 123 CHECK_EQ(handles.size(), 1u); | 123 CHECK_EQ(handles.size(), 1u); |
| 124 CHECK_EQ(num_handles, 1u); | 124 CHECK_EQ(num_handles, 1u); |
| 125 CHECK_EQ(handles[0].dispatcher->GetType(), Dispatcher::Type::MESSAGE_PIPE); | 125 CHECK_EQ(handles[0].dispatcher->GetType(), Dispatcher::Type::MESSAGE_PIPE); |
| (...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 718 | 718 |
| 719 test_io_thread.PostTaskAndWait( | 719 test_io_thread.PostTaskAndWait( |
| 720 [&ipc_support]() { ipc_support.ShutdownOnIOThread(); }); | 720 [&ipc_support]() { ipc_support.ShutdownOnIOThread(); }); |
| 721 } | 721 } |
| 722 | 722 |
| 723 // TODO(vtl): Also test the case of the master "dying" before the slave. (The | 723 // TODO(vtl): Also test the case of the master "dying" before the slave. (The |
| 724 // slave should get OnMasterDisconnect(), which we currently don't test.) | 724 // slave should get OnMasterDisconnect(), which we currently don't test.) |
| 725 | 725 |
| 726 } // namespace system | 726 } // namespace system |
| 727 } // namespace mojo | 727 } // namespace mojo |
| OLD | NEW |