[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 1 | // 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] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 7 | #include "ipc/ipc_test_base.h" |
| 8 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 9 | #include "base/command_line.h" |
| 10 | #include "base/debug/debug_on_start_win.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 11 | #include "base/threading/thread.h" |
[email protected] | b43e556 | 2013-06-28 15:20:02 | [diff] [blame^] | 12 | #include "base/time/time.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 13 | #include "ipc/ipc_descriptors.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 14 | #include "ipc/ipc_switches.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 15 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 16 | // static |
| 17 | std::string IPCTestBase::GetChannelName(const std::string& test_client_name) { |
| 18 | DCHECK(!test_client_name.empty()); |
| 19 | return test_client_name + "__Channel"; |
| 20 | } |
| 21 | |
| 22 | IPCTestBase::IPCTestBase() |
| 23 | : client_process_(base::kNullProcessHandle) { |
| 24 | } |
| 25 | |
| 26 | IPCTestBase::~IPCTestBase() { |
| 27 | } |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 28 | |
| 29 | void IPCTestBase::SetUp() { |
| 30 | MultiProcessTest::SetUp(); |
| 31 | |
| 32 | // Construct a fresh IO Message loop for the duration of each test. |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 33 | DCHECK(!message_loop_.get()); |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 34 | message_loop_.reset(new base::MessageLoopForIO()); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void IPCTestBase::TearDown() { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 38 | DCHECK(message_loop_.get()); |
| 39 | message_loop_.reset(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 40 | MultiProcessTest::TearDown(); |
| 41 | } |
| 42 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 43 | void 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 | |
| 49 | void IPCTestBase::CreateChannel(IPC::Listener* listener) { |
| 50 | return CreateChannelFromChannelHandle(GetChannelName(test_client_name_), |
| 51 | listener); |
| 52 | } |
| 53 | |
| 54 | bool IPCTestBase::ConnectChannel() { |
| 55 | CHECK(channel_.get()); |
| 56 | return channel_->Connect(); |
| 57 | } |
| 58 | |
| 59 | void IPCTestBase::DestroyChannel() { |
| 60 | DCHECK(channel_.get()); |
| 61 | channel_.reset(); |
| 62 | } |
| 63 | |
| 64 | void 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 | |
| 74 | void 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 | |
| 85 | void IPCTestBase::DestroyChannelProxy() { |
| 86 | CHECK(channel_proxy_.get()); |
| 87 | channel_proxy_.reset(); |
| 88 | } |
| 89 | |
| 90 | bool 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] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 97 | #if defined(OS_WIN) |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 98 | client_process_ = MultiProcessTest::SpawnChild(test_main, debug_on_start); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 99 | #elif defined(OS_POSIX) |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 100 | base::FileHandleMappingVector fds_to_map; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 101 | const int ipcfd = channel_.get() ? channel_->GetClientFileDescriptor() : |
| 102 | channel_proxy_->GetClientFileDescriptor(); |
| 103 | if (ipcfd > -1) |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 104 | fds_to_map.push_back(std::pair<int, int>(ipcfd, kPrimaryIPCChannel + 3)); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 105 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 106 | 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] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 112 | } |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 113 | |
| 114 | bool 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 | } |