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

Side by Side Diff: ipc/ipc_channel_mojo_unittest.cc

Issue 2137353002: Adds associated interface support to IPC::Channel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@channel-bindings-1
Patch Set: rebase 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
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
9 #include <memory> 10 #include <memory>
10 #include <utility> 11 #include <utility>
11 12
12 #include "base/base_paths.h" 13 #include "base/base_paths.h"
13 #include "base/files/file.h" 14 #include "base/files/file.h"
14 #include "base/files/scoped_temp_dir.h" 15 #include "base/files/scoped_temp_dir.h"
15 #include "base/location.h" 16 #include "base/location.h"
16 #include "base/path_service.h" 17 #include "base/path_service.h"
17 #include "base/pickle.h" 18 #include "base/pickle.h"
18 #include "base/run_loop.h" 19 #include "base/run_loop.h"
19 #include "base/single_thread_task_runner.h" 20 #include "base/single_thread_task_runner.h"
21 #include "base/strings/stringprintf.h"
20 #include "base/test/test_io_thread.h" 22 #include "base/test/test_io_thread.h"
21 #include "base/test/test_timeouts.h" 23 #include "base/test/test_timeouts.h"
22 #include "base/threading/thread.h" 24 #include "base/threading/thread.h"
23 #include "base/threading/thread_task_runner_handle.h" 25 #include "base/threading/thread_task_runner_handle.h"
24 #include "build/build_config.h" 26 #include "build/build_config.h"
25 #include "ipc/ipc_message.h" 27 #include "ipc/ipc_message.h"
26 #include "ipc/ipc_mojo_handle_attachment.h" 28 #include "ipc/ipc_mojo_handle_attachment.h"
27 #include "ipc/ipc_mojo_message_helper.h" 29 #include "ipc/ipc_mojo_message_helper.h"
28 #include "ipc/ipc_mojo_param_traits.h" 30 #include "ipc/ipc_mojo_param_traits.h"
31 #include "ipc/ipc_test.mojom.h"
29 #include "ipc/ipc_test_base.h" 32 #include "ipc/ipc_test_base.h"
30 #include "ipc/ipc_test_channel_listener.h" 33 #include "ipc/ipc_test_channel_listener.h"
31 #include "mojo/edk/test/mojo_test_base.h" 34 #include "mojo/edk/test/mojo_test_base.h"
32 #include "mojo/edk/test/multiprocess_test_helper.h" 35 #include "mojo/edk/test/multiprocess_test_helper.h"
33 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
34 37
35 #if defined(OS_POSIX) 38 #if defined(OS_POSIX)
36 #include "base/file_descriptor_posix.h" 39 #include "base/file_descriptor_posix.h"
37 #include "ipc/ipc_platform_file_attachment_posix.h" 40 #include "ipc/ipc_platform_file_attachment_posix.h"
38 #endif 41 #endif
(...skipping 14 matching lines...) Expand all
53 test.Main(); \ 56 test.Main(); \
54 return (::testing::Test::HasFatalFailure() || \ 57 return (::testing::Test::HasFatalFailure() || \
55 ::testing::Test::HasNonfatalFailure()) \ 58 ::testing::Test::HasNonfatalFailure()) \
56 ? 1 \ 59 ? 1 \
57 : 0; \ 60 : 0; \
58 } \ 61 } \
59 void client_name##_MainFixture::Main() 62 void client_name##_MainFixture::Main()
60 63
61 namespace { 64 namespace {
62 65
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
63 class ListenerThatExpectsOK : public IPC::Listener { 72 class ListenerThatExpectsOK : public IPC::Listener {
64 public: 73 public:
65 ListenerThatExpectsOK() : received_ok_(false) {} 74 ListenerThatExpectsOK() : received_ok_(false) {}
66 75
67 ~ListenerThatExpectsOK() override {} 76 ~ListenerThatExpectsOK() override {}
68 77
69 bool OnMessageReceived(const IPC::Message& message) override { 78 bool OnMessageReceived(const IPC::Message& message) override {
70 base::PickleIterator iter(message); 79 base::PickleIterator iter(message);
71 std::string should_be_ok; 80 std::string should_be_ok;
72 EXPECT_TRUE(iter.ReadString(&should_be_ok)); 81 EXPECT_TRUE(iter.ReadString(&should_be_ok));
73 EXPECT_EQ(should_be_ok, "OK"); 82 EXPECT_EQ(should_be_ok, "OK");
74 received_ok_ = true; 83 received_ok_ = true;
75 base::MessageLoop::current()->QuitWhenIdle(); 84 base::MessageLoop::current()->QuitWhenIdle();
76 return true; 85 return true;
77 } 86 }
78 87
79 void OnChannelError() override { 88 void OnChannelError() override {
80 // The connection should be healthy while the listener is waiting 89 // The connection should be healthy while the listener is waiting
81 // message. An error can occur after that because the peer 90 // message. An error can occur after that because the peer
82 // process dies. 91 // process dies.
83 DCHECK(received_ok_); 92 DCHECK(received_ok_);
84 } 93 }
85 94
86 static void SendOK(IPC::Sender* sender) { 95 static void SendOK(IPC::Sender* sender) { SendString(sender, "OK"); }
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 }
92 96
93 private: 97 private:
94 bool received_ok_; 98 bool received_ok_;
95 }; 99 };
96 100
97 class ChannelClient { 101 class ChannelClient {
98 public: 102 public:
99 void Init(mojo::ScopedMessagePipeHandle handle) { 103 void Init(mojo::ScopedMessagePipeHandle handle) {
100 handle_ = std::move(handle); 104 handle_ = std::move(handle);
101 } 105 }
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 ChannelClient) { 693 ChannelClient) {
690 ListenerThatExpectsFileAndPipe listener; 694 ListenerThatExpectsFileAndPipe listener;
691 Connect(&listener); 695 Connect(&listener);
692 listener.set_sender(channel()); 696 listener.set_sender(channel());
693 697
694 base::RunLoop().Run(); 698 base::RunLoop().Run();
695 699
696 Close(); 700 Close();
697 } 701 }
698 702
699 #endif 703 class ListenerWithSimpleAssociatedInterface
704 : public IPC::Listener,
705 public IPC::mojom::SimpleTestDriver {
706 public:
707 static const int kNumMessages;
708
709 ListenerWithSimpleAssociatedInterface() : binding_(this) {}
710
711 ~ListenerWithSimpleAssociatedInterface() override {}
712
713 bool OnMessageReceived(const IPC::Message& message) override {
714 base::PickleIterator iter(message);
715 std::string should_be_expected;
716 EXPECT_TRUE(iter.ReadString(&should_be_expected));
717 EXPECT_EQ(should_be_expected, next_expected_string_);
718 num_messages_received_++;
719 return true;
720 }
721
722 void OnChannelError() override {
723 DCHECK(received_quit_);
724 }
725
726 void RegisterInterfaceFactory(IPC::Channel* channel) {
727 channel->AddAssociatedInterface(
728 base::Bind(&ListenerWithSimpleAssociatedInterface::BindRequest,
729 base::Unretained(this)));
730 }
731
732 private:
733 // IPC::mojom::SimpleTestDriver:
734 void ExpectString(const mojo::String& str) override {
735 next_expected_string_ = str;
736 }
737
738 void RequestQuit(const RequestQuitCallback& callback) override {
739 EXPECT_EQ(kNumMessages, num_messages_received_);
740 received_quit_ = true;
741 callback.Run();
742 base::MessageLoop::current()->QuitWhenIdle();
743 }
744
745 void BindRequest(IPC::mojom::SimpleTestDriverAssociatedRequest request) {
746 DCHECK(!binding_.is_bound());
747 binding_.Bind(std::move(request));
748 }
749
750 std::string next_expected_string_;
751 int num_messages_received_ = 0;
752 bool received_quit_ = false;
753
754 mojo::AssociatedBinding<IPC::mojom::SimpleTestDriver> binding_;
755 };
756
757 const int ListenerWithSimpleAssociatedInterface::kNumMessages = 1000;
758
759 class ListenerSendingAssociatedMessages : public IPC::Listener {
760 public:
761 ListenerSendingAssociatedMessages() {}
762
763 bool OnMessageReceived(const IPC::Message& message) override { return true; }
764
765 void OnChannelConnected(int32_t peer_pid) override {
766 DCHECK(channel_);
767 channel_->GetRemoteAssociatedInterface(&driver_);
768
769 // Send a bunch of interleaved messages, alternating between the associated
770 // interface and a legacy IPC::Message.
771 for (int i = 0; i < ListenerWithSimpleAssociatedInterface::kNumMessages;
772 ++i) {
773 std::string str = base::StringPrintf("Hello! %d", i);
774 driver_->ExpectString(str);
775 SendString(channel_, str);
776 }
777 driver_->RequestQuit(base::Bind(&OnQuitAck));
778 }
779
780 void set_channel(IPC::Channel* channel) { channel_ = channel; }
781
782 private:
783 static void OnQuitAck() { base::MessageLoop::current()->QuitWhenIdle(); }
784
785 IPC::Channel* channel_ = nullptr;
786 IPC::mojom::SimpleTestDriverAssociatedPtr driver_;
787 };
788
789 TEST_F(IPCChannelMojoTest, SimpleAssociatedInterface) {
790 InitWithMojo("SimpleAssociatedInterfaceClient");
791
792 ListenerWithSimpleAssociatedInterface listener;
793 CreateChannel(&listener);
794 ASSERT_TRUE(ConnectChannel());
795
796 listener.RegisterInterfaceFactory(channel());
797
798 base::RunLoop().Run();
799 channel()->Close();
800
801 EXPECT_TRUE(WaitForClientShutdown());
802 DestroyChannel();
803 }
804
805 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(SimpleAssociatedInterfaceClient,
806 ChannelClient) {
807 ListenerSendingAssociatedMessages listener;
808 Connect(&listener);
809 listener.set_channel(channel());
810
811 base::RunLoop().Run();
812
813 Close();
814 }
815
816 #endif // defined(OS_POSIX)
700 817
701 #if defined(OS_LINUX) 818 #if defined(OS_LINUX)
702 819
703 const base::ProcessId kMagicChildId = 54321; 820 const base::ProcessId kMagicChildId = 54321;
704 821
705 class ListenerThatVerifiesPeerPid : public IPC::Listener { 822 class ListenerThatVerifiesPeerPid : public IPC::Listener {
706 public: 823 public:
707 void OnChannelConnected(int32_t peer_pid) override { 824 void OnChannelConnected(int32_t peer_pid) override {
708 EXPECT_EQ(peer_pid, kMagicChildId); 825 EXPECT_EQ(peer_pid, kMagicChildId);
709 base::MessageLoop::current()->QuitWhenIdle(); 826 base::MessageLoop::current()->QuitWhenIdle();
(...skipping 26 matching lines...) Expand all
736 Connect(&listener); 853 Connect(&listener);
737 854
738 base::MessageLoop::current()->Run(); 855 base::MessageLoop::current()->Run();
739 856
740 Close(); 857 Close();
741 } 858 }
742 859
743 #endif // OS_LINUX 860 #endif // OS_LINUX
744 861
745 } // namespace 862 } // namespace
OLDNEW
« ipc/ipc_channel.h ('K') | « 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