blob: e57aac468e51b1c8584b1ea235e94619c90af9cf [file] [log] [blame]
[email protected]64860882014-08-04 23:44:171// Copyright 2014 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
amistryd4aa70d2016-06-23 07:52:375#include "ipc/ipc_channel_mojo.h"
[email protected]64860882014-08-04 23:44:176
avi246998d82015-12-22 02:39:047#include <stddef.h>
tfarina10a5c062015-09-04 18:47:578#include <stdint.h>
rockot7c6bf952016-07-14 00:34:119
danakj03de39b22016-04-23 04:21:0910#include <memory>
dchenge48600452015-12-28 02:24:5011#include <utility>
tfarina10a5c062015-09-04 18:47:5712
[email protected]64860882014-08-04 23:44:1713#include "base/base_paths.h"
rockot8d890f62016-07-14 16:37:1414#include "base/bind.h"
sammc5c8a6c62017-02-04 01:33:3815#include "base/callback_helpers.h"
Brett Wilsona62d9c02017-09-20 20:53:2016#include "base/containers/queue.h"
[email protected]64860882014-08-04 23:44:1717#include "base/files/file.h"
amistry20e2b1d62016-06-23 06:12:3518#include "base/files/scoped_temp_dir.h"
skyostile687bdff2015-05-12 11:29:2119#include "base/location.h"
rockot8d890f62016-07-14 16:37:1420#include "base/macros.h"
Alexandr Ilind497eee2018-04-19 22:50:5421#include "base/memory/platform_shared_memory_region.h"
Alex Ilinf2c7fa232019-11-20 14:09:5822#include "base/memory/read_only_shared_memory_region.h"
Alexandr Ilind497eee2018-04-19 22:50:5423#include "base/memory/shared_memory_mapping.h"
Alex Ilinf2c7fa232019-11-20 14:09:5824#include "base/memory/unsafe_shared_memory_region.h"
25#include "base/memory/writable_shared_memory_region.h"
Carlos Caballerodd8bf7b042019-07-30 14:14:1526#include "base/message_loop/message_pump_type.h"
[email protected]64860882014-08-04 23:44:1727#include "base/path_service.h"
28#include "base/pickle.h"
rockotcbca72f2015-03-03 16:31:0429#include "base/run_loop.h"
skyostile687bdff2015-05-12 11:29:2130#include "base/single_thread_task_runner.h"
rockot9abe09b2016-08-02 20:57:3431#include "base/synchronization/waitable_event.h"
Guido Urdanetaef4e91942020-11-09 15:06:2432#include "base/test/bind.h"
Gabriel Charettec7108742019-08-23 03:31:4033#include "base/test/task_environment.h"
sammc57ed9f982016-03-10 06:28:3534#include "base/test/test_io_thread.h"
Alexandr Ilind497eee2018-04-19 22:50:5435#include "base/test/test_shared_memory_util.h"
morrita0bd20bd2015-02-25 20:11:2736#include "base/test/test_timeouts.h"
[email protected]64860882014-08-04 23:44:1737#include "base/threading/thread.h"
gabf08ccc02016-05-11 18:51:1138#include "base/threading/thread_task_runner_handle.h"
avi246998d82015-12-22 02:39:0439#include "build/build_config.h"
[email protected]64860882014-08-04 23:44:1740#include "ipc/ipc_message.h"
Wez831ae412017-08-30 00:29:3641#include "ipc/ipc_message_utils.h"
amistryd4aa70d2016-06-23 07:52:3742#include "ipc/ipc_mojo_handle_attachment.h"
43#include "ipc/ipc_mojo_message_helper.h"
44#include "ipc/ipc_mojo_param_traits.h"
rockot9abe09b2016-08-02 20:57:3445#include "ipc/ipc_sync_channel.h"
46#include "ipc/ipc_sync_message.h"
rockot7c6bf952016-07-14 00:34:1147#include "ipc/ipc_test.mojom.h"
[email protected]64860882014-08-04 23:44:1748#include "ipc/ipc_test_base.h"
49#include "ipc/ipc_test_channel_listener.h"
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:1550#include "mojo/public/cpp/bindings/associated_receiver.h"
51#include "mojo/public/cpp/bindings/associated_remote.h"
Ken Rockot138153b2018-07-13 23:31:5752#include "mojo/public/cpp/bindings/lib/validation_errors.h"
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:1553#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
Ken Rockot942af102020-06-04 05:48:4654#include "mojo/public/cpp/system/functions.h"
rockota9d566a2017-03-17 19:36:1555#include "mojo/public/cpp/system/wait.h"
sammc57ed9f982016-03-10 06:28:3556#include "testing/gtest/include/gtest/gtest.h"
Anton Bikineev1f42a452021-05-15 18:02:5057#include "third_party/abseil-cpp/absl/types/optional.h"
[email protected]64860882014-08-04 23:44:1758
Fabrice de Gans-Riberi894661c2018-05-24 18:43:2259#if defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]64860882014-08-04 23:44:1760#include "base/file_descriptor_posix.h"
morrita1aa788c2015-01-31 05:45:4261#include "ipc/ipc_platform_file_attachment_posix.h"
[email protected]64860882014-08-04 23:44:1762#endif
63
64namespace {
65
rockot7c6bf952016-07-14 00:34:1166void SendString(IPC::Sender* sender, const std::string& str) {
67 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
68 message->WriteString(str);
69 ASSERT_TRUE(sender->Send(message));
70}
71
rockot9abe09b2016-08-02 20:57:3472void SendValue(IPC::Sender* sender, int32_t value) {
73 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
74 message->WriteInt(value);
75 ASSERT_TRUE(sender->Send(message));
76}
77
[email protected]64860882014-08-04 23:44:1778class ListenerThatExpectsOK : public IPC::Listener {
79 public:
Wezfdb98482018-07-12 01:21:3980 explicit ListenerThatExpectsOK(base::OnceClosure quit_closure)
81 : received_ok_(false), quit_closure_(std::move(quit_closure)) {}
[email protected]64860882014-08-04 23:44:1782
Chris Watkins2d879af2017-11-30 02:11:5983 ~ListenerThatExpectsOK() override = default;
[email protected]64860882014-08-04 23:44:1784
dchengfe61fca2014-10-22 02:29:5285 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:2586 base::PickleIterator iter(message);
[email protected]64860882014-08-04 23:44:1787 std::string should_be_ok;
88 EXPECT_TRUE(iter.ReadString(&should_be_ok));
89 EXPECT_EQ(should_be_ok, "OK");
[email protected]e5c27752014-08-08 21:45:1390 received_ok_ = true;
Wezfdb98482018-07-12 01:21:3991 std::move(quit_closure_).Run();
[email protected]64860882014-08-04 23:44:1792 return true;
93 }
94
dchengfe61fca2014-10-22 02:29:5295 void OnChannelError() override {
[email protected]e5c27752014-08-08 21:45:1396 // The connection should be healthy while the listener is waiting
97 // message. An error can occur after that because the peer
98 // process dies.
Wezd95d48042018-02-26 20:50:4799 CHECK(received_ok_);
[email protected]64860882014-08-04 23:44:17100 }
101
rockot7c6bf952016-07-14 00:34:11102 static void SendOK(IPC::Sender* sender) { SendString(sender, "OK"); }
[email protected]e5c27752014-08-08 21:45:13103
104 private:
105 bool received_ok_;
Wezfdb98482018-07-12 01:21:39106 base::OnceClosure quit_closure_;
Wez831ae412017-08-30 00:29:36107};
108
109class TestListenerBase : public IPC::Listener {
110 public:
Wezfdb98482018-07-12 01:21:39111 explicit TestListenerBase(base::OnceClosure quit_closure)
112 : quit_closure_(std::move(quit_closure)) {}
Wez831ae412017-08-30 00:29:36113
Chris Watkins2d879af2017-11-30 02:11:59114 ~TestListenerBase() override = default;
Wezfdb98482018-07-12 01:21:39115 void OnChannelError() override { RunQuitClosure(); }
Wez831ae412017-08-30 00:29:36116
117 void set_sender(IPC::Sender* sender) { sender_ = sender; }
118 IPC::Sender* sender() const { return sender_; }
Ken Rockot138153b2018-07-13 23:31:57119 void RunQuitClosure() {
120 if (quit_closure_)
121 std::move(quit_closure_).Run();
122 }
Wez831ae412017-08-30 00:29:36123
124 private:
125 IPC::Sender* sender_ = nullptr;
Wezfdb98482018-07-12 01:21:39126 base::OnceClosure quit_closure_;
[email protected]64860882014-08-04 23:44:17127};
128
sammc4bcc4ed62016-10-27 10:13:59129using IPCChannelMojoTest = IPCChannelMojoTestBase;
rockotcbca72f2015-03-03 16:31:04130
[email protected]64860882014-08-04 23:44:17131class TestChannelListenerWithExtraExpectations
132 : public IPC::TestChannelListener {
133 public:
sammc57ed9f982016-03-10 06:28:35134 TestChannelListenerWithExtraExpectations() : is_connected_called_(false) {}
[email protected]64860882014-08-04 23:44:17135
tfarina10a5c062015-09-04 18:47:57136 void OnChannelConnected(int32_t peer_pid) override {
[email protected]64860882014-08-04 23:44:17137 IPC::TestChannelListener::OnChannelConnected(peer_pid);
138 EXPECT_TRUE(base::kNullProcessId != peer_pid);
139 is_connected_called_ = true;
140 }
141
142 bool is_connected_called() const { return is_connected_called_; }
143
144 private:
145 bool is_connected_called_;
146};
147
amistry0027a0952016-05-03 00:52:47148TEST_F(IPCChannelMojoTest, ConnectedFromClient) {
sammc4bcc4ed62016-10-27 10:13:59149 Init("IPCChannelMojoTestClient");
[email protected]64860882014-08-04 23:44:17150
151 // Set up IPC channel and start client.
152 TestChannelListenerWithExtraExpectations listener;
morrita373af03b2014-09-09 19:35:24153 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17154 listener.Init(sender());
155 ASSERT_TRUE(ConnectChannel());
[email protected]64860882014-08-04 23:44:17156
sammc57ed9f982016-03-10 06:28:35157 IPC::TestChannelListener::SendOneMessage(sender(), "hello from parent");
[email protected]64860882014-08-04 23:44:17158
fdoray8e32586852016-06-22 19:56:16159 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17160
sammc57ed9f982016-03-10 06:28:35161 channel()->Close();
[email protected]64860882014-08-04 23:44:17162
163 EXPECT_TRUE(WaitForClientShutdown());
164 EXPECT_TRUE(listener.is_connected_called());
165 EXPECT_TRUE(listener.HasSentAll());
166
167 DestroyChannel();
168}
169
170// A long running process that connects to us
sammc4bcc4ed62016-10-27 10:13:59171DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestClient) {
[email protected]64860882014-08-04 23:44:17172 TestChannelListenerWithExtraExpectations listener;
sammc57ed9f982016-03-10 06:28:35173 Connect(&listener);
174 listener.Init(channel());
[email protected]64860882014-08-04 23:44:17175
sammc57ed9f982016-03-10 06:28:35176 IPC::TestChannelListener::SendOneMessage(channel(), "hello from child");
fdoray8e32586852016-06-22 19:56:16177 base::RunLoop().Run();
[email protected]64860882014-08-04 23:44:17178 EXPECT_TRUE(listener.is_connected_called());
179 EXPECT_TRUE(listener.HasSentAll());
180
sammc57ed9f982016-03-10 06:28:35181 Close();
[email protected]64860882014-08-04 23:44:17182}
183
Wez831ae412017-08-30 00:29:36184class ListenerExpectingErrors : public TestListenerBase {
morrita0a24cfc92014-09-16 03:20:48185 public:
Wezfdb98482018-07-12 01:21:39186 ListenerExpectingErrors(base::OnceClosure quit_closure)
187 : TestListenerBase(std::move(quit_closure)), has_error_(false) {}
morrita0a24cfc92014-09-16 03:20:48188
dchengfe61fca2014-10-22 02:29:52189 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita0a24cfc92014-09-16 03:20:48190
dchengfe61fca2014-10-22 02:29:52191 void OnChannelError() override {
morrita0a24cfc92014-09-16 03:20:48192 has_error_ = true;
Wez831ae412017-08-30 00:29:36193 TestListenerBase::OnChannelError();
morrita0a24cfc92014-09-16 03:20:48194 }
195
196 bool has_error() const { return has_error_; }
197
198 private:
199 bool has_error_;
200};
201
morrita0a24cfc92014-09-16 03:20:48202class ListenerThatQuits : public IPC::Listener {
203 public:
Wezfdb98482018-07-12 01:21:39204 explicit ListenerThatQuits(base::OnceClosure quit_closure)
205 : quit_closure_(std::move(quit_closure)) {}
morrita0a24cfc92014-09-16 03:20:48206
sammc57ed9f982016-03-10 06:28:35207 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita0a24cfc92014-09-16 03:20:48208
Wezfdb98482018-07-12 01:21:39209 void OnChannelConnected(int32_t peer_pid) override {
210 std::move(quit_closure_).Run();
211 }
Wez831ae412017-08-30 00:29:36212
213 private:
Wezfdb98482018-07-12 01:21:39214 base::OnceClosure quit_closure_;
morrita0a24cfc92014-09-16 03:20:48215};
216
217// A long running process that connects to us.
sammc4bcc4ed62016-10-27 10:13:59218DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoErraticTestClient) {
Wez831ae412017-08-30 00:29:36219 base::RunLoop run_loop;
220 ListenerThatQuits listener(run_loop.QuitClosure());
sammc57ed9f982016-03-10 06:28:35221 Connect(&listener);
morrita0a24cfc92014-09-16 03:20:48222
Wez831ae412017-08-30 00:29:36223 run_loop.Run();
morrita0a24cfc92014-09-16 03:20:48224
sammc57ed9f982016-03-10 06:28:35225 Close();
morrita0a24cfc92014-09-16 03:20:48226}
227
rockot8c23d462016-07-25 17:33:04228TEST_F(IPCChannelMojoTest, SendFailWithPendingMessages) {
sammc4bcc4ed62016-10-27 10:13:59229 Init("IPCChannelMojoErraticTestClient");
morrita0a24cfc92014-09-16 03:20:48230
231 // Set up IPC channel and start client.
Wez831ae412017-08-30 00:29:36232 base::RunLoop run_loop;
233 ListenerExpectingErrors listener(run_loop.QuitClosure());
morrita0a24cfc92014-09-16 03:20:48234 CreateChannel(&listener);
235 ASSERT_TRUE(ConnectChannel());
236
jamesra03ae492014-10-03 04:26:48237 // This matches a value in mojo/edk/system/constants.h
morritabe6c4cc2014-09-24 23:38:44238 const int kMaxMessageNumBytes = 4 * 1024 * 1024;
239 std::string overly_large_data(kMaxMessageNumBytes, '*');
morrita0a24cfc92014-09-16 03:20:48240 // This messages are queued as pending.
morritabe6c4cc2014-09-24 23:38:44241 for (size_t i = 0; i < 10; ++i) {
sammc57ed9f982016-03-10 06:28:35242 IPC::TestChannelListener::SendOneMessage(sender(),
243 overly_large_data.c_str());
morrita0a24cfc92014-09-16 03:20:48244 }
245
Wez831ae412017-08-30 00:29:36246 run_loop.Run();
morrita0a24cfc92014-09-16 03:20:48247
sammc57ed9f982016-03-10 06:28:35248 channel()->Close();
morrita0a24cfc92014-09-16 03:20:48249
250 EXPECT_TRUE(WaitForClientShutdown());
251 EXPECT_TRUE(listener.has_error());
252
253 DestroyChannel();
254}
255
Ken Rockot138153b2018-07-13 23:31:57256class ListenerThatBindsATestStructPasser : public IPC::Listener,
257 public IPC::mojom::TestStructPasser {
258 public:
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:54259 ListenerThatBindsATestStructPasser() = default;
260 ~ListenerThatBindsATestStructPasser() override = default;
Ken Rockot138153b2018-07-13 23:31:57261
262 bool OnMessageReceived(const IPC::Message& message) override { return true; }
263
264 void OnChannelConnected(int32_t peer_pid) override {}
265
266 void OnChannelError() override { NOTREACHED(); }
267
268 void OnAssociatedInterfaceRequest(
269 const std::string& interface_name,
270 mojo::ScopedInterfaceEndpointHandle handle) override {
271 CHECK_EQ(interface_name, IPC::mojom::TestStructPasser::Name_);
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:54272 receiver_.Bind(
273 mojo::PendingAssociatedReceiver<IPC::mojom::TestStructPasser>(
274 std::move(handle)));
Ken Rockot138153b2018-07-13 23:31:57275 }
276
277 private:
278 // IPC::mojom::TestStructPasser:
279 void Pass(IPC::mojom::TestStructPtr) override { NOTREACHED(); }
280
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:54281 mojo::AssociatedReceiver<IPC::mojom::TestStructPasser> receiver_{this};
Ken Rockot138153b2018-07-13 23:31:57282};
283
284class ListenerThatExpectsNoError : public IPC::Listener {
285 public:
286 ListenerThatExpectsNoError(base::OnceClosure connect_closure,
287 base::OnceClosure quit_closure)
288 : connect_closure_(std::move(connect_closure)),
289 quit_closure_(std::move(quit_closure)) {}
290
291 bool OnMessageReceived(const IPC::Message& message) override {
292 base::PickleIterator iter(message);
293 std::string should_be_ok;
294 EXPECT_TRUE(iter.ReadString(&should_be_ok));
295 EXPECT_EQ(should_be_ok, "OK");
296 std::move(quit_closure_).Run();
297 return true;
298 }
299
300 void OnChannelConnected(int32_t peer_pid) override {
301 std::move(connect_closure_).Run();
302 }
303
304 void OnChannelError() override { NOTREACHED(); }
305
306 private:
307 base::OnceClosure connect_closure_;
308 base::OnceClosure quit_closure_;
309};
310
311DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
312 IPCChannelMojoNoImplicitChanelClosureClient) {
313 base::RunLoop wait_to_connect_loop;
314 base::RunLoop wait_to_quit_loop;
315 ListenerThatExpectsNoError listener(wait_to_connect_loop.QuitClosure(),
316 wait_to_quit_loop.QuitClosure());
317 Connect(&listener);
318 wait_to_connect_loop.Run();
319
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:54320 mojo::AssociatedRemote<IPC::mojom::TestStructPasser> passer;
Ken Rockot138153b2018-07-13 23:31:57321 channel()->GetAssociatedInterfaceSupport()->GetRemoteAssociatedInterface(
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:54322 passer.BindNewEndpointAndPassReceiver());
Ken Rockot138153b2018-07-13 23:31:57323
324 // This avoids hitting DCHECKs in the serialization code meant to stop us from
325 // making such "mistakes" as the one we're about to make below.
326 mojo::internal::SerializationWarningObserverForTesting suppress_those_dchecks;
327
328 // Send an invalid message. The TestStruct argument is not allowed to be null.
329 // This will elicit a validation error in the parent process, but should not
330 // actually disconnect the channel.
331 passer->Pass(nullptr);
332
333 // Wait until the parent says it's OK to quit, so it has time to verify its
334 // expected behavior.
335 wait_to_quit_loop.Run();
336
337 Close();
338}
339
340TEST_F(IPCChannelMojoTest, NoImplicitChannelClosure) {
341 // Verifies that OnChannelError is not invoked due to conditions other than
342 // peer closure (e.g. a malformed inbound message). Instead we should always
343 // be able to handle validation errors via Mojo bad message reporting.
344
345 // NOTE: We can't create a RunLoop before Init() is called, but we have to set
346 // the default ProcessErrorCallback (which we want to reference the RunLoop)
Anton Bikineev1f42a452021-05-15 18:02:50347 // before Init() launches a child process. Hence the absl::optional here.
348 absl::optional<base::RunLoop> wait_for_error_loop;
Ken Rockot138153b2018-07-13 23:31:57349 bool process_error_received = false;
Ken Rockot942af102020-06-04 05:48:46350 mojo::SetDefaultProcessErrorHandler(
Ken Rockot138153b2018-07-13 23:31:57351 base::BindLambdaForTesting([&](const std::string&) {
352 process_error_received = true;
353 wait_for_error_loop->Quit();
354 }));
355
356 Init("IPCChannelMojoNoImplicitChanelClosureClient");
357
358 wait_for_error_loop.emplace();
359 ListenerThatBindsATestStructPasser listener;
360 CreateChannel(&listener);
361 ASSERT_TRUE(ConnectChannel());
362
363 wait_for_error_loop->Run();
364 EXPECT_TRUE(process_error_received);
Ken Rockot942af102020-06-04 05:48:46365 mojo::SetDefaultProcessErrorHandler(base::NullCallback());
Ken Rockot138153b2018-07-13 23:31:57366
367 // Tell the child it can quit and wait for it to shut down.
368 ListenerThatExpectsOK::SendOK(channel());
369 EXPECT_TRUE(WaitForClientShutdown());
370 DestroyChannel();
371}
372
morrita81b17e02015-02-06 00:58:30373struct TestingMessagePipe {
374 TestingMessagePipe() {
375 EXPECT_EQ(MOJO_RESULT_OK, mojo::CreateMessagePipe(nullptr, &self, &peer));
376 }
377
378 mojo::ScopedMessagePipeHandle self;
379 mojo::ScopedMessagePipeHandle peer;
380};
381
382class HandleSendingHelper {
383 public:
384 static std::string GetSendingFileContent() { return "Hello"; }
385
386 static void WritePipe(IPC::Message* message, TestingMessagePipe* pipe) {
387 std::string content = HandleSendingHelper::GetSendingFileContent();
388 EXPECT_EQ(MOJO_RESULT_OK,
389 mojo::WriteMessageRaw(pipe->self.get(), &content[0],
390 static_cast<uint32_t>(content.size()),
391 nullptr, 0, 0));
dchenge48600452015-12-28 02:24:50392 EXPECT_TRUE(IPC::MojoMessageHelper::WriteMessagePipeTo(
393 message, std::move(pipe->peer)));
morrita81b17e02015-02-06 00:58:30394 }
395
396 static void WritePipeThenSend(IPC::Sender* sender, TestingMessagePipe* pipe) {
397 IPC::Message* message =
398 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
399 WritePipe(message, pipe);
400 ASSERT_TRUE(sender->Send(message));
401 }
402
403 static void ReadReceivedPipe(const IPC::Message& message,
brettwbd4d7112015-06-03 04:29:25404 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30405 mojo::ScopedMessagePipeHandle pipe;
406 EXPECT_TRUE(
407 IPC::MojoMessageHelper::ReadMessagePipeFrom(&message, iter, &pipe));
Ken Rockotfada7d282017-06-12 21:25:13408 std::vector<uint8_t> content;
morrita81b17e02015-02-06 00:58:30409
sammc57ed9f982016-03-10 06:28:35410 ASSERT_EQ(MOJO_RESULT_OK,
rockota9d566a2017-03-17 19:36:15411 mojo::Wait(pipe.get(), MOJO_HANDLE_SIGNAL_READABLE));
morrita81b17e02015-02-06 00:58:30412 EXPECT_EQ(MOJO_RESULT_OK,
Ken Rockotfada7d282017-06-12 21:25:13413 mojo::ReadMessageRaw(pipe.get(), &content, nullptr, 0));
414 EXPECT_EQ(std::string(content.begin(), content.end()),
415 GetSendingFileContent());
morrita81b17e02015-02-06 00:58:30416 }
417
Fabrice de Gans-Riberi894661c2018-05-24 18:43:22418#if defined(OS_POSIX) || defined(OS_FUCHSIA)
amistry20e2b1d62016-06-23 06:12:35419 static base::FilePath GetSendingFilePath(const base::FilePath& dir_path) {
420 return dir_path.Append("ListenerThatExpectsFile.txt");
morrita81b17e02015-02-06 00:58:30421 }
422
423 static void WriteFile(IPC::Message* message, base::File& file) {
424 std::string content = GetSendingFileContent();
425 file.WriteAtCurrentPos(content.data(), content.size());
426 file.Flush();
427 message->WriteAttachment(new IPC::internal::PlatformFileAttachment(
428 base::ScopedFD(file.TakePlatformFile())));
429 }
430
431 static void WriteFileThenSend(IPC::Sender* sender, base::File& file) {
432 IPC::Message* message =
433 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
434 WriteFile(message, file);
435 ASSERT_TRUE(sender->Send(message));
436 }
437
438 static void WriteFileAndPipeThenSend(IPC::Sender* sender,
439 base::File& file,
440 TestingMessagePipe* pipe) {
441 IPC::Message* message =
442 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
443 WriteFile(message, file);
444 WritePipe(message, pipe);
445 ASSERT_TRUE(sender->Send(message));
446 }
447
448 static void ReadReceivedFile(const IPC::Message& message,
brettwbd4d7112015-06-03 04:29:25449 base::PickleIterator* iter) {
rockot502c94f2016-02-03 20:20:16450 scoped_refptr<base::Pickle::Attachment> attachment;
morrita81b17e02015-02-06 00:58:30451 EXPECT_TRUE(message.ReadAttachment(iter, &attachment));
sammc6ed3efb2016-11-23 03:17:35452 EXPECT_EQ(
453 IPC::MessageAttachment::Type::PLATFORM_FILE,
454 static_cast<IPC::MessageAttachment*>(attachment.get())->GetType());
455 base::File file(
456 static_cast<IPC::internal::PlatformFileAttachment*>(attachment.get())
457 ->TakePlatformFile());
morrita81b17e02015-02-06 00:58:30458 std::string content(GetSendingFileContent().size(), ' ');
459 file.Read(0, &content[0], content.size());
460 EXPECT_EQ(content, GetSendingFileContent());
461 }
462#endif
463};
464
Wez831ae412017-08-30 00:29:36465class ListenerThatExpectsMessagePipe : public TestListenerBase {
morrita81b17e02015-02-06 00:58:30466 public:
Wezfdb98482018-07-12 01:21:39467 ListenerThatExpectsMessagePipe(base::OnceClosure quit_closure)
468 : TestListenerBase(std::move(quit_closure)) {}
morrita81b17e02015-02-06 00:58:30469
Chris Watkins2d879af2017-11-30 02:11:59470 ~ListenerThatExpectsMessagePipe() override = default;
morrita81b17e02015-02-06 00:58:30471
472 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25473 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30474 HandleSendingHelper::ReadReceivedPipe(message, &iter);
Wez831ae412017-08-30 00:29:36475 ListenerThatExpectsOK::SendOK(sender());
morrita81b17e02015-02-06 00:58:30476 return true;
477 }
morrita81b17e02015-02-06 00:58:30478};
479
amistry0027a0952016-05-03 00:52:47480TEST_F(IPCChannelMojoTest, SendMessagePipe) {
sammc4bcc4ed62016-10-27 10:13:59481 Init("IPCChannelMojoTestSendMessagePipeClient");
morrita81b17e02015-02-06 00:58:30482
Wez831ae412017-08-30 00:29:36483 base::RunLoop run_loop;
484 ListenerThatExpectsOK listener(run_loop.QuitClosure());
morrita81b17e02015-02-06 00:58:30485 CreateChannel(&listener);
486 ASSERT_TRUE(ConnectChannel());
morrita81b17e02015-02-06 00:58:30487
488 TestingMessagePipe pipe;
489 HandleSendingHelper::WritePipeThenSend(channel(), &pipe);
490
Wez831ae412017-08-30 00:29:36491 run_loop.Run();
sammc57ed9f982016-03-10 06:28:35492 channel()->Close();
morrita81b17e02015-02-06 00:58:30493
494 EXPECT_TRUE(WaitForClientShutdown());
495 DestroyChannel();
496}
497
sammc4bcc4ed62016-10-27 10:13:59498DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendMessagePipeClient) {
Wez831ae412017-08-30 00:29:36499 base::RunLoop run_loop;
500 ListenerThatExpectsMessagePipe listener(run_loop.QuitClosure());
sammc57ed9f982016-03-10 06:28:35501 Connect(&listener);
502 listener.set_sender(channel());
morrita81b17e02015-02-06 00:58:30503
Wez831ae412017-08-30 00:29:36504 run_loop.Run();
morrita81b17e02015-02-06 00:58:30505
sammc57ed9f982016-03-10 06:28:35506 Close();
morrita81b17e02015-02-06 00:58:30507}
508
morrita438a2ee2015-04-03 05:28:21509void ReadOK(mojo::MessagePipeHandle pipe) {
Ken Rockotfada7d282017-06-12 21:25:13510 std::vector<uint8_t> should_be_ok;
rockota9d566a2017-03-17 19:36:15511 CHECK_EQ(MOJO_RESULT_OK, mojo::Wait(pipe, MOJO_HANDLE_SIGNAL_READABLE));
morrita438a2ee2015-04-03 05:28:21512 CHECK_EQ(MOJO_RESULT_OK,
Ken Rockotfada7d282017-06-12 21:25:13513 mojo::ReadMessageRaw(pipe, &should_be_ok, nullptr, 0));
514 EXPECT_EQ("OK", std::string(should_be_ok.begin(), should_be_ok.end()));
morrita438a2ee2015-04-03 05:28:21515}
516
517void WriteOK(mojo::MessagePipeHandle pipe) {
518 std::string ok("OK");
519 CHECK_EQ(MOJO_RESULT_OK,
520 mojo::WriteMessageRaw(pipe, &ok[0], static_cast<uint32_t>(ok.size()),
521 nullptr, 0, 0));
522}
523
Wez831ae412017-08-30 00:29:36524class ListenerThatExpectsMessagePipeUsingParamTrait : public TestListenerBase {
morrita438a2ee2015-04-03 05:28:21525 public:
Wez831ae412017-08-30 00:29:36526 explicit ListenerThatExpectsMessagePipeUsingParamTrait(
Wezfdb98482018-07-12 01:21:39527 base::OnceClosure quit_closure,
Wez831ae412017-08-30 00:29:36528 bool receiving_valid)
Wezfdb98482018-07-12 01:21:39529 : TestListenerBase(std::move(quit_closure)),
530 receiving_valid_(receiving_valid) {}
morrita438a2ee2015-04-03 05:28:21531
Chris Watkins2d879af2017-11-30 02:11:59532 ~ListenerThatExpectsMessagePipeUsingParamTrait() override = default;
morrita438a2ee2015-04-03 05:28:21533
534 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25535 base::PickleIterator iter(message);
morrita438a2ee2015-04-03 05:28:21536 mojo::MessagePipeHandle handle;
537 EXPECT_TRUE(IPC::ParamTraits<mojo::MessagePipeHandle>::Read(&message, &iter,
538 &handle));
539 EXPECT_EQ(handle.is_valid(), receiving_valid_);
540 if (receiving_valid_) {
541 ReadOK(handle);
542 MojoClose(handle.value());
543 }
544
Wez831ae412017-08-30 00:29:36545 ListenerThatExpectsOK::SendOK(sender());
morrita438a2ee2015-04-03 05:28:21546 return true;
547 }
548
morrita438a2ee2015-04-03 05:28:21549 private:
morrita438a2ee2015-04-03 05:28:21550 bool receiving_valid_;
551};
552
sammc4bcc4ed62016-10-27 10:13:59553class ParamTraitMessagePipeClient : public IpcChannelMojoTestClient {
sammc57ed9f982016-03-10 06:28:35554 public:
555 void RunTest(bool receiving_valid_handle) {
Wez831ae412017-08-30 00:29:36556 base::RunLoop run_loop;
sammc57ed9f982016-03-10 06:28:35557 ListenerThatExpectsMessagePipeUsingParamTrait listener(
Wez831ae412017-08-30 00:29:36558 run_loop.QuitClosure(), receiving_valid_handle);
sammc57ed9f982016-03-10 06:28:35559 Connect(&listener);
560 listener.set_sender(channel());
morrita438a2ee2015-04-03 05:28:21561
Wez831ae412017-08-30 00:29:36562 run_loop.Run();
morrita438a2ee2015-04-03 05:28:21563
sammc57ed9f982016-03-10 06:28:35564 Close();
565 }
566};
morrita438a2ee2015-04-03 05:28:21567
amistry0027a0952016-05-03 00:52:47568TEST_F(IPCChannelMojoTest, ParamTraitValidMessagePipe) {
sammc4bcc4ed62016-10-27 10:13:59569 Init("ParamTraitValidMessagePipeClient");
morrita438a2ee2015-04-03 05:28:21570
Wez831ae412017-08-30 00:29:36571 base::RunLoop run_loop;
572 ListenerThatExpectsOK listener(run_loop.QuitClosure());
morrita438a2ee2015-04-03 05:28:21573 CreateChannel(&listener);
574 ASSERT_TRUE(ConnectChannel());
morrita438a2ee2015-04-03 05:28:21575
576 TestingMessagePipe pipe;
577
danakj03de39b22016-04-23 04:21:09578 std::unique_ptr<IPC::Message> message(new IPC::Message());
morrita438a2ee2015-04-03 05:28:21579 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
580 pipe.peer.release());
581 WriteOK(pipe.self.get());
582
sammc57ed9f982016-03-10 06:28:35583 channel()->Send(message.release());
Wez831ae412017-08-30 00:29:36584 run_loop.Run();
sammc57ed9f982016-03-10 06:28:35585 channel()->Close();
morrita438a2ee2015-04-03 05:28:21586
587 EXPECT_TRUE(WaitForClientShutdown());
588 DestroyChannel();
589}
590
sammc4bcc4ed62016-10-27 10:13:59591DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(
592 ParamTraitValidMessagePipeClient,
593 ParamTraitMessagePipeClient) {
sammc57ed9f982016-03-10 06:28:35594 RunTest(true);
morrita438a2ee2015-04-03 05:28:21595}
596
amistry0027a0952016-05-03 00:52:47597TEST_F(IPCChannelMojoTest, ParamTraitInvalidMessagePipe) {
sammc4bcc4ed62016-10-27 10:13:59598 Init("ParamTraitInvalidMessagePipeClient");
morrita438a2ee2015-04-03 05:28:21599
Wez831ae412017-08-30 00:29:36600 base::RunLoop run_loop;
601 ListenerThatExpectsOK listener(run_loop.QuitClosure());
morrita438a2ee2015-04-03 05:28:21602 CreateChannel(&listener);
603 ASSERT_TRUE(ConnectChannel());
morrita438a2ee2015-04-03 05:28:21604
605 mojo::MessagePipeHandle invalid_handle;
danakj03de39b22016-04-23 04:21:09606 std::unique_ptr<IPC::Message> message(new IPC::Message());
morrita438a2ee2015-04-03 05:28:21607 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
608 invalid_handle);
609
sammc57ed9f982016-03-10 06:28:35610 channel()->Send(message.release());
Wez831ae412017-08-30 00:29:36611 run_loop.Run();
sammc57ed9f982016-03-10 06:28:35612 channel()->Close();
morrita438a2ee2015-04-03 05:28:21613
614 EXPECT_TRUE(WaitForClientShutdown());
615 DestroyChannel();
616}
617
sammc4bcc4ed62016-10-27 10:13:59618DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(
619 ParamTraitInvalidMessagePipeClient,
620 ParamTraitMessagePipeClient) {
sammc57ed9f982016-03-10 06:28:35621 RunTest(false);
morrita438a2ee2015-04-03 05:28:21622}
623
amistry0027a0952016-05-03 00:52:47624TEST_F(IPCChannelMojoTest, SendFailAfterClose) {
sammc4bcc4ed62016-10-27 10:13:59625 Init("IPCChannelMojoTestSendOkClient");
morrita17137e62015-06-23 22:29:36626
Wez831ae412017-08-30 00:29:36627 base::RunLoop run_loop;
628 ListenerThatExpectsOK listener(run_loop.QuitClosure());
morrita17137e62015-06-23 22:29:36629 CreateChannel(&listener);
630 ASSERT_TRUE(ConnectChannel());
morrita17137e62015-06-23 22:29:36631
Wez831ae412017-08-30 00:29:36632 run_loop.Run();
sammc57ed9f982016-03-10 06:28:35633 channel()->Close();
634 ASSERT_FALSE(channel()->Send(new IPC::Message()));
morrita17137e62015-06-23 22:29:36635
636 EXPECT_TRUE(WaitForClientShutdown());
637 DestroyChannel();
638}
639
Wez831ae412017-08-30 00:29:36640class ListenerSendingOneOk : public TestListenerBase {
morrita17137e62015-06-23 22:29:36641 public:
Wezfdb98482018-07-12 01:21:39642 ListenerSendingOneOk(base::OnceClosure quit_closure)
643 : TestListenerBase(std::move(quit_closure)) {}
morrita17137e62015-06-23 22:29:36644
sammc57ed9f982016-03-10 06:28:35645 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita17137e62015-06-23 22:29:36646
tfarina10a5c062015-09-04 18:47:57647 void OnChannelConnected(int32_t peer_pid) override {
Wez831ae412017-08-30 00:29:36648 ListenerThatExpectsOK::SendOK(sender());
Wezfdb98482018-07-12 01:21:39649 RunQuitClosure();
morrita17137e62015-06-23 22:29:36650 }
morrita17137e62015-06-23 22:29:36651};
652
sammc4bcc4ed62016-10-27 10:13:59653DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendOkClient) {
Wez831ae412017-08-30 00:29:36654 base::RunLoop run_loop;
655 ListenerSendingOneOk listener(run_loop.QuitClosure());
sammc57ed9f982016-03-10 06:28:35656 Connect(&listener);
657 listener.set_sender(channel());
morrita17137e62015-06-23 22:29:36658
Wez831ae412017-08-30 00:29:36659 run_loop.Run();
morrita17137e62015-06-23 22:29:36660
sammc57ed9f982016-03-10 06:28:35661 Close();
morrita17137e62015-06-23 22:29:36662}
663
rockot7c6bf952016-07-14 00:34:11664class ListenerWithSimpleAssociatedInterface
665 : public IPC::Listener,
666 public IPC::mojom::SimpleTestDriver {
667 public:
668 static const int kNumMessages;
669
Wezfdb98482018-07-12 01:21:39670 explicit ListenerWithSimpleAssociatedInterface(base::OnceClosure quit_closure)
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15671 : quit_closure_(std::move(quit_closure)) {}
rockot7c6bf952016-07-14 00:34:11672
Chris Watkins2d879af2017-11-30 02:11:59673 ~ListenerWithSimpleAssociatedInterface() override = default;
rockot7c6bf952016-07-14 00:34:11674
675 bool OnMessageReceived(const IPC::Message& message) override {
676 base::PickleIterator iter(message);
rockot9abe09b2016-08-02 20:57:34677 int32_t should_be_expected;
678 EXPECT_TRUE(iter.ReadInt(&should_be_expected));
679 EXPECT_EQ(should_be_expected, next_expected_value_);
rockot7c6bf952016-07-14 00:34:11680 num_messages_received_++;
681 return true;
682 }
683
Wezfdb98482018-07-12 01:21:39684 void OnChannelError() override { CHECK(!quit_closure_); }
rockot7c6bf952016-07-14 00:34:11685
686 void RegisterInterfaceFactory(IPC::Channel* channel) {
687 channel->GetAssociatedInterfaceSupport()->AddAssociatedInterface(
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15688 base::BindRepeating(
689 &ListenerWithSimpleAssociatedInterface::BindReceiver,
690 base::Unretained(this)));
rockot7c6bf952016-07-14 00:34:11691 }
692
693 private:
694 // IPC::mojom::SimpleTestDriver:
rockot9abe09b2016-08-02 20:57:34695 void ExpectValue(int32_t value) override {
696 next_expected_value_ = value;
697 }
698
tzikdd76ce712017-06-08 05:27:04699 void GetExpectedValue(GetExpectedValueCallback callback) override {
rockot9abe09b2016-08-02 20:57:34700 NOTREACHED();
701 }
702
tzikdd76ce712017-06-08 05:27:04703 void RequestValue(RequestValueCallback callback) override { NOTREACHED(); }
rockot7c6bf952016-07-14 00:34:11704
tzikdd76ce712017-06-08 05:27:04705 void RequestQuit(RequestQuitCallback callback) override {
rockot7c6bf952016-07-14 00:34:11706 EXPECT_EQ(kNumMessages, num_messages_received_);
tzikdd76ce712017-06-08 05:27:04707 std::move(callback).Run();
Wezfdb98482018-07-12 01:21:39708 std::move(quit_closure_).Run();
rockot7c6bf952016-07-14 00:34:11709 }
710
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15711 void BindReceiver(
712 mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver) {
713 DCHECK(!receiver_.is_bound());
714 receiver_.Bind(std::move(receiver));
rockot7c6bf952016-07-14 00:34:11715 }
716
rockot9abe09b2016-08-02 20:57:34717 int32_t next_expected_value_ = 0;
rockot7c6bf952016-07-14 00:34:11718 int num_messages_received_ = 0;
Wezfdb98482018-07-12 01:21:39719 base::OnceClosure quit_closure_;
rockot7c6bf952016-07-14 00:34:11720
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15721 mojo::AssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver_{this};
rockot7c6bf952016-07-14 00:34:11722};
723
724const int ListenerWithSimpleAssociatedInterface::kNumMessages = 1000;
725
726class ListenerSendingAssociatedMessages : public IPC::Listener {
727 public:
Wezfdb98482018-07-12 01:21:39728 explicit ListenerSendingAssociatedMessages(base::OnceClosure quit_closure)
729 : quit_closure_(std::move(quit_closure)) {}
rockot7c6bf952016-07-14 00:34:11730
731 bool OnMessageReceived(const IPC::Message& message) override { return true; }
732
733 void OnChannelConnected(int32_t peer_pid) override {
734 DCHECK(channel_);
735 channel_->GetAssociatedInterfaceSupport()->GetRemoteAssociatedInterface(
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15736 driver_.BindNewEndpointAndPassReceiver());
rockot7c6bf952016-07-14 00:34:11737
738 // Send a bunch of interleaved messages, alternating between the associated
739 // interface and a legacy IPC::Message.
740 for (int i = 0; i < ListenerWithSimpleAssociatedInterface::kNumMessages;
741 ++i) {
rockot9abe09b2016-08-02 20:57:34742 driver_->ExpectValue(i);
743 SendValue(channel_, i);
rockot7c6bf952016-07-14 00:34:11744 }
Wezfdb98482018-07-12 01:21:39745 driver_->RequestQuit(base::BindOnce(
746 &ListenerSendingAssociatedMessages::OnQuitAck, base::Unretained(this)));
rockot7c6bf952016-07-14 00:34:11747 }
748
749 void set_channel(IPC::Channel* channel) { channel_ = channel; }
750
751 private:
Wezfdb98482018-07-12 01:21:39752 void OnQuitAck() { std::move(quit_closure_).Run(); }
rockot7c6bf952016-07-14 00:34:11753
754 IPC::Channel* channel_ = nullptr;
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15755 mojo::AssociatedRemote<IPC::mojom::SimpleTestDriver> driver_;
Wezfdb98482018-07-12 01:21:39756 base::OnceClosure quit_closure_;
rockot7c6bf952016-07-14 00:34:11757};
758
759TEST_F(IPCChannelMojoTest, SimpleAssociatedInterface) {
sammc4bcc4ed62016-10-27 10:13:59760 Init("SimpleAssociatedInterfaceClient");
rockot7c6bf952016-07-14 00:34:11761
Wezfdb98482018-07-12 01:21:39762 base::RunLoop run_loop;
763 ListenerWithSimpleAssociatedInterface listener(run_loop.QuitClosure());
rockot7c6bf952016-07-14 00:34:11764 CreateChannel(&listener);
765 ASSERT_TRUE(ConnectChannel());
766
767 listener.RegisterInterfaceFactory(channel());
768
Wezfdb98482018-07-12 01:21:39769 run_loop.Run();
rockot7c6bf952016-07-14 00:34:11770 channel()->Close();
771
772 EXPECT_TRUE(WaitForClientShutdown());
773 DestroyChannel();
774}
775
sammc4bcc4ed62016-10-27 10:13:59776DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(SimpleAssociatedInterfaceClient) {
Wezfdb98482018-07-12 01:21:39777 base::RunLoop run_loop;
778 ListenerSendingAssociatedMessages listener(run_loop.QuitClosure());
rockot7c6bf952016-07-14 00:34:11779 Connect(&listener);
780 listener.set_channel(channel());
781
Wezfdb98482018-07-12 01:21:39782 run_loop.Run();
rockot7c6bf952016-07-14 00:34:11783
784 Close();
785}
786
rockot8d890f62016-07-14 16:37:14787class ChannelProxyRunner {
788 public:
rockota34707ca2016-07-20 04:28:32789 ChannelProxyRunner(mojo::ScopedMessagePipeHandle handle,
790 bool for_server)
791 : for_server_(for_server),
792 handle_(std::move(handle)),
rockot9abe09b2016-08-02 20:57:34793 io_thread_("ChannelProxyRunner IO thread"),
794 never_signaled_(base::WaitableEvent::ResetPolicy::MANUAL,
795 base::WaitableEvent::InitialState::NOT_SIGNALED) {
rockot8d890f62016-07-14 16:37:14796 }
797
798 void CreateProxy(IPC::Listener* listener) {
799 io_thread_.StartWithOptions(
Carlos Caballerodd8bf7b042019-07-30 14:14:15800 base::Thread::Options(base::MessagePumpType::IO, 0));
Hajime Hoshiff15e972017-11-09 06:37:09801 proxy_ = IPC::SyncChannel::Create(listener, io_thread_.task_runner(),
802 base::ThreadTaskRunnerHandle::Get(),
803 &never_signaled_);
rockot8d890f62016-07-14 16:37:14804 }
rockota34707ca2016-07-20 04:28:32805
rockot10188752016-09-08 18:24:56806 void RunProxy() {
rockota34707ca2016-07-20 04:28:32807 std::unique_ptr<IPC::ChannelFactory> factory;
808 if (for_server_) {
809 factory = IPC::ChannelMojo::CreateServerFactory(
Hajime Hoshia98f1102017-11-20 06:34:35810 std::move(handle_), io_thread_.task_runner(),
811 base::ThreadTaskRunnerHandle::Get());
rockota34707ca2016-07-20 04:28:32812 } else {
813 factory = IPC::ChannelMojo::CreateClientFactory(
Hajime Hoshia98f1102017-11-20 06:34:35814 std::move(handle_), io_thread_.task_runner(),
815 base::ThreadTaskRunnerHandle::Get());
rockota34707ca2016-07-20 04:28:32816 }
rockot10188752016-09-08 18:24:56817 proxy_->Init(std::move(factory), true);
rockota34707ca2016-07-20 04:28:32818 }
rockot8d890f62016-07-14 16:37:14819
820 IPC::ChannelProxy* proxy() { return proxy_.get(); }
821
822 private:
rockota34707ca2016-07-20 04:28:32823 const bool for_server_;
rockot8d890f62016-07-14 16:37:14824
rockota34707ca2016-07-20 04:28:32825 mojo::ScopedMessagePipeHandle handle_;
rockot8d890f62016-07-14 16:37:14826 base::Thread io_thread_;
rockot9abe09b2016-08-02 20:57:34827 base::WaitableEvent never_signaled_;
rockotb62e2e32017-03-24 18:36:44828 std::unique_ptr<IPC::ChannelProxy> proxy_;
rockot8d890f62016-07-14 16:37:14829
830 DISALLOW_COPY_AND_ASSIGN(ChannelProxyRunner);
831};
832
833class IPCChannelProxyMojoTest : public IPCChannelMojoTestBase {
834 public:
sammc4bcc4ed62016-10-27 10:13:59835 void Init(const std::string& client_name) {
836 IPCChannelMojoTestBase::Init(client_name);
Peter Boströmfb60ea02021-04-05 21:06:12837 runner_ = std::make_unique<ChannelProxyRunner>(TakeHandle(), true);
rockot8d890f62016-07-14 16:37:14838 }
839 void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
rockot10188752016-09-08 18:24:56840 void RunProxy() {
841 runner_->RunProxy();
rockot401fb2c2016-09-06 18:35:57842 }
rockot0e4de5f2016-07-22 21:18:07843 void DestroyProxy() {
844 runner_.reset();
845 base::RunLoop().RunUntilIdle();
846 }
rockot8d890f62016-07-14 16:37:14847
848 IPC::ChannelProxy* proxy() { return runner_->proxy(); }
849
850 private:
rockot8d890f62016-07-14 16:37:14851 std::unique_ptr<ChannelProxyRunner> runner_;
852};
853
854class ListenerWithSimpleProxyAssociatedInterface
855 : public IPC::Listener,
856 public IPC::mojom::SimpleTestDriver {
857 public:
858 static const int kNumMessages;
859
Wezfdb98482018-07-12 01:21:39860 explicit ListenerWithSimpleProxyAssociatedInterface(
861 base::OnceClosure quit_closure)
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15862 : quit_closure_(std::move(quit_closure)) {}
rockot8d890f62016-07-14 16:37:14863
Chris Watkins2d879af2017-11-30 02:11:59864 ~ListenerWithSimpleProxyAssociatedInterface() override = default;
rockot8d890f62016-07-14 16:37:14865
866 bool OnMessageReceived(const IPC::Message& message) override {
867 base::PickleIterator iter(message);
rockot9abe09b2016-08-02 20:57:34868 int32_t should_be_expected;
869 EXPECT_TRUE(iter.ReadInt(&should_be_expected));
870 EXPECT_EQ(should_be_expected, next_expected_value_);
rockot8d890f62016-07-14 16:37:14871 num_messages_received_++;
872 return true;
873 }
874
Wezfdb98482018-07-12 01:21:39875 void OnChannelError() override { CHECK(!quit_closure_); }
rockot8d890f62016-07-14 16:37:14876
rockotf62002a2016-09-15 00:08:59877 void OnAssociatedInterfaceRequest(
878 const std::string& interface_name,
879 mojo::ScopedInterfaceEndpointHandle handle) override {
880 DCHECK_EQ(interface_name, IPC::mojom::SimpleTestDriver::Name_);
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15881 receiver_.Bind(
882 mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver>(
883 std::move(handle)));
rockot8d890f62016-07-14 16:37:14884 }
885
886 bool received_all_messages() const {
Wezfdb98482018-07-12 01:21:39887 return num_messages_received_ == kNumMessages && !quit_closure_;
rockot8d890f62016-07-14 16:37:14888 }
889
890 private:
891 // IPC::mojom::SimpleTestDriver:
rockot9abe09b2016-08-02 20:57:34892 void ExpectValue(int32_t value) override {
893 next_expected_value_ = value;
894 }
895
tzikdd76ce712017-06-08 05:27:04896 void GetExpectedValue(GetExpectedValueCallback callback) override {
897 std::move(callback).Run(next_expected_value_);
rockot9abe09b2016-08-02 20:57:34898 }
899
tzikdd76ce712017-06-08 05:27:04900 void RequestValue(RequestValueCallback callback) override { NOTREACHED(); }
rockot8d890f62016-07-14 16:37:14901
tzikdd76ce712017-06-08 05:27:04902 void RequestQuit(RequestQuitCallback callback) override {
tzikdd76ce712017-06-08 05:27:04903 std::move(callback).Run();
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15904 receiver_.reset();
Wezfdb98482018-07-12 01:21:39905 std::move(quit_closure_).Run();
rockot8d890f62016-07-14 16:37:14906 }
907
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15908 void BindReceiver(
909 mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver) {
910 DCHECK(!receiver_.is_bound());
911 receiver_.Bind(std::move(receiver));
rockot8d890f62016-07-14 16:37:14912 }
913
rockot9abe09b2016-08-02 20:57:34914 int32_t next_expected_value_ = 0;
rockot8d890f62016-07-14 16:37:14915 int num_messages_received_ = 0;
Wezfdb98482018-07-12 01:21:39916 base::OnceClosure quit_closure_;
rockot8d890f62016-07-14 16:37:14917
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15918 mojo::AssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver_{this};
rockot8d890f62016-07-14 16:37:14919};
920
921const int ListenerWithSimpleProxyAssociatedInterface::kNumMessages = 1000;
922
923TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterface) {
sammc4bcc4ed62016-10-27 10:13:59924 Init("ProxyThreadAssociatedInterfaceClient");
rockot8d890f62016-07-14 16:37:14925
Wezfdb98482018-07-12 01:21:39926 base::RunLoop run_loop;
927 ListenerWithSimpleProxyAssociatedInterface listener(run_loop.QuitClosure());
rockot8d890f62016-07-14 16:37:14928 CreateProxy(&listener);
rockot8d890f62016-07-14 16:37:14929 RunProxy();
930
Wezfdb98482018-07-12 01:21:39931 run_loop.Run();
rockot8d890f62016-07-14 16:37:14932
933 EXPECT_TRUE(WaitForClientShutdown());
934 EXPECT_TRUE(listener.received_all_messages());
935
rockot0e4de5f2016-07-22 21:18:07936 DestroyProxy();
rockot8d890f62016-07-14 16:37:14937}
938
939class ChannelProxyClient {
940 public:
941 void Init(mojo::ScopedMessagePipeHandle handle) {
Peter Boströmfb60ea02021-04-05 21:06:12942 runner_ = std::make_unique<ChannelProxyRunner>(std::move(handle), false);
rockot8d890f62016-07-14 16:37:14943 }
rockot9abe09b2016-08-02 20:57:34944
rockot8d890f62016-07-14 16:37:14945 void CreateProxy(IPC::Listener* listener) { runner_->CreateProxy(listener); }
rockot9abe09b2016-08-02 20:57:34946
rockot10188752016-09-08 18:24:56947 void RunProxy() { runner_->RunProxy(); }
rockot9abe09b2016-08-02 20:57:34948
rockot0e4de5f2016-07-22 21:18:07949 void DestroyProxy() {
950 runner_.reset();
951 base::RunLoop().RunUntilIdle();
952 }
rockot8d890f62016-07-14 16:37:14953
rockot9abe09b2016-08-02 20:57:34954 void RequestQuitAndWaitForAck(IPC::mojom::SimpleTestDriver* driver) {
955 base::RunLoop loop;
956 driver->RequestQuit(loop.QuitClosure());
957 loop.Run();
958 }
959
rockot8d890f62016-07-14 16:37:14960 IPC::ChannelProxy* proxy() { return runner_->proxy(); }
961
962 private:
Gabriel Charettec523fa62019-09-09 23:03:32963 base::test::SingleThreadTaskEnvironment task_environment_;
rockot8d890f62016-07-14 16:37:14964 std::unique_ptr<ChannelProxyRunner> runner_;
965};
966
rockot0e4de5f2016-07-22 21:18:07967class DummyListener : public IPC::Listener {
rockot8d890f62016-07-14 16:37:14968 public:
rockot8d890f62016-07-14 16:37:14969 // IPC::Listener
970 bool OnMessageReceived(const IPC::Message& message) override { return true; }
rockot8d890f62016-07-14 16:37:14971};
972
sammc4bcc4ed62016-10-27 10:13:59973DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(
974 ProxyThreadAssociatedInterfaceClient,
975 ChannelProxyClient) {
rockot0e4de5f2016-07-22 21:18:07976 DummyListener listener;
rockot8d890f62016-07-14 16:37:14977 CreateProxy(&listener);
978 RunProxy();
rockot8d890f62016-07-14 16:37:14979
980 // Send a bunch of interleaved messages, alternating between the associated
981 // interface and a legacy IPC::Message.
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15982 mojo::AssociatedRemote<IPC::mojom::SimpleTestDriver> driver;
rockot8d890f62016-07-14 16:37:14983 proxy()->GetRemoteAssociatedInterface(&driver);
984 for (int i = 0; i < ListenerWithSimpleProxyAssociatedInterface::kNumMessages;
985 ++i) {
rockot9abe09b2016-08-02 20:57:34986 driver->ExpectValue(i);
987 SendValue(proxy(), i);
rockot8d890f62016-07-14 16:37:14988 }
Wezfdb98482018-07-12 01:21:39989 base::RunLoop run_loop;
990 driver->RequestQuit(run_loop.QuitClosure());
991 run_loop.Run();
rockot0e4de5f2016-07-22 21:18:07992
993 DestroyProxy();
rockot8d890f62016-07-14 16:37:14994}
995
rockot401fb2c2016-09-06 18:35:57996class ListenerWithIndirectProxyAssociatedInterface
997 : public IPC::Listener,
998 public IPC::mojom::IndirectTestDriver,
999 public IPC::mojom::PingReceiver {
1000 public:
Julie Jeongeun Kimac82819f2019-11-20 00:01:271001 ListenerWithIndirectProxyAssociatedInterface() = default;
Chris Watkins2d879af2017-11-30 02:11:591002 ~ListenerWithIndirectProxyAssociatedInterface() override = default;
rockot401fb2c2016-09-06 18:35:571003
rockot70bbb59492017-01-25 00:56:511004 // IPC::Listener:
rockot401fb2c2016-09-06 18:35:571005 bool OnMessageReceived(const IPC::Message& message) override { return true; }
1006
rockot70bbb59492017-01-25 00:56:511007 void OnAssociatedInterfaceRequest(
1008 const std::string& interface_name,
1009 mojo::ScopedInterfaceEndpointHandle handle) override {
Julie Jeongeun Kimac82819f2019-11-20 00:01:271010 DCHECK(!driver_receiver_.is_bound());
rockot70bbb59492017-01-25 00:56:511011 DCHECK_EQ(interface_name, IPC::mojom::IndirectTestDriver::Name_);
Julie Jeongeun Kimac82819f2019-11-20 00:01:271012 driver_receiver_.Bind(
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:541013 mojo::PendingAssociatedReceiver<IPC::mojom::IndirectTestDriver>(
1014 std::move(handle)));
rockot401fb2c2016-09-06 18:35:571015 }
1016
Wezfdb98482018-07-12 01:21:391017 void set_ping_handler(const base::RepeatingClosure& handler) {
rockot401fb2c2016-09-06 18:35:571018 ping_handler_ = handler;
1019 }
1020
1021 private:
1022 // IPC::mojom::IndirectTestDriver:
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491023 void GetPingReceiver(mojo::PendingAssociatedReceiver<IPC::mojom::PingReceiver>
1024 receiver) override {
1025 ping_receiver_receiver_.Bind(std::move(receiver));
rockot401fb2c2016-09-06 18:35:571026 }
1027
1028 // IPC::mojom::PingReceiver:
tzikdd76ce712017-06-08 05:27:041029 void Ping(PingCallback callback) override {
1030 std::move(callback).Run();
rockot401fb2c2016-09-06 18:35:571031 ping_handler_.Run();
1032 }
1033
Julie Jeongeun Kimac82819f2019-11-20 00:01:271034 mojo::AssociatedReceiver<IPC::mojom::IndirectTestDriver> driver_receiver_{
1035 this};
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491036 mojo::AssociatedReceiver<IPC::mojom::PingReceiver> ping_receiver_receiver_{
1037 this};
rockot401fb2c2016-09-06 18:35:571038
Wezfdb98482018-07-12 01:21:391039 base::RepeatingClosure ping_handler_;
rockot401fb2c2016-09-06 18:35:571040};
1041
1042TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterfaceIndirect) {
1043 // Tests that we can pipeline interface requests and subsequent messages
1044 // targeting proxy thread bindings, and the channel will still dispatch
1045 // messages appropriately.
1046
sammc4bcc4ed62016-10-27 10:13:591047 Init("ProxyThreadAssociatedInterfaceIndirectClient");
rockot401fb2c2016-09-06 18:35:571048
1049 ListenerWithIndirectProxyAssociatedInterface listener;
1050 CreateProxy(&listener);
rockot401fb2c2016-09-06 18:35:571051 RunProxy();
1052
1053 base::RunLoop loop;
1054 listener.set_ping_handler(loop.QuitClosure());
1055 loop.Run();
1056
1057 EXPECT_TRUE(WaitForClientShutdown());
1058
1059 DestroyProxy();
1060}
1061
sammc4bcc4ed62016-10-27 10:13:591062DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(
rockot401fb2c2016-09-06 18:35:571063 ProxyThreadAssociatedInterfaceIndirectClient,
1064 ChannelProxyClient) {
1065 DummyListener listener;
1066 CreateProxy(&listener);
1067 RunProxy();
1068
1069 // Use an interface requested via another interface. On the remote end both
1070 // interfaces are bound on the proxy thread. This ensures that the Ping
1071 // message we send will still be dispatched properly even though the remote
1072 // endpoint may not have been bound yet by the time the message is initially
1073 // processed on the IO thread.
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:541074 mojo::AssociatedRemote<IPC::mojom::IndirectTestDriver> driver;
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491075 mojo::AssociatedRemote<IPC::mojom::PingReceiver> ping_receiver;
rockot401fb2c2016-09-06 18:35:571076 proxy()->GetRemoteAssociatedInterface(&driver);
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491077 driver->GetPingReceiver(ping_receiver.BindNewEndpointAndPassReceiver());
rockot401fb2c2016-09-06 18:35:571078
1079 base::RunLoop loop;
1080 ping_receiver->Ping(loop.QuitClosure());
1081 loop.Run();
1082
1083 DestroyProxy();
1084}
1085
rockot9abe09b2016-08-02 20:57:341086class ListenerWithSyncAssociatedInterface
1087 : public IPC::Listener,
1088 public IPC::mojom::SimpleTestDriver {
1089 public:
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151090 ListenerWithSyncAssociatedInterface() = default;
Chris Watkins2d879af2017-11-30 02:11:591091 ~ListenerWithSyncAssociatedInterface() override = default;
rockot9abe09b2016-08-02 20:57:341092
1093 void set_sync_sender(IPC::Sender* sync_sender) { sync_sender_ = sync_sender; }
1094
rockot9abe09b2016-08-02 20:57:341095 void RunUntilQuitRequested() {
1096 base::RunLoop loop;
1097 quit_closure_ = loop.QuitClosure();
1098 loop.Run();
1099 }
1100
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151101 void CloseBinding() { receiver_.reset(); }
rockot9abe09b2016-08-02 20:57:341102
1103 void set_response_value(int32_t response) {
1104 response_value_ = response;
1105 }
1106
1107 private:
1108 // IPC::mojom::SimpleTestDriver:
1109 void ExpectValue(int32_t value) override {
1110 next_expected_value_ = value;
1111 }
1112
tzikdd76ce712017-06-08 05:27:041113 void GetExpectedValue(GetExpectedValueCallback callback) override {
1114 std::move(callback).Run(next_expected_value_);
rockot9abe09b2016-08-02 20:57:341115 }
1116
tzikdd76ce712017-06-08 05:27:041117 void RequestValue(RequestValueCallback callback) override {
1118 std::move(callback).Run(response_value_);
rockot9abe09b2016-08-02 20:57:341119 }
1120
tzikdd76ce712017-06-08 05:27:041121 void RequestQuit(RequestQuitCallback callback) override {
Wezfdb98482018-07-12 01:21:391122 std::move(quit_closure_).Run();
tzikdd76ce712017-06-08 05:27:041123 std::move(callback).Run();
rockot9abe09b2016-08-02 20:57:341124 }
1125
1126 // IPC::Listener:
1127 bool OnMessageReceived(const IPC::Message& message) override {
1128 EXPECT_EQ(0u, message.type());
1129 EXPECT_TRUE(message.is_sync());
1130 EXPECT_TRUE(message.should_unblock());
1131 std::unique_ptr<IPC::Message> reply(
1132 IPC::SyncMessage::GenerateReply(&message));
1133 reply->WriteInt(response_value_);
1134 DCHECK(sync_sender_);
1135 EXPECT_TRUE(sync_sender_->Send(reply.release()));
1136 return true;
1137 }
1138
rockot70bbb59492017-01-25 00:56:511139 void OnAssociatedInterfaceRequest(
1140 const std::string& interface_name,
1141 mojo::ScopedInterfaceEndpointHandle handle) override {
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151142 DCHECK(!receiver_.is_bound());
rockot70bbb59492017-01-25 00:56:511143 DCHECK_EQ(interface_name, IPC::mojom::SimpleTestDriver::Name_);
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151144 receiver_.Bind(
1145 mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver>(
1146 std::move(handle)));
rockot70bbb59492017-01-25 00:56:511147 }
1148
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151149 void BindReceiver(
1150 mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver) {
1151 DCHECK(!receiver_.is_bound());
1152 receiver_.Bind(std::move(receiver));
rockot9abe09b2016-08-02 20:57:341153 }
1154
1155 IPC::Sender* sync_sender_ = nullptr;
1156 int32_t next_expected_value_ = 0;
1157 int32_t response_value_ = 0;
Wezfdb98482018-07-12 01:21:391158 base::OnceClosure quit_closure_;
rockot9abe09b2016-08-02 20:57:341159
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151160 mojo::AssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver_{this};
rockot9abe09b2016-08-02 20:57:341161};
1162
1163class SyncReplyReader : public IPC::MessageReplyDeserializer {
1164 public:
1165 explicit SyncReplyReader(int32_t* storage) : storage_(storage) {}
Chris Watkins2d879af2017-11-30 02:11:591166 ~SyncReplyReader() override = default;
rockot9abe09b2016-08-02 20:57:341167
1168 private:
1169 // IPC::MessageReplyDeserializer:
1170 bool SerializeOutputParameters(const IPC::Message& message,
1171 base::PickleIterator iter) override {
1172 if (!iter.ReadInt(storage_))
1173 return false;
1174 return true;
1175 }
1176
1177 int32_t* storage_;
1178
1179 DISALLOW_COPY_AND_ASSIGN(SyncReplyReader);
1180};
1181
1182TEST_F(IPCChannelProxyMojoTest, SyncAssociatedInterface) {
sammc4bcc4ed62016-10-27 10:13:591183 Init("SyncAssociatedInterface");
rockot9abe09b2016-08-02 20:57:341184
1185 ListenerWithSyncAssociatedInterface listener;
1186 CreateProxy(&listener);
1187 listener.set_sync_sender(proxy());
rockot9abe09b2016-08-02 20:57:341188 RunProxy();
1189
1190 // Run the client's simple sanity check to completion.
1191 listener.RunUntilQuitRequested();
1192
1193 // Verify that we can send a sync IPC and service an incoming sync request
1194 // while waiting on it
1195 listener.set_response_value(42);
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151196 mojo::AssociatedRemote<IPC::mojom::SimpleTestClient> client;
rockot9abe09b2016-08-02 20:57:341197 proxy()->GetRemoteAssociatedInterface(&client);
1198 int32_t received_value;
1199 EXPECT_TRUE(client->RequestValue(&received_value));
1200 EXPECT_EQ(42, received_value);
1201
1202 // Do it again. This time the client will send a classical sync IPC to us
1203 // while we wait.
1204 received_value = 0;
1205 EXPECT_TRUE(client->RequestValue(&received_value));
1206 EXPECT_EQ(42, received_value);
1207
1208 // Now make a classical sync IPC request to the client. It will send a
1209 // sync associated interface message to us while we wait.
1210 received_value = 0;
1211 std::unique_ptr<IPC::SyncMessage> request(
1212 new IPC::SyncMessage(0, 0, IPC::Message::PRIORITY_NORMAL,
1213 new SyncReplyReader(&received_value)));
1214 EXPECT_TRUE(proxy()->Send(request.release()));
1215 EXPECT_EQ(42, received_value);
1216
1217 listener.CloseBinding();
1218 EXPECT_TRUE(WaitForClientShutdown());
1219
1220 DestroyProxy();
1221}
1222
1223class SimpleTestClientImpl : public IPC::mojom::SimpleTestClient,
1224 public IPC::Listener {
1225 public:
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151226 SimpleTestClientImpl() = default;
1227 ~SimpleTestClientImpl() override = default;
rockot9abe09b2016-08-02 20:57:341228
1229 void set_driver(IPC::mojom::SimpleTestDriver* driver) { driver_ = driver; }
1230 void set_sync_sender(IPC::Sender* sync_sender) { sync_sender_ = sync_sender; }
1231
rockot9abe09b2016-08-02 20:57:341232 void WaitForValueRequest() {
Peter Boströmfb60ea02021-04-05 21:06:121233 run_loop_ = std::make_unique<base::RunLoop>();
rockot9abe09b2016-08-02 20:57:341234 run_loop_->Run();
1235 }
1236
1237 void UseSyncSenderForRequest(bool use_sync_sender) {
1238 use_sync_sender_ = use_sync_sender;
1239 }
1240
1241 private:
1242 // IPC::mojom::SimpleTestClient:
tzikdd76ce712017-06-08 05:27:041243 void RequestValue(RequestValueCallback callback) override {
rockot9abe09b2016-08-02 20:57:341244 int32_t response = 0;
1245 if (use_sync_sender_) {
1246 std::unique_ptr<IPC::SyncMessage> reply(new IPC::SyncMessage(
1247 0, 0, IPC::Message::PRIORITY_NORMAL, new SyncReplyReader(&response)));
1248 EXPECT_TRUE(sync_sender_->Send(reply.release()));
1249 } else {
1250 DCHECK(driver_);
1251 EXPECT_TRUE(driver_->RequestValue(&response));
1252 }
1253
tzikdd76ce712017-06-08 05:27:041254 std::move(callback).Run(response);
rockot9abe09b2016-08-02 20:57:341255
1256 DCHECK(run_loop_);
1257 run_loop_->Quit();
1258 }
1259
1260 // IPC::Listener:
1261 bool OnMessageReceived(const IPC::Message& message) override {
1262 int32_t response;
1263 DCHECK(driver_);
1264 EXPECT_TRUE(driver_->RequestValue(&response));
1265 std::unique_ptr<IPC::Message> reply(
1266 IPC::SyncMessage::GenerateReply(&message));
1267 reply->WriteInt(response);
1268 EXPECT_TRUE(sync_sender_->Send(reply.release()));
1269
1270 DCHECK(run_loop_);
1271 run_loop_->Quit();
1272 return true;
1273 }
1274
rockot70bbb59492017-01-25 00:56:511275 void OnAssociatedInterfaceRequest(
1276 const std::string& interface_name,
1277 mojo::ScopedInterfaceEndpointHandle handle) override {
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151278 DCHECK(!receiver_.is_bound());
rockot70bbb59492017-01-25 00:56:511279 DCHECK_EQ(interface_name, IPC::mojom::SimpleTestClient::Name_);
1280
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151281 receiver_.Bind(
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491282 mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestClient>(
1283 std::move(handle)));
rockot70bbb59492017-01-25 00:56:511284 }
1285
rockot9abe09b2016-08-02 20:57:341286 bool use_sync_sender_ = false;
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491287 mojo::AssociatedReceiver<IPC::mojom::SimpleTestClient> receiver_{this};
rockot9abe09b2016-08-02 20:57:341288 IPC::Sender* sync_sender_ = nullptr;
1289 IPC::mojom::SimpleTestDriver* driver_ = nullptr;
1290 std::unique_ptr<base::RunLoop> run_loop_;
1291
1292 DISALLOW_COPY_AND_ASSIGN(SimpleTestClientImpl);
1293};
1294
sammc4bcc4ed62016-10-27 10:13:591295DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(SyncAssociatedInterface,
1296 ChannelProxyClient) {
rockot9abe09b2016-08-02 20:57:341297 SimpleTestClientImpl client_impl;
1298 CreateProxy(&client_impl);
1299 client_impl.set_sync_sender(proxy());
rockot9abe09b2016-08-02 20:57:341300 RunProxy();
1301
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151302 mojo::AssociatedRemote<IPC::mojom::SimpleTestDriver> driver;
rockot9abe09b2016-08-02 20:57:341303 proxy()->GetRemoteAssociatedInterface(&driver);
1304 client_impl.set_driver(driver.get());
1305
1306 // Simple sync message sanity check.
1307 driver->ExpectValue(42);
1308 int32_t expected_value = 0;
1309 EXPECT_TRUE(driver->GetExpectedValue(&expected_value));
1310 EXPECT_EQ(42, expected_value);
1311 RequestQuitAndWaitForAck(driver.get());
1312
1313 // Wait for the test driver to perform a sync call test with our own sync
1314 // associated interface message nested inside.
1315 client_impl.UseSyncSenderForRequest(false);
1316 client_impl.WaitForValueRequest();
1317
1318 // Wait for the test driver to perform a sync call test with our own classical
1319 // sync IPC nested inside.
1320 client_impl.UseSyncSenderForRequest(true);
1321 client_impl.WaitForValueRequest();
1322
1323 // Wait for the test driver to perform a classical sync IPC request, with our
1324 // own sync associated interface message nested inside.
1325 client_impl.UseSyncSenderForRequest(false);
1326 client_impl.WaitForValueRequest();
1327
1328 DestroyProxy();
1329}
1330
rockot10188752016-09-08 18:24:561331TEST_F(IPCChannelProxyMojoTest, Pause) {
1332 // Ensures that pausing a channel elicits the expected behavior when sending
1333 // messages, unpausing, sending more messages, and then manually flushing.
1334 // Specifically a sequence like:
rockot401fb2c2016-09-06 18:35:571335 //
1336 // Connect()
1337 // Send(A)
rockot10188752016-09-08 18:24:561338 // Pause()
rockot401fb2c2016-09-06 18:35:571339 // Send(B)
rockot401fb2c2016-09-06 18:35:571340 // Send(C)
rockot10188752016-09-08 18:24:561341 // Unpause(false)
1342 // Send(D)
1343 // Send(E)
rockot401fb2c2016-09-06 18:35:571344 // Flush()
1345 //
rockot10188752016-09-08 18:24:561346 // must result in the other end receiving messages A, D, E, B, D; in that
rockot401fb2c2016-09-06 18:35:571347 // order.
1348 //
1349 // This behavior is required by some consumers of IPC::Channel, and it is not
1350 // sufficient to leave this up to the consumer to implement since associated
1351 // interface requests and messages also need to be queued according to the
1352 // same policy.
sammc4bcc4ed62016-10-27 10:13:591353 Init("CreatePausedClient");
rockot401fb2c2016-09-06 18:35:571354
1355 DummyListener listener;
1356 CreateProxy(&listener);
rockot10188752016-09-08 18:24:561357 RunProxy();
1358
1359 // This message must be sent immediately since the channel is unpaused.
1360 SendValue(proxy(), 1);
1361
1362 proxy()->Pause();
rockot401fb2c2016-09-06 18:35:571363
1364 // These messages must be queued internally since the channel is paused.
rockot401fb2c2016-09-06 18:35:571365 SendValue(proxy(), 2);
rockot10188752016-09-08 18:24:561366 SendValue(proxy(), 3);
rockot401fb2c2016-09-06 18:35:571367
1368 proxy()->Unpause(false /* flush */);
1369
1370 // These messages must be sent immediately since the channel is unpaused.
rockot401fb2c2016-09-06 18:35:571371 SendValue(proxy(), 4);
rockot10188752016-09-08 18:24:561372 SendValue(proxy(), 5);
rockot401fb2c2016-09-06 18:35:571373
1374 // Now we flush the previously queued messages.
1375 proxy()->Flush();
1376
1377 EXPECT_TRUE(WaitForClientShutdown());
1378 DestroyProxy();
1379}
1380
1381class ExpectValueSequenceListener : public IPC::Listener {
1382 public:
Wezfdb98482018-07-12 01:21:391383 ExpectValueSequenceListener(base::queue<int32_t>* expected_values,
1384 base::OnceClosure quit_closure)
1385 : expected_values_(expected_values),
1386 quit_closure_(std::move(quit_closure)) {}
Chris Watkins2d879af2017-11-30 02:11:591387 ~ExpectValueSequenceListener() override = default;
rockot401fb2c2016-09-06 18:35:571388
1389 // IPC::Listener:
1390 bool OnMessageReceived(const IPC::Message& message) override {
1391 DCHECK(!expected_values_->empty());
1392 base::PickleIterator iter(message);
1393 int32_t should_be_expected;
1394 EXPECT_TRUE(iter.ReadInt(&should_be_expected));
1395 EXPECT_EQ(expected_values_->front(), should_be_expected);
1396 expected_values_->pop();
1397 if (expected_values_->empty())
Wezfdb98482018-07-12 01:21:391398 std::move(quit_closure_).Run();
rockot401fb2c2016-09-06 18:35:571399 return true;
1400 }
1401
1402 private:
Brett Wilsona62d9c02017-09-20 20:53:201403 base::queue<int32_t>* expected_values_;
Wezfdb98482018-07-12 01:21:391404 base::OnceClosure quit_closure_;
rockot401fb2c2016-09-06 18:35:571405
1406 DISALLOW_COPY_AND_ASSIGN(ExpectValueSequenceListener);
1407};
1408
sammc4bcc4ed62016-10-27 10:13:591409DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(CreatePausedClient,
1410 ChannelProxyClient) {
Brett Wilsona62d9c02017-09-20 20:53:201411 base::queue<int32_t> expected_values;
Wezfdb98482018-07-12 01:21:391412 base::RunLoop run_loop;
1413 ExpectValueSequenceListener listener(&expected_values,
1414 run_loop.QuitClosure());
rockot401fb2c2016-09-06 18:35:571415 CreateProxy(&listener);
rockot401fb2c2016-09-06 18:35:571416 expected_values.push(1);
rockot10188752016-09-08 18:24:561417 expected_values.push(4);
1418 expected_values.push(5);
rockot401fb2c2016-09-06 18:35:571419 expected_values.push(2);
rockot10188752016-09-08 18:24:561420 expected_values.push(3);
rockot401fb2c2016-09-06 18:35:571421 RunProxy();
Wezfdb98482018-07-12 01:21:391422 run_loop.Run();
rockot401fb2c2016-09-06 18:35:571423 EXPECT_TRUE(expected_values.empty());
1424 DestroyProxy();
1425}
1426
sammc5c8a6c62017-02-04 01:33:381427TEST_F(IPCChannelProxyMojoTest, AssociatedRequestClose) {
1428 Init("DropAssociatedRequest");
1429
1430 DummyListener listener;
1431 CreateProxy(&listener);
1432 RunProxy();
1433
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:541434 mojo::AssociatedRemote<IPC::mojom::AssociatedInterfaceVendor> vendor;
sammc5c8a6c62017-02-04 01:33:381435 proxy()->GetRemoteAssociatedInterface(&vendor);
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151436 mojo::AssociatedRemote<IPC::mojom::SimpleTestDriver> tester;
1437 vendor->GetTestInterface(tester.BindNewEndpointAndPassReceiver());
sammc5c8a6c62017-02-04 01:33:381438 base::RunLoop run_loop;
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151439 tester.set_disconnect_handler(run_loop.QuitClosure());
sammc5c8a6c62017-02-04 01:33:381440 run_loop.Run();
1441
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151442 tester.reset();
sammc5c8a6c62017-02-04 01:33:381443 proxy()->GetRemoteAssociatedInterface(&tester);
1444 EXPECT_TRUE(WaitForClientShutdown());
1445 DestroyProxy();
1446}
1447
1448class AssociatedInterfaceDroppingListener : public IPC::Listener {
1449 public:
Wezfdb98482018-07-12 01:21:391450 AssociatedInterfaceDroppingListener(base::OnceClosure callback)
1451 : callback_(std::move(callback)) {}
sammc5c8a6c62017-02-04 01:33:381452 bool OnMessageReceived(const IPC::Message& message) override { return false; }
1453
1454 void OnAssociatedInterfaceRequest(
1455 const std::string& interface_name,
1456 mojo::ScopedInterfaceEndpointHandle handle) override {
1457 if (interface_name == IPC::mojom::SimpleTestDriver::Name_)
Wezfdb98482018-07-12 01:21:391458 std::move(callback_).Run();
sammc5c8a6c62017-02-04 01:33:381459 }
1460
1461 private:
Wezfdb98482018-07-12 01:21:391462 base::OnceClosure callback_;
sammc5c8a6c62017-02-04 01:33:381463};
1464
1465DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(DropAssociatedRequest,
1466 ChannelProxyClient) {
1467 base::RunLoop run_loop;
1468 AssociatedInterfaceDroppingListener listener(run_loop.QuitClosure());
1469 CreateProxy(&listener);
1470 RunProxy();
1471 run_loop.Run();
1472 DestroyProxy();
1473}
1474
Avi Drissman2b97d032020-07-30 03:02:261475#if !defined(OS_APPLE)
Wez831ae412017-08-30 00:29:361476// TODO(wez): On Mac we need to set up a MachPortBroker before we can transfer
1477// Mach ports (which underpin Sharedmemory on Mac) across IPC.
1478
Alexandr Ilind497eee2018-04-19 22:50:541479template <class SharedMemoryRegionType>
1480class IPCChannelMojoSharedMemoryRegionTypedTest : public IPCChannelMojoTest {};
1481
1482struct WritableRegionTraits {
1483 using RegionType = base::WritableSharedMemoryRegion;
1484 static const char kClientName[];
1485};
1486const char WritableRegionTraits::kClientName[] =
1487 "IPCChannelMojoTestSendWritableSharedMemoryRegionClient";
1488struct UnsafeRegionTraits {
1489 using RegionType = base::UnsafeSharedMemoryRegion;
1490 static const char kClientName[];
1491};
1492const char UnsafeRegionTraits::kClientName[] =
1493 "IPCChannelMojoTestSendUnsafeSharedMemoryRegionClient";
1494struct ReadOnlyRegionTraits {
1495 using RegionType = base::ReadOnlySharedMemoryRegion;
1496 static const char kClientName[];
1497};
1498const char ReadOnlyRegionTraits::kClientName[] =
1499 "IPCChannelMojoTestSendReadOnlySharedMemoryRegionClient";
1500
1501typedef ::testing::
1502 Types<WritableRegionTraits, UnsafeRegionTraits, ReadOnlyRegionTraits>
1503 AllSharedMemoryRegionTraits;
Victor Costanebc52732019-02-15 02:39:471504TYPED_TEST_SUITE(IPCChannelMojoSharedMemoryRegionTypedTest,
1505 AllSharedMemoryRegionTraits);
Alexandr Ilind497eee2018-04-19 22:50:541506
1507template <class SharedMemoryRegionType>
1508class ListenerThatExpectsSharedMemoryRegion : public TestListenerBase {
1509 public:
Wezfdb98482018-07-12 01:21:391510 explicit ListenerThatExpectsSharedMemoryRegion(base::OnceClosure quit_closure)
Alexandr Ilind497eee2018-04-19 22:50:541511 : TestListenerBase(std::move(quit_closure)) {}
1512
1513 bool OnMessageReceived(const IPC::Message& message) override {
1514 base::PickleIterator iter(message);
1515
1516 SharedMemoryRegionType region;
1517 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &region));
1518 EXPECT_TRUE(region.IsValid());
1519
1520 // Verify the shared memory region has expected content.
1521 typename SharedMemoryRegionType::MappingType mapping = region.Map();
1522 std::string content = HandleSendingHelper::GetSendingFileContent();
1523 EXPECT_EQ(0, memcmp(mapping.memory(), content.data(), content.size()));
1524
1525 ListenerThatExpectsOK::SendOK(sender());
1526 return true;
1527 }
1528};
1529
1530TYPED_TEST(IPCChannelMojoSharedMemoryRegionTypedTest, Send) {
1531 this->Init(TypeParam::kClientName);
1532
1533 const size_t size = 1004;
1534 typename TypeParam::RegionType region;
1535 base::WritableSharedMemoryMapping mapping;
1536 std::tie(region, mapping) =
1537 base::CreateMappedRegion<typename TypeParam::RegionType>(size);
1538
1539 std::string content = HandleSendingHelper::GetSendingFileContent();
1540 memcpy(mapping.memory(), content.data(), content.size());
1541
1542 // Create a success listener, and launch the child process.
1543 base::RunLoop run_loop;
1544 ListenerThatExpectsOK listener(run_loop.QuitClosure());
1545 this->CreateChannel(&listener);
1546 ASSERT_TRUE(this->ConnectChannel());
1547
1548 // Send the child process an IPC with |shmem| attached, to verify
1549 // that is is correctly wrapped, transferred and unwrapped.
1550 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
1551 IPC::WriteParam(message, region);
1552 ASSERT_TRUE(this->channel()->Send(message));
1553
1554 run_loop.Run();
1555
1556 this->channel()->Close();
1557
1558 EXPECT_TRUE(this->WaitForClientShutdown());
1559 EXPECT_FALSE(region.IsValid());
1560 this->DestroyChannel();
1561}
1562
1563DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
1564 IPCChannelMojoTestSendWritableSharedMemoryRegionClient) {
1565 base::RunLoop run_loop;
1566 ListenerThatExpectsSharedMemoryRegion<base::WritableSharedMemoryRegion>
1567 listener(run_loop.QuitClosure());
1568 Connect(&listener);
1569 listener.set_sender(channel());
1570
1571 run_loop.Run();
1572
1573 Close();
1574}
1575DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
1576 IPCChannelMojoTestSendUnsafeSharedMemoryRegionClient) {
1577 base::RunLoop run_loop;
1578 ListenerThatExpectsSharedMemoryRegion<base::UnsafeSharedMemoryRegion>
1579 listener(run_loop.QuitClosure());
1580 Connect(&listener);
1581 listener.set_sender(channel());
1582
1583 run_loop.Run();
1584
1585 Close();
1586}
1587DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
1588 IPCChannelMojoTestSendReadOnlySharedMemoryRegionClient) {
1589 base::RunLoop run_loop;
1590 ListenerThatExpectsSharedMemoryRegion<base::ReadOnlySharedMemoryRegion>
1591 listener(run_loop.QuitClosure());
1592 Connect(&listener);
1593 listener.set_sender(channel());
1594
1595 run_loop.Run();
1596
1597 Close();
1598}
Avi Drissman2b97d032020-07-30 03:02:261599#endif // !defined(OS_APPLE)
Wez831ae412017-08-30 00:29:361600
Fabrice de Gans-Riberi894661c2018-05-24 18:43:221601#if defined(OS_POSIX) || defined(OS_FUCHSIA)
rockot8d890f62016-07-14 16:37:141602
Wez831ae412017-08-30 00:29:361603class ListenerThatExpectsFile : public TestListenerBase {
[email protected]64860882014-08-04 23:44:171604 public:
Wezfdb98482018-07-12 01:21:391605 explicit ListenerThatExpectsFile(base::OnceClosure quit_closure)
1606 : TestListenerBase(std::move(quit_closure)) {}
[email protected]64860882014-08-04 23:44:171607
dchengfe61fca2014-10-22 02:29:521608 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:251609 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:301610 HandleSendingHelper::ReadReceivedFile(message, &iter);
Wez831ae412017-08-30 00:29:361611 ListenerThatExpectsOK::SendOK(sender());
[email protected]64860882014-08-04 23:44:171612 return true;
1613 }
[email protected]64860882014-08-04 23:44:171614};
1615
Wez831ae412017-08-30 00:29:361616TEST_F(IPCChannelMojoTest, SendPlatformFile) {
1617 Init("IPCChannelMojoTestSendPlatformFileClient");
[email protected]64860882014-08-04 23:44:171618
Wez831ae412017-08-30 00:29:361619 base::RunLoop run_loop;
1620 ListenerThatExpectsOK listener(run_loop.QuitClosure());
morrita373af03b2014-09-09 19:35:241621 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:171622 ASSERT_TRUE(ConnectChannel());
[email protected]64860882014-08-04 23:44:171623
amistry20e2b1d62016-06-23 06:12:351624 base::ScopedTempDir temp_dir;
1625 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
vabr16e5f602016-09-15 18:14:001626 base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.GetPath()),
[email protected]64860882014-08-04 23:44:171627 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
morrita81b17e02015-02-06 00:58:301628 base::File::FLAG_READ);
1629 HandleSendingHelper::WriteFileThenSend(channel(), file);
Wez831ae412017-08-30 00:29:361630 run_loop.Run();
[email protected]64860882014-08-04 23:44:171631
sammc57ed9f982016-03-10 06:28:351632 channel()->Close();
[email protected]64860882014-08-04 23:44:171633
1634 EXPECT_TRUE(WaitForClientShutdown());
1635 DestroyChannel();
1636}
1637
Wez831ae412017-08-30 00:29:361638DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendPlatformFileClient) {
1639 base::RunLoop run_loop;
1640 ListenerThatExpectsFile listener(run_loop.QuitClosure());
sammc57ed9f982016-03-10 06:28:351641 Connect(&listener);
1642 listener.set_sender(channel());
[email protected]64860882014-08-04 23:44:171643
Wez831ae412017-08-30 00:29:361644 run_loop.Run();
[email protected]64860882014-08-04 23:44:171645
sammc57ed9f982016-03-10 06:28:351646 Close();
[email protected]64860882014-08-04 23:44:171647}
morrita81b17e02015-02-06 00:58:301648
Wez831ae412017-08-30 00:29:361649class ListenerThatExpectsFileAndMessagePipe : public TestListenerBase {
morrita81b17e02015-02-06 00:58:301650 public:
Wezfdb98482018-07-12 01:21:391651 explicit ListenerThatExpectsFileAndMessagePipe(base::OnceClosure quit_closure)
1652 : TestListenerBase(std::move(quit_closure)) {}
morrita81b17e02015-02-06 00:58:301653
Chris Watkins2d879af2017-11-30 02:11:591654 ~ListenerThatExpectsFileAndMessagePipe() override = default;
morrita81b17e02015-02-06 00:58:301655
1656 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:251657 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:301658 HandleSendingHelper::ReadReceivedFile(message, &iter);
1659 HandleSendingHelper::ReadReceivedPipe(message, &iter);
Wez831ae412017-08-30 00:29:361660 ListenerThatExpectsOK::SendOK(sender());
morrita81b17e02015-02-06 00:58:301661 return true;
1662 }
morrita81b17e02015-02-06 00:58:301663};
1664
Wez831ae412017-08-30 00:29:361665TEST_F(IPCChannelMojoTest, SendPlatformFileAndMessagePipe) {
1666 Init("IPCChannelMojoTestSendPlatformFileAndMessagePipeClient");
morrita81b17e02015-02-06 00:58:301667
Wez831ae412017-08-30 00:29:361668 base::RunLoop run_loop;
1669 ListenerThatExpectsOK listener(run_loop.QuitClosure());
morrita81b17e02015-02-06 00:58:301670 CreateChannel(&listener);
1671 ASSERT_TRUE(ConnectChannel());
morrita81b17e02015-02-06 00:58:301672
amistry20e2b1d62016-06-23 06:12:351673 base::ScopedTempDir temp_dir;
1674 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
vabr16e5f602016-09-15 18:14:001675 base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.GetPath()),
morrita81b17e02015-02-06 00:58:301676 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
1677 base::File::FLAG_READ);
1678 TestingMessagePipe pipe;
1679 HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe);
1680
Wez831ae412017-08-30 00:29:361681 run_loop.Run();
sammc57ed9f982016-03-10 06:28:351682 channel()->Close();
morrita81b17e02015-02-06 00:58:301683
1684 EXPECT_TRUE(WaitForClientShutdown());
1685 DestroyChannel();
1686}
1687
sammc57ed9f982016-03-10 06:28:351688DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
Wez831ae412017-08-30 00:29:361689 IPCChannelMojoTestSendPlatformFileAndMessagePipeClient) {
1690 base::RunLoop run_loop;
1691 ListenerThatExpectsFileAndMessagePipe listener(run_loop.QuitClosure());
sammc57ed9f982016-03-10 06:28:351692 Connect(&listener);
1693 listener.set_sender(channel());
morrita81b17e02015-02-06 00:58:301694
Wez831ae412017-08-30 00:29:361695 run_loop.Run();
morrita81b17e02015-02-06 00:58:301696
sammc57ed9f982016-03-10 06:28:351697 Close();
morrita81b17e02015-02-06 00:58:301698}
1699
Fabrice de Gans-Riberi894661c2018-05-24 18:43:221700#endif // defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]64860882014-08-04 23:44:171701
Sean McAllister82700412020-08-19 20:10:351702#if defined(OS_LINUX) || defined(OS_CHROMEOS)
morrita0bd20bd2015-02-25 20:11:271703
1704const base::ProcessId kMagicChildId = 54321;
1705
Wez831ae412017-08-30 00:29:361706class ListenerThatVerifiesPeerPid : public TestListenerBase {
morrita0bd20bd2015-02-25 20:11:271707 public:
Wezfdb98482018-07-12 01:21:391708 explicit ListenerThatVerifiesPeerPid(base::OnceClosure quit_closure)
1709 : TestListenerBase(std::move(quit_closure)) {}
Wez831ae412017-08-30 00:29:361710
tfarina10a5c062015-09-04 18:47:571711 void OnChannelConnected(int32_t peer_pid) override {
morrita0bd20bd2015-02-25 20:11:271712 EXPECT_EQ(peer_pid, kMagicChildId);
Wezfdb98482018-07-12 01:21:391713 RunQuitClosure();
morrita0bd20bd2015-02-25 20:11:271714 }
1715
1716 bool OnMessageReceived(const IPC::Message& message) override {
1717 NOTREACHED();
1718 return true;
1719 }
1720};
1721
sammc57ed9f982016-03-10 06:28:351722TEST_F(IPCChannelMojoTest, VerifyGlobalPid) {
sammc4bcc4ed62016-10-27 10:13:591723 Init("IPCChannelMojoTestVerifyGlobalPidClient");
morrita0bd20bd2015-02-25 20:11:271724
Wez831ae412017-08-30 00:29:361725 base::RunLoop run_loop;
1726 ListenerThatVerifiesPeerPid listener(run_loop.QuitClosure());
morrita0bd20bd2015-02-25 20:11:271727 CreateChannel(&listener);
1728 ASSERT_TRUE(ConnectChannel());
morrita0bd20bd2015-02-25 20:11:271729
Wez831ae412017-08-30 00:29:361730 run_loop.Run();
rockotcbca72f2015-03-03 16:31:041731 channel()->Close();
morrita0bd20bd2015-02-25 20:11:271732
1733 EXPECT_TRUE(WaitForClientShutdown());
1734 DestroyChannel();
1735}
1736
sammc4bcc4ed62016-10-27 10:13:591737DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestVerifyGlobalPidClient) {
morrita0bd20bd2015-02-25 20:11:271738 IPC::Channel::SetGlobalPid(kMagicChildId);
Wez831ae412017-08-30 00:29:361739
1740 base::RunLoop run_loop;
1741 ListenerThatQuits listener(run_loop.QuitClosure());
sammc57ed9f982016-03-10 06:28:351742 Connect(&listener);
morrita0bd20bd2015-02-25 20:11:271743
Wez831ae412017-08-30 00:29:361744 run_loop.Run();
morrita0bd20bd2015-02-25 20:11:271745
sammc57ed9f982016-03-10 06:28:351746 Close();
morrita0bd20bd2015-02-25 20:11:271747}
1748
Sean McAllister82700412020-08-19 20:10:351749#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
morrita0bd20bd2015-02-25 20:11:271750
[email protected]64860882014-08-04 23:44:171751} // namespace