Chromium Code Reviews| 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. | |
| 43 void CreateChannel(IPC::Listener* listener); | |
| 44 bool ConnectChannel(); | |
| 45 | |
| 46 // Use this instead of CreateChannel() if you want to use some different | |
| 47 // channel specification (then use ConnectChannel() as usual). | |
| 48 void CreateChannelFromChannelHandle(const IPC::ChannelHandle& channel_handle, | |
| 49 IPC::Listener* listener); | |
| 50 | |
| 51 // Creates a channel proxy with the given listener and task runner. (The | |
| 52 // channel proxy will automatically create and connect a channel.) You must | |
| 53 // (manually) destroy the channel proxy before the task runner's thread is | |
| 54 // destroyed. | |
| 55 void CreateChannelProxy(IPC::Listener* listener, | |
| 56 base::SingleThreadTaskRunner* ipc_task_runner); | |
| 57 void DestroyChannelProxy(); | |
| 58 | |
| 59 // Starts the client process, returning true if successful; this should be | |
| 60 // done after connecting to the channel. | |
| 61 bool StartClient(); | |
| 62 | |
| 63 // Waits for the client to shut down, returning true if successful. Note that | |
| 64 // this does not initiate client shutdown; that must be done by the test | |
| 65 // (somehow). This must be called before the end of the test whenever | |
| 66 // StartClient() was called successfully. | |
| 67 bool WaitForClientShutdown(); | |
| 68 | |
| 69 // Use this to send IPC messages (when you don't care if you're using a | |
| 70 // channel or a proxy). | |
| 71 IPC::Sender* sender() { | |
| 72 return channel_.get() ? static_cast<IPC::Sender*>(channel_.get()) : | |
| 73 static_cast<IPC::Sender*>(channel_proxy_.get()); | |
| 74 } | |
| 75 | |
| 76 IPC::Channel* channel() { return channel_.get(); } | |
| 77 IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); } | |
| 78 | |
| 79 const base::ProcessHandle& client_process() const { return client_process_; } | |
| 80 | |
| 81 private: | |
| 82 std::string test_client_name_; | |
| 83 scoped_ptr<MessageLoopForIO> message_loop_; | |
| 84 | |
| 85 scoped_ptr<IPC::Channel> channel_; | |
| 86 scoped_ptr<IPC::ChannelProxy> channel_proxy_; | |
| 87 | |
| 88 base::ProcessHandle client_process_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(IPCTestBase); | |
| 44 }; | 91 }; |
| 45 | 92 |
| 93 // Use this to declare the client side for tests using IPCTestBase. | |
| 94 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \ | |
| 95 MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain) | |
|
brettw
2013/01/24 20:39:48
need 2 more spaces indent.
| |
| 96 | |
| 46 #endif // IPC_IPC_TEST_BASE_H_ | 97 #endif // IPC_IPC_TEST_BASE_H_ |
| OLD | NEW |