OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ipc/mojo/ipc_channel_mojo.h" | 5 #include "ipc/mojo/ipc_channel_mojo.h" |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
15 #include "ipc/ipc_message.h" | 15 #include "ipc/ipc_message.h" |
16 #include "ipc/ipc_test_base.h" | 16 #include "ipc/ipc_test_base.h" |
17 #include "ipc/ipc_test_channel_listener.h" | 17 #include "ipc/ipc_test_channel_listener.h" |
18 #include "ipc/mojo/ipc_channel_mojo_host.h" | 18 #include "ipc/mojo/ipc_channel_mojo_host.h" |
19 #include "ipc/mojo/ipc_mojo_handle_attachment.h" | 19 #include "ipc/mojo/ipc_mojo_handle_attachment.h" |
20 #include "ipc/mojo/ipc_mojo_message_helper.h" | 20 #include "ipc/mojo/ipc_mojo_message_helper.h" |
| 21 #include "ipc/mojo/ipc_mojo_param_traits.h" |
21 #include "ipc/mojo/scoped_ipc_support.h" | 22 #include "ipc/mojo/scoped_ipc_support.h" |
22 | 23 |
23 #if defined(OS_POSIX) | 24 #if defined(OS_POSIX) |
24 #include "base/file_descriptor_posix.h" | 25 #include "base/file_descriptor_posix.h" |
25 #include "ipc/ipc_platform_file_attachment_posix.h" | 26 #include "ipc/ipc_platform_file_attachment_posix.h" |
26 #endif | 27 #endif |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 class ListenerThatExpectsOK : public IPC::Listener { | 31 class ListenerThatExpectsOK : public IPC::Listener { |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 client.Connect(); | 436 client.Connect(); |
436 listener.set_sender(client.channel()); | 437 listener.set_sender(client.channel()); |
437 | 438 |
438 base::MessageLoop::current()->Run(); | 439 base::MessageLoop::current()->Run(); |
439 | 440 |
440 client.Close(); | 441 client.Close(); |
441 | 442 |
442 return 0; | 443 return 0; |
443 } | 444 } |
444 | 445 |
| 446 void ReadOK(mojo::MessagePipeHandle pipe) { |
| 447 std::string should_be_ok("xx"); |
| 448 uint32_t num_bytes = static_cast<uint32_t>(should_be_ok.size()); |
| 449 CHECK_EQ(MOJO_RESULT_OK, |
| 450 mojo::ReadMessageRaw(pipe, &should_be_ok[0], &num_bytes, nullptr, |
| 451 nullptr, 0)); |
| 452 EXPECT_EQ(should_be_ok, std::string("OK")); |
| 453 } |
| 454 |
| 455 void WriteOK(mojo::MessagePipeHandle pipe) { |
| 456 std::string ok("OK"); |
| 457 CHECK_EQ(MOJO_RESULT_OK, |
| 458 mojo::WriteMessageRaw(pipe, &ok[0], static_cast<uint32_t>(ok.size()), |
| 459 nullptr, 0, 0)); |
| 460 } |
| 461 |
| 462 class ListenerThatExpectsMessagePipeUsingParamTrait : public IPC::Listener { |
| 463 public: |
| 464 explicit ListenerThatExpectsMessagePipeUsingParamTrait(bool receiving_valid) |
| 465 : sender_(NULL), receiving_valid_(receiving_valid) {} |
| 466 |
| 467 ~ListenerThatExpectsMessagePipeUsingParamTrait() override {} |
| 468 |
| 469 bool OnMessageReceived(const IPC::Message& message) override { |
| 470 PickleIterator iter(message); |
| 471 mojo::MessagePipeHandle handle; |
| 472 EXPECT_TRUE(IPC::ParamTraits<mojo::MessagePipeHandle>::Read(&message, &iter, |
| 473 &handle)); |
| 474 EXPECT_EQ(handle.is_valid(), receiving_valid_); |
| 475 if (receiving_valid_) { |
| 476 ReadOK(handle); |
| 477 MojoClose(handle.value()); |
| 478 } |
| 479 |
| 480 base::MessageLoop::current()->Quit(); |
| 481 ListenerThatExpectsOK::SendOK(sender_); |
| 482 return true; |
| 483 } |
| 484 |
| 485 void OnChannelError() override { NOTREACHED(); } |
| 486 void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 487 |
| 488 private: |
| 489 IPC::Sender* sender_; |
| 490 bool receiving_valid_; |
| 491 }; |
| 492 |
| 493 void ParamTraitMesssagePipeClient(bool receiving_valid_handle) { |
| 494 ListenerThatExpectsMessagePipeUsingParamTrait listener( |
| 495 receiving_valid_handle); |
| 496 ChannelClient client(&listener, "ParamTraitValidMesssagePipeClient"); |
| 497 client.Connect(); |
| 498 listener.set_sender(client.channel()); |
| 499 |
| 500 base::MessageLoop::current()->Run(); |
| 501 |
| 502 client.Close(); |
| 503 } |
| 504 |
| 505 TEST_F(IPCChannelMojoTest, ParamTraitValidMesssagePipe) { |
| 506 InitWithMojo("ParamTraitValidMesssagePipeClient"); |
| 507 |
| 508 ListenerThatExpectsOK listener; |
| 509 CreateChannel(&listener); |
| 510 ASSERT_TRUE(ConnectChannel()); |
| 511 ASSERT_TRUE(StartClient()); |
| 512 |
| 513 TestingMessagePipe pipe; |
| 514 |
| 515 scoped_ptr<IPC::Message> message(new IPC::Message()); |
| 516 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(), |
| 517 pipe.peer.release()); |
| 518 WriteOK(pipe.self.get()); |
| 519 |
| 520 this->channel()->Send(message.release()); |
| 521 base::MessageLoop::current()->Run(); |
| 522 this->channel()->Close(); |
| 523 |
| 524 EXPECT_TRUE(WaitForClientShutdown()); |
| 525 DestroyChannel(); |
| 526 } |
| 527 |
| 528 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitValidMesssagePipeClient) { |
| 529 ParamTraitMesssagePipeClient(true); |
| 530 return 0; |
| 531 } |
| 532 |
| 533 TEST_F(IPCChannelMojoTest, ParamTraitInvalidMesssagePipe) { |
| 534 InitWithMojo("ParamTraitInvalidMesssagePipeClient"); |
| 535 |
| 536 ListenerThatExpectsOK listener; |
| 537 CreateChannel(&listener); |
| 538 ASSERT_TRUE(ConnectChannel()); |
| 539 ASSERT_TRUE(StartClient()); |
| 540 |
| 541 mojo::MessagePipeHandle invalid_handle; |
| 542 scoped_ptr<IPC::Message> message(new IPC::Message()); |
| 543 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(), |
| 544 invalid_handle); |
| 545 |
| 546 this->channel()->Send(message.release()); |
| 547 base::MessageLoop::current()->Run(); |
| 548 this->channel()->Close(); |
| 549 |
| 550 EXPECT_TRUE(WaitForClientShutdown()); |
| 551 DestroyChannel(); |
| 552 } |
| 553 |
| 554 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitInvalidMesssagePipeClient) { |
| 555 ParamTraitMesssagePipeClient(false); |
| 556 return 0; |
| 557 } |
| 558 |
445 #if defined(OS_WIN) | 559 #if defined(OS_WIN) |
446 class IPCChannelMojoDeadHandleTest : public IPCChannelMojoTestBase { | 560 class IPCChannelMojoDeadHandleTest : public IPCChannelMojoTestBase { |
447 protected: | 561 protected: |
448 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( | 562 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
449 const IPC::ChannelHandle& handle, | 563 const IPC::ChannelHandle& handle, |
450 base::SequencedTaskRunner* runner) override { | 564 base::SequencedTaskRunner* runner) override { |
451 host_.reset(new IPC::ChannelMojoHost(task_runner())); | 565 host_.reset(new IPC::ChannelMojoHost(task_runner())); |
452 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), | 566 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), |
453 handle); | 567 handle); |
454 } | 568 } |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 base::MessageLoop::current()->Run(); | 782 base::MessageLoop::current()->Run(); |
669 | 783 |
670 client.Close(); | 784 client.Close(); |
671 | 785 |
672 return 0; | 786 return 0; |
673 } | 787 } |
674 | 788 |
675 #endif // OS_LINUX | 789 #endif // OS_LINUX |
676 | 790 |
677 } // namespace | 791 } // namespace |
OLD | NEW |