morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 5 | #include "ipc/ipc_mojo_bootstrap.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stdint.h> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 8 | #include <memory> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 9 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 10 | #include "base/base_paths.h" |
| 11 | #include "base/files/file.h" |
| 12 | #include "base/message_loop/message_loop.h" |
rockot | cf1d7d0 | 2016-11-22 05:27:27 | [diff] [blame] | 13 | #include "base/run_loop.h" |
gab | f08ccc0 | 2016-05-11 18:51:11 | [diff] [blame] | 14 | #include "base/threading/thread_task_runner_handle.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 15 | #include "build/build_config.h" |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 16 | #include "ipc/ipc.mojom.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 17 | #include "ipc/ipc_test_base.h" |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 18 | #include "mojo/edk/embedder/embedder.h" |
| 19 | #include "mojo/edk/test/mojo_test_base.h" |
| 20 | #include "mojo/edk/test/multiprocess_test_helper.h" |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 21 | #include "mojo/public/cpp/bindings/associated_binding.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 22 | |
| 23 | #if defined(OS_POSIX) |
| 24 | #include "base/file_descriptor_posix.h" |
| 25 | #endif |
| 26 | |
| 27 | namespace { |
| 28 | |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 29 | constexpr int32_t kTestServerPid = 42; |
| 30 | constexpr int32_t kTestClientPid = 4242; |
| 31 | |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame^] | 32 | class Connection { |
| 33 | public: |
| 34 | explicit Connection(std::unique_ptr<IPC::MojoBootstrap> bootstrap, |
| 35 | int32_t sender_id) |
| 36 | : bootstrap_(std::move(bootstrap)) { |
| 37 | bootstrap_->Connect(&sender_, &receiver_); |
| 38 | sender_->SetPeerPid(sender_id); |
| 39 | } |
| 40 | |
| 41 | void TakeReceiver(IPC::mojom::ChannelAssociatedRequest* receiver) { |
| 42 | *receiver = std::move(receiver_); |
| 43 | } |
| 44 | |
| 45 | IPC::mojom::ChannelAssociatedPtr& GetSender() { return sender_; } |
| 46 | |
| 47 | private: |
| 48 | IPC::mojom::ChannelAssociatedPtr sender_; |
| 49 | IPC::mojom::ChannelAssociatedRequest receiver_; |
| 50 | std::unique_ptr<IPC::MojoBootstrap> bootstrap_; |
| 51 | }; |
| 52 | |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 53 | class PeerPidReceiver : public IPC::mojom::Channel { |
| 54 | public: |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame^] | 55 | enum class MessageExpectation { |
| 56 | kNotExpected, |
| 57 | kExpectedValid, |
| 58 | kExptectedInvalid |
| 59 | }; |
| 60 | |
| 61 | PeerPidReceiver( |
| 62 | IPC::mojom::ChannelAssociatedRequest request, |
| 63 | const base::Closure& on_peer_pid_set, |
| 64 | MessageExpectation message_expectation = MessageExpectation::kNotExpected) |
| 65 | : binding_(this, std::move(request)), |
| 66 | on_peer_pid_set_(on_peer_pid_set), |
| 67 | message_expectation_(message_expectation) {} |
| 68 | ~PeerPidReceiver() override {} |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 69 | |
| 70 | // mojom::Channel: |
| 71 | void SetPeerPid(int32_t pid) override { |
| 72 | peer_pid_ = pid; |
| 73 | on_peer_pid_set_.Run(); |
| 74 | } |
| 75 | |
Yuzhu Shen | a0a2b36 | 2017-08-25 22:31:39 | [diff] [blame] | 76 | void Receive(base::span<const uint8_t> data, |
Ken Rockot | fd90763 | 2017-09-14 04:23:41 | [diff] [blame] | 77 | base::Optional<std::vector<mojo::native::SerializedHandlePtr>> |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame^] | 78 | handles) override { |
| 79 | ASSERT_NE(MessageExpectation::kNotExpected, message_expectation_); |
| 80 | |
| 81 | IPC::Message message(reinterpret_cast<const char*>(data.data()), |
| 82 | static_cast<uint32_t>(data.size())); |
| 83 | bool expected_valid = |
| 84 | message_expectation_ == MessageExpectation::kExpectedValid; |
| 85 | EXPECT_EQ(expected_valid, message.IsValid()); |
| 86 | } |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 87 | |
| 88 | void GetAssociatedInterface( |
| 89 | const std::string& name, |
| 90 | IPC::mojom::GenericInterfaceAssociatedRequest request) override {} |
| 91 | |
| 92 | int32_t peer_pid() const { return peer_pid_; } |
| 93 | |
| 94 | private: |
| 95 | mojo::AssociatedBinding<IPC::mojom::Channel> binding_; |
| 96 | const base::Closure on_peer_pid_set_; |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame^] | 97 | MessageExpectation message_expectation_; |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 98 | int32_t peer_pid_ = -1; |
| 99 | |
| 100 | DISALLOW_COPY_AND_ASSIGN(PeerPidReceiver); |
| 101 | }; |
| 102 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 103 | class IPCMojoBootstrapTest : public testing::Test { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 104 | protected: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 105 | mojo::edk::test::MultiprocessTestHelper helper_; |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 106 | }; |
| 107 | |
amistry | 6de2ee4f | 2016-05-05 05:12:09 | [diff] [blame] | 108 | TEST_F(IPCMojoBootstrapTest, Connect) { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 109 | base::MessageLoop message_loop; |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame^] | 110 | Connection connection( |
| 111 | IPC::MojoBootstrap::Create( |
| 112 | helper_.StartChild("IPCMojoBootstrapTestClient"), |
| 113 | IPC::Channel::MODE_SERVER, base::ThreadTaskRunnerHandle::Get(), |
| 114 | base::ThreadTaskRunnerHandle::Get()), |
| 115 | kTestServerPid); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 116 | |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 117 | IPC::mojom::ChannelAssociatedRequest receiver; |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame^] | 118 | connection.TakeReceiver(&receiver); |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 119 | |
| 120 | base::RunLoop run_loop; |
| 121 | PeerPidReceiver impl(std::move(receiver), run_loop.QuitClosure()); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 122 | run_loop.Run(); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 123 | |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 124 | EXPECT_EQ(kTestClientPid, impl.peer_pid()); |
| 125 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 126 | EXPECT_TRUE(helper_.WaitForChildTestShutdown()); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 127 | } |
| 128 | |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame^] | 129 | TEST_F(IPCMojoBootstrapTest, ReceiveEmptyMessage) { |
| 130 | base::MessageLoop message_loop; |
| 131 | Connection connection( |
| 132 | IPC::MojoBootstrap::Create( |
| 133 | helper_.StartChild("IPCMojoBootstrapTestEmptyMessage"), |
| 134 | IPC::Channel::MODE_SERVER, base::ThreadTaskRunnerHandle::Get(), |
| 135 | base::ThreadTaskRunnerHandle::Get()), |
| 136 | kTestServerPid); |
| 137 | |
| 138 | IPC::mojom::ChannelAssociatedRequest receiver; |
| 139 | connection.TakeReceiver(&receiver); |
| 140 | |
| 141 | base::RunLoop run_loop; |
| 142 | PeerPidReceiver impl(std::move(receiver), run_loop.QuitClosure(), |
| 143 | PeerPidReceiver::MessageExpectation::kExptectedInvalid); |
| 144 | run_loop.Run(); |
| 145 | |
| 146 | EXPECT_TRUE(helper_.WaitForChildTestShutdown()); |
| 147 | } |
| 148 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 149 | // A long running process that connects to us. |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 150 | MULTIPROCESS_TEST_MAIN_WITH_SETUP( |
| 151 | IPCMojoBootstrapTestClientTestChildMain, |
| 152 | ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 153 | base::MessageLoop message_loop; |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame^] | 154 | Connection connection( |
| 155 | IPC::MojoBootstrap::Create( |
| 156 | std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe), |
| 157 | IPC::Channel::MODE_CLIENT, base::ThreadTaskRunnerHandle::Get(), |
| 158 | base::ThreadTaskRunnerHandle::Get()), |
| 159 | kTestClientPid); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 160 | |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 161 | IPC::mojom::ChannelAssociatedRequest receiver; |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame^] | 162 | connection.TakeReceiver(&receiver); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 163 | |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 164 | base::RunLoop run_loop; |
| 165 | PeerPidReceiver impl(std::move(receiver), run_loop.QuitClosure()); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 166 | run_loop.Run(); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 167 | |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 168 | EXPECT_EQ(kTestServerPid, impl.peer_pid()); |
| 169 | |
| 170 | return 0; |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 171 | } |
| 172 | |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame^] | 173 | // A long running process that connects to us. |
| 174 | MULTIPROCESS_TEST_MAIN_WITH_SETUP( |
| 175 | IPCMojoBootstrapTestEmptyMessageTestChildMain, |
| 176 | ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { |
| 177 | base::MessageLoop message_loop; |
| 178 | Connection connection( |
| 179 | IPC::MojoBootstrap::Create( |
| 180 | std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe), |
| 181 | IPC::Channel::MODE_CLIENT, base::ThreadTaskRunnerHandle::Get(), |
| 182 | base::ThreadTaskRunnerHandle::Get()), |
| 183 | kTestClientPid); |
| 184 | |
| 185 | IPC::mojom::ChannelAssociatedRequest receiver; |
| 186 | connection.TakeReceiver(&receiver); |
| 187 | auto& sender = connection.GetSender(); |
| 188 | |
| 189 | uint8_t data = 0; |
| 190 | sender->Receive(base::make_span(&data, 0), {}); |
| 191 | |
| 192 | base::RunLoop run_loop; |
| 193 | PeerPidReceiver impl(std::move(receiver), run_loop.QuitClosure()); |
| 194 | run_loop.Run(); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 199 | } // namespace |