blob: 757d9f5bb626e854946719e59555824c6f25665a [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
[email protected]0cb7d8c82013-01-11 15:13:375#include "ipc/ipc_test_base.h"
6
dchenge48600452015-12-28 02:24:507#include <utility>
8
[email protected]0cb7d8c82013-01-11 15:13:379#include "base/command_line.h"
danakj03de39b22016-04-23 04:21:0910#include "base/memory/ptr_util.h"
[email protected]e66ef602013-07-24 05:15:2411#include "base/process/kill.h"
dchenge48600452015-12-28 02:24:5012#include "base/single_thread_task_runner.h"
[email protected]0cb7d8c82013-01-11 15:13:3713#include "base/threading/thread.h"
[email protected]b43e5562013-06-28 15:20:0214#include "base/time/time.h"
dchenge48600452015-12-28 02:24:5015#include "build/build_config.h"
[email protected]0cb7d8c82013-01-11 15:13:3716#include "ipc/ipc_descriptors.h"
[email protected]0cb7d8c82013-01-11 15:13:3717
[email protected]2862eebc2013-09-06 23:11:1818#if defined(OS_POSIX)
19#include "base/posix/global_descriptors.h"
20#endif
21
[email protected]3c788582013-01-25 21:51:3522// static
23std::string IPCTestBase::GetChannelName(const std::string& test_client_name) {
24 DCHECK(!test_client_name.empty());
25 return test_client_name + "__Channel";
26}
27
rvargas07b589c2015-01-12 22:23:2328IPCTestBase::IPCTestBase() {
[email protected]3c788582013-01-25 21:51:3529}
30
31IPCTestBase::~IPCTestBase() {
32}
[email protected]0cb7d8c82013-01-11 15:13:3733
[email protected]0cb7d8c82013-01-11 15:13:3734void IPCTestBase::TearDown() {
[email protected]3c788582013-01-25 21:51:3535 message_loop_.reset();
[email protected]0cb7d8c82013-01-11 15:13:3736 MultiProcessTest::TearDown();
37}
38
[email protected]3c788582013-01-25 21:51:3539void IPCTestBase::Init(const std::string& test_client_name) {
danakj03de39b22016-04-23 04:21:0940 InitWithCustomMessageLoop(test_client_name,
41 base::WrapUnique(new base::MessageLoopForIO()));
yzshen22796d82014-09-05 04:42:5842}
43
44void IPCTestBase::InitWithCustomMessageLoop(
45 const std::string& test_client_name,
danakj03de39b22016-04-23 04:21:0946 std::unique_ptr<base::MessageLoop> message_loop) {
[email protected]3c788582013-01-25 21:51:3547 DCHECK(!test_client_name.empty());
48 DCHECK(test_client_name_.empty());
yzshen22796d82014-09-05 04:42:5849 DCHECK(!message_loop_);
50
[email protected]3c788582013-01-25 21:51:3551 test_client_name_ = test_client_name;
dchenge48600452015-12-28 02:24:5052 message_loop_ = std::move(message_loop);
[email protected]3c788582013-01-25 21:51:3553}
54
55void IPCTestBase::CreateChannel(IPC::Listener* listener) {
morrita54f6f80c2014-09-23 21:16:0056 CreateChannelFromChannelHandle(GetTestChannelHandle(), listener);
[email protected]3c788582013-01-25 21:51:3557}
58
59bool IPCTestBase::ConnectChannel() {
60 CHECK(channel_.get());
61 return channel_->Connect();
62}
63
danakj03de39b22016-04-23 04:21:0964std::unique_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() {
dchenge48600452015-12-28 02:24:5065 return std::move(channel_);
[email protected]64860882014-08-04 23:44:1766}
67
danakj03de39b22016-04-23 04:21:0968void IPCTestBase::SetChannel(std::unique_ptr<IPC::Channel> channel) {
dchenge48600452015-12-28 02:24:5069 channel_ = std::move(channel);
[email protected]64860882014-08-04 23:44:1770}
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(
morrita54f6f80c2014-09-23 21:16:0093 CreateChannelFactory(GetTestChannelHandle(), ipc_task_runner.get()),
erikchen27aa7d82015-06-16 21:21:0494 listener, 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() {
rvargas07b589c2015-01-12 22:23:23107 DCHECK(client_process_.IsValid());
108 return client_process_.IsValid();
morrita54f6f80c2014-09-23 21:16:00109}
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) {
rvargas07b589c2015-01-12 22:23:23120 DCHECK(!client_process_.IsValid());
[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() {
rvargas07b589c2015-01-12 22:23:23136 DCHECK(!client_process_.IsValid());
morrita54f6f80c2014-09-23 21:16:00137 client_process_ = SpawnChild(GetTestMainName());
138 return DidStartClient();
139}
140
141#endif
142
[email protected]3c788582013-01-25 21:51:35143bool IPCTestBase::WaitForClientShutdown() {
rvargas07b589c2015-01-12 22:23:23144 DCHECK(client_process_.IsValid());
[email protected]3c788582013-01-25 21:51:35145
rvargas2f70a152015-02-24 00:28:11146 int exit_code;
amistry6de2ee4f2016-05-05 05:12:09147#if defined(OS_ANDROID)
148 bool rv = AndroidWaitForChildExitWithTimeout(
149 client_process_, base::TimeDelta::FromSeconds(5), &exit_code);
150#else
rvargas2f70a152015-02-24 00:28:11151 bool rv = client_process_.WaitForExitWithTimeout(
152 base::TimeDelta::FromSeconds(5), &exit_code);
amistry6de2ee4f2016-05-05 05:12:09153#endif // defined(OS_ANDROID)
rvargas07b589c2015-01-12 22:23:23154 client_process_.Close();
[email protected]3c788582013-01-25 21:51:35155 return rv;
156}
[email protected]64860882014-08-04 23:44:17157
morrita54f6f80c2014-09-23 21:16:00158IPC::ChannelHandle IPCTestBase::GetTestChannelHandle() {
159 return GetChannelName(test_client_name_);
160}
161
morrita70b91492015-03-04 01:52:27162scoped_refptr<base::SequencedTaskRunner> IPCTestBase::task_runner() {
skyostile687bdff2015-05-12 11:29:21163 return message_loop_->task_runner();
[email protected]64860882014-08-04 23:44:17164}
morrita373af03b2014-09-09 19:35:24165
danakj03de39b22016-04-23 04:21:09166std::unique_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory(
morrita373af03b2014-09-09 19:35:24167 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39168 base::SequencedTaskRunner* runner) {
erikchen30dc2812015-09-24 03:26:38169 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER);
morrita373af03b2014-09-09 19:35:24170}