| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // TODO(vtl): The POSIX-specific bits have been factored out. Apply this test to | 5 // TODO(vtl): The POSIX-specific bits have been factored out. Apply this test to |
| 6 // non-POSIX once we have a non-POSIX implementation. | 6 // non-POSIX once we have a non-POSIX implementation. |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/threading/platform_thread.h" // For |Sleep()|. | 18 #include "base/threading/platform_thread.h" // For |Sleep()|. |
| 19 #include "mojo/system/channel.h" | 19 #include "mojo/system/channel.h" |
| 20 #include "mojo/system/embedder/platform_channel_pair.h" | 20 #include "mojo/system/embedder/platform_channel_pair.h" |
| 21 #include "mojo/system/embedder/scoped_platform_handle.h" | 21 #include "mojo/system/embedder/scoped_platform_handle.h" |
| 22 #include "mojo/system/local_message_pipe_endpoint.h" | 22 #include "mojo/system/local_message_pipe_endpoint.h" |
| 23 #include "mojo/system/message_pipe.h" | 23 #include "mojo/system/message_pipe.h" |
| 24 #include "mojo/system/message_pipe_dispatcher.h" | 24 #include "mojo/system/message_pipe_dispatcher.h" |
| 25 #include "mojo/system/proxy_message_pipe_endpoint.h" | 25 #include "mojo/system/proxy_message_pipe_endpoint.h" |
| 26 #include "mojo/system/test_utils.h" | 26 #include "mojo/system/test_utils.h" |
| 27 #include "mojo/system/waiter.h" | 27 #include "mojo/system/waiter.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 29 |
| 29 namespace mojo { | 30 namespace mojo { |
| 30 namespace system { | 31 namespace system { |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 class RemoteMessagePipeTest : public test::TestWithIOThreadBase { | 34 class RemoteMessagePipeTest : public testing::Test { |
| 34 public: | 35 public: |
| 35 RemoteMessagePipeTest() {} | 36 RemoteMessagePipeTest() {} |
| 36 virtual ~RemoteMessagePipeTest() {} | 37 virtual ~RemoteMessagePipeTest() {} |
| 37 | 38 |
| 38 virtual void SetUp() OVERRIDE { | 39 virtual void SetUp() OVERRIDE { |
| 39 test::TestWithIOThreadBase::SetUp(); | 40 test::PostTaskAndWait(io_thread()->task_runner(), |
| 40 test::PostTaskAndWait(io_thread_task_runner(), | |
| 41 FROM_HERE, | 41 FROM_HERE, |
| 42 base::Bind(&RemoteMessagePipeTest::SetUpOnIOThread, | 42 base::Bind(&RemoteMessagePipeTest::SetUpOnIOThread, |
| 43 base::Unretained(this))); | 43 base::Unretained(this))); |
| 44 } | 44 } |
| 45 | 45 |
| 46 virtual void TearDown() OVERRIDE { | 46 virtual void TearDown() OVERRIDE { |
| 47 test::PostTaskAndWait(io_thread_task_runner(), | 47 test::PostTaskAndWait(io_thread()->task_runner(), |
| 48 FROM_HERE, | 48 FROM_HERE, |
| 49 base::Bind(&RemoteMessagePipeTest::TearDownOnIOThread, | 49 base::Bind(&RemoteMessagePipeTest::TearDownOnIOThread, |
| 50 base::Unretained(this))); | 50 base::Unretained(this))); |
| 51 test::TestWithIOThreadBase::TearDown(); | |
| 52 } | 51 } |
| 53 | 52 |
| 53 protected: |
| 54 // This connects MP 0, port 1 and MP 1, port 0 (leaving MP 0, port 0 and MP 1, | 54 // This connects MP 0, port 1 and MP 1, port 0 (leaving MP 0, port 0 and MP 1, |
| 55 // port 1 as the user-visible endpoints) to channel 0 and 1, respectively. MP | 55 // port 1 as the user-visible endpoints) to channel 0 and 1, respectively. MP |
| 56 // 0, port 1 and MP 1, port 0 must have |ProxyMessagePipeEndpoint|s. | 56 // 0, port 1 and MP 1, port 0 must have |ProxyMessagePipeEndpoint|s. |
| 57 void ConnectMessagePipes(scoped_refptr<MessagePipe> mp0, | 57 void ConnectMessagePipes(scoped_refptr<MessagePipe> mp0, |
| 58 scoped_refptr<MessagePipe> mp1) { | 58 scoped_refptr<MessagePipe> mp1) { |
| 59 test::PostTaskAndWait( | 59 test::PostTaskAndWait( |
| 60 io_thread_task_runner(), | 60 io_thread()->task_runner(), |
| 61 FROM_HERE, | 61 FROM_HERE, |
| 62 base::Bind(&RemoteMessagePipeTest::ConnectMessagePipesOnIOThread, | 62 base::Bind(&RemoteMessagePipeTest::ConnectMessagePipesOnIOThread, |
| 63 base::Unretained(this), mp0, mp1)); | 63 base::Unretained(this), mp0, mp1)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 // This connects |mp|'s port |channel_index ^ 1| to channel |channel_index|. | 66 // This connects |mp|'s port |channel_index ^ 1| to channel |channel_index|. |
| 67 // It assumes/requires that this is the bootstrap case, i.e., that the | 67 // It assumes/requires that this is the bootstrap case, i.e., that the |
| 68 // endpoint IDs are both/will both be |Channel::kBootstrapEndpointId|. This | 68 // endpoint IDs are both/will both be |Channel::kBootstrapEndpointId|. This |
| 69 // returns *without* waiting for it to finish connecting. | 69 // returns *without* waiting for it to finish connecting. |
| 70 void BootstrapMessagePipeNoWait(unsigned channel_index, | 70 void BootstrapMessagePipeNoWait(unsigned channel_index, |
| 71 scoped_refptr<MessagePipe> mp) { | 71 scoped_refptr<MessagePipe> mp) { |
| 72 io_thread_task_runner()->PostTask( | 72 io_thread()->task_runner()->PostTask( |
| 73 FROM_HERE, | 73 FROM_HERE, |
| 74 base::Bind(&RemoteMessagePipeTest::BootstrapMessagePipeOnIOThread, | 74 base::Bind(&RemoteMessagePipeTest::BootstrapMessagePipeOnIOThread, |
| 75 base::Unretained(this), channel_index, mp)); | 75 base::Unretained(this), channel_index, mp)); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void RestoreInitialState() { | 78 void RestoreInitialState() { |
| 79 test::PostTaskAndWait( | 79 test::PostTaskAndWait( |
| 80 io_thread_task_runner(), | 80 io_thread()->task_runner(), |
| 81 FROM_HERE, | 81 FROM_HERE, |
| 82 base::Bind(&RemoteMessagePipeTest::RestoreInitialStateOnIOThread, | 82 base::Bind(&RemoteMessagePipeTest::RestoreInitialStateOnIOThread, |
| 83 base::Unretained(this))); | 83 base::Unretained(this))); |
| 84 } | 84 } |
| 85 | 85 |
| 86 test::TestIOThread* io_thread() { return &io_thread_; } |
| 87 |
| 86 private: | 88 private: |
| 87 void SetUpOnIOThread() { | 89 void SetUpOnIOThread() { |
| 88 CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop()); | 90 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); |
| 89 | 91 |
| 90 embedder::PlatformChannelPair channel_pair; | 92 embedder::PlatformChannelPair channel_pair; |
| 91 platform_handles_[0] = channel_pair.PassServerHandle(); | 93 platform_handles_[0] = channel_pair.PassServerHandle(); |
| 92 platform_handles_[1] = channel_pair.PassClientHandle(); | 94 platform_handles_[1] = channel_pair.PassClientHandle(); |
| 93 } | 95 } |
| 94 | 96 |
| 95 void TearDownOnIOThread() { | 97 void TearDownOnIOThread() { |
| 96 CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop()); | 98 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); |
| 97 | 99 |
| 98 if (channels_[0].get()) { | 100 if (channels_[0].get()) { |
| 99 channels_[0]->Shutdown(); | 101 channels_[0]->Shutdown(); |
| 100 channels_[0] = NULL; | 102 channels_[0] = NULL; |
| 101 } | 103 } |
| 102 if (channels_[1].get()) { | 104 if (channels_[1].get()) { |
| 103 channels_[1]->Shutdown(); | 105 channels_[1]->Shutdown(); |
| 104 channels_[1] = NULL; | 106 channels_[1] = NULL; |
| 105 } | 107 } |
| 106 } | 108 } |
| 107 | 109 |
| 108 void CreateAndInitChannel(unsigned channel_index) { | 110 void CreateAndInitChannel(unsigned channel_index) { |
| 109 CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop()); | 111 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); |
| 110 CHECK(channel_index == 0 || channel_index == 1); | 112 CHECK(channel_index == 0 || channel_index == 1); |
| 111 CHECK(!channels_[channel_index].get()); | 113 CHECK(!channels_[channel_index].get()); |
| 112 | 114 |
| 113 channels_[channel_index] = new Channel(); | 115 channels_[channel_index] = new Channel(); |
| 114 CHECK(channels_[channel_index]->Init( | 116 CHECK(channels_[channel_index]->Init( |
| 115 platform_handles_[channel_index].Pass())); | 117 platform_handles_[channel_index].Pass())); |
| 116 } | 118 } |
| 117 | 119 |
| 118 void ConnectMessagePipesOnIOThread(scoped_refptr<MessagePipe> mp0, | 120 void ConnectMessagePipesOnIOThread(scoped_refptr<MessagePipe> mp0, |
| 119 scoped_refptr<MessagePipe> mp1) { | 121 scoped_refptr<MessagePipe> mp1) { |
| 120 CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop()); | 122 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); |
| 121 | 123 |
| 122 if (!channels_[0].get()) | 124 if (!channels_[0].get()) |
| 123 CreateAndInitChannel(0); | 125 CreateAndInitChannel(0); |
| 124 if (!channels_[1].get()) | 126 if (!channels_[1].get()) |
| 125 CreateAndInitChannel(1); | 127 CreateAndInitChannel(1); |
| 126 | 128 |
| 127 MessageInTransit::EndpointId local_id0 = | 129 MessageInTransit::EndpointId local_id0 = |
| 128 channels_[0]->AttachMessagePipeEndpoint(mp0, 1); | 130 channels_[0]->AttachMessagePipeEndpoint(mp0, 1); |
| 129 MessageInTransit::EndpointId local_id1 = | 131 MessageInTransit::EndpointId local_id1 = |
| 130 channels_[1]->AttachMessagePipeEndpoint(mp1, 0); | 132 channels_[1]->AttachMessagePipeEndpoint(mp1, 0); |
| 131 | 133 |
| 132 channels_[0]->RunMessagePipeEndpoint(local_id0, local_id1); | 134 channels_[0]->RunMessagePipeEndpoint(local_id0, local_id1); |
| 133 channels_[1]->RunMessagePipeEndpoint(local_id1, local_id0); | 135 channels_[1]->RunMessagePipeEndpoint(local_id1, local_id0); |
| 134 } | 136 } |
| 135 | 137 |
| 136 void BootstrapMessagePipeOnIOThread(unsigned channel_index, | 138 void BootstrapMessagePipeOnIOThread(unsigned channel_index, |
| 137 scoped_refptr<MessagePipe> mp) { | 139 scoped_refptr<MessagePipe> mp) { |
| 138 CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop()); | 140 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); |
| 139 CHECK(channel_index == 0 || channel_index == 1); | 141 CHECK(channel_index == 0 || channel_index == 1); |
| 140 | 142 |
| 141 unsigned port = channel_index ^ 1u; | 143 unsigned port = channel_index ^ 1u; |
| 142 | 144 |
| 143 // Important: If we don't boot | 145 // Important: If we don't boot |
| 144 CreateAndInitChannel(channel_index); | 146 CreateAndInitChannel(channel_index); |
| 145 CHECK_EQ(channels_[channel_index]->AttachMessagePipeEndpoint(mp, port), | 147 CHECK_EQ(channels_[channel_index]->AttachMessagePipeEndpoint(mp, port), |
| 146 Channel::kBootstrapEndpointId); | 148 Channel::kBootstrapEndpointId); |
| 147 channels_[channel_index]->RunMessagePipeEndpoint( | 149 channels_[channel_index]->RunMessagePipeEndpoint( |
| 148 Channel::kBootstrapEndpointId, Channel::kBootstrapEndpointId); | 150 Channel::kBootstrapEndpointId, Channel::kBootstrapEndpointId); |
| 149 } | 151 } |
| 150 | 152 |
| 151 void RestoreInitialStateOnIOThread() { | 153 void RestoreInitialStateOnIOThread() { |
| 152 CHECK_EQ(base::MessageLoop::current(), io_thread_message_loop()); | 154 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); |
| 153 | 155 |
| 154 TearDownOnIOThread(); | 156 TearDownOnIOThread(); |
| 155 SetUpOnIOThread(); | 157 SetUpOnIOThread(); |
| 156 } | 158 } |
| 157 | 159 |
| 160 test::TestIOThread io_thread_; |
| 158 embedder::ScopedPlatformHandle platform_handles_[2]; | 161 embedder::ScopedPlatformHandle platform_handles_[2]; |
| 159 scoped_refptr<Channel> channels_[2]; | 162 scoped_refptr<Channel> channels_[2]; |
| 160 | 163 |
| 161 DISALLOW_COPY_AND_ASSIGN(RemoteMessagePipeTest); | 164 DISALLOW_COPY_AND_ASSIGN(RemoteMessagePipeTest); |
| 162 }; | 165 }; |
| 163 | 166 |
| 164 TEST_F(RemoteMessagePipeTest, Basic) { | 167 TEST_F(RemoteMessagePipeTest, Basic) { |
| 165 const char hello[] = "hello"; | 168 const char hello[] = "hello"; |
| 166 const char world[] = "world!!!1!!!1!"; | 169 const char world[] = "world!!!1!!!1!"; |
| 167 char buffer[100] = { 0 }; | 170 char buffer[100] = { 0 }; |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), | 565 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), |
| 563 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); | 566 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); |
| 564 BootstrapMessagePipeNoWait(0, mp0); | 567 BootstrapMessagePipeNoWait(0, mp0); |
| 565 | 568 |
| 566 scoped_refptr<MessagePipe> mp1(new MessagePipe( | 569 scoped_refptr<MessagePipe> mp1(new MessagePipe( |
| 567 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()), | 570 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()), |
| 568 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()))); | 571 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()))); |
| 569 BootstrapMessagePipeNoWait(1, mp1); | 572 BootstrapMessagePipeNoWait(1, mp1); |
| 570 | 573 |
| 571 if (i & 1u) { | 574 if (i & 1u) { |
| 572 io_thread_task_runner()->PostTask( | 575 io_thread()->task_runner()->PostTask( |
| 573 FROM_HERE, base::Bind(&base::PlatformThread::Sleep, delay)); | 576 FROM_HERE, base::Bind(&base::PlatformThread::Sleep, delay)); |
| 574 } | 577 } |
| 575 if (i & 2u) | 578 if (i & 2u) |
| 576 base::PlatformThread::Sleep(delay); | 579 base::PlatformThread::Sleep(delay); |
| 577 | 580 |
| 578 mp0->Close(0); | 581 mp0->Close(0); |
| 579 | 582 |
| 580 if (i & 4u) { | 583 if (i & 4u) { |
| 581 io_thread_task_runner()->PostTask( | 584 io_thread()->task_runner()->PostTask( |
| 582 FROM_HERE, base::Bind(&base::PlatformThread::Sleep, delay)); | 585 FROM_HERE, base::Bind(&base::PlatformThread::Sleep, delay)); |
| 583 } | 586 } |
| 584 if (i & 8u) | 587 if (i & 8u) |
| 585 base::PlatformThread::Sleep(delay); | 588 base::PlatformThread::Sleep(delay); |
| 586 | 589 |
| 587 mp1->Close(1); | 590 mp1->Close(1); |
| 588 | 591 |
| 589 RestoreInitialState(); | 592 RestoreInitialState(); |
| 590 } | 593 } |
| 591 } | 594 } |
| 592 | 595 |
| 593 } // namespace | 596 } // namespace |
| 594 } // namespace system | 597 } // namespace system |
| 595 } // namespace mojo | 598 } // namespace mojo |
| OLD | NEW |