1 // Copyright (c) 2012 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.
5 #include "build/build_config.h"
7 #include "ipc/ipc_test_base.h"
9 #include "base/command_line.h"
10 #include "base/process/kill.h"
11 #include "base/threading/thread.h"
12 #include "base/time/time.h"
13 #include "ipc/ipc_descriptors.h"
16 #include "base/posix/global_descriptors.h"
20 std::string
IPCTestBase::GetChannelName(const std::string
& test_client_name
) {
21 DCHECK(!test_client_name
.empty());
22 return test_client_name
+ "__Channel";
25 IPCTestBase::IPCTestBase() {
28 IPCTestBase::~IPCTestBase() {
31 void IPCTestBase::TearDown() {
32 message_loop_
.reset();
33 MultiProcessTest::TearDown();
36 void IPCTestBase::Init(const std::string
& test_client_name
) {
37 InitWithCustomMessageLoop(
39 scoped_ptr
<base::MessageLoop
>(new base::MessageLoopForIO()));
42 void IPCTestBase::InitWithCustomMessageLoop(
43 const std::string
& test_client_name
,
44 scoped_ptr
<base::MessageLoop
> message_loop
) {
45 DCHECK(!test_client_name
.empty());
46 DCHECK(test_client_name_
.empty());
47 DCHECK(!message_loop_
);
49 test_client_name_
= test_client_name
;
50 message_loop_
= message_loop
.Pass();
53 void IPCTestBase::CreateChannel(IPC::Listener
* listener
) {
54 CreateChannelFromChannelHandle(GetTestChannelHandle(), listener
);
57 bool IPCTestBase::ConnectChannel() {
58 CHECK(channel_
.get());
59 return channel_
->Connect();
62 scoped_ptr
<IPC::Channel
> IPCTestBase::ReleaseChannel() {
63 return channel_
.Pass();
66 void IPCTestBase::SetChannel(scoped_ptr
<IPC::Channel
> channel
) {
67 channel_
= channel
.Pass();
71 void IPCTestBase::DestroyChannel() {
72 DCHECK(channel_
.get());
76 void IPCTestBase::CreateChannelFromChannelHandle(
77 const IPC::ChannelHandle
& channel_handle
,
78 IPC::Listener
* listener
) {
79 CHECK(!channel_
.get());
80 CHECK(!channel_proxy_
.get());
81 channel_
= CreateChannelFactory(
82 channel_handle
, task_runner().get())->BuildChannel(listener
);
85 void IPCTestBase::CreateChannelProxy(
86 IPC::Listener
* listener
,
87 const scoped_refptr
<base::SingleThreadTaskRunner
>& ipc_task_runner
) {
88 CHECK(!channel_
.get());
89 CHECK(!channel_proxy_
.get());
90 channel_proxy_
= IPC::ChannelProxy::Create(
91 CreateChannelFactory(GetTestChannelHandle(), ipc_task_runner
.get()),
96 void IPCTestBase::DestroyChannelProxy() {
97 CHECK(channel_proxy_
.get());
98 channel_proxy_
.reset();
101 std::string
IPCTestBase::GetTestMainName() const {
102 return test_client_name_
+ "TestClientMain";
105 bool IPCTestBase::DidStartClient() {
106 DCHECK(client_process_
.IsValid());
107 return client_process_
.IsValid();
110 #if defined(OS_POSIX)
112 bool IPCTestBase::StartClient() {
113 return StartClientWithFD(channel_
114 ? channel_
->GetClientFileDescriptor()
115 : channel_proxy_
->GetClientFileDescriptor());
118 bool IPCTestBase::StartClientWithFD(int ipcfd
) {
119 DCHECK(!client_process_
.IsValid());
121 base::FileHandleMappingVector fds_to_map
;
123 fds_to_map
.push_back(std::pair
<int, int>(ipcfd
,
124 kPrimaryIPCChannel
+ base::GlobalDescriptors::kBaseDescriptor
));
125 base::LaunchOptions options
;
126 options
.fds_to_remap
= &fds_to_map
;
127 client_process_
= SpawnChildWithOptions(GetTestMainName(), options
);
129 return DidStartClient();
132 #elif defined(OS_WIN)
134 bool IPCTestBase::StartClient() {
135 DCHECK(!client_process_
.IsValid());
136 client_process_
= SpawnChild(GetTestMainName());
137 return DidStartClient();
142 bool IPCTestBase::WaitForClientShutdown() {
143 DCHECK(client_process_
.IsValid());
145 bool rv
= base::WaitForSingleProcess(client_process_
.Handle(),
146 base::TimeDelta::FromSeconds(5));
147 client_process_
.Close();
151 IPC::ChannelHandle
IPCTestBase::GetTestChannelHandle() {
152 return GetChannelName(test_client_name_
);
155 scoped_refptr
<base::TaskRunner
> IPCTestBase::task_runner() {
156 return message_loop_
->message_loop_proxy();
159 scoped_ptr
<IPC::ChannelFactory
> IPCTestBase::CreateChannelFactory(
160 const IPC::ChannelHandle
& handle
,
161 base::TaskRunner
* runner
) {
162 return IPC::ChannelFactory::Create(handle
, IPC::Channel::MODE_SERVER
);