QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/quic_connection.h" |
| 6 | |
| 7 | #include <errno.h> |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 8 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <ostream> |
zhongyi | 9e84364 | 2019-04-12 09:04:54 -0700 | [diff] [blame] | 11 | #include <string> |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 12 | #include <utility> |
zhongyi | 9e84364 | 2019-04-12 09:04:54 -0700 | [diff] [blame] | 13 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/core/congestion_control/loss_detection_interface.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/congestion_control/send_algorithm_interface.h" |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/core/crypto/null_decrypter.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h" |
| 19 | #include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h" |
| 20 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 21 | #include "net/third_party/quiche/src/quic/core/quic_constants.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 22 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 23 | #include "net/third_party/quiche/src/quic/core/quic_simple_buffer_allocator.h" |
| 24 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 25 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 26 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | #include "net/third_party/quiche/src/quic/platform/api/quic_error_code_wrappers.h" |
| 28 | #include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h" |
| 29 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 30 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 31 | #include "net/third_party/quiche/src/quic/platform/api/quic_reference_counted.h" |
renjietang | 5b24589 | 2020-05-18 11:57:26 -0700 | [diff] [blame] | 32 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 34 | #include "net/third_party/quiche/src/quic/test_tools/mock_clock.h" |
| 35 | #include "net/third_party/quiche/src/quic/test_tools/mock_random.h" |
| 36 | #include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h" |
| 37 | #include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h" |
| 38 | #include "net/third_party/quiche/src/quic/test_tools/quic_framer_peer.h" |
| 39 | #include "net/third_party/quiche/src/quic/test_tools/quic_packet_creator_peer.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | #include "net/third_party/quiche/src/quic/test_tools/quic_sent_packet_manager_peer.h" |
| 41 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 42 | #include "net/third_party/quiche/src/quic/test_tools/simple_data_producer.h" |
| 43 | #include "net/third_party/quiche/src/quic/test_tools/simple_quic_framer.h" |
| 44 | #include "net/third_party/quiche/src/quic/test_tools/simple_session_notifier.h" |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 45 | #include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 46 | #include "net/third_party/quiche/src/common/platform/api/quiche_str_cat.h" |
| 47 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 48 | |
| 49 | using testing::_; |
| 50 | using testing::AnyNumber; |
| 51 | using testing::AtLeast; |
| 52 | using testing::DoAll; |
| 53 | using testing::Exactly; |
| 54 | using testing::Ge; |
| 55 | using testing::IgnoreResult; |
| 56 | using testing::InSequence; |
| 57 | using testing::Invoke; |
| 58 | using testing::InvokeWithoutArgs; |
| 59 | using testing::Lt; |
| 60 | using testing::Ref; |
| 61 | using testing::Return; |
| 62 | using testing::SaveArg; |
| 63 | using testing::SetArgPointee; |
| 64 | using testing::StrictMock; |
| 65 | |
| 66 | namespace quic { |
| 67 | namespace test { |
| 68 | namespace { |
| 69 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 70 | const char data1[] = "foo data"; |
| 71 | const char data2[] = "bar data"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 72 | |
| 73 | const bool kHasStopWaiting = true; |
| 74 | |
| 75 | const int kDefaultRetransmissionTimeMs = 500; |
| 76 | |
QUICHE team | 548d51b | 2019-03-14 10:06:54 -0700 | [diff] [blame] | 77 | DiversificationNonce kTestDiversificationNonce = { |
| 78 | 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', |
| 79 | 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', |
| 80 | 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', |
| 81 | }; |
| 82 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 83 | const QuicSocketAddress kPeerAddress = |
| 84 | QuicSocketAddress(QuicIpAddress::Loopback6(), |
| 85 | /*port=*/12345); |
| 86 | const QuicSocketAddress kSelfAddress = |
| 87 | QuicSocketAddress(QuicIpAddress::Loopback6(), |
| 88 | /*port=*/443); |
| 89 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 90 | QuicStreamId GetNthClientInitiatedStreamId(int n, |
| 91 | QuicTransportVersion version) { |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 92 | return QuicUtils::GetFirstBidirectionalStreamId(version, |
| 93 | Perspective::IS_CLIENT) + |
| 94 | n * 2; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 95 | } |
| 96 | |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 97 | QuicLongHeaderType EncryptionlevelToLongHeaderType(EncryptionLevel level) { |
| 98 | switch (level) { |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 99 | case ENCRYPTION_INITIAL: |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 100 | return INITIAL; |
QUICHE team | 88ea008 | 2019-03-15 10:05:26 -0700 | [diff] [blame] | 101 | case ENCRYPTION_HANDSHAKE: |
| 102 | return HANDSHAKE; |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 103 | case ENCRYPTION_ZERO_RTT: |
| 104 | return ZERO_RTT_PROTECTED; |
| 105 | case ENCRYPTION_FORWARD_SECURE: |
| 106 | DCHECK(false); |
| 107 | return INVALID_PACKET_TYPE; |
| 108 | default: |
| 109 | DCHECK(false); |
| 110 | return INVALID_PACKET_TYPE; |
| 111 | } |
| 112 | } |
| 113 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 114 | // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message. |
| 115 | class TaggingEncrypter : public QuicEncrypter { |
| 116 | public: |
| 117 | explicit TaggingEncrypter(uint8_t tag) : tag_(tag) {} |
| 118 | TaggingEncrypter(const TaggingEncrypter&) = delete; |
| 119 | TaggingEncrypter& operator=(const TaggingEncrypter&) = delete; |
| 120 | |
| 121 | ~TaggingEncrypter() override {} |
| 122 | |
| 123 | // QuicEncrypter interface. |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 124 | bool SetKey(quiche::QuicheStringPiece /*key*/) override { return true; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 125 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 126 | bool SetNoncePrefix(quiche::QuicheStringPiece /*nonce_prefix*/) override { |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 127 | return true; |
| 128 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 129 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 130 | bool SetIV(quiche::QuicheStringPiece /*iv*/) override { return true; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 131 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 132 | bool SetHeaderProtectionKey(quiche::QuicheStringPiece /*key*/) override { |
| 133 | return true; |
| 134 | } |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 135 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 136 | bool EncryptPacket(uint64_t /*packet_number*/, |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 137 | quiche::QuicheStringPiece /*associated_data*/, |
| 138 | quiche::QuicheStringPiece plaintext, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 139 | char* output, |
| 140 | size_t* output_length, |
| 141 | size_t max_output_length) override { |
| 142 | const size_t len = plaintext.size() + kTagSize; |
| 143 | if (max_output_length < len) { |
| 144 | return false; |
| 145 | } |
| 146 | // Memmove is safe for inplace encryption. |
| 147 | memmove(output, plaintext.data(), plaintext.size()); |
| 148 | output += plaintext.size(); |
| 149 | memset(output, tag_, kTagSize); |
| 150 | *output_length = len; |
| 151 | return true; |
| 152 | } |
| 153 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 154 | std::string GenerateHeaderProtectionMask( |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 155 | quiche::QuicheStringPiece /*sample*/) override { |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 156 | return std::string(5, 0); |
| 157 | } |
| 158 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 159 | size_t GetKeySize() const override { return 0; } |
| 160 | size_t GetNoncePrefixSize() const override { return 0; } |
| 161 | size_t GetIVSize() const override { return 0; } |
| 162 | |
| 163 | size_t GetMaxPlaintextSize(size_t ciphertext_size) const override { |
| 164 | return ciphertext_size - kTagSize; |
| 165 | } |
| 166 | |
| 167 | size_t GetCiphertextSize(size_t plaintext_size) const override { |
| 168 | return plaintext_size + kTagSize; |
| 169 | } |
| 170 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 171 | quiche::QuicheStringPiece GetKey() const override { |
| 172 | return quiche::QuicheStringPiece(); |
| 173 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 174 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 175 | quiche::QuicheStringPiece GetNoncePrefix() const override { |
| 176 | return quiche::QuicheStringPiece(); |
| 177 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 178 | |
| 179 | private: |
| 180 | enum { |
| 181 | kTagSize = 12, |
| 182 | }; |
| 183 | |
| 184 | const uint8_t tag_; |
| 185 | }; |
| 186 | |
| 187 | // TaggingDecrypter ensures that the final kTagSize bytes of the message all |
| 188 | // have the same value and then removes them. |
| 189 | class TaggingDecrypter : public QuicDecrypter { |
| 190 | public: |
| 191 | ~TaggingDecrypter() override {} |
| 192 | |
| 193 | // QuicDecrypter interface |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 194 | bool SetKey(quiche::QuicheStringPiece /*key*/) override { return true; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 195 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 196 | bool SetNoncePrefix(quiche::QuicheStringPiece /*nonce_prefix*/) override { |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 197 | return true; |
| 198 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 199 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 200 | bool SetIV(quiche::QuicheStringPiece /*iv*/) override { return true; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 201 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 202 | bool SetHeaderProtectionKey(quiche::QuicheStringPiece /*key*/) override { |
| 203 | return true; |
| 204 | } |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 205 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 206 | bool SetPreliminaryKey(quiche::QuicheStringPiece /*key*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 207 | QUIC_BUG << "should not be called"; |
| 208 | return false; |
| 209 | } |
| 210 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 211 | bool SetDiversificationNonce(const DiversificationNonce& /*key*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 212 | return true; |
| 213 | } |
| 214 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 215 | bool DecryptPacket(uint64_t /*packet_number*/, |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 216 | quiche::QuicheStringPiece /*associated_data*/, |
| 217 | quiche::QuicheStringPiece ciphertext, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 218 | char* output, |
| 219 | size_t* output_length, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 220 | size_t /*max_output_length*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 221 | if (ciphertext.size() < kTagSize) { |
| 222 | return false; |
| 223 | } |
| 224 | if (!CheckTag(ciphertext, GetTag(ciphertext))) { |
| 225 | return false; |
| 226 | } |
| 227 | *output_length = ciphertext.size() - kTagSize; |
| 228 | memcpy(output, ciphertext.data(), *output_length); |
| 229 | return true; |
| 230 | } |
| 231 | |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 232 | std::string GenerateHeaderProtectionMask( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 233 | QuicDataReader* /*sample_reader*/) override { |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 234 | return std::string(5, 0); |
| 235 | } |
| 236 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 237 | size_t GetKeySize() const override { return 0; } |
nharper | 965e592 | 2019-09-23 22:33:54 -0700 | [diff] [blame] | 238 | size_t GetNoncePrefixSize() const override { return 0; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 239 | size_t GetIVSize() const override { return 0; } |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 240 | quiche::QuicheStringPiece GetKey() const override { |
| 241 | return quiche::QuicheStringPiece(); |
| 242 | } |
| 243 | quiche::QuicheStringPiece GetNoncePrefix() const override { |
| 244 | return quiche::QuicheStringPiece(); |
| 245 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 246 | // Use a distinct value starting with 0xFFFFFF, which is never used by TLS. |
| 247 | uint32_t cipher_id() const override { return 0xFFFFFFF0; } |
| 248 | |
| 249 | protected: |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 250 | virtual uint8_t GetTag(quiche::QuicheStringPiece ciphertext) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 251 | return ciphertext.data()[ciphertext.size() - 1]; |
| 252 | } |
| 253 | |
| 254 | private: |
| 255 | enum { |
| 256 | kTagSize = 12, |
| 257 | }; |
| 258 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 259 | bool CheckTag(quiche::QuicheStringPiece ciphertext, uint8_t tag) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 260 | for (size_t i = ciphertext.size() - kTagSize; i < ciphertext.size(); i++) { |
| 261 | if (ciphertext.data()[i] != tag) { |
| 262 | return false; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return true; |
| 267 | } |
| 268 | }; |
| 269 | |
| 270 | // StringTaggingDecrypter ensures that the final kTagSize bytes of the message |
| 271 | // match the expected value. |
| 272 | class StrictTaggingDecrypter : public TaggingDecrypter { |
| 273 | public: |
| 274 | explicit StrictTaggingDecrypter(uint8_t tag) : tag_(tag) {} |
| 275 | ~StrictTaggingDecrypter() override {} |
| 276 | |
| 277 | // TaggingQuicDecrypter |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 278 | uint8_t GetTag(quiche::QuicheStringPiece /*ciphertext*/) override { |
| 279 | return tag_; |
| 280 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 281 | |
| 282 | // Use a distinct value starting with 0xFFFFFF, which is never used by TLS. |
| 283 | uint32_t cipher_id() const override { return 0xFFFFFFF1; } |
| 284 | |
| 285 | private: |
| 286 | const uint8_t tag_; |
| 287 | }; |
| 288 | |
| 289 | class TestConnectionHelper : public QuicConnectionHelperInterface { |
| 290 | public: |
| 291 | TestConnectionHelper(MockClock* clock, MockRandom* random_generator) |
| 292 | : clock_(clock), random_generator_(random_generator) { |
| 293 | clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 294 | } |
| 295 | TestConnectionHelper(const TestConnectionHelper&) = delete; |
| 296 | TestConnectionHelper& operator=(const TestConnectionHelper&) = delete; |
| 297 | |
| 298 | // QuicConnectionHelperInterface |
| 299 | const QuicClock* GetClock() const override { return clock_; } |
| 300 | |
| 301 | QuicRandom* GetRandomGenerator() override { return random_generator_; } |
| 302 | |
| 303 | QuicBufferAllocator* GetStreamSendBufferAllocator() override { |
| 304 | return &buffer_allocator_; |
| 305 | } |
| 306 | |
| 307 | private: |
| 308 | MockClock* clock_; |
| 309 | MockRandom* random_generator_; |
| 310 | SimpleBufferAllocator buffer_allocator_; |
| 311 | }; |
| 312 | |
| 313 | class TestAlarmFactory : public QuicAlarmFactory { |
| 314 | public: |
| 315 | class TestAlarm : public QuicAlarm { |
| 316 | public: |
| 317 | explicit TestAlarm(QuicArenaScopedPtr<QuicAlarm::Delegate> delegate) |
| 318 | : QuicAlarm(std::move(delegate)) {} |
| 319 | |
| 320 | void SetImpl() override {} |
| 321 | void CancelImpl() override {} |
| 322 | using QuicAlarm::Fire; |
| 323 | }; |
| 324 | |
| 325 | TestAlarmFactory() {} |
| 326 | TestAlarmFactory(const TestAlarmFactory&) = delete; |
| 327 | TestAlarmFactory& operator=(const TestAlarmFactory&) = delete; |
| 328 | |
| 329 | QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) override { |
| 330 | return new TestAlarm(QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)); |
| 331 | } |
| 332 | |
| 333 | QuicArenaScopedPtr<QuicAlarm> CreateAlarm( |
| 334 | QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, |
| 335 | QuicConnectionArena* arena) override { |
| 336 | return arena->New<TestAlarm>(std::move(delegate)); |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | class TestPacketWriter : public QuicPacketWriter { |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 341 | struct PacketBuffer { |
| 342 | QUIC_CACHELINE_ALIGNED char buffer[1500]; |
| 343 | bool in_use = false; |
| 344 | }; |
| 345 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 346 | public: |
| 347 | TestPacketWriter(ParsedQuicVersion version, MockClock* clock) |
| 348 | : version_(version), |
| 349 | framer_(SupportedVersions(version_), Perspective::IS_SERVER), |
wub | b442b86 | 2020-01-31 08:16:21 -0800 | [diff] [blame] | 350 | clock_(clock) { |
dschinazi | b953d02 | 2019-08-01 18:05:58 -0700 | [diff] [blame] | 351 | QuicFramerPeer::SetLastSerializedServerConnectionId(framer_.framer(), |
| 352 | TestConnectionId()); |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 353 | framer_.framer()->SetInitialObfuscators(TestConnectionId()); |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 354 | |
| 355 | for (int i = 0; i < 128; ++i) { |
| 356 | PacketBuffer* p = new PacketBuffer(); |
| 357 | packet_buffer_pool_.push_back(p); |
| 358 | packet_buffer_pool_index_[p->buffer] = p; |
| 359 | packet_buffer_free_list_.push_back(p); |
| 360 | } |
dschinazi | b953d02 | 2019-08-01 18:05:58 -0700 | [diff] [blame] | 361 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 362 | TestPacketWriter(const TestPacketWriter&) = delete; |
| 363 | TestPacketWriter& operator=(const TestPacketWriter&) = delete; |
| 364 | |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 365 | ~TestPacketWriter() override { |
| 366 | EXPECT_EQ(packet_buffer_pool_.size(), packet_buffer_free_list_.size()) |
| 367 | << packet_buffer_pool_.size() - packet_buffer_free_list_.size() |
| 368 | << " out of " << packet_buffer_pool_.size() |
| 369 | << " packet buffers have been leaked."; |
| 370 | for (auto p : packet_buffer_pool_) { |
| 371 | delete p; |
| 372 | } |
| 373 | } |
| 374 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 375 | // QuicPacketWriter interface |
| 376 | WriteResult WritePacket(const char* buffer, |
| 377 | size_t buf_len, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 378 | const QuicIpAddress& /*self_address*/, |
| 379 | const QuicSocketAddress& /*peer_address*/, |
| 380 | PerPacketOptions* /*options*/) override { |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 381 | // If the buffer is allocated from the pool, return it back to the pool. |
| 382 | // Note the buffer content doesn't change. |
| 383 | if (packet_buffer_pool_index_.find(const_cast<char*>(buffer)) != |
| 384 | packet_buffer_pool_index_.end()) { |
| 385 | FreePacketBuffer(buffer); |
| 386 | } |
| 387 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 388 | QuicEncryptedPacket packet(buffer, buf_len); |
| 389 | ++packets_write_attempts_; |
| 390 | |
| 391 | if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { |
| 392 | final_bytes_of_previous_packet_ = final_bytes_of_last_packet_; |
| 393 | memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, |
| 394 | sizeof(final_bytes_of_last_packet_)); |
| 395 | } |
| 396 | |
| 397 | if (use_tagging_decrypter_) { |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 398 | if (framer_.framer()->version().KnowsWhichDecrypterToUse()) { |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 399 | framer_.framer()->InstallDecrypter( |
| 400 | ENCRYPTION_INITIAL, std::make_unique<TaggingDecrypter>()); |
| 401 | framer_.framer()->InstallDecrypter( |
fayang | 18ff23b | 2020-01-28 09:19:00 -0800 | [diff] [blame] | 402 | ENCRYPTION_HANDSHAKE, std::make_unique<TaggingDecrypter>()); |
| 403 | framer_.framer()->InstallDecrypter( |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 404 | ENCRYPTION_ZERO_RTT, std::make_unique<TaggingDecrypter>()); |
| 405 | framer_.framer()->InstallDecrypter( |
| 406 | ENCRYPTION_FORWARD_SECURE, std::make_unique<TaggingDecrypter>()); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 407 | } else { |
| 408 | framer_.framer()->SetDecrypter(ENCRYPTION_INITIAL, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 409 | std::make_unique<TaggingDecrypter>()); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 410 | } |
| 411 | } else if (framer_.framer()->version().KnowsWhichDecrypterToUse()) { |
| 412 | framer_.framer()->InstallDecrypter( |
| 413 | ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 414 | std::make_unique<NullDecrypter>(Perspective::IS_SERVER)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 415 | } |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 416 | EXPECT_TRUE(framer_.ProcessPacket(packet)) |
| 417 | << framer_.framer()->detailed_error(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 418 | if (block_on_next_write_) { |
| 419 | write_blocked_ = true; |
| 420 | block_on_next_write_ = false; |
| 421 | } |
| 422 | if (next_packet_too_large_) { |
| 423 | next_packet_too_large_ = false; |
| 424 | return WriteResult(WRITE_STATUS_ERROR, QUIC_EMSGSIZE); |
| 425 | } |
| 426 | if (always_get_packet_too_large_) { |
| 427 | return WriteResult(WRITE_STATUS_ERROR, QUIC_EMSGSIZE); |
| 428 | } |
| 429 | if (IsWriteBlocked()) { |
| 430 | return WriteResult(is_write_blocked_data_buffered_ |
| 431 | ? WRITE_STATUS_BLOCKED_DATA_BUFFERED |
| 432 | : WRITE_STATUS_BLOCKED, |
| 433 | 0); |
| 434 | } |
| 435 | |
| 436 | if (ShouldWriteFail()) { |
| 437 | return WriteResult(WRITE_STATUS_ERROR, 0); |
| 438 | } |
| 439 | |
| 440 | last_packet_size_ = packet.length(); |
| 441 | last_packet_header_ = framer_.header(); |
rch | d672c6d | 2019-11-27 15:30:54 -0800 | [diff] [blame] | 442 | if (!framer_.connection_close_frames().empty()) { |
| 443 | ++connection_close_packets_; |
| 444 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 445 | if (!write_pause_time_delta_.IsZero()) { |
| 446 | clock_->AdvanceTime(write_pause_time_delta_); |
| 447 | } |
wub | b442b86 | 2020-01-31 08:16:21 -0800 | [diff] [blame] | 448 | if (is_batch_mode_) { |
| 449 | bytes_buffered_ += last_packet_size_; |
| 450 | return WriteResult(WRITE_STATUS_OK, 0); |
| 451 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 452 | return WriteResult(WRITE_STATUS_OK, last_packet_size_); |
| 453 | } |
| 454 | |
| 455 | bool ShouldWriteFail() { return write_should_fail_; } |
| 456 | |
| 457 | bool IsWriteBlocked() const override { return write_blocked_; } |
| 458 | |
| 459 | void SetWriteBlocked() { write_blocked_ = true; } |
| 460 | |
| 461 | void SetWritable() override { write_blocked_ = false; } |
| 462 | |
| 463 | void SetShouldWriteFail() { write_should_fail_ = true; } |
| 464 | |
| 465 | QuicByteCount GetMaxPacketSize( |
| 466 | const QuicSocketAddress& /*peer_address*/) const override { |
| 467 | return max_packet_size_; |
| 468 | } |
| 469 | |
dschinazi | 61eb643 | 2019-06-14 16:27:16 -0700 | [diff] [blame] | 470 | bool SupportsReleaseTime() const override { return supports_release_time_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 471 | |
| 472 | bool IsBatchMode() const override { return is_batch_mode_; } |
| 473 | |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 474 | QuicPacketBuffer GetNextWriteLocation( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 475 | const QuicIpAddress& /*self_address*/, |
| 476 | const QuicSocketAddress& /*peer_address*/) override { |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 477 | if (GetQuicReloadableFlag(quic_avoid_leak_writer_buffer)) { |
| 478 | return {AllocPacketBuffer(), |
| 479 | [this](const char* p) { FreePacketBuffer(p); }}; |
| 480 | } |
| 481 | // Do not use writer buffer for serializing packets. |
| 482 | return {nullptr, nullptr}; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | WriteResult Flush() override { |
wub | b442b86 | 2020-01-31 08:16:21 -0800 | [diff] [blame] | 486 | flush_attempts_++; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 487 | if (block_on_next_flush_) { |
| 488 | block_on_next_flush_ = false; |
| 489 | SetWriteBlocked(); |
| 490 | return WriteResult(WRITE_STATUS_BLOCKED, /*errno*/ -1); |
| 491 | } |
wub | 748e20b | 2020-03-20 14:33:59 -0700 | [diff] [blame] | 492 | if (write_should_fail_) { |
| 493 | return WriteResult(WRITE_STATUS_ERROR, /*errno*/ -1); |
| 494 | } |
wub | b442b86 | 2020-01-31 08:16:21 -0800 | [diff] [blame] | 495 | int bytes_flushed = bytes_buffered_; |
| 496 | bytes_buffered_ = 0; |
| 497 | return WriteResult(WRITE_STATUS_OK, bytes_flushed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void BlockOnNextFlush() { block_on_next_flush_ = true; } |
| 501 | |
| 502 | void BlockOnNextWrite() { block_on_next_write_ = true; } |
| 503 | |
| 504 | void SimulateNextPacketTooLarge() { next_packet_too_large_ = true; } |
| 505 | |
| 506 | void AlwaysGetPacketTooLarge() { always_get_packet_too_large_ = true; } |
| 507 | |
| 508 | // Sets the amount of time that the writer should before the actual write. |
| 509 | void SetWritePauseTimeDelta(QuicTime::Delta delta) { |
| 510 | write_pause_time_delta_ = delta; |
| 511 | } |
| 512 | |
| 513 | void SetBatchMode(bool new_value) { is_batch_mode_ = new_value; } |
| 514 | |
| 515 | const QuicPacketHeader& header() { return framer_.header(); } |
| 516 | |
| 517 | size_t frame_count() const { return framer_.num_frames(); } |
| 518 | |
| 519 | const std::vector<QuicAckFrame>& ack_frames() const { |
| 520 | return framer_.ack_frames(); |
| 521 | } |
| 522 | |
| 523 | const std::vector<QuicStopWaitingFrame>& stop_waiting_frames() const { |
| 524 | return framer_.stop_waiting_frames(); |
| 525 | } |
| 526 | |
| 527 | const std::vector<QuicConnectionCloseFrame>& connection_close_frames() const { |
| 528 | return framer_.connection_close_frames(); |
| 529 | } |
| 530 | |
| 531 | const std::vector<QuicRstStreamFrame>& rst_stream_frames() const { |
| 532 | return framer_.rst_stream_frames(); |
| 533 | } |
| 534 | |
| 535 | const std::vector<std::unique_ptr<QuicStreamFrame>>& stream_frames() const { |
| 536 | return framer_.stream_frames(); |
| 537 | } |
| 538 | |
| 539 | const std::vector<std::unique_ptr<QuicCryptoFrame>>& crypto_frames() const { |
| 540 | return framer_.crypto_frames(); |
| 541 | } |
| 542 | |
| 543 | const std::vector<QuicPingFrame>& ping_frames() const { |
| 544 | return framer_.ping_frames(); |
| 545 | } |
| 546 | |
| 547 | const std::vector<QuicMessageFrame>& message_frames() const { |
| 548 | return framer_.message_frames(); |
| 549 | } |
| 550 | |
| 551 | const std::vector<QuicWindowUpdateFrame>& window_update_frames() const { |
| 552 | return framer_.window_update_frames(); |
| 553 | } |
| 554 | |
| 555 | const std::vector<QuicPaddingFrame>& padding_frames() const { |
| 556 | return framer_.padding_frames(); |
| 557 | } |
| 558 | |
| 559 | const std::vector<QuicPathChallengeFrame>& path_challenge_frames() const { |
| 560 | return framer_.path_challenge_frames(); |
| 561 | } |
| 562 | |
| 563 | const std::vector<QuicPathResponseFrame>& path_response_frames() const { |
| 564 | return framer_.path_response_frames(); |
| 565 | } |
| 566 | |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 567 | const QuicEncryptedPacket* coalesced_packet() const { |
| 568 | return framer_.coalesced_packet(); |
| 569 | } |
| 570 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 571 | size_t last_packet_size() { return last_packet_size_; } |
| 572 | |
| 573 | const QuicPacketHeader& last_packet_header() const { |
| 574 | return last_packet_header_; |
| 575 | } |
| 576 | |
| 577 | const QuicVersionNegotiationPacket* version_negotiation_packet() { |
| 578 | return framer_.version_negotiation_packet(); |
| 579 | } |
| 580 | |
| 581 | void set_is_write_blocked_data_buffered(bool buffered) { |
| 582 | is_write_blocked_data_buffered_ = buffered; |
| 583 | } |
| 584 | |
| 585 | void set_perspective(Perspective perspective) { |
| 586 | // We invert perspective here, because the framer needs to parse packets |
| 587 | // we send. |
| 588 | QuicFramerPeer::SetPerspective(framer_.framer(), |
nharper | 4eba09b | 2019-06-26 20:17:25 -0700 | [diff] [blame] | 589 | QuicUtils::InvertPerspective(perspective)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | // final_bytes_of_last_packet_ returns the last four bytes of the previous |
| 593 | // packet as a little-endian, uint32_t. This is intended to be used with a |
| 594 | // TaggingEncrypter so that tests can determine which encrypter was used for |
| 595 | // a given packet. |
| 596 | uint32_t final_bytes_of_last_packet() { return final_bytes_of_last_packet_; } |
| 597 | |
| 598 | // Returns the final bytes of the second to last packet. |
| 599 | uint32_t final_bytes_of_previous_packet() { |
| 600 | return final_bytes_of_previous_packet_; |
| 601 | } |
| 602 | |
| 603 | void use_tagging_decrypter() { use_tagging_decrypter_ = true; } |
| 604 | |
wub | b442b86 | 2020-01-31 08:16:21 -0800 | [diff] [blame] | 605 | uint32_t packets_write_attempts() const { return packets_write_attempts_; } |
| 606 | |
| 607 | uint32_t flush_attempts() const { return flush_attempts_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 608 | |
rch | d672c6d | 2019-11-27 15:30:54 -0800 | [diff] [blame] | 609 | uint32_t connection_close_packets() const { |
| 610 | return connection_close_packets_; |
| 611 | } |
| 612 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 613 | void Reset() { framer_.Reset(); } |
| 614 | |
| 615 | void SetSupportedVersions(const ParsedQuicVersionVector& versions) { |
| 616 | framer_.SetSupportedVersions(versions); |
| 617 | } |
| 618 | |
| 619 | void set_max_packet_size(QuicByteCount max_packet_size) { |
| 620 | max_packet_size_ = max_packet_size; |
| 621 | } |
| 622 | |
| 623 | void set_supports_release_time(bool supports_release_time) { |
| 624 | supports_release_time_ = supports_release_time; |
| 625 | } |
| 626 | |
| 627 | SimpleQuicFramer* framer() { return &framer_; } |
| 628 | |
| 629 | private: |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 630 | char* AllocPacketBuffer() { |
| 631 | PacketBuffer* p = packet_buffer_free_list_.front(); |
| 632 | EXPECT_FALSE(p->in_use); |
| 633 | p->in_use = true; |
| 634 | packet_buffer_free_list_.pop_front(); |
| 635 | return p->buffer; |
| 636 | } |
| 637 | |
| 638 | void FreePacketBuffer(const char* buffer) { |
| 639 | auto iter = packet_buffer_pool_index_.find(const_cast<char*>(buffer)); |
| 640 | ASSERT_TRUE(iter != packet_buffer_pool_index_.end()); |
| 641 | PacketBuffer* p = iter->second; |
| 642 | ASSERT_TRUE(p->in_use); |
| 643 | p->in_use = false; |
| 644 | packet_buffer_free_list_.push_back(p); |
| 645 | } |
| 646 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 647 | ParsedQuicVersion version_; |
| 648 | SimpleQuicFramer framer_; |
wub | b442b86 | 2020-01-31 08:16:21 -0800 | [diff] [blame] | 649 | size_t last_packet_size_ = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 650 | QuicPacketHeader last_packet_header_; |
wub | b442b86 | 2020-01-31 08:16:21 -0800 | [diff] [blame] | 651 | bool write_blocked_ = false; |
| 652 | bool write_should_fail_ = false; |
| 653 | bool block_on_next_flush_ = false; |
| 654 | bool block_on_next_write_ = false; |
| 655 | bool next_packet_too_large_ = false; |
| 656 | bool always_get_packet_too_large_ = false; |
| 657 | bool is_write_blocked_data_buffered_ = false; |
| 658 | bool is_batch_mode_ = false; |
| 659 | // Number of times Flush() was called. |
| 660 | uint32_t flush_attempts_ = 0; |
| 661 | // (Batch mode only) Number of bytes buffered in writer. It is used as the |
| 662 | // return value of a successful Flush(). |
| 663 | uint32_t bytes_buffered_ = 0; |
| 664 | uint32_t final_bytes_of_last_packet_ = 0; |
| 665 | uint32_t final_bytes_of_previous_packet_ = 0; |
| 666 | bool use_tagging_decrypter_ = false; |
| 667 | uint32_t packets_write_attempts_ = 0; |
| 668 | uint32_t connection_close_packets_ = 0; |
| 669 | MockClock* clock_ = nullptr; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 670 | // If non-zero, the clock will pause during WritePacket for this amount of |
| 671 | // time. |
wub | b442b86 | 2020-01-31 08:16:21 -0800 | [diff] [blame] | 672 | QuicTime::Delta write_pause_time_delta_ = QuicTime::Delta::Zero(); |
| 673 | QuicByteCount max_packet_size_ = kMaxOutgoingPacketSize; |
| 674 | bool supports_release_time_ = false; |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 675 | // Used to verify writer-allocated packet buffers are properly released. |
| 676 | std::vector<PacketBuffer*> packet_buffer_pool_; |
| 677 | // Buffer address => Address of the owning PacketBuffer. |
| 678 | QuicHashMap<char*, PacketBuffer*> packet_buffer_pool_index_; |
| 679 | // Indices in packet_buffer_pool_ that are not allocated. |
| 680 | std::list<PacketBuffer*> packet_buffer_free_list_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 681 | }; |
| 682 | |
| 683 | class TestConnection : public QuicConnection { |
| 684 | public: |
| 685 | TestConnection(QuicConnectionId connection_id, |
| 686 | QuicSocketAddress address, |
| 687 | TestConnectionHelper* helper, |
| 688 | TestAlarmFactory* alarm_factory, |
| 689 | TestPacketWriter* writer, |
| 690 | Perspective perspective, |
| 691 | ParsedQuicVersion version) |
| 692 | : QuicConnection(connection_id, |
| 693 | address, |
| 694 | helper, |
| 695 | alarm_factory, |
| 696 | writer, |
| 697 | /* owns_writer= */ false, |
| 698 | perspective, |
| 699 | SupportedVersions(version)), |
| 700 | notifier_(nullptr) { |
| 701 | writer->set_perspective(perspective); |
| 702 | SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 703 | std::make_unique<NullEncrypter>(perspective)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 704 | SetDataProducer(&producer_); |
| 705 | } |
| 706 | TestConnection(const TestConnection&) = delete; |
| 707 | TestConnection& operator=(const TestConnection&) = delete; |
| 708 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 709 | void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) { |
| 710 | QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm); |
| 711 | } |
| 712 | |
| 713 | void SetLossAlgorithm(LossDetectionInterface* loss_algorithm) { |
| 714 | QuicConnectionPeer::SetLossAlgorithm(this, loss_algorithm); |
| 715 | } |
| 716 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 717 | void SendPacket(EncryptionLevel /*level*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 718 | uint64_t packet_number, |
| 719 | std::unique_ptr<QuicPacket> packet, |
| 720 | HasRetransmittableData retransmittable, |
| 721 | bool has_ack, |
| 722 | bool has_pending_frames) { |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 723 | ScopedPacketFlusher flusher(this); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 724 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 725 | size_t encrypted_length = |
| 726 | QuicConnectionPeer::GetFramer(this)->EncryptPayload( |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 727 | ENCRYPTION_INITIAL, QuicPacketNumber(packet_number), *packet, |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 728 | buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 729 | SerializedPacket serialized_packet( |
| 730 | QuicPacketNumber(packet_number), PACKET_4BYTE_PACKET_NUMBER, buffer, |
| 731 | encrypted_length, has_ack, has_pending_frames); |
| 732 | if (retransmittable == HAS_RETRANSMITTABLE_DATA) { |
| 733 | serialized_packet.retransmittable_frames.push_back( |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 734 | QuicFrame(QuicPingFrame())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 735 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 736 | OnSerializedPacket(std::move(serialized_packet)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | QuicConsumedData SaveAndSendStreamData(QuicStreamId id, |
| 740 | const struct iovec* iov, |
| 741 | int iov_count, |
| 742 | size_t total_length, |
| 743 | QuicStreamOffset offset, |
| 744 | StreamSendingState state) { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 745 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 746 | producer_.SaveStreamData(id, iov, iov_count, 0u, total_length); |
| 747 | if (notifier_ != nullptr) { |
| 748 | return notifier_->WriteOrBufferData(id, total_length, state); |
| 749 | } |
| 750 | return QuicConnection::SendStreamData(id, total_length, offset, state); |
| 751 | } |
| 752 | |
| 753 | QuicConsumedData SendStreamDataWithString(QuicStreamId id, |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 754 | quiche::QuicheStringPiece data, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 755 | QuicStreamOffset offset, |
| 756 | StreamSendingState state) { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 757 | ScopedPacketFlusher flusher(this); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 758 | if (!QuicUtils::IsCryptoStreamId(transport_version(), id) && |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 759 | this->encryption_level() == ENCRYPTION_INITIAL) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 760 | this->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
fayang | fa3b1d6 | 2019-11-18 08:02:13 -0800 | [diff] [blame] | 761 | if (perspective() == Perspective::IS_CLIENT && !IsHandshakeComplete()) { |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 762 | OnHandshakeComplete(); |
| 763 | } |
| 764 | if (version().SupportsAntiAmplificationLimit()) { |
| 765 | QuicConnectionPeer::SetAddressValidated(this); |
| 766 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 767 | } |
| 768 | struct iovec iov; |
| 769 | MakeIOVector(data, &iov); |
| 770 | return SaveAndSendStreamData(id, &iov, 1, data.length(), offset, state); |
| 771 | } |
| 772 | |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 773 | QuicConsumedData SendApplicationDataAtLevel(EncryptionLevel encryption_level, |
| 774 | QuicStreamId id, |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 775 | quiche::QuicheStringPiece data, |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 776 | QuicStreamOffset offset, |
| 777 | StreamSendingState state) { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 778 | ScopedPacketFlusher flusher(this); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 779 | DCHECK(encryption_level >= ENCRYPTION_ZERO_RTT); |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 780 | SetEncrypter(encryption_level, std::make_unique<TaggingEncrypter>(0x01)); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 781 | SetDefaultEncryptionLevel(encryption_level); |
| 782 | struct iovec iov; |
| 783 | MakeIOVector(data, &iov); |
| 784 | return SaveAndSendStreamData(id, &iov, 1, data.length(), offset, state); |
| 785 | } |
| 786 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 787 | QuicConsumedData SendStreamData3() { |
| 788 | return SendStreamDataWithString( |
| 789 | GetNthClientInitiatedStreamId(1, transport_version()), "food", 0, |
| 790 | NO_FIN); |
| 791 | } |
| 792 | |
| 793 | QuicConsumedData SendStreamData5() { |
| 794 | return SendStreamDataWithString( |
| 795 | GetNthClientInitiatedStreamId(2, transport_version()), "food2", 0, |
| 796 | NO_FIN); |
| 797 | } |
| 798 | |
| 799 | // Ensures the connection can write stream data before writing. |
| 800 | QuicConsumedData EnsureWritableAndSendStreamData5() { |
ianswett | fc16a2b | 2020-05-18 16:05:49 -0700 | [diff] [blame] | 801 | EXPECT_TRUE(CanWrite(HAS_RETRANSMITTABLE_DATA)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 802 | return SendStreamData5(); |
| 803 | } |
| 804 | |
| 805 | // The crypto stream has special semantics so that it is not blocked by a |
| 806 | // congestion window limitation, and also so that it gets put into a separate |
| 807 | // packet (so that it is easier to reason about a crypto frame not being |
| 808 | // split needlessly across packet boundaries). As a result, we have separate |
| 809 | // tests for some cases for this stream. |
| 810 | QuicConsumedData SendCryptoStreamData() { |
| 811 | QuicStreamOffset offset = 0; |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 812 | quiche::QuicheStringPiece data("chlo"); |
fayang | 2ce6608 | 2019-10-02 06:29:04 -0700 | [diff] [blame] | 813 | if (!QuicVersionUsesCryptoFrames(transport_version())) { |
| 814 | return SendCryptoDataWithString(data, offset); |
| 815 | } |
| 816 | producer_.SaveCryptoData(ENCRYPTION_INITIAL, offset, data); |
| 817 | size_t bytes_written; |
| 818 | if (notifier_) { |
| 819 | bytes_written = |
| 820 | notifier_->WriteCryptoData(ENCRYPTION_INITIAL, data.length(), offset); |
| 821 | } else { |
| 822 | bytes_written = QuicConnection::SendCryptoData(ENCRYPTION_INITIAL, |
| 823 | data.length(), offset); |
| 824 | } |
| 825 | return QuicConsumedData(bytes_written, /*fin_consumed*/ false); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 826 | } |
| 827 | |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 828 | QuicConsumedData SendCryptoDataWithString(quiche::QuicheStringPiece data, |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 829 | QuicStreamOffset offset) { |
fayang | 18ff23b | 2020-01-28 09:19:00 -0800 | [diff] [blame] | 830 | return SendCryptoDataWithString(data, offset, ENCRYPTION_INITIAL); |
| 831 | } |
| 832 | |
| 833 | QuicConsumedData SendCryptoDataWithString(quiche::QuicheStringPiece data, |
| 834 | QuicStreamOffset offset, |
| 835 | EncryptionLevel encryption_level) { |
QUICHE team | ea74008 | 2019-03-11 17:58:43 -0700 | [diff] [blame] | 836 | if (!QuicVersionUsesCryptoFrames(transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 837 | return SendStreamDataWithString( |
| 838 | QuicUtils::GetCryptoStreamId(transport_version()), data, offset, |
| 839 | NO_FIN); |
| 840 | } |
fayang | 18ff23b | 2020-01-28 09:19:00 -0800 | [diff] [blame] | 841 | producer_.SaveCryptoData(encryption_level, offset, data); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 842 | size_t bytes_written; |
| 843 | if (notifier_) { |
| 844 | bytes_written = |
fayang | 18ff23b | 2020-01-28 09:19:00 -0800 | [diff] [blame] | 845 | notifier_->WriteCryptoData(encryption_level, data.length(), offset); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 846 | } else { |
fayang | 18ff23b | 2020-01-28 09:19:00 -0800 | [diff] [blame] | 847 | bytes_written = QuicConnection::SendCryptoData(encryption_level, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 848 | data.length(), offset); |
| 849 | } |
| 850 | return QuicConsumedData(bytes_written, /*fin_consumed*/ false); |
| 851 | } |
| 852 | |
| 853 | void set_version(ParsedQuicVersion version) { |
| 854 | QuicConnectionPeer::GetFramer(this)->set_version(version); |
| 855 | } |
| 856 | |
| 857 | void SetSupportedVersions(const ParsedQuicVersionVector& versions) { |
| 858 | QuicConnectionPeer::GetFramer(this)->SetSupportedVersions(versions); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 859 | writer()->SetSupportedVersions(versions); |
| 860 | } |
| 861 | |
| 862 | void set_perspective(Perspective perspective) { |
| 863 | writer()->set_perspective(perspective); |
| 864 | QuicConnectionPeer::SetPerspective(this, perspective); |
| 865 | } |
| 866 | |
| 867 | // Enable path MTU discovery. Assumes that the test is performed from the |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 868 | // server perspective and the higher value of MTU target is used. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 869 | void EnablePathMtuDiscovery(MockSendAlgorithm* send_algorithm) { |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 870 | ASSERT_EQ(Perspective::IS_SERVER, perspective()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 871 | |
| 872 | QuicConfig config; |
| 873 | QuicTagVector connection_options; |
| 874 | connection_options.push_back(kMTUH); |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 875 | config.SetInitialReceivedConnectionOptions(connection_options); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 876 | EXPECT_CALL(*send_algorithm, SetFromConfig(_, _)); |
| 877 | SetFromConfig(config); |
| 878 | |
| 879 | // Normally, the pacing would be disabled in the test, but calling |
| 880 | // SetFromConfig enables it. Set nearly-infinite bandwidth to make the |
| 881 | // pacing algorithm work. |
| 882 | EXPECT_CALL(*send_algorithm, PacingRate(_)) |
| 883 | .WillRepeatedly(Return(QuicBandwidth::Infinite())); |
| 884 | } |
| 885 | |
| 886 | TestAlarmFactory::TestAlarm* GetAckAlarm() { |
| 887 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 888 | QuicConnectionPeer::GetAckAlarm(this)); |
| 889 | } |
| 890 | |
| 891 | TestAlarmFactory::TestAlarm* GetPingAlarm() { |
| 892 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 893 | QuicConnectionPeer::GetPingAlarm(this)); |
| 894 | } |
| 895 | |
| 896 | TestAlarmFactory::TestAlarm* GetRetransmissionAlarm() { |
| 897 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 898 | QuicConnectionPeer::GetRetransmissionAlarm(this)); |
| 899 | } |
| 900 | |
| 901 | TestAlarmFactory::TestAlarm* GetSendAlarm() { |
| 902 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 903 | QuicConnectionPeer::GetSendAlarm(this)); |
| 904 | } |
| 905 | |
| 906 | TestAlarmFactory::TestAlarm* GetTimeoutAlarm() { |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 907 | if (GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 908 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 909 | QuicConnectionPeer::GetIdleNetworkDetectorAlarm(this)); |
| 910 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 911 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 912 | QuicConnectionPeer::GetTimeoutAlarm(this)); |
| 913 | } |
| 914 | |
| 915 | TestAlarmFactory::TestAlarm* GetMtuDiscoveryAlarm() { |
| 916 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 917 | QuicConnectionPeer::GetMtuDiscoveryAlarm(this)); |
| 918 | } |
| 919 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 920 | TestAlarmFactory::TestAlarm* GetProcessUndecryptablePacketsAlarm() { |
| 921 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 922 | QuicConnectionPeer::GetProcessUndecryptablePacketsAlarm(this)); |
| 923 | } |
| 924 | |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 925 | TestAlarmFactory::TestAlarm* GetBlackholeDetectorAlarm() { |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 926 | return reinterpret_cast<TestAlarmFactory::TestAlarm*>( |
| 927 | QuicConnectionPeer::GetBlackholeDetectorAlarm(this)); |
| 928 | } |
| 929 | |
| 930 | void PathDegradingTimeout() { |
| 931 | DCHECK(PathDegradingDetectionInProgress()); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 932 | GetBlackholeDetectorAlarm()->Fire(); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | bool PathDegradingDetectionInProgress() { |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 936 | return QuicConnectionPeer::GetPathDegradingDeadline(this).IsInitialized(); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | bool BlackholeDetectionInProgress() { |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 940 | return QuicConnectionPeer::GetBlackholeDetectionDeadline(this) |
| 941 | .IsInitialized(); |
| 942 | } |
| 943 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 944 | void SetMaxTailLossProbes(size_t max_tail_loss_probes) { |
| 945 | QuicSentPacketManagerPeer::SetMaxTailLossProbes( |
| 946 | QuicConnectionPeer::GetSentPacketManager(this), max_tail_loss_probes); |
| 947 | } |
| 948 | |
| 949 | QuicByteCount GetBytesInFlight() { |
ianswett | 9f459cb | 2019-04-21 06:39:59 -0700 | [diff] [blame] | 950 | return QuicConnectionPeer::GetSentPacketManager(this)->GetBytesInFlight(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | void set_notifier(SimpleSessionNotifier* notifier) { notifier_ = notifier; } |
| 954 | |
| 955 | void ReturnEffectivePeerAddressForNextPacket(const QuicSocketAddress& addr) { |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 956 | next_effective_peer_addr_ = std::make_unique<QuicSocketAddress>(addr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 957 | } |
| 958 | |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 959 | bool PtoEnabled() { |
| 960 | if (QuicConnectionPeer::GetSentPacketManager(this)->pto_enabled()) { |
| 961 | // PTO mode is default enabled for T099. And TLP/RTO related tests are |
| 962 | // stale. |
fayang | 5d01198 | 2020-05-13 14:14:38 -0700 | [diff] [blame] | 963 | DCHECK(PROTOCOL_TLS1_3 == version().handshake_protocol || |
| 964 | GetQuicReloadableFlag(quic_default_on_pto)); |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 965 | return true; |
| 966 | } |
| 967 | return false; |
| 968 | } |
| 969 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 970 | SimpleDataProducer* producer() { return &producer_; } |
| 971 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 972 | using QuicConnection::active_effective_peer_migration_type; |
| 973 | using QuicConnection::IsCurrentPacketConnectivityProbing; |
| 974 | using QuicConnection::SelectMutualVersion; |
| 975 | using QuicConnection::SendProbingRetransmissions; |
| 976 | using QuicConnection::set_defer_send_in_response_to_packets; |
| 977 | |
| 978 | protected: |
| 979 | QuicSocketAddress GetEffectivePeerAddressFromCurrentPacket() const override { |
| 980 | if (next_effective_peer_addr_) { |
| 981 | return *std::move(next_effective_peer_addr_); |
| 982 | } |
| 983 | return QuicConnection::GetEffectivePeerAddressFromCurrentPacket(); |
| 984 | } |
| 985 | |
| 986 | private: |
| 987 | TestPacketWriter* writer() { |
| 988 | return static_cast<TestPacketWriter*>(QuicConnection::writer()); |
| 989 | } |
| 990 | |
| 991 | SimpleDataProducer producer_; |
| 992 | |
| 993 | SimpleSessionNotifier* notifier_; |
| 994 | |
| 995 | std::unique_ptr<QuicSocketAddress> next_effective_peer_addr_; |
| 996 | }; |
| 997 | |
| 998 | enum class AckResponse { kDefer, kImmediate }; |
| 999 | |
| 1000 | // Run tests with combinations of {ParsedQuicVersion, AckResponse}. |
| 1001 | struct TestParams { |
| 1002 | TestParams(ParsedQuicVersion version, |
| 1003 | AckResponse ack_response, |
| 1004 | bool no_stop_waiting) |
| 1005 | : version(version), |
| 1006 | ack_response(ack_response), |
| 1007 | no_stop_waiting(no_stop_waiting) {} |
| 1008 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1009 | ParsedQuicVersion version; |
| 1010 | AckResponse ack_response; |
| 1011 | bool no_stop_waiting; |
| 1012 | }; |
| 1013 | |
dschinazi | 142051a | 2019-09-18 18:17:29 -0700 | [diff] [blame] | 1014 | // Used by ::testing::PrintToStringParamName(). |
| 1015 | std::string PrintToString(const TestParams& p) { |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 1016 | return quiche::QuicheStrCat( |
dschinazi | 142051a | 2019-09-18 18:17:29 -0700 | [diff] [blame] | 1017 | ParsedQuicVersionToString(p.version), "_", |
| 1018 | (p.ack_response == AckResponse::kDefer ? "defer" : "immediate"), "_", |
| 1019 | (p.no_stop_waiting ? "No" : ""), "StopWaiting"); |
| 1020 | } |
| 1021 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1022 | // Constructs various test permutations. |
| 1023 | std::vector<TestParams> GetTestParams() { |
| 1024 | QuicFlagSaver flags; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1025 | std::vector<TestParams> params; |
| 1026 | ParsedQuicVersionVector all_supported_versions = AllSupportedVersions(); |
| 1027 | for (size_t i = 0; i < all_supported_versions.size(); ++i) { |
| 1028 | for (AckResponse ack_response : |
| 1029 | {AckResponse::kDefer, AckResponse::kImmediate}) { |
dschinazi | 142051a | 2019-09-18 18:17:29 -0700 | [diff] [blame] | 1030 | params.push_back( |
| 1031 | TestParams(all_supported_versions[i], ack_response, true)); |
| 1032 | if (!VersionHasIetfInvariantHeader( |
| 1033 | all_supported_versions[i].transport_version)) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1034 | params.push_back( |
dschinazi | 142051a | 2019-09-18 18:17:29 -0700 | [diff] [blame] | 1035 | TestParams(all_supported_versions[i], ack_response, false)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | return params; |
| 1040 | } |
| 1041 | |
| 1042 | class QuicConnectionTest : public QuicTestWithParam<TestParams> { |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 1043 | public: |
| 1044 | // For tests that do silent connection closes, no such packet is generated. In |
| 1045 | // order to verify the contents of the OnConnectionClosed upcall, EXPECTs |
| 1046 | // should invoke this method, saving the frame, and then the test can verify |
| 1047 | // the contents. |
| 1048 | void SaveConnectionCloseFrame(const QuicConnectionCloseFrame& frame, |
| 1049 | ConnectionCloseSource /*source*/) { |
| 1050 | saved_connection_close_frame_ = frame; |
| 1051 | connection_close_frame_count_++; |
| 1052 | } |
| 1053 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1054 | protected: |
| 1055 | QuicConnectionTest() |
| 1056 | : connection_id_(TestConnectionId()), |
| 1057 | framer_(SupportedVersions(version()), |
| 1058 | QuicTime::Zero(), |
| 1059 | Perspective::IS_CLIENT, |
| 1060 | connection_id_.length()), |
| 1061 | send_algorithm_(new StrictMock<MockSendAlgorithm>), |
| 1062 | loss_algorithm_(new MockLossAlgorithm()), |
| 1063 | helper_(new TestConnectionHelper(&clock_, &random_generator_)), |
| 1064 | alarm_factory_(new TestAlarmFactory()), |
| 1065 | peer_framer_(SupportedVersions(version()), |
| 1066 | QuicTime::Zero(), |
| 1067 | Perspective::IS_SERVER, |
| 1068 | connection_id_.length()), |
| 1069 | peer_creator_(connection_id_, |
| 1070 | &peer_framer_, |
| 1071 | /*delegate=*/nullptr), |
| 1072 | writer_(new TestPacketWriter(version(), &clock_)), |
| 1073 | connection_(connection_id_, |
| 1074 | kPeerAddress, |
| 1075 | helper_.get(), |
| 1076 | alarm_factory_.get(), |
| 1077 | writer_.get(), |
| 1078 | Perspective::IS_CLIENT, |
| 1079 | version()), |
| 1080 | creator_(QuicConnectionPeer::GetPacketCreator(&connection_)), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1081 | manager_(QuicConnectionPeer::GetSentPacketManager(&connection_)), |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 1082 | frame1_(0, false, 0, quiche::QuicheStringPiece(data1)), |
| 1083 | frame2_(0, false, 3, quiche::QuicheStringPiece(data2)), |
| 1084 | crypto_frame_(ENCRYPTION_INITIAL, 0, quiche::QuicheStringPiece(data1)), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1085 | packet_number_length_(PACKET_4BYTE_PACKET_NUMBER), |
| 1086 | connection_id_included_(CONNECTION_ID_PRESENT), |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 1087 | notifier_(&connection_), |
| 1088 | connection_close_frame_count_(0) { |
dschinazi | 142051a | 2019-09-18 18:17:29 -0700 | [diff] [blame] | 1089 | QUIC_DVLOG(2) << "QuicConnectionTest(" << PrintToString(GetParam()) << ")"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1090 | connection_.set_defer_send_in_response_to_packets(GetParam().ack_response == |
| 1091 | AckResponse::kDefer); |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 1092 | framer_.SetInitialObfuscators(TestConnectionId()); |
| 1093 | connection_.InstallInitialCrypters(TestConnectionId()); |
| 1094 | CrypterPair crypters; |
| 1095 | CryptoUtils::CreateInitialObfuscators(Perspective::IS_SERVER, version(), |
| 1096 | TestConnectionId(), &crypters); |
| 1097 | peer_creator_.SetEncrypter(ENCRYPTION_INITIAL, |
| 1098 | std::move(crypters.encrypter)); |
| 1099 | if (version().KnowsWhichDecrypterToUse()) { |
| 1100 | peer_framer_.InstallDecrypter(ENCRYPTION_INITIAL, |
| 1101 | std::move(crypters.decrypter)); |
| 1102 | } else { |
| 1103 | peer_framer_.SetDecrypter(ENCRYPTION_INITIAL, |
| 1104 | std::move(crypters.decrypter)); |
| 1105 | } |
nharper | 2c9f02a | 2019-05-08 10:25:50 -0700 | [diff] [blame] | 1106 | for (EncryptionLevel level : |
| 1107 | {ENCRYPTION_ZERO_RTT, ENCRYPTION_FORWARD_SECURE}) { |
| 1108 | peer_creator_.SetEncrypter( |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 1109 | level, std::make_unique<NullEncrypter>(peer_framer_.perspective())); |
nharper | 2c9f02a | 2019-05-08 10:25:50 -0700 | [diff] [blame] | 1110 | } |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 1111 | QuicFramerPeer::SetLastSerializedServerConnectionId( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1112 | QuicConnectionPeer::GetFramer(&connection_), connection_id_); |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 1113 | QuicFramerPeer::SetLastWrittenPacketNumberLength( |
| 1114 | QuicConnectionPeer::GetFramer(&connection_), packet_number_length_); |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1115 | if (VersionHasIetfInvariantHeader(version().transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1116 | EXPECT_TRUE(QuicConnectionPeer::GetNoStopWaitingFrames(&connection_)); |
| 1117 | } else { |
| 1118 | QuicConnectionPeer::SetNoStopWaitingFrames(&connection_, |
| 1119 | GetParam().no_stop_waiting); |
| 1120 | } |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1121 | QuicStreamId stream_id; |
| 1122 | if (QuicVersionUsesCryptoFrames(version().transport_version)) { |
| 1123 | stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 1124 | version().transport_version, Perspective::IS_CLIENT); |
| 1125 | } else { |
| 1126 | stream_id = QuicUtils::GetCryptoStreamId(version().transport_version); |
| 1127 | } |
| 1128 | frame1_.stream_id = stream_id; |
| 1129 | frame2_.stream_id = stream_id; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1130 | connection_.set_visitor(&visitor_); |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 1131 | connection_.SetSessionNotifier(¬ifier_); |
| 1132 | connection_.set_notifier(¬ifier_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1133 | connection_.SetSendAlgorithm(send_algorithm_); |
| 1134 | connection_.SetLossAlgorithm(loss_algorithm_.get()); |
| 1135 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 1136 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1137 | .Times(AnyNumber()); |
wub | f4ab965 | 2020-02-20 14:45:43 -0800 | [diff] [blame] | 1138 | EXPECT_CALL(*send_algorithm_, OnPacketNeutered(_)).Times(AnyNumber()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1139 | EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) |
| 1140 | .WillRepeatedly(Return(kDefaultTCPMSS)); |
| 1141 | EXPECT_CALL(*send_algorithm_, PacingRate(_)) |
| 1142 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1143 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) |
| 1144 | .Times(AnyNumber()) |
| 1145 | .WillRepeatedly(Return(QuicBandwidth::Zero())); |
wub | 5cd4959 | 2019-11-25 15:17:13 -0800 | [diff] [blame] | 1146 | EXPECT_CALL(*send_algorithm_, PopulateConnectionStats(_)) |
| 1147 | .Times(AnyNumber()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1148 | EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber()); |
| 1149 | EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber()); |
| 1150 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(AnyNumber()); |
| 1151 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).Times(AnyNumber()); |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 1152 | EXPECT_CALL(visitor_, OnPacketDecrypted(_)).Times(AnyNumber()); |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 1153 | EXPECT_CALL(visitor_, OnCanWrite()) |
| 1154 | .WillRepeatedly(Invoke(¬ifier_, &SimpleSessionNotifier::OnCanWrite)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1155 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 1156 | .WillRepeatedly(Return(false)); |
| 1157 | EXPECT_CALL(visitor_, OnCongestionWindowChange(_)).Times(AnyNumber()); |
zhongyi | 83161e4 | 2019-08-19 09:06:25 -0700 | [diff] [blame] | 1158 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(AnyNumber()); |
wub | 256b2d6 | 2019-11-25 08:46:55 -0800 | [diff] [blame] | 1159 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)).Times(AnyNumber()); |
fayang | 2f2915d | 2020-01-24 06:47:15 -0800 | [diff] [blame] | 1160 | EXPECT_CALL(visitor_, OnOneRttPacketAcknowledged()) |
| 1161 | .Times(testing::AtMost(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1162 | EXPECT_CALL(*loss_algorithm_, GetLossTimeout()) |
| 1163 | .WillRepeatedly(Return(QuicTime::Zero())); |
| 1164 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 1165 | .Times(AnyNumber()); |
fayang | 5014e92 | 2020-01-22 12:28:11 -0800 | [diff] [blame] | 1166 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 1167 | .WillRepeatedly(Return(HANDSHAKE_START)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1168 | if (connection_.version().KnowsWhichDecrypterToUse()) { |
| 1169 | connection_.InstallDecrypter( |
| 1170 | ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 1171 | std::make_unique<NullDecrypter>(Perspective::IS_CLIENT)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1172 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | QuicConnectionTest(const QuicConnectionTest&) = delete; |
| 1176 | QuicConnectionTest& operator=(const QuicConnectionTest&) = delete; |
| 1177 | |
| 1178 | ParsedQuicVersion version() { return GetParam().version; } |
| 1179 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1180 | QuicStopWaitingFrame* stop_waiting() { |
| 1181 | QuicConnectionPeer::PopulateStopWaitingFrame(&connection_, &stop_waiting_); |
| 1182 | return &stop_waiting_; |
| 1183 | } |
| 1184 | |
| 1185 | QuicPacketNumber least_unacked() { |
| 1186 | if (writer_->stop_waiting_frames().empty()) { |
| 1187 | return QuicPacketNumber(); |
| 1188 | } |
| 1189 | return writer_->stop_waiting_frames()[0].least_unacked; |
| 1190 | } |
| 1191 | |
| 1192 | void use_tagging_decrypter() { writer_->use_tagging_decrypter(); } |
| 1193 | |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1194 | void SetDecrypter(EncryptionLevel level, |
| 1195 | std::unique_ptr<QuicDecrypter> decrypter) { |
| 1196 | if (connection_.version().KnowsWhichDecrypterToUse()) { |
| 1197 | connection_.InstallDecrypter(level, std::move(decrypter)); |
| 1198 | connection_.RemoveDecrypter(ENCRYPTION_INITIAL); |
| 1199 | } else { |
| 1200 | connection_.SetDecrypter(level, std::move(decrypter)); |
| 1201 | } |
| 1202 | } |
| 1203 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1204 | void ProcessPacket(uint64_t number) { |
| 1205 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 1206 | ProcessDataPacket(number); |
| 1207 | if (connection_.GetSendAlarm()->IsSet()) { |
| 1208 | connection_.GetSendAlarm()->Fire(); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | void ProcessReceivedPacket(const QuicSocketAddress& self_address, |
| 1213 | const QuicSocketAddress& peer_address, |
| 1214 | const QuicReceivedPacket& packet) { |
| 1215 | connection_.ProcessUdpPacket(self_address, peer_address, packet); |
| 1216 | if (connection_.GetSendAlarm()->IsSet()) { |
| 1217 | connection_.GetSendAlarm()->Fire(); |
| 1218 | } |
| 1219 | } |
| 1220 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1221 | QuicFrame MakeCryptoFrame() const { |
| 1222 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 1223 | return QuicFrame(new QuicCryptoFrame(crypto_frame_)); |
| 1224 | } |
| 1225 | return QuicFrame(QuicStreamFrame( |
| 1226 | QuicUtils::GetCryptoStreamId(connection_.transport_version()), false, |
| 1227 | 0u, quiche::QuicheStringPiece())); |
| 1228 | } |
| 1229 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1230 | void ProcessFramePacket(QuicFrame frame) { |
| 1231 | ProcessFramePacketWithAddresses(frame, kSelfAddress, kPeerAddress); |
| 1232 | } |
| 1233 | |
| 1234 | void ProcessFramePacketWithAddresses(QuicFrame frame, |
| 1235 | QuicSocketAddress self_address, |
| 1236 | QuicSocketAddress peer_address) { |
| 1237 | QuicFrames frames; |
| 1238 | frames.push_back(QuicFrame(frame)); |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 1239 | return ProcessFramesPacketWithAddresses(frames, self_address, peer_address); |
| 1240 | } |
| 1241 | |
| 1242 | void ProcessFramesPacketWithAddresses(QuicFrames frames, |
| 1243 | QuicSocketAddress self_address, |
| 1244 | QuicSocketAddress peer_address) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1245 | QuicPacketCreatorPeer::SetSendVersionInPacket( |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 1246 | &peer_creator_, |
| 1247 | QuicPacketCreatorPeer::GetEncryptionLevel(&peer_creator_) < |
| 1248 | ENCRYPTION_FORWARD_SECURE && |
| 1249 | connection_.perspective() == Perspective::IS_SERVER); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1250 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1251 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1252 | SerializedPacket serialized_packet = |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1253 | QuicPacketCreatorPeer::SerializeAllFrames( |
| 1254 | &peer_creator_, frames, buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1255 | connection_.ProcessUdpPacket( |
| 1256 | self_address, peer_address, |
| 1257 | QuicReceivedPacket(serialized_packet.encrypted_buffer, |
| 1258 | serialized_packet.encrypted_length, clock_.Now())); |
| 1259 | if (connection_.GetSendAlarm()->IsSet()) { |
| 1260 | connection_.GetSendAlarm()->Fire(); |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | // Bypassing the packet creator is unrealistic, but allows us to process |
| 1265 | // packets the QuicPacketCreator won't allow us to create. |
| 1266 | void ForceProcessFramePacket(QuicFrame frame) { |
| 1267 | QuicFrames frames; |
| 1268 | frames.push_back(QuicFrame(frame)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1269 | bool send_version = connection_.perspective() == Perspective::IS_SERVER; |
| 1270 | if (connection_.version().KnowsWhichDecrypterToUse()) { |
| 1271 | send_version = true; |
| 1272 | } |
| 1273 | QuicPacketCreatorPeer::SetSendVersionInPacket(&peer_creator_, send_version); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1274 | QuicPacketHeader header; |
| 1275 | QuicPacketCreatorPeer::FillPacketHeader(&peer_creator_, &header); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1276 | char encrypted_buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1277 | size_t length = peer_framer_.BuildDataPacket( |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1278 | header, frames, encrypted_buffer, kMaxOutgoingPacketSize, |
| 1279 | ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1280 | DCHECK_GT(length, 0u); |
| 1281 | |
| 1282 | const size_t encrypted_length = peer_framer_.EncryptInPlace( |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 1283 | ENCRYPTION_INITIAL, header.packet_number, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1284 | GetStartOfEncryptedData(peer_framer_.version().transport_version, |
| 1285 | header), |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1286 | length, kMaxOutgoingPacketSize, encrypted_buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1287 | DCHECK_GT(encrypted_length, 0u); |
| 1288 | |
| 1289 | connection_.ProcessUdpPacket( |
| 1290 | kSelfAddress, kPeerAddress, |
| 1291 | QuicReceivedPacket(encrypted_buffer, encrypted_length, clock_.Now())); |
| 1292 | } |
| 1293 | |
| 1294 | size_t ProcessFramePacketAtLevel(uint64_t number, |
| 1295 | QuicFrame frame, |
| 1296 | EncryptionLevel level) { |
fayang | 0106294 | 2020-01-22 07:23:23 -0800 | [diff] [blame] | 1297 | QuicFrames frames; |
| 1298 | frames.push_back(frame); |
| 1299 | return ProcessFramesPacketAtLevel(number, frames, level); |
| 1300 | } |
| 1301 | |
| 1302 | size_t ProcessFramesPacketAtLevel(uint64_t number, |
| 1303 | const QuicFrames& frames, |
| 1304 | EncryptionLevel level) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1305 | QuicPacketHeader header; |
| 1306 | header.destination_connection_id = connection_id_; |
| 1307 | header.packet_number_length = packet_number_length_; |
| 1308 | header.destination_connection_id_included = connection_id_included_; |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 1309 | if (peer_framer_.perspective() == Perspective::IS_SERVER) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1310 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
| 1311 | } |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1312 | if (level == ENCRYPTION_INITIAL && |
| 1313 | peer_framer_.version().KnowsWhichDecrypterToUse()) { |
| 1314 | header.version_flag = true; |
nharper | d43f1d6 | 2019-07-01 15:18:20 -0700 | [diff] [blame] | 1315 | if (QuicVersionHasLongHeaderLengths(peer_framer_.transport_version())) { |
| 1316 | header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1; |
| 1317 | header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2; |
| 1318 | } |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1319 | } |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 1320 | if (header.version_flag && |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1321 | peer_framer_.perspective() == Perspective::IS_SERVER) { |
| 1322 | header.source_connection_id = connection_id_; |
| 1323 | header.source_connection_id_included = CONNECTION_ID_PRESENT; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1324 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1325 | header.packet_number = QuicPacketNumber(number); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1326 | std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 1327 | // Set the correct encryption level and encrypter on peer_creator and |
| 1328 | // peer_framer, respectively. |
| 1329 | peer_creator_.set_encryption_level(level); |
| 1330 | if (QuicPacketCreatorPeer::GetEncryptionLevel(&peer_creator_) > |
| 1331 | ENCRYPTION_INITIAL) { |
| 1332 | peer_framer_.SetEncrypter( |
| 1333 | QuicPacketCreatorPeer::GetEncryptionLevel(&peer_creator_), |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 1334 | std::make_unique<TaggingEncrypter>(0x01)); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 1335 | // Set the corresponding decrypter. |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1336 | if (connection_.version().KnowsWhichDecrypterToUse()) { |
| 1337 | connection_.InstallDecrypter( |
| 1338 | QuicPacketCreatorPeer::GetEncryptionLevel(&peer_creator_), |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 1339 | std::make_unique<StrictTaggingDecrypter>(0x01)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1340 | connection_.RemoveDecrypter(ENCRYPTION_INITIAL); |
| 1341 | } else { |
| 1342 | connection_.SetDecrypter( |
| 1343 | QuicPacketCreatorPeer::GetEncryptionLevel(&peer_creator_), |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 1344 | std::make_unique<StrictTaggingDecrypter>(0x01)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1345 | } |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 1346 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1347 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1348 | char buffer[kMaxOutgoingPacketSize]; |
| 1349 | size_t encrypted_length = |
| 1350 | peer_framer_.EncryptPayload(level, QuicPacketNumber(number), *packet, |
| 1351 | buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1352 | connection_.ProcessUdpPacket( |
| 1353 | kSelfAddress, kPeerAddress, |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 1354 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 1355 | if (connection_.GetSendAlarm()->IsSet()) { |
| 1356 | connection_.GetSendAlarm()->Fire(); |
| 1357 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1358 | return encrypted_length; |
| 1359 | } |
| 1360 | |
| 1361 | size_t ProcessDataPacket(uint64_t number) { |
nharper | 2c9f02a | 2019-05-08 10:25:50 -0700 | [diff] [blame] | 1362 | return ProcessDataPacketAtLevel(number, false, ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | size_t ProcessDataPacket(QuicPacketNumber packet_number) { |
nharper | 2c9f02a | 2019-05-08 10:25:50 -0700 | [diff] [blame] | 1366 | return ProcessDataPacketAtLevel(packet_number, false, |
| 1367 | ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | size_t ProcessDataPacketAtLevel(QuicPacketNumber packet_number, |
| 1371 | bool has_stop_waiting, |
| 1372 | EncryptionLevel level) { |
| 1373 | return ProcessDataPacketAtLevel(packet_number.ToUint64(), has_stop_waiting, |
| 1374 | level); |
| 1375 | } |
| 1376 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 1377 | size_t ProcessCryptoPacketAtLevel(uint64_t number, |
| 1378 | EncryptionLevel /*level*/) { |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 1379 | QuicPacketHeader header = ConstructPacketHeader(number, ENCRYPTION_INITIAL); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1380 | QuicFrames frames; |
| 1381 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 1382 | frames.push_back(QuicFrame(&crypto_frame_)); |
| 1383 | } else { |
| 1384 | frames.push_back(QuicFrame(frame1_)); |
| 1385 | } |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 1386 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1387 | std::unique_ptr<QuicPacket> packet = ConstructPacket(header, frames); |
| 1388 | char buffer[kMaxOutgoingPacketSize]; |
| 1389 | peer_creator_.set_encryption_level(ENCRYPTION_INITIAL); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 1390 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 1391 | ENCRYPTION_INITIAL, QuicPacketNumber(number), *packet, buffer, |
| 1392 | kMaxOutgoingPacketSize); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1393 | connection_.ProcessUdpPacket( |
| 1394 | kSelfAddress, kPeerAddress, |
| 1395 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 1396 | if (connection_.GetSendAlarm()->IsSet()) { |
| 1397 | connection_.GetSendAlarm()->Fire(); |
| 1398 | } |
| 1399 | return encrypted_length; |
| 1400 | } |
| 1401 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1402 | size_t ProcessDataPacketAtLevel(uint64_t number, |
| 1403 | bool has_stop_waiting, |
| 1404 | EncryptionLevel level) { |
| 1405 | std::unique_ptr<QuicPacket> packet( |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 1406 | ConstructDataPacket(number, has_stop_waiting, level)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1407 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 1408 | peer_creator_.set_encryption_level(level); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1409 | size_t encrypted_length = |
| 1410 | peer_framer_.EncryptPayload(level, QuicPacketNumber(number), *packet, |
| 1411 | buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1412 | connection_.ProcessUdpPacket( |
| 1413 | kSelfAddress, kPeerAddress, |
| 1414 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 1415 | if (connection_.GetSendAlarm()->IsSet()) { |
| 1416 | connection_.GetSendAlarm()->Fire(); |
| 1417 | } |
| 1418 | return encrypted_length; |
| 1419 | } |
| 1420 | |
| 1421 | void ProcessClosePacket(uint64_t number) { |
| 1422 | std::unique_ptr<QuicPacket> packet(ConstructClosePacket(number)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1423 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 1424 | size_t encrypted_length = peer_framer_.EncryptPayload( |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 1425 | ENCRYPTION_FORWARD_SECURE, QuicPacketNumber(number), *packet, buffer, |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1426 | kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1427 | connection_.ProcessUdpPacket( |
| 1428 | kSelfAddress, kPeerAddress, |
| 1429 | QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); |
| 1430 | } |
| 1431 | |
| 1432 | QuicByteCount SendStreamDataToPeer(QuicStreamId id, |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 1433 | quiche::QuicheStringPiece data, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1434 | QuicStreamOffset offset, |
| 1435 | StreamSendingState state, |
| 1436 | QuicPacketNumber* last_packet) { |
| 1437 | QuicByteCount packet_size; |
fayang | 0fcbf35 | 2019-08-30 11:15:58 -0700 | [diff] [blame] | 1438 | // Save the last packet's size. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1439 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
fayang | 0fcbf35 | 2019-08-30 11:15:58 -0700 | [diff] [blame] | 1440 | .Times(AnyNumber()) |
| 1441 | .WillRepeatedly(SaveArg<3>(&packet_size)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1442 | connection_.SendStreamDataWithString(id, data, offset, state); |
| 1443 | if (last_packet != nullptr) { |
| 1444 | *last_packet = creator_->packet_number(); |
| 1445 | } |
| 1446 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1447 | .Times(AnyNumber()); |
| 1448 | return packet_size; |
| 1449 | } |
| 1450 | |
| 1451 | void SendAckPacketToPeer() { |
| 1452 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 1453 | { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 1454 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1455 | connection_.SendAck(); |
| 1456 | } |
| 1457 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 1458 | .Times(AnyNumber()); |
| 1459 | } |
| 1460 | |
| 1461 | void SendRstStream(QuicStreamId id, |
| 1462 | QuicRstStreamErrorCode error, |
| 1463 | QuicStreamOffset bytes_written) { |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 1464 | notifier_.WriteOrBufferRstStream(id, error, bytes_written); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1465 | connection_.OnStreamReset(id, error); |
| 1466 | } |
| 1467 | |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 1468 | void SendPing() { notifier_.WriteOrBufferPing(); } |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 1469 | |
fayang | 61453cb | 2020-03-11 11:32:26 -0700 | [diff] [blame] | 1470 | MessageStatus SendMessage(quiche::QuicheStringPiece message) { |
| 1471 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1472 | QuicMemSliceStorage storage(nullptr, 0, nullptr, 0); |
| 1473 | return connection_.SendMessage( |
| 1474 | 1, |
| 1475 | MakeSpan(connection_.helper()->GetStreamSendBufferAllocator(), message, |
| 1476 | &storage), |
| 1477 | false); |
| 1478 | } |
| 1479 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1480 | void ProcessAckPacket(uint64_t packet_number, QuicAckFrame* frame) { |
| 1481 | if (packet_number > 1) { |
| 1482 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, packet_number - 1); |
| 1483 | } else { |
| 1484 | QuicPacketCreatorPeer::ClearPacketNumber(&peer_creator_); |
| 1485 | } |
| 1486 | ProcessFramePacket(QuicFrame(frame)); |
| 1487 | } |
| 1488 | |
| 1489 | void ProcessAckPacket(QuicAckFrame* frame) { |
| 1490 | ProcessFramePacket(QuicFrame(frame)); |
| 1491 | } |
| 1492 | |
| 1493 | void ProcessStopWaitingPacket(QuicStopWaitingFrame frame) { |
| 1494 | ProcessFramePacket(QuicFrame(frame)); |
| 1495 | } |
| 1496 | |
| 1497 | size_t ProcessStopWaitingPacketAtLevel(uint64_t number, |
| 1498 | QuicStopWaitingFrame frame, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 1499 | EncryptionLevel /*level*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1500 | return ProcessFramePacketAtLevel(number, QuicFrame(frame), |
| 1501 | ENCRYPTION_ZERO_RTT); |
| 1502 | } |
| 1503 | |
| 1504 | void ProcessGoAwayPacket(QuicGoAwayFrame* frame) { |
| 1505 | ProcessFramePacket(QuicFrame(frame)); |
| 1506 | } |
| 1507 | |
| 1508 | bool IsMissing(uint64_t number) { |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 1509 | return IsAwaitingPacket(connection_.ack_frame(), QuicPacketNumber(number), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1510 | QuicPacketNumber()); |
| 1511 | } |
| 1512 | |
| 1513 | std::unique_ptr<QuicPacket> ConstructPacket(const QuicPacketHeader& header, |
| 1514 | const QuicFrames& frames) { |
| 1515 | auto packet = BuildUnsizedDataPacket(&peer_framer_, header, frames); |
| 1516 | EXPECT_NE(nullptr, packet.get()); |
| 1517 | return packet; |
| 1518 | } |
| 1519 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1520 | QuicPacketHeader ConstructPacketHeader(uint64_t number, |
| 1521 | EncryptionLevel level) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1522 | QuicPacketHeader header; |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1523 | if (VersionHasIetfInvariantHeader(peer_framer_.transport_version()) && |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 1524 | level < ENCRYPTION_FORWARD_SECURE) { |
| 1525 | // Set long header type accordingly. |
| 1526 | header.version_flag = true; |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 1527 | header.form = IETF_QUIC_LONG_HEADER_PACKET; |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 1528 | header.long_packet_type = EncryptionlevelToLongHeaderType(level); |
| 1529 | if (QuicVersionHasLongHeaderLengths( |
| 1530 | peer_framer_.version().transport_version)) { |
| 1531 | header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2; |
| 1532 | if (header.long_packet_type == INITIAL) { |
| 1533 | header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1; |
| 1534 | } |
| 1535 | } |
| 1536 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1537 | // Set connection_id to peer's in memory representation as this data packet |
| 1538 | // is created by peer_framer. |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 1539 | if (peer_framer_.perspective() == Perspective::IS_SERVER) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1540 | header.source_connection_id = connection_id_; |
| 1541 | header.source_connection_id_included = connection_id_included_; |
| 1542 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
| 1543 | } else { |
| 1544 | header.destination_connection_id = connection_id_; |
| 1545 | header.destination_connection_id_included = connection_id_included_; |
| 1546 | } |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1547 | if (VersionHasIetfInvariantHeader(peer_framer_.transport_version()) && |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1548 | peer_framer_.perspective() == Perspective::IS_SERVER) { |
| 1549 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 1550 | if (header.version_flag) { |
| 1551 | header.source_connection_id = connection_id_; |
| 1552 | header.source_connection_id_included = CONNECTION_ID_PRESENT; |
| 1553 | if (GetParam().version.handshake_protocol == PROTOCOL_QUIC_CRYPTO && |
| 1554 | header.long_packet_type == ZERO_RTT_PROTECTED) { |
QUICHE team | 548d51b | 2019-03-14 10:06:54 -0700 | [diff] [blame] | 1555 | header.nonce = &kTestDiversificationNonce; |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 1556 | } |
| 1557 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1558 | } |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1559 | header.packet_number_length = packet_number_length_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1560 | header.packet_number = QuicPacketNumber(number); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1561 | return header; |
| 1562 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1563 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1564 | std::unique_ptr<QuicPacket> ConstructDataPacket(uint64_t number, |
| 1565 | bool has_stop_waiting, |
| 1566 | EncryptionLevel level) { |
| 1567 | QuicPacketHeader header = ConstructPacketHeader(number, level); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1568 | QuicFrames frames; |
| 1569 | frames.push_back(QuicFrame(frame1_)); |
| 1570 | if (has_stop_waiting) { |
| 1571 | frames.push_back(QuicFrame(stop_waiting_)); |
| 1572 | } |
| 1573 | return ConstructPacket(header, frames); |
| 1574 | } |
| 1575 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1576 | std::unique_ptr<SerializedPacket> ConstructProbingPacket() { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 1577 | if (VersionHasIetfQuicFrames(version().transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1578 | QuicPathFrameBuffer payload = { |
| 1579 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe}}; |
| 1580 | return QuicPacketCreatorPeer:: |
| 1581 | SerializePathChallengeConnectivityProbingPacket(&peer_creator_, |
| 1582 | &payload); |
| 1583 | } |
| 1584 | return QuicPacketCreatorPeer::SerializeConnectivityProbingPacket( |
| 1585 | &peer_creator_); |
| 1586 | } |
| 1587 | |
| 1588 | std::unique_ptr<QuicPacket> ConstructClosePacket(uint64_t number) { |
| 1589 | QuicPacketHeader header; |
| 1590 | // Set connection_id to peer's in memory representation as this connection |
| 1591 | // close packet is created by peer_framer. |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 1592 | if (peer_framer_.perspective() == Perspective::IS_SERVER) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1593 | header.source_connection_id = connection_id_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1594 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1595 | if (!VersionHasIetfInvariantHeader(peer_framer_.transport_version())) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1596 | header.source_connection_id_included = CONNECTION_ID_PRESENT; |
| 1597 | } |
| 1598 | } else { |
| 1599 | header.destination_connection_id = connection_id_; |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1600 | if (VersionHasIetfInvariantHeader(peer_framer_.transport_version())) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1601 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
| 1602 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1603 | } |
| 1604 | |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1605 | header.packet_number = QuicPacketNumber(number); |
| 1606 | |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 1607 | QuicErrorCode kQuicErrorCode = QUIC_PEER_GOING_AWAY; |
fkastenholz | 591814c | 2019-09-06 12:11:46 -0700 | [diff] [blame] | 1608 | QuicConnectionCloseFrame qccf(peer_framer_.transport_version(), |
| 1609 | kQuicErrorCode, "", |
| 1610 | /*transport_close_frame_type=*/0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1611 | QuicFrames frames; |
| 1612 | frames.push_back(QuicFrame(&qccf)); |
| 1613 | return ConstructPacket(header, frames); |
| 1614 | } |
| 1615 | |
| 1616 | QuicTime::Delta DefaultRetransmissionTime() { |
| 1617 | return QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs); |
| 1618 | } |
| 1619 | |
| 1620 | QuicTime::Delta DefaultDelayedAckTime() { |
| 1621 | return QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs); |
| 1622 | } |
| 1623 | |
| 1624 | const QuicStopWaitingFrame InitStopWaitingFrame(uint64_t least_unacked) { |
| 1625 | QuicStopWaitingFrame frame; |
| 1626 | frame.least_unacked = QuicPacketNumber(least_unacked); |
| 1627 | return frame; |
| 1628 | } |
| 1629 | |
| 1630 | // Construct a ack_frame that acks all packet numbers between 1 and |
| 1631 | // |largest_acked|, except |missing|. |
| 1632 | // REQUIRES: 1 <= |missing| < |largest_acked| |
| 1633 | QuicAckFrame ConstructAckFrame(uint64_t largest_acked, uint64_t missing) { |
| 1634 | return ConstructAckFrame(QuicPacketNumber(largest_acked), |
| 1635 | QuicPacketNumber(missing)); |
| 1636 | } |
| 1637 | |
| 1638 | QuicAckFrame ConstructAckFrame(QuicPacketNumber largest_acked, |
| 1639 | QuicPacketNumber missing) { |
| 1640 | if (missing == QuicPacketNumber(1)) { |
| 1641 | return InitAckFrame({{missing + 1, largest_acked + 1}}); |
| 1642 | } |
| 1643 | return InitAckFrame( |
| 1644 | {{QuicPacketNumber(1), missing}, {missing + 1, largest_acked + 1}}); |
| 1645 | } |
| 1646 | |
| 1647 | // Undo nacking a packet within the frame. |
| 1648 | void AckPacket(QuicPacketNumber arrived, QuicAckFrame* frame) { |
| 1649 | EXPECT_FALSE(frame->packets.Contains(arrived)); |
| 1650 | frame->packets.Add(arrived); |
| 1651 | } |
| 1652 | |
| 1653 | void TriggerConnectionClose() { |
| 1654 | // Send an erroneous packet to close the connection. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 1655 | EXPECT_CALL(visitor_, |
| 1656 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 1657 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 1658 | |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 1659 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 1660 | // Triggers a connection by receiving ACK of unsent packet. |
| 1661 | QuicAckFrame frame = InitAckFrame(10000); |
| 1662 | ProcessAckPacket(1, &frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1663 | EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) == |
| 1664 | nullptr); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 1665 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 1666 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 1667 | IsError(QUIC_INVALID_ACK_DATA)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | void BlockOnNextWrite() { |
| 1671 | writer_->BlockOnNextWrite(); |
| 1672 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); |
| 1673 | } |
| 1674 | |
| 1675 | void SimulateNextPacketTooLarge() { writer_->SimulateNextPacketTooLarge(); } |
| 1676 | |
| 1677 | void AlwaysGetPacketTooLarge() { writer_->AlwaysGetPacketTooLarge(); } |
| 1678 | |
| 1679 | void SetWritePauseTimeDelta(QuicTime::Delta delta) { |
| 1680 | writer_->SetWritePauseTimeDelta(delta); |
| 1681 | } |
| 1682 | |
| 1683 | void CongestionBlockWrites() { |
| 1684 | EXPECT_CALL(*send_algorithm_, CanSend(_)) |
| 1685 | .WillRepeatedly(testing::Return(false)); |
| 1686 | } |
| 1687 | |
| 1688 | void CongestionUnblockWrites() { |
| 1689 | EXPECT_CALL(*send_algorithm_, CanSend(_)) |
| 1690 | .WillRepeatedly(testing::Return(true)); |
| 1691 | } |
| 1692 | |
| 1693 | void set_perspective(Perspective perspective) { |
| 1694 | connection_.set_perspective(perspective); |
| 1695 | if (perspective == Perspective::IS_SERVER) { |
| 1696 | connection_.set_can_truncate_connection_ids(true); |
wub | 256b2d6 | 2019-11-25 08:46:55 -0800 | [diff] [blame] | 1697 | QuicConnectionPeer::SetNegotiatedVersion(&connection_); |
wub | bea386e | 2020-01-27 09:04:51 -0800 | [diff] [blame] | 1698 | connection_.OnSuccessfulVersionNegotiation(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1699 | } |
| 1700 | QuicFramerPeer::SetPerspective(&peer_framer_, |
nharper | 4eba09b | 2019-06-26 20:17:25 -0700 | [diff] [blame] | 1701 | QuicUtils::InvertPerspective(perspective)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | void set_packets_between_probes_base( |
| 1705 | const QuicPacketCount packets_between_probes_base) { |
wub | 173916e | 2019-11-27 14:36:24 -0800 | [diff] [blame] | 1706 | QuicConnectionPeer::ReInitializeMtuDiscoverer( |
| 1707 | &connection_, packets_between_probes_base, |
| 1708 | QuicPacketNumber(packets_between_probes_base)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | bool IsDefaultTestConfiguration() { |
| 1712 | TestParams p = GetParam(); |
| 1713 | return p.ack_response == AckResponse::kImmediate && |
| 1714 | p.version == AllSupportedVersions()[0] && p.no_stop_waiting; |
| 1715 | } |
| 1716 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 1717 | void TestConnectionCloseQuicErrorCode(QuicErrorCode expected_code) { |
| 1718 | // Not strictly needed for this test, but is commonly done. |
| 1719 | EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) == |
| 1720 | nullptr); |
| 1721 | const std::vector<QuicConnectionCloseFrame>& connection_close_frames = |
| 1722 | writer_->connection_close_frames(); |
| 1723 | ASSERT_EQ(1u, connection_close_frames.size()); |
bnc | 77e77b8 | 2020-04-05 10:36:49 -0700 | [diff] [blame] | 1724 | |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 1725 | EXPECT_THAT(connection_close_frames[0].quic_error_code, |
| 1726 | IsError(expected_code)); |
bnc | 77e77b8 | 2020-04-05 10:36:49 -0700 | [diff] [blame] | 1727 | |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 1728 | if (!VersionHasIetfQuicFrames(version().transport_version)) { |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 1729 | EXPECT_THAT(connection_close_frames[0].wire_error_code, |
| 1730 | IsError(expected_code)); |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 1731 | EXPECT_EQ(GOOGLE_QUIC_CONNECTION_CLOSE, |
| 1732 | connection_close_frames[0].close_type); |
| 1733 | return; |
| 1734 | } |
| 1735 | |
| 1736 | QuicErrorCodeToIetfMapping mapping = |
| 1737 | QuicErrorCodeToTransportErrorCode(expected_code); |
| 1738 | |
bnc | 0054cc6 | 2020-04-09 18:22:57 -0700 | [diff] [blame] | 1739 | if (mapping.is_transport_close) { |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 1740 | // This Google QUIC Error Code maps to a transport close, |
| 1741 | EXPECT_EQ(IETF_QUIC_TRANSPORT_CONNECTION_CLOSE, |
| 1742 | connection_close_frames[0].close_type); |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 1743 | } else { |
| 1744 | // This maps to an application close. |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 1745 | EXPECT_EQ(IETF_QUIC_APPLICATION_CONNECTION_CLOSE, |
| 1746 | connection_close_frames[0].close_type); |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 1747 | } |
bnc | 0054cc6 | 2020-04-09 18:22:57 -0700 | [diff] [blame] | 1748 | EXPECT_EQ(mapping.error_code, connection_close_frames[0].wire_error_code); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 1751 | void MtuDiscoveryTestInit() { |
| 1752 | set_perspective(Perspective::IS_SERVER); |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 1753 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
fayang | 6a25841 | 2020-05-28 08:57:12 -0700 | [diff] [blame] | 1754 | if (version().SupportsAntiAmplificationLimit()) { |
| 1755 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 1756 | } |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 1757 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1758 | peer_creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 1759 | // QuicFramer::GetMaxPlaintextSize uses the smallest max plaintext size |
| 1760 | // across all encrypters. The initial encrypter used with IETF QUIC has a |
| 1761 | // 16-byte overhead, while the NullEncrypter used throughout this test has a |
| 1762 | // 12-byte overhead. This test tests behavior that relies on computing the |
| 1763 | // packet size correctly, so by unsetting the initial encrypter, we avoid |
| 1764 | // having a mismatch between the overheads for the encrypters used. In |
| 1765 | // non-test scenarios all encrypters used for a given connection have the |
| 1766 | // same overhead, either 12 bytes for ones using Google QUIC crypto, or 16 |
| 1767 | // bytes for ones using TLS. |
| 1768 | connection_.SetEncrypter(ENCRYPTION_INITIAL, nullptr); |
wub | ecb643f | 2020-03-19 08:58:46 -0700 | [diff] [blame] | 1769 | // Prevent packets from being coalesced. |
| 1770 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 1771 | .WillRepeatedly(Return(HANDSHAKE_CONFIRMED)); |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 1772 | EXPECT_TRUE(connection_.connected()); |
| 1773 | } |
| 1774 | |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 1775 | void TestClientRetryHandling(bool invalid_retry_tag, |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 1776 | bool missing_original_id_in_config, |
| 1777 | bool wrong_original_id_in_config, |
| 1778 | bool missing_retry_id_in_config, |
| 1779 | bool wrong_retry_id_in_config); |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 1780 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1781 | QuicConnectionId connection_id_; |
| 1782 | QuicFramer framer_; |
| 1783 | |
| 1784 | MockSendAlgorithm* send_algorithm_; |
| 1785 | std::unique_ptr<MockLossAlgorithm> loss_algorithm_; |
| 1786 | MockClock clock_; |
| 1787 | MockRandom random_generator_; |
| 1788 | SimpleBufferAllocator buffer_allocator_; |
| 1789 | std::unique_ptr<TestConnectionHelper> helper_; |
| 1790 | std::unique_ptr<TestAlarmFactory> alarm_factory_; |
| 1791 | QuicFramer peer_framer_; |
| 1792 | QuicPacketCreator peer_creator_; |
| 1793 | std::unique_ptr<TestPacketWriter> writer_; |
| 1794 | TestConnection connection_; |
| 1795 | QuicPacketCreator* creator_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1796 | QuicSentPacketManager* manager_; |
| 1797 | StrictMock<MockQuicConnectionVisitor> visitor_; |
| 1798 | |
| 1799 | QuicStreamFrame frame1_; |
| 1800 | QuicStreamFrame frame2_; |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1801 | QuicCryptoFrame crypto_frame_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1802 | QuicAckFrame ack_; |
| 1803 | QuicStopWaitingFrame stop_waiting_; |
| 1804 | QuicPacketNumberLength packet_number_length_; |
| 1805 | QuicConnectionIdIncluded connection_id_included_; |
| 1806 | |
| 1807 | SimpleSessionNotifier notifier_; |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 1808 | |
| 1809 | QuicConnectionCloseFrame saved_connection_close_frame_; |
| 1810 | int connection_close_frame_count_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1811 | }; |
| 1812 | |
| 1813 | // Run all end to end tests with all supported versions. |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 1814 | INSTANTIATE_TEST_SUITE_P(QuicConnectionTests, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1815 | QuicConnectionTest, |
dschinazi | 142051a | 2019-09-18 18:17:29 -0700 | [diff] [blame] | 1816 | ::testing::ValuesIn(GetTestParams()), |
| 1817 | ::testing::PrintToStringParamName()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1818 | |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 1819 | // These two tests ensure that the QuicErrorCode mapping works correctly. |
| 1820 | // Both tests expect to see a Google QUIC close if not running IETF QUIC. |
| 1821 | // If running IETF QUIC, the first will generate a transport connection |
| 1822 | // close, the second an application connection close. |
| 1823 | // The connection close codes for the two tests are manually chosen; |
| 1824 | // they are expected to always map to transport- and application- |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 1825 | // closes, respectively. If that changes, new codes should be chosen. |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 1826 | TEST_P(QuicConnectionTest, CloseErrorCodeTestTransport) { |
| 1827 | EXPECT_TRUE(connection_.connected()); |
| 1828 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 1829 | connection_.CloseConnection( |
| 1830 | IETF_QUIC_PROTOCOL_VIOLATION, "Should be transport close", |
| 1831 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 1832 | EXPECT_FALSE(connection_.connected()); |
| 1833 | TestConnectionCloseQuicErrorCode(IETF_QUIC_PROTOCOL_VIOLATION); |
| 1834 | } |
| 1835 | |
| 1836 | // Test that the IETF QUIC Error code mapping function works |
| 1837 | // properly for application connection close codes. |
| 1838 | TEST_P(QuicConnectionTest, CloseErrorCodeTestApplication) { |
| 1839 | EXPECT_TRUE(connection_.connected()); |
| 1840 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 1841 | connection_.CloseConnection( |
| 1842 | QUIC_HEADERS_STREAM_DATA_DECOMPRESS_FAILURE, |
| 1843 | "Should be application close", |
| 1844 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 1845 | EXPECT_FALSE(connection_.connected()); |
| 1846 | TestConnectionCloseQuicErrorCode(QUIC_HEADERS_STREAM_DATA_DECOMPRESS_FAILURE); |
| 1847 | } |
| 1848 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1849 | TEST_P(QuicConnectionTest, SelfAddressChangeAtClient) { |
| 1850 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 1851 | |
| 1852 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 1853 | EXPECT_TRUE(connection_.connected()); |
| 1854 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1855 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1856 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 1857 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1858 | EXPECT_CALL(visitor_, OnStreamFrame(_)); |
| 1859 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1860 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 1861 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1862 | // Cause change in self_address. |
| 1863 | QuicIpAddress host; |
| 1864 | host.FromString("1.1.1.1"); |
| 1865 | QuicSocketAddress self_address(host, 123); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1866 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 1867 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 1868 | } else { |
| 1869 | EXPECT_CALL(visitor_, OnStreamFrame(_)); |
| 1870 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1871 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address, |
| 1872 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1873 | EXPECT_TRUE(connection_.connected()); |
| 1874 | } |
| 1875 | |
| 1876 | TEST_P(QuicConnectionTest, SelfAddressChangeAtServer) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1877 | set_perspective(Perspective::IS_SERVER); |
| 1878 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1879 | |
| 1880 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 1881 | EXPECT_TRUE(connection_.connected()); |
| 1882 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1883 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1884 | EXPECT_CALL(visitor_, OnCryptoFrame(_)); |
| 1885 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1886 | EXPECT_CALL(visitor_, OnStreamFrame(_)); |
| 1887 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1888 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 1889 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1890 | // Cause change in self_address. |
| 1891 | QuicIpAddress host; |
| 1892 | host.FromString("1.1.1.1"); |
| 1893 | QuicSocketAddress self_address(host, 123); |
| 1894 | EXPECT_CALL(visitor_, AllowSelfAddressChange()).WillOnce(Return(false)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 1895 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1896 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address, |
| 1897 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1898 | EXPECT_FALSE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 1899 | TestConnectionCloseQuicErrorCode(QUIC_ERROR_MIGRATING_ADDRESS); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | TEST_P(QuicConnectionTest, AllowSelfAddressChangeToMappedIpv4AddressAtServer) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1903 | set_perspective(Perspective::IS_SERVER); |
| 1904 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1905 | |
| 1906 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 1907 | EXPECT_TRUE(connection_.connected()); |
| 1908 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1909 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1910 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(3); |
| 1911 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1912 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(3); |
| 1913 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1914 | QuicIpAddress host; |
| 1915 | host.FromString("1.1.1.1"); |
| 1916 | QuicSocketAddress self_address1(host, 443); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1917 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address1, |
| 1918 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1919 | // Cause self_address change to mapped Ipv4 address. |
| 1920 | QuicIpAddress host2; |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 1921 | host2.FromString(quiche::QuicheStrCat( |
| 1922 | "::ffff:", connection_.self_address().host().ToString())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1923 | QuicSocketAddress self_address2(host2, connection_.self_address().port()); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1924 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address2, |
| 1925 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1926 | EXPECT_TRUE(connection_.connected()); |
| 1927 | // self_address change back to Ipv4 address. |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1928 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address1, |
| 1929 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1930 | EXPECT_TRUE(connection_.connected()); |
| 1931 | } |
| 1932 | |
| 1933 | TEST_P(QuicConnectionTest, ClientAddressChangeAndPacketReordered) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1934 | set_perspective(Perspective::IS_SERVER); |
| 1935 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1936 | |
| 1937 | // Clear direct_peer_address. |
| 1938 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 1939 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 1940 | // this test. |
| 1941 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 1942 | QuicSocketAddress()); |
| 1943 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1944 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1945 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 1946 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1947 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 1948 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1949 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 5); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1950 | const QuicSocketAddress kNewPeerAddress = |
| 1951 | QuicSocketAddress(QuicIpAddress::Loopback6(), |
| 1952 | /*port=*/23456); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1953 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 1954 | kNewPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1955 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 1956 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 1957 | |
| 1958 | // Decrease packet number to simulate out-of-order packets. |
| 1959 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 4); |
| 1960 | // This is an old packet, do not migrate. |
| 1961 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1962 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 1963 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1964 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 1965 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 1966 | } |
| 1967 | |
| 1968 | TEST_P(QuicConnectionTest, PeerAddressChangeAtServer) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1969 | set_perspective(Perspective::IS_SERVER); |
| 1970 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 1971 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 1972 | |
| 1973 | // Clear direct_peer_address. |
| 1974 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 1975 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 1976 | // this test. |
| 1977 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 1978 | QuicSocketAddress()); |
| 1979 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 1980 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1981 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1982 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 1983 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1984 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 1985 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1986 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 1987 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1988 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 1989 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 1990 | |
| 1991 | // Process another packet with a different peer address on server side will |
| 1992 | // start connection migration. |
| 1993 | const QuicSocketAddress kNewPeerAddress = |
| 1994 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 1995 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 1996 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 1997 | kNewPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1998 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 1999 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 2000 | } |
| 2001 | |
| 2002 | TEST_P(QuicConnectionTest, EffectivePeerAddressChangeAtServer) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2003 | set_perspective(Perspective::IS_SERVER); |
| 2004 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2005 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
fayang | 6a25841 | 2020-05-28 08:57:12 -0700 | [diff] [blame] | 2006 | if (version().SupportsAntiAmplificationLimit()) { |
| 2007 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 2008 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2009 | |
| 2010 | // Clear direct_peer_address. |
| 2011 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2012 | // Clear effective_peer_address, it is different from direct_peer_address for |
| 2013 | // this test. |
| 2014 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2015 | QuicSocketAddress()); |
| 2016 | const QuicSocketAddress kEffectivePeerAddress = |
| 2017 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/43210); |
| 2018 | connection_.ReturnEffectivePeerAddressForNextPacket(kEffectivePeerAddress); |
| 2019 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2020 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2021 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2022 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2023 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2024 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2025 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2026 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2027 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2028 | EXPECT_EQ(kEffectivePeerAddress, connection_.effective_peer_address()); |
| 2029 | |
| 2030 | // Process another packet with the same direct peer address and different |
| 2031 | // effective peer address on server side will start connection migration. |
| 2032 | const QuicSocketAddress kNewEffectivePeerAddress = |
| 2033 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/54321); |
| 2034 | connection_.ReturnEffectivePeerAddressForNextPacket(kNewEffectivePeerAddress); |
| 2035 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2036 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2037 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2038 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2039 | EXPECT_EQ(kNewEffectivePeerAddress, connection_.effective_peer_address()); |
| 2040 | |
| 2041 | // Process another packet with a different direct peer address and the same |
| 2042 | // effective peer address on server side will not start connection migration. |
| 2043 | const QuicSocketAddress kNewPeerAddress = |
| 2044 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2045 | connection_.ReturnEffectivePeerAddressForNextPacket(kNewEffectivePeerAddress); |
| 2046 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2047 | // ack_frame is used to complete the migration started by the last packet, we |
| 2048 | // need to make sure a new migration does not start after the previous one is |
| 2049 | // completed. |
| 2050 | QuicAckFrame ack_frame = InitAckFrame(1); |
| 2051 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _)); |
| 2052 | ProcessFramePacketWithAddresses(QuicFrame(&ack_frame), kSelfAddress, |
| 2053 | kNewPeerAddress); |
| 2054 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 2055 | EXPECT_EQ(kNewEffectivePeerAddress, connection_.effective_peer_address()); |
| 2056 | |
| 2057 | // Process another packet with different direct peer address and different |
| 2058 | // effective peer address on server side will start connection migration. |
| 2059 | const QuicSocketAddress kNewerEffectivePeerAddress = |
| 2060 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/65432); |
| 2061 | const QuicSocketAddress kFinalPeerAddress = |
| 2062 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/34567); |
| 2063 | connection_.ReturnEffectivePeerAddressForNextPacket( |
| 2064 | kNewerEffectivePeerAddress); |
| 2065 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2066 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2067 | kFinalPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2068 | EXPECT_EQ(kFinalPeerAddress, connection_.peer_address()); |
| 2069 | EXPECT_EQ(kNewerEffectivePeerAddress, connection_.effective_peer_address()); |
| 2070 | EXPECT_EQ(PORT_CHANGE, connection_.active_effective_peer_migration_type()); |
| 2071 | |
| 2072 | // While the previous migration is ongoing, process another packet with the |
| 2073 | // same direct peer address and different effective peer address on server |
| 2074 | // side will start a new connection migration. |
| 2075 | const QuicSocketAddress kNewestEffectivePeerAddress = |
| 2076 | QuicSocketAddress(QuicIpAddress::Loopback4(), /*port=*/65430); |
| 2077 | connection_.ReturnEffectivePeerAddressForNextPacket( |
| 2078 | kNewestEffectivePeerAddress); |
| 2079 | EXPECT_CALL(visitor_, OnConnectionMigration(IPV6_TO_IPV4_CHANGE)).Times(1); |
| 2080 | EXPECT_CALL(*send_algorithm_, OnConnectionMigration()).Times(1); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2081 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2082 | kFinalPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2083 | EXPECT_EQ(kFinalPeerAddress, connection_.peer_address()); |
| 2084 | EXPECT_EQ(kNewestEffectivePeerAddress, connection_.effective_peer_address()); |
| 2085 | EXPECT_EQ(IPV6_TO_IPV4_CHANGE, |
| 2086 | connection_.active_effective_peer_migration_type()); |
| 2087 | } |
| 2088 | |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2089 | TEST_P(QuicConnectionTest, ReceivePathProbeWithNoAddressChangeAtServer) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2090 | set_perspective(Perspective::IS_SERVER); |
| 2091 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2092 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 2093 | |
| 2094 | // Clear direct_peer_address. |
| 2095 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2096 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2097 | // this test. |
| 2098 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2099 | QuicSocketAddress()); |
| 2100 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2101 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2102 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2103 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2104 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2105 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2106 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2107 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2108 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2109 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2110 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2111 | |
| 2112 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
zhongyi | 83161e4 | 2019-08-19 09:06:25 -0700 | [diff] [blame] | 2113 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, false)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2114 | |
| 2115 | // Process a padded PING or PATH CHALLENGE packet with no peer address change |
| 2116 | // on server side will be ignored. |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2117 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2118 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2119 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2120 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2121 | probing_packet->encrypted_length), |
| 2122 | clock_.Now())); |
| 2123 | |
| 2124 | uint64_t num_probing_received = |
| 2125 | connection_.GetStats().num_connectivity_probing_received; |
| 2126 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, *received); |
| 2127 | |
| 2128 | EXPECT_EQ(num_probing_received, |
| 2129 | connection_.GetStats().num_connectivity_probing_received); |
| 2130 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2131 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2132 | } |
| 2133 | |
wub | 54bd5bf | 2020-05-13 13:02:16 -0700 | [diff] [blame] | 2134 | // Regression test for b/150161358. |
| 2135 | TEST_P(QuicConnectionTest, BufferedMtuPacketTooBig) { |
wub | 54bd5bf | 2020-05-13 13:02:16 -0700 | [diff] [blame] | 2136 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(1); |
| 2137 | writer_->SetWriteBlocked(); |
| 2138 | |
| 2139 | // Send a MTU packet while blocked. It should be buffered. |
| 2140 | connection_.SendMtuDiscoveryPacket(kMaxOutgoingPacketSize); |
| 2141 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 2142 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 2143 | |
| 2144 | writer_->AlwaysGetPacketTooLarge(); |
| 2145 | writer_->SetWritable(); |
| 2146 | connection_.OnCanWrite(); |
| 2147 | } |
| 2148 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2149 | TEST_P(QuicConnectionTest, WriteOutOfOrderQueuedPackets) { |
| 2150 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2151 | if (!IsDefaultTestConfiguration()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2152 | return; |
| 2153 | } |
| 2154 | |
| 2155 | set_perspective(Perspective::IS_CLIENT); |
| 2156 | |
| 2157 | BlockOnNextWrite(); |
| 2158 | |
| 2159 | QuicStreamId stream_id = 2; |
| 2160 | connection_.SendStreamDataWithString(stream_id, "foo", 0, NO_FIN); |
| 2161 | |
| 2162 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 2163 | |
| 2164 | writer_->SetWritable(); |
| 2165 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 2166 | connection_.peer_address()); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 2167 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
| 2168 | connection_.OnCanWrite(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | TEST_P(QuicConnectionTest, DiscardQueuedPacketsAfterConnectionClose) { |
| 2172 | // Regression test for b/74073386. |
| 2173 | { |
| 2174 | InSequence seq; |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 2175 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 2176 | .Times(AtLeast(1)); |
| 2177 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | set_perspective(Perspective::IS_CLIENT); |
| 2181 | |
| 2182 | writer_->SimulateNextPacketTooLarge(); |
| 2183 | |
| 2184 | // This packet write should fail, which should cause the connection to close |
| 2185 | // after sending a connection close packet, then the failed packet should be |
| 2186 | // queued. |
| 2187 | connection_.SendStreamDataWithString(/*id=*/2, "foo", 0, NO_FIN); |
| 2188 | |
| 2189 | EXPECT_FALSE(connection_.connected()); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 2190 | // No need to buffer packets. |
| 2191 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2192 | |
| 2193 | EXPECT_EQ(0u, connection_.GetStats().packets_discarded); |
| 2194 | connection_.OnCanWrite(); |
fayang | 0f0c4e6 | 2019-07-16 08:55:54 -0700 | [diff] [blame] | 2195 | EXPECT_EQ(0u, connection_.GetStats().packets_discarded); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2196 | } |
| 2197 | |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2198 | // Receive a path probe request at the server side, i.e., |
| 2199 | // in non-IETF version: receive a padded PING packet with a peer addess change; |
| 2200 | // in IETF version: receive a packet contains PATH CHALLENGE with peer address |
| 2201 | // change. |
| 2202 | TEST_P(QuicConnectionTest, ReceivePathProbingAtServer) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2203 | set_perspective(Perspective::IS_SERVER); |
| 2204 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2205 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 2206 | |
| 2207 | // Clear direct_peer_address. |
| 2208 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2209 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2210 | // this test. |
| 2211 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2212 | QuicSocketAddress()); |
| 2213 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2214 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2215 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2216 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2217 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2218 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2219 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2220 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2221 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2222 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2223 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2224 | |
| 2225 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2226 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 2227 | EXPECT_CALL(visitor_, |
| 2228 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 2229 | .Times(1); |
| 2230 | } else { |
| 2231 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2232 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2233 | // Process a padded PING packet from a new peer address on server side |
| 2234 | // is effectively receiving a connectivity probing. |
| 2235 | const QuicSocketAddress kNewPeerAddress = |
| 2236 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2237 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2238 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2239 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2240 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2241 | probing_packet->encrypted_length), |
| 2242 | clock_.Now())); |
| 2243 | |
| 2244 | uint64_t num_probing_received = |
| 2245 | connection_.GetStats().num_connectivity_probing_received; |
| 2246 | ProcessReceivedPacket(kSelfAddress, kNewPeerAddress, *received); |
| 2247 | |
| 2248 | EXPECT_EQ(num_probing_received + 1, |
| 2249 | connection_.GetStats().num_connectivity_probing_received); |
| 2250 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2251 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2252 | |
| 2253 | // Process another packet with the old peer address on server side will not |
| 2254 | // start peer migration. |
| 2255 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2256 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2257 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2258 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2259 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2260 | } |
| 2261 | |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2262 | // Receive a padded PING packet with a port change on server side. |
| 2263 | TEST_P(QuicConnectionTest, ReceivePaddedPingWithPortChangeAtServer) { |
| 2264 | set_perspective(Perspective::IS_SERVER); |
| 2265 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2266 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 2267 | |
| 2268 | // Clear direct_peer_address. |
| 2269 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2270 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2271 | // this test. |
| 2272 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2273 | QuicSocketAddress()); |
| 2274 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2275 | |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2276 | if (GetParam().version.UsesCryptoFrames()) { |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2277 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2278 | } else { |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2279 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2280 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2281 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2282 | kPeerAddress); |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2283 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2284 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2285 | |
| 2286 | if (GetParam().version.HasIetfQuicFrames()) { |
| 2287 | // In IETF version, a padded PING packet with port change is not taken as |
| 2288 | // connectivity probe. |
| 2289 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
| 2290 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2291 | } else { |
| 2292 | // In non-IETF version, process a padded PING packet from a new peer |
| 2293 | // address on server side is effectively receiving a connectivity probing. |
| 2294 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2295 | EXPECT_CALL(visitor_, |
| 2296 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 2297 | .Times(1); |
| 2298 | } |
| 2299 | const QuicSocketAddress kNewPeerAddress = |
| 2300 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2301 | |
| 2302 | QuicFrames frames; |
| 2303 | // Write a PING frame, which has no data payload. |
| 2304 | QuicPingFrame ping_frame; |
| 2305 | frames.push_back(QuicFrame(ping_frame)); |
| 2306 | |
| 2307 | // Add padding to the rest of the packet. |
| 2308 | QuicPaddingFrame padding_frame; |
| 2309 | frames.push_back(QuicFrame(padding_frame)); |
| 2310 | |
| 2311 | uint64_t num_probing_received = |
| 2312 | connection_.GetStats().num_connectivity_probing_received; |
| 2313 | |
| 2314 | ProcessFramesPacketWithAddresses(frames, kSelfAddress, kNewPeerAddress); |
| 2315 | |
| 2316 | if (GetParam().version.HasIetfQuicFrames()) { |
| 2317 | // Padded PING with port changen is not considered as connectivity probe but |
| 2318 | // a PORT CHANGE. |
| 2319 | EXPECT_EQ(num_probing_received, |
| 2320 | connection_.GetStats().num_connectivity_probing_received); |
| 2321 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 2322 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 2323 | } else { |
| 2324 | EXPECT_EQ(num_probing_received + 1, |
| 2325 | connection_.GetStats().num_connectivity_probing_received); |
| 2326 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2327 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2328 | } |
| 2329 | |
| 2330 | // Process another packet with the old peer address on server side. |
| 2331 | if (GetParam().version.HasIetfQuicFrames()) { |
| 2332 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
| 2333 | } else { |
| 2334 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
| 2335 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2336 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2337 | kPeerAddress); |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2338 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2339 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2340 | } |
| 2341 | |
| 2342 | TEST_P(QuicConnectionTest, ReceiveReorderedPathProbingAtServer) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2343 | set_perspective(Perspective::IS_SERVER); |
| 2344 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2345 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 2346 | |
| 2347 | // Clear direct_peer_address. |
| 2348 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2349 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2350 | // this test. |
| 2351 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2352 | QuicSocketAddress()); |
| 2353 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2354 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2355 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2356 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2357 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2358 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2359 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2360 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 5); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2361 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2362 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2363 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2364 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2365 | |
| 2366 | // Decrease packet number to simulate out-of-order packets. |
| 2367 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 4); |
| 2368 | |
| 2369 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2370 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 2371 | EXPECT_CALL(visitor_, |
| 2372 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 2373 | .Times(1); |
| 2374 | } else { |
| 2375 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2376 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2377 | |
| 2378 | // Process a padded PING packet from a new peer address on server side |
| 2379 | // is effectively receiving a connectivity probing, even if a newer packet has |
| 2380 | // been received before this one. |
| 2381 | const QuicSocketAddress kNewPeerAddress = |
| 2382 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2383 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2384 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2385 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2386 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2387 | probing_packet->encrypted_length), |
| 2388 | clock_.Now())); |
| 2389 | |
| 2390 | uint64_t num_probing_received = |
| 2391 | connection_.GetStats().num_connectivity_probing_received; |
| 2392 | ProcessReceivedPacket(kSelfAddress, kNewPeerAddress, *received); |
| 2393 | |
| 2394 | EXPECT_EQ(num_probing_received + 1, |
| 2395 | connection_.GetStats().num_connectivity_probing_received); |
| 2396 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2397 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2398 | } |
| 2399 | |
| 2400 | TEST_P(QuicConnectionTest, MigrateAfterProbingAtServer) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2401 | set_perspective(Perspective::IS_SERVER); |
| 2402 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 2403 | EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); |
| 2404 | |
| 2405 | // Clear direct_peer_address. |
| 2406 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2407 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2408 | // this test. |
| 2409 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2410 | QuicSocketAddress()); |
| 2411 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2412 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2413 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2414 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2415 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2416 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2417 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2418 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2419 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2420 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2421 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2422 | |
| 2423 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2424 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 2425 | EXPECT_CALL(visitor_, |
| 2426 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 2427 | .Times(1); |
| 2428 | } else { |
| 2429 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2430 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2431 | |
| 2432 | // Process a padded PING packet from a new peer address on server side |
| 2433 | // is effectively receiving a connectivity probing. |
| 2434 | const QuicSocketAddress kNewPeerAddress = |
| 2435 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2436 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2437 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2438 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2439 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2440 | probing_packet->encrypted_length), |
| 2441 | clock_.Now())); |
| 2442 | ProcessReceivedPacket(kSelfAddress, kNewPeerAddress, *received); |
| 2443 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2444 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2445 | |
| 2446 | // Process another non-probing packet with the new peer address on server |
| 2447 | // side will start peer migration. |
| 2448 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(1); |
| 2449 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2450 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2451 | kNewPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2452 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 2453 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 2454 | } |
| 2455 | |
| 2456 | TEST_P(QuicConnectionTest, ReceivePaddedPingAtClient) { |
| 2457 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2458 | set_perspective(Perspective::IS_CLIENT); |
| 2459 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 2460 | |
| 2461 | // Clear direct_peer_address. |
| 2462 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2463 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2464 | // this test. |
| 2465 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2466 | QuicSocketAddress()); |
| 2467 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2468 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2469 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2470 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2471 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2472 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2473 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2474 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2475 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2476 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2477 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2478 | |
| 2479 | // Client takes all padded PING packet as speculative connectivity |
| 2480 | // probing packet, and reports to visitor. |
| 2481 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
zhongyi | 83161e4 | 2019-08-19 09:06:25 -0700 | [diff] [blame] | 2482 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, false)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2483 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2484 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2485 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2486 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2487 | probing_packet->encrypted_length), |
| 2488 | clock_.Now())); |
| 2489 | uint64_t num_probing_received = |
| 2490 | connection_.GetStats().num_connectivity_probing_received; |
| 2491 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, *received); |
| 2492 | |
| 2493 | EXPECT_EQ(num_probing_received, |
| 2494 | connection_.GetStats().num_connectivity_probing_received); |
| 2495 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2496 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2497 | } |
| 2498 | |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2499 | TEST_P(QuicConnectionTest, ReceiveConnectivityProbingResponseAtClient) { |
| 2500 | // TODO(b/150095484): add test coverage for IETF to verify that client takes |
| 2501 | // PATH RESPONSE with peer address change as correct validation on the new |
| 2502 | // path. |
| 2503 | if (GetParam().version.HasIetfQuicFrames()) { |
| 2504 | return; |
| 2505 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2506 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2507 | set_perspective(Perspective::IS_CLIENT); |
| 2508 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 2509 | |
| 2510 | // Clear direct_peer_address. |
| 2511 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2512 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2513 | // this test. |
| 2514 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2515 | QuicSocketAddress()); |
| 2516 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2517 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2518 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2519 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2520 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2521 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2522 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2523 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2524 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2525 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2526 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2527 | |
| 2528 | // Process a padded PING packet with a different self address on client side |
| 2529 | // is effectively receiving a connectivity probing. |
| 2530 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
zhongyi | f06ca34 | 2020-02-24 14:11:13 -0800 | [diff] [blame] | 2531 | if (!GetParam().version.HasIetfQuicFrames()) { |
| 2532 | EXPECT_CALL(visitor_, |
| 2533 | OnPacketReceived(_, _, /*is_connectivity_probe=*/true)) |
| 2534 | .Times(1); |
| 2535 | } else { |
| 2536 | EXPECT_CALL(visitor_, OnPacketReceived(_, _, _)).Times(0); |
| 2537 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2538 | |
| 2539 | const QuicSocketAddress kNewSelfAddress = |
| 2540 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2541 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2542 | std::unique_ptr<SerializedPacket> probing_packet = ConstructProbingPacket(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2543 | std::unique_ptr<QuicReceivedPacket> received(ConstructReceivedPacket( |
| 2544 | QuicEncryptedPacket(probing_packet->encrypted_buffer, |
| 2545 | probing_packet->encrypted_length), |
| 2546 | clock_.Now())); |
| 2547 | uint64_t num_probing_received = |
| 2548 | connection_.GetStats().num_connectivity_probing_received; |
| 2549 | ProcessReceivedPacket(kNewSelfAddress, kPeerAddress, *received); |
| 2550 | |
| 2551 | EXPECT_EQ(num_probing_received + 1, |
| 2552 | connection_.GetStats().num_connectivity_probing_received); |
| 2553 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2554 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2555 | } |
| 2556 | |
| 2557 | TEST_P(QuicConnectionTest, PeerAddressChangeAtClient) { |
| 2558 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2559 | set_perspective(Perspective::IS_CLIENT); |
| 2560 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 2561 | |
| 2562 | // Clear direct_peer_address. |
| 2563 | QuicConnectionPeer::SetDirectPeerAddress(&connection_, QuicSocketAddress()); |
| 2564 | // Clear effective_peer_address, it is the same as direct_peer_address for |
| 2565 | // this test. |
| 2566 | QuicConnectionPeer::SetEffectivePeerAddress(&connection_, |
| 2567 | QuicSocketAddress()); |
| 2568 | EXPECT_FALSE(connection_.effective_peer_address().IsInitialized()); |
| 2569 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2570 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2571 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 2572 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2573 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 2574 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2575 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2576 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2577 | EXPECT_EQ(kPeerAddress, connection_.peer_address()); |
| 2578 | EXPECT_EQ(kPeerAddress, connection_.effective_peer_address()); |
| 2579 | |
| 2580 | // Process another packet with a different peer address on client side will |
| 2581 | // only update peer address. |
| 2582 | const QuicSocketAddress kNewPeerAddress = |
| 2583 | QuicSocketAddress(QuicIpAddress::Loopback6(), /*port=*/23456); |
| 2584 | EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 2585 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 2586 | kNewPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2587 | EXPECT_EQ(kNewPeerAddress, connection_.peer_address()); |
| 2588 | EXPECT_EQ(kNewPeerAddress, connection_.effective_peer_address()); |
| 2589 | } |
| 2590 | |
| 2591 | TEST_P(QuicConnectionTest, MaxPacketSize) { |
| 2592 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 2593 | EXPECT_EQ(1350u, connection_.max_packet_length()); |
| 2594 | } |
| 2595 | |
dschinazi | 4ad1f46 | 2020-01-16 11:56:52 -0800 | [diff] [blame] | 2596 | TEST_P(QuicConnectionTest, PeerLowersMaxPacketSize) { |
| 2597 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 2598 | |
| 2599 | // SetFromConfig is always called after construction from InitializeSession. |
| 2600 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 2601 | constexpr uint32_t kTestMaxPacketSize = 1233u; |
| 2602 | QuicConfig config; |
| 2603 | QuicConfigPeer::SetReceivedMaxPacketSize(&config, kTestMaxPacketSize); |
| 2604 | connection_.SetFromConfig(config); |
| 2605 | |
| 2606 | EXPECT_EQ(kTestMaxPacketSize, connection_.max_packet_length()); |
| 2607 | } |
| 2608 | |
vasilvv | ebc5d0c | 2020-01-16 15:19:21 -0800 | [diff] [blame] | 2609 | TEST_P(QuicConnectionTest, PeerCannotRaiseMaxPacketSize) { |
| 2610 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 2611 | |
| 2612 | // SetFromConfig is always called after construction from InitializeSession. |
| 2613 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 2614 | constexpr uint32_t kTestMaxPacketSize = 1450u; |
| 2615 | QuicConfig config; |
| 2616 | QuicConfigPeer::SetReceivedMaxPacketSize(&config, kTestMaxPacketSize); |
| 2617 | connection_.SetFromConfig(config); |
| 2618 | |
| 2619 | EXPECT_EQ(kDefaultMaxPacketSize, connection_.max_packet_length()); |
| 2620 | } |
| 2621 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2622 | TEST_P(QuicConnectionTest, SmallerServerMaxPacketSize) { |
| 2623 | TestConnection connection(TestConnectionId(), kPeerAddress, helper_.get(), |
| 2624 | alarm_factory_.get(), writer_.get(), |
| 2625 | Perspective::IS_SERVER, version()); |
| 2626 | EXPECT_EQ(Perspective::IS_SERVER, connection.perspective()); |
| 2627 | EXPECT_EQ(1000u, connection.max_packet_length()); |
| 2628 | } |
| 2629 | |
| 2630 | TEST_P(QuicConnectionTest, IncreaseServerMaxPacketSize) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2631 | set_perspective(Perspective::IS_SERVER); |
| 2632 | connection_.SetMaxPacketLength(1000); |
| 2633 | |
| 2634 | QuicPacketHeader header; |
| 2635 | header.destination_connection_id = connection_id_; |
| 2636 | header.version_flag = true; |
nharper | 21af28e | 2019-06-17 15:54:34 -0700 | [diff] [blame] | 2637 | header.packet_number = QuicPacketNumber(12); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2638 | |
| 2639 | if (QuicVersionHasLongHeaderLengths( |
| 2640 | peer_framer_.version().transport_version)) { |
| 2641 | header.long_packet_type = INITIAL; |
| 2642 | header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1; |
| 2643 | header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2; |
| 2644 | } |
| 2645 | |
| 2646 | QuicFrames frames; |
| 2647 | QuicPaddingFrame padding; |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2648 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 2649 | frames.push_back(QuicFrame(&crypto_frame_)); |
| 2650 | } else { |
| 2651 | frames.push_back(QuicFrame(frame1_)); |
| 2652 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2653 | frames.push_back(QuicFrame(padding)); |
| 2654 | std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 2655 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 2656 | size_t encrypted_length = |
| 2657 | peer_framer_.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(12), |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 2658 | *packet, buffer, kMaxOutgoingPacketSize); |
| 2659 | EXPECT_EQ(kMaxOutgoingPacketSize, encrypted_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2660 | |
| 2661 | framer_.set_version(version()); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2662 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 2663 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 2664 | } else { |
| 2665 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 2666 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2667 | connection_.ProcessUdpPacket( |
| 2668 | kSelfAddress, kPeerAddress, |
| 2669 | QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); |
| 2670 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 2671 | EXPECT_EQ(kMaxOutgoingPacketSize, connection_.max_packet_length()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | TEST_P(QuicConnectionTest, IncreaseServerMaxPacketSizeWhileWriterLimited) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2675 | const QuicByteCount lower_max_packet_size = 1240; |
| 2676 | writer_->set_max_packet_size(lower_max_packet_size); |
| 2677 | set_perspective(Perspective::IS_SERVER); |
| 2678 | connection_.SetMaxPacketLength(1000); |
| 2679 | EXPECT_EQ(1000u, connection_.max_packet_length()); |
| 2680 | |
| 2681 | QuicPacketHeader header; |
| 2682 | header.destination_connection_id = connection_id_; |
| 2683 | header.version_flag = true; |
nharper | 21af28e | 2019-06-17 15:54:34 -0700 | [diff] [blame] | 2684 | header.packet_number = QuicPacketNumber(12); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2685 | |
| 2686 | if (QuicVersionHasLongHeaderLengths( |
| 2687 | peer_framer_.version().transport_version)) { |
| 2688 | header.long_packet_type = INITIAL; |
| 2689 | header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1; |
| 2690 | header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2; |
| 2691 | } |
| 2692 | |
| 2693 | QuicFrames frames; |
| 2694 | QuicPaddingFrame padding; |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2695 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 2696 | frames.push_back(QuicFrame(&crypto_frame_)); |
| 2697 | } else { |
| 2698 | frames.push_back(QuicFrame(frame1_)); |
| 2699 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2700 | frames.push_back(QuicFrame(padding)); |
| 2701 | std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 2702 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 2703 | size_t encrypted_length = |
| 2704 | peer_framer_.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(12), |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 2705 | *packet, buffer, kMaxOutgoingPacketSize); |
| 2706 | EXPECT_EQ(kMaxOutgoingPacketSize, encrypted_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2707 | |
| 2708 | framer_.set_version(version()); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 2709 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 2710 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 2711 | } else { |
| 2712 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 2713 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2714 | connection_.ProcessUdpPacket( |
| 2715 | kSelfAddress, kPeerAddress, |
| 2716 | QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); |
| 2717 | |
| 2718 | // Here, the limit imposed by the writer is lower than the size of the packet |
| 2719 | // received, so the writer max packet size is used. |
| 2720 | EXPECT_EQ(lower_max_packet_size, connection_.max_packet_length()); |
| 2721 | } |
| 2722 | |
| 2723 | TEST_P(QuicConnectionTest, LimitMaxPacketSizeByWriter) { |
| 2724 | const QuicByteCount lower_max_packet_size = 1240; |
| 2725 | writer_->set_max_packet_size(lower_max_packet_size); |
| 2726 | |
| 2727 | static_assert(lower_max_packet_size < kDefaultMaxPacketSize, |
| 2728 | "Default maximum packet size is too low"); |
| 2729 | connection_.SetMaxPacketLength(kDefaultMaxPacketSize); |
| 2730 | |
| 2731 | EXPECT_EQ(lower_max_packet_size, connection_.max_packet_length()); |
| 2732 | } |
| 2733 | |
| 2734 | TEST_P(QuicConnectionTest, LimitMaxPacketSizeByWriterForNewConnection) { |
| 2735 | const QuicConnectionId connection_id = TestConnectionId(17); |
| 2736 | const QuicByteCount lower_max_packet_size = 1240; |
| 2737 | writer_->set_max_packet_size(lower_max_packet_size); |
| 2738 | TestConnection connection(connection_id, kPeerAddress, helper_.get(), |
| 2739 | alarm_factory_.get(), writer_.get(), |
| 2740 | Perspective::IS_CLIENT, version()); |
| 2741 | EXPECT_EQ(Perspective::IS_CLIENT, connection.perspective()); |
| 2742 | EXPECT_EQ(lower_max_packet_size, connection.max_packet_length()); |
| 2743 | } |
| 2744 | |
| 2745 | TEST_P(QuicConnectionTest, PacketsInOrder) { |
| 2746 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2747 | |
| 2748 | ProcessPacket(1); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2749 | EXPECT_EQ(QuicPacketNumber(1u), LargestAcked(connection_.ack_frame())); |
| 2750 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2751 | |
| 2752 | ProcessPacket(2); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2753 | EXPECT_EQ(QuicPacketNumber(2u), LargestAcked(connection_.ack_frame())); |
| 2754 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2755 | |
| 2756 | ProcessPacket(3); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2757 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
| 2758 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | TEST_P(QuicConnectionTest, PacketsOutOfOrder) { |
| 2762 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2763 | |
| 2764 | ProcessPacket(3); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2765 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2766 | EXPECT_TRUE(IsMissing(2)); |
| 2767 | EXPECT_TRUE(IsMissing(1)); |
| 2768 | |
| 2769 | ProcessPacket(2); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2770 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2771 | EXPECT_FALSE(IsMissing(2)); |
| 2772 | EXPECT_TRUE(IsMissing(1)); |
| 2773 | |
| 2774 | ProcessPacket(1); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2775 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2776 | EXPECT_FALSE(IsMissing(2)); |
| 2777 | EXPECT_FALSE(IsMissing(1)); |
| 2778 | } |
| 2779 | |
| 2780 | TEST_P(QuicConnectionTest, DuplicatePacket) { |
| 2781 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2782 | |
| 2783 | ProcessPacket(3); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2784 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2785 | EXPECT_TRUE(IsMissing(2)); |
| 2786 | EXPECT_TRUE(IsMissing(1)); |
| 2787 | |
| 2788 | // Send packet 3 again, but do not set the expectation that |
| 2789 | // the visitor OnStreamFrame() will be called. |
| 2790 | ProcessDataPacket(3); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2791 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2792 | EXPECT_TRUE(IsMissing(2)); |
| 2793 | EXPECT_TRUE(IsMissing(1)); |
| 2794 | } |
| 2795 | |
| 2796 | TEST_P(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndLeastAwaiting) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 2797 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 2798 | return; |
| 2799 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2800 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2801 | |
| 2802 | ProcessPacket(3); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2803 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2804 | EXPECT_TRUE(IsMissing(2)); |
| 2805 | EXPECT_TRUE(IsMissing(1)); |
| 2806 | |
| 2807 | ProcessPacket(2); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2808 | EXPECT_EQ(QuicPacketNumber(3u), LargestAcked(connection_.ack_frame())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2809 | EXPECT_TRUE(IsMissing(1)); |
| 2810 | |
| 2811 | ProcessPacket(5); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 2812 | EXPECT_EQ(QuicPacketNumber(5u), LargestAcked(connection_.ack_frame())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2813 | EXPECT_TRUE(IsMissing(1)); |
| 2814 | EXPECT_TRUE(IsMissing(4)); |
| 2815 | |
| 2816 | // Pretend at this point the client has gotten acks for 2 and 3 and 1 is a |
| 2817 | // packet the peer will not retransmit. It indicates this by sending 'least |
| 2818 | // awaiting' is 4. The connection should then realize 1 will not be |
| 2819 | // retransmitted, and will remove it from the missing list. |
| 2820 | QuicAckFrame frame = InitAckFrame(1); |
| 2821 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _)); |
| 2822 | ProcessAckPacket(6, &frame); |
| 2823 | |
| 2824 | // Force an ack to be sent. |
| 2825 | SendAckPacketToPeer(); |
| 2826 | EXPECT_TRUE(IsMissing(4)); |
| 2827 | } |
| 2828 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2829 | TEST_P(QuicConnectionTest, RejectUnencryptedStreamData) { |
| 2830 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 2831 | if (!IsDefaultTestConfiguration()) { |
| 2832 | return; |
| 2833 | } |
| 2834 | |
| 2835 | // Process an unencrypted packet from the non-crypto stream. |
| 2836 | frame1_.stream_id = 3; |
| 2837 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 2838 | EXPECT_CALL(visitor_, |
| 2839 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
nharper | 2c9f02a | 2019-05-08 10:25:50 -0700 | [diff] [blame] | 2840 | EXPECT_QUIC_PEER_BUG(ProcessDataPacketAtLevel(1, false, ENCRYPTION_INITIAL), |
| 2841 | ""); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 2842 | TestConnectionCloseQuicErrorCode(QUIC_UNENCRYPTED_STREAM_DATA); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | TEST_P(QuicConnectionTest, OutOfOrderReceiptCausesAckSend) { |
| 2846 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2847 | |
| 2848 | ProcessPacket(3); |
fayang | 6dba490 | 2019-06-17 10:04:23 -0700 | [diff] [blame] | 2849 | // Should not cause an ack. |
| 2850 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2851 | |
| 2852 | ProcessPacket(2); |
fayang | 6dba490 | 2019-06-17 10:04:23 -0700 | [diff] [blame] | 2853 | // Should ack immediately, since this fills the last hole. |
| 2854 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2855 | |
| 2856 | ProcessPacket(1); |
| 2857 | // Should ack immediately, since this fills the last hole. |
fayang | 6dba490 | 2019-06-17 10:04:23 -0700 | [diff] [blame] | 2858 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2859 | |
| 2860 | ProcessPacket(4); |
| 2861 | // Should not cause an ack. |
fayang | 6dba490 | 2019-06-17 10:04:23 -0700 | [diff] [blame] | 2862 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | TEST_P(QuicConnectionTest, OutOfOrderAckReceiptCausesNoAck) { |
| 2866 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2867 | |
| 2868 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); |
| 2869 | SendStreamDataToPeer(1, "bar", 3, NO_FIN, nullptr); |
| 2870 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 2871 | |
| 2872 | QuicAckFrame ack1 = InitAckFrame(1); |
| 2873 | QuicAckFrame ack2 = InitAckFrame(2); |
| 2874 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
fayang | 2f2915d | 2020-01-24 06:47:15 -0800 | [diff] [blame] | 2875 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 2876 | EXPECT_CALL(visitor_, OnOneRttPacketAcknowledged()).Times(1); |
| 2877 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2878 | ProcessAckPacket(2, &ack2); |
| 2879 | // Should ack immediately since we have missing packets. |
| 2880 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 2881 | |
fayang | 2f2915d | 2020-01-24 06:47:15 -0800 | [diff] [blame] | 2882 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 2883 | EXPECT_CALL(visitor_, OnOneRttPacketAcknowledged()).Times(0); |
| 2884 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2885 | ProcessAckPacket(1, &ack1); |
| 2886 | // Should not ack an ack filling a missing packet. |
| 2887 | EXPECT_EQ(2u, writer_->packets_write_attempts()); |
| 2888 | } |
| 2889 | |
| 2890 | TEST_P(QuicConnectionTest, AckReceiptCausesAckSend) { |
| 2891 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2892 | QuicPacketNumber original, second; |
| 2893 | |
| 2894 | QuicByteCount packet_size = |
| 2895 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, &original); // 1st packet. |
| 2896 | SendStreamDataToPeer(3, "bar", 3, NO_FIN, &second); // 2nd packet. |
| 2897 | |
| 2898 | QuicAckFrame frame = InitAckFrame({{second, second + 1}}); |
| 2899 | // First nack triggers early retransmit. |
| 2900 | LostPacketVector lost_packets; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 2901 | lost_packets.push_back(LostPacket(original, kMaxOutgoingPacketSize)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2902 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
wub | aa51f0e | 2020-05-12 15:08:16 -0700 | [diff] [blame] | 2903 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 2904 | Return(LossDetectionInterface::DetectionStats()))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2905 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 2906 | QuicPacketNumber retransmission; |
| 2907 | // Packet 1 is short header for IETF QUIC because the encryption level |
| 2908 | // switched to ENCRYPTION_FORWARD_SECURE in SendStreamDataToPeer. |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 2909 | EXPECT_CALL(*send_algorithm_, |
| 2910 | OnPacketSent(_, _, _, |
| 2911 | VersionHasIetfInvariantHeader( |
| 2912 | GetParam().version.transport_version) |
| 2913 | ? packet_size |
| 2914 | : packet_size - kQuicVersionSize, |
| 2915 | _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2916 | .WillOnce(SaveArg<2>(&retransmission)); |
| 2917 | |
| 2918 | ProcessAckPacket(&frame); |
| 2919 | |
| 2920 | QuicAckFrame frame2 = ConstructAckFrame(retransmission, original); |
| 2921 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 2922 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 2923 | ProcessAckPacket(&frame2); |
| 2924 | |
| 2925 | // Now if the peer sends an ack which still reports the retransmitted packet |
| 2926 | // as missing, that will bundle an ack with data after two acks in a row |
| 2927 | // indicate the high water mark needs to be raised. |
| 2928 | EXPECT_CALL(*send_algorithm_, |
| 2929 | OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)); |
| 2930 | connection_.SendStreamDataWithString(3, "foo", 6, NO_FIN); |
| 2931 | // No ack sent. |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 2932 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 2933 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2934 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 2935 | |
| 2936 | // No more packet loss for the rest of the test. |
| 2937 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
| 2938 | .Times(AnyNumber()); |
| 2939 | ProcessAckPacket(&frame2); |
| 2940 | EXPECT_CALL(*send_algorithm_, |
| 2941 | OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)); |
fayang | 0391669 | 2019-05-22 17:57:18 -0700 | [diff] [blame] | 2942 | connection_.SendStreamDataWithString(3, "foofoofoo", 9, NO_FIN); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2943 | // Ack bundled. |
| 2944 | if (GetParam().no_stop_waiting) { |
fayang | 8a27b0f | 2019-11-04 11:27:40 -0800 | [diff] [blame] | 2945 | // Do not ACK acks. |
| 2946 | EXPECT_EQ(1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2947 | } else { |
| 2948 | EXPECT_EQ(3u, writer_->frame_count()); |
| 2949 | } |
| 2950 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
fayang | 8a27b0f | 2019-11-04 11:27:40 -0800 | [diff] [blame] | 2951 | if (GetParam().no_stop_waiting) { |
fayang | 0391669 | 2019-05-22 17:57:18 -0700 | [diff] [blame] | 2952 | EXPECT_TRUE(writer_->ack_frames().empty()); |
| 2953 | } else { |
| 2954 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 2955 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2956 | |
| 2957 | // But an ack with no missing packets will not send an ack. |
| 2958 | AckPacket(original, &frame2); |
| 2959 | ProcessAckPacket(&frame2); |
| 2960 | ProcessAckPacket(&frame2); |
| 2961 | } |
| 2962 | |
| 2963 | TEST_P(QuicConnectionTest, AckSentEveryNthPacket) { |
| 2964 | connection_.set_ack_frequency_before_ack_decimation(3); |
| 2965 | |
| 2966 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2967 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(39); |
| 2968 | |
| 2969 | // Expect 13 acks, every 3rd packet. |
| 2970 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(13); |
| 2971 | // Receives packets 1 - 39. |
| 2972 | for (size_t i = 1; i <= 39; ++i) { |
| 2973 | ProcessDataPacket(i); |
| 2974 | } |
| 2975 | } |
| 2976 | |
| 2977 | TEST_P(QuicConnectionTest, AckDecimationReducesAcks) { |
| 2978 | const size_t kMinRttMs = 40; |
| 2979 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 2980 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 2981 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 2982 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 2983 | |
| 2984 | QuicConnectionPeer::SetAckMode(&connection_, ACK_DECIMATION_WITH_REORDERING); |
| 2985 | |
| 2986 | // Start ack decimation from 10th packet. |
| 2987 | connection_.set_min_received_before_ack_decimation(10); |
| 2988 | |
| 2989 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 2990 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(30); |
| 2991 | |
| 2992 | // Expect 6 acks: 5 acks between packets 1-10, and ack at 20. |
| 2993 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(6); |
| 2994 | // Receives packets 1 - 29. |
| 2995 | for (size_t i = 1; i <= 29; ++i) { |
| 2996 | ProcessDataPacket(i); |
| 2997 | } |
| 2998 | |
| 2999 | // We now receive the 30th packet, and so we send an ack. |
| 3000 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3001 | ProcessDataPacket(30); |
| 3002 | } |
| 3003 | |
| 3004 | TEST_P(QuicConnectionTest, AckNeedsRetransmittableFrames) { |
ianswett | 68cf004 | 2019-05-09 08:37:58 -0700 | [diff] [blame] | 3005 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3006 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3007 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(99); |
| 3008 | |
| 3009 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(19); |
| 3010 | // Receives packets 1 - 39. |
| 3011 | for (size_t i = 1; i <= 39; ++i) { |
| 3012 | ProcessDataPacket(i); |
| 3013 | } |
| 3014 | // Receiving Packet 40 causes 20th ack to send. Session is informed and adds |
| 3015 | // WINDOW_UPDATE. |
| 3016 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()) |
| 3017 | .WillOnce(Invoke([this]() { |
| 3018 | connection_.SendControlFrame( |
| 3019 | QuicFrame(new QuicWindowUpdateFrame(1, 0, 0))); |
| 3020 | })); |
| 3021 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3022 | EXPECT_EQ(0u, writer_->window_update_frames().size()); |
| 3023 | ProcessDataPacket(40); |
| 3024 | EXPECT_EQ(1u, writer_->window_update_frames().size()); |
| 3025 | |
| 3026 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(9); |
| 3027 | // Receives packets 41 - 59. |
| 3028 | for (size_t i = 41; i <= 59; ++i) { |
| 3029 | ProcessDataPacket(i); |
| 3030 | } |
| 3031 | // Send a packet containing stream frame. |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 3032 | SendStreamDataToPeer( |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3033 | QuicUtils::GetFirstBidirectionalStreamId( |
| 3034 | connection_.version().transport_version, Perspective::IS_CLIENT), |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 3035 | "bar", 0, NO_FIN, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3036 | |
| 3037 | // Session will not be informed until receiving another 20 packets. |
| 3038 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(19); |
| 3039 | for (size_t i = 60; i <= 98; ++i) { |
| 3040 | ProcessDataPacket(i); |
| 3041 | EXPECT_EQ(0u, writer_->window_update_frames().size()); |
| 3042 | } |
| 3043 | // Session does not add a retransmittable frame. |
| 3044 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()) |
| 3045 | .WillOnce(Invoke([this]() { |
| 3046 | connection_.SendControlFrame(QuicFrame(QuicPingFrame(1))); |
| 3047 | })); |
| 3048 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3049 | EXPECT_EQ(0u, writer_->ping_frames().size()); |
| 3050 | ProcessDataPacket(99); |
| 3051 | EXPECT_EQ(0u, writer_->window_update_frames().size()); |
| 3052 | // A ping frame will be added. |
| 3053 | EXPECT_EQ(1u, writer_->ping_frames().size()); |
| 3054 | } |
| 3055 | |
ianswett | 6083a10 | 2020-02-09 12:04:04 -0800 | [diff] [blame] | 3056 | TEST_P(QuicConnectionTest, AckNeedsRetransmittableFramesAfterPto) { |
ianswett | 6083a10 | 2020-02-09 12:04:04 -0800 | [diff] [blame] | 3057 | // Disable TLP so the RTO fires immediately. |
| 3058 | connection_.SetMaxTailLossProbes(0); |
| 3059 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 3060 | QuicConfig config; |
| 3061 | QuicTagVector connection_options; |
| 3062 | connection_options.push_back(kEACK); |
| 3063 | config.SetConnectionOptionsToSend(connection_options); |
| 3064 | connection_.SetFromConfig(config); |
| 3065 | |
| 3066 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3067 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3068 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(10); |
| 3069 | |
| 3070 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(4); |
| 3071 | // Receive packets 1 - 9. |
| 3072 | for (size_t i = 1; i <= 9; ++i) { |
| 3073 | ProcessDataPacket(i); |
| 3074 | } |
| 3075 | |
| 3076 | // Send a ping and fire the retransmission alarm. |
| 3077 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 3078 | SendPing(); |
| 3079 | QuicTime retransmission_time = |
| 3080 | connection_.GetRetransmissionAlarm()->deadline(); |
| 3081 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
| 3082 | connection_.GetRetransmissionAlarm()->Fire(); |
| 3083 | ASSERT_TRUE(manager_->GetConsecutiveRtoCount() > 0 || |
| 3084 | manager_->GetConsecutivePtoCount() > 0); |
| 3085 | |
| 3086 | // Process a packet, which requests a retransmittable frame be bundled |
| 3087 | // with the ACK. |
| 3088 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()) |
| 3089 | .WillOnce(Invoke([this]() { |
| 3090 | connection_.SendControlFrame( |
| 3091 | QuicFrame(new QuicWindowUpdateFrame(1, 0, 0))); |
| 3092 | })); |
| 3093 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3094 | ProcessDataPacket(11); |
| 3095 | EXPECT_EQ(1u, writer_->window_update_frames().size()); |
| 3096 | } |
| 3097 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3098 | TEST_P(QuicConnectionTest, LeastUnackedLower) { |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 3099 | if (VersionHasIetfInvariantHeader(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3100 | return; |
| 3101 | } |
| 3102 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3103 | |
| 3104 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); |
| 3105 | SendStreamDataToPeer(1, "bar", 3, NO_FIN, nullptr); |
| 3106 | SendStreamDataToPeer(1, "eep", 6, NO_FIN, nullptr); |
| 3107 | |
| 3108 | // Start out saying the least unacked is 2. |
| 3109 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 5); |
| 3110 | ProcessStopWaitingPacket(InitStopWaitingFrame(2)); |
| 3111 | |
| 3112 | // Change it to 1, but lower the packet number to fake out-of-order packets. |
| 3113 | // This should be fine. |
| 3114 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 1); |
| 3115 | // The scheduler will not process out of order acks, but all packet processing |
| 3116 | // causes the connection to try to write. |
| 3117 | if (!GetParam().no_stop_waiting) { |
| 3118 | EXPECT_CALL(visitor_, OnCanWrite()); |
| 3119 | } |
| 3120 | ProcessStopWaitingPacket(InitStopWaitingFrame(1)); |
| 3121 | |
| 3122 | // Now claim it's one, but set the ordering so it was sent "after" the first |
| 3123 | // one. This should cause a connection error. |
| 3124 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 7); |
| 3125 | if (!GetParam().no_stop_waiting) { |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 3126 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 3127 | .Times(AtLeast(1)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 3128 | EXPECT_CALL(visitor_, |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 3129 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 3130 | .Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3131 | } |
| 3132 | ProcessStopWaitingPacket(InitStopWaitingFrame(1)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 3133 | if (!GetParam().no_stop_waiting) { |
| 3134 | TestConnectionCloseQuicErrorCode(QUIC_INVALID_STOP_WAITING_DATA); |
| 3135 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | TEST_P(QuicConnectionTest, TooManySentPackets) { |
| 3139 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3140 | |
| 3141 | QuicPacketCount max_tracked_packets = 50; |
| 3142 | QuicConnectionPeer::SetMaxTrackedPackets(&connection_, max_tracked_packets); |
| 3143 | |
| 3144 | const int num_packets = max_tracked_packets + 5; |
| 3145 | |
| 3146 | for (int i = 0; i < num_packets; ++i) { |
| 3147 | SendStreamDataToPeer(1, "foo", 3 * i, NO_FIN, nullptr); |
| 3148 | } |
| 3149 | |
| 3150 | // Ack packet 1, which leaves more than the limit outstanding. |
| 3151 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3152 | EXPECT_CALL(visitor_, |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 3153 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3154 | |
| 3155 | // Nack the first packet and ack the rest, leaving a huge gap. |
| 3156 | QuicAckFrame frame1 = ConstructAckFrame(num_packets, 1); |
| 3157 | ProcessAckPacket(&frame1); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 3158 | TestConnectionCloseQuicErrorCode(QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3159 | } |
| 3160 | |
| 3161 | TEST_P(QuicConnectionTest, LargestObservedLower) { |
| 3162 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3163 | |
| 3164 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); |
| 3165 | SendStreamDataToPeer(1, "bar", 3, NO_FIN, nullptr); |
| 3166 | SendStreamDataToPeer(1, "eep", 6, NO_FIN, nullptr); |
| 3167 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3168 | |
| 3169 | // Start out saying the largest observed is 2. |
| 3170 | QuicAckFrame frame1 = InitAckFrame(1); |
| 3171 | QuicAckFrame frame2 = InitAckFrame(2); |
| 3172 | ProcessAckPacket(&frame2); |
| 3173 | |
fayang | 745c93a | 2019-06-21 13:43:04 -0700 | [diff] [blame] | 3174 | EXPECT_CALL(visitor_, OnCanWrite()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3175 | ProcessAckPacket(&frame1); |
| 3176 | } |
| 3177 | |
| 3178 | TEST_P(QuicConnectionTest, AckUnsentData) { |
| 3179 | // Ack a packet which has not been sent. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 3180 | EXPECT_CALL(visitor_, |
| 3181 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3182 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 3183 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3184 | QuicAckFrame frame = InitAckFrame(1); |
| 3185 | EXPECT_CALL(visitor_, OnCanWrite()).Times(0); |
| 3186 | ProcessAckPacket(&frame); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 3187 | TestConnectionCloseQuicErrorCode(QUIC_INVALID_ACK_DATA); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | TEST_P(QuicConnectionTest, BasicSending) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 3191 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 3192 | return; |
| 3193 | } |
fayang | 643c7dd | 2020-05-18 07:11:27 -0700 | [diff] [blame] | 3194 | const QuicConnectionStats& stats = connection_.GetStats(); |
| 3195 | EXPECT_FALSE(stats.first_decrypted_packet.IsInitialized()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3196 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3197 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 3198 | ProcessDataPacket(1); |
fayang | 643c7dd | 2020-05-18 07:11:27 -0700 | [diff] [blame] | 3199 | EXPECT_EQ(QuicPacketNumber(1), stats.first_decrypted_packet); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3200 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 2); |
| 3201 | QuicPacketNumber last_packet; |
| 3202 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); // Packet 1 |
| 3203 | EXPECT_EQ(QuicPacketNumber(1u), last_packet); |
| 3204 | SendAckPacketToPeer(); // Packet 2 |
| 3205 | |
| 3206 | if (GetParam().no_stop_waiting) { |
| 3207 | // Expect no stop waiting frame is sent. |
| 3208 | EXPECT_FALSE(least_unacked().IsInitialized()); |
| 3209 | } else { |
| 3210 | EXPECT_EQ(QuicPacketNumber(1u), least_unacked()); |
| 3211 | } |
| 3212 | |
| 3213 | SendAckPacketToPeer(); // Packet 3 |
| 3214 | if (GetParam().no_stop_waiting) { |
| 3215 | // Expect no stop waiting frame is sent. |
| 3216 | EXPECT_FALSE(least_unacked().IsInitialized()); |
| 3217 | } else { |
| 3218 | EXPECT_EQ(QuicPacketNumber(1u), least_unacked()); |
| 3219 | } |
| 3220 | |
| 3221 | SendStreamDataToPeer(1, "bar", 3, NO_FIN, &last_packet); // Packet 4 |
| 3222 | EXPECT_EQ(QuicPacketNumber(4u), last_packet); |
| 3223 | SendAckPacketToPeer(); // Packet 5 |
| 3224 | if (GetParam().no_stop_waiting) { |
| 3225 | // Expect no stop waiting frame is sent. |
| 3226 | EXPECT_FALSE(least_unacked().IsInitialized()); |
| 3227 | } else { |
| 3228 | EXPECT_EQ(QuicPacketNumber(1u), least_unacked()); |
| 3229 | } |
| 3230 | |
| 3231 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3232 | |
| 3233 | // Peer acks up to packet 3. |
| 3234 | QuicAckFrame frame = InitAckFrame(3); |
| 3235 | ProcessAckPacket(&frame); |
| 3236 | SendAckPacketToPeer(); // Packet 6 |
| 3237 | |
| 3238 | // As soon as we've acked one, we skip ack packets 2 and 3 and note lack of |
| 3239 | // ack for 4. |
| 3240 | if (GetParam().no_stop_waiting) { |
| 3241 | // Expect no stop waiting frame is sent. |
| 3242 | EXPECT_FALSE(least_unacked().IsInitialized()); |
| 3243 | } else { |
| 3244 | EXPECT_EQ(QuicPacketNumber(4u), least_unacked()); |
| 3245 | } |
| 3246 | |
| 3247 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3248 | |
| 3249 | // Peer acks up to packet 4, the last packet. |
| 3250 | QuicAckFrame frame2 = InitAckFrame(6); |
| 3251 | ProcessAckPacket(&frame2); // Acks don't instigate acks. |
| 3252 | |
| 3253 | // Verify that we did not send an ack. |
| 3254 | EXPECT_EQ(QuicPacketNumber(6u), writer_->header().packet_number); |
| 3255 | |
| 3256 | // So the last ack has not changed. |
| 3257 | if (GetParam().no_stop_waiting) { |
| 3258 | // Expect no stop waiting frame is sent. |
| 3259 | EXPECT_FALSE(least_unacked().IsInitialized()); |
| 3260 | } else { |
| 3261 | EXPECT_EQ(QuicPacketNumber(4u), least_unacked()); |
| 3262 | } |
| 3263 | |
| 3264 | // If we force an ack, we shouldn't change our retransmit state. |
| 3265 | SendAckPacketToPeer(); // Packet 7 |
| 3266 | if (GetParam().no_stop_waiting) { |
| 3267 | // Expect no stop waiting frame is sent. |
| 3268 | EXPECT_FALSE(least_unacked().IsInitialized()); |
| 3269 | } else { |
| 3270 | EXPECT_EQ(QuicPacketNumber(7u), least_unacked()); |
| 3271 | } |
| 3272 | |
| 3273 | // But if we send more data it should. |
| 3274 | SendStreamDataToPeer(1, "eep", 6, NO_FIN, &last_packet); // Packet 8 |
| 3275 | EXPECT_EQ(QuicPacketNumber(8u), last_packet); |
| 3276 | SendAckPacketToPeer(); // Packet 9 |
| 3277 | if (GetParam().no_stop_waiting) { |
| 3278 | // Expect no stop waiting frame is sent. |
| 3279 | EXPECT_FALSE(least_unacked().IsInitialized()); |
| 3280 | } else { |
| 3281 | EXPECT_EQ(QuicPacketNumber(7u), least_unacked()); |
| 3282 | } |
fayang | 643c7dd | 2020-05-18 07:11:27 -0700 | [diff] [blame] | 3283 | EXPECT_EQ(QuicPacketNumber(1), stats.first_decrypted_packet); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | // QuicConnection should record the packet sent-time prior to sending the |
| 3287 | // packet. |
| 3288 | TEST_P(QuicConnectionTest, RecordSentTimeBeforePacketSent) { |
| 3289 | // We're using a MockClock for the tests, so we have complete control over the |
| 3290 | // time. |
| 3291 | // Our recorded timestamp for the last packet sent time will be passed in to |
| 3292 | // the send_algorithm. Make sure that it is set to the correct value. |
| 3293 | QuicTime actual_recorded_send_time = QuicTime::Zero(); |
| 3294 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 3295 | .WillOnce(SaveArg<0>(&actual_recorded_send_time)); |
| 3296 | |
| 3297 | // First send without any pause and check the result. |
| 3298 | QuicTime expected_recorded_send_time = clock_.Now(); |
| 3299 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 3300 | EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time) |
| 3301 | << "Expected time = " << expected_recorded_send_time.ToDebuggingValue() |
| 3302 | << ". Actual time = " << actual_recorded_send_time.ToDebuggingValue(); |
| 3303 | |
| 3304 | // Now pause during the write, and check the results. |
| 3305 | actual_recorded_send_time = QuicTime::Zero(); |
| 3306 | const QuicTime::Delta write_pause_time_delta = |
| 3307 | QuicTime::Delta::FromMilliseconds(5000); |
| 3308 | SetWritePauseTimeDelta(write_pause_time_delta); |
| 3309 | expected_recorded_send_time = clock_.Now(); |
| 3310 | |
| 3311 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 3312 | .WillOnce(SaveArg<0>(&actual_recorded_send_time)); |
| 3313 | connection_.SendStreamDataWithString(2, "baz", 0, NO_FIN); |
| 3314 | EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time) |
| 3315 | << "Expected time = " << expected_recorded_send_time.ToDebuggingValue() |
| 3316 | << ". Actual time = " << actual_recorded_send_time.ToDebuggingValue(); |
| 3317 | } |
| 3318 | |
| 3319 | TEST_P(QuicConnectionTest, FramePacking) { |
| 3320 | // Send two stream frames in 1 packet by queueing them. |
| 3321 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3322 | { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 3323 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3324 | connection_.SendStreamData3(); |
| 3325 | connection_.SendStreamData5(); |
| 3326 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3327 | } |
| 3328 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3329 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3330 | |
| 3331 | // Parse the last packet and ensure it's an ack and two stream frames from |
| 3332 | // two different streams. |
| 3333 | if (GetParam().no_stop_waiting) { |
| 3334 | EXPECT_EQ(2u, writer_->frame_count()); |
| 3335 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 3336 | } else { |
| 3337 | EXPECT_EQ(2u, writer_->frame_count()); |
| 3338 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 3339 | } |
| 3340 | |
| 3341 | EXPECT_TRUE(writer_->ack_frames().empty()); |
| 3342 | |
| 3343 | ASSERT_EQ(2u, writer_->stream_frames().size()); |
| 3344 | EXPECT_EQ(GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 3345 | writer_->stream_frames()[0]->stream_id); |
| 3346 | EXPECT_EQ(GetNthClientInitiatedStreamId(2, connection_.transport_version()), |
| 3347 | writer_->stream_frames()[1]->stream_id); |
| 3348 | } |
| 3349 | |
| 3350 | TEST_P(QuicConnectionTest, FramePackingNonCryptoThenCrypto) { |
| 3351 | // Send two stream frames (one non-crypto, then one crypto) in 2 packets by |
| 3352 | // queueing them. |
| 3353 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3354 | { |
| 3355 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 3356 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3357 | connection_.SendStreamData3(); |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 3358 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3359 | connection_.SendCryptoStreamData(); |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 3360 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3361 | } |
| 3362 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3363 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3364 | |
| 3365 | // Parse the last packet and ensure it's the crypto stream frame. |
| 3366 | EXPECT_EQ(2u, writer_->frame_count()); |
| 3367 | ASSERT_EQ(1u, writer_->padding_frames().size()); |
QUICHE team | ea74008 | 2019-03-11 17:58:43 -0700 | [diff] [blame] | 3368 | if (!QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3369 | ASSERT_EQ(1u, writer_->stream_frames().size()); |
| 3370 | EXPECT_EQ(QuicUtils::GetCryptoStreamId(connection_.transport_version()), |
| 3371 | writer_->stream_frames()[0]->stream_id); |
| 3372 | } else { |
| 3373 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
| 3374 | } |
| 3375 | } |
| 3376 | |
| 3377 | TEST_P(QuicConnectionTest, FramePackingCryptoThenNonCrypto) { |
| 3378 | // Send two stream frames (one crypto, then one non-crypto) in 2 packets by |
| 3379 | // queueing them. |
| 3380 | { |
| 3381 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3382 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 3383 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3384 | connection_.SendCryptoStreamData(); |
| 3385 | connection_.SendStreamData3(); |
| 3386 | } |
| 3387 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3388 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3389 | |
| 3390 | // Parse the last packet and ensure it's the stream frame from stream 3. |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 3391 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 3392 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3393 | ASSERT_EQ(1u, writer_->stream_frames().size()); |
| 3394 | EXPECT_EQ(GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 3395 | writer_->stream_frames()[0]->stream_id); |
| 3396 | } |
| 3397 | |
| 3398 | TEST_P(QuicConnectionTest, FramePackingAckResponse) { |
| 3399 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3400 | // Process a data packet to queue up a pending ack. |
fayang | 3451f6e | 2019-06-11 08:18:12 -0700 | [diff] [blame] | 3401 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 3402 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 3403 | } else { |
| 3404 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 3405 | } |
| 3406 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 3407 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3408 | QuicPacketNumber last_packet; |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3409 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 3410 | connection_.SendCryptoDataWithString("foo", 0); |
| 3411 | } else { |
| 3412 | SendStreamDataToPeer( |
| 3413 | QuicUtils::GetCryptoStreamId(connection_.transport_version()), "foo", 0, |
| 3414 | NO_FIN, &last_packet); |
| 3415 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3416 | // Verify ack is bundled with outging packet. |
| 3417 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 3418 | |
| 3419 | EXPECT_CALL(visitor_, OnCanWrite()) |
| 3420 | .WillOnce(DoAll(IgnoreResult(InvokeWithoutArgs( |
| 3421 | &connection_, &TestConnection::SendStreamData3)), |
| 3422 | IgnoreResult(InvokeWithoutArgs( |
| 3423 | &connection_, &TestConnection::SendStreamData5)))); |
| 3424 | |
| 3425 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3426 | |
| 3427 | // Process a data packet to cause the visitor's OnCanWrite to be invoked. |
| 3428 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 3429 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 3430 | std::make_unique<TaggingEncrypter>(0x01)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 3431 | SetDecrypter(ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 3432 | std::make_unique<StrictTaggingDecrypter>(0x01)); |
nharper | 2c9f02a | 2019-05-08 10:25:50 -0700 | [diff] [blame] | 3433 | ProcessDataPacket(2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3434 | |
| 3435 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3436 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3437 | |
| 3438 | // Parse the last packet and ensure it's an ack and two stream frames from |
| 3439 | // two different streams. |
| 3440 | if (GetParam().no_stop_waiting) { |
| 3441 | EXPECT_EQ(3u, writer_->frame_count()); |
| 3442 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 3443 | } else { |
| 3444 | EXPECT_EQ(4u, writer_->frame_count()); |
| 3445 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 3446 | } |
| 3447 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 3448 | ASSERT_EQ(2u, writer_->stream_frames().size()); |
| 3449 | EXPECT_EQ(GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 3450 | writer_->stream_frames()[0]->stream_id); |
| 3451 | EXPECT_EQ(GetNthClientInitiatedStreamId(2, connection_.transport_version()), |
| 3452 | writer_->stream_frames()[1]->stream_id); |
| 3453 | } |
| 3454 | |
| 3455 | TEST_P(QuicConnectionTest, FramePackingSendv) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3456 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3457 | // Send data in 1 packet by writing multiple blocks in a single iovector |
| 3458 | // using writev. |
| 3459 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 3460 | |
| 3461 | char data[] = "ABCDEF"; |
| 3462 | struct iovec iov[2]; |
| 3463 | iov[0].iov_base = data; |
| 3464 | iov[0].iov_len = 4; |
| 3465 | iov[1].iov_base = data + 4; |
| 3466 | iov[1].iov_len = 2; |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3467 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 3468 | connection_.transport_version(), Perspective::IS_CLIENT); |
| 3469 | connection_.SaveAndSendStreamData(stream_id, iov, 2, 6, 0, NO_FIN); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3470 | |
| 3471 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3472 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3473 | |
| 3474 | // Parse the last packet and ensure multiple iovector blocks have |
| 3475 | // been packed into a single stream frame from one stream. |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3476 | EXPECT_EQ(1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3477 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3478 | EXPECT_EQ(0u, writer_->padding_frames().size()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3479 | QuicStreamFrame* frame = writer_->stream_frames()[0].get(); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3480 | EXPECT_EQ(stream_id, frame->stream_id); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 3481 | EXPECT_EQ("ABCDEF", |
| 3482 | quiche::QuicheStringPiece(frame->data_buffer, frame->data_length)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3483 | } |
| 3484 | |
| 3485 | TEST_P(QuicConnectionTest, FramePackingSendvQueued) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3486 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3487 | // Try to send two stream frames in 1 packet by using writev. |
| 3488 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 3489 | |
| 3490 | BlockOnNextWrite(); |
| 3491 | char data[] = "ABCDEF"; |
| 3492 | struct iovec iov[2]; |
| 3493 | iov[0].iov_base = data; |
| 3494 | iov[0].iov_len = 4; |
| 3495 | iov[1].iov_base = data + 4; |
| 3496 | iov[1].iov_len = 2; |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3497 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 3498 | connection_.transport_version(), Perspective::IS_CLIENT); |
| 3499 | connection_.SaveAndSendStreamData(stream_id, iov, 2, 6, 0, NO_FIN); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3500 | |
| 3501 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 3502 | EXPECT_TRUE(connection_.HasQueuedData()); |
| 3503 | |
| 3504 | // Unblock the writes and actually send. |
| 3505 | writer_->SetWritable(); |
| 3506 | connection_.OnCanWrite(); |
| 3507 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3508 | |
| 3509 | // Parse the last packet and ensure it's one stream frame from one stream. |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3510 | EXPECT_EQ(1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3511 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3512 | EXPECT_EQ(0u, writer_->padding_frames().size()); |
| 3513 | EXPECT_EQ(stream_id, writer_->stream_frames()[0]->stream_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | TEST_P(QuicConnectionTest, SendingZeroBytes) { |
| 3517 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3518 | // Send a zero byte write with a fin using writev. |
| 3519 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3520 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 3521 | connection_.transport_version(), Perspective::IS_CLIENT); |
| 3522 | connection_.SaveAndSendStreamData(stream_id, nullptr, 0, 0, 0, FIN); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3523 | |
| 3524 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3525 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3526 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 3527 | // Padding frames are added by v99 to ensure a minimum packet size. |
| 3528 | size_t extra_padding_frames = 0; |
| 3529 | if (GetParam().version.HasHeaderProtection()) { |
| 3530 | extra_padding_frames = 1; |
| 3531 | } |
| 3532 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3533 | // Parse the last packet and ensure it's one stream frame from one stream. |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 3534 | EXPECT_EQ(1u + extra_padding_frames, writer_->frame_count()); |
| 3535 | EXPECT_EQ(extra_padding_frames, writer_->padding_frames().size()); |
| 3536 | ASSERT_EQ(1u, writer_->stream_frames().size()); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3537 | EXPECT_EQ(stream_id, writer_->stream_frames()[0]->stream_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3538 | EXPECT_TRUE(writer_->stream_frames()[0]->fin); |
| 3539 | } |
| 3540 | |
| 3541 | TEST_P(QuicConnectionTest, LargeSendWithPendingAck) { |
| 3542 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 3543 | // Set the ack alarm by processing a ping frame. |
| 3544 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3545 | |
| 3546 | // Processs a PING frame. |
| 3547 | ProcessFramePacket(QuicFrame(QuicPingFrame())); |
| 3548 | // Ensure that this has caused the ACK alarm to be set. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 3549 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3550 | |
| 3551 | // Send data and ensure the ack is bundled. |
| 3552 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(8); |
| 3553 | size_t len = 10000; |
| 3554 | std::unique_ptr<char[]> data_array(new char[len]); |
| 3555 | memset(data_array.get(), '?', len); |
| 3556 | struct iovec iov; |
| 3557 | iov.iov_base = data_array.get(); |
| 3558 | iov.iov_len = len; |
| 3559 | QuicConsumedData consumed = connection_.SaveAndSendStreamData( |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 3560 | GetNthClientInitiatedStreamId(0, connection_.transport_version()), &iov, |
| 3561 | 1, len, 0, FIN); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3562 | EXPECT_EQ(len, consumed.bytes_consumed); |
| 3563 | EXPECT_TRUE(consumed.fin_consumed); |
| 3564 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3565 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 3566 | |
| 3567 | // Parse the last packet and ensure it's one stream frame with a fin. |
| 3568 | EXPECT_EQ(1u, writer_->frame_count()); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 3569 | ASSERT_EQ(1u, writer_->stream_frames().size()); |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 3570 | EXPECT_EQ(GetNthClientInitiatedStreamId(0, connection_.transport_version()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3571 | writer_->stream_frames()[0]->stream_id); |
| 3572 | EXPECT_TRUE(writer_->stream_frames()[0]->fin); |
| 3573 | // Ensure the ack alarm was cancelled when the ack was sent. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 3574 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | TEST_P(QuicConnectionTest, OnCanWrite) { |
| 3578 | // Visitor's OnCanWrite will send data, but will have more pending writes. |
| 3579 | EXPECT_CALL(visitor_, OnCanWrite()) |
| 3580 | .WillOnce(DoAll(IgnoreResult(InvokeWithoutArgs( |
| 3581 | &connection_, &TestConnection::SendStreamData3)), |
| 3582 | IgnoreResult(InvokeWithoutArgs( |
| 3583 | &connection_, &TestConnection::SendStreamData5)))); |
| 3584 | { |
| 3585 | InSequence seq; |
| 3586 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillOnce(Return(true)); |
| 3587 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()) |
| 3588 | .WillRepeatedly(Return(false)); |
| 3589 | } |
| 3590 | |
| 3591 | EXPECT_CALL(*send_algorithm_, CanSend(_)) |
| 3592 | .WillRepeatedly(testing::Return(true)); |
| 3593 | |
| 3594 | connection_.OnCanWrite(); |
| 3595 | |
| 3596 | // Parse the last packet and ensure it's the two stream frames from |
| 3597 | // two different streams. |
| 3598 | EXPECT_EQ(2u, writer_->frame_count()); |
| 3599 | EXPECT_EQ(2u, writer_->stream_frames().size()); |
| 3600 | EXPECT_EQ(GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 3601 | writer_->stream_frames()[0]->stream_id); |
| 3602 | EXPECT_EQ(GetNthClientInitiatedStreamId(2, connection_.transport_version()), |
| 3603 | writer_->stream_frames()[1]->stream_id); |
| 3604 | } |
| 3605 | |
| 3606 | TEST_P(QuicConnectionTest, RetransmitOnNack) { |
| 3607 | QuicPacketNumber last_packet; |
| 3608 | QuicByteCount second_packet_size; |
| 3609 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, &last_packet); // Packet 1 |
| 3610 | second_packet_size = |
| 3611 | SendStreamDataToPeer(3, "foos", 3, NO_FIN, &last_packet); // Packet 2 |
| 3612 | SendStreamDataToPeer(3, "fooos", 7, NO_FIN, &last_packet); // Packet 3 |
| 3613 | |
| 3614 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3615 | |
| 3616 | // Don't lose a packet on an ack, and nothing is retransmitted. |
| 3617 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3618 | QuicAckFrame ack_one = InitAckFrame(1); |
| 3619 | ProcessAckPacket(&ack_one); |
| 3620 | |
| 3621 | // Lose a packet and ensure it triggers retransmission. |
| 3622 | QuicAckFrame nack_two = ConstructAckFrame(3, 2); |
| 3623 | LostPacketVector lost_packets; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 3624 | lost_packets.push_back( |
| 3625 | LostPacket(QuicPacketNumber(2), kMaxOutgoingPacketSize)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3626 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
wub | aa51f0e | 2020-05-12 15:08:16 -0700 | [diff] [blame] | 3627 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 3628 | Return(LossDetectionInterface::DetectionStats()))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3629 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3630 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3631 | EXPECT_FALSE(QuicPacketCreatorPeer::SendVersionInPacket(creator_)); |
| 3632 | ProcessAckPacket(&nack_two); |
| 3633 | } |
| 3634 | |
| 3635 | TEST_P(QuicConnectionTest, DoNotSendQueuedPacketForResetStream) { |
| 3636 | // Block the connection to queue the packet. |
| 3637 | BlockOnNextWrite(); |
| 3638 | |
| 3639 | QuicStreamId stream_id = 2; |
| 3640 | connection_.SendStreamDataWithString(stream_id, "foo", 0, NO_FIN); |
| 3641 | |
| 3642 | // Now that there is a queued packet, reset the stream. |
| 3643 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 3644 | |
| 3645 | // Unblock the connection and verify that only the RST_STREAM is sent. |
| 3646 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3647 | writer_->SetWritable(); |
| 3648 | connection_.OnCanWrite(); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 3649 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 3650 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3651 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
| 3652 | } |
| 3653 | |
| 3654 | TEST_P(QuicConnectionTest, SendQueuedPacketForQuicRstStreamNoError) { |
| 3655 | // Block the connection to queue the packet. |
| 3656 | BlockOnNextWrite(); |
| 3657 | |
| 3658 | QuicStreamId stream_id = 2; |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 3659 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3660 | connection_.SendStreamDataWithString(stream_id, "foo", 0, NO_FIN); |
| 3661 | |
| 3662 | // Now that there is a queued packet, reset the stream. |
| 3663 | SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 3); |
| 3664 | |
| 3665 | // Unblock the connection and verify that the RST_STREAM is sent and the data |
| 3666 | // packet is sent. |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 3667 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3668 | writer_->SetWritable(); |
| 3669 | connection_.OnCanWrite(); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 3670 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 3671 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3672 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
| 3673 | } |
| 3674 | |
| 3675 | TEST_P(QuicConnectionTest, DoNotRetransmitForResetStreamOnNack) { |
| 3676 | QuicStreamId stream_id = 2; |
| 3677 | QuicPacketNumber last_packet; |
| 3678 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 3679 | SendStreamDataToPeer(stream_id, "foos", 3, NO_FIN, &last_packet); |
| 3680 | SendStreamDataToPeer(stream_id, "fooos", 7, NO_FIN, &last_packet); |
| 3681 | |
| 3682 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3683 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 12); |
| 3684 | |
| 3685 | // Lose a packet and ensure it does not trigger retransmission. |
| 3686 | QuicAckFrame nack_two = ConstructAckFrame(last_packet, last_packet - 1); |
| 3687 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3688 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 3689 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3690 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 3691 | ProcessAckPacket(&nack_two); |
| 3692 | } |
| 3693 | |
| 3694 | TEST_P(QuicConnectionTest, RetransmitForQuicRstStreamNoErrorOnNack) { |
| 3695 | QuicStreamId stream_id = 2; |
| 3696 | QuicPacketNumber last_packet; |
| 3697 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 3698 | SendStreamDataToPeer(stream_id, "foos", 3, NO_FIN, &last_packet); |
| 3699 | SendStreamDataToPeer(stream_id, "fooos", 7, NO_FIN, &last_packet); |
| 3700 | |
| 3701 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3702 | SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 12); |
| 3703 | |
| 3704 | // Lose a packet, ensure it triggers retransmission. |
| 3705 | QuicAckFrame nack_two = ConstructAckFrame(last_packet, last_packet - 1); |
| 3706 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3707 | LostPacketVector lost_packets; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 3708 | lost_packets.push_back(LostPacket(last_packet - 1, kMaxOutgoingPacketSize)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3709 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
wub | aa51f0e | 2020-05-12 15:08:16 -0700 | [diff] [blame] | 3710 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 3711 | Return(LossDetectionInterface::DetectionStats()))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3712 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3713 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
| 3714 | ProcessAckPacket(&nack_two); |
| 3715 | } |
| 3716 | |
| 3717 | TEST_P(QuicConnectionTest, DoNotRetransmitForResetStreamOnRTO) { |
| 3718 | QuicStreamId stream_id = 2; |
| 3719 | QuicPacketNumber last_packet; |
| 3720 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 3721 | |
| 3722 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3723 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 3724 | |
| 3725 | // Fire the RTO and verify that the RST_STREAM is resent, not stream data. |
| 3726 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3727 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 3728 | connection_.GetRetransmissionAlarm()->Fire(); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 3729 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 3730 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3731 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
| 3732 | EXPECT_EQ(stream_id, writer_->rst_stream_frames().front().stream_id); |
| 3733 | } |
| 3734 | |
| 3735 | // Ensure that if the only data in flight is non-retransmittable, the |
| 3736 | // retransmission alarm is not set. |
| 3737 | TEST_P(QuicConnectionTest, CancelRetransmissionAlarmAfterResetStream) { |
| 3738 | QuicStreamId stream_id = 2; |
| 3739 | QuicPacketNumber last_data_packet; |
| 3740 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_data_packet); |
| 3741 | |
| 3742 | // Cancel the stream. |
| 3743 | const QuicPacketNumber rst_packet = last_data_packet + 1; |
| 3744 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, rst_packet, _, _)).Times(1); |
| 3745 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 3746 | |
| 3747 | // Ack the RST_STREAM frame (since it's retransmittable), but not the data |
| 3748 | // packet, which is no longer retransmittable since the stream was cancelled. |
| 3749 | QuicAckFrame nack_stream_data = |
| 3750 | ConstructAckFrame(rst_packet, last_data_packet); |
| 3751 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3752 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3753 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 3754 | ProcessAckPacket(&nack_stream_data); |
| 3755 | |
| 3756 | // Ensure that the data is still in flight, but the retransmission alarm is no |
| 3757 | // longer set. |
ianswett | 9f459cb | 2019-04-21 06:39:59 -0700 | [diff] [blame] | 3758 | EXPECT_GT(manager_->GetBytesInFlight(), 0u); |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 3759 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | TEST_P(QuicConnectionTest, RetransmitForQuicRstStreamNoErrorOnRTO) { |
| 3763 | connection_.SetMaxTailLossProbes(0); |
| 3764 | |
| 3765 | QuicStreamId stream_id = 2; |
| 3766 | QuicPacketNumber last_packet; |
| 3767 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 3768 | |
| 3769 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3770 | SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 3); |
| 3771 | |
| 3772 | // Fire the RTO and verify that the RST_STREAM is resent, the stream data |
| 3773 | // is sent. |
fayang | 1c2d1ab | 2020-03-11 12:08:41 -0700 | [diff] [blame] | 3774 | const size_t num_retransmissions = |
| 3775 | connection_.SupportsMultiplePacketNumberSpaces() ? 1 : 2; |
| 3776 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 3777 | .Times(AtLeast(num_retransmissions)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3778 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 3779 | connection_.GetRetransmissionAlarm()->Fire(); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 3780 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 3781 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
fayang | 1c2d1ab | 2020-03-11 12:08:41 -0700 | [diff] [blame] | 3782 | if (num_retransmissions == 2) { |
| 3783 | ASSERT_EQ(1u, writer_->rst_stream_frames().size()); |
| 3784 | EXPECT_EQ(stream_id, writer_->rst_stream_frames().front().stream_id); |
| 3785 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | TEST_P(QuicConnectionTest, DoNotSendPendingRetransmissionForResetStream) { |
| 3789 | QuicStreamId stream_id = 2; |
| 3790 | QuicPacketNumber last_packet; |
| 3791 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 3792 | SendStreamDataToPeer(stream_id, "foos", 3, NO_FIN, &last_packet); |
| 3793 | BlockOnNextWrite(); |
| 3794 | connection_.SendStreamDataWithString(stream_id, "fooos", 7, NO_FIN); |
| 3795 | |
| 3796 | // Lose a packet which will trigger a pending retransmission. |
| 3797 | QuicAckFrame ack = ConstructAckFrame(last_packet, last_packet - 1); |
| 3798 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3799 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 3800 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3801 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 3802 | ProcessAckPacket(&ack); |
| 3803 | |
| 3804 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 12); |
| 3805 | |
| 3806 | // Unblock the connection and verify that the RST_STREAM is sent but not the |
| 3807 | // second data packet nor a retransmit. |
| 3808 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3809 | writer_->SetWritable(); |
| 3810 | connection_.OnCanWrite(); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 3811 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 3812 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 3813 | ASSERT_EQ(1u, writer_->rst_stream_frames().size()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3814 | EXPECT_EQ(stream_id, writer_->rst_stream_frames().front().stream_id); |
| 3815 | } |
| 3816 | |
| 3817 | TEST_P(QuicConnectionTest, SendPendingRetransmissionForQuicRstStreamNoError) { |
| 3818 | QuicStreamId stream_id = 2; |
| 3819 | QuicPacketNumber last_packet; |
| 3820 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 3821 | SendStreamDataToPeer(stream_id, "foos", 3, NO_FIN, &last_packet); |
| 3822 | BlockOnNextWrite(); |
| 3823 | connection_.SendStreamDataWithString(stream_id, "fooos", 7, NO_FIN); |
| 3824 | |
| 3825 | // Lose a packet which will trigger a pending retransmission. |
| 3826 | QuicAckFrame ack = ConstructAckFrame(last_packet, last_packet - 1); |
| 3827 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3828 | LostPacketVector lost_packets; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 3829 | lost_packets.push_back(LostPacket(last_packet - 1, kMaxOutgoingPacketSize)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3830 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
wub | aa51f0e | 2020-05-12 15:08:16 -0700 | [diff] [blame] | 3831 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 3832 | Return(LossDetectionInterface::DetectionStats()))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3833 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3834 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 3835 | ProcessAckPacket(&ack); |
| 3836 | |
| 3837 | SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 12); |
| 3838 | |
| 3839 | // Unblock the connection and verify that the RST_STREAM is sent and the |
| 3840 | // second data packet or a retransmit is sent. |
| 3841 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(2)); |
| 3842 | writer_->SetWritable(); |
| 3843 | connection_.OnCanWrite(); |
| 3844 | // The RST_STREAM_FRAME is sent after queued packets and pending |
| 3845 | // retransmission. |
| 3846 | connection_.SendControlFrame(QuicFrame( |
| 3847 | new QuicRstStreamFrame(1, stream_id, QUIC_STREAM_NO_ERROR, 14))); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 3848 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 3849 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3850 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
| 3851 | } |
| 3852 | |
| 3853 | TEST_P(QuicConnectionTest, RetransmitAckedPacket) { |
| 3854 | QuicPacketNumber last_packet; |
| 3855 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); // Packet 1 |
| 3856 | SendStreamDataToPeer(1, "foos", 3, NO_FIN, &last_packet); // Packet 2 |
| 3857 | SendStreamDataToPeer(1, "fooos", 7, NO_FIN, &last_packet); // Packet 3 |
| 3858 | |
| 3859 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3860 | |
| 3861 | // Instigate a loss with an ack. |
| 3862 | QuicAckFrame nack_two = ConstructAckFrame(3, 2); |
| 3863 | // The first nack should trigger a fast retransmission, but we'll be |
| 3864 | // write blocked, so the packet will be queued. |
| 3865 | BlockOnNextWrite(); |
| 3866 | |
| 3867 | LostPacketVector lost_packets; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 3868 | lost_packets.push_back( |
| 3869 | LostPacket(QuicPacketNumber(2), kMaxOutgoingPacketSize)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3870 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
wub | aa51f0e | 2020-05-12 15:08:16 -0700 | [diff] [blame] | 3871 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 3872 | Return(LossDetectionInterface::DetectionStats()))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3873 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 3874 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(4), _, _)) |
| 3875 | .Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3876 | ProcessAckPacket(&nack_two); |
| 3877 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 3878 | |
| 3879 | // Now, ack the previous transmission. |
| 3880 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 3881 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(false, _, _, _, _)); |
| 3882 | QuicAckFrame ack_all = InitAckFrame(3); |
| 3883 | ProcessAckPacket(&ack_all); |
| 3884 | |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 3885 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(4), _, _)) |
| 3886 | .Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3887 | |
| 3888 | writer_->SetWritable(); |
| 3889 | connection_.OnCanWrite(); |
| 3890 | |
| 3891 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 3892 | // We do not store retransmittable frames of this retransmission. |
| 3893 | EXPECT_FALSE(QuicConnectionPeer::HasRetransmittableFrames(&connection_, 4)); |
| 3894 | } |
| 3895 | |
| 3896 | TEST_P(QuicConnectionTest, RetransmitNackedLargestObserved) { |
| 3897 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3898 | QuicPacketNumber original, second; |
| 3899 | |
| 3900 | QuicByteCount packet_size = |
| 3901 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, &original); // 1st packet. |
| 3902 | SendStreamDataToPeer(3, "bar", 3, NO_FIN, &second); // 2nd packet. |
| 3903 | |
| 3904 | QuicAckFrame frame = InitAckFrame({{second, second + 1}}); |
| 3905 | // The first nack should retransmit the largest observed packet. |
| 3906 | LostPacketVector lost_packets; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 3907 | lost_packets.push_back(LostPacket(original, kMaxOutgoingPacketSize)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3908 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
wub | aa51f0e | 2020-05-12 15:08:16 -0700 | [diff] [blame] | 3909 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 3910 | Return(LossDetectionInterface::DetectionStats()))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3911 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 3912 | // Packet 1 is short header for IETF QUIC because the encryption level |
| 3913 | // switched to ENCRYPTION_FORWARD_SECURE in SendStreamDataToPeer. |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 3914 | EXPECT_CALL(*send_algorithm_, |
| 3915 | OnPacketSent(_, _, _, |
| 3916 | VersionHasIetfInvariantHeader( |
| 3917 | GetParam().version.transport_version) |
| 3918 | ? packet_size |
| 3919 | : packet_size - kQuicVersionSize, |
| 3920 | _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3921 | ProcessAckPacket(&frame); |
| 3922 | } |
| 3923 | |
| 3924 | TEST_P(QuicConnectionTest, QueueAfterTwoRTOs) { |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 3925 | if (connection_.PtoEnabled()) { |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 3926 | return; |
| 3927 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3928 | connection_.SetMaxTailLossProbes(0); |
| 3929 | |
| 3930 | for (int i = 0; i < 10; ++i) { |
| 3931 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3932 | connection_.SendStreamDataWithString(3, "foo", i * 3, NO_FIN); |
| 3933 | } |
| 3934 | |
| 3935 | // Block the writer and ensure they're queued. |
| 3936 | BlockOnNextWrite(); |
| 3937 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 3938 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3939 | connection_.GetRetransmissionAlarm()->Fire(); |
| 3940 | EXPECT_TRUE(connection_.HasQueuedData()); |
| 3941 | |
| 3942 | // Unblock the writer. |
| 3943 | writer_->SetWritable(); |
| 3944 | clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds( |
| 3945 | 2 * DefaultRetransmissionTime().ToMicroseconds())); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 3946 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3947 | connection_.GetRetransmissionAlarm()->Fire(); |
| 3948 | connection_.OnCanWrite(); |
| 3949 | } |
| 3950 | |
| 3951 | TEST_P(QuicConnectionTest, WriteBlockedBufferedThenSent) { |
| 3952 | BlockOnNextWrite(); |
| 3953 | writer_->set_is_write_blocked_data_buffered(true); |
| 3954 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 3955 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 3956 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 3957 | |
| 3958 | writer_->SetWritable(); |
| 3959 | connection_.OnCanWrite(); |
| 3960 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 3961 | } |
| 3962 | |
| 3963 | TEST_P(QuicConnectionTest, WriteBlockedThenSent) { |
| 3964 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 3965 | BlockOnNextWrite(); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 3966 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3967 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 3968 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3969 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 3970 | |
| 3971 | // The second packet should also be queued, in order to ensure packets are |
| 3972 | // never sent out of order. |
| 3973 | writer_->SetWritable(); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 3974 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3975 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 3976 | EXPECT_EQ(2u, connection_.NumQueuedPackets()); |
| 3977 | |
| 3978 | // Now both are sent in order when we unblock. |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 3979 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3980 | connection_.OnCanWrite(); |
| 3981 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
fayang | 2ce6608 | 2019-10-02 06:29:04 -0700 | [diff] [blame] | 3982 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3983 | } |
| 3984 | |
| 3985 | TEST_P(QuicConnectionTest, RetransmitWriteBlockedAckedOriginalThenSent) { |
| 3986 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 3987 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 3988 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 3989 | |
| 3990 | BlockOnNextWrite(); |
| 3991 | writer_->set_is_write_blocked_data_buffered(true); |
| 3992 | // Simulate the retransmission alarm firing. |
| 3993 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 3994 | connection_.GetRetransmissionAlarm()->Fire(); |
| 3995 | |
| 3996 | // Ack the sent packet before the callback returns, which happens in |
| 3997 | // rare circumstances with write blocked sockets. |
| 3998 | QuicAckFrame ack = InitAckFrame(1); |
| 3999 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4000 | ProcessAckPacket(&ack); |
| 4001 | |
| 4002 | writer_->SetWritable(); |
| 4003 | connection_.OnCanWrite(); |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 4004 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
fayang | 5d01198 | 2020-05-13 14:14:38 -0700 | [diff] [blame] | 4005 | uint64_t retransmission = connection_.SupportsMultiplePacketNumberSpaces() && |
| 4006 | !GetQuicReloadableFlag(quic_default_on_pto) |
| 4007 | ? 3 |
| 4008 | : 2; |
fayang | 1c2d1ab | 2020-03-11 12:08:41 -0700 | [diff] [blame] | 4009 | EXPECT_FALSE(QuicConnectionPeer::HasRetransmittableFrames(&connection_, |
| 4010 | retransmission)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4011 | } |
| 4012 | |
| 4013 | TEST_P(QuicConnectionTest, AlarmsWhenWriteBlocked) { |
| 4014 | // Block the connection. |
| 4015 | BlockOnNextWrite(); |
| 4016 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 4017 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 4018 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 4019 | |
| 4020 | // Set the send alarm. Fire the alarm and ensure it doesn't attempt to write. |
| 4021 | connection_.GetSendAlarm()->Set(clock_.ApproximateNow()); |
| 4022 | connection_.GetSendAlarm()->Fire(); |
| 4023 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 4024 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 4025 | } |
| 4026 | |
| 4027 | TEST_P(QuicConnectionTest, NoSendAlarmAfterProcessPacketWhenWriteBlocked) { |
| 4028 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4029 | |
| 4030 | // Block the connection. |
| 4031 | BlockOnNextWrite(); |
| 4032 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 4033 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 4034 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 4035 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 4036 | |
| 4037 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4038 | // Process packet number 1. Can not call ProcessPacket or ProcessDataPacket |
| 4039 | // here, because they will fire the alarm after QuicConnection::ProcessPacket |
| 4040 | // is returned. |
| 4041 | const uint64_t received_packet_num = 1; |
| 4042 | const bool has_stop_waiting = false; |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 4043 | const EncryptionLevel level = ENCRYPTION_FORWARD_SECURE; |
| 4044 | std::unique_ptr<QuicPacket> packet( |
| 4045 | ConstructDataPacket(received_packet_num, has_stop_waiting, level)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 4046 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4047 | size_t encrypted_length = |
| 4048 | peer_framer_.EncryptPayload(level, QuicPacketNumber(received_packet_num), |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 4049 | *packet, buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4050 | connection_.ProcessUdpPacket( |
| 4051 | kSelfAddress, kPeerAddress, |
| 4052 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 4053 | |
| 4054 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 4055 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 4056 | } |
| 4057 | |
| 4058 | TEST_P(QuicConnectionTest, AddToWriteBlockedListIfWriterBlockedWhenProcessing) { |
| 4059 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4060 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); |
| 4061 | |
| 4062 | // Simulate the case where a shared writer gets blocked by another connection. |
| 4063 | writer_->SetWriteBlocked(); |
| 4064 | |
| 4065 | // Process an ACK, make sure the connection calls visitor_->OnWriteBlocked(). |
| 4066 | QuicAckFrame ack1 = InitAckFrame(1); |
| 4067 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _)); |
| 4068 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(1); |
| 4069 | ProcessAckPacket(1, &ack1); |
| 4070 | } |
| 4071 | |
| 4072 | TEST_P(QuicConnectionTest, DoNotAddToWriteBlockedListAfterDisconnect) { |
| 4073 | writer_->SetBatchMode(true); |
| 4074 | EXPECT_TRUE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4075 | // Have to explicitly grab the OnConnectionClosed frame and check |
| 4076 | // its parameters because this is a silent connection close and the |
| 4077 | // frame is not also transmitted to the peer. |
| 4078 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 4079 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4080 | |
| 4081 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(0); |
| 4082 | |
| 4083 | { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 4084 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4085 | connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason", |
| 4086 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 4087 | |
| 4088 | EXPECT_FALSE(connection_.connected()); |
| 4089 | writer_->SetWriteBlocked(); |
| 4090 | } |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4091 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 4092 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 4093 | IsError(QUIC_PEER_GOING_AWAY)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | TEST_P(QuicConnectionTest, AddToWriteBlockedListIfBlockedOnFlushPackets) { |
| 4097 | writer_->SetBatchMode(true); |
| 4098 | writer_->BlockOnNextFlush(); |
| 4099 | |
| 4100 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(1); |
| 4101 | { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 4102 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4103 | // flusher's destructor will call connection_.FlushPackets, which should add |
| 4104 | // the connection to the write blocked list. |
| 4105 | } |
| 4106 | } |
| 4107 | |
| 4108 | TEST_P(QuicConnectionTest, NoLimitPacketsPerNack) { |
| 4109 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4110 | int offset = 0; |
| 4111 | // Send packets 1 to 15. |
| 4112 | for (int i = 0; i < 15; ++i) { |
| 4113 | SendStreamDataToPeer(1, "foo", offset, NO_FIN, nullptr); |
| 4114 | offset += 3; |
| 4115 | } |
| 4116 | |
| 4117 | // Ack 15, nack 1-14. |
| 4118 | |
| 4119 | QuicAckFrame nack = |
| 4120 | InitAckFrame({{QuicPacketNumber(15), QuicPacketNumber(16)}}); |
| 4121 | |
| 4122 | // 14 packets have been NACK'd and lost. |
| 4123 | LostPacketVector lost_packets; |
| 4124 | for (int i = 1; i < 15; ++i) { |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 4125 | lost_packets.push_back( |
| 4126 | LostPacket(QuicPacketNumber(i), kMaxOutgoingPacketSize)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4127 | } |
| 4128 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
wub | aa51f0e | 2020-05-12 15:08:16 -0700 | [diff] [blame] | 4129 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 4130 | Return(LossDetectionInterface::DetectionStats()))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4131 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 4132 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4133 | ProcessAckPacket(&nack); |
| 4134 | } |
| 4135 | |
| 4136 | // Test sending multiple acks from the connection to the session. |
| 4137 | TEST_P(QuicConnectionTest, MultipleAcks) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 4138 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 4139 | return; |
| 4140 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4141 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4142 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4143 | ProcessDataPacket(1); |
| 4144 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 2); |
| 4145 | QuicPacketNumber last_packet; |
| 4146 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); // Packet 1 |
| 4147 | EXPECT_EQ(QuicPacketNumber(1u), last_packet); |
| 4148 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, &last_packet); // Packet 2 |
| 4149 | EXPECT_EQ(QuicPacketNumber(2u), last_packet); |
| 4150 | SendAckPacketToPeer(); // Packet 3 |
| 4151 | SendStreamDataToPeer(5, "foo", 0, NO_FIN, &last_packet); // Packet 4 |
| 4152 | EXPECT_EQ(QuicPacketNumber(4u), last_packet); |
| 4153 | SendStreamDataToPeer(1, "foo", 3, NO_FIN, &last_packet); // Packet 5 |
| 4154 | EXPECT_EQ(QuicPacketNumber(5u), last_packet); |
| 4155 | SendStreamDataToPeer(3, "foo", 3, NO_FIN, &last_packet); // Packet 6 |
| 4156 | EXPECT_EQ(QuicPacketNumber(6u), last_packet); |
| 4157 | |
| 4158 | // Client will ack packets 1, 2, [!3], 4, 5. |
| 4159 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4160 | QuicAckFrame frame1 = ConstructAckFrame(5, 3); |
| 4161 | ProcessAckPacket(&frame1); |
| 4162 | |
| 4163 | // Now the client implicitly acks 3, and explicitly acks 6. |
| 4164 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4165 | QuicAckFrame frame2 = InitAckFrame(6); |
| 4166 | ProcessAckPacket(&frame2); |
| 4167 | } |
| 4168 | |
| 4169 | TEST_P(QuicConnectionTest, DontLatchUnackedPacket) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 4170 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 4171 | return; |
| 4172 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4173 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4174 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4175 | ProcessDataPacket(1); |
| 4176 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 2); |
| 4177 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, nullptr); // Packet 1; |
| 4178 | // From now on, we send acks, so the send algorithm won't mark them pending. |
| 4179 | SendAckPacketToPeer(); // Packet 2 |
| 4180 | |
| 4181 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4182 | QuicAckFrame frame = InitAckFrame(1); |
| 4183 | ProcessAckPacket(&frame); |
| 4184 | |
| 4185 | // Verify that our internal state has least-unacked as 2, because we're still |
| 4186 | // waiting for a potential ack for 2. |
| 4187 | |
| 4188 | EXPECT_EQ(QuicPacketNumber(2u), stop_waiting()->least_unacked); |
| 4189 | |
| 4190 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4191 | frame = InitAckFrame(2); |
| 4192 | ProcessAckPacket(&frame); |
| 4193 | EXPECT_EQ(QuicPacketNumber(3u), stop_waiting()->least_unacked); |
| 4194 | |
| 4195 | // When we send an ack, we make sure our least-unacked makes sense. In this |
| 4196 | // case since we're not waiting on an ack for 2 and all packets are acked, we |
| 4197 | // set it to 3. |
| 4198 | SendAckPacketToPeer(); // Packet 3 |
| 4199 | // Least_unacked remains at 3 until another ack is received. |
| 4200 | EXPECT_EQ(QuicPacketNumber(3u), stop_waiting()->least_unacked); |
| 4201 | if (GetParam().no_stop_waiting) { |
| 4202 | // Expect no stop waiting frame is sent. |
| 4203 | EXPECT_FALSE(least_unacked().IsInitialized()); |
| 4204 | } else { |
| 4205 | // Check that the outgoing ack had its packet number as least_unacked. |
| 4206 | EXPECT_EQ(QuicPacketNumber(3u), least_unacked()); |
| 4207 | } |
| 4208 | |
| 4209 | // Ack the ack, which updates the rtt and raises the least unacked. |
| 4210 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4211 | frame = InitAckFrame(3); |
| 4212 | ProcessAckPacket(&frame); |
| 4213 | |
| 4214 | SendStreamDataToPeer(1, "bar", 3, NO_FIN, nullptr); // Packet 4 |
| 4215 | EXPECT_EQ(QuicPacketNumber(4u), stop_waiting()->least_unacked); |
| 4216 | SendAckPacketToPeer(); // Packet 5 |
| 4217 | if (GetParam().no_stop_waiting) { |
| 4218 | // Expect no stop waiting frame is sent. |
| 4219 | EXPECT_FALSE(least_unacked().IsInitialized()); |
| 4220 | } else { |
| 4221 | EXPECT_EQ(QuicPacketNumber(4u), least_unacked()); |
| 4222 | } |
| 4223 | |
| 4224 | // Send two data packets at the end, and ensure if the last one is acked, |
| 4225 | // the least unacked is raised above the ack packets. |
| 4226 | SendStreamDataToPeer(1, "bar", 6, NO_FIN, nullptr); // Packet 6 |
| 4227 | SendStreamDataToPeer(1, "bar", 9, NO_FIN, nullptr); // Packet 7 |
| 4228 | |
| 4229 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4230 | frame = InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(5)}, |
| 4231 | {QuicPacketNumber(7), QuicPacketNumber(8)}}); |
| 4232 | ProcessAckPacket(&frame); |
| 4233 | |
| 4234 | EXPECT_EQ(QuicPacketNumber(6u), stop_waiting()->least_unacked); |
| 4235 | } |
| 4236 | |
| 4237 | TEST_P(QuicConnectionTest, TLP) { |
fayang | 5d01198 | 2020-05-13 14:14:38 -0700 | [diff] [blame] | 4238 | if (connection_.PtoEnabled()) { |
| 4239 | return; |
| 4240 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4241 | connection_.SetMaxTailLossProbes(1); |
| 4242 | |
| 4243 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, nullptr); |
| 4244 | EXPECT_EQ(QuicPacketNumber(1u), stop_waiting()->least_unacked); |
| 4245 | QuicTime retransmission_time = |
| 4246 | connection_.GetRetransmissionAlarm()->deadline(); |
| 4247 | EXPECT_NE(QuicTime::Zero(), retransmission_time); |
| 4248 | |
| 4249 | EXPECT_EQ(QuicPacketNumber(1u), writer_->header().packet_number); |
| 4250 | // Simulate the retransmission alarm firing and sending a tlp, |
| 4251 | // so send algorithm's OnRetransmissionTimeout is not called. |
| 4252 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
fayang | 1c2d1ab | 2020-03-11 12:08:41 -0700 | [diff] [blame] | 4253 | const QuicPacketNumber retransmission( |
| 4254 | connection_.SupportsMultiplePacketNumberSpaces() ? 3 : 2); |
| 4255 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, retransmission, _, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4256 | connection_.GetRetransmissionAlarm()->Fire(); |
fayang | 1c2d1ab | 2020-03-11 12:08:41 -0700 | [diff] [blame] | 4257 | EXPECT_EQ(retransmission, writer_->header().packet_number); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4258 | // We do not raise the high water mark yet. |
| 4259 | EXPECT_EQ(QuicPacketNumber(1u), stop_waiting()->least_unacked); |
| 4260 | } |
| 4261 | |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4262 | TEST_P(QuicConnectionTest, TailLossProbeDelayForStreamDataInTLPR) { |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 4263 | if (connection_.PtoEnabled()) { |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 4264 | return; |
| 4265 | } |
| 4266 | |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4267 | // Set TLPR from QuicConfig. |
| 4268 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4269 | QuicConfig config; |
| 4270 | QuicTagVector options; |
| 4271 | options.push_back(kTLPR); |
| 4272 | config.SetConnectionOptionsToSend(options); |
| 4273 | connection_.SetFromConfig(config); |
| 4274 | connection_.SetMaxTailLossProbes(1); |
| 4275 | |
| 4276 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, nullptr); |
| 4277 | EXPECT_EQ(QuicPacketNumber(1u), stop_waiting()->least_unacked); |
| 4278 | |
| 4279 | QuicTime retransmission_time = |
| 4280 | connection_.GetRetransmissionAlarm()->deadline(); |
| 4281 | EXPECT_NE(QuicTime::Zero(), retransmission_time); |
| 4282 | QuicTime::Delta expected_tlp_delay = |
| 4283 | 0.5 * manager_->GetRttStats()->SmoothedOrInitialRtt(); |
| 4284 | EXPECT_EQ(expected_tlp_delay, retransmission_time - clock_.Now()); |
| 4285 | |
| 4286 | EXPECT_EQ(QuicPacketNumber(1u), writer_->header().packet_number); |
| 4287 | // Simulate firing of the retransmission alarm and retransmit the packet. |
| 4288 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(2), _, _)); |
| 4289 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
| 4290 | connection_.GetRetransmissionAlarm()->Fire(); |
| 4291 | EXPECT_EQ(QuicPacketNumber(2u), writer_->header().packet_number); |
| 4292 | |
| 4293 | // We do not raise the high water mark yet. |
| 4294 | EXPECT_EQ(QuicPacketNumber(1u), stop_waiting()->least_unacked); |
| 4295 | } |
| 4296 | |
| 4297 | TEST_P(QuicConnectionTest, TailLossProbeDelayForNonStreamDataInTLPR) { |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 4298 | if (connection_.PtoEnabled()) { |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 4299 | return; |
| 4300 | } |
| 4301 | |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4302 | // Set TLPR from QuicConfig. |
| 4303 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4304 | QuicConfig config; |
| 4305 | QuicTagVector options; |
| 4306 | options.push_back(kTLPR); |
| 4307 | config.SetConnectionOptionsToSend(options); |
dschinazi | f7c6a91 | 2020-05-05 11:39:53 -0700 | [diff] [blame] | 4308 | QuicConfigPeer::SetNegotiated(&config, true); |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 4309 | if (connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 4310 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 4311 | &config, connection_.connection_id()); |
| 4312 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 4313 | &config, connection_.connection_id()); |
| 4314 | } |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4315 | connection_.SetFromConfig(config); |
| 4316 | connection_.SetMaxTailLossProbes(1); |
| 4317 | |
| 4318 | // Sets retransmittable on wire. |
| 4319 | const QuicTime::Delta retransmittable_on_wire_timeout = |
| 4320 | QuicTime::Delta::FromMilliseconds(50); |
zhongyi | 79ace16 | 2019-10-21 15:57:09 -0700 | [diff] [blame] | 4321 | connection_.set_initial_retransmittable_on_wire_timeout( |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4322 | retransmittable_on_wire_timeout); |
| 4323 | |
| 4324 | EXPECT_TRUE(connection_.connected()); |
| 4325 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 4326 | .WillRepeatedly(Return(true)); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 4327 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4328 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 4329 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 4330 | |
| 4331 | const char data[] = "data"; |
| 4332 | size_t data_size = strlen(data); |
| 4333 | QuicStreamOffset offset = 0; |
| 4334 | |
| 4335 | // Send a data packet. |
| 4336 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 4337 | offset += data_size; |
| 4338 | |
| 4339 | // Path degrading alarm should be set when there is a retransmittable packet |
| 4340 | // on the wire. |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 4341 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4342 | |
| 4343 | // Verify the path degrading delay. |
| 4344 | // First TLP with stream data. |
| 4345 | QuicTime::Delta srtt = manager_->GetRttStats()->SmoothedOrInitialRtt(); |
| 4346 | QuicTime::Delta expected_delay = 0.5 * srtt; |
| 4347 | // Add 1st RTO. |
| 4348 | QuicTime::Delta retransmission_delay = |
| 4349 | QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs); |
| 4350 | expected_delay = expected_delay + retransmission_delay; |
| 4351 | // Add 2nd RTO. |
| 4352 | expected_delay = expected_delay + retransmission_delay * 2; |
| 4353 | EXPECT_EQ(expected_delay, |
| 4354 | QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 4355 | ->GetPathDegradingDelay()); |
| 4356 | ASSERT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 4357 | |
| 4358 | // The ping alarm is set for the ping timeout, not the shorter |
| 4359 | // retransmittable_on_wire_timeout. |
| 4360 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 4361 | EXPECT_EQ(connection_.ping_timeout(), |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4362 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 4363 | |
| 4364 | // Receive an ACK for the data packet. |
| 4365 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 4366 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4367 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4368 | QuicAckFrame frame = |
| 4369 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 4370 | ProcessAckPacket(&frame); |
| 4371 | |
| 4372 | // Path degrading alarm should be cancelled as there is no more |
| 4373 | // reretransmittable packets on the wire. |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 4374 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4375 | // The ping alarm should be set to the retransmittable_on_wire_timeout. |
| 4376 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 4377 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 4378 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 4379 | |
| 4380 | // Simulate firing of the retransmittable on wire and send a PING. |
| 4381 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { SendPing(); })); |
| 4382 | clock_.AdvanceTime(retransmittable_on_wire_timeout); |
| 4383 | connection_.GetPingAlarm()->Fire(); |
| 4384 | |
| 4385 | // The retransmission alarm and the path degrading alarm should be set as |
| 4386 | // there is a retransmittable packet (PING) on the wire, |
| 4387 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 4388 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4389 | |
| 4390 | // Verify the retransmission delay. |
| 4391 | QuicTime::Delta min_rto_timeout = |
| 4392 | QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs); |
| 4393 | srtt = manager_->GetRttStats()->SmoothedOrInitialRtt(); |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 4394 | |
| 4395 | // First TLP without unacked stream data will no longer use TLPR. |
| 4396 | expected_delay = std::max(2 * srtt, 1.5 * srtt + 0.5 * min_rto_timeout); |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4397 | EXPECT_EQ(expected_delay, |
| 4398 | connection_.GetRetransmissionAlarm()->deadline() - clock_.Now()); |
| 4399 | |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 4400 | // Verify the path degrading delay = TLP delay + 1st RTO + 2nd RTO. |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4401 | // Add 1st RTO. |
| 4402 | retransmission_delay = |
| 4403 | std::max(manager_->GetRttStats()->smoothed_rtt() + |
| 4404 | 4 * manager_->GetRttStats()->mean_deviation(), |
| 4405 | min_rto_timeout); |
| 4406 | expected_delay = expected_delay + retransmission_delay; |
| 4407 | // Add 2nd RTO. |
| 4408 | expected_delay = expected_delay + retransmission_delay * 2; |
| 4409 | EXPECT_EQ(expected_delay, |
| 4410 | QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 4411 | ->GetPathDegradingDelay()); |
| 4412 | |
| 4413 | // The ping alarm is set for the ping timeout, not the shorter |
| 4414 | // retransmittable_on_wire_timeout. |
| 4415 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 4416 | EXPECT_EQ(connection_.ping_timeout(), |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4417 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 4418 | |
| 4419 | // Advance a small period of time: 5ms. And receive a retransmitted ACK. |
| 4420 | // This will update the retransmission alarm, verify the retransmission delay |
| 4421 | // is correct. |
| 4422 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 4423 | QuicAckFrame ack = InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 4424 | ProcessAckPacket(&ack); |
| 4425 | |
| 4426 | // Verify the retransmission delay. |
zhongyi | 9fa2be3 | 2019-09-10 12:39:01 -0700 | [diff] [blame] | 4427 | // First TLP without unacked stream data will no longer use TLPR. |
| 4428 | expected_delay = std::max(2 * srtt, 1.5 * srtt + 0.5 * min_rto_timeout); |
zhongyi | 1b2f783 | 2019-06-14 13:31:34 -0700 | [diff] [blame] | 4429 | expected_delay = expected_delay - QuicTime::Delta::FromMilliseconds(5); |
| 4430 | EXPECT_EQ(expected_delay, |
| 4431 | connection_.GetRetransmissionAlarm()->deadline() - clock_.Now()); |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4432 | } |
| 4433 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4434 | TEST_P(QuicConnectionTest, RTO) { |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 4435 | if (connection_.PtoEnabled()) { |
| 4436 | return; |
| 4437 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4438 | connection_.SetMaxTailLossProbes(0); |
| 4439 | |
| 4440 | QuicTime default_retransmission_time = |
| 4441 | clock_.ApproximateNow() + DefaultRetransmissionTime(); |
| 4442 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, nullptr); |
| 4443 | EXPECT_EQ(QuicPacketNumber(1u), stop_waiting()->least_unacked); |
| 4444 | |
| 4445 | EXPECT_EQ(QuicPacketNumber(1u), writer_->header().packet_number); |
| 4446 | EXPECT_EQ(default_retransmission_time, |
| 4447 | connection_.GetRetransmissionAlarm()->deadline()); |
| 4448 | // Simulate the retransmission alarm firing. |
| 4449 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 4450 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(2), _, _)); |
| 4451 | connection_.GetRetransmissionAlarm()->Fire(); |
| 4452 | EXPECT_EQ(QuicPacketNumber(2u), writer_->header().packet_number); |
| 4453 | // We do not raise the high water mark yet. |
| 4454 | EXPECT_EQ(QuicPacketNumber(1u), stop_waiting()->least_unacked); |
| 4455 | } |
| 4456 | |
fayang | a29eb24 | 2019-07-16 12:25:38 -0700 | [diff] [blame] | 4457 | // Regression test of b/133771183. |
| 4458 | TEST_P(QuicConnectionTest, RtoWithNoDataToRetransmit) { |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 4459 | if (connection_.PtoEnabled()) { |
fayang | a29eb24 | 2019-07-16 12:25:38 -0700 | [diff] [blame] | 4460 | return; |
| 4461 | } |
| 4462 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 4463 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4464 | connection_.SetMaxTailLossProbes(0); |
| 4465 | |
| 4466 | SendStreamDataToPeer(3, "foo", 0, NO_FIN, nullptr); |
| 4467 | // Connection is cwnd limited. |
| 4468 | CongestionBlockWrites(); |
| 4469 | // Stream gets reset. |
| 4470 | SendRstStream(3, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 4471 | // Simulate the retransmission alarm firing. |
| 4472 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 4473 | // RTO fires, but there is no packet to be RTOed. |
fayang | e861aee | 2019-10-16 13:40:39 -0700 | [diff] [blame] | 4474 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
fayang | a29eb24 | 2019-07-16 12:25:38 -0700 | [diff] [blame] | 4475 | connection_.GetRetransmissionAlarm()->Fire(); |
fayang | e861aee | 2019-10-16 13:40:39 -0700 | [diff] [blame] | 4476 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
fayang | a29eb24 | 2019-07-16 12:25:38 -0700 | [diff] [blame] | 4477 | |
| 4478 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(40); |
| 4479 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(20); |
fayang | e861aee | 2019-10-16 13:40:39 -0700 | [diff] [blame] | 4480 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillRepeatedly(Return(false)); |
| 4481 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(1); |
fayang | a29eb24 | 2019-07-16 12:25:38 -0700 | [diff] [blame] | 4482 | // Receives packets 1 - 40. |
| 4483 | for (size_t i = 1; i <= 40; ++i) { |
| 4484 | ProcessDataPacket(i); |
| 4485 | } |
| 4486 | } |
| 4487 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4488 | TEST_P(QuicConnectionTest, SendHandshakeMessages) { |
| 4489 | use_tagging_decrypter(); |
| 4490 | // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at |
| 4491 | // the end of the packet. We can test this to check which encrypter was used. |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 4492 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4493 | std::make_unique<TaggingEncrypter>(0x01)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4494 | |
| 4495 | // Attempt to send a handshake message and have the socket block. |
| 4496 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 4497 | BlockOnNextWrite(); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 4498 | connection_.SendCryptoDataWithString("foo", 0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4499 | // The packet should be serialized, but not queued. |
| 4500 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 4501 | |
| 4502 | // Switch to the new encrypter. |
| 4503 | connection_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4504 | std::make_unique<TaggingEncrypter>(0x02)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4505 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 4506 | |
| 4507 | // Now become writeable and flush the packets. |
| 4508 | writer_->SetWritable(); |
| 4509 | EXPECT_CALL(visitor_, OnCanWrite()); |
| 4510 | connection_.OnCanWrite(); |
| 4511 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4512 | |
| 4513 | // Verify that the handshake packet went out at the null encryption. |
| 4514 | EXPECT_EQ(0x01010101u, writer_->final_bytes_of_last_packet()); |
| 4515 | } |
| 4516 | |
| 4517 | TEST_P(QuicConnectionTest, |
| 4518 | DropRetransmitsForNullEncryptedPacketAfterForwardSecure) { |
| 4519 | use_tagging_decrypter(); |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 4520 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4521 | std::make_unique<TaggingEncrypter>(0x01)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4522 | connection_.SendCryptoStreamData(); |
| 4523 | |
| 4524 | // Simulate the retransmission alarm firing and the socket blocking. |
| 4525 | BlockOnNextWrite(); |
| 4526 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 4527 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4528 | connection_.GetRetransmissionAlarm()->Fire(); |
fayang | 2ce6608 | 2019-10-02 06:29:04 -0700 | [diff] [blame] | 4529 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4530 | |
| 4531 | // Go forward secure. |
| 4532 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4533 | std::make_unique<TaggingEncrypter>(0x02)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4534 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 4535 | notifier_.NeuterUnencryptedData(); |
| 4536 | connection_.NeuterUnencryptedPackets(); |
fayang | 2ce6608 | 2019-10-02 06:29:04 -0700 | [diff] [blame] | 4537 | connection_.OnHandshakeComplete(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4538 | |
| 4539 | EXPECT_EQ(QuicTime::Zero(), connection_.GetRetransmissionAlarm()->deadline()); |
| 4540 | // Unblock the socket and ensure that no packets are sent. |
| 4541 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 4542 | writer_->SetWritable(); |
| 4543 | connection_.OnCanWrite(); |
| 4544 | } |
| 4545 | |
| 4546 | TEST_P(QuicConnectionTest, RetransmitPacketsWithInitialEncryption) { |
| 4547 | use_tagging_decrypter(); |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 4548 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4549 | std::make_unique<TaggingEncrypter>(0x01)); |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 4550 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4551 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 4552 | connection_.SendCryptoDataWithString("foo", 0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4553 | |
| 4554 | connection_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4555 | std::make_unique<TaggingEncrypter>(0x02)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4556 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 4557 | |
| 4558 | SendStreamDataToPeer(2, "bar", 0, NO_FIN, nullptr); |
| 4559 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 4560 | |
renjietang | c008008 | 2020-05-26 18:35:19 -0700 | [diff] [blame] | 4561 | connection_.RetransmitZeroRttPackets(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | TEST_P(QuicConnectionTest, BufferNonDecryptablePackets) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 4565 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 4566 | return; |
| 4567 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4568 | // SetFromConfig is always called after construction from InitializeSession. |
| 4569 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4570 | QuicConfig config; |
| 4571 | connection_.SetFromConfig(config); |
| 4572 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4573 | use_tagging_decrypter(); |
| 4574 | |
| 4575 | const uint8_t tag = 0x07; |
| 4576 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4577 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4578 | |
| 4579 | // Process an encrypted packet which can not yet be decrypted which should |
| 4580 | // result in the packet being buffered. |
| 4581 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 4582 | |
| 4583 | // Transition to the new encryption state and process another encrypted packet |
| 4584 | // which should result in the original packet being processed. |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 4585 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4586 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4587 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 4588 | connection_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4589 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4590 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(2); |
| 4591 | ProcessDataPacketAtLevel(2, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 4592 | |
| 4593 | // Finally, process a third packet and note that we do not reprocess the |
| 4594 | // buffered packet. |
| 4595 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4596 | ProcessDataPacketAtLevel(3, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 4597 | } |
| 4598 | |
| 4599 | TEST_P(QuicConnectionTest, TestRetransmitOrder) { |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 4600 | if (connection_.PtoEnabled()) { |
| 4601 | return; |
| 4602 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4603 | connection_.SetMaxTailLossProbes(0); |
| 4604 | |
| 4605 | QuicByteCount first_packet_size; |
| 4606 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 4607 | .WillOnce(SaveArg<3>(&first_packet_size)); |
| 4608 | |
| 4609 | connection_.SendStreamDataWithString(3, "first_packet", 0, NO_FIN); |
| 4610 | QuicByteCount second_packet_size; |
| 4611 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 4612 | .WillOnce(SaveArg<3>(&second_packet_size)); |
| 4613 | connection_.SendStreamDataWithString(3, "second_packet", 12, NO_FIN); |
| 4614 | EXPECT_NE(first_packet_size, second_packet_size); |
| 4615 | // Advance the clock by huge time to make sure packets will be retransmitted. |
| 4616 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); |
| 4617 | { |
| 4618 | InSequence s; |
| 4619 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, first_packet_size, _)); |
| 4620 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, second_packet_size, _)); |
| 4621 | } |
| 4622 | connection_.GetRetransmissionAlarm()->Fire(); |
| 4623 | |
| 4624 | // Advance again and expect the packets to be sent again in the same order. |
| 4625 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(20)); |
| 4626 | { |
| 4627 | InSequence s; |
| 4628 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, first_packet_size, _)); |
| 4629 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, second_packet_size, _)); |
| 4630 | } |
| 4631 | connection_.GetRetransmissionAlarm()->Fire(); |
| 4632 | } |
| 4633 | |
| 4634 | TEST_P(QuicConnectionTest, Buffer100NonDecryptablePacketsThenKeyChange) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 4635 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 4636 | return; |
| 4637 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4638 | // SetFromConfig is always called after construction from InitializeSession. |
| 4639 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4640 | QuicConfig config; |
| 4641 | config.set_max_undecryptable_packets(100); |
| 4642 | connection_.SetFromConfig(config); |
| 4643 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4644 | use_tagging_decrypter(); |
| 4645 | |
| 4646 | const uint8_t tag = 0x07; |
| 4647 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4648 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4649 | |
| 4650 | // Process an encrypted packet which can not yet be decrypted which should |
| 4651 | // result in the packet being buffered. |
| 4652 | for (uint64_t i = 1; i <= 100; ++i) { |
| 4653 | ProcessDataPacketAtLevel(i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 4654 | } |
| 4655 | |
| 4656 | // Transition to the new encryption state and process another encrypted packet |
| 4657 | // which should result in the original packets being processed. |
| 4658 | EXPECT_FALSE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 4659 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4660 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4661 | EXPECT_TRUE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
| 4662 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 4663 | connection_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 4664 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4665 | |
| 4666 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(100); |
| 4667 | connection_.GetProcessUndecryptablePacketsAlarm()->Fire(); |
| 4668 | |
| 4669 | // Finally, process a third packet and note that we do not reprocess the |
| 4670 | // buffered packet. |
| 4671 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 4672 | ProcessDataPacketAtLevel(102, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 4673 | } |
| 4674 | |
| 4675 | TEST_P(QuicConnectionTest, SetRTOAfterWritingToSocket) { |
| 4676 | BlockOnNextWrite(); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 4677 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4678 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 4679 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4680 | |
| 4681 | // Test that RTO is started once we write to the socket. |
| 4682 | writer_->SetWritable(); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 4683 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4684 | connection_.OnCanWrite(); |
| 4685 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4686 | } |
| 4687 | |
| 4688 | TEST_P(QuicConnectionTest, DelayRTOWithAckReceipt) { |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 4689 | if (connection_.PtoEnabled()) { |
| 4690 | return; |
| 4691 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4692 | connection_.SetMaxTailLossProbes(0); |
| 4693 | |
| 4694 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4695 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 4696 | connection_.SendStreamDataWithString(2, "foo", 0, NO_FIN); |
| 4697 | connection_.SendStreamDataWithString(3, "bar", 0, NO_FIN); |
| 4698 | QuicAlarm* retransmission_alarm = connection_.GetRetransmissionAlarm(); |
| 4699 | EXPECT_TRUE(retransmission_alarm->IsSet()); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 4700 | EXPECT_EQ(DefaultRetransmissionTime(), |
| 4701 | retransmission_alarm->deadline() - clock_.Now()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4702 | |
| 4703 | // Advance the time right before the RTO, then receive an ack for the first |
| 4704 | // packet to delay the RTO. |
| 4705 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 4706 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4707 | QuicAckFrame ack = InitAckFrame(1); |
| 4708 | ProcessAckPacket(&ack); |
| 4709 | // Now we have an RTT sample of DefaultRetransmissionTime(500ms), |
| 4710 | // so the RTO has increased to 2 * SRTT. |
| 4711 | EXPECT_TRUE(retransmission_alarm->IsSet()); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 4712 | EXPECT_EQ(retransmission_alarm->deadline() - clock_.Now(), |
| 4713 | 2 * DefaultRetransmissionTime()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4714 | |
| 4715 | // Move forward past the original RTO and ensure the RTO is still pending. |
| 4716 | clock_.AdvanceTime(2 * DefaultRetransmissionTime()); |
| 4717 | |
| 4718 | // Ensure the second packet gets retransmitted when it finally fires. |
| 4719 | EXPECT_TRUE(retransmission_alarm->IsSet()); |
| 4720 | EXPECT_EQ(retransmission_alarm->deadline(), clock_.ApproximateNow()); |
| 4721 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 4722 | // Manually cancel the alarm to simulate a real test. |
| 4723 | connection_.GetRetransmissionAlarm()->Fire(); |
| 4724 | |
| 4725 | // The new retransmitted packet number should set the RTO to a larger value |
| 4726 | // than previously. |
| 4727 | EXPECT_TRUE(retransmission_alarm->IsSet()); |
| 4728 | QuicTime next_rto_time = retransmission_alarm->deadline(); |
| 4729 | QuicTime expected_rto_time = |
| 4730 | connection_.sent_packet_manager().GetRetransmissionTime(); |
| 4731 | EXPECT_EQ(next_rto_time, expected_rto_time); |
| 4732 | } |
| 4733 | |
| 4734 | TEST_P(QuicConnectionTest, TestQueued) { |
| 4735 | connection_.SetMaxTailLossProbes(0); |
| 4736 | |
| 4737 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4738 | BlockOnNextWrite(); |
| 4739 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 4740 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 4741 | |
| 4742 | // Unblock the writes and actually send. |
| 4743 | writer_->SetWritable(); |
| 4744 | connection_.OnCanWrite(); |
| 4745 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 4746 | } |
| 4747 | |
| 4748 | TEST_P(QuicConnectionTest, InitialTimeout) { |
| 4749 | EXPECT_TRUE(connection_.connected()); |
| 4750 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 4751 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4752 | |
| 4753 | // SetFromConfig sets the initial timeouts before negotiation. |
| 4754 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4755 | QuicConfig config; |
| 4756 | connection_.SetFromConfig(config); |
| 4757 | // Subtract a second from the idle timeout on the client side. |
| 4758 | QuicTime default_timeout = |
| 4759 | clock_.ApproximateNow() + |
| 4760 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 4761 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 4762 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4763 | EXPECT_CALL(visitor_, |
| 4764 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4765 | // Simulate the timeout alarm firing. |
| 4766 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1)); |
| 4767 | connection_.GetTimeoutAlarm()->Fire(); |
| 4768 | |
| 4769 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4770 | EXPECT_FALSE(connection_.connected()); |
| 4771 | |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 4772 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4773 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 4774 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4775 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 4776 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
renjietang | 11e4a3d | 2019-05-03 11:27:26 -0700 | [diff] [blame] | 4777 | EXPECT_FALSE(connection_.GetProcessUndecryptablePacketsAlarm()->IsSet()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4778 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | TEST_P(QuicConnectionTest, IdleTimeoutAfterFirstSentPacket) { |
| 4782 | EXPECT_TRUE(connection_.connected()); |
| 4783 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 4784 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4785 | |
| 4786 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4787 | QuicConfig config; |
| 4788 | connection_.SetFromConfig(config); |
| 4789 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4790 | QuicTime initial_ddl = |
| 4791 | clock_.ApproximateNow() + |
| 4792 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 4793 | EXPECT_EQ(initial_ddl, connection_.GetTimeoutAlarm()->deadline()); |
| 4794 | EXPECT_TRUE(connection_.connected()); |
| 4795 | |
| 4796 | // Advance the time and send the first packet to the peer. |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 4797 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4798 | QuicPacketNumber last_packet; |
| 4799 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); |
| 4800 | EXPECT_EQ(QuicPacketNumber(1u), last_packet); |
| 4801 | // This will be the updated deadline for the connection to idle time out. |
| 4802 | QuicTime new_ddl = clock_.ApproximateNow() + |
| 4803 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 4804 | |
| 4805 | // Simulate the timeout alarm firing, the connection should not be closed as |
| 4806 | // a new packet has been sent. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4807 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4808 | QuicTime::Delta delay = initial_ddl - clock_.ApproximateNow(); |
| 4809 | clock_.AdvanceTime(delay); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 4810 | if (!GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 4811 | connection_.GetTimeoutAlarm()->Fire(); |
| 4812 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4813 | // Verify the timeout alarm deadline is updated. |
| 4814 | EXPECT_TRUE(connection_.connected()); |
| 4815 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4816 | EXPECT_EQ(new_ddl, connection_.GetTimeoutAlarm()->deadline()); |
| 4817 | |
| 4818 | // Simulate the timeout alarm firing again, the connection now should be |
| 4819 | // closed. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4820 | EXPECT_CALL(visitor_, |
| 4821 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4822 | clock_.AdvanceTime(new_ddl - clock_.ApproximateNow()); |
| 4823 | connection_.GetTimeoutAlarm()->Fire(); |
| 4824 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4825 | EXPECT_FALSE(connection_.connected()); |
| 4826 | |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 4827 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4828 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 4829 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4830 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 4831 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4832 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | TEST_P(QuicConnectionTest, IdleTimeoutAfterSendTwoPackets) { |
| 4836 | EXPECT_TRUE(connection_.connected()); |
| 4837 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 4838 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4839 | |
| 4840 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 4841 | QuicConfig config; |
| 4842 | connection_.SetFromConfig(config); |
| 4843 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4844 | QuicTime initial_ddl = |
| 4845 | clock_.ApproximateNow() + |
| 4846 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 4847 | EXPECT_EQ(initial_ddl, connection_.GetTimeoutAlarm()->deadline()); |
| 4848 | EXPECT_TRUE(connection_.connected()); |
| 4849 | |
| 4850 | // Immediately send the first packet, this is a rare case but test code will |
| 4851 | // hit this issue often as MockClock used for tests doesn't move with code |
| 4852 | // execution until manually adjusted. |
| 4853 | QuicPacketNumber last_packet; |
| 4854 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); |
| 4855 | EXPECT_EQ(QuicPacketNumber(1u), last_packet); |
| 4856 | |
| 4857 | // Advance the time and send the second packet to the peer. |
| 4858 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20)); |
| 4859 | SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet); |
| 4860 | EXPECT_EQ(QuicPacketNumber(2u), last_packet); |
| 4861 | |
zhongyi | c1cab06 | 2019-06-19 12:02:24 -0700 | [diff] [blame] | 4862 | // Simulate the timeout alarm firing, the connection will be closed. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4863 | EXPECT_CALL(visitor_, |
| 4864 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
zhongyi | c1cab06 | 2019-06-19 12:02:24 -0700 | [diff] [blame] | 4865 | clock_.AdvanceTime(initial_ddl - clock_.ApproximateNow()); |
| 4866 | connection_.GetTimeoutAlarm()->Fire(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4867 | |
| 4868 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4869 | EXPECT_FALSE(connection_.connected()); |
| 4870 | |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 4871 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4872 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 4873 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4874 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 4875 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4876 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | TEST_P(QuicConnectionTest, HandshakeTimeout) { |
| 4880 | // Use a shorter handshake timeout than idle timeout for this test. |
| 4881 | const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(5); |
| 4882 | connection_.SetNetworkTimeouts(timeout, timeout); |
| 4883 | EXPECT_TRUE(connection_.connected()); |
| 4884 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 4885 | |
| 4886 | QuicTime handshake_timeout = |
| 4887 | clock_.ApproximateNow() + timeout - QuicTime::Delta::FromSeconds(1); |
| 4888 | EXPECT_EQ(handshake_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 4889 | EXPECT_TRUE(connection_.connected()); |
| 4890 | |
| 4891 | // Send and ack new data 3 seconds later to lengthen the idle timeout. |
| 4892 | SendStreamDataToPeer( |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 4893 | GetNthClientInitiatedStreamId(0, connection_.transport_version()), |
| 4894 | "GET /", 0, FIN, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4895 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(3)); |
| 4896 | QuicAckFrame frame = InitAckFrame(1); |
| 4897 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4898 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4899 | ProcessAckPacket(&frame); |
| 4900 | |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 4901 | if (!GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 4902 | // Fire early to verify it wouldn't timeout yet. |
| 4903 | connection_.GetTimeoutAlarm()->Fire(); |
| 4904 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4905 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4906 | EXPECT_TRUE(connection_.connected()); |
| 4907 | |
| 4908 | clock_.AdvanceTime(timeout - QuicTime::Delta::FromSeconds(2)); |
| 4909 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4910 | EXPECT_CALL(visitor_, |
| 4911 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4912 | // Simulate the timeout alarm firing. |
| 4913 | connection_.GetTimeoutAlarm()->Fire(); |
| 4914 | |
| 4915 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 4916 | EXPECT_FALSE(connection_.connected()); |
| 4917 | |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 4918 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4919 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 4920 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4921 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 4922 | TestConnectionCloseQuicErrorCode(QUIC_HANDSHAKE_TIMEOUT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | TEST_P(QuicConnectionTest, PingAfterSend) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 4926 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 4927 | return; |
| 4928 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4929 | EXPECT_TRUE(connection_.connected()); |
| 4930 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 4931 | .WillRepeatedly(Return(true)); |
| 4932 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 4933 | |
| 4934 | // Advance to 5ms, and send a packet to the peer, which will set |
| 4935 | // the ping alarm. |
| 4936 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 4937 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4938 | SendStreamDataToPeer( |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 4939 | GetNthClientInitiatedStreamId(0, connection_.transport_version()), |
| 4940 | "GET /", 0, FIN, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4941 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 4942 | EXPECT_EQ(QuicTime::Delta::FromSeconds(15), |
| 4943 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4944 | |
| 4945 | // Now recevie an ACK of the previous packet, which will move the |
| 4946 | // ping alarm forward. |
| 4947 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 4948 | QuicAckFrame frame = InitAckFrame(1); |
| 4949 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 4950 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 4951 | ProcessAckPacket(&frame); |
| 4952 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 4953 | // The ping timer is set slightly less than 15 seconds in the future, because |
| 4954 | // of the 1s ping timer alarm granularity. |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 4955 | EXPECT_EQ( |
| 4956 | QuicTime::Delta::FromSeconds(15) - QuicTime::Delta::FromMilliseconds(5), |
| 4957 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4958 | |
| 4959 | writer_->Reset(); |
| 4960 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(15)); |
zhongyi | fbb2577 | 2019-04-10 16:54:08 -0700 | [diff] [blame] | 4961 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { SendPing(); })); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4962 | connection_.GetPingAlarm()->Fire(); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 4963 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 4964 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4965 | ASSERT_EQ(1u, writer_->ping_frames().size()); |
| 4966 | writer_->Reset(); |
| 4967 | |
| 4968 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 4969 | .WillRepeatedly(Return(false)); |
| 4970 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 4971 | SendAckPacketToPeer(); |
| 4972 | |
| 4973 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 4974 | } |
| 4975 | |
| 4976 | TEST_P(QuicConnectionTest, ReducedPingTimeout) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 4977 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 4978 | return; |
| 4979 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4980 | EXPECT_TRUE(connection_.connected()); |
| 4981 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 4982 | .WillRepeatedly(Return(true)); |
| 4983 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 4984 | |
| 4985 | // Use a reduced ping timeout for this connection. |
| 4986 | connection_.set_ping_timeout(QuicTime::Delta::FromSeconds(10)); |
| 4987 | |
| 4988 | // Advance to 5ms, and send a packet to the peer, which will set |
| 4989 | // the ping alarm. |
| 4990 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 4991 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 4992 | SendStreamDataToPeer( |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 4993 | GetNthClientInitiatedStreamId(0, connection_.transport_version()), |
| 4994 | "GET /", 0, FIN, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4995 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 4996 | EXPECT_EQ(QuicTime::Delta::FromSeconds(10), |
| 4997 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4998 | |
| 4999 | // Now recevie an ACK of the previous packet, which will move the |
| 5000 | // ping alarm forward. |
| 5001 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 5002 | QuicAckFrame frame = InitAckFrame(1); |
| 5003 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 5004 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 5005 | ProcessAckPacket(&frame); |
| 5006 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 5007 | // The ping timer is set slightly less than 10 seconds in the future, because |
| 5008 | // of the 1s ping timer alarm granularity. |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 5009 | EXPECT_EQ( |
| 5010 | QuicTime::Delta::FromSeconds(10) - QuicTime::Delta::FromMilliseconds(5), |
| 5011 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5012 | |
| 5013 | writer_->Reset(); |
| 5014 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); |
| 5015 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { |
| 5016 | connection_.SendControlFrame(QuicFrame(QuicPingFrame(1))); |
| 5017 | })); |
| 5018 | connection_.GetPingAlarm()->Fire(); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 5019 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 5020 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5021 | ASSERT_EQ(1u, writer_->ping_frames().size()); |
| 5022 | writer_->Reset(); |
| 5023 | |
| 5024 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 5025 | .WillRepeatedly(Return(false)); |
| 5026 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 5027 | SendAckPacketToPeer(); |
| 5028 | |
| 5029 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 5030 | } |
| 5031 | |
| 5032 | // Tests whether sending an MTU discovery packet to peer successfully causes the |
| 5033 | // maximum packet size to increase. |
| 5034 | TEST_P(QuicConnectionTest, SendMtuDiscoveryPacket) { |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 5035 | MtuDiscoveryTestInit(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5036 | |
| 5037 | // Send an MTU probe. |
| 5038 | const size_t new_mtu = kDefaultMaxPacketSize + 100; |
| 5039 | QuicByteCount mtu_probe_size; |
| 5040 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5041 | .WillOnce(SaveArg<3>(&mtu_probe_size)); |
| 5042 | connection_.SendMtuDiscoveryPacket(new_mtu); |
| 5043 | EXPECT_EQ(new_mtu, mtu_probe_size); |
| 5044 | EXPECT_EQ(QuicPacketNumber(1u), creator_->packet_number()); |
| 5045 | |
| 5046 | // Send more than MTU worth of data. No acknowledgement was received so far, |
| 5047 | // so the MTU should be at its old value. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 5048 | const std::string data(kDefaultMaxPacketSize + 1, '.'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5049 | QuicByteCount size_before_mtu_change; |
| 5050 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5051 | .Times(2) |
| 5052 | .WillOnce(SaveArg<3>(&size_before_mtu_change)) |
| 5053 | .WillOnce(Return()); |
| 5054 | connection_.SendStreamDataWithString(3, data, 0, FIN); |
| 5055 | EXPECT_EQ(QuicPacketNumber(3u), creator_->packet_number()); |
| 5056 | EXPECT_EQ(kDefaultMaxPacketSize, size_before_mtu_change); |
| 5057 | |
| 5058 | // Acknowledge all packets so far. |
| 5059 | QuicAckFrame probe_ack = InitAckFrame(3); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5060 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 5061 | ProcessAckPacket(&probe_ack); |
| 5062 | EXPECT_EQ(new_mtu, connection_.max_packet_length()); |
| 5063 | |
| 5064 | // Send the same data again. Check that it fits into a single packet now. |
| 5065 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 5066 | connection_.SendStreamDataWithString(3, data, 0, FIN); |
| 5067 | EXPECT_EQ(QuicPacketNumber(4u), creator_->packet_number()); |
| 5068 | } |
| 5069 | |
wub | b442b86 | 2020-01-31 08:16:21 -0800 | [diff] [blame] | 5070 | // Verifies that when a MTU probe packet is sent and buffered in a batch writer, |
| 5071 | // the writer is flushed immediately. |
| 5072 | TEST_P(QuicConnectionTest, BatchWriterFlushedAfterMtuDiscoveryPacket) { |
| 5073 | writer_->SetBatchMode(true); |
| 5074 | MtuDiscoveryTestInit(); |
| 5075 | |
| 5076 | // Send an MTU probe. |
| 5077 | const size_t target_mtu = kDefaultMaxPacketSize + 100; |
| 5078 | QuicByteCount mtu_probe_size; |
| 5079 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5080 | .WillOnce(SaveArg<3>(&mtu_probe_size)); |
| 5081 | const uint32_t prior_flush_attempts = writer_->flush_attempts(); |
| 5082 | connection_.SendMtuDiscoveryPacket(target_mtu); |
| 5083 | EXPECT_EQ(target_mtu, mtu_probe_size); |
wub | 18f4734 | 2020-03-16 15:56:03 -0700 | [diff] [blame] | 5084 | EXPECT_EQ(writer_->flush_attempts(), prior_flush_attempts + 1); |
wub | b442b86 | 2020-01-31 08:16:21 -0800 | [diff] [blame] | 5085 | } |
| 5086 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5087 | // Tests whether MTU discovery does not happen when it is not explicitly enabled |
| 5088 | // by the connection options. |
| 5089 | TEST_P(QuicConnectionTest, MtuDiscoveryDisabled) { |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 5090 | MtuDiscoveryTestInit(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5091 | |
| 5092 | const QuicPacketCount packets_between_probes_base = 10; |
| 5093 | set_packets_between_probes_base(packets_between_probes_base); |
| 5094 | |
| 5095 | const QuicPacketCount number_of_packets = packets_between_probes_base * 2; |
| 5096 | for (QuicPacketCount i = 0; i < number_of_packets; i++) { |
| 5097 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5098 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5099 | EXPECT_EQ(0u, connection_.mtu_probe_count()); |
| 5100 | } |
| 5101 | } |
| 5102 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5103 | // Tests whether MTU discovery works when all probes are acknowledged on the |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5104 | // first try. |
| 5105 | TEST_P(QuicConnectionTest, MtuDiscoveryEnabled) { |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 5106 | MtuDiscoveryTestInit(); |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 5107 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5108 | const QuicPacketCount packets_between_probes_base = 5; |
| 5109 | set_packets_between_probes_base(packets_between_probes_base); |
| 5110 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5111 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5112 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5113 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5114 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5115 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5116 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5117 | } |
| 5118 | |
| 5119 | // Trigger the probe. |
| 5120 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5121 | nullptr); |
| 5122 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5123 | QuicByteCount probe_size; |
| 5124 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5125 | .WillOnce(SaveArg<3>(&probe_size)); |
| 5126 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
wub | 173916e | 2019-11-27 14:36:24 -0800 | [diff] [blame] | 5127 | |
| 5128 | EXPECT_THAT(probe_size, InRange(connection_.max_packet_length(), |
| 5129 | kMtuDiscoveryTargetPacketSizeHigh)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5130 | |
| 5131 | const QuicPacketNumber probe_packet_number = |
| 5132 | FirstSendingPacketNumber() + packets_between_probes_base; |
| 5133 | ASSERT_EQ(probe_packet_number, creator_->packet_number()); |
| 5134 | |
| 5135 | // Acknowledge all packets sent so far. |
| 5136 | QuicAckFrame probe_ack = InitAckFrame(probe_packet_number); |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5137 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)) |
| 5138 | .Times(AnyNumber()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5139 | ProcessAckPacket(&probe_ack); |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5140 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5141 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5142 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5143 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5144 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5145 | QuicStreamOffset stream_offset = packets_between_probes_base; |
wub | ecb643f | 2020-03-19 08:58:46 -0700 | [diff] [blame] | 5146 | QuicByteCount last_probe_size = 0; |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5147 | for (size_t num_probes = 1; num_probes < kMtuDiscoveryAttempts; |
| 5148 | ++num_probes) { |
| 5149 | // Send just enough packets without triggering the next probe. |
| 5150 | for (QuicPacketCount i = 0; |
| 5151 | i < (packets_between_probes_base << num_probes) - 1; ++i) { |
| 5152 | SendStreamDataToPeer(3, ".", stream_offset++, NO_FIN, nullptr); |
| 5153 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5154 | } |
| 5155 | |
| 5156 | // Trigger the next probe. |
| 5157 | SendStreamDataToPeer(3, "!", stream_offset++, NO_FIN, nullptr); |
| 5158 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5159 | QuicByteCount new_probe_size; |
| 5160 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5161 | .WillOnce(SaveArg<3>(&new_probe_size)); |
| 5162 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5163 | EXPECT_THAT(new_probe_size, |
| 5164 | InRange(probe_size, kMtuDiscoveryTargetPacketSizeHigh)); |
| 5165 | EXPECT_EQ(num_probes + 1, connection_.mtu_probe_count()); |
| 5166 | |
| 5167 | // Acknowledge all packets sent so far. |
| 5168 | QuicAckFrame probe_ack = InitAckFrame(creator_->packet_number()); |
| 5169 | ProcessAckPacket(&probe_ack); |
| 5170 | EXPECT_EQ(new_probe_size, connection_.max_packet_length()); |
| 5171 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5172 | |
wub | ecb643f | 2020-03-19 08:58:46 -0700 | [diff] [blame] | 5173 | last_probe_size = probe_size; |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5174 | probe_size = new_probe_size; |
| 5175 | } |
| 5176 | |
| 5177 | // The last probe size should be equal to the target. |
| 5178 | EXPECT_EQ(probe_size, kMtuDiscoveryTargetPacketSizeHigh); |
wub | ecb643f | 2020-03-19 08:58:46 -0700 | [diff] [blame] | 5179 | |
wub | ae6581a | 2020-05-04 12:13:05 -0700 | [diff] [blame] | 5180 | writer_->SetShouldWriteFail(); |
wub | ecb643f | 2020-03-19 08:58:46 -0700 | [diff] [blame] | 5181 | |
wub | ae6581a | 2020-05-04 12:13:05 -0700 | [diff] [blame] | 5182 | // Ignore PACKET_WRITE_ERROR once. |
| 5183 | SendStreamDataToPeer(3, "(", stream_offset++, NO_FIN, nullptr); |
| 5184 | EXPECT_EQ(last_probe_size, connection_.max_packet_length()); |
| 5185 | EXPECT_TRUE(connection_.connected()); |
wub | ecb643f | 2020-03-19 08:58:46 -0700 | [diff] [blame] | 5186 | |
wub | ae6581a | 2020-05-04 12:13:05 -0700 | [diff] [blame] | 5187 | // Close connection on another PACKET_WRITE_ERROR. |
| 5188 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)) |
| 5189 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 5190 | SendStreamDataToPeer(3, ")", stream_offset++, NO_FIN, nullptr); |
| 5191 | EXPECT_EQ(last_probe_size, connection_.max_packet_length()); |
| 5192 | EXPECT_FALSE(connection_.connected()); |
| 5193 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 5194 | IsError(QUIC_PACKET_WRITE_ERROR)); |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5195 | } |
| 5196 | |
wub | 748e20b | 2020-03-20 14:33:59 -0700 | [diff] [blame] | 5197 | // After a successful MTU probe, one and only one write error should be ignored |
| 5198 | // if it happened in QuicConnection::FlushPacket. |
| 5199 | TEST_P(QuicConnectionTest, |
| 5200 | MtuDiscoveryIgnoreOneWriteErrorInFlushAfterSuccessfulProbes) { |
| 5201 | MtuDiscoveryTestInit(); |
| 5202 | writer_->SetBatchMode(true); |
| 5203 | |
| 5204 | const QuicPacketCount packets_between_probes_base = 5; |
| 5205 | set_packets_between_probes_base(packets_between_probes_base); |
| 5206 | |
| 5207 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5208 | |
| 5209 | const QuicByteCount original_max_packet_length = |
| 5210 | connection_.max_packet_length(); |
| 5211 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5212 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5213 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5214 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5215 | } |
| 5216 | |
| 5217 | // Trigger the probe. |
| 5218 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5219 | nullptr); |
| 5220 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5221 | QuicByteCount probe_size; |
| 5222 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5223 | .WillOnce(SaveArg<3>(&probe_size)); |
| 5224 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5225 | |
| 5226 | EXPECT_THAT(probe_size, InRange(connection_.max_packet_length(), |
| 5227 | kMtuDiscoveryTargetPacketSizeHigh)); |
| 5228 | |
| 5229 | const QuicPacketNumber probe_packet_number = |
| 5230 | FirstSendingPacketNumber() + packets_between_probes_base; |
| 5231 | ASSERT_EQ(probe_packet_number, creator_->packet_number()); |
| 5232 | |
| 5233 | // Acknowledge all packets sent so far. |
| 5234 | QuicAckFrame probe_ack = InitAckFrame(probe_packet_number); |
| 5235 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)) |
| 5236 | .Times(AnyNumber()); |
| 5237 | ProcessAckPacket(&probe_ack); |
| 5238 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
| 5239 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5240 | |
| 5241 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5242 | |
wub | ae6581a | 2020-05-04 12:13:05 -0700 | [diff] [blame] | 5243 | writer_->SetShouldWriteFail(); |
wub | 748e20b | 2020-03-20 14:33:59 -0700 | [diff] [blame] | 5244 | |
wub | ae6581a | 2020-05-04 12:13:05 -0700 | [diff] [blame] | 5245 | // Ignore PACKET_WRITE_ERROR once. |
| 5246 | { |
| 5247 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 5248 | // flusher's destructor will call connection_.FlushPackets, which should |
| 5249 | // get a WRITE_STATUS_ERROR from the writer and ignore it. |
wub | 748e20b | 2020-03-20 14:33:59 -0700 | [diff] [blame] | 5250 | } |
wub | ae6581a | 2020-05-04 12:13:05 -0700 | [diff] [blame] | 5251 | EXPECT_EQ(original_max_packet_length, connection_.max_packet_length()); |
| 5252 | EXPECT_TRUE(connection_.connected()); |
| 5253 | |
| 5254 | // Close connection on another PACKET_WRITE_ERROR. |
| 5255 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)) |
| 5256 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 5257 | { |
| 5258 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 5259 | // flusher's destructor will call connection_.FlushPackets, which should |
| 5260 | // get a WRITE_STATUS_ERROR from the writer and ignore it. |
| 5261 | } |
| 5262 | EXPECT_EQ(original_max_packet_length, connection_.max_packet_length()); |
| 5263 | EXPECT_FALSE(connection_.connected()); |
| 5264 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 5265 | IsError(QUIC_PACKET_WRITE_ERROR)); |
wub | 748e20b | 2020-03-20 14:33:59 -0700 | [diff] [blame] | 5266 | } |
| 5267 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5268 | // Simulate the case where the first attempt to send a probe is write blocked, |
| 5269 | // and after unblock, the second attempt returns a MSG_TOO_BIG error. |
| 5270 | TEST_P(QuicConnectionTest, MtuDiscoveryWriteBlocked) { |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 5271 | MtuDiscoveryTestInit(); |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5272 | |
| 5273 | const QuicPacketCount packets_between_probes_base = 5; |
| 5274 | set_packets_between_probes_base(packets_between_probes_base); |
| 5275 | |
| 5276 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5277 | |
| 5278 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5279 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5280 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5281 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5282 | } |
| 5283 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5284 | QuicByteCount original_max_packet_length = connection_.max_packet_length(); |
| 5285 | |
| 5286 | // Trigger the probe. |
| 5287 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5288 | nullptr); |
| 5289 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 5290 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5291 | BlockOnNextWrite(); |
| 5292 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 5293 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5294 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5295 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 5296 | ASSERT_TRUE(connection_.connected()); |
| 5297 | |
| 5298 | writer_->SetWritable(); |
| 5299 | SimulateNextPacketTooLarge(); |
| 5300 | connection_.OnCanWrite(); |
| 5301 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 5302 | EXPECT_EQ(original_max_packet_length, connection_.max_packet_length()); |
| 5303 | EXPECT_TRUE(connection_.connected()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5304 | } |
| 5305 | |
| 5306 | // Tests whether MTU discovery works correctly when the probes never get |
| 5307 | // acknowledged. |
| 5308 | TEST_P(QuicConnectionTest, MtuDiscoveryFailed) { |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 5309 | MtuDiscoveryTestInit(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5310 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5311 | // Lower the number of probes between packets in order to make the test go |
| 5312 | // much faster. |
| 5313 | const QuicPacketCount packets_between_probes_base = 5; |
| 5314 | set_packets_between_probes_base(packets_between_probes_base); |
| 5315 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5316 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5317 | |
| 5318 | const QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(100); |
| 5319 | |
| 5320 | EXPECT_EQ(packets_between_probes_base, |
| 5321 | QuicConnectionPeer::GetPacketsBetweenMtuProbes(&connection_)); |
| 5322 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5323 | // This tests sends more packets than strictly necessary to make sure that if |
| 5324 | // the connection was to send more discovery packets than needed, those would |
| 5325 | // get caught as well. |
| 5326 | const QuicPacketCount number_of_packets = |
| 5327 | packets_between_probes_base * (1 << (kMtuDiscoveryAttempts + 1)); |
| 5328 | std::vector<QuicPacketNumber> mtu_discovery_packets; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5329 | // Called on many acks. |
| 5330 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)) |
| 5331 | .Times(AnyNumber()); |
| 5332 | for (QuicPacketCount i = 0; i < number_of_packets; i++) { |
| 5333 | SendStreamDataToPeer(3, "!", i, NO_FIN, nullptr); |
| 5334 | clock_.AdvanceTime(rtt); |
| 5335 | |
| 5336 | // Receive an ACK, which marks all data packets as received, and all MTU |
| 5337 | // discovery packets as missing. |
| 5338 | |
| 5339 | QuicAckFrame ack; |
| 5340 | |
| 5341 | if (!mtu_discovery_packets.empty()) { |
| 5342 | QuicPacketNumber min_packet = *min_element(mtu_discovery_packets.begin(), |
| 5343 | mtu_discovery_packets.end()); |
| 5344 | QuicPacketNumber max_packet = *max_element(mtu_discovery_packets.begin(), |
| 5345 | mtu_discovery_packets.end()); |
| 5346 | ack.packets.AddRange(QuicPacketNumber(1), min_packet); |
| 5347 | ack.packets.AddRange(QuicPacketNumber(max_packet + 1), |
| 5348 | creator_->packet_number() + 1); |
| 5349 | ack.largest_acked = creator_->packet_number(); |
| 5350 | |
| 5351 | } else { |
| 5352 | ack.packets.AddRange(QuicPacketNumber(1), creator_->packet_number() + 1); |
| 5353 | ack.largest_acked = creator_->packet_number(); |
| 5354 | } |
| 5355 | |
| 5356 | ProcessAckPacket(&ack); |
| 5357 | |
| 5358 | // Trigger MTU probe if it would be scheduled now. |
| 5359 | if (!connection_.GetMtuDiscoveryAlarm()->IsSet()) { |
| 5360 | continue; |
| 5361 | } |
| 5362 | |
| 5363 | // Fire the alarm. The alarm should cause a packet to be sent. |
| 5364 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 5365 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5366 | // Record the packet number of the MTU discovery packet in order to |
| 5367 | // mark it as NACK'd. |
| 5368 | mtu_discovery_packets.push_back(creator_->packet_number()); |
| 5369 | } |
| 5370 | |
| 5371 | // Ensure the number of packets between probes grows exponentially by checking |
| 5372 | // it against the closed-form expression for the packet number. |
| 5373 | ASSERT_EQ(kMtuDiscoveryAttempts, mtu_discovery_packets.size()); |
| 5374 | for (uint64_t i = 0; i < kMtuDiscoveryAttempts; i++) { |
| 5375 | // 2^0 + 2^1 + 2^2 + ... + 2^n = 2^(n + 1) - 1 |
| 5376 | const QuicPacketCount packets_between_probes = |
| 5377 | packets_between_probes_base * ((1 << (i + 1)) - 1); |
| 5378 | EXPECT_EQ(QuicPacketNumber(packets_between_probes + (i + 1)), |
| 5379 | mtu_discovery_packets[i]); |
| 5380 | } |
| 5381 | |
| 5382 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5383 | EXPECT_EQ(kDefaultMaxPacketSize, connection_.max_packet_length()); |
| 5384 | EXPECT_EQ(kMtuDiscoveryAttempts, connection_.mtu_probe_count()); |
| 5385 | } |
| 5386 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5387 | // Probe 3 times, the first one succeeds, then fails, then succeeds again. |
| 5388 | TEST_P(QuicConnectionTest, MtuDiscoverySecondProbeFailed) { |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 5389 | MtuDiscoveryTestInit(); |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5390 | |
| 5391 | const QuicPacketCount packets_between_probes_base = 5; |
| 5392 | set_packets_between_probes_base(packets_between_probes_base); |
| 5393 | |
| 5394 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5395 | |
| 5396 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5397 | QuicStreamOffset stream_offset = 0; |
| 5398 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5399 | SendStreamDataToPeer(3, ".", stream_offset++, NO_FIN, nullptr); |
| 5400 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5401 | } |
| 5402 | |
| 5403 | // Trigger the probe. |
| 5404 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5405 | nullptr); |
| 5406 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5407 | QuicByteCount probe_size; |
| 5408 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5409 | .WillOnce(SaveArg<3>(&probe_size)); |
| 5410 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5411 | EXPECT_THAT(probe_size, InRange(connection_.max_packet_length(), |
| 5412 | kMtuDiscoveryTargetPacketSizeHigh)); |
| 5413 | |
| 5414 | const QuicPacketNumber probe_packet_number = |
| 5415 | FirstSendingPacketNumber() + packets_between_probes_base; |
| 5416 | ASSERT_EQ(probe_packet_number, creator_->packet_number()); |
| 5417 | |
| 5418 | // Acknowledge all packets sent so far. |
| 5419 | QuicAckFrame first_ack = InitAckFrame(probe_packet_number); |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5420 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)) |
| 5421 | .Times(AnyNumber()); |
| 5422 | ProcessAckPacket(&first_ack); |
| 5423 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
| 5424 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5425 | |
| 5426 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5427 | |
| 5428 | // Send just enough packets without triggering the second probe. |
| 5429 | for (QuicPacketCount i = 0; i < (packets_between_probes_base << 1) - 1; ++i) { |
| 5430 | SendStreamDataToPeer(3, ".", stream_offset++, NO_FIN, nullptr); |
| 5431 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5432 | } |
| 5433 | |
| 5434 | // Trigger the second probe. |
| 5435 | SendStreamDataToPeer(3, "!", stream_offset++, NO_FIN, nullptr); |
| 5436 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5437 | QuicByteCount second_probe_size; |
| 5438 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5439 | .WillOnce(SaveArg<3>(&second_probe_size)); |
| 5440 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5441 | EXPECT_THAT(second_probe_size, |
| 5442 | InRange(probe_size, kMtuDiscoveryTargetPacketSizeHigh)); |
| 5443 | EXPECT_EQ(2u, connection_.mtu_probe_count()); |
| 5444 | |
| 5445 | // Acknowledge all packets sent so far, except the second probe. |
| 5446 | QuicPacketNumber second_probe_packet_number = creator_->packet_number(); |
| 5447 | QuicAckFrame second_ack = InitAckFrame(second_probe_packet_number - 1); |
| 5448 | ProcessAckPacket(&first_ack); |
| 5449 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
| 5450 | |
| 5451 | // Send just enough packets without triggering the third probe. |
| 5452 | for (QuicPacketCount i = 0; i < (packets_between_probes_base << 2) - 1; ++i) { |
| 5453 | SendStreamDataToPeer(3, "@", stream_offset++, NO_FIN, nullptr); |
| 5454 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5455 | } |
| 5456 | |
| 5457 | // Trigger the third probe. |
| 5458 | SendStreamDataToPeer(3, "#", stream_offset++, NO_FIN, nullptr); |
| 5459 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5460 | QuicByteCount third_probe_size; |
| 5461 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5462 | .WillOnce(SaveArg<3>(&third_probe_size)); |
| 5463 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5464 | EXPECT_THAT(third_probe_size, InRange(probe_size, second_probe_size)); |
| 5465 | EXPECT_EQ(3u, connection_.mtu_probe_count()); |
| 5466 | |
| 5467 | // Acknowledge all packets sent so far, except the second probe. |
| 5468 | QuicAckFrame third_ack = |
| 5469 | ConstructAckFrame(creator_->packet_number(), second_probe_packet_number); |
| 5470 | ProcessAckPacket(&third_ack); |
| 5471 | EXPECT_EQ(third_probe_size, connection_.max_packet_length()); |
| 5472 | } |
| 5473 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5474 | // Tests whether MTU discovery works when the writer has a limit on how large a |
| 5475 | // packet can be. |
| 5476 | TEST_P(QuicConnectionTest, MtuDiscoveryWriterLimited) { |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 5477 | MtuDiscoveryTestInit(); |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 5478 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5479 | const QuicByteCount mtu_limit = kMtuDiscoveryTargetPacketSizeHigh - 1; |
| 5480 | writer_->set_max_packet_size(mtu_limit); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5481 | |
| 5482 | const QuicPacketCount packets_between_probes_base = 5; |
| 5483 | set_packets_between_probes_base(packets_between_probes_base); |
| 5484 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5485 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5486 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5487 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5488 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5489 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5490 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5491 | } |
| 5492 | |
| 5493 | // Trigger the probe. |
| 5494 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5495 | nullptr); |
| 5496 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5497 | QuicByteCount probe_size; |
| 5498 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5499 | .WillOnce(SaveArg<3>(&probe_size)); |
| 5500 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
wub | 173916e | 2019-11-27 14:36:24 -0800 | [diff] [blame] | 5501 | |
| 5502 | EXPECT_THAT(probe_size, InRange(connection_.max_packet_length(), mtu_limit)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5503 | |
| 5504 | const QuicPacketNumber probe_sequence_number = |
| 5505 | FirstSendingPacketNumber() + packets_between_probes_base; |
| 5506 | ASSERT_EQ(probe_sequence_number, creator_->packet_number()); |
| 5507 | |
| 5508 | // Acknowledge all packets sent so far. |
| 5509 | QuicAckFrame probe_ack = InitAckFrame(probe_sequence_number); |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5510 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)) |
| 5511 | .Times(AnyNumber()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5512 | ProcessAckPacket(&probe_ack); |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5513 | EXPECT_EQ(probe_size, connection_.max_packet_length()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5514 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5515 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5516 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5517 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5518 | QuicStreamOffset stream_offset = packets_between_probes_base; |
| 5519 | for (size_t num_probes = 1; num_probes < kMtuDiscoveryAttempts; |
| 5520 | ++num_probes) { |
| 5521 | // Send just enough packets without triggering the next probe. |
| 5522 | for (QuicPacketCount i = 0; |
| 5523 | i < (packets_between_probes_base << num_probes) - 1; ++i) { |
| 5524 | SendStreamDataToPeer(3, ".", stream_offset++, NO_FIN, nullptr); |
| 5525 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5526 | } |
| 5527 | |
| 5528 | // Trigger the next probe. |
| 5529 | SendStreamDataToPeer(3, "!", stream_offset++, NO_FIN, nullptr); |
| 5530 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5531 | QuicByteCount new_probe_size; |
| 5532 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) |
| 5533 | .WillOnce(SaveArg<3>(&new_probe_size)); |
| 5534 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5535 | EXPECT_THAT(new_probe_size, InRange(probe_size, mtu_limit)); |
| 5536 | EXPECT_EQ(num_probes + 1, connection_.mtu_probe_count()); |
| 5537 | |
| 5538 | // Acknowledge all packets sent so far. |
| 5539 | QuicAckFrame probe_ack = InitAckFrame(creator_->packet_number()); |
| 5540 | ProcessAckPacket(&probe_ack); |
| 5541 | EXPECT_EQ(new_probe_size, connection_.max_packet_length()); |
| 5542 | EXPECT_EQ(0u, connection_.GetBytesInFlight()); |
| 5543 | |
| 5544 | probe_size = new_probe_size; |
| 5545 | } |
| 5546 | |
| 5547 | // The last probe size should be equal to the target. |
| 5548 | EXPECT_EQ(probe_size, mtu_limit); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | // Tests whether MTU discovery works when the writer returns an error despite |
| 5552 | // advertising higher packet length. |
| 5553 | TEST_P(QuicConnectionTest, MtuDiscoveryWriterFailed) { |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 5554 | MtuDiscoveryTestInit(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5555 | |
| 5556 | const QuicByteCount mtu_limit = kMtuDiscoveryTargetPacketSizeHigh - 1; |
| 5557 | const QuicByteCount initial_mtu = connection_.max_packet_length(); |
| 5558 | EXPECT_LT(initial_mtu, mtu_limit); |
| 5559 | writer_->set_max_packet_size(mtu_limit); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5560 | |
| 5561 | const QuicPacketCount packets_between_probes_base = 5; |
| 5562 | set_packets_between_probes_base(packets_between_probes_base); |
| 5563 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5564 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5565 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5566 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5567 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5568 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5569 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5570 | } |
| 5571 | |
| 5572 | // Trigger the probe. |
| 5573 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5574 | nullptr); |
| 5575 | ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5576 | writer_->SimulateNextPacketTooLarge(); |
| 5577 | connection_.GetMtuDiscoveryAlarm()->Fire(); |
| 5578 | ASSERT_TRUE(connection_.connected()); |
| 5579 | |
| 5580 | // Send more data. |
| 5581 | QuicPacketNumber probe_number = creator_->packet_number(); |
| 5582 | QuicPacketCount extra_packets = packets_between_probes_base * 3; |
| 5583 | for (QuicPacketCount i = 0; i < extra_packets; i++) { |
| 5584 | connection_.EnsureWritableAndSendStreamData5(); |
| 5585 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5586 | } |
| 5587 | |
| 5588 | // Acknowledge all packets sent so far, except for the lost probe. |
| 5589 | QuicAckFrame probe_ack = |
| 5590 | ConstructAckFrame(creator_->packet_number(), probe_number); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5591 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 5592 | ProcessAckPacket(&probe_ack); |
| 5593 | EXPECT_EQ(initial_mtu, connection_.max_packet_length()); |
| 5594 | |
| 5595 | // Send more packets, and ensure that none of them sets the alarm. |
| 5596 | for (QuicPacketCount i = 0; i < 4 * packets_between_probes_base; i++) { |
| 5597 | connection_.EnsureWritableAndSendStreamData5(); |
| 5598 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5599 | } |
| 5600 | |
| 5601 | EXPECT_EQ(initial_mtu, connection_.max_packet_length()); |
| 5602 | EXPECT_EQ(1u, connection_.mtu_probe_count()); |
| 5603 | } |
| 5604 | |
| 5605 | TEST_P(QuicConnectionTest, NoMtuDiscoveryAfterConnectionClosed) { |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 5606 | MtuDiscoveryTestInit(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5607 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5608 | const QuicPacketCount packets_between_probes_base = 10; |
| 5609 | set_packets_between_probes_base(packets_between_probes_base); |
| 5610 | |
wub | f76cf2a | 2019-10-11 18:49:07 -0700 | [diff] [blame] | 5611 | connection_.EnablePathMtuDiscovery(send_algorithm_); |
| 5612 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5613 | // Send enough packets so that the next one triggers path MTU discovery. |
| 5614 | for (QuicPacketCount i = 0; i < packets_between_probes_base - 1; i++) { |
| 5615 | SendStreamDataToPeer(3, ".", i, NO_FIN, nullptr); |
| 5616 | ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5617 | } |
| 5618 | |
| 5619 | SendStreamDataToPeer(3, "!", packets_between_probes_base - 1, NO_FIN, |
| 5620 | nullptr); |
| 5621 | EXPECT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5622 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5623 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5624 | connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason", |
| 5625 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 5626 | EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); |
| 5627 | } |
| 5628 | |
dschinazi | 9a6194e | 2020-04-30 16:21:09 -0700 | [diff] [blame] | 5629 | TEST_P(QuicConnectionTest, TimeoutAfterSendDuringHandshake) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5630 | EXPECT_TRUE(connection_.connected()); |
| 5631 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 5632 | QuicConfig config; |
| 5633 | connection_.SetFromConfig(config); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5634 | |
| 5635 | const QuicTime::Delta initial_idle_timeout = |
| 5636 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 5637 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 5638 | QuicTime default_timeout = clock_.ApproximateNow() + initial_idle_timeout; |
| 5639 | |
| 5640 | // When we send a packet, the timeout will change to 5ms + |
| 5641 | // kInitialIdleTimeoutSecs. |
| 5642 | clock_.AdvanceTime(five_ms); |
| 5643 | SendStreamDataToPeer( |
| 5644 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5645 | 0, FIN, nullptr); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 5646 | if (GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 5647 | EXPECT_EQ(default_timeout + five_ms, |
| 5648 | connection_.GetTimeoutAlarm()->deadline()); |
| 5649 | } else { |
| 5650 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 5651 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5652 | |
| 5653 | // Now send more data. This will not move the timeout because |
| 5654 | // no data has been received since the previous write. |
| 5655 | clock_.AdvanceTime(five_ms); |
| 5656 | SendStreamDataToPeer( |
| 5657 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5658 | 3, FIN, nullptr); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 5659 | if (GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 5660 | EXPECT_EQ(default_timeout + five_ms, |
| 5661 | connection_.GetTimeoutAlarm()->deadline()); |
| 5662 | } else { |
| 5663 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 5664 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5665 | |
| 5666 | // The original alarm will fire. We should not time out because we had a |
| 5667 | // network event at t=5ms. The alarm will reregister. |
| 5668 | clock_.AdvanceTime(initial_idle_timeout - five_ms - five_ms); |
| 5669 | EXPECT_EQ(default_timeout, clock_.ApproximateNow()); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 5670 | if (!GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 5671 | connection_.GetTimeoutAlarm()->Fire(); |
| 5672 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5673 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5674 | EXPECT_TRUE(connection_.connected()); |
| 5675 | EXPECT_EQ(default_timeout + five_ms, |
| 5676 | connection_.GetTimeoutAlarm()->deadline()); |
| 5677 | |
| 5678 | // This time, we should time out. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5679 | EXPECT_CALL(visitor_, |
| 5680 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 5681 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5682 | clock_.AdvanceTime(five_ms); |
| 5683 | EXPECT_EQ(default_timeout + five_ms, clock_.ApproximateNow()); |
| 5684 | connection_.GetTimeoutAlarm()->Fire(); |
| 5685 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5686 | EXPECT_FALSE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5687 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | TEST_P(QuicConnectionTest, TimeoutAfterRetransmission) { |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 5691 | if (connection_.PtoEnabled()) { |
| 5692 | return; |
| 5693 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5694 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 5695 | EXPECT_TRUE(connection_.connected()); |
| 5696 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 5697 | QuicConfig config; |
| 5698 | connection_.SetFromConfig(config); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5699 | |
| 5700 | const QuicTime start_time = clock_.Now(); |
| 5701 | const QuicTime::Delta initial_idle_timeout = |
| 5702 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 5703 | QuicTime default_timeout = clock_.Now() + initial_idle_timeout; |
| 5704 | |
| 5705 | connection_.SetMaxTailLossProbes(0); |
| 5706 | const QuicTime default_retransmission_time = |
| 5707 | start_time + DefaultRetransmissionTime(); |
| 5708 | |
| 5709 | ASSERT_LT(default_retransmission_time, default_timeout); |
| 5710 | |
| 5711 | // When we send a packet, the timeout will change to 5 ms + |
| 5712 | // kInitialIdleTimeoutSecs (but it will not reschedule the alarm). |
| 5713 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 5714 | const QuicTime send_time = start_time + five_ms; |
| 5715 | clock_.AdvanceTime(five_ms); |
| 5716 | ASSERT_EQ(send_time, clock_.Now()); |
| 5717 | SendStreamDataToPeer( |
| 5718 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5719 | 0, FIN, nullptr); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 5720 | if (GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 5721 | EXPECT_EQ(default_timeout + five_ms, |
| 5722 | connection_.GetTimeoutAlarm()->deadline()); |
| 5723 | } else { |
| 5724 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 5725 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5726 | |
| 5727 | // Move forward 5 ms and receive a packet, which will move the timeout |
| 5728 | // forward 5 ms more (but will not reschedule the alarm). |
| 5729 | const QuicTime receive_time = send_time + five_ms; |
| 5730 | clock_.AdvanceTime(receive_time - clock_.Now()); |
| 5731 | ASSERT_EQ(receive_time, clock_.Now()); |
| 5732 | ProcessPacket(1); |
| 5733 | |
| 5734 | // Now move forward to the retransmission time and retransmit the |
| 5735 | // packet, which should move the timeout forward again (but will not |
| 5736 | // reschedule the alarm). |
| 5737 | EXPECT_EQ(default_retransmission_time + five_ms, |
| 5738 | connection_.GetRetransmissionAlarm()->deadline()); |
| 5739 | // Simulate the retransmission alarm firing. |
| 5740 | const QuicTime rto_time = send_time + DefaultRetransmissionTime(); |
| 5741 | const QuicTime final_timeout = rto_time + initial_idle_timeout; |
| 5742 | clock_.AdvanceTime(rto_time - clock_.Now()); |
| 5743 | ASSERT_EQ(rto_time, clock_.Now()); |
| 5744 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(2u), _, _)); |
| 5745 | connection_.GetRetransmissionAlarm()->Fire(); |
| 5746 | |
| 5747 | // Advance to the original timeout and fire the alarm. The connection should |
| 5748 | // timeout, and the alarm should be registered based on the time of the |
| 5749 | // retransmission. |
| 5750 | clock_.AdvanceTime(default_timeout - clock_.Now()); |
| 5751 | ASSERT_EQ(default_timeout.ToDebuggingValue(), |
| 5752 | clock_.Now().ToDebuggingValue()); |
| 5753 | EXPECT_EQ(default_timeout, clock_.Now()); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 5754 | if (!GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 5755 | connection_.GetTimeoutAlarm()->Fire(); |
| 5756 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5757 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5758 | EXPECT_TRUE(connection_.connected()); |
| 5759 | ASSERT_EQ(final_timeout.ToDebuggingValue(), |
| 5760 | connection_.GetTimeoutAlarm()->deadline().ToDebuggingValue()); |
| 5761 | |
| 5762 | // This time, we should time out. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5763 | EXPECT_CALL(visitor_, |
| 5764 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 5765 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5766 | clock_.AdvanceTime(final_timeout - clock_.Now()); |
| 5767 | EXPECT_EQ(connection_.GetTimeoutAlarm()->deadline(), clock_.Now()); |
| 5768 | EXPECT_EQ(final_timeout, clock_.Now()); |
| 5769 | connection_.GetTimeoutAlarm()->Fire(); |
| 5770 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5771 | EXPECT_FALSE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5772 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5773 | } |
| 5774 | |
dschinazi | 9a6194e | 2020-04-30 16:21:09 -0700 | [diff] [blame] | 5775 | TEST_P(QuicConnectionTest, TimeoutAfterSendAfterHandshake) { |
| 5776 | // When the idle timeout fires, verify that by default we do not send any |
| 5777 | // connection close packets. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5778 | EXPECT_TRUE(connection_.connected()); |
| 5779 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 5780 | QuicConfig config; |
| 5781 | |
| 5782 | // Create a handshake message that also enables silent close. |
| 5783 | CryptoHandshakeMessage msg; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 5784 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5785 | QuicConfig client_config; |
| 5786 | client_config.SetInitialStreamFlowControlWindowToSend( |
| 5787 | kInitialStreamFlowControlWindowForTest); |
| 5788 | client_config.SetInitialSessionFlowControlWindowToSend( |
| 5789 | kInitialSessionFlowControlWindowForTest); |
| 5790 | client_config.SetIdleNetworkTimeout( |
dschinazi | 027366e | 2020-05-01 14:31:19 -0700 | [diff] [blame] | 5791 | QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs)); |
fkastenholz | d3a1de9 | 2019-05-15 07:00:07 -0700 | [diff] [blame] | 5792 | client_config.ToHandshakeMessage(&msg, connection_.transport_version()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5793 | const QuicErrorCode error = |
| 5794 | config.ProcessPeerHello(msg, CLIENT, &error_details); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 5795 | EXPECT_THAT(error, IsQuicNoError()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5796 | |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 5797 | if (connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 5798 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 5799 | &config, connection_.connection_id()); |
| 5800 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 5801 | &config, connection_.connection_id()); |
| 5802 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5803 | connection_.SetFromConfig(config); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5804 | |
| 5805 | const QuicTime::Delta default_idle_timeout = |
dschinazi | 027366e | 2020-05-01 14:31:19 -0700 | [diff] [blame] | 5806 | QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs - 1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5807 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 5808 | QuicTime default_timeout = clock_.ApproximateNow() + default_idle_timeout; |
| 5809 | |
| 5810 | // When we send a packet, the timeout will change to 5ms + |
| 5811 | // kInitialIdleTimeoutSecs. |
| 5812 | clock_.AdvanceTime(five_ms); |
| 5813 | SendStreamDataToPeer( |
| 5814 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5815 | 0, FIN, nullptr); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 5816 | if (GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 5817 | EXPECT_EQ(default_timeout + five_ms, |
| 5818 | connection_.GetTimeoutAlarm()->deadline()); |
| 5819 | } else { |
| 5820 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 5821 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5822 | |
| 5823 | // Now send more data. This will not move the timeout because |
| 5824 | // no data has been received since the previous write. |
| 5825 | clock_.AdvanceTime(five_ms); |
| 5826 | SendStreamDataToPeer( |
| 5827 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5828 | 3, FIN, nullptr); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 5829 | if (GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 5830 | EXPECT_EQ(default_timeout + five_ms, |
| 5831 | connection_.GetTimeoutAlarm()->deadline()); |
| 5832 | } else { |
| 5833 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 5834 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5835 | |
| 5836 | // The original alarm will fire. We should not time out because we had a |
| 5837 | // network event at t=5ms. The alarm will reregister. |
| 5838 | clock_.AdvanceTime(default_idle_timeout - five_ms - five_ms); |
| 5839 | EXPECT_EQ(default_timeout, clock_.ApproximateNow()); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 5840 | if (!GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 5841 | connection_.GetTimeoutAlarm()->Fire(); |
| 5842 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5843 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5844 | EXPECT_TRUE(connection_.connected()); |
| 5845 | EXPECT_EQ(default_timeout + five_ms, |
| 5846 | connection_.GetTimeoutAlarm()->deadline()); |
| 5847 | |
| 5848 | // This time, we should time out. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5849 | // This results in a SILENT_CLOSE, so the writer will not be invoked |
| 5850 | // and will not save the frame. Grab the frame from OnConnectionClosed |
| 5851 | // directly. |
| 5852 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 5853 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 5854 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5855 | clock_.AdvanceTime(five_ms); |
| 5856 | EXPECT_EQ(default_timeout + five_ms, clock_.ApproximateNow()); |
| 5857 | connection_.GetTimeoutAlarm()->Fire(); |
| 5858 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5859 | EXPECT_FALSE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5860 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 5861 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 5862 | IsError(QUIC_NETWORK_IDLE_TIMEOUT)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5863 | } |
| 5864 | |
| 5865 | TEST_P(QuicConnectionTest, TimeoutAfterSendSilentCloseAndTLP) { |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 5866 | if (connection_.PtoEnabled()) { |
| 5867 | return; |
| 5868 | } |
dschinazi | 9a6194e | 2020-04-30 16:21:09 -0700 | [diff] [blame] | 5869 | // Same test as above, but sending TLPs causes a connection close to be sent. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5870 | EXPECT_TRUE(connection_.connected()); |
| 5871 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 5872 | QuicConfig config; |
| 5873 | |
| 5874 | // Create a handshake message that also enables silent close. |
| 5875 | CryptoHandshakeMessage msg; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 5876 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5877 | QuicConfig client_config; |
| 5878 | client_config.SetInitialStreamFlowControlWindowToSend( |
| 5879 | kInitialStreamFlowControlWindowForTest); |
| 5880 | client_config.SetInitialSessionFlowControlWindowToSend( |
| 5881 | kInitialSessionFlowControlWindowForTest); |
| 5882 | client_config.SetIdleNetworkTimeout( |
dschinazi | 027366e | 2020-05-01 14:31:19 -0700 | [diff] [blame] | 5883 | QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs)); |
fkastenholz | d3a1de9 | 2019-05-15 07:00:07 -0700 | [diff] [blame] | 5884 | client_config.ToHandshakeMessage(&msg, connection_.transport_version()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5885 | const QuicErrorCode error = |
| 5886 | config.ProcessPeerHello(msg, CLIENT, &error_details); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 5887 | EXPECT_THAT(error, IsQuicNoError()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5888 | |
| 5889 | connection_.SetFromConfig(config); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5890 | |
| 5891 | const QuicTime::Delta default_idle_timeout = |
dschinazi | 027366e | 2020-05-01 14:31:19 -0700 | [diff] [blame] | 5892 | QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs - 1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5893 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 5894 | QuicTime default_timeout = clock_.ApproximateNow() + default_idle_timeout; |
| 5895 | |
| 5896 | // When we send a packet, the timeout will change to 5ms + |
| 5897 | // kInitialIdleTimeoutSecs. |
| 5898 | clock_.AdvanceTime(five_ms); |
| 5899 | SendStreamDataToPeer( |
| 5900 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5901 | 0, FIN, nullptr); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 5902 | if (GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 5903 | EXPECT_EQ(default_timeout + five_ms, |
| 5904 | connection_.GetTimeoutAlarm()->deadline()); |
| 5905 | } else { |
| 5906 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 5907 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5908 | |
| 5909 | // Retransmit the packet via tail loss probe. |
| 5910 | clock_.AdvanceTime(connection_.GetRetransmissionAlarm()->deadline() - |
| 5911 | clock_.Now()); |
| 5912 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(2u), _, _)); |
| 5913 | connection_.GetRetransmissionAlarm()->Fire(); |
| 5914 | |
| 5915 | // This time, we should time out and send a connection close due to the TLP. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5916 | EXPECT_CALL(visitor_, |
| 5917 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 5918 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5919 | clock_.AdvanceTime(connection_.GetTimeoutAlarm()->deadline() - |
| 5920 | clock_.ApproximateNow() + five_ms); |
| 5921 | connection_.GetTimeoutAlarm()->Fire(); |
| 5922 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5923 | EXPECT_FALSE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5924 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5925 | } |
| 5926 | |
| 5927 | TEST_P(QuicConnectionTest, TimeoutAfterSendSilentCloseWithOpenStreams) { |
dschinazi | 9a6194e | 2020-04-30 16:21:09 -0700 | [diff] [blame] | 5928 | // Same test as above, but having open streams causes a connection close |
| 5929 | // to be sent. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5930 | EXPECT_TRUE(connection_.connected()); |
| 5931 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 5932 | QuicConfig config; |
| 5933 | |
| 5934 | // Create a handshake message that also enables silent close. |
| 5935 | CryptoHandshakeMessage msg; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 5936 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5937 | QuicConfig client_config; |
| 5938 | client_config.SetInitialStreamFlowControlWindowToSend( |
| 5939 | kInitialStreamFlowControlWindowForTest); |
| 5940 | client_config.SetInitialSessionFlowControlWindowToSend( |
| 5941 | kInitialSessionFlowControlWindowForTest); |
| 5942 | client_config.SetIdleNetworkTimeout( |
dschinazi | 027366e | 2020-05-01 14:31:19 -0700 | [diff] [blame] | 5943 | QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs)); |
fkastenholz | d3a1de9 | 2019-05-15 07:00:07 -0700 | [diff] [blame] | 5944 | client_config.ToHandshakeMessage(&msg, connection_.transport_version()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5945 | const QuicErrorCode error = |
| 5946 | config.ProcessPeerHello(msg, CLIENT, &error_details); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 5947 | EXPECT_THAT(error, IsQuicNoError()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5948 | |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 5949 | if (connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 5950 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 5951 | &config, connection_.connection_id()); |
| 5952 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 5953 | &config, connection_.connection_id()); |
| 5954 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5955 | connection_.SetFromConfig(config); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5956 | |
| 5957 | const QuicTime::Delta default_idle_timeout = |
dschinazi | 027366e | 2020-05-01 14:31:19 -0700 | [diff] [blame] | 5958 | QuicTime::Delta::FromSeconds(kMaximumIdleTimeoutSecs - 1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5959 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 5960 | QuicTime default_timeout = clock_.ApproximateNow() + default_idle_timeout; |
| 5961 | |
| 5962 | // When we send a packet, the timeout will change to 5ms + |
| 5963 | // kInitialIdleTimeoutSecs. |
| 5964 | clock_.AdvanceTime(five_ms); |
| 5965 | SendStreamDataToPeer( |
| 5966 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 5967 | 0, FIN, nullptr); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 5968 | if (GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 5969 | EXPECT_EQ(default_timeout + five_ms, |
| 5970 | connection_.GetTimeoutAlarm()->deadline()); |
| 5971 | } else { |
| 5972 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 5973 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5974 | |
| 5975 | // Indicate streams are still open. |
| 5976 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 5977 | .WillRepeatedly(Return(true)); |
| 5978 | |
| 5979 | // This time, we should time out and send a connection close due to the TLP. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5980 | EXPECT_CALL(visitor_, |
| 5981 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 5982 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5983 | clock_.AdvanceTime(connection_.GetTimeoutAlarm()->deadline() - |
| 5984 | clock_.ApproximateNow() + five_ms); |
| 5985 | connection_.GetTimeoutAlarm()->Fire(); |
| 5986 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 5987 | EXPECT_FALSE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 5988 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5989 | } |
| 5990 | |
| 5991 | TEST_P(QuicConnectionTest, TimeoutAfterReceive) { |
| 5992 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 5993 | EXPECT_TRUE(connection_.connected()); |
| 5994 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 5995 | QuicConfig config; |
| 5996 | connection_.SetFromConfig(config); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5997 | |
| 5998 | const QuicTime::Delta initial_idle_timeout = |
| 5999 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 6000 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 6001 | QuicTime default_timeout = clock_.ApproximateNow() + initial_idle_timeout; |
| 6002 | |
| 6003 | connection_.SendStreamDataWithString( |
| 6004 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 6005 | 0, NO_FIN); |
| 6006 | connection_.SendStreamDataWithString( |
| 6007 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 6008 | 3, NO_FIN); |
| 6009 | |
| 6010 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 6011 | clock_.AdvanceTime(five_ms); |
| 6012 | |
| 6013 | // When we receive a packet, the timeout will change to 5ms + |
| 6014 | // kInitialIdleTimeoutSecs. |
| 6015 | QuicAckFrame ack = InitAckFrame(2); |
| 6016 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 6017 | ProcessAckPacket(&ack); |
| 6018 | |
| 6019 | // The original alarm will fire. We should not time out because we had a |
| 6020 | // network event at t=5ms. The alarm will reregister. |
| 6021 | clock_.AdvanceTime(initial_idle_timeout - five_ms); |
| 6022 | EXPECT_EQ(default_timeout, clock_.ApproximateNow()); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 6023 | if (!GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 6024 | connection_.GetTimeoutAlarm()->Fire(); |
| 6025 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6026 | EXPECT_TRUE(connection_.connected()); |
| 6027 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 6028 | EXPECT_EQ(default_timeout + five_ms, |
| 6029 | connection_.GetTimeoutAlarm()->deadline()); |
| 6030 | |
| 6031 | // This time, we should time out. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 6032 | EXPECT_CALL(visitor_, |
| 6033 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 6034 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6035 | clock_.AdvanceTime(five_ms); |
| 6036 | EXPECT_EQ(default_timeout + five_ms, clock_.ApproximateNow()); |
| 6037 | connection_.GetTimeoutAlarm()->Fire(); |
| 6038 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 6039 | EXPECT_FALSE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 6040 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6041 | } |
| 6042 | |
| 6043 | TEST_P(QuicConnectionTest, TimeoutAfterReceiveNotSendWhenUnacked) { |
| 6044 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6045 | EXPECT_TRUE(connection_.connected()); |
| 6046 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 6047 | QuicConfig config; |
| 6048 | connection_.SetFromConfig(config); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6049 | |
| 6050 | const QuicTime::Delta initial_idle_timeout = |
| 6051 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 6052 | connection_.SetNetworkTimeouts( |
| 6053 | QuicTime::Delta::Infinite(), |
| 6054 | initial_idle_timeout + QuicTime::Delta::FromSeconds(1)); |
| 6055 | const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); |
| 6056 | QuicTime default_timeout = clock_.ApproximateNow() + initial_idle_timeout; |
| 6057 | |
| 6058 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 6059 | connection_.SendStreamDataWithString( |
| 6060 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 6061 | 0, NO_FIN); |
| 6062 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 6063 | connection_.SendStreamDataWithString( |
| 6064 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 6065 | 3, NO_FIN); |
| 6066 | |
| 6067 | EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); |
| 6068 | |
| 6069 | clock_.AdvanceTime(five_ms); |
| 6070 | |
| 6071 | // When we receive a packet, the timeout will change to 5ms + |
| 6072 | // kInitialIdleTimeoutSecs. |
| 6073 | QuicAckFrame ack = InitAckFrame(2); |
| 6074 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 6075 | ProcessAckPacket(&ack); |
| 6076 | |
| 6077 | // The original alarm will fire. We should not time out because we had a |
| 6078 | // network event at t=5ms. The alarm will reregister. |
| 6079 | clock_.AdvanceTime(initial_idle_timeout - five_ms); |
| 6080 | EXPECT_EQ(default_timeout, clock_.ApproximateNow()); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 6081 | if (!GetQuicReloadableFlag(quic_use_idle_network_detector)) { |
fayang | b9c8844 | 2020-03-26 07:03:57 -0700 | [diff] [blame] | 6082 | connection_.GetTimeoutAlarm()->Fire(); |
| 6083 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6084 | EXPECT_TRUE(connection_.connected()); |
| 6085 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 6086 | EXPECT_EQ(default_timeout + five_ms, |
| 6087 | connection_.GetTimeoutAlarm()->deadline()); |
| 6088 | |
| 6089 | // Now, send packets while advancing the time and verify that the connection |
| 6090 | // eventually times out. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 6091 | EXPECT_CALL(visitor_, |
| 6092 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6093 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); |
| 6094 | for (int i = 0; i < 100 && connection_.connected(); ++i) { |
| 6095 | QUIC_LOG(INFO) << "sending data packet"; |
| 6096 | connection_.SendStreamDataWithString( |
| 6097 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), |
| 6098 | "foo", 0, NO_FIN); |
| 6099 | connection_.GetTimeoutAlarm()->Fire(); |
| 6100 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 6101 | } |
| 6102 | EXPECT_FALSE(connection_.connected()); |
| 6103 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 6104 | TestConnectionCloseQuicErrorCode(QUIC_NETWORK_IDLE_TIMEOUT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6105 | } |
| 6106 | |
| 6107 | TEST_P(QuicConnectionTest, TimeoutAfter5ClientRTOs) { |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 6108 | if (connection_.PtoEnabled()) { |
| 6109 | return; |
| 6110 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6111 | connection_.SetMaxTailLossProbes(2); |
| 6112 | EXPECT_TRUE(connection_.connected()); |
| 6113 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 6114 | QuicConfig config; |
| 6115 | QuicTagVector connection_options; |
| 6116 | connection_options.push_back(k5RTO); |
| 6117 | config.SetConnectionOptionsToSend(connection_options); |
dschinazi | f7c6a91 | 2020-05-05 11:39:53 -0700 | [diff] [blame] | 6118 | QuicConfigPeer::SetNegotiated(&config, true); |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 6119 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
fayang | f78b693 | 2020-06-08 08:36:45 -0700 | [diff] [blame] | 6120 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 6121 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 6122 | } |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 6123 | if (connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 6124 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 6125 | &config, connection_.connection_id()); |
| 6126 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 6127 | &config, connection_.connection_id()); |
| 6128 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6129 | connection_.SetFromConfig(config); |
| 6130 | |
| 6131 | // Send stream data. |
| 6132 | SendStreamDataToPeer( |
| 6133 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 6134 | 0, FIN, nullptr); |
| 6135 | |
| 6136 | // Fire the retransmission alarm 6 times, twice for TLP and 4 times for RTO. |
| 6137 | for (int i = 0; i < 6; ++i) { |
| 6138 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 6139 | connection_.GetRetransmissionAlarm()->Fire(); |
| 6140 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 6141 | EXPECT_TRUE(connection_.connected()); |
| 6142 | } |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 6143 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 6144 | connection_.PathDegradingTimeout(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6145 | |
| 6146 | EXPECT_EQ(2u, connection_.sent_packet_manager().GetConsecutiveTlpCount()); |
| 6147 | EXPECT_EQ(4u, connection_.sent_packet_manager().GetConsecutiveRtoCount()); |
| 6148 | // This time, we should time out. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 6149 | EXPECT_CALL(visitor_, |
| 6150 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 6151 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 6152 | ASSERT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 6153 | connection_.GetBlackholeDetectorAlarm()->Fire(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6154 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 6155 | EXPECT_FALSE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 6156 | TestConnectionCloseQuicErrorCode(QUIC_TOO_MANY_RTOS); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6157 | } |
| 6158 | |
| 6159 | TEST_P(QuicConnectionTest, SendScheduler) { |
| 6160 | // Test that if we send a packet without delay, it is not queued. |
| 6161 | QuicFramerPeer::SetPerspective(&peer_framer_, Perspective::IS_CLIENT); |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 6162 | std::unique_ptr<QuicPacket> packet = |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6163 | ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6164 | QuicPacketCreatorPeer::SetPacketNumber(creator_, 1); |
| 6165 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6166 | connection_.SendPacket(ENCRYPTION_INITIAL, 1, std::move(packet), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6167 | HAS_RETRANSMITTABLE_DATA, false, false); |
| 6168 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 6169 | } |
| 6170 | |
| 6171 | TEST_P(QuicConnectionTest, FailToSendFirstPacket) { |
| 6172 | // Test that the connection does not crash when it fails to send the first |
| 6173 | // packet at which point self_address_ might be uninitialized. |
| 6174 | QuicFramerPeer::SetPerspective(&peer_framer_, Perspective::IS_CLIENT); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 6175 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(1); |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 6176 | std::unique_ptr<QuicPacket> packet = |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6177 | ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6178 | QuicPacketCreatorPeer::SetPacketNumber(creator_, 1); |
| 6179 | writer_->SetShouldWriteFail(); |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6180 | connection_.SendPacket(ENCRYPTION_INITIAL, 1, std::move(packet), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6181 | HAS_RETRANSMITTABLE_DATA, false, false); |
| 6182 | } |
| 6183 | |
| 6184 | TEST_P(QuicConnectionTest, SendSchedulerEAGAIN) { |
| 6185 | QuicFramerPeer::SetPerspective(&peer_framer_, Perspective::IS_CLIENT); |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 6186 | std::unique_ptr<QuicPacket> packet = |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6187 | ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6188 | QuicPacketCreatorPeer::SetPacketNumber(creator_, 1); |
| 6189 | BlockOnNextWrite(); |
| 6190 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(2u), _, _)) |
| 6191 | .Times(0); |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6192 | connection_.SendPacket(ENCRYPTION_INITIAL, 1, std::move(packet), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6193 | HAS_RETRANSMITTABLE_DATA, false, false); |
| 6194 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 6195 | } |
| 6196 | |
| 6197 | TEST_P(QuicConnectionTest, TestQueueLimitsOnSendStreamData) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6198 | // Queue the first packet. |
ianswett | 3085da8 | 2019-04-04 07:24:24 -0700 | [diff] [blame] | 6199 | size_t payload_length = connection_.max_packet_length(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6200 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(testing::Return(false)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 6201 | const std::string payload(payload_length, 'a'); |
ianswett | 3085da8 | 2019-04-04 07:24:24 -0700 | [diff] [blame] | 6202 | QuicStreamId first_bidi_stream_id(QuicUtils::GetFirstBidirectionalStreamId( |
| 6203 | connection_.version().transport_version, Perspective::IS_CLIENT)); |
| 6204 | EXPECT_EQ(0u, connection_ |
| 6205 | .SendStreamDataWithString(first_bidi_stream_id, payload, 0, |
| 6206 | NO_FIN) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6207 | .bytes_consumed); |
| 6208 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 6209 | } |
| 6210 | |
ianswett | 3085da8 | 2019-04-04 07:24:24 -0700 | [diff] [blame] | 6211 | TEST_P(QuicConnectionTest, SendingThreePackets) { |
ianswett | 3085da8 | 2019-04-04 07:24:24 -0700 | [diff] [blame] | 6212 | // Make the payload twice the size of the packet, so 3 packets are written. |
| 6213 | size_t total_payload_length = 2 * connection_.max_packet_length(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 6214 | const std::string payload(total_payload_length, 'a'); |
ianswett | 3085da8 | 2019-04-04 07:24:24 -0700 | [diff] [blame] | 6215 | QuicStreamId first_bidi_stream_id(QuicUtils::GetFirstBidirectionalStreamId( |
| 6216 | connection_.version().transport_version, Perspective::IS_CLIENT)); |
| 6217 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(3); |
| 6218 | EXPECT_EQ(payload.size(), connection_ |
| 6219 | .SendStreamDataWithString(first_bidi_stream_id, |
| 6220 | payload, 0, NO_FIN) |
| 6221 | .bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | TEST_P(QuicConnectionTest, LoopThroughSendingPacketsWithTruncation) { |
| 6225 | set_perspective(Perspective::IS_SERVER); |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 6226 | if (!VersionHasIetfInvariantHeader(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6227 | // For IETF QUIC, encryption level will be switched to FORWARD_SECURE in |
| 6228 | // SendStreamDataWithString. |
| 6229 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 6230 | } |
| 6231 | // Set up a larger payload than will fit in one packet. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 6232 | const std::string payload(connection_.max_packet_length(), 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6233 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); |
| 6234 | |
| 6235 | // Now send some packets with no truncation. |
| 6236 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 6237 | EXPECT_EQ(payload.size(), |
| 6238 | connection_.SendStreamDataWithString(3, payload, 0, NO_FIN) |
| 6239 | .bytes_consumed); |
| 6240 | // Track the size of the second packet here. The overhead will be the largest |
| 6241 | // we see in this test, due to the non-truncated connection id. |
| 6242 | size_t non_truncated_packet_size = writer_->last_packet_size(); |
| 6243 | |
| 6244 | // Change to a 0 byte connection id. |
| 6245 | QuicConfig config; |
| 6246 | QuicConfigPeer::SetReceivedBytesForConnectionId(&config, 0); |
| 6247 | connection_.SetFromConfig(config); |
| 6248 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 6249 | EXPECT_EQ(payload.size(), |
| 6250 | connection_.SendStreamDataWithString(3, payload, 1350, NO_FIN) |
| 6251 | .bytes_consumed); |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 6252 | if (VersionHasIetfInvariantHeader(connection_.transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6253 | // Short header packets sent from server omit connection ID already, and |
| 6254 | // stream offset size increases from 0 to 2. |
| 6255 | EXPECT_EQ(non_truncated_packet_size, writer_->last_packet_size() - 2); |
| 6256 | } else { |
| 6257 | // Just like above, we save 8 bytes on payload, and 8 on truncation. -2 |
| 6258 | // because stream offset size is 2 instead of 0. |
| 6259 | EXPECT_EQ(non_truncated_packet_size, |
| 6260 | writer_->last_packet_size() + 8 * 2 - 2); |
| 6261 | } |
| 6262 | } |
| 6263 | |
| 6264 | TEST_P(QuicConnectionTest, SendDelayedAck) { |
| 6265 | QuicTime ack_time = clock_.ApproximateNow() + DefaultDelayedAckTime(); |
| 6266 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6267 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6268 | const uint8_t tag = 0x07; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 6269 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6270 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6271 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6272 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6273 | // Process a packet from the non-crypto stream. |
| 6274 | frame1_.stream_id = 3; |
| 6275 | |
| 6276 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6277 | // instead of ENCRYPTION_INITIAL. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6278 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6279 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6280 | |
| 6281 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6282 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6283 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6284 | // Simulate delayed ack alarm firing. |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 6285 | clock_.AdvanceTime(DefaultDelayedAckTime()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6286 | connection_.GetAckAlarm()->Fire(); |
| 6287 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6288 | size_t padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6289 | if (GetParam().no_stop_waiting) { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6290 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6291 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6292 | } else { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6293 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6294 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6295 | } |
| 6296 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6297 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6298 | } |
| 6299 | |
| 6300 | TEST_P(QuicConnectionTest, SendDelayedAfterQuiescence) { |
| 6301 | QuicConnectionPeer::SetFastAckAfterQuiescence(&connection_, true); |
| 6302 | |
| 6303 | // The beginning of the connection counts as quiescence. |
ianswett | 8f90e51 | 2019-12-18 10:50:27 -0800 | [diff] [blame] | 6304 | QuicTime ack_time = clock_.ApproximateNow() + kAlarmGranularity; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6305 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6306 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6307 | const uint8_t tag = 0x07; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 6308 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6309 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6310 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6311 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6312 | // Process a packet from the non-crypto stream. |
| 6313 | frame1_.stream_id = 3; |
| 6314 | |
| 6315 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6316 | // instead of ENCRYPTION_INITIAL. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6317 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6318 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6319 | |
| 6320 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6321 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6322 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6323 | // Simulate delayed ack alarm firing. |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 6324 | clock_.AdvanceTime(DefaultDelayedAckTime()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6325 | connection_.GetAckAlarm()->Fire(); |
| 6326 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6327 | size_t padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6328 | if (GetParam().no_stop_waiting) { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6329 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6330 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6331 | } else { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6332 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6333 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6334 | } |
| 6335 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6336 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6337 | |
| 6338 | // Process another packet immedately after sending the ack and expect the |
| 6339 | // ack alarm to be set delayed ack time in the future. |
| 6340 | ack_time = clock_.ApproximateNow() + DefaultDelayedAckTime(); |
| 6341 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6342 | ProcessDataPacketAtLevel(2, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6343 | |
| 6344 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6345 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6346 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6347 | // Simulate delayed ack alarm firing. |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 6348 | clock_.AdvanceTime(DefaultDelayedAckTime()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6349 | connection_.GetAckAlarm()->Fire(); |
| 6350 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6351 | padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6352 | if (GetParam().no_stop_waiting) { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6353 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6354 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6355 | } else { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6356 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6357 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6358 | } |
| 6359 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6360 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6361 | |
ianswett | 8f90e51 | 2019-12-18 10:50:27 -0800 | [diff] [blame] | 6362 | // Wait 1 second and ensure the ack alarm is set to 1ms in the future. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6363 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 6364 | ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(1); |
| 6365 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6366 | ProcessDataPacketAtLevel(3, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6367 | |
| 6368 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6369 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6370 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6371 | } |
| 6372 | |
| 6373 | TEST_P(QuicConnectionTest, SendDelayedAckDecimation) { |
| 6374 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6375 | QuicConnectionPeer::SetAckMode(&connection_, ACK_DECIMATION); |
| 6376 | |
| 6377 | const size_t kMinRttMs = 40; |
| 6378 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6379 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6380 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6381 | // The ack time should be based on min_rtt/4, since it's less than the |
| 6382 | // default delayed ack time. |
| 6383 | QuicTime ack_time = clock_.ApproximateNow() + |
| 6384 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 4); |
| 6385 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6386 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6387 | const uint8_t tag = 0x07; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 6388 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6389 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6390 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6391 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6392 | // Process a packet from the non-crypto stream. |
| 6393 | frame1_.stream_id = 3; |
| 6394 | |
| 6395 | // Process all the initial packets in order so there aren't missing packets. |
| 6396 | uint64_t kFirstDecimatedPacket = 101; |
| 6397 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { |
| 6398 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6399 | ProcessDataPacketAtLevel(1 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6400 | } |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6401 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6402 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6403 | // instead of ENCRYPTION_INITIAL. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6404 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6405 | ProcessDataPacketAtLevel(kFirstDecimatedPacket, !kHasStopWaiting, |
| 6406 | ENCRYPTION_ZERO_RTT); |
| 6407 | |
| 6408 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6409 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6410 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6411 | |
| 6412 | // The 10th received packet causes an ack to be sent. |
| 6413 | for (int i = 0; i < 9; ++i) { |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6414 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6415 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6416 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 1 + i, !kHasStopWaiting, |
| 6417 | ENCRYPTION_ZERO_RTT); |
| 6418 | } |
| 6419 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6420 | size_t padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6421 | if (GetParam().no_stop_waiting) { |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6422 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6423 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6424 | } else { |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6425 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6426 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6427 | } |
| 6428 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6429 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | TEST_P(QuicConnectionTest, SendDelayedAckAckDecimationAfterQuiescence) { |
| 6433 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6434 | QuicConnectionPeer::SetAckMode(&connection_, ACK_DECIMATION); |
| 6435 | QuicConnectionPeer::SetFastAckAfterQuiescence(&connection_, true); |
| 6436 | |
| 6437 | const size_t kMinRttMs = 40; |
| 6438 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6439 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6440 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6441 | |
| 6442 | // The beginning of the connection counts as quiescence. |
| 6443 | QuicTime ack_time = |
| 6444 | clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(1); |
| 6445 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6446 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6447 | const uint8_t tag = 0x07; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 6448 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6449 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6450 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6451 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6452 | // Process a packet from the non-crypto stream. |
| 6453 | frame1_.stream_id = 3; |
| 6454 | |
| 6455 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6456 | // instead of ENCRYPTION_INITIAL. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6457 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6458 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6459 | |
| 6460 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6461 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6462 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6463 | // Simulate delayed ack alarm firing. |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 6464 | clock_.AdvanceTime(DefaultDelayedAckTime()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6465 | connection_.GetAckAlarm()->Fire(); |
| 6466 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6467 | size_t padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6468 | if (GetParam().no_stop_waiting) { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6469 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6470 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6471 | } else { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6472 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6473 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6474 | } |
| 6475 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6476 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6477 | |
| 6478 | // Process another packet immedately after sending the ack and expect the |
| 6479 | // ack alarm to be set delayed ack time in the future. |
| 6480 | ack_time = clock_.ApproximateNow() + DefaultDelayedAckTime(); |
| 6481 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6482 | ProcessDataPacketAtLevel(2, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6483 | |
| 6484 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6485 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6486 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6487 | // Simulate delayed ack alarm firing. |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 6488 | clock_.AdvanceTime(DefaultDelayedAckTime()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6489 | connection_.GetAckAlarm()->Fire(); |
| 6490 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6491 | padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6492 | if (GetParam().no_stop_waiting) { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6493 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6494 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6495 | } else { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 6496 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6497 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6498 | } |
| 6499 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6500 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6501 | |
| 6502 | // Wait 1 second and enesure the ack alarm is set to 1ms in the future. |
| 6503 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 6504 | ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(1); |
| 6505 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6506 | ProcessDataPacketAtLevel(3, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6507 | |
| 6508 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6509 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6510 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6511 | |
| 6512 | // Process enough packets to get into ack decimation behavior. |
| 6513 | // The ack time should be based on min_rtt/4, since it's less than the |
| 6514 | // default delayed ack time. |
| 6515 | ack_time = clock_.ApproximateNow() + |
| 6516 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 4); |
| 6517 | uint64_t kFirstDecimatedPacket = 101; |
| 6518 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 4; ++i) { |
| 6519 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6520 | ProcessDataPacketAtLevel(4 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6521 | } |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6522 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6523 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6524 | // instead of ENCRYPTION_INITIAL. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6525 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6526 | ProcessDataPacketAtLevel(kFirstDecimatedPacket, !kHasStopWaiting, |
| 6527 | ENCRYPTION_ZERO_RTT); |
| 6528 | |
| 6529 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6530 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6531 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6532 | |
| 6533 | // The 10th received packet causes an ack to be sent. |
| 6534 | for (int i = 0; i < 9; ++i) { |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6535 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6536 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6537 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 1 + i, !kHasStopWaiting, |
| 6538 | ENCRYPTION_ZERO_RTT); |
| 6539 | } |
| 6540 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6541 | padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6542 | if (GetParam().no_stop_waiting) { |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6543 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6544 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6545 | } else { |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6546 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6547 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6548 | } |
| 6549 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6550 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6551 | |
| 6552 | // Wait 1 second and enesure the ack alarm is set to 1ms in the future. |
| 6553 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 6554 | ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(1); |
| 6555 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6556 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 10, !kHasStopWaiting, |
| 6557 | ENCRYPTION_ZERO_RTT); |
| 6558 | |
| 6559 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6560 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6561 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6562 | } |
| 6563 | |
| 6564 | TEST_P(QuicConnectionTest, SendDelayedAckDecimationUnlimitedAggregation) { |
| 6565 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6566 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 6567 | QuicConfig config; |
| 6568 | QuicTagVector connection_options; |
| 6569 | connection_options.push_back(kACKD); |
| 6570 | // No limit on the number of packets received before sending an ack. |
| 6571 | connection_options.push_back(kAKDU); |
| 6572 | config.SetConnectionOptionsToSend(connection_options); |
| 6573 | connection_.SetFromConfig(config); |
| 6574 | |
| 6575 | const size_t kMinRttMs = 40; |
| 6576 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6577 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6578 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6579 | // The ack time should be based on min_rtt/4, since it's less than the |
| 6580 | // default delayed ack time. |
| 6581 | QuicTime ack_time = clock_.ApproximateNow() + |
| 6582 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 4); |
| 6583 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6584 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6585 | const uint8_t tag = 0x07; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 6586 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6587 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6588 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6589 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6590 | // Process a packet from the non-crypto stream. |
| 6591 | frame1_.stream_id = 3; |
| 6592 | |
| 6593 | // Process all the initial packets in order so there aren't missing packets. |
| 6594 | uint64_t kFirstDecimatedPacket = 101; |
| 6595 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { |
| 6596 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6597 | ProcessDataPacketAtLevel(1 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6598 | } |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6599 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6600 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6601 | // instead of ENCRYPTION_INITIAL. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6602 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6603 | ProcessDataPacketAtLevel(kFirstDecimatedPacket, !kHasStopWaiting, |
| 6604 | ENCRYPTION_ZERO_RTT); |
| 6605 | |
| 6606 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6607 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6608 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6609 | |
| 6610 | // 18 packets will not cause an ack to be sent. 19 will because when |
| 6611 | // stop waiting frames are in use, we ack every 20 packets no matter what. |
| 6612 | for (int i = 0; i < 18; ++i) { |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6613 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6614 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6615 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 1 + i, !kHasStopWaiting, |
| 6616 | ENCRYPTION_ZERO_RTT); |
| 6617 | } |
| 6618 | // The delayed ack timer should still be set to the expected deadline. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6619 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6620 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6621 | } |
| 6622 | |
| 6623 | TEST_P(QuicConnectionTest, SendDelayedAckDecimationEighthRtt) { |
| 6624 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6625 | QuicConnectionPeer::SetAckMode(&connection_, ACK_DECIMATION); |
| 6626 | QuicConnectionPeer::SetAckDecimationDelay(&connection_, 0.125); |
| 6627 | |
| 6628 | const size_t kMinRttMs = 40; |
| 6629 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6630 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6631 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6632 | // The ack time should be based on min_rtt/8, since it's less than the |
| 6633 | // default delayed ack time. |
| 6634 | QuicTime ack_time = clock_.ApproximateNow() + |
| 6635 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 8); |
| 6636 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6637 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6638 | const uint8_t tag = 0x07; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 6639 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6640 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6641 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6642 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6643 | // Process a packet from the non-crypto stream. |
| 6644 | frame1_.stream_id = 3; |
| 6645 | |
| 6646 | // Process all the initial packets in order so there aren't missing packets. |
| 6647 | uint64_t kFirstDecimatedPacket = 101; |
| 6648 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { |
| 6649 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6650 | ProcessDataPacketAtLevel(1 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6651 | } |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6652 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6653 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6654 | // instead of ENCRYPTION_INITIAL. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6655 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6656 | ProcessDataPacketAtLevel(kFirstDecimatedPacket, !kHasStopWaiting, |
| 6657 | ENCRYPTION_ZERO_RTT); |
| 6658 | |
| 6659 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6660 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6661 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6662 | |
| 6663 | // The 10th received packet causes an ack to be sent. |
| 6664 | for (int i = 0; i < 9; ++i) { |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6665 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6666 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6667 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 1 + i, !kHasStopWaiting, |
| 6668 | ENCRYPTION_ZERO_RTT); |
| 6669 | } |
| 6670 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6671 | size_t padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6672 | if (GetParam().no_stop_waiting) { |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6673 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6674 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6675 | } else { |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6676 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6677 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6678 | } |
| 6679 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6680 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6681 | } |
| 6682 | |
| 6683 | TEST_P(QuicConnectionTest, SendDelayedAckDecimationWithReordering) { |
| 6684 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6685 | QuicConnectionPeer::SetAckMode(&connection_, ACK_DECIMATION_WITH_REORDERING); |
| 6686 | |
| 6687 | const size_t kMinRttMs = 40; |
| 6688 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6689 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6690 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6691 | // The ack time should be based on min_rtt/4, since it's less than the |
| 6692 | // default delayed ack time. |
| 6693 | QuicTime ack_time = clock_.ApproximateNow() + |
| 6694 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 4); |
| 6695 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6696 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6697 | const uint8_t tag = 0x07; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 6698 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6699 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6700 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6701 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6702 | // Process a packet from the non-crypto stream. |
| 6703 | frame1_.stream_id = 3; |
| 6704 | |
| 6705 | // Process all the initial packets in order so there aren't missing packets. |
| 6706 | uint64_t kFirstDecimatedPacket = 101; |
| 6707 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { |
| 6708 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6709 | ProcessDataPacketAtLevel(1 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6710 | } |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6711 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6712 | |
| 6713 | // Receive one packet out of order and then the rest in order. |
| 6714 | // The loop leaves a one packet gap between acks sent to simulate some loss. |
| 6715 | for (int j = 0; j < 3; ++j) { |
| 6716 | // Process packet 10 first and ensure the alarm is one eighth min_rtt. |
| 6717 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6718 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 9 + (j * 11), |
| 6719 | !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6720 | ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(5); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6721 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6722 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6723 | |
| 6724 | // The 10th received packet causes an ack to be sent. |
| 6725 | writer_->Reset(); |
| 6726 | for (int i = 0; i < 9; ++i) { |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6727 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6728 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6729 | // The ACK shouldn't be sent until the 10th packet is processed. |
| 6730 | EXPECT_TRUE(writer_->ack_frames().empty()); |
| 6731 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + i + (j * 11), |
| 6732 | !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6733 | } |
| 6734 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6735 | size_t padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6736 | if (GetParam().no_stop_waiting) { |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6737 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6738 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6739 | } else { |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6740 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6741 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6742 | } |
| 6743 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6744 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6745 | } |
| 6746 | } |
| 6747 | |
| 6748 | TEST_P(QuicConnectionTest, SendDelayedAckDecimationWithLargeReordering) { |
| 6749 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6750 | QuicConnectionPeer::SetAckMode(&connection_, ACK_DECIMATION_WITH_REORDERING); |
| 6751 | |
| 6752 | const size_t kMinRttMs = 40; |
| 6753 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6754 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6755 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6756 | // The ack time should be based on min_rtt/4, since it's less than the |
| 6757 | // default delayed ack time. |
| 6758 | QuicTime ack_time = clock_.ApproximateNow() + |
| 6759 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 4); |
| 6760 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6761 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6762 | const uint8_t tag = 0x07; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 6763 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6764 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6765 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6766 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6767 | // Process a packet from the non-crypto stream. |
| 6768 | frame1_.stream_id = 3; |
| 6769 | |
| 6770 | // Process all the initial packets in order so there aren't missing packets. |
| 6771 | uint64_t kFirstDecimatedPacket = 101; |
| 6772 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { |
| 6773 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6774 | ProcessDataPacketAtLevel(1 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6775 | } |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6776 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6777 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6778 | // instead of ENCRYPTION_INITIAL. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6779 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6780 | ProcessDataPacketAtLevel(kFirstDecimatedPacket, !kHasStopWaiting, |
| 6781 | ENCRYPTION_ZERO_RTT); |
| 6782 | |
| 6783 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6784 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6785 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6786 | |
| 6787 | // Process packet 10 first and ensure the alarm is one eighth min_rtt. |
| 6788 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6789 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 19, !kHasStopWaiting, |
| 6790 | ENCRYPTION_ZERO_RTT); |
| 6791 | ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(5); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6792 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6793 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6794 | |
| 6795 | // The 10th received packet causes an ack to be sent. |
| 6796 | for (int i = 0; i < 8; ++i) { |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6797 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6798 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6799 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 1 + i, !kHasStopWaiting, |
| 6800 | ENCRYPTION_ZERO_RTT); |
| 6801 | } |
| 6802 | // Check that ack is sent and that delayed ack alarm is reset. |
| 6803 | if (GetParam().no_stop_waiting) { |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 6804 | EXPECT_EQ(writer_->padding_frames().size() + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6805 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6806 | } else { |
| 6807 | EXPECT_EQ(2u, writer_->frame_count()); |
| 6808 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6809 | } |
| 6810 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6811 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6812 | |
| 6813 | // The next packet received in order will cause an immediate ack, |
| 6814 | // because it fills a hole. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6815 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6816 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6817 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 10, !kHasStopWaiting, |
| 6818 | ENCRYPTION_ZERO_RTT); |
| 6819 | // Check that ack is sent and that delayed ack alarm is reset. |
| 6820 | if (GetParam().no_stop_waiting) { |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 6821 | EXPECT_EQ(writer_->padding_frames().size() + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6822 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6823 | } else { |
| 6824 | EXPECT_EQ(2u, writer_->frame_count()); |
| 6825 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6826 | } |
| 6827 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6828 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6829 | } |
| 6830 | |
| 6831 | TEST_P(QuicConnectionTest, SendDelayedAckDecimationWithReorderingEighthRtt) { |
| 6832 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6833 | QuicConnectionPeer::SetAckMode(&connection_, ACK_DECIMATION_WITH_REORDERING); |
| 6834 | QuicConnectionPeer::SetAckDecimationDelay(&connection_, 0.125); |
| 6835 | |
| 6836 | const size_t kMinRttMs = 40; |
| 6837 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6838 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6839 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6840 | // The ack time should be based on min_rtt/8, since it's less than the |
| 6841 | // default delayed ack time. |
| 6842 | QuicTime ack_time = clock_.ApproximateNow() + |
| 6843 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 8); |
| 6844 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6845 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6846 | const uint8_t tag = 0x07; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 6847 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6848 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6849 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6850 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6851 | // Process a packet from the non-crypto stream. |
| 6852 | frame1_.stream_id = 3; |
| 6853 | |
| 6854 | // Process all the initial packets in order so there aren't missing packets. |
| 6855 | uint64_t kFirstDecimatedPacket = 101; |
| 6856 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { |
| 6857 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6858 | ProcessDataPacketAtLevel(1 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6859 | } |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6860 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6861 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6862 | // instead of ENCRYPTION_INITIAL. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6863 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6864 | ProcessDataPacketAtLevel(kFirstDecimatedPacket, !kHasStopWaiting, |
| 6865 | ENCRYPTION_ZERO_RTT); |
| 6866 | |
| 6867 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6868 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6869 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6870 | |
| 6871 | // Process packet 10 first and ensure the alarm is one eighth min_rtt. |
| 6872 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6873 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 9, !kHasStopWaiting, |
| 6874 | ENCRYPTION_ZERO_RTT); |
| 6875 | ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(5); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6876 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6877 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6878 | |
| 6879 | // The 10th received packet causes an ack to be sent. |
| 6880 | for (int i = 0; i < 8; ++i) { |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6881 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6882 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6883 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 1 + i, !kHasStopWaiting, |
| 6884 | ENCRYPTION_ZERO_RTT); |
| 6885 | } |
| 6886 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6887 | size_t padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6888 | if (GetParam().no_stop_waiting) { |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6889 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6890 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6891 | } else { |
nharper | c32d8ab | 2019-10-09 11:09:06 -0700 | [diff] [blame] | 6892 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6893 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6894 | } |
| 6895 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6896 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6897 | } |
| 6898 | |
| 6899 | TEST_P(QuicConnectionTest, |
| 6900 | SendDelayedAckDecimationWithLargeReorderingEighthRtt) { |
| 6901 | EXPECT_CALL(visitor_, OnAckNeedsRetransmittableFrame()).Times(AnyNumber()); |
| 6902 | QuicConnectionPeer::SetAckMode(&connection_, ACK_DECIMATION_WITH_REORDERING); |
| 6903 | QuicConnectionPeer::SetAckDecimationDelay(&connection_, 0.125); |
| 6904 | |
| 6905 | const size_t kMinRttMs = 40; |
| 6906 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 6907 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 6908 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 6909 | // The ack time should be based on min_rtt/8, since it's less than the |
| 6910 | // default delayed ack time. |
| 6911 | QuicTime ack_time = clock_.ApproximateNow() + |
| 6912 | QuicTime::Delta::FromMilliseconds(kMinRttMs / 8); |
| 6913 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6914 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6915 | const uint8_t tag = 0x07; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 6916 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6917 | std::make_unique<StrictTaggingDecrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6918 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 6919 | std::make_unique<TaggingEncrypter>(tag)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6920 | // Process a packet from the non-crypto stream. |
| 6921 | frame1_.stream_id = 3; |
| 6922 | |
| 6923 | // Process all the initial packets in order so there aren't missing packets. |
| 6924 | uint64_t kFirstDecimatedPacket = 101; |
| 6925 | for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { |
| 6926 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6927 | ProcessDataPacketAtLevel(1 + i, !kHasStopWaiting, ENCRYPTION_ZERO_RTT); |
| 6928 | } |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6929 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6930 | // The same as ProcessPacket(1) except that ENCRYPTION_ZERO_RTT is used |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 6931 | // instead of ENCRYPTION_INITIAL. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6932 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6933 | ProcessDataPacketAtLevel(kFirstDecimatedPacket, !kHasStopWaiting, |
| 6934 | ENCRYPTION_ZERO_RTT); |
| 6935 | |
| 6936 | // Check if delayed ack timer is running for the expected interval. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6937 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6938 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6939 | |
| 6940 | // Process packet 10 first and ensure the alarm is one eighth min_rtt. |
| 6941 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6942 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 19, !kHasStopWaiting, |
| 6943 | ENCRYPTION_ZERO_RTT); |
| 6944 | ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(5); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6945 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6946 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6947 | |
| 6948 | // The 10th received packet causes an ack to be sent. |
| 6949 | for (int i = 0; i < 8; ++i) { |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6950 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6951 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6952 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 1 + i, !kHasStopWaiting, |
| 6953 | ENCRYPTION_ZERO_RTT); |
| 6954 | } |
| 6955 | // Check that ack is sent and that delayed ack alarm is reset. |
| 6956 | if (GetParam().no_stop_waiting) { |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 6957 | EXPECT_EQ(writer_->padding_frames().size() + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6958 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6959 | } else { |
| 6960 | EXPECT_EQ(2u, writer_->frame_count()); |
| 6961 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6962 | } |
| 6963 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6964 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6965 | |
| 6966 | // The next packet received in order will cause an immediate ack, |
| 6967 | // because it fills a hole. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6968 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6969 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 6970 | ProcessDataPacketAtLevel(kFirstDecimatedPacket + 10, !kHasStopWaiting, |
| 6971 | ENCRYPTION_ZERO_RTT); |
| 6972 | // Check that ack is sent and that delayed ack alarm is reset. |
| 6973 | if (GetParam().no_stop_waiting) { |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 6974 | EXPECT_EQ(writer_->padding_frames().size() + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6975 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 6976 | } else { |
| 6977 | EXPECT_EQ(2u, writer_->frame_count()); |
| 6978 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 6979 | } |
| 6980 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6981 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6982 | } |
| 6983 | |
| 6984 | TEST_P(QuicConnectionTest, SendDelayedAckOnHandshakeConfirmed) { |
| 6985 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 6986 | ProcessPacket(1); |
| 6987 | // Check that ack is sent and that delayed ack alarm is set. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6988 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6989 | QuicTime ack_time = clock_.ApproximateNow() + DefaultDelayedAckTime(); |
| 6990 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6991 | |
| 6992 | // Completing the handshake as the server does nothing. |
| 6993 | QuicConnectionPeer::SetPerspective(&connection_, Perspective::IS_SERVER); |
| 6994 | connection_.OnHandshakeComplete(); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 6995 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6996 | EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); |
| 6997 | |
| 6998 | // Complete the handshake as the client decreases the delayed ack time to 0ms. |
| 6999 | QuicConnectionPeer::SetPerspective(&connection_, Perspective::IS_CLIENT); |
| 7000 | connection_.OnHandshakeComplete(); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7001 | EXPECT_TRUE(connection_.HasPendingAcks()); |
fayang | b296fb8 | 2020-02-11 08:14:28 -0800 | [diff] [blame] | 7002 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7003 | EXPECT_EQ(clock_.ApproximateNow() + DefaultDelayedAckTime(), |
| 7004 | connection_.GetAckAlarm()->deadline()); |
| 7005 | } else { |
| 7006 | EXPECT_EQ(clock_.ApproximateNow(), connection_.GetAckAlarm()->deadline()); |
| 7007 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7008 | } |
| 7009 | |
| 7010 | TEST_P(QuicConnectionTest, SendDelayedAckOnSecondPacket) { |
| 7011 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7012 | ProcessPacket(1); |
| 7013 | ProcessPacket(2); |
| 7014 | // Check that ack is sent and that delayed ack alarm is reset. |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 7015 | size_t padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7016 | if (GetParam().no_stop_waiting) { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 7017 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7018 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 7019 | } else { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 7020 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7021 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 7022 | } |
| 7023 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7024 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7025 | } |
| 7026 | |
| 7027 | TEST_P(QuicConnectionTest, NoAckOnOldNacks) { |
| 7028 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 6dba490 | 2019-06-17 10:04:23 -0700 | [diff] [blame] | 7029 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7030 | ProcessPacket(2); |
| 7031 | size_t frames_per_ack = GetParam().no_stop_waiting ? 1 : 2; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7032 | |
| 7033 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7034 | ProcessPacket(3); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 7035 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 7036 | EXPECT_EQ(padding_frame_count + frames_per_ack, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7037 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 7038 | writer_->Reset(); |
| 7039 | |
fayang | 6dba490 | 2019-06-17 10:04:23 -0700 | [diff] [blame] | 7040 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7041 | ProcessPacket(4); |
fayang | 6dba490 | 2019-06-17 10:04:23 -0700 | [diff] [blame] | 7042 | EXPECT_EQ(0u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7043 | |
| 7044 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7045 | ProcessPacket(5); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 7046 | padding_frame_count = writer_->padding_frames().size(); |
| 7047 | EXPECT_EQ(padding_frame_count + frames_per_ack, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7048 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 7049 | writer_->Reset(); |
| 7050 | |
| 7051 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 7052 | // Now only set the timer on the 6th packet, instead of sending another ack. |
| 7053 | ProcessPacket(6); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 7054 | padding_frame_count = writer_->padding_frames().size(); |
| 7055 | EXPECT_EQ(padding_frame_count, writer_->frame_count()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7056 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7057 | } |
| 7058 | |
| 7059 | TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingPacket) { |
| 7060 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 7061 | EXPECT_CALL(visitor_, OnStreamFrame(_)); |
| 7062 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 7063 | std::make_unique<TaggingEncrypter>(0x01)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 7064 | SetDecrypter(ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 7065 | std::make_unique<StrictTaggingDecrypter>(0x01)); |
nharper | 2c9f02a | 2019-05-08 10:25:50 -0700 | [diff] [blame] | 7066 | ProcessDataPacket(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7067 | connection_.SendStreamDataWithString( |
| 7068 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 7069 | 0, NO_FIN); |
| 7070 | // Check that ack is bundled with outgoing data and that delayed ack |
| 7071 | // alarm is reset. |
| 7072 | if (GetParam().no_stop_waiting) { |
| 7073 | EXPECT_EQ(2u, writer_->frame_count()); |
| 7074 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 7075 | } else { |
| 7076 | EXPECT_EQ(3u, writer_->frame_count()); |
| 7077 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 7078 | } |
| 7079 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7080 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7081 | } |
| 7082 | |
| 7083 | TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingCryptoPacket) { |
| 7084 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 7085 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 7086 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 7087 | } else { |
| 7088 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 7089 | } |
| 7090 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 7091 | connection_.SendCryptoDataWithString("foo", 0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7092 | // Check that ack is bundled with outgoing crypto data. |
| 7093 | if (GetParam().no_stop_waiting) { |
| 7094 | EXPECT_EQ(3u, writer_->frame_count()); |
| 7095 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 7096 | } else { |
| 7097 | EXPECT_EQ(4u, writer_->frame_count()); |
| 7098 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 7099 | } |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7100 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7101 | } |
| 7102 | |
| 7103 | TEST_P(QuicConnectionTest, BlockAndBufferOnFirstCHLOPacketOfTwo) { |
| 7104 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7105 | ProcessPacket(1); |
| 7106 | BlockOnNextWrite(); |
| 7107 | writer_->set_is_write_blocked_data_buffered(true); |
fayang | 6a25841 | 2020-05-28 08:57:12 -0700 | [diff] [blame] | 7108 | if (GetQuicReloadableFlag(quic_move_amplification_limit) && |
| 7109 | QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 7110 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7111 | } else { |
| 7112 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 7113 | } |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 7114 | connection_.SendCryptoDataWithString("foo", 0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7115 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 7116 | EXPECT_FALSE(connection_.HasQueuedData()); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 7117 | connection_.SendCryptoDataWithString("bar", 3); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7118 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
fayang | 6a25841 | 2020-05-28 08:57:12 -0700 | [diff] [blame] | 7119 | if (GetQuicReloadableFlag(quic_move_amplification_limit) && |
| 7120 | QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 7121 | // CRYPTO frames are not flushed when writer is blocked. |
| 7122 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 7123 | } else { |
| 7124 | EXPECT_TRUE(connection_.HasQueuedData()); |
| 7125 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7126 | } |
| 7127 | |
| 7128 | TEST_P(QuicConnectionTest, BundleAckForSecondCHLO) { |
| 7129 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7130 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7131 | EXPECT_CALL(visitor_, OnCanWrite()) |
| 7132 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 7133 | &connection_, &TestConnection::SendCryptoStreamData))); |
| 7134 | // Process a packet from the crypto stream, which is frame1_'s default. |
| 7135 | // Receiving the CHLO as packet 2 first will cause the connection to |
| 7136 | // immediately send an ack, due to the packet gap. |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 7137 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 7138 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 7139 | } else { |
| 7140 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 7141 | } |
| 7142 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7143 | // Check that ack is sent and that delayed ack alarm is reset. |
| 7144 | if (GetParam().no_stop_waiting) { |
| 7145 | EXPECT_EQ(3u, writer_->frame_count()); |
| 7146 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 7147 | } else { |
| 7148 | EXPECT_EQ(4u, writer_->frame_count()); |
| 7149 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 7150 | } |
QUICHE team | ea74008 | 2019-03-11 17:58:43 -0700 | [diff] [blame] | 7151 | if (!QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7152 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 7153 | } else { |
| 7154 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
| 7155 | } |
| 7156 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 7157 | ASSERT_FALSE(writer_->ack_frames().empty()); |
| 7158 | EXPECT_EQ(QuicPacketNumber(2u), LargestAcked(writer_->ack_frames().front())); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7159 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7160 | } |
| 7161 | |
| 7162 | TEST_P(QuicConnectionTest, BundleAckForSecondCHLOTwoPacketReject) { |
| 7163 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7164 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7165 | |
| 7166 | // Process two packets from the crypto stream, which is frame1_'s default, |
| 7167 | // simulating a 2 packet reject. |
| 7168 | { |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 7169 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 7170 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 7171 | } else { |
| 7172 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 7173 | } |
| 7174 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7175 | // Send the new CHLO when the REJ is processed. |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 7176 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 7177 | EXPECT_CALL(visitor_, OnCryptoFrame(_)) |
| 7178 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 7179 | &connection_, &TestConnection::SendCryptoStreamData))); |
| 7180 | } else { |
| 7181 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 7182 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 7183 | &connection_, &TestConnection::SendCryptoStreamData))); |
| 7184 | } |
| 7185 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7186 | } |
| 7187 | // Check that ack is sent and that delayed ack alarm is reset. |
| 7188 | if (GetParam().no_stop_waiting) { |
| 7189 | EXPECT_EQ(3u, writer_->frame_count()); |
| 7190 | EXPECT_TRUE(writer_->stop_waiting_frames().empty()); |
| 7191 | } else { |
| 7192 | EXPECT_EQ(4u, writer_->frame_count()); |
| 7193 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 7194 | } |
QUICHE team | ea74008 | 2019-03-11 17:58:43 -0700 | [diff] [blame] | 7195 | if (!QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7196 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 7197 | } else { |
| 7198 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
| 7199 | } |
| 7200 | EXPECT_EQ(1u, writer_->padding_frames().size()); |
| 7201 | ASSERT_FALSE(writer_->ack_frames().empty()); |
| 7202 | EXPECT_EQ(QuicPacketNumber(2u), LargestAcked(writer_->ack_frames().front())); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7203 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7204 | } |
| 7205 | |
| 7206 | TEST_P(QuicConnectionTest, BundleAckWithDataOnIncomingAck) { |
| 7207 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7208 | connection_.SendStreamDataWithString( |
| 7209 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 7210 | 0, NO_FIN); |
| 7211 | connection_.SendStreamDataWithString( |
| 7212 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 7213 | 3, NO_FIN); |
| 7214 | // Ack the second packet, which will retransmit the first packet. |
| 7215 | QuicAckFrame ack = ConstructAckFrame(2, 1); |
| 7216 | LostPacketVector lost_packets; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 7217 | lost_packets.push_back( |
| 7218 | LostPacket(QuicPacketNumber(1), kMaxOutgoingPacketSize)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7219 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
wub | aa51f0e | 2020-05-12 15:08:16 -0700 | [diff] [blame] | 7220 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 7221 | Return(LossDetectionInterface::DetectionStats()))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7222 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 7223 | ProcessAckPacket(&ack); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 7224 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 7225 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7226 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 7227 | writer_->Reset(); |
| 7228 | |
| 7229 | // Now ack the retransmission, which will both raise the high water mark |
| 7230 | // and see if there is more data to send. |
| 7231 | ack = ConstructAckFrame(3, 1); |
| 7232 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 7233 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 7234 | ProcessAckPacket(&ack); |
| 7235 | |
| 7236 | // Check that no packet is sent and the ack alarm isn't set. |
| 7237 | EXPECT_EQ(0u, writer_->frame_count()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7238 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7239 | writer_->Reset(); |
| 7240 | |
| 7241 | // Send the same ack, but send both data and an ack together. |
| 7242 | ack = ConstructAckFrame(3, 1); |
| 7243 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 7244 | EXPECT_CALL(visitor_, OnCanWrite()) |
| 7245 | .WillOnce(IgnoreResult(InvokeWithoutArgs( |
| 7246 | &connection_, &TestConnection::EnsureWritableAndSendStreamData5))); |
| 7247 | ProcessAckPacket(&ack); |
| 7248 | |
| 7249 | // Check that ack is bundled with outgoing data and the delayed ack |
| 7250 | // alarm is reset. |
| 7251 | if (GetParam().no_stop_waiting) { |
fayang | 8a27b0f | 2019-11-04 11:27:40 -0800 | [diff] [blame] | 7252 | // Do not ACK acks. |
| 7253 | EXPECT_EQ(1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7254 | } else { |
| 7255 | EXPECT_EQ(3u, writer_->frame_count()); |
| 7256 | EXPECT_FALSE(writer_->stop_waiting_frames().empty()); |
| 7257 | } |
fayang | 8a27b0f | 2019-11-04 11:27:40 -0800 | [diff] [blame] | 7258 | if (GetParam().no_stop_waiting) { |
fayang | 0391669 | 2019-05-22 17:57:18 -0700 | [diff] [blame] | 7259 | EXPECT_TRUE(writer_->ack_frames().empty()); |
| 7260 | } else { |
| 7261 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 7262 | EXPECT_EQ(QuicPacketNumber(3u), |
| 7263 | LargestAcked(writer_->ack_frames().front())); |
| 7264 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7265 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7266 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7267 | } |
| 7268 | |
| 7269 | TEST_P(QuicConnectionTest, NoAckSentForClose) { |
| 7270 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7271 | ProcessPacket(1); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7272 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_PEER)) |
| 7273 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7274 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 7275 | ProcessClosePacket(2); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7276 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 7277 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 7278 | IsError(QUIC_PEER_GOING_AWAY)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7279 | } |
| 7280 | |
| 7281 | TEST_P(QuicConnectionTest, SendWhenDisconnected) { |
| 7282 | EXPECT_TRUE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7283 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 7284 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7285 | connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason", |
| 7286 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 7287 | EXPECT_FALSE(connection_.connected()); |
ianswett | fc16a2b | 2020-05-18 16:05:49 -0700 | [diff] [blame] | 7288 | EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)); |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 7289 | std::unique_ptr<QuicPacket> packet = |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 7290 | ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7291 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 7292 | .Times(0); |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 7293 | connection_.SendPacket(ENCRYPTION_INITIAL, 1, std::move(packet), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7294 | HAS_RETRANSMITTABLE_DATA, false, false); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7295 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 7296 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 7297 | IsError(QUIC_PEER_GOING_AWAY)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7298 | } |
| 7299 | |
| 7300 | TEST_P(QuicConnectionTest, SendConnectivityProbingWhenDisconnected) { |
| 7301 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 7302 | if (!IsDefaultTestConfiguration()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7303 | return; |
| 7304 | } |
| 7305 | |
| 7306 | EXPECT_TRUE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7307 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 7308 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7309 | connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason", |
| 7310 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 7311 | EXPECT_FALSE(connection_.connected()); |
ianswett | fc16a2b | 2020-05-18 16:05:49 -0700 | [diff] [blame] | 7312 | EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7313 | |
| 7314 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 7315 | .Times(0); |
| 7316 | |
| 7317 | EXPECT_QUIC_BUG(connection_.SendConnectivityProbingPacket( |
| 7318 | writer_.get(), connection_.peer_address()), |
| 7319 | "Not sending connectivity probing packet as connection is " |
| 7320 | "disconnected."); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7321 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 7322 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 7323 | IsError(QUIC_PEER_GOING_AWAY)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7324 | } |
| 7325 | |
| 7326 | TEST_P(QuicConnectionTest, WriteBlockedAfterClientSendsConnectivityProbe) { |
| 7327 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 7328 | TestPacketWriter probing_writer(version(), &clock_); |
| 7329 | // Block next write so that sending connectivity probe will encounter a |
| 7330 | // blocked write when send a connectivity probe to the peer. |
| 7331 | probing_writer.BlockOnNextWrite(); |
| 7332 | // Connection will not be marked as write blocked as connectivity probe only |
| 7333 | // affects the probing_writer which is not the default. |
| 7334 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(0); |
| 7335 | |
| 7336 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 7337 | .Times(1); |
| 7338 | connection_.SendConnectivityProbingPacket(&probing_writer, |
| 7339 | connection_.peer_address()); |
| 7340 | } |
| 7341 | |
| 7342 | TEST_P(QuicConnectionTest, WriterBlockedAfterServerSendsConnectivityProbe) { |
| 7343 | set_perspective(Perspective::IS_SERVER); |
| 7344 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 7345 | |
| 7346 | // Block next write so that sending connectivity probe will encounter a |
| 7347 | // blocked write when send a connectivity probe to the peer. |
| 7348 | writer_->BlockOnNextWrite(); |
| 7349 | // Connection will be marked as write blocked as server uses the default |
| 7350 | // writer to send connectivity probes. |
| 7351 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(1); |
| 7352 | |
| 7353 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 7354 | .Times(1); |
| 7355 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 7356 | connection_.peer_address()); |
| 7357 | } |
| 7358 | |
| 7359 | TEST_P(QuicConnectionTest, WriterErrorWhenClientSendsConnectivityProbe) { |
| 7360 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 7361 | TestPacketWriter probing_writer(version(), &clock_); |
| 7362 | probing_writer.SetShouldWriteFail(); |
| 7363 | |
| 7364 | // Connection should not be closed if a connectivity probe is failed to be |
| 7365 | // sent. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7366 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7367 | |
| 7368 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 7369 | .Times(0); |
| 7370 | connection_.SendConnectivityProbingPacket(&probing_writer, |
| 7371 | connection_.peer_address()); |
| 7372 | } |
| 7373 | |
| 7374 | TEST_P(QuicConnectionTest, WriterErrorWhenServerSendsConnectivityProbe) { |
| 7375 | set_perspective(Perspective::IS_SERVER); |
| 7376 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 7377 | |
| 7378 | writer_->SetShouldWriteFail(); |
| 7379 | // Connection should not be closed if a connectivity probe is failed to be |
| 7380 | // sent. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7381 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7382 | |
| 7383 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _)) |
| 7384 | .Times(0); |
| 7385 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 7386 | connection_.peer_address()); |
| 7387 | } |
| 7388 | |
| 7389 | TEST_P(QuicConnectionTest, PublicReset) { |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 7390 | if (VersionHasIetfInvariantHeader(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7391 | return; |
| 7392 | } |
| 7393 | QuicPublicResetPacket header; |
| 7394 | // Public reset packet in only built by server. |
| 7395 | header.connection_id = connection_id_; |
| 7396 | std::unique_ptr<QuicEncryptedPacket> packet( |
| 7397 | framer_.BuildPublicResetPacket(header)); |
| 7398 | std::unique_ptr<QuicReceivedPacket> received( |
| 7399 | ConstructReceivedPacket(*packet, QuicTime::Zero())); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7400 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_PEER)) |
| 7401 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7402 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7403 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 7404 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 7405 | IsError(QUIC_PUBLIC_RESET)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7406 | } |
| 7407 | |
| 7408 | TEST_P(QuicConnectionTest, IetfStatelessReset) { |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 7409 | if (!VersionHasIetfInvariantHeader(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7410 | return; |
| 7411 | } |
| 7412 | const QuicUint128 kTestStatelessResetToken = 1010101; |
| 7413 | QuicConfig config; |
| 7414 | QuicConfigPeer::SetReceivedStatelessResetToken(&config, |
| 7415 | kTestStatelessResetToken); |
| 7416 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 7417 | connection_.SetFromConfig(config); |
| 7418 | std::unique_ptr<QuicEncryptedPacket> packet( |
| 7419 | QuicFramer::BuildIetfStatelessResetPacket(connection_id_, |
| 7420 | kTestStatelessResetToken)); |
| 7421 | std::unique_ptr<QuicReceivedPacket> received( |
| 7422 | ConstructReceivedPacket(*packet, QuicTime::Zero())); |
renjietang | 7f483b5 | 2020-05-20 14:30:47 -0700 | [diff] [blame] | 7423 | EXPECT_CALL(visitor_, ValidateStatelessReset(_, _)).WillOnce(Return(true)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7424 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_PEER)) |
| 7425 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7426 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7427 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 7428 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 7429 | IsError(QUIC_PUBLIC_RESET)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7430 | } |
| 7431 | |
| 7432 | TEST_P(QuicConnectionTest, GoAway) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 7433 | if (VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7434 | // GoAway is not available in version 99. |
| 7435 | return; |
| 7436 | } |
| 7437 | |
| 7438 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7439 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 7440 | QuicGoAwayFrame* goaway = new QuicGoAwayFrame(); |
| 7441 | goaway->last_good_stream_id = 1; |
| 7442 | goaway->error_code = QUIC_PEER_GOING_AWAY; |
| 7443 | goaway->reason_phrase = "Going away."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7444 | EXPECT_CALL(visitor_, OnGoAway(_)); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 7445 | ProcessGoAwayPacket(goaway); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7446 | } |
| 7447 | |
| 7448 | TEST_P(QuicConnectionTest, WindowUpdate) { |
| 7449 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7450 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 7451 | QuicWindowUpdateFrame* window_update = new QuicWindowUpdateFrame(); |
| 7452 | window_update->stream_id = 3; |
| 7453 | window_update->max_data = 1234; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7454 | EXPECT_CALL(visitor_, OnWindowUpdateFrame(_)); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 7455 | ProcessFramePacket(QuicFrame(window_update)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7456 | } |
| 7457 | |
| 7458 | TEST_P(QuicConnectionTest, Blocked) { |
| 7459 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7460 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 7461 | QuicBlockedFrame* blocked = new QuicBlockedFrame(); |
| 7462 | blocked->stream_id = 3; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7463 | EXPECT_CALL(visitor_, OnBlockedFrame(_)); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 7464 | ProcessFramePacket(QuicFrame(blocked)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7465 | EXPECT_EQ(1u, connection_.GetStats().blocked_frames_received); |
| 7466 | EXPECT_EQ(0u, connection_.GetStats().blocked_frames_sent); |
| 7467 | } |
| 7468 | |
| 7469 | TEST_P(QuicConnectionTest, ZeroBytePacket) { |
| 7470 | // Don't close the connection for zero byte packets. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7471 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7472 | QuicReceivedPacket encrypted(nullptr, 0, QuicTime::Zero()); |
| 7473 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, encrypted); |
| 7474 | } |
| 7475 | |
| 7476 | TEST_P(QuicConnectionTest, MissingPacketsBeforeLeastUnacked) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 7477 | if (VersionHasIetfInvariantHeader(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7478 | return; |
| 7479 | } |
| 7480 | // Set the packet number of the ack packet to be least unacked (4). |
| 7481 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 3); |
| 7482 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7483 | ProcessStopWaitingPacket(InitStopWaitingFrame(4)); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 7484 | EXPECT_FALSE(connection_.ack_frame().packets.Empty()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7485 | } |
| 7486 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7487 | TEST_P(QuicConnectionTest, ClientHandlesVersionNegotiation) { |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 7488 | // All supported versions except the one the connection supports. |
| 7489 | ParsedQuicVersionVector versions; |
| 7490 | for (auto version : AllSupportedVersions()) { |
| 7491 | if (version != connection_.version()) { |
| 7492 | versions.push_back(version); |
| 7493 | } |
| 7494 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7495 | |
| 7496 | // Send a version negotiation packet. |
| 7497 | std::unique_ptr<QuicEncryptedPacket> encrypted( |
dschinazi | b417d60 | 2019-05-29 13:08:45 -0700 | [diff] [blame] | 7498 | QuicFramer::BuildVersionNegotiationPacket( |
| 7499 | connection_id_, EmptyQuicConnectionId(), |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 7500 | VersionHasIetfInvariantHeader(connection_.transport_version()), |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 7501 | connection_.version().HasLengthPrefixedConnectionIds(), versions)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7502 | std::unique_ptr<QuicReceivedPacket> received( |
| 7503 | ConstructReceivedPacket(*encrypted, QuicTime::Zero())); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7504 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 7505 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
fayang | 95cef07 | 2019-10-10 12:44:14 -0700 | [diff] [blame] | 7506 | // Verify no connection close packet gets sent. |
| 7507 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7508 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); |
fayang | 9ed391a | 2019-06-20 11:16:59 -0700 | [diff] [blame] | 7509 | EXPECT_FALSE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7510 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 7511 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 7512 | IsError(QUIC_INVALID_VERSION)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7513 | } |
| 7514 | |
| 7515 | TEST_P(QuicConnectionTest, BadVersionNegotiation) { |
| 7516 | // Send a version negotiation packet with the version the client started with. |
| 7517 | // It should be rejected. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7518 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 7519 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7520 | std::unique_ptr<QuicEncryptedPacket> encrypted( |
dschinazi | b417d60 | 2019-05-29 13:08:45 -0700 | [diff] [blame] | 7521 | QuicFramer::BuildVersionNegotiationPacket( |
| 7522 | connection_id_, EmptyQuicConnectionId(), |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 7523 | VersionHasIetfInvariantHeader(connection_.transport_version()), |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 7524 | connection_.version().HasLengthPrefixedConnectionIds(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7525 | AllSupportedVersions())); |
| 7526 | std::unique_ptr<QuicReceivedPacket> received( |
| 7527 | ConstructReceivedPacket(*encrypted, QuicTime::Zero())); |
| 7528 | connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7529 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 7530 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 7531 | IsError(QUIC_INVALID_VERSION_NEGOTIATION_PACKET)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7532 | } |
| 7533 | |
| 7534 | TEST_P(QuicConnectionTest, CheckSendStats) { |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 7535 | if (connection_.PtoEnabled()) { |
| 7536 | return; |
| 7537 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7538 | connection_.SetMaxTailLossProbes(0); |
| 7539 | |
| 7540 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 7541 | connection_.SendStreamDataWithString(3, "first", 0, NO_FIN); |
| 7542 | size_t first_packet_size = writer_->last_packet_size(); |
| 7543 | |
| 7544 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 7545 | connection_.SendStreamDataWithString(5, "second", 0, NO_FIN); |
| 7546 | size_t second_packet_size = writer_->last_packet_size(); |
| 7547 | |
| 7548 | // 2 retransmissions due to rto, 1 due to explicit nack. |
| 7549 | EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true)); |
| 7550 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(3); |
| 7551 | |
| 7552 | // Retransmit due to RTO. |
| 7553 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); |
| 7554 | connection_.GetRetransmissionAlarm()->Fire(); |
| 7555 | |
| 7556 | // Retransmit due to explicit nacks. |
| 7557 | QuicAckFrame nack_three = |
| 7558 | InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}, |
| 7559 | {QuicPacketNumber(4), QuicPacketNumber(5)}}); |
| 7560 | |
| 7561 | LostPacketVector lost_packets; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 7562 | lost_packets.push_back( |
| 7563 | LostPacket(QuicPacketNumber(1), kMaxOutgoingPacketSize)); |
| 7564 | lost_packets.push_back( |
| 7565 | LostPacket(QuicPacketNumber(3), kMaxOutgoingPacketSize)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7566 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)) |
wub | aa51f0e | 2020-05-12 15:08:16 -0700 | [diff] [blame] | 7567 | .WillOnce(DoAll(SetArgPointee<5>(lost_packets), |
| 7568 | Return(LossDetectionInterface::DetectionStats()))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7569 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7570 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7571 | ProcessAckPacket(&nack_three); |
| 7572 | |
| 7573 | EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) |
| 7574 | .WillOnce(Return(QuicBandwidth::Zero())); |
| 7575 | |
| 7576 | const QuicConnectionStats& stats = connection_.GetStats(); |
| 7577 | // For IETF QUIC, version is not included as the encryption level switches to |
| 7578 | // FORWARD_SECURE in SendStreamDataWithString. |
| 7579 | size_t save_on_version = |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 7580 | VersionHasIetfInvariantHeader(GetParam().version.transport_version) |
| 7581 | ? 0 |
| 7582 | : kQuicVersionSize; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7583 | EXPECT_EQ(3 * first_packet_size + 2 * second_packet_size - save_on_version, |
| 7584 | stats.bytes_sent); |
| 7585 | EXPECT_EQ(5u, stats.packets_sent); |
| 7586 | EXPECT_EQ(2 * first_packet_size + second_packet_size - save_on_version, |
| 7587 | stats.bytes_retransmitted); |
| 7588 | EXPECT_EQ(3u, stats.packets_retransmitted); |
| 7589 | EXPECT_EQ(1u, stats.rto_count); |
| 7590 | EXPECT_EQ(kDefaultMaxPacketSize, stats.max_packet_size); |
| 7591 | } |
| 7592 | |
| 7593 | TEST_P(QuicConnectionTest, ProcessFramesIfPacketClosedConnection) { |
| 7594 | // Construct a packet with stream frame and connection close frame. |
| 7595 | QuicPacketHeader header; |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 7596 | if (peer_framer_.perspective() == Perspective::IS_SERVER) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 7597 | header.source_connection_id = connection_id_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7598 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 7599 | if (!VersionHasIetfInvariantHeader(peer_framer_.transport_version())) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 7600 | header.source_connection_id_included = CONNECTION_ID_PRESENT; |
| 7601 | } |
| 7602 | } else { |
| 7603 | header.destination_connection_id = connection_id_; |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 7604 | if (VersionHasIetfInvariantHeader(peer_framer_.transport_version())) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 7605 | header.destination_connection_id_included = CONNECTION_ID_ABSENT; |
| 7606 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7607 | } |
| 7608 | header.packet_number = QuicPacketNumber(1); |
| 7609 | header.version_flag = false; |
| 7610 | |
fkastenholz | 488a462 | 2019-08-26 06:24:46 -0700 | [diff] [blame] | 7611 | QuicErrorCode kQuicErrorCode = QUIC_PEER_GOING_AWAY; |
| 7612 | // This QuicConnectionCloseFrame will default to being for a Google QUIC |
| 7613 | // close. If doing IETF QUIC then set fields appropriately for CC/T or CC/A, |
| 7614 | // depending on the mapping. |
fkastenholz | 591814c | 2019-09-06 12:11:46 -0700 | [diff] [blame] | 7615 | QuicConnectionCloseFrame qccf(peer_framer_.transport_version(), |
| 7616 | kQuicErrorCode, "", |
| 7617 | /*transport_close_frame_type=*/0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7618 | QuicFrames frames; |
| 7619 | frames.push_back(QuicFrame(frame1_)); |
| 7620 | frames.push_back(QuicFrame(&qccf)); |
| 7621 | std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); |
| 7622 | EXPECT_TRUE(nullptr != packet); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 7623 | char buffer[kMaxOutgoingPacketSize]; |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 7624 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 7625 | ENCRYPTION_FORWARD_SECURE, QuicPacketNumber(1), *packet, buffer, |
| 7626 | kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7627 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7628 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_PEER)) |
| 7629 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7630 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 7631 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7632 | |
| 7633 | connection_.ProcessUdpPacket( |
| 7634 | kSelfAddress, kPeerAddress, |
| 7635 | QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7636 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | 77e77b8 | 2020-04-05 10:36:49 -0700 | [diff] [blame] | 7637 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 7638 | IsError(QUIC_PEER_GOING_AWAY)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7639 | } |
| 7640 | |
| 7641 | TEST_P(QuicConnectionTest, SelectMutualVersion) { |
| 7642 | connection_.SetSupportedVersions(AllSupportedVersions()); |
| 7643 | // Set the connection to speak the lowest quic version. |
| 7644 | connection_.set_version(QuicVersionMin()); |
| 7645 | EXPECT_EQ(QuicVersionMin(), connection_.version()); |
| 7646 | |
| 7647 | // Pass in available versions which includes a higher mutually supported |
| 7648 | // version. The higher mutually supported version should be selected. |
| 7649 | ParsedQuicVersionVector supported_versions = AllSupportedVersions(); |
| 7650 | EXPECT_TRUE(connection_.SelectMutualVersion(supported_versions)); |
| 7651 | EXPECT_EQ(QuicVersionMax(), connection_.version()); |
| 7652 | |
| 7653 | // Expect that the lowest version is selected. |
| 7654 | // Ensure the lowest supported version is less than the max, unless they're |
| 7655 | // the same. |
| 7656 | ParsedQuicVersionVector lowest_version_vector; |
| 7657 | lowest_version_vector.push_back(QuicVersionMin()); |
| 7658 | EXPECT_TRUE(connection_.SelectMutualVersion(lowest_version_vector)); |
| 7659 | EXPECT_EQ(QuicVersionMin(), connection_.version()); |
| 7660 | |
| 7661 | // Shouldn't be able to find a mutually supported version. |
| 7662 | ParsedQuicVersionVector unsupported_version; |
| 7663 | unsupported_version.push_back(UnsupportedQuicVersion()); |
| 7664 | EXPECT_FALSE(connection_.SelectMutualVersion(unsupported_version)); |
| 7665 | } |
| 7666 | |
| 7667 | TEST_P(QuicConnectionTest, ConnectionCloseWhenWritable) { |
| 7668 | EXPECT_FALSE(writer_->IsWriteBlocked()); |
| 7669 | |
| 7670 | // Send a packet. |
| 7671 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 7672 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 7673 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 7674 | |
| 7675 | TriggerConnectionClose(); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 7676 | EXPECT_LE(2u, writer_->packets_write_attempts()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7677 | } |
| 7678 | |
| 7679 | TEST_P(QuicConnectionTest, ConnectionCloseGettingWriteBlocked) { |
| 7680 | BlockOnNextWrite(); |
| 7681 | TriggerConnectionClose(); |
| 7682 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 7683 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 7684 | } |
| 7685 | |
| 7686 | TEST_P(QuicConnectionTest, ConnectionCloseWhenWriteBlocked) { |
| 7687 | BlockOnNextWrite(); |
| 7688 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 7689 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 7690 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 7691 | EXPECT_TRUE(writer_->IsWriteBlocked()); |
| 7692 | TriggerConnectionClose(); |
| 7693 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 7694 | } |
| 7695 | |
| 7696 | TEST_P(QuicConnectionTest, OnPacketSentDebugVisitor) { |
| 7697 | MockQuicConnectionDebugVisitor debug_visitor; |
| 7698 | connection_.set_debug_visitor(&debug_visitor); |
| 7699 | |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 7700 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7701 | connection_.SendStreamDataWithString(1, "foo", 0, NO_FIN); |
| 7702 | |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 7703 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7704 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 7705 | connection_.peer_address()); |
| 7706 | } |
| 7707 | |
| 7708 | TEST_P(QuicConnectionTest, OnPacketHeaderDebugVisitor) { |
| 7709 | QuicPacketHeader header; |
| 7710 | header.packet_number = QuicPacketNumber(1); |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 7711 | if (VersionHasIetfInvariantHeader(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7712 | header.form = IETF_QUIC_LONG_HEADER_PACKET; |
| 7713 | } |
| 7714 | |
| 7715 | MockQuicConnectionDebugVisitor debug_visitor; |
| 7716 | connection_.set_debug_visitor(&debug_visitor); |
| 7717 | EXPECT_CALL(debug_visitor, OnPacketHeader(Ref(header))).Times(1); |
| 7718 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)).Times(1); |
| 7719 | EXPECT_CALL(debug_visitor, OnSuccessfulVersionNegotiation(_)).Times(1); |
| 7720 | connection_.OnPacketHeader(header); |
| 7721 | } |
| 7722 | |
| 7723 | TEST_P(QuicConnectionTest, Pacing) { |
| 7724 | TestConnection server(connection_id_, kSelfAddress, helper_.get(), |
| 7725 | alarm_factory_.get(), writer_.get(), |
| 7726 | Perspective::IS_SERVER, version()); |
| 7727 | TestConnection client(connection_id_, kPeerAddress, helper_.get(), |
| 7728 | alarm_factory_.get(), writer_.get(), |
| 7729 | Perspective::IS_CLIENT, version()); |
| 7730 | EXPECT_FALSE(QuicSentPacketManagerPeer::UsingPacing( |
| 7731 | static_cast<const QuicSentPacketManager*>( |
| 7732 | &client.sent_packet_manager()))); |
| 7733 | EXPECT_FALSE(QuicSentPacketManagerPeer::UsingPacing( |
| 7734 | static_cast<const QuicSentPacketManager*>( |
| 7735 | &server.sent_packet_manager()))); |
| 7736 | } |
| 7737 | |
| 7738 | TEST_P(QuicConnectionTest, WindowUpdateInstigateAcks) { |
| 7739 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7740 | |
| 7741 | // Send a WINDOW_UPDATE frame. |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 7742 | QuicWindowUpdateFrame* window_update = new QuicWindowUpdateFrame(); |
| 7743 | window_update->stream_id = 3; |
| 7744 | window_update->max_data = 1234; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7745 | EXPECT_CALL(visitor_, OnWindowUpdateFrame(_)); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 7746 | ProcessFramePacket(QuicFrame(window_update)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7747 | |
| 7748 | // Ensure that this has caused the ACK alarm to be set. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7749 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7750 | } |
| 7751 | |
| 7752 | TEST_P(QuicConnectionTest, BlockedFrameInstigateAcks) { |
| 7753 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7754 | |
| 7755 | // Send a BLOCKED frame. |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 7756 | QuicBlockedFrame* blocked = new QuicBlockedFrame(); |
| 7757 | blocked->stream_id = 3; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7758 | EXPECT_CALL(visitor_, OnBlockedFrame(_)); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 7759 | ProcessFramePacket(QuicFrame(blocked)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7760 | |
| 7761 | // Ensure that this has caused the ACK alarm to be set. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 7762 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7763 | } |
| 7764 | |
| 7765 | TEST_P(QuicConnectionTest, ReevaluateTimeUntilSendOnAck) { |
| 7766 | // Enable pacing. |
| 7767 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 7768 | QuicConfig config; |
| 7769 | connection_.SetFromConfig(config); |
| 7770 | |
| 7771 | // Send two packets. One packet is not sufficient because if it gets acked, |
| 7772 | // there will be no packets in flight after that and the pacer will always |
| 7773 | // allow the next packet in that situation. |
| 7774 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7775 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 7776 | connection_.SendStreamDataWithString( |
| 7777 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 7778 | 0, NO_FIN); |
| 7779 | connection_.SendStreamDataWithString( |
| 7780 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "bar", |
| 7781 | 3, NO_FIN); |
| 7782 | connection_.OnCanWrite(); |
| 7783 | |
| 7784 | // Schedule the next packet for a few milliseconds in future. |
| 7785 | QuicSentPacketManagerPeer::DisablePacerBursts(manager_); |
| 7786 | QuicTime scheduled_pacing_time = |
| 7787 | clock_.Now() + QuicTime::Delta::FromMilliseconds(5); |
| 7788 | QuicSentPacketManagerPeer::SetNextPacedPacketTime(manager_, |
| 7789 | scheduled_pacing_time); |
| 7790 | |
| 7791 | // Send a packet and have it be blocked by congestion control. |
| 7792 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(false)); |
| 7793 | connection_.SendStreamDataWithString( |
| 7794 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "baz", |
| 7795 | 6, NO_FIN); |
| 7796 | EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); |
| 7797 | |
| 7798 | // Process an ack and the send alarm will be set to the new 5ms delay. |
| 7799 | QuicAckFrame ack = InitAckFrame(1); |
| 7800 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 7801 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 7802 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillRepeatedly(Return(true)); |
| 7803 | ProcessAckPacket(&ack); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 7804 | size_t padding_frame_count = writer_->padding_frames().size(); |
| 7805 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7806 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 7807 | EXPECT_TRUE(connection_.GetSendAlarm()->IsSet()); |
| 7808 | EXPECT_EQ(scheduled_pacing_time, connection_.GetSendAlarm()->deadline()); |
| 7809 | writer_->Reset(); |
| 7810 | } |
| 7811 | |
| 7812 | TEST_P(QuicConnectionTest, SendAcksImmediately) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 7813 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7814 | return; |
| 7815 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7816 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7817 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 7818 | ProcessDataPacket(1); |
| 7819 | CongestionBlockWrites(); |
| 7820 | SendAckPacketToPeer(); |
| 7821 | } |
| 7822 | |
| 7823 | TEST_P(QuicConnectionTest, SendPingImmediately) { |
| 7824 | MockQuicConnectionDebugVisitor debug_visitor; |
| 7825 | connection_.set_debug_visitor(&debug_visitor); |
| 7826 | |
| 7827 | CongestionBlockWrites(); |
fayang | 93cc53a | 2019-08-22 12:47:30 -0700 | [diff] [blame] | 7828 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7829 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 7830 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7831 | EXPECT_CALL(debug_visitor, OnPingSent()).Times(1); |
| 7832 | connection_.SendControlFrame(QuicFrame(QuicPingFrame(1))); |
| 7833 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 7834 | } |
| 7835 | |
| 7836 | TEST_P(QuicConnectionTest, SendBlockedImmediately) { |
| 7837 | MockQuicConnectionDebugVisitor debug_visitor; |
| 7838 | connection_.set_debug_visitor(&debug_visitor); |
| 7839 | |
fayang | 93cc53a | 2019-08-22 12:47:30 -0700 | [diff] [blame] | 7840 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7841 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 7842 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7843 | EXPECT_EQ(0u, connection_.GetStats().blocked_frames_sent); |
| 7844 | connection_.SendControlFrame(QuicFrame(new QuicBlockedFrame(1, 3))); |
| 7845 | EXPECT_EQ(1u, connection_.GetStats().blocked_frames_sent); |
| 7846 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 7847 | } |
| 7848 | |
fayang | 93cc53a | 2019-08-22 12:47:30 -0700 | [diff] [blame] | 7849 | TEST_P(QuicConnectionTest, FailedToSendBlockedFrames) { |
| 7850 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 7851 | return; |
| 7852 | } |
| 7853 | MockQuicConnectionDebugVisitor debug_visitor; |
| 7854 | connection_.set_debug_visitor(&debug_visitor); |
| 7855 | QuicBlockedFrame blocked(1, 3); |
| 7856 | |
| 7857 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 7858 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _)).Times(0); |
fayang | 93cc53a | 2019-08-22 12:47:30 -0700 | [diff] [blame] | 7859 | EXPECT_EQ(0u, connection_.GetStats().blocked_frames_sent); |
| 7860 | connection_.SendControlFrame(QuicFrame(&blocked)); |
| 7861 | EXPECT_EQ(0u, connection_.GetStats().blocked_frames_sent); |
| 7862 | EXPECT_FALSE(connection_.HasQueuedData()); |
| 7863 | } |
| 7864 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7865 | TEST_P(QuicConnectionTest, SendingUnencryptedStreamDataFails) { |
| 7866 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 7867 | if (!IsDefaultTestConfiguration()) { |
| 7868 | return; |
| 7869 | } |
| 7870 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7871 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 7872 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7873 | struct iovec iov; |
| 7874 | MakeIOVector("", &iov); |
| 7875 | EXPECT_QUIC_BUG(connection_.SaveAndSendStreamData(3, &iov, 1, 0, 0, FIN), |
fayang | 4952323 | 2019-05-03 06:28:22 -0700 | [diff] [blame] | 7876 | "Cannot send stream data with level: ENCRYPTION_INITIAL"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7877 | EXPECT_FALSE(connection_.connected()); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 7878 | EXPECT_EQ(1, connection_close_frame_count_); |
bnc | f54082a | 2019-11-27 10:19:47 -0800 | [diff] [blame] | 7879 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 7880 | IsError(QUIC_ATTEMPT_TO_SEND_UNENCRYPTED_STREAM_DATA)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7881 | } |
| 7882 | |
| 7883 | TEST_P(QuicConnectionTest, SetRetransmissionAlarmForCryptoPacket) { |
| 7884 | EXPECT_TRUE(connection_.connected()); |
| 7885 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 7886 | |
| 7887 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7888 | connection_.SendCryptoStreamData(); |
| 7889 | |
| 7890 | // Verify retransmission timer is correctly set after crypto packet has been |
| 7891 | // sent. |
| 7892 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 7893 | QuicTime retransmission_time = |
| 7894 | QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7895 | ->GetRetransmissionTime(); |
| 7896 | EXPECT_NE(retransmission_time, clock_.ApproximateNow()); |
| 7897 | EXPECT_EQ(retransmission_time, |
| 7898 | connection_.GetRetransmissionAlarm()->deadline()); |
| 7899 | |
| 7900 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7901 | connection_.GetRetransmissionAlarm()->Fire(); |
| 7902 | } |
| 7903 | |
| 7904 | TEST_P(QuicConnectionTest, PathDegradingAlarmForCryptoPacket) { |
| 7905 | EXPECT_TRUE(connection_.connected()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 7906 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7907 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 7908 | |
| 7909 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 7910 | connection_.SendCryptoStreamData(); |
| 7911 | |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 7912 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7913 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 7914 | QuicTime::Delta delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7915 | ->GetPathDegradingDelay(); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 7916 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7917 | clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7918 | |
| 7919 | // Fire the path degrading alarm, path degrading signal should be sent to |
| 7920 | // the visitor. |
| 7921 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 7922 | clock_.AdvanceTime(delay); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 7923 | connection_.PathDegradingTimeout(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7924 | EXPECT_TRUE(connection_.IsPathDegrading()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 7925 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7926 | } |
| 7927 | |
vasilvv | 693d5b0 | 2019-04-09 21:58:56 -0700 | [diff] [blame] | 7928 | // Includes regression test for b/69979024. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7929 | TEST_P(QuicConnectionTest, PathDegradingAlarmForNonCryptoPackets) { |
| 7930 | EXPECT_TRUE(connection_.connected()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 7931 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7932 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 7933 | |
| 7934 | const char data[] = "data"; |
| 7935 | size_t data_size = strlen(data); |
| 7936 | QuicStreamOffset offset = 0; |
| 7937 | |
| 7938 | for (int i = 0; i < 2; ++i) { |
| 7939 | // Send a packet. Now there's a retransmittable packet on the wire, so the |
| 7940 | // path degrading alarm should be set. |
| 7941 | connection_.SendStreamDataWithString( |
| 7942 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), data, |
| 7943 | offset, NO_FIN); |
| 7944 | offset += data_size; |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 7945 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7946 | // Check the deadline of the path degrading alarm. |
| 7947 | QuicTime::Delta delay = |
| 7948 | QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7949 | ->GetPathDegradingDelay(); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 7950 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7951 | clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7952 | |
| 7953 | // Send a second packet. The path degrading alarm's deadline should remain |
| 7954 | // the same. |
vasilvv | 693d5b0 | 2019-04-09 21:58:56 -0700 | [diff] [blame] | 7955 | // Regression test for b/69979024. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7956 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 7957 | QuicTime prev_deadline = |
| 7958 | connection_.GetBlackholeDetectorAlarm()->deadline(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7959 | connection_.SendStreamDataWithString( |
| 7960 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), data, |
| 7961 | offset, NO_FIN); |
| 7962 | offset += data_size; |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 7963 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 7964 | EXPECT_EQ(prev_deadline, |
| 7965 | connection_.GetBlackholeDetectorAlarm()->deadline()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7966 | |
| 7967 | // Now receive an ACK of the first packet. This should advance the path |
| 7968 | // degrading alarm's deadline since forward progress has been made. |
| 7969 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7970 | if (i == 0) { |
| 7971 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 7972 | } |
| 7973 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 7974 | QuicAckFrame frame = InitAckFrame( |
| 7975 | {{QuicPacketNumber(1u + 2u * i), QuicPacketNumber(2u + 2u * i)}}); |
| 7976 | ProcessAckPacket(&frame); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 7977 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7978 | // Check the deadline of the path degrading alarm. |
| 7979 | delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 7980 | ->GetPathDegradingDelay(); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 7981 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 7982 | clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7983 | |
| 7984 | if (i == 0) { |
| 7985 | // Now receive an ACK of the second packet. Since there are no more |
| 7986 | // retransmittable packets on the wire, this should cancel the path |
| 7987 | // degrading alarm. |
| 7988 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 7989 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 7990 | frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 7991 | ProcessAckPacket(&frame); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 7992 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7993 | } else { |
| 7994 | // Advance time to the path degrading alarm's deadline and simulate |
| 7995 | // firing the alarm. |
| 7996 | clock_.AdvanceTime(delay); |
| 7997 | EXPECT_CALL(visitor_, OnPathDegrading()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 7998 | connection_.PathDegradingTimeout(); |
| 7999 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8000 | } |
| 8001 | } |
| 8002 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 8003 | } |
| 8004 | |
| 8005 | TEST_P(QuicConnectionTest, RetransmittableOnWireSetsPingAlarm) { |
| 8006 | const QuicTime::Delta retransmittable_on_wire_timeout = |
| 8007 | QuicTime::Delta::FromMilliseconds(50); |
zhongyi | 79ace16 | 2019-10-21 15:57:09 -0700 | [diff] [blame] | 8008 | connection_.set_initial_retransmittable_on_wire_timeout( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8009 | retransmittable_on_wire_timeout); |
| 8010 | |
| 8011 | EXPECT_TRUE(connection_.connected()); |
| 8012 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 8013 | .WillRepeatedly(Return(true)); |
| 8014 | |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8015 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8016 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 8017 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 8018 | |
| 8019 | const char data[] = "data"; |
| 8020 | size_t data_size = strlen(data); |
| 8021 | QuicStreamOffset offset = 0; |
| 8022 | |
| 8023 | // Send a packet. |
| 8024 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8025 | offset += data_size; |
| 8026 | // Now there's a retransmittable packet on the wire, so the path degrading |
| 8027 | // alarm should be set. |
| 8028 | // The retransmittable-on-wire alarm should not be set. |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8029 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8030 | QuicTime::Delta delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 8031 | ->GetPathDegradingDelay(); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 8032 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 8033 | clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8034 | ASSERT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8035 | // The ping alarm is set for the ping timeout, not the shorter |
| 8036 | // retransmittable_on_wire_timeout. |
| 8037 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8038 | QuicTime::Delta ping_delay = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 8039 | EXPECT_EQ(ping_delay, |
| 8040 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8041 | |
| 8042 | // Now receive an ACK of the packet. |
| 8043 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8044 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8045 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 8046 | QuicAckFrame frame = |
| 8047 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 8048 | ProcessAckPacket(&frame); |
| 8049 | // No more retransmittable packets on the wire, so the path degrading alarm |
| 8050 | // should be cancelled, and the ping alarm should be set to the |
| 8051 | // retransmittable_on_wire_timeout. |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8052 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8053 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 8054 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 8055 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8056 | |
| 8057 | // Simulate firing the ping alarm and sending a PING. |
| 8058 | clock_.AdvanceTime(retransmittable_on_wire_timeout); |
| 8059 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { |
| 8060 | connection_.SendControlFrame(QuicFrame(QuicPingFrame(1))); |
| 8061 | })); |
| 8062 | connection_.GetPingAlarm()->Fire(); |
| 8063 | |
| 8064 | // Now there's a retransmittable packet (PING) on the wire, so the path |
| 8065 | // degrading alarm should be set. |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8066 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8067 | delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 8068 | ->GetPathDegradingDelay(); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 8069 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 8070 | clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8071 | } |
| 8072 | |
| 8073 | // This test verifies that the connection marks path as degrading and does not |
| 8074 | // spin timer to detect path degrading when a new packet is sent on the |
| 8075 | // degraded path. |
| 8076 | TEST_P(QuicConnectionTest, NoPathDegradingAlarmIfPathIsDegrading) { |
| 8077 | EXPECT_TRUE(connection_.connected()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8078 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8079 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 8080 | |
| 8081 | const char data[] = "data"; |
| 8082 | size_t data_size = strlen(data); |
| 8083 | QuicStreamOffset offset = 0; |
| 8084 | |
| 8085 | // Send the first packet. Now there's a retransmittable packet on the wire, so |
| 8086 | // the path degrading alarm should be set. |
| 8087 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8088 | offset += data_size; |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8089 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8090 | // Check the deadline of the path degrading alarm. |
| 8091 | QuicTime::Delta delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 8092 | ->GetPathDegradingDelay(); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 8093 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 8094 | clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8095 | |
| 8096 | // Send a second packet. The path degrading alarm's deadline should remain |
| 8097 | // the same. |
| 8098 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 8099 | QuicTime prev_deadline = connection_.GetBlackholeDetectorAlarm()->deadline(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8100 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8101 | offset += data_size; |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8102 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 8103 | EXPECT_EQ(prev_deadline, connection_.GetBlackholeDetectorAlarm()->deadline()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8104 | |
| 8105 | // Now receive an ACK of the first packet. This should advance the path |
| 8106 | // degrading alarm's deadline since forward progress has been made. |
| 8107 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8108 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8109 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 8110 | QuicAckFrame frame = |
| 8111 | InitAckFrame({{QuicPacketNumber(1u), QuicPacketNumber(2u)}}); |
| 8112 | ProcessAckPacket(&frame); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8113 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8114 | // Check the deadline of the path degrading alarm. |
| 8115 | delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 8116 | ->GetPathDegradingDelay(); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 8117 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 8118 | clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8119 | |
| 8120 | // Advance time to the path degrading alarm's deadline and simulate |
| 8121 | // firing the path degrading alarm. This path will be considered as |
| 8122 | // degrading. |
| 8123 | clock_.AdvanceTime(delay); |
| 8124 | EXPECT_CALL(visitor_, OnPathDegrading()).Times(1); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8125 | connection_.PathDegradingTimeout(); |
| 8126 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8127 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 8128 | |
| 8129 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8130 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8131 | // Send a third packet. The path degrading alarm is no longer set but path |
| 8132 | // should still be marked as degrading. |
| 8133 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8134 | offset += data_size; |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8135 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8136 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 8137 | } |
| 8138 | |
| 8139 | // This test verifies that the connection unmarks path as degrarding and spins |
| 8140 | // the timer to detect future path degrading when forward progress is made |
| 8141 | // after path has been marked degrading. |
| 8142 | TEST_P(QuicConnectionTest, UnmarkPathDegradingOnForwardProgress) { |
| 8143 | EXPECT_TRUE(connection_.connected()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8144 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8145 | EXPECT_FALSE(connection_.IsPathDegrading()); |
| 8146 | |
| 8147 | const char data[] = "data"; |
| 8148 | size_t data_size = strlen(data); |
| 8149 | QuicStreamOffset offset = 0; |
| 8150 | |
| 8151 | // Send the first packet. Now there's a retransmittable packet on the wire, so |
| 8152 | // the path degrading alarm should be set. |
| 8153 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8154 | offset += data_size; |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8155 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8156 | // Check the deadline of the path degrading alarm. |
| 8157 | QuicTime::Delta delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 8158 | ->GetPathDegradingDelay(); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 8159 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 8160 | clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8161 | |
| 8162 | // Send a second packet. The path degrading alarm's deadline should remain |
| 8163 | // the same. |
| 8164 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 8165 | QuicTime prev_deadline = connection_.GetBlackholeDetectorAlarm()->deadline(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8166 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8167 | offset += data_size; |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8168 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 8169 | EXPECT_EQ(prev_deadline, connection_.GetBlackholeDetectorAlarm()->deadline()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8170 | |
| 8171 | // Now receive an ACK of the first packet. This should advance the path |
| 8172 | // degrading alarm's deadline since forward progress has been made. |
| 8173 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8174 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8175 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 8176 | QuicAckFrame frame = |
| 8177 | InitAckFrame({{QuicPacketNumber(1u), QuicPacketNumber(2u)}}); |
| 8178 | ProcessAckPacket(&frame); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8179 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8180 | // Check the deadline of the path degrading alarm. |
| 8181 | delay = QuicConnectionPeer::GetSentPacketManager(&connection_) |
| 8182 | ->GetPathDegradingDelay(); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 8183 | EXPECT_EQ(delay, connection_.GetBlackholeDetectorAlarm()->deadline() - |
| 8184 | clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8185 | |
| 8186 | // Advance time to the path degrading alarm's deadline and simulate |
| 8187 | // firing the alarm. |
| 8188 | clock_.AdvanceTime(delay); |
| 8189 | EXPECT_CALL(visitor_, OnPathDegrading()).Times(1); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8190 | connection_.PathDegradingTimeout(); |
| 8191 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8192 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 8193 | |
| 8194 | // Send a third packet. The path degrading alarm is no longer set but path |
| 8195 | // should still be marked as degrading. |
| 8196 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8197 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8198 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8199 | offset += data_size; |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8200 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8201 | EXPECT_TRUE(connection_.IsPathDegrading()); |
| 8202 | |
| 8203 | // Now receive an ACK of the second packet. This should unmark the path as |
| 8204 | // degrading. And will set a timer to detect new path degrading. |
| 8205 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8206 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
zhongyi | ef1d675 | 2020-06-11 16:19:28 -0700 | [diff] [blame^] | 8207 | EXPECT_CALL(visitor_, OnForwardProgressMadeAfterPathDegrading()).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8208 | frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 8209 | ProcessAckPacket(&frame); |
| 8210 | EXPECT_FALSE(connection_.IsPathDegrading()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8211 | EXPECT_TRUE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8212 | } |
| 8213 | |
| 8214 | TEST_P(QuicConnectionTest, NoPathDegradingOnServer) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 8215 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 8216 | return; |
| 8217 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8218 | set_perspective(Perspective::IS_SERVER); |
| 8219 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 8220 | |
| 8221 | EXPECT_FALSE(connection_.IsPathDegrading()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8222 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8223 | |
| 8224 | // Send data. |
| 8225 | const char data[] = "data"; |
| 8226 | connection_.SendStreamDataWithString(1, data, 0, NO_FIN); |
| 8227 | EXPECT_FALSE(connection_.IsPathDegrading()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8228 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8229 | |
| 8230 | // Ack data. |
| 8231 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8232 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 8233 | QuicAckFrame frame = |
| 8234 | InitAckFrame({{QuicPacketNumber(1u), QuicPacketNumber(2u)}}); |
| 8235 | ProcessAckPacket(&frame); |
| 8236 | EXPECT_FALSE(connection_.IsPathDegrading()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8237 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8238 | } |
| 8239 | |
| 8240 | TEST_P(QuicConnectionTest, NoPathDegradingAfterSendingAck) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 8241 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 8242 | return; |
| 8243 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8244 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8245 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 8246 | ProcessDataPacket(1); |
| 8247 | SendAckPacketToPeer(); |
| 8248 | EXPECT_FALSE(connection_.sent_packet_manager().unacked_packets().empty()); |
| 8249 | EXPECT_FALSE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8250 | EXPECT_FALSE(connection_.IsPathDegrading()); |
fayang | b59c6f1 | 2020-03-23 15:06:14 -0700 | [diff] [blame] | 8251 | EXPECT_FALSE(connection_.PathDegradingDetectionInProgress()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8252 | } |
| 8253 | |
| 8254 | TEST_P(QuicConnectionTest, MultipleCallsToCloseConnection) { |
| 8255 | // Verifies that multiple calls to CloseConnection do not |
| 8256 | // result in multiple attempts to close the connection - it will be marked as |
| 8257 | // disconnected after the first call. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8258 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8259 | connection_.CloseConnection(QUIC_NO_ERROR, "no reason", |
| 8260 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 8261 | connection_.CloseConnection(QUIC_NO_ERROR, "no reason", |
| 8262 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 8263 | } |
| 8264 | |
| 8265 | TEST_P(QuicConnectionTest, ServerReceivesChloOnNonCryptoStream) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8266 | set_perspective(Perspective::IS_SERVER); |
| 8267 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 8268 | |
| 8269 | CryptoHandshakeMessage message; |
| 8270 | CryptoFramer framer; |
| 8271 | message.set_tag(kCHLO); |
QUICHE team | 3fe6a8b | 2019-03-14 09:10:38 -0700 | [diff] [blame] | 8272 | std::unique_ptr<QuicData> data = framer.ConstructHandshakeMessage(message); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8273 | frame1_.stream_id = 10; |
| 8274 | frame1_.data_buffer = data->data(); |
| 8275 | frame1_.data_length = data->length(); |
| 8276 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8277 | EXPECT_CALL(visitor_, |
| 8278 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8279 | ForceProcessFramePacket(QuicFrame(frame1_)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8280 | TestConnectionCloseQuicErrorCode(QUIC_MAYBE_CORRUPTED_MEMORY); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8281 | } |
| 8282 | |
| 8283 | TEST_P(QuicConnectionTest, ClientReceivesRejOnNonCryptoStream) { |
| 8284 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8285 | |
| 8286 | CryptoHandshakeMessage message; |
| 8287 | CryptoFramer framer; |
| 8288 | message.set_tag(kREJ); |
QUICHE team | 3fe6a8b | 2019-03-14 09:10:38 -0700 | [diff] [blame] | 8289 | std::unique_ptr<QuicData> data = framer.ConstructHandshakeMessage(message); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8290 | frame1_.stream_id = 10; |
| 8291 | frame1_.data_buffer = data->data(); |
| 8292 | frame1_.data_length = data->length(); |
| 8293 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8294 | EXPECT_CALL(visitor_, |
| 8295 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8296 | ForceProcessFramePacket(QuicFrame(frame1_)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8297 | TestConnectionCloseQuicErrorCode(QUIC_MAYBE_CORRUPTED_MEMORY); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8298 | } |
| 8299 | |
| 8300 | TEST_P(QuicConnectionTest, CloseConnectionOnPacketTooLarge) { |
| 8301 | SimulateNextPacketTooLarge(); |
| 8302 | // A connection close packet is sent |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8303 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8304 | .Times(1); |
| 8305 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8306 | TestConnectionCloseQuicErrorCode(QUIC_PACKET_WRITE_ERROR); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8307 | } |
| 8308 | |
| 8309 | TEST_P(QuicConnectionTest, AlwaysGetPacketTooLarge) { |
| 8310 | // Test even we always get packet too large, we do not infinitely try to send |
| 8311 | // close packet. |
| 8312 | AlwaysGetPacketTooLarge(); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8313 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8314 | .Times(1); |
| 8315 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8316 | TestConnectionCloseQuicErrorCode(QUIC_PACKET_WRITE_ERROR); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8317 | } |
| 8318 | |
nharper | ef46896 | 2019-07-02 14:15:38 -0700 | [diff] [blame] | 8319 | TEST_P(QuicConnectionTest, CloseConnectionOnQueuedWriteError) { |
nharper | ef46896 | 2019-07-02 14:15:38 -0700 | [diff] [blame] | 8320 | // Regression test for crbug.com/979507. |
| 8321 | // |
| 8322 | // If we get a write error when writing queued packets, we should attempt to |
| 8323 | // send a connection close packet, but if sending that fails, it shouldn't get |
| 8324 | // queued. |
| 8325 | |
| 8326 | // Queue a packet to write. |
| 8327 | BlockOnNextWrite(); |
| 8328 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 8329 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 8330 | |
| 8331 | // Configure writer to always fail. |
| 8332 | AlwaysGetPacketTooLarge(); |
| 8333 | |
| 8334 | // Expect that we attempt to close the connection exactly once. |
| 8335 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 8336 | .Times(1); |
| 8337 | |
| 8338 | // Unblock the writes and actually send. |
| 8339 | writer_->SetWritable(); |
| 8340 | connection_.OnCanWrite(); |
| 8341 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 8342 | |
| 8343 | TestConnectionCloseQuicErrorCode(QUIC_PACKET_WRITE_ERROR); |
| 8344 | } |
| 8345 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8346 | // Verify that if connection has no outstanding data, it notifies the send |
| 8347 | // algorithm after the write. |
| 8348 | TEST_P(QuicConnectionTest, SendDataAndBecomeApplicationLimited) { |
| 8349 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(1); |
| 8350 | { |
| 8351 | InSequence seq; |
| 8352 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillRepeatedly(Return(true)); |
| 8353 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 8354 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()) |
| 8355 | .WillRepeatedly(Return(false)); |
| 8356 | } |
| 8357 | |
| 8358 | connection_.SendStreamData3(); |
| 8359 | } |
| 8360 | |
| 8361 | // Verify that the connection does not become app-limited if there is |
| 8362 | // outstanding data to send after the write. |
| 8363 | TEST_P(QuicConnectionTest, NotBecomeApplicationLimitedIfMoreDataAvailable) { |
| 8364 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(0); |
| 8365 | { |
| 8366 | InSequence seq; |
| 8367 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 8368 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillRepeatedly(Return(true)); |
| 8369 | } |
| 8370 | |
| 8371 | connection_.SendStreamData3(); |
| 8372 | } |
| 8373 | |
| 8374 | // Verify that the connection does not become app-limited after blocked write |
| 8375 | // even if there is outstanding data to send after the write. |
| 8376 | TEST_P(QuicConnectionTest, NotBecomeApplicationLimitedDueToWriteBlock) { |
| 8377 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(0); |
| 8378 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillRepeatedly(Return(true)); |
| 8379 | BlockOnNextWrite(); |
| 8380 | |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 8381 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8382 | connection_.SendStreamData3(); |
| 8383 | |
| 8384 | // Now unblock the writer, become congestion control blocked, |
| 8385 | // and ensure we become app-limited after writing. |
| 8386 | writer_->SetWritable(); |
| 8387 | CongestionBlockWrites(); |
| 8388 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillRepeatedly(Return(false)); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 8389 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
fayang | 2ce6608 | 2019-10-02 06:29:04 -0700 | [diff] [blame] | 8390 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8391 | connection_.OnCanWrite(); |
| 8392 | } |
| 8393 | |
| 8394 | // Test the mode in which the link is filled up with probing retransmissions if |
| 8395 | // the connection becomes application-limited. |
| 8396 | TEST_P(QuicConnectionTest, SendDataWhenApplicationLimited) { |
| 8397 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8398 | EXPECT_CALL(*send_algorithm_, ShouldSendProbingPacket()) |
| 8399 | .WillRepeatedly(Return(true)); |
| 8400 | { |
| 8401 | InSequence seq; |
| 8402 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillRepeatedly(Return(true)); |
| 8403 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 8404 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()) |
| 8405 | .WillRepeatedly(Return(false)); |
| 8406 | } |
QUICHE team | b834325 | 2019-04-29 13:58:01 -0700 | [diff] [blame] | 8407 | EXPECT_CALL(visitor_, SendProbingData()).WillRepeatedly([this] { |
| 8408 | return connection_.sent_packet_manager().MaybeRetransmitOldestPacket( |
| 8409 | PROBING_RETRANSMISSION); |
| 8410 | }); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8411 | // Fix congestion window to be 20,000 bytes. |
| 8412 | EXPECT_CALL(*send_algorithm_, CanSend(Ge(20000u))) |
| 8413 | .WillRepeatedly(Return(false)); |
| 8414 | EXPECT_CALL(*send_algorithm_, CanSend(Lt(20000u))) |
| 8415 | .WillRepeatedly(Return(true)); |
| 8416 | |
| 8417 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(0); |
| 8418 | ASSERT_EQ(0u, connection_.GetStats().packets_sent); |
| 8419 | connection_.set_fill_up_link_during_probing(true); |
fayang | edf9ad5 | 2020-03-05 13:49:18 -0800 | [diff] [blame] | 8420 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 8421 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8422 | connection_.OnHandshakeComplete(); |
| 8423 | connection_.SendStreamData3(); |
| 8424 | |
| 8425 | // We expect a lot of packets from a 20 kbyte window. |
| 8426 | EXPECT_GT(connection_.GetStats().packets_sent, 10u); |
| 8427 | // Ensure that the packets are padded. |
| 8428 | QuicByteCount average_packet_size = |
| 8429 | connection_.GetStats().bytes_sent / connection_.GetStats().packets_sent; |
| 8430 | EXPECT_GT(average_packet_size, 1000u); |
| 8431 | |
| 8432 | // Acknowledge all packets sent, except for the last one. |
| 8433 | QuicAckFrame ack = InitAckFrame( |
| 8434 | connection_.sent_packet_manager().GetLargestSentPacket() - 1); |
| 8435 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 8436 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 8437 | |
| 8438 | // Ensure that since we no longer have retransmittable bytes in flight, this |
| 8439 | // will not cause any responses to be sent. |
| 8440 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 8441 | EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(1); |
| 8442 | ProcessAckPacket(&ack); |
| 8443 | } |
| 8444 | |
rch | d672c6d | 2019-11-27 15:30:54 -0800 | [diff] [blame] | 8445 | TEST_P(QuicConnectionTest, DoNotForceSendingAckOnPacketTooLarge) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8446 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8447 | // Send an ack by simulating delayed ack alarm firing. |
| 8448 | ProcessPacket(1); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 8449 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8450 | connection_.GetAckAlarm()->Fire(); |
| 8451 | // Simulate data packet causes write error. |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8452 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8453 | SimulateNextPacketTooLarge(); |
| 8454 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
rch | d672c6d | 2019-11-27 15:30:54 -0800 | [diff] [blame] | 8455 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8456 | // Ack frame is not bundled in connection close packet. |
| 8457 | EXPECT_TRUE(writer_->ack_frames().empty()); |
rch | d672c6d | 2019-11-27 15:30:54 -0800 | [diff] [blame] | 8458 | if (writer_->padding_frames().empty()) { |
| 8459 | EXPECT_EQ(1u, writer_->frame_count()); |
| 8460 | } else { |
| 8461 | EXPECT_EQ(2u, writer_->frame_count()); |
| 8462 | } |
| 8463 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8464 | TestConnectionCloseQuicErrorCode(QUIC_PACKET_WRITE_ERROR); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8465 | } |
| 8466 | |
rch | d672c6d | 2019-11-27 15:30:54 -0800 | [diff] [blame] | 8467 | TEST_P(QuicConnectionTest, CloseConnectionAllLevels) { |
fayang | c303bfd | 2020-02-11 09:19:54 -0800 | [diff] [blame] | 8468 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 8469 | return; |
| 8470 | } |
rch | d672c6d | 2019-11-27 15:30:54 -0800 | [diff] [blame] | 8471 | |
| 8472 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 8473 | const QuicErrorCode kQuicErrorCode = QUIC_INTERNAL_ERROR; |
| 8474 | connection_.CloseConnection( |
| 8475 | kQuicErrorCode, "Some random error message", |
| 8476 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 8477 | |
| 8478 | EXPECT_EQ(2u, QuicConnectionPeer::GetNumEncryptionLevels(&connection_)); |
| 8479 | |
| 8480 | TestConnectionCloseQuicErrorCode(kQuicErrorCode); |
| 8481 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 8482 | |
| 8483 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 8484 | // Each connection close packet should be sent in distinct UDP packets. |
| 8485 | EXPECT_EQ(QuicConnectionPeer::GetNumEncryptionLevels(&connection_), |
| 8486 | writer_->connection_close_packets()); |
| 8487 | EXPECT_EQ(QuicConnectionPeer::GetNumEncryptionLevels(&connection_), |
| 8488 | writer_->packets_write_attempts()); |
| 8489 | return; |
| 8490 | } |
| 8491 | |
| 8492 | // A single UDP packet should be sent with multiple connection close packets |
| 8493 | // coalesced together. |
| 8494 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 8495 | |
| 8496 | // Only the first packet has been processed yet. |
| 8497 | EXPECT_EQ(1u, writer_->connection_close_packets()); |
| 8498 | |
| 8499 | // ProcessPacket resets the visitor and frees the coalesced packet. |
| 8500 | ASSERT_TRUE(writer_->coalesced_packet() != nullptr); |
| 8501 | auto packet = writer_->coalesced_packet()->Clone(); |
| 8502 | writer_->framer()->ProcessPacket(*packet); |
| 8503 | EXPECT_EQ(1u, writer_->connection_close_packets()); |
| 8504 | ASSERT_TRUE(writer_->coalesced_packet() == nullptr); |
| 8505 | } |
| 8506 | |
| 8507 | TEST_P(QuicConnectionTest, CloseConnectionOneLevel) { |
fayang | c303bfd | 2020-02-11 09:19:54 -0800 | [diff] [blame] | 8508 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 8509 | return; |
| 8510 | } |
rch | d672c6d | 2019-11-27 15:30:54 -0800 | [diff] [blame] | 8511 | |
| 8512 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 8513 | const QuicErrorCode kQuicErrorCode = QUIC_INTERNAL_ERROR; |
| 8514 | connection_.CloseConnection( |
| 8515 | kQuicErrorCode, "Some random error message", |
| 8516 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 8517 | |
| 8518 | EXPECT_EQ(2u, QuicConnectionPeer::GetNumEncryptionLevels(&connection_)); |
| 8519 | |
| 8520 | TestConnectionCloseQuicErrorCode(kQuicErrorCode); |
| 8521 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 8522 | EXPECT_EQ(1u, writer_->connection_close_packets()); |
| 8523 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 8524 | ASSERT_TRUE(writer_->coalesced_packet() == nullptr); |
| 8525 | } |
| 8526 | |
fayang | 9abdfec | 2020-02-13 12:34:58 -0800 | [diff] [blame] | 8527 | TEST_P(QuicConnectionTest, DoNotPadServerInitialConnectionClose) { |
| 8528 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 8529 | return; |
| 8530 | } |
| 8531 | set_perspective(Perspective::IS_SERVER); |
| 8532 | |
| 8533 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 8534 | const QuicErrorCode kQuicErrorCode = QUIC_INTERNAL_ERROR; |
| 8535 | connection_.CloseConnection( |
| 8536 | kQuicErrorCode, "Some random error message", |
| 8537 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 8538 | |
| 8539 | EXPECT_EQ(2u, QuicConnectionPeer::GetNumEncryptionLevels(&connection_)); |
| 8540 | |
| 8541 | TestConnectionCloseQuicErrorCode(kQuicErrorCode); |
| 8542 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 8543 | EXPECT_TRUE(writer_->padding_frames().empty()); |
| 8544 | EXPECT_EQ(ENCRYPTION_INITIAL, writer_->framer()->last_decrypted_level()); |
| 8545 | } |
| 8546 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8547 | // Regression test for b/63620844. |
| 8548 | TEST_P(QuicConnectionTest, FailedToWriteHandshakePacket) { |
| 8549 | SimulateNextPacketTooLarge(); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8550 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8551 | .Times(1); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8552 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8553 | connection_.SendCryptoStreamData(); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 8554 | TestConnectionCloseQuicErrorCode(QUIC_PACKET_WRITE_ERROR); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8555 | } |
| 8556 | |
| 8557 | TEST_P(QuicConnectionTest, MaxPacingRate) { |
| 8558 | EXPECT_EQ(0, connection_.MaxPacingRate().ToBytesPerSecond()); |
| 8559 | connection_.SetMaxPacingRate(QuicBandwidth::FromBytesPerSecond(100)); |
| 8560 | EXPECT_EQ(100, connection_.MaxPacingRate().ToBytesPerSecond()); |
| 8561 | } |
| 8562 | |
| 8563 | TEST_P(QuicConnectionTest, ClientAlwaysSendConnectionId) { |
| 8564 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
| 8565 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 8566 | connection_.SendStreamDataWithString(3, "foo", 0, NO_FIN); |
| 8567 | EXPECT_EQ(CONNECTION_ID_PRESENT, |
| 8568 | writer_->last_packet_header().destination_connection_id_included); |
| 8569 | |
| 8570 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 8571 | QuicConfig config; |
| 8572 | QuicConfigPeer::SetReceivedBytesForConnectionId(&config, 0); |
| 8573 | connection_.SetFromConfig(config); |
| 8574 | |
| 8575 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 8576 | connection_.SendStreamDataWithString(3, "bar", 3, NO_FIN); |
| 8577 | // Verify connection id is still sent in the packet. |
| 8578 | EXPECT_EQ(CONNECTION_ID_PRESENT, |
| 8579 | writer_->last_packet_header().destination_connection_id_included); |
| 8580 | } |
| 8581 | |
| 8582 | TEST_P(QuicConnectionTest, SendProbingRetransmissions) { |
| 8583 | MockQuicConnectionDebugVisitor debug_visitor; |
| 8584 | connection_.set_debug_visitor(&debug_visitor); |
| 8585 | |
| 8586 | const QuicStreamId stream_id = 2; |
| 8587 | QuicPacketNumber last_packet; |
| 8588 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_packet); |
| 8589 | SendStreamDataToPeer(stream_id, "bar", 3, NO_FIN, &last_packet); |
| 8590 | SendStreamDataToPeer(stream_id, "test", 6, NO_FIN, &last_packet); |
| 8591 | |
| 8592 | const QuicByteCount old_bytes_in_flight = |
| 8593 | connection_.sent_packet_manager().GetBytesInFlight(); |
| 8594 | |
| 8595 | // Allow 9 probing retransmissions to be sent. |
| 8596 | { |
| 8597 | InSequence seq; |
| 8598 | EXPECT_CALL(*send_algorithm_, CanSend(_)) |
| 8599 | .Times(9 * 2) |
| 8600 | .WillRepeatedly(Return(true)); |
| 8601 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(Return(false)); |
| 8602 | } |
| 8603 | // Expect them retransmitted in cyclic order (foo, bar, test, foo, bar...). |
| 8604 | QuicPacketCount sent_count = 0; |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 8605 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8606 | .WillRepeatedly(Invoke([this, &sent_count](const SerializedPacket&, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8607 | TransmissionType, QuicTime) { |
| 8608 | ASSERT_EQ(1u, writer_->stream_frames().size()); |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 8609 | if (connection_.version().CanSendCoalescedPackets()) { |
| 8610 | // There is a delay of sending coalesced packet, so (6, 0, 3, 6, |
| 8611 | // 0...). |
| 8612 | EXPECT_EQ(3 * ((sent_count + 2) % 3), |
| 8613 | writer_->stream_frames()[0]->offset); |
| 8614 | } else { |
| 8615 | // Identify the frames by stream offset (0, 3, 6, 0, 3...). |
| 8616 | EXPECT_EQ(3 * (sent_count % 3), writer_->stream_frames()[0]->offset); |
| 8617 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8618 | sent_count++; |
| 8619 | })); |
| 8620 | EXPECT_CALL(*send_algorithm_, ShouldSendProbingPacket()) |
| 8621 | .WillRepeatedly(Return(true)); |
QUICHE team | b834325 | 2019-04-29 13:58:01 -0700 | [diff] [blame] | 8622 | EXPECT_CALL(visitor_, SendProbingData()).WillRepeatedly([this] { |
| 8623 | return connection_.sent_packet_manager().MaybeRetransmitOldestPacket( |
| 8624 | PROBING_RETRANSMISSION); |
| 8625 | }); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8626 | |
| 8627 | connection_.SendProbingRetransmissions(); |
| 8628 | |
| 8629 | // Ensure that the in-flight has increased. |
| 8630 | const QuicByteCount new_bytes_in_flight = |
| 8631 | connection_.sent_packet_manager().GetBytesInFlight(); |
| 8632 | EXPECT_GT(new_bytes_in_flight, old_bytes_in_flight); |
| 8633 | } |
| 8634 | |
| 8635 | // Ensure that SendProbingRetransmissions() does not retransmit anything when |
| 8636 | // there are no outstanding packets. |
| 8637 | TEST_P(QuicConnectionTest, |
| 8638 | SendProbingRetransmissionsFailsWhenNothingToRetransmit) { |
| 8639 | ASSERT_TRUE(connection_.sent_packet_manager().unacked_packets().empty()); |
| 8640 | |
| 8641 | MockQuicConnectionDebugVisitor debug_visitor; |
| 8642 | connection_.set_debug_visitor(&debug_visitor); |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 8643 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8644 | EXPECT_CALL(*send_algorithm_, ShouldSendProbingPacket()) |
| 8645 | .WillRepeatedly(Return(true)); |
QUICHE team | b834325 | 2019-04-29 13:58:01 -0700 | [diff] [blame] | 8646 | EXPECT_CALL(visitor_, SendProbingData()).WillRepeatedly([this] { |
| 8647 | return connection_.sent_packet_manager().MaybeRetransmitOldestPacket( |
| 8648 | PROBING_RETRANSMISSION); |
| 8649 | }); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8650 | |
| 8651 | connection_.SendProbingRetransmissions(); |
| 8652 | } |
| 8653 | |
| 8654 | TEST_P(QuicConnectionTest, PingAfterLastRetransmittablePacketAcked) { |
| 8655 | const QuicTime::Delta retransmittable_on_wire_timeout = |
| 8656 | QuicTime::Delta::FromMilliseconds(50); |
zhongyi | 79ace16 | 2019-10-21 15:57:09 -0700 | [diff] [blame] | 8657 | connection_.set_initial_retransmittable_on_wire_timeout( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8658 | retransmittable_on_wire_timeout); |
| 8659 | |
| 8660 | EXPECT_TRUE(connection_.connected()); |
| 8661 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 8662 | .WillRepeatedly(Return(true)); |
| 8663 | |
| 8664 | const char data[] = "data"; |
| 8665 | size_t data_size = strlen(data); |
| 8666 | QuicStreamOffset offset = 0; |
| 8667 | |
| 8668 | // Advance 5ms, send a retransmittable packet to the peer. |
| 8669 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8670 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 8671 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8672 | offset += data_size; |
| 8673 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8674 | // The ping alarm is set for the ping timeout, not the shorter |
| 8675 | // retransmittable_on_wire_timeout. |
| 8676 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8677 | QuicTime::Delta ping_delay = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 8678 | EXPECT_EQ(ping_delay, |
| 8679 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8680 | |
| 8681 | // Advance 5ms, send a second retransmittable packet to the peer. |
| 8682 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8683 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8684 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8685 | offset += data_size; |
| 8686 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8687 | |
| 8688 | // Now receive an ACK of the first packet. This should not set the |
| 8689 | // retransmittable-on-wire alarm since packet 2 is still on the wire. |
| 8690 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8691 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8692 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 8693 | QuicAckFrame frame = |
| 8694 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 8695 | ProcessAckPacket(&frame); |
| 8696 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8697 | // The ping alarm is set for the ping timeout, not the shorter |
| 8698 | // retransmittable_on_wire_timeout. |
| 8699 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8700 | // The ping alarm has a 1 second granularity, and the clock has been advanced |
| 8701 | // 10ms since it was originally set. |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 8702 | EXPECT_EQ(ping_delay - QuicTime::Delta::FromMilliseconds(10), |
| 8703 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8704 | |
| 8705 | // Now receive an ACK of the second packet. This should set the |
| 8706 | // retransmittable-on-wire alarm now that no retransmittable packets are on |
| 8707 | // the wire. |
| 8708 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8709 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 8710 | frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 8711 | ProcessAckPacket(&frame); |
| 8712 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 8713 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 8714 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8715 | |
| 8716 | // Now receive a duplicate ACK of the second packet. This should not update |
| 8717 | // the ping alarm. |
| 8718 | QuicTime prev_deadline = connection_.GetPingAlarm()->deadline(); |
| 8719 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8720 | frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 8721 | ProcessAckPacket(&frame); |
| 8722 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8723 | EXPECT_EQ(prev_deadline, connection_.GetPingAlarm()->deadline()); |
| 8724 | |
| 8725 | // Now receive a non-ACK packet. This should not update the ping alarm. |
| 8726 | prev_deadline = connection_.GetPingAlarm()->deadline(); |
| 8727 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8728 | ProcessPacket(4); |
| 8729 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8730 | EXPECT_EQ(prev_deadline, connection_.GetPingAlarm()->deadline()); |
| 8731 | |
| 8732 | // Simulate the alarm firing and check that a PING is sent. |
| 8733 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { |
| 8734 | connection_.SendControlFrame(QuicFrame(QuicPingFrame(1))); |
| 8735 | })); |
| 8736 | connection_.GetPingAlarm()->Fire(); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 8737 | size_t padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8738 | if (GetParam().no_stop_waiting) { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 8739 | EXPECT_EQ(padding_frame_count + 2u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8740 | } else { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 8741 | EXPECT_EQ(padding_frame_count + 3u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8742 | } |
| 8743 | ASSERT_EQ(1u, writer_->ping_frames().size()); |
| 8744 | } |
| 8745 | |
| 8746 | TEST_P(QuicConnectionTest, NoPingIfRetransmittablePacketSent) { |
| 8747 | const QuicTime::Delta retransmittable_on_wire_timeout = |
| 8748 | QuicTime::Delta::FromMilliseconds(50); |
zhongyi | 79ace16 | 2019-10-21 15:57:09 -0700 | [diff] [blame] | 8749 | connection_.set_initial_retransmittable_on_wire_timeout( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8750 | retransmittable_on_wire_timeout); |
| 8751 | |
| 8752 | EXPECT_TRUE(connection_.connected()); |
| 8753 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 8754 | .WillRepeatedly(Return(true)); |
| 8755 | |
| 8756 | const char data[] = "data"; |
| 8757 | size_t data_size = strlen(data); |
| 8758 | QuicStreamOffset offset = 0; |
| 8759 | |
| 8760 | // Advance 5ms, send a retransmittable packet to the peer. |
| 8761 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8762 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 8763 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8764 | offset += data_size; |
| 8765 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8766 | // The ping alarm is set for the ping timeout, not the shorter |
| 8767 | // retransmittable_on_wire_timeout. |
| 8768 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8769 | QuicTime::Delta ping_delay = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 8770 | EXPECT_EQ(ping_delay, |
| 8771 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8772 | |
| 8773 | // Now receive an ACK of the first packet. This should set the |
| 8774 | // retransmittable-on-wire alarm now that no retransmittable packets are on |
| 8775 | // the wire. |
| 8776 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8777 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 8778 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 8779 | QuicAckFrame frame = |
| 8780 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 8781 | ProcessAckPacket(&frame); |
| 8782 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 8783 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 8784 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8785 | |
| 8786 | // Before the alarm fires, send another retransmittable packet. This should |
| 8787 | // cancel the retransmittable-on-wire alarm since now there's a |
| 8788 | // retransmittable packet on the wire. |
| 8789 | connection_.SendStreamDataWithString(1, data, offset, NO_FIN); |
| 8790 | offset += data_size; |
| 8791 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8792 | |
| 8793 | // Now receive an ACK of the second packet. This should set the |
| 8794 | // retransmittable-on-wire alarm now that no retransmittable packets are on |
| 8795 | // the wire. |
| 8796 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8797 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 8798 | frame = InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}}); |
| 8799 | ProcessAckPacket(&frame); |
| 8800 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
zhongyi | eef848f | 2019-10-18 07:09:37 -0700 | [diff] [blame] | 8801 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 8802 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8803 | |
| 8804 | // Simulate the alarm firing and check that a PING is sent. |
| 8805 | writer_->Reset(); |
| 8806 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { |
| 8807 | connection_.SendControlFrame(QuicFrame(QuicPingFrame(1))); |
| 8808 | })); |
| 8809 | connection_.GetPingAlarm()->Fire(); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 8810 | size_t padding_frame_count = writer_->padding_frames().size(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8811 | if (GetParam().no_stop_waiting) { |
fayang | 8a27b0f | 2019-11-04 11:27:40 -0800 | [diff] [blame] | 8812 | // Do not ACK acks. |
| 8813 | EXPECT_EQ(padding_frame_count + 1u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8814 | } else { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 8815 | EXPECT_EQ(padding_frame_count + 3u, writer_->frame_count()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8816 | } |
| 8817 | ASSERT_EQ(1u, writer_->ping_frames().size()); |
| 8818 | } |
| 8819 | |
zhongyi | 79ace16 | 2019-10-21 15:57:09 -0700 | [diff] [blame] | 8820 | // When there is no stream data received but are open streams, send the |
| 8821 | // first few consecutive pings with aggressive retransmittable-on-wire |
| 8822 | // timeout. Exponentially back off the retransmittable-on-wire ping timeout |
| 8823 | // afterwards until it exceeds the default ping timeout. |
| 8824 | TEST_P(QuicConnectionTest, BackOffRetransmittableOnWireTimeout) { |
| 8825 | int max_aggressive_retransmittable_on_wire_ping_count = 5; |
| 8826 | SetQuicFlag(FLAGS_quic_max_aggressive_retransmittable_on_wire_ping_count, |
| 8827 | max_aggressive_retransmittable_on_wire_ping_count); |
| 8828 | const QuicTime::Delta initial_retransmittable_on_wire_timeout = |
| 8829 | QuicTime::Delta::FromMilliseconds(200); |
| 8830 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 8831 | initial_retransmittable_on_wire_timeout); |
| 8832 | |
| 8833 | EXPECT_TRUE(connection_.connected()); |
| 8834 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 8835 | .WillRepeatedly(Return(true)); |
| 8836 | |
| 8837 | const char data[] = "data"; |
| 8838 | // Advance 5ms, send a retransmittable data packet to the peer. |
| 8839 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8840 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 8841 | connection_.SendStreamDataWithString(1, data, 0, NO_FIN); |
| 8842 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8843 | // The ping alarm is set for the ping timeout, not the shorter |
| 8844 | // retransmittable_on_wire_timeout. |
| 8845 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8846 | EXPECT_EQ(connection_.ping_timeout(), |
| 8847 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8848 | |
| 8849 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)).Times(AnyNumber()); |
| 8850 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)) |
| 8851 | .Times(AnyNumber()); |
| 8852 | |
| 8853 | // Verify that the first few consecutive retransmittable on wire pings are |
| 8854 | // sent with aggressive timeout. |
| 8855 | for (int i = 0; i <= max_aggressive_retransmittable_on_wire_ping_count; i++) { |
| 8856 | // Receive an ACK of the previous packet. This should set the ping alarm |
| 8857 | // with the initial retransmittable-on-wire timeout. |
| 8858 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8859 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8860 | QuicAckFrame frame = InitAckFrame( |
| 8861 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8862 | ProcessAckPacket(&frame); |
| 8863 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8864 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8865 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8866 | // Simulate the alarm firing and check that a PING is sent. |
| 8867 | writer_->Reset(); |
| 8868 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { |
| 8869 | SendPing(); |
| 8870 | })); |
| 8871 | clock_.AdvanceTime(initial_retransmittable_on_wire_timeout); |
| 8872 | connection_.GetPingAlarm()->Fire(); |
| 8873 | } |
| 8874 | |
| 8875 | QuicTime::Delta retransmittable_on_wire_timeout = |
| 8876 | initial_retransmittable_on_wire_timeout; |
| 8877 | |
| 8878 | // Verify subsequent pings are sent with timeout that is exponentially backed |
| 8879 | // off. |
| 8880 | while (retransmittable_on_wire_timeout * 2 < connection_.ping_timeout()) { |
| 8881 | // Receive an ACK for the previous PING. This should set the |
| 8882 | // ping alarm with backed off retransmittable-on-wire timeout. |
| 8883 | retransmittable_on_wire_timeout = retransmittable_on_wire_timeout * 2; |
| 8884 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8885 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8886 | QuicAckFrame frame = InitAckFrame( |
| 8887 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8888 | ProcessAckPacket(&frame); |
| 8889 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8890 | EXPECT_EQ(retransmittable_on_wire_timeout, |
| 8891 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8892 | |
| 8893 | // Simulate the alarm firing and check that a PING is sent. |
| 8894 | writer_->Reset(); |
| 8895 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { |
| 8896 | SendPing(); |
| 8897 | })); |
| 8898 | clock_.AdvanceTime(retransmittable_on_wire_timeout); |
| 8899 | connection_.GetPingAlarm()->Fire(); |
| 8900 | } |
| 8901 | |
| 8902 | // The ping alarm is set with default ping timeout. |
| 8903 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8904 | EXPECT_EQ(connection_.ping_timeout(), |
| 8905 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8906 | |
| 8907 | // Receive an ACK for the previous PING. The ping alarm is set with an |
| 8908 | // earlier deadline. |
| 8909 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8910 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8911 | QuicAckFrame frame = InitAckFrame( |
| 8912 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8913 | ProcessAckPacket(&frame); |
| 8914 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8915 | EXPECT_EQ(connection_.ping_timeout() - QuicTime::Delta::FromMilliseconds(5), |
| 8916 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8917 | } |
| 8918 | |
| 8919 | // This test verify that the count of consecutive aggressive pings is reset |
| 8920 | // when new data is received. And it also verifies the connection resets |
| 8921 | // the exponential back-off of the retransmittable-on-wire ping timeout |
| 8922 | // after receiving new stream data. |
| 8923 | TEST_P(QuicConnectionTest, ResetBackOffRetransmitableOnWireTimeout) { |
| 8924 | int max_aggressive_retransmittable_on_wire_ping_count = 3; |
| 8925 | SetQuicFlag(FLAGS_quic_max_aggressive_retransmittable_on_wire_ping_count, 3); |
| 8926 | const QuicTime::Delta initial_retransmittable_on_wire_timeout = |
| 8927 | QuicTime::Delta::FromMilliseconds(200); |
| 8928 | connection_.set_initial_retransmittable_on_wire_timeout( |
| 8929 | initial_retransmittable_on_wire_timeout); |
| 8930 | |
| 8931 | EXPECT_TRUE(connection_.connected()); |
| 8932 | EXPECT_CALL(visitor_, ShouldKeepConnectionAlive()) |
| 8933 | .WillRepeatedly(Return(true)); |
| 8934 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)).Times(AnyNumber()); |
| 8935 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)) |
| 8936 | .Times(AnyNumber()); |
| 8937 | |
| 8938 | const char data[] = "data"; |
| 8939 | // Advance 5ms, send a retransmittable data packet to the peer. |
| 8940 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8941 | EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); |
| 8942 | connection_.SendStreamDataWithString(1, data, 0, NO_FIN); |
| 8943 | EXPECT_TRUE(connection_.sent_packet_manager().HasInFlightPackets()); |
| 8944 | // The ping alarm is set for the ping timeout, not the shorter |
| 8945 | // retransmittable_on_wire_timeout. |
| 8946 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8947 | EXPECT_EQ(connection_.ping_timeout(), |
| 8948 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8949 | |
| 8950 | // Receive an ACK of the first packet. This should set the ping alarm with |
| 8951 | // initial retransmittable-on-wire timeout since there is no retransmittable |
| 8952 | // packet on the wire. |
| 8953 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8954 | QuicAckFrame frame = |
| 8955 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(2)}}); |
| 8956 | ProcessAckPacket(&frame); |
| 8957 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8958 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8959 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8960 | |
| 8961 | // Simulate the alarm firing and check that a PING is sent. |
| 8962 | writer_->Reset(); |
| 8963 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { SendPing(); })); |
| 8964 | clock_.AdvanceTime(initial_retransmittable_on_wire_timeout); |
| 8965 | connection_.GetPingAlarm()->Fire(); |
| 8966 | |
| 8967 | // Receive an ACK for the previous PING. Ping alarm will be set with |
| 8968 | // aggressive timeout. |
| 8969 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 8970 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8971 | frame = InitAckFrame( |
| 8972 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8973 | ProcessAckPacket(&frame); |
| 8974 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8975 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8976 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8977 | |
| 8978 | // Process a data packet. |
| 8979 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 8980 | ProcessDataPacket(peer_creator_.packet_number() + 1); |
| 8981 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, |
| 8982 | peer_creator_.packet_number() + 1); |
| 8983 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8984 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8985 | |
| 8986 | // Verify the count of consecutive aggressive pings is reset. |
| 8987 | for (int i = 0; i < max_aggressive_retransmittable_on_wire_ping_count; i++) { |
| 8988 | // Receive an ACK of the previous packet. This should set the ping alarm |
| 8989 | // with the initial retransmittable-on-wire timeout. |
| 8990 | QuicPacketNumber ack_num = creator_->packet_number(); |
| 8991 | QuicAckFrame frame = InitAckFrame( |
| 8992 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 8993 | ProcessAckPacket(&frame); |
| 8994 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 8995 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 8996 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 8997 | // Simulate the alarm firing and check that a PING is sent. |
| 8998 | writer_->Reset(); |
| 8999 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { |
| 9000 | SendPing(); |
| 9001 | })); |
| 9002 | clock_.AdvanceTime(initial_retransmittable_on_wire_timeout); |
| 9003 | connection_.GetPingAlarm()->Fire(); |
| 9004 | // Advance 5ms to receive next packet. |
| 9005 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 9006 | } |
| 9007 | |
| 9008 | // Receive another ACK for the previous PING. This should set the |
| 9009 | // ping alarm with backed off retransmittable-on-wire timeout. |
| 9010 | ack_num = creator_->packet_number(); |
| 9011 | frame = InitAckFrame( |
| 9012 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 9013 | ProcessAckPacket(&frame); |
| 9014 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 9015 | EXPECT_EQ(initial_retransmittable_on_wire_timeout * 2, |
| 9016 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 9017 | |
| 9018 | writer_->Reset(); |
| 9019 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { SendPing(); })); |
| 9020 | clock_.AdvanceTime(2 * initial_retransmittable_on_wire_timeout); |
| 9021 | connection_.GetPingAlarm()->Fire(); |
| 9022 | |
| 9023 | // Process another data packet and a new ACK packet. The ping alarm is set |
| 9024 | // with aggressive ping timeout again. |
| 9025 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 9026 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 9027 | ProcessDataPacket(peer_creator_.packet_number() + 1); |
| 9028 | QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, |
| 9029 | peer_creator_.packet_number() + 1); |
| 9030 | ack_num = creator_->packet_number(); |
| 9031 | frame = InitAckFrame( |
| 9032 | {{QuicPacketNumber(ack_num), QuicPacketNumber(ack_num + 1)}}); |
| 9033 | ProcessAckPacket(&frame); |
| 9034 | EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); |
| 9035 | EXPECT_EQ(initial_retransmittable_on_wire_timeout, |
| 9036 | connection_.GetPingAlarm()->deadline() - clock_.ApproximateNow()); |
| 9037 | } |
| 9038 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9039 | TEST_P(QuicConnectionTest, ValidStatelessResetToken) { |
| 9040 | const QuicUint128 kTestToken = 1010101; |
| 9041 | const QuicUint128 kWrongTestToken = 1010100; |
| 9042 | QuicConfig config; |
| 9043 | // No token has been received. |
| 9044 | EXPECT_FALSE(connection_.IsValidStatelessResetToken(kTestToken)); |
| 9045 | |
| 9046 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(2); |
| 9047 | // Token is different from received token. |
| 9048 | QuicConfigPeer::SetReceivedStatelessResetToken(&config, kTestToken); |
| 9049 | connection_.SetFromConfig(config); |
| 9050 | EXPECT_FALSE(connection_.IsValidStatelessResetToken(kWrongTestToken)); |
| 9051 | |
| 9052 | QuicConfigPeer::SetReceivedStatelessResetToken(&config, kTestToken); |
| 9053 | connection_.SetFromConfig(config); |
| 9054 | EXPECT_TRUE(connection_.IsValidStatelessResetToken(kTestToken)); |
| 9055 | } |
| 9056 | |
| 9057 | TEST_P(QuicConnectionTest, WriteBlockedWithInvalidAck) { |
| 9058 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 9059 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9060 | BlockOnNextWrite(); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 9061 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9062 | connection_.SendStreamDataWithString(5, "foo", 0, FIN); |
| 9063 | // This causes connection to be closed because packet 1 has not been sent yet. |
| 9064 | QuicAckFrame frame = InitAckFrame(1); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 9065 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9066 | ProcessAckPacket(1, &frame); |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 9067 | EXPECT_EQ(0, connection_close_frame_count_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9068 | } |
| 9069 | |
| 9070 | TEST_P(QuicConnectionTest, SendMessage) { |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 9071 | if (!VersionSupportsMessageFrames(connection_.transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9072 | return; |
| 9073 | } |
dschinazi | ed459c0 | 2020-05-07 16:12:23 -0700 | [diff] [blame] | 9074 | if (connection_.version().UsesTls()) { |
| 9075 | QuicConfig config; |
| 9076 | QuicConfigPeer::SetReceivedMaxDatagramFrameSize( |
| 9077 | &config, kMaxAcceptedDatagramFrameSize); |
| 9078 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9079 | connection_.SetFromConfig(config); |
| 9080 | } |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 9081 | std::string message(connection_.GetCurrentLargestMessagePayload() * 2, 'a'); |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 9082 | quiche::QuicheStringPiece message_data(message); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9083 | QuicMemSliceStorage storage(nullptr, 0, nullptr, 0); |
| 9084 | { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 9085 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9086 | connection_.SendStreamData3(); |
| 9087 | // Send a message which cannot fit into current open packet, and 2 packets |
| 9088 | // get sent, one contains stream frame, and the other only contains the |
| 9089 | // message frame. |
| 9090 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
QUICHE team | 350e9e6 | 2019-11-19 13:16:24 -0800 | [diff] [blame] | 9091 | EXPECT_EQ(MESSAGE_STATUS_SUCCESS, |
| 9092 | connection_.SendMessage( |
| 9093 | 1, |
| 9094 | MakeSpan(connection_.helper()->GetStreamSendBufferAllocator(), |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 9095 | quiche::QuicheStringPiece( |
QUICHE team | 350e9e6 | 2019-11-19 13:16:24 -0800 | [diff] [blame] | 9096 | message_data.data(), |
| 9097 | connection_.GetCurrentLargestMessagePayload()), |
| 9098 | &storage), |
| 9099 | false)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9100 | } |
| 9101 | // Fail to send a message if connection is congestion control blocked. |
| 9102 | EXPECT_CALL(*send_algorithm_, CanSend(_)).WillOnce(Return(false)); |
QUICHE team | 350e9e6 | 2019-11-19 13:16:24 -0800 | [diff] [blame] | 9103 | EXPECT_EQ(MESSAGE_STATUS_BLOCKED, |
| 9104 | connection_.SendMessage( |
| 9105 | 2, |
| 9106 | MakeSpan(connection_.helper()->GetStreamSendBufferAllocator(), |
| 9107 | "message", &storage), |
| 9108 | false)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9109 | |
| 9110 | // Always fail to send a message which cannot fit into one packet. |
| 9111 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
QUICHE team | 350e9e6 | 2019-11-19 13:16:24 -0800 | [diff] [blame] | 9112 | EXPECT_EQ(MESSAGE_STATUS_TOO_LARGE, |
| 9113 | connection_.SendMessage( |
| 9114 | 3, |
| 9115 | MakeSpan(connection_.helper()->GetStreamSendBufferAllocator(), |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 9116 | quiche::QuicheStringPiece( |
QUICHE team | 350e9e6 | 2019-11-19 13:16:24 -0800 | [diff] [blame] | 9117 | message_data.data(), |
| 9118 | connection_.GetCurrentLargestMessagePayload() + 1), |
| 9119 | &storage), |
| 9120 | false)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9121 | } |
| 9122 | |
dschinazi | ed459c0 | 2020-05-07 16:12:23 -0700 | [diff] [blame] | 9123 | TEST_P(QuicConnectionTest, GetCurrentLargestMessagePayload) { |
| 9124 | if (!connection_.version().SupportsMessageFrames()) { |
| 9125 | return; |
| 9126 | } |
| 9127 | // Force use of this encrypter to simplify test expectations by making sure |
| 9128 | // that the encryption overhead is constant across versions. |
| 9129 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
| 9130 | std::make_unique<TaggingEncrypter>(0x00)); |
| 9131 | QuicPacketLength expected_largest_payload = 1319; |
| 9132 | if (connection_.version().SendsVariableLengthPacketNumberInLongHeader()) { |
| 9133 | expected_largest_payload += 3; |
| 9134 | } |
| 9135 | if (connection_.version().HasLongHeaderLengths()) { |
| 9136 | expected_largest_payload -= 2; |
| 9137 | } |
| 9138 | if (connection_.version().HasLengthPrefixedConnectionIds()) { |
| 9139 | expected_largest_payload -= 1; |
| 9140 | } |
| 9141 | if (connection_.version().UsesTls()) { |
| 9142 | // QUIC+TLS disallows DATAGRAM/MESSAGE frames before the handshake. |
| 9143 | EXPECT_EQ(connection_.GetCurrentLargestMessagePayload(), 0); |
| 9144 | QuicConfig config; |
| 9145 | QuicConfigPeer::SetReceivedMaxDatagramFrameSize( |
| 9146 | &config, kMaxAcceptedDatagramFrameSize); |
| 9147 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9148 | connection_.SetFromConfig(config); |
| 9149 | // Verify the value post-handshake. |
| 9150 | EXPECT_EQ(connection_.GetCurrentLargestMessagePayload(), |
| 9151 | expected_largest_payload); |
| 9152 | } else { |
| 9153 | EXPECT_EQ(connection_.GetCurrentLargestMessagePayload(), |
| 9154 | expected_largest_payload); |
| 9155 | } |
| 9156 | } |
| 9157 | |
| 9158 | TEST_P(QuicConnectionTest, GetGuaranteedLargestMessagePayload) { |
| 9159 | if (!connection_.version().SupportsMessageFrames()) { |
| 9160 | return; |
| 9161 | } |
| 9162 | // Force use of this encrypter to simplify test expectations by making sure |
| 9163 | // that the encryption overhead is constant across versions. |
| 9164 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
| 9165 | std::make_unique<TaggingEncrypter>(0x00)); |
| 9166 | QuicPacketLength expected_largest_payload = 1319; |
| 9167 | if (connection_.version().HasLongHeaderLengths()) { |
| 9168 | expected_largest_payload -= 2; |
| 9169 | } |
| 9170 | if (connection_.version().HasLengthPrefixedConnectionIds()) { |
| 9171 | expected_largest_payload -= 1; |
| 9172 | } |
| 9173 | if (connection_.version().UsesTls()) { |
| 9174 | // QUIC+TLS disallows DATAGRAM/MESSAGE frames before the handshake. |
| 9175 | EXPECT_EQ(connection_.GetGuaranteedLargestMessagePayload(), 0); |
| 9176 | QuicConfig config; |
| 9177 | QuicConfigPeer::SetReceivedMaxDatagramFrameSize( |
| 9178 | &config, kMaxAcceptedDatagramFrameSize); |
| 9179 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9180 | connection_.SetFromConfig(config); |
| 9181 | // Verify the value post-handshake. |
| 9182 | EXPECT_EQ(connection_.GetGuaranteedLargestMessagePayload(), |
| 9183 | expected_largest_payload); |
| 9184 | } else { |
| 9185 | EXPECT_EQ(connection_.GetGuaranteedLargestMessagePayload(), |
| 9186 | expected_largest_payload); |
| 9187 | } |
| 9188 | } |
| 9189 | |
| 9190 | TEST_P(QuicConnectionTest, LimitedLargestMessagePayload) { |
| 9191 | if (!connection_.version().SupportsMessageFrames() || |
| 9192 | !connection_.version().UsesTls()) { |
| 9193 | return; |
| 9194 | } |
| 9195 | constexpr QuicPacketLength kFrameSizeLimit = 1000; |
| 9196 | constexpr QuicPacketLength kPayloadSizeLimit = |
| 9197 | kFrameSizeLimit - kQuicFrameTypeSize; |
| 9198 | // QUIC+TLS disallows DATAGRAM/MESSAGE frames before the handshake. |
| 9199 | EXPECT_EQ(connection_.GetCurrentLargestMessagePayload(), 0); |
| 9200 | EXPECT_EQ(connection_.GetGuaranteedLargestMessagePayload(), 0); |
| 9201 | QuicConfig config; |
| 9202 | QuicConfigPeer::SetReceivedMaxDatagramFrameSize(&config, kFrameSizeLimit); |
| 9203 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9204 | connection_.SetFromConfig(config); |
| 9205 | // Verify the value post-handshake. |
| 9206 | EXPECT_EQ(connection_.GetCurrentLargestMessagePayload(), kPayloadSizeLimit); |
| 9207 | EXPECT_EQ(connection_.GetGuaranteedLargestMessagePayload(), |
| 9208 | kPayloadSizeLimit); |
| 9209 | } |
| 9210 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9211 | // Test to check that the path challenge/path response logic works |
| 9212 | // correctly. This test is only for version-99 |
| 9213 | TEST_P(QuicConnectionTest, PathChallengeResponse) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 9214 | if (!VersionHasIetfQuicFrames(connection_.version().transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9215 | return; |
| 9216 | } |
| 9217 | // First check if we can probe from server to client and back |
| 9218 | set_perspective(Perspective::IS_SERVER); |
| 9219 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 9220 | |
| 9221 | // Create and send the probe request (PATH_CHALLENGE frame). |
| 9222 | // SendConnectivityProbingPacket ends up calling |
| 9223 | // TestPacketWriter::WritePacket() which in turns receives and parses the |
| 9224 | // packet by calling framer_.ProcessPacket() -- which in turn calls |
| 9225 | // SimpleQuicFramer::OnPathChallengeFrame(). SimpleQuicFramer saves |
| 9226 | // the packet in writer_->path_challenge_frames() |
| 9227 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9228 | connection_.SendConnectivityProbingPacket(writer_.get(), |
| 9229 | connection_.peer_address()); |
| 9230 | // Save the random contents of the challenge for later comparison to the |
| 9231 | // response. |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 9232 | ASSERT_GE(writer_->path_challenge_frames().size(), 1u); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9233 | QuicPathFrameBuffer challenge_data = |
| 9234 | writer_->path_challenge_frames().front().data_buffer; |
| 9235 | |
| 9236 | // Normally, QuicConnection::OnPathChallengeFrame and OnPaddingFrame would be |
| 9237 | // called and it will perform actions to ensure that the rest of the protocol |
| 9238 | // is performed (specifically, call UpdatePacketContent to say that this is a |
| 9239 | // path challenge so that when QuicConnection::OnPacketComplete is called |
| 9240 | // (again, out of the framer), the response is generated). Simulate those |
| 9241 | // calls so that the right internal state is set up for generating |
| 9242 | // the response. |
| 9243 | EXPECT_TRUE(connection_.OnPathChallengeFrame( |
| 9244 | writer_->path_challenge_frames().front())); |
| 9245 | EXPECT_TRUE(connection_.OnPaddingFrame(writer_->padding_frames().front())); |
| 9246 | // Cause the response to be created and sent. Result is that the response |
| 9247 | // should be stashed in writer's path_response_frames. |
| 9248 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9249 | connection_.SendConnectivityProbingResponsePacket(connection_.peer_address()); |
| 9250 | |
| 9251 | // The final check is to ensure that the random data in the response matches |
| 9252 | // the random data from the challenge. |
| 9253 | EXPECT_EQ(0, memcmp(&challenge_data, |
| 9254 | &(writer_->path_response_frames().front().data_buffer), |
| 9255 | sizeof(challenge_data))); |
| 9256 | } |
| 9257 | |
| 9258 | // Regression test for b/110259444 |
| 9259 | TEST_P(QuicConnectionTest, DoNotScheduleSpuriousAckAlarm) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9260 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9261 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); |
| 9262 | writer_->SetWriteBlocked(); |
| 9263 | |
| 9264 | ProcessPacket(1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9265 | // Verify ack alarm is set. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9266 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9267 | // Fire the ack alarm, verify no packet is sent because the writer is blocked. |
| 9268 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9269 | connection_.GetAckAlarm()->Fire(); |
| 9270 | |
| 9271 | writer_->SetWritable(); |
| 9272 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9273 | ProcessPacket(2); |
| 9274 | // Verify ack alarm is not set. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9275 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9276 | } |
| 9277 | |
| 9278 | TEST_P(QuicConnectionTest, DisablePacingOffloadConnectionOptions) { |
| 9279 | EXPECT_FALSE(QuicConnectionPeer::SupportsReleaseTime(&connection_)); |
| 9280 | writer_->set_supports_release_time(true); |
| 9281 | QuicConfig config; |
| 9282 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9283 | connection_.SetFromConfig(config); |
| 9284 | EXPECT_TRUE(QuicConnectionPeer::SupportsReleaseTime(&connection_)); |
| 9285 | |
| 9286 | QuicTagVector connection_options; |
| 9287 | connection_options.push_back(kNPCO); |
| 9288 | config.SetConnectionOptionsToSend(connection_options); |
| 9289 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9290 | connection_.SetFromConfig(config); |
| 9291 | // Verify pacing offload is disabled. |
| 9292 | EXPECT_FALSE(QuicConnectionPeer::SupportsReleaseTime(&connection_)); |
| 9293 | } |
| 9294 | |
| 9295 | // Regression test for b/110259444 |
| 9296 | // Get a path response without having issued a path challenge... |
| 9297 | TEST_P(QuicConnectionTest, OrphanPathResponse) { |
| 9298 | QuicPathFrameBuffer data = {{0, 1, 2, 3, 4, 5, 6, 7}}; |
| 9299 | |
| 9300 | QuicPathResponseFrame frame(99, data); |
| 9301 | EXPECT_TRUE(connection_.OnPathResponseFrame(frame)); |
| 9302 | // If PATH_RESPONSE was accepted (payload matches the payload saved |
| 9303 | // in QuicConnection::transmitted_connectivity_probe_payload_) then |
| 9304 | // current_packet_content_ would be set to FIRST_FRAME_IS_PING. |
| 9305 | // Since this PATH_RESPONSE does not match, current_packet_content_ |
| 9306 | // must not be FIRST_FRAME_IS_PING. |
| 9307 | EXPECT_NE(QuicConnection::FIRST_FRAME_IS_PING, |
| 9308 | QuicConnectionPeer::GetCurrentPacketContent(&connection_)); |
| 9309 | } |
| 9310 | |
| 9311 | // Regression test for b/120791670 |
| 9312 | TEST_P(QuicConnectionTest, StopProcessingGQuicPacketInIetfQuicConnection) { |
| 9313 | // This test mimics a problematic scenario where an IETF QUIC connection |
| 9314 | // receives a Google QUIC packet and continue processing it using Google QUIC |
| 9315 | // wire format. |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 9316 | if (!VersionHasIetfInvariantHeader(version().transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9317 | return; |
| 9318 | } |
| 9319 | set_perspective(Perspective::IS_SERVER); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 9320 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 9321 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(1); |
| 9322 | } else { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 9323 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); |
| 9324 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 9325 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 9326 | kPeerAddress); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9327 | |
| 9328 | // Let connection process a Google QUIC packet. |
| 9329 | peer_framer_.set_version_for_tests( |
| 9330 | ParsedQuicVersion(PROTOCOL_QUIC_CRYPTO, QUIC_VERSION_43)); |
QUICHE team | 8c1daa2 | 2019-03-13 08:33:41 -0700 | [diff] [blame] | 9331 | std::unique_ptr<QuicPacket> packet( |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 9332 | ConstructDataPacket(2, !kHasStopWaiting, ENCRYPTION_INITIAL)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 9333 | char buffer[kMaxOutgoingPacketSize]; |
| 9334 | size_t encrypted_length = |
| 9335 | peer_framer_.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(2), |
| 9336 | *packet, buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9337 | // Make sure no stream frame is processed. |
| 9338 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(0); |
| 9339 | connection_.ProcessUdpPacket( |
| 9340 | kSelfAddress, kPeerAddress, |
| 9341 | QuicReceivedPacket(buffer, encrypted_length, clock_.Now(), false)); |
| 9342 | |
| 9343 | EXPECT_EQ(2u, connection_.GetStats().packets_received); |
| 9344 | EXPECT_EQ(1u, connection_.GetStats().packets_processed); |
| 9345 | } |
| 9346 | |
| 9347 | TEST_P(QuicConnectionTest, AcceptPacketNumberZero) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 9348 | if (!VersionHasIetfQuicFrames(version().transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9349 | return; |
| 9350 | } |
| 9351 | // Set first_sending_packet_number to be 0 to allow successfully processing |
| 9352 | // acks which ack packet number 0. |
| 9353 | QuicFramerPeer::SetFirstSendingPacketNumber(writer_->framer()->framer(), 0); |
| 9354 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9355 | |
| 9356 | ProcessPacket(0); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 9357 | EXPECT_EQ(QuicPacketNumber(0), LargestAcked(connection_.ack_frame())); |
| 9358 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9359 | |
| 9360 | ProcessPacket(1); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 9361 | EXPECT_EQ(QuicPacketNumber(1), LargestAcked(connection_.ack_frame())); |
| 9362 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9363 | |
| 9364 | ProcessPacket(2); |
fayang | c31c995 | 2019-06-05 13:54:48 -0700 | [diff] [blame] | 9365 | EXPECT_EQ(QuicPacketNumber(2), LargestAcked(connection_.ack_frame())); |
| 9366 | EXPECT_EQ(1u, connection_.ack_frame().packets.NumIntervals()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9367 | } |
| 9368 | |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9369 | TEST_P(QuicConnectionTest, MultiplePacketNumberSpacesBasicSending) { |
| 9370 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 9371 | return; |
| 9372 | } |
| 9373 | use_tagging_decrypter(); |
| 9374 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9375 | std::make_unique<TaggingEncrypter>(0x01)); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9376 | |
| 9377 | connection_.SendCryptoStreamData(); |
| 9378 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9379 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 9380 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 9381 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9382 | QuicAckFrame frame1 = InitAckFrame(1); |
| 9383 | // Received ACK for packet 1. |
| 9384 | ProcessFramePacketAtLevel(30, QuicFrame(&frame1), ENCRYPTION_INITIAL); |
| 9385 | |
| 9386 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(4); |
| 9387 | connection_.SendApplicationDataAtLevel(ENCRYPTION_ZERO_RTT, 5, "data", 0, |
| 9388 | NO_FIN); |
| 9389 | connection_.SendApplicationDataAtLevel(ENCRYPTION_ZERO_RTT, 5, "data", 4, |
| 9390 | NO_FIN); |
| 9391 | connection_.SendApplicationDataAtLevel(ENCRYPTION_FORWARD_SECURE, 5, "data", |
| 9392 | 8, NO_FIN); |
| 9393 | connection_.SendApplicationDataAtLevel(ENCRYPTION_FORWARD_SECURE, 5, "data", |
| 9394 | 12, FIN); |
| 9395 | // Received ACK for packets 2, 4, 5. |
| 9396 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 9397 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 9398 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9399 | QuicAckFrame frame2 = |
| 9400 | InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(3)}, |
| 9401 | {QuicPacketNumber(4), QuicPacketNumber(6)}}); |
| 9402 | // Make sure although the same packet number is used, but they are in |
| 9403 | // different packet number spaces. |
| 9404 | ProcessFramePacketAtLevel(30, QuicFrame(&frame2), ENCRYPTION_FORWARD_SECURE); |
| 9405 | } |
| 9406 | |
| 9407 | TEST_P(QuicConnectionTest, PeerAcksPacketsInWrongPacketNumberSpace) { |
| 9408 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 9409 | return; |
| 9410 | } |
| 9411 | use_tagging_decrypter(); |
| 9412 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9413 | std::make_unique<TaggingEncrypter>(0x01)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 9414 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 9415 | std::make_unique<TaggingEncrypter>(0x01)); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9416 | |
| 9417 | connection_.SendCryptoStreamData(); |
| 9418 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9419 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 9420 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 9421 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9422 | QuicAckFrame frame1 = InitAckFrame(1); |
| 9423 | // Received ACK for packet 1. |
| 9424 | ProcessFramePacketAtLevel(30, QuicFrame(&frame1), ENCRYPTION_INITIAL); |
| 9425 | |
| 9426 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 9427 | connection_.SendApplicationDataAtLevel(ENCRYPTION_ZERO_RTT, 5, "data", 0, |
| 9428 | NO_FIN); |
| 9429 | connection_.SendApplicationDataAtLevel(ENCRYPTION_ZERO_RTT, 5, "data", 4, |
| 9430 | NO_FIN); |
| 9431 | |
| 9432 | // Received ACK for packets 2 and 3 in wrong packet number space. |
| 9433 | QuicAckFrame invalid_ack = |
| 9434 | InitAckFrame({{QuicPacketNumber(2), QuicPacketNumber(4)}}); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 9435 | EXPECT_CALL(visitor_, |
| 9436 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 9437 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9438 | ProcessFramePacketAtLevel(300, QuicFrame(&invalid_ack), ENCRYPTION_INITIAL); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 9439 | TestConnectionCloseQuicErrorCode(QUIC_INVALID_ACK_DATA); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9440 | } |
| 9441 | |
| 9442 | TEST_P(QuicConnectionTest, MultiplePacketNumberSpacesBasicReceiving) { |
| 9443 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 9444 | return; |
| 9445 | } |
| 9446 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 9447 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9448 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 9449 | } |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9450 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 9451 | use_tagging_decrypter(); |
| 9452 | // Receives packet 1000 in initial data. |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 9453 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9454 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9455 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9456 | std::make_unique<TaggingEncrypter>(0x02)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 9457 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9458 | std::make_unique<StrictTaggingDecrypter>(0x02)); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9459 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9460 | std::make_unique<TaggingEncrypter>(0x02)); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9461 | // Receives packet 1000 in application data. |
| 9462 | ProcessDataPacketAtLevel(1000, false, ENCRYPTION_ZERO_RTT); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9463 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9464 | connection_.SendApplicationDataAtLevel(ENCRYPTION_ZERO_RTT, 5, "data", 0, |
| 9465 | NO_FIN); |
| 9466 | // Verify application data ACK gets bundled with outgoing data. |
| 9467 | EXPECT_EQ(2u, writer_->frame_count()); |
| 9468 | // Make sure ACK alarm is still set because initial data is not ACKed. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9469 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9470 | // Receive packet 1001 in application data. |
| 9471 | ProcessDataPacketAtLevel(1001, false, ENCRYPTION_ZERO_RTT); |
| 9472 | clock_.AdvanceTime(DefaultRetransmissionTime()); |
| 9473 | // Simulates ACK alarm fires and verify two ACKs are flushed. |
| 9474 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 9475 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9476 | std::make_unique<TaggingEncrypter>(0x02)); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9477 | connection_.GetAckAlarm()->Fire(); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9478 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9479 | // Receives more packets in application data. |
| 9480 | ProcessDataPacketAtLevel(1002, false, ENCRYPTION_ZERO_RTT); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9481 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9482 | |
| 9483 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9484 | std::make_unique<TaggingEncrypter>(0x02)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 9485 | SetDecrypter(ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9486 | std::make_unique<StrictTaggingDecrypter>(0x02)); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9487 | // Verify zero rtt and forward secure packets get acked in the same packet. |
| 9488 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
nharper | 2c9f02a | 2019-05-08 10:25:50 -0700 | [diff] [blame] | 9489 | ProcessDataPacket(1003); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9490 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 9491 | } |
| 9492 | |
QUICHE team | 552f71f | 2019-03-23 15:37:59 -0700 | [diff] [blame] | 9493 | TEST_P(QuicConnectionTest, CancelAckAlarmOnWriteBlocked) { |
| 9494 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 9495 | return; |
| 9496 | } |
| 9497 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 9498 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9499 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 9500 | } |
QUICHE team | 552f71f | 2019-03-23 15:37:59 -0700 | [diff] [blame] | 9501 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 9502 | use_tagging_decrypter(); |
| 9503 | // Receives packet 1000 in initial data. |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 9504 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9505 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | 552f71f | 2019-03-23 15:37:59 -0700 | [diff] [blame] | 9506 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9507 | std::make_unique<TaggingEncrypter>(0x02)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 9508 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9509 | std::make_unique<StrictTaggingDecrypter>(0x02)); |
QUICHE team | 552f71f | 2019-03-23 15:37:59 -0700 | [diff] [blame] | 9510 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9511 | std::make_unique<TaggingEncrypter>(0x02)); |
QUICHE team | 552f71f | 2019-03-23 15:37:59 -0700 | [diff] [blame] | 9512 | // Receives packet 1000 in application data. |
| 9513 | ProcessDataPacketAtLevel(1000, false, ENCRYPTION_ZERO_RTT); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9514 | EXPECT_TRUE(connection_.HasPendingAcks()); |
QUICHE team | 552f71f | 2019-03-23 15:37:59 -0700 | [diff] [blame] | 9515 | |
| 9516 | writer_->SetWriteBlocked(); |
| 9517 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AnyNumber()); |
| 9518 | // Simulates ACK alarm fires and verify no ACK is flushed because of write |
| 9519 | // blocked. |
| 9520 | clock_.AdvanceTime(DefaultDelayedAckTime()); |
| 9521 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9522 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 9523 | std::make_unique<TaggingEncrypter>(0x02)); |
QUICHE team | 552f71f | 2019-03-23 15:37:59 -0700 | [diff] [blame] | 9524 | connection_.GetAckAlarm()->Fire(); |
| 9525 | // Verify ACK alarm is not set. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9526 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | 552f71f | 2019-03-23 15:37:59 -0700 | [diff] [blame] | 9527 | |
| 9528 | writer_->SetWritable(); |
| 9529 | // Verify 2 ACKs are sent when connection gets unblocked. |
| 9530 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); |
| 9531 | connection_.OnCanWrite(); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9532 | EXPECT_FALSE(connection_.HasPendingAcks()); |
QUICHE team | 552f71f | 2019-03-23 15:37:59 -0700 | [diff] [blame] | 9533 | } |
| 9534 | |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 9535 | // Make sure a packet received with the right client connection ID is processed. |
| 9536 | TEST_P(QuicConnectionTest, ValidClientConnectionId) { |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 9537 | if (!framer_.version().SupportsClientConnectionIds()) { |
| 9538 | return; |
| 9539 | } |
| 9540 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9541 | connection_.set_client_connection_id(TestConnectionId(0x33)); |
| 9542 | QuicPacketHeader header = ConstructPacketHeader(1, ENCRYPTION_FORWARD_SECURE); |
| 9543 | header.destination_connection_id = TestConnectionId(0x33); |
| 9544 | header.destination_connection_id_included = CONNECTION_ID_PRESENT; |
| 9545 | header.source_connection_id_included = CONNECTION_ID_ABSENT; |
| 9546 | QuicFrames frames; |
| 9547 | QuicPingFrame ping_frame; |
| 9548 | QuicPaddingFrame padding_frame; |
| 9549 | frames.push_back(QuicFrame(ping_frame)); |
| 9550 | frames.push_back(QuicFrame(padding_frame)); |
| 9551 | std::unique_ptr<QuicPacket> packet = |
| 9552 | BuildUnsizedDataPacket(&framer_, header, frames); |
| 9553 | char buffer[kMaxOutgoingPacketSize]; |
| 9554 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 9555 | ENCRYPTION_FORWARD_SECURE, QuicPacketNumber(1), *packet, buffer, |
| 9556 | kMaxOutgoingPacketSize); |
| 9557 | QuicReceivedPacket received_packet(buffer, encrypted_length, clock_.Now(), |
| 9558 | false); |
| 9559 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9560 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, received_packet); |
| 9561 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9562 | } |
| 9563 | |
| 9564 | // Make sure a packet received with a different client connection ID is dropped. |
| 9565 | TEST_P(QuicConnectionTest, InvalidClientConnectionId) { |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 9566 | if (!framer_.version().SupportsClientConnectionIds()) { |
| 9567 | return; |
| 9568 | } |
| 9569 | connection_.set_client_connection_id(TestConnectionId(0x33)); |
| 9570 | QuicPacketHeader header = ConstructPacketHeader(1, ENCRYPTION_FORWARD_SECURE); |
| 9571 | header.destination_connection_id = TestConnectionId(0xbad); |
| 9572 | header.destination_connection_id_included = CONNECTION_ID_PRESENT; |
| 9573 | header.source_connection_id_included = CONNECTION_ID_ABSENT; |
| 9574 | QuicFrames frames; |
| 9575 | QuicPingFrame ping_frame; |
| 9576 | QuicPaddingFrame padding_frame; |
| 9577 | frames.push_back(QuicFrame(ping_frame)); |
| 9578 | frames.push_back(QuicFrame(padding_frame)); |
| 9579 | std::unique_ptr<QuicPacket> packet = |
| 9580 | BuildUnsizedDataPacket(&framer_, header, frames); |
| 9581 | char buffer[kMaxOutgoingPacketSize]; |
| 9582 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 9583 | ENCRYPTION_FORWARD_SECURE, QuicPacketNumber(1), *packet, buffer, |
| 9584 | kMaxOutgoingPacketSize); |
| 9585 | QuicReceivedPacket received_packet(buffer, encrypted_length, clock_.Now(), |
| 9586 | false); |
| 9587 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9588 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, received_packet); |
| 9589 | EXPECT_EQ(1u, connection_.GetStats().packets_dropped); |
| 9590 | } |
| 9591 | |
| 9592 | // Make sure the first packet received with a different client connection ID on |
| 9593 | // the server is processed and it changes the client connection ID. |
| 9594 | TEST_P(QuicConnectionTest, UpdateClientConnectionIdFromFirstPacket) { |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 9595 | if (!framer_.version().SupportsClientConnectionIds()) { |
| 9596 | return; |
| 9597 | } |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 9598 | set_perspective(Perspective::IS_SERVER); |
| 9599 | QuicPacketHeader header = ConstructPacketHeader(1, ENCRYPTION_INITIAL); |
| 9600 | header.source_connection_id = TestConnectionId(0x33); |
| 9601 | header.source_connection_id_included = CONNECTION_ID_PRESENT; |
| 9602 | QuicFrames frames; |
| 9603 | QuicPingFrame ping_frame; |
| 9604 | QuicPaddingFrame padding_frame; |
| 9605 | frames.push_back(QuicFrame(ping_frame)); |
| 9606 | frames.push_back(QuicFrame(padding_frame)); |
| 9607 | std::unique_ptr<QuicPacket> packet = |
| 9608 | BuildUnsizedDataPacket(&framer_, header, frames); |
| 9609 | char buffer[kMaxOutgoingPacketSize]; |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 9610 | size_t encrypted_length = |
| 9611 | peer_framer_.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(1), |
| 9612 | *packet, buffer, kMaxOutgoingPacketSize); |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 9613 | QuicReceivedPacket received_packet(buffer, encrypted_length, clock_.Now(), |
| 9614 | false); |
| 9615 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9616 | ProcessReceivedPacket(kSelfAddress, kPeerAddress, received_packet); |
| 9617 | EXPECT_EQ(0u, connection_.GetStats().packets_dropped); |
| 9618 | EXPECT_EQ(TestConnectionId(0x33), connection_.client_connection_id()); |
| 9619 | } |
| 9620 | |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 9621 | // Regression test for b/134416344. |
| 9622 | TEST_P(QuicConnectionTest, CheckConnectedBeforeFlush) { |
| 9623 | // This test mimics a scenario where a connection processes 2 packets and the |
| 9624 | // 2nd packet contains connection close frame. When the 2nd flusher goes out |
| 9625 | // of scope, a delayed ACK is pending, and ACK alarm should not be scheduled |
| 9626 | // because connection is disconnected. |
| 9627 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 9628 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 9629 | EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); |
fkastenholz | 88d08f4 | 2019-09-06 07:38:04 -0700 | [diff] [blame] | 9630 | const QuicErrorCode kErrorCode = QUIC_INTERNAL_ERROR; |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 9631 | std::unique_ptr<QuicConnectionCloseFrame> connection_close_frame( |
fkastenholz | 591814c | 2019-09-06 12:11:46 -0700 | [diff] [blame] | 9632 | new QuicConnectionCloseFrame(connection_.transport_version(), kErrorCode, |
| 9633 | "", |
| 9634 | /*transport_close_frame_type=*/0)); |
| 9635 | |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 9636 | // Received 2 packets. |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 9637 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 9638 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 9639 | } else { |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 9640 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 9641 | } |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 9642 | ProcessFramePacketWithAddresses(MakeCryptoFrame(), kSelfAddress, |
| 9643 | kPeerAddress); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9644 | EXPECT_TRUE(connection_.HasPendingAcks()); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 9645 | ProcessFramePacketWithAddresses(QuicFrame(connection_close_frame.release()), |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 9646 | kSelfAddress, kPeerAddress); |
fayang | 0f0c4e6 | 2019-07-16 08:55:54 -0700 | [diff] [blame] | 9647 | // Verify ack alarm is not set. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 9648 | EXPECT_FALSE(connection_.HasPendingAcks()); |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 9649 | } |
| 9650 | |
dschinazi | 8d55113 | 2019-08-02 19:17:16 -0700 | [diff] [blame] | 9651 | // Verify that a packet containing three coalesced packets is parsed correctly. |
| 9652 | TEST_P(QuicConnectionTest, CoalescedPacket) { |
| 9653 | if (!QuicVersionHasLongHeaderLengths(connection_.transport_version())) { |
| 9654 | // Coalesced packets can only be encoded using long header lengths. |
| 9655 | return; |
| 9656 | } |
| 9657 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9658 | EXPECT_TRUE(connection_.connected()); |
| 9659 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9660 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(3); |
| 9661 | } else { |
| 9662 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(3); |
| 9663 | } |
| 9664 | |
| 9665 | uint64_t packet_numbers[3] = {1, 2, 3}; |
| 9666 | EncryptionLevel encryption_levels[3] = { |
| 9667 | ENCRYPTION_INITIAL, ENCRYPTION_INITIAL, ENCRYPTION_FORWARD_SECURE}; |
| 9668 | char buffer[kMaxOutgoingPacketSize] = {}; |
| 9669 | size_t total_encrypted_length = 0; |
| 9670 | for (int i = 0; i < 3; i++) { |
| 9671 | QuicPacketHeader header = |
| 9672 | ConstructPacketHeader(packet_numbers[i], encryption_levels[i]); |
| 9673 | QuicFrames frames; |
| 9674 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9675 | frames.push_back(QuicFrame(&crypto_frame_)); |
| 9676 | } else { |
| 9677 | frames.push_back(QuicFrame(frame1_)); |
| 9678 | } |
| 9679 | std::unique_ptr<QuicPacket> packet = ConstructPacket(header, frames); |
| 9680 | peer_creator_.set_encryption_level(encryption_levels[i]); |
| 9681 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 9682 | encryption_levels[i], QuicPacketNumber(packet_numbers[i]), *packet, |
| 9683 | buffer + total_encrypted_length, |
| 9684 | sizeof(buffer) - total_encrypted_length); |
| 9685 | EXPECT_GT(encrypted_length, 0u); |
| 9686 | total_encrypted_length += encrypted_length; |
| 9687 | } |
| 9688 | connection_.ProcessUdpPacket( |
| 9689 | kSelfAddress, kPeerAddress, |
| 9690 | QuicReceivedPacket(buffer, total_encrypted_length, clock_.Now(), false)); |
| 9691 | if (connection_.GetSendAlarm()->IsSet()) { |
| 9692 | connection_.GetSendAlarm()->Fire(); |
| 9693 | } |
| 9694 | |
| 9695 | EXPECT_TRUE(connection_.connected()); |
| 9696 | } |
| 9697 | |
dschinazi | 66fc024 | 2019-08-16 10:00:25 -0700 | [diff] [blame] | 9698 | // Regression test for crbug.com/992831. |
| 9699 | TEST_P(QuicConnectionTest, CoalescedPacketThatSavesFrames) { |
| 9700 | if (!QuicVersionHasLongHeaderLengths(connection_.transport_version())) { |
| 9701 | // Coalesced packets can only be encoded using long header lengths. |
| 9702 | return; |
| 9703 | } |
| 9704 | if (connection_.SupportsMultiplePacketNumberSpaces()) { |
| 9705 | // TODO(b/129151114) Enable this test with multiple packet number spaces. |
| 9706 | return; |
| 9707 | } |
| 9708 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9709 | EXPECT_TRUE(connection_.connected()); |
| 9710 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9711 | EXPECT_CALL(visitor_, OnCryptoFrame(_)) |
| 9712 | .Times(3) |
| 9713 | .WillRepeatedly([this](const QuicCryptoFrame& /*frame*/) { |
| 9714 | // QuicFrame takes ownership of the QuicBlockedFrame. |
| 9715 | connection_.SendControlFrame(QuicFrame(new QuicBlockedFrame(1, 3))); |
| 9716 | }); |
| 9717 | } else { |
| 9718 | EXPECT_CALL(visitor_, OnStreamFrame(_)) |
| 9719 | .Times(3) |
| 9720 | .WillRepeatedly([this](const QuicStreamFrame& /*frame*/) { |
| 9721 | // QuicFrame takes ownership of the QuicBlockedFrame. |
| 9722 | connection_.SendControlFrame(QuicFrame(new QuicBlockedFrame(1, 3))); |
| 9723 | }); |
| 9724 | } |
| 9725 | |
| 9726 | uint64_t packet_numbers[3] = {1, 2, 3}; |
| 9727 | EncryptionLevel encryption_levels[3] = { |
| 9728 | ENCRYPTION_INITIAL, ENCRYPTION_INITIAL, ENCRYPTION_FORWARD_SECURE}; |
| 9729 | char buffer[kMaxOutgoingPacketSize] = {}; |
| 9730 | size_t total_encrypted_length = 0; |
| 9731 | for (int i = 0; i < 3; i++) { |
| 9732 | QuicPacketHeader header = |
| 9733 | ConstructPacketHeader(packet_numbers[i], encryption_levels[i]); |
| 9734 | QuicFrames frames; |
| 9735 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 9736 | frames.push_back(QuicFrame(&crypto_frame_)); |
| 9737 | } else { |
| 9738 | frames.push_back(QuicFrame(frame1_)); |
| 9739 | } |
| 9740 | std::unique_ptr<QuicPacket> packet = ConstructPacket(header, frames); |
| 9741 | peer_creator_.set_encryption_level(encryption_levels[i]); |
| 9742 | size_t encrypted_length = peer_framer_.EncryptPayload( |
| 9743 | encryption_levels[i], QuicPacketNumber(packet_numbers[i]), *packet, |
| 9744 | buffer + total_encrypted_length, |
| 9745 | sizeof(buffer) - total_encrypted_length); |
| 9746 | EXPECT_GT(encrypted_length, 0u); |
| 9747 | total_encrypted_length += encrypted_length; |
| 9748 | } |
| 9749 | connection_.ProcessUdpPacket( |
| 9750 | kSelfAddress, kPeerAddress, |
| 9751 | QuicReceivedPacket(buffer, total_encrypted_length, clock_.Now(), false)); |
| 9752 | if (connection_.GetSendAlarm()->IsSet()) { |
| 9753 | connection_.GetSendAlarm()->Fire(); |
| 9754 | } |
| 9755 | |
| 9756 | EXPECT_TRUE(connection_.connected()); |
| 9757 | |
| 9758 | SendAckPacketToPeer(); |
| 9759 | } |
| 9760 | |
fayang | d301683 | 2019-08-08 07:24:45 -0700 | [diff] [blame] | 9761 | // Regresstion test for b/138962304. |
| 9762 | TEST_P(QuicConnectionTest, RtoAndWriteBlocked) { |
fayang | d301683 | 2019-08-08 07:24:45 -0700 | [diff] [blame] | 9763 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9764 | |
| 9765 | QuicStreamId stream_id = 2; |
| 9766 | QuicPacketNumber last_data_packet; |
| 9767 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_data_packet); |
| 9768 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9769 | |
| 9770 | // Writer gets blocked. |
| 9771 | writer_->SetWriteBlocked(); |
| 9772 | |
| 9773 | // Cancel the stream. |
| 9774 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9775 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); |
fayang | 67f8227 | 2019-08-14 16:08:45 -0700 | [diff] [blame] | 9776 | EXPECT_CALL(visitor_, WillingAndAbleToWrite()) |
| 9777 | .WillRepeatedly( |
| 9778 | Invoke(¬ifier_, &SimpleSessionNotifier::WillingToWrite)); |
fayang | d301683 | 2019-08-08 07:24:45 -0700 | [diff] [blame] | 9779 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 9780 | |
| 9781 | // Retransmission timer fires in RTO mode. |
| 9782 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9783 | // Verify no packets get flushed when writer is blocked. |
| 9784 | EXPECT_EQ(0u, connection_.NumQueuedPackets()); |
| 9785 | } |
| 9786 | |
| 9787 | // Regresstion test for b/138962304. |
| 9788 | TEST_P(QuicConnectionTest, TlpAndWriteBlocked) { |
fayang | d301683 | 2019-08-08 07:24:45 -0700 | [diff] [blame] | 9789 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9790 | connection_.SetMaxTailLossProbes(1); |
| 9791 | |
| 9792 | QuicStreamId stream_id = 2; |
| 9793 | QuicPacketNumber last_data_packet; |
| 9794 | SendStreamDataToPeer(stream_id, "foo", 0, NO_FIN, &last_data_packet); |
| 9795 | SendStreamDataToPeer(4, "foo", 0, NO_FIN, &last_data_packet); |
| 9796 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9797 | |
| 9798 | // Writer gets blocked. |
| 9799 | writer_->SetWriteBlocked(); |
| 9800 | |
| 9801 | // Cancel stream 2. |
| 9802 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 9803 | EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); |
| 9804 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 9805 | |
fayang | e62e63c | 2019-12-04 07:16:25 -0800 | [diff] [blame] | 9806 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
fayang | d301683 | 2019-08-08 07:24:45 -0700 | [diff] [blame] | 9807 | // Retransmission timer fires in TLP mode. |
| 9808 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9809 | // Verify one packets is forced flushed when writer is blocked. |
| 9810 | EXPECT_EQ(1u, connection_.NumQueuedPackets()); |
| 9811 | } |
| 9812 | |
fayang | 67f8227 | 2019-08-14 16:08:45 -0700 | [diff] [blame] | 9813 | // Regresstion test for b/139375344. |
| 9814 | TEST_P(QuicConnectionTest, RtoForcesSendingPing) { |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 9815 | if (connection_.PtoEnabled()) { |
fayang | 67f8227 | 2019-08-14 16:08:45 -0700 | [diff] [blame] | 9816 | return; |
| 9817 | } |
| 9818 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 9819 | connection_.SetMaxTailLossProbes(2); |
| 9820 | EXPECT_EQ(0u, connection_.GetStats().tlp_count); |
| 9821 | EXPECT_EQ(0u, connection_.GetStats().rto_count); |
| 9822 | |
| 9823 | SendStreamDataToPeer(2, "foo", 0, NO_FIN, nullptr); |
| 9824 | QuicTime retransmission_time = |
| 9825 | connection_.GetRetransmissionAlarm()->deadline(); |
| 9826 | EXPECT_NE(QuicTime::Zero(), retransmission_time); |
| 9827 | // TLP fires. |
| 9828 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(2), _, _)); |
| 9829 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
| 9830 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9831 | EXPECT_EQ(1u, connection_.GetStats().tlp_count); |
| 9832 | EXPECT_EQ(0u, connection_.GetStats().rto_count); |
| 9833 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9834 | |
| 9835 | // Packet 1 gets acked. |
| 9836 | QuicAckFrame frame = InitAckFrame(1); |
| 9837 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _)); |
| 9838 | ProcessAckPacket(1, &frame); |
| 9839 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9840 | retransmission_time = connection_.GetRetransmissionAlarm()->deadline(); |
| 9841 | |
| 9842 | // RTO fires, verify a PING packet gets sent because there is no data to send. |
| 9843 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(3), _, _)); |
| 9844 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { SendPing(); })); |
| 9845 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
| 9846 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9847 | EXPECT_EQ(1u, connection_.GetStats().tlp_count); |
| 9848 | EXPECT_EQ(1u, connection_.GetStats().rto_count); |
| 9849 | EXPECT_EQ(1u, writer_->ping_frames().size()); |
| 9850 | } |
| 9851 | |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9852 | TEST_P(QuicConnectionTest, ProbeTimeout) { |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9853 | QuicConfig config; |
| 9854 | QuicTagVector connection_options; |
| 9855 | connection_options.push_back(k2PTO); |
| 9856 | config.SetConnectionOptionsToSend(connection_options); |
| 9857 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9858 | connection_.SetFromConfig(config); |
| 9859 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9860 | |
| 9861 | QuicStreamId stream_id = 2; |
| 9862 | QuicPacketNumber last_packet; |
| 9863 | SendStreamDataToPeer(stream_id, "foooooo", 0, NO_FIN, &last_packet); |
| 9864 | SendStreamDataToPeer(stream_id, "foooooo", 7, NO_FIN, &last_packet); |
| 9865 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9866 | |
| 9867 | // Reset stream. |
| 9868 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9869 | SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 3); |
| 9870 | |
| 9871 | // Fire the PTO and verify only the RST_STREAM is resent, not stream data. |
| 9872 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 9873 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9874 | EXPECT_EQ(0u, writer_->stream_frames().size()); |
| 9875 | EXPECT_EQ(1u, writer_->rst_stream_frames().size()); |
| 9876 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9877 | } |
| 9878 | |
fayang | 97ce41e | 2019-10-07 08:37:29 -0700 | [diff] [blame] | 9879 | TEST_P(QuicConnectionTest, CloseConnectionAfter6ClientPTOs) { |
fayang | 97ce41e | 2019-10-07 08:37:29 -0700 | [diff] [blame] | 9880 | QuicConfig config; |
| 9881 | QuicTagVector connection_options; |
| 9882 | connection_options.push_back(k1PTO); |
| 9883 | connection_options.push_back(k6PTO); |
| 9884 | config.SetConnectionOptionsToSend(connection_options); |
dschinazi | f7c6a91 | 2020-05-05 11:39:53 -0700 | [diff] [blame] | 9885 | QuicConfigPeer::SetNegotiated(&config, true); |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 9886 | if (connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 9887 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 9888 | &config, connection_.connection_id()); |
| 9889 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 9890 | &config, connection_.connection_id()); |
| 9891 | } |
fayang | 97ce41e | 2019-10-07 08:37:29 -0700 | [diff] [blame] | 9892 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9893 | connection_.SetFromConfig(config); |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 9894 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
fayang | aa4f3f2 | 2020-06-05 16:22:00 -0700 | [diff] [blame] | 9895 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 9896 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 9897 | } |
fayang | 97ce41e | 2019-10-07 08:37:29 -0700 | [diff] [blame] | 9898 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9899 | |
| 9900 | // Send stream data. |
| 9901 | SendStreamDataToPeer( |
| 9902 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 9903 | 0, FIN, nullptr); |
| 9904 | |
fayang | 97ce41e | 2019-10-07 08:37:29 -0700 | [diff] [blame] | 9905 | // Fire the retransmission alarm 5 times. |
| 9906 | for (int i = 0; i < 5; ++i) { |
ianswett | 825b48b | 2020-05-11 06:25:04 -0700 | [diff] [blame] | 9907 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
fayang | 97ce41e | 2019-10-07 08:37:29 -0700 | [diff] [blame] | 9908 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9909 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 9910 | EXPECT_TRUE(connection_.connected()); |
| 9911 | } |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 9912 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 9913 | connection_.PathDegradingTimeout(); |
fayang | 97ce41e | 2019-10-07 08:37:29 -0700 | [diff] [blame] | 9914 | |
| 9915 | EXPECT_EQ(0u, connection_.sent_packet_manager().GetConsecutiveTlpCount()); |
| 9916 | EXPECT_EQ(0u, connection_.sent_packet_manager().GetConsecutiveRtoCount()); |
| 9917 | EXPECT_EQ(5u, connection_.sent_packet_manager().GetConsecutivePtoCount()); |
| 9918 | // Closes connection on 6th PTO. |
ianswett | 825b48b | 2020-05-11 06:25:04 -0700 | [diff] [blame] | 9919 | // May send multiple connecction close packets with multiple PN spaces. |
| 9920 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
fayang | 97ce41e | 2019-10-07 08:37:29 -0700 | [diff] [blame] | 9921 | EXPECT_CALL(visitor_, |
| 9922 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 9923 | ASSERT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 9924 | connection_.GetBlackholeDetectorAlarm()->Fire(); |
fayang | 97ce41e | 2019-10-07 08:37:29 -0700 | [diff] [blame] | 9925 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 9926 | EXPECT_FALSE(connection_.connected()); |
| 9927 | TestConnectionCloseQuicErrorCode(QUIC_TOO_MANY_RTOS); |
| 9928 | } |
| 9929 | |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9930 | TEST_P(QuicConnectionTest, CloseConnectionAfter7ClientPTOs) { |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9931 | QuicConfig config; |
| 9932 | QuicTagVector connection_options; |
| 9933 | connection_options.push_back(k2PTO); |
| 9934 | connection_options.push_back(k7PTO); |
| 9935 | config.SetConnectionOptionsToSend(connection_options); |
dschinazi | f7c6a91 | 2020-05-05 11:39:53 -0700 | [diff] [blame] | 9936 | QuicConfigPeer::SetNegotiated(&config, true); |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 9937 | if (connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 9938 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 9939 | &config, connection_.connection_id()); |
| 9940 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 9941 | &config, connection_.connection_id()); |
| 9942 | } |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9943 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9944 | connection_.SetFromConfig(config); |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 9945 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
fayang | aa4f3f2 | 2020-06-05 16:22:00 -0700 | [diff] [blame] | 9946 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 9947 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 9948 | } |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9949 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 9950 | |
| 9951 | // Send stream data. |
| 9952 | SendStreamDataToPeer( |
| 9953 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 9954 | 0, FIN, nullptr); |
| 9955 | |
| 9956 | // Fire the retransmission alarm 6 times. |
| 9957 | for (int i = 0; i < 6; ++i) { |
| 9958 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 9959 | connection_.GetRetransmissionAlarm()->Fire(); |
| 9960 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 9961 | EXPECT_TRUE(connection_.connected()); |
| 9962 | } |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 9963 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 9964 | connection_.PathDegradingTimeout(); |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9965 | |
| 9966 | EXPECT_EQ(0u, connection_.sent_packet_manager().GetConsecutiveTlpCount()); |
| 9967 | EXPECT_EQ(0u, connection_.sent_packet_manager().GetConsecutiveRtoCount()); |
| 9968 | EXPECT_EQ(6u, connection_.sent_packet_manager().GetConsecutivePtoCount()); |
| 9969 | // Closes connection on 7th PTO. |
| 9970 | EXPECT_CALL(visitor_, |
| 9971 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 9972 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 9973 | ASSERT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 9974 | connection_.GetBlackholeDetectorAlarm()->Fire(); |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9975 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 9976 | EXPECT_FALSE(connection_.connected()); |
| 9977 | TestConnectionCloseQuicErrorCode(QUIC_TOO_MANY_RTOS); |
| 9978 | } |
| 9979 | |
| 9980 | TEST_P(QuicConnectionTest, CloseConnectionAfter8ClientPTOs) { |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9981 | QuicConfig config; |
| 9982 | QuicTagVector connection_options; |
| 9983 | connection_options.push_back(k2PTO); |
| 9984 | connection_options.push_back(k8PTO); |
dschinazi | f7c6a91 | 2020-05-05 11:39:53 -0700 | [diff] [blame] | 9985 | QuicConfigPeer::SetNegotiated(&config, true); |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 9986 | if (connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 9987 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 9988 | &config, connection_.connection_id()); |
| 9989 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 9990 | &config, connection_.connection_id()); |
| 9991 | } |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9992 | config.SetConnectionOptionsToSend(connection_options); |
| 9993 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 9994 | connection_.SetFromConfig(config); |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 9995 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
fayang | aa4f3f2 | 2020-06-05 16:22:00 -0700 | [diff] [blame] | 9996 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 9997 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 9998 | } |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 9999 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10000 | |
| 10001 | // Send stream data. |
| 10002 | SendStreamDataToPeer( |
| 10003 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 10004 | 0, FIN, nullptr); |
| 10005 | |
| 10006 | // Fire the retransmission alarm 7 times. |
| 10007 | for (int i = 0; i < 7; ++i) { |
| 10008 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); |
| 10009 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10010 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 10011 | EXPECT_TRUE(connection_.connected()); |
| 10012 | } |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 10013 | EXPECT_CALL(visitor_, OnPathDegrading()); |
| 10014 | connection_.PathDegradingTimeout(); |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 10015 | |
| 10016 | EXPECT_EQ(0u, connection_.sent_packet_manager().GetConsecutiveTlpCount()); |
| 10017 | EXPECT_EQ(0u, connection_.sent_packet_manager().GetConsecutiveRtoCount()); |
| 10018 | EXPECT_EQ(7u, connection_.sent_packet_manager().GetConsecutivePtoCount()); |
| 10019 | // Closes connection on 8th PTO. |
| 10020 | EXPECT_CALL(visitor_, |
| 10021 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)); |
rch | 39c88ab | 2019-10-16 19:24:40 -0700 | [diff] [blame] | 10022 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 10023 | ASSERT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 10024 | connection_.GetBlackholeDetectorAlarm()->Fire(); |
fayang | ce0a316 | 2019-08-15 09:05:36 -0700 | [diff] [blame] | 10025 | EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); |
| 10026 | EXPECT_FALSE(connection_.connected()); |
| 10027 | TestConnectionCloseQuicErrorCode(QUIC_TOO_MANY_RTOS); |
| 10028 | } |
| 10029 | |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 10030 | TEST_P(QuicConnectionTest, DeprecateHandshakeMode) { |
| 10031 | if (!connection_.version().SupportsAntiAmplificationLimit()) { |
| 10032 | return; |
| 10033 | } |
| 10034 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 10035 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10036 | |
| 10037 | // Send CHLO. |
| 10038 | connection_.SendCryptoStreamData(); |
| 10039 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10040 | |
| 10041 | EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _, _)); |
| 10042 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _, _)); |
| 10043 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 10044 | QuicAckFrame frame1 = InitAckFrame(1); |
| 10045 | // Received ACK for packet 1. |
| 10046 | ProcessFramePacketAtLevel(1, QuicFrame(&frame1), ENCRYPTION_INITIAL); |
| 10047 | |
| 10048 | // Verify retransmission alarm is still set because handshake is not |
| 10049 | // confirmed although there is nothing in flight. |
| 10050 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10051 | EXPECT_EQ(0u, connection_.GetStats().pto_count); |
| 10052 | EXPECT_EQ(0u, connection_.GetStats().crypto_retransmit_count); |
| 10053 | |
| 10054 | // PTO fires, verify a PING packet gets sent because there is no data to send. |
fayang | 5d01198 | 2020-05-13 14:14:38 -0700 | [diff] [blame] | 10055 | EXPECT_CALL(*send_algorithm_, |
| 10056 | OnPacketSent(_, _, |
| 10057 | GetQuicReloadableFlag(quic_default_on_pto) |
| 10058 | ? QuicPacketNumber(2) |
| 10059 | : QuicPacketNumber(3), |
| 10060 | _, _)); |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 10061 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { SendPing(); })); |
| 10062 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10063 | EXPECT_EQ(1u, connection_.GetStats().pto_count); |
fayang | af9903b | 2020-05-14 14:34:28 -0700 | [diff] [blame] | 10064 | EXPECT_EQ(1u, connection_.GetStats().crypto_retransmit_count); |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 10065 | EXPECT_EQ(1u, writer_->ping_frames().size()); |
| 10066 | } |
| 10067 | |
| 10068 | TEST_P(QuicConnectionTest, AntiAmplificationLimit) { |
| 10069 | if (!connection_.version().SupportsAntiAmplificationLimit()) { |
| 10070 | return; |
| 10071 | } |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 10072 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 10073 | |
| 10074 | set_perspective(Perspective::IS_SERVER); |
| 10075 | // Verify no data can be sent at the beginning because bytes received is 0. |
| 10076 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 10077 | connection_.SendCryptoDataWithString("foo", 0); |
fayang | 6a25841 | 2020-05-28 08:57:12 -0700 | [diff] [blame] | 10078 | if (GetQuicReloadableFlag(quic_move_amplification_limit)) { |
| 10079 | EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)); |
| 10080 | EXPECT_FALSE(connection_.CanWrite(NO_RETRANSMITTABLE_DATA)); |
| 10081 | } |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 10082 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10083 | |
| 10084 | // Receives packet 1. |
| 10085 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 10086 | |
| 10087 | const size_t anti_amplification_factor = |
fayang | 6a25841 | 2020-05-28 08:57:12 -0700 | [diff] [blame] | 10088 | connection_.anti_amplification_factor(); |
fayang | 5f13505 | 2019-08-22 17:59:40 -0700 | [diff] [blame] | 10089 | // Verify now packets can be sent. |
| 10090 | for (size_t i = 0; i < anti_amplification_factor; ++i) { |
| 10091 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 10092 | connection_.SendCryptoDataWithString("foo", i * 3); |
| 10093 | // Verify retransmission alarm is not set if throttled by anti-amplification |
| 10094 | // limit. |
| 10095 | EXPECT_EQ(i != anti_amplification_factor - 1, |
| 10096 | connection_.GetRetransmissionAlarm()->IsSet()); |
| 10097 | } |
| 10098 | // Verify server is throttled by anti-amplification limit. |
| 10099 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 10100 | connection_.SendCryptoDataWithString("foo", anti_amplification_factor * 3); |
| 10101 | |
| 10102 | // Receives packet 2. |
| 10103 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
| 10104 | // Verify more packets can be sent. |
| 10105 | for (size_t i = anti_amplification_factor; i < anti_amplification_factor * 2; |
| 10106 | ++i) { |
| 10107 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 10108 | connection_.SendCryptoDataWithString("foo", i * 3); |
| 10109 | } |
| 10110 | // Verify server is throttled by anti-amplification limit. |
| 10111 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 10112 | connection_.SendCryptoDataWithString("foo", |
| 10113 | 2 * anti_amplification_factor * 3); |
| 10114 | |
| 10115 | ProcessPacket(3); |
| 10116 | // Verify anti-amplification limit is gone after address validation. |
| 10117 | for (size_t i = 0; i < 100; ++i) { |
| 10118 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 10119 | connection_.SendStreamDataWithString(3, "first", i * 0, NO_FIN); |
| 10120 | } |
| 10121 | } |
| 10122 | |
fayang | 6a25841 | 2020-05-28 08:57:12 -0700 | [diff] [blame] | 10123 | TEST_P(QuicConnectionTest, AckPendingWithAmplificationLimited) { |
| 10124 | if (!connection_.version().SupportsAntiAmplificationLimit() || |
| 10125 | !GetQuicReloadableFlag(quic_move_amplification_limit)) { |
| 10126 | return; |
| 10127 | } |
| 10128 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 10129 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(AnyNumber()); |
| 10130 | set_perspective(Perspective::IS_SERVER); |
| 10131 | use_tagging_decrypter(); |
| 10132 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
| 10133 | std::make_unique<TaggingEncrypter>(0x01)); |
| 10134 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 10135 | // Receives packet 1. |
| 10136 | ProcessCryptoPacketAtLevel(1, ENCRYPTION_INITIAL); |
| 10137 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 10138 | std::make_unique<TaggingEncrypter>(0x02)); |
| 10139 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 10140 | EXPECT_TRUE(connection_.HasPendingAcks()); |
fayang | 6a25841 | 2020-05-28 08:57:12 -0700 | [diff] [blame] | 10141 | // Send response in different encryption level and cause amplification factor |
| 10142 | // throttled. |
| 10143 | size_t i = 0; |
| 10144 | while (connection_.CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
| 10145 | connection_.SendCryptoDataWithString(std::string(1024, 'a'), i * 1024, |
| 10146 | ENCRYPTION_HANDSHAKE); |
| 10147 | ++i; |
| 10148 | } |
| 10149 | // Verify ACK is still pending. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 10150 | EXPECT_TRUE(connection_.HasPendingAcks()); |
fayang | 6a25841 | 2020-05-28 08:57:12 -0700 | [diff] [blame] | 10151 | |
| 10152 | // Fire ACK alarm and verify ACK cannot be sent due to amplification factor. |
| 10153 | clock_.AdvanceTime(connection_.GetAckAlarm()->deadline() - clock_.Now()); |
| 10154 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 10155 | connection_.GetAckAlarm()->Fire(); |
| 10156 | // Verify ACK alarm is cancelled. |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 10157 | EXPECT_FALSE(connection_.HasPendingAcks()); |
fayang | 6a25841 | 2020-05-28 08:57:12 -0700 | [diff] [blame] | 10158 | |
| 10159 | // Receives packet 2 and verify ACK gets flushed. |
| 10160 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 10161 | ProcessCryptoPacketAtLevel(2, ENCRYPTION_INITIAL); |
| 10162 | EXPECT_FALSE(writer_->ack_frames().empty()); |
| 10163 | } |
| 10164 | |
fkastenholz | a366010 | 2019-08-28 05:19:24 -0700 | [diff] [blame] | 10165 | TEST_P(QuicConnectionTest, ConnectionCloseFrameType) { |
| 10166 | if (!VersionHasIetfQuicFrames(version().transport_version)) { |
| 10167 | // Test relevent only for IETF QUIC. |
| 10168 | return; |
| 10169 | } |
| 10170 | const QuicErrorCode kQuicErrorCode = IETF_QUIC_PROTOCOL_VIOLATION; |
| 10171 | // Use the (unknown) frame type of 9999 to avoid triggering any logic |
| 10172 | // which might be associated with the processing of a known frame type. |
| 10173 | const uint64_t kTransportCloseFrameType = 9999u; |
| 10174 | QuicFramerPeer::set_current_received_frame_type( |
| 10175 | QuicConnectionPeer::GetFramer(&connection_), kTransportCloseFrameType); |
| 10176 | // Do a transport connection close |
| 10177 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 10178 | connection_.CloseConnection( |
| 10179 | kQuicErrorCode, "Some random error message", |
| 10180 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 10181 | const std::vector<QuicConnectionCloseFrame>& connection_close_frames = |
| 10182 | writer_->connection_close_frames(); |
| 10183 | ASSERT_EQ(1u, connection_close_frames.size()); |
| 10184 | EXPECT_EQ(IETF_QUIC_TRANSPORT_CONNECTION_CLOSE, |
| 10185 | connection_close_frames[0].close_type); |
bnc | 77e77b8 | 2020-04-05 10:36:49 -0700 | [diff] [blame] | 10186 | EXPECT_EQ(kQuicErrorCode, connection_close_frames[0].quic_error_code); |
fkastenholz | a366010 | 2019-08-28 05:19:24 -0700 | [diff] [blame] | 10187 | EXPECT_EQ(kTransportCloseFrameType, |
| 10188 | connection_close_frames[0].transport_close_frame_type); |
| 10189 | } |
| 10190 | |
fayang | 0fcbf35 | 2019-08-30 11:15:58 -0700 | [diff] [blame] | 10191 | // Regression test for b/137401387 and b/138962304. |
| 10192 | TEST_P(QuicConnectionTest, RtoPacketAsTwo) { |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 10193 | if (connection_.PtoEnabled()) { |
fayang | 0fcbf35 | 2019-08-30 11:15:58 -0700 | [diff] [blame] | 10194 | return; |
| 10195 | } |
| 10196 | connection_.SetMaxTailLossProbes(1); |
| 10197 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 10198 | std::string stream_data(3000, 's'); |
| 10199 | // Send packets 1 - 66 and exhaust cwnd. |
| 10200 | for (size_t i = 0; i < 22; ++i) { |
| 10201 | // 3 packets for each stream, the first 2 are guaranteed to be full packets. |
| 10202 | SendStreamDataToPeer(i + 2, stream_data, 0, FIN, nullptr); |
| 10203 | } |
| 10204 | CongestionBlockWrites(); |
| 10205 | |
| 10206 | // Fires TLP. Please note, this tail loss probe has 1 byte less stream data |
| 10207 | // compared to packet 1 because packet number length increases. |
| 10208 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(67), _, _)); |
| 10209 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10210 | // Fires RTO. Please note, although packets 2 and 3 *should* be RTOed, but |
| 10211 | // packet 2 gets RTOed to two packets because packet number length increases. |
| 10212 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(68), _, _)); |
| 10213 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(69), _, _)); |
| 10214 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10215 | |
| 10216 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 10217 | // Resets all streams except 2 and ack packets 1 and 2. Now, packet 3 is the |
| 10218 | // only one containing retransmittable frames. |
| 10219 | for (size_t i = 1; i < 22; ++i) { |
| 10220 | notifier_.OnStreamReset(i + 2, QUIC_STREAM_CANCELLED); |
| 10221 | } |
| 10222 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _)); |
| 10223 | QuicAckFrame frame = |
| 10224 | InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(3)}}); |
| 10225 | ProcessAckPacket(1, &frame); |
| 10226 | CongestionUnblockWrites(); |
| 10227 | |
| 10228 | // Fires TLP, verify a PING gets sent because packet 3 is marked |
| 10229 | // RTO_RETRANSMITTED. |
| 10230 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(70), _, _)); |
| 10231 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { SendPing(); })); |
| 10232 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10233 | } |
| 10234 | |
fayang | 4c1c236 | 2019-09-13 07:20:01 -0700 | [diff] [blame] | 10235 | TEST_P(QuicConnectionTest, PtoSkipsPacketNumber) { |
fayang | 4c1c236 | 2019-09-13 07:20:01 -0700 | [diff] [blame] | 10236 | QuicConfig config; |
| 10237 | QuicTagVector connection_options; |
| 10238 | connection_options.push_back(k1PTO); |
| 10239 | connection_options.push_back(kPTOS); |
| 10240 | config.SetConnectionOptionsToSend(connection_options); |
| 10241 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10242 | connection_.SetFromConfig(config); |
| 10243 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10244 | |
| 10245 | QuicStreamId stream_id = 2; |
| 10246 | QuicPacketNumber last_packet; |
| 10247 | SendStreamDataToPeer(stream_id, "foooooo", 0, NO_FIN, &last_packet); |
| 10248 | SendStreamDataToPeer(stream_id, "foooooo", 7, NO_FIN, &last_packet); |
| 10249 | EXPECT_EQ(QuicPacketNumber(2), last_packet); |
| 10250 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10251 | |
| 10252 | // Fire PTO and verify the PTO retransmission skips one packet number. |
| 10253 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 10254 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10255 | EXPECT_EQ(1u, writer_->stream_frames().size()); |
| 10256 | EXPECT_EQ(QuicPacketNumber(4), writer_->last_packet_header().packet_number); |
| 10257 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10258 | } |
| 10259 | |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 10260 | TEST_P(QuicConnectionTest, SendCoalescedPackets) { |
| 10261 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 10262 | return; |
| 10263 | } |
fayang | 6100ab7 | 2020-03-18 13:16:25 -0700 | [diff] [blame] | 10264 | MockQuicConnectionDebugVisitor debug_visitor; |
| 10265 | connection_.set_debug_visitor(&debug_visitor); |
| 10266 | EXPECT_CALL(debug_visitor, OnPacketSent(_, _, _)).Times(3); |
| 10267 | EXPECT_CALL(debug_visitor, OnCoalescedPacketSent(_, _)).Times(1); |
fayang | 44ae4e9 | 2020-04-28 13:09:42 -0700 | [diff] [blame] | 10268 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 10269 | { |
| 10270 | QuicConnection::ScopedPacketFlusher flusher(&connection_); |
| 10271 | use_tagging_decrypter(); |
| 10272 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
| 10273 | std::make_unique<TaggingEncrypter>(0x01)); |
| 10274 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
| 10275 | connection_.SendCryptoDataWithString("foo", 0); |
| 10276 | // Verify this packet is on hold. |
| 10277 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 10278 | |
| 10279 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 10280 | std::make_unique<TaggingEncrypter>(0x02)); |
| 10281 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 10282 | connection_.SendCryptoDataWithString("bar", 3); |
| 10283 | EXPECT_EQ(0u, writer_->packets_write_attempts()); |
| 10284 | |
| 10285 | connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 10286 | std::make_unique<TaggingEncrypter>(0x03)); |
| 10287 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 10288 | SendStreamDataToPeer(2, "baz", 3, NO_FIN, nullptr); |
| 10289 | } |
| 10290 | // Verify all 3 packets are coalesced in the same UDP datagram. |
| 10291 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 10292 | EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); |
| 10293 | // Verify the packet is padded to full. |
| 10294 | EXPECT_EQ(connection_.max_packet_length(), writer_->last_packet_size()); |
| 10295 | |
| 10296 | // Verify packet process. |
| 10297 | EXPECT_EQ(1u, writer_->crypto_frames().size()); |
| 10298 | EXPECT_EQ(0u, writer_->stream_frames().size()); |
| 10299 | // Verify there is coalesced packet. |
| 10300 | EXPECT_NE(nullptr, writer_->coalesced_packet()); |
| 10301 | } |
| 10302 | |
fayang | 0106294 | 2020-01-22 07:23:23 -0800 | [diff] [blame] | 10303 | TEST_P(QuicConnectionTest, ClientReceivedHandshakeDone) { |
| 10304 | if (!connection_.version().HasHandshakeDone()) { |
| 10305 | return; |
| 10306 | } |
| 10307 | EXPECT_CALL(visitor_, OnHandshakeDoneReceived()); |
| 10308 | QuicFrames frames; |
| 10309 | frames.push_back(QuicFrame(QuicHandshakeDoneFrame())); |
| 10310 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 10311 | ProcessFramesPacketAtLevel(1, frames, ENCRYPTION_FORWARD_SECURE); |
| 10312 | } |
| 10313 | |
| 10314 | TEST_P(QuicConnectionTest, ServerReceivedHandshakeDone) { |
| 10315 | if (!connection_.version().HasHandshakeDone()) { |
| 10316 | return; |
| 10317 | } |
| 10318 | set_perspective(Perspective::IS_SERVER); |
| 10319 | EXPECT_CALL(visitor_, OnHandshakeDoneReceived()).Times(0); |
| 10320 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 10321 | .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame)); |
| 10322 | QuicFrames frames; |
| 10323 | frames.push_back(QuicFrame(QuicHandshakeDoneFrame())); |
| 10324 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 10325 | ProcessFramesPacketAtLevel(1, frames, ENCRYPTION_FORWARD_SECURE); |
| 10326 | EXPECT_EQ(1, connection_close_frame_count_); |
| 10327 | EXPECT_THAT(saved_connection_close_frame_.quic_error_code, |
| 10328 | IsError(IETF_QUIC_PROTOCOL_VIOLATION)); |
| 10329 | } |
| 10330 | |
fayang | 18ff23b | 2020-01-28 09:19:00 -0800 | [diff] [blame] | 10331 | TEST_P(QuicConnectionTest, MultiplePacketNumberSpacePto) { |
| 10332 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10333 | return; |
| 10334 | } |
| 10335 | use_tagging_decrypter(); |
| 10336 | // Send handshake packet. |
| 10337 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 10338 | std::make_unique<TaggingEncrypter>(0x02)); |
| 10339 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
fayang | 44ae4e9 | 2020-04-28 13:09:42 -0700 | [diff] [blame] | 10340 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
fayang | 18ff23b | 2020-01-28 09:19:00 -0800 | [diff] [blame] | 10341 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
| 10342 | EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet()); |
| 10343 | |
| 10344 | // Send application data. |
| 10345 | connection_.SendApplicationDataAtLevel(ENCRYPTION_FORWARD_SECURE, 5, "data", |
| 10346 | 0, NO_FIN); |
| 10347 | EXPECT_EQ(0x01010101u, writer_->final_bytes_of_last_packet()); |
| 10348 | QuicTime retransmission_time = |
| 10349 | connection_.GetRetransmissionAlarm()->deadline(); |
| 10350 | EXPECT_NE(QuicTime::Zero(), retransmission_time); |
| 10351 | |
| 10352 | // Retransmit handshake data. |
| 10353 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
fayang | 5d01198 | 2020-05-13 14:14:38 -0700 | [diff] [blame] | 10354 | EXPECT_CALL(*send_algorithm_, |
| 10355 | OnPacketSent(_, _, |
| 10356 | GetQuicReloadableFlag(quic_default_on_pto) |
| 10357 | ? QuicPacketNumber(3) |
| 10358 | : QuicPacketNumber(4), |
| 10359 | _, _)); |
fayang | 44ae4e9 | 2020-04-28 13:09:42 -0700 | [diff] [blame] | 10360 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
fayang | 18ff23b | 2020-01-28 09:19:00 -0800 | [diff] [blame] | 10361 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10362 | EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet()); |
| 10363 | |
| 10364 | // Send application data. |
| 10365 | connection_.SendApplicationDataAtLevel(ENCRYPTION_FORWARD_SECURE, 5, "data", |
| 10366 | 4, NO_FIN); |
| 10367 | EXPECT_EQ(0x01010101u, writer_->final_bytes_of_last_packet()); |
| 10368 | retransmission_time = connection_.GetRetransmissionAlarm()->deadline(); |
| 10369 | EXPECT_NE(QuicTime::Zero(), retransmission_time); |
| 10370 | |
| 10371 | // Retransmit handshake data again. |
| 10372 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
fayang | 5d01198 | 2020-05-13 14:14:38 -0700 | [diff] [blame] | 10373 | EXPECT_CALL(*send_algorithm_, |
| 10374 | OnPacketSent(_, _, |
| 10375 | GetQuicReloadableFlag(quic_default_on_pto) |
| 10376 | ? QuicPacketNumber(5) |
| 10377 | : QuicPacketNumber(7), |
| 10378 | _, _)); |
fayang | 44ae4e9 | 2020-04-28 13:09:42 -0700 | [diff] [blame] | 10379 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
fayang | 18ff23b | 2020-01-28 09:19:00 -0800 | [diff] [blame] | 10380 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10381 | EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet()); |
| 10382 | |
| 10383 | // Discard handshake key. |
| 10384 | connection_.OnHandshakeComplete(); |
| 10385 | retransmission_time = connection_.GetRetransmissionAlarm()->deadline(); |
| 10386 | EXPECT_NE(QuicTime::Zero(), retransmission_time); |
| 10387 | |
| 10388 | // Retransmit application data. |
| 10389 | clock_.AdvanceTime(retransmission_time - clock_.Now()); |
fayang | 5d01198 | 2020-05-13 14:14:38 -0700 | [diff] [blame] | 10390 | EXPECT_CALL(*send_algorithm_, |
| 10391 | OnPacketSent(_, _, |
| 10392 | GetQuicReloadableFlag(quic_default_on_pto) |
| 10393 | ? QuicPacketNumber(6) |
| 10394 | : QuicPacketNumber(9), |
| 10395 | _, _)); |
fayang | 18ff23b | 2020-01-28 09:19:00 -0800 | [diff] [blame] | 10396 | connection_.GetRetransmissionAlarm()->Fire(); |
| 10397 | EXPECT_EQ(0x01010101u, writer_->final_bytes_of_last_packet()); |
| 10398 | } |
| 10399 | |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10400 | void QuicConnectionTest::TestClientRetryHandling( |
| 10401 | bool invalid_retry_tag, |
| 10402 | bool missing_original_id_in_config, |
| 10403 | bool wrong_original_id_in_config, |
| 10404 | bool missing_retry_id_in_config, |
| 10405 | bool wrong_retry_id_in_config) { |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10406 | if (invalid_retry_tag) { |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10407 | ASSERT_FALSE(missing_original_id_in_config); |
| 10408 | ASSERT_FALSE(wrong_original_id_in_config); |
| 10409 | ASSERT_FALSE(missing_retry_id_in_config); |
| 10410 | ASSERT_FALSE(wrong_retry_id_in_config); |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10411 | } else { |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10412 | ASSERT_FALSE(missing_original_id_in_config && wrong_original_id_in_config); |
| 10413 | ASSERT_FALSE(missing_retry_id_in_config && wrong_retry_id_in_config); |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10414 | } |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 10415 | if (!version().HasRetryIntegrityTag()) { |
| 10416 | return; |
| 10417 | } |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10418 | |
| 10419 | // These values come from draft-ietf-quic-tls Appendix A.4. |
| 10420 | char retry_packet25[] = { |
| 10421 | 0xff, 0xff, 0x00, 0x00, 0x19, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, |
| 10422 | 0x42, 0x62, 0xb5, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x1e, 0x5e, 0xc5, 0xb0, |
| 10423 | 0x14, 0xcb, 0xb1, 0xf0, 0xfd, 0x93, 0xdf, 0x40, 0x48, 0xc4, 0x46, 0xa6}; |
| 10424 | char retry_packet27[] = { |
| 10425 | 0xff, 0xff, 0x00, 0x00, 0x1b, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, |
| 10426 | 0x42, 0x62, 0xb5, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0xa5, 0x23, 0xcb, 0x5b, |
| 10427 | 0xa5, 0x24, 0x69, 0x5f, 0x65, 0x69, 0xf2, 0x93, 0xa1, 0x35, 0x9d, 0x8e}; |
dschinazi | 2c78aac | 2020-05-21 17:21:58 -0700 | [diff] [blame] | 10428 | char retry_packet28[] = { |
| 10429 | 0xff, 0xff, 0x00, 0x00, 0x1c, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, |
| 10430 | 0x42, 0x62, 0xb5, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0xf7, 0x1a, 0x5f, 0x12, |
| 10431 | 0xaf, 0xe3, 0xec, 0xf8, 0x00, 0x1a, 0x92, 0x0e, 0x6f, 0xdf, 0x1d, 0x63}; |
dschinazi | b3fed9e | 2020-06-11 11:59:33 -0700 | [diff] [blame] | 10432 | char retry_packet29[] = { |
| 10433 | 0xff, 0xff, 0x00, 0x00, 0x1d, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, |
| 10434 | 0x42, 0x62, 0xb5, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0xd1, 0x69, 0x26, 0xd8, |
| 10435 | 0x1f, 0x6f, 0x9c, 0xa2, 0x95, 0x3a, 0x8a, 0xa4, 0x57, 0x5e, 0x1e, 0x49}; |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10436 | |
| 10437 | char* retry_packet; |
| 10438 | size_t retry_packet_length; |
dschinazi | b3fed9e | 2020-06-11 11:59:33 -0700 | [diff] [blame] | 10439 | if (version() == ParsedQuicVersion::Draft29()) { |
| 10440 | retry_packet = retry_packet29; |
| 10441 | retry_packet_length = QUICHE_ARRAYSIZE(retry_packet29); |
| 10442 | } else if (version() == ParsedQuicVersion::Draft28()) { |
dschinazi | 2c78aac | 2020-05-21 17:21:58 -0700 | [diff] [blame] | 10443 | retry_packet = retry_packet28; |
| 10444 | retry_packet_length = QUICHE_ARRAYSIZE(retry_packet28); |
dschinazi | b3fed9e | 2020-06-11 11:59:33 -0700 | [diff] [blame] | 10445 | } else if (version() == ParsedQuicVersion::Draft27()) { |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10446 | retry_packet = retry_packet27; |
| 10447 | retry_packet_length = QUICHE_ARRAYSIZE(retry_packet27); |
dschinazi | b3fed9e | 2020-06-11 11:59:33 -0700 | [diff] [blame] | 10448 | } else if (version() == ParsedQuicVersion::Draft25()) { |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10449 | retry_packet = retry_packet25; |
| 10450 | retry_packet_length = QUICHE_ARRAYSIZE(retry_packet25); |
| 10451 | } else { |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 10452 | // TODO(dschinazi) generate retry packets for all versions once we have |
| 10453 | // server-side support for generating these programmatically. |
| 10454 | return; |
| 10455 | } |
| 10456 | |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 10457 | char original_connection_id_bytes[] = {0x83, 0x94, 0xc8, 0xf0, |
| 10458 | 0x3e, 0x51, 0x57, 0x08}; |
| 10459 | char new_connection_id_bytes[] = {0xf0, 0x67, 0xa5, 0x50, |
| 10460 | 0x2a, 0x42, 0x62, 0xb5}; |
| 10461 | char retry_token_bytes[] = {0x74, 0x6f, 0x6b, 0x65, 0x6e}; |
| 10462 | |
| 10463 | QuicConnectionId original_connection_id( |
| 10464 | original_connection_id_bytes, |
| 10465 | QUICHE_ARRAYSIZE(original_connection_id_bytes)); |
| 10466 | QuicConnectionId new_connection_id(new_connection_id_bytes, |
| 10467 | QUICHE_ARRAYSIZE(new_connection_id_bytes)); |
| 10468 | |
| 10469 | std::string retry_token(retry_token_bytes, |
| 10470 | QUICHE_ARRAYSIZE(retry_token_bytes)); |
| 10471 | |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10472 | if (invalid_retry_tag) { |
| 10473 | // Flip the last bit of the retry packet to prevent the integrity tag |
| 10474 | // from validating correctly. |
| 10475 | retry_packet[retry_packet_length - 1] ^= 1; |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 10476 | } |
| 10477 | |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10478 | QuicConnectionId config_original_connection_id = original_connection_id; |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10479 | if (wrong_original_id_in_config) { |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10480 | // Flip the first bit of the connection ID. |
| 10481 | ASSERT_FALSE(config_original_connection_id.IsEmpty()); |
| 10482 | config_original_connection_id.mutable_data()[0] ^= 0x80; |
| 10483 | } |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10484 | QuicConnectionId config_retry_source_connection_id = new_connection_id; |
| 10485 | if (wrong_retry_id_in_config) { |
| 10486 | // Flip the first bit of the connection ID. |
| 10487 | ASSERT_FALSE(config_retry_source_connection_id.IsEmpty()); |
| 10488 | config_retry_source_connection_id.mutable_data()[0] ^= 0x80; |
| 10489 | } |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 10490 | |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10491 | // Make sure the connection uses the connection ID from the test vectors, |
| 10492 | QuicConnectionPeer::SetServerConnectionId(&connection_, |
| 10493 | original_connection_id); |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 10494 | |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10495 | // Process the RETRY packet. |
| 10496 | connection_.ProcessUdpPacket( |
| 10497 | kSelfAddress, kPeerAddress, |
| 10498 | QuicReceivedPacket(retry_packet, retry_packet_length, clock_.Now())); |
| 10499 | |
| 10500 | if (invalid_retry_tag) { |
| 10501 | // Make sure we refuse to process a RETRY with invalid tag. |
| 10502 | EXPECT_FALSE(connection_.GetStats().retry_packet_processed); |
| 10503 | EXPECT_EQ(connection_.connection_id(), original_connection_id); |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 10504 | EXPECT_TRUE(QuicPacketCreatorPeer::GetRetryToken( |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10505 | QuicConnectionPeer::GetPacketCreator(&connection_)) |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 10506 | .empty()); |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10507 | return; |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 10508 | } |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10509 | |
| 10510 | // Make sure we correctly parsed the RETRY. |
| 10511 | EXPECT_TRUE(connection_.GetStats().retry_packet_processed); |
| 10512 | EXPECT_EQ(connection_.connection_id(), new_connection_id); |
| 10513 | EXPECT_EQ(QuicPacketCreatorPeer::GetRetryToken( |
| 10514 | QuicConnectionPeer::GetPacketCreator(&connection_)), |
| 10515 | retry_token); |
| 10516 | // Make sure our fake framer has the new post-retry INITIAL keys. |
| 10517 | writer_->framer()->framer()->SetInitialObfuscators(new_connection_id); |
| 10518 | |
| 10519 | // Test validating the original_connection_id from the config. |
| 10520 | QuicConfig received_config; |
| 10521 | QuicConfigPeer::SetNegotiated(&received_config, true); |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10522 | if (connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 10523 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 10524 | &received_config, connection_.connection_id()); |
| 10525 | if (!missing_retry_id_in_config) { |
| 10526 | QuicConfigPeer::SetReceivedRetrySourceConnectionId( |
| 10527 | &received_config, config_retry_source_connection_id); |
| 10528 | } |
| 10529 | } |
| 10530 | if (!missing_original_id_in_config) { |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10531 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 10532 | &received_config, config_original_connection_id); |
| 10533 | } |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10534 | |
| 10535 | if (missing_original_id_in_config || wrong_original_id_in_config || |
| 10536 | missing_retry_id_in_config || wrong_retry_id_in_config) { |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10537 | EXPECT_CALL(visitor_, |
| 10538 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 10539 | .Times(1); |
| 10540 | } else { |
| 10541 | EXPECT_CALL(visitor_, |
| 10542 | OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 10543 | .Times(0); |
| 10544 | } |
| 10545 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); |
| 10546 | connection_.SetFromConfig(received_config); |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10547 | if (missing_original_id_in_config || wrong_original_id_in_config || |
| 10548 | missing_retry_id_in_config || wrong_retry_id_in_config) { |
| 10549 | ASSERT_FALSE(connection_.connected()); |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10550 | TestConnectionCloseQuicErrorCode(IETF_QUIC_PROTOCOL_VIOLATION); |
| 10551 | } else { |
| 10552 | EXPECT_TRUE(connection_.connected()); |
| 10553 | } |
| 10554 | } |
| 10555 | |
| 10556 | TEST_P(QuicConnectionTest, ClientParsesRetry) { |
| 10557 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10558 | /*missing_original_id_in_config=*/false, |
| 10559 | /*wrong_original_id_in_config=*/false, |
| 10560 | /*missing_retry_id_in_config=*/false, |
| 10561 | /*wrong_retry_id_in_config=*/false); |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10562 | } |
| 10563 | |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10564 | TEST_P(QuicConnectionTest, ClientParsesRetryInvalidTag) { |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10565 | TestClientRetryHandling(/*invalid_retry_tag=*/true, |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10566 | /*missing_original_id_in_config=*/false, |
| 10567 | /*wrong_original_id_in_config=*/false, |
| 10568 | /*missing_retry_id_in_config=*/false, |
| 10569 | /*wrong_retry_id_in_config=*/false); |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10570 | } |
| 10571 | |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10572 | TEST_P(QuicConnectionTest, ClientParsesRetryMissingOriginalId) { |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10573 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10574 | /*missing_original_id_in_config=*/true, |
| 10575 | /*wrong_original_id_in_config=*/false, |
| 10576 | /*missing_retry_id_in_config=*/false, |
| 10577 | /*wrong_retry_id_in_config=*/false); |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10578 | } |
| 10579 | |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10580 | TEST_P(QuicConnectionTest, ClientParsesRetryWrongOriginalId) { |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10581 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10582 | /*missing_original_id_in_config=*/false, |
| 10583 | /*wrong_original_id_in_config=*/true, |
| 10584 | /*missing_retry_id_in_config=*/false, |
| 10585 | /*wrong_retry_id_in_config=*/false); |
| 10586 | } |
| 10587 | |
| 10588 | TEST_P(QuicConnectionTest, ClientParsesRetryMissingRetryId) { |
| 10589 | if (!connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 10590 | // Versions that do not authenticate connection IDs never send the |
| 10591 | // retry_source_connection_id transport parameter. |
| 10592 | return; |
| 10593 | } |
| 10594 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 10595 | /*missing_original_id_in_config=*/false, |
| 10596 | /*wrong_original_id_in_config=*/false, |
| 10597 | /*missing_retry_id_in_config=*/true, |
| 10598 | /*wrong_retry_id_in_config=*/false); |
| 10599 | } |
| 10600 | |
| 10601 | TEST_P(QuicConnectionTest, ClientParsesRetryWrongRetryId) { |
| 10602 | if (!connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 10603 | // Versions that do not authenticate connection IDs never send the |
| 10604 | // retry_source_connection_id transport parameter. |
| 10605 | return; |
| 10606 | } |
| 10607 | TestClientRetryHandling(/*invalid_retry_tag=*/false, |
| 10608 | /*missing_original_id_in_config=*/false, |
| 10609 | /*wrong_original_id_in_config=*/false, |
| 10610 | /*missing_retry_id_in_config=*/false, |
| 10611 | /*wrong_retry_id_in_config=*/true); |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10612 | } |
| 10613 | |
| 10614 | TEST_P(QuicConnectionTest, ClientReceivesOriginalConnectionIdWithoutRetry) { |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10615 | if (!connection_.version().UsesTls()) { |
| 10616 | // QUIC+TLS is required to transmit connection ID transport parameters. |
| 10617 | return; |
| 10618 | } |
| 10619 | if (connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 10620 | // Versions that authenticate connection IDs always send the |
| 10621 | // original_destination_connection_id transport parameter. |
| 10622 | return; |
| 10623 | } |
| 10624 | // Make sure that receiving the original_destination_connection_id transport |
| 10625 | // parameter fails the handshake when no RETRY packet was received before it. |
dschinazi | 39e5e55 | 2020-05-06 13:55:24 -0700 | [diff] [blame] | 10626 | QuicConfig received_config; |
| 10627 | QuicConfigPeer::SetNegotiated(&received_config, true); |
| 10628 | QuicConfigPeer::SetReceivedOriginalConnectionId(&received_config, |
| 10629 | TestConnectionId(0x12345)); |
| 10630 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); |
| 10631 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 10632 | .Times(1); |
| 10633 | connection_.SetFromConfig(received_config); |
| 10634 | EXPECT_FALSE(connection_.connected()); |
| 10635 | TestConnectionCloseQuicErrorCode(IETF_QUIC_PROTOCOL_VIOLATION); |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 10636 | } |
| 10637 | |
dschinazi | e7c38a5 | 2020-05-29 15:25:45 -0700 | [diff] [blame] | 10638 | TEST_P(QuicConnectionTest, ClientReceivesRetrySourceConnectionIdWithoutRetry) { |
| 10639 | if (!connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 10640 | // Versions that do not authenticate connection IDs never send the |
| 10641 | // retry_source_connection_id transport parameter. |
| 10642 | return; |
| 10643 | } |
| 10644 | // Make sure that receiving the retry_source_connection_id transport parameter |
| 10645 | // fails the handshake when no RETRY packet was received before it. |
| 10646 | QuicConfig received_config; |
| 10647 | QuicConfigPeer::SetNegotiated(&received_config, true); |
| 10648 | QuicConfigPeer::SetReceivedRetrySourceConnectionId(&received_config, |
| 10649 | TestConnectionId(0x12345)); |
| 10650 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); |
| 10651 | EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF)) |
| 10652 | .Times(1); |
| 10653 | connection_.SetFromConfig(received_config); |
| 10654 | EXPECT_FALSE(connection_.connected()); |
| 10655 | TestConnectionCloseQuicErrorCode(IETF_QUIC_PROTOCOL_VIOLATION); |
| 10656 | } |
| 10657 | |
fayang | 0e3035e | 2020-02-03 13:30:36 -0800 | [diff] [blame] | 10658 | // Regression test for http://crbug/1047977 |
| 10659 | TEST_P(QuicConnectionTest, MaxStreamsFrameCausesConnectionClose) { |
| 10660 | if (!VersionHasIetfQuicFrames(connection_.transport_version())) { |
| 10661 | return; |
| 10662 | } |
| 10663 | // Received frame causes connection close. |
| 10664 | EXPECT_CALL(visitor_, OnMaxStreamsFrame(_)) |
| 10665 | .WillOnce(InvokeWithoutArgs([this]() { |
| 10666 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 10667 | connection_.CloseConnection( |
| 10668 | QUIC_TOO_MANY_BUFFERED_CONTROL_FRAMES, "error", |
| 10669 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 10670 | return true; |
| 10671 | })); |
| 10672 | QuicFrames frames; |
| 10673 | frames.push_back(QuicFrame(QuicMaxStreamsFrame())); |
| 10674 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 10675 | ProcessFramesPacketAtLevel(1, frames, ENCRYPTION_FORWARD_SECURE); |
| 10676 | } |
| 10677 | |
| 10678 | TEST_P(QuicConnectionTest, StreamsBlockedFrameCausesConnectionClose) { |
| 10679 | if (!VersionHasIetfQuicFrames(connection_.transport_version())) { |
| 10680 | return; |
| 10681 | } |
| 10682 | // Received frame causes connection close. |
| 10683 | EXPECT_CALL(visitor_, OnStreamsBlockedFrame(_)) |
| 10684 | .WillOnce(InvokeWithoutArgs([this]() { |
| 10685 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 10686 | connection_.CloseConnection( |
| 10687 | QUIC_TOO_MANY_BUFFERED_CONTROL_FRAMES, "error", |
| 10688 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 10689 | return true; |
| 10690 | })); |
| 10691 | QuicFrames frames; |
| 10692 | frames.push_back( |
| 10693 | QuicFrame(QuicStreamsBlockedFrame(kInvalidControlFrameId, 10, false))); |
| 10694 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 10695 | ProcessFramesPacketAtLevel(1, frames, ENCRYPTION_FORWARD_SECURE); |
| 10696 | } |
| 10697 | |
fayang | 79400d5 | 2020-02-13 10:13:05 -0800 | [diff] [blame] | 10698 | TEST_P(QuicConnectionTest, |
| 10699 | BundleAckWithConnectionCloseMultiplePacketNumberSpace) { |
| 10700 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10701 | return; |
| 10702 | } |
| 10703 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 10704 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 10705 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 10706 | // Receives packet 1000 in initial data. |
| 10707 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
| 10708 | // Receives packet 2000 in application data. |
| 10709 | ProcessDataPacketAtLevel(2000, false, ENCRYPTION_FORWARD_SECURE); |
| 10710 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)); |
| 10711 | const QuicErrorCode kQuicErrorCode = QUIC_INTERNAL_ERROR; |
| 10712 | connection_.CloseConnection( |
| 10713 | kQuicErrorCode, "Some random error message", |
| 10714 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 10715 | |
| 10716 | EXPECT_EQ(2u, QuicConnectionPeer::GetNumEncryptionLevels(&connection_)); |
| 10717 | |
| 10718 | TestConnectionCloseQuicErrorCode(kQuicErrorCode); |
| 10719 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 10720 | // Verify ack is bundled. |
| 10721 | EXPECT_EQ(1u, writer_->ack_frames().size()); |
| 10722 | |
| 10723 | if (!connection_.version().CanSendCoalescedPackets()) { |
| 10724 | // Each connection close packet should be sent in distinct UDP packets. |
| 10725 | EXPECT_EQ(QuicConnectionPeer::GetNumEncryptionLevels(&connection_), |
| 10726 | writer_->connection_close_packets()); |
| 10727 | EXPECT_EQ(QuicConnectionPeer::GetNumEncryptionLevels(&connection_), |
| 10728 | writer_->packets_write_attempts()); |
| 10729 | return; |
| 10730 | } |
| 10731 | |
| 10732 | // A single UDP packet should be sent with multiple connection close packets |
| 10733 | // coalesced together. |
| 10734 | EXPECT_EQ(1u, writer_->packets_write_attempts()); |
| 10735 | |
| 10736 | // Only the first packet has been processed yet. |
| 10737 | EXPECT_EQ(1u, writer_->connection_close_packets()); |
| 10738 | |
| 10739 | // ProcessPacket resets the visitor and frees the coalesced packet. |
| 10740 | ASSERT_TRUE(writer_->coalesced_packet() != nullptr); |
| 10741 | auto packet = writer_->coalesced_packet()->Clone(); |
| 10742 | writer_->framer()->ProcessPacket(*packet); |
| 10743 | EXPECT_EQ(1u, writer_->connection_close_packets()); |
| 10744 | EXPECT_EQ(1u, writer_->connection_close_frames().size()); |
| 10745 | // Verify ack is bundled. |
| 10746 | EXPECT_EQ(1u, writer_->ack_frames().size()); |
| 10747 | ASSERT_TRUE(writer_->coalesced_packet() == nullptr); |
| 10748 | } |
| 10749 | |
fayang | 61453cb | 2020-03-11 11:32:26 -0700 | [diff] [blame] | 10750 | // Regression test for b/151220135. |
| 10751 | TEST_P(QuicConnectionTest, SendPingWhenSkipPacketNumberForPto) { |
| 10752 | if (!VersionSupportsMessageFrames(connection_.transport_version())) { |
| 10753 | return; |
| 10754 | } |
| 10755 | QuicConfig config; |
| 10756 | QuicTagVector connection_options; |
| 10757 | connection_options.push_back(kPTOS); |
| 10758 | connection_options.push_back(k1PTO); |
| 10759 | config.SetConnectionOptionsToSend(connection_options); |
dschinazi | ed459c0 | 2020-05-07 16:12:23 -0700 | [diff] [blame] | 10760 | if (connection_.version().UsesTls()) { |
| 10761 | QuicConfigPeer::SetReceivedMaxDatagramFrameSize( |
| 10762 | &config, kMaxAcceptedDatagramFrameSize); |
| 10763 | } |
fayang | 61453cb | 2020-03-11 11:32:26 -0700 | [diff] [blame] | 10764 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10765 | connection_.SetFromConfig(config); |
| 10766 | EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10767 | |
| 10768 | EXPECT_EQ(MESSAGE_STATUS_SUCCESS, SendMessage("message")); |
| 10769 | EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); |
| 10770 | |
fayang | 80f9cc6 | 2020-04-22 08:08:20 -0700 | [diff] [blame] | 10771 | // PTO fires, verify a PING packet gets sent because there is no data to |
| 10772 | // send. |
| 10773 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(3), _, _)); |
| 10774 | EXPECT_CALL(visitor_, SendPing()).WillOnce(Invoke([this]() { SendPing(); })); |
fayang | 61453cb | 2020-03-11 11:32:26 -0700 | [diff] [blame] | 10775 | connection_.GetRetransmissionAlarm()->Fire(); |
fayang | 80f9cc6 | 2020-04-22 08:08:20 -0700 | [diff] [blame] | 10776 | EXPECT_EQ(1u, connection_.GetStats().pto_count); |
| 10777 | EXPECT_EQ(0u, connection_.GetStats().crypto_retransmit_count); |
| 10778 | EXPECT_EQ(1u, writer_->ping_frames().size()); |
fayang | 61453cb | 2020-03-11 11:32:26 -0700 | [diff] [blame] | 10779 | } |
| 10780 | |
fayang | cc210e7 | 2020-05-05 14:41:34 -0700 | [diff] [blame] | 10781 | // Regression test for b/155757133 |
| 10782 | TEST_P(QuicConnectionTest, DonotChangeQueuedAcks) { |
| 10783 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10784 | return; |
| 10785 | } |
| 10786 | const size_t kMinRttMs = 40; |
| 10787 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 10788 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 10789 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 10790 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 10791 | EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _, _)); |
| 10792 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 10793 | |
| 10794 | ProcessPacket(2); |
| 10795 | ProcessPacket(3); |
| 10796 | ProcessPacket(4); |
| 10797 | // Process a packet containing stream frame followed by ACK of packets 1. |
| 10798 | QuicFrames frames; |
| 10799 | frames.push_back(QuicFrame(QuicStreamFrame( |
| 10800 | QuicUtils::GetFirstBidirectionalStreamId( |
| 10801 | connection_.version().transport_version, Perspective::IS_CLIENT), |
| 10802 | false, 0u, quiche::QuicheStringPiece()))); |
| 10803 | QuicAckFrame ack_frame = InitAckFrame(1); |
| 10804 | frames.push_back(QuicFrame(&ack_frame)); |
| 10805 | // Receiving stream frame causes something to send. |
| 10806 | EXPECT_CALL(visitor_, OnStreamFrame(_)).WillOnce(Invoke([this]() { |
| 10807 | connection_.SendControlFrame(QuicFrame(new QuicWindowUpdateFrame(1, 0, 0))); |
| 10808 | // Verify now the queued ACK contains packet number 2. |
| 10809 | EXPECT_TRUE(QuicPacketCreatorPeer::QueuedFrames( |
| 10810 | QuicConnectionPeer::GetPacketCreator(&connection_))[0] |
| 10811 | .ack_frame->packets.Contains(QuicPacketNumber(2))); |
| 10812 | })); |
| 10813 | ProcessFramesPacketAtLevel(9, frames, ENCRYPTION_FORWARD_SECURE); |
| 10814 | if (GetQuicReloadableFlag(quic_donot_change_queued_ack)) { |
| 10815 | EXPECT_TRUE(writer_->ack_frames()[0].packets.Contains(QuicPacketNumber(2))); |
| 10816 | } else { |
| 10817 | // ACK frame changes mid packet serialiation! |
| 10818 | EXPECT_FALSE( |
| 10819 | writer_->ack_frames()[0].packets.Contains(QuicPacketNumber(2))); |
| 10820 | } |
| 10821 | } |
| 10822 | |
fayang | e930400 | 2020-05-07 11:57:48 -0700 | [diff] [blame] | 10823 | TEST_P(QuicConnectionTest, DonotExtendIdleTimeOnUndecryptablePackets) { |
| 10824 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10825 | QuicConfig config; |
| 10826 | connection_.SetFromConfig(config); |
| 10827 | // Subtract a second from the idle timeout on the client side. |
| 10828 | QuicTime initial_deadline = |
| 10829 | clock_.ApproximateNow() + |
| 10830 | QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); |
| 10831 | EXPECT_EQ(initial_deadline, connection_.GetTimeoutAlarm()->deadline()); |
| 10832 | |
| 10833 | // Received an undecryptable packet. |
| 10834 | clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 10835 | const uint8_t tag = 0x07; |
| 10836 | peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 10837 | std::make_unique<TaggingEncrypter>(tag)); |
| 10838 | ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_FORWARD_SECURE); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 10839 | if (GetQuicReloadableFlag(quic_extend_idle_time_on_decryptable_packets)) { |
fayang | e930400 | 2020-05-07 11:57:48 -0700 | [diff] [blame] | 10840 | // Verify deadline does not get extended. |
| 10841 | EXPECT_EQ(initial_deadline, connection_.GetTimeoutAlarm()->deadline()); |
| 10842 | } |
| 10843 | if (GetQuicReloadableFlag(quic_extend_idle_time_on_decryptable_packets)) { |
| 10844 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(1); |
| 10845 | } else { |
| 10846 | EXPECT_CALL(visitor_, OnConnectionClosed(_, _)).Times(0); |
| 10847 | } |
| 10848 | QuicTime::Delta delay = initial_deadline - clock_.ApproximateNow(); |
| 10849 | clock_.AdvanceTime(delay); |
fayang | 2205d95 | 2020-05-12 13:45:56 -0700 | [diff] [blame] | 10850 | if (GetQuicReloadableFlag(quic_extend_idle_time_on_decryptable_packets)) { |
fayang | e930400 | 2020-05-07 11:57:48 -0700 | [diff] [blame] | 10851 | connection_.GetTimeoutAlarm()->Fire(); |
| 10852 | } |
| 10853 | if (GetQuicReloadableFlag(quic_extend_idle_time_on_decryptable_packets)) { |
| 10854 | // Verify connection gets closed. |
| 10855 | EXPECT_FALSE(connection_.connected()); |
| 10856 | } else { |
| 10857 | // Verify the timeout alarm deadline is updated. |
| 10858 | EXPECT_TRUE(connection_.connected()); |
| 10859 | EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); |
| 10860 | } |
| 10861 | } |
| 10862 | |
fayang | 9f430a5 | 2020-05-08 07:28:33 -0700 | [diff] [blame] | 10863 | TEST_P(QuicConnectionTest, BundleAckWithImmediateResponse) { |
| 10864 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 10865 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 10866 | |
| 10867 | EXPECT_CALL(visitor_, OnStreamFrame(_)).WillOnce(Invoke([this]() { |
| 10868 | connection_.SendControlFrame(QuicFrame(new QuicWindowUpdateFrame(1, 0, 0))); |
| 10869 | })); |
| 10870 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 10871 | ProcessDataPacket(1); |
fayang | 9f430a5 | 2020-05-08 07:28:33 -0700 | [diff] [blame] | 10872 | if (GetQuicReloadableFlag(quic_advance_ack_timeout_update)) { |
| 10873 | // Verify ACK is bundled with WINDOW_UPDATE. |
| 10874 | EXPECT_FALSE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 10875 | EXPECT_FALSE(connection_.HasPendingAcks()); |
fayang | 9f430a5 | 2020-05-08 07:28:33 -0700 | [diff] [blame] | 10876 | } else { |
| 10877 | // ACK is pending. |
| 10878 | EXPECT_TRUE(writer_->ack_frames().empty()); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 10879 | EXPECT_TRUE(connection_.HasPendingAcks()); |
fayang | 9f430a5 | 2020-05-08 07:28:33 -0700 | [diff] [blame] | 10880 | } |
| 10881 | } |
| 10882 | |
fayang | f44a667 | 2020-05-27 12:51:45 -0700 | [diff] [blame] | 10883 | TEST_P(QuicConnectionTest, AckAlarmFiresEarly) { |
| 10884 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 10885 | return; |
| 10886 | } |
| 10887 | EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); |
| 10888 | if (QuicVersionUsesCryptoFrames(connection_.transport_version())) { |
| 10889 | EXPECT_CALL(visitor_, OnCryptoFrame(_)).Times(AnyNumber()); |
| 10890 | } |
| 10891 | EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); |
| 10892 | use_tagging_decrypter(); |
| 10893 | // Receives packet 1000 in initial data. |
| 10894 | ProcessCryptoPacketAtLevel(1000, ENCRYPTION_INITIAL); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 10895 | EXPECT_TRUE(connection_.HasPendingAcks()); |
fayang | f44a667 | 2020-05-27 12:51:45 -0700 | [diff] [blame] | 10896 | |
| 10897 | peer_framer_.SetEncrypter(ENCRYPTION_ZERO_RTT, |
| 10898 | std::make_unique<TaggingEncrypter>(0x02)); |
| 10899 | SetDecrypter(ENCRYPTION_ZERO_RTT, |
| 10900 | std::make_unique<StrictTaggingDecrypter>(0x02)); |
| 10901 | connection_.SetEncrypter(ENCRYPTION_INITIAL, |
| 10902 | std::make_unique<TaggingEncrypter>(0x02)); |
| 10903 | // Receives packet 1000 in application data. |
| 10904 | ProcessDataPacketAtLevel(1000, false, ENCRYPTION_ZERO_RTT); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 10905 | EXPECT_TRUE(connection_.HasPendingAcks()); |
fayang | f44a667 | 2020-05-27 12:51:45 -0700 | [diff] [blame] | 10906 | // Verify ACK deadline does not change. |
| 10907 | EXPECT_EQ(clock_.ApproximateNow() + kAlarmGranularity, |
| 10908 | connection_.GetAckAlarm()->deadline()); |
| 10909 | |
| 10910 | // Ack alarm fires early. |
| 10911 | if (GetQuicReloadableFlag(quic_always_send_earliest_ack)) { |
| 10912 | // Verify the earliest ACK is flushed. |
| 10913 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); |
| 10914 | } else { |
| 10915 | EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); |
| 10916 | } |
| 10917 | connection_.GetAckAlarm()->Fire(); |
fayang | 9adfb53 | 2020-06-04 06:58:45 -0700 | [diff] [blame] | 10918 | EXPECT_TRUE(connection_.HasPendingAcks()); |
fayang | f44a667 | 2020-05-27 12:51:45 -0700 | [diff] [blame] | 10919 | if (GetQuicReloadableFlag(quic_always_send_earliest_ack)) { |
| 10920 | EXPECT_EQ(clock_.ApproximateNow() + DefaultDelayedAckTime(), |
| 10921 | connection_.GetAckAlarm()->deadline()); |
| 10922 | } else { |
| 10923 | // No forward progress has been made. |
| 10924 | EXPECT_EQ(clock_.ApproximateNow() + kAlarmGranularity, |
| 10925 | connection_.GetAckAlarm()->deadline()); |
| 10926 | } |
| 10927 | } |
| 10928 | |
fayang | f78b693 | 2020-06-08 08:36:45 -0700 | [diff] [blame] | 10929 | TEST_P(QuicConnectionTest, ClientOnlyBlackholeDetectionClient) { |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 10930 | if (!GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
fayang | f78b693 | 2020-06-08 08:36:45 -0700 | [diff] [blame] | 10931 | return; |
| 10932 | } |
| 10933 | QuicConfig config; |
| 10934 | QuicTagVector connection_options; |
| 10935 | connection_options.push_back(kCBHD); |
| 10936 | config.SetConnectionOptionsToSend(connection_options); |
| 10937 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10938 | connection_.SetFromConfig(config); |
| 10939 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 10940 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 10941 | EXPECT_FALSE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 10942 | // Send stream data. |
| 10943 | SendStreamDataToPeer( |
| 10944 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 10945 | 0, FIN, nullptr); |
| 10946 | // Verify blackhole detection is in progress. |
| 10947 | EXPECT_TRUE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 10948 | } |
| 10949 | |
| 10950 | TEST_P(QuicConnectionTest, ClientOnlyBlackholeDetectionServer) { |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 10951 | if (!GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
fayang | f78b693 | 2020-06-08 08:36:45 -0700 | [diff] [blame] | 10952 | return; |
| 10953 | } |
| 10954 | set_perspective(Perspective::IS_SERVER); |
| 10955 | QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); |
| 10956 | if (version().SupportsAntiAmplificationLimit()) { |
| 10957 | QuicConnectionPeer::SetAddressValidated(&connection_); |
| 10958 | } |
| 10959 | QuicConfig config; |
| 10960 | QuicTagVector connection_options; |
| 10961 | connection_options.push_back(kCBHD); |
| 10962 | config.SetInitialReceivedConnectionOptions(connection_options); |
| 10963 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10964 | connection_.SetFromConfig(config); |
| 10965 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 10966 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 10967 | EXPECT_FALSE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 10968 | // Send stream data. |
| 10969 | SendStreamDataToPeer( |
| 10970 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 10971 | 0, FIN, nullptr); |
| 10972 | // Verify blackhole detection is disabled. |
| 10973 | EXPECT_FALSE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 10974 | } |
| 10975 | |
| 10976 | TEST_P(QuicConnectionTest, 2RtoBlackholeDetection) { |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 10977 | if (!GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
fayang | f78b693 | 2020-06-08 08:36:45 -0700 | [diff] [blame] | 10978 | return; |
| 10979 | } |
| 10980 | QuicConfig config; |
| 10981 | QuicTagVector connection_options; |
| 10982 | connection_options.push_back(k2RTO); |
| 10983 | config.SetConnectionOptionsToSend(connection_options); |
| 10984 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 10985 | connection_.SetFromConfig(config); |
| 10986 | const size_t kMinRttMs = 40; |
| 10987 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 10988 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 10989 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 10990 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 10991 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 10992 | EXPECT_FALSE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 10993 | // Send stream data. |
| 10994 | SendStreamDataToPeer( |
| 10995 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 10996 | 0, FIN, nullptr); |
| 10997 | // Verify blackhole delay is expected. |
| 10998 | EXPECT_EQ(clock_.Now() + |
| 10999 | connection_.sent_packet_manager().GetNetworkBlackholeDelay(2), |
| 11000 | QuicConnectionPeer::GetBlackholeDetectionDeadline(&connection_)); |
| 11001 | } |
| 11002 | |
| 11003 | TEST_P(QuicConnectionTest, 3RtoBlackholeDetection) { |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 11004 | if (!GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
fayang | f78b693 | 2020-06-08 08:36:45 -0700 | [diff] [blame] | 11005 | return; |
| 11006 | } |
| 11007 | QuicConfig config; |
| 11008 | QuicTagVector connection_options; |
| 11009 | connection_options.push_back(k3RTO); |
| 11010 | config.SetConnectionOptionsToSend(connection_options); |
| 11011 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 11012 | connection_.SetFromConfig(config); |
| 11013 | const size_t kMinRttMs = 40; |
| 11014 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 11015 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 11016 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 11017 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 11018 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 11019 | EXPECT_FALSE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 11020 | // Send stream data. |
| 11021 | SendStreamDataToPeer( |
| 11022 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 11023 | 0, FIN, nullptr); |
| 11024 | // Verify blackhole delay is expected. |
| 11025 | EXPECT_EQ(clock_.Now() + |
| 11026 | connection_.sent_packet_manager().GetNetworkBlackholeDelay(3), |
| 11027 | QuicConnectionPeer::GetBlackholeDetectionDeadline(&connection_)); |
| 11028 | } |
| 11029 | |
| 11030 | TEST_P(QuicConnectionTest, 4RtoBlackholeDetection) { |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 11031 | if (!GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
fayang | f78b693 | 2020-06-08 08:36:45 -0700 | [diff] [blame] | 11032 | return; |
| 11033 | } |
| 11034 | QuicConfig config; |
| 11035 | QuicTagVector connection_options; |
| 11036 | connection_options.push_back(k4RTO); |
| 11037 | config.SetConnectionOptionsToSend(connection_options); |
| 11038 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 11039 | connection_.SetFromConfig(config); |
| 11040 | const size_t kMinRttMs = 40; |
| 11041 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 11042 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 11043 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 11044 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 11045 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 11046 | EXPECT_FALSE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 11047 | // Send stream data. |
| 11048 | SendStreamDataToPeer( |
| 11049 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 11050 | 0, FIN, nullptr); |
| 11051 | // Verify blackhole delay is expected. |
| 11052 | EXPECT_EQ(clock_.Now() + |
| 11053 | connection_.sent_packet_manager().GetNetworkBlackholeDelay(4), |
| 11054 | QuicConnectionPeer::GetBlackholeDetectionDeadline(&connection_)); |
| 11055 | } |
| 11056 | |
| 11057 | TEST_P(QuicConnectionTest, 6RtoBlackholeDetection) { |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 11058 | if (!GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
fayang | f78b693 | 2020-06-08 08:36:45 -0700 | [diff] [blame] | 11059 | return; |
| 11060 | } |
| 11061 | QuicConfig config; |
| 11062 | QuicTagVector connection_options; |
| 11063 | connection_options.push_back(k6RTO); |
| 11064 | config.SetConnectionOptionsToSend(connection_options); |
| 11065 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 11066 | connection_.SetFromConfig(config); |
| 11067 | const size_t kMinRttMs = 40; |
| 11068 | RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); |
| 11069 | rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), |
| 11070 | QuicTime::Delta::Zero(), QuicTime::Zero()); |
| 11071 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 11072 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 11073 | EXPECT_FALSE(connection_.GetBlackholeDetectorAlarm()->IsSet()); |
| 11074 | // Send stream data. |
| 11075 | SendStreamDataToPeer( |
| 11076 | GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo", |
| 11077 | 0, FIN, nullptr); |
| 11078 | // Verify blackhole delay is expected. |
| 11079 | EXPECT_EQ(clock_.Now() + |
| 11080 | connection_.sent_packet_manager().GetNetworkBlackholeDelay(6), |
| 11081 | QuicConnectionPeer::GetBlackholeDetectionDeadline(&connection_)); |
| 11082 | } |
| 11083 | |
fayang | 656cbb5 | 2020-06-09 13:29:35 -0700 | [diff] [blame] | 11084 | // Regresstion test for b/158491591. |
| 11085 | TEST_P(QuicConnectionTest, MadeForwardProgressOnDiscardingKeys) { |
| 11086 | if (!connection_.SupportsMultiplePacketNumberSpaces()) { |
| 11087 | return; |
| 11088 | } |
| 11089 | use_tagging_decrypter(); |
| 11090 | // Send handshake packet. |
| 11091 | connection_.SetEncrypter(ENCRYPTION_HANDSHAKE, |
| 11092 | std::make_unique<TaggingEncrypter>(0x02)); |
| 11093 | connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE); |
| 11094 | EXPECT_CALL(visitor_, OnHandshakePacketSent()).Times(1); |
| 11095 | QuicConfig config; |
| 11096 | QuicTagVector connection_options; |
| 11097 | connection_options.push_back(k5RTO); |
| 11098 | config.SetConnectionOptionsToSend(connection_options); |
| 11099 | QuicConfigPeer::SetNegotiated(&config, true); |
| 11100 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
| 11101 | EXPECT_CALL(visitor_, GetHandshakeState()) |
| 11102 | .WillRepeatedly(Return(HANDSHAKE_COMPLETE)); |
| 11103 | } |
| 11104 | if (connection_.version().AuthenticatesHandshakeConnectionIds()) { |
| 11105 | QuicConfigPeer::SetReceivedOriginalConnectionId( |
| 11106 | &config, connection_.connection_id()); |
| 11107 | QuicConfigPeer::SetReceivedInitialSourceConnectionId( |
| 11108 | &config, connection_.connection_id()); |
| 11109 | } |
| 11110 | EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); |
| 11111 | connection_.SetFromConfig(config); |
| 11112 | |
| 11113 | connection_.SendCryptoDataWithString("foo", 0, ENCRYPTION_HANDSHAKE); |
| 11114 | EXPECT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 11115 | // Discard handshake keys. |
| 11116 | connection_.OnHandshakeComplete(); |
| 11117 | if (GetQuicReloadableFlag(quic_default_enable_5rto_blackhole_detection2)) { |
| 11118 | // Verify blackhole detection stops. |
| 11119 | EXPECT_FALSE(connection_.BlackholeDetectionInProgress()); |
| 11120 | } else { |
| 11121 | // Problematic: although there is nothing in flight, blackhole detection is |
| 11122 | // still in progress. |
| 11123 | EXPECT_TRUE(connection_.BlackholeDetectionInProgress()); |
| 11124 | } |
| 11125 | } |
| 11126 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11127 | } // namespace |
| 11128 | } // namespace test |
| 11129 | } // namespace quic |