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::SetUp() {
33 MultiProcessTest::SetUp();
35 // Construct a fresh Message loop for the duration of each test.
36 DCHECK(!message_loop_
.get());
37 message_loop_
.reset(new base::MessageLoopForIO());
40 void IPCTestBase::TearDown() {
41 DCHECK(message_loop_
.get());
42 message_loop_
.reset();
43 MultiProcessTest::TearDown();
46 void IPCTestBase::Init(const std::string
& test_client_name
) {
47 DCHECK(!test_client_name
.empty());
48 DCHECK(test_client_name_
.empty());
49 test_client_name_
= test_client_name
;
52 void IPCTestBase::CreateChannel(IPC::Listener
* listener
) {
53 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_
),
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_
= IPC::Channel::CreateServer(channel_handle
, listener
);
84 void IPCTestBase::CreateChannelProxy(
85 IPC::Listener
* listener
,
86 base::SingleThreadTaskRunner
* ipc_task_runner
) {
87 CHECK(!channel_
.get());
88 CHECK(!channel_proxy_
.get());
89 channel_proxy_
= IPC::ChannelProxy::Create(GetChannelName(test_client_name_
),
90 IPC::Channel::MODE_SERVER
,
95 void IPCTestBase::DestroyChannelProxy() {
96 CHECK(channel_proxy_
.get());
97 channel_proxy_
.reset();
100 bool IPCTestBase::StartClient() {
101 DCHECK(client_process_
== base::kNullProcessHandle
);
103 std::string test_main
= test_client_name_
+ "TestClientMain";
106 client_process_
= SpawnChild(test_main
);
107 #elif defined(OS_POSIX)
108 base::FileHandleMappingVector fds_to_map
;
109 const int ipcfd
= channel_
.get()
110 ? channel_
->GetClientFileDescriptor()
111 : channel_proxy_
->GetClientFileDescriptor();
113 fds_to_map
.push_back(std::pair
<int, int>(ipcfd
,
114 kPrimaryIPCChannel
+ base::GlobalDescriptors::kBaseDescriptor
));
115 base::LaunchOptions options
;
116 options
.fds_to_remap
= &fds_to_map
;
117 client_process_
= SpawnChildWithOptions(test_main
, options
);
120 return client_process_
!= base::kNullProcessHandle
;
123 bool IPCTestBase::WaitForClientShutdown() {
124 DCHECK(client_process_
!= base::kNullProcessHandle
);
126 bool rv
= base::WaitForSingleProcess(client_process_
,
127 base::TimeDelta::FromSeconds(5));
128 base::CloseProcessHandle(client_process_
);
129 client_process_
= base::kNullProcessHandle
;
133 scoped_refptr
<base::TaskRunner
> IPCTestBase::task_runner() {
134 return message_loop_
->message_loop_proxy();
137 void IPCTestBase::set_message_loop(scoped_ptr
<base::MessageLoop
> loop
) {
138 message_loop_
= loop
.Pass();