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

Side by Side Diff: ipc/ipc_test_base.h

Issue 2473993003: Delete IPC::ChannelPosix, IPC::ChannelWin and IPC::AttachmentBroker. (Closed)
Patch Set: Created 4 years, 1 month 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_send_fds_test.cc ('k') | ipc/ipc_test_base.cc » ('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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #ifndef IPC_IPC_TEST_BASE_H_ 5 #ifndef IPC_IPC_TEST_BASE_H_
6 #define IPC_IPC_TEST_BASE_H_ 6 #define IPC_IPC_TEST_BASE_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/process/process.h" 12 #include "base/process/process.h"
13 #include "base/test/multiprocess_test.h" 13 #include "base/test/multiprocess_test.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "ipc/ipc_channel.h" 15 #include "ipc/ipc_channel.h"
16 #include "ipc/ipc_channel_factory.h" 16 #include "ipc/ipc_channel_factory.h"
17 #include "ipc/ipc_channel_proxy.h" 17 #include "ipc/ipc_channel_proxy.h"
18 #include "ipc/ipc_multiprocess_test.h"
19 #include "mojo/edk/test/mojo_test_base.h" 18 #include "mojo/edk/test/mojo_test_base.h"
20 #include "mojo/edk/test/multiprocess_test_helper.h" 19 #include "mojo/edk/test/multiprocess_test_helper.h"
21 20
22 namespace base { 21 namespace base {
23 class MessageLoop; 22 class MessageLoop;
24 } 23 }
25 24
26 namespace IPC {
27 class AttachmentBroker;
28 }
29
30 // A test fixture for multiprocess IPC tests. Such tests include a "client" side
31 // (running in a separate process). The same client may be shared between
32 // several different tests.
33 class IPCTestBase : public base::MultiProcessTest {
34 public:
35 // The channel name is based on the client's name. This is a public static
36 // helper to be used by the client-side code; server-side test code should
37 // usually not use this (directly).
38 static std::string GetChannelName(const std::string& test_client_name);
39
40 protected:
41 IPCTestBase();
42 ~IPCTestBase() override;
43
44 void TearDown() override;
45
46 // Initializes the test to use the given client and creates an IO message loop
47 // on the current thread.
48 void Init(const std::string& test_client_name);
49 // Some tests create separate thread for IO message loop and run non-IO
50 // message loop on the main thread. As IPCTestBase creates IO message loop by
51 // default, such tests need to provide a custom message loop for the main
52 // thread.
53 virtual void InitWithCustomMessageLoop(
54 const std::string& test_client_name,
55 std::unique_ptr<base::MessageLoop> message_loop);
56
57 // Creates a channel with the given listener and connects to the channel
58 // (returning true if successful), respectively. Use these to use a channel
59 // directly. Since the listener must outlive the channel, you must destroy the
60 // channel before the listener gets destroyed.
61 void CreateChannel(IPC::Listener* listener);
62 bool ConnectChannel();
63 void DestroyChannel();
64
65 // Releases or replaces existing channel.
66 // These are useful for testing specific types of channel subclasses.
67 std::unique_ptr<IPC::Channel> ReleaseChannel();
68 void SetChannel(std::unique_ptr<IPC::Channel> channel);
69
70 // Use this instead of CreateChannel() if you want to use some different
71 // channel specification (then use ConnectChannel() as usual).
72 void CreateChannelFromChannelHandle(const IPC::ChannelHandle& channel_handle,
73 IPC::Listener* listener);
74
75 // Creates a channel proxy with the given listener and task runner. (The
76 // channel proxy will automatically create and connect a channel.) You must
77 // (manually) destroy the channel proxy before the task runner's thread is
78 // destroyed.
79 void CreateChannelProxy(
80 IPC::Listener* listener,
81 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
82 void DestroyChannelProxy();
83
84 // Starts the client process, returning true if successful; this should be
85 // done after connecting to the channel.
86 virtual bool StartClient();
87
88 #if defined(OS_POSIX)
89 // A StartClient() variant that allows caller to pass the FD of IPC pipe
90 bool StartClientWithFD(int ipcfd);
91 #endif
92
93 // Waits for the client to shut down, returning true if successful. Note that
94 // this does not initiate client shutdown; that must be done by the test
95 // (somehow). This must be called before the end of the test whenever
96 // StartClient() was called successfully.
97 virtual bool WaitForClientShutdown();
98
99 IPC::ChannelHandle GetTestChannelHandle();
100
101 // Use this to send IPC messages (when you don't care if you're using a
102 // channel or a proxy).
103 IPC::Sender* sender() {
104 return channel_.get() ? static_cast<IPC::Sender*>(channel_.get()) :
105 static_cast<IPC::Sender*>(channel_proxy_.get());
106 }
107
108 IPC::Channel* channel() { return channel_.get(); }
109 IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); }
110 void set_channel_proxy(std::unique_ptr<IPC::ChannelProxy> proxy) {
111 DCHECK(!channel_proxy_);
112 channel_proxy_.swap(proxy);
113 }
114
115 const base::Process& client_process() const { return client_process_; }
116 scoped_refptr<base::SingleThreadTaskRunner> task_runner();
117
118 virtual std::unique_ptr<IPC::ChannelFactory> CreateChannelFactory(
119 const IPC::ChannelHandle& handle,
120 base::SingleThreadTaskRunner* runner);
121
122 virtual bool DidStartClient();
123
124 private:
125 std::string GetTestMainName() const;
126
127 std::string test_client_name_;
128 std::unique_ptr<base::MessageLoop> message_loop_;
129
130 std::unique_ptr<IPC::Channel> channel_;
131 std::unique_ptr<IPC::ChannelProxy> channel_proxy_;
132
133 base::Process client_process_;
134
135 DISALLOW_COPY_AND_ASSIGN(IPCTestBase);
136 };
137
138 class IPCChannelMojoTestBase : public testing::Test { 25 class IPCChannelMojoTestBase : public testing::Test {
139 public: 26 public:
140 IPCChannelMojoTestBase(); 27 IPCChannelMojoTestBase();
141 ~IPCChannelMojoTestBase() override; 28 ~IPCChannelMojoTestBase() override;
142 29
143 void Init(const std::string& test_client_name); 30 void Init(const std::string& test_client_name);
31 void InitWithCustomMessageLoop(
32 const std::string& test_client_name,
33 std::unique_ptr<base::MessageLoop> message_loop);
144 34
145 bool WaitForClientShutdown(); 35 bool WaitForClientShutdown();
146 36
147 void TearDown() override; 37 void TearDown() override;
148 38
149 void CreateChannel(IPC::Listener* listener); 39 void CreateChannel(IPC::Listener* listener);
150 40
151 bool ConnectChannel(); 41 bool ConnectChannel();
152 42
153 void DestroyChannel(); 43 void DestroyChannel();
154 44
155 IPC::Sender* sender() { return channel(); } 45 IPC::Sender* sender() { return channel(); }
156 IPC::Channel* channel() { return channel_.get(); } 46 IPC::Channel* channel() { return channel_.get(); }
157 const base::Process& client_process() const { return helper_.test_child(); } 47 const base::Process& client_process() const { return helper_.test_child(); }
158 48
159 protected: 49 protected:
160 mojo::ScopedMessagePipeHandle TakeHandle(); 50 mojo::ScopedMessagePipeHandle TakeHandle();
161 51
162 private: 52 private:
163 base::MessageLoop message_loop_; 53 std::unique_ptr<base::MessageLoop> message_loop_;
164 54
165 mojo::ScopedMessagePipeHandle handle_; 55 mojo::ScopedMessagePipeHandle handle_;
166 mojo::edk::test::MultiprocessTestHelper helper_; 56 mojo::edk::test::MultiprocessTestHelper helper_;
167 57
168 std::unique_ptr<IPC::Channel> channel_; 58 std::unique_ptr<IPC::Channel> channel_;
169 59
170 DISALLOW_COPY_AND_ASSIGN(IPCChannelMojoTestBase); 60 DISALLOW_COPY_AND_ASSIGN(IPCChannelMojoTestBase);
171 }; 61 };
172 62
173 class IpcChannelMojoTestClient { 63 class IpcChannelMojoTestClient {
174 public: 64 public:
175 IpcChannelMojoTestClient(); 65 IpcChannelMojoTestClient();
176 ~IpcChannelMojoTestClient(); 66 ~IpcChannelMojoTestClient();
177 67
178 void Init(mojo::ScopedMessagePipeHandle handle); 68 void Init(mojo::ScopedMessagePipeHandle handle);
179 69
180 void Connect(IPC::Listener* listener); 70 void Connect(IPC::Listener* listener);
181 71
182 void Close(); 72 void Close();
183 73
184 IPC::Channel* channel() const { return channel_.get(); } 74 IPC::Channel* channel() const { return channel_.get(); }
185 75
186 private: 76 private:
187 base::MessageLoopForIO main_message_loop_; 77 base::MessageLoopForIO main_message_loop_;
188 mojo::ScopedMessagePipeHandle handle_; 78 mojo::ScopedMessagePipeHandle handle_;
189 std::unique_ptr<IPC::Channel> channel_; 79 std::unique_ptr<IPC::Channel> channel_;
190 }; 80 };
191 81
192 // Use this to declare the client side for tests using IPCTestBase.
193 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \
194 MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain)
195
196 // Use this to declare the client side for tests using IPCChannelMojoTestBase 82 // Use this to declare the client side for tests using IPCChannelMojoTestBase
197 // when a custom test fixture class is required in the client. |test_base| must 83 // when a custom test fixture class is required in the client. |test_base| must
198 // be derived from IpcChannelMojoTestClient. 84 // be derived from IpcChannelMojoTestClient.
199 #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(client_name, \ 85 #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(client_name, \
200 test_base) \ 86 test_base) \
201 class client_name##_MainFixture : public test_base { \ 87 class client_name##_MainFixture : public test_base { \
202 public: \ 88 public: \
203 void Main(); \ 89 void Main(); \
204 }; \ 90 }; \
205 MULTIPROCESS_TEST_MAIN_WITH_SETUP( \ 91 MULTIPROCESS_TEST_MAIN_WITH_SETUP( \
206 client_name##TestChildMain, \ 92 client_name##TestChildMain, \
207 ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { \ 93 ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { \
208 client_name##_MainFixture test; \ 94 client_name##_MainFixture test; \
209 test.Init( \ 95 test.Init( \
210 std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe)); \ 96 std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe)); \
211 test.Main(); \ 97 test.Main(); \
212 return (::testing::Test::HasFatalFailure() || \ 98 return (::testing::Test::HasFatalFailure() || \
213 ::testing::Test::HasNonfatalFailure()) \ 99 ::testing::Test::HasNonfatalFailure()) \
214 ? 1 \ 100 ? 1 \
215 : 0; \ 101 : 0; \
216 } \ 102 } \
217 void client_name##_MainFixture::Main() 103 void client_name##_MainFixture::Main()
218 104
219 // Use this to declare the client side for tests using IPCChannelMojoTestBase. 105 // Use this to declare the client side for tests using IPCChannelMojoTestBase.
220 #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(client_name) \ 106 #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(client_name) \
221 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE( \ 107 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE( \
222 client_name, IpcChannelMojoTestClient) 108 client_name, IpcChannelMojoTestClient)
223 109
224 #endif // IPC_IPC_TEST_BASE_H_ 110 #endif // IPC_IPC_TEST_BASE_H_
OLDNEW
« no previous file with comments | « ipc/ipc_send_fds_test.cc ('k') | ipc/ipc_test_base.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698