blob: b25f844f07df84d92c5a7cd173fcef002e3ddc69 [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"
10#include "base/debug/debug_on_start_win.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#include "ipc/ipc_switches.h"
[email protected]0cb7d8c82013-01-11 15:13:3716
[email protected]2862eebc2013-09-06 23:11:1817#if defined(OS_POSIX)
18#include "base/posix/global_descriptors.h"
19#endif
20
[email protected]3c788582013-01-25 21:51:3521// static
22std::string IPCTestBase::GetChannelName(const std::string& test_client_name) {
23 DCHECK(!test_client_name.empty());
24 return test_client_name + "__Channel";
25}
26
27IPCTestBase::IPCTestBase()
28 : client_process_(base::kNullProcessHandle) {
29}
30
31IPCTestBase::~IPCTestBase() {
32}
[email protected]0cb7d8c82013-01-11 15:13:3733
34void IPCTestBase::SetUp() {
35 MultiProcessTest::SetUp();
36
37 // Construct a fresh IO Message loop for the duration of each test.
[email protected]3c788582013-01-25 21:51:3538 DCHECK(!message_loop_.get());
[email protected]fd0a773a2013-04-30 20:55:0339 message_loop_.reset(new base::MessageLoopForIO());
[email protected]0cb7d8c82013-01-11 15:13:3740}
41
42void IPCTestBase::TearDown() {
[email protected]3c788582013-01-25 21:51:3543 DCHECK(message_loop_.get());
44 message_loop_.reset();
[email protected]0cb7d8c82013-01-11 15:13:3745 MultiProcessTest::TearDown();
46}
47
[email protected]3c788582013-01-25 21:51:3548void IPCTestBase::Init(const std::string& test_client_name) {
49 DCHECK(!test_client_name.empty());
50 DCHECK(test_client_name_.empty());
51 test_client_name_ = test_client_name;
52}
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
64void IPCTestBase::DestroyChannel() {
65 DCHECK(channel_.get());
66 channel_.reset();
67}
68
69void IPCTestBase::CreateChannelFromChannelHandle(
70 const IPC::ChannelHandle& channel_handle,
71 IPC::Listener* listener) {
72 CHECK(!channel_.get());
73 CHECK(!channel_proxy_.get());
74 channel_.reset(new IPC::Channel(channel_handle,
75 IPC::Channel::MODE_SERVER,
76 listener));
77}
78
79void IPCTestBase::CreateChannelProxy(
80 IPC::Listener* listener,
81 base::SingleThreadTaskRunner* ipc_task_runner) {
82 CHECK(!channel_.get());
83 CHECK(!channel_proxy_.get());
84 channel_proxy_.reset(new IPC::ChannelProxy(GetChannelName(test_client_name_),
85 IPC::Channel::MODE_SERVER,
86 listener,
87 ipc_task_runner));
88}
89
90void IPCTestBase::DestroyChannelProxy() {
91 CHECK(channel_proxy_.get());
92 channel_proxy_.reset();
93}
94
95bool IPCTestBase::StartClient() {
96 DCHECK(client_process_ == base::kNullProcessHandle);
97
98 std::string test_main = test_client_name_ + "TestClientMain";
99 bool debug_on_start =
100 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren);
101
[email protected]0cb7d8c82013-01-11 15:13:37102#if defined(OS_WIN)
[email protected]a715b602014-03-06 10:21:42103 client_process_ = SpawnChild(test_main, debug_on_start);
[email protected]0cb7d8c82013-01-11 15:13:37104#elif defined(OS_POSIX)
[email protected]0cb7d8c82013-01-11 15:13:37105 base::FileHandleMappingVector fds_to_map;
[email protected]3c788582013-01-25 21:51:35106 const int ipcfd = channel_.get() ? channel_->GetClientFileDescriptor() :
107 channel_proxy_->GetClientFileDescriptor();
108 if (ipcfd > -1)
[email protected]2862eebc2013-09-06 23:11:18109 fds_to_map.push_back(std::pair<int, int>(ipcfd,
110 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
[email protected]a715b602014-03-06 10:21:42111 base::LaunchOptions options;
112 options.fds_to_remap = &fds_to_map;
113 client_process_ = SpawnChildWithOptions(test_main, options, debug_on_start);
[email protected]3c788582013-01-25 21:51:35114#endif
115
116 return client_process_ != base::kNullProcessHandle;
[email protected]0cb7d8c82013-01-11 15:13:37117}
[email protected]3c788582013-01-25 21:51:35118
119bool IPCTestBase::WaitForClientShutdown() {
120 DCHECK(client_process_ != base::kNullProcessHandle);
121
122 bool rv = base::WaitForSingleProcess(client_process_,
123 base::TimeDelta::FromSeconds(5));
124 base::CloseProcessHandle(client_process_);
125 client_process_ = base::kNullProcessHandle;
126 return rv;
127}