[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] | e66ef60 | 2013-07-24 05:15:24 | [diff] [blame] | 11 | #include "base/process/kill.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 12 | #include "base/threading/thread.h" |
[email protected] | b43e556 | 2013-06-28 15:20:02 | [diff] [blame] | 13 | #include "base/time/time.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 14 | #include "ipc/ipc_descriptors.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 15 | #include "ipc/ipc_switches.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 16 | |
[email protected] | 2862eebc | 2013-09-06 23:11:18 | [diff] [blame] | 17 | #if defined(OS_POSIX) |
| 18 | #include "base/posix/global_descriptors.h" |
| 19 | #endif |
| 20 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 21 | // static |
| 22 | std::string IPCTestBase::GetChannelName(const std::string& test_client_name) { |
| 23 | DCHECK(!test_client_name.empty()); |
| 24 | return test_client_name + "__Channel"; |
| 25 | } |
| 26 | |
| 27 | IPCTestBase::IPCTestBase() |
| 28 | : client_process_(base::kNullProcessHandle) { |
| 29 | } |
| 30 | |
| 31 | IPCTestBase::~IPCTestBase() { |
| 32 | } |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 33 | |
| 34 | void IPCTestBase::SetUp() { |
| 35 | MultiProcessTest::SetUp(); |
| 36 | |
| 37 | // Construct a fresh IO Message loop for the duration of each test. |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 38 | DCHECK(!message_loop_.get()); |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 39 | message_loop_.reset(new base::MessageLoopForIO()); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void IPCTestBase::TearDown() { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 43 | DCHECK(message_loop_.get()); |
| 44 | message_loop_.reset(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 45 | MultiProcessTest::TearDown(); |
| 46 | } |
| 47 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 48 | void 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 | |
| 54 | void IPCTestBase::CreateChannel(IPC::Listener* listener) { |
| 55 | return CreateChannelFromChannelHandle(GetChannelName(test_client_name_), |
| 56 | listener); |
| 57 | } |
| 58 | |
| 59 | bool IPCTestBase::ConnectChannel() { |
| 60 | CHECK(channel_.get()); |
| 61 | return channel_->Connect(); |
| 62 | } |
| 63 | |
| 64 | void IPCTestBase::DestroyChannel() { |
| 65 | DCHECK(channel_.get()); |
| 66 | channel_.reset(); |
| 67 | } |
| 68 | |
| 69 | void 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 | |
| 79 | void 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 | |
| 90 | void IPCTestBase::DestroyChannelProxy() { |
| 91 | CHECK(channel_proxy_.get()); |
| 92 | channel_proxy_.reset(); |
| 93 | } |
| 94 | |
| 95 | bool 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] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 102 | #if defined(OS_WIN) |
[email protected] | a715b60 | 2014-03-06 10:21:42 | [diff] [blame^] | 103 | client_process_ = SpawnChild(test_main, debug_on_start); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 104 | #elif defined(OS_POSIX) |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 105 | base::FileHandleMappingVector fds_to_map; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 106 | const int ipcfd = channel_.get() ? channel_->GetClientFileDescriptor() : |
| 107 | channel_proxy_->GetClientFileDescriptor(); |
| 108 | if (ipcfd > -1) |
[email protected] | 2862eebc | 2013-09-06 23:11:18 | [diff] [blame] | 109 | fds_to_map.push_back(std::pair<int, int>(ipcfd, |
| 110 | kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
[email protected] | a715b60 | 2014-03-06 10:21:42 | [diff] [blame^] | 111 | base::LaunchOptions options; |
| 112 | options.fds_to_remap = &fds_to_map; |
| 113 | client_process_ = SpawnChildWithOptions(test_main, options, debug_on_start); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 114 | #endif |
| 115 | |
| 116 | return client_process_ != base::kNullProcessHandle; |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 117 | } |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 118 | |
| 119 | bool 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 | } |