blob: f7652e7156c6d2eab2c4480e58a0a7c511ba07f9 [file] [log] [blame]
[email protected]64860882014-08-04 23:44:171// 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
amistryd4aa70d2016-06-23 07:52:375#include "ipc/ipc_channel_mojo.h"
[email protected]64860882014-08-04 23:44:176
avi246998d82015-12-22 02:39:047#include <stddef.h>
tfarina10a5c062015-09-04 18:47:578#include <stdint.h>
rockot7c6bf952016-07-14 00:34:119
danakj03de39b22016-04-23 04:21:0910#include <memory>
dchenge48600452015-12-28 02:24:5011#include <utility>
tfarina10a5c062015-09-04 18:47:5712
[email protected]64860882014-08-04 23:44:1713#include "base/base_paths.h"
rockot8d890f62016-07-14 16:37:1414#include "base/bind.h"
[email protected]64860882014-08-04 23:44:1715#include "base/files/file.h"
amistry20e2b1d62016-06-23 06:12:3516#include "base/files/scoped_temp_dir.h"
skyostile687bdff2015-05-12 11:29:2117#include "base/location.h"
rockot8d890f62016-07-14 16:37:1418#include "base/macros.h"
19#include "base/message_loop/message_loop.h"
[email protected]64860882014-08-04 23:44:1720#include "base/path_service.h"
21#include "base/pickle.h"
rockotcbca72f2015-03-03 16:31:0422#include "base/run_loop.h"
skyostile687bdff2015-05-12 11:29:2123#include "base/single_thread_task_runner.h"
rockot7c6bf952016-07-14 00:34:1124#include "base/strings/stringprintf.h"
sammc57ed9f982016-03-10 06:28:3525#include "base/test/test_io_thread.h"
morrita0bd20bd2015-02-25 20:11:2726#include "base/test/test_timeouts.h"
[email protected]64860882014-08-04 23:44:1727#include "base/threading/thread.h"
gabf08ccc02016-05-11 18:51:1128#include "base/threading/thread_task_runner_handle.h"
avi246998d82015-12-22 02:39:0429#include "build/build_config.h"
[email protected]64860882014-08-04 23:44:1730#include "ipc/ipc_message.h"
amistryd4aa70d2016-06-23 07:52:3731#include "ipc/ipc_mojo_handle_attachment.h"
32#include "ipc/ipc_mojo_message_helper.h"
33#include "ipc/ipc_mojo_param_traits.h"
rockot7c6bf952016-07-14 00:34:1134#include "ipc/ipc_test.mojom.h"
[email protected]64860882014-08-04 23:44:1735#include "ipc/ipc_test_base.h"
36#include "ipc/ipc_test_channel_listener.h"
sammc57ed9f982016-03-10 06:28:3537#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]64860882014-08-04 23:44:1740
41#if defined(OS_POSIX)
42#include "base/file_descriptor_posix.h"
morrita1aa788c2015-01-31 05:45:4243#include "ipc/ipc_platform_file_attachment_posix.h"
[email protected]64860882014-08-04 23:44:1744#endif
45
sammc57ed9f982016-03-10 06:28:3546#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]64860882014-08-04 23:44:1767namespace {
68
rockot7c6bf952016-07-14 00:34:1169void 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]64860882014-08-04 23:44:1775class ListenerThatExpectsOK : public IPC::Listener {
76 public:
sammc57ed9f982016-03-10 06:28:3577 ListenerThatExpectsOK() : received_ok_(false) {}
[email protected]64860882014-08-04 23:44:1778
dchengfe61fca2014-10-22 02:29:5279 ~ListenerThatExpectsOK() override {}
[email protected]64860882014-08-04 23:44:1780
dchengfe61fca2014-10-22 02:29:5281 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:2582 base::PickleIterator iter(message);
[email protected]64860882014-08-04 23:44:1783 std::string should_be_ok;
84 EXPECT_TRUE(iter.ReadString(&should_be_ok));
85 EXPECT_EQ(should_be_ok, "OK");
[email protected]e5c27752014-08-08 21:45:1386 received_ok_ = true;
ki.stfua21ed8c2015-10-12 17:26:0087 base::MessageLoop::current()->QuitWhenIdle();
[email protected]64860882014-08-04 23:44:1788 return true;
89 }
90
dchengfe61fca2014-10-22 02:29:5291 void OnChannelError() override {
[email protected]e5c27752014-08-08 21:45:1392 // 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]64860882014-08-04 23:44:1796 }
97
rockot7c6bf952016-07-14 00:34:1198 static void SendOK(IPC::Sender* sender) { SendString(sender, "OK"); }
[email protected]e5c27752014-08-08 21:45:1399
100 private:
101 bool received_ok_;
[email protected]64860882014-08-04 23:44:17102};
103
[email protected]64860882014-08-04 23:44:17104class ChannelClient {
105 public:
sammc57ed9f982016-03-10 06:28:35106 void Init(mojo::ScopedMessagePipeHandle handle) {
107 handle_ = std::move(handle);
tsergeant896012862016-03-10 03:51:33108 }
rockot8d890f62016-07-14 16:37:14109
sammc57ed9f982016-03-10 06:28:35110 void Connect(IPC::Listener* listener) {
rockota34707ca2016-07-20 04:28:32111 channel_ = IPC::ChannelMojo::Create(
112 std::move(handle_), IPC::Channel::MODE_CLIENT, listener,
113 base::ThreadTaskRunnerHandle::Get());
[email protected]64860882014-08-04 23:44:17114 CHECK(channel_->Connect());
115 }
116
rockotcbca72f2015-03-03 16:31:04117 void Close() {
118 channel_->Close();
119
120 base::RunLoop run_loop;
skyostile687bdff2015-05-12 11:29:21121 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
122 run_loop.QuitClosure());
rockotcbca72f2015-03-03 16:31:04123 run_loop.Run();
124 }
125
[email protected]64860882014-08-04 23:44:17126 IPC::ChannelMojo* channel() const { return channel_.get(); }
127
128 private:
[email protected]64860882014-08-04 23:44:17129 base::MessageLoopForIO main_message_loop_;
sammc57ed9f982016-03-10 06:28:35130 mojo::ScopedMessagePipeHandle handle_;
danakj03de39b22016-04-23 04:21:09131 std::unique_ptr<IPC::ChannelMojo> channel_;
[email protected]64860882014-08-04 23:44:17132};
133
rockot8d890f62016-07-14 16:37:14134class IPCChannelMojoTestBase : public testing::Test {
rockotcbca72f2015-03-03 16:31:04135 public:
136 void InitWithMojo(const std::string& test_client_name) {
sammc57ed9f982016-03-10 06:28:35137 handle_ = helper_.StartChild(test_client_name);
rockotcbca72f2015-03-03 16:31:04138 }
139
rockot8d890f62016-07-14 16:37:14140 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
150class IPCChannelMojoTest : public IPCChannelMojoTestBase {
151 public:
152 void TearDown() override { base::RunLoop().RunUntilIdle(); }
153
sammc57ed9f982016-03-10 06:28:35154 void CreateChannel(IPC::Listener* listener) {
rockot8d890f62016-07-14 16:37:14155 channel_ = IPC::ChannelMojo::Create(
rockota34707ca2016-07-20 04:28:32156 TakeHandle(), IPC::Channel::MODE_SERVER, listener,
157 base::ThreadTaskRunnerHandle::Get());
rockotcbca72f2015-03-03 16:31:04158 }
sammc57ed9f982016-03-10 06:28:35159
160 bool ConnectChannel() { return channel_->Connect(); }
161
162 void DestroyChannel() { channel_.reset(); }
163
sammc57ed9f982016-03-10 06:28:35164 IPC::Sender* sender() { return channel(); }
165 IPC::Channel* channel() { return channel_.get(); }
166
167 private:
168 base::MessageLoop message_loop_;
danakj03de39b22016-04-23 04:21:09169 std::unique_ptr<IPC::Channel> channel_;
rockotcbca72f2015-03-03 16:31:04170};
171
[email protected]64860882014-08-04 23:44:17172class TestChannelListenerWithExtraExpectations
173 : public IPC::TestChannelListener {
174 public:
sammc57ed9f982016-03-10 06:28:35175 TestChannelListenerWithExtraExpectations() : is_connected_called_(false) {}
[email protected]64860882014-08-04 23:44:17176
tfarina10a5c062015-09-04 18:47:57177 void OnChannelConnected(int32_t peer_pid) override {
[email protected]64860882014-08-04 23:44:17178 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
amistry0027a0952016-05-03 00:52:47189TEST_F(IPCChannelMojoTest, ConnectedFromClient) {
rockotcbca72f2015-03-03 16:31:04190 InitWithMojo("IPCChannelMojoTestClient");
[email protected]64860882014-08-04 23:44:17191
192 // Set up IPC channel and start client.
193 TestChannelListenerWithExtraExpectations listener;
morrita373af03b2014-09-09 19:35:24194 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17195 listener.Init(sender());
196 ASSERT_TRUE(ConnectChannel());
[email protected]64860882014-08-04 23:44:17197
sammc57ed9f982016-03-10 06:28:35198 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]64860882014-08-04 23:44:17199
fdoray8e32586852016-06-22 19:56:16200 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17201
sammc57ed9f982016-03-10 06:28:35202 channel()->Close();
[email protected]64860882014-08-04 23:44:17203
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
sammc57ed9f982016-03-10 06:28:35212DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestClient, ChannelClient) {
[email protected]64860882014-08-04 23:44:17213 TestChannelListenerWithExtraExpectations listener;
sammc57ed9f982016-03-10 06:28:35214 Connect(&listener);
215 listener.Init(channel());
[email protected]64860882014-08-04 23:44:17216
sammc57ed9f982016-03-10 06:28:35217 IPC::TestChannelListener::SendOneMessage(channel(), "hello from child");
fdoray8e32586852016-06-22 19:56:16218 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17219 EXPECT_TRUE(listener.is_connected_called());
220 EXPECT_TRUE(listener.HasSentAll());
221
sammc57ed9f982016-03-10 06:28:35222 Close();
[email protected]64860882014-08-04 23:44:17223}
224
morrita0a24cfc92014-09-16 03:20:48225class ListenerExpectingErrors : public IPC::Listener {
226 public:
sammc57ed9f982016-03-10 06:28:35227 ListenerExpectingErrors() : has_error_(false) {}
morrita0a24cfc92014-09-16 03:20:48228
tfarina10a5c062015-09-04 18:47:57229 void OnChannelConnected(int32_t peer_pid) override {
ki.stfua21ed8c2015-10-12 17:26:00230 base::MessageLoop::current()->QuitWhenIdle();
morritabe6c4cc2014-09-24 23:38:44231 }
232
dchengfe61fca2014-10-22 02:29:52233 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita0a24cfc92014-09-16 03:20:48234
dchengfe61fca2014-10-22 02:29:52235 void OnChannelError() override {
morrita0a24cfc92014-09-16 03:20:48236 has_error_ = true;
ki.stfua21ed8c2015-10-12 17:26:00237 base::MessageLoop::current()->QuitWhenIdle();
morrita0a24cfc92014-09-16 03:20:48238 }
239
240 bool has_error() const { return has_error_; }
241
242 private:
243 bool has_error_;
244};
245
morrita0a24cfc92014-09-16 03:20:48246class ListenerThatQuits : public IPC::Listener {
247 public:
sammc57ed9f982016-03-10 06:28:35248 ListenerThatQuits() {}
morrita0a24cfc92014-09-16 03:20:48249
sammc57ed9f982016-03-10 06:28:35250 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita0a24cfc92014-09-16 03:20:48251
tfarina10a5c062015-09-04 18:47:57252 void OnChannelConnected(int32_t peer_pid) override {
ki.stfua21ed8c2015-10-12 17:26:00253 base::MessageLoop::current()->QuitWhenIdle();
morrita0a24cfc92014-09-16 03:20:48254 }
255};
256
257// A long running process that connects to us.
sammc57ed9f982016-03-10 06:28:35258DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoErraticTestClient,
259 ChannelClient) {
morrita0a24cfc92014-09-16 03:20:48260 ListenerThatQuits listener;
sammc57ed9f982016-03-10 06:28:35261 Connect(&listener);
morrita0a24cfc92014-09-16 03:20:48262
fdoray8e32586852016-06-22 19:56:16263 base::RunLoop().Run();
morrita0a24cfc92014-09-16 03:20:48264
sammc57ed9f982016-03-10 06:28:35265 Close();
morrita0a24cfc92014-09-16 03:20:48266}
267
amistry0027a0952016-05-03 00:52:47268TEST_F(IPCChannelMojoTest, SendFailWithPendingMessages) {
rockotcbca72f2015-03-03 16:31:04269 InitWithMojo("IPCChannelMojoErraticTestClient");
morrita0a24cfc92014-09-16 03:20:48270
271 // Set up IPC channel and start client.
272 ListenerExpectingErrors listener;
273 CreateChannel(&listener);
274 ASSERT_TRUE(ConnectChannel());
275
jamesra03ae492014-10-03 04:26:48276 // This matches a value in mojo/edk/system/constants.h
morritabe6c4cc2014-09-24 23:38:44277 const int kMaxMessageNumBytes = 4 * 1024 * 1024;
278 std::string overly_large_data(kMaxMessageNumBytes, '*');
morrita0a24cfc92014-09-16 03:20:48279 // This messages are queued as pending.
morritabe6c4cc2014-09-24 23:38:44280 for (size_t i = 0; i < 10; ++i) {
sammc57ed9f982016-03-10 06:28:35281 IPC::TestChannelListener::SendOneMessage(sender(),
282 overly_large_data.c_str());
morrita0a24cfc92014-09-16 03:20:48283 }
284
fdoray8e32586852016-06-22 19:56:16285 base::RunLoop().Run();
morrita0a24cfc92014-09-16 03:20:48286
sammc57ed9f982016-03-10 06:28:35287 channel()->Close();
morrita0a24cfc92014-09-16 03:20:48288
289 EXPECT_TRUE(WaitForClientShutdown());
290 EXPECT_TRUE(listener.has_error());
291
292 DestroyChannel();
293}
294
morrita81b17e02015-02-06 00:58:30295struct 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
304class 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));
dchenge48600452015-12-28 02:24:50314 EXPECT_TRUE(IPC::MojoMessageHelper::WriteMessagePipeTo(
315 message, std::move(pipe->peer)));
morrita81b17e02015-02-06 00:58:30316 }
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,
brettwbd4d7112015-06-03 04:29:25326 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30327 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());
sammc57ed9f982016-03-10 06:28:35333 ASSERT_EQ(MOJO_RESULT_OK,
334 mojo::Wait(pipe.get(), MOJO_HANDLE_SIGNAL_READABLE,
335 MOJO_DEADLINE_INDEFINITE, nullptr));
morrita81b17e02015-02-06 00:58:30336 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)
amistry20e2b1d62016-06-23 06:12:35343 static base::FilePath GetSendingFilePath(const base::FilePath& dir_path) {
344 return dir_path.Append("ListenerThatExpectsFile.txt");
morrita81b17e02015-02-06 00:58:30345 }
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,
brettwbd4d7112015-06-03 04:29:25373 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30374 base::ScopedFD fd;
rockot502c94f2016-02-03 20:20:16375 scoped_refptr<base::Pickle::Attachment> attachment;
morrita81b17e02015-02-06 00:58:30376 EXPECT_TRUE(message.ReadAttachment(iter, &attachment));
amistry980a61b2016-06-09 02:51:20377 EXPECT_EQ(IPC::MessageAttachment::TYPE_PLATFORM_FILE,
378 static_cast<IPC::MessageAttachment*>(attachment.get())
379 ->GetType());
rockot502c94f2016-02-03 20:20:16380 base::File file(static_cast<IPC::MessageAttachment*>(attachment.get())
381 ->TakePlatformFile());
morrita81b17e02015-02-06 00:58:30382 std::string content(GetSendingFileContent().size(), ' ');
383 file.Read(0, &content[0], content.size());
384 EXPECT_EQ(content, GetSendingFileContent());
385 }
386#endif
387};
388
389class ListenerThatExpectsMessagePipe : public IPC::Listener {
390 public:
391 ListenerThatExpectsMessagePipe() : sender_(NULL) {}
392
393 ~ListenerThatExpectsMessagePipe() override {}
394
395 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25396 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30397 HandleSendingHelper::ReadReceivedPipe(message, &iter);
morrita81b17e02015-02-06 00:58:30398 ListenerThatExpectsOK::SendOK(sender_);
399 return true;
400 }
401
amistry6c70caea2016-06-09 03:08:29402 void OnChannelError() override {
403 base::MessageLoop::current()->QuitWhenIdle();
404 }
morrita81b17e02015-02-06 00:58:30405
406 void set_sender(IPC::Sender* sender) { sender_ = sender; }
407
408 private:
409 IPC::Sender* sender_;
410};
411
amistry0027a0952016-05-03 00:52:47412TEST_F(IPCChannelMojoTest, SendMessagePipe) {
rockotcbca72f2015-03-03 16:31:04413 InitWithMojo("IPCChannelMojoTestSendMessagePipeClient");
morrita81b17e02015-02-06 00:58:30414
415 ListenerThatExpectsOK listener;
416 CreateChannel(&listener);
417 ASSERT_TRUE(ConnectChannel());
morrita81b17e02015-02-06 00:58:30418
419 TestingMessagePipe pipe;
420 HandleSendingHelper::WritePipeThenSend(channel(), &pipe);
421
fdoray8e32586852016-06-22 19:56:16422 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35423 channel()->Close();
morrita81b17e02015-02-06 00:58:30424
425 EXPECT_TRUE(WaitForClientShutdown());
426 DestroyChannel();
427}
428
sammc57ed9f982016-03-10 06:28:35429DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendMessagePipeClient,
430 ChannelClient) {
morrita81b17e02015-02-06 00:58:30431 ListenerThatExpectsMessagePipe listener;
sammc57ed9f982016-03-10 06:28:35432 Connect(&listener);
433 listener.set_sender(channel());
morrita81b17e02015-02-06 00:58:30434
fdoray8e32586852016-06-22 19:56:16435 base::RunLoop().Run();
morrita81b17e02015-02-06 00:58:30436
sammc57ed9f982016-03-10 06:28:35437 Close();
morrita81b17e02015-02-06 00:58:30438}
439
morrita438a2ee2015-04-03 05:28:21440void ReadOK(mojo::MessagePipeHandle pipe) {
441 std::string should_be_ok("xx");
442 uint32_t num_bytes = static_cast<uint32_t>(should_be_ok.size());
sammc57ed9f982016-03-10 06:28:35443 CHECK_EQ(MOJO_RESULT_OK, mojo::Wait(pipe, MOJO_HANDLE_SIGNAL_READABLE,
444 MOJO_DEADLINE_INDEFINITE, nullptr));
morrita438a2ee2015-04-03 05:28:21445 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
451void 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
458class 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 {
brettwbd4d7112015-06-03 04:29:25466 base::PickleIterator iter(message);
morrita438a2ee2015-04-03 05:28:21467 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
morrita438a2ee2015-04-03 05:28:21476 ListenerThatExpectsOK::SendOK(sender_);
477 return true;
478 }
479
amistry6c70caea2016-06-09 03:08:29480 void OnChannelError() override {
481 base::MessageLoop::current()->QuitWhenIdle();
482 }
483
morrita438a2ee2015-04-03 05:28:21484 void set_sender(IPC::Sender* sender) { sender_ = sender; }
485
486 private:
487 IPC::Sender* sender_;
488 bool receiving_valid_;
489};
490
sammc57ed9f982016-03-10 06:28:35491class 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());
morrita438a2ee2015-04-03 05:28:21498
fdoray8e32586852016-06-22 19:56:16499 base::RunLoop().Run();
morrita438a2ee2015-04-03 05:28:21500
sammc57ed9f982016-03-10 06:28:35501 Close();
502 }
503};
morrita438a2ee2015-04-03 05:28:21504
amistry0027a0952016-05-03 00:52:47505TEST_F(IPCChannelMojoTest, ParamTraitValidMessagePipe) {
morrita438a2ee2015-04-03 05:28:21506 InitWithMojo("ParamTraitValidMessagePipeClient");
507
508 ListenerThatExpectsOK listener;
509 CreateChannel(&listener);
510 ASSERT_TRUE(ConnectChannel());
morrita438a2ee2015-04-03 05:28:21511
512 TestingMessagePipe pipe;
513
danakj03de39b22016-04-23 04:21:09514 std::unique_ptr<IPC::Message> message(new IPC::Message());
morrita438a2ee2015-04-03 05:28:21515 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
516 pipe.peer.release());
517 WriteOK(pipe.self.get());
518
sammc57ed9f982016-03-10 06:28:35519 channel()->Send(message.release());
fdoray8e32586852016-06-22 19:56:16520 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35521 channel()->Close();
morrita438a2ee2015-04-03 05:28:21522
523 EXPECT_TRUE(WaitForClientShutdown());
524 DestroyChannel();
525}
526
sammc57ed9f982016-03-10 06:28:35527DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ParamTraitValidMessagePipeClient,
528 ParamTraitMessagePipeClient) {
529 RunTest(true);
morrita438a2ee2015-04-03 05:28:21530}
531
amistry0027a0952016-05-03 00:52:47532TEST_F(IPCChannelMojoTest, ParamTraitInvalidMessagePipe) {
morrita438a2ee2015-04-03 05:28:21533 InitWithMojo("ParamTraitInvalidMessagePipeClient");
534
535 ListenerThatExpectsOK listener;
536 CreateChannel(&listener);
537 ASSERT_TRUE(ConnectChannel());
morrita438a2ee2015-04-03 05:28:21538
539 mojo::MessagePipeHandle invalid_handle;
danakj03de39b22016-04-23 04:21:09540 std::unique_ptr<IPC::Message> message(new IPC::Message());
morrita438a2ee2015-04-03 05:28:21541 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
542 invalid_handle);
543
sammc57ed9f982016-03-10 06:28:35544 channel()->Send(message.release());
fdoray8e32586852016-06-22 19:56:16545 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35546 channel()->Close();
morrita438a2ee2015-04-03 05:28:21547
548 EXPECT_TRUE(WaitForClientShutdown());
549 DestroyChannel();
550}
551
sammc57ed9f982016-03-10 06:28:35552DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ParamTraitInvalidMessagePipeClient,
553 ParamTraitMessagePipeClient) {
554 RunTest(false);
morrita438a2ee2015-04-03 05:28:21555}
556
amistry0027a0952016-05-03 00:52:47557TEST_F(IPCChannelMojoTest, SendFailAfterClose) {
morrita17137e62015-06-23 22:29:36558 InitWithMojo("IPCChannelMojoTestSendOkClient");
559
560 ListenerThatExpectsOK listener;
561 CreateChannel(&listener);
562 ASSERT_TRUE(ConnectChannel());
morrita17137e62015-06-23 22:29:36563
fdoray8e32586852016-06-22 19:56:16564 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35565 channel()->Close();
566 ASSERT_FALSE(channel()->Send(new IPC::Message()));
morrita17137e62015-06-23 22:29:36567
568 EXPECT_TRUE(WaitForClientShutdown());
569 DestroyChannel();
570}
571
572class ListenerSendingOneOk : public IPC::Listener {
573 public:
sammc57ed9f982016-03-10 06:28:35574 ListenerSendingOneOk() {}
morrita17137e62015-06-23 22:29:36575
sammc57ed9f982016-03-10 06:28:35576 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita17137e62015-06-23 22:29:36577
tfarina10a5c062015-09-04 18:47:57578 void OnChannelConnected(int32_t peer_pid) override {
morrita17137e62015-06-23 22:29:36579 ListenerThatExpectsOK::SendOK(sender_);
ki.stfua21ed8c2015-10-12 17:26:00580 base::MessageLoop::current()->QuitWhenIdle();
morrita17137e62015-06-23 22:29:36581 }
582
583 void set_sender(IPC::Sender* sender) { sender_ = sender; }
584
585 private:
586 IPC::Sender* sender_;
587};
588
sammc57ed9f982016-03-10 06:28:35589DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendOkClient,
590 ChannelClient) {
morrita17137e62015-06-23 22:29:36591 ListenerSendingOneOk listener;
sammc57ed9f982016-03-10 06:28:35592 Connect(&listener);
593 listener.set_sender(channel());
morrita17137e62015-06-23 22:29:36594
fdoray8e32586852016-06-22 19:56:16595 base::RunLoop().Run();
morrita17137e62015-06-23 22:29:36596
sammc57ed9f982016-03-10 06:28:35597 Close();
morrita17137e62015-06-23 22:29:36598}
599
rockot7c6bf952016-07-14 00:34:11600class 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
654const int ListenerWithSimpleAssociatedInterface::kNumMessages = 1000;
655
656class 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
687TEST_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
703DEFINE_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
rockot8d890f62016-07-14 16:37:14714class ChannelProxyRunner {
715 public:
rockota34707ca2016-07-20 04:28:32716 ChannelProxyRunner(mojo::ScopedMessagePipeHandle handle,
717 bool for_server)
718 : for_server_(for_server),
719 handle_(std::move(handle)),
rockot8d890f62016-07-14 16:37:14720 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 }
rockota34707ca2016-07-20 04:28:32728
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 }
rockot8d890f62016-07-14 16:37:14740
741 IPC::ChannelProxy* proxy() { return proxy_.get(); }
742
743 private:
rockota34707ca2016-07-20 04:28:32744 const bool for_server_;
rockot8d890f62016-07-14 16:37:14745
rockota34707ca2016-07-20 04:28:32746 mojo::ScopedMessagePipeHandle handle_;
rockot8d890f62016-07-14 16:37:14747 base::Thread io_thread_;
748 std::unique_ptr<IPC::ChannelProxy> proxy_;
749
750 DISALLOW_COPY_AND_ASSIGN(ChannelProxyRunner);
751};
752
753class IPCChannelProxyMojoTest : public IPCChannelMojoTestBase {
754 public:
755 void InitWithMojo(const std::string& client_name) {
756 IPCChannelMojoTestBase::InitWithMojo(client_name);
rockota34707ca2016-07-20 04:28:32757 runner_.reset(new ChannelProxyRunner(TakeHandle(), true));
rockot8d890f62016-07-14 16:37:14758 }
759 void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
760 void RunProxy() { runner_->RunProxy(); }
rockot0e4de5f2016-07-22 21:18:07761 void DestroyProxy() {
762 runner_.reset();
763 base::RunLoop().RunUntilIdle();
764 }
rockot8d890f62016-07-14 16:37:14765
766 IPC::ChannelProxy* proxy() { return runner_->proxy(); }
767
768 private:
769 base::MessageLoop message_loop_;
770 std::unique_ptr<ChannelProxyRunner> runner_;
771};
772
773class 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();
rockot0e4de5f2016-07-22 21:18:07815 binding_.Close();
rockot8d890f62016-07-14 16:37:14816 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
831const int ListenerWithSimpleProxyAssociatedInterface::kNumMessages = 1000;
832
833TEST_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
rockot0e4de5f2016-07-22 21:18:07846 DestroyProxy();
rockot8d890f62016-07-14 16:37:14847}
848
849class ChannelProxyClient {
850 public:
851 void Init(mojo::ScopedMessagePipeHandle handle) {
rockota34707ca2016-07-20 04:28:32852 runner_.reset(new ChannelProxyRunner(std::move(handle), false));
rockot8d890f62016-07-14 16:37:14853 }
854 void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
855 void RunProxy() { runner_->RunProxy(); }
rockot0e4de5f2016-07-22 21:18:07856 void DestroyProxy() {
857 runner_.reset();
858 base::RunLoop().RunUntilIdle();
859 }
rockot8d890f62016-07-14 16:37:14860
861 IPC::ChannelProxy* proxy() { return runner_->proxy(); }
862
863 private:
864 base::MessageLoop message_loop_;
865 std::unique_ptr<ChannelProxyRunner> runner_;
866};
867
rockot0e4de5f2016-07-22 21:18:07868class DummyListener : public IPC::Listener {
rockot8d890f62016-07-14 16:37:14869 public:
rockot8d890f62016-07-14 16:37:14870 // IPC::Listener
871 bool OnMessageReceived(const IPC::Message& message) override { return true; }
rockot8d890f62016-07-14 16:37:14872};
873
874DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ProxyThreadAssociatedInterfaceClient,
875 ChannelProxyClient) {
rockot0e4de5f2016-07-22 21:18:07876 DummyListener listener;
rockot8d890f62016-07-14 16:37:14877 CreateProxy(&listener);
878 RunProxy();
rockot8d890f62016-07-14 16:37:14879
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();
rockot0e4de5f2016-07-22 21:18:07892
893 DestroyProxy();
rockot8d890f62016-07-14 16:37:14894}
895
[email protected]64860882014-08-04 23:44:17896#if defined(OS_POSIX)
rockot8d890f62016-07-14 16:37:14897
[email protected]64860882014-08-04 23:44:17898class ListenerThatExpectsFile : public IPC::Listener {
899 public:
sammc57ed9f982016-03-10 06:28:35900 ListenerThatExpectsFile() : sender_(NULL) {}
[email protected]64860882014-08-04 23:44:17901
dchengfe61fca2014-10-22 02:29:52902 ~ListenerThatExpectsFile() override {}
[email protected]64860882014-08-04 23:44:17903
dchengfe61fca2014-10-22 02:29:52904 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25905 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30906 HandleSendingHelper::ReadReceivedFile(message, &iter);
[email protected]64860882014-08-04 23:44:17907 ListenerThatExpectsOK::SendOK(sender_);
908 return true;
909 }
910
amistry6c70caea2016-06-09 03:08:29911 void OnChannelError() override {
912 base::MessageLoop::current()->QuitWhenIdle();
913 }
[email protected]64860882014-08-04 23:44:17914
[email protected]64860882014-08-04 23:44:17915 void set_sender(IPC::Sender* sender) { sender_ = sender; }
916
917 private:
918 IPC::Sender* sender_;
919};
920
amistry0027a0952016-05-03 00:52:47921TEST_F(IPCChannelMojoTest, SendPlatformHandle) {
rockotcbca72f2015-03-03 16:31:04922 InitWithMojo("IPCChannelMojoTestSendPlatformHandleClient");
[email protected]64860882014-08-04 23:44:17923
924 ListenerThatExpectsOK listener;
morrita373af03b2014-09-09 19:35:24925 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17926 ASSERT_TRUE(ConnectChannel());
[email protected]64860882014-08-04 23:44:17927
amistry20e2b1d62016-06-23 06:12:35928 base::ScopedTempDir temp_dir;
929 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
930 base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.path()),
[email protected]64860882014-08-04 23:44:17931 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
morrita81b17e02015-02-06 00:58:30932 base::File::FLAG_READ);
933 HandleSendingHelper::WriteFileThenSend(channel(), file);
fdoray8e32586852016-06-22 19:56:16934 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17935
sammc57ed9f982016-03-10 06:28:35936 channel()->Close();
[email protected]64860882014-08-04 23:44:17937
938 EXPECT_TRUE(WaitForClientShutdown());
939 DestroyChannel();
940}
941
sammc57ed9f982016-03-10 06:28:35942DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendPlatformHandleClient,
943 ChannelClient) {
[email protected]64860882014-08-04 23:44:17944 ListenerThatExpectsFile listener;
sammc57ed9f982016-03-10 06:28:35945 Connect(&listener);
946 listener.set_sender(channel());
[email protected]64860882014-08-04 23:44:17947
fdoray8e32586852016-06-22 19:56:16948 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17949
sammc57ed9f982016-03-10 06:28:35950 Close();
[email protected]64860882014-08-04 23:44:17951}
morrita81b17e02015-02-06 00:58:30952
953class ListenerThatExpectsFileAndPipe : public IPC::Listener {
954 public:
955 ListenerThatExpectsFileAndPipe() : sender_(NULL) {}
956
957 ~ListenerThatExpectsFileAndPipe() override {}
958
959 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25960 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30961 HandleSendingHelper::ReadReceivedFile(message, &iter);
962 HandleSendingHelper::ReadReceivedPipe(message, &iter);
morrita81b17e02015-02-06 00:58:30963 ListenerThatExpectsOK::SendOK(sender_);
964 return true;
965 }
966
amistry6c70caea2016-06-09 03:08:29967 void OnChannelError() override {
968 base::MessageLoop::current()->QuitWhenIdle();
969 }
morrita81b17e02015-02-06 00:58:30970
971 void set_sender(IPC::Sender* sender) { sender_ = sender; }
972
973 private:
974 IPC::Sender* sender_;
975};
976
amistry0027a0952016-05-03 00:52:47977TEST_F(IPCChannelMojoTest, SendPlatformHandleAndPipe) {
rockotcbca72f2015-03-03 16:31:04978 InitWithMojo("IPCChannelMojoTestSendPlatformHandleAndPipeClient");
morrita81b17e02015-02-06 00:58:30979
980 ListenerThatExpectsOK listener;
981 CreateChannel(&listener);
982 ASSERT_TRUE(ConnectChannel());
morrita81b17e02015-02-06 00:58:30983
amistry20e2b1d62016-06-23 06:12:35984 base::ScopedTempDir temp_dir;
985 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
986 base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.path()),
morrita81b17e02015-02-06 00:58:30987 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
988 base::File::FLAG_READ);
989 TestingMessagePipe pipe;
990 HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe);
991
fdoray8e32586852016-06-22 19:56:16992 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35993 channel()->Close();
morrita81b17e02015-02-06 00:58:30994
995 EXPECT_TRUE(WaitForClientShutdown());
996 DestroyChannel();
997}
998
sammc57ed9f982016-03-10 06:28:35999DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
1000 IPCChannelMojoTestSendPlatformHandleAndPipeClient,
1001 ChannelClient) {
morrita81b17e02015-02-06 00:58:301002 ListenerThatExpectsFileAndPipe listener;
sammc57ed9f982016-03-10 06:28:351003 Connect(&listener);
1004 listener.set_sender(channel());
morrita81b17e02015-02-06 00:58:301005
fdoray8e32586852016-06-22 19:56:161006 base::RunLoop().Run();
morrita81b17e02015-02-06 00:58:301007
sammc57ed9f982016-03-10 06:28:351008 Close();
morrita81b17e02015-02-06 00:58:301009}
1010
rockot7c6bf952016-07-14 00:34:111011#endif // defined(OS_POSIX)
[email protected]64860882014-08-04 23:44:171012
morrita0bd20bd2015-02-25 20:11:271013#if defined(OS_LINUX)
1014
1015const base::ProcessId kMagicChildId = 54321;
1016
1017class ListenerThatVerifiesPeerPid : public IPC::Listener {
1018 public:
tfarina10a5c062015-09-04 18:47:571019 void OnChannelConnected(int32_t peer_pid) override {
morrita0bd20bd2015-02-25 20:11:271020 EXPECT_EQ(peer_pid, kMagicChildId);
ki.stfua21ed8c2015-10-12 17:26:001021 base::MessageLoop::current()->QuitWhenIdle();
morrita0bd20bd2015-02-25 20:11:271022 }
1023
1024 bool OnMessageReceived(const IPC::Message& message) override {
1025 NOTREACHED();
1026 return true;
1027 }
1028};
1029
sammc57ed9f982016-03-10 06:28:351030TEST_F(IPCChannelMojoTest, VerifyGlobalPid) {
rockotcbca72f2015-03-03 16:31:041031 InitWithMojo("IPCChannelMojoTestVerifyGlobalPidClient");
morrita0bd20bd2015-02-25 20:11:271032
1033 ListenerThatVerifiesPeerPid listener;
1034 CreateChannel(&listener);
1035 ASSERT_TRUE(ConnectChannel());
morrita0bd20bd2015-02-25 20:11:271036
1037 base::MessageLoop::current()->Run();
rockotcbca72f2015-03-03 16:31:041038 channel()->Close();
morrita0bd20bd2015-02-25 20:11:271039
1040 EXPECT_TRUE(WaitForClientShutdown());
1041 DestroyChannel();
1042}
1043
sammc57ed9f982016-03-10 06:28:351044DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestVerifyGlobalPidClient,
1045 ChannelClient) {
morrita0bd20bd2015-02-25 20:11:271046 IPC::Channel::SetGlobalPid(kMagicChildId);
1047 ListenerThatQuits listener;
sammc57ed9f982016-03-10 06:28:351048 Connect(&listener);
morrita0bd20bd2015-02-25 20:11:271049
1050 base::MessageLoop::current()->Run();
1051
sammc57ed9f982016-03-10 06:28:351052 Close();
morrita0bd20bd2015-02-25 20:11:271053}
1054
sammc57ed9f982016-03-10 06:28:351055#endif // OS_LINUX
morrita0bd20bd2015-02-25 20:11:271056
[email protected]64860882014-08-04 23:44:171057} // namespace