blob: 59f5b8654035b2e1d110501daae37fc810cd6865 [file] [log] [blame]
avi@chromium.org362c8a82011-11-18 01:09:44 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
viettrungluu@chromium.org7d86af22013-01-12 00:13:37 +09005#ifndef IPC_IPC_TEST_BASE_H_
6#define IPC_IPC_TEST_BASE_H_
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09007
viettrungluu@chromium.org00155942013-01-26 06:51:35 +09008#include <string>
9
tfarina2d565d32015-09-16 18:56:21 +090010#include "base/macros.h"
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090011#include "base/memory/scoped_ptr.h"
rsesek@chromium.org19319712013-07-24 14:15:24 +090012#include "base/process/process.h"
viettrungluu@chromium.org7d86af22013-01-12 00:13:37 +090013#include "base/test/multiprocess_test.h"
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090014#include "ipc/ipc_channel.h"
morritaab779702014-09-10 04:35:24 +090015#include "ipc/ipc_channel_factory.h"
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090016#include "ipc/ipc_channel_proxy.h"
17#include "ipc/ipc_multiprocess_test.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090018
brettw@chromium.org87994282013-03-31 11:29:20 +090019namespace base {
morritaa4220942014-08-29 09:20:59 +090020class MessageLoop;
brettw@chromium.org87994282013-03-31 11:29:20 +090021}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090022
erikchen3155dee2015-07-28 05:28:20 +090023namespace IPC {
24class AttachmentBroker;
25}
26
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090027// 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.org7d86af22013-01-12 00:13:37 +090030class IPCTestBase : public base::MultiProcessTest {
31 public:
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090032 // 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.org1c6dcf22009-07-23 08:57:21 +090036
viettrungluu@chromium.org7d86af22013-01-12 00:13:37 +090037 protected:
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090038 IPCTestBase();
dchengcd525762014-12-24 04:59:59 +090039 ~IPCTestBase() override;
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090040
dchengcd525762014-12-24 04:59:59 +090041 void TearDown() override;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090042
yzshen369f9152014-09-05 13:42:58 +090043 // Initializes the test to use the given client and creates an IO message loop
44 // on the current thread.
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090045 void Init(const std::string& test_client_name);
yzshen369f9152014-09-05 13:42:58 +090046 // 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.org1c6dcf22009-07-23 08:57:21 +090052
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090053 // 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.org15996aa2014-08-05 08:44:17 +090061 // 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.org00155942013-01-26 06:51:35 +090066 // 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.
dcheng07bc7972014-09-04 03:01:16 +090075 void CreateChannelProxy(
76 IPC::Listener* listener,
77 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090078 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
morrita7c48ab82014-09-24 06:16:00 +090084#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.org00155942013-01-26 06:51:35 +090089 // 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
morrita7c48ab82014-09-24 06:16:00 +090095 IPC::ChannelHandle GetTestChannelHandle();
96
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090097 // 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(); }
erikchen3155dee2015-07-28 05:28:20 +0900106 void set_attachment_broker(IPC::AttachmentBroker* broker) {
107 attachment_broker_ = broker;
108 }
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900109
rvargasf355d882015-01-13 07:23:23 +0900110 const base::Process& client_process() const { return client_process_; }
morritafadecfb2015-03-04 10:52:27 +0900111 scoped_refptr<base::SequencedTaskRunner> task_runner();
morritaa4220942014-08-29 09:20:59 +0900112
morritaab779702014-09-10 04:35:24 +0900113 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita9d483fc2015-03-14 05:44:39 +0900114 const IPC::ChannelHandle& handle, base::SequencedTaskRunner* runner);
morritaab779702014-09-10 04:35:24 +0900115
morrita7c48ab82014-09-24 06:16:00 +0900116 virtual bool DidStartClient();
117
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900118 private:
morrita7c48ab82014-09-24 06:16:00 +0900119 std::string GetTestMainName() const;
120
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900121 std::string test_client_name_;
morritaa4220942014-08-29 09:20:59 +0900122 scoped_ptr<base::MessageLoop> message_loop_;
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900123
124 scoped_ptr<IPC::Channel> channel_;
125 scoped_ptr<IPC::ChannelProxy> channel_proxy_;
126
erikchen3155dee2015-07-28 05:28:20 +0900127 // The AttachmentBroker that should be passed to |channel_| when it is
128 // created.
129 IPC::AttachmentBroker* attachment_broker_;
130
rvargasf355d882015-01-13 07:23:23 +0900131 base::Process client_process_;
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900132
133 DISALLOW_COPY_AND_ASSIGN(IPCTestBase);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900134};
135
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900136// 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.org7d86af22013-01-12 00:13:37 +0900140#endif // IPC_IPC_TEST_BASE_H_