blob: 3387a210199a00c6faf7336c90a0b0e78cac6294 [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"
rockot9abe09b2016-08-02 20:57:3430#include "base/synchronization/waitable_event.h"
Patrick Monette643cdf62021-10-15 19:13:4231#include "base/task/single_thread_task_runner.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;
Ken Rockot493a59f32021-06-04 22:16:50983 proxy()->GetRemoteAssociatedInterface(
984 driver.BindNewEndpointAndPassReceiver());
rockot8d890f62016-07-14 16:37:14985 for (int i = 0; i < ListenerWithSimpleProxyAssociatedInterface::kNumMessages;
986 ++i) {
rockot9abe09b2016-08-02 20:57:34987 driver->ExpectValue(i);
988 SendValue(proxy(), i);
rockot8d890f62016-07-14 16:37:14989 }
Wezfdb98482018-07-12 01:21:39990 base::RunLoop run_loop;
991 driver->RequestQuit(run_loop.QuitClosure());
992 run_loop.Run();
rockot0e4de5f2016-07-22 21:18:07993
994 DestroyProxy();
rockot8d890f62016-07-14 16:37:14995}
996
rockot401fb2c2016-09-06 18:35:57997class ListenerWithIndirectProxyAssociatedInterface
998 : public IPC::Listener,
999 public IPC::mojom::IndirectTestDriver,
1000 public IPC::mojom::PingReceiver {
1001 public:
Julie Jeongeun Kimac82819f2019-11-20 00:01:271002 ListenerWithIndirectProxyAssociatedInterface() = default;
Chris Watkins2d879af2017-11-30 02:11:591003 ~ListenerWithIndirectProxyAssociatedInterface() override = default;
rockot401fb2c2016-09-06 18:35:571004
rockot70bbb59492017-01-25 00:56:511005 // IPC::Listener:
rockot401fb2c2016-09-06 18:35:571006 bool OnMessageReceived(const IPC::Message& message) override { return true; }
1007
rockot70bbb59492017-01-25 00:56:511008 void OnAssociatedInterfaceRequest(
1009 const std::string& interface_name,
1010 mojo::ScopedInterfaceEndpointHandle handle) override {
Julie Jeongeun Kimac82819f2019-11-20 00:01:271011 DCHECK(!driver_receiver_.is_bound());
rockot70bbb59492017-01-25 00:56:511012 DCHECK_EQ(interface_name, IPC::mojom::IndirectTestDriver::Name_);
Julie Jeongeun Kimac82819f2019-11-20 00:01:271013 driver_receiver_.Bind(
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:541014 mojo::PendingAssociatedReceiver<IPC::mojom::IndirectTestDriver>(
1015 std::move(handle)));
rockot401fb2c2016-09-06 18:35:571016 }
1017
Wezfdb98482018-07-12 01:21:391018 void set_ping_handler(const base::RepeatingClosure& handler) {
rockot401fb2c2016-09-06 18:35:571019 ping_handler_ = handler;
1020 }
1021
1022 private:
1023 // IPC::mojom::IndirectTestDriver:
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491024 void GetPingReceiver(mojo::PendingAssociatedReceiver<IPC::mojom::PingReceiver>
1025 receiver) override {
1026 ping_receiver_receiver_.Bind(std::move(receiver));
rockot401fb2c2016-09-06 18:35:571027 }
1028
1029 // IPC::mojom::PingReceiver:
tzikdd76ce712017-06-08 05:27:041030 void Ping(PingCallback callback) override {
1031 std::move(callback).Run();
rockot401fb2c2016-09-06 18:35:571032 ping_handler_.Run();
1033 }
1034
Julie Jeongeun Kimac82819f2019-11-20 00:01:271035 mojo::AssociatedReceiver<IPC::mojom::IndirectTestDriver> driver_receiver_{
1036 this};
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491037 mojo::AssociatedReceiver<IPC::mojom::PingReceiver> ping_receiver_receiver_{
1038 this};
rockot401fb2c2016-09-06 18:35:571039
Wezfdb98482018-07-12 01:21:391040 base::RepeatingClosure ping_handler_;
rockot401fb2c2016-09-06 18:35:571041};
1042
1043TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterfaceIndirect) {
1044 // Tests that we can pipeline interface requests and subsequent messages
1045 // targeting proxy thread bindings, and the channel will still dispatch
1046 // messages appropriately.
1047
sammc4bcc4ed62016-10-27 10:13:591048 Init("ProxyThreadAssociatedInterfaceIndirectClient");
rockot401fb2c2016-09-06 18:35:571049
1050 ListenerWithIndirectProxyAssociatedInterface listener;
1051 CreateProxy(&listener);
rockot401fb2c2016-09-06 18:35:571052 RunProxy();
1053
1054 base::RunLoop loop;
1055 listener.set_ping_handler(loop.QuitClosure());
1056 loop.Run();
1057
1058 EXPECT_TRUE(WaitForClientShutdown());
1059
1060 DestroyProxy();
1061}
1062
sammc4bcc4ed62016-10-27 10:13:591063DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(
rockot401fb2c2016-09-06 18:35:571064 ProxyThreadAssociatedInterfaceIndirectClient,
1065 ChannelProxyClient) {
1066 DummyListener listener;
1067 CreateProxy(&listener);
1068 RunProxy();
1069
1070 // Use an interface requested via another interface. On the remote end both
1071 // interfaces are bound on the proxy thread. This ensures that the Ping
1072 // message we send will still be dispatched properly even though the remote
1073 // endpoint may not have been bound yet by the time the message is initially
1074 // processed on the IO thread.
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:541075 mojo::AssociatedRemote<IPC::mojom::IndirectTestDriver> driver;
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491076 mojo::AssociatedRemote<IPC::mojom::PingReceiver> ping_receiver;
Ken Rockot493a59f32021-06-04 22:16:501077 proxy()->GetRemoteAssociatedInterface(
1078 driver.BindNewEndpointAndPassReceiver());
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491079 driver->GetPingReceiver(ping_receiver.BindNewEndpointAndPassReceiver());
rockot401fb2c2016-09-06 18:35:571080
1081 base::RunLoop loop;
1082 ping_receiver->Ping(loop.QuitClosure());
1083 loop.Run();
1084
1085 DestroyProxy();
1086}
1087
rockot9abe09b2016-08-02 20:57:341088class ListenerWithSyncAssociatedInterface
1089 : public IPC::Listener,
1090 public IPC::mojom::SimpleTestDriver {
1091 public:
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151092 ListenerWithSyncAssociatedInterface() = default;
Chris Watkins2d879af2017-11-30 02:11:591093 ~ListenerWithSyncAssociatedInterface() override = default;
rockot9abe09b2016-08-02 20:57:341094
1095 void set_sync_sender(IPC::Sender* sync_sender) { sync_sender_ = sync_sender; }
1096
rockot9abe09b2016-08-02 20:57:341097 void RunUntilQuitRequested() {
1098 base::RunLoop loop;
1099 quit_closure_ = loop.QuitClosure();
1100 loop.Run();
1101 }
1102
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151103 void CloseBinding() { receiver_.reset(); }
rockot9abe09b2016-08-02 20:57:341104
1105 void set_response_value(int32_t response) {
1106 response_value_ = response;
1107 }
1108
1109 private:
1110 // IPC::mojom::SimpleTestDriver:
1111 void ExpectValue(int32_t value) override {
1112 next_expected_value_ = value;
1113 }
1114
tzikdd76ce712017-06-08 05:27:041115 void GetExpectedValue(GetExpectedValueCallback callback) override {
1116 std::move(callback).Run(next_expected_value_);
rockot9abe09b2016-08-02 20:57:341117 }
1118
tzikdd76ce712017-06-08 05:27:041119 void RequestValue(RequestValueCallback callback) override {
1120 std::move(callback).Run(response_value_);
rockot9abe09b2016-08-02 20:57:341121 }
1122
tzikdd76ce712017-06-08 05:27:041123 void RequestQuit(RequestQuitCallback callback) override {
Wezfdb98482018-07-12 01:21:391124 std::move(quit_closure_).Run();
tzikdd76ce712017-06-08 05:27:041125 std::move(callback).Run();
rockot9abe09b2016-08-02 20:57:341126 }
1127
1128 // IPC::Listener:
1129 bool OnMessageReceived(const IPC::Message& message) override {
1130 EXPECT_EQ(0u, message.type());
1131 EXPECT_TRUE(message.is_sync());
1132 EXPECT_TRUE(message.should_unblock());
1133 std::unique_ptr<IPC::Message> reply(
1134 IPC::SyncMessage::GenerateReply(&message));
1135 reply->WriteInt(response_value_);
1136 DCHECK(sync_sender_);
1137 EXPECT_TRUE(sync_sender_->Send(reply.release()));
1138 return true;
1139 }
1140
rockot70bbb59492017-01-25 00:56:511141 void OnAssociatedInterfaceRequest(
1142 const std::string& interface_name,
1143 mojo::ScopedInterfaceEndpointHandle handle) override {
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151144 DCHECK(!receiver_.is_bound());
rockot70bbb59492017-01-25 00:56:511145 DCHECK_EQ(interface_name, IPC::mojom::SimpleTestDriver::Name_);
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151146 receiver_.Bind(
1147 mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver>(
1148 std::move(handle)));
rockot70bbb59492017-01-25 00:56:511149 }
1150
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151151 void BindReceiver(
1152 mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver) {
1153 DCHECK(!receiver_.is_bound());
1154 receiver_.Bind(std::move(receiver));
rockot9abe09b2016-08-02 20:57:341155 }
1156
1157 IPC::Sender* sync_sender_ = nullptr;
1158 int32_t next_expected_value_ = 0;
1159 int32_t response_value_ = 0;
Wezfdb98482018-07-12 01:21:391160 base::OnceClosure quit_closure_;
rockot9abe09b2016-08-02 20:57:341161
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151162 mojo::AssociatedReceiver<IPC::mojom::SimpleTestDriver> receiver_{this};
rockot9abe09b2016-08-02 20:57:341163};
1164
1165class SyncReplyReader : public IPC::MessageReplyDeserializer {
1166 public:
1167 explicit SyncReplyReader(int32_t* storage) : storage_(storage) {}
Peter Boströmc68c5aa2021-09-28 00:28:001168
1169 SyncReplyReader(const SyncReplyReader&) = delete;
1170 SyncReplyReader& operator=(const SyncReplyReader&) = delete;
1171
Chris Watkins2d879af2017-11-30 02:11:591172 ~SyncReplyReader() override = default;
rockot9abe09b2016-08-02 20:57:341173
1174 private:
1175 // IPC::MessageReplyDeserializer:
1176 bool SerializeOutputParameters(const IPC::Message& message,
1177 base::PickleIterator iter) override {
1178 if (!iter.ReadInt(storage_))
1179 return false;
1180 return true;
1181 }
1182
1183 int32_t* storage_;
rockot9abe09b2016-08-02 20:57:341184};
1185
1186TEST_F(IPCChannelProxyMojoTest, SyncAssociatedInterface) {
sammc4bcc4ed62016-10-27 10:13:591187 Init("SyncAssociatedInterface");
rockot9abe09b2016-08-02 20:57:341188
1189 ListenerWithSyncAssociatedInterface listener;
1190 CreateProxy(&listener);
1191 listener.set_sync_sender(proxy());
rockot9abe09b2016-08-02 20:57:341192 RunProxy();
1193
1194 // Run the client's simple sanity check to completion.
1195 listener.RunUntilQuitRequested();
1196
1197 // Verify that we can send a sync IPC and service an incoming sync request
1198 // while waiting on it
1199 listener.set_response_value(42);
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151200 mojo::AssociatedRemote<IPC::mojom::SimpleTestClient> client;
Ken Rockot493a59f32021-06-04 22:16:501201 proxy()->GetRemoteAssociatedInterface(
1202 client.BindNewEndpointAndPassReceiver());
rockot9abe09b2016-08-02 20:57:341203 int32_t received_value;
1204 EXPECT_TRUE(client->RequestValue(&received_value));
1205 EXPECT_EQ(42, received_value);
1206
1207 // Do it again. This time the client will send a classical sync IPC to us
1208 // while we wait.
1209 received_value = 0;
1210 EXPECT_TRUE(client->RequestValue(&received_value));
1211 EXPECT_EQ(42, received_value);
1212
1213 // Now make a classical sync IPC request to the client. It will send a
1214 // sync associated interface message to us while we wait.
1215 received_value = 0;
1216 std::unique_ptr<IPC::SyncMessage> request(
1217 new IPC::SyncMessage(0, 0, IPC::Message::PRIORITY_NORMAL,
1218 new SyncReplyReader(&received_value)));
1219 EXPECT_TRUE(proxy()->Send(request.release()));
1220 EXPECT_EQ(42, received_value);
1221
1222 listener.CloseBinding();
1223 EXPECT_TRUE(WaitForClientShutdown());
1224
1225 DestroyProxy();
1226}
1227
1228class SimpleTestClientImpl : public IPC::mojom::SimpleTestClient,
1229 public IPC::Listener {
1230 public:
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151231 SimpleTestClientImpl() = default;
Peter Boströmc68c5aa2021-09-28 00:28:001232
1233 SimpleTestClientImpl(const SimpleTestClientImpl&) = delete;
1234 SimpleTestClientImpl& operator=(const SimpleTestClientImpl&) = delete;
1235
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151236 ~SimpleTestClientImpl() override = default;
rockot9abe09b2016-08-02 20:57:341237
1238 void set_driver(IPC::mojom::SimpleTestDriver* driver) { driver_ = driver; }
1239 void set_sync_sender(IPC::Sender* sync_sender) { sync_sender_ = sync_sender; }
1240
rockot9abe09b2016-08-02 20:57:341241 void WaitForValueRequest() {
Peter Boströmfb60ea02021-04-05 21:06:121242 run_loop_ = std::make_unique<base::RunLoop>();
rockot9abe09b2016-08-02 20:57:341243 run_loop_->Run();
1244 }
1245
1246 void UseSyncSenderForRequest(bool use_sync_sender) {
1247 use_sync_sender_ = use_sync_sender;
1248 }
1249
1250 private:
1251 // IPC::mojom::SimpleTestClient:
tzikdd76ce712017-06-08 05:27:041252 void RequestValue(RequestValueCallback callback) override {
rockot9abe09b2016-08-02 20:57:341253 int32_t response = 0;
1254 if (use_sync_sender_) {
1255 std::unique_ptr<IPC::SyncMessage> reply(new IPC::SyncMessage(
1256 0, 0, IPC::Message::PRIORITY_NORMAL, new SyncReplyReader(&response)));
1257 EXPECT_TRUE(sync_sender_->Send(reply.release()));
1258 } else {
1259 DCHECK(driver_);
1260 EXPECT_TRUE(driver_->RequestValue(&response));
1261 }
1262
tzikdd76ce712017-06-08 05:27:041263 std::move(callback).Run(response);
rockot9abe09b2016-08-02 20:57:341264
1265 DCHECK(run_loop_);
1266 run_loop_->Quit();
1267 }
1268
1269 // IPC::Listener:
1270 bool OnMessageReceived(const IPC::Message& message) override {
1271 int32_t response;
1272 DCHECK(driver_);
1273 EXPECT_TRUE(driver_->RequestValue(&response));
1274 std::unique_ptr<IPC::Message> reply(
1275 IPC::SyncMessage::GenerateReply(&message));
1276 reply->WriteInt(response);
1277 EXPECT_TRUE(sync_sender_->Send(reply.release()));
1278
1279 DCHECK(run_loop_);
1280 run_loop_->Quit();
1281 return true;
1282 }
1283
rockot70bbb59492017-01-25 00:56:511284 void OnAssociatedInterfaceRequest(
1285 const std::string& interface_name,
1286 mojo::ScopedInterfaceEndpointHandle handle) override {
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151287 DCHECK(!receiver_.is_bound());
rockot70bbb59492017-01-25 00:56:511288 DCHECK_EQ(interface_name, IPC::mojom::SimpleTestClient::Name_);
1289
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151290 receiver_.Bind(
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491291 mojo::PendingAssociatedReceiver<IPC::mojom::SimpleTestClient>(
1292 std::move(handle)));
rockot70bbb59492017-01-25 00:56:511293 }
1294
rockot9abe09b2016-08-02 20:57:341295 bool use_sync_sender_ = false;
Julie Jeongeun Kim761a8ff2019-09-27 01:56:491296 mojo::AssociatedReceiver<IPC::mojom::SimpleTestClient> receiver_{this};
rockot9abe09b2016-08-02 20:57:341297 IPC::Sender* sync_sender_ = nullptr;
1298 IPC::mojom::SimpleTestDriver* driver_ = nullptr;
1299 std::unique_ptr<base::RunLoop> run_loop_;
rockot9abe09b2016-08-02 20:57:341300};
1301
sammc4bcc4ed62016-10-27 10:13:591302DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(SyncAssociatedInterface,
1303 ChannelProxyClient) {
rockot9abe09b2016-08-02 20:57:341304 SimpleTestClientImpl client_impl;
1305 CreateProxy(&client_impl);
1306 client_impl.set_sync_sender(proxy());
rockot9abe09b2016-08-02 20:57:341307 RunProxy();
1308
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151309 mojo::AssociatedRemote<IPC::mojom::SimpleTestDriver> driver;
Ken Rockot493a59f32021-06-04 22:16:501310 proxy()->GetRemoteAssociatedInterface(
1311 driver.BindNewEndpointAndPassReceiver());
rockot9abe09b2016-08-02 20:57:341312 client_impl.set_driver(driver.get());
1313
1314 // Simple sync message sanity check.
1315 driver->ExpectValue(42);
1316 int32_t expected_value = 0;
1317 EXPECT_TRUE(driver->GetExpectedValue(&expected_value));
1318 EXPECT_EQ(42, expected_value);
1319 RequestQuitAndWaitForAck(driver.get());
1320
1321 // Wait for the test driver to perform a sync call test with our own sync
1322 // associated interface message nested inside.
1323 client_impl.UseSyncSenderForRequest(false);
1324 client_impl.WaitForValueRequest();
1325
1326 // Wait for the test driver to perform a sync call test with our own classical
1327 // sync IPC nested inside.
1328 client_impl.UseSyncSenderForRequest(true);
1329 client_impl.WaitForValueRequest();
1330
1331 // Wait for the test driver to perform a classical sync IPC request, with our
1332 // own sync associated interface message nested inside.
1333 client_impl.UseSyncSenderForRequest(false);
1334 client_impl.WaitForValueRequest();
1335
1336 DestroyProxy();
1337}
1338
rockot10188752016-09-08 18:24:561339TEST_F(IPCChannelProxyMojoTest, Pause) {
1340 // Ensures that pausing a channel elicits the expected behavior when sending
1341 // messages, unpausing, sending more messages, and then manually flushing.
1342 // Specifically a sequence like:
rockot401fb2c2016-09-06 18:35:571343 //
1344 // Connect()
1345 // Send(A)
rockot10188752016-09-08 18:24:561346 // Pause()
rockot401fb2c2016-09-06 18:35:571347 // Send(B)
rockot401fb2c2016-09-06 18:35:571348 // Send(C)
rockot10188752016-09-08 18:24:561349 // Unpause(false)
1350 // Send(D)
1351 // Send(E)
rockot401fb2c2016-09-06 18:35:571352 // Flush()
1353 //
rockot10188752016-09-08 18:24:561354 // must result in the other end receiving messages A, D, E, B, D; in that
rockot401fb2c2016-09-06 18:35:571355 // order.
1356 //
1357 // This behavior is required by some consumers of IPC::Channel, and it is not
1358 // sufficient to leave this up to the consumer to implement since associated
1359 // interface requests and messages also need to be queued according to the
1360 // same policy.
sammc4bcc4ed62016-10-27 10:13:591361 Init("CreatePausedClient");
rockot401fb2c2016-09-06 18:35:571362
1363 DummyListener listener;
1364 CreateProxy(&listener);
rockot10188752016-09-08 18:24:561365 RunProxy();
1366
1367 // This message must be sent immediately since the channel is unpaused.
1368 SendValue(proxy(), 1);
1369
1370 proxy()->Pause();
rockot401fb2c2016-09-06 18:35:571371
1372 // These messages must be queued internally since the channel is paused.
rockot401fb2c2016-09-06 18:35:571373 SendValue(proxy(), 2);
rockot10188752016-09-08 18:24:561374 SendValue(proxy(), 3);
rockot401fb2c2016-09-06 18:35:571375
1376 proxy()->Unpause(false /* flush */);
1377
1378 // These messages must be sent immediately since the channel is unpaused.
rockot401fb2c2016-09-06 18:35:571379 SendValue(proxy(), 4);
rockot10188752016-09-08 18:24:561380 SendValue(proxy(), 5);
rockot401fb2c2016-09-06 18:35:571381
1382 // Now we flush the previously queued messages.
1383 proxy()->Flush();
1384
1385 EXPECT_TRUE(WaitForClientShutdown());
1386 DestroyProxy();
1387}
1388
1389class ExpectValueSequenceListener : public IPC::Listener {
1390 public:
Wezfdb98482018-07-12 01:21:391391 ExpectValueSequenceListener(base::queue<int32_t>* expected_values,
1392 base::OnceClosure quit_closure)
1393 : expected_values_(expected_values),
1394 quit_closure_(std::move(quit_closure)) {}
Peter Boströmc68c5aa2021-09-28 00:28:001395
1396 ExpectValueSequenceListener(const ExpectValueSequenceListener&) = delete;
1397 ExpectValueSequenceListener& operator=(const ExpectValueSequenceListener&) =
1398 delete;
1399
Chris Watkins2d879af2017-11-30 02:11:591400 ~ExpectValueSequenceListener() override = default;
rockot401fb2c2016-09-06 18:35:571401
1402 // IPC::Listener:
1403 bool OnMessageReceived(const IPC::Message& message) override {
1404 DCHECK(!expected_values_->empty());
1405 base::PickleIterator iter(message);
1406 int32_t should_be_expected;
1407 EXPECT_TRUE(iter.ReadInt(&should_be_expected));
1408 EXPECT_EQ(expected_values_->front(), should_be_expected);
1409 expected_values_->pop();
1410 if (expected_values_->empty())
Wezfdb98482018-07-12 01:21:391411 std::move(quit_closure_).Run();
rockot401fb2c2016-09-06 18:35:571412 return true;
1413 }
1414
1415 private:
Brett Wilsona62d9c02017-09-20 20:53:201416 base::queue<int32_t>* expected_values_;
Wezfdb98482018-07-12 01:21:391417 base::OnceClosure quit_closure_;
rockot401fb2c2016-09-06 18:35:571418};
1419
sammc4bcc4ed62016-10-27 10:13:591420DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(CreatePausedClient,
1421 ChannelProxyClient) {
Brett Wilsona62d9c02017-09-20 20:53:201422 base::queue<int32_t> expected_values;
Wezfdb98482018-07-12 01:21:391423 base::RunLoop run_loop;
1424 ExpectValueSequenceListener listener(&expected_values,
1425 run_loop.QuitClosure());
rockot401fb2c2016-09-06 18:35:571426 CreateProxy(&listener);
rockot401fb2c2016-09-06 18:35:571427 expected_values.push(1);
rockot10188752016-09-08 18:24:561428 expected_values.push(4);
1429 expected_values.push(5);
rockot401fb2c2016-09-06 18:35:571430 expected_values.push(2);
rockot10188752016-09-08 18:24:561431 expected_values.push(3);
rockot401fb2c2016-09-06 18:35:571432 RunProxy();
Wezfdb98482018-07-12 01:21:391433 run_loop.Run();
rockot401fb2c2016-09-06 18:35:571434 EXPECT_TRUE(expected_values.empty());
1435 DestroyProxy();
1436}
1437
sammc5c8a6c62017-02-04 01:33:381438TEST_F(IPCChannelProxyMojoTest, AssociatedRequestClose) {
1439 Init("DropAssociatedRequest");
1440
1441 DummyListener listener;
1442 CreateProxy(&listener);
1443 RunProxy();
1444
Julie Jeongeun Kim6eb409ed2019-09-28 01:27:541445 mojo::AssociatedRemote<IPC::mojom::AssociatedInterfaceVendor> vendor;
Ken Rockot493a59f32021-06-04 22:16:501446 proxy()->GetRemoteAssociatedInterface(
1447 vendor.BindNewEndpointAndPassReceiver());
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151448 mojo::AssociatedRemote<IPC::mojom::SimpleTestDriver> tester;
1449 vendor->GetTestInterface(tester.BindNewEndpointAndPassReceiver());
sammc5c8a6c62017-02-04 01:33:381450 base::RunLoop run_loop;
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151451 tester.set_disconnect_handler(run_loop.QuitClosure());
sammc5c8a6c62017-02-04 01:33:381452 run_loop.Run();
1453
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:151454 tester.reset();
Ken Rockot493a59f32021-06-04 22:16:501455 proxy()->GetRemoteAssociatedInterface(
1456 tester.BindNewEndpointAndPassReceiver());
sammc5c8a6c62017-02-04 01:33:381457 EXPECT_TRUE(WaitForClientShutdown());
1458 DestroyProxy();
1459}
1460
1461class AssociatedInterfaceDroppingListener : public IPC::Listener {
1462 public:
Wezfdb98482018-07-12 01:21:391463 AssociatedInterfaceDroppingListener(base::OnceClosure callback)
1464 : callback_(std::move(callback)) {}
sammc5c8a6c62017-02-04 01:33:381465 bool OnMessageReceived(const IPC::Message& message) override { return false; }
1466
1467 void OnAssociatedInterfaceRequest(
1468 const std::string& interface_name,
1469 mojo::ScopedInterfaceEndpointHandle handle) override {
1470 if (interface_name == IPC::mojom::SimpleTestDriver::Name_)
Wezfdb98482018-07-12 01:21:391471 std::move(callback_).Run();
sammc5c8a6c62017-02-04 01:33:381472 }
1473
1474 private:
Wezfdb98482018-07-12 01:21:391475 base::OnceClosure callback_;
sammc5c8a6c62017-02-04 01:33:381476};
1477
1478DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(DropAssociatedRequest,
1479 ChannelProxyClient) {
1480 base::RunLoop run_loop;
1481 AssociatedInterfaceDroppingListener listener(run_loop.QuitClosure());
1482 CreateProxy(&listener);
1483 RunProxy();
1484 run_loop.Run();
1485 DestroyProxy();
1486}
1487
Avi Drissman2b97d032020-07-30 03:02:261488#if !defined(OS_APPLE)
Wez831ae412017-08-30 00:29:361489// TODO(wez): On Mac we need to set up a MachPortBroker before we can transfer
1490// Mach ports (which underpin Sharedmemory on Mac) across IPC.
1491
Alexandr Ilind497eee2018-04-19 22:50:541492template <class SharedMemoryRegionType>
1493class IPCChannelMojoSharedMemoryRegionTypedTest : public IPCChannelMojoTest {};
1494
1495struct WritableRegionTraits {
1496 using RegionType = base::WritableSharedMemoryRegion;
1497 static const char kClientName[];
1498};
1499const char WritableRegionTraits::kClientName[] =
1500 "IPCChannelMojoTestSendWritableSharedMemoryRegionClient";
1501struct UnsafeRegionTraits {
1502 using RegionType = base::UnsafeSharedMemoryRegion;
1503 static const char kClientName[];
1504};
1505const char UnsafeRegionTraits::kClientName[] =
1506 "IPCChannelMojoTestSendUnsafeSharedMemoryRegionClient";
1507struct ReadOnlyRegionTraits {
1508 using RegionType = base::ReadOnlySharedMemoryRegion;
1509 static const char kClientName[];
1510};
1511const char ReadOnlyRegionTraits::kClientName[] =
1512 "IPCChannelMojoTestSendReadOnlySharedMemoryRegionClient";
1513
1514typedef ::testing::
1515 Types<WritableRegionTraits, UnsafeRegionTraits, ReadOnlyRegionTraits>
1516 AllSharedMemoryRegionTraits;
Victor Costanebc52732019-02-15 02:39:471517TYPED_TEST_SUITE(IPCChannelMojoSharedMemoryRegionTypedTest,
1518 AllSharedMemoryRegionTraits);
Alexandr Ilind497eee2018-04-19 22:50:541519
1520template <class SharedMemoryRegionType>
1521class ListenerThatExpectsSharedMemoryRegion : public TestListenerBase {
1522 public:
Wezfdb98482018-07-12 01:21:391523 explicit ListenerThatExpectsSharedMemoryRegion(base::OnceClosure quit_closure)
Alexandr Ilind497eee2018-04-19 22:50:541524 : TestListenerBase(std::move(quit_closure)) {}
1525
1526 bool OnMessageReceived(const IPC::Message& message) override {
1527 base::PickleIterator iter(message);
1528
1529 SharedMemoryRegionType region;
1530 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &region));
1531 EXPECT_TRUE(region.IsValid());
1532
1533 // Verify the shared memory region has expected content.
1534 typename SharedMemoryRegionType::MappingType mapping = region.Map();
1535 std::string content = HandleSendingHelper::GetSendingFileContent();
1536 EXPECT_EQ(0, memcmp(mapping.memory(), content.data(), content.size()));
1537
1538 ListenerThatExpectsOK::SendOK(sender());
1539 return true;
1540 }
1541};
1542
1543TYPED_TEST(IPCChannelMojoSharedMemoryRegionTypedTest, Send) {
1544 this->Init(TypeParam::kClientName);
1545
1546 const size_t size = 1004;
1547 typename TypeParam::RegionType region;
1548 base::WritableSharedMemoryMapping mapping;
1549 std::tie(region, mapping) =
1550 base::CreateMappedRegion<typename TypeParam::RegionType>(size);
1551
1552 std::string content = HandleSendingHelper::GetSendingFileContent();
1553 memcpy(mapping.memory(), content.data(), content.size());
1554
1555 // Create a success listener, and launch the child process.
1556 base::RunLoop run_loop;
1557 ListenerThatExpectsOK listener(run_loop.QuitClosure());
1558 this->CreateChannel(&listener);
1559 ASSERT_TRUE(this->ConnectChannel());
1560
1561 // Send the child process an IPC with |shmem| attached, to verify
1562 // that is is correctly wrapped, transferred and unwrapped.
1563 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
1564 IPC::WriteParam(message, region);
1565 ASSERT_TRUE(this->channel()->Send(message));
1566
1567 run_loop.Run();
1568
1569 this->channel()->Close();
1570
1571 EXPECT_TRUE(this->WaitForClientShutdown());
1572 EXPECT_FALSE(region.IsValid());
1573 this->DestroyChannel();
1574}
1575
1576DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
1577 IPCChannelMojoTestSendWritableSharedMemoryRegionClient) {
1578 base::RunLoop run_loop;
1579 ListenerThatExpectsSharedMemoryRegion<base::WritableSharedMemoryRegion>
1580 listener(run_loop.QuitClosure());
1581 Connect(&listener);
1582 listener.set_sender(channel());
1583
1584 run_loop.Run();
1585
1586 Close();
1587}
1588DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
1589 IPCChannelMojoTestSendUnsafeSharedMemoryRegionClient) {
1590 base::RunLoop run_loop;
1591 ListenerThatExpectsSharedMemoryRegion<base::UnsafeSharedMemoryRegion>
1592 listener(run_loop.QuitClosure());
1593 Connect(&listener);
1594 listener.set_sender(channel());
1595
1596 run_loop.Run();
1597
1598 Close();
1599}
1600DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
1601 IPCChannelMojoTestSendReadOnlySharedMemoryRegionClient) {
1602 base::RunLoop run_loop;
1603 ListenerThatExpectsSharedMemoryRegion<base::ReadOnlySharedMemoryRegion>
1604 listener(run_loop.QuitClosure());
1605 Connect(&listener);
1606 listener.set_sender(channel());
1607
1608 run_loop.Run();
1609
1610 Close();
1611}
Avi Drissman2b97d032020-07-30 03:02:261612#endif // !defined(OS_APPLE)
Wez831ae412017-08-30 00:29:361613
Fabrice de Gans-Riberi894661c2018-05-24 18:43:221614#if defined(OS_POSIX) || defined(OS_FUCHSIA)
rockot8d890f62016-07-14 16:37:141615
Wez831ae412017-08-30 00:29:361616class ListenerThatExpectsFile : public TestListenerBase {
[email protected]64860882014-08-04 23:44:171617 public:
Wezfdb98482018-07-12 01:21:391618 explicit ListenerThatExpectsFile(base::OnceClosure quit_closure)
1619 : TestListenerBase(std::move(quit_closure)) {}
[email protected]64860882014-08-04 23:44:171620
dchengfe61fca2014-10-22 02:29:521621 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:251622 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:301623 HandleSendingHelper::ReadReceivedFile(message, &iter);
Wez831ae412017-08-30 00:29:361624 ListenerThatExpectsOK::SendOK(sender());
[email protected]64860882014-08-04 23:44:171625 return true;
1626 }
[email protected]64860882014-08-04 23:44:171627};
1628
Wez831ae412017-08-30 00:29:361629TEST_F(IPCChannelMojoTest, SendPlatformFile) {
1630 Init("IPCChannelMojoTestSendPlatformFileClient");
[email protected]64860882014-08-04 23:44:171631
Wez831ae412017-08-30 00:29:361632 base::RunLoop run_loop;
1633 ListenerThatExpectsOK listener(run_loop.QuitClosure());
morrita373af03b2014-09-09 19:35:241634 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:171635 ASSERT_TRUE(ConnectChannel());
[email protected]64860882014-08-04 23:44:171636
amistry20e2b1d62016-06-23 06:12:351637 base::ScopedTempDir temp_dir;
1638 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
vabr16e5f602016-09-15 18:14:001639 base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.GetPath()),
[email protected]64860882014-08-04 23:44:171640 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
morrita81b17e02015-02-06 00:58:301641 base::File::FLAG_READ);
1642 HandleSendingHelper::WriteFileThenSend(channel(), file);
Wez831ae412017-08-30 00:29:361643 run_loop.Run();
[email protected]64860882014-08-04 23:44:171644
sammc57ed9f982016-03-10 06:28:351645 channel()->Close();
[email protected]64860882014-08-04 23:44:171646
1647 EXPECT_TRUE(WaitForClientShutdown());
1648 DestroyChannel();
1649}
1650
Wez831ae412017-08-30 00:29:361651DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestSendPlatformFileClient) {
1652 base::RunLoop run_loop;
1653 ListenerThatExpectsFile listener(run_loop.QuitClosure());
sammc57ed9f982016-03-10 06:28:351654 Connect(&listener);
1655 listener.set_sender(channel());
[email protected]64860882014-08-04 23:44:171656
Wez831ae412017-08-30 00:29:361657 run_loop.Run();
[email protected]64860882014-08-04 23:44:171658
sammc57ed9f982016-03-10 06:28:351659 Close();
[email protected]64860882014-08-04 23:44:171660}
morrita81b17e02015-02-06 00:58:301661
Wez831ae412017-08-30 00:29:361662class ListenerThatExpectsFileAndMessagePipe : public TestListenerBase {
morrita81b17e02015-02-06 00:58:301663 public:
Wezfdb98482018-07-12 01:21:391664 explicit ListenerThatExpectsFileAndMessagePipe(base::OnceClosure quit_closure)
1665 : TestListenerBase(std::move(quit_closure)) {}
morrita81b17e02015-02-06 00:58:301666
Chris Watkins2d879af2017-11-30 02:11:591667 ~ListenerThatExpectsFileAndMessagePipe() override = default;
morrita81b17e02015-02-06 00:58:301668
1669 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:251670 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:301671 HandleSendingHelper::ReadReceivedFile(message, &iter);
1672 HandleSendingHelper::ReadReceivedPipe(message, &iter);
Wez831ae412017-08-30 00:29:361673 ListenerThatExpectsOK::SendOK(sender());
morrita81b17e02015-02-06 00:58:301674 return true;
1675 }
morrita81b17e02015-02-06 00:58:301676};
1677
Wez831ae412017-08-30 00:29:361678TEST_F(IPCChannelMojoTest, SendPlatformFileAndMessagePipe) {
1679 Init("IPCChannelMojoTestSendPlatformFileAndMessagePipeClient");
morrita81b17e02015-02-06 00:58:301680
Wez831ae412017-08-30 00:29:361681 base::RunLoop run_loop;
1682 ListenerThatExpectsOK listener(run_loop.QuitClosure());
morrita81b17e02015-02-06 00:58:301683 CreateChannel(&listener);
1684 ASSERT_TRUE(ConnectChannel());
morrita81b17e02015-02-06 00:58:301685
amistry20e2b1d62016-06-23 06:12:351686 base::ScopedTempDir temp_dir;
1687 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
vabr16e5f602016-09-15 18:14:001688 base::File file(HandleSendingHelper::GetSendingFilePath(temp_dir.GetPath()),
morrita81b17e02015-02-06 00:58:301689 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
1690 base::File::FLAG_READ);
1691 TestingMessagePipe pipe;
1692 HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe);
1693
Wez831ae412017-08-30 00:29:361694 run_loop.Run();
sammc57ed9f982016-03-10 06:28:351695 channel()->Close();
morrita81b17e02015-02-06 00:58:301696
1697 EXPECT_TRUE(WaitForClientShutdown());
1698 DestroyChannel();
1699}
1700
sammc57ed9f982016-03-10 06:28:351701DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(
Wez831ae412017-08-30 00:29:361702 IPCChannelMojoTestSendPlatformFileAndMessagePipeClient) {
1703 base::RunLoop run_loop;
1704 ListenerThatExpectsFileAndMessagePipe listener(run_loop.QuitClosure());
sammc57ed9f982016-03-10 06:28:351705 Connect(&listener);
1706 listener.set_sender(channel());
morrita81b17e02015-02-06 00:58:301707
Wez831ae412017-08-30 00:29:361708 run_loop.Run();
morrita81b17e02015-02-06 00:58:301709
sammc57ed9f982016-03-10 06:28:351710 Close();
morrita81b17e02015-02-06 00:58:301711}
1712
Fabrice de Gans-Riberi894661c2018-05-24 18:43:221713#endif // defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]64860882014-08-04 23:44:171714
Sean McAllister82700412020-08-19 20:10:351715#if defined(OS_LINUX) || defined(OS_CHROMEOS)
morrita0bd20bd2015-02-25 20:11:271716
1717const base::ProcessId kMagicChildId = 54321;
1718
Wez831ae412017-08-30 00:29:361719class ListenerThatVerifiesPeerPid : public TestListenerBase {
morrita0bd20bd2015-02-25 20:11:271720 public:
Wezfdb98482018-07-12 01:21:391721 explicit ListenerThatVerifiesPeerPid(base::OnceClosure quit_closure)
1722 : TestListenerBase(std::move(quit_closure)) {}
Wez831ae412017-08-30 00:29:361723
tfarina10a5c062015-09-04 18:47:571724 void OnChannelConnected(int32_t peer_pid) override {
morrita0bd20bd2015-02-25 20:11:271725 EXPECT_EQ(peer_pid, kMagicChildId);
Wezfdb98482018-07-12 01:21:391726 RunQuitClosure();
morrita0bd20bd2015-02-25 20:11:271727 }
1728
1729 bool OnMessageReceived(const IPC::Message& message) override {
1730 NOTREACHED();
1731 return true;
1732 }
1733};
1734
sammc57ed9f982016-03-10 06:28:351735TEST_F(IPCChannelMojoTest, VerifyGlobalPid) {
sammc4bcc4ed62016-10-27 10:13:591736 Init("IPCChannelMojoTestVerifyGlobalPidClient");
morrita0bd20bd2015-02-25 20:11:271737
Wez831ae412017-08-30 00:29:361738 base::RunLoop run_loop;
1739 ListenerThatVerifiesPeerPid listener(run_loop.QuitClosure());
morrita0bd20bd2015-02-25 20:11:271740 CreateChannel(&listener);
1741 ASSERT_TRUE(ConnectChannel());
morrita0bd20bd2015-02-25 20:11:271742
Wez831ae412017-08-30 00:29:361743 run_loop.Run();
rockotcbca72f2015-03-03 16:31:041744 channel()->Close();
morrita0bd20bd2015-02-25 20:11:271745
1746 EXPECT_TRUE(WaitForClientShutdown());
1747 DestroyChannel();
1748}
1749
sammc4bcc4ed62016-10-27 10:13:591750DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(IPCChannelMojoTestVerifyGlobalPidClient) {
morrita0bd20bd2015-02-25 20:11:271751 IPC::Channel::SetGlobalPid(kMagicChildId);
Wez831ae412017-08-30 00:29:361752
1753 base::RunLoop run_loop;
1754 ListenerThatQuits listener(run_loop.QuitClosure());
sammc57ed9f982016-03-10 06:28:351755 Connect(&listener);
morrita0bd20bd2015-02-25 20:11:271756
Wez831ae412017-08-30 00:29:361757 run_loop.Run();
morrita0bd20bd2015-02-25 20:11:271758
sammc57ed9f982016-03-10 06:28:351759 Close();
morrita0bd20bd2015-02-25 20:11:271760}
1761
Sean McAllister82700412020-08-19 20:10:351762#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
morrita0bd20bd2015-02-25 20:11:271763
[email protected]64860882014-08-04 23:44:171764} // namespace