OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_TEST_BASE_H_ | 5 #ifndef IPC_IPC_TEST_BASE_H_ |
6 #define IPC_IPC_TEST_BASE_H_ | 6 #define IPC_IPC_TEST_BASE_H_ |
7 | 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/memory/scoped_ptr.h" |
8 #include "base/process.h" | 12 #include "base/process.h" |
9 #include "base/test/multiprocess_test.h" | 13 #include "base/test/multiprocess_test.h" |
10 | 14 #include "ipc/ipc_channel.h" |
11 // The different channel names for the child processes. | 15 #include "ipc/ipc_channel_proxy.h" |
12 extern const char kTestClientChannel[]; | 16 #include "ipc/ipc_multiprocess_test.h" |
13 extern const char kReflectorChannel[]; | |
14 extern const char kFuzzerChannel[]; | |
15 extern const char kSyncSocketChannel[]; | |
16 | 17 |
17 class MessageLoopForIO; | 18 class MessageLoopForIO; |
18 namespace IPC { | |
19 class Channel; | |
20 } // namespace IPC | |
21 | 19 |
22 // Base class to facilitate spawning IPC client processes. | 20 // A test fixture for multiprocess IPC tests. Such tests include a "client" side |
| 21 // (running in a separate process). The same client may be shared between |
| 22 // several different tests. |
23 class IPCTestBase : public base::MultiProcessTest { | 23 class IPCTestBase : public base::MultiProcessTest { |
24 public: | 24 public: |
25 enum ChildType { | 25 // The channel name is based on the client's name. This is a public static |
26 TEST_CLIENT, | 26 // helper to be used by the client-side code; server-side test code should |
27 TEST_DESCRIPTOR_CLIENT, | 27 // usually not use this (directly). |
28 TEST_DESCRIPTOR_CLIENT_SANDBOXED, | 28 static std::string GetChannelName(const std::string& test_client_name); |
29 TEST_REFLECTOR, | |
30 FUZZER_SERVER, | |
31 SYNC_SOCKET_SERVER | |
32 }; | |
33 | 29 |
34 protected: | 30 protected: |
35 // Create a new MessageLoopForIO for each test. | 31 IPCTestBase(); |
| 32 virtual ~IPCTestBase(); |
| 33 |
36 virtual void SetUp() OVERRIDE; | 34 virtual void SetUp() OVERRIDE; |
37 virtual void TearDown() OVERRIDE; | 35 virtual void TearDown() OVERRIDE; |
38 | 36 |
39 // Spawns a child process of the specified type | 37 // Initializes the test to use the given client. |
40 base::ProcessHandle SpawnChild(ChildType child_type, IPC::Channel* channel); | 38 void Init(const std::string& test_client_name); |
41 | 39 |
42 // Created around each test instantiation. | 40 // Creates a channel with the given listener and connects to the channel |
43 MessageLoopForIO* message_loop_; | 41 // (returning true if successful), respectively. Use these to use a channel |
| 42 // directly. Since the listener must outlive the channel, you must destroy the |
| 43 // channel before the listener gets destroyed. |
| 44 void CreateChannel(IPC::Listener* listener); |
| 45 bool ConnectChannel(); |
| 46 void DestroyChannel(); |
| 47 |
| 48 // Use this instead of CreateChannel() if you want to use some different |
| 49 // channel specification (then use ConnectChannel() as usual). |
| 50 void CreateChannelFromChannelHandle(const IPC::ChannelHandle& channel_handle, |
| 51 IPC::Listener* listener); |
| 52 |
| 53 // Creates a channel proxy with the given listener and task runner. (The |
| 54 // channel proxy will automatically create and connect a channel.) You must |
| 55 // (manually) destroy the channel proxy before the task runner's thread is |
| 56 // destroyed. |
| 57 void CreateChannelProxy(IPC::Listener* listener, |
| 58 base::SingleThreadTaskRunner* ipc_task_runner); |
| 59 void DestroyChannelProxy(); |
| 60 |
| 61 // Starts the client process, returning true if successful; this should be |
| 62 // done after connecting to the channel. |
| 63 bool StartClient(); |
| 64 |
| 65 // Waits for the client to shut down, returning true if successful. Note that |
| 66 // this does not initiate client shutdown; that must be done by the test |
| 67 // (somehow). This must be called before the end of the test whenever |
| 68 // StartClient() was called successfully. |
| 69 bool WaitForClientShutdown(); |
| 70 |
| 71 // Use this to send IPC messages (when you don't care if you're using a |
| 72 // channel or a proxy). |
| 73 IPC::Sender* sender() { |
| 74 return channel_.get() ? static_cast<IPC::Sender*>(channel_.get()) : |
| 75 static_cast<IPC::Sender*>(channel_proxy_.get()); |
| 76 } |
| 77 |
| 78 IPC::Channel* channel() { return channel_.get(); } |
| 79 IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); } |
| 80 |
| 81 const base::ProcessHandle& client_process() const { return client_process_; } |
| 82 |
| 83 private: |
| 84 std::string test_client_name_; |
| 85 scoped_ptr<MessageLoopForIO> message_loop_; |
| 86 |
| 87 scoped_ptr<IPC::Channel> channel_; |
| 88 scoped_ptr<IPC::ChannelProxy> channel_proxy_; |
| 89 |
| 90 base::ProcessHandle client_process_; |
| 91 |
| 92 DISALLOW_COPY_AND_ASSIGN(IPCTestBase); |
44 }; | 93 }; |
45 | 94 |
| 95 // Use this to declare the client side for tests using IPCTestBase. |
| 96 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \ |
| 97 MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain) |
| 98 |
46 #endif // IPC_IPC_TEST_BASE_H_ | 99 #endif // IPC_IPC_TEST_BASE_H_ |
OLD | NEW |