henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "p2p/base/pseudo_tcp.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 15 | #include <algorithm> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <cstddef> |
Steve Anton | 5c8231c | 2017-12-06 10:39:22 -0800 | [diff] [blame] | 17 | #include <string> |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 18 | #include <utility> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 21 | #include "api/task_queue/pending_task_safety_flag.h" |
| 22 | #include "api/task_queue/task_queue_base.h" |
| 23 | #include "api/units/time_delta.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/gunit.h" |
| 25 | #include "rtc_base/helpers.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 26 | #include "rtc_base/logging.h" |
Niels Möller | e7547d5 | 2018-11-01 09:33:08 +0100 | [diff] [blame] | 27 | #include "rtc_base/memory_stream.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/time_utils.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 29 | #include "test/gtest.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 30 | |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 31 | using ::cricket::PseudoTcp; |
| 32 | using ::webrtc::ScopedTaskSafety; |
| 33 | using ::webrtc::TaskQueueBase; |
| 34 | using ::webrtc::TimeDelta; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 35 | |
| 36 | static const int kConnectTimeoutMs = 10000; // ~3 * default RTO of 3000ms |
| 37 | static const int kTransferTimeoutMs = 15000; |
| 38 | static const int kBlockSize = 4096; |
| 39 | |
| 40 | class PseudoTcpForTest : public cricket::PseudoTcp { |
| 41 | public: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 42 | PseudoTcpForTest(cricket::IPseudoTcpNotify* notify, uint32_t conv) |
| 43 | : PseudoTcp(notify, conv) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 44 | |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 45 | bool isReceiveBufferFull() const { return PseudoTcp::isReceiveBufferFull(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 46 | |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 47 | void disableWindowScale() { PseudoTcp::disableWindowScale(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 50 | class PseudoTcpTestBase : public ::testing::Test, |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 51 | public cricket::IPseudoTcpNotify { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 52 | public: |
| 53 | PseudoTcpTestBase() |
| 54 | : local_(this, 1), |
| 55 | remote_(this, 1), |
| 56 | have_connected_(false), |
| 57 | have_disconnected_(false), |
| 58 | local_mtu_(65535), |
| 59 | remote_mtu_(65535), |
| 60 | delay_(0), |
| 61 | loss_(0) { |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 62 | // Set use of the test RNG to get predictable loss patterns. Otherwise, |
| 63 | // this test would occasionally get really unlucky loss and time out. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 64 | rtc::SetRandomTestMode(true); |
| 65 | } |
| 66 | ~PseudoTcpTestBase() { |
| 67 | // Put it back for the next test. |
| 68 | rtc::SetRandomTestMode(false); |
| 69 | } |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 70 | // If true, both endpoints will send the "connect" segment simultaneously, |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 71 | // rather than `local_` sending it followed by a response from `remote_`. |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 72 | // Note that this is what chromoting ends up doing. |
| 73 | void SetSimultaneousOpen(bool enabled) { simultaneous_open_ = enabled; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 74 | void SetLocalMtu(int mtu) { |
| 75 | local_.NotifyMTU(mtu); |
| 76 | local_mtu_ = mtu; |
| 77 | } |
| 78 | void SetRemoteMtu(int mtu) { |
| 79 | remote_.NotifyMTU(mtu); |
| 80 | remote_mtu_ = mtu; |
| 81 | } |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 82 | void SetDelay(int delay) { delay_ = delay; } |
| 83 | void SetLoss(int percent) { loss_ = percent; } |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 84 | // Used to cause the initial "connect" segment to be lost, needed for a |
| 85 | // regression test. |
| 86 | void DropNextPacket() { drop_next_packet_ = true; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 87 | void SetOptNagling(bool enable_nagles) { |
| 88 | local_.SetOption(PseudoTcp::OPT_NODELAY, !enable_nagles); |
| 89 | remote_.SetOption(PseudoTcp::OPT_NODELAY, !enable_nagles); |
| 90 | } |
| 91 | void SetOptAckDelay(int ack_delay) { |
| 92 | local_.SetOption(PseudoTcp::OPT_ACKDELAY, ack_delay); |
| 93 | remote_.SetOption(PseudoTcp::OPT_ACKDELAY, ack_delay); |
| 94 | } |
| 95 | void SetOptSndBuf(int size) { |
| 96 | local_.SetOption(PseudoTcp::OPT_SNDBUF, size); |
| 97 | remote_.SetOption(PseudoTcp::OPT_SNDBUF, size); |
| 98 | } |
| 99 | void SetRemoteOptRcvBuf(int size) { |
| 100 | remote_.SetOption(PseudoTcp::OPT_RCVBUF, size); |
| 101 | } |
| 102 | void SetLocalOptRcvBuf(int size) { |
| 103 | local_.SetOption(PseudoTcp::OPT_RCVBUF, size); |
| 104 | } |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 105 | void DisableRemoteWindowScale() { remote_.disableWindowScale(); } |
| 106 | void DisableLocalWindowScale() { local_.disableWindowScale(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 107 | |
| 108 | protected: |
| 109 | int Connect() { |
| 110 | int ret = local_.Connect(); |
| 111 | if (ret == 0) { |
| 112 | UpdateLocalClock(); |
| 113 | } |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 114 | if (simultaneous_open_) { |
| 115 | ret = remote_.Connect(); |
| 116 | if (ret == 0) { |
| 117 | UpdateRemoteClock(); |
| 118 | } |
| 119 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 120 | return ret; |
| 121 | } |
| 122 | void Close() { |
| 123 | local_.Close(false); |
| 124 | UpdateLocalClock(); |
| 125 | } |
| 126 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 127 | virtual void OnTcpOpen(PseudoTcp* tcp) { |
| 128 | // Consider ourselves connected when the local side gets OnTcpOpen. |
| 129 | // OnTcpWriteable isn't fired at open, so we trigger it now. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 130 | RTC_LOG(LS_VERBOSE) << "Opened"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 131 | if (tcp == &local_) { |
| 132 | have_connected_ = true; |
| 133 | OnTcpWriteable(tcp); |
| 134 | } |
| 135 | } |
| 136 | // Test derived from the base should override |
| 137 | // virtual void OnTcpReadable(PseudoTcp* tcp) |
| 138 | // and |
| 139 | // virtual void OnTcpWritable(PseudoTcp* tcp) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 140 | virtual void OnTcpClosed(PseudoTcp* tcp, uint32_t error) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 141 | // Consider ourselves closed when the remote side gets OnTcpClosed. |
Steve Anton | 5c8231c | 2017-12-06 10:39:22 -0800 | [diff] [blame] | 142 | // TODO(?): OnTcpClosed is only ever notified in case of error in |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 143 | // the current implementation. Solicited close is not (yet) supported. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 144 | RTC_LOG(LS_VERBOSE) << "Closed"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 145 | EXPECT_EQ(0U, error); |
| 146 | if (tcp == &remote_) { |
| 147 | have_disconnected_ = true; |
| 148 | } |
| 149 | } |
| 150 | virtual WriteResult TcpWritePacket(PseudoTcp* tcp, |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 151 | const char* buffer, |
| 152 | size_t len) { |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 153 | // Drop a packet if the test called DropNextPacket. |
| 154 | if (drop_next_packet_) { |
| 155 | drop_next_packet_ = false; |
| 156 | RTC_LOG(LS_VERBOSE) << "Dropping packet due to DropNextPacket, size=" |
| 157 | << len; |
| 158 | return WR_SUCCESS; |
| 159 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 160 | // Randomly drop the desired percentage of packets. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 161 | if (rtc::CreateRandomId() % 100 < static_cast<uint32_t>(loss_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 162 | RTC_LOG(LS_VERBOSE) << "Randomly dropping packet, size=" << len; |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 163 | return WR_SUCCESS; |
| 164 | } |
| 165 | // Also drop packets that are larger than the configured MTU. |
| 166 | if (len > static_cast<size_t>(std::min(local_mtu_, remote_mtu_))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 167 | RTC_LOG(LS_VERBOSE) << "Dropping packet that exceeds path MTU, size=" |
| 168 | << len; |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 169 | return WR_SUCCESS; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 170 | } |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 171 | PseudoTcp* other; |
| 172 | ScopedTaskSafety* timer; |
| 173 | if (tcp == &local_) { |
| 174 | other = &remote_; |
| 175 | timer = &remote_timer_; |
| 176 | } else { |
| 177 | other = &local_; |
| 178 | timer = &local_timer_; |
| 179 | } |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 180 | std::string packet(buffer, len); |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 181 | ++packets_in_flight_; |
| 182 | TaskQueueBase::Current()->PostDelayedTask( |
| 183 | [other, timer, packet = std::move(packet), this] { |
| 184 | --packets_in_flight_; |
| 185 | other->NotifyPacket(packet.c_str(), packet.size()); |
| 186 | UpdateClock(*other, *timer); |
| 187 | }, |
| 188 | TimeDelta::Millis(delay_)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 189 | return WR_SUCCESS; |
| 190 | } |
| 191 | |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 192 | void UpdateLocalClock() { UpdateClock(local_, local_timer_); } |
| 193 | void UpdateRemoteClock() { UpdateClock(remote_, remote_timer_); } |
| 194 | static void UpdateClock(PseudoTcp& tcp, ScopedTaskSafety& timer) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 195 | long interval = 0; // NOLINT |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 196 | tcp.GetNextClock(PseudoTcp::Now(), interval); |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 197 | interval = std::max<int>(interval, 0L); // sometimes interval is < 0 |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 198 | timer.reset(); |
| 199 | TaskQueueBase::Current()->PostDelayedTask( |
| 200 | SafeTask(timer.flag(), |
| 201 | [&tcp, &timer] { |
| 202 | tcp.NotifyClock(PseudoTcp::Now()); |
| 203 | UpdateClock(tcp, timer); |
| 204 | }), |
| 205 | TimeDelta::Millis(interval)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Niels Möller | 83830f3 | 2022-05-20 09:12:57 +0200 | [diff] [blame] | 208 | rtc::AutoThread main_thread_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 209 | PseudoTcpForTest local_; |
| 210 | PseudoTcpForTest remote_; |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 211 | ScopedTaskSafety local_timer_; |
| 212 | ScopedTaskSafety remote_timer_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 213 | rtc::MemoryStream send_stream_; |
| 214 | rtc::MemoryStream recv_stream_; |
| 215 | bool have_connected_; |
| 216 | bool have_disconnected_; |
| 217 | int local_mtu_; |
| 218 | int remote_mtu_; |
| 219 | int delay_; |
| 220 | int loss_; |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 221 | bool drop_next_packet_ = false; |
| 222 | bool simultaneous_open_ = false; |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 223 | int packets_in_flight_ = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | class PseudoTcpTest : public PseudoTcpTestBase { |
| 227 | public: |
| 228 | void TestTransfer(int size) { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 229 | uint32_t start; |
| 230 | int32_t elapsed; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 231 | size_t received; |
| 232 | // Create some dummy data to send. |
| 233 | send_stream_.ReserveSize(size); |
| 234 | for (int i = 0; i < size; ++i) { |
Harald Alvestrand | dd4c406 | 2022-11-16 07:29:57 +0000 | [diff] [blame] | 235 | uint8_t ch = static_cast<uint8_t>(i); |
| 236 | size_t written; |
| 237 | int error; |
| 238 | send_stream_.Write(rtc::MakeArrayView(&ch, 1), written, error); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 239 | } |
| 240 | send_stream_.Rewind(); |
| 241 | // Prepare the receive stream. |
| 242 | recv_stream_.ReserveSize(size); |
| 243 | // Connect and wait until connected. |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 244 | start = rtc::Time32(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 245 | EXPECT_EQ(0, Connect()); |
| 246 | EXPECT_TRUE_WAIT(have_connected_, kConnectTimeoutMs); |
| 247 | // Sending will start from OnTcpWriteable and complete when all data has |
| 248 | // been received. |
| 249 | EXPECT_TRUE_WAIT(have_disconnected_, kTransferTimeoutMs); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 250 | elapsed = rtc::Time32() - start; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 251 | recv_stream_.GetSize(&received); |
| 252 | // Ensure we closed down OK and we got the right data. |
Steve Anton | 5c8231c | 2017-12-06 10:39:22 -0800 | [diff] [blame] | 253 | // TODO(?): Ensure the errors are cleared properly. |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 254 | // EXPECT_EQ(0, local_.GetError()); |
| 255 | // EXPECT_EQ(0, remote_.GetError()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 256 | EXPECT_EQ(static_cast<size_t>(size), received); |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 257 | EXPECT_EQ(0, |
| 258 | memcmp(send_stream_.GetBuffer(), recv_stream_.GetBuffer(), size)); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 259 | RTC_LOG(LS_INFO) << "Transferred " << received << " bytes in " << elapsed |
| 260 | << " ms (" << size * 8 / elapsed << " Kbps)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | private: |
| 264 | // IPseudoTcpNotify interface |
| 265 | |
| 266 | virtual void OnTcpReadable(PseudoTcp* tcp) { |
| 267 | // Stream bytes to the recv stream as they arrive. |
| 268 | if (tcp == &remote_) { |
| 269 | ReadData(); |
| 270 | |
Steve Anton | 5c8231c | 2017-12-06 10:39:22 -0800 | [diff] [blame] | 271 | // TODO(?): OnTcpClosed() is currently only notified on error - |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 272 | // there is no on-the-wire equivalent of TCP FIN. |
| 273 | // So we fake the notification when all the data has been read. |
| 274 | size_t received, required; |
| 275 | recv_stream_.GetPosition(&received); |
| 276 | send_stream_.GetSize(&required); |
| 277 | if (received == required) |
| 278 | OnTcpClosed(&remote_, 0); |
| 279 | } |
| 280 | } |
| 281 | virtual void OnTcpWriteable(PseudoTcp* tcp) { |
| 282 | // Write bytes from the send stream when we can. |
| 283 | // Shut down when we've sent everything. |
| 284 | if (tcp == &local_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 285 | RTC_LOG(LS_VERBOSE) << "Flow Control Lifted"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 286 | bool done; |
| 287 | WriteData(&done); |
| 288 | if (done) { |
| 289 | Close(); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void ReadData() { |
| 295 | char block[kBlockSize]; |
| 296 | size_t position; |
| 297 | int rcvd; |
| 298 | do { |
| 299 | rcvd = remote_.Recv(block, sizeof(block)); |
| 300 | if (rcvd != -1) { |
Harald Alvestrand | dd4c406 | 2022-11-16 07:29:57 +0000 | [diff] [blame] | 301 | size_t written; |
| 302 | int error; |
| 303 | recv_stream_.Write( |
| 304 | rtc::MakeArrayView(reinterpret_cast<uint8_t*>(block), rcvd), |
| 305 | written, error); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 306 | recv_stream_.GetPosition(&position); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 307 | RTC_LOG(LS_VERBOSE) << "Received: " << position; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 308 | } |
| 309 | } while (rcvd > 0); |
| 310 | } |
| 311 | void WriteData(bool* done) { |
| 312 | size_t position, tosend; |
| 313 | int sent; |
| 314 | char block[kBlockSize]; |
| 315 | do { |
| 316 | send_stream_.GetPosition(&position); |
Harald Alvestrand | dd4c406 | 2022-11-16 07:29:57 +0000 | [diff] [blame] | 317 | int error; |
| 318 | if (send_stream_.Read( |
| 319 | rtc::MakeArrayView(reinterpret_cast<uint8_t*>(block), kBlockSize), |
| 320 | tosend, error) != rtc::SR_EOS) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 321 | sent = local_.Send(block, tosend); |
| 322 | UpdateLocalClock(); |
| 323 | if (sent != -1) { |
| 324 | send_stream_.SetPosition(position + sent); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 325 | RTC_LOG(LS_VERBOSE) << "Sent: " << position + sent; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 326 | } else { |
| 327 | send_stream_.SetPosition(position); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 328 | RTC_LOG(LS_VERBOSE) << "Flow Controlled"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 329 | } |
| 330 | } else { |
| 331 | sent = static_cast<int>(tosend = 0); |
| 332 | } |
| 333 | } while (sent > 0); |
| 334 | *done = (tosend == 0); |
| 335 | } |
| 336 | |
| 337 | private: |
| 338 | rtc::MemoryStream send_stream_; |
| 339 | rtc::MemoryStream recv_stream_; |
| 340 | }; |
| 341 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 342 | class PseudoTcpTestPingPong : public PseudoTcpTestBase { |
| 343 | public: |
| 344 | PseudoTcpTestPingPong() |
| 345 | : iterations_remaining_(0), |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 346 | sender_(NULL), |
| 347 | receiver_(NULL), |
| 348 | bytes_per_send_(0) {} |
| 349 | void SetBytesPerSend(int bytes) { bytes_per_send_ = bytes; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 350 | void TestPingPong(int size, int iterations) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 351 | uint32_t start, elapsed; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 352 | iterations_remaining_ = iterations; |
| 353 | receiver_ = &remote_; |
| 354 | sender_ = &local_; |
| 355 | // Create some dummy data to send. |
| 356 | send_stream_.ReserveSize(size); |
| 357 | for (int i = 0; i < size; ++i) { |
Harald Alvestrand | dd4c406 | 2022-11-16 07:29:57 +0000 | [diff] [blame] | 358 | uint8_t ch = static_cast<uint8_t>(i); |
| 359 | size_t written; |
| 360 | int error; |
| 361 | send_stream_.Write(rtc::MakeArrayView(&ch, 1), written, error); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 362 | } |
| 363 | send_stream_.Rewind(); |
| 364 | // Prepare the receive stream. |
| 365 | recv_stream_.ReserveSize(size); |
| 366 | // Connect and wait until connected. |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 367 | start = rtc::Time32(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 368 | EXPECT_EQ(0, Connect()); |
| 369 | EXPECT_TRUE_WAIT(have_connected_, kConnectTimeoutMs); |
| 370 | // Sending will start from OnTcpWriteable and stop when the required |
| 371 | // number of iterations have completed. |
| 372 | EXPECT_TRUE_WAIT(have_disconnected_, kTransferTimeoutMs); |
| 373 | elapsed = rtc::TimeSince(start); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 374 | RTC_LOG(LS_INFO) << "Performed " << iterations << " pings in " << elapsed |
| 375 | << " ms"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | private: |
| 379 | // IPseudoTcpNotify interface |
| 380 | |
| 381 | virtual void OnTcpReadable(PseudoTcp* tcp) { |
| 382 | if (tcp != receiver_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 383 | RTC_LOG_F(LS_ERROR) << "unexpected OnTcpReadable"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 384 | return; |
| 385 | } |
| 386 | // Stream bytes to the recv stream as they arrive. |
| 387 | ReadData(); |
| 388 | // If we've received the desired amount of data, rewind things |
| 389 | // and send it back the other way! |
| 390 | size_t position, desired; |
| 391 | recv_stream_.GetPosition(&position); |
| 392 | send_stream_.GetSize(&desired); |
| 393 | if (position == desired) { |
| 394 | if (receiver_ == &local_ && --iterations_remaining_ == 0) { |
| 395 | Close(); |
Steve Anton | 5c8231c | 2017-12-06 10:39:22 -0800 | [diff] [blame] | 396 | // TODO(?): Fake OnTcpClosed() on the receiver for now. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 397 | OnTcpClosed(&remote_, 0); |
| 398 | return; |
| 399 | } |
| 400 | PseudoTcp* tmp = receiver_; |
| 401 | receiver_ = sender_; |
| 402 | sender_ = tmp; |
| 403 | recv_stream_.Rewind(); |
| 404 | send_stream_.Rewind(); |
| 405 | OnTcpWriteable(sender_); |
| 406 | } |
| 407 | } |
| 408 | virtual void OnTcpWriteable(PseudoTcp* tcp) { |
| 409 | if (tcp != sender_) |
| 410 | return; |
| 411 | // Write bytes from the send stream when we can. |
| 412 | // Shut down when we've sent everything. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 413 | RTC_LOG(LS_VERBOSE) << "Flow Control Lifted"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 414 | WriteData(); |
| 415 | } |
| 416 | |
| 417 | void ReadData() { |
| 418 | char block[kBlockSize]; |
| 419 | size_t position; |
| 420 | int rcvd; |
| 421 | do { |
| 422 | rcvd = receiver_->Recv(block, sizeof(block)); |
| 423 | if (rcvd != -1) { |
Harald Alvestrand | dd4c406 | 2022-11-16 07:29:57 +0000 | [diff] [blame] | 424 | size_t written; |
| 425 | int error; |
| 426 | recv_stream_.Write( |
| 427 | rtc::MakeArrayView(reinterpret_cast<const uint8_t*>(block), rcvd), |
| 428 | written, error); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 429 | recv_stream_.GetPosition(&position); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 430 | RTC_LOG(LS_VERBOSE) << "Received: " << position; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 431 | } |
| 432 | } while (rcvd > 0); |
| 433 | } |
| 434 | void WriteData() { |
| 435 | size_t position, tosend; |
| 436 | int sent; |
| 437 | char block[kBlockSize]; |
| 438 | do { |
| 439 | send_stream_.GetPosition(&position); |
| 440 | tosend = bytes_per_send_ ? bytes_per_send_ : sizeof(block); |
Harald Alvestrand | dd4c406 | 2022-11-16 07:29:57 +0000 | [diff] [blame] | 441 | int error; |
| 442 | if (send_stream_.Read( |
| 443 | rtc::MakeArrayView(reinterpret_cast<uint8_t*>(block), tosend), |
| 444 | tosend, error) != rtc::SR_EOS) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 445 | sent = sender_->Send(block, tosend); |
| 446 | UpdateLocalClock(); |
| 447 | if (sent != -1) { |
| 448 | send_stream_.SetPosition(position + sent); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 449 | RTC_LOG(LS_VERBOSE) << "Sent: " << position + sent; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 450 | } else { |
| 451 | send_stream_.SetPosition(position); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 452 | RTC_LOG(LS_VERBOSE) << "Flow Controlled"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 453 | } |
| 454 | } else { |
| 455 | sent = static_cast<int>(tosend = 0); |
| 456 | } |
| 457 | } while (sent > 0); |
| 458 | } |
| 459 | |
| 460 | private: |
| 461 | int iterations_remaining_; |
| 462 | PseudoTcp* sender_; |
| 463 | PseudoTcp* receiver_; |
| 464 | int bytes_per_send_; |
| 465 | }; |
| 466 | |
| 467 | // Fill the receiver window until it is full, drain it and then |
| 468 | // fill it with the same amount. This is to test that receiver window |
| 469 | // contracts and enlarges correctly. |
| 470 | class PseudoTcpTestReceiveWindow : public PseudoTcpTestBase { |
| 471 | public: |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 472 | // Not all the data are transfered, `size` just need to be big enough |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 473 | // to fill up the receiver window twice. |
| 474 | void TestTransfer(int size) { |
| 475 | // Create some dummy data to send. |
| 476 | send_stream_.ReserveSize(size); |
| 477 | for (int i = 0; i < size; ++i) { |
Harald Alvestrand | dd4c406 | 2022-11-16 07:29:57 +0000 | [diff] [blame] | 478 | uint8_t ch = static_cast<uint8_t>(i); |
| 479 | size_t written; |
| 480 | int error; |
| 481 | send_stream_.Write(rtc::MakeArrayView(&ch, 1), written, error); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 482 | } |
| 483 | send_stream_.Rewind(); |
| 484 | |
| 485 | // Prepare the receive stream. |
| 486 | recv_stream_.ReserveSize(size); |
| 487 | |
| 488 | // Connect and wait until connected. |
| 489 | EXPECT_EQ(0, Connect()); |
| 490 | EXPECT_TRUE_WAIT(have_connected_, kConnectTimeoutMs); |
| 491 | |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 492 | TaskQueueBase::Current()->PostTask([this] { WriteData(); }); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 493 | EXPECT_TRUE_WAIT(have_disconnected_, kTransferTimeoutMs); |
| 494 | |
| 495 | ASSERT_EQ(2u, send_position_.size()); |
| 496 | ASSERT_EQ(2u, recv_position_.size()); |
| 497 | |
| 498 | const size_t estimated_recv_window = EstimateReceiveWindowSize(); |
| 499 | |
| 500 | // The difference in consecutive send positions should equal the |
| 501 | // receive window size or match very closely. This verifies that receive |
| 502 | // window is open after receiver drained all the data. |
| 503 | const size_t send_position_diff = send_position_[1] - send_position_[0]; |
| 504 | EXPECT_GE(1024u, estimated_recv_window - send_position_diff); |
| 505 | |
| 506 | // Receiver drained the receive window twice. |
| 507 | EXPECT_EQ(2 * estimated_recv_window, recv_position_[1]); |
| 508 | } |
| 509 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 510 | uint32_t EstimateReceiveWindowSize() const { |
| 511 | return static_cast<uint32_t>(recv_position_[0]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 514 | uint32_t EstimateSendWindowSize() const { |
| 515 | return static_cast<uint32_t>(send_position_[0] - recv_position_[0]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | private: |
| 519 | // IPseudoTcpNotify interface |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 520 | virtual void OnTcpReadable(PseudoTcp* tcp) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 521 | |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 522 | virtual void OnTcpWriteable(PseudoTcp* tcp) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 523 | |
| 524 | void ReadUntilIOPending() { |
| 525 | char block[kBlockSize]; |
| 526 | size_t position; |
| 527 | int rcvd; |
| 528 | |
| 529 | do { |
| 530 | rcvd = remote_.Recv(block, sizeof(block)); |
| 531 | if (rcvd != -1) { |
Harald Alvestrand | dd4c406 | 2022-11-16 07:29:57 +0000 | [diff] [blame] | 532 | size_t written; |
| 533 | int error; |
| 534 | recv_stream_.Write( |
| 535 | rtc::MakeArrayView(reinterpret_cast<uint8_t*>(block), rcvd), |
| 536 | written, error); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 537 | recv_stream_.GetPosition(&position); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 538 | RTC_LOG(LS_VERBOSE) << "Received: " << position; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 539 | } |
| 540 | } while (rcvd > 0); |
| 541 | |
| 542 | recv_stream_.GetPosition(&position); |
| 543 | recv_position_.push_back(position); |
| 544 | |
| 545 | // Disconnect if we have done two transfers. |
| 546 | if (recv_position_.size() == 2u) { |
| 547 | Close(); |
| 548 | OnTcpClosed(&remote_, 0); |
| 549 | } else { |
| 550 | WriteData(); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void WriteData() { |
| 555 | size_t position, tosend; |
| 556 | int sent; |
| 557 | char block[kBlockSize]; |
| 558 | do { |
| 559 | send_stream_.GetPosition(&position); |
Harald Alvestrand | dd4c406 | 2022-11-16 07:29:57 +0000 | [diff] [blame] | 560 | int error; |
| 561 | if (send_stream_.Read( |
| 562 | rtc::MakeArrayView(reinterpret_cast<uint8_t*>(block), |
| 563 | sizeof(block)), |
| 564 | tosend, error) != rtc::SR_EOS) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 565 | sent = local_.Send(block, tosend); |
| 566 | UpdateLocalClock(); |
| 567 | if (sent != -1) { |
| 568 | send_stream_.SetPosition(position + sent); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 569 | RTC_LOG(LS_VERBOSE) << "Sent: " << position + sent; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 570 | } else { |
| 571 | send_stream_.SetPosition(position); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 572 | RTC_LOG(LS_VERBOSE) << "Flow Controlled"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 573 | } |
| 574 | } else { |
| 575 | sent = static_cast<int>(tosend = 0); |
| 576 | } |
| 577 | } while (sent > 0); |
| 578 | // At this point, we've filled up the available space in the send queue. |
| 579 | |
Danil Chapovalov | 4d71538 | 2022-08-19 10:28:40 +0200 | [diff] [blame] | 580 | if (packets_in_flight_ > 0) { |
| 581 | // If there are packet tasks, attempt to continue sending after giving |
| 582 | // those packets time to process, which should free up the send buffer. |
| 583 | rtc::Thread::Current()->PostDelayedTask([this] { WriteData(); }, |
| 584 | TimeDelta::Millis(10)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 585 | } else { |
| 586 | if (!remote_.isReceiveBufferFull()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 587 | RTC_LOG(LS_ERROR) << "This shouldn't happen - the send buffer is full, " |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 588 | "the receive buffer is not, and there are no " |
| 589 | "remaining messages to process."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 590 | } |
| 591 | send_stream_.GetPosition(&position); |
| 592 | send_position_.push_back(position); |
| 593 | |
| 594 | // Drain the receiver buffer. |
| 595 | ReadUntilIOPending(); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | private: |
| 600 | rtc::MemoryStream send_stream_; |
| 601 | rtc::MemoryStream recv_stream_; |
| 602 | |
| 603 | std::vector<size_t> send_position_; |
| 604 | std::vector<size_t> recv_position_; |
| 605 | }; |
| 606 | |
| 607 | // Basic end-to-end data transfer tests |
| 608 | |
| 609 | // Test the normal case of sending data from one side to the other. |
| 610 | TEST_F(PseudoTcpTest, TestSend) { |
| 611 | SetLocalMtu(1500); |
| 612 | SetRemoteMtu(1500); |
| 613 | TestTransfer(1000000); |
| 614 | } |
| 615 | |
| 616 | // Test sending data with a 50 ms RTT. Transmission should take longer due |
| 617 | // to a slower ramp-up in send rate. |
| 618 | TEST_F(PseudoTcpTest, TestSendWithDelay) { |
| 619 | SetLocalMtu(1500); |
| 620 | SetRemoteMtu(1500); |
| 621 | SetDelay(50); |
| 622 | TestTransfer(1000000); |
| 623 | } |
| 624 | |
| 625 | // Test sending data with packet loss. Transmission should take much longer due |
| 626 | // to send back-off when loss occurs. |
| 627 | TEST_F(PseudoTcpTest, TestSendWithLoss) { |
| 628 | SetLocalMtu(1500); |
| 629 | SetRemoteMtu(1500); |
| 630 | SetLoss(10); |
| 631 | TestTransfer(100000); // less data so test runs faster |
| 632 | } |
| 633 | |
| 634 | // Test sending data with a 50 ms RTT and 10% packet loss. Transmission should |
| 635 | // take much longer due to send back-off and slower detection of loss. |
| 636 | TEST_F(PseudoTcpTest, TestSendWithDelayAndLoss) { |
| 637 | SetLocalMtu(1500); |
| 638 | SetRemoteMtu(1500); |
| 639 | SetDelay(50); |
| 640 | SetLoss(10); |
| 641 | TestTransfer(100000); // less data so test runs faster |
| 642 | } |
| 643 | |
| 644 | // Test sending data with 10% packet loss and Nagling disabled. Transmission |
| 645 | // should take about the same time as with Nagling enabled. |
| 646 | TEST_F(PseudoTcpTest, TestSendWithLossAndOptNaglingOff) { |
| 647 | SetLocalMtu(1500); |
| 648 | SetRemoteMtu(1500); |
| 649 | SetLoss(10); |
| 650 | SetOptNagling(false); |
| 651 | TestTransfer(100000); // less data so test runs faster |
| 652 | } |
| 653 | |
Taylor Brandstetter | 20e8cfb | 2018-05-24 16:43:02 -0700 | [diff] [blame] | 654 | // Regression test for bugs.webrtc.org/9208. |
| 655 | // |
| 656 | // This bug resulted in corrupted data if a "connect" segment was received after |
| 657 | // a data segment. This is only possible if: |
| 658 | // |
| 659 | // * The initial "connect" segment is lost, and retransmitted later. |
| 660 | // * Both sides send "connect"s simultaneously, such that the local side thinks |
| 661 | // a connection is established even before its "connect" has been |
| 662 | // acknowledged. |
| 663 | // * Nagle algorithm disabled, allowing a data segment to be sent before the |
| 664 | // "connect" has been acknowledged. |
| 665 | TEST_F(PseudoTcpTest, |
| 666 | TestSendWhenFirstPacketLostWithOptNaglingOffAndSimultaneousOpen) { |
| 667 | SetLocalMtu(1500); |
| 668 | SetRemoteMtu(1500); |
| 669 | DropNextPacket(); |
| 670 | SetOptNagling(false); |
| 671 | SetSimultaneousOpen(true); |
| 672 | TestTransfer(10000); |
| 673 | } |
| 674 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 675 | // Test sending data with 10% packet loss and Delayed ACK disabled. |
| 676 | // Transmission should be slightly faster than with it enabled. |
| 677 | TEST_F(PseudoTcpTest, TestSendWithLossAndOptAckDelayOff) { |
| 678 | SetLocalMtu(1500); |
| 679 | SetRemoteMtu(1500); |
| 680 | SetLoss(10); |
| 681 | SetOptAckDelay(0); |
| 682 | TestTransfer(100000); |
| 683 | } |
| 684 | |
| 685 | // Test sending data with 50ms delay and Nagling disabled. |
| 686 | TEST_F(PseudoTcpTest, TestSendWithDelayAndOptNaglingOff) { |
| 687 | SetLocalMtu(1500); |
| 688 | SetRemoteMtu(1500); |
| 689 | SetDelay(50); |
| 690 | SetOptNagling(false); |
| 691 | TestTransfer(100000); // less data so test runs faster |
| 692 | } |
| 693 | |
| 694 | // Test sending data with 50ms delay and Delayed ACK disabled. |
| 695 | TEST_F(PseudoTcpTest, TestSendWithDelayAndOptAckDelayOff) { |
| 696 | SetLocalMtu(1500); |
| 697 | SetRemoteMtu(1500); |
| 698 | SetDelay(50); |
| 699 | SetOptAckDelay(0); |
| 700 | TestTransfer(100000); // less data so test runs faster |
| 701 | } |
| 702 | |
| 703 | // Test a large receive buffer with a sender that doesn't support scaling. |
| 704 | TEST_F(PseudoTcpTest, TestSendRemoteNoWindowScale) { |
| 705 | SetLocalMtu(1500); |
| 706 | SetRemoteMtu(1500); |
| 707 | SetLocalOptRcvBuf(100000); |
| 708 | DisableRemoteWindowScale(); |
| 709 | TestTransfer(1000000); |
| 710 | } |
| 711 | |
| 712 | // Test a large sender-side receive buffer with a receiver that doesn't support |
| 713 | // scaling. |
| 714 | TEST_F(PseudoTcpTest, TestSendLocalNoWindowScale) { |
| 715 | SetLocalMtu(1500); |
| 716 | SetRemoteMtu(1500); |
| 717 | SetRemoteOptRcvBuf(100000); |
| 718 | DisableLocalWindowScale(); |
| 719 | TestTransfer(1000000); |
| 720 | } |
| 721 | |
| 722 | // Test when both sides use window scaling. |
| 723 | TEST_F(PseudoTcpTest, TestSendBothUseWindowScale) { |
| 724 | SetLocalMtu(1500); |
| 725 | SetRemoteMtu(1500); |
| 726 | SetRemoteOptRcvBuf(100000); |
| 727 | SetLocalOptRcvBuf(100000); |
| 728 | TestTransfer(1000000); |
| 729 | } |
| 730 | |
| 731 | // Test using a large window scale value. |
| 732 | TEST_F(PseudoTcpTest, TestSendLargeInFlight) { |
| 733 | SetLocalMtu(1500); |
| 734 | SetRemoteMtu(1500); |
| 735 | SetRemoteOptRcvBuf(100000); |
| 736 | SetLocalOptRcvBuf(100000); |
| 737 | SetOptSndBuf(150000); |
| 738 | TestTransfer(1000000); |
| 739 | } |
| 740 | |
| 741 | TEST_F(PseudoTcpTest, TestSendBothUseLargeWindowScale) { |
| 742 | SetLocalMtu(1500); |
| 743 | SetRemoteMtu(1500); |
| 744 | SetRemoteOptRcvBuf(1000000); |
| 745 | SetLocalOptRcvBuf(1000000); |
| 746 | TestTransfer(10000000); |
| 747 | } |
| 748 | |
| 749 | // Test using a small receive buffer. |
| 750 | TEST_F(PseudoTcpTest, TestSendSmallReceiveBuffer) { |
| 751 | SetLocalMtu(1500); |
| 752 | SetRemoteMtu(1500); |
| 753 | SetRemoteOptRcvBuf(10000); |
| 754 | SetLocalOptRcvBuf(10000); |
| 755 | TestTransfer(1000000); |
| 756 | } |
| 757 | |
| 758 | // Test using a very small receive buffer. |
| 759 | TEST_F(PseudoTcpTest, TestSendVerySmallReceiveBuffer) { |
| 760 | SetLocalMtu(1500); |
| 761 | SetRemoteMtu(1500); |
| 762 | SetRemoteOptRcvBuf(100); |
| 763 | SetLocalOptRcvBuf(100); |
| 764 | TestTransfer(100000); |
| 765 | } |
| 766 | |
| 767 | // Ping-pong (request/response) tests |
| 768 | |
| 769 | // Test sending <= 1x MTU of data in each ping/pong. Should take <10ms. |
| 770 | TEST_F(PseudoTcpTestPingPong, TestPingPong1xMtu) { |
| 771 | SetLocalMtu(1500); |
| 772 | SetRemoteMtu(1500); |
| 773 | TestPingPong(100, 100); |
| 774 | } |
| 775 | |
| 776 | // Test sending 2x-3x MTU of data in each ping/pong. Should take <10ms. |
| 777 | TEST_F(PseudoTcpTestPingPong, TestPingPong3xMtu) { |
| 778 | SetLocalMtu(1500); |
| 779 | SetRemoteMtu(1500); |
| 780 | TestPingPong(400, 100); |
| 781 | } |
| 782 | |
| 783 | // Test sending 1x-2x MTU of data in each ping/pong. |
| 784 | // Should take ~1s, due to interaction between Nagling and Delayed ACK. |
| 785 | TEST_F(PseudoTcpTestPingPong, TestPingPong2xMtu) { |
| 786 | SetLocalMtu(1500); |
| 787 | SetRemoteMtu(1500); |
| 788 | TestPingPong(2000, 5); |
| 789 | } |
| 790 | |
| 791 | // Test sending 1x-2x MTU of data in each ping/pong with Delayed ACK off. |
| 792 | // Should take <10ms. |
| 793 | TEST_F(PseudoTcpTestPingPong, TestPingPong2xMtuWithAckDelayOff) { |
| 794 | SetLocalMtu(1500); |
| 795 | SetRemoteMtu(1500); |
| 796 | SetOptAckDelay(0); |
| 797 | TestPingPong(2000, 100); |
| 798 | } |
| 799 | |
| 800 | // Test sending 1x-2x MTU of data in each ping/pong with Nagling off. |
| 801 | // Should take <10ms. |
| 802 | TEST_F(PseudoTcpTestPingPong, TestPingPong2xMtuWithNaglingOff) { |
| 803 | SetLocalMtu(1500); |
| 804 | SetRemoteMtu(1500); |
| 805 | SetOptNagling(false); |
| 806 | TestPingPong(2000, 5); |
| 807 | } |
| 808 | |
| 809 | // Test sending a ping as pair of short (non-full) segments. |
| 810 | // Should take ~1s, due to Delayed ACK interaction with Nagling. |
| 811 | TEST_F(PseudoTcpTestPingPong, TestPingPongShortSegments) { |
| 812 | SetLocalMtu(1500); |
| 813 | SetRemoteMtu(1500); |
| 814 | SetOptAckDelay(5000); |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 815 | SetBytesPerSend(50); // i.e. two Send calls per payload |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 816 | TestPingPong(100, 5); |
| 817 | } |
| 818 | |
| 819 | // Test sending ping as a pair of short (non-full) segments, with Nagling off. |
| 820 | // Should take <10ms. |
| 821 | TEST_F(PseudoTcpTestPingPong, TestPingPongShortSegmentsWithNaglingOff) { |
| 822 | SetLocalMtu(1500); |
| 823 | SetRemoteMtu(1500); |
| 824 | SetOptNagling(false); |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 825 | SetBytesPerSend(50); // i.e. two Send calls per payload |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 826 | TestPingPong(100, 5); |
| 827 | } |
| 828 | |
| 829 | // Test sending <= 1x MTU of data ping/pong, in two segments, no Delayed ACK. |
| 830 | // Should take ~1s. |
| 831 | TEST_F(PseudoTcpTestPingPong, TestPingPongShortSegmentsWithAckDelayOff) { |
| 832 | SetLocalMtu(1500); |
| 833 | SetRemoteMtu(1500); |
Steve Anton | cc65bd0 | 2017-11-29 10:19:58 -0800 | [diff] [blame] | 834 | SetBytesPerSend(50); // i.e. two Send calls per payload |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 835 | SetOptAckDelay(0); |
| 836 | TestPingPong(100, 5); |
| 837 | } |
| 838 | |
| 839 | // Test that receive window expands and contract correctly. |
| 840 | TEST_F(PseudoTcpTestReceiveWindow, TestReceiveWindow) { |
| 841 | SetLocalMtu(1500); |
| 842 | SetRemoteMtu(1500); |
| 843 | SetOptNagling(false); |
| 844 | SetOptAckDelay(0); |
| 845 | TestTransfer(1024 * 1000); |
| 846 | } |
| 847 | |
| 848 | // Test setting send window size to a very small value. |
| 849 | TEST_F(PseudoTcpTestReceiveWindow, TestSetVerySmallSendWindowSize) { |
| 850 | SetLocalMtu(1500); |
| 851 | SetRemoteMtu(1500); |
| 852 | SetOptNagling(false); |
| 853 | SetOptAckDelay(0); |
| 854 | SetOptSndBuf(900); |
| 855 | TestTransfer(1024 * 1000); |
| 856 | EXPECT_EQ(900u, EstimateSendWindowSize()); |
| 857 | } |
| 858 | |
| 859 | // Test setting receive window size to a value other than default. |
| 860 | TEST_F(PseudoTcpTestReceiveWindow, TestSetReceiveWindowSize) { |
| 861 | SetLocalMtu(1500); |
| 862 | SetRemoteMtu(1500); |
| 863 | SetOptNagling(false); |
| 864 | SetOptAckDelay(0); |
| 865 | SetRemoteOptRcvBuf(100000); |
| 866 | SetLocalOptRcvBuf(100000); |
| 867 | TestTransfer(1024 * 1000); |
| 868 | EXPECT_EQ(100000u, EstimateReceiveWindowSize()); |
| 869 | } |
| 870 | |
| 871 | /* Test sending data with mismatched MTUs. We should detect this and reduce |
| 872 | // our packet size accordingly. |
Steve Anton | 5c8231c | 2017-12-06 10:39:22 -0800 | [diff] [blame] | 873 | // TODO(?): This doesn't actually work right now. The current code |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 874 | // doesn't detect if the MTU is set too high on either side. |
| 875 | TEST_F(PseudoTcpTest, TestSendWithMismatchedMtus) { |
| 876 | SetLocalMtu(1500); |
| 877 | SetRemoteMtu(1280); |
| 878 | TestTransfer(1000000); |
| 879 | } |
| 880 | */ |