blob: 80ddd2da2ea6c2842236a85a12392086086c0eea [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) {
55 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_),
56 listener);
57}
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());
[email protected]e482111a82014-05-30 03:58:5983 channel_ = IPC::Channel::CreateServer(channel_handle, 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());
[email protected]fca876a12014-06-05 16:15:3891 channel_proxy_ = IPC::ChannelProxy::Create(GetChannelName(test_client_name_),
[email protected]3b0e4662014-06-02 20:29:3092 IPC::Channel::MODE_SERVER,
93 listener,
[email protected]fca876a12014-06-05 16:15:3894 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
102bool IPCTestBase::StartClient() {
103 DCHECK(client_process_ == base::kNullProcessHandle);
104
105 std::string test_main = test_client_name_ + "TestClientMain";
[email protected]3c788582013-01-25 21:51:35106
[email protected]0cb7d8c82013-01-11 15:13:37107#if defined(OS_WIN)
[email protected]963a91b2014-03-09 00:59:31108 client_process_ = SpawnChild(test_main);
[email protected]0cb7d8c82013-01-11 15:13:37109#elif defined(OS_POSIX)
[email protected]0cb7d8c82013-01-11 15:13:37110 base::FileHandleMappingVector fds_to_map;
[email protected]2f60c9b2014-06-06 20:13:51111 const int ipcfd = channel_.get()
112 ? channel_->GetClientFileDescriptor()
113 : channel_proxy_->GetClientFileDescriptor();
[email protected]3c788582013-01-25 21:51:35114 if (ipcfd > -1)
[email protected]2862eebc2013-09-06 23:11:18115 fds_to_map.push_back(std::pair<int, int>(ipcfd,
116 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
[email protected]a715b602014-03-06 10:21:42117 base::LaunchOptions options;
118 options.fds_to_remap = &fds_to_map;
[email protected]963a91b2014-03-09 00:59:31119 client_process_ = SpawnChildWithOptions(test_main, options);
[email protected]3c788582013-01-25 21:51:35120#endif
121
122 return client_process_ != base::kNullProcessHandle;
[email protected]0cb7d8c82013-01-11 15:13:37123}
[email protected]3c788582013-01-25 21:51:35124
125bool IPCTestBase::WaitForClientShutdown() {
126 DCHECK(client_process_ != base::kNullProcessHandle);
127
128 bool rv = base::WaitForSingleProcess(client_process_,
129 base::TimeDelta::FromSeconds(5));
130 base::CloseProcessHandle(client_process_);
131 client_process_ = base::kNullProcessHandle;
132 return rv;
133}
[email protected]64860882014-08-04 23:44:17134
morritad36736c2014-08-29 00:20:59135scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() {
[email protected]64860882014-08-04 23:44:17136 return message_loop_->message_loop_proxy();
137}