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/debug/debug_on_start_win.h"
11 #include "base/process/kill.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "ipc/ipc_descriptors.h"
17 #include "base/posix/global_descriptors.h"
21 std::string
IPCTestBase::GetChannelName(const std::string
& test_client_name
) {
22 DCHECK(!test_client_name
.empty());
23 return test_client_name
+ "__Channel";
26 IPCTestBase::IPCTestBase()
27 : client_process_(base::kNullProcessHandle
) {
30 IPCTestBase::~IPCTestBase() {
33 void IPCTestBase::SetUp() {
34 MultiProcessTest::SetUp();
36 // Construct a fresh IO Message loop for the duration of each test.
37 DCHECK(!message_loop_
.get());
38 message_loop_
.reset(new base::MessageLoopForIO());
41 void IPCTestBase::TearDown() {
42 DCHECK(message_loop_
.get());
43 message_loop_
.reset();
44 MultiProcessTest::TearDown();
47 void IPCTestBase::Init(const std::string
& test_client_name
) {
48 DCHECK(!test_client_name
.empty());
49 DCHECK(test_client_name_
.empty());
50 test_client_name_
= test_client_name
;
53 void IPCTestBase::CreateChannel(IPC::Listener
* listener
) {
54 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_
),
58 bool IPCTestBase::ConnectChannel() {
59 CHECK(channel_
.get());
60 return channel_
->Connect();
63 void IPCTestBase::DestroyChannel() {
64 DCHECK(channel_
.get());
68 void IPCTestBase::CreateChannelFromChannelHandle(
69 const IPC::ChannelHandle
& channel_handle
,
70 IPC::Listener
* listener
) {
71 CHECK(!channel_
.get());
72 CHECK(!channel_proxy_
.get());
73 channel_
.reset(new IPC::Channel(channel_handle
,
74 IPC::Channel::MODE_SERVER
,
78 void IPCTestBase::CreateChannelProxy(
79 IPC::Listener
* listener
,
80 base::SingleThreadTaskRunner
* ipc_task_runner
) {
81 CHECK(!channel_
.get());
82 CHECK(!channel_proxy_
.get());
83 channel_proxy_
.reset(new IPC::ChannelProxy(GetChannelName(test_client_name_
),
84 IPC::Channel::MODE_SERVER
,
89 void IPCTestBase::DestroyChannelProxy() {
90 CHECK(channel_proxy_
.get());
91 channel_proxy_
.reset();
94 bool IPCTestBase::StartClient() {
95 DCHECK(client_process_
== base::kNullProcessHandle
);
97 std::string test_main
= test_client_name_
+ "TestClientMain";
100 client_process_
= SpawnChild(test_main
);
101 #elif defined(OS_POSIX)
102 base::FileHandleMappingVector fds_to_map
;
103 const int ipcfd
= channel_
.get() ? channel_
->GetClientFileDescriptor() :
104 channel_proxy_
->GetClientFileDescriptor();
106 fds_to_map
.push_back(std::pair
<int, int>(ipcfd
,
107 kPrimaryIPCChannel
+ base::GlobalDescriptors::kBaseDescriptor
));
108 base::LaunchOptions options
;
109 options
.fds_to_remap
= &fds_to_map
;
110 client_process_
= SpawnChildWithOptions(test_main
, options
);
113 return client_process_
!= base::kNullProcessHandle
;
116 bool IPCTestBase::WaitForClientShutdown() {
117 DCHECK(client_process_
!= base::kNullProcessHandle
);
119 bool rv
= base::WaitForSingleProcess(client_process_
,
120 base::TimeDelta::FromSeconds(5));
121 base::CloseProcessHandle(client_process_
);
122 client_process_
= base::kNullProcessHandle
;