[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 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 5 | #include "ipc/ipc_test_base.h" |
| 6 | |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 9 | #include "base/command_line.h" |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
[email protected] | e66ef60 | 2013-07-24 05:15:24 | [diff] [blame] | 11 | #include "base/process/kill.h" |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 12 | #include "base/single_thread_task_runner.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 13 | #include "base/threading/thread.h" |
[email protected] | b43e556 | 2013-06-28 15:20:02 | [diff] [blame] | 14 | #include "base/time/time.h" |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 16 | #include "ipc/ipc_descriptors.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 17 | |
[email protected] | 2862eebc | 2013-09-06 23:11:18 | [diff] [blame] | 18 | #if defined(OS_POSIX) |
| 19 | #include "base/posix/global_descriptors.h" |
| 20 | #endif |
| 21 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 22 | // static |
| 23 | std::string IPCTestBase::GetChannelName(const std::string& test_client_name) { |
| 24 | DCHECK(!test_client_name.empty()); |
| 25 | return test_client_name + "__Channel"; |
| 26 | } |
| 27 | |
rvargas | 07b589c | 2015-01-12 22:23:23 | [diff] [blame] | 28 | IPCTestBase::IPCTestBase() { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | IPCTestBase::~IPCTestBase() { |
| 32 | } |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 33 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 34 | void IPCTestBase::TearDown() { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 35 | message_loop_.reset(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 36 | MultiProcessTest::TearDown(); |
| 37 | } |
| 38 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 39 | void IPCTestBase::Init(const std::string& test_client_name) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 40 | InitWithCustomMessageLoop(test_client_name, |
| 41 | base::WrapUnique(new base::MessageLoopForIO())); |
yzshen | 22796d8 | 2014-09-05 04:42:58 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void IPCTestBase::InitWithCustomMessageLoop( |
| 45 | const std::string& test_client_name, |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 46 | std::unique_ptr<base::MessageLoop> message_loop) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 47 | DCHECK(!test_client_name.empty()); |
| 48 | DCHECK(test_client_name_.empty()); |
yzshen | 22796d8 | 2014-09-05 04:42:58 | [diff] [blame] | 49 | DCHECK(!message_loop_); |
| 50 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 51 | test_client_name_ = test_client_name; |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 52 | message_loop_ = std::move(message_loop); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void IPCTestBase::CreateChannel(IPC::Listener* listener) { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 56 | CreateChannelFromChannelHandle(GetTestChannelHandle(), listener); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool IPCTestBase::ConnectChannel() { |
| 60 | CHECK(channel_.get()); |
| 61 | return channel_->Connect(); |
| 62 | } |
| 63 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 64 | std::unique_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() { |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 65 | return std::move(channel_); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 66 | } |
| 67 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 68 | void IPCTestBase::SetChannel(std::unique_ptr<IPC::Channel> channel) { |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 69 | channel_ = std::move(channel); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [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 | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 83 | channel_ = CreateChannelFactory( |
| 84 | channel_handle, task_runner().get())->BuildChannel(listener); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void IPCTestBase::CreateChannelProxy( |
| 88 | IPC::Listener* listener, |
dcheng | ff04843f | 2014-09-03 18:01:16 | [diff] [blame] | 89 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 90 | CHECK(!channel_.get()); |
| 91 | CHECK(!channel_proxy_.get()); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 92 | channel_proxy_ = IPC::ChannelProxy::Create( |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 93 | CreateChannelFactory(GetTestChannelHandle(), ipc_task_runner.get()), |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 94 | listener, ipc_task_runner); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void IPCTestBase::DestroyChannelProxy() { |
| 98 | CHECK(channel_proxy_.get()); |
| 99 | channel_proxy_.reset(); |
| 100 | } |
| 101 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 102 | std::string IPCTestBase::GetTestMainName() const { |
| 103 | return test_client_name_ + "TestClientMain"; |
| 104 | } |
| 105 | |
| 106 | bool IPCTestBase::DidStartClient() { |
rvargas | 07b589c | 2015-01-12 22:23:23 | [diff] [blame] | 107 | DCHECK(client_process_.IsValid()); |
| 108 | return client_process_.IsValid(); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | #if defined(OS_POSIX) |
| 112 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 113 | bool IPCTestBase::StartClient() { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 114 | return StartClientWithFD(channel_ |
| 115 | ? channel_->GetClientFileDescriptor() |
| 116 | : channel_proxy_->GetClientFileDescriptor()); |
| 117 | } |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 118 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 119 | bool IPCTestBase::StartClientWithFD(int ipcfd) { |
rvargas | 07b589c | 2015-01-12 22:23:23 | [diff] [blame] | 120 | DCHECK(!client_process_.IsValid()); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 121 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 122 | base::FileHandleMappingVector fds_to_map; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 123 | if (ipcfd > -1) |
[email protected] | 2862eebc | 2013-09-06 23:11:18 | [diff] [blame] | 124 | fds_to_map.push_back(std::pair<int, int>(ipcfd, |
| 125 | kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
[email protected] | a715b60 | 2014-03-06 10:21:42 | [diff] [blame] | 126 | base::LaunchOptions options; |
| 127 | options.fds_to_remap = &fds_to_map; |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 128 | client_process_ = SpawnChildWithOptions(GetTestMainName(), options); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 129 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 130 | return DidStartClient(); |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 131 | } |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 132 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 133 | #elif defined(OS_WIN) |
| 134 | |
| 135 | bool IPCTestBase::StartClient() { |
rvargas | 07b589c | 2015-01-12 22:23:23 | [diff] [blame] | 136 | DCHECK(!client_process_.IsValid()); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 137 | client_process_ = SpawnChild(GetTestMainName()); |
| 138 | return DidStartClient(); |
| 139 | } |
| 140 | |
| 141 | #endif |
| 142 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 143 | bool IPCTestBase::WaitForClientShutdown() { |
rvargas | 07b589c | 2015-01-12 22:23:23 | [diff] [blame] | 144 | DCHECK(client_process_.IsValid()); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 145 | |
rvargas | 2f70a15 | 2015-02-24 00:28:11 | [diff] [blame] | 146 | int exit_code; |
amistry | 6de2ee4f | 2016-05-05 05:12:09 | [diff] [blame] | 147 | #if defined(OS_ANDROID) |
| 148 | bool rv = AndroidWaitForChildExitWithTimeout( |
| 149 | client_process_, base::TimeDelta::FromSeconds(5), &exit_code); |
| 150 | #else |
rvargas | 2f70a15 | 2015-02-24 00:28:11 | [diff] [blame] | 151 | bool rv = client_process_.WaitForExitWithTimeout( |
| 152 | base::TimeDelta::FromSeconds(5), &exit_code); |
amistry | 6de2ee4f | 2016-05-05 05:12:09 | [diff] [blame] | 153 | #endif // defined(OS_ANDROID) |
rvargas | 07b589c | 2015-01-12 22:23:23 | [diff] [blame] | 154 | client_process_.Close(); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 155 | return rv; |
| 156 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 157 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 158 | IPC::ChannelHandle IPCTestBase::GetTestChannelHandle() { |
| 159 | return GetChannelName(test_client_name_); |
| 160 | } |
| 161 | |
morrita | 70b9149 | 2015-03-04 01:52:27 | [diff] [blame] | 162 | scoped_refptr<base::SequencedTaskRunner> IPCTestBase::task_runner() { |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 163 | return message_loop_->task_runner(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 164 | } |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 165 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 166 | std::unique_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory( |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 167 | const IPC::ChannelHandle& handle, |
morrita | c4db547 | 2015-03-13 20:44:39 | [diff] [blame] | 168 | base::SequencedTaskRunner* runner) { |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 169 | return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER); |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 170 | } |