avi@chromium.org | 362c8a8 | 2011-11-18 01:09:44 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 5 | #ifndef IPC_IPC_TEST_BASE_H_ |
| 6 | #define IPC_IPC_TEST_BASE_H_ |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 7 | |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame^] | 8 | #include <memory> |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 9 | #include <string> |
| 10 | |
tfarina | 2d565d3 | 2015-09-16 18:56:21 +0900 | [diff] [blame] | 11 | #include "base/macros.h" |
rsesek@chromium.org | 1931971 | 2013-07-24 14:15:24 +0900 | [diff] [blame] | 12 | #include "base/process/process.h" |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 13 | #include "base/test/multiprocess_test.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 14 | #include "build/build_config.h" |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 15 | #include "ipc/ipc_channel.h" |
morrita | ab77970 | 2014-09-10 04:35:24 +0900 | [diff] [blame] | 16 | #include "ipc/ipc_channel_factory.h" |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 17 | #include "ipc/ipc_channel_proxy.h" |
| 18 | #include "ipc/ipc_multiprocess_test.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 19 | |
brettw@chromium.org | 8799428 | 2013-03-31 11:29:20 +0900 | [diff] [blame] | 20 | namespace base { |
morrita | a422094 | 2014-08-29 09:20:59 +0900 | [diff] [blame] | 21 | class MessageLoop; |
brettw@chromium.org | 8799428 | 2013-03-31 11:29:20 +0900 | [diff] [blame] | 22 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 23 | |
erikchen | 3155dee | 2015-07-28 05:28:20 +0900 | [diff] [blame] | 24 | namespace IPC { |
| 25 | class AttachmentBroker; |
| 26 | } |
| 27 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 28 | // A test fixture for multiprocess IPC tests. Such tests include a "client" side |
| 29 | // (running in a separate process). The same client may be shared between |
| 30 | // several different tests. |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 31 | class IPCTestBase : public base::MultiProcessTest { |
| 32 | public: |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 33 | // The channel name is based on the client's name. This is a public static |
| 34 | // helper to be used by the client-side code; server-side test code should |
| 35 | // usually not use this (directly). |
| 36 | static std::string GetChannelName(const std::string& test_client_name); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 37 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 38 | protected: |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 39 | IPCTestBase(); |
dcheng | cd52576 | 2014-12-24 04:59:59 +0900 | [diff] [blame] | 40 | ~IPCTestBase() override; |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 41 | |
dcheng | cd52576 | 2014-12-24 04:59:59 +0900 | [diff] [blame] | 42 | void TearDown() override; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 43 | |
yzshen | 369f915 | 2014-09-05 13:42:58 +0900 | [diff] [blame] | 44 | // Initializes the test to use the given client and creates an IO message loop |
| 45 | // on the current thread. |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 46 | void Init(const std::string& test_client_name); |
yzshen | 369f915 | 2014-09-05 13:42:58 +0900 | [diff] [blame] | 47 | // Some tests create separate thread for IO message loop and run non-IO |
| 48 | // message loop on the main thread. As IPCTestBase creates IO message loop by |
| 49 | // default, such tests need to provide a custom message loop for the main |
| 50 | // thread. |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 51 | virtual void InitWithCustomMessageLoop( |
| 52 | const std::string& test_client_name, |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame^] | 53 | std::unique_ptr<base::MessageLoop> message_loop); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 54 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 55 | // Creates a channel with the given listener and connects to the channel |
| 56 | // (returning true if successful), respectively. Use these to use a channel |
| 57 | // directly. Since the listener must outlive the channel, you must destroy the |
| 58 | // channel before the listener gets destroyed. |
| 59 | void CreateChannel(IPC::Listener* listener); |
| 60 | bool ConnectChannel(); |
| 61 | void DestroyChannel(); |
| 62 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 63 | // Releases or replaces existing channel. |
| 64 | // These are useful for testing specific types of channel subclasses. |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame^] | 65 | std::unique_ptr<IPC::Channel> ReleaseChannel(); |
| 66 | void SetChannel(std::unique_ptr<IPC::Channel> channel); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 67 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 68 | // Use this instead of CreateChannel() if you want to use some different |
| 69 | // channel specification (then use ConnectChannel() as usual). |
| 70 | void CreateChannelFromChannelHandle(const IPC::ChannelHandle& channel_handle, |
| 71 | IPC::Listener* listener); |
| 72 | |
| 73 | // Creates a channel proxy with the given listener and task runner. (The |
| 74 | // channel proxy will automatically create and connect a channel.) You must |
| 75 | // (manually) destroy the channel proxy before the task runner's thread is |
| 76 | // destroyed. |
dcheng | 07bc797 | 2014-09-04 03:01:16 +0900 | [diff] [blame] | 77 | void CreateChannelProxy( |
| 78 | IPC::Listener* listener, |
| 79 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 80 | void DestroyChannelProxy(); |
| 81 | |
| 82 | // Starts the client process, returning true if successful; this should be |
| 83 | // done after connecting to the channel. |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 84 | virtual bool StartClient(); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 85 | |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 86 | #if defined(OS_POSIX) |
| 87 | // A StartClient() variant that allows caller to pass the FD of IPC pipe |
| 88 | bool StartClientWithFD(int ipcfd); |
| 89 | #endif |
| 90 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 91 | // Waits for the client to shut down, returning true if successful. Note that |
| 92 | // this does not initiate client shutdown; that must be done by the test |
| 93 | // (somehow). This must be called before the end of the test whenever |
| 94 | // StartClient() was called successfully. |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 95 | virtual bool WaitForClientShutdown(); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 96 | |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 97 | IPC::ChannelHandle GetTestChannelHandle(); |
| 98 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 99 | // Use this to send IPC messages (when you don't care if you're using a |
| 100 | // channel or a proxy). |
| 101 | IPC::Sender* sender() { |
| 102 | return channel_.get() ? static_cast<IPC::Sender*>(channel_.get()) : |
| 103 | static_cast<IPC::Sender*>(channel_proxy_.get()); |
| 104 | } |
| 105 | |
| 106 | IPC::Channel* channel() { return channel_.get(); } |
| 107 | IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); } |
| 108 | |
rvargas | f355d88 | 2015-01-13 07:23:23 +0900 | [diff] [blame] | 109 | const base::Process& client_process() const { return client_process_; } |
morrita | fadecfb | 2015-03-04 10:52:27 +0900 | [diff] [blame] | 110 | scoped_refptr<base::SequencedTaskRunner> task_runner(); |
morrita | a422094 | 2014-08-29 09:20:59 +0900 | [diff] [blame] | 111 | |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame^] | 112 | virtual std::unique_ptr<IPC::ChannelFactory> CreateChannelFactory( |
| 113 | const IPC::ChannelHandle& handle, |
| 114 | base::SequencedTaskRunner* runner); |
morrita | ab77970 | 2014-09-10 04:35:24 +0900 | [diff] [blame] | 115 | |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 116 | virtual bool DidStartClient(); |
| 117 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 118 | private: |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 119 | std::string GetTestMainName() const; |
| 120 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 121 | std::string test_client_name_; |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame^] | 122 | std::unique_ptr<base::MessageLoop> message_loop_; |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 123 | |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame^] | 124 | std::unique_ptr<IPC::Channel> channel_; |
| 125 | std::unique_ptr<IPC::ChannelProxy> channel_proxy_; |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 126 | |
rvargas | f355d88 | 2015-01-13 07:23:23 +0900 | [diff] [blame] | 127 | base::Process client_process_; |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 128 | |
| 129 | DISALLOW_COPY_AND_ASSIGN(IPCTestBase); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 130 | }; |
| 131 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 132 | // Use this to declare the client side for tests using IPCTestBase. |
| 133 | #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \ |
| 134 | MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain) |
| 135 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 136 | #endif // IPC_IPC_TEST_BASE_H_ |