blob: 6abef0f1470b47b721dbf16ad7de8a188e9d2204 [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) {
morrita373af03b2014-09-09 19:35:2455 CreateChannelFromChannelHandle(
56 GetChannelName(test_client_name_), listener);
[email protected]3c788582013-01-25 21:51:3557}
58
59bool IPCTestBase::ConnectChannel() {
60 CHECK(channel_.get());
61 return channel_->Connect();
62}
63
[email protected]64860882014-08-04 23:44:1764scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() {
65 return channel_.Pass();
66}
67
68void IPCTestBase::SetChannel(scoped_ptr<IPC::Channel> channel) {
69 channel_ = channel.Pass();
70}
71
72
[email protected]3c788582013-01-25 21:51:3573void IPCTestBase::DestroyChannel() {
74 DCHECK(channel_.get());
75 channel_.reset();
76}
77
78void IPCTestBase::CreateChannelFromChannelHandle(
79 const IPC::ChannelHandle& channel_handle,
80 IPC::Listener* listener) {
81 CHECK(!channel_.get());
82 CHECK(!channel_proxy_.get());
morrita373af03b2014-09-09 19:35:2483 channel_ = CreateChannelFactory(
84 channel_handle, task_runner().get())->BuildChannel(listener);
[email protected]3c788582013-01-25 21:51:3585}
86
87void IPCTestBase::CreateChannelProxy(
88 IPC::Listener* listener,
dchengff04843f2014-09-03 18:01:1689 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) {
[email protected]3c788582013-01-25 21:51:3590 CHECK(!channel_.get());
91 CHECK(!channel_proxy_.get());
morrita373af03b2014-09-09 19:35:2492 channel_proxy_ = IPC::ChannelProxy::Create(
93 CreateChannelFactory(GetChannelName(test_client_name_),
94 ipc_task_runner.get()),
95 listener,
96 ipc_task_runner);
[email protected]3c788582013-01-25 21:51:3597}
98
99void IPCTestBase::DestroyChannelProxy() {
100 CHECK(channel_proxy_.get());
101 channel_proxy_.reset();
102}
103
104bool IPCTestBase::StartClient() {
105 DCHECK(client_process_ == base::kNullProcessHandle);
106
107 std::string test_main = test_client_name_ + "TestClientMain";
[email protected]3c788582013-01-25 21:51:35108
[email protected]0cb7d8c82013-01-11 15:13:37109#if defined(OS_WIN)
[email protected]963a91b2014-03-09 00:59:31110 client_process_ = SpawnChild(test_main);
[email protected]0cb7d8c82013-01-11 15:13:37111#elif defined(OS_POSIX)
[email protected]0cb7d8c82013-01-11 15:13:37112 base::FileHandleMappingVector fds_to_map;
[email protected]2f60c9b2014-06-06 20:13:51113 const int ipcfd = channel_.get()
114 ? channel_->GetClientFileDescriptor()
115 : channel_proxy_->GetClientFileDescriptor();
[email protected]3c788582013-01-25 21:51:35116 if (ipcfd > -1)
[email protected]2862eebc2013-09-06 23:11:18117 fds_to_map.push_back(std::pair<int, int>(ipcfd,
118 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
[email protected]a715b602014-03-06 10:21:42119 base::LaunchOptions options;
120 options.fds_to_remap = &fds_to_map;
[email protected]963a91b2014-03-09 00:59:31121 client_process_ = SpawnChildWithOptions(test_main, options);
[email protected]3c788582013-01-25 21:51:35122#endif
123
124 return client_process_ != base::kNullProcessHandle;
[email protected]0cb7d8c82013-01-11 15:13:37125}
[email protected]3c788582013-01-25 21:51:35126
127bool IPCTestBase::WaitForClientShutdown() {
128 DCHECK(client_process_ != base::kNullProcessHandle);
129
130 bool rv = base::WaitForSingleProcess(client_process_,
131 base::TimeDelta::FromSeconds(5));
132 base::CloseProcessHandle(client_process_);
133 client_process_ = base::kNullProcessHandle;
134 return rv;
135}
[email protected]64860882014-08-04 23:44:17136
morritad36736c2014-08-29 00:20:59137scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() {
[email protected]64860882014-08-04 23:44:17138 return message_loop_->message_loop_proxy();
139}
morrita373af03b2014-09-09 19:35:24140
141scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory(
142 const IPC::ChannelHandle& handle,
143 base::TaskRunner* runner) {
144 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER);
145}