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