1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ipc/mojo/ipc_channel_mojo.h"
9 #include "base/base_paths.h"
10 #include "base/files/file.h"
11 #include "base/location.h"
12 #include "base/path_service.h"
13 #include "base/pickle.h"
14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/test/test_timeouts.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "base/threading/thread.h"
19 #include "ipc/ipc_message.h"
20 #include "ipc/ipc_test_base.h"
21 #include "ipc/ipc_test_channel_listener.h"
22 #include "ipc/mojo/ipc_mojo_handle_attachment.h"
23 #include "ipc/mojo/ipc_mojo_message_helper.h"
24 #include "ipc/mojo/ipc_mojo_param_traits.h"
25 #include "ipc/mojo/scoped_ipc_support.h"
28 #include "base/file_descriptor_posix.h"
29 #include "ipc/ipc_platform_file_attachment_posix.h"
34 class ListenerThatExpectsOK
: public IPC::Listener
{
36 ListenerThatExpectsOK()
37 : received_ok_(false) {}
39 ~ListenerThatExpectsOK() override
{}
41 bool OnMessageReceived(const IPC::Message
& message
) override
{
42 base::PickleIterator
iter(message
);
43 std::string should_be_ok
;
44 EXPECT_TRUE(iter
.ReadString(&should_be_ok
));
45 EXPECT_EQ(should_be_ok
, "OK");
47 base::MessageLoop::current()->Quit();
51 void OnChannelError() override
{
52 // The connection should be healthy while the listener is waiting
53 // message. An error can occur after that because the peer
58 static void SendOK(IPC::Sender
* sender
) {
59 IPC::Message
* message
= new IPC::Message(
60 0, 2, IPC::Message::PRIORITY_NORMAL
);
61 message
->WriteString(std::string("OK"));
62 ASSERT_TRUE(sender
->Send(message
));
71 explicit ChannelClient(IPC::Listener
* listener
, const char* name
) {
72 channel_
= IPC::ChannelMojo::Create(
73 main_message_loop_
.task_runner(), IPCTestBase::GetChannelName(name
),
74 IPC::Channel::MODE_CLIENT
, listener
, nullptr);
78 CHECK(channel_
->Connect());
84 base::RunLoop run_loop
;
85 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
86 run_loop
.QuitClosure());
90 IPC::ChannelMojo
* channel() const { return channel_
.get(); }
93 base::MessageLoopForIO main_message_loop_
;
94 scoped_ptr
<IPC::ChannelMojo
> channel_
;
97 class IPCChannelMojoTestBase
: public IPCTestBase
{
99 void InitWithMojo(const std::string
& test_client_name
) {
100 Init(test_client_name
);
103 void TearDown() override
{
104 // Make sure Mojo IPC support is properly shutdown on the I/O loop before
105 // TearDown continues.
106 base::RunLoop run_loop
;
107 task_runner()->PostTask(FROM_HERE
, run_loop
.QuitClosure());
110 IPCTestBase::TearDown();
114 class IPCChannelMojoTest
: public IPCChannelMojoTestBase
{
116 scoped_ptr
<IPC::ChannelFactory
> CreateChannelFactory(
117 const IPC::ChannelHandle
& handle
,
118 base::SequencedTaskRunner
* runner
) override
{
119 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle
,
123 bool DidStartClient() override
{
124 bool ok
= IPCTestBase::DidStartClient();
131 class TestChannelListenerWithExtraExpectations
132 : public IPC::TestChannelListener
{
134 TestChannelListenerWithExtraExpectations()
135 : is_connected_called_(false) {
138 void OnChannelConnected(int32_t peer_pid
) override
{
139 IPC::TestChannelListener::OnChannelConnected(peer_pid
);
140 EXPECT_TRUE(base::kNullProcessId
!= peer_pid
);
141 is_connected_called_
= true;
144 bool is_connected_called() const { return is_connected_called_
; }
147 bool is_connected_called_
;
150 // Times out on Android; see http://crbug.com/502290
151 #if defined(OS_ANDROID)
152 #define MAYBE_ConnectedFromClient DISABLED_ConnectedFromClient
154 #define MAYBE_ConnectedFromClient ConnectedFromClient
156 TEST_F(IPCChannelMojoTest
, MAYBE_ConnectedFromClient
) {
157 InitWithMojo("IPCChannelMojoTestClient");
159 // Set up IPC channel and start client.
160 TestChannelListenerWithExtraExpectations listener
;
161 CreateChannel(&listener
);
162 listener
.Init(sender());
163 ASSERT_TRUE(ConnectChannel());
164 ASSERT_TRUE(StartClient());
166 IPC::TestChannelListener::SendOneMessage(
167 sender(), "hello from parent");
169 base::MessageLoop::current()->Run();
170 EXPECT_TRUE(base::kNullProcessId
!= this->channel()->GetPeerPID());
172 this->channel()->Close();
174 EXPECT_TRUE(WaitForClientShutdown());
175 EXPECT_TRUE(listener
.is_connected_called());
176 EXPECT_TRUE(listener
.HasSentAll());
181 // A long running process that connects to us
182 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestClient
) {
183 TestChannelListenerWithExtraExpectations listener
;
184 ChannelClient
client(&listener
, "IPCChannelMojoTestClient");
186 listener
.Init(client
.channel());
188 IPC::TestChannelListener::SendOneMessage(
189 client
.channel(), "hello from child");
190 base::MessageLoop::current()->Run();
191 EXPECT_TRUE(listener
.is_connected_called());
192 EXPECT_TRUE(listener
.HasSentAll());
199 class ListenerExpectingErrors
: public IPC::Listener
{
201 ListenerExpectingErrors()
202 : has_error_(false) {
205 void OnChannelConnected(int32_t peer_pid
) override
{
206 base::MessageLoop::current()->Quit();
209 bool OnMessageReceived(const IPC::Message
& message
) override
{ return true; }
211 void OnChannelError() override
{
213 base::MessageLoop::current()->Quit();
216 bool has_error() const { return has_error_
; }
223 class IPCChannelMojoErrorTest
: public IPCChannelMojoTestBase
{
225 scoped_ptr
<IPC::ChannelFactory
> CreateChannelFactory(
226 const IPC::ChannelHandle
& handle
,
227 base::SequencedTaskRunner
* runner
) override
{
228 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle
,
232 bool DidStartClient() override
{
233 bool ok
= IPCTestBase::DidStartClient();
239 class ListenerThatQuits
: public IPC::Listener
{
241 ListenerThatQuits() {
244 bool OnMessageReceived(const IPC::Message
& message
) override
{
248 void OnChannelConnected(int32_t peer_pid
) override
{
249 base::MessageLoop::current()->Quit();
253 // A long running process that connects to us.
254 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient
) {
255 ListenerThatQuits listener
;
256 ChannelClient
client(&listener
, "IPCChannelMojoErraticTestClient");
259 base::MessageLoop::current()->Run();
266 // Times out on Android; see http://crbug.com/502290
267 #if defined(OS_ANDROID)
268 #define MAYBE_SendFailWithPendingMessages DISABLED_SendFailWithPendingMessages
270 #define MAYBE_SendFailWithPendingMessages SendFailWithPendingMessages
272 TEST_F(IPCChannelMojoErrorTest
, MAYBE_SendFailWithPendingMessages
) {
273 InitWithMojo("IPCChannelMojoErraticTestClient");
275 // Set up IPC channel and start client.
276 ListenerExpectingErrors listener
;
277 CreateChannel(&listener
);
278 ASSERT_TRUE(ConnectChannel());
280 // This matches a value in mojo/edk/system/constants.h
281 const int kMaxMessageNumBytes
= 4 * 1024 * 1024;
282 std::string
overly_large_data(kMaxMessageNumBytes
, '*');
283 // This messages are queued as pending.
284 for (size_t i
= 0; i
< 10; ++i
) {
285 IPC::TestChannelListener::SendOneMessage(
286 sender(), overly_large_data
.c_str());
289 ASSERT_TRUE(StartClient());
290 base::MessageLoop::current()->Run();
292 this->channel()->Close();
294 EXPECT_TRUE(WaitForClientShutdown());
295 EXPECT_TRUE(listener
.has_error());
300 struct TestingMessagePipe
{
301 TestingMessagePipe() {
302 EXPECT_EQ(MOJO_RESULT_OK
, mojo::CreateMessagePipe(nullptr, &self
, &peer
));
305 mojo::ScopedMessagePipeHandle self
;
306 mojo::ScopedMessagePipeHandle peer
;
309 class HandleSendingHelper
{
311 static std::string
GetSendingFileContent() { return "Hello"; }
313 static void WritePipe(IPC::Message
* message
, TestingMessagePipe
* pipe
) {
314 std::string content
= HandleSendingHelper::GetSendingFileContent();
315 EXPECT_EQ(MOJO_RESULT_OK
,
316 mojo::WriteMessageRaw(pipe
->self
.get(), &content
[0],
317 static_cast<uint32_t>(content
.size()),
320 IPC::MojoMessageHelper::WriteMessagePipeTo(message
, pipe
->peer
.Pass()));
323 static void WritePipeThenSend(IPC::Sender
* sender
, TestingMessagePipe
* pipe
) {
324 IPC::Message
* message
=
325 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL
);
326 WritePipe(message
, pipe
);
327 ASSERT_TRUE(sender
->Send(message
));
330 static void ReadReceivedPipe(const IPC::Message
& message
,
331 base::PickleIterator
* iter
) {
332 mojo::ScopedMessagePipeHandle pipe
;
334 IPC::MojoMessageHelper::ReadMessagePipeFrom(&message
, iter
, &pipe
));
335 std::string
content(GetSendingFileContent().size(), ' ');
337 uint32_t num_bytes
= static_cast<uint32_t>(content
.size());
338 EXPECT_EQ(MOJO_RESULT_OK
,
339 mojo::ReadMessageRaw(pipe
.get(), &content
[0], &num_bytes
, nullptr,
341 EXPECT_EQ(content
, GetSendingFileContent());
344 #if defined(OS_POSIX)
345 static base::FilePath
GetSendingFilePath() {
347 bool ok
= PathService::Get(base::DIR_CACHE
, &path
);
349 return path
.Append("ListenerThatExpectsFile.txt");
352 static void WriteFile(IPC::Message
* message
, base::File
& file
) {
353 std::string content
= GetSendingFileContent();
354 file
.WriteAtCurrentPos(content
.data(), content
.size());
356 message
->WriteAttachment(new IPC::internal::PlatformFileAttachment(
357 base::ScopedFD(file
.TakePlatformFile())));
360 static void WriteFileThenSend(IPC::Sender
* sender
, base::File
& file
) {
361 IPC::Message
* message
=
362 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL
);
363 WriteFile(message
, file
);
364 ASSERT_TRUE(sender
->Send(message
));
367 static void WriteFileAndPipeThenSend(IPC::Sender
* sender
,
369 TestingMessagePipe
* pipe
) {
370 IPC::Message
* message
=
371 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL
);
372 WriteFile(message
, file
);
373 WritePipe(message
, pipe
);
374 ASSERT_TRUE(sender
->Send(message
));
377 static void ReadReceivedFile(const IPC::Message
& message
,
378 base::PickleIterator
* iter
) {
380 scoped_refptr
<IPC::MessageAttachment
> attachment
;
381 EXPECT_TRUE(message
.ReadAttachment(iter
, &attachment
));
382 base::File
file(attachment
->TakePlatformFile());
383 std::string
content(GetSendingFileContent().size(), ' ');
384 file
.Read(0, &content
[0], content
.size());
385 EXPECT_EQ(content
, GetSendingFileContent());
390 class ListenerThatExpectsMessagePipe
: public IPC::Listener
{
392 ListenerThatExpectsMessagePipe() : sender_(NULL
) {}
394 ~ListenerThatExpectsMessagePipe() override
{}
396 bool OnMessageReceived(const IPC::Message
& message
) override
{
397 base::PickleIterator
iter(message
);
398 HandleSendingHelper::ReadReceivedPipe(message
, &iter
);
399 base::MessageLoop::current()->Quit();
400 ListenerThatExpectsOK::SendOK(sender_
);
404 void OnChannelError() override
{ NOTREACHED(); }
406 void set_sender(IPC::Sender
* sender
) { sender_
= sender
; }
409 IPC::Sender
* sender_
;
412 // Times out on Android; see http://crbug.com/502290
413 #if defined(OS_ANDROID)
414 #define MAYBE_SendMessagePipe DISABLED_SendMessagePipe
416 #define MAYBE_SendMessagePipe SendMessagePipe
418 TEST_F(IPCChannelMojoTest
, MAYBE_SendMessagePipe
) {
419 InitWithMojo("IPCChannelMojoTestSendMessagePipeClient");
421 ListenerThatExpectsOK listener
;
422 CreateChannel(&listener
);
423 ASSERT_TRUE(ConnectChannel());
424 ASSERT_TRUE(StartClient());
426 TestingMessagePipe pipe
;
427 HandleSendingHelper::WritePipeThenSend(channel(), &pipe
);
429 base::MessageLoop::current()->Run();
430 this->channel()->Close();
432 EXPECT_TRUE(WaitForClientShutdown());
436 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendMessagePipeClient
) {
437 ListenerThatExpectsMessagePipe listener
;
438 ChannelClient
client(&listener
, "IPCChannelMojoTestSendMessagePipeClient");
440 listener
.set_sender(client
.channel());
442 base::MessageLoop::current()->Run();
449 void ReadOK(mojo::MessagePipeHandle pipe
) {
450 std::string
should_be_ok("xx");
451 uint32_t num_bytes
= static_cast<uint32_t>(should_be_ok
.size());
452 CHECK_EQ(MOJO_RESULT_OK
,
453 mojo::ReadMessageRaw(pipe
, &should_be_ok
[0], &num_bytes
, nullptr,
455 EXPECT_EQ(should_be_ok
, std::string("OK"));
458 void WriteOK(mojo::MessagePipeHandle pipe
) {
459 std::string
ok("OK");
460 CHECK_EQ(MOJO_RESULT_OK
,
461 mojo::WriteMessageRaw(pipe
, &ok
[0], static_cast<uint32_t>(ok
.size()),
465 class ListenerThatExpectsMessagePipeUsingParamTrait
: public IPC::Listener
{
467 explicit ListenerThatExpectsMessagePipeUsingParamTrait(bool receiving_valid
)
468 : sender_(NULL
), receiving_valid_(receiving_valid
) {}
470 ~ListenerThatExpectsMessagePipeUsingParamTrait() override
{}
472 bool OnMessageReceived(const IPC::Message
& message
) override
{
473 base::PickleIterator
iter(message
);
474 mojo::MessagePipeHandle handle
;
475 EXPECT_TRUE(IPC::ParamTraits
<mojo::MessagePipeHandle
>::Read(&message
, &iter
,
477 EXPECT_EQ(handle
.is_valid(), receiving_valid_
);
478 if (receiving_valid_
) {
480 MojoClose(handle
.value());
483 base::MessageLoop::current()->Quit();
484 ListenerThatExpectsOK::SendOK(sender_
);
488 void OnChannelError() override
{ NOTREACHED(); }
489 void set_sender(IPC::Sender
* sender
) { sender_
= sender
; }
492 IPC::Sender
* sender_
;
493 bool receiving_valid_
;
496 void ParamTraitMessagePipeClient(bool receiving_valid_handle
,
497 const char* channel_name
) {
498 ListenerThatExpectsMessagePipeUsingParamTrait
listener(
499 receiving_valid_handle
);
500 ChannelClient
client(&listener
, channel_name
);
502 listener
.set_sender(client
.channel());
504 base::MessageLoop::current()->Run();
509 // Times out on Android; see http://crbug.com/502290
510 #if defined(OS_ANDROID)
511 #define MAYBE_ParamTraitValidMessagePipe DISABLED_ParamTraitValidMessagePipe
513 #define MAYBE_ParamTraitValidMessagePipe ParamTraitValidMessagePipe
515 TEST_F(IPCChannelMojoTest
, MAYBE_ParamTraitValidMessagePipe
) {
516 InitWithMojo("ParamTraitValidMessagePipeClient");
518 ListenerThatExpectsOK listener
;
519 CreateChannel(&listener
);
520 ASSERT_TRUE(ConnectChannel());
521 ASSERT_TRUE(StartClient());
523 TestingMessagePipe pipe
;
525 scoped_ptr
<IPC::Message
> message(new IPC::Message());
526 IPC::ParamTraits
<mojo::MessagePipeHandle
>::Write(message
.get(),
527 pipe
.peer
.release());
528 WriteOK(pipe
.self
.get());
530 this->channel()->Send(message
.release());
531 base::MessageLoop::current()->Run();
532 this->channel()->Close();
534 EXPECT_TRUE(WaitForClientShutdown());
538 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitValidMessagePipeClient
) {
539 ParamTraitMessagePipeClient(true, "ParamTraitValidMessagePipeClient");
543 // Times out on Android; see http://crbug.com/502290
544 #if defined(OS_ANDROID)
545 #define MAYBE_ParamTraitInvalidMessagePipe DISABLED_ParamTraitInvalidMessagePipe
547 #define MAYBE_ParamTraitInvalidMessagePipe ParamTraitInvalidMessagePipe
549 TEST_F(IPCChannelMojoTest
, MAYBE_ParamTraitInvalidMessagePipe
) {
550 InitWithMojo("ParamTraitInvalidMessagePipeClient");
552 ListenerThatExpectsOK listener
;
553 CreateChannel(&listener
);
554 ASSERT_TRUE(ConnectChannel());
555 ASSERT_TRUE(StartClient());
557 mojo::MessagePipeHandle invalid_handle
;
558 scoped_ptr
<IPC::Message
> message(new IPC::Message());
559 IPC::ParamTraits
<mojo::MessagePipeHandle
>::Write(message
.get(),
562 this->channel()->Send(message
.release());
563 base::MessageLoop::current()->Run();
564 this->channel()->Close();
566 EXPECT_TRUE(WaitForClientShutdown());
570 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitInvalidMessagePipeClient
) {
571 ParamTraitMessagePipeClient(false, "ParamTraitInvalidMessagePipeClient");
575 TEST_F(IPCChannelMojoTest
, SendFailAfterClose
) {
576 InitWithMojo("IPCChannelMojoTestSendOkClient");
578 ListenerThatExpectsOK listener
;
579 CreateChannel(&listener
);
580 ASSERT_TRUE(ConnectChannel());
581 ASSERT_TRUE(StartClient());
583 base::MessageLoop::current()->Run();
584 this->channel()->Close();
585 ASSERT_FALSE(this->channel()->Send(new IPC::Message()));
587 EXPECT_TRUE(WaitForClientShutdown());
591 class ListenerSendingOneOk
: public IPC::Listener
{
593 ListenerSendingOneOk() {
596 bool OnMessageReceived(const IPC::Message
& message
) override
{
600 void OnChannelConnected(int32_t peer_pid
) override
{
601 ListenerThatExpectsOK::SendOK(sender_
);
602 base::MessageLoop::current()->Quit();
605 void set_sender(IPC::Sender
* sender
) { sender_
= sender
; }
608 IPC::Sender
* sender_
;
611 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendOkClient
) {
612 ListenerSendingOneOk listener
;
613 ChannelClient
client(&listener
, "IPCChannelMojoTestSendOkClient");
615 listener
.set_sender(client
.channel());
617 base::MessageLoop::current()->Run();
625 class IPCChannelMojoDeadHandleTest
: public IPCChannelMojoTestBase
{
627 scoped_ptr
<IPC::ChannelFactory
> CreateChannelFactory(
628 const IPC::ChannelHandle
& handle
,
629 base::SequencedTaskRunner
* runner
) override
{
630 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle
,
634 bool DidStartClient() override
{
635 IPCTestBase::DidStartClient();
636 // const base::ProcessHandle client = client_process().Handle();
637 // Forces GetFileHandleForProcess() fail. It happens occasionally
638 // in production, so we should exercise it somehow.
639 // TODO(morrita): figure out how to safely test this. See crbug.com/464109.
640 // ::CloseHandle(client);
645 TEST_F(IPCChannelMojoDeadHandleTest
, InvalidClientHandle
) {
646 // Any client type is fine as it is going to be killed anyway.
647 InitWithMojo("IPCChannelMojoTestDoNothingClient");
649 // Set up IPC channel and start client.
650 ListenerExpectingErrors listener
;
651 CreateChannel(&listener
);
652 ASSERT_TRUE(ConnectChannel());
654 ASSERT_TRUE(StartClient());
655 base::MessageLoop::current()->Run();
657 this->channel()->Close();
659 // TODO(morrita): We need CloseHandle() call in DidStartClient(),
660 // which has been disabled since crrev.com/843113003, to
661 // make this fail. See crbug.com/464109.
662 // EXPECT_FALSE(WaitForClientShutdown());
663 WaitForClientShutdown();
664 EXPECT_TRUE(listener
.has_error());
669 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestDoNothingClient
) {
670 ListenerThatQuits listener
;
671 ChannelClient
client(&listener
, "IPCChannelMojoTestDoNothingClient");
674 // Quits without running the message loop as this client won't
675 // receive any messages from the server.
681 #if defined(OS_POSIX)
682 class ListenerThatExpectsFile
: public IPC::Listener
{
684 ListenerThatExpectsFile()
687 ~ListenerThatExpectsFile() override
{}
689 bool OnMessageReceived(const IPC::Message
& message
) override
{
690 base::PickleIterator
iter(message
);
691 HandleSendingHelper::ReadReceivedFile(message
, &iter
);
692 base::MessageLoop::current()->Quit();
693 ListenerThatExpectsOK::SendOK(sender_
);
697 void OnChannelError() override
{
701 void set_sender(IPC::Sender
* sender
) { sender_
= sender
; }
704 IPC::Sender
* sender_
;
707 // Times out on Android; see http://crbug.com/502290
708 #if defined(OS_ANDROID)
709 #define MAYBE_SendPlatformHandle DISABLED_SendPlatformHandle
711 #define MAYBE_SendPlatformHandle SendPlatformHandle
713 TEST_F(IPCChannelMojoTest
, MAYBE_SendPlatformHandle
) {
714 InitWithMojo("IPCChannelMojoTestSendPlatformHandleClient");
716 ListenerThatExpectsOK listener
;
717 CreateChannel(&listener
);
718 ASSERT_TRUE(ConnectChannel());
719 ASSERT_TRUE(StartClient());
721 base::File
file(HandleSendingHelper::GetSendingFilePath(),
722 base::File::FLAG_CREATE_ALWAYS
| base::File::FLAG_WRITE
|
723 base::File::FLAG_READ
);
724 HandleSendingHelper::WriteFileThenSend(channel(), file
);
725 base::MessageLoop::current()->Run();
727 this->channel()->Close();
729 EXPECT_TRUE(WaitForClientShutdown());
733 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendPlatformHandleClient
) {
734 ListenerThatExpectsFile listener
;
735 ChannelClient
client(
736 &listener
, "IPCChannelMojoTestSendPlatformHandleClient");
738 listener
.set_sender(client
.channel());
740 base::MessageLoop::current()->Run();
747 class ListenerThatExpectsFileAndPipe
: public IPC::Listener
{
749 ListenerThatExpectsFileAndPipe() : sender_(NULL
) {}
751 ~ListenerThatExpectsFileAndPipe() override
{}
753 bool OnMessageReceived(const IPC::Message
& message
) override
{
754 base::PickleIterator
iter(message
);
755 HandleSendingHelper::ReadReceivedFile(message
, &iter
);
756 HandleSendingHelper::ReadReceivedPipe(message
, &iter
);
757 base::MessageLoop::current()->Quit();
758 ListenerThatExpectsOK::SendOK(sender_
);
762 void OnChannelError() override
{ NOTREACHED(); }
764 void set_sender(IPC::Sender
* sender
) { sender_
= sender
; }
767 IPC::Sender
* sender_
;
770 // Times out on Android; see http://crbug.com/502290
771 #if defined(OS_ANDROID)
772 #define MAYBE_SendPlatformHandleAndPipe DISABLED_SendPlatformHandleAndPipe
774 #define MAYBE_SendPlatformHandleAndPipe SendPlatformHandleAndPipe
776 TEST_F(IPCChannelMojoTest
, MAYBE_SendPlatformHandleAndPipe
) {
777 InitWithMojo("IPCChannelMojoTestSendPlatformHandleAndPipeClient");
779 ListenerThatExpectsOK listener
;
780 CreateChannel(&listener
);
781 ASSERT_TRUE(ConnectChannel());
782 ASSERT_TRUE(StartClient());
784 base::File
file(HandleSendingHelper::GetSendingFilePath(),
785 base::File::FLAG_CREATE_ALWAYS
| base::File::FLAG_WRITE
|
786 base::File::FLAG_READ
);
787 TestingMessagePipe pipe
;
788 HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file
, &pipe
);
790 base::MessageLoop::current()->Run();
791 this->channel()->Close();
793 EXPECT_TRUE(WaitForClientShutdown());
797 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(
798 IPCChannelMojoTestSendPlatformHandleAndPipeClient
) {
799 ListenerThatExpectsFileAndPipe listener
;
800 ChannelClient
client(&listener
,
801 "IPCChannelMojoTestSendPlatformHandleAndPipeClient");
803 listener
.set_sender(client
.channel());
805 base::MessageLoop::current()->Run();
814 #if defined(OS_LINUX)
816 const base::ProcessId kMagicChildId
= 54321;
818 class ListenerThatVerifiesPeerPid
: public IPC::Listener
{
820 void OnChannelConnected(int32_t peer_pid
) override
{
821 EXPECT_EQ(peer_pid
, kMagicChildId
);
822 base::MessageLoop::current()->Quit();
825 bool OnMessageReceived(const IPC::Message
& message
) override
{
831 TEST_F(IPCChannelMojoTest
, VerifyGlobalPid
) {
832 InitWithMojo("IPCChannelMojoTestVerifyGlobalPidClient");
834 ListenerThatVerifiesPeerPid listener
;
835 CreateChannel(&listener
);
836 ASSERT_TRUE(ConnectChannel());
837 ASSERT_TRUE(StartClient());
839 base::MessageLoop::current()->Run();
842 EXPECT_TRUE(WaitForClientShutdown());
846 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestVerifyGlobalPidClient
) {
847 IPC::Channel::SetGlobalPid(kMagicChildId
);
848 ListenerThatQuits listener
;
849 ChannelClient
client(&listener
,
850 "IPCChannelMojoTestVerifyGlobalPidClient");
853 base::MessageLoop::current()->Run();