blob: 37267eb6dd894dd58fbab00070921bbfd31c8538 [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>
9
[email protected]64860882014-08-04 23:44:1710#include "base/base_paths.h"
11#include "base/files/file.h"
skyostile687bdff2015-05-12 11:29:2112#include "base/location.h"
[email protected]64860882014-08-04 23:44:1713#include "base/path_service.h"
14#include "base/pickle.h"
rockotcbca72f2015-03-03 16:31:0415#include "base/run_loop.h"
skyostile687bdff2015-05-12 11:29:2116#include "base/single_thread_task_runner.h"
morrita0bd20bd2015-02-25 20:11:2717#include "base/test/test_timeouts.h"
skyostile687bdff2015-05-12 11:29:2118#include "base/thread_task_runner_handle.h"
[email protected]64860882014-08-04 23:44:1719#include "base/threading/thread.h"
avi246998d82015-12-22 02:39:0420#include "build/build_config.h"
[email protected]64860882014-08-04 23:44:1721#include "ipc/ipc_message.h"
22#include "ipc/ipc_test_base.h"
23#include "ipc/ipc_test_channel_listener.h"
morrita81b17e02015-02-06 00:58:3024#include "ipc/mojo/ipc_mojo_handle_attachment.h"
25#include "ipc/mojo/ipc_mojo_message_helper.h"
morrita438a2ee2015-04-03 05:28:2126#include "ipc/mojo/ipc_mojo_param_traits.h"
rockotcbca72f2015-03-03 16:31:0427#include "ipc/mojo/scoped_ipc_support.h"
[email protected]64860882014-08-04 23:44:1728
29#if defined(OS_POSIX)
30#include "base/file_descriptor_posix.h"
morrita1aa788c2015-01-31 05:45:4231#include "ipc/ipc_platform_file_attachment_posix.h"
[email protected]64860882014-08-04 23:44:1732#endif
33
34namespace {
35
36class ListenerThatExpectsOK : public IPC::Listener {
37 public:
[email protected]e5c27752014-08-08 21:45:1338 ListenerThatExpectsOK()
39 : received_ok_(false) {}
[email protected]64860882014-08-04 23:44:1740
dchengfe61fca2014-10-22 02:29:5241 ~ListenerThatExpectsOK() override {}
[email protected]64860882014-08-04 23:44:1742
dchengfe61fca2014-10-22 02:29:5243 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:2544 base::PickleIterator iter(message);
[email protected]64860882014-08-04 23:44:1745 std::string should_be_ok;
46 EXPECT_TRUE(iter.ReadString(&should_be_ok));
47 EXPECT_EQ(should_be_ok, "OK");
[email protected]e5c27752014-08-08 21:45:1348 received_ok_ = true;
ki.stfua21ed8c2015-10-12 17:26:0049 base::MessageLoop::current()->QuitWhenIdle();
[email protected]64860882014-08-04 23:44:1750 return true;
51 }
52
dchengfe61fca2014-10-22 02:29:5253 void OnChannelError() override {
[email protected]e5c27752014-08-08 21:45:1354 // The connection should be healthy while the listener is waiting
55 // message. An error can occur after that because the peer
56 // process dies.
57 DCHECK(received_ok_);
[email protected]64860882014-08-04 23:44:1758 }
59
60 static void SendOK(IPC::Sender* sender) {
61 IPC::Message* message = new IPC::Message(
62 0, 2, IPC::Message::PRIORITY_NORMAL);
63 message->WriteString(std::string("OK"));
64 ASSERT_TRUE(sender->Send(message));
65 }
[email protected]e5c27752014-08-08 21:45:1366
67 private:
68 bool received_ok_;
[email protected]64860882014-08-04 23:44:1769};
70
[email protected]64860882014-08-04 23:44:1771class ChannelClient {
72 public:
73 explicit ChannelClient(IPC::Listener* listener, const char* name) {
erikchen30dc2812015-09-24 03:26:3874 channel_ = IPC::ChannelMojo::Create(main_message_loop_.task_runner(),
75 IPCTestBase::GetChannelName(name),
76 IPC::Channel::MODE_CLIENT, listener);
[email protected]64860882014-08-04 23:44:1777 }
78
79 void Connect() {
80 CHECK(channel_->Connect());
81 }
82
rockotcbca72f2015-03-03 16:31:0483 void Close() {
84 channel_->Close();
85
86 base::RunLoop run_loop;
skyostile687bdff2015-05-12 11:29:2187 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
88 run_loop.QuitClosure());
rockotcbca72f2015-03-03 16:31:0489 run_loop.Run();
90 }
91
[email protected]64860882014-08-04 23:44:1792 IPC::ChannelMojo* channel() const { return channel_.get(); }
93
94 private:
[email protected]64860882014-08-04 23:44:1795 base::MessageLoopForIO main_message_loop_;
morrita54f6f80c2014-09-23 21:16:0096 scoped_ptr<IPC::ChannelMojo> channel_;
[email protected]64860882014-08-04 23:44:1797};
98
rockotcbca72f2015-03-03 16:31:0499class IPCChannelMojoTestBase : public IPCTestBase {
100 public:
101 void InitWithMojo(const std::string& test_client_name) {
102 Init(test_client_name);
rockotcbca72f2015-03-03 16:31:04103 }
104
105 void TearDown() override {
106 // Make sure Mojo IPC support is properly shutdown on the I/O loop before
107 // TearDown continues.
rockotcbca72f2015-03-03 16:31:04108 base::RunLoop run_loop;
109 task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure());
110 run_loop.Run();
111
112 IPCTestBase::TearDown();
113 }
rockotcbca72f2015-03-03 16:31:04114};
115
116class IPCChannelMojoTest : public IPCChannelMojoTestBase {
[email protected]64860882014-08-04 23:44:17117 protected:
dchengfe61fca2014-10-22 02:29:52118 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita373af03b2014-09-09 19:35:24119 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39120 base::SequencedTaskRunner* runner) override {
erikchen30dc2812015-09-24 03:26:38121 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle);
[email protected]64860882014-08-04 23:44:17122 }
morrita54f6f80c2014-09-23 21:16:00123
dchengfe61fca2014-10-22 02:29:52124 bool DidStartClient() override {
morrita54f6f80c2014-09-23 21:16:00125 bool ok = IPCTestBase::DidStartClient();
126 DCHECK(ok);
morrita54f6f80c2014-09-23 21:16:00127 return ok;
128 }
[email protected]64860882014-08-04 23:44:17129};
130
131
[email protected]64860882014-08-04 23:44:17132class TestChannelListenerWithExtraExpectations
133 : public IPC::TestChannelListener {
134 public:
135 TestChannelListenerWithExtraExpectations()
136 : is_connected_called_(false) {
137 }
138
tfarina10a5c062015-09-04 18:47:57139 void OnChannelConnected(int32_t peer_pid) override {
[email protected]64860882014-08-04 23:44:17140 IPC::TestChannelListener::OnChannelConnected(peer_pid);
141 EXPECT_TRUE(base::kNullProcessId != peer_pid);
142 is_connected_called_ = true;
143 }
144
145 bool is_connected_called() const { return is_connected_called_; }
146
147 private:
148 bool is_connected_called_;
149};
150
msw8f7f53902015-06-19 16:26:47151// Times out on Android; see http://crbug.com/502290
152#if defined(OS_ANDROID)
153#define MAYBE_ConnectedFromClient DISABLED_ConnectedFromClient
154#else
155#define MAYBE_ConnectedFromClient ConnectedFromClient
156#endif
157TEST_F(IPCChannelMojoTest, MAYBE_ConnectedFromClient) {
rockotcbca72f2015-03-03 16:31:04158 InitWithMojo("IPCChannelMojoTestClient");
[email protected]64860882014-08-04 23:44:17159
160 // Set up IPC channel and start client.
161 TestChannelListenerWithExtraExpectations listener;
morrita373af03b2014-09-09 19:35:24162 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17163 listener.Init(sender());
164 ASSERT_TRUE(ConnectChannel());
165 ASSERT_TRUE(StartClient());
166
167 IPC::TestChannelListener::SendOneMessage(
168 sender(), "hello from parent");
169
170 base::MessageLoop::current()->Run();
171 EXPECT_TRUE(base::kNullProcessId != this->channel()->GetPeerPID());
172
173 this->channel()->Close();
174
175 EXPECT_TRUE(WaitForClientShutdown());
176 EXPECT_TRUE(listener.is_connected_called());
177 EXPECT_TRUE(listener.HasSentAll());
178
179 DestroyChannel();
180}
181
182// A long running process that connects to us
183MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestClient) {
184 TestChannelListenerWithExtraExpectations listener;
185 ChannelClient client(&listener, "IPCChannelMojoTestClient");
186 client.Connect();
187 listener.Init(client.channel());
188
189 IPC::TestChannelListener::SendOneMessage(
190 client.channel(), "hello from child");
191 base::MessageLoop::current()->Run();
192 EXPECT_TRUE(listener.is_connected_called());
193 EXPECT_TRUE(listener.HasSentAll());
194
rockotcbca72f2015-03-03 16:31:04195 client.Close();
196
[email protected]64860882014-08-04 23:44:17197 return 0;
198}
199
morrita0a24cfc92014-09-16 03:20:48200class ListenerExpectingErrors : public IPC::Listener {
201 public:
202 ListenerExpectingErrors()
203 : has_error_(false) {
204 }
205
tfarina10a5c062015-09-04 18:47:57206 void OnChannelConnected(int32_t peer_pid) override {
ki.stfua21ed8c2015-10-12 17:26:00207 base::MessageLoop::current()->QuitWhenIdle();
morritabe6c4cc2014-09-24 23:38:44208 }
209
dchengfe61fca2014-10-22 02:29:52210 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita0a24cfc92014-09-16 03:20:48211
dchengfe61fca2014-10-22 02:29:52212 void OnChannelError() override {
morrita0a24cfc92014-09-16 03:20:48213 has_error_ = true;
ki.stfua21ed8c2015-10-12 17:26:00214 base::MessageLoop::current()->QuitWhenIdle();
morrita0a24cfc92014-09-16 03:20:48215 }
216
217 bool has_error() const { return has_error_; }
218
219 private:
220 bool has_error_;
221};
222
223
rockotcbca72f2015-03-03 16:31:04224class IPCChannelMojoErrorTest : public IPCChannelMojoTestBase {
morrita0a24cfc92014-09-16 03:20:48225 protected:
dchengfe61fca2014-10-22 02:29:52226 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita0a24cfc92014-09-16 03:20:48227 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39228 base::SequencedTaskRunner* runner) override {
erikchen30dc2812015-09-24 03:26:38229 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle);
morrita0a24cfc92014-09-16 03:20:48230 }
morrita54f6f80c2014-09-23 21:16:00231
dchengfe61fca2014-10-22 02:29:52232 bool DidStartClient() override {
morrita54f6f80c2014-09-23 21:16:00233 bool ok = IPCTestBase::DidStartClient();
234 DCHECK(ok);
morrita54f6f80c2014-09-23 21:16:00235 return ok;
236 }
morrita0a24cfc92014-09-16 03:20:48237};
238
239class ListenerThatQuits : public IPC::Listener {
240 public:
241 ListenerThatQuits() {
242 }
243
dchengfe61fca2014-10-22 02:29:52244 bool OnMessageReceived(const IPC::Message& message) override {
dchengf3076af2014-10-21 18:02:42245 return true;
246 }
morrita0a24cfc92014-09-16 03:20:48247
tfarina10a5c062015-09-04 18:47:57248 void OnChannelConnected(int32_t peer_pid) override {
ki.stfua21ed8c2015-10-12 17:26:00249 base::MessageLoop::current()->QuitWhenIdle();
morrita0a24cfc92014-09-16 03:20:48250 }
251};
252
253// A long running process that connects to us.
254MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) {
255 ListenerThatQuits listener;
256 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient");
257 client.Connect();
258
259 base::MessageLoop::current()->Run();
260
rockotcbca72f2015-03-03 16:31:04261 client.Close();
262
morrita0a24cfc92014-09-16 03:20:48263 return 0;
264}
265
msw8f7f53902015-06-19 16:26:47266// Times out on Android; see http://crbug.com/502290
267#if defined(OS_ANDROID)
268#define MAYBE_SendFailWithPendingMessages DISABLED_SendFailWithPendingMessages
269#else
270#define MAYBE_SendFailWithPendingMessages SendFailWithPendingMessages
271#endif
272TEST_F(IPCChannelMojoErrorTest, MAYBE_SendFailWithPendingMessages) {
rockotcbca72f2015-03-03 16:31:04273 InitWithMojo("IPCChannelMojoErraticTestClient");
morrita0a24cfc92014-09-16 03:20:48274
275 // Set up IPC channel and start client.
276 ListenerExpectingErrors listener;
277 CreateChannel(&listener);
278 ASSERT_TRUE(ConnectChannel());
279
jamesra03ae492014-10-03 04:26:48280 // This matches a value in mojo/edk/system/constants.h
morritabe6c4cc2014-09-24 23:38:44281 const int kMaxMessageNumBytes = 4 * 1024 * 1024;
282 std::string overly_large_data(kMaxMessageNumBytes, '*');
morrita0a24cfc92014-09-16 03:20:48283 // This messages are queued as pending.
morritabe6c4cc2014-09-24 23:38:44284 for (size_t i = 0; i < 10; ++i) {
morrita0a24cfc92014-09-16 03:20:48285 IPC::TestChannelListener::SendOneMessage(
morritabe6c4cc2014-09-24 23:38:44286 sender(), overly_large_data.c_str());
morrita0a24cfc92014-09-16 03:20:48287 }
288
289 ASSERT_TRUE(StartClient());
290 base::MessageLoop::current()->Run();
291
292 this->channel()->Close();
293
294 EXPECT_TRUE(WaitForClientShutdown());
295 EXPECT_TRUE(listener.has_error());
296
297 DestroyChannel();
298}
299
morrita81b17e02015-02-06 00:58:30300struct TestingMessagePipe {
301 TestingMessagePipe() {
302 EXPECT_EQ(MOJO_RESULT_OK, mojo::CreateMessagePipe(nullptr, &self, &peer));
303 }
304
305 mojo::ScopedMessagePipeHandle self;
306 mojo::ScopedMessagePipeHandle peer;
307};
308
309class HandleSendingHelper {
310 public:
311 static std::string GetSendingFileContent() { return "Hello"; }
312
313 static void WritePipe(IPC::Message* message, TestingMessagePipe* pipe) {
314 std::string content = HandleSendingHelper::GetSendingFileContent();
315 EXPECT_EQ(MOJO_RESULT_OK,
316 mojo::WriteMessageRaw(pipe->self.get(), &content[0],
317 static_cast<uint32_t>(content.size()),
318 nullptr, 0, 0));
319 EXPECT_TRUE(
320 IPC::MojoMessageHelper::WriteMessagePipeTo(message, pipe->peer.Pass()));
321 }
322
323 static void WritePipeThenSend(IPC::Sender* sender, TestingMessagePipe* pipe) {
324 IPC::Message* message =
325 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
326 WritePipe(message, pipe);
327 ASSERT_TRUE(sender->Send(message));
328 }
329
330 static void ReadReceivedPipe(const IPC::Message& message,
brettwbd4d7112015-06-03 04:29:25331 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30332 mojo::ScopedMessagePipeHandle pipe;
333 EXPECT_TRUE(
334 IPC::MojoMessageHelper::ReadMessagePipeFrom(&message, iter, &pipe));
335 std::string content(GetSendingFileContent().size(), ' ');
336
337 uint32_t num_bytes = static_cast<uint32_t>(content.size());
338 EXPECT_EQ(MOJO_RESULT_OK,
339 mojo::ReadMessageRaw(pipe.get(), &content[0], &num_bytes, nullptr,
340 nullptr, 0));
341 EXPECT_EQ(content, GetSendingFileContent());
342 }
343
344#if defined(OS_POSIX)
345 static base::FilePath GetSendingFilePath() {
346 base::FilePath path;
347 bool ok = PathService::Get(base::DIR_CACHE, &path);
348 EXPECT_TRUE(ok);
349 return path.Append("ListenerThatExpectsFile.txt");
350 }
351
352 static void WriteFile(IPC::Message* message, base::File& file) {
353 std::string content = GetSendingFileContent();
354 file.WriteAtCurrentPos(content.data(), content.size());
355 file.Flush();
356 message->WriteAttachment(new IPC::internal::PlatformFileAttachment(
357 base::ScopedFD(file.TakePlatformFile())));
358 }
359
360 static void WriteFileThenSend(IPC::Sender* sender, base::File& file) {
361 IPC::Message* message =
362 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
363 WriteFile(message, file);
364 ASSERT_TRUE(sender->Send(message));
365 }
366
367 static void WriteFileAndPipeThenSend(IPC::Sender* sender,
368 base::File& file,
369 TestingMessagePipe* pipe) {
370 IPC::Message* message =
371 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
372 WriteFile(message, file);
373 WritePipe(message, pipe);
374 ASSERT_TRUE(sender->Send(message));
375 }
376
377 static void ReadReceivedFile(const IPC::Message& message,
brettwbd4d7112015-06-03 04:29:25378 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30379 base::ScopedFD fd;
380 scoped_refptr<IPC::MessageAttachment> attachment;
381 EXPECT_TRUE(message.ReadAttachment(iter, &attachment));
382 base::File file(attachment->TakePlatformFile());
383 std::string content(GetSendingFileContent().size(), ' ');
384 file.Read(0, &content[0], content.size());
385 EXPECT_EQ(content, GetSendingFileContent());
386 }
387#endif
388};
389
390class ListenerThatExpectsMessagePipe : public IPC::Listener {
391 public:
392 ListenerThatExpectsMessagePipe() : sender_(NULL) {}
393
394 ~ListenerThatExpectsMessagePipe() override {}
395
396 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25397 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30398 HandleSendingHelper::ReadReceivedPipe(message, &iter);
ki.stfua21ed8c2015-10-12 17:26:00399 base::MessageLoop::current()->QuitWhenIdle();
morrita81b17e02015-02-06 00:58:30400 ListenerThatExpectsOK::SendOK(sender_);
401 return true;
402 }
403
404 void OnChannelError() override { NOTREACHED(); }
405
406 void set_sender(IPC::Sender* sender) { sender_ = sender; }
407
408 private:
409 IPC::Sender* sender_;
410};
411
msw8f7f53902015-06-19 16:26:47412// Times out on Android; see http://crbug.com/502290
413#if defined(OS_ANDROID)
414#define MAYBE_SendMessagePipe DISABLED_SendMessagePipe
415#else
416#define MAYBE_SendMessagePipe SendMessagePipe
417#endif
418TEST_F(IPCChannelMojoTest, MAYBE_SendMessagePipe) {
rockotcbca72f2015-03-03 16:31:04419 InitWithMojo("IPCChannelMojoTestSendMessagePipeClient");
morrita81b17e02015-02-06 00:58:30420
421 ListenerThatExpectsOK listener;
422 CreateChannel(&listener);
423 ASSERT_TRUE(ConnectChannel());
424 ASSERT_TRUE(StartClient());
425
426 TestingMessagePipe pipe;
427 HandleSendingHelper::WritePipeThenSend(channel(), &pipe);
428
429 base::MessageLoop::current()->Run();
430 this->channel()->Close();
431
432 EXPECT_TRUE(WaitForClientShutdown());
433 DestroyChannel();
434}
435
436MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendMessagePipeClient) {
437 ListenerThatExpectsMessagePipe listener;
morritae73c0b42015-03-05 02:55:19438 ChannelClient client(&listener, "IPCChannelMojoTestSendMessagePipeClient");
morrita81b17e02015-02-06 00:58:30439 client.Connect();
440 listener.set_sender(client.channel());
441
442 base::MessageLoop::current()->Run();
443
rockotcbca72f2015-03-03 16:31:04444 client.Close();
445
morrita81b17e02015-02-06 00:58:30446 return 0;
447}
448
morrita438a2ee2015-04-03 05:28:21449void ReadOK(mojo::MessagePipeHandle pipe) {
450 std::string should_be_ok("xx");
451 uint32_t num_bytes = static_cast<uint32_t>(should_be_ok.size());
452 CHECK_EQ(MOJO_RESULT_OK,
453 mojo::ReadMessageRaw(pipe, &should_be_ok[0], &num_bytes, nullptr,
454 nullptr, 0));
455 EXPECT_EQ(should_be_ok, std::string("OK"));
456}
457
458void WriteOK(mojo::MessagePipeHandle pipe) {
459 std::string ok("OK");
460 CHECK_EQ(MOJO_RESULT_OK,
461 mojo::WriteMessageRaw(pipe, &ok[0], static_cast<uint32_t>(ok.size()),
462 nullptr, 0, 0));
463}
464
465class ListenerThatExpectsMessagePipeUsingParamTrait : public IPC::Listener {
466 public:
467 explicit ListenerThatExpectsMessagePipeUsingParamTrait(bool receiving_valid)
468 : sender_(NULL), receiving_valid_(receiving_valid) {}
469
470 ~ListenerThatExpectsMessagePipeUsingParamTrait() override {}
471
472 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25473 base::PickleIterator iter(message);
morrita438a2ee2015-04-03 05:28:21474 mojo::MessagePipeHandle handle;
475 EXPECT_TRUE(IPC::ParamTraits<mojo::MessagePipeHandle>::Read(&message, &iter,
476 &handle));
477 EXPECT_EQ(handle.is_valid(), receiving_valid_);
478 if (receiving_valid_) {
479 ReadOK(handle);
480 MojoClose(handle.value());
481 }
482
ki.stfua21ed8c2015-10-12 17:26:00483 base::MessageLoop::current()->QuitWhenIdle();
morrita438a2ee2015-04-03 05:28:21484 ListenerThatExpectsOK::SendOK(sender_);
485 return true;
486 }
487
488 void OnChannelError() override { NOTREACHED(); }
489 void set_sender(IPC::Sender* sender) { sender_ = sender; }
490
491 private:
492 IPC::Sender* sender_;
493 bool receiving_valid_;
494};
495
496void ParamTraitMessagePipeClient(bool receiving_valid_handle,
497 const char* channel_name) {
498 ListenerThatExpectsMessagePipeUsingParamTrait listener(
499 receiving_valid_handle);
500 ChannelClient client(&listener, channel_name);
501 client.Connect();
502 listener.set_sender(client.channel());
503
504 base::MessageLoop::current()->Run();
505
506 client.Close();
507}
508
msw8f7f53902015-06-19 16:26:47509// Times out on Android; see http://crbug.com/502290
510#if defined(OS_ANDROID)
511#define MAYBE_ParamTraitValidMessagePipe DISABLED_ParamTraitValidMessagePipe
512#else
513#define MAYBE_ParamTraitValidMessagePipe ParamTraitValidMessagePipe
514#endif
515TEST_F(IPCChannelMojoTest, MAYBE_ParamTraitValidMessagePipe) {
morrita438a2ee2015-04-03 05:28:21516 InitWithMojo("ParamTraitValidMessagePipeClient");
517
518 ListenerThatExpectsOK listener;
519 CreateChannel(&listener);
520 ASSERT_TRUE(ConnectChannel());
521 ASSERT_TRUE(StartClient());
522
523 TestingMessagePipe pipe;
524
525 scoped_ptr<IPC::Message> message(new IPC::Message());
526 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
527 pipe.peer.release());
528 WriteOK(pipe.self.get());
529
530 this->channel()->Send(message.release());
531 base::MessageLoop::current()->Run();
532 this->channel()->Close();
533
534 EXPECT_TRUE(WaitForClientShutdown());
535 DestroyChannel();
536}
537
538MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitValidMessagePipeClient) {
539 ParamTraitMessagePipeClient(true, "ParamTraitValidMessagePipeClient");
540 return 0;
541}
542
msw8f7f53902015-06-19 16:26:47543// Times out on Android; see http://crbug.com/502290
544#if defined(OS_ANDROID)
545#define MAYBE_ParamTraitInvalidMessagePipe DISABLED_ParamTraitInvalidMessagePipe
546#else
547#define MAYBE_ParamTraitInvalidMessagePipe ParamTraitInvalidMessagePipe
548#endif
549TEST_F(IPCChannelMojoTest, MAYBE_ParamTraitInvalidMessagePipe) {
morrita438a2ee2015-04-03 05:28:21550 InitWithMojo("ParamTraitInvalidMessagePipeClient");
551
552 ListenerThatExpectsOK listener;
553 CreateChannel(&listener);
554 ASSERT_TRUE(ConnectChannel());
555 ASSERT_TRUE(StartClient());
556
557 mojo::MessagePipeHandle invalid_handle;
558 scoped_ptr<IPC::Message> message(new IPC::Message());
559 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
560 invalid_handle);
561
562 this->channel()->Send(message.release());
563 base::MessageLoop::current()->Run();
564 this->channel()->Close();
565
566 EXPECT_TRUE(WaitForClientShutdown());
567 DestroyChannel();
568}
569
570MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitInvalidMessagePipeClient) {
571 ParamTraitMessagePipeClient(false, "ParamTraitInvalidMessagePipeClient");
572 return 0;
573}
574
morrita17137e62015-06-23 22:29:36575TEST_F(IPCChannelMojoTest, SendFailAfterClose) {
576 InitWithMojo("IPCChannelMojoTestSendOkClient");
577
578 ListenerThatExpectsOK listener;
579 CreateChannel(&listener);
580 ASSERT_TRUE(ConnectChannel());
581 ASSERT_TRUE(StartClient());
582
583 base::MessageLoop::current()->Run();
584 this->channel()->Close();
585 ASSERT_FALSE(this->channel()->Send(new IPC::Message()));
586
587 EXPECT_TRUE(WaitForClientShutdown());
588 DestroyChannel();
589}
590
591class ListenerSendingOneOk : public IPC::Listener {
592 public:
593 ListenerSendingOneOk() {
594 }
595
596 bool OnMessageReceived(const IPC::Message& message) override {
597 return true;
598 }
599
tfarina10a5c062015-09-04 18:47:57600 void OnChannelConnected(int32_t peer_pid) override {
morrita17137e62015-06-23 22:29:36601 ListenerThatExpectsOK::SendOK(sender_);
ki.stfua21ed8c2015-10-12 17:26:00602 base::MessageLoop::current()->QuitWhenIdle();
morrita17137e62015-06-23 22:29:36603 }
604
605 void set_sender(IPC::Sender* sender) { sender_ = sender; }
606
607 private:
608 IPC::Sender* sender_;
609};
610
611MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendOkClient) {
612 ListenerSendingOneOk listener;
613 ChannelClient client(&listener, "IPCChannelMojoTestSendOkClient");
614 client.Connect();
615 listener.set_sender(client.channel());
616
617 base::MessageLoop::current()->Run();
618
619 client.Close();
620
621 return 0;
622}
623
morrita25803672014-10-15 18:50:19624#if defined(OS_WIN)
rockotcbca72f2015-03-03 16:31:04625class IPCChannelMojoDeadHandleTest : public IPCChannelMojoTestBase {
morrita25803672014-10-15 18:50:19626 protected:
nickd60f7172015-04-23 16:42:48627 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita25803672014-10-15 18:50:19628 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39629 base::SequencedTaskRunner* runner) override {
erikchen30dc2812015-09-24 03:26:38630 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle);
morrita25803672014-10-15 18:50:19631 }
632
nickd60f7172015-04-23 16:42:48633 bool DidStartClient() override {
morrita25803672014-10-15 18:50:19634 IPCTestBase::DidStartClient();
leon.hand20a6c4c2015-06-19 02:25:48635 // const base::ProcessHandle client = client_process().Handle();
morrita25803672014-10-15 18:50:19636 // Forces GetFileHandleForProcess() fail. It happens occasionally
637 // in production, so we should exercise it somehow.
morritae73c0b42015-03-05 02:55:19638 // TODO(morrita): figure out how to safely test this. See crbug.com/464109.
rvargas07b589c2015-01-12 22:23:23639 // ::CloseHandle(client);
morrita25803672014-10-15 18:50:19640 return true;
641 }
morrita25803672014-10-15 18:50:19642};
643
644TEST_F(IPCChannelMojoDeadHandleTest, InvalidClientHandle) {
645 // Any client type is fine as it is going to be killed anyway.
rockotcbca72f2015-03-03 16:31:04646 InitWithMojo("IPCChannelMojoTestDoNothingClient");
morrita25803672014-10-15 18:50:19647
648 // Set up IPC channel and start client.
649 ListenerExpectingErrors listener;
650 CreateChannel(&listener);
651 ASSERT_TRUE(ConnectChannel());
652
653 ASSERT_TRUE(StartClient());
654 base::MessageLoop::current()->Run();
655
656 this->channel()->Close();
657
morritae73c0b42015-03-05 02:55:19658 // TODO(morrita): We need CloseHandle() call in DidStartClient(),
659 // which has been disabled since crrev.com/843113003, to
660 // make this fail. See crbug.com/464109.
661 // EXPECT_FALSE(WaitForClientShutdown());
662 WaitForClientShutdown();
morrita25803672014-10-15 18:50:19663 EXPECT_TRUE(listener.has_error());
664
665 DestroyChannel();
666}
667
668MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestDoNothingClient) {
669 ListenerThatQuits listener;
670 ChannelClient client(&listener, "IPCChannelMojoTestDoNothingClient");
671 client.Connect();
672
673 // Quits without running the message loop as this client won't
674 // receive any messages from the server.
675
676 return 0;
677}
678#endif
morrita0a24cfc92014-09-16 03:20:48679
[email protected]64860882014-08-04 23:44:17680#if defined(OS_POSIX)
681class ListenerThatExpectsFile : public IPC::Listener {
682 public:
683 ListenerThatExpectsFile()
684 : sender_(NULL) {}
685
dchengfe61fca2014-10-22 02:29:52686 ~ListenerThatExpectsFile() override {}
[email protected]64860882014-08-04 23:44:17687
dchengfe61fca2014-10-22 02:29:52688 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25689 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30690 HandleSendingHelper::ReadReceivedFile(message, &iter);
ki.stfua21ed8c2015-10-12 17:26:00691 base::MessageLoop::current()->QuitWhenIdle();
[email protected]64860882014-08-04 23:44:17692 ListenerThatExpectsOK::SendOK(sender_);
693 return true;
694 }
695
dchengfe61fca2014-10-22 02:29:52696 void OnChannelError() override {
dchengf3076af2014-10-21 18:02:42697 NOTREACHED();
698 }
[email protected]64860882014-08-04 23:44:17699
[email protected]64860882014-08-04 23:44:17700 void set_sender(IPC::Sender* sender) { sender_ = sender; }
701
702 private:
703 IPC::Sender* sender_;
704};
705
msw8f7f53902015-06-19 16:26:47706// Times out on Android; see http://crbug.com/502290
707#if defined(OS_ANDROID)
708#define MAYBE_SendPlatformHandle DISABLED_SendPlatformHandle
709#else
710#define MAYBE_SendPlatformHandle SendPlatformHandle
711#endif
712TEST_F(IPCChannelMojoTest, MAYBE_SendPlatformHandle) {
rockotcbca72f2015-03-03 16:31:04713 InitWithMojo("IPCChannelMojoTestSendPlatformHandleClient");
[email protected]64860882014-08-04 23:44:17714
715 ListenerThatExpectsOK listener;
morrita373af03b2014-09-09 19:35:24716 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17717 ASSERT_TRUE(ConnectChannel());
718 ASSERT_TRUE(StartClient());
719
morrita81b17e02015-02-06 00:58:30720 base::File file(HandleSendingHelper::GetSendingFilePath(),
[email protected]64860882014-08-04 23:44:17721 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
morrita81b17e02015-02-06 00:58:30722 base::File::FLAG_READ);
723 HandleSendingHelper::WriteFileThenSend(channel(), file);
[email protected]64860882014-08-04 23:44:17724 base::MessageLoop::current()->Run();
725
726 this->channel()->Close();
727
728 EXPECT_TRUE(WaitForClientShutdown());
729 DestroyChannel();
730}
731
732MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendPlatformHandleClient) {
733 ListenerThatExpectsFile listener;
734 ChannelClient client(
735 &listener, "IPCChannelMojoTestSendPlatformHandleClient");
736 client.Connect();
737 listener.set_sender(client.channel());
738
739 base::MessageLoop::current()->Run();
740
rockotcbca72f2015-03-03 16:31:04741 client.Close();
742
[email protected]64860882014-08-04 23:44:17743 return 0;
744}
morrita81b17e02015-02-06 00:58:30745
746class ListenerThatExpectsFileAndPipe : public IPC::Listener {
747 public:
748 ListenerThatExpectsFileAndPipe() : sender_(NULL) {}
749
750 ~ListenerThatExpectsFileAndPipe() override {}
751
752 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25753 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30754 HandleSendingHelper::ReadReceivedFile(message, &iter);
755 HandleSendingHelper::ReadReceivedPipe(message, &iter);
ki.stfua21ed8c2015-10-12 17:26:00756 base::MessageLoop::current()->QuitWhenIdle();
morrita81b17e02015-02-06 00:58:30757 ListenerThatExpectsOK::SendOK(sender_);
758 return true;
759 }
760
761 void OnChannelError() override { NOTREACHED(); }
762
763 void set_sender(IPC::Sender* sender) { sender_ = sender; }
764
765 private:
766 IPC::Sender* sender_;
767};
768
msw8f7f53902015-06-19 16:26:47769// Times out on Android; see http://crbug.com/502290
770#if defined(OS_ANDROID)
771#define MAYBE_SendPlatformHandleAndPipe DISABLED_SendPlatformHandleAndPipe
772#else
773#define MAYBE_SendPlatformHandleAndPipe SendPlatformHandleAndPipe
774#endif
775TEST_F(IPCChannelMojoTest, MAYBE_SendPlatformHandleAndPipe) {
rockotcbca72f2015-03-03 16:31:04776 InitWithMojo("IPCChannelMojoTestSendPlatformHandleAndPipeClient");
morrita81b17e02015-02-06 00:58:30777
778 ListenerThatExpectsOK listener;
779 CreateChannel(&listener);
780 ASSERT_TRUE(ConnectChannel());
781 ASSERT_TRUE(StartClient());
782
783 base::File file(HandleSendingHelper::GetSendingFilePath(),
784 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
785 base::File::FLAG_READ);
786 TestingMessagePipe pipe;
787 HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe);
788
789 base::MessageLoop::current()->Run();
790 this->channel()->Close();
791
792 EXPECT_TRUE(WaitForClientShutdown());
793 DestroyChannel();
794}
795
796MULTIPROCESS_IPC_TEST_CLIENT_MAIN(
797 IPCChannelMojoTestSendPlatformHandleAndPipeClient) {
798 ListenerThatExpectsFileAndPipe listener;
799 ChannelClient client(&listener,
800 "IPCChannelMojoTestSendPlatformHandleAndPipeClient");
801 client.Connect();
802 listener.set_sender(client.channel());
803
804 base::MessageLoop::current()->Run();
805
rockotcbca72f2015-03-03 16:31:04806 client.Close();
807
morrita81b17e02015-02-06 00:58:30808 return 0;
809}
810
[email protected]64860882014-08-04 23:44:17811#endif
812
morrita0bd20bd2015-02-25 20:11:27813#if defined(OS_LINUX)
814
815const base::ProcessId kMagicChildId = 54321;
816
817class ListenerThatVerifiesPeerPid : public IPC::Listener {
818 public:
tfarina10a5c062015-09-04 18:47:57819 void OnChannelConnected(int32_t peer_pid) override {
morrita0bd20bd2015-02-25 20:11:27820 EXPECT_EQ(peer_pid, kMagicChildId);
ki.stfua21ed8c2015-10-12 17:26:00821 base::MessageLoop::current()->QuitWhenIdle();
morrita0bd20bd2015-02-25 20:11:27822 }
823
824 bool OnMessageReceived(const IPC::Message& message) override {
825 NOTREACHED();
826 return true;
827 }
828};
829
830TEST_F(IPCChannelMojoTest, VerifyGlobalPid) {
rockotcbca72f2015-03-03 16:31:04831 InitWithMojo("IPCChannelMojoTestVerifyGlobalPidClient");
morrita0bd20bd2015-02-25 20:11:27832
833 ListenerThatVerifiesPeerPid listener;
834 CreateChannel(&listener);
835 ASSERT_TRUE(ConnectChannel());
836 ASSERT_TRUE(StartClient());
837
838 base::MessageLoop::current()->Run();
rockotcbca72f2015-03-03 16:31:04839 channel()->Close();
morrita0bd20bd2015-02-25 20:11:27840
841 EXPECT_TRUE(WaitForClientShutdown());
842 DestroyChannel();
843}
844
845MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestVerifyGlobalPidClient) {
846 IPC::Channel::SetGlobalPid(kMagicChildId);
847 ListenerThatQuits listener;
848 ChannelClient client(&listener,
849 "IPCChannelMojoTestVerifyGlobalPidClient");
850 client.Connect();
851
852 base::MessageLoop::current()->Run();
853
rockotcbca72f2015-03-03 16:31:04854 client.Close();
855
morrita0bd20bd2015-02-25 20:11:27856 return 0;
857}
858
859#endif // OS_LINUX
860
[email protected]64860882014-08-04 23:44:17861} // namespace