blob: 5e16e42cd8d1db0ae332d85bfa4df63a81d6645a [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]3c788582013-01-25 21:51:3517// static
18std::string IPCTestBase::GetChannelName(const std::string& test_client_name) {
19 DCHECK(!test_client_name.empty());
20 return test_client_name + "__Channel";
21}
22
23IPCTestBase::IPCTestBase()
24 : client_process_(base::kNullProcessHandle) {
25}
26
27IPCTestBase::~IPCTestBase() {
28}
[email protected]0cb7d8c82013-01-11 15:13:3729
30void IPCTestBase::SetUp() {
31 MultiProcessTest::SetUp();
32
33 // Construct a fresh IO Message loop for the duration of each test.
[email protected]3c788582013-01-25 21:51:3534 DCHECK(!message_loop_.get());
[email protected]fd0a773a2013-04-30 20:55:0335 message_loop_.reset(new base::MessageLoopForIO());
[email protected]0cb7d8c82013-01-11 15:13:3736}
37
38void IPCTestBase::TearDown() {
[email protected]3c788582013-01-25 21:51:3539 DCHECK(message_loop_.get());
40 message_loop_.reset();
[email protected]0cb7d8c82013-01-11 15:13:3741 MultiProcessTest::TearDown();
42}
43
[email protected]3c788582013-01-25 21:51:3544void IPCTestBase::Init(const std::string& test_client_name) {
45 DCHECK(!test_client_name.empty());
46 DCHECK(test_client_name_.empty());
47 test_client_name_ = test_client_name;
48}
49
50void IPCTestBase::CreateChannel(IPC::Listener* listener) {
51 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_),
52 listener);
53}
54
55bool IPCTestBase::ConnectChannel() {
56 CHECK(channel_.get());
57 return channel_->Connect();
58}
59
60void IPCTestBase::DestroyChannel() {
61 DCHECK(channel_.get());
62 channel_.reset();
63}
64
65void IPCTestBase::CreateChannelFromChannelHandle(
66 const IPC::ChannelHandle& channel_handle,
67 IPC::Listener* listener) {
68 CHECK(!channel_.get());
69 CHECK(!channel_proxy_.get());
70 channel_.reset(new IPC::Channel(channel_handle,
71 IPC::Channel::MODE_SERVER,
72 listener));
73}
74
75void IPCTestBase::CreateChannelProxy(
76 IPC::Listener* listener,
77 base::SingleThreadTaskRunner* ipc_task_runner) {
78 CHECK(!channel_.get());
79 CHECK(!channel_proxy_.get());
80 channel_proxy_.reset(new IPC::ChannelProxy(GetChannelName(test_client_name_),
81 IPC::Channel::MODE_SERVER,
82 listener,
83 ipc_task_runner));
84}
85
86void IPCTestBase::DestroyChannelProxy() {
87 CHECK(channel_proxy_.get());
88 channel_proxy_.reset();
89}
90
91bool IPCTestBase::StartClient() {
92 DCHECK(client_process_ == base::kNullProcessHandle);
93
94 std::string test_main = test_client_name_ + "TestClientMain";
95 bool debug_on_start =
96 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren);
97
[email protected]0cb7d8c82013-01-11 15:13:3798#if defined(OS_WIN)
[email protected]3c788582013-01-25 21:51:3599 client_process_ = MultiProcessTest::SpawnChild(test_main, debug_on_start);
[email protected]0cb7d8c82013-01-11 15:13:37100#elif defined(OS_POSIX)
[email protected]0cb7d8c82013-01-11 15:13:37101 base::FileHandleMappingVector fds_to_map;
[email protected]3c788582013-01-25 21:51:35102 const int ipcfd = channel_.get() ? channel_->GetClientFileDescriptor() :
103 channel_proxy_->GetClientFileDescriptor();
104 if (ipcfd > -1)
[email protected]0cb7d8c82013-01-11 15:13:37105 fds_to_map.push_back(std::pair<int, int>(ipcfd, kPrimaryIPCChannel + 3));
[email protected]0cb7d8c82013-01-11 15:13:37106
[email protected]3c788582013-01-25 21:51:35107 client_process_ = MultiProcessTest::SpawnChild(test_main,
108 fds_to_map,
109 debug_on_start);
110#endif
111
112 return client_process_ != base::kNullProcessHandle;
[email protected]0cb7d8c82013-01-11 15:13:37113}
[email protected]3c788582013-01-25 21:51:35114
115bool IPCTestBase::WaitForClientShutdown() {
116 DCHECK(client_process_ != base::kNullProcessHandle);
117
118 bool rv = base::WaitForSingleProcess(client_process_,
119 base::TimeDelta::FromSeconds(5));
120 base::CloseProcessHandle(client_process_);
121 client_process_ = base::kNullProcessHandle;
122 return rv;
123}