OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ipc/ipc_test_base.h" | 5 #include "ipc/ipc_test_base.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/command_line.h" | |
10 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
11 #include "base/process/kill.h" | |
12 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
13 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
14 #include "base/threading/thread.h" | |
15 #include "base/time/time.h" | |
16 #include "build/build_config.h" | 12 #include "build/build_config.h" |
17 #include "ipc/ipc_channel_mojo.h" | 13 #include "ipc/ipc_channel_mojo.h" |
18 #include "ipc/ipc_descriptors.h" | |
19 | 14 |
20 #if defined(OS_POSIX) | |
21 #include "base/posix/global_descriptors.h" | |
22 #endif | |
23 | |
24 // static | |
25 std::string IPCTestBase::GetChannelName(const std::string& test_client_name) { | |
26 DCHECK(!test_client_name.empty()); | |
27 return test_client_name + "__Channel"; | |
28 } | |
29 | |
30 IPCTestBase::IPCTestBase() { | |
31 } | |
32 | |
33 IPCTestBase::~IPCTestBase() { | |
34 } | |
35 | |
36 void IPCTestBase::TearDown() { | |
37 message_loop_.reset(); | |
38 MultiProcessTest::TearDown(); | |
39 } | |
40 | |
41 void IPCTestBase::Init(const std::string& test_client_name) { | |
42 InitWithCustomMessageLoop(test_client_name, | |
43 base::MakeUnique<base::MessageLoopForIO>()); | |
44 } | |
45 | |
46 void IPCTestBase::InitWithCustomMessageLoop( | |
47 const std::string& test_client_name, | |
48 std::unique_ptr<base::MessageLoop> message_loop) { | |
49 DCHECK(!test_client_name.empty()); | |
50 DCHECK(test_client_name_.empty()); | |
51 DCHECK(!message_loop_); | |
52 | |
53 test_client_name_ = test_client_name; | |
54 message_loop_ = std::move(message_loop); | |
55 } | |
56 | |
57 void IPCTestBase::CreateChannel(IPC::Listener* listener) { | |
58 CreateChannelFromChannelHandle(GetTestChannelHandle(), listener); | |
59 } | |
60 | |
61 bool IPCTestBase::ConnectChannel() { | |
62 CHECK(channel_.get()); | |
63 return channel_->Connect(); | |
64 } | |
65 | |
66 std::unique_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() { | |
67 return std::move(channel_); | |
68 } | |
69 | |
70 void IPCTestBase::SetChannel(std::unique_ptr<IPC::Channel> channel) { | |
71 channel_ = std::move(channel); | |
72 } | |
73 | |
74 | |
75 void IPCTestBase::DestroyChannel() { | |
76 DCHECK(channel_.get()); | |
77 channel_.reset(); | |
78 } | |
79 | |
80 void IPCTestBase::CreateChannelFromChannelHandle( | |
81 const IPC::ChannelHandle& channel_handle, | |
82 IPC::Listener* listener) { | |
83 CHECK(!channel_.get()); | |
84 CHECK(!channel_proxy_.get()); | |
85 channel_ = CreateChannelFactory( | |
86 channel_handle, task_runner().get())->BuildChannel(listener); | |
87 } | |
88 | |
89 void IPCTestBase::CreateChannelProxy( | |
90 IPC::Listener* listener, | |
91 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { | |
92 CHECK(!channel_.get()); | |
93 CHECK(!channel_proxy_.get()); | |
94 channel_proxy_ = IPC::ChannelProxy::Create( | |
95 CreateChannelFactory(GetTestChannelHandle(), ipc_task_runner.get()), | |
96 listener, ipc_task_runner); | |
97 } | |
98 | |
99 void IPCTestBase::DestroyChannelProxy() { | |
100 CHECK(channel_proxy_.get()); | |
101 channel_proxy_.reset(); | |
102 } | |
103 | |
104 std::string IPCTestBase::GetTestMainName() const { | |
105 return test_client_name_ + "TestClientMain"; | |
106 } | |
107 | |
108 bool IPCTestBase::DidStartClient() { | |
109 DCHECK(client_process_.IsValid()); | |
110 return client_process_.IsValid(); | |
111 } | |
112 | |
113 #if defined(OS_POSIX) | |
114 | |
115 bool IPCTestBase::StartClient() { | |
116 return StartClientWithFD(channel_ | |
117 ? channel_->GetClientFileDescriptor() | |
118 : channel_proxy_->GetClientFileDescriptor()); | |
119 } | |
120 | |
121 bool IPCTestBase::StartClientWithFD(int ipcfd) { | |
122 DCHECK(!client_process_.IsValid()); | |
123 | |
124 base::FileHandleMappingVector fds_to_map; | |
125 if (ipcfd > -1) | |
126 fds_to_map.push_back(std::pair<int, int>(ipcfd, | |
127 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); | |
128 base::LaunchOptions options; | |
129 options.fds_to_remap = &fds_to_map; | |
130 client_process_ = SpawnChildWithOptions(GetTestMainName(), options); | |
131 | |
132 return DidStartClient(); | |
133 } | |
134 | |
135 #elif defined(OS_WIN) | |
136 | |
137 bool IPCTestBase::StartClient() { | |
138 DCHECK(!client_process_.IsValid()); | |
139 client_process_ = SpawnChild(GetTestMainName()); | |
140 return DidStartClient(); | |
141 } | |
142 | |
143 #endif | |
144 | |
145 bool IPCTestBase::WaitForClientShutdown() { | |
146 DCHECK(client_process_.IsValid()); | |
147 | |
148 int exit_code; | |
149 #if defined(OS_ANDROID) | |
150 bool rv = AndroidWaitForChildExitWithTimeout( | |
151 client_process_, base::TimeDelta::FromSeconds(5), &exit_code); | |
152 #else | |
153 bool rv = client_process_.WaitForExitWithTimeout( | |
154 base::TimeDelta::FromSeconds(5), &exit_code); | |
155 #endif // defined(OS_ANDROID) | |
156 client_process_.Close(); | |
157 return rv; | |
158 } | |
159 | |
160 IPC::ChannelHandle IPCTestBase::GetTestChannelHandle() { | |
161 return GetChannelName(test_client_name_); | |
162 } | |
163 | |
164 scoped_refptr<base::SingleThreadTaskRunner> IPCTestBase::task_runner() { | |
165 return message_loop_->task_runner(); | |
166 } | |
167 | |
168 std::unique_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory( | |
169 const IPC::ChannelHandle& handle, | |
170 base::SingleThreadTaskRunner* runner) { | |
171 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER, runner); | |
172 } | |
173 IPCChannelMojoTestBase::IPCChannelMojoTestBase() = default; | 15 IPCChannelMojoTestBase::IPCChannelMojoTestBase() = default; |
174 IPCChannelMojoTestBase::~IPCChannelMojoTestBase() = default; | 16 IPCChannelMojoTestBase::~IPCChannelMojoTestBase() = default; |
175 | 17 |
176 void IPCChannelMojoTestBase::Init(const std::string& test_client_name) { | 18 void IPCChannelMojoTestBase::Init(const std::string& test_client_name) { |
| 19 InitWithCustomMessageLoop(test_client_name, |
| 20 base::MakeUnique<base::MessageLoop>()); |
| 21 } |
| 22 |
| 23 void IPCChannelMojoTestBase::InitWithCustomMessageLoop( |
| 24 const std::string& test_client_name, |
| 25 std::unique_ptr<base::MessageLoop> message_loop) { |
177 handle_ = helper_.StartChild(test_client_name); | 26 handle_ = helper_.StartChild(test_client_name); |
| 27 message_loop_ = std::move(message_loop); |
178 } | 28 } |
179 | 29 |
180 bool IPCChannelMojoTestBase::WaitForClientShutdown() { | 30 bool IPCChannelMojoTestBase::WaitForClientShutdown() { |
181 return helper_.WaitForChildTestShutdown(); | 31 return helper_.WaitForChildTestShutdown(); |
182 } | 32 } |
183 | 33 |
184 void IPCChannelMojoTestBase::TearDown() { | 34 void IPCChannelMojoTestBase::TearDown() { |
185 base::RunLoop().RunUntilIdle(); | 35 if (message_loop_) |
| 36 base::RunLoop().RunUntilIdle(); |
186 } | 37 } |
187 | 38 |
188 void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) { | 39 void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) { |
189 channel_ = | 40 channel_ = |
190 IPC::ChannelMojo::Create(TakeHandle(), IPC::Channel::MODE_SERVER, | 41 IPC::ChannelMojo::Create(TakeHandle(), IPC::Channel::MODE_SERVER, |
191 listener, base::ThreadTaskRunnerHandle::Get()); | 42 listener, base::ThreadTaskRunnerHandle::Get()); |
192 } | 43 } |
193 | 44 |
194 bool IPCChannelMojoTestBase::ConnectChannel() { | 45 bool IPCChannelMojoTestBase::ConnectChannel() { |
195 return channel_->Connect(); | 46 return channel_->Connect(); |
(...skipping 23 matching lines...) Expand all Loading... |
219 } | 70 } |
220 | 71 |
221 void IpcChannelMojoTestClient::Close() { | 72 void IpcChannelMojoTestClient::Close() { |
222 channel_->Close(); | 73 channel_->Close(); |
223 | 74 |
224 base::RunLoop run_loop; | 75 base::RunLoop run_loop; |
225 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 76 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
226 run_loop.QuitClosure()); | 77 run_loop.QuitClosure()); |
227 run_loop.Run(); | 78 run_loop.Run(); |
228 } | 79 } |
OLD | NEW |