blob: 7adb1a4880ec3dcb8d9dfa3e16768e66802a214e [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
32void IPCTestBase::SetUp() {
33 MultiProcessTest::SetUp();
34
morritad36736c2014-08-29 00:20:5935 // Construct a fresh Message loop for the duration of each test.
[email protected]3c788582013-01-25 21:51:3536 DCHECK(!message_loop_.get());
[email protected]fd0a773a2013-04-30 20:55:0337 message_loop_.reset(new base::MessageLoopForIO());
[email protected]0cb7d8c82013-01-11 15:13:3738}
39
40void IPCTestBase::TearDown() {
[email protected]3c788582013-01-25 21:51:3541 DCHECK(message_loop_.get());
42 message_loop_.reset();
[email protected]0cb7d8c82013-01-11 15:13:3743 MultiProcessTest::TearDown();
44}
45
[email protected]3c788582013-01-25 21:51:3546void IPCTestBase::Init(const std::string& test_client_name) {
47 DCHECK(!test_client_name.empty());
48 DCHECK(test_client_name_.empty());
49 test_client_name_ = test_client_name;
50}
51
52void IPCTestBase::CreateChannel(IPC::Listener* listener) {
53 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_),
54 listener);
55}
56
57bool IPCTestBase::ConnectChannel() {
58 CHECK(channel_.get());
59 return channel_->Connect();
60}
61
[email protected]64860882014-08-04 23:44:1762scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() {
63 return channel_.Pass();
64}
65
66void IPCTestBase::SetChannel(scoped_ptr<IPC::Channel> channel) {
67 channel_ = channel.Pass();
68}
69
70
[email protected]3c788582013-01-25 21:51:3571void IPCTestBase::DestroyChannel() {
72 DCHECK(channel_.get());
73 channel_.reset();
74}
75
76void IPCTestBase::CreateChannelFromChannelHandle(
77 const IPC::ChannelHandle& channel_handle,
78 IPC::Listener* listener) {
79 CHECK(!channel_.get());
80 CHECK(!channel_proxy_.get());
[email protected]e482111a82014-05-30 03:58:5981 channel_ = IPC::Channel::CreateServer(channel_handle, listener);
[email protected]3c788582013-01-25 21:51:3582}
83
84void IPCTestBase::CreateChannelProxy(
85 IPC::Listener* listener,
dchengff04843f2014-09-03 18:01:1686 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) {
[email protected]3c788582013-01-25 21:51:3587 CHECK(!channel_.get());
88 CHECK(!channel_proxy_.get());
[email protected]fca876a12014-06-05 16:15:3889 channel_proxy_ = IPC::ChannelProxy::Create(GetChannelName(test_client_name_),
[email protected]3b0e4662014-06-02 20:29:3090 IPC::Channel::MODE_SERVER,
91 listener,
[email protected]fca876a12014-06-05 16:15:3892 ipc_task_runner);
[email protected]3c788582013-01-25 21:51:3593}
94
95void IPCTestBase::DestroyChannelProxy() {
96 CHECK(channel_proxy_.get());
97 channel_proxy_.reset();
98}
99
100bool IPCTestBase::StartClient() {
101 DCHECK(client_process_ == base::kNullProcessHandle);
102
103 std::string test_main = test_client_name_ + "TestClientMain";
[email protected]3c788582013-01-25 21:51:35104
[email protected]0cb7d8c82013-01-11 15:13:37105#if defined(OS_WIN)
[email protected]963a91b2014-03-09 00:59:31106 client_process_ = SpawnChild(test_main);
[email protected]0cb7d8c82013-01-11 15:13:37107#elif defined(OS_POSIX)
[email protected]0cb7d8c82013-01-11 15:13:37108 base::FileHandleMappingVector fds_to_map;
[email protected]2f60c9b2014-06-06 20:13:51109 const int ipcfd = channel_.get()
110 ? channel_->GetClientFileDescriptor()
111 : channel_proxy_->GetClientFileDescriptor();
[email protected]3c788582013-01-25 21:51:35112 if (ipcfd > -1)
[email protected]2862eebc2013-09-06 23:11:18113 fds_to_map.push_back(std::pair<int, int>(ipcfd,
114 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
[email protected]a715b602014-03-06 10:21:42115 base::LaunchOptions options;
116 options.fds_to_remap = &fds_to_map;
[email protected]963a91b2014-03-09 00:59:31117 client_process_ = SpawnChildWithOptions(test_main, options);
[email protected]3c788582013-01-25 21:51:35118#endif
119
120 return client_process_ != base::kNullProcessHandle;
[email protected]0cb7d8c82013-01-11 15:13:37121}
[email protected]3c788582013-01-25 21:51:35122
123bool IPCTestBase::WaitForClientShutdown() {
124 DCHECK(client_process_ != base::kNullProcessHandle);
125
126 bool rv = base::WaitForSingleProcess(client_process_,
127 base::TimeDelta::FromSeconds(5));
128 base::CloseProcessHandle(client_process_);
129 client_process_ = base::kNullProcessHandle;
130 return rv;
131}
[email protected]64860882014-08-04 23:44:17132
morritad36736c2014-08-29 00:20:59133scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() {
[email protected]64860882014-08-04 23:44:17134 return message_loop_->message_loop_proxy();
135}
morritad36736c2014-08-29 00:20:59136
137void IPCTestBase::set_message_loop(scoped_ptr<base::MessageLoop> loop) {
138 message_loop_ = loop.Pass();
139}