[email protected] | 6486088 | 2014-08-04 23:44:17 | [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_channel_mojo.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 8 | #include <stdint.h> |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 9 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 10 | #include <memory> |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 11 | #include <utility> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 12 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 13 | #include "base/base_paths.h" |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 14 | #include "base/bind.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 15 | #include "base/files/file.h" |
amistry | 20e2b1d6 | 2016-06-23 06:12:35 | [diff] [blame] | 16 | #include "base/files/scoped_temp_dir.h" |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 17 | #include "base/location.h" |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 18 | #include "base/macros.h" |
| 19 | #include "base/message_loop/message_loop.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 20 | #include "base/path_service.h" |
| 21 | #include "base/pickle.h" |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 22 | #include "base/run_loop.h" |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 23 | #include "base/single_thread_task_runner.h" |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 24 | #include "base/strings/stringprintf.h" |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 25 | #include "base/test/test_io_thread.h" |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 26 | #include "base/test/test_timeouts.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 27 | #include "base/threading/thread.h" |
gab | f08ccc0 | 2016-05-11 18:51:11 | [diff] [blame] | 28 | #include "base/threading/thread_task_runner_handle.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 29 | #include "build/build_config.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 30 | #include "ipc/ipc_message.h" |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 31 | #include "ipc/ipc_mojo_handle_attachment.h" |
| 32 | #include "ipc/ipc_mojo_message_helper.h" |
| 33 | #include "ipc/ipc_mojo_param_traits.h" |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 34 | #include "ipc/ipc_test.mojom.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 35 | #include "ipc/ipc_test_base.h" |
| 36 | #include "ipc/ipc_test_channel_listener.h" |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 37 | #include "mojo/edk/test/mojo_test_base.h" |
| 38 | #include "mojo/edk/test/multiprocess_test_helper.h" |
| 39 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 40 | |
| 41 | #if defined(OS_POSIX) |
| 42 | #include "base/file_descriptor_posix.h" |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 43 | #include "ipc/ipc_platform_file_attachment_posix.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 44 | #endif |
| 45 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 46 | #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(client_name, test_base) \ |
| 47 | class client_name##_MainFixture : public test_base { \ |
| 48 | public: \ |
| 49 | void Main(); \ |
| 50 | }; \ |
| 51 | MULTIPROCESS_TEST_MAIN_WITH_SETUP( \ |
| 52 | client_name##TestChildMain, \ |
| 53 | ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { \ |
| 54 | CHECK(!mojo::edk::test::MultiprocessTestHelper::primordial_pipe_token \ |
| 55 | .empty()); \ |
| 56 | client_name##_MainFixture test; \ |
| 57 | test.Init(mojo::edk::CreateChildMessagePipe( \ |
| 58 | mojo::edk::test::MultiprocessTestHelper::primordial_pipe_token)); \ |
| 59 | test.Main(); \ |
| 60 | return (::testing::Test::HasFatalFailure() || \ |
| 61 | ::testing::Test::HasNonfatalFailure()) \ |
| 62 | ? 1 \ |
| 63 | : 0; \ |
| 64 | } \ |
| 65 | void client_name##_MainFixture::Main() |
| 66 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 67 | namespace { |
| 68 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 69 | void SendString(IPC::Sender* sender, const std::string& str) { |
| 70 | IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 71 | message->WriteString(str); |
| 72 | ASSERT_TRUE(sender->Send(message)); |
| 73 | } |
| 74 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 75 | class ListenerThatExpectsOK : public IPC::Listener { |
| 76 | public: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 77 | ListenerThatExpectsOK() : received_ok_(false) {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 78 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 79 | ~ListenerThatExpectsOK() override {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 80 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 81 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 82 | base::PickleIterator iter(message); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 83 | std::string should_be_ok; |
| 84 | EXPECT_TRUE(iter.ReadString(&should_be_ok)); |
| 85 | EXPECT_EQ(should_be_ok, "OK"); |
[email protected] | e5c2775 | 2014-08-08 21:45:13 | [diff] [blame] | 86 | received_ok_ = true; |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 87 | base::MessageLoop::current()->QuitWhenIdle(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 91 | void OnChannelError() override { |
[email protected] | e5c2775 | 2014-08-08 21:45:13 | [diff] [blame] | 92 | // The connection should be healthy while the listener is waiting |
| 93 | // message. An error can occur after that because the peer |
| 94 | // process dies. |
| 95 | DCHECK(received_ok_); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 96 | } |
| 97 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 98 | static void SendOK(IPC::Sender* sender) { SendString(sender, "OK"); } |
[email protected] | e5c2775 | 2014-08-08 21:45:13 | [diff] [blame] | 99 | |
| 100 | private: |
| 101 | bool received_ok_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 102 | }; |
| 103 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 104 | class ChannelClient { |
| 105 | public: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 106 | void Init(mojo::ScopedMessagePipeHandle handle) { |
| 107 | handle_ = std::move(handle); |
tsergeant | 89601286 | 2016-03-10 03:51:33 | [diff] [blame] | 108 | } |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 109 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 110 | void Connect(IPC::Listener* listener) { |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 111 | channel_ = IPC::ChannelMojo::Create( |
| 112 | std::move(handle_), IPC::Channel::MODE_CLIENT, listener, |
| 113 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 114 | CHECK(channel_->Connect()); |
| 115 | } |
| 116 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 117 | void Close() { |
| 118 | channel_->Close(); |
| 119 | |
| 120 | base::RunLoop run_loop; |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 121 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 122 | run_loop.QuitClosure()); |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 123 | run_loop.Run(); |
| 124 | } |
| 125 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 126 | IPC::ChannelMojo* channel() const { return channel_.get(); } |
| 127 | |
| 128 | private: |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 129 | base::MessageLoopForIO main_message_loop_; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 130 | mojo::ScopedMessagePipeHandle handle_; |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 131 | std::unique_ptr<IPC::ChannelMojo> channel_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 132 | }; |
| 133 | |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 134 | class IPCChannelMojoTestBase : public testing::Test { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 135 | public: |
| 136 | void InitWithMojo(const std::string& test_client_name) { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 137 | handle_ = helper_.StartChild(test_client_name); |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 138 | } |
| 139 | |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 140 | bool WaitForClientShutdown() { return helper_.WaitForChildTestShutdown(); } |
| 141 | |
| 142 | protected: |
| 143 | mojo::ScopedMessagePipeHandle TakeHandle() { return std::move(handle_); } |
| 144 | |
| 145 | private: |
| 146 | mojo::ScopedMessagePipeHandle handle_; |
| 147 | mojo::edk::test::MultiprocessTestHelper helper_; |
| 148 | }; |
| 149 | |
| 150 | class IPCChannelMojoTest : public IPCChannelMojoTestBase { |
| 151 | public: |
| 152 | void TearDown() override { base::RunLoop().RunUntilIdle(); } |
| 153 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 154 | void CreateChannel(IPC::Listener* listener) { |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 155 | channel_ = IPC::ChannelMojo::Create( |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 156 | TakeHandle(), IPC::Channel::MODE_SERVER, listener, |
| 157 | base::ThreadTaskRunnerHandle::Get()); |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 158 | } |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 159 | |
| 160 | bool ConnectChannel() { return channel_->Connect(); } |
| 161 | |
| 162 | void DestroyChannel() { channel_.reset(); } |
| 163 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 164 | IPC::Sender* sender() { return channel(); } |
| 165 | IPC::Channel* channel() { return channel_.get(); } |
| 166 | |
| 167 | private: |
| 168 | base::MessageLoop message_loop_; |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 169 | std::unique_ptr<IPC::Channel> channel_; |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 170 | }; |
| 171 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 172 | class TestChannelListenerWithExtraExpectations |
| 173 | : public IPC::TestChannelListener { |
| 174 | public: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 175 | TestChannelListenerWithExtraExpectations() : is_connected_called_(false) {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 176 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 177 | void OnChannelConnected(int32_t peer_pid) override { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 178 | IPC::TestChannelListener::OnChannelConnected(peer_pid); |
| 179 | EXPECT_TRUE(base::kNullProcessId != peer_pid); |
| 180 | is_connected_called_ = true; |
| 181 | } |
| 182 | |
| 183 | bool is_connected_called() const { return is_connected_called_; } |
| 184 | |
| 185 | private: |
| 186 | bool is_connected_called_; |
| 187 | }; |
| 188 | |
amistry | 0027a095 | 2016-05-03 00:52:47 | [diff] [blame] | 189 | TEST_F(IPCChannelMojoTest, ConnectedFromClient) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 190 | InitWithMojo("IPCChannelMojoTestClient"); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 191 | |
| 192 | // Set up IPC channel and start client. |
| 193 | TestChannelListenerWithExtraExpectations listener; |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 194 | CreateChannel(&listener); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 195 | listener.Init(sender()); |
| 196 | ASSERT_TRUE(ConnectChannel()); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 197 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 198 | IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent"); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 199 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 200 | base::RunLoop().Run(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 201 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 202 | channel()->Close(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 203 | |
| 204 | EXPECT_TRUE(WaitForClientShutdown()); |
| 205 | EXPECT_TRUE(listener.is_connected_called()); |
| 206 | EXPECT_TRUE(listener.HasSentAll()); |
| 207 | |
| 208 | DestroyChannel(); |
| 209 | } |
| 210 | |
| 211 | // A long running process that connects to us |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 212 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestClient, ChannelClient) { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 213 | TestChannelListenerWithExtraExpectations listener; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 214 | Connect(&listener); |
| 215 | listener.Init(channel()); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 216 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 217 | IPC::TestChannelListener::SendOneMessage(channel(), "hello from child"); |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 218 | base::RunLoop().Run(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 219 | EXPECT_TRUE(listener.is_connected_called()); |
| 220 | EXPECT_TRUE(listener.HasSentAll()); |
| 221 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 222 | Close(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 223 | } |
| 224 | |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 225 | class ListenerExpectingErrors : public IPC::Listener { |
| 226 | public: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 227 | ListenerExpectingErrors() : has_error_(false) {} |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 228 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 229 | void OnChannelConnected(int32_t peer_pid) override { |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 230 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | be6c4cc | 2014-09-24 23:38:44 | [diff] [blame] | 231 | } |
| 232 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 233 | bool OnMessageReceived(const IPC::Message& message) override { return true; } |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 234 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 235 | void OnChannelError() override { |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 236 | has_error_ = true; |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 237 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool has_error() const { return has_error_; } |
| 241 | |
| 242 | private: |
| 243 | bool has_error_; |
| 244 | }; |
| 245 | |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 246 | class ListenerThatQuits : public IPC::Listener { |
| 247 | public: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 248 | ListenerThatQuits() {} |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 249 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 250 | bool OnMessageReceived(const IPC::Message& message) override { return true; } |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 251 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 252 | void OnChannelConnected(int32_t peer_pid) override { |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 253 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 254 | } |
| 255 | }; |
| 256 | |
| 257 | // A long running process that connects to us. |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 258 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoErraticTestClient, |
| 259 | ChannelClient) { |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 260 | ListenerThatQuits listener; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 261 | Connect(&listener); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 262 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 263 | base::RunLoop().Run(); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 264 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 265 | Close(); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 266 | } |
| 267 | |
amistry | 0027a095 | 2016-05-03 00:52:47 | [diff] [blame] | 268 | TEST_F(IPCChannelMojoTest, SendFailWithPendingMessages) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 269 | InitWithMojo("IPCChannelMojoErraticTestClient"); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 270 | |
| 271 | // Set up IPC channel and start client. |
| 272 | ListenerExpectingErrors listener; |
| 273 | CreateChannel(&listener); |
| 274 | ASSERT_TRUE(ConnectChannel()); |
| 275 | |
jamesr | a03ae49 | 2014-10-03 04:26:48 | [diff] [blame] | 276 | // This matches a value in mojo/edk/system/constants.h |
morrita | be6c4cc | 2014-09-24 23:38:44 | [diff] [blame] | 277 | const int kMaxMessageNumBytes = 4 * 1024 * 1024; |
| 278 | std::string overly_large_data(kMaxMessageNumBytes, '*'); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 279 | // This messages are queued as pending. |
morrita | be6c4cc | 2014-09-24 23:38:44 | [diff] [blame] | 280 | for (size_t i = 0; i < 10; ++i) { |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 281 | IPC::TestChannelListener::SendOneMessage(sender(), |
| 282 | overly_large_data.c_str()); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 283 | } |
| 284 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 285 | base::RunLoop().Run(); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 286 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 287 | channel()->Close(); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 288 | |
| 289 | EXPECT_TRUE(WaitForClientShutdown()); |
| 290 | EXPECT_TRUE(listener.has_error()); |
| 291 | |
| 292 | DestroyChannel(); |
| 293 | } |
| 294 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 295 | struct TestingMessagePipe { |
| 296 | TestingMessagePipe() { |
| 297 | EXPECT_EQ(MOJO_RESULT_OK, mojo::CreateMessagePipe(nullptr, &self, &peer)); |
| 298 | } |
| 299 | |
| 300 | mojo::ScopedMessagePipeHandle self; |
| 301 | mojo::ScopedMessagePipeHandle peer; |
| 302 | }; |
| 303 | |
| 304 | class HandleSendingHelper { |
| 305 | public: |
| 306 | static std::string GetSendingFileContent() { return "Hello"; } |
| 307 | |
| 308 | static void WritePipe(IPC::Message* message, TestingMessagePipe* pipe) { |
| 309 | std::string content = HandleSendingHelper::GetSendingFileContent(); |
| 310 | EXPECT_EQ(MOJO_RESULT_OK, |
| 311 | mojo::WriteMessageRaw(pipe->self.get(), &content[0], |
| 312 | static_cast<uint32_t>(content.size()), |
| 313 | nullptr, 0, 0)); |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 314 | EXPECT_TRUE(IPC::MojoMessageHelper::WriteMessagePipeTo( |
| 315 | message, std::move(pipe->peer))); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void WritePipeThenSend(IPC::Sender* sender, TestingMessagePipe* pipe) { |
| 319 | IPC::Message* message = |
| 320 | new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 321 | WritePipe(message, pipe); |
| 322 | ASSERT_TRUE(sender->Send(message)); |
| 323 | } |
| 324 | |
| 325 | static void ReadReceivedPipe(const IPC::Message& message, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 326 | base::PickleIterator* iter) { |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 327 | mojo::ScopedMessagePipeHandle pipe; |
| 328 | EXPECT_TRUE( |
| 329 | IPC::MojoMessageHelper::ReadMessagePipeFrom(&message, iter, &pipe)); |
| 330 | std::string content(GetSendingFileContent().size(), ' '); |
| 331 | |
| 332 | uint32_t num_bytes = static_cast<uint32_t>(content.size()); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 333 | ASSERT_EQ(MOJO_RESULT_OK, |
| 334 | mojo::Wait(pipe.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 335 | MOJO_DEADLINE_INDEFINITE, nullptr)); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 336 | EXPECT_EQ(MOJO_RESULT_OK, |
| 337 | mojo::ReadMessageRaw(pipe.get(), &content[0], &num_bytes, nullptr, |
| 338 | nullptr, 0)); |
| 339 | EXPECT_EQ(content, GetSendingFileContent()); |
| 340 | } |
| 341 | |
| 342 | #if defined(OS_POSIX) |
amistry | 20e2b1d6 | 2016-06-23 06:12:35 | [diff] [blame] | 343 | static base::FilePath GetSendingFilePath(const base::FilePath& dir_path) { |
| 344 | return dir_path.Append("ListenerThatExpectsFile.txt"); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static void WriteFile(IPC::Message* message, base::File& file) { |
| 348 | std::string content = GetSendingFileContent(); |
| 349 | file.WriteAtCurrentPos(content.data(), content.size()); |
| 350 | file.Flush(); |
| 351 | message->WriteAttachment(new IPC::internal::PlatformFileAttachment( |
| 352 | base::ScopedFD(file.TakePlatformFile()))); |
| 353 | } |
| 354 | |
| 355 | static void WriteFileThenSend(IPC::Sender* sender, base::File& file) { |
| 356 | IPC::Message* message = |
| 357 | new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 358 | WriteFile(message, file); |
| 359 | ASSERT_TRUE(sender->Send(message)); |
| 360 | } |
| 361 | |
| 362 | static void WriteFileAndPipeThenSend(IPC::Sender* sender, |
| 363 | base::File& file, |
| 364 | TestingMessagePipe* pipe) { |
| 365 | IPC::Message* message = |
| 366 | new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 367 | WriteFile(message, file); |
| 368 | WritePipe(message, pipe); |
| 369 | ASSERT_TRUE(sender->Send(message)); |
| 370 | } |
| 371 | |
| 372 | static void ReadReceivedFile(const IPC::Message& message, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 373 | base::PickleIterator* iter) { |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 374 | base::ScopedFD fd; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 375 | scoped_refptr<base::Pickle::Attachment> attachment; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 376 | EXPECT_TRUE(message.ReadAttachment(iter, &attachment)); |
amistry | 980a61b | 2016-06-09 02:51:20 | [diff] [blame] | 377 | EXPECT_EQ(IPC::MessageAttachment::TYPE_PLATFORM_FILE, |
| 378 | static_cast<IPC::MessageAttachment*>(attachment.get()) |
| 379 | ->GetType()); |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 380 | base::File file(static_cast<IPC::MessageAttachment*>(attachment.get()) |
| 381 | ->TakePlatformFile()); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 382 | std::string content(GetSendingFileContent().size(), ' '); |
| 383 | file.Read(0, &content[0], content.size()); |
| 384 | EXPECT_EQ(content, GetSendingFileContent()); |
| 385 | } |
| 386 | #endif |
| 387 | }; |
| 388 | |
| 389 | class ListenerThatExpectsMessagePipe : public IPC::Listener { |
| 390 | public: |
| 391 | ListenerThatExpectsMessagePipe() : sender_(NULL) {} |
| 392 | |
| 393 | ~ListenerThatExpectsMessagePipe() override {} |
| 394 | |
| 395 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 396 | base::PickleIterator iter(message); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 397 | HandleSendingHelper::ReadReceivedPipe(message, &iter); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 398 | ListenerThatExpectsOK::SendOK(sender_); |
| 399 | return true; |
| 400 | } |
| 401 | |
amistry | 6c70caea | 2016-06-09 03:08:29 | [diff] [blame] | 402 | void OnChannelError() override { |
| 403 | base::MessageLoop::current()->QuitWhenIdle(); |
| 404 | } |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 405 | |
| 406 | void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 407 | |
| 408 | private: |
| 409 | IPC::Sender* sender_; |
| 410 | }; |
| 411 | |
amistry | 0027a095 | 2016-05-03 00:52:47 | [diff] [blame] | 412 | TEST_F(IPCChannelMojoTest, SendMessagePipe) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 413 | InitWithMojo("IPCChannelMojoTestSendMessagePipeClient"); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 414 | |
| 415 | ListenerThatExpectsOK listener; |
| 416 | CreateChannel(&listener); |
| 417 | ASSERT_TRUE(ConnectChannel()); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 418 | |
| 419 | TestingMessagePipe pipe; |
| 420 | HandleSendingHelper::WritePipeThenSend(channel(), &pipe); |
| 421 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 422 | base::RunLoop().Run(); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 423 | channel()->Close(); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 424 | |
| 425 | EXPECT_TRUE(WaitForClientShutdown()); |
| 426 | DestroyChannel(); |
| 427 | } |
| 428 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 429 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendMessagePipeClient, |
| 430 | ChannelClient) { |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 431 | ListenerThatExpectsMessagePipe listener; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 432 | Connect(&listener); |
| 433 | listener.set_sender(channel()); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 434 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 435 | base::RunLoop().Run(); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 436 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 437 | Close(); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 438 | } |
| 439 | |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 440 | void ReadOK(mojo::MessagePipeHandle pipe) { |
| 441 | std::string should_be_ok("xx"); |
| 442 | uint32_t num_bytes = static_cast<uint32_t>(should_be_ok.size()); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 443 | CHECK_EQ(MOJO_RESULT_OK, mojo::Wait(pipe, MOJO_HANDLE_SIGNAL_READABLE, |
| 444 | MOJO_DEADLINE_INDEFINITE, nullptr)); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 445 | CHECK_EQ(MOJO_RESULT_OK, |
| 446 | mojo::ReadMessageRaw(pipe, &should_be_ok[0], &num_bytes, nullptr, |
| 447 | nullptr, 0)); |
| 448 | EXPECT_EQ(should_be_ok, std::string("OK")); |
| 449 | } |
| 450 | |
| 451 | void WriteOK(mojo::MessagePipeHandle pipe) { |
| 452 | std::string ok("OK"); |
| 453 | CHECK_EQ(MOJO_RESULT_OK, |
| 454 | mojo::WriteMessageRaw(pipe, &ok[0], static_cast<uint32_t>(ok.size()), |
| 455 | nullptr, 0, 0)); |
| 456 | } |
| 457 | |
| 458 | class ListenerThatExpectsMessagePipeUsingParamTrait : public IPC::Listener { |
| 459 | public: |
| 460 | explicit ListenerThatExpectsMessagePipeUsingParamTrait(bool receiving_valid) |
| 461 | : sender_(NULL), receiving_valid_(receiving_valid) {} |
| 462 | |
| 463 | ~ListenerThatExpectsMessagePipeUsingParamTrait() override {} |
| 464 | |
| 465 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 466 | base::PickleIterator iter(message); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 467 | mojo::MessagePipeHandle handle; |
| 468 | EXPECT_TRUE(IPC::ParamTraits<mojo::MessagePipeHandle>::Read(&message, &iter, |
| 469 | &handle)); |
| 470 | EXPECT_EQ(handle.is_valid(), receiving_valid_); |
| 471 | if (receiving_valid_) { |
| 472 | ReadOK(handle); |
| 473 | MojoClose(handle.value()); |
| 474 | } |
| 475 | |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 476 | ListenerThatExpectsOK::SendOK(sender_); |
| 477 | return true; |
| 478 | } |
| 479 | |
amistry | 6c70caea | 2016-06-09 03:08:29 | [diff] [blame] | 480 | void OnChannelError() override { |
| 481 | base::MessageLoop::current()->QuitWhenIdle(); |
| 482 | } |
| 483 | |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 484 | void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 485 | |
| 486 | private: |
| 487 | IPC::Sender* sender_; |
| 488 | bool receiving_valid_; |
| 489 | }; |
| 490 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 491 | class ParamTraitMessagePipeClient : public ChannelClient { |
| 492 | public: |
| 493 | void RunTest(bool receiving_valid_handle) { |
| 494 | ListenerThatExpectsMessagePipeUsingParamTrait listener( |
| 495 | receiving_valid_handle); |
| 496 | Connect(&listener); |
| 497 | listener.set_sender(channel()); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 498 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 499 | base::RunLoop().Run(); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 500 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 501 | Close(); |
| 502 | } |
| 503 | }; |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 504 | |
amistry | 0027a095 | 2016-05-03 00:52:47 | [diff] [blame] | 505 | TEST_F(IPCChannelMojoTest, ParamTraitValidMessagePipe) { |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 506 | InitWithMojo("ParamTraitValidMessagePipeClient"); |
| 507 | |
| 508 | ListenerThatExpectsOK listener; |
| 509 | CreateChannel(&listener); |
| 510 | ASSERT_TRUE(ConnectChannel()); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 511 | |
| 512 | TestingMessagePipe pipe; |
| 513 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 514 | std::unique_ptr<IPC::Message> message(new IPC::Message()); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 515 | IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(), |
| 516 | pipe.peer.release()); |
| 517 | WriteOK(pipe.self.get()); |
| 518 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 519 | channel()->Send(message.release()); |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 520 | base::RunLoop().Run(); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 521 | channel()->Close(); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 522 | |
| 523 | EXPECT_TRUE(WaitForClientShutdown()); |
| 524 | DestroyChannel(); |
| 525 | } |
| 526 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 527 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ParamTraitValidMessagePipeClient, |
| 528 | ParamTraitMessagePipeClient) { |
| 529 | RunTest(true); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 530 | } |
| 531 | |
amistry | 0027a095 | 2016-05-03 00:52:47 | [diff] [blame] | 532 | TEST_F(IPCChannelMojoTest, ParamTraitInvalidMessagePipe) { |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 533 | InitWithMojo("ParamTraitInvalidMessagePipeClient"); |
| 534 | |
| 535 | ListenerThatExpectsOK listener; |
| 536 | CreateChannel(&listener); |
| 537 | ASSERT_TRUE(ConnectChannel()); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 538 | |
| 539 | mojo::MessagePipeHandle invalid_handle; |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 540 | std::unique_ptr<IPC::Message> message(new IPC::Message()); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 541 | IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(), |
| 542 | invalid_handle); |
| 543 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 544 | channel()->Send(message.release()); |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 545 | base::RunLoop().Run(); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 546 | channel()->Close(); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 547 | |
| 548 | EXPECT_TRUE(WaitForClientShutdown()); |
| 549 | DestroyChannel(); |
| 550 | } |
| 551 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 552 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ParamTraitInvalidMessagePipeClient, |
| 553 | ParamTraitMessagePipeClient) { |
| 554 | RunTest(false); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 555 | } |
| 556 | |
amistry | 0027a095 | 2016-05-03 00:52:47 | [diff] [blame] | 557 | TEST_F(IPCChannelMojoTest, SendFailAfterClose) { |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 558 | InitWithMojo("IPCChannelMojoTestSendOkClient"); |
| 559 | |
| 560 | ListenerThatExpectsOK listener; |
| 561 | CreateChannel(&listener); |
| 562 | ASSERT_TRUE(ConnectChannel()); |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 563 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 564 | base::RunLoop().Run(); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 565 | channel()->Close(); |
| 566 | ASSERT_FALSE(channel()->Send(new IPC::Message())); |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 567 | |
| 568 | EXPECT_TRUE(WaitForClientShutdown()); |
| 569 | DestroyChannel(); |
| 570 | } |
| 571 | |
| 572 | class ListenerSendingOneOk : public IPC::Listener { |
| 573 | public: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 574 | ListenerSendingOneOk() {} |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 575 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 576 | bool OnMessageReceived(const IPC::Message& message) override { return true; } |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 577 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 578 | void OnChannelConnected(int32_t peer_pid) override { |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 579 | ListenerThatExpectsOK::SendOK(sender_); |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 580 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 584 | |
| 585 | private: |
| 586 | IPC::Sender* sender_; |
| 587 | }; |
| 588 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 589 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendOkClient, |
| 590 | ChannelClient) { |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 591 | ListenerSendingOneOk listener; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 592 | Connect(&listener); |
| 593 | listener.set_sender(channel()); |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 594 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 595 | base::RunLoop().Run(); |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 596 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 597 | Close(); |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 598 | } |
| 599 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 600 | class ListenerWithSimpleAssociatedInterface |
| 601 | : public IPC::Listener, |
| 602 | public IPC::mojom::SimpleTestDriver { |
| 603 | public: |
| 604 | static const int kNumMessages; |
| 605 | |
| 606 | ListenerWithSimpleAssociatedInterface() : binding_(this) {} |
| 607 | |
| 608 | ~ListenerWithSimpleAssociatedInterface() override {} |
| 609 | |
| 610 | bool OnMessageReceived(const IPC::Message& message) override { |
| 611 | base::PickleIterator iter(message); |
| 612 | std::string should_be_expected; |
| 613 | EXPECT_TRUE(iter.ReadString(&should_be_expected)); |
| 614 | EXPECT_EQ(should_be_expected, next_expected_string_); |
| 615 | num_messages_received_++; |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | void OnChannelError() override { |
| 620 | DCHECK(received_quit_); |
| 621 | } |
| 622 | |
| 623 | void RegisterInterfaceFactory(IPC::Channel* channel) { |
| 624 | channel->GetAssociatedInterfaceSupport()->AddAssociatedInterface( |
| 625 | base::Bind(&ListenerWithSimpleAssociatedInterface::BindRequest, |
| 626 | base::Unretained(this))); |
| 627 | } |
| 628 | |
| 629 | private: |
| 630 | // IPC::mojom::SimpleTestDriver: |
| 631 | void ExpectString(const mojo::String& str) override { |
| 632 | next_expected_string_ = str; |
| 633 | } |
| 634 | |
| 635 | void RequestQuit(const RequestQuitCallback& callback) override { |
| 636 | EXPECT_EQ(kNumMessages, num_messages_received_); |
| 637 | received_quit_ = true; |
| 638 | callback.Run(); |
| 639 | base::MessageLoop::current()->QuitWhenIdle(); |
| 640 | } |
| 641 | |
| 642 | void BindRequest(IPC::mojom::SimpleTestDriverAssociatedRequest request) { |
| 643 | DCHECK(!binding_.is_bound()); |
| 644 | binding_.Bind(std::move(request)); |
| 645 | } |
| 646 | |
| 647 | std::string next_expected_string_; |
| 648 | int num_messages_received_ = 0; |
| 649 | bool received_quit_ = false; |
| 650 | |
| 651 | mojo::AssociatedBinding<IPC::mojom::SimpleTestDriver> binding_; |
| 652 | }; |
| 653 | |
| 654 | const int ListenerWithSimpleAssociatedInterface::kNumMessages = 1000; |
| 655 | |
| 656 | class ListenerSendingAssociatedMessages : public IPC::Listener { |
| 657 | public: |
| 658 | ListenerSendingAssociatedMessages() {} |
| 659 | |
| 660 | bool OnMessageReceived(const IPC::Message& message) override { return true; } |
| 661 | |
| 662 | void OnChannelConnected(int32_t peer_pid) override { |
| 663 | DCHECK(channel_); |
| 664 | channel_->GetAssociatedInterfaceSupport()->GetRemoteAssociatedInterface( |
| 665 | &driver_); |
| 666 | |
| 667 | // Send a bunch of interleaved messages, alternating between the associated |
| 668 | // interface and a legacy IPC::Message. |
| 669 | for (int i = 0; i < ListenerWithSimpleAssociatedInterface::kNumMessages; |
| 670 | ++i) { |
| 671 | std::string str = base::StringPrintf("Hello! %d", i); |
| 672 | driver_->ExpectString(str); |
| 673 | SendString(channel_, str); |
| 674 | } |
| 675 | driver_->RequestQuit(base::Bind(&OnQuitAck)); |
| 676 | } |
| 677 | |
| 678 | void set_channel(IPC::Channel* channel) { channel_ = channel; } |
| 679 | |
| 680 | private: |
| 681 | static void OnQuitAck() { base::MessageLoop::current()->QuitWhenIdle(); } |
| 682 | |
| 683 | IPC::Channel* channel_ = nullptr; |
| 684 | IPC::mojom::SimpleTestDriverAssociatedPtr driver_; |
| 685 | }; |
| 686 | |
| 687 | TEST_F(IPCChannelMojoTest, SimpleAssociatedInterface) { |
| 688 | InitWithMojo("SimpleAssociatedInterfaceClient"); |
| 689 | |
| 690 | ListenerWithSimpleAssociatedInterface listener; |
| 691 | CreateChannel(&listener); |
| 692 | ASSERT_TRUE(ConnectChannel()); |
| 693 | |
| 694 | listener.RegisterInterfaceFactory(channel()); |
| 695 | |
| 696 | base::RunLoop().Run(); |
| 697 | channel()->Close(); |
| 698 | |
| 699 | EXPECT_TRUE(WaitForClientShutdown()); |
| 700 | DestroyChannel(); |
| 701 | } |
| 702 | |
| 703 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(SimpleAssociatedInterfaceClient, |
| 704 | ChannelClient) { |
| 705 | ListenerSendingAssociatedMessages listener; |
| 706 | Connect(&listener); |
| 707 | listener.set_channel(channel()); |
| 708 | |
| 709 | base::RunLoop().Run(); |
| 710 | |
| 711 | Close(); |
| 712 | } |
| 713 | |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 714 | class ChannelProxyRunner { |
| 715 | public: |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 716 | ChannelProxyRunner(mojo::ScopedMessagePipeHandle handle, |
| 717 | bool for_server) |
| 718 | : for_server_(for_server), |
| 719 | handle_(std::move(handle)), |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 720 | io_thread_("ChannelProxyRunner IO thread") { |
| 721 | } |
| 722 | |
| 723 | void CreateProxy(IPC::Listener* listener) { |
| 724 | io_thread_.StartWithOptions( |
| 725 | base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 726 | proxy_.reset(new IPC::ChannelProxy(listener, io_thread_.task_runner())); |
| 727 | } |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 728 | |
| 729 | void RunProxy() { |
| 730 | std::unique_ptr<IPC::ChannelFactory> factory; |
| 731 | if (for_server_) { |
| 732 | factory = IPC::ChannelMojo::CreateServerFactory( |
| 733 | std::move(handle_), io_thread_.task_runner()); |
| 734 | } else { |
| 735 | factory = IPC::ChannelMojo::CreateClientFactory( |
| 736 | std::move(handle_), io_thread_.task_runner()); |
| 737 | } |
| 738 | proxy_->Init(std::move(factory), true); |
| 739 | } |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 740 | |
| 741 | IPC::ChannelProxy* proxy() { return proxy_.get(); } |
| 742 | |
| 743 | private: |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 744 | const bool for_server_; |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 745 | |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 746 | mojo::ScopedMessagePipeHandle handle_; |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 747 | base::Thread io_thread_; |
| 748 | std::unique_ptr<IPC::ChannelProxy> proxy_; |
| 749 | |
| 750 | DISALLOW_COPY_AND_ASSIGN(ChannelProxyRunner); |
| 751 | }; |
| 752 | |
| 753 | class IPCChannelProxyMojoTest : public IPCChannelMojoTestBase { |
| 754 | public: |
| 755 | void InitWithMojo(const std::string& client_name) { |
| 756 | IPCChannelMojoTestBase::InitWithMojo(client_name); |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 757 | runner_.reset(new ChannelProxyRunner(TakeHandle(), true)); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 758 | } |
| 759 | void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); } |
| 760 | void RunProxy() { runner_->RunProxy(); } |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame^] | 761 | void DestroyProxy() { |
| 762 | runner_.reset(); |
| 763 | base::RunLoop().RunUntilIdle(); |
| 764 | } |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 765 | |
| 766 | IPC::ChannelProxy* proxy() { return runner_->proxy(); } |
| 767 | |
| 768 | private: |
| 769 | base::MessageLoop message_loop_; |
| 770 | std::unique_ptr<ChannelProxyRunner> runner_; |
| 771 | }; |
| 772 | |
| 773 | class ListenerWithSimpleProxyAssociatedInterface |
| 774 | : public IPC::Listener, |
| 775 | public IPC::mojom::SimpleTestDriver { |
| 776 | public: |
| 777 | static const int kNumMessages; |
| 778 | |
| 779 | ListenerWithSimpleProxyAssociatedInterface() : binding_(this) {} |
| 780 | |
| 781 | ~ListenerWithSimpleProxyAssociatedInterface() override {} |
| 782 | |
| 783 | bool OnMessageReceived(const IPC::Message& message) override { |
| 784 | base::PickleIterator iter(message); |
| 785 | std::string should_be_expected; |
| 786 | EXPECT_TRUE(iter.ReadString(&should_be_expected)); |
| 787 | EXPECT_EQ(should_be_expected, next_expected_string_); |
| 788 | num_messages_received_++; |
| 789 | return true; |
| 790 | } |
| 791 | |
| 792 | void OnChannelError() override { |
| 793 | DCHECK(received_quit_); |
| 794 | } |
| 795 | |
| 796 | void RegisterInterfaceFactory(IPC::ChannelProxy* proxy) { |
| 797 | proxy->AddAssociatedInterface( |
| 798 | base::Bind(&ListenerWithSimpleProxyAssociatedInterface::BindRequest, |
| 799 | base::Unretained(this))); |
| 800 | } |
| 801 | |
| 802 | bool received_all_messages() const { |
| 803 | return num_messages_received_ == kNumMessages && received_quit_; |
| 804 | } |
| 805 | |
| 806 | private: |
| 807 | // IPC::mojom::SimpleTestDriver: |
| 808 | void ExpectString(const mojo::String& str) override { |
| 809 | next_expected_string_ = str; |
| 810 | } |
| 811 | |
| 812 | void RequestQuit(const RequestQuitCallback& callback) override { |
| 813 | received_quit_ = true; |
| 814 | callback.Run(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame^] | 815 | binding_.Close(); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 816 | base::MessageLoop::current()->QuitWhenIdle(); |
| 817 | } |
| 818 | |
| 819 | void BindRequest(IPC::mojom::SimpleTestDriverAssociatedRequest request) { |
| 820 | DCHECK(!binding_.is_bound()); |
| 821 | binding_.Bind(std::move(request)); |
| 822 | } |
| 823 | |
| 824 | std::string next_expected_string_; |
| 825 | int num_messages_received_ = 0; |
| 826 | bool received_quit_ = false; |
| 827 | |
| 828 | mojo::AssociatedBinding<IPC::mojom::SimpleTestDriver> binding_; |
| 829 | }; |
| 830 | |
| 831 | const int ListenerWithSimpleProxyAssociatedInterface::kNumMessages = 1000; |
| 832 | |
| 833 | TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterface) { |
| 834 | InitWithMojo("ProxyThreadAssociatedInterfaceClient"); |
| 835 | |
| 836 | ListenerWithSimpleProxyAssociatedInterface listener; |
| 837 | CreateProxy(&listener); |
| 838 | listener.RegisterInterfaceFactory(proxy()); |
| 839 | RunProxy(); |
| 840 | |
| 841 | base::RunLoop().Run(); |
| 842 | |
| 843 | EXPECT_TRUE(WaitForClientShutdown()); |
| 844 | EXPECT_TRUE(listener.received_all_messages()); |
| 845 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame^] | 846 | DestroyProxy(); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | class ChannelProxyClient { |
| 850 | public: |
| 851 | void Init(mojo::ScopedMessagePipeHandle handle) { |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 852 | runner_.reset(new ChannelProxyRunner(std::move(handle), false)); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 853 | } |
| 854 | void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); } |
| 855 | void RunProxy() { runner_->RunProxy(); } |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame^] | 856 | void DestroyProxy() { |
| 857 | runner_.reset(); |
| 858 | base::RunLoop().RunUntilIdle(); |
| 859 | } |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 860 | |
| 861 | IPC::ChannelProxy* proxy() { return runner_->proxy(); } |
| 862 | |
| 863 | private: |
| 864 | base::MessageLoop message_loop_; |
| 865 | std::unique_ptr<ChannelProxyRunner> runner_; |
| 866 | }; |
| 867 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame^] | 868 | class DummyListener : public IPC::Listener { |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 869 | public: |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 870 | // IPC::Listener |
| 871 | bool OnMessageReceived(const IPC::Message& message) override { return true; } |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 872 | }; |
| 873 | |
| 874 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ProxyThreadAssociatedInterfaceClient, |
| 875 | ChannelProxyClient) { |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame^] | 876 | DummyListener listener; |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 877 | CreateProxy(&listener); |
| 878 | RunProxy(); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 879 | |
| 880 | // Send a bunch of interleaved messages, alternating between the associated |
| 881 | // interface and a legacy IPC::Message. |
| 882 | IPC::mojom::SimpleTestDriverAssociatedPtr driver; |
| 883 | proxy()->GetRemoteAssociatedInterface(&driver); |
| 884 | for (int i = 0; i < ListenerWithSimpleProxyAssociatedInterface::kNumMessages; |
| 885 | ++i) { |
| 886 | std::string str = base::StringPrintf("Hello! %d", i); |
| 887 | driver->ExpectString(str); |
| 888 | SendString(proxy(), str); |
| 889 | } |
| 890 | driver->RequestQuit(base::MessageLoop::QuitWhenIdleClosure()); |
| 891 | base::RunLoop().Run(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame^] | 892 | |
| 893 | DestroyProxy(); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 894 | } |
| 895 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 896 | #if defined(OS_POSIX) |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 897 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 898 | class ListenerThatExpectsFile : public IPC::Listener { |
| 899 | public: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 900 | ListenerThatExpectsFile() : sender_(NULL) {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 901 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 902 | ~ListenerThatExpectsFile() override {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 903 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 904 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 905 | base::PickleIterator iter(message); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 906 | HandleSendingHelper::ReadReceivedFile(message, &iter); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 907 | ListenerThatExpectsOK::SendOK(sender_); |
| 908 | return true; |
| 909 | } |
| 910 | |
amistry | 6c70caea | 2016-06-09 03:08:29 | [diff] [blame] | 911 | void OnChannelError() override { |
| 912 | base::MessageLoop::current()->QuitWhenIdle(); |
| 913 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 914 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 915 | void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 916 | |
| 917 | private: |
| 918 | IPC::Sender* sender_; |
| 919 | }; |
| 920 | |
amistry | 0027a095 | 2016-05-03 00:52:47 | [diff] [blame] | 921 | TEST_F(IPCChannelMojoTest, SendPlatformHandle) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 922 | InitWithMojo("IPCChannelMojoTestSendPlatformHandleClient"); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 923 | |
| 924 | ListenerThatExpectsOK listener; |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 925 | CreateChannel(&listener); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 926 | ASSERT_TRUE(ConnectChannel()); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 927 | |
amistry | 20e2b1d6 | 2016-06-23 06:12:35 | [diff] [blame] | 928 | base::ScopedTempDir temp_dir; |
| 929 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 930 | base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.path()), |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 931 | base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 932 | base::File::FLAG_READ); |
| 933 | HandleSendingHelper::WriteFileThenSend(channel(), file); |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 934 | base::RunLoop().Run(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 935 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 936 | channel()->Close(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 937 | |
| 938 | EXPECT_TRUE(WaitForClientShutdown()); |
| 939 | DestroyChannel(); |
| 940 | } |
| 941 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 942 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendPlatformHandleClient, |
| 943 | ChannelClient) { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 944 | ListenerThatExpectsFile listener; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 945 | Connect(&listener); |
| 946 | listener.set_sender(channel()); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 947 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 948 | base::RunLoop().Run(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 949 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 950 | Close(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 951 | } |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 952 | |
| 953 | class ListenerThatExpectsFileAndPipe : public IPC::Listener { |
| 954 | public: |
| 955 | ListenerThatExpectsFileAndPipe() : sender_(NULL) {} |
| 956 | |
| 957 | ~ListenerThatExpectsFileAndPipe() override {} |
| 958 | |
| 959 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 960 | base::PickleIterator iter(message); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 961 | HandleSendingHelper::ReadReceivedFile(message, &iter); |
| 962 | HandleSendingHelper::ReadReceivedPipe(message, &iter); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 963 | ListenerThatExpectsOK::SendOK(sender_); |
| 964 | return true; |
| 965 | } |
| 966 | |
amistry | 6c70caea | 2016-06-09 03:08:29 | [diff] [blame] | 967 | void OnChannelError() override { |
| 968 | base::MessageLoop::current()->QuitWhenIdle(); |
| 969 | } |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 970 | |
| 971 | void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 972 | |
| 973 | private: |
| 974 | IPC::Sender* sender_; |
| 975 | }; |
| 976 | |
amistry | 0027a095 | 2016-05-03 00:52:47 | [diff] [blame] | 977 | TEST_F(IPCChannelMojoTest, SendPlatformHandleAndPipe) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 978 | InitWithMojo("IPCChannelMojoTestSendPlatformHandleAndPipeClient"); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 979 | |
| 980 | ListenerThatExpectsOK listener; |
| 981 | CreateChannel(&listener); |
| 982 | ASSERT_TRUE(ConnectChannel()); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 983 | |
amistry | 20e2b1d6 | 2016-06-23 06:12:35 | [diff] [blame] | 984 | base::ScopedTempDir temp_dir; |
| 985 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 986 | base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.path()), |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 987 | base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | |
| 988 | base::File::FLAG_READ); |
| 989 | TestingMessagePipe pipe; |
| 990 | HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe); |
| 991 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 992 | base::RunLoop().Run(); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 993 | channel()->Close(); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 994 | |
| 995 | EXPECT_TRUE(WaitForClientShutdown()); |
| 996 | DestroyChannel(); |
| 997 | } |
| 998 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 999 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT( |
| 1000 | IPCChannelMojoTestSendPlatformHandleAndPipeClient, |
| 1001 | ChannelClient) { |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 1002 | ListenerThatExpectsFileAndPipe listener; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1003 | Connect(&listener); |
| 1004 | listener.set_sender(channel()); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 1005 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 1006 | base::RunLoop().Run(); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 1007 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1008 | Close(); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 1009 | } |
| 1010 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 1011 | #endif // defined(OS_POSIX) |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 1012 | |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 1013 | #if defined(OS_LINUX) |
| 1014 | |
| 1015 | const base::ProcessId kMagicChildId = 54321; |
| 1016 | |
| 1017 | class ListenerThatVerifiesPeerPid : public IPC::Listener { |
| 1018 | public: |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1019 | void OnChannelConnected(int32_t peer_pid) override { |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 1020 | EXPECT_EQ(peer_pid, kMagicChildId); |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 1021 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | bool OnMessageReceived(const IPC::Message& message) override { |
| 1025 | NOTREACHED(); |
| 1026 | return true; |
| 1027 | } |
| 1028 | }; |
| 1029 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1030 | TEST_F(IPCChannelMojoTest, VerifyGlobalPid) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 1031 | InitWithMojo("IPCChannelMojoTestVerifyGlobalPidClient"); |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 1032 | |
| 1033 | ListenerThatVerifiesPeerPid listener; |
| 1034 | CreateChannel(&listener); |
| 1035 | ASSERT_TRUE(ConnectChannel()); |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 1036 | |
| 1037 | base::MessageLoop::current()->Run(); |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 1038 | channel()->Close(); |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 1039 | |
| 1040 | EXPECT_TRUE(WaitForClientShutdown()); |
| 1041 | DestroyChannel(); |
| 1042 | } |
| 1043 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1044 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestVerifyGlobalPidClient, |
| 1045 | ChannelClient) { |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 1046 | IPC::Channel::SetGlobalPid(kMagicChildId); |
| 1047 | ListenerThatQuits listener; |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1048 | Connect(&listener); |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 1049 | |
| 1050 | base::MessageLoop::current()->Run(); |
| 1051 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1052 | Close(); |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 1053 | } |
| 1054 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 1055 | #endif // OS_LINUX |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 1056 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 1057 | } // namespace |