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 760 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
771 // delivered properly. | 771 // delivered properly. |
772 | 772 |
773 // Close everything that belongs to us. | 773 // Close everything that belongs to us. |
774 mp0->Close(0); | 774 mp0->Close(0); |
775 mp1->Close(1); | 775 mp1->Close(1); |
776 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 776 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
777 // Note that |local_mp|'s port 0 belong to |dispatcher|, which was closed. | 777 // Note that |local_mp|'s port 0 belong to |dispatcher|, which was closed. |
778 local_mp->Close(1); | 778 local_mp->Close(1); |
779 } | 779 } |
780 | 780 |
| 781 TEST_F(RemoteMessagePipeTest, HandlePassingHalfClosed) { |
| 782 static const char kHello[] = "hello"; |
| 783 static const char kWorld[] = "world!"; |
| 784 Waiter waiter; |
| 785 HandleSignalsState hss; |
| 786 uint32_t context = 0; |
| 787 |
| 788 // We'll try to pass this dispatcher. |
| 789 scoped_refptr<MessagePipeDispatcher> dispatcher( |
| 790 new MessagePipeDispatcher(MessagePipeDispatcher::kDefaultCreateOptions)); |
| 791 scoped_refptr<MessagePipe> local_mp(MessagePipe::CreateLocalLocal()); |
| 792 dispatcher->Init(local_mp, 0); |
| 793 |
| 794 hss = local_mp->GetHandleSignalsState(0); |
| 795 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
| 796 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 797 hss.satisfiable_signals); |
| 798 // Write to the other end (|local_mp|, port 1), and then close it. |
| 799 EXPECT_EQ(MOJO_RESULT_OK, |
| 800 local_mp->WriteMessage(1, |
| 801 UserPointer<const void>(kHello), |
| 802 sizeof(kHello), |
| 803 nullptr, |
| 804 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 805 hss = local_mp->GetHandleSignalsState(0); |
| 806 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 807 hss.satisfied_signals); |
| 808 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 809 hss.satisfiable_signals); |
| 810 // Then the second message.... |
| 811 EXPECT_EQ(MOJO_RESULT_OK, |
| 812 local_mp->WriteMessage(1, |
| 813 UserPointer<const void>(kWorld), |
| 814 sizeof(kWorld), |
| 815 nullptr, |
| 816 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 817 hss = local_mp->GetHandleSignalsState(0); |
| 818 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 819 hss.satisfied_signals); |
| 820 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 821 hss.satisfiable_signals); |
| 822 // Then close it. |
| 823 local_mp->Close(1); |
| 824 |
| 825 scoped_refptr<ChannelEndpoint> ep0; |
| 826 scoped_refptr<MessagePipe> mp0(MessagePipe::CreateLocalProxy(&ep0)); |
| 827 scoped_refptr<ChannelEndpoint> ep1; |
| 828 scoped_refptr<MessagePipe> mp1(MessagePipe::CreateProxyLocal(&ep1)); |
| 829 BootstrapChannelEndpoints(ep0, ep1); |
| 830 |
| 831 // Prepare to wait on MP 1, port 1. (Add the waiter now. Otherwise, if we do |
| 832 // it later, it might already be readable.) |
| 833 waiter.Init(); |
| 834 ASSERT_EQ( |
| 835 MOJO_RESULT_OK, |
| 836 mp1->AddWaiter(1, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, nullptr)); |
| 837 |
| 838 // Write to MP 0, port 0. |
| 839 { |
| 840 DispatcherTransport transport( |
| 841 test::DispatcherTryStartTransport(dispatcher.get())); |
| 842 EXPECT_TRUE(transport.is_valid()); |
| 843 |
| 844 std::vector<DispatcherTransport> transports; |
| 845 transports.push_back(transport); |
| 846 EXPECT_EQ(MOJO_RESULT_OK, |
| 847 mp0->WriteMessage(0, |
| 848 UserPointer<const void>(kHello), |
| 849 sizeof(kHello), |
| 850 &transports, |
| 851 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 852 transport.End(); |
| 853 |
| 854 // |dispatcher| should have been closed. This is |DCHECK()|ed when the |
| 855 // |dispatcher| is destroyed. |
| 856 EXPECT_TRUE(dispatcher->HasOneRef()); |
| 857 dispatcher = nullptr; |
| 858 } |
| 859 |
| 860 // Wait. |
| 861 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(MOJO_DEADLINE_INDEFINITE, &context)); |
| 862 EXPECT_EQ(123u, context); |
| 863 hss = HandleSignalsState(); |
| 864 mp1->RemoveWaiter(1, &waiter, &hss); |
| 865 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 866 hss.satisfied_signals); |
| 867 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 868 hss.satisfiable_signals); |
| 869 |
| 870 // Read from MP 1, port 1. |
| 871 char read_buffer[100] = {0}; |
| 872 uint32_t read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); |
| 873 DispatcherVector read_dispatchers; |
| 874 uint32_t read_num_dispatchers = 10; // Maximum to get. |
| 875 EXPECT_EQ(MOJO_RESULT_OK, |
| 876 mp1->ReadMessage(1, |
| 877 UserPointer<void>(read_buffer), |
| 878 MakeUserPointer(&read_buffer_size), |
| 879 &read_dispatchers, |
| 880 &read_num_dispatchers, |
| 881 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 882 EXPECT_EQ(sizeof(kHello), static_cast<size_t>(read_buffer_size)); |
| 883 EXPECT_STREQ(kHello, read_buffer); |
| 884 EXPECT_EQ(1u, read_dispatchers.size()); |
| 885 EXPECT_EQ(1u, read_num_dispatchers); |
| 886 ASSERT_TRUE(read_dispatchers[0].get()); |
| 887 EXPECT_TRUE(read_dispatchers[0]->HasOneRef()); |
| 888 |
| 889 EXPECT_EQ(Dispatcher::kTypeMessagePipe, read_dispatchers[0]->GetType()); |
| 890 dispatcher = static_cast<MessagePipeDispatcher*>(read_dispatchers[0].get()); |
| 891 |
| 892 // |dispatcher| should already be readable and not writable. |
| 893 hss = dispatcher->GetHandleSignalsState(); |
| 894 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
| 895 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
| 896 // So read from it. |
| 897 memset(read_buffer, 0, sizeof(read_buffer)); |
| 898 read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); |
| 899 EXPECT_EQ(MOJO_RESULT_OK, |
| 900 dispatcher->ReadMessage(UserPointer<void>(read_buffer), |
| 901 MakeUserPointer(&read_buffer_size), |
| 902 0, |
| 903 nullptr, |
| 904 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 905 EXPECT_EQ(sizeof(kHello), static_cast<size_t>(read_buffer_size)); |
| 906 EXPECT_STREQ(kHello, read_buffer); |
| 907 // It should still be readable. |
| 908 hss = dispatcher->GetHandleSignalsState(); |
| 909 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
| 910 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
| 911 // So read from it. |
| 912 memset(read_buffer, 0, sizeof(read_buffer)); |
| 913 read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); |
| 914 EXPECT_EQ(MOJO_RESULT_OK, |
| 915 dispatcher->ReadMessage(UserPointer<void>(read_buffer), |
| 916 MakeUserPointer(&read_buffer_size), |
| 917 0, |
| 918 nullptr, |
| 919 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 920 EXPECT_EQ(sizeof(kWorld), static_cast<size_t>(read_buffer_size)); |
| 921 EXPECT_STREQ(kWorld, read_buffer); |
| 922 // Now it should no longer be readable. |
| 923 hss = dispatcher->GetHandleSignalsState(); |
| 924 EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, hss.satisfied_signals); |
| 925 EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, hss.satisfiable_signals); |
| 926 |
| 927 // Close everything that belongs to us. |
| 928 mp0->Close(0); |
| 929 mp1->Close(1); |
| 930 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
| 931 } |
| 932 |
781 #if defined(OS_POSIX) | 933 #if defined(OS_POSIX) |
782 #define MAYBE_SharedBufferPassing SharedBufferPassing | 934 #define MAYBE_SharedBufferPassing SharedBufferPassing |
783 #else | 935 #else |
784 // Not yet implemented (on Windows). | 936 // Not yet implemented (on Windows). |
785 #define MAYBE_SharedBufferPassing DISABLED_SharedBufferPassing | 937 #define MAYBE_SharedBufferPassing DISABLED_SharedBufferPassing |
786 #endif | 938 #endif |
787 TEST_F(RemoteMessagePipeTest, MAYBE_SharedBufferPassing) { | 939 TEST_F(RemoteMessagePipeTest, MAYBE_SharedBufferPassing) { |
788 static const char kHello[] = "hello"; | 940 static const char kHello[] = "hello"; |
789 Waiter waiter; | 941 Waiter waiter; |
790 HandleSignalsState hss; | 942 HandleSignalsState hss; |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1288 mp0->Close(0); | 1440 mp0->Close(0); |
1289 mp1->Close(1); | 1441 mp1->Close(1); |
1290 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 1442 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
1291 // Note that |local_mp|'s port 0 belong to |dispatcher|, which was closed. | 1443 // Note that |local_mp|'s port 0 belong to |dispatcher|, which was closed. |
1292 local_mp->Close(1); | 1444 local_mp->Close(1); |
1293 } | 1445 } |
1294 | 1446 |
1295 } // namespace | 1447 } // namespace |
1296 } // namespace system | 1448 } // namespace system |
1297 } // namespace mojo | 1449 } // namespace mojo |
OLD | NEW |