blob: ba4e71156af50a6e5dc80036ebf689f4d82463c6 [file] [log] [blame]
[email protected]0cb7d8c82013-01-11 15:13:371// 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.
4
5#include "build/build_config.h"
6
[email protected]0cb7d8c82013-01-11 15:13:377#include "ipc/ipc_test_base.h"
8
[email protected]0cb7d8c82013-01-11 15:13:379#include "base/command_line.h"
[email protected]e66ef602013-07-24 05:15:2410#include "base/process/kill.h"
[email protected]0cb7d8c82013-01-11 15:13:3711#include "base/threading/thread.h"
[email protected]b43e5562013-06-28 15:20:0212#include "base/time/time.h"
[email protected]0cb7d8c82013-01-11 15:13:3713#include "ipc/ipc_descriptors.h"
[email protected]0cb7d8c82013-01-11 15:13:3714
[email protected]2862eebc2013-09-06 23:11:1815#if defined(OS_POSIX)
16#include "base/posix/global_descriptors.h"
17#endif
18
[email protected]3c788582013-01-25 21:51:3519// static
20std::string IPCTestBase::GetChannelName(const std::string& test_client_name) {
21 DCHECK(!test_client_name.empty());
22 return test_client_name + "__Channel";
23}
24
25IPCTestBase::IPCTestBase()
26 : client_process_(base::kNullProcessHandle) {
27}
28
29IPCTestBase::~IPCTestBase() {
30}
[email protected]0cb7d8c82013-01-11 15:13:3731
[email protected]0cb7d8c82013-01-11 15:13:3732void IPCTestBase::TearDown() {
[email protected]3c788582013-01-25 21:51:3533 message_loop_.reset();
[email protected]0cb7d8c82013-01-11 15:13:3734 MultiProcessTest::TearDown();
35}
36
[email protected]3c788582013-01-25 21:51:3537void IPCTestBase::Init(const std::string& test_client_name) {
yzshen22796d82014-09-05 04:42:5838 InitWithCustomMessageLoop(
39 test_client_name,
40 scoped_ptr<base::MessageLoop>(new base::MessageLoopForIO()));
41}
42
43void IPCTestBase::InitWithCustomMessageLoop(
44 const std::string& test_client_name,
45 scoped_ptr<base::MessageLoop> message_loop) {
[email protected]3c788582013-01-25 21:51:3546 DCHECK(!test_client_name.empty());
47 DCHECK(test_client_name_.empty());
yzshen22796d82014-09-05 04:42:5848 DCHECK(!message_loop_);
49
[email protected]3c788582013-01-25 21:51:3550 test_client_name_ = test_client_name;
yzshen22796d82014-09-05 04:42:5851 message_loop_ = message_loop.Pass();
[email protected]3c788582013-01-25 21:51:3552}
53
54void IPCTestBase::CreateChannel(IPC::Listener* listener) {
morrita54f6f80c2014-09-23 21:16:0055 CreateChannelFromChannelHandle(GetTestChannelHandle(), listener);
[email protected]3c788582013-01-25 21:51:3556}
57
58bool IPCTestBase::ConnectChannel() {
59 CHECK(channel_.get());
60 return channel_->Connect();
61}
62
[email protected]64860882014-08-04 23:44:1763scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() {
64 return channel_.Pass();
65}
66
67void IPCTestBase::SetChannel(scoped_ptr<IPC::Channel> channel) {
68 channel_ = channel.Pass();
69}
70
71
[email protected]3c788582013-01-25 21:51:3572void IPCTestBase::DestroyChannel() {
73 DCHECK(channel_.get());
74 channel_.reset();
75}
76
77void IPCTestBase::CreateChannelFromChannelHandle(
78 const IPC::ChannelHandle& channel_handle,
79 IPC::Listener* listener) {
80 CHECK(!channel_.get());
81 CHECK(!channel_proxy_.get());
morrita373af03b2014-09-09 19:35:2482 channel_ = CreateChannelFactory(
83 channel_handle, task_runner().get())->BuildChannel(listener);
[email protected]3c788582013-01-25 21:51:3584}
85
86void IPCTestBase::CreateChannelProxy(
87 IPC::Listener* listener,
dchengff04843f2014-09-03 18:01:1688 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) {
[email protected]3c788582013-01-25 21:51:3589 CHECK(!channel_.get());
90 CHECK(!channel_proxy_.get());
morrita373af03b2014-09-09 19:35:2491 channel_proxy_ = IPC::ChannelProxy::Create(
morrita54f6f80c2014-09-23 21:16:0092 CreateChannelFactory(GetTestChannelHandle(), ipc_task_runner.get()),
morrita373af03b2014-09-09 19:35:2493 listener,
94 ipc_task_runner);
[email protected]3c788582013-01-25 21:51:3595}
96
97void IPCTestBase::DestroyChannelProxy() {
98 CHECK(channel_proxy_.get());
99 channel_proxy_.reset();
100}
101
morrita54f6f80c2014-09-23 21:16:00102std::string IPCTestBase::GetTestMainName() const {
103 return test_client_name_ + "TestClientMain";
104}
105
106bool IPCTestBase::DidStartClient() {
107 DCHECK_NE(base::kNullProcessHandle, client_process_);
108 return client_process_ != base::kNullProcessHandle;
109}
110
111#if defined(OS_POSIX)
112
[email protected]3c788582013-01-25 21:51:35113bool IPCTestBase::StartClient() {
morrita54f6f80c2014-09-23 21:16:00114 return StartClientWithFD(channel_
115 ? channel_->GetClientFileDescriptor()
116 : channel_proxy_->GetClientFileDescriptor());
117}
[email protected]3c788582013-01-25 21:51:35118
morrita54f6f80c2014-09-23 21:16:00119bool IPCTestBase::StartClientWithFD(int ipcfd) {
120 DCHECK_EQ(client_process_, base::kNullProcessHandle);
[email protected]3c788582013-01-25 21:51:35121
[email protected]0cb7d8c82013-01-11 15:13:37122 base::FileHandleMappingVector fds_to_map;
[email protected]3c788582013-01-25 21:51:35123 if (ipcfd > -1)
[email protected]2862eebc2013-09-06 23:11:18124 fds_to_map.push_back(std::pair<int, int>(ipcfd,
125 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
[email protected]a715b602014-03-06 10:21:42126 base::LaunchOptions options;
127 options.fds_to_remap = &fds_to_map;
morrita54f6f80c2014-09-23 21:16:00128 client_process_ = SpawnChildWithOptions(GetTestMainName(), options);
[email protected]3c788582013-01-25 21:51:35129
morrita54f6f80c2014-09-23 21:16:00130 return DidStartClient();
[email protected]0cb7d8c82013-01-11 15:13:37131}
[email protected]3c788582013-01-25 21:51:35132
morrita54f6f80c2014-09-23 21:16:00133#elif defined(OS_WIN)
134
135bool IPCTestBase::StartClient() {
136 DCHECK_EQ(client_process_, base::kNullProcessHandle);
137 client_process_ = SpawnChild(GetTestMainName());
138 return DidStartClient();
139}
140
141#endif
142
[email protected]3c788582013-01-25 21:51:35143bool IPCTestBase::WaitForClientShutdown() {
144 DCHECK(client_process_ != base::kNullProcessHandle);
145
146 bool rv = base::WaitForSingleProcess(client_process_,
147 base::TimeDelta::FromSeconds(5));
148 base::CloseProcessHandle(client_process_);
149 client_process_ = base::kNullProcessHandle;
150 return rv;
151}
[email protected]64860882014-08-04 23:44:17152
morrita54f6f80c2014-09-23 21:16:00153IPC::ChannelHandle IPCTestBase::GetTestChannelHandle() {
154 return GetChannelName(test_client_name_);
155}
156
morritad36736c2014-08-29 00:20:59157scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() {
[email protected]64860882014-08-04 23:44:17158 return message_loop_->message_loop_proxy();
159}
morrita373af03b2014-09-09 19:35:24160
161scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory(
162 const IPC::ChannelHandle& handle,
163 base::TaskRunner* runner) {
164 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER);
165}