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()
26 : client_process_(base::kNullProcessHandle
) {
29 IPCTestBase::~IPCTestBase() {
32 void IPCTestBase::TearDown() {
33 message_loop_
.reset();
34 MultiProcessTest::TearDown();
37 void IPCTestBase::Init(const std::string
& test_client_name
) {
38 InitWithCustomMessageLoop(
40 scoped_ptr
<base::MessageLoop
>(new base::MessageLoopForIO()));
43 void IPCTestBase::InitWithCustomMessageLoop(
44 const std::string
& test_client_name
,
45 scoped_ptr
<base::MessageLoop
> message_loop
) {
46 DCHECK(!test_client_name
.empty());
47 DCHECK(test_client_name_
.empty());
48 DCHECK(!message_loop_
);
50 test_client_name_
= test_client_name
;
51 message_loop_
= message_loop
.Pass();
54 void IPCTestBase::CreateChannel(IPC::Listener
* listener
) {
55 CreateChannelFromChannelHandle(
56 GetChannelName(test_client_name_
), listener
);
59 bool IPCTestBase::ConnectChannel() {
60 CHECK(channel_
.get());
61 return channel_
->Connect();
64 scoped_ptr
<IPC::Channel
> IPCTestBase::ReleaseChannel() {
65 return channel_
.Pass();
68 void IPCTestBase::SetChannel(scoped_ptr
<IPC::Channel
> channel
) {
69 channel_
= channel
.Pass();
73 void IPCTestBase::DestroyChannel() {
74 DCHECK(channel_
.get());
78 void IPCTestBase::CreateChannelFromChannelHandle(
79 const IPC::ChannelHandle
& channel_handle
,
80 IPC::Listener
* listener
) {
81 CHECK(!channel_
.get());
82 CHECK(!channel_proxy_
.get());
83 channel_
= CreateChannelFactory(
84 channel_handle
, task_runner().get())->BuildChannel(listener
);
87 void IPCTestBase::CreateChannelProxy(
88 IPC::Listener
* listener
,
89 const scoped_refptr
<base::SingleThreadTaskRunner
>& ipc_task_runner
) {
90 CHECK(!channel_
.get());
91 CHECK(!channel_proxy_
.get());
92 channel_proxy_
= IPC::ChannelProxy::Create(
93 CreateChannelFactory(GetChannelName(test_client_name_
),
94 ipc_task_runner
.get()),
99 void IPCTestBase::DestroyChannelProxy() {
100 CHECK(channel_proxy_
.get());
101 channel_proxy_
.reset();
104 bool IPCTestBase::StartClient() {
105 DCHECK(client_process_
== base::kNullProcessHandle
);
107 std::string test_main
= test_client_name_
+ "TestClientMain";
110 client_process_
= SpawnChild(test_main
);
111 #elif defined(OS_POSIX)
112 base::FileHandleMappingVector fds_to_map
;
113 const int ipcfd
= channel_
.get()
114 ? channel_
->GetClientFileDescriptor()
115 : channel_proxy_
->GetClientFileDescriptor();
117 fds_to_map
.push_back(std::pair
<int, int>(ipcfd
,
118 kPrimaryIPCChannel
+ base::GlobalDescriptors::kBaseDescriptor
));
119 base::LaunchOptions options
;
120 options
.fds_to_remap
= &fds_to_map
;
121 client_process_
= SpawnChildWithOptions(test_main
, options
);
124 return client_process_
!= base::kNullProcessHandle
;
127 bool IPCTestBase::WaitForClientShutdown() {
128 DCHECK(client_process_
!= base::kNullProcessHandle
);
130 bool rv
= base::WaitForSingleProcess(client_process_
,
131 base::TimeDelta::FromSeconds(5));
132 base::CloseProcessHandle(client_process_
);
133 client_process_
= base::kNullProcessHandle
;
137 scoped_refptr
<base::TaskRunner
> IPCTestBase::task_runner() {
138 return message_loop_
->message_loop_proxy();
141 scoped_ptr
<IPC::ChannelFactory
> IPCTestBase::CreateChannelFactory(
142 const IPC::ChannelHandle
& handle
,
143 base::TaskRunner
* runner
) {
144 return IPC::ChannelFactory::Create(handle
, IPC::Channel::MODE_SERVER
);