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" | |
22 #include "ipc/mojo/scoped_ipc_support.h" | 21 #include "ipc/mojo/scoped_ipc_support.h" |
23 | 22 |
24 #if defined(OS_POSIX) | 23 #if defined(OS_POSIX) |
25 #include "base/file_descriptor_posix.h" | 24 #include "base/file_descriptor_posix.h" |
26 #include "ipc/ipc_platform_file_attachment_posix.h" | 25 #include "ipc/ipc_platform_file_attachment_posix.h" |
27 #endif | 26 #endif |
28 | 27 |
29 namespace { | 28 namespace { |
30 | 29 |
31 class ListenerThatExpectsOK : public IPC::Listener { | 30 class ListenerThatExpectsOK : public IPC::Listener { |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 client.Connect(); | 435 client.Connect(); |
437 listener.set_sender(client.channel()); | 436 listener.set_sender(client.channel()); |
438 | 437 |
439 base::MessageLoop::current()->Run(); | 438 base::MessageLoop::current()->Run(); |
440 | 439 |
441 client.Close(); | 440 client.Close(); |
442 | 441 |
443 return 0; | 442 return 0; |
444 } | 443 } |
445 | 444 |
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 ParamTraitMessagePipeClient(bool receiving_valid_handle, | |
494 const char* channel_name) { | |
495 ListenerThatExpectsMessagePipeUsingParamTrait listener( | |
496 receiving_valid_handle); | |
497 ChannelClient client(&listener, channel_name); | |
498 client.Connect(); | |
499 listener.set_sender(client.channel()); | |
500 | |
501 base::MessageLoop::current()->Run(); | |
502 | |
503 client.Close(); | |
504 } | |
505 | |
506 TEST_F(IPCChannelMojoTest, ParamTraitValidMessagePipe) { | |
507 InitWithMojo("ParamTraitValidMessagePipeClient"); | |
508 | |
509 ListenerThatExpectsOK listener; | |
510 CreateChannel(&listener); | |
511 ASSERT_TRUE(ConnectChannel()); | |
512 ASSERT_TRUE(StartClient()); | |
513 | |
514 TestingMessagePipe pipe; | |
515 | |
516 scoped_ptr<IPC::Message> message(new IPC::Message()); | |
517 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(), | |
518 pipe.peer.release()); | |
519 WriteOK(pipe.self.get()); | |
520 | |
521 this->channel()->Send(message.release()); | |
522 base::MessageLoop::current()->Run(); | |
523 this->channel()->Close(); | |
524 | |
525 EXPECT_TRUE(WaitForClientShutdown()); | |
526 DestroyChannel(); | |
527 } | |
528 | |
529 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitValidMessagePipeClient) { | |
530 ParamTraitMessagePipeClient(true, "ParamTraitValidMessagePipeClient"); | |
531 return 0; | |
532 } | |
533 | |
534 TEST_F(IPCChannelMojoTest, ParamTraitInvalidMessagePipe) { | |
535 InitWithMojo("ParamTraitInvalidMessagePipeClient"); | |
536 | |
537 ListenerThatExpectsOK listener; | |
538 CreateChannel(&listener); | |
539 ASSERT_TRUE(ConnectChannel()); | |
540 ASSERT_TRUE(StartClient()); | |
541 | |
542 mojo::MessagePipeHandle invalid_handle; | |
543 scoped_ptr<IPC::Message> message(new IPC::Message()); | |
544 IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(), | |
545 invalid_handle); | |
546 | |
547 this->channel()->Send(message.release()); | |
548 base::MessageLoop::current()->Run(); | |
549 this->channel()->Close(); | |
550 | |
551 EXPECT_TRUE(WaitForClientShutdown()); | |
552 DestroyChannel(); | |
553 } | |
554 | |
555 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitInvalidMessagePipeClient) { | |
556 ParamTraitMessagePipeClient(false, "ParamTraitInvalidMessagePipeClient"); | |
557 return 0; | |
558 } | |
559 | |
560 #if defined(OS_WIN) | 445 #if defined(OS_WIN) |
561 class IPCChannelMojoDeadHandleTest : public IPCChannelMojoTestBase { | 446 class IPCChannelMojoDeadHandleTest : public IPCChannelMojoTestBase { |
562 protected: | 447 protected: |
563 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( | 448 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
564 const IPC::ChannelHandle& handle, | 449 const IPC::ChannelHandle& handle, |
565 base::SequencedTaskRunner* runner) override { | 450 base::SequencedTaskRunner* runner) override { |
566 host_.reset(new IPC::ChannelMojoHost(task_runner())); | 451 host_.reset(new IPC::ChannelMojoHost(task_runner())); |
567 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), | 452 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), |
568 handle); | 453 handle); |
569 } | 454 } |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 base::MessageLoop::current()->Run(); | 668 base::MessageLoop::current()->Run(); |
784 | 669 |
785 client.Close(); | 670 client.Close(); |
786 | 671 |
787 return 0; | 672 return 0; |
788 } | 673 } |
789 | 674 |
790 #endif // OS_LINUX | 675 #endif // OS_LINUX |
791 | 676 |
792 } // namespace | 677 } // namespace |
OLD | NEW |