blob: 041672023a6a84f173bc45e4c82c27d7ab6b89d2 [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(); }
761
762 IPC::ChannelProxy* proxy() { return runner_->proxy(); }
763
764 private:
765 base::MessageLoop message_loop_;
766 std::unique_ptr<ChannelProxyRunner> runner_;
767};
768
769class ListenerWithSimpleProxyAssociatedInterface
770 : public IPC::Listener,
771 public IPC::mojom::SimpleTestDriver {
772 public:
773 static const int kNumMessages;
774
775 ListenerWithSimpleProxyAssociatedInterface() : binding_(this) {}
776
777 ~ListenerWithSimpleProxyAssociatedInterface() override {}
778
779 bool OnMessageReceived(const IPC::Message& message) override {
780 base::PickleIterator iter(message);
781 std::string should_be_expected;
782 EXPECT_TRUE(iter.ReadString(&should_be_expected));
783 EXPECT_EQ(should_be_expected, next_expected_string_);
784 num_messages_received_++;
785 return true;
786 }
787
788 void OnChannelError() override {
789 DCHECK(received_quit_);
790 }
791
792 void RegisterInterfaceFactory(IPC::ChannelProxy* proxy) {
793 proxy->AddAssociatedInterface(
794 base::Bind(&ListenerWithSimpleProxyAssociatedInterface::BindRequest,
795 base::Unretained(this)));
796 }
797
798 bool received_all_messages() const {
799 return num_messages_received_ == kNumMessages && received_quit_;
800 }
801
802 private:
803 // IPC::mojom::SimpleTestDriver:
804 void ExpectString(const mojo::String& str) override {
805 next_expected_string_ = str;
806 }
807
808 void RequestQuit(const RequestQuitCallback& callback) override {
809 received_quit_ = true;
810 callback.Run();
811 base::MessageLoop::current()->QuitWhenIdle();
812 }
813
814 void BindRequest(IPC::mojom::SimpleTestDriverAssociatedRequest request) {
815 DCHECK(!binding_.is_bound());
816 binding_.Bind(std::move(request));
817 }
818
819 std::string next_expected_string_;
820 int num_messages_received_ = 0;
821 bool received_quit_ = false;
822
823 mojo::AssociatedBinding<IPC::mojom::SimpleTestDriver> binding_;
824};
825
826const int ListenerWithSimpleProxyAssociatedInterface::kNumMessages = 1000;
827
828TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterface) {
829 InitWithMojo("ProxyThreadAssociatedInterfaceClient");
830
831 ListenerWithSimpleProxyAssociatedInterface listener;
832 CreateProxy(&listener);
833 listener.RegisterInterfaceFactory(proxy());
834 RunProxy();
835
836 base::RunLoop().Run();
837
838 EXPECT_TRUE(WaitForClientShutdown());
839 EXPECT_TRUE(listener.received_all_messages());
840
841 base::RunLoop().RunUntilIdle();
842}
843
844class ChannelProxyClient {
845 public:
846 void Init(mojo::ScopedMessagePipeHandle handle) {
rockota34707ca2016-07-20 04:28:32847 runner_.reset(new ChannelProxyRunner(std::move(handle), false));
rockot8d890f62016-07-14 16:37:14848 }
849 void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
850 void RunProxy() { runner_->RunProxy(); }
851
852 IPC::ChannelProxy* proxy() { return runner_->proxy(); }
853
854 private:
855 base::MessageLoop message_loop_;
856 std::unique_ptr<ChannelProxyRunner> runner_;
857};
858
859class ListenerThatWaitsForConnect : public IPC::Listener {
860 public:
861 explicit ListenerThatWaitsForConnect(const base::Closure& connect_handler)
862 : connect_handler_(connect_handler) {}
863
864 // IPC::Listener
865 bool OnMessageReceived(const IPC::Message& message) override { return true; }
866 void OnChannelConnected(int32_t) override { connect_handler_.Run(); }
867
868 private:
869 base::Closure connect_handler_;
870};
871
872DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ProxyThreadAssociatedInterfaceClient,
873 ChannelProxyClient) {
874 base::RunLoop connect_loop;
875 ListenerThatWaitsForConnect listener(connect_loop.QuitClosure());
876 CreateProxy(&listener);
877 RunProxy();
878 connect_loop.Run();
879
880 // Send a bunch of interleaved messages, alternating between the associated
881 // interface and a legacy IPC::Message.
882 IPC::mojom::SimpleTestDriverAssociatedPtr driver;
883 proxy()->GetRemoteAssociatedInterface(&driver);
884 for (int i = 0; i < ListenerWithSimpleProxyAssociatedInterface::kNumMessages;
885 ++i) {
886 std::string str = base::StringPrintf("Hello! %d", i);
887 driver->ExpectString(str);
888 SendString(proxy(), str);
889 }
890 driver->RequestQuit(base::MessageLoop::QuitWhenIdleClosure());
891 base::RunLoop().Run();
892}
893
[email protected]64860882014-08-04 23:44:17894#if defined(OS_POSIX)
rockot8d890f62016-07-14 16:37:14895
[email protected]64860882014-08-04 23:44:17896class ListenerThatExpectsFile : public IPC::Listener {
897 public:
sammc57ed9f982016-03-10 06:28:35898 ListenerThatExpectsFile() : sender_(NULL) {}
[email protected]64860882014-08-04 23:44:17899
dchengfe61fca2014-10-22 02:29:52900 ~ListenerThatExpectsFile() override {}
[email protected]64860882014-08-04 23:44:17901
dchengfe61fca2014-10-22 02:29:52902 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25903 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30904 HandleSendingHelper::ReadReceivedFile(message, &iter);
[email protected]64860882014-08-04 23:44:17905 ListenerThatExpectsOK::SendOK(sender_);
906 return true;
907 }
908
amistry6c70caea2016-06-09 03:08:29909 void OnChannelError() override {
910 base::MessageLoop::current()->QuitWhenIdle();
911 }
[email protected]64860882014-08-04 23:44:17912
[email protected]64860882014-08-04 23:44:17913 void set_sender(IPC::Sender* sender) { sender_ = sender; }
914
915 private:
916 IPC::Sender* sender_;
917};
918
amistry0027a0952016-05-03 00:52:47919TEST_F(IPCChannelMojoTest, SendPlatformHandle) {
rockotcbca72f2015-03-03 16:31:04920 InitWithMojo("IPCChannelMojoTestSendPlatformHandleClient");
[email protected]64860882014-08-04 23:44:17921
922 ListenerThatExpectsOK listener;
morrita373af03b2014-09-09 19:35:24923 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17924 ASSERT_TRUE(ConnectChannel());
[email protected]64860882014-08-04 23:44:17925
amistry20e2b1d62016-06-23 06:12:35926 base::ScopedTempDir temp_dir;
927 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
928 base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.path()),
[email protected]64860882014-08-04 23:44:17929 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
morrita81b17e02015-02-06 00:58:30930 base::File::FLAG_READ);
931 HandleSendingHelper::WriteFileThenSend(channel(), file);
fdoray8e32586852016-06-22 19:56:16932 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17933
sammc57ed9f982016-03-10 06:28:35934 channel()->Close();
[email protected]64860882014-08-04 23:44:17935
936 EXPECT_TRUE(WaitForClientShutdown());
937 DestroyChannel();
938}
939
sammc57ed9f982016-03-10 06:28:35940DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendPlatformHandleClient,
941 ChannelClient) {
[email protected]64860882014-08-04 23:44:17942 ListenerThatExpectsFile listener;
sammc57ed9f982016-03-10 06:28:35943 Connect(&listener);
944 listener.set_sender(channel());
[email protected]64860882014-08-04 23:44:17945
fdoray8e32586852016-06-22 19:56:16946 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17947
sammc57ed9f982016-03-10 06:28:35948 Close();
[email protected]64860882014-08-04 23:44:17949}
morrita81b17e02015-02-06 00:58:30950
951class ListenerThatExpectsFileAndPipe : public IPC::Listener {
952 public:
953 ListenerThatExpectsFileAndPipe() : sender_(NULL) {}
954
955 ~ListenerThatExpectsFileAndPipe() override {}
956
957 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25958 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30959 HandleSendingHelper::ReadReceivedFile(message, &iter);
960 HandleSendingHelper::ReadReceivedPipe(message, &iter);
morrita81b17e02015-02-06 00:58:30961 ListenerThatExpectsOK::SendOK(sender_);
962 return true;
963 }
964
amistry6c70caea2016-06-09 03:08:29965 void OnChannelError() override {
966 base::MessageLoop::current()->QuitWhenIdle();
967 }
morrita81b17e02015-02-06 00:58:30968
969 void set_sender(IPC::Sender* sender) { sender_ = sender; }
970
971 private:
972 IPC::Sender* sender_;
973};
974
amistry0027a0952016-05-03 00:52:47975TEST_F(IPCChannelMojoTest, SendPlatformHandleAndPipe) {
rockotcbca72f2015-03-03 16:31:04976 InitWithMojo("IPCChannelMojoTestSendPlatformHandleAndPipeClient");
morrita81b17e02015-02-06 00:58:30977
978 ListenerThatExpectsOK listener;
979 CreateChannel(&listener);
980 ASSERT_TRUE(ConnectChannel());
morrita81b17e02015-02-06 00:58:30981
amistry20e2b1d62016-06-23 06:12:35982 base::ScopedTempDir temp_dir;
983 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
984 base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.path()),
morrita81b17e02015-02-06 00:58:30985 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
986 base::File::FLAG_READ);
987 TestingMessagePipe pipe;
988 HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe);
989
fdoray8e32586852016-06-22 19:56:16990 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35991 channel()->Close();
morrita81b17e02015-02-06 00:58:30992
993 EXPECT_TRUE(WaitForClientShutdown());
994 DestroyChannel();
995}
996
sammc57ed9f982016-03-10 06:28:35997DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
998 IPCChannelMojoTestSendPlatformHandleAndPipeClient,
999 ChannelClient) {
morrita81b17e02015-02-06 00:58:301000 ListenerThatExpectsFileAndPipe listener;
sammc57ed9f982016-03-10 06:28:351001 Connect(&listener);
1002 listener.set_sender(channel());
morrita81b17e02015-02-06 00:58:301003
fdoray8e32586852016-06-22 19:56:161004 base::RunLoop().Run();
morrita81b17e02015-02-06 00:58:301005
sammc57ed9f982016-03-10 06:28:351006 Close();
morrita81b17e02015-02-06 00:58:301007}
1008
rockot7c6bf952016-07-14 00:34:111009#endif // defined(OS_POSIX)
[email protected]64860882014-08-04 23:44:171010
morrita0bd20bd2015-02-25 20:11:271011#if defined(OS_LINUX)
1012
1013const base::ProcessId kMagicChildId = 54321;
1014
1015class ListenerThatVerifiesPeerPid : public IPC::Listener {
1016 public:
tfarina10a5c062015-09-04 18:47:571017 void OnChannelConnected(int32_t peer_pid) override {
morrita0bd20bd2015-02-25 20:11:271018 EXPECT_EQ(peer_pid, kMagicChildId);
ki.stfua21ed8c2015-10-12 17:26:001019 base::MessageLoop::current()->QuitWhenIdle();
morrita0bd20bd2015-02-25 20:11:271020 }
1021
1022 bool OnMessageReceived(const IPC::Message& message) override {
1023 NOTREACHED();
1024 return true;
1025 }
1026};
1027
sammc57ed9f982016-03-10 06:28:351028TEST_F(IPCChannelMojoTest, VerifyGlobalPid) {
rockotcbca72f2015-03-03 16:31:041029 InitWithMojo("IPCChannelMojoTestVerifyGlobalPidClient");
morrita0bd20bd2015-02-25 20:11:271030
1031 ListenerThatVerifiesPeerPid listener;
1032 CreateChannel(&listener);
1033 ASSERT_TRUE(ConnectChannel());
morrita0bd20bd2015-02-25 20:11:271034
1035 base::MessageLoop::current()->Run();
rockotcbca72f2015-03-03 16:31:041036 channel()->Close();
morrita0bd20bd2015-02-25 20:11:271037
1038 EXPECT_TRUE(WaitForClientShutdown());
1039 DestroyChannel();
1040}
1041
sammc57ed9f982016-03-10 06:28:351042DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestVerifyGlobalPidClient,
1043 ChannelClient) {
morrita0bd20bd2015-02-25 20:11:271044 IPC::Channel::SetGlobalPid(kMagicChildId);
1045 ListenerThatQuits listener;
sammc57ed9f982016-03-10 06:28:351046 Connect(&listener);
morrita0bd20bd2015-02-25 20:11:271047
1048 base::MessageLoop::current()->Run();
1049
sammc57ed9f982016-03-10 06:28:351050 Close();
morrita0bd20bd2015-02-25 20:11:271051}
1052
sammc57ed9f982016-03-10 06:28:351053#endif // OS_LINUX
morrita0bd20bd2015-02-25 20:11:271054
[email protected]64860882014-08-04 23:44:171055} // namespace