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 | |
| 10 | #include "base/basictypes.h" |
| 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" |
| 15 | #include "ipc/ipc_channel_proxy.h" |
| 16 | #include "ipc/ipc_multiprocess_test.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 17 | |
brettw@chromium.org | 8799428 | 2013-03-31 11:29:20 +0900 | [diff] [blame] | 18 | namespace base { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 19 | class MessageLoopForIO; |
brettw@chromium.org | 8799428 | 2013-03-31 11:29:20 +0900 | [diff] [blame] | 20 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 21 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 22 | // A test fixture for multiprocess IPC tests. Such tests include a "client" side |
| 23 | // (running in a separate process). The same client may be shared between |
| 24 | // several different tests. |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 25 | class IPCTestBase : public base::MultiProcessTest { |
| 26 | public: |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 27 | // The channel name is based on the client's name. This is a public static |
| 28 | // helper to be used by the client-side code; server-side test code should |
| 29 | // usually not use this (directly). |
| 30 | static std::string GetChannelName(const std::string& test_client_name); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 31 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 32 | protected: |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 33 | IPCTestBase(); |
| 34 | virtual ~IPCTestBase(); |
| 35 | |
avi@chromium.org | 362c8a8 | 2011-11-18 01:09:44 +0900 | [diff] [blame] | 36 | virtual void SetUp() OVERRIDE; |
| 37 | virtual void TearDown() OVERRIDE; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 38 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 39 | // Initializes the test to use the given client. |
| 40 | void Init(const std::string& test_client_name); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 41 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 42 | // Creates a channel with the given listener and connects to the channel |
| 43 | // (returning true if successful), respectively. Use these to use a channel |
| 44 | // directly. Since the listener must outlive the channel, you must destroy the |
| 45 | // channel before the listener gets destroyed. |
| 46 | void CreateChannel(IPC::Listener* listener); |
| 47 | bool ConnectChannel(); |
| 48 | void DestroyChannel(); |
| 49 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 50 | // Releases or replaces existing channel. |
| 51 | // These are useful for testing specific types of channel subclasses. |
| 52 | scoped_ptr<IPC::Channel> ReleaseChannel(); |
| 53 | void SetChannel(scoped_ptr<IPC::Channel> channel); |
| 54 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 55 | // Use this instead of CreateChannel() if you want to use some different |
| 56 | // channel specification (then use ConnectChannel() as usual). |
| 57 | void CreateChannelFromChannelHandle(const IPC::ChannelHandle& channel_handle, |
| 58 | IPC::Listener* listener); |
| 59 | |
| 60 | // Creates a channel proxy with the given listener and task runner. (The |
| 61 | // channel proxy will automatically create and connect a channel.) You must |
| 62 | // (manually) destroy the channel proxy before the task runner's thread is |
| 63 | // destroyed. |
| 64 | void CreateChannelProxy(IPC::Listener* listener, |
| 65 | base::SingleThreadTaskRunner* ipc_task_runner); |
| 66 | void DestroyChannelProxy(); |
| 67 | |
| 68 | // Starts the client process, returning true if successful; this should be |
| 69 | // done after connecting to the channel. |
| 70 | bool StartClient(); |
| 71 | |
| 72 | // Waits for the client to shut down, returning true if successful. Note that |
| 73 | // this does not initiate client shutdown; that must be done by the test |
| 74 | // (somehow). This must be called before the end of the test whenever |
| 75 | // StartClient() was called successfully. |
| 76 | bool WaitForClientShutdown(); |
| 77 | |
| 78 | // Use this to send IPC messages (when you don't care if you're using a |
| 79 | // channel or a proxy). |
| 80 | IPC::Sender* sender() { |
| 81 | return channel_.get() ? static_cast<IPC::Sender*>(channel_.get()) : |
| 82 | static_cast<IPC::Sender*>(channel_proxy_.get()); |
| 83 | } |
| 84 | |
| 85 | IPC::Channel* channel() { return channel_.get(); } |
| 86 | IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); } |
| 87 | |
| 88 | const base::ProcessHandle& client_process() const { return client_process_; } |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 89 | scoped_refptr<base::TaskRunner> io_thread_task_runner(); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | std::string test_client_name_; |
brettw@chromium.org | 8799428 | 2013-03-31 11:29:20 +0900 | [diff] [blame] | 93 | scoped_ptr<base::MessageLoopForIO> message_loop_; |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 94 | |
| 95 | scoped_ptr<IPC::Channel> channel_; |
| 96 | scoped_ptr<IPC::ChannelProxy> channel_proxy_; |
| 97 | |
| 98 | base::ProcessHandle client_process_; |
| 99 | |
| 100 | DISALLOW_COPY_AND_ASSIGN(IPCTestBase); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 101 | }; |
| 102 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 103 | // Use this to declare the client side for tests using IPCTestBase. |
| 104 | #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \ |
| 105 | MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain) |
| 106 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 107 | #endif // IPC_IPC_TEST_BASE_H_ |