aw: Disable ipc command buffer by default
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo_unittest.cc
blob02bdcf17975626d4235b83b1f5b98a25d46246db
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"
7 #include <stdint.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"
27 #if defined(OS_POSIX)
28 #include "base/file_descriptor_posix.h"
29 #include "ipc/ipc_platform_file_attachment_posix.h"
30 #endif
32 namespace {
34 class ListenerThatExpectsOK : public IPC::Listener {
35 public:
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");
46 received_ok_ = true;
47 base::MessageLoop::current()->Quit();
48 return true;
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
54 // process dies.
55 DCHECK(received_ok_);
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));
65 private:
66 bool received_ok_;
69 class ChannelClient {
70 public:
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);
77 void Connect() {
78 CHECK(channel_->Connect());
81 void Close() {
82 channel_->Close();
84 base::RunLoop run_loop;
85 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
86 run_loop.QuitClosure());
87 run_loop.Run();
90 IPC::ChannelMojo* channel() const { return channel_.get(); }
92 private:
93 base::MessageLoopForIO main_message_loop_;
94 scoped_ptr<IPC::ChannelMojo> channel_;
97 class IPCChannelMojoTestBase : public IPCTestBase {
98 public:
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());
108 run_loop.Run();
110 IPCTestBase::TearDown();
114 class IPCChannelMojoTest : public IPCChannelMojoTestBase {
115 protected:
116 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
117 const IPC::ChannelHandle& handle,
118 base::SequencedTaskRunner* runner) override {
119 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle,
120 nullptr);
123 bool DidStartClient() override {
124 bool ok = IPCTestBase::DidStartClient();
125 DCHECK(ok);
126 return ok;
131 class TestChannelListenerWithExtraExpectations
132 : public IPC::TestChannelListener {
133 public:
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_; }
146 private:
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
153 #else
154 #define MAYBE_ConnectedFromClient ConnectedFromClient
155 #endif
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());
178 DestroyChannel();
181 // A long running process that connects to us
182 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestClient) {
183 TestChannelListenerWithExtraExpectations listener;
184 ChannelClient client(&listener, "IPCChannelMojoTestClient");
185 client.Connect();
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());
194 client.Close();
196 return 0;
199 class ListenerExpectingErrors : public IPC::Listener {
200 public:
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 {
212 has_error_ = true;
213 base::MessageLoop::current()->Quit();
216 bool has_error() const { return has_error_; }
218 private:
219 bool has_error_;
223 class IPCChannelMojoErrorTest : public IPCChannelMojoTestBase {
224 protected:
225 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
226 const IPC::ChannelHandle& handle,
227 base::SequencedTaskRunner* runner) override {
228 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle,
229 nullptr);
232 bool DidStartClient() override {
233 bool ok = IPCTestBase::DidStartClient();
234 DCHECK(ok);
235 return ok;
239 class ListenerThatQuits : public IPC::Listener {
240 public:
241 ListenerThatQuits() {
244 bool OnMessageReceived(const IPC::Message& message) override {
245 return true;
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");
257 client.Connect();
259 base::MessageLoop::current()->Run();
261 client.Close();
263 return 0;
266 // Times out on Android; see http://crbug.com/502290
267 #if defined(OS_ANDROID)
268 #define MAYBE_SendFailWithPendingMessages DISABLED_SendFailWithPendingMessages
269 #else
270 #define MAYBE_SendFailWithPendingMessages SendFailWithPendingMessages
271 #endif
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());
297 DestroyChannel();
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 {
310 public:
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()),
318 nullptr, 0, 0));
319 EXPECT_TRUE(
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;
333 EXPECT_TRUE(
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,
340 nullptr, 0));
341 EXPECT_EQ(content, GetSendingFileContent());
344 #if defined(OS_POSIX)
345 static base::FilePath GetSendingFilePath() {
346 base::FilePath path;
347 bool ok = PathService::Get(base::DIR_CACHE, &path);
348 EXPECT_TRUE(ok);
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());
355 file.Flush();
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,
368 base::File& file,
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) {
379 base::ScopedFD fd;
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());
387 #endif
390 class ListenerThatExpectsMessagePipe : public IPC::Listener {
391 public:
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_);
401 return true;
404 void OnChannelError() override { NOTREACHED(); }
406 void set_sender(IPC::Sender* sender) { sender_ = sender; }
408 private:
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
415 #else
416 #define MAYBE_SendMessagePipe SendMessagePipe
417 #endif
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());
433 DestroyChannel();
436 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendMessagePipeClient) {
437 ListenerThatExpectsMessagePipe listener;
438 ChannelClient client(&listener, "IPCChannelMojoTestSendMessagePipeClient");
439 client.Connect();
440 listener.set_sender(client.channel());
442 base::MessageLoop::current()->Run();
444 client.Close();
446 return 0;
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,
454 nullptr, 0));
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()),
462 nullptr, 0, 0));
465 class ListenerThatExpectsMessagePipeUsingParamTrait : public IPC::Listener {
466 public:
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,
476 &handle));
477 EXPECT_EQ(handle.is_valid(), receiving_valid_);
478 if (receiving_valid_) {
479 ReadOK(handle);
480 MojoClose(handle.value());
483 base::MessageLoop::current()->Quit();
484 ListenerThatExpectsOK::SendOK(sender_);
485 return true;
488 void OnChannelError() override { NOTREACHED(); }
489 void set_sender(IPC::Sender* sender) { sender_ = sender; }
491 private:
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);
501 client.Connect();
502 listener.set_sender(client.channel());
504 base::MessageLoop::current()->Run();
506 client.Close();
509 // Times out on Android; see http://crbug.com/502290
510 #if defined(OS_ANDROID)
511 #define MAYBE_ParamTraitValidMessagePipe DISABLED_ParamTraitValidMessagePipe
512 #else
513 #define MAYBE_ParamTraitValidMessagePipe ParamTraitValidMessagePipe
514 #endif
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());
535 DestroyChannel();
538 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitValidMessagePipeClient) {
539 ParamTraitMessagePipeClient(true, "ParamTraitValidMessagePipeClient");
540 return 0;
543 // Times out on Android; see http://crbug.com/502290
544 #if defined(OS_ANDROID)
545 #define MAYBE_ParamTraitInvalidMessagePipe DISABLED_ParamTraitInvalidMessagePipe
546 #else
547 #define MAYBE_ParamTraitInvalidMessagePipe ParamTraitInvalidMessagePipe
548 #endif
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(),
560 invalid_handle);
562 this->channel()->Send(message.release());
563 base::MessageLoop::current()->Run();
564 this->channel()->Close();
566 EXPECT_TRUE(WaitForClientShutdown());
567 DestroyChannel();
570 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitInvalidMessagePipeClient) {
571 ParamTraitMessagePipeClient(false, "ParamTraitInvalidMessagePipeClient");
572 return 0;
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());
588 DestroyChannel();
591 class ListenerSendingOneOk : public IPC::Listener {
592 public:
593 ListenerSendingOneOk() {
596 bool OnMessageReceived(const IPC::Message& message) override {
597 return true;
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; }
607 private:
608 IPC::Sender* sender_;
611 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendOkClient) {
612 ListenerSendingOneOk listener;
613 ChannelClient client(&listener, "IPCChannelMojoTestSendOkClient");
614 client.Connect();
615 listener.set_sender(client.channel());
617 base::MessageLoop::current()->Run();
619 client.Close();
621 return 0;
624 #if defined(OS_WIN)
625 class IPCChannelMojoDeadHandleTest : public IPCChannelMojoTestBase {
626 protected:
627 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
628 const IPC::ChannelHandle& handle,
629 base::SequencedTaskRunner* runner) override {
630 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle,
631 nullptr);
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);
641 return true;
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());
666 DestroyChannel();
669 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestDoNothingClient) {
670 ListenerThatQuits listener;
671 ChannelClient client(&listener, "IPCChannelMojoTestDoNothingClient");
672 client.Connect();
674 // Quits without running the message loop as this client won't
675 // receive any messages from the server.
677 return 0;
679 #endif
681 #if defined(OS_POSIX)
682 class ListenerThatExpectsFile : public IPC::Listener {
683 public:
684 ListenerThatExpectsFile()
685 : sender_(NULL) {}
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_);
694 return true;
697 void OnChannelError() override {
698 NOTREACHED();
701 void set_sender(IPC::Sender* sender) { sender_ = sender; }
703 private:
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
710 #else
711 #define MAYBE_SendPlatformHandle SendPlatformHandle
712 #endif
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());
730 DestroyChannel();
733 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendPlatformHandleClient) {
734 ListenerThatExpectsFile listener;
735 ChannelClient client(
736 &listener, "IPCChannelMojoTestSendPlatformHandleClient");
737 client.Connect();
738 listener.set_sender(client.channel());
740 base::MessageLoop::current()->Run();
742 client.Close();
744 return 0;
747 class ListenerThatExpectsFileAndPipe : public IPC::Listener {
748 public:
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_);
759 return true;
762 void OnChannelError() override { NOTREACHED(); }
764 void set_sender(IPC::Sender* sender) { sender_ = sender; }
766 private:
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
773 #else
774 #define MAYBE_SendPlatformHandleAndPipe SendPlatformHandleAndPipe
775 #endif
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());
794 DestroyChannel();
797 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(
798 IPCChannelMojoTestSendPlatformHandleAndPipeClient) {
799 ListenerThatExpectsFileAndPipe listener;
800 ChannelClient client(&listener,
801 "IPCChannelMojoTestSendPlatformHandleAndPipeClient");
802 client.Connect();
803 listener.set_sender(client.channel());
805 base::MessageLoop::current()->Run();
807 client.Close();
809 return 0;
812 #endif
814 #if defined(OS_LINUX)
816 const base::ProcessId kMagicChildId = 54321;
818 class ListenerThatVerifiesPeerPid : public IPC::Listener {
819 public:
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 {
826 NOTREACHED();
827 return true;
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();
840 channel()->Close();
842 EXPECT_TRUE(WaitForClientShutdown());
843 DestroyChannel();
846 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestVerifyGlobalPidClient) {
847 IPC::Channel::SetGlobalPid(kMagicChildId);
848 ListenerThatQuits listener;
849 ChannelClient client(&listener,
850 "IPCChannelMojoTestVerifyGlobalPidClient");
851 client.Connect();
853 base::MessageLoop::current()->Run();
855 client.Close();
857 return 0;
860 #endif // OS_LINUX
862 } // namespace