1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef IPC_IPC_TEST_BASE_H_ 6 #define IPC_IPC_TEST_BASE_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/process/process.h" 13 #include "base/test/multiprocess_test.h" 14 #include "ipc/ipc_channel.h" 15 #include "ipc/ipc_channel_factory.h" 16 #include "ipc/ipc_channel_proxy.h" 17 #include "ipc/ipc_multiprocess_test.h" 18 19 namespace base { 20 class MessageLoop; 21 } 22 23 // A test fixture for multiprocess IPC tests. Such tests include a "client" side 24 // (running in a separate process). The same client may be shared between 25 // several different tests. 26 class IPCTestBase : public base::MultiProcessTest { 27 public: 28 // The channel name is based on the client's name. This is a public static 29 // helper to be used by the client-side code; server-side test code should 30 // usually not use this (directly). 31 static std::string GetChannelName(const std::string& test_client_name); 32 33 protected: 34 IPCTestBase(); 35 virtual ~IPCTestBase(); 36 37 virtual void TearDown() OVERRIDE; 38 39 // Initializes the test to use the given client and creates an IO message loop 40 // on the current thread. 41 void Init(const std::string& test_client_name); 42 // Some tests create separate thread for IO message loop and run non-IO 43 // message loop on the main thread. As IPCTestBase creates IO message loop by 44 // default, such tests need to provide a custom message loop for the main 45 // thread. 46 void InitWithCustomMessageLoop(const std::string& test_client_name, 47 scoped_ptr<base::MessageLoop> message_loop); 48 49 // Creates a channel with the given listener and connects to the channel 50 // (returning true if successful), respectively. Use these to use a channel 51 // directly. Since the listener must outlive the channel, you must destroy the 52 // channel before the listener gets destroyed. 53 void CreateChannel(IPC::Listener* listener); 54 bool ConnectChannel(); 55 void DestroyChannel(); 56 57 // Releases or replaces existing channel. 58 // These are useful for testing specific types of channel subclasses. 59 scoped_ptr<IPC::Channel> ReleaseChannel(); 60 void SetChannel(scoped_ptr<IPC::Channel> channel); 61 62 // Use this instead of CreateChannel() if you want to use some different 63 // channel specification (then use ConnectChannel() as usual). 64 void CreateChannelFromChannelHandle(const IPC::ChannelHandle& channel_handle, 65 IPC::Listener* listener); 66 67 // Creates a channel proxy with the given listener and task runner. (The 68 // channel proxy will automatically create and connect a channel.) You must 69 // (manually) destroy the channel proxy before the task runner's thread is 70 // destroyed. 71 void CreateChannelProxy( 72 IPC::Listener* listener, 73 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner); 74 void DestroyChannelProxy(); 75 76 // Starts the client process, returning true if successful; this should be 77 // done after connecting to the channel. 78 bool StartClient(); 79 80 #if defined(OS_POSIX) 81 // A StartClient() variant that allows caller to pass the FD of IPC pipe 82 bool StartClientWithFD(int ipcfd); 83 #endif 84 85 // Waits for the client to shut down, returning true if successful. Note that 86 // this does not initiate client shutdown; that must be done by the test 87 // (somehow). This must be called before the end of the test whenever 88 // StartClient() was called successfully. 89 bool WaitForClientShutdown(); 90 91 IPC::ChannelHandle GetTestChannelHandle(); 92 93 // Use this to send IPC messages (when you don't care if you're using a 94 // channel or a proxy). 95 IPC::Sender* sender() { 96 return channel_.get() ? static_cast<IPC::Sender*>(channel_.get()) : 97 static_cast<IPC::Sender*>(channel_proxy_.get()); 98 } 99 100 IPC::Channel* channel() { return channel_.get(); } 101 IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); } 102 103 const base::ProcessHandle& client_process() const { return client_process_; } 104 scoped_refptr<base::TaskRunner> task_runner(); 105 106 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( 107 const IPC::ChannelHandle& handle, base::TaskRunner* runner); 108 109 virtual bool DidStartClient(); 110 111 private: 112 std::string GetTestMainName() const; 113 114 std::string test_client_name_; 115 scoped_ptr<base::MessageLoop> message_loop_; 116 117 scoped_ptr<IPC::Channel> channel_; 118 scoped_ptr<IPC::ChannelProxy> channel_proxy_; 119 120 base::ProcessHandle client_process_; 121 122 DISALLOW_COPY_AND_ASSIGN(IPCTestBase); 123 }; 124 125 // Use this to declare the client side for tests using IPCTestBase. 126 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \ 127 MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain) 128 129 #endif // IPC_IPC_TEST_BASE_H_ 130