OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "remoting/protocol/channel_multiplexer.h" | 5 #include "remoting/protocol/channel_multiplexer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "net/socket/socket.h" | 10 #include "net/socket/socket.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 host_mux_.reset(); | 49 host_mux_.reset(); |
50 client_mux_.reset(); | 50 client_mux_.reset(); |
51 } | 51 } |
52 | 52 |
53 protected: | 53 protected: |
54 virtual void SetUp() OVERRIDE { | 54 virtual void SetUp() OVERRIDE { |
55 // Create pair of multiplexers and connect them to each other. | 55 // Create pair of multiplexers and connect them to each other. |
56 host_mux_.reset(new ChannelMultiplexer(&host_session_, kMuxChannelName)); | 56 host_mux_.reset(new ChannelMultiplexer(&host_session_, kMuxChannelName)); |
57 client_mux_.reset(new ChannelMultiplexer(&client_session_, | 57 client_mux_.reset(new ChannelMultiplexer(&client_session_, |
58 kMuxChannelName)); | 58 kMuxChannelName)); |
| 59 } |
| 60 |
| 61 // Connect sockets to each other. Must be called after we've created at least |
| 62 // one channel with each multiplexer. |
| 63 void ConnectSockets() { |
59 FakeSocket* host_socket = | 64 FakeSocket* host_socket = |
60 host_session_.GetStreamChannel(ChannelMultiplexer::kMuxChannelName); | 65 host_session_.GetStreamChannel(ChannelMultiplexer::kMuxChannelName); |
61 FakeSocket* client_socket = | 66 FakeSocket* client_socket = |
62 client_session_.GetStreamChannel(ChannelMultiplexer::kMuxChannelName); | 67 client_session_.GetStreamChannel(ChannelMultiplexer::kMuxChannelName); |
63 host_socket->PairWith(client_socket); | 68 host_socket->PairWith(client_socket); |
64 | 69 |
65 // Make writes asynchronous in one direction. | 70 // Make writes asynchronous in one direction. |
66 host_socket->set_async_write(true); | 71 host_socket->set_async_write(true); |
67 } | 72 } |
68 | 73 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 scoped_ptr<net::StreamSocket> host_socket2_; | 121 scoped_ptr<net::StreamSocket> host_socket2_; |
117 scoped_ptr<net::StreamSocket> client_socket2_; | 122 scoped_ptr<net::StreamSocket> client_socket2_; |
118 }; | 123 }; |
119 | 124 |
120 | 125 |
121 TEST_F(ChannelMultiplexerTest, OneChannel) { | 126 TEST_F(ChannelMultiplexerTest, OneChannel) { |
122 scoped_ptr<net::StreamSocket> host_socket; | 127 scoped_ptr<net::StreamSocket> host_socket; |
123 scoped_ptr<net::StreamSocket> client_socket; | 128 scoped_ptr<net::StreamSocket> client_socket; |
124 ASSERT_NO_FATAL_FAILURE(CreateChannel("test", &host_socket, &client_socket)); | 129 ASSERT_NO_FATAL_FAILURE(CreateChannel("test", &host_socket, &client_socket)); |
125 | 130 |
| 131 ConnectSockets(); |
| 132 |
126 StreamConnectionTester tester(host_socket.get(), client_socket.get(), | 133 StreamConnectionTester tester(host_socket.get(), client_socket.get(), |
127 kMessageSize, kMessages); | 134 kMessageSize, kMessages); |
128 tester.Start(); | 135 tester.Start(); |
129 message_loop_.Run(); | 136 message_loop_.Run(); |
130 tester.CheckResults(); | 137 tester.CheckResults(); |
131 } | 138 } |
132 | 139 |
133 TEST_F(ChannelMultiplexerTest, TwoChannels) { | 140 TEST_F(ChannelMultiplexerTest, TwoChannels) { |
134 scoped_ptr<net::StreamSocket> host_socket1_; | 141 scoped_ptr<net::StreamSocket> host_socket1_; |
135 scoped_ptr<net::StreamSocket> client_socket1_; | 142 scoped_ptr<net::StreamSocket> client_socket1_; |
136 ASSERT_NO_FATAL_FAILURE( | 143 ASSERT_NO_FATAL_FAILURE( |
137 CreateChannel("test", &host_socket1_, &client_socket1_)); | 144 CreateChannel("test", &host_socket1_, &client_socket1_)); |
138 | 145 |
139 scoped_ptr<net::StreamSocket> host_socket2_; | 146 scoped_ptr<net::StreamSocket> host_socket2_; |
140 scoped_ptr<net::StreamSocket> client_socket2_; | 147 scoped_ptr<net::StreamSocket> client_socket2_; |
141 ASSERT_NO_FATAL_FAILURE( | 148 ASSERT_NO_FATAL_FAILURE( |
142 CreateChannel("ch2", &host_socket2_, &client_socket2_)); | 149 CreateChannel("ch2", &host_socket2_, &client_socket2_)); |
143 | 150 |
| 151 ConnectSockets(); |
| 152 |
144 StreamConnectionTester tester1(host_socket1_.get(), client_socket1_.get(), | 153 StreamConnectionTester tester1(host_socket1_.get(), client_socket1_.get(), |
145 kMessageSize, kMessages); | 154 kMessageSize, kMessages); |
146 StreamConnectionTester tester2(host_socket2_.get(), client_socket2_.get(), | 155 StreamConnectionTester tester2(host_socket2_.get(), client_socket2_.get(), |
147 kMessageSize, kMessages); | 156 kMessageSize, kMessages); |
148 tester1.Start(); | 157 tester1.Start(); |
149 tester2.Start(); | 158 tester2.Start(); |
150 while (!tester1.done() || !tester2.done()) { | 159 while (!tester1.done() || !tester2.done()) { |
151 message_loop_.Run(); | 160 message_loop_.Run(); |
152 } | 161 } |
153 tester1.CheckResults(); | 162 tester1.CheckResults(); |
(...skipping 15 matching lines...) Expand all Loading... |
169 scoped_ptr<net::StreamSocket> host_socket3; | 178 scoped_ptr<net::StreamSocket> host_socket3; |
170 scoped_ptr<net::StreamSocket> client_socket3; | 179 scoped_ptr<net::StreamSocket> client_socket3; |
171 ASSERT_NO_FATAL_FAILURE( | 180 ASSERT_NO_FATAL_FAILURE( |
172 CreateChannel("test3", &host_socket3, &client_socket3)); | 181 CreateChannel("test3", &host_socket3, &client_socket3)); |
173 | 182 |
174 scoped_ptr<net::StreamSocket> host_socket4; | 183 scoped_ptr<net::StreamSocket> host_socket4; |
175 scoped_ptr<net::StreamSocket> client_socket4; | 184 scoped_ptr<net::StreamSocket> client_socket4; |
176 ASSERT_NO_FATAL_FAILURE( | 185 ASSERT_NO_FATAL_FAILURE( |
177 CreateChannel("ch4", &host_socket4, &client_socket4)); | 186 CreateChannel("ch4", &host_socket4, &client_socket4)); |
178 | 187 |
| 188 ConnectSockets(); |
| 189 |
179 StreamConnectionTester tester1(host_socket1_.get(), client_socket1_.get(), | 190 StreamConnectionTester tester1(host_socket1_.get(), client_socket1_.get(), |
180 kMessageSize, kMessages); | 191 kMessageSize, kMessages); |
181 StreamConnectionTester tester2(host_socket2_.get(), client_socket2_.get(), | 192 StreamConnectionTester tester2(host_socket2_.get(), client_socket2_.get(), |
182 kMessageSize, kMessages); | 193 kMessageSize, kMessages); |
183 StreamConnectionTester tester3(client_socket3.get(), host_socket3.get(), | 194 StreamConnectionTester tester3(client_socket3.get(), host_socket3.get(), |
184 kMessageSize, kMessages); | 195 kMessageSize, kMessages); |
185 StreamConnectionTester tester4(client_socket4.get(), host_socket4.get(), | 196 StreamConnectionTester tester4(client_socket4.get(), host_socket4.get(), |
186 kMessageSize, kMessages); | 197 kMessageSize, kMessages); |
187 tester1.Start(); | 198 tester1.Start(); |
188 tester2.Start(); | 199 tester2.Start(); |
(...skipping 13 matching lines...) Expand all Loading... |
202 scoped_ptr<net::StreamSocket> host_socket1_; | 213 scoped_ptr<net::StreamSocket> host_socket1_; |
203 scoped_ptr<net::StreamSocket> client_socket1_; | 214 scoped_ptr<net::StreamSocket> client_socket1_; |
204 ASSERT_NO_FATAL_FAILURE( | 215 ASSERT_NO_FATAL_FAILURE( |
205 CreateChannel("test", &host_socket1_, &client_socket1_)); | 216 CreateChannel("test", &host_socket1_, &client_socket1_)); |
206 | 217 |
207 scoped_ptr<net::StreamSocket> host_socket2_; | 218 scoped_ptr<net::StreamSocket> host_socket2_; |
208 scoped_ptr<net::StreamSocket> client_socket2_; | 219 scoped_ptr<net::StreamSocket> client_socket2_; |
209 ASSERT_NO_FATAL_FAILURE( | 220 ASSERT_NO_FATAL_FAILURE( |
210 CreateChannel("ch2", &host_socket2_, &client_socket2_)); | 221 CreateChannel("ch2", &host_socket2_, &client_socket2_)); |
211 | 222 |
| 223 ConnectSockets(); |
| 224 |
212 host_session_.GetStreamChannel(kMuxChannelName)-> | 225 host_session_.GetStreamChannel(kMuxChannelName)-> |
213 set_next_write_error(net::ERR_FAILED); | 226 set_next_write_error(net::ERR_FAILED); |
214 host_session_.GetStreamChannel(kMuxChannelName)-> | 227 host_session_.GetStreamChannel(kMuxChannelName)-> |
215 set_async_write(false); | 228 set_async_write(false); |
216 | 229 |
217 scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100); | 230 scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100); |
218 | 231 |
219 MockSocketCallback cb1; | 232 MockSocketCallback cb1; |
220 MockSocketCallback cb2; | 233 MockSocketCallback cb2; |
221 | 234 |
(...skipping 10 matching lines...) Expand all Loading... |
232 message_loop_.RunAllPending(); | 245 message_loop_.RunAllPending(); |
233 } | 246 } |
234 | 247 |
235 TEST_F(ChannelMultiplexerTest, AsyncFail) { | 248 TEST_F(ChannelMultiplexerTest, AsyncFail) { |
236 ASSERT_NO_FATAL_FAILURE( | 249 ASSERT_NO_FATAL_FAILURE( |
237 CreateChannel("test", &host_socket1_, &client_socket1_)); | 250 CreateChannel("test", &host_socket1_, &client_socket1_)); |
238 | 251 |
239 ASSERT_NO_FATAL_FAILURE( | 252 ASSERT_NO_FATAL_FAILURE( |
240 CreateChannel("ch2", &host_socket2_, &client_socket2_)); | 253 CreateChannel("ch2", &host_socket2_, &client_socket2_)); |
241 | 254 |
| 255 ConnectSockets(); |
| 256 |
242 host_session_.GetStreamChannel(kMuxChannelName)-> | 257 host_session_.GetStreamChannel(kMuxChannelName)-> |
243 set_next_write_error(net::ERR_FAILED); | 258 set_next_write_error(net::ERR_FAILED); |
244 host_session_.GetStreamChannel(kMuxChannelName)-> | 259 host_session_.GetStreamChannel(kMuxChannelName)-> |
245 set_async_write(true); | 260 set_async_write(true); |
246 | 261 |
247 scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100); | 262 scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100); |
248 | 263 |
249 MockSocketCallback cb1; | 264 MockSocketCallback cb1; |
250 MockSocketCallback cb2; | 265 MockSocketCallback cb2; |
251 EXPECT_CALL(cb1, OnDone(net::ERR_FAILED)); | 266 EXPECT_CALL(cb1, OnDone(net::ERR_FAILED)); |
252 EXPECT_CALL(cb2, OnDone(net::ERR_FAILED)); | 267 EXPECT_CALL(cb2, OnDone(net::ERR_FAILED)); |
253 | 268 |
254 EXPECT_EQ(net::ERR_IO_PENDING, | 269 EXPECT_EQ(net::ERR_IO_PENDING, |
255 host_socket1_->Write(buf, buf->size(), base::Bind( | 270 host_socket1_->Write(buf, buf->size(), base::Bind( |
256 &MockSocketCallback::OnDone, base::Unretained(&cb1)))); | 271 &MockSocketCallback::OnDone, base::Unretained(&cb1)))); |
257 EXPECT_EQ(net::ERR_IO_PENDING, | 272 EXPECT_EQ(net::ERR_IO_PENDING, |
258 host_socket2_->Write(buf, buf->size(), base::Bind( | 273 host_socket2_->Write(buf, buf->size(), base::Bind( |
259 &MockSocketCallback::OnDone, base::Unretained(&cb2)))); | 274 &MockSocketCallback::OnDone, base::Unretained(&cb2)))); |
260 | 275 |
261 message_loop_.RunAllPending(); | 276 message_loop_.RunAllPending(); |
262 } | 277 } |
263 | 278 |
264 TEST_F(ChannelMultiplexerTest, DeleteWhenFailed) { | 279 TEST_F(ChannelMultiplexerTest, DeleteWhenFailed) { |
265 ASSERT_NO_FATAL_FAILURE( | 280 ASSERT_NO_FATAL_FAILURE( |
266 CreateChannel("test", &host_socket1_, &client_socket1_)); | 281 CreateChannel("test", &host_socket1_, &client_socket1_)); |
267 ASSERT_NO_FATAL_FAILURE( | 282 ASSERT_NO_FATAL_FAILURE( |
268 CreateChannel("ch2", &host_socket2_, &client_socket2_)); | 283 CreateChannel("ch2", &host_socket2_, &client_socket2_)); |
269 | 284 |
| 285 ConnectSockets(); |
| 286 |
270 host_session_.GetStreamChannel(kMuxChannelName)-> | 287 host_session_.GetStreamChannel(kMuxChannelName)-> |
271 set_next_write_error(net::ERR_FAILED); | 288 set_next_write_error(net::ERR_FAILED); |
272 host_session_.GetStreamChannel(kMuxChannelName)-> | 289 host_session_.GetStreamChannel(kMuxChannelName)-> |
273 set_async_write(true); | 290 set_async_write(true); |
274 | 291 |
275 scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100); | 292 scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100); |
276 | 293 |
277 MockSocketCallback cb1; | 294 MockSocketCallback cb1; |
278 MockSocketCallback cb2; | 295 MockSocketCallback cb2; |
279 | 296 |
(...skipping 12 matching lines...) Expand all Loading... |
292 &MockSocketCallback::OnDone, base::Unretained(&cb2)))); | 309 &MockSocketCallback::OnDone, base::Unretained(&cb2)))); |
293 | 310 |
294 message_loop_.RunAllPending(); | 311 message_loop_.RunAllPending(); |
295 | 312 |
296 // Check that the sockets were destroyed. | 313 // Check that the sockets were destroyed. |
297 EXPECT_FALSE(host_mux_.get()); | 314 EXPECT_FALSE(host_mux_.get()); |
298 } | 315 } |
299 | 316 |
300 } // namespace protocol | 317 } // namespace protocol |
301 } // namespace remoting | 318 } // namespace remoting |
OLD | NEW |