Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: ipc/ipc_channel_mojo_unittest.cc

Issue 2141253004: Revert of Adds associated interface support to IPC::Channel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@channel-bindings-1
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ipc/ipc_channel_mojo.cc ('k') | ipc/ipc_message_pipe_reader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ipc/ipc_channel_mojo.h" 5 #include "ipc/ipc_channel_mojo.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9
10 #include <memory> 9 #include <memory>
11 #include <utility> 10 #include <utility>
12 11
13 #include "base/base_paths.h" 12 #include "base/base_paths.h"
14 #include "base/files/file.h" 13 #include "base/files/file.h"
15 #include "base/files/scoped_temp_dir.h" 14 #include "base/files/scoped_temp_dir.h"
16 #include "base/location.h" 15 #include "base/location.h"
17 #include "base/path_service.h" 16 #include "base/path_service.h"
18 #include "base/pickle.h" 17 #include "base/pickle.h"
19 #include "base/run_loop.h" 18 #include "base/run_loop.h"
20 #include "base/single_thread_task_runner.h" 19 #include "base/single_thread_task_runner.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/test/test_io_thread.h" 20 #include "base/test/test_io_thread.h"
23 #include "base/test/test_timeouts.h" 21 #include "base/test/test_timeouts.h"
24 #include "base/threading/thread.h" 22 #include "base/threading/thread.h"
25 #include "base/threading/thread_task_runner_handle.h" 23 #include "base/threading/thread_task_runner_handle.h"
26 #include "build/build_config.h" 24 #include "build/build_config.h"
27 #include "ipc/ipc_message.h" 25 #include "ipc/ipc_message.h"
28 #include "ipc/ipc_mojo_handle_attachment.h" 26 #include "ipc/ipc_mojo_handle_attachment.h"
29 #include "ipc/ipc_mojo_message_helper.h" 27 #include "ipc/ipc_mojo_message_helper.h"
30 #include "ipc/ipc_mojo_param_traits.h" 28 #include "ipc/ipc_mojo_param_traits.h"
31 #include "ipc/ipc_test.mojom.h"
32 #include "ipc/ipc_test_base.h" 29 #include "ipc/ipc_test_base.h"
33 #include "ipc/ipc_test_channel_listener.h" 30 #include "ipc/ipc_test_channel_listener.h"
34 #include "mojo/edk/test/mojo_test_base.h" 31 #include "mojo/edk/test/mojo_test_base.h"
35 #include "mojo/edk/test/multiprocess_test_helper.h" 32 #include "mojo/edk/test/multiprocess_test_helper.h"
36 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
37 34
38 #if defined(OS_POSIX) 35 #if defined(OS_POSIX)
39 #include "base/file_descriptor_posix.h" 36 #include "base/file_descriptor_posix.h"
40 #include "ipc/ipc_platform_file_attachment_posix.h" 37 #include "ipc/ipc_platform_file_attachment_posix.h"
41 #endif 38 #endif
(...skipping 14 matching lines...) Expand all
56 test.Main(); \ 53 test.Main(); \
57 return (::testing::Test::HasFatalFailure() || \ 54 return (::testing::Test::HasFatalFailure() || \
58 ::testing::Test::HasNonfatalFailure()) \ 55 ::testing::Test::HasNonfatalFailure()) \
59 ? 1 \ 56 ? 1 \
60 : 0; \ 57 : 0; \
61 } \ 58 } \
62 void client_name##_MainFixture::Main() 59 void client_name##_MainFixture::Main()
63 60
64 namespace { 61 namespace {
65 62
66 void SendString(IPC::Sender* sender, const std::string& str) {
67 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
68 message->WriteString(str);
69 ASSERT_TRUE(sender->Send(message));
70 }
71
72 class ListenerThatExpectsOK : public IPC::Listener { 63 class ListenerThatExpectsOK : public IPC::Listener {
73 public: 64 public:
74 ListenerThatExpectsOK() : received_ok_(false) {} 65 ListenerThatExpectsOK() : received_ok_(false) {}
75 66
76 ~ListenerThatExpectsOK() override {} 67 ~ListenerThatExpectsOK() override {}
77 68
78 bool OnMessageReceived(const IPC::Message& message) override { 69 bool OnMessageReceived(const IPC::Message& message) override {
79 base::PickleIterator iter(message); 70 base::PickleIterator iter(message);
80 std::string should_be_ok; 71 std::string should_be_ok;
81 EXPECT_TRUE(iter.ReadString(&should_be_ok)); 72 EXPECT_TRUE(iter.ReadString(&should_be_ok));
82 EXPECT_EQ(should_be_ok, "OK"); 73 EXPECT_EQ(should_be_ok, "OK");
83 received_ok_ = true; 74 received_ok_ = true;
84 base::MessageLoop::current()->QuitWhenIdle(); 75 base::MessageLoop::current()->QuitWhenIdle();
85 return true; 76 return true;
86 } 77 }
87 78
88 void OnChannelError() override { 79 void OnChannelError() override {
89 // The connection should be healthy while the listener is waiting 80 // The connection should be healthy while the listener is waiting
90 // message. An error can occur after that because the peer 81 // message. An error can occur after that because the peer
91 // process dies. 82 // process dies.
92 DCHECK(received_ok_); 83 DCHECK(received_ok_);
93 } 84 }
94 85
95 static void SendOK(IPC::Sender* sender) { SendString(sender, "OK"); } 86 static void SendOK(IPC::Sender* sender) {
87 IPC::Message* message =
88 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
89 message->WriteString(std::string("OK"));
90 ASSERT_TRUE(sender->Send(message));
91 }
96 92
97 private: 93 private:
98 bool received_ok_; 94 bool received_ok_;
99 }; 95 };
100 96
101 class ChannelClient { 97 class ChannelClient {
102 public: 98 public:
103 void Init(mojo::ScopedMessagePipeHandle handle) { 99 void Init(mojo::ScopedMessagePipeHandle handle) {
104 handle_ = std::move(handle); 100 handle_ = std::move(handle);
105 } 101 }
(...skipping 15 matching lines...) Expand all
121 IPC::ChannelMojo* channel() const { return channel_.get(); } 117 IPC::ChannelMojo* channel() const { return channel_.get(); }
122 118
123 private: 119 private:
124 base::MessageLoopForIO main_message_loop_; 120 base::MessageLoopForIO main_message_loop_;
125 mojo::ScopedMessagePipeHandle handle_; 121 mojo::ScopedMessagePipeHandle handle_;
126 std::unique_ptr<IPC::ChannelMojo> channel_; 122 std::unique_ptr<IPC::ChannelMojo> channel_;
127 }; 123 };
128 124
129 class IPCChannelMojoTest : public testing::Test { 125 class IPCChannelMojoTest : public testing::Test {
130 public: 126 public:
131 IPCChannelMojoTest() {} 127 IPCChannelMojoTest() : io_thread_(base::TestIOThread::Mode::kAutoStart) {}
132 128
133 void TearDown() override { base::RunLoop().RunUntilIdle(); } 129 void TearDown() override { base::RunLoop().RunUntilIdle(); }
134 130
135 void InitWithMojo(const std::string& test_client_name) { 131 void InitWithMojo(const std::string& test_client_name) {
136 handle_ = helper_.StartChild(test_client_name); 132 handle_ = helper_.StartChild(test_client_name);
137 } 133 }
138 134
139 void CreateChannel(IPC::Listener* listener) { 135 void CreateChannel(IPC::Listener* listener) {
140 channel_ = IPC::ChannelMojo::Create(std::move(handle_), 136 channel_ = IPC::ChannelMojo::Create(std::move(handle_),
141 IPC::Channel::MODE_SERVER, listener); 137 IPC::Channel::MODE_SERVER, listener);
142 } 138 }
143 139
144 bool ConnectChannel() { return channel_->Connect(); } 140 bool ConnectChannel() { return channel_->Connect(); }
145 141
146 void DestroyChannel() { channel_.reset(); } 142 void DestroyChannel() { channel_.reset(); }
147 143
148 bool WaitForClientShutdown() { return helper_.WaitForChildTestShutdown(); } 144 bool WaitForClientShutdown() { return helper_.WaitForChildTestShutdown(); }
149 145
150 IPC::Sender* sender() { return channel(); } 146 IPC::Sender* sender() { return channel(); }
151 IPC::Channel* channel() { return channel_.get(); } 147 IPC::Channel* channel() { return channel_.get(); }
152 148
153 private: 149 private:
154 base::MessageLoop message_loop_; 150 base::MessageLoop message_loop_;
151 base::TestIOThread io_thread_;
155 mojo::edk::test::MultiprocessTestHelper helper_; 152 mojo::edk::test::MultiprocessTestHelper helper_;
156 mojo::ScopedMessagePipeHandle handle_; 153 mojo::ScopedMessagePipeHandle handle_;
157 std::unique_ptr<IPC::Channel> channel_; 154 std::unique_ptr<IPC::Channel> channel_;
158 }; 155 };
159 156
160 class TestChannelListenerWithExtraExpectations 157 class TestChannelListenerWithExtraExpectations
161 : public IPC::TestChannelListener { 158 : public IPC::TestChannelListener {
162 public: 159 public:
163 TestChannelListenerWithExtraExpectations() : is_connected_called_(false) {} 160 TestChannelListenerWithExtraExpectations() : is_connected_called_(false) {}
164 161
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 ChannelClient) { 575 ChannelClient) {
579 ListenerSendingOneOk listener; 576 ListenerSendingOneOk listener;
580 Connect(&listener); 577 Connect(&listener);
581 listener.set_sender(channel()); 578 listener.set_sender(channel());
582 579
583 base::RunLoop().Run(); 580 base::RunLoop().Run();
584 581
585 Close(); 582 Close();
586 } 583 }
587 584
588 class ListenerWithSimpleAssociatedInterface
589 : public IPC::Listener,
590 public IPC::mojom::SimpleTestDriver {
591 public:
592 static const int kNumMessages;
593
594 ListenerWithSimpleAssociatedInterface() : binding_(this) {}
595
596 ~ListenerWithSimpleAssociatedInterface() override {}
597
598 bool OnMessageReceived(const IPC::Message& message) override {
599 base::PickleIterator iter(message);
600 std::string should_be_expected;
601 EXPECT_TRUE(iter.ReadString(&should_be_expected));
602 EXPECT_EQ(should_be_expected, next_expected_string_);
603 num_messages_received_++;
604 return true;
605 }
606
607 void OnChannelError() override {
608 DCHECK(received_quit_);
609 }
610
611 void RegisterInterfaceFactory(IPC::Channel* channel) {
612 channel->GetAssociatedInterfaceSupport()->AddAssociatedInterface(
613 base::Bind(&ListenerWithSimpleAssociatedInterface::BindRequest,
614 base::Unretained(this)));
615 }
616
617 private:
618 // IPC::mojom::SimpleTestDriver:
619 void ExpectString(const mojo::String& str) override {
620 next_expected_string_ = str;
621 }
622
623 void RequestQuit(const RequestQuitCallback& callback) override {
624 EXPECT_EQ(kNumMessages, num_messages_received_);
625 received_quit_ = true;
626 callback.Run();
627 base::MessageLoop::current()->QuitWhenIdle();
628 }
629
630 void BindRequest(IPC::mojom::SimpleTestDriverAssociatedRequest request) {
631 DCHECK(!binding_.is_bound());
632 binding_.Bind(std::move(request));
633 }
634
635 std::string next_expected_string_;
636 int num_messages_received_ = 0;
637 bool received_quit_ = false;
638
639 mojo::AssociatedBinding<IPC::mojom::SimpleTestDriver> binding_;
640 };
641
642 const int ListenerWithSimpleAssociatedInterface::kNumMessages = 1000;
643
644 class ListenerSendingAssociatedMessages : public IPC::Listener {
645 public:
646 ListenerSendingAssociatedMessages() {}
647
648 bool OnMessageReceived(const IPC::Message& message) override { return true; }
649
650 void OnChannelConnected(int32_t peer_pid) override {
651 DCHECK(channel_);
652 channel_->GetAssociatedInterfaceSupport()->GetRemoteAssociatedInterface(
653 &driver_);
654
655 // Send a bunch of interleaved messages, alternating between the associated
656 // interface and a legacy IPC::Message.
657 for (int i = 0; i < ListenerWithSimpleAssociatedInterface::kNumMessages;
658 ++i) {
659 std::string str = base::StringPrintf("Hello! %d", i);
660 driver_->ExpectString(str);
661 SendString(channel_, str);
662 }
663 driver_->RequestQuit(base::Bind(&OnQuitAck));
664 }
665
666 void set_channel(IPC::Channel* channel) { channel_ = channel; }
667
668 private:
669 static void OnQuitAck() { base::MessageLoop::current()->QuitWhenIdle(); }
670
671 IPC::Channel* channel_ = nullptr;
672 IPC::mojom::SimpleTestDriverAssociatedPtr driver_;
673 };
674
675 TEST_F(IPCChannelMojoTest, SimpleAssociatedInterface) {
676 InitWithMojo("SimpleAssociatedInterfaceClient");
677
678 ListenerWithSimpleAssociatedInterface listener;
679 CreateChannel(&listener);
680 ASSERT_TRUE(ConnectChannel());
681
682 listener.RegisterInterfaceFactory(channel());
683
684 base::RunLoop().Run();
685 channel()->Close();
686
687 EXPECT_TRUE(WaitForClientShutdown());
688 DestroyChannel();
689 }
690
691 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(SimpleAssociatedInterfaceClient,
692 ChannelClient) {
693 ListenerSendingAssociatedMessages listener;
694 Connect(&listener);
695 listener.set_channel(channel());
696
697 base::RunLoop().Run();
698
699 Close();
700 }
701
702 #if defined(OS_POSIX) 585 #if defined(OS_POSIX)
703 class ListenerThatExpectsFile : public IPC::Listener { 586 class ListenerThatExpectsFile : public IPC::Listener {
704 public: 587 public:
705 ListenerThatExpectsFile() : sender_(NULL) {} 588 ListenerThatExpectsFile() : sender_(NULL) {}
706 589
707 ~ListenerThatExpectsFile() override {} 590 ~ListenerThatExpectsFile() override {}
708 591
709 bool OnMessageReceived(const IPC::Message& message) override { 592 bool OnMessageReceived(const IPC::Message& message) override {
710 base::PickleIterator iter(message); 593 base::PickleIterator iter(message);
711 HandleSendingHelper::ReadReceivedFile(message, &iter); 594 HandleSendingHelper::ReadReceivedFile(message, &iter);
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
806 ChannelClient) { 689 ChannelClient) {
807 ListenerThatExpectsFileAndPipe listener; 690 ListenerThatExpectsFileAndPipe listener;
808 Connect(&listener); 691 Connect(&listener);
809 listener.set_sender(channel()); 692 listener.set_sender(channel());
810 693
811 base::RunLoop().Run(); 694 base::RunLoop().Run();
812 695
813 Close(); 696 Close();
814 } 697 }
815 698
816 #endif // defined(OS_POSIX) 699 #endif
817 700
818 #if defined(OS_LINUX) 701 #if defined(OS_LINUX)
819 702
820 const base::ProcessId kMagicChildId = 54321; 703 const base::ProcessId kMagicChildId = 54321;
821 704
822 class ListenerThatVerifiesPeerPid : public IPC::Listener { 705 class ListenerThatVerifiesPeerPid : public IPC::Listener {
823 public: 706 public:
824 void OnChannelConnected(int32_t peer_pid) override { 707 void OnChannelConnected(int32_t peer_pid) override {
825 EXPECT_EQ(peer_pid, kMagicChildId); 708 EXPECT_EQ(peer_pid, kMagicChildId);
826 base::MessageLoop::current()->QuitWhenIdle(); 709 base::MessageLoop::current()->QuitWhenIdle();
(...skipping 26 matching lines...) Expand all
853 Connect(&listener); 736 Connect(&listener);
854 737
855 base::MessageLoop::current()->Run(); 738 base::MessageLoop::current()->Run();
856 739
857 Close(); 740 Close();
858 } 741 }
859 742
860 #endif // OS_LINUX 743 #endif // OS_LINUX
861 744
862 } // namespace 745 } // namespace
OLDNEW
« no previous file with comments | « ipc/ipc_channel_mojo.cc ('k') | ipc/ipc_message_pipe_reader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698