sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 5 | #include "remoting/protocol/pseudotcp_adapter.h" |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 6 | |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 7 | #include <utility> |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
[email protected] | 31a7eef7 | 2011-11-12 21:18:16 | [diff] [blame] | 10 | #include "base/bind.h" |
| 11 | #include "base/bind_helpers.h" |
[email protected] | 3f19773 | 2011-11-17 20:08:56 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 13 | #include "base/containers/circular_deque.h" |
fdoray | b0bfadc | 2016-06-06 18:06:52 | [diff] [blame] | 14 | #include "base/location.h" |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame^] | 16 | #include "base/message_loop/message_loop.h" |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 17 | #include "base/run_loop.h" |
Gabriel Charette | 5ff87ce | 2017-05-16 18:03:45 | [diff] [blame] | 18 | #include "base/single_thread_task_runner.h" |
fdoray | b0bfadc | 2016-06-06 18:06:52 | [diff] [blame] | 19 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | e86b28f | 2011-07-29 22:21:07 | [diff] [blame] | 20 | #include "jingle/glue/thread_wrapper.h" |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 21 | #include "net/base/io_buffer.h" |
| 22 | #include "net/base/net_errors.h" |
| 23 | #include "net/base/test_completion_callback.h" |
[email protected] | f94cd970 | 2017-12-07 06:09:40 | [diff] [blame] | 24 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 25 | #include "remoting/protocol/p2p_datagram_socket.h" |
| 26 | #include "remoting/protocol/p2p_stream_socket.h" |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 27 | #include "testing/gmock/include/gmock/gmock.h" |
| 28 | #include "testing/gtest/include/gtest/gtest.h" |
| 29 | |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 30 | namespace remoting { |
| 31 | namespace protocol { |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 32 | |
| 33 | namespace { |
| 34 | |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 35 | const int kMessageSize = 1024; |
| 36 | const int kMessages = 100; |
| 37 | const int kTestDataSize = kMessages * kMessageSize; |
| 38 | |
[email protected] | bbb8ba0 | 2011-08-23 01:29:25 | [diff] [blame] | 39 | class RateLimiter { |
| 40 | public: |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 41 | virtual ~RateLimiter() = default; |
| 42 | ; |
[email protected] | bbb8ba0 | 2011-08-23 01:29:25 | [diff] [blame] | 43 | // Returns true if the new packet needs to be dropped, false otherwise. |
| 44 | virtual bool DropNextPacket() = 0; |
| 45 | }; |
| 46 | |
| 47 | class LeakyBucket : public RateLimiter { |
| 48 | public: |
| 49 | // |rate| is in drops per second. |
| 50 | LeakyBucket(double volume, double rate) |
| 51 | : volume_(volume), |
| 52 | rate_(rate), |
| 53 | level_(0.0), |
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 54 | last_update_(base::TimeTicks::Now()) { |
[email protected] | bbb8ba0 | 2011-08-23 01:29:25 | [diff] [blame] | 55 | } |
| 56 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 57 | ~LeakyBucket() override = default; |
[email protected] | bbb8ba0 | 2011-08-23 01:29:25 | [diff] [blame] | 58 | |
dcheng | 5364bdee | 2014-10-22 23:38:31 | [diff] [blame] | 59 | bool DropNextPacket() override { |
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 60 | base::TimeTicks now = base::TimeTicks::Now(); |
[email protected] | bbb8ba0 | 2011-08-23 01:29:25 | [diff] [blame] | 61 | double interval = (now - last_update_).InSecondsF(); |
| 62 | last_update_ = now; |
| 63 | level_ = level_ + 1.0 - interval * rate_; |
| 64 | if (level_ > volume_) { |
| 65 | level_ = volume_; |
| 66 | return true; |
| 67 | } else if (level_ < 0.0) { |
| 68 | level_ = 0.0; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | double volume_; |
| 75 | double rate_; |
| 76 | double level_; |
| 77 | base::TimeTicks last_update_; |
| 78 | }; |
| 79 | |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 80 | class FakeSocket : public P2PDatagramSocket { |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 81 | public: |
| 82 | FakeSocket() |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 83 | : rate_limiter_(NULL), |
[email protected] | bbb8ba0 | 2011-08-23 01:29:25 | [diff] [blame] | 84 | latency_ms_(0) { |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 85 | } |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 86 | ~FakeSocket() override = default; |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 87 | |
| 88 | void AppendInputPacket(const std::vector<char>& data) { |
[email protected] | bbb8ba0 | 2011-08-23 01:29:25 | [diff] [blame] | 89 | if (rate_limiter_ && rate_limiter_->DropNextPacket()) |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 90 | return; // Lose the packet. |
| 91 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 92 | if (!read_callback_.is_null()) { |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 93 | int size = std::min(read_buffer_size_, static_cast<int>(data.size())); |
| 94 | memcpy(read_buffer_->data(), &data[0], data.size()); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 95 | net::CompletionCallback cb = read_callback_; |
| 96 | read_callback_.Reset(); |
| 97 | read_buffer_ = NULL; |
| 98 | cb.Run(size); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 99 | } else { |
| 100 | incoming_packets_.push_back(data); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void Connect(FakeSocket* peer_socket) { |
| 105 | peer_socket_ = peer_socket; |
| 106 | } |
| 107 | |
[email protected] | bbb8ba0 | 2011-08-23 01:29:25 | [diff] [blame] | 108 | void set_rate_limiter(RateLimiter* rate_limiter) { |
| 109 | rate_limiter_ = rate_limiter; |
| 110 | }; |
| 111 | |
| 112 | void set_latency(int latency_ms) { latency_ms_ = latency_ms; }; |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 113 | |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 114 | // P2PDatagramSocket interface. |
| 115 | int Recv(const scoped_refptr<net::IOBuffer>& buf, int buf_len, |
dcheng | 5364bdee | 2014-10-22 23:38:31 | [diff] [blame] | 116 | const net::CompletionCallback& callback) override { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 117 | CHECK(read_callback_.is_null()); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 118 | CHECK(buf); |
| 119 | |
| 120 | if (incoming_packets_.size() > 0) { |
| 121 | scoped_refptr<net::IOBuffer> buffer(buf); |
| 122 | int size = std::min( |
| 123 | static_cast<int>(incoming_packets_.front().size()), buf_len); |
| 124 | memcpy(buffer->data(), &*incoming_packets_.front().begin(), size); |
| 125 | incoming_packets_.pop_front(); |
| 126 | return size; |
| 127 | } else { |
| 128 | read_callback_ = callback; |
| 129 | read_buffer_ = buf; |
| 130 | read_buffer_size_ = buf_len; |
| 131 | return net::ERR_IO_PENDING; |
| 132 | } |
| 133 | } |
| 134 | |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 135 | int Send(const scoped_refptr<net::IOBuffer>& buf, int buf_len, |
| 136 | const net::CompletionCallback& callback) override { |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 137 | DCHECK(buf); |
| 138 | if (peer_socket_) { |
fdoray | b0bfadc | 2016-06-06 18:06:52 | [diff] [blame] | 139 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | 31a7eef7 | 2011-11-12 21:18:16 | [diff] [blame] | 140 | FROM_HERE, |
| 141 | base::Bind(&FakeSocket::AppendInputPacket, |
| 142 | base::Unretained(peer_socket_), |
| 143 | std::vector<char>(buf->data(), buf->data() + buf_len)), |
[email protected] | 1e2076be | 2012-03-05 01:16:33 | [diff] [blame] | 144 | base::TimeDelta::FromMilliseconds(latency_ms_)); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return buf_len; |
| 148 | } |
| 149 | |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 150 | private: |
| 151 | scoped_refptr<net::IOBuffer> read_buffer_; |
| 152 | int read_buffer_size_; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 153 | net::CompletionCallback read_callback_; |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 154 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 155 | base::circular_deque<std::vector<char>> incoming_packets_; |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 156 | |
| 157 | FakeSocket* peer_socket_; |
[email protected] | bbb8ba0 | 2011-08-23 01:29:25 | [diff] [blame] | 158 | RateLimiter* rate_limiter_; |
| 159 | int latency_ms_; |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { |
| 163 | public: |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 164 | TCPChannelTester(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 165 | P2PStreamSocket* client_socket, |
| 166 | P2PStreamSocket* host_socket) |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 167 | : task_runner_(std::move(task_runner)), |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 168 | host_socket_(host_socket), |
| 169 | client_socket_(client_socket), |
| 170 | done_(false), |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 171 | write_errors_(0), |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 172 | read_errors_(0) {} |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 173 | |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 174 | void Start() { |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 175 | task_runner_->PostTask(FROM_HERE, |
| 176 | base::Bind(&TCPChannelTester::DoStart, this)); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void CheckResults() { |
| 180 | EXPECT_EQ(0, write_errors_); |
| 181 | EXPECT_EQ(0, read_errors_); |
| 182 | |
| 183 | ASSERT_EQ(kTestDataSize + kMessageSize, input_buffer_->capacity()); |
| 184 | |
| 185 | output_buffer_->SetOffset(0); |
| 186 | ASSERT_EQ(kTestDataSize, output_buffer_->size()); |
| 187 | |
| 188 | EXPECT_EQ(0, memcmp(output_buffer_->data(), |
| 189 | input_buffer_->StartOfBuffer(), kTestDataSize)); |
| 190 | } |
| 191 | |
| 192 | protected: |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 193 | virtual ~TCPChannelTester() = default; |
[email protected] | 60e60dd | 2012-04-28 08:16:04 | [diff] [blame] | 194 | |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 195 | void Done() { |
| 196 | done_ = true; |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 197 | task_runner_->PostTask( |
| 198 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated()); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void DoStart() { |
| 202 | InitBuffers(); |
| 203 | DoRead(); |
| 204 | DoWrite(); |
| 205 | } |
| 206 | |
| 207 | void InitBuffers() { |
Victor Costan | cd43978 | 2018-08-30 07:27:57 | [diff] [blame] | 208 | output_buffer_ = base::MakeRefCounted<net::DrainableIOBuffer>( |
| 209 | base::MakeRefCounted<net::IOBuffer>(kTestDataSize), kTestDataSize); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 210 | memset(output_buffer_->data(), 123, kTestDataSize); |
| 211 | |
Victor Costan | 8a7a19a | 2018-09-10 21:57:16 | [diff] [blame] | 212 | input_buffer_ = base::MakeRefCounted<net::GrowableIOBuffer>(); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 213 | // Always keep kMessageSize bytes available at the end of the input buffer. |
| 214 | input_buffer_->SetCapacity(kMessageSize); |
| 215 | } |
| 216 | |
| 217 | void DoWrite() { |
| 218 | int result = 1; |
| 219 | while (result > 0) { |
| 220 | if (output_buffer_->BytesRemaining() == 0) |
| 221 | break; |
| 222 | |
| 223 | int bytes_to_write = std::min(output_buffer_->BytesRemaining(), |
| 224 | kMessageSize); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 225 | result = client_socket_->Write( |
[email protected] | f94cd970 | 2017-12-07 06:09:40 | [diff] [blame] | 226 | output_buffer_.get(), bytes_to_write, |
| 227 | base::Bind(&TCPChannelTester::OnWritten, base::Unretained(this)), |
| 228 | TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 229 | HandleWriteResult(result); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void OnWritten(int result) { |
| 234 | HandleWriteResult(result); |
| 235 | DoWrite(); |
| 236 | } |
| 237 | |
| 238 | void HandleWriteResult(int result) { |
| 239 | if (result <= 0 && result != net::ERR_IO_PENDING) { |
| 240 | LOG(ERROR) << "Received error " << result << " when trying to write"; |
| 241 | write_errors_++; |
| 242 | Done(); |
| 243 | } else if (result > 0) { |
| 244 | output_buffer_->DidConsume(result); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void DoRead() { |
| 249 | int result = 1; |
| 250 | while (result > 0) { |
| 251 | input_buffer_->set_offset(input_buffer_->capacity() - kMessageSize); |
| 252 | |
[email protected] | f9d8a77 | 2013-06-01 04:33:17 | [diff] [blame] | 253 | result = host_socket_->Read( |
| 254 | input_buffer_.get(), |
| 255 | kMessageSize, |
| 256 | base::Bind(&TCPChannelTester::OnRead, base::Unretained(this))); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 257 | HandleReadResult(result); |
| 258 | }; |
| 259 | } |
| 260 | |
| 261 | void OnRead(int result) { |
| 262 | HandleReadResult(result); |
| 263 | DoRead(); |
| 264 | } |
| 265 | |
| 266 | void HandleReadResult(int result) { |
| 267 | if (result <= 0 && result != net::ERR_IO_PENDING) { |
| 268 | if (!done_) { |
| 269 | LOG(ERROR) << "Received error " << result << " when trying to read"; |
| 270 | read_errors_++; |
| 271 | Done(); |
| 272 | } |
| 273 | } else if (result > 0) { |
| 274 | // Allocate memory for the next read. |
| 275 | input_buffer_->SetCapacity(input_buffer_->capacity() + result); |
| 276 | if (input_buffer_->capacity() == kTestDataSize + kMessageSize) |
| 277 | Done(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | private: |
[email protected] | 60e60dd | 2012-04-28 08:16:04 | [diff] [blame] | 282 | friend class base::RefCountedThreadSafe<TCPChannelTester>; |
| 283 | |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 284 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 285 | P2PStreamSocket* host_socket_; |
| 286 | P2PStreamSocket* client_socket_; |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 287 | bool done_; |
| 288 | |
| 289 | scoped_refptr<net::DrainableIOBuffer> output_buffer_; |
| 290 | scoped_refptr<net::GrowableIOBuffer> input_buffer_; |
| 291 | |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 292 | int write_errors_; |
| 293 | int read_errors_; |
| 294 | }; |
| 295 | |
[email protected] | 36cf510 | 2011-05-27 01:43:57 | [diff] [blame] | 296 | class PseudoTcpAdapterTest : public testing::Test { |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 297 | protected: |
dcheng | a4ebdc48 | 2014-12-22 23:11:30 | [diff] [blame] | 298 | void SetUp() override { |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 299 | jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
[email protected] | e86b28f | 2011-07-29 22:21:07 | [diff] [blame] | 300 | |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 301 | host_socket_ = new FakeSocket(); |
| 302 | client_socket_ = new FakeSocket(); |
| 303 | |
| 304 | host_socket_->Connect(client_socket_); |
| 305 | client_socket_->Connect(host_socket_); |
| 306 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 307 | host_pseudotcp_.reset(new PseudoTcpAdapter(base::WrapUnique(host_socket_))); |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 308 | client_pseudotcp_.reset( |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 309 | new PseudoTcpAdapter(base::WrapUnique(client_socket_))); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | FakeSocket* host_socket_; |
| 313 | FakeSocket* client_socket_; |
| 314 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 315 | std::unique_ptr<PseudoTcpAdapter> host_pseudotcp_; |
| 316 | std::unique_ptr<PseudoTcpAdapter> client_pseudotcp_; |
[email protected] | b231184d | 2013-04-27 07:51:55 | [diff] [blame] | 317 | base::MessageLoop message_loop_; |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 318 | }; |
| 319 | |
[email protected] | 36cf510 | 2011-05-27 01:43:57 | [diff] [blame] | 320 | TEST_F(PseudoTcpAdapterTest, DataTransfer) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 321 | net::TestCompletionCallback host_connect_cb; |
| 322 | net::TestCompletionCallback client_connect_cb; |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 323 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 324 | int rv1 = host_pseudotcp_->Connect(host_connect_cb.callback()); |
| 325 | int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); |
[email protected] | 886ae9f | 2011-07-05 17:37:04 | [diff] [blame] | 326 | |
| 327 | if (rv1 == net::ERR_IO_PENDING) |
| 328 | rv1 = host_connect_cb.WaitForResult(); |
| 329 | if (rv2 == net::ERR_IO_PENDING) |
| 330 | rv2 = client_connect_cb.WaitForResult(); |
| 331 | ASSERT_EQ(net::OK, rv1); |
| 332 | ASSERT_EQ(net::OK, rv2); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 333 | |
| 334 | scoped_refptr<TCPChannelTester> tester = |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 335 | new TCPChannelTester(base::ThreadTaskRunnerHandle::Get(), |
| 336 | host_pseudotcp_.get(), client_pseudotcp_.get()); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 337 | |
| 338 | tester->Start(); |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 339 | base::RunLoop().Run(); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 340 | tester->CheckResults(); |
| 341 | } |
| 342 | |
[email protected] | bbb8ba0 | 2011-08-23 01:29:25 | [diff] [blame] | 343 | TEST_F(PseudoTcpAdapterTest, LimitedChannel) { |
| 344 | const int kLatencyMs = 20; |
| 345 | const int kPacketsPerSecond = 400; |
| 346 | const int kBurstPackets = 10; |
| 347 | |
| 348 | LeakyBucket host_limiter(kBurstPackets, kPacketsPerSecond); |
| 349 | host_socket_->set_latency(kLatencyMs); |
| 350 | host_socket_->set_rate_limiter(&host_limiter); |
| 351 | |
| 352 | LeakyBucket client_limiter(kBurstPackets, kPacketsPerSecond); |
| 353 | host_socket_->set_latency(kLatencyMs); |
| 354 | client_socket_->set_rate_limiter(&client_limiter); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 355 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 356 | net::TestCompletionCallback host_connect_cb; |
| 357 | net::TestCompletionCallback client_connect_cb; |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 358 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 359 | int rv1 = host_pseudotcp_->Connect(host_connect_cb.callback()); |
| 360 | int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); |
[email protected] | 886ae9f | 2011-07-05 17:37:04 | [diff] [blame] | 361 | |
| 362 | if (rv1 == net::ERR_IO_PENDING) |
| 363 | rv1 = host_connect_cb.WaitForResult(); |
| 364 | if (rv2 == net::ERR_IO_PENDING) |
| 365 | rv2 = client_connect_cb.WaitForResult(); |
| 366 | ASSERT_EQ(net::OK, rv1); |
| 367 | ASSERT_EQ(net::OK, rv2); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 368 | |
| 369 | scoped_refptr<TCPChannelTester> tester = |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 370 | new TCPChannelTester(base::ThreadTaskRunnerHandle::Get(), |
| 371 | host_pseudotcp_.get(), client_pseudotcp_.get()); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 372 | |
| 373 | tester->Start(); |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 374 | base::RunLoop().Run(); |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 375 | tester->CheckResults(); |
| 376 | } |
| 377 | |
[email protected] | 6b302a86 | 2011-05-25 04:23:29 | [diff] [blame] | 378 | class DeleteOnConnected { |
| 379 | public: |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 380 | DeleteOnConnected(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 381 | std::unique_ptr<PseudoTcpAdapter>* adapter) |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 382 | : task_runner_(std::move(task_runner)), adapter_(adapter) {} |
[email protected] | 6b302a86 | 2011-05-25 04:23:29 | [diff] [blame] | 383 | void OnConnected(int error) { |
| 384 | adapter_->reset(); |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 385 | task_runner_->PostTask( |
| 386 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated()); |
[email protected] | 6b302a86 | 2011-05-25 04:23:29 | [diff] [blame] | 387 | } |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 388 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 389 | std::unique_ptr<PseudoTcpAdapter>* adapter_; |
[email protected] | 6b302a86 | 2011-05-25 04:23:29 | [diff] [blame] | 390 | }; |
| 391 | |
[email protected] | 36cf510 | 2011-05-27 01:43:57 | [diff] [blame] | 392 | TEST_F(PseudoTcpAdapterTest, DeleteOnConnected) { |
[email protected] | 6b302a86 | 2011-05-25 04:23:29 | [diff] [blame] | 393 | // This test verifies that deleting the adapter mid-callback doesn't lead |
| 394 | // to deleted structures being touched as the stack unrolls, so the failure |
| 395 | // mode is a crash rather than a normal test failure. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 396 | net::TestCompletionCallback client_connect_cb; |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 397 | DeleteOnConnected host_delete(base::ThreadTaskRunnerHandle::Get(), |
| 398 | &host_pseudotcp_); |
[email protected] | 6b302a86 | 2011-05-25 04:23:29 | [diff] [blame] | 399 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 400 | host_pseudotcp_->Connect(base::Bind(&DeleteOnConnected::OnConnected, |
| 401 | base::Unretained(&host_delete))); |
| 402 | client_pseudotcp_->Connect(client_connect_cb.callback()); |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 403 | base::RunLoop().Run(); |
[email protected] | 6b302a86 | 2011-05-25 04:23:29 | [diff] [blame] | 404 | |
| 405 | ASSERT_EQ(NULL, host_pseudotcp_.get()); |
| 406 | } |
| 407 | |
[email protected] | 46934c0 | 2012-04-06 02:56:39 | [diff] [blame] | 408 | // Verify that we can send/receive data with the write-waits-for-send |
| 409 | // flag set. |
| 410 | TEST_F(PseudoTcpAdapterTest, WriteWaitsForSendLetsDataThrough) { |
| 411 | net::TestCompletionCallback host_connect_cb; |
| 412 | net::TestCompletionCallback client_connect_cb; |
| 413 | |
| 414 | host_pseudotcp_->SetWriteWaitsForSend(true); |
| 415 | client_pseudotcp_->SetWriteWaitsForSend(true); |
| 416 | |
| 417 | // Disable Nagle's algorithm because the test is slow when it is |
| 418 | // enabled. |
| 419 | host_pseudotcp_->SetNoDelay(true); |
| 420 | |
| 421 | int rv1 = host_pseudotcp_->Connect(host_connect_cb.callback()); |
| 422 | int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); |
| 423 | |
| 424 | if (rv1 == net::ERR_IO_PENDING) |
| 425 | rv1 = host_connect_cb.WaitForResult(); |
| 426 | if (rv2 == net::ERR_IO_PENDING) |
| 427 | rv2 = client_connect_cb.WaitForResult(); |
| 428 | ASSERT_EQ(net::OK, rv1); |
| 429 | ASSERT_EQ(net::OK, rv2); |
| 430 | |
| 431 | scoped_refptr<TCPChannelTester> tester = |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 432 | new TCPChannelTester(base::ThreadTaskRunnerHandle::Get(), |
| 433 | host_pseudotcp_.get(), client_pseudotcp_.get()); |
[email protected] | 46934c0 | 2012-04-06 02:56:39 | [diff] [blame] | 434 | |
| 435 | tester->Start(); |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 436 | base::RunLoop().Run(); |
[email protected] | 46934c0 | 2012-04-06 02:56:39 | [diff] [blame] | 437 | tester->CheckResults(); |
| 438 | } |
| 439 | |
[email protected] | db6609b8 | 2011-04-22 01:30:51 | [diff] [blame] | 440 | } // namespace |
| 441 | |
sergeyu | 031ce3b3 | 2015-07-06 19:00:46 | [diff] [blame] | 442 | } // namespace protocol |
| 443 | } // namespace remoting |