blob: f1982533bca1d870c221581cf71cf31258366018 [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
tfarina10a5c062015-09-04 18:47:577#include <stdint.h>
8
[email protected]64860882014-08-04 23:44:179#include "base/base_paths.h"
10#include "base/files/file.h"
skyostile687bdff2015-05-12 11:29:2111#include "base/location.h"
[email protected]64860882014-08-04 23:44:1712#include "base/path_service.h"
13#include "base/pickle.h"
rockotcbca72f2015-03-03 16:31:0414#include "base/run_loop.h"
skyostile687bdff2015-05-12 11:29:2115#include "base/single_thread_task_runner.h"
morrita0bd20bd2015-02-25 20:11:2716#include "base/test/test_timeouts.h"
skyostile687bdff2015-05-12 11:29:2117#include "base/thread_task_runner_handle.h"
[email protected]64860882014-08-04 23:44:1718#include "base/threading/thread.h"
19#include "ipc/ipc_message.h"
20#include "ipc/ipc_test_base.h"
21#include "ipc/ipc_test_channel_listener.h"
morrita81b17e02015-02-06 00:58:3022#include "ipc/mojo/ipc_mojo_handle_attachment.h"
23#include "ipc/mojo/ipc_mojo_message_helper.h"
morrita438a2ee2015-04-03 05:28:2124#include "ipc/mojo/ipc_mojo_param_traits.h"
rockotcbca72f2015-03-03 16:31:0425#include "ipc/mojo/scoped_ipc_support.h"
[email protected]64860882014-08-04 23:44:1726
27#if defined(OS_POSIX)
28#include "base/file_descriptor_posix.h"
morrita1aa788c2015-01-31 05:45:4229#include "ipc/ipc_platform_file_attachment_posix.h"
[email protected]64860882014-08-04 23:44:1730#endif
31
32namespace {
33
34class ListenerThatExpectsOK : public IPC::Listener {
35 public:
[email protected]e5c27752014-08-08 21:45:1336 ListenerThatExpectsOK()
37 : received_ok_(false) {}
[email protected]64860882014-08-04 23:44:1738
dchengfe61fca2014-10-22 02:29:5239 ~ListenerThatExpectsOK() override {}
[email protected]64860882014-08-04 23:44:1740
dchengfe61fca2014-10-22 02:29:5241 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:2542 base::PickleIterator iter(message);
[email protected]64860882014-08-04 23:44:1743 std::string should_be_ok;
44 EXPECT_TRUE(iter.ReadString(&should_be_ok));
45 EXPECT_EQ(should_be_ok, "OK");
[email protected]e5c27752014-08-08 21:45:1346 received_ok_ = true;
ki.stfua21ed8c2015-10-12 17:26:0047 base::MessageLoop::current()->QuitWhenIdle();
[email protected]64860882014-08-04 23:44:1748 return true;
49 }
50
dchengfe61fca2014-10-22 02:29:5251 void OnChannelError() override {
[email protected]e5c27752014-08-08 21:45:1352 // The connection should be healthy while the listener is waiting
53 // message. An error can occur after that because the peer
54 // process dies.
55 DCHECK(received_ok_);
[email protected]64860882014-08-04 23:44:1756 }
57
58 static void SendOK(IPC::Sender* sender) {
59 IPC::Message* message = new IPC::Message(
60 0, 2, IPC::Message::PRIORITY_NORMAL);
61 message->WriteString(std::string("OK"));
62 ASSERT_TRUE(sender->Send(message));
63 }
[email protected]e5c27752014-08-08 21:45:1364
65 private:
66 bool received_ok_;
[email protected]64860882014-08-04 23:44:1767};
68
[email protected]64860882014-08-04 23:44:1769class ChannelClient {
70 public:
71 explicit ChannelClient(IPC::Listener* listener, const char* name) {
erikchen30dc2812015-09-24 03:26:3872 channel_ = IPC::ChannelMojo::Create(main_message_loop_.task_runner(),
73 IPCTestBase::GetChannelName(name),
74 IPC::Channel::MODE_CLIENT, listener);
[email protected]64860882014-08-04 23:44:1775 }
76
77 void Connect() {
78 CHECK(channel_->Connect());
79 }
80
rockotcbca72f2015-03-03 16:31:0481 void Close() {
82 channel_->Close();
83
84 base::RunLoop run_loop;
skyostile687bdff2015-05-12 11:29:2185 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
86 run_loop.QuitClosure());
rockotcbca72f2015-03-03 16:31:0487 run_loop.Run();
88 }
89
[email protected]64860882014-08-04 23:44:1790 IPC::ChannelMojo* channel() const { return channel_.get(); }
91
92 private:
[email protected]64860882014-08-04 23:44:1793 base::MessageLoopForIO main_message_loop_;
morrita54f6f80c2014-09-23 21:16:0094 scoped_ptr<IPC::ChannelMojo> channel_;
[email protected]64860882014-08-04 23:44:1795};
96
rockotcbca72f2015-03-03 16:31:0497class IPCChannelMojoTestBase : public IPCTestBase {
98 public:
99 void InitWithMojo(const std::string& test_client_name) {
100 Init(test_client_name);
rockotcbca72f2015-03-03 16:31:04101 }
102
103 void TearDown() override {
104 // Make sure Mojo IPC support is properly shutdown on the I/O loop before
105 // TearDown continues.
rockotcbca72f2015-03-03 16:31:04106 base::RunLoop run_loop;
107 task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure());
108 run_loop.Run();
109
110 IPCTestBase::TearDown();
111 }
rockotcbca72f2015-03-03 16:31:04112};
113
114class IPCChannelMojoTest : public IPCChannelMojoTestBase {
[email protected]64860882014-08-04 23:44:17115 protected:
dchengfe61fca2014-10-22 02:29:52116 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita373af03b2014-09-09 19:35:24117 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39118 base::SequencedTaskRunner* runner) override {
erikchen30dc2812015-09-24 03:26:38119 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle);
[email protected]64860882014-08-04 23:44:17120 }
morrita54f6f80c2014-09-23 21:16:00121
dchengfe61fca2014-10-22 02:29:52122 bool DidStartClient() override {
morrita54f6f80c2014-09-23 21:16:00123 bool ok = IPCTestBase::DidStartClient();
124 DCHECK(ok);
morrita54f6f80c2014-09-23 21:16:00125 return ok;
126 }
[email protected]64860882014-08-04 23:44:17127};
128
129
[email protected]64860882014-08-04 23:44:17130class TestChannelListenerWithExtraExpectations
131 : public IPC::TestChannelListener {
132 public:
133 TestChannelListenerWithExtraExpectations()
134 : is_connected_called_(false) {
135 }
136
tfarina10a5c062015-09-04 18:47:57137 void OnChannelConnected(int32_t peer_pid) override {
[email protected]64860882014-08-04 23:44:17138 IPC::TestChannelListener::OnChannelConnected(peer_pid);
139 EXPECT_TRUE(base::kNullProcessId != peer_pid);
140 is_connected_called_ = true;
141 }
142
143 bool is_connected_called() const { return is_connected_called_; }
144
145 private:
146 bool is_connected_called_;
147};
148
msw8f7f53902015-06-19 16:26:47149// Times out on Android; see http://crbug.com/502290
150#if defined(OS_ANDROID)
151#define MAYBE_ConnectedFromClient DISABLED_ConnectedFromClient
152#else
153#define MAYBE_ConnectedFromClient ConnectedFromClient
154#endif
155TEST_F(IPCChannelMojoTest, MAYBE_ConnectedFromClient) {
rockotcbca72f2015-03-03 16:31:04156 InitWithMojo("IPCChannelMojoTestClient");
[email protected]64860882014-08-04 23:44:17157
158 // Set up IPC channel and start client.
159 TestChannelListenerWithExtraExpectations listener;
morrita373af03b2014-09-09 19:35:24160 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17161 listener.Init(sender());
162 ASSERT_TRUE(ConnectChannel());
163 ASSERT_TRUE(StartClient());
164
165 IPC::TestChannelListener::SendOneMessage(
166 sender(), "hello from parent");
167
168 base::MessageLoop::current()->Run();
169 EXPECT_TRUE(base::kNullProcessId != this->channel()->GetPeerPID());
170
171 this->channel()->Close();
172
173 EXPECT_TRUE(WaitForClientShutdown());
174 EXPECT_TRUE(listener.is_connected_called());
175 EXPECT_TRUE(listener.HasSentAll());
176
177 DestroyChannel();
178}
179
180// A long running process that connects to us
181MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestClient) {
182 TestChannelListenerWithExtraExpectations listener;
183 ChannelClient client(&listener, "IPCChannelMojoTestClient");
184 client.Connect();
185 listener.Init(client.channel());
186
187 IPC::TestChannelListener::SendOneMessage(
188 client.channel(), "hello from child");
189 base::MessageLoop::current()->Run();
190 EXPECT_TRUE(listener.is_connected_called());
191 EXPECT_TRUE(listener.HasSentAll());
192
rockotcbca72f2015-03-03 16:31:04193 client.Close();
194
[email protected]64860882014-08-04 23:44:17195 return 0;
196}
197
morrita0a24cfc92014-09-16 03:20:48198class ListenerExpectingErrors : public IPC::Listener {
199 public:
200 ListenerExpectingErrors()
201 : has_error_(false) {
202 }
203
tfarina10a5c062015-09-04 18:47:57204 void OnChannelConnected(int32_t peer_pid) override {
ki.stfua21ed8c2015-10-12 17:26:00205 base::MessageLoop::current()->QuitWhenIdle();
morritabe6c4cc2014-09-24 23:38:44206 }
207
dchengfe61fca2014-10-22 02:29:52208 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita0a24cfc92014-09-16 03:20:48209
dchengfe61fca2014-10-22 02:29:52210 void OnChannelError() override {
morrita0a24cfc92014-09-16 03:20:48211 has_error_ = true;
ki.stfua21ed8c2015-10-12 17:26:00212 base::MessageLoop::current()->QuitWhenIdle();
morrita0a24cfc92014-09-16 03:20:48213 }
214
215 bool has_error() const { return has_error_; }
216
217 private:
218 bool has_error_;
219};
220
221
rockotcbca72f2015-03-03 16:31:04222class IPCChannelMojoErrorTest : public IPCChannelMojoTestBase {
morrita0a24cfc92014-09-16 03:20:48223 protected:
dchengfe61fca2014-10-22 02:29:52224 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita0a24cfc92014-09-16 03:20:48225 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39226 base::SequencedTaskRunner* runner) override {
erikchen30dc2812015-09-24 03:26:38227 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle);
morrita0a24cfc92014-09-16 03:20:48228 }
morrita54f6f80c2014-09-23 21:16:00229
dchengfe61fca2014-10-22 02:29:52230 bool DidStartClient() override {
morrita54f6f80c2014-09-23 21:16:00231 bool ok = IPCTestBase::DidStartClient();
232 DCHECK(ok);
morrita54f6f80c2014-09-23 21:16:00233 return ok;
234 }
morrita0a24cfc92014-09-16 03:20:48235};
236
237class ListenerThatQuits : public IPC::Listener {
238 public:
239 ListenerThatQuits() {
240 }
241
dchengfe61fca2014-10-22 02:29:52242 bool OnMessageReceived(const IPC::Message& message) override {
dchengf3076af2014-10-21 18:02:42243 return true;
244 }
morrita0a24cfc92014-09-16 03:20:48245
tfarina10a5c062015-09-04 18:47:57246 void OnChannelConnected(int32_t peer_pid) override {
ki.stfua21ed8c2015-10-12 17:26:00247 base::MessageLoop::current()->QuitWhenIdle();
morrita0a24cfc92014-09-16 03:20:48248 }
249};
250
251// A long running process that connects to us.
252MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) {
253 ListenerThatQuits listener;
254 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient");
255 client.Connect();
256
257 base::MessageLoop::current()->Run();
258
rockotcbca72f2015-03-03 16:31:04259 client.Close();
260
morrita0a24cfc92014-09-16 03:20:48261 return 0;
262}
263
msw8f7f53902015-06-19 16:26:47264// Times out on Android; see http://crbug.com/502290
265#if defined(OS_ANDROID)
266#define MAYBE_SendFailWithPendingMessages DISABLED_SendFailWithPendingMessages
267#else
268#define MAYBE_SendFailWithPendingMessages SendFailWithPendingMessages
269#endif
270TEST_F(IPCChannelMojoErrorTest, MAYBE_SendFailWithPendingMessages) {
rockotcbca72f2015-03-03 16:31:04271 InitWithMojo("IPCChannelMojoErraticTestClient");
morrita0a24cfc92014-09-16 03:20:48272
273 // Set up IPC channel and start client.
274 ListenerExpectingErrors listener;
275 CreateChannel(&listener);
276 ASSERT_TRUE(ConnectChannel());
277
jamesra03ae492014-10-03 04:26:48278 // This matches a value in mojo/edk/system/constants.h
morritabe6c4cc2014-09-24 23:38:44279 const int kMaxMessageNumBytes = 4 * 1024 * 1024;
280 std::string overly_large_data(kMaxMessageNumBytes, '*');
morrita0a24cfc92014-09-16 03:20:48281 // This messages are queued as pending.
morritabe6c4cc2014-09-24 23:38:44282 for (size_t i = 0; i < 10; ++i) {
morrita0a24cfc92014-09-16 03:20:48283 IPC::TestChannelListener::SendOneMessage(
morritabe6c4cc2014-09-24 23:38:44284 sender(), overly_large_data.c_str());
morrita0a24cfc92014-09-16 03:20:48285 }
286
287 ASSERT_TRUE(StartClient());
288 base::MessageLoop::current()->Run();
289
290 this->channel()->Close();
291
292 EXPECT_TRUE(WaitForClientShutdown());
293 EXPECT_TRUE(listener.has_error());
294
295 DestroyChannel();
296}
297
morrita81b17e02015-02-06 00:58:30298struct TestingMessagePipe {
299 TestingMessagePipe() {
300 EXPECT_EQ(MOJO_RESULT_OK, mojo::CreateMessagePipe(nullptr, &self, &peer));
301 }
302
303 mojo::ScopedMessagePipeHandle self;
304 mojo::ScopedMessagePipeHandle peer;
305};
306
307class HandleSendingHelper {
308 public:
309 static std::string GetSendingFileContent() { return "Hello"; }
310
311 static void WritePipe(IPC::Message* message, TestingMessagePipe* pipe) {
312 std::string content = HandleSendingHelper::GetSendingFileContent();
313 EXPECT_EQ(MOJO_RESULT_OK,
314 mojo::WriteMessageRaw(pipe->self.get(), &content[0],
315 static_cast<uint32_t>(content.size()),
316 nullptr, 0, 0));
317 EXPECT_TRUE(
318 IPC::MojoMessageHelper::WriteMessagePipeTo(message, pipe->peer.Pass()));
319 }
320
321 static void WritePipeThenSend(IPC::Sender* sender, TestingMessagePipe* pipe) {
322 IPC::Message* message =
323 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
324 WritePipe(message, pipe);
325 ASSERT_TRUE(sender->Send(message));
326 }
327
328 static void ReadReceivedPipe(const IPC::Message& message,
brettwbd4d7112015-06-03 04:29:25329 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30330 mojo::ScopedMessagePipeHandle pipe;
331 EXPECT_TRUE(
332 IPC::MojoMessageHelper::ReadMessagePipeFrom(&message, iter, &pipe));
333 std::string content(GetSendingFileContent().size(), ' ');
334
335 uint32_t num_bytes = static_cast<uint32_t>(content.size());
336 EXPECT_EQ(MOJO_RESULT_OK,
337 mojo::ReadMessageRaw(pipe.get(), &content[0], &num_bytes, nullptr,
338 nullptr, 0));
339 EXPECT_EQ(content, GetSendingFileContent());
340 }
341
342#if defined(OS_POSIX)
343 static base::FilePath GetSendingFilePath() {
344 base::FilePath path;
345 bool ok = PathService::Get(base::DIR_CACHE, &path);
346 EXPECT_TRUE(ok);
347 return path.Append("ListenerThatExpectsFile.txt");
348 }
349
350 static void WriteFile(IPC::Message* message, base::File& file) {
351 std::string content = GetSendingFileContent();
352 file.WriteAtCurrentPos(content.data(), content.size());
353 file.Flush();
354 message->WriteAttachment(new IPC::internal::PlatformFileAttachment(
355 base::ScopedFD(file.TakePlatformFile())));
356 }
357
358 static void WriteFileThenSend(IPC::Sender* sender, base::File& file) {
359 IPC::Message* message =
360 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
361 WriteFile(message, file);
362 ASSERT_TRUE(sender->Send(message));
363 }
364
365 static void WriteFileAndPipeThenSend(IPC::Sender* sender,
366 base::File& file,
367 TestingMessagePipe* pipe) {
368 IPC::Message* message =
369 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL);
370 WriteFile(message, file);
371 WritePipe(message, pipe);
372 ASSERT_TRUE(sender->Send(message));
373 }
374
375 static void ReadReceivedFile(const IPC::Message& message,
brettwbd4d7112015-06-03 04:29:25376 base::PickleIterator* iter) {
morrita81b17e02015-02-06 00:58:30377 base::ScopedFD fd;
378 scoped_refptr<IPC::MessageAttachment> attachment;
379 EXPECT_TRUE(message.ReadAttachment(iter, &attachment));
380 base::File file(attachment->TakePlatformFile());
381 std::string content(GetSendingFileContent().size(), ' ');
382 file.Read(0, &content[0], content.size());
383 EXPECT_EQ(content, GetSendingFileContent());
384 }
385#endif
386};
387
388class ListenerThatExpectsMessagePipe : public IPC::Listener {
389 public:
390 ListenerThatExpectsMessagePipe() : sender_(NULL) {}
391
392 ~ListenerThatExpectsMessagePipe() override {}
393
394 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25395 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30396 HandleSendingHelper::ReadReceivedPipe(message, &iter);
ki.stfua21ed8c2015-10-12 17:26:00397 base::MessageLoop::current()->QuitWhenIdle();
morrita81b17e02015-02-06 00:58:30398 ListenerThatExpectsOK::SendOK(sender_);
399 return true;
400 }
401
402 void OnChannelError() override { NOTREACHED(); }
403
404 void set_sender(IPC::Sender* sender) { sender_ = sender; }
405
406 private:
407 IPC::Sender* sender_;
408};
409
msw8f7f53902015-06-19 16:26:47410// Times out on Android; see http://crbug.com/502290
411#if defined(OS_ANDROID)
412#define MAYBE_SendMessagePipe DISABLED_SendMessagePipe
413#else
414#define MAYBE_SendMessagePipe SendMessagePipe
415#endif
416TEST_F(IPCChannelMojoTest, MAYBE_SendMessagePipe) {
rockotcbca72f2015-03-03 16:31:04417 InitWithMojo("IPCChannelMojoTestSendMessagePipeClient");
morrita81b17e02015-02-06 00:58:30418
419 ListenerThatExpectsOK listener;
420 CreateChannel(&listener);
421 ASSERT_TRUE(ConnectChannel());
422 ASSERT_TRUE(StartClient());
423
424 TestingMessagePipe pipe;
425 HandleSendingHelper::WritePipeThenSend(channel(), &pipe);
426
427 base::MessageLoop::current()->Run();
428 this->channel()->Close();
429
430 EXPECT_TRUE(WaitForClientShutdown());
431 DestroyChannel();
432}
433
434MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendMessagePipeClient) {
435 ListenerThatExpectsMessagePipe listener;
morritae73c0b42015-03-05 02:55:19436 ChannelClient client(&listener, "IPCChannelMojoTestSendMessagePipeClient");
morrita81b17e02015-02-06 00:58:30437 client.Connect();
438 listener.set_sender(client.channel());
439
440 base::MessageLoop::current()->Run();
441
rockotcbca72f2015-03-03 16:31:04442 client.Close();
443
morrita81b17e02015-02-06 00:58:30444 return 0;
445}
446
morrita438a2ee2015-04-03 05:28:21447void ReadOK(mojo::MessagePipeHandle pipe) {
448 std::string should_be_ok("xx");
449 uint32_t num_bytes = static_cast<uint32_t>(should_be_ok.size());
450 CHECK_EQ(MOJO_RESULT_OK,
451 mojo::ReadMessageRaw(pipe, &should_be_ok[0], &num_bytes, nullptr,
452 nullptr, 0));
453 EXPECT_EQ(should_be_ok, std::string("OK"));
454}
455
456void WriteOK(mojo::MessagePipeHandle pipe) {
457 std::string ok("OK");
458 CHECK_EQ(MOJO_RESULT_OK,
459 mojo::WriteMessageRaw(pipe, &ok[0], static_cast<uint32_t>(ok.size()),
460 nullptr, 0, 0));
461}
462
463class ListenerThatExpectsMessagePipeUsingParamTrait : public IPC::Listener {
464 public:
465 explicit ListenerThatExpectsMessagePipeUsingParamTrait(bool receiving_valid)
466 : sender_(NULL), receiving_valid_(receiving_valid) {}
467
468 ~ListenerThatExpectsMessagePipeUsingParamTrait() override {}
469
470 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25471 base::PickleIterator iter(message);
morrita438a2ee2015-04-03 05:28:21472 mojo::MessagePipeHandle handle;
473 EXPECT_TRUE(IPC::ParamTraits<mojo::MessagePipeHandle>::Read(&message, &iter,
474 &handle));
475 EXPECT_EQ(handle.is_valid(), receiving_valid_);
476 if (receiving_valid_) {
477 ReadOK(handle);
478 MojoClose(handle.value());
479 }
480
ki.stfua21ed8c2015-10-12 17:26:00481 base::MessageLoop::current()->QuitWhenIdle();
morrita438a2ee2015-04-03 05:28:21482 ListenerThatExpectsOK::SendOK(sender_);
483 return true;
484 }
485
486 void OnChannelError() override { NOTREACHED(); }
487 void set_sender(IPC::Sender* sender) { sender_ = sender; }
488
489 private:
490 IPC::Sender* sender_;
491 bool receiving_valid_;
492};
493
494void ParamTraitMessagePipeClient(bool receiving_valid_handle,
495 const char* channel_name) {
496 ListenerThatExpectsMessagePipeUsingParamTrait listener(
497 receiving_valid_handle);
498 ChannelClient client(&listener, channel_name);
499 client.Connect();
500 listener.set_sender(client.channel());
501
502 base::MessageLoop::current()->Run();
503
504 client.Close();
505}
506
msw8f7f53902015-06-19 16:26:47507// Times out on Android; see http://crbug.com/502290
508#if defined(OS_ANDROID)
509#define MAYBE_ParamTraitValidMessagePipe DISABLED_ParamTraitValidMessagePipe
510#else
511#define MAYBE_ParamTraitValidMessagePipe ParamTraitValidMessagePipe
512#endif
513TEST_F(IPCChannelMojoTest, MAYBE_ParamTraitValidMessagePipe) {
morrita438a2ee2015-04-03 05:28:21514 InitWithMojo("ParamTraitValidMessagePipeClient");
515
516 ListenerThatExpectsOK listener;
517 CreateChannel(&listener);
518 ASSERT_TRUE(ConnectChannel());
519 ASSERT_TRUE(StartClient());
520
521 TestingMessagePipe pipe;
522
523 scoped_ptr<IPC::Message> message(new IPC::Message());
524 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
525 pipe.peer.release());
526 WriteOK(pipe.self.get());
527
528 this->channel()->Send(message.release());
529 base::MessageLoop::current()->Run();
530 this->channel()->Close();
531
532 EXPECT_TRUE(WaitForClientShutdown());
533 DestroyChannel();
534}
535
536MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitValidMessagePipeClient) {
537 ParamTraitMessagePipeClient(true, "ParamTraitValidMessagePipeClient");
538 return 0;
539}
540
msw8f7f53902015-06-19 16:26:47541// Times out on Android; see http://crbug.com/502290
542#if defined(OS_ANDROID)
543#define MAYBE_ParamTraitInvalidMessagePipe DISABLED_ParamTraitInvalidMessagePipe
544#else
545#define MAYBE_ParamTraitInvalidMessagePipe ParamTraitInvalidMessagePipe
546#endif
547TEST_F(IPCChannelMojoTest, MAYBE_ParamTraitInvalidMessagePipe) {
morrita438a2ee2015-04-03 05:28:21548 InitWithMojo("ParamTraitInvalidMessagePipeClient");
549
550 ListenerThatExpectsOK listener;
551 CreateChannel(&listener);
552 ASSERT_TRUE(ConnectChannel());
553 ASSERT_TRUE(StartClient());
554
555 mojo::MessagePipeHandle invalid_handle;
556 scoped_ptr<IPC::Message> message(new IPC::Message());
557 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(),
558 invalid_handle);
559
560 this->channel()->Send(message.release());
561 base::MessageLoop::current()->Run();
562 this->channel()->Close();
563
564 EXPECT_TRUE(WaitForClientShutdown());
565 DestroyChannel();
566}
567
568MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitInvalidMessagePipeClient) {
569 ParamTraitMessagePipeClient(false, "ParamTraitInvalidMessagePipeClient");
570 return 0;
571}
572
morrita17137e62015-06-23 22:29:36573TEST_F(IPCChannelMojoTest, SendFailAfterClose) {
574 InitWithMojo("IPCChannelMojoTestSendOkClient");
575
576 ListenerThatExpectsOK listener;
577 CreateChannel(&listener);
578 ASSERT_TRUE(ConnectChannel());
579 ASSERT_TRUE(StartClient());
580
581 base::MessageLoop::current()->Run();
582 this->channel()->Close();
583 ASSERT_FALSE(this->channel()->Send(new IPC::Message()));
584
585 EXPECT_TRUE(WaitForClientShutdown());
586 DestroyChannel();
587}
588
589class ListenerSendingOneOk : public IPC::Listener {
590 public:
591 ListenerSendingOneOk() {
592 }
593
594 bool OnMessageReceived(const IPC::Message& message) override {
595 return true;
596 }
597
tfarina10a5c062015-09-04 18:47:57598 void OnChannelConnected(int32_t peer_pid) override {
morrita17137e62015-06-23 22:29:36599 ListenerThatExpectsOK::SendOK(sender_);
ki.stfua21ed8c2015-10-12 17:26:00600 base::MessageLoop::current()->QuitWhenIdle();
morrita17137e62015-06-23 22:29:36601 }
602
603 void set_sender(IPC::Sender* sender) { sender_ = sender; }
604
605 private:
606 IPC::Sender* sender_;
607};
608
609MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendOkClient) {
610 ListenerSendingOneOk listener;
611 ChannelClient client(&listener, "IPCChannelMojoTestSendOkClient");
612 client.Connect();
613 listener.set_sender(client.channel());
614
615 base::MessageLoop::current()->Run();
616
617 client.Close();
618
619 return 0;
620}
621
morrita25803672014-10-15 18:50:19622#if defined(OS_WIN)
rockotcbca72f2015-03-03 16:31:04623class IPCChannelMojoDeadHandleTest : public IPCChannelMojoTestBase {
morrita25803672014-10-15 18:50:19624 protected:
nickd60f7172015-04-23 16:42:48625 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita25803672014-10-15 18:50:19626 const IPC::ChannelHandle& handle,
morritac4db5472015-03-13 20:44:39627 base::SequencedTaskRunner* runner) override {
erikchen30dc2812015-09-24 03:26:38628 return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle);
morrita25803672014-10-15 18:50:19629 }
630
nickd60f7172015-04-23 16:42:48631 bool DidStartClient() override {
morrita25803672014-10-15 18:50:19632 IPCTestBase::DidStartClient();
leon.hand20a6c4c2015-06-19 02:25:48633 // const base::ProcessHandle client = client_process().Handle();
morrita25803672014-10-15 18:50:19634 // Forces GetFileHandleForProcess() fail. It happens occasionally
635 // in production, so we should exercise it somehow.
morritae73c0b42015-03-05 02:55:19636 // TODO(morrita): figure out how to safely test this. See crbug.com/464109.
rvargas07b589c2015-01-12 22:23:23637 // ::CloseHandle(client);
morrita25803672014-10-15 18:50:19638 return true;
639 }
morrita25803672014-10-15 18:50:19640};
641
642TEST_F(IPCChannelMojoDeadHandleTest, InvalidClientHandle) {
643 // Any client type is fine as it is going to be killed anyway.
rockotcbca72f2015-03-03 16:31:04644 InitWithMojo("IPCChannelMojoTestDoNothingClient");
morrita25803672014-10-15 18:50:19645
646 // Set up IPC channel and start client.
647 ListenerExpectingErrors listener;
648 CreateChannel(&listener);
649 ASSERT_TRUE(ConnectChannel());
650
651 ASSERT_TRUE(StartClient());
652 base::MessageLoop::current()->Run();
653
654 this->channel()->Close();
655
morritae73c0b42015-03-05 02:55:19656 // TODO(morrita): We need CloseHandle() call in DidStartClient(),
657 // which has been disabled since crrev.com/843113003, to
658 // make this fail. See crbug.com/464109.
659 // EXPECT_FALSE(WaitForClientShutdown());
660 WaitForClientShutdown();
morrita25803672014-10-15 18:50:19661 EXPECT_TRUE(listener.has_error());
662
663 DestroyChannel();
664}
665
666MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestDoNothingClient) {
667 ListenerThatQuits listener;
668 ChannelClient client(&listener, "IPCChannelMojoTestDoNothingClient");
669 client.Connect();
670
671 // Quits without running the message loop as this client won't
672 // receive any messages from the server.
673
674 return 0;
675}
676#endif
morrita0a24cfc92014-09-16 03:20:48677
[email protected]64860882014-08-04 23:44:17678#if defined(OS_POSIX)
679class ListenerThatExpectsFile : public IPC::Listener {
680 public:
681 ListenerThatExpectsFile()
682 : sender_(NULL) {}
683
dchengfe61fca2014-10-22 02:29:52684 ~ListenerThatExpectsFile() override {}
[email protected]64860882014-08-04 23:44:17685
dchengfe61fca2014-10-22 02:29:52686 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25687 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30688 HandleSendingHelper::ReadReceivedFile(message, &iter);
ki.stfua21ed8c2015-10-12 17:26:00689 base::MessageLoop::current()->QuitWhenIdle();
[email protected]64860882014-08-04 23:44:17690 ListenerThatExpectsOK::SendOK(sender_);
691 return true;
692 }
693
dchengfe61fca2014-10-22 02:29:52694 void OnChannelError() override {
dchengf3076af2014-10-21 18:02:42695 NOTREACHED();
696 }
[email protected]64860882014-08-04 23:44:17697
[email protected]64860882014-08-04 23:44:17698 void set_sender(IPC::Sender* sender) { sender_ = sender; }
699
700 private:
701 IPC::Sender* sender_;
702};
703
msw8f7f53902015-06-19 16:26:47704// Times out on Android; see http://crbug.com/502290
705#if defined(OS_ANDROID)
706#define MAYBE_SendPlatformHandle DISABLED_SendPlatformHandle
707#else
708#define MAYBE_SendPlatformHandle SendPlatformHandle
709#endif
710TEST_F(IPCChannelMojoTest, MAYBE_SendPlatformHandle) {
rockotcbca72f2015-03-03 16:31:04711 InitWithMojo("IPCChannelMojoTestSendPlatformHandleClient");
[email protected]64860882014-08-04 23:44:17712
713 ListenerThatExpectsOK listener;
morrita373af03b2014-09-09 19:35:24714 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17715 ASSERT_TRUE(ConnectChannel());
716 ASSERT_TRUE(StartClient());
717
morrita81b17e02015-02-06 00:58:30718 base::File file(HandleSendingHelper::GetSendingFilePath(),
[email protected]64860882014-08-04 23:44:17719 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
morrita81b17e02015-02-06 00:58:30720 base::File::FLAG_READ);
721 HandleSendingHelper::WriteFileThenSend(channel(), file);
[email protected]64860882014-08-04 23:44:17722 base::MessageLoop::current()->Run();
723
724 this->channel()->Close();
725
726 EXPECT_TRUE(WaitForClientShutdown());
727 DestroyChannel();
728}
729
730MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendPlatformHandleClient) {
731 ListenerThatExpectsFile listener;
732 ChannelClient client(
733 &listener, "IPCChannelMojoTestSendPlatformHandleClient");
734 client.Connect();
735 listener.set_sender(client.channel());
736
737 base::MessageLoop::current()->Run();
738
rockotcbca72f2015-03-03 16:31:04739 client.Close();
740
[email protected]64860882014-08-04 23:44:17741 return 0;
742}
morrita81b17e02015-02-06 00:58:30743
744class ListenerThatExpectsFileAndPipe : public IPC::Listener {
745 public:
746 ListenerThatExpectsFileAndPipe() : sender_(NULL) {}
747
748 ~ListenerThatExpectsFileAndPipe() override {}
749
750 bool OnMessageReceived(const IPC::Message& message) override {
brettwbd4d7112015-06-03 04:29:25751 base::PickleIterator iter(message);
morrita81b17e02015-02-06 00:58:30752 HandleSendingHelper::ReadReceivedFile(message, &iter);
753 HandleSendingHelper::ReadReceivedPipe(message, &iter);
ki.stfua21ed8c2015-10-12 17:26:00754 base::MessageLoop::current()->QuitWhenIdle();
morrita81b17e02015-02-06 00:58:30755 ListenerThatExpectsOK::SendOK(sender_);
756 return true;
757 }
758
759 void OnChannelError() override { NOTREACHED(); }
760
761 void set_sender(IPC::Sender* sender) { sender_ = sender; }
762
763 private:
764 IPC::Sender* sender_;
765};
766
msw8f7f53902015-06-19 16:26:47767// Times out on Android; see http://crbug.com/502290
768#if defined(OS_ANDROID)
769#define MAYBE_SendPlatformHandleAndPipe DISABLED_SendPlatformHandleAndPipe
770#else
771#define MAYBE_SendPlatformHandleAndPipe SendPlatformHandleAndPipe
772#endif
773TEST_F(IPCChannelMojoTest, MAYBE_SendPlatformHandleAndPipe) {
rockotcbca72f2015-03-03 16:31:04774 InitWithMojo("IPCChannelMojoTestSendPlatformHandleAndPipeClient");
morrita81b17e02015-02-06 00:58:30775
776 ListenerThatExpectsOK listener;
777 CreateChannel(&listener);
778 ASSERT_TRUE(ConnectChannel());
779 ASSERT_TRUE(StartClient());
780
781 base::File file(HandleSendingHelper::GetSendingFilePath(),
782 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
783 base::File::FLAG_READ);
784 TestingMessagePipe pipe;
785 HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe);
786
787 base::MessageLoop::current()->Run();
788 this->channel()->Close();
789
790 EXPECT_TRUE(WaitForClientShutdown());
791 DestroyChannel();
792}
793
794MULTIPROCESS_IPC_TEST_CLIENT_MAIN(
795 IPCChannelMojoTestSendPlatformHandleAndPipeClient) {
796 ListenerThatExpectsFileAndPipe listener;
797 ChannelClient client(&listener,
798 "IPCChannelMojoTestSendPlatformHandleAndPipeClient");
799 client.Connect();
800 listener.set_sender(client.channel());
801
802 base::MessageLoop::current()->Run();
803
rockotcbca72f2015-03-03 16:31:04804 client.Close();
805
morrita81b17e02015-02-06 00:58:30806 return 0;
807}
808
[email protected]64860882014-08-04 23:44:17809#endif
810
morrita0bd20bd2015-02-25 20:11:27811#if defined(OS_LINUX)
812
813const base::ProcessId kMagicChildId = 54321;
814
815class ListenerThatVerifiesPeerPid : public IPC::Listener {
816 public:
tfarina10a5c062015-09-04 18:47:57817 void OnChannelConnected(int32_t peer_pid) override {
morrita0bd20bd2015-02-25 20:11:27818 EXPECT_EQ(peer_pid, kMagicChildId);
ki.stfua21ed8c2015-10-12 17:26:00819 base::MessageLoop::current()->QuitWhenIdle();
morrita0bd20bd2015-02-25 20:11:27820 }
821
822 bool OnMessageReceived(const IPC::Message& message) override {
823 NOTREACHED();
824 return true;
825 }
826};
827
828TEST_F(IPCChannelMojoTest, VerifyGlobalPid) {
rockotcbca72f2015-03-03 16:31:04829 InitWithMojo("IPCChannelMojoTestVerifyGlobalPidClient");
morrita0bd20bd2015-02-25 20:11:27830
831 ListenerThatVerifiesPeerPid listener;
832 CreateChannel(&listener);
833 ASSERT_TRUE(ConnectChannel());
834 ASSERT_TRUE(StartClient());
835
836 base::MessageLoop::current()->Run();
rockotcbca72f2015-03-03 16:31:04837 channel()->Close();
morrita0bd20bd2015-02-25 20:11:27838
839 EXPECT_TRUE(WaitForClientShutdown());
840 DestroyChannel();
841}
842
843MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestVerifyGlobalPidClient) {
844 IPC::Channel::SetGlobalPid(kMagicChildId);
845 ListenerThatQuits listener;
846 ChannelClient client(&listener,
847 "IPCChannelMojoTestVerifyGlobalPidClient");
848 client.Connect();
849
850 base::MessageLoop::current()->Run();
851
rockotcbca72f2015-03-03 16:31:04852 client.Close();
853
morrita0bd20bd2015-02-25 20:11:27854 return 0;
855}
856
857#endif // OS_LINUX
858
[email protected]64860882014-08-04 23:44:17859} // namespace