viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [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 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 7 | #include "ipc/ipc_test_base.h" |
| 8 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 9 | #include "base/command_line.h" |
rsesek@chromium.org | 1931971 | 2013-07-24 14:15:24 +0900 | [diff] [blame] | 10 | #include "base/process/kill.h" |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 11 | #include "base/threading/thread.h" |
avi@chromium.org | 78a7e7b | 2013-06-29 00:20:02 +0900 | [diff] [blame] | 12 | #include "base/time/time.h" |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 13 | #include "ipc/ipc_descriptors.h" |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 14 | |
dongseong.hwang@intel.com | b8b1be4 | 2013-09-07 08:11:18 +0900 | [diff] [blame] | 15 | #if defined(OS_POSIX) |
| 16 | #include "base/posix/global_descriptors.h" |
| 17 | #endif |
| 18 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 19 | // static |
| 20 | std::string IPCTestBase::GetChannelName(const std::string& test_client_name) { |
| 21 | DCHECK(!test_client_name.empty()); |
| 22 | return test_client_name + "__Channel"; |
| 23 | } |
| 24 | |
| 25 | IPCTestBase::IPCTestBase() |
| 26 | : client_process_(base::kNullProcessHandle) { |
| 27 | } |
| 28 | |
| 29 | IPCTestBase::~IPCTestBase() { |
| 30 | } |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 31 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 32 | void IPCTestBase::TearDown() { |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 33 | message_loop_.reset(); |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 34 | MultiProcessTest::TearDown(); |
| 35 | } |
| 36 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 37 | void IPCTestBase::Init(const std::string& test_client_name) { |
yzshen | 369f915 | 2014-09-05 13:42:58 +0900 | [diff] [blame] | 38 | InitWithCustomMessageLoop( |
| 39 | test_client_name, |
| 40 | scoped_ptr<base::MessageLoop>(new base::MessageLoopForIO())); |
| 41 | } |
| 42 | |
| 43 | void IPCTestBase::InitWithCustomMessageLoop( |
| 44 | const std::string& test_client_name, |
| 45 | scoped_ptr<base::MessageLoop> message_loop) { |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 46 | DCHECK(!test_client_name.empty()); |
| 47 | DCHECK(test_client_name_.empty()); |
yzshen | 369f915 | 2014-09-05 13:42:58 +0900 | [diff] [blame] | 48 | DCHECK(!message_loop_); |
| 49 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 50 | test_client_name_ = test_client_name; |
yzshen | 369f915 | 2014-09-05 13:42:58 +0900 | [diff] [blame] | 51 | message_loop_ = message_loop.Pass(); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void IPCTestBase::CreateChannel(IPC::Listener* listener) { |
morrita | ab77970 | 2014-09-10 04:35:24 +0900 | [diff] [blame^] | 55 | CreateChannelFromChannelHandle( |
| 56 | GetChannelName(test_client_name_), listener); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool IPCTestBase::ConnectChannel() { |
| 60 | CHECK(channel_.get()); |
| 61 | return channel_->Connect(); |
| 62 | } |
| 63 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 64 | scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() { |
| 65 | return channel_.Pass(); |
| 66 | } |
| 67 | |
| 68 | void IPCTestBase::SetChannel(scoped_ptr<IPC::Channel> channel) { |
| 69 | channel_ = channel.Pass(); |
| 70 | } |
| 71 | |
| 72 | |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 73 | void IPCTestBase::DestroyChannel() { |
| 74 | DCHECK(channel_.get()); |
| 75 | channel_.reset(); |
| 76 | } |
| 77 | |
| 78 | void IPCTestBase::CreateChannelFromChannelHandle( |
| 79 | const IPC::ChannelHandle& channel_handle, |
| 80 | IPC::Listener* listener) { |
| 81 | CHECK(!channel_.get()); |
| 82 | CHECK(!channel_proxy_.get()); |
morrita | ab77970 | 2014-09-10 04:35:24 +0900 | [diff] [blame^] | 83 | channel_ = CreateChannelFactory( |
| 84 | channel_handle, task_runner().get())->BuildChannel(listener); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void IPCTestBase::CreateChannelProxy( |
| 88 | IPC::Listener* listener, |
dcheng | 07bc797 | 2014-09-04 03:01:16 +0900 | [diff] [blame] | 89 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 90 | CHECK(!channel_.get()); |
| 91 | CHECK(!channel_proxy_.get()); |
morrita | ab77970 | 2014-09-10 04:35:24 +0900 | [diff] [blame^] | 92 | channel_proxy_ = IPC::ChannelProxy::Create( |
| 93 | CreateChannelFactory(GetChannelName(test_client_name_), |
| 94 | ipc_task_runner.get()), |
| 95 | listener, |
| 96 | ipc_task_runner); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void IPCTestBase::DestroyChannelProxy() { |
| 100 | CHECK(channel_proxy_.get()); |
| 101 | channel_proxy_.reset(); |
| 102 | } |
| 103 | |
| 104 | bool IPCTestBase::StartClient() { |
| 105 | DCHECK(client_process_ == base::kNullProcessHandle); |
| 106 | |
| 107 | std::string test_main = test_client_name_ + "TestClientMain"; |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 108 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 109 | #if defined(OS_WIN) |
viettrungluu@chromium.org | ceed605 | 2014-03-09 09:59:31 +0900 | [diff] [blame] | 110 | client_process_ = SpawnChild(test_main); |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 111 | #elif defined(OS_POSIX) |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 112 | base::FileHandleMappingVector fds_to_map; |
morrita@chromium.org | fde2b6b | 2014-06-07 05:13:51 +0900 | [diff] [blame] | 113 | const int ipcfd = channel_.get() |
| 114 | ? channel_->GetClientFileDescriptor() |
| 115 | : channel_proxy_->GetClientFileDescriptor(); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 116 | if (ipcfd > -1) |
dongseong.hwang@intel.com | b8b1be4 | 2013-09-07 08:11:18 +0900 | [diff] [blame] | 117 | fds_to_map.push_back(std::pair<int, int>(ipcfd, |
| 118 | kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
viettrungluu@chromium.org | dc9e0c8 | 2014-03-06 19:21:42 +0900 | [diff] [blame] | 119 | base::LaunchOptions options; |
| 120 | options.fds_to_remap = &fds_to_map; |
viettrungluu@chromium.org | ceed605 | 2014-03-09 09:59:31 +0900 | [diff] [blame] | 121 | client_process_ = SpawnChildWithOptions(test_main, options); |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 122 | #endif |
| 123 | |
| 124 | return client_process_ != base::kNullProcessHandle; |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 125 | } |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 126 | |
| 127 | bool IPCTestBase::WaitForClientShutdown() { |
| 128 | DCHECK(client_process_ != base::kNullProcessHandle); |
| 129 | |
| 130 | bool rv = base::WaitForSingleProcess(client_process_, |
| 131 | base::TimeDelta::FromSeconds(5)); |
| 132 | base::CloseProcessHandle(client_process_); |
| 133 | client_process_ = base::kNullProcessHandle; |
| 134 | return rv; |
| 135 | } |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 136 | |
morrita | a422094 | 2014-08-29 09:20:59 +0900 | [diff] [blame] | 137 | scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() { |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 138 | return message_loop_->message_loop_proxy(); |
| 139 | } |
morrita | ab77970 | 2014-09-10 04:35:24 +0900 | [diff] [blame^] | 140 | |
| 141 | scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory( |
| 142 | const IPC::ChannelHandle& handle, |
| 143 | base::TaskRunner* runner) { |
| 144 | return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER); |
| 145 | } |