OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/pseudotcp_adapter.h" | 5 #include "remoting/protocol/pseudotcp_adapter.h" |
6 | 6 |
| 7 #include <utility> |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
12 #include "base/location.h" | 13 #include "base/location.h" |
13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
14 #include "base/single_thread_task_runner.h" | 15 #include "base/run_loop.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
16 #include "jingle/glue/thread_wrapper.h" | 17 #include "jingle/glue/thread_wrapper.h" |
17 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
19 #include "net/base/test_completion_callback.h" | 20 #include "net/base/test_completion_callback.h" |
20 #include "remoting/protocol/p2p_datagram_socket.h" | 21 #include "remoting/protocol/p2p_datagram_socket.h" |
21 #include "remoting/protocol/p2p_stream_socket.h" | 22 #include "remoting/protocol/p2p_stream_socket.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 | 149 |
149 std::deque<std::vector<char> > incoming_packets_; | 150 std::deque<std::vector<char> > incoming_packets_; |
150 | 151 |
151 FakeSocket* peer_socket_; | 152 FakeSocket* peer_socket_; |
152 RateLimiter* rate_limiter_; | 153 RateLimiter* rate_limiter_; |
153 int latency_ms_; | 154 int latency_ms_; |
154 }; | 155 }; |
155 | 156 |
156 class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { | 157 class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { |
157 public: | 158 public: |
158 TCPChannelTester(base::MessageLoop* message_loop, | 159 TCPChannelTester(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
159 P2PStreamSocket* client_socket, | 160 P2PStreamSocket* client_socket, |
160 P2PStreamSocket* host_socket) | 161 P2PStreamSocket* host_socket) |
161 : message_loop_(message_loop), | 162 : task_runner_(std::move(task_runner)), |
162 host_socket_(host_socket), | 163 host_socket_(host_socket), |
163 client_socket_(client_socket), | 164 client_socket_(client_socket), |
164 done_(false), | 165 done_(false), |
165 write_errors_(0), | 166 write_errors_(0), |
166 read_errors_(0) {} | 167 read_errors_(0) {} |
167 | 168 |
168 void Start() { | 169 void Start() { |
169 message_loop_->PostTask( | 170 task_runner_->PostTask(FROM_HERE, |
170 FROM_HERE, base::Bind(&TCPChannelTester::DoStart, this)); | 171 base::Bind(&TCPChannelTester::DoStart, this)); |
171 } | 172 } |
172 | 173 |
173 void CheckResults() { | 174 void CheckResults() { |
174 EXPECT_EQ(0, write_errors_); | 175 EXPECT_EQ(0, write_errors_); |
175 EXPECT_EQ(0, read_errors_); | 176 EXPECT_EQ(0, read_errors_); |
176 | 177 |
177 ASSERT_EQ(kTestDataSize + kMessageSize, input_buffer_->capacity()); | 178 ASSERT_EQ(kTestDataSize + kMessageSize, input_buffer_->capacity()); |
178 | 179 |
179 output_buffer_->SetOffset(0); | 180 output_buffer_->SetOffset(0); |
180 ASSERT_EQ(kTestDataSize, output_buffer_->size()); | 181 ASSERT_EQ(kTestDataSize, output_buffer_->size()); |
181 | 182 |
182 EXPECT_EQ(0, memcmp(output_buffer_->data(), | 183 EXPECT_EQ(0, memcmp(output_buffer_->data(), |
183 input_buffer_->StartOfBuffer(), kTestDataSize)); | 184 input_buffer_->StartOfBuffer(), kTestDataSize)); |
184 } | 185 } |
185 | 186 |
186 protected: | 187 protected: |
187 virtual ~TCPChannelTester() {} | 188 virtual ~TCPChannelTester() {} |
188 | 189 |
189 void Done() { | 190 void Done() { |
190 done_ = true; | 191 done_ = true; |
191 message_loop_->PostTask(FROM_HERE, | 192 task_runner_->PostTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
192 base::MessageLoop::QuitWhenIdleClosure()); | |
193 } | 193 } |
194 | 194 |
195 void DoStart() { | 195 void DoStart() { |
196 InitBuffers(); | 196 InitBuffers(); |
197 DoRead(); | 197 DoRead(); |
198 DoWrite(); | 198 DoWrite(); |
199 } | 199 } |
200 | 200 |
201 void InitBuffers() { | 201 void InitBuffers() { |
202 output_buffer_ = new net::DrainableIOBuffer( | 202 output_buffer_ = new net::DrainableIOBuffer( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 // Allocate memory for the next read. | 268 // Allocate memory for the next read. |
269 input_buffer_->SetCapacity(input_buffer_->capacity() + result); | 269 input_buffer_->SetCapacity(input_buffer_->capacity() + result); |
270 if (input_buffer_->capacity() == kTestDataSize + kMessageSize) | 270 if (input_buffer_->capacity() == kTestDataSize + kMessageSize) |
271 Done(); | 271 Done(); |
272 } | 272 } |
273 } | 273 } |
274 | 274 |
275 private: | 275 private: |
276 friend class base::RefCountedThreadSafe<TCPChannelTester>; | 276 friend class base::RefCountedThreadSafe<TCPChannelTester>; |
277 | 277 |
278 base::MessageLoop* message_loop_; | 278 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
279 P2PStreamSocket* host_socket_; | 279 P2PStreamSocket* host_socket_; |
280 P2PStreamSocket* client_socket_; | 280 P2PStreamSocket* client_socket_; |
281 bool done_; | 281 bool done_; |
282 | 282 |
283 scoped_refptr<net::DrainableIOBuffer> output_buffer_; | 283 scoped_refptr<net::DrainableIOBuffer> output_buffer_; |
284 scoped_refptr<net::GrowableIOBuffer> input_buffer_; | 284 scoped_refptr<net::GrowableIOBuffer> input_buffer_; |
285 | 285 |
286 int write_errors_; | 286 int write_errors_; |
287 int read_errors_; | 287 int read_errors_; |
288 }; | 288 }; |
(...skipping 30 matching lines...) Expand all Loading... |
319 int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); | 319 int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); |
320 | 320 |
321 if (rv1 == net::ERR_IO_PENDING) | 321 if (rv1 == net::ERR_IO_PENDING) |
322 rv1 = host_connect_cb.WaitForResult(); | 322 rv1 = host_connect_cb.WaitForResult(); |
323 if (rv2 == net::ERR_IO_PENDING) | 323 if (rv2 == net::ERR_IO_PENDING) |
324 rv2 = client_connect_cb.WaitForResult(); | 324 rv2 = client_connect_cb.WaitForResult(); |
325 ASSERT_EQ(net::OK, rv1); | 325 ASSERT_EQ(net::OK, rv1); |
326 ASSERT_EQ(net::OK, rv2); | 326 ASSERT_EQ(net::OK, rv2); |
327 | 327 |
328 scoped_refptr<TCPChannelTester> tester = | 328 scoped_refptr<TCPChannelTester> tester = |
329 new TCPChannelTester(&message_loop_, host_pseudotcp_.get(), | 329 new TCPChannelTester(base::ThreadTaskRunnerHandle::Get(), |
330 client_pseudotcp_.get()); | 330 host_pseudotcp_.get(), client_pseudotcp_.get()); |
331 | 331 |
332 tester->Start(); | 332 tester->Start(); |
333 message_loop_.Run(); | 333 base::RunLoop().Run(); |
334 tester->CheckResults(); | 334 tester->CheckResults(); |
335 } | 335 } |
336 | 336 |
337 TEST_F(PseudoTcpAdapterTest, LimitedChannel) { | 337 TEST_F(PseudoTcpAdapterTest, LimitedChannel) { |
338 const int kLatencyMs = 20; | 338 const int kLatencyMs = 20; |
339 const int kPacketsPerSecond = 400; | 339 const int kPacketsPerSecond = 400; |
340 const int kBurstPackets = 10; | 340 const int kBurstPackets = 10; |
341 | 341 |
342 LeakyBucket host_limiter(kBurstPackets, kPacketsPerSecond); | 342 LeakyBucket host_limiter(kBurstPackets, kPacketsPerSecond); |
343 host_socket_->set_latency(kLatencyMs); | 343 host_socket_->set_latency(kLatencyMs); |
(...skipping 10 matching lines...) Expand all Loading... |
354 int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); | 354 int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); |
355 | 355 |
356 if (rv1 == net::ERR_IO_PENDING) | 356 if (rv1 == net::ERR_IO_PENDING) |
357 rv1 = host_connect_cb.WaitForResult(); | 357 rv1 = host_connect_cb.WaitForResult(); |
358 if (rv2 == net::ERR_IO_PENDING) | 358 if (rv2 == net::ERR_IO_PENDING) |
359 rv2 = client_connect_cb.WaitForResult(); | 359 rv2 = client_connect_cb.WaitForResult(); |
360 ASSERT_EQ(net::OK, rv1); | 360 ASSERT_EQ(net::OK, rv1); |
361 ASSERT_EQ(net::OK, rv2); | 361 ASSERT_EQ(net::OK, rv2); |
362 | 362 |
363 scoped_refptr<TCPChannelTester> tester = | 363 scoped_refptr<TCPChannelTester> tester = |
364 new TCPChannelTester(&message_loop_, host_pseudotcp_.get(), | 364 new TCPChannelTester(base::ThreadTaskRunnerHandle::Get(), |
365 client_pseudotcp_.get()); | 365 host_pseudotcp_.get(), client_pseudotcp_.get()); |
366 | 366 |
367 tester->Start(); | 367 tester->Start(); |
368 message_loop_.Run(); | 368 base::RunLoop().Run(); |
369 tester->CheckResults(); | 369 tester->CheckResults(); |
370 } | 370 } |
371 | 371 |
372 class DeleteOnConnected { | 372 class DeleteOnConnected { |
373 public: | 373 public: |
374 DeleteOnConnected(base::MessageLoop* message_loop, | 374 DeleteOnConnected(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
375 std::unique_ptr<PseudoTcpAdapter>* adapter) | 375 std::unique_ptr<PseudoTcpAdapter>* adapter) |
376 : message_loop_(message_loop), adapter_(adapter) {} | 376 : task_runner_(std::move(task_runner)), adapter_(adapter) {} |
377 void OnConnected(int error) { | 377 void OnConnected(int error) { |
378 adapter_->reset(); | 378 adapter_->reset(); |
379 message_loop_->PostTask(FROM_HERE, | 379 task_runner_->PostTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
380 base::MessageLoop::QuitWhenIdleClosure()); | |
381 } | 380 } |
382 base::MessageLoop* message_loop_; | 381 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
383 std::unique_ptr<PseudoTcpAdapter>* adapter_; | 382 std::unique_ptr<PseudoTcpAdapter>* adapter_; |
384 }; | 383 }; |
385 | 384 |
386 TEST_F(PseudoTcpAdapterTest, DeleteOnConnected) { | 385 TEST_F(PseudoTcpAdapterTest, DeleteOnConnected) { |
387 // This test verifies that deleting the adapter mid-callback doesn't lead | 386 // This test verifies that deleting the adapter mid-callback doesn't lead |
388 // to deleted structures being touched as the stack unrolls, so the failure | 387 // to deleted structures being touched as the stack unrolls, so the failure |
389 // mode is a crash rather than a normal test failure. | 388 // mode is a crash rather than a normal test failure. |
390 net::TestCompletionCallback client_connect_cb; | 389 net::TestCompletionCallback client_connect_cb; |
391 DeleteOnConnected host_delete(&message_loop_, &host_pseudotcp_); | 390 DeleteOnConnected host_delete(base::ThreadTaskRunnerHandle::Get(), |
| 391 &host_pseudotcp_); |
392 | 392 |
393 host_pseudotcp_->Connect(base::Bind(&DeleteOnConnected::OnConnected, | 393 host_pseudotcp_->Connect(base::Bind(&DeleteOnConnected::OnConnected, |
394 base::Unretained(&host_delete))); | 394 base::Unretained(&host_delete))); |
395 client_pseudotcp_->Connect(client_connect_cb.callback()); | 395 client_pseudotcp_->Connect(client_connect_cb.callback()); |
396 message_loop_.Run(); | 396 base::RunLoop().Run(); |
397 | 397 |
398 ASSERT_EQ(NULL, host_pseudotcp_.get()); | 398 ASSERT_EQ(NULL, host_pseudotcp_.get()); |
399 } | 399 } |
400 | 400 |
401 // Verify that we can send/receive data with the write-waits-for-send | 401 // Verify that we can send/receive data with the write-waits-for-send |
402 // flag set. | 402 // flag set. |
403 TEST_F(PseudoTcpAdapterTest, WriteWaitsForSendLetsDataThrough) { | 403 TEST_F(PseudoTcpAdapterTest, WriteWaitsForSendLetsDataThrough) { |
404 net::TestCompletionCallback host_connect_cb; | 404 net::TestCompletionCallback host_connect_cb; |
405 net::TestCompletionCallback client_connect_cb; | 405 net::TestCompletionCallback client_connect_cb; |
406 | 406 |
407 host_pseudotcp_->SetWriteWaitsForSend(true); | 407 host_pseudotcp_->SetWriteWaitsForSend(true); |
408 client_pseudotcp_->SetWriteWaitsForSend(true); | 408 client_pseudotcp_->SetWriteWaitsForSend(true); |
409 | 409 |
410 // Disable Nagle's algorithm because the test is slow when it is | 410 // Disable Nagle's algorithm because the test is slow when it is |
411 // enabled. | 411 // enabled. |
412 host_pseudotcp_->SetNoDelay(true); | 412 host_pseudotcp_->SetNoDelay(true); |
413 | 413 |
414 int rv1 = host_pseudotcp_->Connect(host_connect_cb.callback()); | 414 int rv1 = host_pseudotcp_->Connect(host_connect_cb.callback()); |
415 int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); | 415 int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); |
416 | 416 |
417 if (rv1 == net::ERR_IO_PENDING) | 417 if (rv1 == net::ERR_IO_PENDING) |
418 rv1 = host_connect_cb.WaitForResult(); | 418 rv1 = host_connect_cb.WaitForResult(); |
419 if (rv2 == net::ERR_IO_PENDING) | 419 if (rv2 == net::ERR_IO_PENDING) |
420 rv2 = client_connect_cb.WaitForResult(); | 420 rv2 = client_connect_cb.WaitForResult(); |
421 ASSERT_EQ(net::OK, rv1); | 421 ASSERT_EQ(net::OK, rv1); |
422 ASSERT_EQ(net::OK, rv2); | 422 ASSERT_EQ(net::OK, rv2); |
423 | 423 |
424 scoped_refptr<TCPChannelTester> tester = | 424 scoped_refptr<TCPChannelTester> tester = |
425 new TCPChannelTester(&message_loop_, host_pseudotcp_.get(), | 425 new TCPChannelTester(base::ThreadTaskRunnerHandle::Get(), |
426 client_pseudotcp_.get()); | 426 host_pseudotcp_.get(), client_pseudotcp_.get()); |
427 | 427 |
428 tester->Start(); | 428 tester->Start(); |
429 message_loop_.Run(); | 429 base::RunLoop().Run(); |
430 tester->CheckResults(); | 430 tester->CheckResults(); |
431 } | 431 } |
432 | 432 |
433 } // namespace | 433 } // namespace |
434 | 434 |
435 } // namespace protocol | 435 } // namespace protocol |
436 } // namespace remoting | 436 } // namespace remoting |
OLD | NEW |