1 // Copyright 2011 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef IPC_IPC_TEST_BASE_H_ 6 #define IPC_IPC_TEST_BASE_H_ 7 8 #include <memory> 9 #include <string> 10 11 #include "base/process/process.h" 12 #include "base/test/multiprocess_test.h" 13 #include "base/test/task_environment.h" 14 #include "build/build_config.h" 15 #include "ipc/ipc_channel.h" 16 #include "ipc/ipc_channel_factory.h" 17 #include "ipc/ipc_channel_proxy.h" 18 #include "mojo/core/test/mojo_test_base.h" 19 #include "mojo/core/test/multiprocess_test_helper.h" 20 21 class IPCChannelMojoTestBase : public testing::Test { 22 public: 23 IPCChannelMojoTestBase(); 24 25 IPCChannelMojoTestBase(const IPCChannelMojoTestBase&) = delete; 26 IPCChannelMojoTestBase& operator=(const IPCChannelMojoTestBase&) = delete; 27 28 ~IPCChannelMojoTestBase() override; 29 30 void Init(const std::string& test_client_name); 31 32 bool WaitForClientShutdown(); 33 34 void TearDown() override; 35 36 void CreateChannel(IPC::Listener* listener); 37 38 bool ConnectChannel(); 39 40 void DestroyChannel(); 41 sender()42 IPC::Sender* sender() { return channel(); } channel()43 IPC::Channel* channel() { return channel_.get(); } client_process()44 const base::Process& client_process() const { return helper_.test_child(); } 45 46 protected: 47 mojo::ScopedMessagePipeHandle TakeHandle(); 48 49 private: 50 base::test::SingleThreadTaskEnvironment task_environment_; 51 52 mojo::ScopedMessagePipeHandle handle_; 53 mojo::core::test::MultiprocessTestHelper helper_; 54 55 std::unique_ptr<IPC::Channel> channel_; 56 }; 57 58 class IpcChannelMojoTestClient { 59 public: 60 IpcChannelMojoTestClient(); 61 ~IpcChannelMojoTestClient(); 62 63 void Init(mojo::ScopedMessagePipeHandle handle); 64 65 void Connect(IPC::Listener* listener); 66 67 void Close(); 68 channel()69 IPC::Channel* channel() const { return channel_.get(); } 70 71 private: 72 base::test::SingleThreadTaskEnvironment task_environment_{ 73 base::test::SingleThreadTaskEnvironment::MainThreadType::IO}; 74 mojo::ScopedMessagePipeHandle handle_; 75 std::unique_ptr<IPC::Channel> channel_; 76 }; 77 78 // Use this to declare the client side for tests using IPCChannelMojoTestBase 79 // when a custom test fixture class is required in the client. |test_base| must 80 // be derived from IpcChannelMojoTestClient. 81 #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(client_name, \ 82 test_base) \ 83 class client_name##_MainFixture : public test_base { \ 84 public: \ 85 void Main(); \ 86 }; \ 87 MULTIPROCESS_TEST_MAIN_WITH_SETUP( \ 88 client_name##TestChildMain, \ 89 ::mojo::core::test::MultiprocessTestHelper::ChildSetup) { \ 90 client_name##_MainFixture test; \ 91 test.Init( \ 92 std::move(mojo::core::test::MultiprocessTestHelper::primordial_pipe)); \ 93 test.Main(); \ 94 return (::testing::Test::HasFatalFailure() || \ 95 ::testing::Test::HasNonfatalFailure()) \ 96 ? 1 \ 97 : 0; \ 98 } \ 99 void client_name##_MainFixture::Main() 100 101 // Use this to declare the client side for tests using IPCChannelMojoTestBase. 102 #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(client_name) \ 103 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE( \ 104 client_name, IpcChannelMojoTestClient) 105 106 #endif // IPC_IPC_TEST_BASE_H_ 107