blob: ce3328ad1058d6544f88779e42bda308735fe645 [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
10#include "base/basictypes.h"
11#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"
15#include "ipc/ipc_channel_proxy.h"
16#include "ipc/ipc_multiprocess_test.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090017
brettw@chromium.org87994282013-03-31 11:29:20 +090018namespace base {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090019class MessageLoopForIO;
brettw@chromium.org87994282013-03-31 11:29:20 +090020}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090021
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090022// 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.org7d86af22013-01-12 00:13:37 +090025class IPCTestBase : public base::MultiProcessTest {
26 public:
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090027 // 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.org1c6dcf22009-07-23 08:57:21 +090031
viettrungluu@chromium.org7d86af22013-01-12 00:13:37 +090032 protected:
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090033 IPCTestBase();
34 virtual ~IPCTestBase();
35
avi@chromium.org362c8a82011-11-18 01:09:44 +090036 virtual void SetUp() OVERRIDE;
37 virtual void TearDown() OVERRIDE;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090038
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090039 // Initializes the test to use the given client.
40 void Init(const std::string& test_client_name);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090041
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090042 // 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.org15996aa2014-08-05 08:44:17 +090050 // 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.org00155942013-01-26 06:51:35 +090055 // 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.org15996aa2014-08-05 08:44:17 +090089 scoped_refptr<base::TaskRunner> io_thread_task_runner();
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090090
91 private:
92 std::string test_client_name_;
brettw@chromium.org87994282013-03-31 11:29:20 +090093 scoped_ptr<base::MessageLoopForIO> message_loop_;
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090094
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.org1c6dcf22009-07-23 08:57:21 +0900101};
102
viettrungluu@chromium.org00155942013-01-26 06:51:35 +0900103// 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.org7d86af22013-01-12 00:13:37 +0900107#endif // IPC_IPC_TEST_BASE_H_