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 <stdint.h> | 5 #include <stdint.h> |
6 #include <stdio.h> | 6 #include <stdio.h> |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "mojo/common/test/test_utils.h" | 21 #include "mojo/common/test/test_utils.h" |
22 #include "mojo/embedder/platform_channel_pair.h" | 22 #include "mojo/embedder/platform_channel_pair.h" |
23 #include "mojo/embedder/scoped_platform_handle.h" | 23 #include "mojo/embedder/scoped_platform_handle.h" |
24 #include "mojo/system/channel.h" | 24 #include "mojo/system/channel.h" |
25 #include "mojo/system/local_message_pipe_endpoint.h" | 25 #include "mojo/system/local_message_pipe_endpoint.h" |
26 #include "mojo/system/message_pipe.h" | 26 #include "mojo/system/message_pipe.h" |
27 #include "mojo/system/message_pipe_dispatcher.h" | 27 #include "mojo/system/message_pipe_dispatcher.h" |
28 #include "mojo/system/platform_handle_dispatcher.h" | 28 #include "mojo/system/platform_handle_dispatcher.h" |
29 #include "mojo/system/proxy_message_pipe_endpoint.h" | 29 #include "mojo/system/proxy_message_pipe_endpoint.h" |
30 #include "mojo/system/raw_channel.h" | 30 #include "mojo/system/raw_channel.h" |
| 31 #include "mojo/system/shared_buffer_dispatcher.h" |
31 #include "mojo/system/test_utils.h" | 32 #include "mojo/system/test_utils.h" |
32 #include "mojo/system/waiter.h" | 33 #include "mojo/system/waiter.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
34 | 35 |
35 namespace mojo { | 36 namespace mojo { |
36 namespace system { | 37 namespace system { |
37 namespace { | 38 namespace { |
38 | 39 |
39 class RemoteMessagePipeTest : public testing::Test { | 40 class RemoteMessagePipeTest : public testing::Test { |
40 public: | 41 public: |
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 | 557 |
557 // Close everything that belongs to us. | 558 // Close everything that belongs to us. |
558 mp0->Close(0); | 559 mp0->Close(0); |
559 mp1->Close(1); | 560 mp1->Close(1); |
560 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 561 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
561 // Note that |local_mp|'s port 0 belong to |dispatcher|, which was closed. | 562 // Note that |local_mp|'s port 0 belong to |dispatcher|, which was closed. |
562 local_mp->Close(1); | 563 local_mp->Close(1); |
563 } | 564 } |
564 | 565 |
565 #if defined(OS_POSIX) | 566 #if defined(OS_POSIX) |
| 567 #define MAYBE_SharedBufferPassing SharedBufferPassing |
| 568 #else |
| 569 // Not yet implemented (on Windows). |
| 570 #define MAYBE_SharedBufferPassing DISABLED_SharedBufferPassing |
| 571 #endif |
| 572 TEST_F(RemoteMessagePipeTest, MAYBE_SharedBufferPassing) { |
| 573 static const char kHello[] = "hello"; |
| 574 Waiter waiter; |
| 575 |
| 576 scoped_refptr<MessagePipe> mp0(new MessagePipe( |
| 577 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), |
| 578 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); |
| 579 scoped_refptr<MessagePipe> mp1(new MessagePipe( |
| 580 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()), |
| 581 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()))); |
| 582 ConnectMessagePipes(mp0, mp1); |
| 583 |
| 584 // We'll try to pass this dispatcher. |
| 585 scoped_refptr<SharedBufferDispatcher> dispatcher; |
| 586 MojoCreateSharedBufferOptions validated_options = {}; |
| 587 EXPECT_EQ(MOJO_RESULT_OK, |
| 588 SharedBufferDispatcher::ValidateOptions(NULL, &validated_options)); |
| 589 EXPECT_EQ(MOJO_RESULT_OK, |
| 590 SharedBufferDispatcher::Create(validated_options, 100, |
| 591 &dispatcher)); |
| 592 ASSERT_TRUE(dispatcher); |
| 593 |
| 594 // Make a mapping. |
| 595 scoped_ptr<RawSharedBufferMapping> mapping0; |
| 596 EXPECT_EQ(MOJO_RESULT_OK, |
| 597 dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, |
| 598 &mapping0)); |
| 599 ASSERT_TRUE(mapping0); |
| 600 ASSERT_TRUE(mapping0->base()); |
| 601 ASSERT_EQ(100u, mapping0->length()); |
| 602 static_cast<char*>(mapping0->base())[0] = 'A'; |
| 603 static_cast<char*>(mapping0->base())[50] = 'B'; |
| 604 static_cast<char*>(mapping0->base())[99] = 'C'; |
| 605 |
| 606 // Prepare to wait on MP 1, port 1. (Add the waiter now. Otherwise, if we do |
| 607 // it later, it might already be readable.) |
| 608 waiter.Init(); |
| 609 EXPECT_EQ(MOJO_RESULT_OK, |
| 610 mp1->AddWaiter(1, &waiter, MOJO_WAIT_FLAG_READABLE, 123)); |
| 611 |
| 612 // Write to MP 0, port 0. |
| 613 { |
| 614 DispatcherTransport |
| 615 transport(test::DispatcherTryStartTransport(dispatcher.get())); |
| 616 EXPECT_TRUE(transport.is_valid()); |
| 617 |
| 618 std::vector<DispatcherTransport> transports; |
| 619 transports.push_back(transport); |
| 620 EXPECT_EQ(MOJO_RESULT_OK, |
| 621 mp0->WriteMessage(0, kHello, sizeof(kHello), &transports, |
| 622 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 623 transport.End(); |
| 624 |
| 625 // |dispatcher| should have been closed. This is |DCHECK()|ed when the |
| 626 // |dispatcher| is destroyed. |
| 627 EXPECT_TRUE(dispatcher->HasOneRef()); |
| 628 dispatcher = NULL; |
| 629 } |
| 630 |
| 631 // Wait. |
| 632 EXPECT_EQ(123, waiter.Wait(MOJO_DEADLINE_INDEFINITE)); |
| 633 mp1->RemoveWaiter(1, &waiter); |
| 634 |
| 635 // Read from MP 1, port 1. |
| 636 char read_buffer[100] = { 0 }; |
| 637 uint32_t read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); |
| 638 DispatcherVector read_dispatchers; |
| 639 uint32_t read_num_dispatchers = 10; // Maximum to get. |
| 640 EXPECT_EQ(MOJO_RESULT_OK, |
| 641 mp1->ReadMessage(1, read_buffer, &read_buffer_size, |
| 642 &read_dispatchers, &read_num_dispatchers, |
| 643 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 644 EXPECT_EQ(sizeof(kHello), static_cast<size_t>(read_buffer_size)); |
| 645 EXPECT_STREQ(kHello, read_buffer); |
| 646 EXPECT_EQ(1u, read_dispatchers.size()); |
| 647 EXPECT_EQ(1u, read_num_dispatchers); |
| 648 ASSERT_TRUE(read_dispatchers[0]); |
| 649 EXPECT_TRUE(read_dispatchers[0]->HasOneRef()); |
| 650 |
| 651 EXPECT_EQ(Dispatcher::kTypeSharedBuffer, read_dispatchers[0]->GetType()); |
| 652 dispatcher = |
| 653 static_cast<SharedBufferDispatcher*>(read_dispatchers[0].get()); |
| 654 |
| 655 // Make another mapping. |
| 656 scoped_ptr<RawSharedBufferMapping> mapping1; |
| 657 EXPECT_EQ(MOJO_RESULT_OK, |
| 658 dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, |
| 659 &mapping1)); |
| 660 ASSERT_TRUE(mapping1); |
| 661 ASSERT_TRUE(mapping1->base()); |
| 662 ASSERT_EQ(100u, mapping1->length()); |
| 663 EXPECT_NE(mapping1->base(), mapping0->base()); |
| 664 EXPECT_EQ('A', static_cast<char*>(mapping1->base())[0]); |
| 665 EXPECT_EQ('B', static_cast<char*>(mapping1->base())[50]); |
| 666 EXPECT_EQ('C', static_cast<char*>(mapping1->base())[99]); |
| 667 |
| 668 // Write stuff either way. |
| 669 static_cast<char*>(mapping1->base())[1] = 'x'; |
| 670 EXPECT_EQ('x', static_cast<char*>(mapping0->base())[1]); |
| 671 static_cast<char*>(mapping0->base())[2] = 'y'; |
| 672 EXPECT_EQ('y', static_cast<char*>(mapping1->base())[2]); |
| 673 |
| 674 // Kill the first mapping; the second should still be valid. |
| 675 mapping0.reset(); |
| 676 EXPECT_EQ('A', static_cast<char*>(mapping1->base())[0]); |
| 677 |
| 678 // Close everything that belongs to us. |
| 679 mp0->Close(0); |
| 680 mp1->Close(1); |
| 681 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
| 682 |
| 683 // The second mapping should still be good. |
| 684 EXPECT_EQ('x', static_cast<char*>(mapping1->base())[1]); |
| 685 } |
| 686 |
| 687 #if defined(OS_POSIX) |
566 #define MAYBE_PlatformHandlePassing PlatformHandlePassing | 688 #define MAYBE_PlatformHandlePassing PlatformHandlePassing |
567 #else | 689 #else |
568 // Not yet implemented (on Windows). | 690 // Not yet implemented (on Windows). |
569 #define MAYBE_PlatformHandlePassing DISABLED_PlatformHandlePassing | 691 #define MAYBE_PlatformHandlePassing DISABLED_PlatformHandlePassing |
570 #endif | 692 #endif |
571 TEST_F(RemoteMessagePipeTest, MAYBE_PlatformHandlePassing) { | 693 TEST_F(RemoteMessagePipeTest, MAYBE_PlatformHandlePassing) { |
572 static const char kHello[] = "hello"; | 694 static const char kHello[] = "hello"; |
573 static const char kWorld[] = "world"; | 695 static const char kWorld[] = "world"; |
574 Waiter waiter; | 696 Waiter waiter; |
575 | 697 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 | 817 |
696 mp1->Close(1); | 818 mp1->Close(1); |
697 | 819 |
698 RestoreInitialState(); | 820 RestoreInitialState(); |
699 } | 821 } |
700 } | 822 } |
701 | 823 |
702 } // namespace | 824 } // namespace |
703 } // namespace system | 825 } // namespace system |
704 } // namespace mojo | 826 } // namespace mojo |
OLD | NEW |