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