blob: 0e085162ca0cc81caa09796ed629b3487fad0bf1 [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) {
111 channel_ = IPC::ChannelMojo::Create(std::move(handle_),
112 IPC::Channel::MODE_CLIENT, listener);
[email protected]64860882014-08-04 23:44:17113 CHECK(channel_->Connect());
114 }
115
rockotcbca72f2015-03-03 16:31:04116 void Close() {
117 channel_->Close();
118
119 base::RunLoop run_loop;
skyostile687bdff2015-05-12 11:29:21120 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
121 run_loop.QuitClosure());
rockotcbca72f2015-03-03 16:31:04122 run_loop.Run();
123 }
124
[email protected]64860882014-08-04 23:44:17125 IPC::ChannelMojo* channel() const { return channel_.get(); }
126
127 private:
[email protected]64860882014-08-04 23:44:17128 base::MessageLoopForIO main_message_loop_;
sammc57ed9f982016-03-10 06:28:35129 mojo::ScopedMessagePipeHandle handle_;
danakj03de39b22016-04-23 04:21:09130 std::unique_ptr<IPC::ChannelMojo> channel_;
[email protected]64860882014-08-04 23:44:17131};
132
rockot8d890f62016-07-14 16:37:14133class IPCChannelMojoTestBase : public testing::Test {
rockotcbca72f2015-03-03 16:31:04134 public:
135 void InitWithMojo(const std::string& test_client_name) {
sammc57ed9f982016-03-10 06:28:35136 handle_ = helper_.StartChild(test_client_name);
rockotcbca72f2015-03-03 16:31:04137 }
138
rockot8d890f62016-07-14 16:37:14139 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
149class IPCChannelMojoTest : public IPCChannelMojoTestBase {
150 public:
151 void TearDown() override { base::RunLoop().RunUntilIdle(); }
152
sammc57ed9f982016-03-10 06:28:35153 void CreateChannel(IPC::Listener* listener) {
rockot8d890f62016-07-14 16:37:14154 channel_ = IPC::ChannelMojo::Create(
155 TakeHandle(), IPC::Channel::MODE_SERVER, listener);
rockotcbca72f2015-03-03 16:31:04156 }
sammc57ed9f982016-03-10 06:28:35157
158 bool ConnectChannel() { return channel_->Connect(); }
159
160 void DestroyChannel() { channel_.reset(); }
161
sammc57ed9f982016-03-10 06:28:35162 IPC::Sender* sender() { return channel(); }
163 IPC::Channel* channel() { return channel_.get(); }
164
165 private:
166 base::MessageLoop message_loop_;
danakj03de39b22016-04-23 04:21:09167 std::unique_ptr<IPC::Channel> channel_;
rockotcbca72f2015-03-03 16:31:04168};
169
[email protected]64860882014-08-04 23:44:17170class TestChannelListenerWithExtraExpectations
171 : public IPC::TestChannelListener {
172 public:
sammc57ed9f982016-03-10 06:28:35173 TestChannelListenerWithExtraExpectations() : is_connected_called_(false) {}
[email protected]64860882014-08-04 23:44:17174
tfarina10a5c062015-09-04 18:47:57175 void OnChannelConnected(int32_t peer_pid) override {
[email protected]64860882014-08-04 23:44:17176 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
amistry0027a0952016-05-03 00:52:47187TEST_F(IPCChannelMojoTest, ConnectedFromClient) {
rockotcbca72f2015-03-03 16:31:04188 InitWithMojo("IPCChannelMojoTestClient");
[email protected]64860882014-08-04 23:44:17189
190 // Set up IPC channel and start client.
191 TestChannelListenerWithExtraExpectations listener;
morrita373af03b2014-09-09 19:35:24192 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17193 listener.Init(sender());
194 ASSERT_TRUE(ConnectChannel());
[email protected]64860882014-08-04 23:44:17195
sammc57ed9f982016-03-10 06:28:35196 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]64860882014-08-04 23:44:17197
fdoray8e32586852016-06-22 19:56:16198 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17199
sammc57ed9f982016-03-10 06:28:35200 channel()->Close();
[email protected]64860882014-08-04 23:44:17201
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
sammc57ed9f982016-03-10 06:28:35210DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestClient, ChannelClient) {
[email protected]64860882014-08-04 23:44:17211 TestChannelListenerWithExtraExpectations listener;
sammc57ed9f982016-03-10 06:28:35212 Connect(&listener);
213 listener.Init(channel());
[email protected]64860882014-08-04 23:44:17214
sammc57ed9f982016-03-10 06:28:35215 IPC::TestChannelListener::SendOneMessage(channel(), "hello from child");
fdoray8e32586852016-06-22 19:56:16216 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17217 EXPECT_TRUE(listener.is_connected_called());
218 EXPECT_TRUE(listener.HasSentAll());
219
sammc57ed9f982016-03-10 06:28:35220 Close();
[email protected]64860882014-08-04 23:44:17221}
222
morrita0a24cfc92014-09-16 03:20:48223class ListenerExpectingErrors : public IPC::Listener {
224 public:
sammc57ed9f982016-03-10 06:28:35225 ListenerExpectingErrors() : has_error_(false) {}
morrita0a24cfc92014-09-16 03:20:48226
tfarina10a5c062015-09-04 18:47:57227 void OnChannelConnected(int32_t peer_pid) override {
ki.stfua21ed8c2015-10-12 17:26:00228 base::MessageLoop::current()->QuitWhenIdle();
morritabe6c4cc2014-09-24 23:38:44229 }
230
dchengfe61fca2014-10-22 02:29:52231 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita0a24cfc92014-09-16 03:20:48232
dchengfe61fca2014-10-22 02:29:52233 void OnChannelError() override {
morrita0a24cfc92014-09-16 03:20:48234 has_error_ = true;
ki.stfua21ed8c2015-10-12 17:26:00235 base::MessageLoop::current()->QuitWhenIdle();
morrita0a24cfc92014-09-16 03:20:48236 }
237
238 bool has_error() const { return has_error_; }
239
240 private:
241 bool has_error_;
242};
243
morrita0a24cfc92014-09-16 03:20:48244class ListenerThatQuits : public IPC::Listener {
245 public:
sammc57ed9f982016-03-10 06:28:35246 ListenerThatQuits() {}
morrita0a24cfc92014-09-16 03:20:48247
sammc57ed9f982016-03-10 06:28:35248 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita0a24cfc92014-09-16 03:20:48249
tfarina10a5c062015-09-04 18:47:57250 void OnChannelConnected(int32_t peer_pid) override {
ki.stfua21ed8c2015-10-12 17:26:00251 base::MessageLoop::current()->QuitWhenIdle();
morrita0a24cfc92014-09-16 03:20:48252 }
253};
254
255// A long running process that connects to us.
sammc57ed9f982016-03-10 06:28:35256DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoErraticTestClient,
257 ChannelClient) {
morrita0a24cfc92014-09-16 03:20:48258 ListenerThatQuits listener;
sammc57ed9f982016-03-10 06:28:35259 Connect(&listener);
morrita0a24cfc92014-09-16 03:20:48260
fdoray8e32586852016-06-22 19:56:16261 base::RunLoop().Run();
morrita0a24cfc92014-09-16 03:20:48262
sammc57ed9f982016-03-10 06:28:35263 Close();
morrita0a24cfc92014-09-16 03:20:48264}
265
amistry0027a0952016-05-03 00:52:47266TEST_F(IPCChannelMojoTest, SendFailWithPendingMessages) {
rockotcbca72f2015-03-03 16:31:04267 InitWithMojo("IPCChannelMojoErraticTestClient");
morrita0a24cfc92014-09-16 03:20:48268
269 // Set up IPC channel and start client.
270 ListenerExpectingErrors listener;
271 CreateChannel(&listener);
272 ASSERT_TRUE(ConnectChannel());
273
jamesra03ae492014-10-03 04:26:48274 // This matches a value in mojo/edk/system/constants.h
morritabe6c4cc2014-09-24 23:38:44275 const int kMaxMessageNumBytes = 4 * 1024 * 1024;
276 std::string overly_large_data(kMaxMessageNumBytes, '*');
morrita0a24cfc92014-09-16 03:20:48277 // This messages are queued as pending.
morritabe6c4cc2014-09-24 23:38:44278 for (size_t i = 0; i < 10; ++i) {
sammc57ed9f982016-03-10 06:28:35279 IPC::TestChannelListener::SendOneMessage(sender(),
280 overly_large_data.c_str());
morrita0a24cfc92014-09-16 03:20:48281 }
282
fdoray8e32586852016-06-22 19:56:16283 base::RunLoop().Run();
morrita0a24cfc92014-09-16 03:20:48284
sammc57ed9f982016-03-10 06:28:35285 channel()->Close();
morrita0a24cfc92014-09-16 03:20:48286
287 EXPECT_TRUE(WaitForClientShutdown());
288 EXPECT_TRUE(listener.has_error());
289
290 DestroyChannel();
291}
292
morrita81b17e02015-02-06 00:58:30293struct 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
302class 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));
dchenge48600452015-12-28 02:24:50312 EXPECT_TRUE(IPC::MojoMessageHelper::WriteMessagePipeTo(
313 message, std::move(pipe->peer)));
morrita81b17e02015-02-06 00:58:30314 }
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,
brettwbd4d7112015-06-03 04:29:25324 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30325 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());
sammc57ed9f982016-03-10 06:28:35331 ASSERT_EQ(MOJO_RESULT_OK,
332 mojo::Wait(pipe.get(), MOJO_HANDLE_SIGNAL_READABLE,
333 MOJO_DEADLINE_INDEFINITE, nullptr));
morrita81b17e02015-02-06 00:58:30334 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)
amistry20e2b1d62016-06-23 06:12:35341 static base::FilePath GetSendingFilePath(const base::FilePath& dir_path) {
342 return dir_path.Append("ListenerThatExpectsFile.txt");
morrita81b17e02015-02-06 00:58:30343 }
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,
brettwbd4d7112015-06-03 04:29:25371 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30372 base::ScopedFD fd;
rockot502c94f2016-02-03 20:20:16373 scoped_refptr<base::Pickle::Attachment> attachment;
morrita81b17e02015-02-06 00:58:30374 EXPECT_TRUE(message.ReadAttachment(iter, &attachment));
amistry980a61b2016-06-09 02:51:20375 EXPECT_EQ(IPC::MessageAttachment::TYPE_PLATFORM_FILE,
376 static_cast<IPC::MessageAttachment*>(attachment.get())
377 ->GetType());
rockot502c94f2016-02-03 20:20:16378 base::File file(static_cast<IPC::MessageAttachment*>(attachment.get())
379 ->TakePlatformFile());
morrita81b17e02015-02-06 00:58:30380 std::string content(GetSendingFileContent().size(), ' ');
381 file.Read(0, &content[0], content.size());
382 EXPECT_EQ(content, GetSendingFileContent());
383 }
384#endif
385};
386
387class ListenerThatExpectsMessagePipe : public IPC::Listener {
388 public:
389 ListenerThatExpectsMessagePipe() : sender_(NULL) {}
390
391 ~ListenerThatExpectsMessagePipe() override {}
392
393 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25394 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30395 HandleSendingHelper::ReadReceivedPipe(message, &iter);
morrita81b17e02015-02-06 00:58:30396 ListenerThatExpectsOK::SendOK(sender_);
397 return true;
398 }
399
amistry6c70caea2016-06-09 03:08:29400 void OnChannelError() override {
401 base::MessageLoop::current()->QuitWhenIdle();
402 }
morrita81b17e02015-02-06 00:58:30403
404 void set_sender(IPC::Sender* sender) { sender_ = sender; }
405
406 private:
407 IPC::Sender* sender_;
408};
409
amistry0027a0952016-05-03 00:52:47410TEST_F(IPCChannelMojoTest, SendMessagePipe) {
rockotcbca72f2015-03-03 16:31:04411 InitWithMojo("IPCChannelMojoTestSendMessagePipeClient");
morrita81b17e02015-02-06 00:58:30412
413 ListenerThatExpectsOK listener;
414 CreateChannel(&listener);
415 ASSERT_TRUE(ConnectChannel());
morrita81b17e02015-02-06 00:58:30416
417 TestingMessagePipe pipe;
418 HandleSendingHelper::WritePipeThenSend(channel(), &pipe);
419
fdoray8e32586852016-06-22 19:56:16420 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35421 channel()->Close();
morrita81b17e02015-02-06 00:58:30422
423 EXPECT_TRUE(WaitForClientShutdown());
424 DestroyChannel();
425}
426
sammc57ed9f982016-03-10 06:28:35427DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendMessagePipeClient,
428 ChannelClient) {
morrita81b17e02015-02-06 00:58:30429 ListenerThatExpectsMessagePipe listener;
sammc57ed9f982016-03-10 06:28:35430 Connect(&listener);
431 listener.set_sender(channel());
morrita81b17e02015-02-06 00:58:30432
fdoray8e32586852016-06-22 19:56:16433 base::RunLoop().Run();
morrita81b17e02015-02-06 00:58:30434
sammc57ed9f982016-03-10 06:28:35435 Close();
morrita81b17e02015-02-06 00:58:30436}
437
morrita438a2ee2015-04-03 05:28:21438void ReadOK(mojo::MessagePipeHandle pipe) {
439 std::string should_be_ok("xx");
440 uint32_t num_bytes = static_cast<uint32_t>(should_be_ok.size());
sammc57ed9f982016-03-10 06:28:35441 CHECK_EQ(MOJO_RESULT_OK, mojo::Wait(pipe, MOJO_HANDLE_SIGNAL_READABLE,
442 MOJO_DEADLINE_INDEFINITE, nullptr));
morrita438a2ee2015-04-03 05:28:21443 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
449void 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
456class 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 {
brettwbd4d7112015-06-03 04:29:25464 base::PickleIterator iter(message);
morrita438a2ee2015-04-03 05:28:21465 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
morrita438a2ee2015-04-03 05:28:21474 ListenerThatExpectsOK::SendOK(sender_);
475 return true;
476 }
477
amistry6c70caea2016-06-09 03:08:29478 void OnChannelError() override {
479 base::MessageLoop::current()->QuitWhenIdle();
480 }
481
morrita438a2ee2015-04-03 05:28:21482 void set_sender(IPC::Sender* sender) { sender_ = sender; }
483
484 private:
485 IPC::Sender* sender_;
486 bool receiving_valid_;
487};
488
sammc57ed9f982016-03-10 06:28:35489class 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());
morrita438a2ee2015-04-03 05:28:21496
fdoray8e32586852016-06-22 19:56:16497 base::RunLoop().Run();
morrita438a2ee2015-04-03 05:28:21498
sammc57ed9f982016-03-10 06:28:35499 Close();
500 }
501};
morrita438a2ee2015-04-03 05:28:21502
amistry0027a0952016-05-03 00:52:47503TEST_F(IPCChannelMojoTest, ParamTraitValidMessagePipe) {
morrita438a2ee2015-04-03 05:28:21504 InitWithMojo("ParamTraitValidMessagePipeClient");
505
506 ListenerThatExpectsOK listener;
507 CreateChannel(&listener);
508 ASSERT_TRUE(ConnectChannel());
morrita438a2ee2015-04-03 05:28:21509
510 TestingMessagePipe pipe;
511
danakj03de39b22016-04-23 04:21:09512 std::unique_ptr<IPC::Message> message(new IPC::Message());
morrita438a2ee2015-04-03 05:28:21513 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
514 pipe.peer.release());
515 WriteOK(pipe.self.get());
516
sammc57ed9f982016-03-10 06:28:35517 channel()->Send(message.release());
fdoray8e32586852016-06-22 19:56:16518 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35519 channel()->Close();
morrita438a2ee2015-04-03 05:28:21520
521 EXPECT_TRUE(WaitForClientShutdown());
522 DestroyChannel();
523}
524
sammc57ed9f982016-03-10 06:28:35525DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ParamTraitValidMessagePipeClient,
526 ParamTraitMessagePipeClient) {
527 RunTest(true);
morrita438a2ee2015-04-03 05:28:21528}
529
amistry0027a0952016-05-03 00:52:47530TEST_F(IPCChannelMojoTest, ParamTraitInvalidMessagePipe) {
morrita438a2ee2015-04-03 05:28:21531 InitWithMojo("ParamTraitInvalidMessagePipeClient");
532
533 ListenerThatExpectsOK listener;
534 CreateChannel(&listener);
535 ASSERT_TRUE(ConnectChannel());
morrita438a2ee2015-04-03 05:28:21536
537 mojo::MessagePipeHandle invalid_handle;
danakj03de39b22016-04-23 04:21:09538 std::unique_ptr<IPC::Message> message(new IPC::Message());
morrita438a2ee2015-04-03 05:28:21539 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
540 invalid_handle);
541
sammc57ed9f982016-03-10 06:28:35542 channel()->Send(message.release());
fdoray8e32586852016-06-22 19:56:16543 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35544 channel()->Close();
morrita438a2ee2015-04-03 05:28:21545
546 EXPECT_TRUE(WaitForClientShutdown());
547 DestroyChannel();
548}
549
sammc57ed9f982016-03-10 06:28:35550DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ParamTraitInvalidMessagePipeClient,
551 ParamTraitMessagePipeClient) {
552 RunTest(false);
morrita438a2ee2015-04-03 05:28:21553}
554
amistry0027a0952016-05-03 00:52:47555TEST_F(IPCChannelMojoTest, SendFailAfterClose) {
morrita17137e62015-06-23 22:29:36556 InitWithMojo("IPCChannelMojoTestSendOkClient");
557
558 ListenerThatExpectsOK listener;
559 CreateChannel(&listener);
560 ASSERT_TRUE(ConnectChannel());
morrita17137e62015-06-23 22:29:36561
fdoray8e32586852016-06-22 19:56:16562 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35563 channel()->Close();
564 ASSERT_FALSE(channel()->Send(new IPC::Message()));
morrita17137e62015-06-23 22:29:36565
566 EXPECT_TRUE(WaitForClientShutdown());
567 DestroyChannel();
568}
569
570class ListenerSendingOneOk : public IPC::Listener {
571 public:
sammc57ed9f982016-03-10 06:28:35572 ListenerSendingOneOk() {}
morrita17137e62015-06-23 22:29:36573
sammc57ed9f982016-03-10 06:28:35574 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita17137e62015-06-23 22:29:36575
tfarina10a5c062015-09-04 18:47:57576 void OnChannelConnected(int32_t peer_pid) override {
morrita17137e62015-06-23 22:29:36577 ListenerThatExpectsOK::SendOK(sender_);
ki.stfua21ed8c2015-10-12 17:26:00578 base::MessageLoop::current()->QuitWhenIdle();
morrita17137e62015-06-23 22:29:36579 }
580
581 void set_sender(IPC::Sender* sender) { sender_ = sender; }
582
583 private:
584 IPC::Sender* sender_;
585};
586
sammc57ed9f982016-03-10 06:28:35587DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendOkClient,
588 ChannelClient) {
morrita17137e62015-06-23 22:29:36589 ListenerSendingOneOk listener;
sammc57ed9f982016-03-10 06:28:35590 Connect(&listener);
591 listener.set_sender(channel());
morrita17137e62015-06-23 22:29:36592
fdoray8e32586852016-06-22 19:56:16593 base::RunLoop().Run();
morrita17137e62015-06-23 22:29:36594
sammc57ed9f982016-03-10 06:28:35595 Close();
morrita17137e62015-06-23 22:29:36596}
597
rockot7c6bf952016-07-14 00:34:11598class 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
652const int ListenerWithSimpleAssociatedInterface::kNumMessages = 1000;
653
654class 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
685TEST_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
701DEFINE_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
rockot8d890f62016-07-14 16:37:14712class 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
737class 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
754class 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
811const int ListenerWithSimpleProxyAssociatedInterface::kNumMessages = 1000;
812
813TEST_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
829class 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
845class 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
858DEFINE_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]64860882014-08-04 23:44:17880#if defined(OS_POSIX)
rockot8d890f62016-07-14 16:37:14881
[email protected]64860882014-08-04 23:44:17882class ListenerThatExpectsFile : public IPC::Listener {
883 public:
sammc57ed9f982016-03-10 06:28:35884 ListenerThatExpectsFile() : sender_(NULL) {}
[email protected]64860882014-08-04 23:44:17885
dchengfe61fca2014-10-22 02:29:52886 ~ListenerThatExpectsFile() override {}
[email protected]64860882014-08-04 23:44:17887
dchengfe61fca2014-10-22 02:29:52888 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25889 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30890 HandleSendingHelper::ReadReceivedFile(message, &iter);
[email protected]64860882014-08-04 23:44:17891 ListenerThatExpectsOK::SendOK(sender_);
892 return true;
893 }
894
amistry6c70caea2016-06-09 03:08:29895 void OnChannelError() override {
896 base::MessageLoop::current()->QuitWhenIdle();
897 }
[email protected]64860882014-08-04 23:44:17898
[email protected]64860882014-08-04 23:44:17899 void set_sender(IPC::Sender* sender) { sender_ = sender; }
900
901 private:
902 IPC::Sender* sender_;
903};
904
amistry0027a0952016-05-03 00:52:47905TEST_F(IPCChannelMojoTest, SendPlatformHandle) {
rockotcbca72f2015-03-03 16:31:04906 InitWithMojo("IPCChannelMojoTestSendPlatformHandleClient");
[email protected]64860882014-08-04 23:44:17907
908 ListenerThatExpectsOK listener;
morrita373af03b2014-09-09 19:35:24909 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17910 ASSERT_TRUE(ConnectChannel());
[email protected]64860882014-08-04 23:44:17911
amistry20e2b1d62016-06-23 06:12:35912 base::ScopedTempDir temp_dir;
913 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
914 base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.path()),
[email protected]64860882014-08-04 23:44:17915 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
morrita81b17e02015-02-06 00:58:30916 base::File::FLAG_READ);
917 HandleSendingHelper::WriteFileThenSend(channel(), file);
fdoray8e32586852016-06-22 19:56:16918 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17919
sammc57ed9f982016-03-10 06:28:35920 channel()->Close();
[email protected]64860882014-08-04 23:44:17921
922 EXPECT_TRUE(WaitForClientShutdown());
923 DestroyChannel();
924}
925
sammc57ed9f982016-03-10 06:28:35926DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendPlatformHandleClient,
927 ChannelClient) {
[email protected]64860882014-08-04 23:44:17928 ListenerThatExpectsFile listener;
sammc57ed9f982016-03-10 06:28:35929 Connect(&listener);
930 listener.set_sender(channel());
[email protected]64860882014-08-04 23:44:17931
fdoray8e32586852016-06-22 19:56:16932 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17933
sammc57ed9f982016-03-10 06:28:35934 Close();
[email protected]64860882014-08-04 23:44:17935}
morrita81b17e02015-02-06 00:58:30936
937class ListenerThatExpectsFileAndPipe : public IPC::Listener {
938 public:
939 ListenerThatExpectsFileAndPipe() : sender_(NULL) {}
940
941 ~ListenerThatExpectsFileAndPipe() override {}
942
943 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25944 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30945 HandleSendingHelper::ReadReceivedFile(message, &iter);
946 HandleSendingHelper::ReadReceivedPipe(message, &iter);
morrita81b17e02015-02-06 00:58:30947 ListenerThatExpectsOK::SendOK(sender_);
948 return true;
949 }
950
amistry6c70caea2016-06-09 03:08:29951 void OnChannelError() override {
952 base::MessageLoop::current()->QuitWhenIdle();
953 }
morrita81b17e02015-02-06 00:58:30954
955 void set_sender(IPC::Sender* sender) { sender_ = sender; }
956
957 private:
958 IPC::Sender* sender_;
959};
960
amistry0027a0952016-05-03 00:52:47961TEST_F(IPCChannelMojoTest, SendPlatformHandleAndPipe) {
rockotcbca72f2015-03-03 16:31:04962 InitWithMojo("IPCChannelMojoTestSendPlatformHandleAndPipeClient");
morrita81b17e02015-02-06 00:58:30963
964 ListenerThatExpectsOK listener;
965 CreateChannel(&listener);
966 ASSERT_TRUE(ConnectChannel());
morrita81b17e02015-02-06 00:58:30967
amistry20e2b1d62016-06-23 06:12:35968 base::ScopedTempDir temp_dir;
969 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
970 base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.path()),
morrita81b17e02015-02-06 00:58:30971 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
972 base::File::FLAG_READ);
973 TestingMessagePipe pipe;
974 HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe);
975
fdoray8e32586852016-06-22 19:56:16976 base::RunLoop().Run();
sammc57ed9f982016-03-10 06:28:35977 channel()->Close();
morrita81b17e02015-02-06 00:58:30978
979 EXPECT_TRUE(WaitForClientShutdown());
980 DestroyChannel();
981}
982
sammc57ed9f982016-03-10 06:28:35983DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
984 IPCChannelMojoTestSendPlatformHandleAndPipeClient,
985 ChannelClient) {
morrita81b17e02015-02-06 00:58:30986 ListenerThatExpectsFileAndPipe listener;
sammc57ed9f982016-03-10 06:28:35987 Connect(&listener);
988 listener.set_sender(channel());
morrita81b17e02015-02-06 00:58:30989
fdoray8e32586852016-06-22 19:56:16990 base::RunLoop().Run();
morrita81b17e02015-02-06 00:58:30991
sammc57ed9f982016-03-10 06:28:35992 Close();
morrita81b17e02015-02-06 00:58:30993}
994
rockot7c6bf952016-07-14 00:34:11995#endif // defined(OS_POSIX)
[email protected]64860882014-08-04 23:44:17996
morrita0bd20bd2015-02-25 20:11:27997#if defined(OS_LINUX)
998
999const base::ProcessId kMagicChildId = 54321;
1000
1001class ListenerThatVerifiesPeerPid : public IPC::Listener {
1002 public:
tfarina10a5c062015-09-04 18:47:571003 void OnChannelConnected(int32_t peer_pid) override {
morrita0bd20bd2015-02-25 20:11:271004 EXPECT_EQ(peer_pid, kMagicChildId);
ki.stfua21ed8c2015-10-12 17:26:001005 base::MessageLoop::current()->QuitWhenIdle();
morrita0bd20bd2015-02-25 20:11:271006 }
1007
1008 bool OnMessageReceived(const IPC::Message& message) override {
1009 NOTREACHED();
1010 return true;
1011 }
1012};
1013
sammc57ed9f982016-03-10 06:28:351014TEST_F(IPCChannelMojoTest, VerifyGlobalPid) {
rockotcbca72f2015-03-03 16:31:041015 InitWithMojo("IPCChannelMojoTestVerifyGlobalPidClient");
morrita0bd20bd2015-02-25 20:11:271016
1017 ListenerThatVerifiesPeerPid listener;
1018 CreateChannel(&listener);
1019 ASSERT_TRUE(ConnectChannel());
morrita0bd20bd2015-02-25 20:11:271020
1021 base::MessageLoop::current()->Run();
rockotcbca72f2015-03-03 16:31:041022 channel()->Close();
morrita0bd20bd2015-02-25 20:11:271023
1024 EXPECT_TRUE(WaitForClientShutdown());
1025 DestroyChannel();
1026}
1027
sammc57ed9f982016-03-10 06:28:351028DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestVerifyGlobalPidClient,
1029 ChannelClient) {
morrita0bd20bd2015-02-25 20:11:271030 IPC::Channel::SetGlobalPid(kMagicChildId);
1031 ListenerThatQuits listener;
sammc57ed9f982016-03-10 06:28:351032 Connect(&listener);
morrita0bd20bd2015-02-25 20:11:271033
1034 base::MessageLoop::current()->Run();
1035
sammc57ed9f982016-03-10 06:28:351036 Close();
morrita0bd20bd2015-02-25 20:11:271037}
1038
sammc57ed9f982016-03-10 06:28:351039#endif // OS_LINUX
morrita0bd20bd2015-02-25 20:11:271040
[email protected]64860882014-08-04 23:44:171041} // namespace