blob: 5637a57e8906a10de5915c06a9a4863f4b6a1054 [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
danakjc3fb6c52016-04-23 13:21:09 +09008#include <memory>
viettrungluu@chromium.org00155942013-01-26 06:51:35 +09009#include <string>
10
tfarina2d565d32015-09-16 18:56:21 +090011#include "base/macros.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"
avi42ebda42015-12-22 11:39:04 +090014#include "build/build_config.h"
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090015#include "ipc/ipc_channel.h"
morritaab779702014-09-10 04:35:24 +090016#include "ipc/ipc_channel_factory.h"
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090017#include "ipc/ipc_channel_proxy.h"
18#include "ipc/ipc_multiprocess_test.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090019
brettw@chromium.org87994282013-03-31 11:29:20 +090020namespace base {
morritaa4220942014-08-29 09:20:59 +090021class MessageLoop;
brettw@chromium.org87994282013-03-31 11:29:20 +090022}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090023
erikchen3155dee2015-07-28 05:28:20 +090024namespace IPC {
25class AttachmentBroker;
26}
27
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090028// 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.org7d86af22013-01-12 00:13:37 +090031class IPCTestBase : public base::MultiProcessTest {
32 public:
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090033 // 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.org1c6dcf22009-07-23 08:57:21 +090037
viettrungluu@chromium.org7d86af22013-01-12 00:13:37 +090038 protected:
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090039 IPCTestBase();
dchengcd525762014-12-24 04:59:59 +090040 ~IPCTestBase() override;
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090041
dchengcd525762014-12-24 04:59:59 +090042 void TearDown() override;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090043
yzshen369f9152014-09-05 13:42:58 +090044 // Initializes the test to use the given client and creates an IO message loop
45 // on the current thread.
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090046 void Init(const std::string& test_client_name);
yzshen369f9152014-09-05 13:42:58 +090047 // 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.
sammc6194d972016-03-08 07:38:04 +090051 virtual void InitWithCustomMessageLoop(
52 const std::string& test_client_name,
danakjc3fb6c52016-04-23 13:21:09 +090053 std::unique_ptr<base::MessageLoop> message_loop);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090054
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090055 // 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.org15996aa2014-08-05 08:44:17 +090063 // Releases or replaces existing channel.
64 // These are useful for testing specific types of channel subclasses.
danakjc3fb6c52016-04-23 13:21:09 +090065 std::unique_ptr<IPC::Channel> ReleaseChannel();
66 void SetChannel(std::unique_ptr<IPC::Channel> channel);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090067
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090068 // 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.
dcheng07bc7972014-09-04 03:01:16 +090077 void CreateChannelProxy(
78 IPC::Listener* listener,
79 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090080 void DestroyChannelProxy();
81
82 // Starts the client process, returning true if successful; this should be
83 // done after connecting to the channel.
sammc6194d972016-03-08 07:38:04 +090084 virtual bool StartClient();
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090085
morrita7c48ab82014-09-24 06:16:00 +090086#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.org00155942013-01-26 06:51:35 +090091 // 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.
sammc6194d972016-03-08 07:38:04 +090095 virtual bool WaitForClientShutdown();
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090096
morrita7c48ab82014-09-24 06:16:00 +090097 IPC::ChannelHandle GetTestChannelHandle();
98
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090099 // 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
rvargasf355d882015-01-13 07:23:23 +0900109 const base::Process& client_process() const { return client_process_; }
morritafadecfb2015-03-04 10:52:27 +0900110 scoped_refptr<base::SequencedTaskRunner> task_runner();
morritaa4220942014-08-29 09:20:59 +0900111
danakjc3fb6c52016-04-23 13:21:09 +0900112 virtual std::unique_ptr<IPC::ChannelFactory> CreateChannelFactory(
113 const IPC::ChannelHandle& handle,
114 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_;
danakjc3fb6c52016-04-23 13:21:09 +0900122 std::unique_ptr<base::MessageLoop> message_loop_;
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900123
danakjc3fb6c52016-04-23 13:21:09 +0900124 std::unique_ptr<IPC::Channel> channel_;
125 std::unique_ptr<IPC::ChannelProxy> channel_proxy_;
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900126
rvargasf355d882015-01-13 07:23:23 +0900127 base::Process client_process_;
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900128
129 DISALLOW_COPY_AND_ASSIGN(IPCTestBase);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900130};
131
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900132// 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.org7d86af22013-01-12 00:13:37 +0900136#endif // IPC_IPC_TEST_BASE_H_