blob: a8ee64f8caabab123936a5faf329a076e97264d1 [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]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#include "ipc/ipc_switches.h"
[email protected]0cb7d8c82013-01-11 15:13:3715
[email protected]3c788582013-01-25 21:51:3516// static
17std::string IPCTestBase::GetChannelName(const std::string& test_client_name) {
18 DCHECK(!test_client_name.empty());
19 return test_client_name + "__Channel";
20}
21
22IPCTestBase::IPCTestBase()
23 : client_process_(base::kNullProcessHandle) {
24}
25
26IPCTestBase::~IPCTestBase() {
27}
[email protected]0cb7d8c82013-01-11 15:13:3728
29void IPCTestBase::SetUp() {
30 MultiProcessTest::SetUp();
31
32 // Construct a fresh IO Message loop for the duration of each test.
[email protected]3c788582013-01-25 21:51:3533 DCHECK(!message_loop_.get());
[email protected]fd0a773a2013-04-30 20:55:0334 message_loop_.reset(new base::MessageLoopForIO());
[email protected]0cb7d8c82013-01-11 15:13:3735}
36
37void IPCTestBase::TearDown() {
[email protected]3c788582013-01-25 21:51:3538 DCHECK(message_loop_.get());
39 message_loop_.reset();
[email protected]0cb7d8c82013-01-11 15:13:3740 MultiProcessTest::TearDown();
41}
42
[email protected]3c788582013-01-25 21:51:3543void IPCTestBase::Init(const std::string& test_client_name) {
44 DCHECK(!test_client_name.empty());
45 DCHECK(test_client_name_.empty());
46 test_client_name_ = test_client_name;
47}
48
49void IPCTestBase::CreateChannel(IPC::Listener* listener) {
50 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_),
51 listener);
52}
53
54bool IPCTestBase::ConnectChannel() {
55 CHECK(channel_.get());
56 return channel_->Connect();
57}
58
59void IPCTestBase::DestroyChannel() {
60 DCHECK(channel_.get());
61 channel_.reset();
62}
63
64void IPCTestBase::CreateChannelFromChannelHandle(
65 const IPC::ChannelHandle& channel_handle,
66 IPC::Listener* listener) {
67 CHECK(!channel_.get());
68 CHECK(!channel_proxy_.get());
69 channel_.reset(new IPC::Channel(channel_handle,
70 IPC::Channel::MODE_SERVER,
71 listener));
72}
73
74void IPCTestBase::CreateChannelProxy(
75 IPC::Listener* listener,
76 base::SingleThreadTaskRunner* ipc_task_runner) {
77 CHECK(!channel_.get());
78 CHECK(!channel_proxy_.get());
79 channel_proxy_.reset(new IPC::ChannelProxy(GetChannelName(test_client_name_),
80 IPC::Channel::MODE_SERVER,
81 listener,
82 ipc_task_runner));
83}
84
85void IPCTestBase::DestroyChannelProxy() {
86 CHECK(channel_proxy_.get());
87 channel_proxy_.reset();
88}
89
90bool IPCTestBase::StartClient() {
91 DCHECK(client_process_ == base::kNullProcessHandle);
92
93 std::string test_main = test_client_name_ + "TestClientMain";
94 bool debug_on_start =
95 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren);
96
[email protected]0cb7d8c82013-01-11 15:13:3797#if defined(OS_WIN)
[email protected]3c788582013-01-25 21:51:3598 client_process_ = MultiProcessTest::SpawnChild(test_main, debug_on_start);
[email protected]0cb7d8c82013-01-11 15:13:3799#elif defined(OS_POSIX)
[email protected]0cb7d8c82013-01-11 15:13:37100 base::FileHandleMappingVector fds_to_map;
[email protected]3c788582013-01-25 21:51:35101 const int ipcfd = channel_.get() ? channel_->GetClientFileDescriptor() :
102 channel_proxy_->GetClientFileDescriptor();
103 if (ipcfd > -1)
[email protected]0cb7d8c82013-01-11 15:13:37104 fds_to_map.push_back(std::pair<int, int>(ipcfd, kPrimaryIPCChannel + 3));
[email protected]0cb7d8c82013-01-11 15:13:37105
[email protected]3c788582013-01-25 21:51:35106 client_process_ = MultiProcessTest::SpawnChild(test_main,
107 fds_to_map,
108 debug_on_start);
109#endif
110
111 return client_process_ != base::kNullProcessHandle;
[email protected]0cb7d8c82013-01-11 15:13:37112}
[email protected]3c788582013-01-25 21:51:35113
114bool IPCTestBase::WaitForClientShutdown() {
115 DCHECK(client_process_ != base::kNullProcessHandle);
116
117 bool rv = base::WaitForSingleProcess(client_process_,
118 base::TimeDelta::FromSeconds(5));
119 base::CloseProcessHandle(client_process_);
120 client_process_ = base::kNullProcessHandle;
121 return rv;
122}