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