blob: 4de55431b38bf9c9242bdb88c5fb3e737620eac9 [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
skyostile687bdff2015-05-12 11:29:217#include "base/single_thread_task_runner.h"
[email protected]0cb7d8c82013-01-11 15:13:378#include "ipc/ipc_test_base.h"
9
[email protected]0cb7d8c82013-01-11 15:13:3710#include "base/command_line.h"
[email protected]e66ef602013-07-24 05:15:2411#include "base/process/kill.h"
[email protected]0cb7d8c82013-01-11 15:13:3712#include "base/threading/thread.h"
[email protected]b43e5562013-06-28 15:20:0213#include "base/time/time.h"
[email protected]0cb7d8c82013-01-11 15:13:3714#include "ipc/ipc_descriptors.h"
[email protected]0cb7d8c82013-01-11 15:13:3715
[email protected]2862eebc2013-09-06 23:11:1816#if defined(OS_POSIX)
17#include "base/posix/global_descriptors.h"
18#endif
19
[email protected]3c788582013-01-25 21:51:3520// static
21std::string IPCTestBase::GetChannelName(const std::string& test_client_name) {
22 DCHECK(!test_client_name.empty());
23 return test_client_name + "__Channel";
24}
25
rvargas07b589c2015-01-12 22:23:2326IPCTestBase::IPCTestBase() {
[email protected]3c788582013-01-25 21:51:3527}
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()),
erikchen27aa7d82015-06-16 21:21:0493 listener, ipc_task_runner);
[email protected]3c788582013-01-25 21:51:3594}
95
96void IPCTestBase::DestroyChannelProxy() {
97 CHECK(channel_proxy_.get());
98 channel_proxy_.reset();
99}
100
morrita54f6f80c2014-09-23 21:16:00101std::string IPCTestBase::GetTestMainName() const {
102 return test_client_name_ + "TestClientMain";
103}
104
105bool IPCTestBase::DidStartClient() {
rvargas07b589c2015-01-12 22:23:23106 DCHECK(client_process_.IsValid());
107 return client_process_.IsValid();
morrita54f6f80c2014-09-23 21:16:00108}
109
110#if defined(OS_POSIX)
111
[email protected]3c788582013-01-25 21:51:35112bool IPCTestBase::StartClient() {
morrita54f6f80c2014-09-23 21:16:00113 return StartClientWithFD(channel_
114 ? channel_->GetClientFileDescriptor()
115 : channel_proxy_->GetClientFileDescriptor());
116}
[email protected]3c788582013-01-25 21:51:35117
morrita54f6f80c2014-09-23 21:16:00118bool IPCTestBase::StartClientWithFD(int ipcfd) {
rvargas07b589c2015-01-12 22:23:23119 DCHECK(!client_process_.IsValid());
[email protected]3c788582013-01-25 21:51:35120
[email protected]0cb7d8c82013-01-11 15:13:37121 base::FileHandleMappingVector fds_to_map;
[email protected]3c788582013-01-25 21:51:35122 if (ipcfd > -1)
[email protected]2862eebc2013-09-06 23:11:18123 fds_to_map.push_back(std::pair<int, int>(ipcfd,
124 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
[email protected]a715b602014-03-06 10:21:42125 base::LaunchOptions options;
126 options.fds_to_remap = &fds_to_map;
morrita54f6f80c2014-09-23 21:16:00127 client_process_ = SpawnChildWithOptions(GetTestMainName(), options);
[email protected]3c788582013-01-25 21:51:35128
morrita54f6f80c2014-09-23 21:16:00129 return DidStartClient();
[email protected]0cb7d8c82013-01-11 15:13:37130}
[email protected]3c788582013-01-25 21:51:35131
morrita54f6f80c2014-09-23 21:16:00132#elif defined(OS_WIN)
133
134bool IPCTestBase::StartClient() {
rvargas07b589c2015-01-12 22:23:23135 DCHECK(!client_process_.IsValid());
morrita54f6f80c2014-09-23 21:16:00136 client_process_ = SpawnChild(GetTestMainName());
137 return DidStartClient();
138}
139
140#endif
141
[email protected]3c788582013-01-25 21:51:35142bool IPCTestBase::WaitForClientShutdown() {
rvargas07b589c2015-01-12 22:23:23143 DCHECK(client_process_.IsValid());
[email protected]3c788582013-01-25 21:51:35144
rvargas2f70a152015-02-24 00:28:11145 int exit_code;
146 bool rv = client_process_.WaitForExitWithTimeout(
147 base::TimeDelta::FromSeconds(5), &exit_code);
rvargas07b589c2015-01-12 22:23:23148 client_process_.Close();
[email protected]3c788582013-01-25 21:51:35149 return rv;
150}
[email protected]64860882014-08-04 23:44:17151
morrita54f6f80c2014-09-23 21:16:00152IPC::ChannelHandle IPCTestBase::GetTestChannelHandle() {
153 return GetChannelName(test_client_name_);
154}
155
morrita70b91492015-03-04 01:52:27156scoped_refptr<base::SequencedTaskRunner> IPCTestBase::task_runner() {
skyostile687bdff2015-05-12 11:29:21157 return message_loop_->task_runner();
[email protected]64860882014-08-04 23:44:17158}
morrita373af03b2014-09-09 19:35:24159
160scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory(
161 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39162 base::SequencedTaskRunner* runner) {
erikchen27aa7d82015-06-16 21:21:04163 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER,
164 nullptr);
morrita373af03b2014-09-09 19:35:24165}