blob: e27ec5df61ffc16994c918eead2ca5654b76901a [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
5#include "ipc/mojo/ipc_channel_mojo.h"
6
avi246998d82015-12-22 02:39:047#include <stddef.h>
tfarina10a5c062015-09-04 18:47:578#include <stdint.h>
dchenge48600452015-12-28 02:24:509#include <utility>
tfarina10a5c062015-09-04 18:47:5710
[email protected]64860882014-08-04 23:44:1711#include "base/base_paths.h"
12#include "base/files/file.h"
skyostile687bdff2015-05-12 11:29:2113#include "base/location.h"
[email protected]64860882014-08-04 23:44:1714#include "base/path_service.h"
15#include "base/pickle.h"
rockotcbca72f2015-03-03 16:31:0416#include "base/run_loop.h"
skyostile687bdff2015-05-12 11:29:2117#include "base/single_thread_task_runner.h"
morrita0bd20bd2015-02-25 20:11:2718#include "base/test/test_timeouts.h"
skyostile687bdff2015-05-12 11:29:2119#include "base/thread_task_runner_handle.h"
[email protected]64860882014-08-04 23:44:1720#include "base/threading/thread.h"
avi246998d82015-12-22 02:39:0421#include "build/build_config.h"
[email protected]64860882014-08-04 23:44:1722#include "ipc/ipc_message.h"
23#include "ipc/ipc_test_base.h"
24#include "ipc/ipc_test_channel_listener.h"
morrita81b17e02015-02-06 00:58:3025#include "ipc/mojo/ipc_mojo_handle_attachment.h"
26#include "ipc/mojo/ipc_mojo_message_helper.h"
morrita438a2ee2015-04-03 05:28:2127#include "ipc/mojo/ipc_mojo_param_traits.h"
rockotcbca72f2015-03-03 16:31:0428#include "ipc/mojo/scoped_ipc_support.h"
[email protected]64860882014-08-04 23:44:1729
30#if defined(OS_POSIX)
31#include "base/file_descriptor_posix.h"
morrita1aa788c2015-01-31 05:45:4232#include "ipc/ipc_platform_file_attachment_posix.h"
[email protected]64860882014-08-04 23:44:1733#endif
34
35namespace {
36
37class ListenerThatExpectsOK : public IPC::Listener {
38 public:
[email protected]e5c27752014-08-08 21:45:1339 ListenerThatExpectsOK()
40 : received_ok_(false) {}
[email protected]64860882014-08-04 23:44:1741
dchengfe61fca2014-10-22 02:29:5242 ~ListenerThatExpectsOK() override {}
[email protected]64860882014-08-04 23:44:1743
dchengfe61fca2014-10-22 02:29:5244 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:2545 base::PickleIterator iter(message);
[email protected]64860882014-08-04 23:44:1746 std::string should_be_ok;
47 EXPECT_TRUE(iter.ReadString(&should_be_ok));
48 EXPECT_EQ(should_be_ok, "OK");
[email protected]e5c27752014-08-08 21:45:1349 received_ok_ = true;
ki.stfua21ed8c2015-10-12 17:26:0050 base::MessageLoop::current()->QuitWhenIdle();
[email protected]64860882014-08-04 23:44:1751 return true;
52 }
53
dchengfe61fca2014-10-22 02:29:5254 void OnChannelError() override {
[email protected]e5c27752014-08-08 21:45:1355 // The connection should be healthy while the listener is waiting
56 // message. An error can occur after that because the peer
57 // process dies.
58 DCHECK(received_ok_);
[email protected]64860882014-08-04 23:44:1759 }
60
61 static void SendOK(IPC::Sender* sender) {
62 IPC::Message* message = new IPC::Message(
63 0, 2, IPC::Message::PRIORITY_NORMAL);
64 message->WriteString(std::string("OK"));
65 ASSERT_TRUE(sender->Send(message));
66 }
[email protected]e5c27752014-08-08 21:45:1367
68 private:
69 bool received_ok_;
[email protected]64860882014-08-04 23:44:1770};
71
[email protected]64860882014-08-04 23:44:1772class ChannelClient {
73 public:
74 explicit ChannelClient(IPC::Listener* listener, const char* name) {
erikchen30dc2812015-09-24 03:26:3875 channel_ = IPC::ChannelMojo::Create(main_message_loop_.task_runner(),
76 IPCTestBase::GetChannelName(name),
77 IPC::Channel::MODE_CLIENT, listener);
[email protected]64860882014-08-04 23:44:1778 }
79
80 void Connect() {
81 CHECK(channel_->Connect());
82 }
83
rockotcbca72f2015-03-03 16:31:0484 void Close() {
85 channel_->Close();
86
87 base::RunLoop run_loop;
skyostile687bdff2015-05-12 11:29:2188 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
89 run_loop.QuitClosure());
rockotcbca72f2015-03-03 16:31:0490 run_loop.Run();
91 }
92
[email protected]64860882014-08-04 23:44:1793 IPC::ChannelMojo* channel() const { return channel_.get(); }
94
95 private:
[email protected]64860882014-08-04 23:44:1796 base::MessageLoopForIO main_message_loop_;
morrita54f6f80c2014-09-23 21:16:0097 scoped_ptr<IPC::ChannelMojo> channel_;
[email protected]64860882014-08-04 23:44:1798};
99
rockotcbca72f2015-03-03 16:31:04100class IPCChannelMojoTestBase : public IPCTestBase {
101 public:
102 void InitWithMojo(const std::string& test_client_name) {
103 Init(test_client_name);
rockotcbca72f2015-03-03 16:31:04104 }
105
106 void TearDown() override {
107 // Make sure Mojo IPC support is properly shutdown on the I/O loop before
108 // TearDown continues.
rockotcbca72f2015-03-03 16:31:04109 base::RunLoop run_loop;
110 task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure());
111 run_loop.Run();
112
113 IPCTestBase::TearDown();
114 }
rockotcbca72f2015-03-03 16:31:04115};
116
117class IPCChannelMojoTest : public IPCChannelMojoTestBase {
[email protected]64860882014-08-04 23:44:17118 protected:
dchengfe61fca2014-10-22 02:29:52119 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita373af03b2014-09-09 19:35:24120 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39121 base::SequencedTaskRunner* runner) override {
erikchen30dc2812015-09-24 03:26:38122 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle);
[email protected]64860882014-08-04 23:44:17123 }
morrita54f6f80c2014-09-23 21:16:00124
dchengfe61fca2014-10-22 02:29:52125 bool DidStartClient() override {
morrita54f6f80c2014-09-23 21:16:00126 bool ok = IPCTestBase::DidStartClient();
127 DCHECK(ok);
morrita54f6f80c2014-09-23 21:16:00128 return ok;
129 }
[email protected]64860882014-08-04 23:44:17130};
131
132
[email protected]64860882014-08-04 23:44:17133class TestChannelListenerWithExtraExpectations
134 : public IPC::TestChannelListener {
135 public:
136 TestChannelListenerWithExtraExpectations()
137 : is_connected_called_(false) {
138 }
139
tfarina10a5c062015-09-04 18:47:57140 void OnChannelConnected(int32_t peer_pid) override {
[email protected]64860882014-08-04 23:44:17141 IPC::TestChannelListener::OnChannelConnected(peer_pid);
142 EXPECT_TRUE(base::kNullProcessId != peer_pid);
143 is_connected_called_ = true;
144 }
145
146 bool is_connected_called() const { return is_connected_called_; }
147
148 private:
149 bool is_connected_called_;
150};
151
msw8f7f53902015-06-19 16:26:47152// Times out on Android; see http://crbug.com/502290
153#if defined(OS_ANDROID)
154#define MAYBE_ConnectedFromClient DISABLED_ConnectedFromClient
155#else
156#define MAYBE_ConnectedFromClient ConnectedFromClient
157#endif
158TEST_F(IPCChannelMojoTest, MAYBE_ConnectedFromClient) {
rockotcbca72f2015-03-03 16:31:04159 InitWithMojo("IPCChannelMojoTestClient");
[email protected]64860882014-08-04 23:44:17160
161 // Set up IPC channel and start client.
162 TestChannelListenerWithExtraExpectations listener;
morrita373af03b2014-09-09 19:35:24163 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17164 listener.Init(sender());
165 ASSERT_TRUE(ConnectChannel());
166 ASSERT_TRUE(StartClient());
167
168 IPC::TestChannelListener::SendOneMessage(
169 sender(), "hello from parent");
170
171 base::MessageLoop::current()->Run();
172 EXPECT_TRUE(base::kNullProcessId != this->channel()->GetPeerPID());
173
174 this->channel()->Close();
175
176 EXPECT_TRUE(WaitForClientShutdown());
177 EXPECT_TRUE(listener.is_connected_called());
178 EXPECT_TRUE(listener.HasSentAll());
179
180 DestroyChannel();
181}
182
183// A long running process that connects to us
184MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestClient) {
185 TestChannelListenerWithExtraExpectations listener;
186 ChannelClient client(&listener, "IPCChannelMojoTestClient");
187 client.Connect();
188 listener.Init(client.channel());
189
190 IPC::TestChannelListener::SendOneMessage(
191 client.channel(), "hello from child");
192 base::MessageLoop::current()->Run();
193 EXPECT_TRUE(listener.is_connected_called());
194 EXPECT_TRUE(listener.HasSentAll());
195
rockotcbca72f2015-03-03 16:31:04196 client.Close();
197
[email protected]64860882014-08-04 23:44:17198 return 0;
199}
200
morrita0a24cfc92014-09-16 03:20:48201class ListenerExpectingErrors : public IPC::Listener {
202 public:
203 ListenerExpectingErrors()
204 : has_error_(false) {
205 }
206
tfarina10a5c062015-09-04 18:47:57207 void OnChannelConnected(int32_t peer_pid) override {
ki.stfua21ed8c2015-10-12 17:26:00208 base::MessageLoop::current()->QuitWhenIdle();
morritabe6c4cc2014-09-24 23:38:44209 }
210
dchengfe61fca2014-10-22 02:29:52211 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita0a24cfc92014-09-16 03:20:48212
dchengfe61fca2014-10-22 02:29:52213 void OnChannelError() override {
morrita0a24cfc92014-09-16 03:20:48214 has_error_ = true;
ki.stfua21ed8c2015-10-12 17:26:00215 base::MessageLoop::current()->QuitWhenIdle();
morrita0a24cfc92014-09-16 03:20:48216 }
217
218 bool has_error() const { return has_error_; }
219
220 private:
221 bool has_error_;
222};
223
224
rockotcbca72f2015-03-03 16:31:04225class IPCChannelMojoErrorTest : public IPCChannelMojoTestBase {
morrita0a24cfc92014-09-16 03:20:48226 protected:
dchengfe61fca2014-10-22 02:29:52227 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita0a24cfc92014-09-16 03:20:48228 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39229 base::SequencedTaskRunner* runner) override {
erikchen30dc2812015-09-24 03:26:38230 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle);
morrita0a24cfc92014-09-16 03:20:48231 }
morrita54f6f80c2014-09-23 21:16:00232
dchengfe61fca2014-10-22 02:29:52233 bool DidStartClient() override {
morrita54f6f80c2014-09-23 21:16:00234 bool ok = IPCTestBase::DidStartClient();
235 DCHECK(ok);
morrita54f6f80c2014-09-23 21:16:00236 return ok;
237 }
morrita0a24cfc92014-09-16 03:20:48238};
239
240class ListenerThatQuits : public IPC::Listener {
241 public:
242 ListenerThatQuits() {
243 }
244
dchengfe61fca2014-10-22 02:29:52245 bool OnMessageReceived(const IPC::Message& message) override {
dchengf3076af2014-10-21 18:02:42246 return true;
247 }
morrita0a24cfc92014-09-16 03:20:48248
tfarina10a5c062015-09-04 18:47:57249 void OnChannelConnected(int32_t peer_pid) override {
ki.stfua21ed8c2015-10-12 17:26:00250 base::MessageLoop::current()->QuitWhenIdle();
morrita0a24cfc92014-09-16 03:20:48251 }
252};
253
254// A long running process that connects to us.
255MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) {
256 ListenerThatQuits listener;
257 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient");
258 client.Connect();
259
260 base::MessageLoop::current()->Run();
261
rockotcbca72f2015-03-03 16:31:04262 client.Close();
263
morrita0a24cfc92014-09-16 03:20:48264 return 0;
265}
266
msw8f7f53902015-06-19 16:26:47267// Times out on Android; see http://crbug.com/502290
268#if defined(OS_ANDROID)
269#define MAYBE_SendFailWithPendingMessages DISABLED_SendFailWithPendingMessages
270#else
271#define MAYBE_SendFailWithPendingMessages SendFailWithPendingMessages
272#endif
273TEST_F(IPCChannelMojoErrorTest, MAYBE_SendFailWithPendingMessages) {
rockotcbca72f2015-03-03 16:31:04274 InitWithMojo("IPCChannelMojoErraticTestClient");
morrita0a24cfc92014-09-16 03:20:48275
276 // Set up IPC channel and start client.
277 ListenerExpectingErrors listener;
278 CreateChannel(&listener);
279 ASSERT_TRUE(ConnectChannel());
280
jamesra03ae492014-10-03 04:26:48281 // This matches a value in mojo/edk/system/constants.h
morritabe6c4cc2014-09-24 23:38:44282 const int kMaxMessageNumBytes = 4 * 1024 * 1024;
283 std::string overly_large_data(kMaxMessageNumBytes, '*');
morrita0a24cfc92014-09-16 03:20:48284 // This messages are queued as pending.
morritabe6c4cc2014-09-24 23:38:44285 for (size_t i = 0; i < 10; ++i) {
morrita0a24cfc92014-09-16 03:20:48286 IPC::TestChannelListener::SendOneMessage(
morritabe6c4cc2014-09-24 23:38:44287 sender(), overly_large_data.c_str());
morrita0a24cfc92014-09-16 03:20:48288 }
289
290 ASSERT_TRUE(StartClient());
291 base::MessageLoop::current()->Run();
292
293 this->channel()->Close();
294
295 EXPECT_TRUE(WaitForClientShutdown());
296 EXPECT_TRUE(listener.has_error());
297
298 DestroyChannel();
299}
300
morrita81b17e02015-02-06 00:58:30301struct TestingMessagePipe {
302 TestingMessagePipe() {
303 EXPECT_EQ(MOJO_RESULT_OK, mojo::CreateMessagePipe(nullptr, &self, &peer));
304 }
305
306 mojo::ScopedMessagePipeHandle self;
307 mojo::ScopedMessagePipeHandle peer;
308};
309
310class HandleSendingHelper {
311 public:
312 static std::string GetSendingFileContent() { return "Hello"; }
313
314 static void WritePipe(IPC::Message* message, TestingMessagePipe* pipe) {
315 std::string content = HandleSendingHelper::GetSendingFileContent();
316 EXPECT_EQ(MOJO_RESULT_OK,
317 mojo::WriteMessageRaw(pipe->self.get(), &content[0],
318 static_cast<uint32_t>(content.size()),
319 nullptr, 0, 0));
dchenge48600452015-12-28 02:24:50320 EXPECT_TRUE(IPC::MojoMessageHelper::WriteMessagePipeTo(
321 message, std::move(pipe->peer)));
morrita81b17e02015-02-06 00:58:30322 }
323
324 static void WritePipeThenSend(IPC::Sender* sender, TestingMessagePipe* pipe) {
325 IPC::Message* message =
326 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
327 WritePipe(message, pipe);
328 ASSERT_TRUE(sender->Send(message));
329 }
330
331 static void ReadReceivedPipe(const IPC::Message& message,
brettwbd4d7112015-06-03 04:29:25332 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30333 mojo::ScopedMessagePipeHandle pipe;
334 EXPECT_TRUE(
335 IPC::MojoMessageHelper::ReadMessagePipeFrom(&message, iter, &pipe));
336 std::string content(GetSendingFileContent().size(), ' ');
337
338 uint32_t num_bytes = static_cast<uint32_t>(content.size());
339 EXPECT_EQ(MOJO_RESULT_OK,
340 mojo::ReadMessageRaw(pipe.get(), &content[0], &num_bytes, nullptr,
341 nullptr, 0));
342 EXPECT_EQ(content, GetSendingFileContent());
343 }
344
345#if defined(OS_POSIX)
346 static base::FilePath GetSendingFilePath() {
347 base::FilePath path;
348 bool ok = PathService::Get(base::DIR_CACHE, &path);
349 EXPECT_TRUE(ok);
350 return path.Append("ListenerThatExpectsFile.txt");
351 }
352
353 static void WriteFile(IPC::Message* message, base::File& file) {
354 std::string content = GetSendingFileContent();
355 file.WriteAtCurrentPos(content.data(), content.size());
356 file.Flush();
357 message->WriteAttachment(new IPC::internal::PlatformFileAttachment(
358 base::ScopedFD(file.TakePlatformFile())));
359 }
360
361 static void WriteFileThenSend(IPC::Sender* sender, base::File& file) {
362 IPC::Message* message =
363 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
364 WriteFile(message, file);
365 ASSERT_TRUE(sender->Send(message));
366 }
367
368 static void WriteFileAndPipeThenSend(IPC::Sender* sender,
369 base::File& file,
370 TestingMessagePipe* pipe) {
371 IPC::Message* message =
372 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
373 WriteFile(message, file);
374 WritePipe(message, pipe);
375 ASSERT_TRUE(sender->Send(message));
376 }
377
378 static void ReadReceivedFile(const IPC::Message& message,
brettwbd4d7112015-06-03 04:29:25379 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30380 base::ScopedFD fd;
rockot502c94f2016-02-03 20:20:16381 scoped_refptr<base::Pickle::Attachment> attachment;
morrita81b17e02015-02-06 00:58:30382 EXPECT_TRUE(message.ReadAttachment(iter, &attachment));
rockot502c94f2016-02-03 20:20:16383 base::File file(static_cast<IPC::MessageAttachment*>(attachment.get())
384 ->TakePlatformFile());
morrita81b17e02015-02-06 00:58:30385 std::string content(GetSendingFileContent().size(), ' ');
386 file.Read(0, &content[0], content.size());
387 EXPECT_EQ(content, GetSendingFileContent());
388 }
389#endif
390};
391
392class ListenerThatExpectsMessagePipe : public IPC::Listener {
393 public:
394 ListenerThatExpectsMessagePipe() : sender_(NULL) {}
395
396 ~ListenerThatExpectsMessagePipe() override {}
397
398 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25399 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30400 HandleSendingHelper::ReadReceivedPipe(message, &iter);
ki.stfua21ed8c2015-10-12 17:26:00401 base::MessageLoop::current()->QuitWhenIdle();
morrita81b17e02015-02-06 00:58:30402 ListenerThatExpectsOK::SendOK(sender_);
403 return true;
404 }
405
406 void OnChannelError() override { NOTREACHED(); }
407
408 void set_sender(IPC::Sender* sender) { sender_ = sender; }
409
410 private:
411 IPC::Sender* sender_;
412};
413
msw8f7f53902015-06-19 16:26:47414// Times out on Android; see http://crbug.com/502290
415#if defined(OS_ANDROID)
416#define MAYBE_SendMessagePipe DISABLED_SendMessagePipe
417#else
418#define MAYBE_SendMessagePipe SendMessagePipe
419#endif
420TEST_F(IPCChannelMojoTest, MAYBE_SendMessagePipe) {
rockotcbca72f2015-03-03 16:31:04421 InitWithMojo("IPCChannelMojoTestSendMessagePipeClient");
morrita81b17e02015-02-06 00:58:30422
423 ListenerThatExpectsOK listener;
424 CreateChannel(&listener);
425 ASSERT_TRUE(ConnectChannel());
426 ASSERT_TRUE(StartClient());
427
428 TestingMessagePipe pipe;
429 HandleSendingHelper::WritePipeThenSend(channel(), &pipe);
430
431 base::MessageLoop::current()->Run();
432 this->channel()->Close();
433
434 EXPECT_TRUE(WaitForClientShutdown());
435 DestroyChannel();
436}
437
438MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendMessagePipeClient) {
439 ListenerThatExpectsMessagePipe listener;
morritae73c0b42015-03-05 02:55:19440 ChannelClient client(&listener, "IPCChannelMojoTestSendMessagePipeClient");
morrita81b17e02015-02-06 00:58:30441 client.Connect();
442 listener.set_sender(client.channel());
443
444 base::MessageLoop::current()->Run();
445
rockotcbca72f2015-03-03 16:31:04446 client.Close();
447
morrita81b17e02015-02-06 00:58:30448 return 0;
449}
450
morrita438a2ee2015-04-03 05:28:21451void ReadOK(mojo::MessagePipeHandle pipe) {
452 std::string should_be_ok("xx");
453 uint32_t num_bytes = static_cast<uint32_t>(should_be_ok.size());
454 CHECK_EQ(MOJO_RESULT_OK,
455 mojo::ReadMessageRaw(pipe, &should_be_ok[0], &num_bytes, nullptr,
456 nullptr, 0));
457 EXPECT_EQ(should_be_ok, std::string("OK"));
458}
459
460void WriteOK(mojo::MessagePipeHandle pipe) {
461 std::string ok("OK");
462 CHECK_EQ(MOJO_RESULT_OK,
463 mojo::WriteMessageRaw(pipe, &ok[0], static_cast<uint32_t>(ok.size()),
464 nullptr, 0, 0));
465}
466
467class ListenerThatExpectsMessagePipeUsingParamTrait : public IPC::Listener {
468 public:
469 explicit ListenerThatExpectsMessagePipeUsingParamTrait(bool receiving_valid)
470 : sender_(NULL), receiving_valid_(receiving_valid) {}
471
472 ~ListenerThatExpectsMessagePipeUsingParamTrait() override {}
473
474 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25475 base::PickleIterator iter(message);
morrita438a2ee2015-04-03 05:28:21476 mojo::MessagePipeHandle handle;
477 EXPECT_TRUE(IPC::ParamTraits<mojo::MessagePipeHandle>::Read(&message, &iter,
478 &handle));
479 EXPECT_EQ(handle.is_valid(), receiving_valid_);
480 if (receiving_valid_) {
481 ReadOK(handle);
482 MojoClose(handle.value());
483 }
484
ki.stfua21ed8c2015-10-12 17:26:00485 base::MessageLoop::current()->QuitWhenIdle();
morrita438a2ee2015-04-03 05:28:21486 ListenerThatExpectsOK::SendOK(sender_);
487 return true;
488 }
489
490 void OnChannelError() override { NOTREACHED(); }
491 void set_sender(IPC::Sender* sender) { sender_ = sender; }
492
493 private:
494 IPC::Sender* sender_;
495 bool receiving_valid_;
496};
497
498void ParamTraitMessagePipeClient(bool receiving_valid_handle,
499 const char* channel_name) {
500 ListenerThatExpectsMessagePipeUsingParamTrait listener(
501 receiving_valid_handle);
502 ChannelClient client(&listener, channel_name);
503 client.Connect();
504 listener.set_sender(client.channel());
505
506 base::MessageLoop::current()->Run();
507
508 client.Close();
509}
510
msw8f7f53902015-06-19 16:26:47511// Times out on Android; see http://crbug.com/502290
512#if defined(OS_ANDROID)
513#define MAYBE_ParamTraitValidMessagePipe DISABLED_ParamTraitValidMessagePipe
514#else
515#define MAYBE_ParamTraitValidMessagePipe ParamTraitValidMessagePipe
516#endif
517TEST_F(IPCChannelMojoTest, MAYBE_ParamTraitValidMessagePipe) {
morrita438a2ee2015-04-03 05:28:21518 InitWithMojo("ParamTraitValidMessagePipeClient");
519
520 ListenerThatExpectsOK listener;
521 CreateChannel(&listener);
522 ASSERT_TRUE(ConnectChannel());
523 ASSERT_TRUE(StartClient());
524
525 TestingMessagePipe pipe;
526
527 scoped_ptr<IPC::Message> message(new IPC::Message());
528 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
529 pipe.peer.release());
530 WriteOK(pipe.self.get());
531
532 this->channel()->Send(message.release());
533 base::MessageLoop::current()->Run();
534 this->channel()->Close();
535
536 EXPECT_TRUE(WaitForClientShutdown());
537 DestroyChannel();
538}
539
540MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitValidMessagePipeClient) {
541 ParamTraitMessagePipeClient(true, "ParamTraitValidMessagePipeClient");
542 return 0;
543}
544
msw8f7f53902015-06-19 16:26:47545// Times out on Android; see http://crbug.com/502290
546#if defined(OS_ANDROID)
547#define MAYBE_ParamTraitInvalidMessagePipe DISABLED_ParamTraitInvalidMessagePipe
548#else
549#define MAYBE_ParamTraitInvalidMessagePipe ParamTraitInvalidMessagePipe
550#endif
551TEST_F(IPCChannelMojoTest, MAYBE_ParamTraitInvalidMessagePipe) {
morrita438a2ee2015-04-03 05:28:21552 InitWithMojo("ParamTraitInvalidMessagePipeClient");
553
554 ListenerThatExpectsOK listener;
555 CreateChannel(&listener);
556 ASSERT_TRUE(ConnectChannel());
557 ASSERT_TRUE(StartClient());
558
559 mojo::MessagePipeHandle invalid_handle;
560 scoped_ptr<IPC::Message> message(new IPC::Message());
561 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
562 invalid_handle);
563
564 this->channel()->Send(message.release());
565 base::MessageLoop::current()->Run();
566 this->channel()->Close();
567
568 EXPECT_TRUE(WaitForClientShutdown());
569 DestroyChannel();
570}
571
572MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitInvalidMessagePipeClient) {
573 ParamTraitMessagePipeClient(false, "ParamTraitInvalidMessagePipeClient");
574 return 0;
575}
576
morrita17137e62015-06-23 22:29:36577TEST_F(IPCChannelMojoTest, SendFailAfterClose) {
578 InitWithMojo("IPCChannelMojoTestSendOkClient");
579
580 ListenerThatExpectsOK listener;
581 CreateChannel(&listener);
582 ASSERT_TRUE(ConnectChannel());
583 ASSERT_TRUE(StartClient());
584
585 base::MessageLoop::current()->Run();
586 this->channel()->Close();
587 ASSERT_FALSE(this->channel()->Send(new IPC::Message()));
588
589 EXPECT_TRUE(WaitForClientShutdown());
590 DestroyChannel();
591}
592
593class ListenerSendingOneOk : public IPC::Listener {
594 public:
595 ListenerSendingOneOk() {
596 }
597
598 bool OnMessageReceived(const IPC::Message& message) override {
599 return true;
600 }
601
tfarina10a5c062015-09-04 18:47:57602 void OnChannelConnected(int32_t peer_pid) override {
morrita17137e62015-06-23 22:29:36603 ListenerThatExpectsOK::SendOK(sender_);
ki.stfua21ed8c2015-10-12 17:26:00604 base::MessageLoop::current()->QuitWhenIdle();
morrita17137e62015-06-23 22:29:36605 }
606
607 void set_sender(IPC::Sender* sender) { sender_ = sender; }
608
609 private:
610 IPC::Sender* sender_;
611};
612
613MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendOkClient) {
614 ListenerSendingOneOk listener;
615 ChannelClient client(&listener, "IPCChannelMojoTestSendOkClient");
616 client.Connect();
617 listener.set_sender(client.channel());
618
619 base::MessageLoop::current()->Run();
620
621 client.Close();
622
623 return 0;
624}
625
morrita25803672014-10-15 18:50:19626#if defined(OS_WIN)
rockotcbca72f2015-03-03 16:31:04627class IPCChannelMojoDeadHandleTest : public IPCChannelMojoTestBase {
morrita25803672014-10-15 18:50:19628 protected:
nickd60f7172015-04-23 16:42:48629 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita25803672014-10-15 18:50:19630 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39631 base::SequencedTaskRunner* runner) override {
erikchen30dc2812015-09-24 03:26:38632 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle);
morrita25803672014-10-15 18:50:19633 }
634
nickd60f7172015-04-23 16:42:48635 bool DidStartClient() override {
morrita25803672014-10-15 18:50:19636 IPCTestBase::DidStartClient();
leon.hand20a6c4c2015-06-19 02:25:48637 // const base::ProcessHandle client = client_process().Handle();
morrita25803672014-10-15 18:50:19638 // Forces GetFileHandleForProcess() fail. It happens occasionally
639 // in production, so we should exercise it somehow.
morritae73c0b42015-03-05 02:55:19640 // TODO(morrita): figure out how to safely test this. See crbug.com/464109.
rvargas07b589c2015-01-12 22:23:23641 // ::CloseHandle(client);
morrita25803672014-10-15 18:50:19642 return true;
643 }
morrita25803672014-10-15 18:50:19644};
645
646TEST_F(IPCChannelMojoDeadHandleTest, InvalidClientHandle) {
647 // Any client type is fine as it is going to be killed anyway.
rockotcbca72f2015-03-03 16:31:04648 InitWithMojo("IPCChannelMojoTestDoNothingClient");
morrita25803672014-10-15 18:50:19649
650 // Set up IPC channel and start client.
651 ListenerExpectingErrors listener;
652 CreateChannel(&listener);
653 ASSERT_TRUE(ConnectChannel());
654
655 ASSERT_TRUE(StartClient());
656 base::MessageLoop::current()->Run();
657
658 this->channel()->Close();
659
morritae73c0b42015-03-05 02:55:19660 // TODO(morrita): We need CloseHandle() call in DidStartClient(),
661 // which has been disabled since crrev.com/843113003, to
662 // make this fail. See crbug.com/464109.
663 // EXPECT_FALSE(WaitForClientShutdown());
664 WaitForClientShutdown();
morrita25803672014-10-15 18:50:19665 EXPECT_TRUE(listener.has_error());
666
667 DestroyChannel();
668}
669
670MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestDoNothingClient) {
671 ListenerThatQuits listener;
672 ChannelClient client(&listener, "IPCChannelMojoTestDoNothingClient");
673 client.Connect();
674
675 // Quits without running the message loop as this client won't
676 // receive any messages from the server.
677
678 return 0;
679}
680#endif
morrita0a24cfc92014-09-16 03:20:48681
[email protected]64860882014-08-04 23:44:17682#if defined(OS_POSIX)
683class ListenerThatExpectsFile : public IPC::Listener {
684 public:
685 ListenerThatExpectsFile()
686 : sender_(NULL) {}
687
dchengfe61fca2014-10-22 02:29:52688 ~ListenerThatExpectsFile() override {}
[email protected]64860882014-08-04 23:44:17689
dchengfe61fca2014-10-22 02:29:52690 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25691 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30692 HandleSendingHelper::ReadReceivedFile(message, &iter);
ki.stfua21ed8c2015-10-12 17:26:00693 base::MessageLoop::current()->QuitWhenIdle();
[email protected]64860882014-08-04 23:44:17694 ListenerThatExpectsOK::SendOK(sender_);
695 return true;
696 }
697
dchengfe61fca2014-10-22 02:29:52698 void OnChannelError() override {
dchengf3076af2014-10-21 18:02:42699 NOTREACHED();
700 }
[email protected]64860882014-08-04 23:44:17701
[email protected]64860882014-08-04 23:44:17702 void set_sender(IPC::Sender* sender) { sender_ = sender; }
703
704 private:
705 IPC::Sender* sender_;
706};
707
msw8f7f53902015-06-19 16:26:47708// Times out on Android; see http://crbug.com/502290
709#if defined(OS_ANDROID)
710#define MAYBE_SendPlatformHandle DISABLED_SendPlatformHandle
711#else
712#define MAYBE_SendPlatformHandle SendPlatformHandle
713#endif
714TEST_F(IPCChannelMojoTest, MAYBE_SendPlatformHandle) {
rockotcbca72f2015-03-03 16:31:04715 InitWithMojo("IPCChannelMojoTestSendPlatformHandleClient");
[email protected]64860882014-08-04 23:44:17716
717 ListenerThatExpectsOK listener;
morrita373af03b2014-09-09 19:35:24718 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17719 ASSERT_TRUE(ConnectChannel());
720 ASSERT_TRUE(StartClient());
721
morrita81b17e02015-02-06 00:58:30722 base::File file(HandleSendingHelper::GetSendingFilePath(),
[email protected]64860882014-08-04 23:44:17723 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
morrita81b17e02015-02-06 00:58:30724 base::File::FLAG_READ);
725 HandleSendingHelper::WriteFileThenSend(channel(), file);
[email protected]64860882014-08-04 23:44:17726 base::MessageLoop::current()->Run();
727
728 this->channel()->Close();
729
730 EXPECT_TRUE(WaitForClientShutdown());
731 DestroyChannel();
732}
733
734MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendPlatformHandleClient) {
735 ListenerThatExpectsFile listener;
736 ChannelClient client(
737 &listener, "IPCChannelMojoTestSendPlatformHandleClient");
738 client.Connect();
739 listener.set_sender(client.channel());
740
741 base::MessageLoop::current()->Run();
742
rockotcbca72f2015-03-03 16:31:04743 client.Close();
744
[email protected]64860882014-08-04 23:44:17745 return 0;
746}
morrita81b17e02015-02-06 00:58:30747
748class ListenerThatExpectsFileAndPipe : public IPC::Listener {
749 public:
750 ListenerThatExpectsFileAndPipe() : sender_(NULL) {}
751
752 ~ListenerThatExpectsFileAndPipe() override {}
753
754 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25755 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30756 HandleSendingHelper::ReadReceivedFile(message, &iter);
757 HandleSendingHelper::ReadReceivedPipe(message, &iter);
ki.stfua21ed8c2015-10-12 17:26:00758 base::MessageLoop::current()->QuitWhenIdle();
morrita81b17e02015-02-06 00:58:30759 ListenerThatExpectsOK::SendOK(sender_);
760 return true;
761 }
762
763 void OnChannelError() override { NOTREACHED(); }
764
765 void set_sender(IPC::Sender* sender) { sender_ = sender; }
766
767 private:
768 IPC::Sender* sender_;
769};
770
msw8f7f53902015-06-19 16:26:47771// Times out on Android; see http://crbug.com/502290
772#if defined(OS_ANDROID)
773#define MAYBE_SendPlatformHandleAndPipe DISABLED_SendPlatformHandleAndPipe
774#else
775#define MAYBE_SendPlatformHandleAndPipe SendPlatformHandleAndPipe
776#endif
777TEST_F(IPCChannelMojoTest, MAYBE_SendPlatformHandleAndPipe) {
rockotcbca72f2015-03-03 16:31:04778 InitWithMojo("IPCChannelMojoTestSendPlatformHandleAndPipeClient");
morrita81b17e02015-02-06 00:58:30779
780 ListenerThatExpectsOK listener;
781 CreateChannel(&listener);
782 ASSERT_TRUE(ConnectChannel());
783 ASSERT_TRUE(StartClient());
784
785 base::File file(HandleSendingHelper::GetSendingFilePath(),
786 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
787 base::File::FLAG_READ);
788 TestingMessagePipe pipe;
789 HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe);
790
791 base::MessageLoop::current()->Run();
792 this->channel()->Close();
793
794 EXPECT_TRUE(WaitForClientShutdown());
795 DestroyChannel();
796}
797
798MULTIPROCESS_IPC_TEST_CLIENT_MAIN(
799 IPCChannelMojoTestSendPlatformHandleAndPipeClient) {
800 ListenerThatExpectsFileAndPipe listener;
801 ChannelClient client(&listener,
802 "IPCChannelMojoTestSendPlatformHandleAndPipeClient");
803 client.Connect();
804 listener.set_sender(client.channel());
805
806 base::MessageLoop::current()->Run();
807
rockotcbca72f2015-03-03 16:31:04808 client.Close();
809
morrita81b17e02015-02-06 00:58:30810 return 0;
811}
812
[email protected]64860882014-08-04 23:44:17813#endif
814
morrita0bd20bd2015-02-25 20:11:27815#if defined(OS_LINUX)
816
817const base::ProcessId kMagicChildId = 54321;
818
819class ListenerThatVerifiesPeerPid : public IPC::Listener {
820 public:
tfarina10a5c062015-09-04 18:47:57821 void OnChannelConnected(int32_t peer_pid) override {
morrita0bd20bd2015-02-25 20:11:27822 EXPECT_EQ(peer_pid, kMagicChildId);
ki.stfua21ed8c2015-10-12 17:26:00823 base::MessageLoop::current()->QuitWhenIdle();
morrita0bd20bd2015-02-25 20:11:27824 }
825
826 bool OnMessageReceived(const IPC::Message& message) override {
827 NOTREACHED();
828 return true;
829 }
830};
831
832TEST_F(IPCChannelMojoTest, VerifyGlobalPid) {
rockotcbca72f2015-03-03 16:31:04833 InitWithMojo("IPCChannelMojoTestVerifyGlobalPidClient");
morrita0bd20bd2015-02-25 20:11:27834
835 ListenerThatVerifiesPeerPid listener;
836 CreateChannel(&listener);
837 ASSERT_TRUE(ConnectChannel());
838 ASSERT_TRUE(StartClient());
839
840 base::MessageLoop::current()->Run();
rockotcbca72f2015-03-03 16:31:04841 channel()->Close();
morrita0bd20bd2015-02-25 20:11:27842
843 EXPECT_TRUE(WaitForClientShutdown());
844 DestroyChannel();
845}
846
847MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestVerifyGlobalPidClient) {
848 IPC::Channel::SetGlobalPid(kMagicChildId);
849 ListenerThatQuits listener;
850 ChannelClient client(&listener,
851 "IPCChannelMojoTestVerifyGlobalPidClient");
852 client.Connect();
853
854 base::MessageLoop::current()->Run();
855
rockotcbca72f2015-03-03 16:31:04856 client.Close();
857
morrita0bd20bd2015-02-25 20:11:27858 return 0;
859}
860
861#endif // OS_LINUX
862
[email protected]64860882014-08-04 23:44:17863} // namespace