OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/protocol/channel_multiplexer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "net/socket/socket.h" |
| 10 #include "net/socket/stream_socket.h" |
| 11 #include "remoting/base/constants.h" |
| 12 #include "remoting/protocol/connection_tester.h" |
| 13 #include "remoting/protocol/fake_session.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace remoting { |
| 18 namespace protocol { |
| 19 |
| 20 namespace { |
| 21 |
| 22 const int kMessageSize = 1024; |
| 23 const int kMessages = 100; |
| 24 |
| 25 void QuitCurrentThread() { |
| 26 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 class ChannelMultiplexerTest : public testing::Test { |
| 32 protected: |
| 33 virtual void SetUp() OVERRIDE { |
| 34 // Create pair of multiplexers and connect them to each other. |
| 35 host_mux_.reset(new ChannelMultiplexer(&host_session_)); |
| 36 client_mux_.reset(new ChannelMultiplexer(&client_session_)); |
| 37 FakeSocket* host_socket = |
| 38 host_session_.GetStreamChannel(ChannelMultiplexer::kMuxChannelName); |
| 39 FakeSocket* client_socket = |
| 40 client_session_.GetStreamChannel(ChannelMultiplexer::kMuxChannelName); |
| 41 host_socket->PairWith(client_socket); |
| 42 |
| 43 // Make writes asynchronous in one direction. |
| 44 host_socket->set_async_write(true); |
| 45 client_socket->set_async_write(true); |
| 46 } |
| 47 |
| 48 void CreateChannel(const std::string& name, |
| 49 scoped_ptr<net::StreamSocket>* host_socket, |
| 50 scoped_ptr<net::StreamSocket>* client_socket) { |
| 51 int counter = 2; |
| 52 host_mux_->CreateStreamChannel(name, base::Bind( |
| 53 &ChannelMultiplexerTest::OnChannelConnected, base::Unretained(this), |
| 54 host_socket, &counter)); |
| 55 client_mux_->CreateStreamChannel(name, base::Bind( |
| 56 &ChannelMultiplexerTest::OnChannelConnected, base::Unretained(this), |
| 57 client_socket, &counter)); |
| 58 |
| 59 message_loop_.Run(); |
| 60 |
| 61 EXPECT_TRUE(host_socket->get()); |
| 62 EXPECT_TRUE(client_socket->get()); |
| 63 } |
| 64 |
| 65 void OnChannelConnected( |
| 66 scoped_ptr<net::StreamSocket>* storage, |
| 67 int* counter, |
| 68 scoped_ptr<net::StreamSocket> socket) { |
| 69 *storage = socket.Pass(); |
| 70 --(*counter); |
| 71 EXPECT_GE(*counter, 0); |
| 72 if (*counter == 0) |
| 73 QuitCurrentThread(); |
| 74 } |
| 75 |
| 76 MessageLoop message_loop_; |
| 77 |
| 78 FakeSession host_session_; |
| 79 FakeSession client_session_; |
| 80 |
| 81 scoped_ptr<ChannelMultiplexer> host_mux_; |
| 82 scoped_ptr<ChannelMultiplexer> client_mux_; |
| 83 }; |
| 84 |
| 85 |
| 86 TEST_F(ChannelMultiplexerTest, OneChannel) { |
| 87 scoped_ptr<net::StreamSocket> host_socket; |
| 88 scoped_ptr<net::StreamSocket> client_socket; |
| 89 ASSERT_NO_FATAL_FAILURE(CreateChannel("test", &host_socket, &client_socket)); |
| 90 |
| 91 StreamConnectionTester tester(host_socket.get(), client_socket.get(), |
| 92 kMessageSize, kMessages); |
| 93 tester.Start(); |
| 94 message_loop_.Run(); |
| 95 tester.CheckResults(); |
| 96 } |
| 97 |
| 98 TEST_F(ChannelMultiplexerTest, TwoChannels) { |
| 99 scoped_ptr<net::StreamSocket> host_socket1; |
| 100 scoped_ptr<net::StreamSocket> client_socket1; |
| 101 ASSERT_NO_FATAL_FAILURE( |
| 102 CreateChannel("test", &host_socket1, &client_socket1)); |
| 103 |
| 104 scoped_ptr<net::StreamSocket> host_socket2; |
| 105 scoped_ptr<net::StreamSocket> client_socket2; |
| 106 ASSERT_NO_FATAL_FAILURE( |
| 107 CreateChannel("ch2", &host_socket2, &client_socket2)); |
| 108 |
| 109 StreamConnectionTester tester1(host_socket1.get(), client_socket1.get(), |
| 110 kMessageSize, kMessages); |
| 111 StreamConnectionTester tester2(host_socket2.get(), client_socket2.get(), |
| 112 kMessageSize, kMessages); |
| 113 tester1.Start(); |
| 114 tester2.Start(); |
| 115 while (!tester1.done() || !tester2.done()) { |
| 116 message_loop_.Run(); |
| 117 } |
| 118 tester1.CheckResults(); |
| 119 tester2.CheckResults(); |
| 120 } |
| 121 |
| 122 // Four channels, two in each direction |
| 123 TEST_F(ChannelMultiplexerTest, FourChannels) { |
| 124 scoped_ptr<net::StreamSocket> host_socket1; |
| 125 scoped_ptr<net::StreamSocket> client_socket1; |
| 126 ASSERT_NO_FATAL_FAILURE( |
| 127 CreateChannel("test", &host_socket1, &client_socket1)); |
| 128 |
| 129 scoped_ptr<net::StreamSocket> host_socket2; |
| 130 scoped_ptr<net::StreamSocket> client_socket2; |
| 131 ASSERT_NO_FATAL_FAILURE( |
| 132 CreateChannel("ch2", &host_socket2, &client_socket2)); |
| 133 |
| 134 scoped_ptr<net::StreamSocket> host_socket3; |
| 135 scoped_ptr<net::StreamSocket> client_socket3; |
| 136 ASSERT_NO_FATAL_FAILURE( |
| 137 CreateChannel("test3", &host_socket3, &client_socket3)); |
| 138 |
| 139 scoped_ptr<net::StreamSocket> host_socket4; |
| 140 scoped_ptr<net::StreamSocket> client_socket4; |
| 141 ASSERT_NO_FATAL_FAILURE( |
| 142 CreateChannel("ch4", &host_socket4, &client_socket4)); |
| 143 |
| 144 StreamConnectionTester tester1(host_socket1.get(), client_socket1.get(), |
| 145 kMessageSize, kMessages); |
| 146 StreamConnectionTester tester2(host_socket2.get(), client_socket2.get(), |
| 147 kMessageSize, kMessages); |
| 148 StreamConnectionTester tester3(client_socket3.get(), host_socket3.get(), |
| 149 kMessageSize, kMessages); |
| 150 StreamConnectionTester tester4(client_socket4.get(), host_socket4.get(), |
| 151 kMessageSize, kMessages); |
| 152 tester1.Start(); |
| 153 tester2.Start(); |
| 154 tester3.Start(); |
| 155 tester4.Start(); |
| 156 while (!tester1.done() || !tester2.done() || |
| 157 !tester3.done() || !tester4.done()) { |
| 158 message_loop_.Run(); |
| 159 } |
| 160 tester1.Start(); |
| 161 tester2.Start(); |
| 162 tester3.Start(); |
| 163 tester4.Start(); |
| 164 } |
| 165 |
| 166 } // namespace protocol |
| 167 } // namespace remoting |
OLD | NEW |