Home | History | Annotate | Download | only in ipc
      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_proxy.h"
     16 #include "ipc/ipc_multiprocess_test.h"
     17 
     18 namespace base {
     19 class MessageLoopForIO;
     20 }
     21 
     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.
     25 class IPCTestBase : public base::MultiProcessTest {
     26  public:
     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);
     31 
     32  protected:
     33   IPCTestBase();
     34   virtual ~IPCTestBase();
     35 
     36   virtual void SetUp() OVERRIDE;
     37   virtual void TearDown() OVERRIDE;
     38 
     39   // Initializes the test to use the given client.
     40   void Init(const std::string& test_client_name);
     41 
     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 
     50   // Use this instead of CreateChannel() if you want to use some different
     51   // channel specification (then use ConnectChannel() as usual).
     52   void CreateChannelFromChannelHandle(const IPC::ChannelHandle& channel_handle,
     53                                       IPC::Listener* listener);
     54 
     55   // Creates a channel proxy with the given listener and task runner. (The
     56   // channel proxy will automatically create and connect a channel.) You must
     57   // (manually) destroy the channel proxy before the task runner's thread is
     58   // destroyed.
     59   void CreateChannelProxy(IPC::Listener* listener,
     60                           base::SingleThreadTaskRunner* ipc_task_runner);
     61   void DestroyChannelProxy();
     62 
     63   // Starts the client process, returning true if successful; this should be
     64   // done after connecting to the channel.
     65   bool StartClient();
     66 
     67   // Waits for the client to shut down, returning true if successful. Note that
     68   // this does not initiate client shutdown; that must be done by the test
     69   // (somehow). This must be called before the end of the test whenever
     70   // StartClient() was called successfully.
     71   bool WaitForClientShutdown();
     72 
     73   // Use this to send IPC messages (when you don't care if you're using a
     74   // channel or a proxy).
     75   IPC::Sender* sender() {
     76     return channel_.get() ? static_cast<IPC::Sender*>(channel_.get()) :
     77                             static_cast<IPC::Sender*>(channel_proxy_.get());
     78   }
     79 
     80   IPC::Channel* channel() { return channel_.get(); }
     81   IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); }
     82 
     83   const base::ProcessHandle& client_process() const { return client_process_; }
     84 
     85  private:
     86   std::string test_client_name_;
     87   scoped_ptr<base::MessageLoopForIO> message_loop_;
     88 
     89   scoped_ptr<IPC::Channel> channel_;
     90   scoped_ptr<IPC::ChannelProxy> channel_proxy_;
     91 
     92   base::ProcessHandle client_process_;
     93 
     94   DISALLOW_COPY_AND_ASSIGN(IPCTestBase);
     95 };
     96 
     97 // Use this to declare the client side for tests using IPCTestBase.
     98 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \
     99     MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain)
    100 
    101 #endif  // IPC_IPC_TEST_BASE_H_
    102