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"
15 #include "ipc/ipc_switches.h"
18 std::string
IPCTestBase::GetChannelName(const std::string
& test_client_name
) {
19 DCHECK(!test_client_name
.empty());
20 return test_client_name
+ "__Channel";
23 IPCTestBase::IPCTestBase()
24 : client_process_(base::kNullProcessHandle
) {
27 IPCTestBase::~IPCTestBase() {
30 void IPCTestBase::SetUp() {
31 MultiProcessTest::SetUp();
33 // Construct a fresh IO Message loop for the duration of each test.
34 DCHECK(!message_loop_
.get());
35 message_loop_
.reset(new base::MessageLoopForIO());
38 void IPCTestBase::TearDown() {
39 DCHECK(message_loop_
.get());
40 message_loop_
.reset();
41 MultiProcessTest::TearDown();
44 void IPCTestBase::Init(const std::string
& test_client_name
) {
45 DCHECK(!test_client_name
.empty());
46 DCHECK(test_client_name_
.empty());
47 test_client_name_
= test_client_name
;
50 void IPCTestBase::CreateChannel(IPC::Listener
* listener
) {
51 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_
),
55 bool IPCTestBase::ConnectChannel() {
56 CHECK(channel_
.get());
57 return channel_
->Connect();
60 void IPCTestBase::DestroyChannel() {
61 DCHECK(channel_
.get());
65 void IPCTestBase::CreateChannelFromChannelHandle(
66 const IPC::ChannelHandle
& channel_handle
,
67 IPC::Listener
* listener
) {
68 CHECK(!channel_
.get());
69 CHECK(!channel_proxy_
.get());
70 channel_
.reset(new IPC::Channel(channel_handle
,
71 IPC::Channel::MODE_SERVER
,
75 void IPCTestBase::CreateChannelProxy(
76 IPC::Listener
* listener
,
77 base::SingleThreadTaskRunner
* ipc_task_runner
) {
78 CHECK(!channel_
.get());
79 CHECK(!channel_proxy_
.get());
80 channel_proxy_
.reset(new IPC::ChannelProxy(GetChannelName(test_client_name_
),
81 IPC::Channel::MODE_SERVER
,
86 void IPCTestBase::DestroyChannelProxy() {
87 CHECK(channel_proxy_
.get());
88 channel_proxy_
.reset();
91 bool IPCTestBase::StartClient() {
92 DCHECK(client_process_
== base::kNullProcessHandle
);
94 std::string test_main
= test_client_name_
+ "TestClientMain";
96 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren
);
99 client_process_
= MultiProcessTest::SpawnChild(test_main
, debug_on_start
);
100 #elif defined(OS_POSIX)
101 base::FileHandleMappingVector fds_to_map
;
102 const int ipcfd
= channel_
.get() ? channel_
->GetClientFileDescriptor() :
103 channel_proxy_
->GetClientFileDescriptor();
105 fds_to_map
.push_back(std::pair
<int, int>(ipcfd
, kPrimaryIPCChannel
+ 3));
107 client_process_
= MultiProcessTest::SpawnChild(test_main
,
112 return client_process_
!= base::kNullProcessHandle
;
115 bool IPCTestBase::WaitForClientShutdown() {
116 DCHECK(client_process_
!= base::kNullProcessHandle
);
118 bool rv
= base::WaitForSingleProcess(client_process_
,
119 base::TimeDelta::FromSeconds(5));
120 base::CloseProcessHandle(client_process_
);
121 client_process_
= base::kNullProcessHandle
;